Oops
[nbd.git] / nbd-client.c
index 6e5225d..b0631ca 100644 (file)
 #include <getopt.h>
 #include <stdarg.h>
 
-#ifndef __GNUC__
-#error I need GCC to work
-#endif
-
 #include <linux/ioctl.h>
 #define MY_NAME "nbd_client"
 #include "cliserv.h"
 
+#ifdef WITH_SDP
+#include <sdp_inet.h>
+#endif
+
 int check_conn(char* devname, int do_print) {
        char buf[256];
        char* p;
@@ -139,14 +139,14 @@ void negotiate(int sock, u64 *rsize64, u32 *flags, char* name) {
        if (read(sock, &magic, sizeof(magic)) < 0)
                err("Failed/2: %m");
        magic = ntohll(magic);
-       if (magic != cliserv_magic)
-               err("Not enough cliserv_magic");
-       printf(".");
        if(name) {
                uint32_t opt;
-               uint64_t namesize;
-               uint64_t reserved = 0;
+               uint32_t namesize;
+               uint32_t reserved = 0;
 
+               if (magic != opts_magic)
+                       err("Not enough opts_magic");
+               printf(".");
                if(read(sock, &tmp, sizeof(uint16_t)) < 0) {
                        err("Failed reading flags: %m");
                }
@@ -156,14 +156,18 @@ void negotiate(int sock, u64 *rsize64, u32 *flags, char* name) {
                write(sock, &reserved, sizeof(reserved));
 
                /* Write the export name that we're after */
-               magic = ntohll(cliserv_magic);
+               magic = ntohll(opts_magic);
                write(sock, &magic, sizeof(magic));
                opt = ntohl(NBD_OPT_EXPORT_NAME);
                write(sock, &opt, sizeof(opt));
-               namesize = (u64)strlen(name);
-               namesize = ntohll(namesize);
+               namesize = (u32)strlen(name);
+               namesize = ntohl(namesize);
                write(sock, &namesize, sizeof(namesize));
                write(sock, name, strlen(name));
+       } else {
+               if (magic != cliserv_magic)
+                       err("Not enough cliserv_magic");
+               printf(".");
        }
 
        if (read(sock, &size64, sizeof(size64)) < 0)
@@ -264,7 +268,7 @@ void usage(char* errmsg, ...) {
        } else {
                fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
        }
-       fprintf(stderr, "Usage: nbd-client host port nbd_device [-block-size|-b block size] [-timeout|-t timeout] [-swap|-s] [-sdp|-S] [-persist|-p] [-nofork|-n]\n");
+       fprintf(stderr, "Usage: nbd-client host port nbd_device [-block-size|-b block size] [-timeout|-t timeout] [-swap|-s] [-sdp|-S] [-persist|-p] [-nofork|-n] [-name|-N name]\n");
        fprintf(stderr, "Or   : nbd-client -d nbd_device\n");
        fprintf(stderr, "Or   : nbd-client -c nbd_device\n");
        fprintf(stderr, "Or   : nbd-client -h|--help\n");
@@ -354,12 +358,12 @@ int main(int argc, char *argv[]) {
                                        break;
                                case 1:
                                        // port
-                                       port = optarg;
                                        if(!strtol(optarg, NULL, 0)) {
                                                // not parseable as a number, assume it's the device and we have a name
                                                nbddev = optarg;
                                                nonspecial++;
                                        } else {
+                                               port = optarg;
                                                if(name) {
                                                        usage("port and name specified at the same time. This is not supported.");
                                                        exit(EXIT_FAILURE);
@@ -380,8 +384,7 @@ int main(int argc, char *argv[]) {
                        blocksize=(int)strtol(optarg, NULL, 0);
                        break;
                case 'c':
-                       check_conn(optarg, 1);
-                       exit(EXIT_SUCCESS);
+                       return check_conn(optarg, 1);
                case 'd':
                        disconnect(optarg);
                        exit(EXIT_SUCCESS);