sockets: allow port to be NULL when listening on IP address
If the port in the SocketAddress struct is NULL, it can allow the kernel to automatically select a free port. This is useful in particular in unit tests to avoid a race trying to find a free port to run a test case on. Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
		
							parent
							
								
									2a8e21c7c8
								
							
						
					
					
						commit
						0983f5e6af
					
				@ -2614,7 +2614,9 @@
 | 
				
			|||||||
#
 | 
					#
 | 
				
			||||||
# @host: host part of the address
 | 
					# @host: host part of the address
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# @port: port part of the address, or lowest port if @to is present
 | 
					# @port: port part of the address, or lowest port if @to is present.
 | 
				
			||||||
 | 
					#        Kernel selects a free port if omitted for listener addresses.
 | 
				
			||||||
 | 
					#        #optional
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# @to: highest port to try
 | 
					# @to: highest port to try
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
@ -2629,7 +2631,7 @@
 | 
				
			|||||||
{ 'struct': 'InetSocketAddress',
 | 
					{ 'struct': 'InetSocketAddress',
 | 
				
			||||||
  'data': {
 | 
					  'data': {
 | 
				
			||||||
    'host': 'str',
 | 
					    'host': 'str',
 | 
				
			||||||
    'port': 'str',
 | 
					    '*port': 'str',
 | 
				
			||||||
    '*to': 'uint16',
 | 
					    '*to': 'uint16',
 | 
				
			||||||
    '*ipv4': 'bool',
 | 
					    '*ipv4': 'bool',
 | 
				
			||||||
    '*ipv6': 'bool' } }
 | 
					    '*ipv6': 'bool' } }
 | 
				
			||||||
 | 
				
			|||||||
@ -128,12 +128,15 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
 | 
				
			|||||||
    ai.ai_family = PF_UNSPEC;
 | 
					    ai.ai_family = PF_UNSPEC;
 | 
				
			||||||
    ai.ai_socktype = SOCK_STREAM;
 | 
					    ai.ai_socktype = SOCK_STREAM;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if ((qemu_opt_get(opts, "host") == NULL) ||
 | 
					    if ((qemu_opt_get(opts, "host") == NULL)) {
 | 
				
			||||||
        (qemu_opt_get(opts, "port") == NULL)) {
 | 
					        error_setg(errp, "host not specified");
 | 
				
			||||||
        error_setg(errp, "host and/or port not specified");
 | 
					 | 
				
			||||||
        return -1;
 | 
					        return -1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
 | 
					    if (qemu_opt_get(opts, "port") != NULL) {
 | 
				
			||||||
 | 
					        pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        port[0] = '\0';
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    addr = qemu_opt_get(opts, "host");
 | 
					    addr = qemu_opt_get(opts, "host");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    to = qemu_opt_get_number(opts, "to", 0);
 | 
					    to = qemu_opt_get_number(opts, "to", 0);
 | 
				
			||||||
@ -145,6 +148,10 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
 | 
				
			|||||||
    /* lookup */
 | 
					    /* lookup */
 | 
				
			||||||
    if (port_offset) {
 | 
					    if (port_offset) {
 | 
				
			||||||
        unsigned long long baseport;
 | 
					        unsigned long long baseport;
 | 
				
			||||||
 | 
					        if (strlen(port) == 0) {
 | 
				
			||||||
 | 
					            error_setg(errp, "port not specified");
 | 
				
			||||||
 | 
					            return -1;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        if (parse_uint_full(port, &baseport, 10) < 0) {
 | 
					        if (parse_uint_full(port, &baseport, 10) < 0) {
 | 
				
			||||||
            error_setg(errp, "can't convert to a number: %s", port);
 | 
					            error_setg(errp, "can't convert to a number: %s", port);
 | 
				
			||||||
            return -1;
 | 
					            return -1;
 | 
				
			||||||
@ -156,7 +163,8 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        snprintf(port, sizeof(port), "%d", (int)baseport + port_offset);
 | 
					        snprintf(port, sizeof(port), "%d", (int)baseport + port_offset);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
 | 
					    rc = getaddrinfo(strlen(addr) ? addr : NULL,
 | 
				
			||||||
 | 
					                     strlen(port) ? port : NULL, &ai, &res);
 | 
				
			||||||
    if (rc != 0) {
 | 
					    if (rc != 0) {
 | 
				
			||||||
        error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
 | 
					        error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
 | 
				
			||||||
                   gai_strerror(rc));
 | 
					                   gai_strerror(rc));
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user