UDP autobind must not fail?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

I was examining how many TCP and UDP ports
are assigned for bind(2) with addr.sin_port = htons(0).
I found strange behavior with 2.6.12-1.1447_FC4 kernel.

In the case of socket(AF_INET, SOCK_STREAM, 0), it returned
error when exhausted local port range during search
as they are described in the source code.
http://lxr.linux.no/source/net/ipv4/tcp_ipv4.c#L218
http://lxr.linux.no/source/net/ipv4/tcp_ipv4.c?v=2.4.28#L218

But in the case of socket(AF_INET, SOCK_DGRAM, 0), it doesn't
seem to return error when exhausted local port range during search.
http://lxr.linux.no/source/net/ipv4/udp.c#L130
http://lxr.linux.no/source/net/ipv4/udp.c?v=2.4.28#L116

Running the following program needs too long busy time
(I think it has fallen into infinite busy loop)
with my FC4 environment.
Does automatic local port assignment for UDP MUST NOT FAIL?
Or is this a bug?

Regards.

--- bindtest.c ---
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>

int main(int argc, char *argv[]) {
	int i;
	for (i = 0; i < 128; i++) {
		switch (fork()) {
		case 0:
			for (i = 0; i < 512; i++) {
				int fd = socket(AF_INET, SOCK_DGRAM, 0);
				struct sockaddr_in addr;
				memset(&addr, 0, sizeof(addr));
				addr.sin_family = AF_INET;
				addr.sin_addr.s_addr = htonl(INADDR_ANY);
				addr.sin_port = htons(0);
				if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == 0) {
					socklen_t size = sizeof(addr);
					getsockname(fd, (struct sockaddr *) &addr, &size);
					printf("%05d\n", ntohs(addr.sin_port)); fflush(stdout);
				}
			}
			while (1) sleep(100);
		}
	}
	while (1) sleep(100);
	return 0;
}

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux