Network Block Device  @PACKAGE_VERSION@
cliserv.h
Go to the documentation of this file.
1 /* This header file is shared by client & server. They really have
2  * something to share...
3  * */
4 
5 /* Client/server protocol is as follows:
6  Send INIT_PASSWD
7  Send 64-bit cliserv_magic
8  Send 64-bit size of exported device
9  Send 128 bytes of zeros (reserved for future use)
10  */
11 
12 #include <errno.h>
13 #include <string.h>
14 #include <netdb.h>
15 #include <netinet/tcp.h>
16 #include <netinet/in.h>
17 #include <stdlib.h>
18 
19 #if SIZEOF_UNSIGNED_SHORT_INT==4
20 typedef unsigned short u32;
21 #elif SIZEOF_UNSIGNED_INT==4
22 typedef unsigned int u32;
23 #elif SIZEOF_UNSIGNED_LONG_INT==4
24 typedef unsigned long u32;
25 #else
26 #error I need at least some 32-bit type
27 #endif
28 
29 #if SIZEOF_UNSIGNED_INT==8
30 typedef unsigned int u64;
31 #elif SIZEOF_UNSIGNED_LONG_INT==8
32 typedef unsigned long u64;
33 #elif SIZEOF_UNSIGNED_LONG_LONG_INT==8
34 typedef unsigned long long u64;
35 #else
36 #error I need at least some 64-bit type
37 #endif
38 
39 #define __be32 u32
40 #define __be64 u64
41 #include "nbd.h"
42 
43 #ifndef HAVE_FDATASYNC
44 #define fdatasync(arg) fsync(arg)
45 #endif
46 
47 #if NBD_LFS==1
48 /* /usr/include/features.h (included from /usr/include/sys/types.h)
49  defines this when _GNU_SOURCE is defined
50  */
51 #ifndef _LARGEFILE_SOURCE
52 #define _LARGEFILE_SOURCE
53 #endif
54 #define _FILE_OFFSET_BITS 64
55 #endif
56 
57 u64 cliserv_magic = 0x00420281861253LL;
58 u64 opts_magic = 0x49484156454F5054LL;
59 u64 rep_magic = 0x3e889045565a9LL;
60 #define INIT_PASSWD "NBDMAGIC"
61 
62 #define INFO(a) do { } while(0)
63 
64 void setmysockopt(int sock) {
65  int size = 1;
66 #if 0
67  if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
68  INFO("(no sockopt/1: %m)");
69 #endif
70 #ifdef IPPROTO_TCP
71  size = 1;
72  if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
73  INFO("(no sockopt/2: %m)");
74 #endif
75 #if 0
76  size = 1024;
77  if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
78  INFO("(no sockopt/3: %m)");
79 #endif
80 }
81 
82 #ifndef G_GNUC_NORETURN
83 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
84 #define G_GNUC_NORETURN __attribute__((__noreturn__))
85 #define G_GNUC_UNUSED __attribute__((unused))
86 #else
87 #define G_GNUC_NORETURN
88 #define G_GNUC_UNUSED
89 #endif
90 #endif
91 
92 void err_nonfatal(const char *s) {
93  char s1[150], *s2;
94 
95  strncpy(s1, s, sizeof(s1));
96  if ((s2 = strstr(s, "%m"))) {
97  strcpy(s1 + (s2 - s), strerror(errno));
98  s2 += 2;
99  strcpy(s1 + strlen(s1), s2);
100  }
101 #ifndef sun
102  /* Solaris doesn't have %h in syslog */
103  else if ((s2 = strstr(s, "%h"))) {
104  strcpy(s1 + (s2 - s), hstrerror(h_errno));
105  s2 += 2;
106  strcpy(s1 + strlen(s1), s2);
107  }
108 #endif
109 
110  s1[sizeof(s1)-1] = '\0';
111 #ifdef ISSERVER
112  syslog(LOG_ERR, "%s", s1);
113  syslog(LOG_ERR, "Exiting.");
114 #endif
115  fprintf(stderr, "Error: %s\nExiting.\n", s1);
116 }
117 
118 void err(const char *s) G_GNUC_NORETURN;
119 
120 void err(const char *s) {
121  err_nonfatal(s);
122  exit(EXIT_FAILURE);
123 }
124 
125 void logging(void) {
126 #ifdef ISSERVER
127  openlog(MY_NAME, LOG_PID, LOG_DAEMON);
128 #endif
129  setvbuf(stdout, NULL, _IONBF, 0);
130  setvbuf(stderr, NULL, _IONBF, 0);
131 }
132 
133 #ifdef WORDS_BIGENDIAN
134 u64 ntohll(u64 a) {
135  return a;
136 }
137 #else
138 u64 ntohll(u64 a) {
139  u32 lo = a & 0xffffffff;
140  u32 hi = a >> 32U;
141  lo = ntohl(lo);
142  hi = ntohl(hi);
143  return ((u64) lo) << 32U | hi;
144 }
145 #endif
146 #define htonll ntohll
147 
148 #define NBD_DEFAULT_PORT "10809" /* Port on which named exports are
149  * served */
150 
151 /* Options that the client can select to the server */
152 #define NBD_OPT_EXPORT_NAME (1) /** Client wants to select a named export (is followed by name of export) */
153 #define NBD_OPT_ABORT (2) /** Client wishes to abort negotiation */
154 #define NBD_OPT_LIST (3) /** Client request list of supported exports (not followed by data) */
155 
156 /* Replies the server can send during negotiation */
157 #define NBD_REP_ACK (1) /** ACK a request. Data: option number to be acked */
158 #define NBD_REP_SERVER (2) /** Reply to NBD_OPT_LIST (one of these per server; must be followed by NBD_REP_ACK to signal the end of the list */
159 #define NBD_REP_FLAG_ERROR (1 << 31) /** If the high bit is set, the reply is an error */
160 #define NBD_REP_ERR_UNSUP (1 | NBD_REP_FLAG_ERROR) /** Client requested an option not understood by this version of the server */
161 #define NBD_REP_ERR_POLICY (2 | NBD_REP_FLAG_ERROR) /** Client requested an option not allowed by server configuration. (e.g., the option was disabled) */
162 #define NBD_REP_ERR_INVALID (3 | NBD_REP_FLAG_ERROR) /** Client issued an invalid request */
163 #define NBD_REP_ERR_PLATFORM (4 | NBD_REP_FLAG_ERROR) /** Option not supported on this platform */
164 
165 /* Global flags */
166 #define NBD_FLAG_FIXED_NEWSTYLE (1 << 0) /* new-style export that actually supports extending */
167 /* Flags from client to server. Only one such option currently. */
168 #define NBD_FLAG_C_FIXED_NEWSTYLE NBD_FLAG_FIXED_NEWSTYLE
#define MY_NAME
Definition: nbd-client.c:44
#define G_GNUC_NORETURN
Definition: cliserv.h:87
u64 ntohll(u64 a)
Definition: cliserv.h:138
void setmysockopt(int sock)
Definition: cliserv.h:64
u64 rep_magic
Definition: cliserv.h:59
void err_nonfatal(const char *s)
Definition: cliserv.h:92
u64 cliserv_magic
Definition: cliserv.h:57
#define INFO(a)
Definition: cliserv.h:62
static uint64_t size
void err(const char *s) G_GNUC_NORETURN
Definition: cliserv.h:120
u64 opts_magic
Definition: cliserv.h:58
void logging(void)
Definition: cliserv.h:125