]> git.proxmox.com Git - mirror_qemu.git/commitdiff
sockets: Clean up inet_listen_opts()'s convoluted bind() loop
authorMarkus Armbruster <armbru@redhat.com>
Tue, 7 Feb 2012 14:09:15 +0000 (15:09 +0100)
committerAnthony Liguori <aliguori@us.ibm.com>
Fri, 24 Feb 2012 15:06:58 +0000 (09:06 -0600)
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
qemu-sockets.c

index e6e6c72dfdd53c52785957da4f63c1b34a3f651b..6bcb8e3e0f46ab369566484c76e1d505a9fb4d31 100644 (file)
@@ -107,7 +107,7 @@ int inet_listen_opts(QemuOpts *opts, int port_offset)
     char port[33];
     char uaddr[INET6_ADDRSTRLEN+1];
     char uport[33];
-    int slisten,rc,to,try_next;
+    int slisten, rc, to, port_min, port_max, p;
 
     memset(&ai,0, sizeof(ai));
     ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
@@ -159,20 +159,18 @@ int inet_listen_opts(QemuOpts *opts, int port_offset)
         }
 #endif
 
-        for (;;) {
+        port_min = inet_getport(e);
+        port_max = to ? to + port_offset : port_min;
+        for (p = port_min; p <= port_max; p++) {
+            inet_setport(e, p);
             if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
                 goto listen;
             }
-            try_next = to && (inet_getport(e) <= to + port_offset);
-            if (!try_next)
+            if (p == port_max) {
                 fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
                         inet_strfamily(e->ai_family), uaddr, inet_getport(e),
                         strerror(errno));
-            if (try_next) {
-                inet_setport(e, inet_getport(e) + 1);
-                continue;
             }
-            break;
         }
         closesocket(slisten);
     }