Changelog updates
[nbd.git] / cliserv.h
blob51c8bd1c883d427659bb2aa0c1525fab8ba336b8
1 /* This header file is shared by client & server. They really have
2 * something to share...
3 * */
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)
12 #include <errno.h>
13 #include <string.h>
14 #include <netdb.h>
15 #include <netinet/tcp.h>
16 #include <stdlib.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;
24 #else
25 #error I need at least some 32-bit type
26 #endif
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;
34 #else
35 #error I need at least some 64-bit type
36 #endif
38 #define __be32 u32
39 #define __be64 u64
40 #include "nbd.h"
42 #if NBD_LFS==1
43 #define _LARGEFILE_SOURCE
44 #define _FILE_OFFSET_BITS 64
45 #endif
47 u64 cliserv_magic = 0x00420281861253LL;
48 u64 opts_magic = 0x49484156454F5054LL;
49 #define INIT_PASSWD "NBDMAGIC"
51 #define INFO(a) do { } while(0)
53 void setmysockopt(int sock) {
54 int size = 1;
55 #if 0
56 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
57 INFO("(no sockopt/1: %m)");
58 #endif
59 #ifdef IPPROTO_TCP
60 size = 1;
61 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
62 INFO("(no sockopt/2: %m)");
63 #endif
64 #if 0
65 size = 1024;
66 if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
67 INFO("(no sockopt/3: %m)");
68 #endif
71 #ifndef G_GNUC_NORETURN
72 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
73 #define G_GNUC_NORETURN __attribute__((__noreturn__))
74 #else
75 #define G_GNUC_NORETURN
76 #endif
77 #endif
79 void err_nonfatal(const char *s) {
80 char s1[150], *s2;
82 strncpy(s1, s, sizeof(s1));
83 if ((s2 = strstr(s, "%m"))) {
84 strcpy(s1 + (s2 - s), strerror(errno));
85 s2 += 2;
86 strcpy(s1 + strlen(s1), s2);
88 #ifndef sun
89 /* Solaris doesn't have %h in syslog */
90 else if ((s2 = strstr(s, "%h"))) {
91 strcpy(s1 + (s2 - s), hstrerror(h_errno));
92 s2 += 2;
93 strcpy(s1 + strlen(s1), s2);
95 #endif
97 s1[sizeof(s1)-1] = '\0';
98 #ifdef ISSERVER
99 syslog(LOG_ERR, "%s", s1);
100 syslog(LOG_ERR, "Exiting.");
101 #endif
102 fprintf(stderr, "Error: %s\nExiting.\n", s1);
105 void err(const char *s) G_GNUC_NORETURN;
107 void err(const char *s) {
108 err_nonfatal(s);
109 exit(EXIT_FAILURE);
112 void logging(void) {
113 #ifdef ISSERVER
114 openlog(MY_NAME, LOG_PID, LOG_DAEMON);
115 #endif
116 setvbuf(stdout, NULL, _IONBF, 0);
117 setvbuf(stderr, NULL, _IONBF, 0);
120 #ifdef WORDS_BIGENDIAN
121 u64 ntohll(u64 a) {
122 return a;
124 #else
125 u64 ntohll(u64 a) {
126 u32 lo = a & 0xffffffff;
127 u32 hi = a >> 32U;
128 lo = ntohl(lo);
129 hi = ntohl(hi);
130 return ((u64) lo) << 32U | hi;
132 #endif
133 #define htonll ntohll
135 /* Flags used between the client and server */
136 #define NBD_FLAG_HAS_FLAGS (1 << 0) /* Flags are there */
137 #define NBD_FLAG_READ_ONLY (1 << 1) /* Device is read-only */
139 #define NBD_DEFAULT_PORT "10809" /* Port on which named exports are
140 * served */
142 /* Options that the client can select to the server */
143 #define NBD_OPT_EXPORT_NAME (1 << 0) /* Client wants to select a named export (is followed by length and name of export) */