Refactor the negotiate() and connected functions
[nbd.git] / cliserv.h
blobb8e797860f3481d6c33084b4f947582d5c4a63f2
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 <netinet/in.h>
17 #include <stdlib.h>
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
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
39 #define __be32 u32
40 #define __be64 u64
41 #include "nbd.h"
43 #ifndef HAVE_FDATASYNC
44 #define fdatasync(arg) fsync(arg)
45 #endif
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
51 #ifndef _LARGEFILE_SOURCE
52 #define _LARGEFILE_SOURCE
53 #endif
54 #define _FILE_OFFSET_BITS 64
55 #endif
57 #ifndef G_GNUC_NORETURN
58 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
59 #define G_GNUC_NORETURN __attribute__((__noreturn__))
60 #define G_GNUC_UNUSED __attribute__((unused))
61 #else
62 #define G_GNUC_NORETURN
63 #define G_GNUC_UNUSED
64 #endif
65 #endif
67 extern const u64 cliserv_magic;
68 extern const u64 opts_magic;
69 extern const u64 rep_magic;
71 #define INIT_PASSWD "NBDMAGIC"
73 #define INFO(a) do { } while(0)
75 int set_nonblocking(int fd, int nb);
76 void setmysockopt(int sock);
77 void err_nonfatal(const char *s);
79 void nbd_err(const char *s) G_GNUC_NORETURN;
80 #define err(S) nbd_err(S)
82 void logging(const char* name);
84 #ifndef ntohll
85 uint64_t ntohll(uint64_t a);
86 #endif
87 #ifndef htonll
88 #define htonll ntohll
89 #endif
91 int readit(int f, void *buf, size_t len);
92 int writeit(int f, const void *buf, size_t len);
94 #define NBD_DEFAULT_PORT "10809" /* Port on which named exports are
95 * served */
97 /* Options that the client can select to the server */
98 #define NBD_OPT_EXPORT_NAME (1) /**< Client wants to select a named export (is followed by name of export) */
99 #define NBD_OPT_ABORT (2) /**< Client wishes to abort negotiation */
100 #define NBD_OPT_LIST (3) /**< Client request list of supported exports (not followed by data) */
101 #define NBD_OPT_STARTTLS (5) /**< Client wishes to initiate TLS */
102 #define NBD_OPT_INFO (6) /**< Client wants information about the given export */
103 #define NBD_OPT_GO (7) /**< Client wants to select the given and move to the transmission phase */
104 #define NBD_OPT_STRUCTURED_REPLY (8) /**< Client wants to see structured replies */
106 /* Replies the server can send during negotiation */
107 #define NBD_REP_ACK (1) /**< ACK a request. Data: option number to be acked */
108 #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 */
109 #define NBD_REP_INFO (3) /**< Reply to NBD_OPT_INFO */
110 #define NBD_REP_FLAG_ERROR (1 << 31) /** If the high bit is set, the reply is an error */
111 #define NBD_REP_ERR_UNSUP (1 | NBD_REP_FLAG_ERROR) /**< Client requested an option not understood by this version of the server */
112 #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) */
113 #define NBD_REP_ERR_INVALID (3 | NBD_REP_FLAG_ERROR) /**< Client issued an invalid request */
114 #define NBD_REP_ERR_PLATFORM (4 | NBD_REP_FLAG_ERROR) /**< Option not supported on this platform */
115 #define NBD_REP_ERR_TLS_REQD (5 | NBD_REP_FLAG_ERROR) /**< TLS required */
116 #define NBD_REP_ERR_UNKNOWN (6 | NBD_REP_FLAG_ERROR) /**< NBD_OPT_INFO or ..._GO requested on unknown export */
117 #define NBD_REP_ERR_BLOCK_SIZE_REQD (8 | NBD_REP_FLAG_ERROR) /**< Server is not willing to serve the export without the block size being negotiated */
119 /* Global flags */
120 #define NBD_FLAG_FIXED_NEWSTYLE (1 << 0) /**< new-style export that actually supports extending */
121 #define NBD_FLAG_NO_ZEROES (1 << 1) /**< we won't send the 128 bits of zeroes if the client sends NBD_FLAG_C_NO_ZEROES */
122 /* Flags from client to server. */
123 #define NBD_FLAG_C_FIXED_NEWSTYLE NBD_FLAG_FIXED_NEWSTYLE
124 #define NBD_FLAG_C_NO_ZEROES NBD_FLAG_NO_ZEROES
126 /* Info types */
127 #define NBD_INFO_EXPORT (0)
128 #define NBD_INFO_NAME (1)
129 #define NBD_INFO_DESCRIPTION (2)
130 #define NBD_INFO_BLOCK_SIZE (3)