Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bind to an Unspecified Port

15 views
Skip to first unread message

Matt Diephouse

unread,
Dec 21, 2006, 6:37:23 PM12/21/06
to parrot-...@perl.org
I've been looking through the sockets code while writing a PIR socket
library and I've noticed one major deficiency: if you pass 0 as the
port you wish to bind to, the OS will choose a port for you. But
there's no way in Parrot to find out which port that is.

Right now, bind just returns 0 on success (and -1 on failure). I'd
like to change bind to return the port it's bound to on success. The
patch below adds this code for the unix sockets code. The windows code
looks like it'd be the same, but I can't test it so I'd have to find
someone to help with that.

--
Matt Diephouse
http://matt.diephouse.com

Index: src/io/io_unix.c
===================================================================
--- src/io/io_unix.c (revision 16202)
+++ src/io/io_unix.c (working copy)
@@ -776,7 +776,18 @@
return -1;
}

- return 0;
+ /* bind was successful. return the port number we're bound to. */
+ if (io->local.sin_port)
+ return ntohs(io->local.sin_port);
+ else {
+ struct sockaddr_in server;
+ int length;
+
+ if (getsockname(io->fd, (struct sockaddr *)&server, &length) == -1)
+ return 0;
+ else
+ return ntohs(server.sin_port);
+ }
}

/*

0 new messages