00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <errno.h>
00013 #include <string.h>
00014 #include <netdb.h>
00015 #include <netinet/tcp.h>
00016 #include <stdlib.h>
00017
00018 #if SIZEOF_UNSIGNED_SHORT_INT==4
00019 typedef unsigned short u32;
00020 #elif SIZEOF_UNSIGNED_INT==4
00021 typedef unsigned int u32;
00022 #elif SIZEOF_UNSIGNED_LONG_INT==4
00023 typedef unsigned long u32;
00024 #else
00025 #error I need at least some 32-bit type
00026 #endif
00027
00028 #if SIZEOF_UNSIGNED_INT==8
00029 typedef unsigned int u64;
00030 #elif SIZEOF_UNSIGNED_LONG_INT==8
00031 typedef unsigned long u64;
00032 #elif SIZEOF_UNSIGNED_LONG_LONG_INT==8
00033 typedef unsigned long long u64;
00034 #else
00035 #error I need at least some 64-bit type
00036 #endif
00037
00038 #ifdef NBD_H_LOCAL
00039
00040 #define __be32 u32
00041 #define __be64 u64
00042 #include "nbd.h"
00043 #endif
00044 #ifdef NBD_H_LINUX
00045 # ifdef HAVE_LINUX_TYPES_H
00046 # include <linux/types.h>
00047 # else
00048 # define __be32 u32
00049 # define __be64 u64
00050 # endif
00051 # include <linux/nbd.h>
00052 #endif
00053
00054 #if NBD_LFS==1
00055 #define _LARGEFILE_SOURCE
00056 #define _FILE_OFFSET_BITS 64
00057 #endif
00058
00059 u64 cliserv_magic = 0x00420281861253LL;
00060 #define INIT_PASSWD "NBDMAGIC"
00061
00062 #define INFO(a) do { } while(0)
00063
00064 void setmysockopt(int sock) {
00065 int size = 1;
00066 #if 0
00067 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
00068 INFO("(no sockopt/1: %m)");
00069 #endif
00070 #ifdef IPPROTO_TCP
00071 size = 1;
00072 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
00073 INFO("(no sockopt/2: %m)");
00074 #endif
00075 #if 0
00076 size = 1024;
00077 if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
00078 INFO("(no sockopt/3: %m)");
00079 #endif
00080 }
00081
00082 #ifndef G_GNUC_NORETURN
00083 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
00084 #define G_GNUC_NORETURN __attribute__((__noreturn__))
00085 #else
00086 #define G_GNUC_NORETURN
00087 #endif
00088 #endif
00089
00090 void err(const char *s) G_GNUC_NORETURN;
00091
00092 void err(const char *s) {
00093 const int maxlen = 150;
00094 char s1[maxlen], *s2;
00095
00096 strncpy(s1, s, maxlen);
00097 if ((s2 = strstr(s, "%m"))) {
00098 strcpy(s1 + (s2 - s), strerror(errno));
00099 s2 += 2;
00100 strcpy(s1 + strlen(s1), s2);
00101 }
00102 #ifndef sun
00103
00104 else if ((s2 = strstr(s, "%h"))) {
00105 strcpy(s1 + (s2 - s), hstrerror(h_errno));
00106 s2 += 2;
00107 strcpy(s1 + strlen(s1), s2);
00108 }
00109 #endif
00110
00111 s1[maxlen-1] = '\0';
00112 #ifdef ISSERVER
00113 syslog(LOG_ERR, "%s", s1);
00114 #endif
00115 fprintf(stderr, "Error: %s\n", s1);
00116 exit(1);
00117 }
00118
00119 void logging(void) {
00120 #ifdef ISSERVER
00121 openlog(MY_NAME, LOG_PID, LOG_DAEMON);
00122 #endif
00123 setvbuf(stdout, NULL, _IONBF, 0);
00124 setvbuf(stderr, NULL, _IONBF, 0);
00125 }
00126
00127 #ifdef WORDS_BIGENDIAN
00128 u64 ntohll(u64 a) {
00129 return a;
00130 }
00131 #else
00132 u64 ntohll(u64 a) {
00133 u32 lo = a & 0xffffffff;
00134 u32 hi = a >> 32U;
00135 lo = ntohl(lo);
00136 hi = ntohl(hi);
00137 return ((u64) lo) << 32U | hi;
00138 }
00139 #endif
00140 #define htonll ntohll
00141
00142
00143 #define NBD_FLAG_HAS_FLAGS (1 << 0)
00144 #define NBD_FLAG_READ_ONLY (1 << 1)