From 8119447fbe723296a8a8e7e1547e170af475eef1 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sat, 27 Jul 1991 21:42:02 +0000 Subject: [PATCH] Add gethostname() and s.avail(). --- Modules/socketmodule.c | 55 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index c5f6fe7ba9f..8ad9adae437 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -30,13 +30,14 @@ This module provides an interface to Berkeley socket IPC. Limitations: - only AF_INET and AF_UNIX address families are supported -- no asynchronous I/O -- no read/write operations (use send/recv instead) +- no asynchronous I/O (but read polling: avail) +- no read/write operations (use send/recv or makefile instead) - no flags on sendto/recvfrom operations - no setsockopt() call Interface: +- socket.gethostname() --> host name (string) - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') - socket.getservbyname(servername, protocolname) --> port number - socket.socket(family, type [, proto]) --> new socket object @@ -50,9 +51,10 @@ Interface: Socket methods: +- s.accept() --> new socket object, sockaddr +- s.avail() --> boolean - s.bind(sockaddr) --> None - s.connect(sockaddr) --> None -- s.accept() --> new socket object, sockaddr - s.listen(n) --> None - s.makefile(mode) --> file object - s.recv(nbytes) --> string @@ -67,11 +69,16 @@ Socket methods: #include "allobjects.h" #include "modsupport.h" +#include #include #include +#include /* Needed for struct timeval */ #include #include #include +#ifdef _AIX /* I *think* this works */ +#include /* Needed for fd_set */ +#endif /* Global variable holding the exception type for errors detected @@ -336,6 +343,27 @@ sock_accept(s, args) } +/* s.avail() method */ + +static object * +sock_avail(s, args) + sockobject *s; + object *args; +{ + struct timeval timeout; + fd_set readers; + int n; + timeout.tv_sec = 0; + timeout.tv_usec = 0; + FD_ZERO(&readers); + FD_SET(s->sock_fd, &readers); + n = select(s->sock_fd+1, &readers, (fd_set *)0, (fd_set *)0, &timeout); + if (n < 0) + return socket_error(); + return newintobject((long) (n != 0)); +} + + /* s.bind(sockaddr) method */ static object * @@ -555,6 +583,7 @@ sock_shutdown(s, args) static struct methodlist sock_methods[] = { {"accept", sock_accept}, + {"avail", sock_avail}, {"bind", sock_bind}, {"close", sock_close}, {"connect", sock_connect}, @@ -614,6 +643,22 @@ static typeobject Socktype = { }; +/* Python interface to gethostname(). */ + +/*ARGSUSED*/ +static object * +socket_gethostname(self, args) + object *self; + object *args; +{ + char buf[1024]; + if (!getnoarg(args)) + return NULL; + if (gethostname(buf, sizeof buf - 1) < 0) + return socket_error(); + buf[sizeof buf - 1] = '\0'; + return newstringobject(buf); +} /* Python interface to gethostbyname(name). */ /*ARGSUSED*/ @@ -685,6 +730,9 @@ socket_socket(self, args) 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; } @@ -693,6 +741,7 @@ socket_socket(self, args) static struct methodlist socket_methods[] = { {"gethostbyname", socket_gethostbyname}, + {"gethostname", socket_gethostname}, {"getservbyname", socket_getservbyname}, {"socket", socket_socket}, {NULL, NULL} /* Sentinel */