Allow varying the port for new-style handshakes
authorWouter Verhelst <w@uter.be>
Sun, 29 May 2011 09:55:19 +0000 (11:55 +0200)
committerWouter Verhelst <w@uter.be>
Sun, 29 May 2011 09:55:19 +0000 (11:55 +0200)
which is useful for debugging purposes.

man/nbd-client.8.in.sgml
man/nbd-server.5.in.sgml
nbd-client.c
nbd-server.c
nbd-tester-client.c

index af19bbc..81e003e 100644 (file)
@@ -131,7 +131,7 @@ manpage.1: manpage.sgml
          <para>The TCP port on which <command>nbd-server</command> is
            running at the server.</para>
          <para>This option is required, unless the -N option is
-           specified, in which case it is not allowed.</para>
+           specified.</para>
        </listitem>
       </varlistentry>
       <varlistentry>
index c6eb7f8..c9f618d 100644 (file)
@@ -181,6 +181,19 @@ manpage.1: manpage.sgml
        </listitem>
       </varlistentry>
       <varlistentry>
+        <term><option>port</option></term>
+       <listitem>
+         <para>
+           Optional; string
+         </para>
+         <para>
+           The port on which to listen for new-style nbd-client
+           connections. If not specified, the IANA-assigned port of
+           10809 is used.
+         </para>
+       </listitem>
+      </varlistentry>
+      <varlistentry>
        <term><option>user</option></term>
         <listitem>
           <para>
index b1e2a3c..2456c59 100644 (file)
@@ -278,7 +278,8 @@ 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] [-name|-N name]\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]\n");
+       fprintf(stderr, "Or   : nbd-client -name|-N name host [port] nbd_device [-block-size|-b block size] [-timeout|-t timeout] [-swap|-s] [-sdp|-S] [-persist|-p] [-nofork|-n]\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");
@@ -286,6 +287,7 @@ void usage(char* errmsg, ...) {
        fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
        fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
        fprintf(stderr, "blocksizes other than 1024 without patches\n");
+       fprintf(stderr, "Default value for port with -N is 10809. Note that port must always be numeric\n");
 }
 
 void disconnect(char* device) {
@@ -374,10 +376,6 @@ int main(int argc, char *argv[]) {
                                                nonspecial++;
                                        } else {
                                                port = optarg;
-                                               if(name) {
-                                                       usage("port and name specified at the same time. This is not supported.");
-                                                       exit(EXIT_FAILURE);
-                                               }
                                        }
                                        break;
                                case 2:
@@ -406,11 +404,9 @@ int main(int argc, char *argv[]) {
                        break;
                case 'N':
                        name=optarg;
-                       if(port) {
-                               usage("port and name specified at the same time. This is not supported.");
-                               exit(EXIT_FAILURE);
+                       if(!port) {
+                               port = NBD_DEFAULT_PORT;
                        }
-                       port = NBD_DEFAULT_PORT;
                        break;
                case 'p':
                        cont=1;
index 41b847a..f8028cb 100644 (file)
@@ -171,6 +171,8 @@ int modernsock=0;     /**< Socket for the modern handler. Not used
                               oldstyle is set to false (and then the
                               command-line client isn't used, gna gna) */
 char* modern_listen;     /**< listenaddr value for modernsock */
+char* modernport=NBD_DEFAULT_PORT; /**< Port number on which to listen for
+                                     new-style nbd-client connections */
 
 /**
  * Types of virtuatlization
@@ -410,7 +412,7 @@ void usage() {
               "\t-p|--pid-file\t\tspecify a filename to write our PID to\n"
               "\t-o|--output-config\toutput a config file section for what you\n\t\t\t\tspecified on the command line, with the\n\t\t\t\tspecified section name\n"
               "\t-M|--max-connections\tspecify the maximum number of opened connections\n\n"
-              "\tif port is set to 0, stdin is used (for running from inetd)\n"
+              "\tif port is set to 0, stdin is used (for running from inetd).\n"
               "\tif file_to_export contains '%%s', it is substituted with the IP\n"
               "\t\taddress of the machine trying to connect\n" 
               "\tif ip is set, it contains the local IP address on which we're listening.\n\tif not, the server will listen on all local IP addresses\n");
@@ -777,6 +779,7 @@ GArray* parse_cfile(gchar* f, GError** e) {
                { "group",      FALSE, PARAM_STRING,    &rungroup,      0 },
                { "oldstyle",   FALSE, PARAM_BOOL,      &do_oldstyle,   1 },
                { "listenaddr", FALSE, PARAM_STRING,    &modern_listen, 0 },
+               { "port",       FALSE, PARAM_STRING,    &modernport,    0 },
        };
        PARAM* p=gp;
        int p_size=sizeof(gp)/sizeof(PARAM);
@@ -846,8 +849,8 @@ GArray* parse_cfile(gchar* f, GError** e) {
                                        }
                                        break;
                        }
-                       if(!strcmp(p[j].paramname, "port") && !strcmp(p[j].target, NBD_DEFAULT_PORT)) {
-                               g_set_error(e, errdomain, CFILE_INCORRECT_PORT, "Config file specifies default port for oldstyle export");
+                       if(!strcmp(p[j].paramname, "port") && !strcmp(p[j].target, modernport)) {
+                               g_set_error(e, errdomain, CFILE_INCORRECT_PORT, "Config file specifies new-style port for oldstyle export");
                                g_key_file_free(cfile);
                                return NULL;
                        }
@@ -2112,7 +2115,7 @@ void open_modern(void) {
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_family = AF_UNSPEC;
        hints.ai_protocol = IPPROTO_TCP;
-       e = getaddrinfo(modern_listen, NBD_DEFAULT_PORT, &hints, &ai);
+       e = getaddrinfo(modern_listen, modernport, &hints, &ai);
        if(e != 0) {
                fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(e));
                exit(EXIT_FAILURE);
index ed9214e..6145997 100644 (file)
@@ -1186,7 +1186,7 @@ int main(int argc, char**argv) {
        if(argc<3) {
                g_message("%d: Not enough arguments", (int)getpid());
                g_message("%d: Usage: %s <hostname> <port>", (int)getpid(), argv[0]);
-               g_message("%d: Or: %s <hostname> -N <exportname>", (int)getpid(), argv[0]);
+               g_message("%d: Or: %s <hostname> -N <exportname> [<port>]", (int)getpid(), argv[0]);
                exit(EXIT_FAILURE);
        }
        logging();
@@ -1199,7 +1199,6 @@ int main(int argc, char**argv) {
                                                nonopt++;
                                                break;
                                        case 1:
-                                               if(want_port)
                                                p=(strtol(argv[2], NULL, 0));
                                                if(p==LONG_MIN||p==LONG_MAX) {
                                                        g_critical("Could not parse port number: %s", strerror(errno));
@@ -1210,7 +1209,9 @@ int main(int argc, char**argv) {
                                break;
                        case 'N':
                                name=g_strdup(optarg);
-                               p = 10809;
+                               if(!p) {
+                                       p = 10809;
+                               }
                                want_port = false;
                                break;
                        case 't':