1 /* This header file is shared by client & server. They really have
2 * something to share...
5 /* Client/server protocol is as follows:
7 Send 64-bit cliserv_magic
8 Send 64-bit size of exported device
9 Send 128 bytes of zeros (reserved for future use)
15 #include <netinet/tcp.h>
18 #if SIZEOF_UNSIGNED_SHORT_INT==4
19 typedef unsigned short u32;
20 #elif SIZEOF_UNSIGNED_INT==4
21 typedef unsigned int u32;
22 #elif SIZEOF_UNSIGNED_LONG_INT==4
23 typedef unsigned long u32;
25 #error I need at least some 32-bit type
28 #if SIZEOF_UNSIGNED_INT==8
29 typedef unsigned int u64;
30 #elif SIZEOF_UNSIGNED_LONG_INT==8
31 typedef unsigned long u64;
32 #elif SIZEOF_UNSIGNED_LONG_LONG_INT==8
33 typedef unsigned long long u64;
35 #error I need at least some 64-bit type
43 /* /usr/include/features.h (included from /usr/include/sys/types.h)
44 defines this when _GNU_SOURCE is defined
46 #ifndef _LARGEFILE_SOURCE
47 #define _LARGEFILE_SOURCE
49 #define _FILE_OFFSET_BITS 64
52 u64 cliserv_magic = 0x00420281861253LL;
53 u64 opts_magic = 0x49484156454F5054LL;
54 #define INIT_PASSWD "NBDMAGIC"
56 #define INFO(a) do { } while(0)
58 void setmysockopt(int sock) {
61 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
62 INFO("(no sockopt/1: %m)");
66 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
67 INFO("(no sockopt/2: %m)");
71 if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
72 INFO("(no sockopt/3: %m)");
76 #ifndef G_GNUC_NORETURN
77 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
78 #define G_GNUC_NORETURN __attribute__((__noreturn__))
79 #define G_GNUC_UNUSED __attribute__((unused))
81 #define G_GNUC_NORETURN
86 void err_nonfatal(const char *s) {
89 strncpy(s1, s, sizeof(s1));
90 if ((s2 = strstr(s, "%m"))) {
91 strcpy(s1 + (s2 - s), strerror(errno));
93 strcpy(s1 + strlen(s1), s2);
96 /* Solaris doesn't have %h in syslog */
97 else if ((s2 = strstr(s, "%h"))) {
98 strcpy(s1 + (s2 - s), hstrerror(h_errno));
100 strcpy(s1 + strlen(s1), s2);
104 s1[sizeof(s1)-1] = '\0';
106 syslog(LOG_ERR, "%s", s1);
107 syslog(LOG_ERR, "Exiting.");
109 fprintf(stderr, "Error: %s\nExiting.\n", s1);
112 void err(const char *s) G_GNUC_NORETURN;
114 void err(const char *s) {
121 openlog(MY_NAME, LOG_PID, LOG_DAEMON);
123 setvbuf(stdout, NULL, _IONBF, 0);
124 setvbuf(stderr, NULL, _IONBF, 0);
127 #ifdef WORDS_BIGENDIAN
133 u32 lo = a & 0xffffffff;
137 return ((u64) lo) << 32U | hi;
140 #define htonll ntohll
142 #define NBD_DEFAULT_PORT "10809" /* Port on which named exports are
145 /* Options that the client can select to the server */
146 #define NBD_OPT_EXPORT_NAME (1 << 0) /* Client wants to select a named export (is followed by length and name of export) */