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
39 /* 2.6.18 and above use __be* rather than u* */
45 #include <linux/types.h>
46 #include <linux/nbd.h>
50 #define _LARGEFILE_SOURCE
51 #define _FILE_OFFSET_BITS 64
54 u64 cliserv_magic = 0x00420281861253LL;
55 #define INIT_PASSWD "NBDMAGIC"
57 #define INFO(a) do { } while(0)
59 void setmysockopt(int sock) {
62 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
63 INFO("(no sockopt/1: %m)");
67 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
68 INFO("(no sockopt/2: %m)");
72 if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
73 INFO("(no sockopt/3: %m)");
77 #ifndef G_GNUC_NORETURN
78 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
79 #define G_GNUC_NORETURN __attribute__((__noreturn__))
81 #define G_GNUC_NORETURN
85 void err(const char *s) G_GNUC_NORETURN;
87 void err(const char *s) {
88 const int maxlen = 150;
91 strncpy(s1, s, maxlen);
92 if ((s2 = strstr(s, "%m"))) {
93 strcpy(s1 + (s2 - s), strerror(errno));
95 strcpy(s1 + strlen(s1), s2);
98 /* Solaris doesn't have %h in syslog */
99 else if ((s2 = strstr(s, "%h"))) {
100 strcpy(s1 + (s2 - s), hstrerror(h_errno));
102 strcpy(s1 + strlen(s1), s2);
108 syslog(LOG_ERR, "%s", s1);
110 fprintf(stderr, "Error: %s\n", s1);
116 openlog(MY_NAME, LOG_PID, LOG_DAEMON);
118 setvbuf(stdout, NULL, _IONBF, 0);
119 setvbuf(stderr, NULL, _IONBF, 0);
122 #ifdef WORDS_BIGENDIAN
128 u32 lo = a & 0xffffffff;
132 return ((u64) lo) << 32U | hi;
135 #define htonll ntohll
137 /* Flags used between the client and server */
138 #define NBD_FLAG_HAS_FLAGS (1 << 0) /* Flags are there */
139 #define NBD_FLAG_READ_ONLY (1 << 1) /* Device is read-only */