Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Socket module */ |
| 33 | |
| 34 | /* |
| 35 | This module provides an interface to Berkeley socket IPC. |
| 36 | |
| 37 | Limitations: |
| 38 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 39 | - only AF_INET and AF_UNIX address families are supported |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 40 | - no read/write operations (use send/recv or makefile instead) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 41 | - additional restrictions apply on Windows |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 43 | Module interface: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 45 | - socket.error: exception raised for socket specific errors |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 46 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 47 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 48 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') |
Guido van Rossum | 25405c7 | 1996-12-19 16:42:52 +0000 | [diff] [blame] | 49 | - socket.getprotobyname(protocolname) --> protocol number |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 50 | - socket.getservbyname(servicename, protocolname) --> port number |
Guido van Rossum | 4dd2a7e | 1991-07-01 18:51:33 +0000 | [diff] [blame] | 51 | - socket.socket(family, type [, proto]) --> new socket object |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 52 | - socket.ntohs(16 bit value) --> new int object |
| 53 | - socket.ntohl(32 bit value) --> new int object |
| 54 | - socket.htons(16 bit value) --> new int object |
| 55 | - socket.htonl(32 bit value) --> new int object |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 56 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 57 | - an Internet socket address is a pair (hostname, port) |
| 58 | where hostname can be anything recognized by gethostbyname() |
| 59 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 60 | - where a hostname is returned, the dd.dd.dd.dd notation is used |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 61 | - a UNIX domain socket address is a string specifying the pathname |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 63 | Socket methods: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 65 | - s.accept() --> new socket object, sockaddr |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 66 | - s.bind(sockaddr) --> None |
| 67 | - s.close() --> None |
| 68 | - s.connect(sockaddr) --> None |
| 69 | - s.fileno() --> file descriptor |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 70 | - s.dup() --> same as socket.fromfd(os.dup(s.fileno(), ...) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 71 | - s.getpeername() --> sockaddr |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 72 | - s.getsockname() --> sockaddr |
| 73 | - s.getsockopt(level, optname[, buflen]) --> int or string |
| 74 | - s.listen(backlog) --> None |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 75 | - s.makefile([mode[, bufsize]]) --> file object |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 76 | - s.recv(buflen [,flags]) --> string |
| 77 | - s.recvfrom(buflen [,flags]) --> string, sockaddr |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 78 | - s.send(string [,flags]) --> nbytes |
| 79 | - s.sendto(string, [flags,] sockaddr) --> nbytes |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 80 | - s.setblocking(0 | 1) --> None |
| 81 | - s.setsockopt(level, optname, value) --> None |
| 82 | - s.shutdown(how) --> None |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 83 | - repr(s) --> "<socket object, fd=%d, family=%d, type=%d, protocol=%d>" |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 85 | */ |
| 86 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 87 | #include "Python.h" |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 89 | #ifdef HAVE_UNISTD_H |
| 90 | #include <unistd.h> |
| 91 | #endif |
| 92 | |
Guido van Rossum | fb4130d | 1996-12-10 15:17:08 +0000 | [diff] [blame] | 93 | #ifndef MS_WINDOWS |
| 94 | extern int gethostname(); /* For Solaris, at least */ |
| 95 | #endif |
| 96 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 97 | #include <sys/types.h> |
| 98 | #include "mytime.h" |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 100 | #include <signal.h> |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 101 | #ifndef MS_WINDOWS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 102 | #include <netdb.h> |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 103 | #include <sys/socket.h> |
| 104 | #include <netinet/in.h> |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 105 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 106 | #else |
| 107 | #include <winsock.h> |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 108 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 109 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 110 | #ifdef HAVE_SYS_UN_H |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 111 | #include <sys/un.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 112 | #else |
| 113 | #undef AF_UNIX |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 114 | #endif |
| 115 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 116 | #ifndef O_NDELAY |
| 117 | #define O_NDELAY O_NONBLOCK /* For QNX only? */ |
| 118 | #endif |
| 119 | |
Jack Jansen | 508537b | 1996-02-14 15:57:45 +0000 | [diff] [blame] | 120 | #ifdef USE_GUSI |
| 121 | /* fdopen() isn't declared in stdio.h (sigh) */ |
| 122 | #include <GUSI.h> |
| 123 | #endif |
| 124 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 126 | /* Here we have some hacks to choose between K&R or ANSI style function |
| 127 | definitions. For NT to build this as an extension module (ie, DLL) |
| 128 | it must be compiled by the C++ compiler, as it takes the address of |
| 129 | a static data item exported from the main Python DLL. |
| 130 | */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 131 | #ifdef MS_WINDOWS |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 132 | /* seem to be a few differences in the API */ |
| 133 | #define close closesocket |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 134 | #define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 135 | #define FORCE_ANSI_FUNC_DEFS |
| 136 | #endif |
| 137 | |
| 138 | #ifdef FORCE_ANSI_FUNC_DEFS |
| 139 | #define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \ |
| 140 | fnname( arg1type arg1name ) |
| 141 | |
| 142 | #define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \ |
| 143 | fnname( arg1type arg1name, arg2type arg2name ) |
| 144 | |
| 145 | #define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name ) \ |
| 146 | fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name ) |
| 147 | |
| 148 | #define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name, arg4type, arg4name ) \ |
| 149 | fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name, arg4type arg4name ) |
| 150 | |
| 151 | #else /* !FORCE_ANSI_FN_DEFS */ |
| 152 | #define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \ |
| 153 | fnname( arg1name ) \ |
| 154 | arg1type arg1name; |
| 155 | |
| 156 | #define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \ |
| 157 | fnname( arg1name, arg2name ) \ |
| 158 | arg1type arg1name; \ |
| 159 | arg2type arg2name; |
| 160 | |
| 161 | #define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name ) \ |
| 162 | fnname( arg1name, arg2name, arg3name ) \ |
| 163 | arg1type arg1name; \ |
| 164 | arg2type arg2name; \ |
| 165 | arg3type arg3name; |
| 166 | |
| 167 | #define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name, arg4type, arg4name ) \ |
| 168 | fnname( arg1name, arg2name, arg3name, arg4name ) \ |
| 169 | arg1type arg1name; \ |
| 170 | arg2type arg2name; \ |
| 171 | arg3type arg3name; \ |
| 172 | arg4type arg4name; |
| 173 | |
| 174 | #endif /* !FORCE_ANSI_FN_DEFS */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 175 | |
| 176 | /* Global variable holding the exception type for errors detected |
| 177 | by this module (but not argument type or memory errors, etc.). */ |
| 178 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 179 | static PyObject *PySocket_Error; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 180 | |
| 181 | |
| 182 | /* Convenience function to raise an error according to errno |
| 183 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 185 | static PyObject * |
| 186 | PySocket_Err() |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 187 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 188 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 189 | if (WSAGetLastError()) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 190 | PyObject *v; |
| 191 | v = Py_BuildValue("(is)", WSAGetLastError(), "winsock error"); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 192 | if (v != NULL) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 193 | PyErr_SetObject(PySocket_Error, v); |
| 194 | Py_DECREF(v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 195 | } |
| 196 | return NULL; |
| 197 | } |
| 198 | else |
| 199 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 200 | return PyErr_SetFromErrno(PySocket_Error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 203 | |
| 204 | /* The object holding a socket. It holds some extra information, |
| 205 | like the address family, which is used to decode socket address |
| 206 | arguments properly. */ |
| 207 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 208 | typedef struct { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 209 | PyObject_HEAD |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 210 | int sock_fd; /* Socket file descriptor */ |
| 211 | int sock_family; /* Address family, e.g., AF_INET */ |
| 212 | int sock_type; /* Socket type, e.g., SOCK_STREAM */ |
| 213 | int sock_proto; /* Protocol type, usually 0 */ |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 214 | union sock_addr { |
| 215 | struct sockaddr_in in; |
| 216 | #ifdef AF_UNIX |
| 217 | struct sockaddr_un un; |
| 218 | #endif |
| 219 | } sock_addr; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 220 | } PySocketSockObject; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 222 | |
| 223 | /* A forward reference to the Socktype type object. |
| 224 | The Socktype variable contains pointers to various functions, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 225 | some of which call newsockobject(), which uses Socktype, so |
Guido van Rossum | 54ba21b | 1991-09-10 14:57:12 +0000 | [diff] [blame] | 226 | there has to be a circular reference. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 227 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 228 | staticforward PyTypeObject PySocketSock_Type; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 230 | |
| 231 | /* Create a new socket object. |
| 232 | This just creates the object and initializes it. |
| 233 | If the creation fails, return NULL and set an exception (implicit |
| 234 | in NEWOBJ()). */ |
| 235 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 236 | static PySocketSockObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 237 | BUILD_FUNC_DEF_4(PySocketSock_New,int,fd, int,family, int,type, int,proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 238 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 239 | PySocketSockObject *s; |
Guido van Rossum | 5555854 | 1996-05-23 22:54:50 +0000 | [diff] [blame] | 240 | PySocketSock_Type.ob_type = &PyType_Type; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 241 | s = PyObject_NEW(PySocketSockObject, &PySocketSock_Type); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 242 | if (s != NULL) { |
| 243 | s->sock_fd = fd; |
| 244 | s->sock_family = family; |
| 245 | s->sock_type = type; |
| 246 | s->sock_proto = proto; |
| 247 | } |
| 248 | return s; |
| 249 | } |
| 250 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 251 | |
| 252 | /* Convert a string specifying a host name or one of a few symbolic |
| 253 | names to a numeric IP address. This usually calls gethostbyname() |
| 254 | to do the work; the names "" and "<broadcast>" are special. |
| 255 | Return the length (should always be 4 bytes), or negative if |
| 256 | an error occurred; then an exception is raised. */ |
| 257 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 258 | static int |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 259 | BUILD_FUNC_DEF_2(setipaddr, char*,name, struct sockaddr_in *,addr_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 260 | { |
| 261 | struct hostent *hp; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 262 | int d1, d2, d3, d4; |
| 263 | char ch; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 264 | #ifdef HAVE_GETHOSTBYNAME_R |
| 265 | struct hostent hp_allocated; |
| 266 | char buf[1001]; |
| 267 | int buf_len = (sizeof buf) - 1; |
| 268 | int errnop; |
| 269 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 271 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 272 | if (name[0] == '\0') { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 273 | addr_ret->sin_addr.s_addr = INADDR_ANY; |
| 274 | return 4; |
| 275 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 276 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 277 | addr_ret->sin_addr.s_addr = INADDR_BROADCAST; |
| 278 | return 4; |
| 279 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 280 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 281 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 282 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 283 | addr_ret->sin_addr.s_addr = htonl( |
| 284 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 285 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 286 | return 4; |
| 287 | } |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 288 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 289 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 290 | hp = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 291 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 292 | #else /* not HAVE_GETHOSTBYNAME_R */ |
| 293 | hp = gethostbyname(name); |
| 294 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 295 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 296 | if (hp == NULL) { |
Guido van Rossum | ca9b323 | 1995-01-10 16:30:54 +0000 | [diff] [blame] | 297 | #ifdef HAVE_HSTRERROR |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 298 | /* Let's get real error message to return */ |
| 299 | extern int h_errno; |
| 300 | PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno)); |
| 301 | #else |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 302 | PyErr_SetString(PySocket_Error, "host not found"); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 303 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 304 | return -1; |
| 305 | } |
| 306 | memcpy((char *) &addr_ret->sin_addr, hp->h_addr, hp->h_length); |
| 307 | return hp->h_length; |
| 308 | } |
| 309 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 310 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 311 | /* Create a string object representing an IP address. |
| 312 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 313 | size numbers). */ |
| 314 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 315 | static PyObject * |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 316 | BUILD_FUNC_DEF_1(makeipaddr, struct sockaddr_in *,addr) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 317 | { |
| 318 | long x = ntohl(addr->sin_addr.s_addr); |
| 319 | char buf[100]; |
| 320 | sprintf(buf, "%d.%d.%d.%d", |
| 321 | (int) (x>>24) & 0xff, (int) (x>>16) & 0xff, |
| 322 | (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 323 | return PyString_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | |
| 327 | /* Create an object representing the given socket address, |
| 328 | suitable for passing it back to bind(), connect() etc. |
| 329 | The family field of the sockaddr structure is inspected |
| 330 | to determine what kind of address it really is. */ |
| 331 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 332 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 333 | static PyObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 334 | BUILD_FUNC_DEF_2(makesockaddr,struct sockaddr *,addr, int,addrlen) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 335 | { |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 336 | if (addrlen == 0) { |
| 337 | /* No address -- may be recvfrom() from known socket */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 338 | Py_INCREF(Py_None); |
| 339 | return Py_None; |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 342 | switch (addr->sa_family) { |
| 343 | |
| 344 | case AF_INET: |
| 345 | { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 346 | struct sockaddr_in *a = (struct sockaddr_in *) addr; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 347 | PyObject *addrobj = makeipaddr(a); |
| 348 | PyObject *ret = NULL; |
| 349 | if (addrobj) { |
| 350 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 351 | Py_DECREF(addrobj); |
| 352 | } |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 353 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 354 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 355 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 356 | #ifdef AF_UNIX |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 357 | case AF_UNIX: |
| 358 | { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 359 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 360 | return PyString_FromString(a->sun_path); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 361 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 362 | #endif /* AF_UNIX */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 363 | |
| 364 | /* More cases here... */ |
| 365 | |
| 366 | default: |
Guido van Rossum | aa948df | 1997-05-07 17:41:48 +0000 | [diff] [blame] | 367 | /* If we don't know the address family, don't raise an |
| 368 | exception -- return it as a tuple. */ |
| 369 | return Py_BuildValue("is#", |
| 370 | addr->sa_family, |
| 371 | addr->sa_data, |
| 372 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 373 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 374 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 377 | |
| 378 | /* Parse a socket address argument according to the socket object's |
| 379 | address family. Return 1 if the address was in the proper format, |
| 380 | 0 of not. The address is returned through addr_ret, its length |
| 381 | through len_ret. */ |
| 382 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 383 | static int |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 384 | BUILD_FUNC_DEF_4( |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 385 | getsockaddrarg,PySocketSockObject *,s, PyObject *,args, struct sockaddr **,addr_ret, int *,len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 386 | { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 387 | switch (s->sock_family) { |
| 388 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 389 | #ifdef AF_UNIX |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 390 | case AF_UNIX: |
| 391 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 392 | struct sockaddr_un* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 393 | char *path; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 394 | int len; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 395 | addr = (struct sockaddr_un* )&(s->sock_addr).un; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 396 | if (!PyArg_Parse(args, "s#", &path, &len)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 397 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 398 | if (len > sizeof addr->sun_path) { |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 399 | PyErr_SetString(PySocket_Error, |
| 400 | "AF_UNIX path too long"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 401 | return 0; |
| 402 | } |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 403 | addr->sun_family = AF_UNIX; |
| 404 | memcpy(addr->sun_path, path, len); |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 405 | addr->sun_path[len] = 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 406 | *addr_ret = (struct sockaddr *) addr; |
Guido van Rossum | 65af28a | 1996-06-11 18:36:33 +0000 | [diff] [blame] | 407 | *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 408 | return 1; |
| 409 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 410 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 411 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 412 | case AF_INET: |
| 413 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 414 | struct sockaddr_in* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 415 | char *host; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 416 | int port; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 417 | addr=(struct sockaddr_in*)&(s->sock_addr).in; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 418 | if (!PyArg_Parse(args, "(si)", &host, &port)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 419 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 420 | if (setipaddr(host, addr) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 421 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 422 | addr->sin_family = AF_INET; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 423 | addr->sin_port = htons((short)port); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 424 | *addr_ret = (struct sockaddr *) addr; |
| 425 | *len_ret = sizeof *addr; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 426 | return 1; |
| 427 | } |
| 428 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 429 | /* More cases here... */ |
| 430 | |
| 431 | default: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 432 | PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 433 | return 0; |
| 434 | |
| 435 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 438 | |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 439 | /* Get the address length according to the socket object's address family. |
| 440 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 441 | through len_ret. */ |
| 442 | |
| 443 | static int |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 444 | BUILD_FUNC_DEF_2(getsockaddrlen,PySocketSockObject *,s, int *,len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 445 | { |
| 446 | switch (s->sock_family) { |
| 447 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 448 | #ifdef AF_UNIX |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 449 | case AF_UNIX: |
| 450 | { |
| 451 | *len_ret = sizeof (struct sockaddr_un); |
| 452 | return 1; |
| 453 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 454 | #endif /* AF_UNIX */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 455 | |
| 456 | case AF_INET: |
| 457 | { |
| 458 | *len_ret = sizeof (struct sockaddr_in); |
| 459 | return 1; |
| 460 | } |
| 461 | |
| 462 | /* More cases here... */ |
| 463 | |
| 464 | default: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 465 | PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family"); |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 466 | return 0; |
| 467 | |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 472 | /* s.accept() method */ |
| 473 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 474 | static PyObject * |
| 475 | BUILD_FUNC_DEF_2(PySocketSock_accept,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 476 | { |
| 477 | char addrbuf[256]; |
| 478 | int addrlen, newfd; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 479 | PyObject *sock = NULL; |
| 480 | PyObject *addr = NULL; |
| 481 | PyObject *res = NULL; |
| 482 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 483 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 484 | return NULL; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 485 | if (!getsockaddrlen(s, &addrlen)) |
| 486 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 487 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 488 | newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 489 | Py_END_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 490 | if (newfd < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 491 | return PySocket_Err(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 492 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 493 | /* Create the new object with unspecified family, |
| 494 | to avoid calls to bind() etc. on it. */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 495 | sock = (PyObject *) PySocketSock_New(newfd, |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 496 | s->sock_family, |
| 497 | s->sock_type, |
| 498 | s->sock_proto); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 499 | if (sock == NULL) { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 500 | close(newfd); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 501 | goto finally; |
| 502 | } |
| 503 | if (!(addr = makesockaddr((struct sockaddr *) addrbuf, addrlen))) |
| 504 | goto finally; |
| 505 | |
| 506 | if (!(res = Py_BuildValue("OO", sock, addr))) |
| 507 | goto finally; |
| 508 | |
| 509 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 510 | Py_XDECREF(sock); |
| 511 | Py_XDECREF(addr); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 512 | return res; |
| 513 | } |
| 514 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 515 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 516 | /* s.setblocking(1 | 0) method */ |
| 517 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 518 | static PyObject * |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 519 | BUILD_FUNC_DEF_2(PySocketSock_setblocking,PySocketSockObject*,s,PyObject*,args) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 520 | { |
| 521 | int block; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 522 | #ifndef MS_WINDOWS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 523 | int delay_flag; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 524 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 525 | if (!PyArg_GetInt(args, &block)) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 526 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 527 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 528 | #ifndef MS_WINDOWS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 529 | delay_flag = fcntl (s->sock_fd, F_GETFL, 0); |
| 530 | if (block) |
| 531 | delay_flag &= (~O_NDELAY); |
| 532 | else |
| 533 | delay_flag |= O_NDELAY; |
| 534 | fcntl (s->sock_fd, F_SETFL, delay_flag); |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 535 | #else |
| 536 | block = !block; |
| 537 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
| 538 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 539 | Py_END_ALLOW_THREADS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 540 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 541 | Py_INCREF(Py_None); |
| 542 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 543 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 544 | |
| 545 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 546 | /* s.setsockopt() method. |
| 547 | With an integer third argument, sets an integer option. |
| 548 | With a string third argument, sets an option from a buffer; |
| 549 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 550 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 551 | static PyObject * |
| 552 | BUILD_FUNC_DEF_2(PySocketSock_setsockopt,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 553 | { |
| 554 | int level; |
| 555 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 556 | int res; |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 557 | char *buf; |
| 558 | int buflen; |
| 559 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 560 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 561 | if (PyArg_Parse(args, "(iii)", &level, &optname, &flag)) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 562 | buf = (char *) &flag; |
| 563 | buflen = sizeof flag; |
| 564 | } |
| 565 | else { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 566 | PyErr_Clear(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 567 | if (!PyArg_Parse(args, "(iis#)", &level, &optname, |
| 568 | &buf, &buflen)) |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 569 | return NULL; |
| 570 | } |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 571 | res = setsockopt(s->sock_fd, level, optname, (ANY *)buf, buflen); |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 572 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 573 | return PySocket_Err(); |
| 574 | Py_INCREF(Py_None); |
| 575 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 579 | /* s.getsockopt() method. |
| 580 | With two arguments, retrieves an integer option. |
| 581 | With a third integer argument, retrieves a string buffer of that size; |
| 582 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 583 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 584 | static PyObject * |
| 585 | BUILD_FUNC_DEF_2(PySocketSock_getsockopt,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 586 | { |
| 587 | int level; |
| 588 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 589 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 590 | PyObject *buf; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 591 | int buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 592 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 593 | if (!PyArg_ParseTuple(args, "ii|i", &level, &optname, &buflen)) |
| 594 | return NULL; |
| 595 | |
| 596 | if (buflen == 0) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 597 | int flag = 0; |
| 598 | int flagsize = sizeof flag; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 599 | res = getsockopt(s->sock_fd, level, optname, |
| 600 | (ANY *)&flag, &flagsize); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 601 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 602 | return PySocket_Err(); |
| 603 | return PyInt_FromLong(flag); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 604 | } |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 605 | if (buflen <= 0 || buflen > 1024) { |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 606 | PyErr_SetString(PySocket_Error, |
| 607 | "getsockopt buflen out of range"); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 608 | return NULL; |
| 609 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 610 | buf = PyString_FromStringAndSize((char *)NULL, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 611 | if (buf == NULL) |
| 612 | return NULL; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 613 | res = getsockopt(s->sock_fd, level, optname, |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 614 | (ANY *)PyString_AsString(buf), &buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 615 | if (res < 0) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 616 | Py_DECREF(buf); |
| 617 | return PySocket_Err(); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 618 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 619 | _PyString_Resize(&buf, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 620 | return buf; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 624 | /* s.bind(sockaddr) method */ |
| 625 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 626 | static PyObject * |
| 627 | BUILD_FUNC_DEF_2(PySocketSock_bind,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 628 | { |
| 629 | struct sockaddr *addr; |
| 630 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 631 | int res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 632 | if (!getsockaddrarg(s, args, &addr, &addrlen)) |
| 633 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 634 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 635 | res = bind(s->sock_fd, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 636 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 637 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 638 | return PySocket_Err(); |
| 639 | Py_INCREF(Py_None); |
| 640 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 643 | |
| 644 | /* s.close() method. |
| 645 | Set the file descriptor to -1 so operations tried subsequently |
| 646 | will surely fail. */ |
| 647 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 648 | static PyObject * |
| 649 | BUILD_FUNC_DEF_2(PySocketSock_close,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 650 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 651 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 652 | return NULL; |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 653 | if (s->sock_fd != -1) { |
| 654 | Py_BEGIN_ALLOW_THREADS |
| 655 | (void) close(s->sock_fd); |
| 656 | Py_END_ALLOW_THREADS |
| 657 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 658 | s->sock_fd = -1; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 659 | Py_INCREF(Py_None); |
| 660 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 663 | |
| 664 | /* s.connect(sockaddr) method */ |
| 665 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 666 | static PyObject * |
| 667 | BUILD_FUNC_DEF_2(PySocketSock_connect,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 668 | { |
| 669 | struct sockaddr *addr; |
| 670 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 671 | int res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 672 | if (!getsockaddrarg(s, args, &addr, &addrlen)) |
| 673 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 674 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 675 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 676 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 677 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 678 | return PySocket_Err(); |
| 679 | Py_INCREF(Py_None); |
| 680 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 683 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 684 | /* s.fileno() method */ |
| 685 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 686 | static PyObject * |
| 687 | BUILD_FUNC_DEF_2(PySocketSock_fileno,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 688 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 689 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 690 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 691 | return PyInt_FromLong((long) s->sock_fd); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 695 | #ifndef NO_DUP |
| 696 | /* s.dup() method */ |
| 697 | |
| 698 | static PyObject * |
| 699 | BUILD_FUNC_DEF_2(PySocketSock_dup,PySocketSockObject *,s, PyObject *,args) |
| 700 | { |
| 701 | int newfd; |
| 702 | PyObject *sock; |
| 703 | if (!PyArg_NoArgs(args)) |
| 704 | return NULL; |
| 705 | newfd = dup(s->sock_fd); |
| 706 | if (newfd < 0) |
| 707 | return PySocket_Err(); |
| 708 | sock = (PyObject *) PySocketSock_New(newfd, |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 709 | s->sock_family, |
| 710 | s->sock_type, |
| 711 | s->sock_proto); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 712 | if (sock == NULL) |
| 713 | close(newfd); |
| 714 | return sock; |
| 715 | } |
| 716 | #endif |
| 717 | |
| 718 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 719 | /* s.getsockname() method */ |
| 720 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 721 | static PyObject * |
| 722 | BUILD_FUNC_DEF_2(PySocketSock_getsockname,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 723 | { |
| 724 | char addrbuf[256]; |
| 725 | int addrlen, res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 726 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 727 | return NULL; |
| 728 | if (!getsockaddrlen(s, &addrlen)) |
| 729 | return NULL; |
Guido van Rossum | ca42b16 | 1996-01-12 01:36:05 +0000 | [diff] [blame] | 730 | memset(addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 731 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 732 | res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 733 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 734 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 735 | return PySocket_Err(); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 736 | return makesockaddr((struct sockaddr *) addrbuf, addrlen); |
| 737 | } |
| 738 | |
| 739 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 740 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 741 | /* s.getpeername() method */ |
| 742 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 743 | static PyObject * |
| 744 | BUILD_FUNC_DEF_2(PySocketSock_getpeername,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 745 | { |
| 746 | char addrbuf[256]; |
| 747 | int addrlen, res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 748 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 749 | return NULL; |
| 750 | if (!getsockaddrlen(s, &addrlen)) |
| 751 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 752 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 753 | res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 754 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 755 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 756 | return PySocket_Err(); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 757 | return makesockaddr((struct sockaddr *) addrbuf, addrlen); |
| 758 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 759 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 760 | |
| 761 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 762 | /* s.listen(n) method */ |
| 763 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 764 | static PyObject * |
| 765 | BUILD_FUNC_DEF_2(PySocketSock_listen,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 766 | { |
| 767 | int backlog; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 768 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 769 | if (!PyArg_GetInt(args, &backlog)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 770 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 771 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 772 | if (backlog < 1) |
| 773 | backlog = 1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 774 | res = listen(s->sock_fd, backlog); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 775 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 776 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 777 | return PySocket_Err(); |
| 778 | Py_INCREF(Py_None); |
| 779 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 782 | #ifndef NO_DUP |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 783 | /* s.makefile(mode) method. |
| 784 | Create a new open file object referring to a dupped version of |
| 785 | the socket's file descriptor. (The dup() call is necessary so |
| 786 | that the open file and socket objects may be closed independent |
| 787 | of each other.) |
| 788 | The mode argument specifies 'r' or 'w' passed to fdopen(). */ |
| 789 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 790 | static PyObject * |
| 791 | BUILD_FUNC_DEF_2(PySocketSock_makefile,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 792 | { |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 793 | extern int fclose Py_PROTO((FILE *)); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 794 | char *mode = "r"; |
| 795 | int bufsize = -1; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 796 | int fd; |
| 797 | FILE *fp; |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 798 | PyObject *f; |
| 799 | |
| 800 | if (!PyArg_ParseTuple(args, "|si", &mode, &bufsize)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 801 | return NULL; |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 802 | #ifdef MS_WIN32 |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 803 | if ( ((fd = _open_osfhandle( s->sock_fd, _O_BINARY )) < 0) || |
| 804 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) { |
| 805 | #else |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 806 | if ((fd = dup(s->sock_fd)) < 0 || |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 807 | (fp = fdopen(fd, mode)) == NULL) { |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 808 | #endif |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 809 | if (fd >= 0) |
| 810 | close(fd); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 811 | return PySocket_Err(); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 812 | } |
| 813 | f = PyFile_FromFile(fp, "<socket>", mode, fclose); |
| 814 | if (f != NULL) |
| 815 | PyFile_SetBufSize(f, bufsize); |
| 816 | return f; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 817 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 818 | #endif /* NO_DUP */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 819 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 820 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 821 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 822 | static PyObject * |
| 823 | BUILD_FUNC_DEF_2(PySocketSock_recv,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 824 | { |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 825 | int len, n, flags = 0; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 826 | PyObject *buf; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 827 | if (!PyArg_ParseTuple(args, "i|i", &len, &flags)) |
| 828 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 829 | buf = PyString_FromStringAndSize((char *) 0, len); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 830 | if (buf == NULL) |
| 831 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 832 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 833 | n = recv(s->sock_fd, PyString_AsString(buf), len, flags); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 834 | Py_END_ALLOW_THREADS |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 835 | if (n < 0) { |
| 836 | Py_DECREF(buf); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 837 | return PySocket_Err(); |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 838 | } |
Guido van Rossum | 53a9bf8 | 1996-06-11 18:35:24 +0000 | [diff] [blame] | 839 | if (n != len && _PyString_Resize(&buf, n) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 840 | return NULL; |
| 841 | return buf; |
| 842 | } |
| 843 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 844 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 845 | /* s.recvfrom(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 846 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 847 | static PyObject * |
| 848 | BUILD_FUNC_DEF_2(PySocketSock_recvfrom,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 849 | { |
| 850 | char addrbuf[256]; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 851 | PyObject *buf = NULL; |
| 852 | PyObject *addr = NULL; |
| 853 | PyObject *ret = NULL; |
| 854 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 855 | int addrlen, len, n, flags = 0; |
| 856 | if (!PyArg_ParseTuple(args, "i|i", &len, &flags)) |
| 857 | return NULL; |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 858 | if (!getsockaddrlen(s, &addrlen)) |
| 859 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 860 | buf = PyString_FromStringAndSize((char *) 0, len); |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 861 | if (buf == NULL) |
| 862 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 863 | Py_BEGIN_ALLOW_THREADS |
| 864 | n = recvfrom(s->sock_fd, PyString_AsString(buf), len, flags, |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 865 | #ifndef MS_WINDOWS |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 866 | (ANY *)addrbuf, &addrlen |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 867 | #else |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 868 | (struct sockaddr *)addrbuf, &addrlen |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 869 | #endif |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 870 | ); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 871 | Py_END_ALLOW_THREADS |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 872 | if (n < 0) { |
| 873 | Py_DECREF(buf); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 874 | return PySocket_Err(); |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 875 | } |
Guido van Rossum | 53a9bf8 | 1996-06-11 18:35:24 +0000 | [diff] [blame] | 876 | if (n != len && _PyString_Resize(&buf, n) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 877 | return NULL; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 878 | |
| 879 | if (!(addr = makesockaddr((struct sockaddr *)addrbuf, addrlen))) |
| 880 | goto finally; |
| 881 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 882 | ret = Py_BuildValue("OO", buf, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 883 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 884 | Py_XDECREF(addr); |
| 885 | Py_XDECREF(buf); |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 886 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 889 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 890 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 891 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 892 | static PyObject * |
| 893 | BUILD_FUNC_DEF_2(PySocketSock_send,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 894 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 895 | char *buf; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 896 | int len, n, flags = 0; |
| 897 | if (!PyArg_ParseTuple(args, "s#|i", &buf, &len, &flags)) |
| 898 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 899 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 900 | n = send(s->sock_fd, buf, len, flags); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 901 | Py_END_ALLOW_THREADS |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 902 | if (n < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 903 | return PySocket_Err(); |
| 904 | return PyInt_FromLong((long)n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 908 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 909 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 910 | static PyObject * |
| 911 | BUILD_FUNC_DEF_2(PySocketSock_sendto,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 912 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 913 | PyObject *addro; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 914 | char *buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 915 | struct sockaddr *addr; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 916 | int addrlen, len, n, flags; |
| 917 | flags = 0; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 918 | if (!PyArg_Parse(args, "(s#O)", &buf, &len, &addro)) { |
| 919 | PyErr_Clear(); |
| 920 | if (!PyArg_Parse(args, "(s#iO)", &buf, &len, &flags, &addro)) |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 921 | return NULL; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 922 | } |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 923 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 924 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 925 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 926 | n = sendto(s->sock_fd, buf, len, flags, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 927 | Py_END_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 928 | if (n < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 929 | return PySocket_Err(); |
| 930 | return PyInt_FromLong((long)n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 933 | |
| 934 | /* s.shutdown(how) method */ |
| 935 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 936 | static PyObject * |
| 937 | BUILD_FUNC_DEF_2(PySocketSock_shutdown,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 938 | { |
| 939 | int how; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 940 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 941 | if (!PyArg_GetInt(args, &how)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 942 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 943 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 944 | res = shutdown(s->sock_fd, how); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 945 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 946 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 947 | return PySocket_Err(); |
| 948 | Py_INCREF(Py_None); |
| 949 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 952 | |
| 953 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 954 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 955 | static PyMethodDef PySocketSock_methods[] = { |
| 956 | {"accept", (PyCFunction)PySocketSock_accept}, |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 957 | {"setblocking", (PyCFunction)PySocketSock_setblocking}, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 958 | {"setsockopt", (PyCFunction)PySocketSock_setsockopt}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 959 | {"getsockopt", (PyCFunction)PySocketSock_getsockopt, 1}, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 960 | {"bind", (PyCFunction)PySocketSock_bind}, |
| 961 | {"close", (PyCFunction)PySocketSock_close}, |
| 962 | {"connect", (PyCFunction)PySocketSock_connect}, |
| 963 | {"fileno", (PyCFunction)PySocketSock_fileno}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 964 | #ifndef NO_DUP |
| 965 | {"dup", (PyCFunction)PySocketSock_dup}, |
| 966 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 967 | {"getsockname", (PyCFunction)PySocketSock_getsockname}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 968 | #ifdef HAVE_GETPEERNAME |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 969 | {"getpeername", (PyCFunction)PySocketSock_getpeername}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 970 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 971 | {"listen", (PyCFunction)PySocketSock_listen}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 972 | #ifndef NO_DUP |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 973 | {"makefile", (PyCFunction)PySocketSock_makefile, 1}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 974 | #endif |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 975 | {"recv", (PyCFunction)PySocketSock_recv, 1}, |
| 976 | {"recvfrom", (PyCFunction)PySocketSock_recvfrom, 1}, |
| 977 | {"send", (PyCFunction)PySocketSock_send, 1}, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 978 | {"sendto", (PyCFunction)PySocketSock_sendto}, |
| 979 | {"shutdown", (PyCFunction)PySocketSock_shutdown}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 980 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 981 | }; |
| 982 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 983 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 984 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 985 | First close the file description. */ |
| 986 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 987 | static void |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 988 | BUILD_FUNC_DEF_1(PySocketSock_dealloc,PySocketSockObject *,s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 989 | { |
| 990 | (void) close(s->sock_fd); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 991 | PyMem_DEL(s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 994 | |
| 995 | /* Return a socket object's named attribute. */ |
| 996 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 997 | static PyObject * |
| 998 | BUILD_FUNC_DEF_2(PySocketSock_getattr,PySocketSockObject *,s, char *,name) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 999 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1000 | return Py_FindMethod(PySocketSock_methods, (PyObject *) s, name); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1003 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1004 | static PyObject * |
| 1005 | BUILD_FUNC_DEF_1(PySocketSock_repr,PySocketSockObject *,s) |
| 1006 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1007 | char buf[512]; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1008 | sprintf(buf, |
| 1009 | "<socket object, fd=%d, family=%d, type=%d, protocol=%d>", |
| 1010 | s->sock_fd, s->sock_family, s->sock_type, s->sock_proto); |
Guido van Rossum | dc1c64a | 1996-02-27 15:21:19 +0000 | [diff] [blame] | 1011 | return PyString_FromString(buf); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1015 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1016 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1017 | static PyTypeObject PySocketSock_Type = { |
Guido van Rossum | 5555854 | 1996-05-23 22:54:50 +0000 | [diff] [blame] | 1018 | PyObject_HEAD_INIT(0) /* Must fill in type value later */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1019 | 0, |
| 1020 | "socket", |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1021 | sizeof(PySocketSockObject), |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1022 | 0, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1023 | (destructor)PySocketSock_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1024 | 0, /*tp_print*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1025 | (getattrfunc)PySocketSock_getattr, /*tp_getattr*/ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1026 | 0, /*tp_setattr*/ |
| 1027 | 0, /*tp_compare*/ |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1028 | (reprfunc)PySocketSock_repr, /*tp_repr*/ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1029 | 0, /*tp_as_number*/ |
| 1030 | 0, /*tp_as_sequence*/ |
| 1031 | 0, /*tp_as_mapping*/ |
| 1032 | }; |
| 1033 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1034 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1035 | /* Python interface to gethostname(). */ |
| 1036 | |
| 1037 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1038 | static PyObject * |
| 1039 | BUILD_FUNC_DEF_2(PySocket_gethostname,PyObject *,self, PyObject *,args) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1040 | { |
| 1041 | char buf[1024]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1042 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1043 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1044 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1045 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1046 | res = gethostname(buf, (int) sizeof buf - 1); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1047 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1048 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1049 | return PySocket_Err(); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1050 | buf[sizeof buf - 1] = '\0'; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1051 | return PyString_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1052 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1053 | |
| 1054 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1055 | /* Python interface to gethostbyname(name). */ |
| 1056 | |
| 1057 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1058 | static PyObject * |
| 1059 | BUILD_FUNC_DEF_2(PySocket_gethostbyname,PyObject *,self, PyObject *,args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1060 | { |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1061 | char *name; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1062 | struct sockaddr_in addrbuf; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1063 | if (!PyArg_Parse(args, "s", &name)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1064 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1065 | if (setipaddr(name, &addrbuf) < 0) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1066 | return NULL; |
| 1067 | return makeipaddr(&addrbuf); |
| 1068 | } |
| 1069 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1070 | /* Python interface to gethostbyaddr(IP). */ |
| 1071 | |
| 1072 | /*ARGSUSED*/ |
| 1073 | static PyObject * |
| 1074 | BUILD_FUNC_DEF_2(PySocket_gethostbyaddr,PyObject *,self, PyObject *, args) |
| 1075 | { |
| 1076 | struct sockaddr_in addr; |
| 1077 | char *ip_num; |
| 1078 | struct hostent *h; |
Guido van Rossum | dc1c64a | 1996-02-27 15:21:19 +0000 | [diff] [blame] | 1079 | char **pch; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1080 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 1081 | PyObject *name_list = (PyObject *)NULL; |
| 1082 | PyObject *addr_list = (PyObject *)NULL; |
| 1083 | PyObject *tmp; |
| 1084 | |
| 1085 | if (!PyArg_Parse(args, "s", &ip_num)) |
| 1086 | return NULL; |
| 1087 | if (setipaddr(ip_num, &addr) < 0) |
| 1088 | return NULL; |
| 1089 | h = gethostbyaddr((char *)&addr.sin_addr, |
| 1090 | sizeof(addr.sin_addr), |
| 1091 | AF_INET); |
| 1092 | if (h == NULL) { |
Guido van Rossum | ca9b323 | 1995-01-10 16:30:54 +0000 | [diff] [blame] | 1093 | #ifdef HAVE_HSTRERROR |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1094 | /* Let's get real error message to return */ |
| 1095 | extern int h_errno; |
| 1096 | PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno)); |
| 1097 | #else |
| 1098 | PyErr_SetString(PySocket_Error, "host not found"); |
| 1099 | #endif |
| 1100 | return NULL; |
| 1101 | } |
| 1102 | if ((name_list = PyList_New(0)) == NULL) |
| 1103 | goto err; |
| 1104 | if ((addr_list = PyList_New(0)) == NULL) |
| 1105 | goto err; |
| 1106 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1107 | int status; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1108 | tmp = PyString_FromString(*pch); |
| 1109 | if (tmp == NULL) |
| 1110 | goto err; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1111 | status = PyList_Append(name_list, tmp); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1112 | Py_DECREF(tmp); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1113 | if (status) |
| 1114 | goto err; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1115 | } |
| 1116 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1117 | int status; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1118 | memcpy((char *) &addr.sin_addr, *pch, h->h_length); |
| 1119 | tmp = makeipaddr(&addr); |
| 1120 | if (tmp == NULL) |
| 1121 | goto err; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1122 | status = PyList_Append(addr_list, tmp); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1123 | Py_DECREF(tmp); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1124 | if (status) |
| 1125 | goto err; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1126 | } |
| 1127 | rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list); |
| 1128 | err: |
| 1129 | Py_XDECREF(name_list); |
| 1130 | Py_XDECREF(addr_list); |
| 1131 | return rtn_tuple; |
| 1132 | } |
| 1133 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1134 | |
| 1135 | /* Python interface to getservbyname(name). |
| 1136 | This only returns the port number, since the other info is already |
| 1137 | known or not useful (like the list of aliases). */ |
| 1138 | |
| 1139 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1140 | static PyObject * |
| 1141 | BUILD_FUNC_DEF_2(PySocket_getservbyname,PyObject *,self, PyObject *,args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1142 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1143 | char *name, *proto; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1144 | struct servent *sp; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1145 | if (!PyArg_Parse(args, "(ss)", &name, &proto)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1146 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1147 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1148 | sp = getservbyname(name, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1149 | Py_END_ALLOW_THREADS |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1150 | if (sp == NULL) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1151 | PyErr_SetString(PySocket_Error, "service/proto not found"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1152 | return NULL; |
| 1153 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1154 | return PyInt_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 1158 | /* Python interface to getprotobyname(name). |
| 1159 | This only returns the protocol number, since the other info is |
| 1160 | already known or not useful (like the list of aliases). */ |
| 1161 | |
| 1162 | /*ARGSUSED*/ |
| 1163 | static PyObject * |
| 1164 | BUILD_FUNC_DEF_2(PySocket_getprotobyname,PyObject *,self, PyObject *,args) |
| 1165 | { |
| 1166 | char *name; |
| 1167 | struct protoent *sp; |
| 1168 | if (!PyArg_Parse(args, "s", &name)) |
| 1169 | return NULL; |
| 1170 | Py_BEGIN_ALLOW_THREADS |
| 1171 | sp = getprotobyname(name); |
| 1172 | Py_END_ALLOW_THREADS |
| 1173 | if (sp == NULL) { |
| 1174 | PyErr_SetString(PySocket_Error, "protocol not found"); |
| 1175 | return NULL; |
| 1176 | } |
| 1177 | return PyInt_FromLong((long) sp->p_proto); |
| 1178 | } |
| 1179 | |
| 1180 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1181 | /* Python interface to socket(family, type, proto). |
| 1182 | The third (protocol) argument is optional. |
| 1183 | Return a new socket object. */ |
| 1184 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1185 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1186 | static PyObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1187 | BUILD_FUNC_DEF_2(PySocket_socket,PyObject *,self, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1188 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1189 | PySocketSockObject *s; |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 1190 | #ifdef MS_WINDOWS |
Guido van Rossum | d639d4d | 1996-06-12 04:04:22 +0000 | [diff] [blame] | 1191 | SOCKET fd; |
| 1192 | #else |
| 1193 | int fd; |
| 1194 | #endif |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1195 | int family, type, proto = 0; |
| 1196 | if (!PyArg_ParseTuple(args, "ii|i", &family, &type, &proto)) |
| 1197 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1198 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1199 | fd = socket(family, type, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1200 | Py_END_ALLOW_THREADS |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 1201 | #ifdef MS_WINDOWS |
Guido van Rossum | d639d4d | 1996-06-12 04:04:22 +0000 | [diff] [blame] | 1202 | if (fd == INVALID_SOCKET) |
| 1203 | #else |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1204 | if (fd < 0) |
Guido van Rossum | d639d4d | 1996-06-12 04:04:22 +0000 | [diff] [blame] | 1205 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1206 | return PySocket_Err(); |
| 1207 | s = PySocketSock_New(fd, family, type, proto); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1208 | /* If the object can't be created, don't forget to close the |
| 1209 | file descriptor again! */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1210 | if (s == NULL) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1211 | (void) close(fd); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1212 | /* From now on, ignore SIGPIPE and let the error checking |
| 1213 | do the work. */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1214 | #ifdef SIGPIPE |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1215 | (void) signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1216 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1217 | return (PyObject *) s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1220 | #ifndef NO_DUP |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1221 | /* Create a socket object from a numeric file description. |
| 1222 | Useful e.g. if stdin is a socket. |
| 1223 | Additional arguments as for socket(). */ |
| 1224 | |
| 1225 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1226 | static PyObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1227 | BUILD_FUNC_DEF_2(PySocket_fromfd,PyObject *,self, PyObject *,args) |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1228 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1229 | PySocketSockObject *s; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1230 | int fd, family, type, proto = 0; |
| 1231 | if (!PyArg_ParseTuple(args, "iii|i", &fd, &family, &type, &proto)) |
| 1232 | return NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1233 | /* Dup the fd so it and the socket can be closed independently */ |
| 1234 | fd = dup(fd); |
| 1235 | if (fd < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1236 | return PySocket_Err(); |
| 1237 | s = PySocketSock_New(fd, family, type, proto); |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1238 | /* From now on, ignore SIGPIPE and let the error checking |
| 1239 | do the work. */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1240 | #ifdef SIGPIPE |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1241 | (void) signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1242 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1243 | return (PyObject *) s; |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1244 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1245 | #endif /* NO_DUP */ |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1246 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1247 | static PyObject * |
| 1248 | BUILD_FUNC_DEF_2(PySocket_ntohs, PyObject *, self, PyObject *, args) |
| 1249 | { |
| 1250 | int x1, x2; |
| 1251 | |
| 1252 | if (!PyArg_Parse(args, "i", &x1)) { |
| 1253 | return NULL; |
| 1254 | } |
| 1255 | x2 = (int)ntohs((short)x1); |
| 1256 | return PyInt_FromLong(x2); |
| 1257 | } |
| 1258 | |
| 1259 | static PyObject * |
| 1260 | BUILD_FUNC_DEF_2(PySocket_ntohl, PyObject *, self, PyObject *, args) |
| 1261 | { |
| 1262 | int x1, x2; |
| 1263 | |
| 1264 | if (!PyArg_Parse(args, "i", &x1)) { |
| 1265 | return NULL; |
| 1266 | } |
| 1267 | x2 = ntohl(x1); |
| 1268 | return PyInt_FromLong(x2); |
| 1269 | } |
| 1270 | |
| 1271 | static PyObject * |
| 1272 | BUILD_FUNC_DEF_2(PySocket_htons, PyObject *, self, PyObject *, args) |
| 1273 | { |
| 1274 | int x1, x2; |
| 1275 | |
| 1276 | if (!PyArg_Parse(args, "i", &x1)) { |
| 1277 | return NULL; |
| 1278 | } |
| 1279 | x2 = (int)htons((short)x1); |
| 1280 | return PyInt_FromLong(x2); |
| 1281 | } |
| 1282 | |
| 1283 | static PyObject * |
| 1284 | BUILD_FUNC_DEF_2(PySocket_htonl, PyObject *, self, PyObject *, args) |
| 1285 | { |
| 1286 | int x1, x2; |
| 1287 | |
| 1288 | if (!PyArg_Parse(args, "i", &x1)) { |
| 1289 | return NULL; |
| 1290 | } |
| 1291 | x2 = htonl(x1); |
| 1292 | return PyInt_FromLong(x2); |
| 1293 | } |
| 1294 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1295 | /* List of functions exported by this module. */ |
| 1296 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1297 | static PyMethodDef PySocket_methods[] = { |
| 1298 | {"gethostbyname", PySocket_gethostbyname}, |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1299 | {"gethostbyaddr", PySocket_gethostbyaddr}, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1300 | {"gethostname", PySocket_gethostname}, |
| 1301 | {"getservbyname", PySocket_getservbyname}, |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 1302 | {"getprotobyname", PySocket_getprotobyname}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1303 | {"socket", PySocket_socket, 1}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1304 | #ifndef NO_DUP |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1305 | {"fromfd", PySocket_fromfd, 1}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1306 | #endif |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1307 | {"ntohs", PySocket_ntohs}, |
| 1308 | {"ntohl", PySocket_ntohl}, |
| 1309 | {"htons", PySocket_htons}, |
| 1310 | {"htonl", PySocket_htonl}, |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1311 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1312 | }; |
| 1313 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1314 | |
| 1315 | /* Convenience routine to export an integer value. |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1316 | * |
| 1317 | * Since this function is called only from initsocket/init_socket(), any |
| 1318 | * errors trigger a fatal exception. |
| 1319 | */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1320 | static void |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1321 | BUILD_FUNC_DEF_3(insint,PyObject *,d, char *,name, int,value) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1322 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1323 | PyObject *v = PyInt_FromLong((long) value); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1324 | if (!v || PyDict_SetItemString(d, name, v)) |
| 1325 | Py_FatalError("can't initialize socket module"); |
| 1326 | |
| 1327 | Py_DECREF(v); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1330 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1331 | #ifdef MS_WINDOWS |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1332 | |
| 1333 | /* Additional initialization and cleanup for NT/Windows */ |
| 1334 | |
| 1335 | static void |
| 1336 | NTcleanup() |
| 1337 | { |
| 1338 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | static int |
| 1342 | NTinit() |
| 1343 | { |
| 1344 | WSADATA WSAData; |
| 1345 | int ret; |
| 1346 | char buf[100]; |
| 1347 | ret = WSAStartup(0x0101, &WSAData); |
| 1348 | switch (ret) { |
| 1349 | case 0: /* no error */ |
| 1350 | atexit(NTcleanup); |
| 1351 | return 1; |
| 1352 | case WSASYSNOTREADY: |
| 1353 | PyErr_SetString(PyExc_ImportError, |
| 1354 | "WSAStartup failed: network not ready"); |
| 1355 | break; |
| 1356 | case WSAVERNOTSUPPORTED: |
| 1357 | case WSAEINVAL: |
| 1358 | PyErr_SetString(PyExc_ImportError, |
| 1359 | "WSAStartup failed: requested version not supported"); |
| 1360 | break; |
| 1361 | default: |
| 1362 | sprintf(buf, "WSAStartup failed: error code %d", ret); |
| 1363 | PyErr_SetString(PyExc_ImportError, buf); |
| 1364 | break; |
| 1365 | } |
| 1366 | return 0; |
| 1367 | } |
| 1368 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1369 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1370 | |
| 1371 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1372 | /* Initialize this module. |
| 1373 | This is called when the first 'import socket' is done, |
| 1374 | via a table in config.c, if config.c is compiled with USE_SOCKET |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1375 | defined. |
| 1376 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1377 | For MS_WINDOWS (which means any Windows variant), this module |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1378 | is actually called "_socket", and there's a wrapper "socket.py" |
| 1379 | which implements some missing functionality (such as makefile(), |
| 1380 | dup() and fromfd()). The import of "_socket" may fail with an |
| 1381 | ImportError exception if initialization of WINSOCK fails. When |
| 1382 | WINSOCK is initialized succesfully, a call to WSACleanup() is |
| 1383 | scheduled to be made at exit time. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1384 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1385 | void |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1386 | #ifdef MS_WINDOWS |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1387 | init_socket() |
| 1388 | #else |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1389 | initsocket() |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1390 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1391 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1392 | PyObject *m, *d; |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1393 | #ifdef MS_WINDOWS |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1394 | if (!NTinit()) |
| 1395 | return; |
| 1396 | m = Py_InitModule("_socket", PySocket_methods); |
| 1397 | #else |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1398 | m = Py_InitModule("socket", PySocket_methods); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1399 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1400 | d = PyModule_GetDict(m); |
| 1401 | PySocket_Error = PyString_FromString("socket.error"); |
| 1402 | if (PySocket_Error == NULL || |
| 1403 | PyDict_SetItemString(d, "error", PySocket_Error) != 0) |
| 1404 | Py_FatalError("can't define socket.error"); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1405 | insint(d, "AF_INET", AF_INET); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1406 | #ifdef AF_UNIX |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1407 | insint(d, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1408 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1409 | insint(d, "SOCK_STREAM", SOCK_STREAM); |
| 1410 | insint(d, "SOCK_DGRAM", SOCK_DGRAM); |
| 1411 | insint(d, "SOCK_RAW", SOCK_RAW); |
| 1412 | insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
| 1413 | insint(d, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 1414 | |
| 1415 | #ifdef SO_DEBUG |
| 1416 | insint(d, "SO_DEBUG", SO_DEBUG); |
| 1417 | #endif |
| 1418 | #ifdef SO_ACCEPTCONN |
| 1419 | insint(d, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
| 1420 | #endif |
| 1421 | #ifdef SO_REUSEADDR |
| 1422 | insint(d, "SO_REUSEADDR", SO_REUSEADDR); |
| 1423 | #endif |
| 1424 | #ifdef SO_KEEPALIVE |
| 1425 | insint(d, "SO_KEEPALIVE", SO_KEEPALIVE); |
| 1426 | #endif |
| 1427 | #ifdef SO_DONTROUTE |
| 1428 | insint(d, "SO_DONTROUTE", SO_DONTROUTE); |
| 1429 | #endif |
| 1430 | #ifdef SO_BROADCAST |
| 1431 | insint(d, "SO_BROADCAST", SO_BROADCAST); |
| 1432 | #endif |
| 1433 | #ifdef SO_USELOOPBACK |
| 1434 | insint(d, "SO_USELOOPBACK", SO_USELOOPBACK); |
| 1435 | #endif |
| 1436 | #ifdef SO_LINGER |
| 1437 | insint(d, "SO_LINGER", SO_LINGER); |
| 1438 | #endif |
| 1439 | #ifdef SO_OOBINLINE |
| 1440 | insint(d, "SO_OOBINLINE", SO_OOBINLINE); |
| 1441 | #endif |
| 1442 | #ifdef SO_REUSEPORT |
| 1443 | insint(d, "SO_REUSEPORT", SO_REUSEPORT); |
| 1444 | #endif |
| 1445 | |
| 1446 | #ifdef SO_SNDBUF |
| 1447 | insint(d, "SO_SNDBUF", SO_SNDBUF); |
| 1448 | #endif |
| 1449 | #ifdef SO_RCVBUF |
| 1450 | insint(d, "SO_RCVBUF", SO_RCVBUF); |
| 1451 | #endif |
| 1452 | #ifdef SO_SNDLOWAT |
| 1453 | insint(d, "SO_SNDLOWAT", SO_SNDLOWAT); |
| 1454 | #endif |
| 1455 | #ifdef SO_RCVLOWAT |
| 1456 | insint(d, "SO_RCVLOWAT", SO_RCVLOWAT); |
| 1457 | #endif |
| 1458 | #ifdef SO_SNDTIMEO |
| 1459 | insint(d, "SO_SNDTIMEO", SO_SNDTIMEO); |
| 1460 | #endif |
| 1461 | #ifdef SO_RCVTIMEO |
| 1462 | insint(d, "SO_RCVTIMEO", SO_RCVTIMEO); |
| 1463 | #endif |
| 1464 | #ifdef SO_ERROR |
| 1465 | insint(d, "SO_ERROR", SO_ERROR); |
| 1466 | #endif |
| 1467 | #ifdef SO_TYPE |
| 1468 | insint(d, "SO_TYPE", SO_TYPE); |
| 1469 | #endif |
| 1470 | |
| 1471 | /* Maximum number of connections for "listen" */ |
| 1472 | #ifdef SOMAXCONN |
| 1473 | insint(d, "SOMAXCONN", SOMAXCONN); |
| 1474 | #else |
| 1475 | insint(d, "SOMAXCONN", 5); /* Common value */ |
| 1476 | #endif |
| 1477 | |
| 1478 | /* Flags for send, recv */ |
| 1479 | #ifdef MSG_OOB |
| 1480 | insint(d, "MSG_OOB", MSG_OOB); |
| 1481 | #endif |
| 1482 | #ifdef MSG_PEEK |
| 1483 | insint(d, "MSG_PEEK", MSG_PEEK); |
| 1484 | #endif |
| 1485 | #ifdef MSG_DONTROUTE |
| 1486 | insint(d, "MSG_DONTROUTE", MSG_DONTROUTE); |
| 1487 | #endif |
| 1488 | #ifdef MSG_EOR |
| 1489 | insint(d, "MSG_EOR", MSG_EOR); |
| 1490 | #endif |
| 1491 | #ifdef MSG_TRUNC |
| 1492 | insint(d, "MSG_TRUNC", MSG_TRUNC); |
| 1493 | #endif |
| 1494 | #ifdef MSG_CTRUNC |
| 1495 | insint(d, "MSG_CTRUNC", MSG_CTRUNC); |
| 1496 | #endif |
| 1497 | #ifdef MSG_WAITALL |
| 1498 | insint(d, "MSG_WAITALL", MSG_WAITALL); |
| 1499 | #endif |
| 1500 | #ifdef MSG_BTAG |
| 1501 | insint(d, "MSG_BTAG", MSG_BTAG); |
| 1502 | #endif |
| 1503 | #ifdef MSG_ETAG |
| 1504 | insint(d, "MSG_ETAG", MSG_ETAG); |
| 1505 | #endif |
| 1506 | |
| 1507 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 1508 | #ifdef SOL_SOCKET |
| 1509 | insint(d, "SOL_SOCKET", SOL_SOCKET); |
| 1510 | #endif |
| 1511 | #ifdef IPPROTO_IP |
| 1512 | insint(d, "IPPROTO_IP", IPPROTO_IP); |
| 1513 | #endif |
| 1514 | #ifdef IPPROTO_ICMP |
| 1515 | insint(d, "IPPROTO_ICMP", IPPROTO_ICMP); |
| 1516 | #endif |
| 1517 | #ifdef IPPROTO_IGMP |
| 1518 | insint(d, "IPPROTO_IGMP", IPPROTO_IGMP); |
| 1519 | #endif |
| 1520 | #ifdef IPPROTO_GGP |
| 1521 | insint(d, "IPPROTO_GGP", IPPROTO_GGP); |
| 1522 | #endif |
| 1523 | #ifdef IPPROTO_TCP |
| 1524 | insint(d, "IPPROTO_TCP", IPPROTO_TCP); |
| 1525 | #endif |
| 1526 | #ifdef IPPROTO_EGP |
| 1527 | insint(d, "IPPROTO_EGP", IPPROTO_EGP); |
| 1528 | #endif |
| 1529 | #ifdef IPPROTO_PUP |
| 1530 | insint(d, "IPPROTO_PUP", IPPROTO_PUP); |
| 1531 | #endif |
| 1532 | #ifdef IPPROTO_UDP |
| 1533 | insint(d, "IPPROTO_UDP", IPPROTO_UDP); |
| 1534 | #endif |
| 1535 | #ifdef IPPROTO_IDP |
| 1536 | insint(d, "IPPROTO_IDP", IPPROTO_IDP); |
| 1537 | #endif |
| 1538 | #ifdef IPPROTO_HELLO |
| 1539 | insint(d, "IPPROTO_HELLO", IPPROTO_HELLO); |
| 1540 | #endif |
| 1541 | #ifdef IPPROTO_ND |
| 1542 | insint(d, "IPPROTO_ND", IPPROTO_ND); |
| 1543 | #endif |
| 1544 | #ifdef IPPROTO_TP |
| 1545 | insint(d, "IPPROTO_TP", IPPROTO_TP); |
| 1546 | #endif |
| 1547 | #ifdef IPPROTO_XTP |
| 1548 | insint(d, "IPPROTO_XTP", IPPROTO_XTP); |
| 1549 | #endif |
| 1550 | #ifdef IPPROTO_EON |
| 1551 | insint(d, "IPPROTO_EON", IPPROTO_EON); |
| 1552 | #endif |
| 1553 | #ifdef IPPROTO_BIP |
| 1554 | insint(d, "IPPROTO_BIP", IPPROTO_BIP); |
| 1555 | #endif |
| 1556 | /**/ |
| 1557 | #ifdef IPPROTO_RAW |
| 1558 | insint(d, "IPPROTO_RAW", IPPROTO_RAW); |
| 1559 | #endif |
| 1560 | #ifdef IPPROTO_MAX |
| 1561 | insint(d, "IPPROTO_MAX", IPPROTO_MAX); |
| 1562 | #endif |
| 1563 | |
| 1564 | /* Some port configuration */ |
| 1565 | #ifdef IPPORT_RESERVED |
| 1566 | insint(d, "IPPORT_RESERVED", IPPORT_RESERVED); |
| 1567 | #else |
| 1568 | insint(d, "IPPORT_RESERVED", 1024); |
| 1569 | #endif |
| 1570 | #ifdef IPPORT_USERRESERVED |
| 1571 | insint(d, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
| 1572 | #else |
| 1573 | insint(d, "IPPORT_USERRESERVED", 5000); |
| 1574 | #endif |
| 1575 | |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 1576 | /* Some reserved IP v.4 addresses */ |
| 1577 | #ifdef INADDR_ANY |
| 1578 | insint(d, "INADDR_ANY", INADDR_ANY); |
| 1579 | #else |
| 1580 | insint(d, "INADDR_ANY", 0x00000000); |
| 1581 | #endif |
| 1582 | #ifdef INADDR_BROADCAST |
| 1583 | insint(d, "INADDR_BROADCAST", INADDR_BROADCAST); |
| 1584 | #else |
| 1585 | insint(d, "INADDR_BROADCAST", 0xffffffff); |
| 1586 | #endif |
| 1587 | #ifdef INADDR_LOOPBACK |
| 1588 | insint(d, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
| 1589 | #else |
| 1590 | insint(d, "INADDR_LOOPBACK", 0x7F000001); |
| 1591 | #endif |
| 1592 | #ifdef INADDR_UNSPEC_GROUP |
| 1593 | insint(d, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
| 1594 | #else |
| 1595 | insint(d, "INADDR_UNSPEC_GROUP", 0xe0000000); |
| 1596 | #endif |
| 1597 | #ifdef INADDR_ALLHOSTS_GROUP |
| 1598 | insint(d, "INADDR_ALLHOSTS_GROUP", INADDR_ALLHOSTS_GROUP); |
| 1599 | #else |
| 1600 | insint(d, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
| 1601 | #endif |
| 1602 | #ifdef INADDR_MAX_LOCAL_GROUP |
| 1603 | insint(d, "INADDR_MAX_LOCAL_GROUP", INADDR_MAX_LOCAL_GROUP); |
| 1604 | #else |
| 1605 | insint(d, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
| 1606 | #endif |
| 1607 | #ifdef INADDR_NONE |
| 1608 | insint(d, "INADDR_NONE", INADDR_NONE); |
| 1609 | #else |
| 1610 | insint(d, "INADDR_NONE", 0xffffffff); |
| 1611 | #endif |
| 1612 | |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 1613 | /* IP [gs]etsockopt options */ |
| 1614 | #ifdef IP_OPTIONS |
| 1615 | insint(d, "IP_OPTIONS", IP_OPTIONS); |
| 1616 | #endif |
| 1617 | #ifdef IP_HDRINCL |
| 1618 | insint(d, "IP_HDRINCL", IP_HDRINCL); |
| 1619 | #endif |
| 1620 | #ifdef IP_TOS |
| 1621 | insint(d, "IP_TOS", IP_TOS); |
| 1622 | #endif |
| 1623 | #ifdef IP_TTL |
| 1624 | insint(d, "IP_TTL", IP_TTL); |
| 1625 | #endif |
| 1626 | #ifdef IP_RECVOPTS |
| 1627 | insint(d, "IP_RECVOPTS", IP_RECVOPTS); |
| 1628 | #endif |
| 1629 | #ifdef IP_RECVRETOPTS |
| 1630 | insint(d, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
| 1631 | #endif |
| 1632 | #ifdef IP_RECVDSTADDR |
| 1633 | insint(d, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
| 1634 | #endif |
| 1635 | #ifdef IP_RETOPTS |
| 1636 | insint(d, "IP_RETOPTS", IP_RETOPTS); |
| 1637 | #endif |
| 1638 | #ifdef IP_MULTICAST_IF |
| 1639 | insint(d, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
| 1640 | #endif |
| 1641 | #ifdef IP_MULTICAST_TTL |
| 1642 | insint(d, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
| 1643 | #endif |
| 1644 | #ifdef IP_MULTICAST_LOOP |
| 1645 | insint(d, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
| 1646 | #endif |
| 1647 | #ifdef IP_ADD_MEMBERSHIP |
| 1648 | insint(d, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
| 1649 | #endif |
| 1650 | #ifdef IP_DROP_MEMBERSHIP |
| 1651 | insint(d, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
| 1652 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1653 | } |