Network Block Device 2.9.20

cliserv.h

Go to the documentation of this file.
00001 /* This header file is shared by client & server. They really have
00002  * something to share...
00003  * */
00004 
00005 /* Client/server protocol is as follows:
00006    Send INIT_PASSWD
00007    Send 64-bit cliserv_magic
00008    Send 64-bit size of exported device
00009    Send 128 bytes of zeros (reserved for future use)
00010  */
00011 
00012 #include <errno.h>
00013 #include <string.h>
00014 #include <netdb.h>
00015 #include <netinet/tcp.h>
00016 #include <stdlib.h>
00017 
00018 #if SIZEOF_UNSIGNED_SHORT_INT==4
00019 typedef unsigned short u32;
00020 #elif SIZEOF_UNSIGNED_INT==4
00021 typedef unsigned int u32;
00022 #elif SIZEOF_UNSIGNED_LONG_INT==4
00023 typedef unsigned long u32;
00024 #else
00025 #error I need at least some 32-bit type
00026 #endif
00027 
00028 #if SIZEOF_UNSIGNED_INT==8
00029 typedef unsigned int u64;
00030 #elif SIZEOF_UNSIGNED_LONG_INT==8
00031 typedef unsigned long u64;
00032 #elif SIZEOF_UNSIGNED_LONG_LONG_INT==8
00033 typedef unsigned long long u64;
00034 #else
00035 #error I need at least some 64-bit type
00036 #endif
00037 
00038 #define __be32 u32
00039 #define __be64 u64
00040 #include "nbd.h"
00041 
00042 #if NBD_LFS==1
00043 #define _LARGEFILE_SOURCE
00044 #define _FILE_OFFSET_BITS 64
00045 #endif
00046 
00047 u64 cliserv_magic = 0x00420281861253LL;
00048 u64 opts_magic = 0x49484156454F5054LL;
00049 #define INIT_PASSWD "NBDMAGIC"
00050 
00051 #define INFO(a) do { } while(0)
00052 
00053 void setmysockopt(int sock) {
00054         int size = 1;
00055 #if 0
00056         if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
00057                  INFO("(no sockopt/1: %m)");
00058 #endif
00059 #ifdef  IPPROTO_TCP
00060         size = 1;
00061         if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
00062                  INFO("(no sockopt/2: %m)");
00063 #endif
00064 #if 0
00065         size = 1024;
00066         if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
00067                  INFO("(no sockopt/3: %m)");
00068 #endif
00069 }
00070 
00071 #ifndef G_GNUC_NORETURN
00072 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
00073 #define G_GNUC_NORETURN __attribute__((__noreturn__))
00074 #else
00075 #define G_GNUC_NORETURN
00076 #endif
00077 #endif
00078 
00079 void err_nonfatal(const char *s) {
00080         char s1[150], *s2;
00081 
00082         strncpy(s1, s, sizeof(s1));
00083         if ((s2 = strstr(s, "%m"))) {
00084                 strcpy(s1 + (s2 - s), strerror(errno));
00085                 s2 += 2;
00086                 strcpy(s1 + strlen(s1), s2);
00087         }
00088 #ifndef sun
00089         /* Solaris doesn't have %h in syslog */
00090         else if ((s2 = strstr(s, "%h"))) {
00091                 strcpy(s1 + (s2 - s), hstrerror(h_errno));
00092                 s2 += 2;
00093                 strcpy(s1 + strlen(s1), s2);
00094         }
00095 #endif
00096 
00097         s1[sizeof(s1)-1] = '\0';
00098 #ifdef ISSERVER
00099         syslog(LOG_ERR, "%s", s1);
00100         syslog(LOG_ERR, "Exiting.");
00101 #endif
00102         fprintf(stderr, "Error: %s\nExiting.\n", s1);
00103 }
00104 
00105 void err(const char *s) G_GNUC_NORETURN;
00106 
00107 void err(const char *s) {
00108         err_nonfatal(s);
00109         exit(EXIT_FAILURE);
00110 }
00111 
00112 void logging(void) {
00113 #ifdef ISSERVER
00114         openlog(MY_NAME, LOG_PID, LOG_DAEMON);
00115 #endif
00116         setvbuf(stdout, NULL, _IONBF, 0);
00117         setvbuf(stderr, NULL, _IONBF, 0);
00118 }
00119 
00120 #ifdef WORDS_BIGENDIAN
00121 u64 ntohll(u64 a) {
00122         return a;
00123 }
00124 #else
00125 u64 ntohll(u64 a) {
00126         u32 lo = a & 0xffffffff;
00127         u32 hi = a >> 32U;
00128         lo = ntohl(lo);
00129         hi = ntohl(hi);
00130         return ((u64) lo) << 32U | hi;
00131 }
00132 #endif
00133 #define htonll ntohll
00134 
00135 /* Flags used between the client and server */
00136 #define NBD_FLAG_HAS_FLAGS      (1 << 0)        /* Flags are there */
00137 #define NBD_FLAG_READ_ONLY      (1 << 1)        /* Device is read-only */
00138 
00139 #define NBD_DEFAULT_PORT        "10809" /* Port on which named exports are
00140                                          * served */
00141 
00142 /* Options that the client can select to the server */
00143 #define NBD_OPT_EXPORT_NAME     (1 << 0)        /* Client wants to select a named export (is followed by length and name of export) */