rresvport in python

Dan Stromberg - OAC-DCS (strombrg@bingy.acs.uci.edu)
Fri, 29 Jul 1994 21:21:14 -0700

I believe this patch allows binding to a reserved port from python,
given sufficient access (uid 0). I've tested it so far as to do an
"lpq"-alike against a SunOS 4.1.x lpd - the same lpd denied access
from an unpriviledged socket. ...and I saw a nice "permission denied"
when I tried to run it from a non-root account.

*** Modules/socketmodule.c.t Thu Jul 14 05:01:53 1994
--- Modules/socketmodule.c Fri Jul 29 21:15:06 1994
***************
*** 963,968 ****
--- 963,996 ----
}


+ /*ARGSUSED*/
+ static object *
+ socket_rresvport(self, args)
+ object *self;
+ object *args;
+ {
+ sockobject *s;
+ int fd, family=AF_INET, type=SOCK_STREAM, proto, port;
+ proto = 0;
+ if (!getargs(args, "i", &port)) {
+ return NULL;
+ }
+ BGN_SAVE
+ fd = rresvport(&port);
+ END_SAVE
+ if (fd < 0)
+ return socket_error();
+ s = newsockobject(fd, family, type, proto);
+ /* If the object can't be created, don't forget to close the
+ file descriptor again! */
+ if (s == NULL)
+ (void) close(fd);
+ /* From now on, ignore SIGPIPE and let the error checking
+ do the work. */
+ (void) signal(SIGPIPE, SIG_IGN);
+ return (object *) s;
+ }
+
/* Create a socket object from a numeric file description.
Useful e.g. if stdin is a socket.
Additional arguments as for socket(). */
***************
*** 1000,1005 ****
--- 1028,1034 ----
{"gethostname", socket_gethostname},
{"getservbyname", socket_getservbyname},
{"socket", socket_socket},
+ {"rresvport", socket_rresvport},
{"fromfd", socket_fromfd},
{NULL, NULL} /* Sentinel */
};