[Patches] New function in socketmodule: getservbyport

Thomas Gellekum tg@melaten.rwth-aachen.de
24 Feb 2000 11:48:22 +0100


--=-=-=

Moin,

The first attachment contains diffs for Modules/socketmodule.c which
implement an interface to getservbyport(3). I chose not to worry about
aliases for the service name.

The second attachment updates the documentation.

Both patches are relative to Python-1.5.2.

I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims").  To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.

I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation.  I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.

tg


--=-=-=
Content-Type: application/octet-stream
Content-Disposition: attachment; filename=socketmodule.c.diff
Content-Description: Patch for socketmodule.c

*** Modules/socketmodule.c.orig	Tue Apr 13 06:07:32 1999
--- Modules/socketmodule.c	Wed Feb 23 18:07:50 2000
***************
*** 48,53 ****
--- 48,54 ----
  - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
  - socket.getprotobyname(protocolname) --> protocol number
  - socket.getservbyname(servicename, protocolname) --> port number
+ - socket.getservbyport(portnumber, protocolname) --> service name
  - socket.socket(family, type [, proto]) --> new socket object
  - socket.ntohs(16 bit value) --> new int object
  - socket.ntohl(32 bit value) --> new int object
***************
*** 1596,1601 ****
--- 1597,1631 ----
  The protocol name should be 'tcp' or 'udp'.";
  
  
+ /* Python interface to getservbyport(port).
+    This only returns the service name for now. */
+ 
+ /*ARGSUSED*/
+ static PyObject *
+ BUILD_FUNC_DEF_2(PySocket_getservbyport,PyObject *,self, PyObject *,args)
+ {
+ 	int port;
+ 	char *proto;
+ 	struct servent *sp;
+ 	if (!PyArg_Parse(args, "(is)", &port, &proto))
+ 		return NULL;
+ 	Py_BEGIN_ALLOW_THREADS
+ 	sp = getservbyport(ntohs(port), proto);
+ 	Py_END_ALLOW_THREADS
+ 	if (sp == NULL) {
+ 		PyErr_SetString(PySocket_Error, "port/proto not found");
+ 		return NULL;
+ 	}
+ 	return PyString_FromString(sp->s_name);
+ }
+ 
+ static char getservbyport_doc[] =
+ "getservbyport(port, protocolname) -> string\n\
+ \n\
+ Return a service name from a port number and protocol name.\n\
+ The protocol name should be 'tcp' or 'udp'.";
+ 
+ 
  /* Python interface to getprotobyname(name).
     This only returns the protocol number, since the other info is
     already known or not useful (like the list of aliases). */
***************
*** 1794,1799 ****
--- 1824,1830 ----
  	{"gethostbyaddr",	PySocket_gethostbyaddr, 0, gethostbyaddr_doc},
  	{"gethostname",		PySocket_gethostname, 0, gethostname_doc},
  	{"getservbyname",	PySocket_getservbyname, 0, getservbyname_doc},
+ 	{"getservbyport",	PySocket_getservbyport, 0, getservbyport_doc},
  	{"getprotobyname",	PySocket_getprotobyname, 0,getprotobyname_doc},
  	{"socket",		PySocket_socket, 1, socket_doc},
  #ifndef NO_DUP
***************
*** 1924,1929 ****
--- 1955,1961 ----
  gethostbyname() -- map a hostname to its IP number\n\
  gethostbyaddr() -- map an IP number or hostname to DNS info\n\
  getservbyname() -- map a service name and a protocol name to a port number\n\
+ getservbyport() -- map a port number and a protocol name to a service name\n\
  getprotobyname() -- mape a protocol name (e.g. 'tcp') to a number\n\
  ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order\n\
  htons(), htonl() -- convert 16, 32 bit int from host to network byte order\n\

--=-=-=
Content-Type: application/octet-stream
Content-Disposition: attachment; filename=libsocket.tex.diff
Content-Description: Patch for libsocket.tex

*** Doc/lib/libsocket.tex.orig	Wed Apr 21 19:29:14 1999
--- Doc/lib/libsocket.tex	Thu Feb 24 11:38:34 2000
***************
*** 150,155 ****
--- 150,161 ----
  \code{'udp'}.
  \end{funcdesc}
  
+ \begin{funcdesc}{getservbyport}{portnumber, protocolname}
+ Translate a port number and protocol name to an Internet service name
+ for that port.  The protocol name should be \code{'tcp'} or
+ \code{'udp'}.
+ \end{funcdesc}
+ 
  \begin{funcdesc}{socket}{family, type\optional{, proto}}
  Create a new socket using the given address family, socket type and
  protocol number.  The address family should be \constant{AF_INET} or

--=-=-=--