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