Back to index

nbd  3.2
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 <netinet/in.h>
00017 #include <stdlib.h>
00018 
00019 #if SIZEOF_UNSIGNED_SHORT_INT==4
00020 typedef unsigned short u32;
00021 #elif SIZEOF_UNSIGNED_INT==4
00022 typedef unsigned int u32;
00023 #elif SIZEOF_UNSIGNED_LONG_INT==4
00024 typedef unsigned long u32;
00025 #else
00026 #error I need at least some 32-bit type
00027 #endif
00028 
00029 #if SIZEOF_UNSIGNED_INT==8
00030 typedef unsigned int u64;
00031 #elif SIZEOF_UNSIGNED_LONG_INT==8
00032 typedef unsigned long u64;
00033 #elif SIZEOF_UNSIGNED_LONG_LONG_INT==8
00034 typedef unsigned long long u64;
00035 #else
00036 #error I need at least some 64-bit type
00037 #endif
00038 
00039 #define __be32 u32
00040 #define __be64 u64
00041 #include "nbd.h"
00042 
00043 #ifndef HAVE_FDATASYNC
00044 #define fdatasync(arg) fsync(arg)
00045 #endif
00046 
00047 #if NBD_LFS==1
00048 /* /usr/include/features.h (included from /usr/include/sys/types.h)
00049    defines this when _GNU_SOURCE is defined
00050  */
00051 #ifndef _LARGEFILE_SOURCE
00052 #define _LARGEFILE_SOURCE
00053 #endif
00054 #define _FILE_OFFSET_BITS 64
00055 #endif
00056 
00057 u64 cliserv_magic = 0x00420281861253LL;
00058 u64 opts_magic = 0x49484156454F5054LL;
00059 u64 rep_magic = 0x3e889045565a9LL;
00060 #define INIT_PASSWD "NBDMAGIC"
00061 
00062 #define INFO(a) do { } while(0)
00063 
00064 void setmysockopt(int sock) {
00065        int size = 1;
00066 #if 0
00067        if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
00068                INFO("(no sockopt/1: %m)");
00069 #endif
00070 #ifdef IPPROTO_TCP
00071        size = 1;
00072        if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
00073                INFO("(no sockopt/2: %m)");
00074 #endif
00075 #if 0
00076        size = 1024;
00077        if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
00078                INFO("(no sockopt/3: %m)");
00079 #endif
00080 }
00081 
00082 #ifndef G_GNUC_NORETURN
00083 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
00084 #define G_GNUC_NORETURN __attribute__((__noreturn__))
00085 #define G_GNUC_UNUSED __attribute__((unused))
00086 #else
00087 #define G_GNUC_NORETURN
00088 #define G_GNUC_UNUSED
00089 #endif
00090 #endif
00091 
00092 void err_nonfatal(const char *s) {
00093        char s1[150], *s2;
00094 
00095        strncpy(s1, s, sizeof(s1));
00096        if ((s2 = strstr(s, "%m"))) {
00097               strcpy(s1 + (s2 - s), strerror(errno));
00098               s2 += 2;
00099               strcpy(s1 + strlen(s1), s2);
00100        }
00101 #ifndef       sun
00102        /* Solaris doesn't have %h in syslog */
00103        else if ((s2 = strstr(s, "%h"))) {
00104               strcpy(s1 + (s2 - s), hstrerror(h_errno));
00105               s2 += 2;
00106               strcpy(s1 + strlen(s1), s2);
00107        }
00108 #endif
00109 
00110        s1[sizeof(s1)-1] = '\0';
00111 #ifdef ISSERVER
00112        syslog(LOG_ERR, "%s", s1);
00113        syslog(LOG_ERR, "Exiting.");
00114 #endif
00115        fprintf(stderr, "Error: %s\nExiting.\n", s1);
00116 }
00117 
00118 void err(const char *s) G_GNUC_NORETURN;
00119 
00120 void err(const char *s) {
00121        err_nonfatal(s);
00122        exit(EXIT_FAILURE);
00123 }
00124 
00125 void logging(void) {
00126 #ifdef ISSERVER
00127        openlog(MY_NAME, LOG_PID, LOG_DAEMON);
00128 #endif
00129        setvbuf(stdout, NULL, _IONBF, 0);
00130        setvbuf(stderr, NULL, _IONBF, 0);
00131 }
00132 
00133 #ifdef WORDS_BIGENDIAN
00134 u64 ntohll(u64 a) {
00135        return a;
00136 }
00137 #else
00138 u64 ntohll(u64 a) {
00139        u32 lo = a & 0xffffffff;
00140        u32 hi = a >> 32U;
00141        lo = ntohl(lo);
00142        hi = ntohl(hi);
00143        return ((u64) lo) << 32U | hi;
00144 }
00145 #endif
00146 #define htonll ntohll
00147 
00148 #define NBD_DEFAULT_PORT    "10809"       /* Port on which named exports are
00149                                     * served */
00150 
00151 /* Options that the client can select to the server */
00152 #define NBD_OPT_EXPORT_NAME (1)    
00153 #define NBD_OPT_ABORT              (2)    
00154 #define NBD_OPT_LIST        (3)    
00156 /* Replies the server can send during negotiation */
00157 #define NBD_REP_ACK         (1)    
00158 #define NBD_REP_SERVER             (2)    
00159 #define NBD_REP_FLAG_ERROR  (1 << 31)     
00160 #define NBD_REP_ERR_UNSUP   (1 | NBD_REP_FLAG_ERROR)    
00161 #define NBD_REP_ERR_POLICY  (2 | NBD_REP_FLAG_ERROR)    
00162 #define NBD_REP_ERR_INVALID (3 | NBD_REP_FLAG_ERROR)    
00163 #define NBD_REP_ERR_PLATFORM       (4 | NBD_REP_FLAG_ERROR)    
00165 /* Global flags */
00166 #define NBD_FLAG_FIXED_NEWSTYLE (1 << 0)  /* new-style export that actually supports extending */
00167 /* Flags from client to server. Only one such option currently. */
00168 #define NBD_FLAG_C_FIXED_NEWSTYLE NBD_FLAG_FIXED_NEWSTYLE