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 #include <linux/types.h>
00046 #include <linux/nbd.h>
00047 #endif
00048
00049 #if NBD_LFS==1
00050 #define _LARGEFILE_SOURCE
00051 #define _FILE_OFFSET_BITS 64
00052 #endif
00053
00054 u64 cliserv_magic = 0x00420281861253LL;
00055 #define INIT_PASSWD "NBDMAGIC"
00056
00057 #define INFO(a) do { } while(0)
00058
00059 void setmysockopt(int sock) {
00060 int size = 1;
00061 #if 0
00062 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
00063 INFO("(no sockopt/1: %m)");
00064 #endif
00065 #ifdef IPPROTO_TCP
00066 size = 1;
00067 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
00068 INFO("(no sockopt/2: %m)");
00069 #endif
00070 #if 0
00071 size = 1024;
00072 if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
00073 INFO("(no sockopt/3: %m)");
00074 #endif
00075 }
00076
00077 #ifndef G_GNUC_NORETURN
00078 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
00079 #define G_GNUC_NORETURN __attribute__((__noreturn__))
00080 #else
00081 #define G_GNUC_NORETURN
00082 #endif
00083 #endif
00084
00085 void err(const char *s) G_GNUC_NORETURN;
00086
00087 void err(const char *s) {
00088 const int maxlen = 150;
00089 char s1[maxlen], *s2;
00090
00091 strncpy(s1, s, maxlen);
00092 if ((s2 = strstr(s, "%m"))) {
00093 strcpy(s1 + (s2 - s), strerror(errno));
00094 s2 += 2;
00095 strcpy(s1 + strlen(s1), s2);
00096 }
00097 #ifndef sun
00098
00099 else if ((s2 = strstr(s, "%h"))) {
00100 strcpy(s1 + (s2 - s), hstrerror(h_errno));
00101 s2 += 2;
00102 strcpy(s1 + strlen(s1), s2);
00103 }
00104 #endif
00105
00106 s1[maxlen-1] = '\0';
00107 #ifdef ISSERVER
00108 syslog(LOG_ERR, "%s", s1);
00109 #endif
00110 fprintf(stderr, "Error: %s\n", s1);
00111 exit(1);
00112 }
00113
00114 void logging(void) {
00115 #ifdef ISSERVER
00116 openlog(MY_NAME, LOG_PID, LOG_DAEMON);
00117 #endif
00118 setvbuf(stdout, NULL, _IONBF, 0);
00119 setvbuf(stderr, NULL, _IONBF, 0);
00120 }
00121
00122 #ifdef WORDS_BIGENDIAN
00123 u64 ntohll(u64 a) {
00124 return a;
00125 }
00126 #else
00127 u64 ntohll(u64 a) {
00128 u32 lo = a & 0xffffffff;
00129 u32 hi = a >> 32U;
00130 lo = ntohl(lo);
00131 hi = ntohl(hi);
00132 return ((u64) lo) << 32U | hi;
00133 }
00134 #endif
00135 #define htonll ntohll
00136
00137
00138 #define NBD_FLAG_HAS_FLAGS (1 << 0)
00139 #define NBD_FLAG_READ_ONLY (1 << 1)