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