Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
| 11 | /* Socket module */ |
| 12 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 13 | /* SSL support based on patches by Brian E Gallew and Laszlo Kovacs */ |
| 14 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 15 | /* |
| 16 | This module provides an interface to Berkeley socket IPC. |
| 17 | |
| 18 | Limitations: |
| 19 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 20 | - only AF_INET and AF_UNIX address families are supported |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 21 | - no read/write operations (use send/recv or makefile instead) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 22 | - additional restrictions apply on Windows |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 24 | Module interface: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 26 | - socket.error: exception raised for socket specific errors |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 27 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 28 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 29 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') |
Guido van Rossum | 25405c7 | 1996-12-19 16:42:52 +0000 | [diff] [blame] | 30 | - socket.getprotobyname(protocolname) --> protocol number |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 31 | - socket.getservbyname(servicename, protocolname) --> port number |
Guido van Rossum | 4dd2a7e | 1991-07-01 18:51:33 +0000 | [diff] [blame] | 32 | - socket.socket(family, type [, proto]) --> new socket object |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 33 | - socket.ntohs(16 bit value) --> new int object |
| 34 | - socket.ntohl(32 bit value) --> new int object |
| 35 | - socket.htons(16 bit value) --> new int object |
| 36 | - socket.htonl(32 bit value) --> new int object |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 37 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 38 | - socket.inet_aton(IP address) -> 32-bit packed IP representation |
| 39 | - socket.inet_ntoa(packed IP) -> IP address string |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 40 | - socket.ssl(socket, keyfile, certfile) -> new ssl object |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 41 | - an Internet socket address is a pair (hostname, port) |
| 42 | where hostname can be anything recognized by gethostbyname() |
| 43 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 44 | - 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] | 45 | - a UNIX domain socket address is a string specifying the pathname |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 47 | Socket methods: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 49 | - s.accept() --> new socket object, sockaddr |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 50 | - s.bind(sockaddr) --> None |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 51 | - s.close() --> None |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 52 | - s.connect(sockaddr) --> None |
| 53 | - 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] | 54 | - s.fileno() --> file descriptor |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 55 | - s.dup() --> same as socket.fromfd(os.dup(s.fileno(), ...) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 56 | - s.getpeername() --> sockaddr |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 57 | - s.getsockname() --> sockaddr |
| 58 | - s.getsockopt(level, optname[, buflen]) --> int or string |
| 59 | - s.listen(backlog) --> None |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 60 | - s.makefile([mode[, bufsize]]) --> file object |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 61 | - s.recv(buflen [,flags]) --> string |
| 62 | - s.recvfrom(buflen [,flags]) --> string, sockaddr |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 63 | - s.send(string [,flags]) --> nbytes |
| 64 | - s.sendto(string, [flags,] sockaddr) --> nbytes |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 65 | - s.setblocking(0 | 1) --> None |
| 66 | - s.setsockopt(level, optname, value) --> None |
| 67 | - s.shutdown(how) --> None |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 68 | - 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] | 69 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 70 | */ |
| 71 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 72 | #include "Python.h" |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 74 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 75 | script doesn't get this right, so we hardcode some platform checks below. |
| 76 | On the other hand, not all Linux versions agree, so there the settings |
| 77 | computed by the configure script are needed! */ |
| 78 | |
| 79 | #ifndef linux |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 80 | #undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 81 | #undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 82 | #undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 83 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 85 | #ifndef WITH_THREAD |
| 86 | #undef HAVE_GETHOSTBYNAME_R |
| 87 | #endif |
| 88 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 89 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 90 | #if defined(_AIX) || defined(__osf__) |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 91 | #define HAVE_GETHOSTBYNAME_R_3_ARG |
| 92 | #elif defined(__sun__) || defined(__sgi) |
| 93 | #define HAVE_GETHOSTBYNAME_R_5_ARG |
| 94 | #elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 95 | /* Rely on the configure script */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 96 | #else |
| 97 | #undef HAVE_GETHOSTBYNAME_R |
| 98 | #endif |
| 99 | #endif |
| 100 | |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 101 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && !defined(MS_WINDOWS) |
| 102 | #define USE_GETHOSTBYNAME_LOCK |
| 103 | #endif |
| 104 | |
| 105 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 106 | #include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 107 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 109 | #ifdef HAVE_UNISTD_H |
| 110 | #include <unistd.h> |
| 111 | #endif |
| 112 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 113 | #if !defined(MS_WINDOWS) && !defined(PYOS_OS2) && !defined(__BEOS__) |
Guido van Rossum | fb4130d | 1996-12-10 15:17:08 +0000 | [diff] [blame] | 114 | extern int gethostname(); /* For Solaris, at least */ |
| 115 | #endif |
| 116 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 117 | #if defined(PYCC_VACPP) |
| 118 | #include <types.h> |
| 119 | #include <io.h> |
| 120 | #include <sys/ioctl.h> |
| 121 | #include <utils.h> |
| 122 | #include <ctype.h> |
| 123 | #endif |
| 124 | |
| 125 | #if defined(PYOS_OS2) |
| 126 | #define INCL_DOS |
| 127 | #define INCL_DOSERRORS |
| 128 | #define INCL_NOPMAPI |
| 129 | #include <os2.h> |
| 130 | #endif |
| 131 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 132 | #if defined(__BEOS__) |
| 133 | /* It's in the libs, but not the headers... - [cjh] */ |
| 134 | int shutdown( int, int ); |
| 135 | #endif |
| 136 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 137 | #include <sys/types.h> |
| 138 | #include "mytime.h" |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 140 | #include <signal.h> |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 141 | #ifndef MS_WINDOWS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 142 | #include <netdb.h> |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 143 | #include <sys/socket.h> |
| 144 | #include <netinet/in.h> |
Fred Drake | 11b0936 | 2000-05-16 13:30:12 +0000 | [diff] [blame] | 145 | #ifndef __BEOS__ |
Andrew M. Kuchling | 52a644c | 2000-03-31 17:24:30 +0000 | [diff] [blame] | 146 | #include <netinet/tcp.h> |
Fred Drake | 11b0936 | 2000-05-16 13:30:12 +0000 | [diff] [blame] | 147 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 149 | /* Headers needed for inet_ntoa() and inet_addr() */ |
| 150 | #ifdef __BEOS__ |
| 151 | #include <net/netdb.h> |
| 152 | #else |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 153 | #ifndef USE_GUSI1 |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 154 | #include <arpa/inet.h> |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 155 | #endif |
Guido van Rossum | 1b6e463 | 1999-11-04 18:22:29 +0000 | [diff] [blame] | 156 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 157 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 158 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 159 | #else |
| 160 | #include <winsock.h> |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 161 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 162 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 163 | #ifdef HAVE_SYS_UN_H |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 164 | #include <sys/un.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 165 | #else |
| 166 | #undef AF_UNIX |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 167 | #endif |
| 168 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 169 | #ifndef O_NDELAY |
| 170 | #define O_NDELAY O_NONBLOCK /* For QNX only? */ |
| 171 | #endif |
| 172 | |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 173 | #ifdef USE_GUSI1 |
Jack Jansen | 508537b | 1996-02-14 15:57:45 +0000 | [diff] [blame] | 174 | /* fdopen() isn't declared in stdio.h (sigh) */ |
| 175 | #include <GUSI.h> |
| 176 | #endif |
| 177 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 178 | #ifdef USE_SSL |
| 179 | #include "rsa.h" |
| 180 | #include "crypto.h" |
| 181 | #include "x509.h" |
| 182 | #include "pem.h" |
| 183 | #include "ssl.h" |
| 184 | #include "err.h" |
| 185 | #endif /* USE_SSL */ |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 187 | #if defined(MS_WINDOWS) || defined(__BEOS__) |
| 188 | /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 189 | /* seem to be a few differences in the API */ |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 190 | #define SOCKETCLOSE closesocket |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 191 | #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] | 192 | #endif |
| 193 | |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 194 | /* abstract the socket file descriptor type */ |
| 195 | #ifdef MS_WINDOWS |
| 196 | typedef SOCKET SOCKET_T; |
| 197 | # ifdef MS_WIN64 |
| 198 | # define SIZEOF_SOCKET_T 8 |
| 199 | # else |
| 200 | # define SIZEOF_SOCKET_T 4 |
| 201 | # endif |
| 202 | #else |
| 203 | typedef int SOCKET_T; |
| 204 | # define SIZEOF_SOCKET_T SIZEOF_INT |
| 205 | #endif |
| 206 | |
| 207 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 208 | #if defined(PYOS_OS2) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 209 | #define SOCKETCLOSE soclose |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 210 | #define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 211 | #endif |
| 212 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 213 | #ifndef SOCKETCLOSE |
| 214 | #define SOCKETCLOSE close |
| 215 | #endif |
| 216 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 217 | /* Global variable holding the exception type for errors detected |
| 218 | by this module (but not argument type or memory errors, etc.). */ |
| 219 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 220 | static PyObject *PySocket_Error; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 222 | #ifdef USE_SSL |
| 223 | static PyObject *SSLErrorObject; |
| 224 | #endif /* USE_SSL */ |
| 225 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 226 | |
| 227 | /* Convenience function to raise an error according to errno |
| 228 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 230 | static PyObject * |
| 231 | PySocket_Err() |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 232 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 233 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 234 | if (WSAGetLastError()) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 235 | PyObject *v; |
| 236 | v = Py_BuildValue("(is)", WSAGetLastError(), "winsock error"); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 237 | if (v != NULL) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 238 | PyErr_SetObject(PySocket_Error, v); |
| 239 | Py_DECREF(v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 240 | } |
| 241 | return NULL; |
| 242 | } |
| 243 | else |
| 244 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 245 | |
| 246 | #if defined(PYOS_OS2) |
| 247 | if (sock_errno() != NO_ERROR) { |
| 248 | APIRET rc; |
| 249 | ULONG msglen; |
| 250 | char outbuf[100]; |
| 251 | int myerrorcode = sock_errno(); |
| 252 | |
| 253 | /* Retrieve Socket-Related Error Message from MPTN.MSG File */ |
| 254 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), |
| 255 | myerrorcode - SOCBASEERR + 26, "mptn.msg", &msglen); |
| 256 | if (rc == NO_ERROR) { |
| 257 | PyObject *v; |
| 258 | |
| 259 | outbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 260 | if (strlen(outbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 261 | char *lastc = &outbuf[ strlen(outbuf)-1 ]; |
| 262 | while (lastc > outbuf && isspace(*lastc)) |
| 263 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 264 | } |
| 265 | v = Py_BuildValue("(is)", myerrorcode, outbuf); |
| 266 | if (v != NULL) { |
| 267 | PyErr_SetObject(PySocket_Error, v); |
| 268 | Py_DECREF(v); |
| 269 | } |
| 270 | return NULL; |
| 271 | } |
| 272 | } |
| 273 | #endif |
| 274 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 275 | return PyErr_SetFromErrno(PySocket_Error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 278 | |
| 279 | /* The object holding a socket. It holds some extra information, |
| 280 | like the address family, which is used to decode socket address |
| 281 | arguments properly. */ |
| 282 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 283 | typedef struct { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 284 | PyObject_HEAD |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 285 | SOCKET_T sock_fd; /* Socket file descriptor */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 286 | int sock_family; /* Address family, e.g., AF_INET */ |
| 287 | int sock_type; /* Socket type, e.g., SOCK_STREAM */ |
| 288 | int sock_proto; /* Protocol type, usually 0 */ |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 289 | union sock_addr { |
| 290 | struct sockaddr_in in; |
| 291 | #ifdef AF_UNIX |
| 292 | struct sockaddr_un un; |
| 293 | #endif |
| 294 | } sock_addr; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 295 | } PySocketSockObject; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 296 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 297 | #ifdef USE_SSL |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 298 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 299 | typedef struct { |
| 300 | PyObject_HEAD |
| 301 | PySocketSockObject *Socket; /* Socket on which we're layered */ |
| 302 | PyObject *x_attr; /* Attributes dictionary */ |
| 303 | SSL_CTX* ctx; |
| 304 | SSL* ssl; |
| 305 | X509* server_cert; |
| 306 | BIO* sbio; |
| 307 | char server[256]; |
| 308 | char issuer[256]; |
| 309 | |
| 310 | } SSLObject; |
| 311 | |
| 312 | staticforward PyTypeObject SSL_Type; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 313 | staticforward PyObject *SSL_SSLwrite(SSLObject *self, PyObject *args); |
| 314 | staticforward PyObject *SSL_SSLread(SSLObject *self, PyObject *args); |
| 315 | |
| 316 | #define SSLObject_Check(v) ((v)->ob_type == &SSL_Type) |
| 317 | |
| 318 | #endif /* USE_SSL */ |
| 319 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 320 | /* A forward reference to the Socktype type object. |
| 321 | The Socktype variable contains pointers to various functions, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 322 | some of which call newsockobject(), which uses Socktype, so |
Guido van Rossum | 54ba21b | 1991-09-10 14:57:12 +0000 | [diff] [blame] | 323 | there has to be a circular reference. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 324 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 325 | staticforward PyTypeObject PySocketSock_Type; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 326 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 327 | |
| 328 | /* Create a new socket object. |
| 329 | This just creates the object and initializes it. |
| 330 | If the creation fails, return NULL and set an exception (implicit |
| 331 | in NEWOBJ()). */ |
| 332 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 333 | static PySocketSockObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 334 | PySocketSock_New(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 335 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 336 | PySocketSockObject *s; |
Guido van Rossum | 5555854 | 1996-05-23 22:54:50 +0000 | [diff] [blame] | 337 | PySocketSock_Type.ob_type = &PyType_Type; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 338 | s = PyObject_New(PySocketSockObject, &PySocketSock_Type); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 339 | if (s != NULL) { |
| 340 | s->sock_fd = fd; |
| 341 | s->sock_family = family; |
| 342 | s->sock_type = type; |
| 343 | s->sock_proto = proto; |
| 344 | } |
| 345 | return s; |
| 346 | } |
| 347 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 348 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 349 | /* Lock to allow python interpreter to continue, but only allow one |
| 350 | thread to be in gethostbyname */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 351 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 352 | PyThread_type_lock gethostbyname_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 353 | #endif |
| 354 | |
| 355 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 356 | /* Convert a string specifying a host name or one of a few symbolic |
| 357 | names to a numeric IP address. This usually calls gethostbyname() |
| 358 | to do the work; the names "" and "<broadcast>" are special. |
| 359 | Return the length (should always be 4 bytes), or negative if |
| 360 | an error occurred; then an exception is raised. */ |
| 361 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 362 | static int |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 363 | setipaddr(char* name, struct sockaddr_in * addr_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 364 | { |
| 365 | struct hostent *hp; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 366 | int d1, d2, d3, d4; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 367 | int h_length; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 368 | char ch; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 369 | #ifdef HAVE_GETHOSTBYNAME_R |
| 370 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 371 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 372 | struct hostent_data data; |
| 373 | #else |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 374 | char buf[1001]; |
| 375 | int buf_len = (sizeof buf) - 1; |
| 376 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 377 | #endif |
| 378 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 379 | int result; |
| 380 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 381 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 382 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 383 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 384 | if (name[0] == '\0') { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 385 | addr_ret->sin_addr.s_addr = INADDR_ANY; |
| 386 | return 4; |
| 387 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 388 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 389 | addr_ret->sin_addr.s_addr = INADDR_BROADCAST; |
| 390 | return 4; |
| 391 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 392 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 393 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 394 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 395 | addr_ret->sin_addr.s_addr = htonl( |
| 396 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 397 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 398 | return 4; |
| 399 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 400 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 401 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 402 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 403 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, &hp, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 404 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 405 | hp = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 406 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 407 | memset((void *) &data, '\0', sizeof(data)); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 408 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 409 | hp = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 410 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 411 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 412 | #ifdef USE_GETHOSTBYNAME_LOCK |
| 413 | PyThread_acquire_lock(gethostbyname_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 414 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 415 | hp = gethostbyname(name); |
| 416 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 417 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 418 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 419 | if (hp == NULL) { |
Guido van Rossum | ca9b323 | 1995-01-10 16:30:54 +0000 | [diff] [blame] | 420 | #ifdef HAVE_HSTRERROR |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 421 | /* Let's get real error message to return */ |
| 422 | extern int h_errno; |
| 423 | PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno)); |
| 424 | #else |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 425 | PyErr_SetString(PySocket_Error, "host not found"); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 426 | #endif |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 427 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 428 | PyThread_release_lock(gethostbyname_lock); |
| 429 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 430 | return -1; |
| 431 | } |
| 432 | memcpy((char *) &addr_ret->sin_addr, hp->h_addr, hp->h_length); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 433 | h_length = hp->h_length; |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 434 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 435 | PyThread_release_lock(gethostbyname_lock); |
| 436 | #endif |
| 437 | return h_length; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 440 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 441 | /* Create a string object representing an IP address. |
| 442 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 443 | size numbers). */ |
| 444 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 445 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 446 | makeipaddr(struct sockaddr_in *addr) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 447 | { |
| 448 | long x = ntohl(addr->sin_addr.s_addr); |
| 449 | char buf[100]; |
| 450 | sprintf(buf, "%d.%d.%d.%d", |
| 451 | (int) (x>>24) & 0xff, (int) (x>>16) & 0xff, |
| 452 | (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 453 | return PyString_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
| 457 | /* Create an object representing the given socket address, |
| 458 | suitable for passing it back to bind(), connect() etc. |
| 459 | The family field of the sockaddr structure is inspected |
| 460 | to determine what kind of address it really is. */ |
| 461 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 462 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 463 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 464 | makesockaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 465 | { |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 466 | if (addrlen == 0) { |
| 467 | /* No address -- may be recvfrom() from known socket */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 468 | Py_INCREF(Py_None); |
| 469 | return Py_None; |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 472 | #ifdef __BEOS__ |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 473 | /* XXX: BeOS version of accept() doesn't set family correctly */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 474 | addr->sa_family = AF_INET; |
| 475 | #endif |
| 476 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 477 | switch (addr->sa_family) { |
| 478 | |
| 479 | case AF_INET: |
| 480 | { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 481 | struct sockaddr_in *a = (struct sockaddr_in *) addr; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 482 | PyObject *addrobj = makeipaddr(a); |
| 483 | PyObject *ret = NULL; |
| 484 | if (addrobj) { |
| 485 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 486 | Py_DECREF(addrobj); |
| 487 | } |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 488 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 489 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 490 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 491 | #ifdef AF_UNIX |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 492 | case AF_UNIX: |
| 493 | { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 494 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 495 | return PyString_FromString(a->sun_path); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 496 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 497 | #endif /* AF_UNIX */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 498 | |
| 499 | /* More cases here... */ |
| 500 | |
| 501 | default: |
Guido van Rossum | aa948df | 1997-05-07 17:41:48 +0000 | [diff] [blame] | 502 | /* If we don't know the address family, don't raise an |
| 503 | exception -- return it as a tuple. */ |
| 504 | return Py_BuildValue("is#", |
| 505 | addr->sa_family, |
| 506 | addr->sa_data, |
| 507 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 508 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 509 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 512 | |
| 513 | /* Parse a socket address argument according to the socket object's |
| 514 | address family. Return 1 if the address was in the proper format, |
| 515 | 0 of not. The address is returned through addr_ret, its length |
| 516 | through len_ret. */ |
| 517 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 518 | static int |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 519 | 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] | 520 | { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 521 | switch (s->sock_family) { |
| 522 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 523 | #ifdef AF_UNIX |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 524 | case AF_UNIX: |
| 525 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 526 | struct sockaddr_un* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 527 | char *path; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 528 | int len; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 529 | addr = (struct sockaddr_un* )&(s->sock_addr).un; |
Guido van Rossum | 7e48898 | 1998-10-08 02:25:24 +0000 | [diff] [blame] | 530 | if (!PyArg_Parse(args, "t#", &path, &len)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 531 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 532 | if (len > sizeof addr->sun_path) { |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 533 | PyErr_SetString(PySocket_Error, |
| 534 | "AF_UNIX path too long"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 535 | return 0; |
| 536 | } |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 537 | addr->sun_family = AF_UNIX; |
| 538 | memcpy(addr->sun_path, path, len); |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 539 | addr->sun_path[len] = 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 540 | *addr_ret = (struct sockaddr *) addr; |
Guido van Rossum | 65af28a | 1996-06-11 18:36:33 +0000 | [diff] [blame] | 541 | *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 542 | return 1; |
| 543 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 544 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 545 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 546 | case AF_INET: |
| 547 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 548 | struct sockaddr_in* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 549 | char *host; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 550 | int port; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 551 | addr=(struct sockaddr_in*)&(s->sock_addr).in; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 552 | if (!PyArg_Parse(args, "(si)", &host, &port)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 553 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 554 | if (setipaddr(host, addr) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 555 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 556 | addr->sin_family = AF_INET; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 557 | addr->sin_port = htons((short)port); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 558 | *addr_ret = (struct sockaddr *) addr; |
| 559 | *len_ret = sizeof *addr; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 560 | return 1; |
| 561 | } |
| 562 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 563 | /* More cases here... */ |
| 564 | |
| 565 | default: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 566 | PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 567 | return 0; |
| 568 | |
| 569 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 572 | |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 573 | /* Get the address length according to the socket object's address family. |
| 574 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 575 | through len_ret. */ |
| 576 | |
| 577 | static int |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 578 | getsockaddrlen(PySocketSockObject *s, int *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 579 | { |
| 580 | switch (s->sock_family) { |
| 581 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 582 | #ifdef AF_UNIX |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 583 | case AF_UNIX: |
| 584 | { |
| 585 | *len_ret = sizeof (struct sockaddr_un); |
| 586 | return 1; |
| 587 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 588 | #endif /* AF_UNIX */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 589 | |
| 590 | case AF_INET: |
| 591 | { |
| 592 | *len_ret = sizeof (struct sockaddr_in); |
| 593 | return 1; |
| 594 | } |
| 595 | |
| 596 | /* More cases here... */ |
| 597 | |
| 598 | default: |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 599 | PyErr_SetString(PySocket_Error, "getsockaddrlen: bad family"); |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 600 | return 0; |
| 601 | |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 606 | /* s.accept() method */ |
| 607 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 608 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 609 | PySocketSock_accept(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 610 | { |
| 611 | char addrbuf[256]; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 612 | SOCKET_T newfd; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 613 | socklen_t addrlen; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 614 | PyObject *sock = NULL; |
| 615 | PyObject *addr = NULL; |
| 616 | PyObject *res = NULL; |
| 617 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 618 | if (!PyArg_ParseTuple(args, ":accept")) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 619 | return NULL; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 620 | if (!getsockaddrlen(s, &addrlen)) |
| 621 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 622 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 623 | newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 624 | Py_END_ALLOW_THREADS |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 625 | #ifdef MS_WINDOWS |
| 626 | if (newfd == INVALID_SOCKET) |
| 627 | #else |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 628 | if (newfd < 0) |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 629 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 630 | return PySocket_Err(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 631 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 632 | /* Create the new object with unspecified family, |
| 633 | to avoid calls to bind() etc. on it. */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 634 | sock = (PyObject *) PySocketSock_New(newfd, |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 635 | s->sock_family, |
| 636 | s->sock_type, |
| 637 | s->sock_proto); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 638 | if (sock == NULL) { |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 639 | SOCKETCLOSE(newfd); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 640 | goto finally; |
| 641 | } |
| 642 | if (!(addr = makesockaddr((struct sockaddr *) addrbuf, addrlen))) |
| 643 | goto finally; |
| 644 | |
| 645 | if (!(res = Py_BuildValue("OO", sock, addr))) |
| 646 | goto finally; |
| 647 | |
| 648 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 649 | Py_XDECREF(sock); |
| 650 | Py_XDECREF(addr); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 651 | return res; |
| 652 | } |
| 653 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 654 | static char accept_doc[] = |
| 655 | "accept() -> (socket object, address info)\n\ |
| 656 | \n\ |
| 657 | Wait for an incoming connection. Return a new socket representing the\n\ |
| 658 | connection, and the address of the client. For IP sockets, the address\n\ |
| 659 | info is a pair (hostaddr, port)."; |
| 660 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 661 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 662 | /* s.setblocking(1 | 0) method */ |
| 663 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 664 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 665 | PySocketSock_setblocking(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 666 | { |
| 667 | int block; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 668 | #ifndef MS_WINDOWS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 669 | int delay_flag; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 670 | #endif |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 671 | if (!PyArg_ParseTuple(args, "i:setblocking", &block)) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 672 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 673 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 674 | #ifdef __BEOS__ |
| 675 | block = !block; |
| 676 | setsockopt( s->sock_fd, SOL_SOCKET, SO_NONBLOCK, |
| 677 | (void *)(&block), sizeof( int ) ); |
| 678 | #else |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 679 | #ifndef MS_WINDOWS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 680 | #ifdef PYOS_OS2 |
| 681 | block = !block; |
| 682 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
| 683 | #else /* !PYOS_OS2 */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 684 | delay_flag = fcntl (s->sock_fd, F_GETFL, 0); |
| 685 | if (block) |
| 686 | delay_flag &= (~O_NDELAY); |
| 687 | else |
| 688 | delay_flag |= O_NDELAY; |
| 689 | fcntl (s->sock_fd, F_SETFL, delay_flag); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 690 | #endif /* !PYOS_OS2 */ |
| 691 | #else /* MS_WINDOWS */ |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 692 | block = !block; |
| 693 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 694 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 695 | #endif /* __BEOS__ */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 696 | Py_END_ALLOW_THREADS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 697 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 698 | Py_INCREF(Py_None); |
| 699 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 700 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 701 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 702 | static char setblocking_doc[] = |
| 703 | "setblocking(flag)\n\ |
| 704 | \n\ |
| 705 | Set the socket to blocking (flag is true) or non-blocking (false).\n\ |
| 706 | This uses the FIONBIO ioctl with the O_NDELAY flag."; |
| 707 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 708 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 709 | /* s.setsockopt() method. |
| 710 | With an integer third argument, sets an integer option. |
| 711 | With a string third argument, sets an option from a buffer; |
| 712 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 713 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 714 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 715 | PySocketSock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 716 | { |
| 717 | int level; |
| 718 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 719 | int res; |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 720 | char *buf; |
| 721 | int buflen; |
| 722 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 723 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 724 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 725 | &level, &optname, &flag)) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 726 | buf = (char *) &flag; |
| 727 | buflen = sizeof flag; |
| 728 | } |
| 729 | else { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 730 | PyErr_Clear(); |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 731 | if (!PyArg_ParseTuple(args, "iis#:setsockopt", |
| 732 | &level, &optname, &buf, &buflen)) |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 733 | return NULL; |
| 734 | } |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 735 | res = setsockopt(s->sock_fd, level, optname, (ANY *)buf, buflen); |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 736 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 737 | return PySocket_Err(); |
| 738 | Py_INCREF(Py_None); |
| 739 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 742 | static char setsockopt_doc[] = |
| 743 | "setsockopt(level, option, value)\n\ |
| 744 | \n\ |
| 745 | Set a socket option. See the Unix manual for level and option.\n\ |
| 746 | The value argument can either be an integer or a string."; |
| 747 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 748 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 749 | /* s.getsockopt() method. |
| 750 | With two arguments, retrieves an integer option. |
| 751 | With a third integer argument, retrieves a string buffer of that size; |
| 752 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 753 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 754 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 755 | PySocketSock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 756 | { |
| 757 | int level; |
| 758 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 759 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 760 | PyObject *buf; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 761 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 762 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 763 | #ifdef __BEOS__ |
| 764 | /* We have incomplete socket support. */ |
| 765 | PyErr_SetString( PySocket_Error, "getsockopt not supported" ); |
| 766 | return NULL; |
| 767 | #else |
| 768 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 769 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 770 | &level, &optname, &buflen)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 771 | return NULL; |
| 772 | |
| 773 | if (buflen == 0) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 774 | int flag = 0; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 775 | socklen_t flagsize = sizeof flag; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 776 | res = getsockopt(s->sock_fd, level, optname, |
| 777 | (ANY *)&flag, &flagsize); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 778 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 779 | return PySocket_Err(); |
| 780 | return PyInt_FromLong(flag); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 781 | } |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 782 | if (buflen <= 0 || buflen > 1024) { |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 783 | PyErr_SetString(PySocket_Error, |
| 784 | "getsockopt buflen out of range"); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 785 | return NULL; |
| 786 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 787 | buf = PyString_FromStringAndSize((char *)NULL, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 788 | if (buf == NULL) |
| 789 | return NULL; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 790 | res = getsockopt(s->sock_fd, level, optname, |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 791 | (ANY *)PyString_AsString(buf), &buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 792 | if (res < 0) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 793 | Py_DECREF(buf); |
| 794 | return PySocket_Err(); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 795 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 796 | _PyString_Resize(&buf, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 797 | return buf; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 798 | #endif /* __BEOS__ */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 801 | static char getsockopt_doc[] = |
| 802 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 803 | \n\ |
| 804 | Get a socket option. See the Unix manual for level and option.\n\ |
| 805 | If a nonzero buffersize argument is given, the return value is a\n\ |
| 806 | string of that length; otherwise it is an integer."; |
| 807 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 808 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 809 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 810 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 811 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 812 | PySocketSock_bind(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 813 | { |
| 814 | struct sockaddr *addr; |
| 815 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 816 | int res; |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 817 | PyObject *addro; |
| 818 | if (!PyArg_ParseTuple(args, "O:bind", &addro)) |
| 819 | return NULL; |
| 820 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 821 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 822 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 823 | res = bind(s->sock_fd, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 824 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 825 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 826 | return PySocket_Err(); |
| 827 | Py_INCREF(Py_None); |
| 828 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 831 | static char bind_doc[] = |
| 832 | "bind(address)\n\ |
| 833 | \n\ |
| 834 | Bind the socket to a local address. For IP sockets, the address is a\n\ |
| 835 | pair (host, port); the host must refer to the local host."; |
| 836 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 837 | |
| 838 | /* s.close() method. |
| 839 | Set the file descriptor to -1 so operations tried subsequently |
| 840 | will surely fail. */ |
| 841 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 842 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 843 | PySocketSock_close(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 844 | { |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 845 | if (!PyArg_ParseTuple(args, ":close")) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 846 | return NULL; |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 847 | if (s->sock_fd != -1) { |
| 848 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 849 | (void) SOCKETCLOSE(s->sock_fd); |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 850 | Py_END_ALLOW_THREADS |
| 851 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 852 | s->sock_fd = -1; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 853 | Py_INCREF(Py_None); |
| 854 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 857 | static char close_doc[] = |
| 858 | "close()\n\ |
| 859 | \n\ |
| 860 | Close the socket. It cannot be used after this call."; |
| 861 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 862 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 863 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 864 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 865 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 866 | PySocketSock_connect(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 867 | { |
| 868 | struct sockaddr *addr; |
| 869 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 870 | int res; |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 871 | PyObject *addro; |
| 872 | if (!PyArg_ParseTuple(args, "O:connect", &addro)) |
| 873 | return NULL; |
| 874 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 875 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 876 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 877 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 878 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 879 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 880 | return PySocket_Err(); |
| 881 | Py_INCREF(Py_None); |
| 882 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 885 | static char connect_doc[] = |
| 886 | "connect(address)\n\ |
| 887 | \n\ |
| 888 | Connect the socket to a remote address. For IP sockets, the address\n\ |
| 889 | is a pair (host, port)."; |
| 890 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 891 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 892 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 893 | |
| 894 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 895 | PySocketSock_connect_ex(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 896 | { |
| 897 | struct sockaddr *addr; |
| 898 | int addrlen; |
| 899 | int res; |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 900 | PyObject *addro; |
| 901 | if (!PyArg_ParseTuple(args, "O:connect_ex", &addro)) |
| 902 | return NULL; |
| 903 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 904 | return NULL; |
| 905 | Py_BEGIN_ALLOW_THREADS |
| 906 | res = connect(s->sock_fd, addr, addrlen); |
| 907 | Py_END_ALLOW_THREADS |
| 908 | if (res != 0) |
| 909 | res = errno; |
| 910 | return PyInt_FromLong((long) res); |
| 911 | } |
| 912 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 913 | static char connect_ex_doc[] = |
| 914 | "connect_ex(address)\n\ |
| 915 | \n\ |
| 916 | This is like connect(address), but returns an error code (the errno value)\n\ |
| 917 | instead of raising an exception when an error occurs."; |
| 918 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 919 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 920 | /* s.fileno() method */ |
| 921 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 922 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 923 | PySocketSock_fileno(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 924 | { |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 925 | if (!PyArg_ParseTuple(args, ":fileno")) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 926 | return NULL; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 927 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 928 | return PyInt_FromLong((long) s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 929 | #else |
| 930 | return PyLong_FromLongLong((LONG_LONG)s->sock_fd); |
| 931 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 934 | static char fileno_doc[] = |
| 935 | "fileno() -> integer\n\ |
| 936 | \n\ |
| 937 | Return the integer file descriptor of the socket."; |
| 938 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 939 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 940 | #ifndef NO_DUP |
| 941 | /* s.dup() method */ |
| 942 | |
| 943 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 944 | PySocketSock_dup(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 945 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 946 | SOCKET_T newfd; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 947 | PyObject *sock; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 948 | if (!PyArg_ParseTuple(args, ":dup")) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 949 | return NULL; |
| 950 | newfd = dup(s->sock_fd); |
| 951 | if (newfd < 0) |
| 952 | return PySocket_Err(); |
| 953 | sock = (PyObject *) PySocketSock_New(newfd, |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 954 | s->sock_family, |
| 955 | s->sock_type, |
| 956 | s->sock_proto); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 957 | if (sock == NULL) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 958 | SOCKETCLOSE(newfd); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 959 | return sock; |
| 960 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 961 | |
| 962 | static char dup_doc[] = |
| 963 | "dup() -> socket object\n\ |
| 964 | \n\ |
| 965 | Return a new socket object connected to the same system resource."; |
| 966 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 967 | #endif |
| 968 | |
| 969 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 970 | /* s.getsockname() method */ |
| 971 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 972 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 973 | PySocketSock_getsockname(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 974 | { |
| 975 | char addrbuf[256]; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 976 | int res; |
| 977 | socklen_t addrlen; |
| 978 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 979 | if (!PyArg_ParseTuple(args, ":getsockname")) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 980 | return NULL; |
| 981 | if (!getsockaddrlen(s, &addrlen)) |
| 982 | return NULL; |
Guido van Rossum | ca42b16 | 1996-01-12 01:36:05 +0000 | [diff] [blame] | 983 | memset(addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 984 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 985 | res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 986 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 987 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 988 | return PySocket_Err(); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 989 | return makesockaddr((struct sockaddr *) addrbuf, addrlen); |
| 990 | } |
| 991 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 992 | static char getsockname_doc[] = |
| 993 | "getsockname() -> address info\n\ |
| 994 | \n\ |
| 995 | Return the address of the local endpoint. For IP sockets, the address\n\ |
| 996 | info is a pair (hostaddr, port)."; |
| 997 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 998 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 999 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1000 | /* s.getpeername() method */ |
| 1001 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1002 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1003 | PySocketSock_getpeername(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1004 | { |
| 1005 | char addrbuf[256]; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1006 | int res; |
| 1007 | socklen_t addrlen; |
| 1008 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1009 | if (!PyArg_ParseTuple(args, ":getpeername")) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1010 | return NULL; |
| 1011 | if (!getsockaddrlen(s, &addrlen)) |
| 1012 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1013 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1014 | res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1015 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1016 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1017 | return PySocket_Err(); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1018 | return makesockaddr((struct sockaddr *) addrbuf, addrlen); |
| 1019 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1020 | |
| 1021 | static char getpeername_doc[] = |
| 1022 | "getpeername() -> address info\n\ |
| 1023 | \n\ |
| 1024 | Return the address of the remote endpoint. For IP sockets, the address\n\ |
| 1025 | info is a pair (hostaddr, port)."; |
| 1026 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1027 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1028 | |
| 1029 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1030 | /* s.listen(n) method */ |
| 1031 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1032 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1033 | PySocketSock_listen(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1034 | { |
| 1035 | int backlog; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1036 | int res; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1037 | if (!PyArg_ParseTuple(args, "i:listen", &backlog)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1038 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1039 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1040 | if (backlog < 1) |
| 1041 | backlog = 1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1042 | res = listen(s->sock_fd, backlog); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1043 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1044 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1045 | return PySocket_Err(); |
| 1046 | Py_INCREF(Py_None); |
| 1047 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1050 | static char listen_doc[] = |
| 1051 | "listen(backlog)\n\ |
| 1052 | \n\ |
| 1053 | Enable a server to accept connections. The backlog argument must be at\n\ |
| 1054 | least 1; it specifies the number of unaccepted connection that the system\n\ |
| 1055 | will allow before refusing new connections."; |
| 1056 | |
| 1057 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1058 | #ifndef NO_DUP |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1059 | /* s.makefile(mode) method. |
| 1060 | Create a new open file object referring to a dupped version of |
| 1061 | the socket's file descriptor. (The dup() call is necessary so |
| 1062 | that the open file and socket objects may be closed independent |
| 1063 | of each other.) |
| 1064 | The mode argument specifies 'r' or 'w' passed to fdopen(). */ |
| 1065 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1066 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1067 | PySocketSock_makefile(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1068 | { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1069 | extern int fclose(FILE *); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1070 | char *mode = "r"; |
| 1071 | int bufsize = -1; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1072 | #ifdef MS_WIN32 |
| 1073 | intptr_t fd; |
| 1074 | #else |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1075 | int fd; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1076 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1077 | FILE *fp; |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1078 | PyObject *f; |
| 1079 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1080 | if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1081 | return NULL; |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1082 | #ifdef MS_WIN32 |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1083 | if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) || |
| 1084 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 1085 | #else |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1086 | 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] | 1087 | #endif |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1088 | { |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1089 | if (fd >= 0) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1090 | SOCKETCLOSE(fd); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1091 | return PySocket_Err(); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1092 | } |
| 1093 | f = PyFile_FromFile(fp, "<socket>", mode, fclose); |
| 1094 | if (f != NULL) |
| 1095 | PyFile_SetBufSize(f, bufsize); |
| 1096 | return f; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1097 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1098 | |
| 1099 | static char makefile_doc[] = |
| 1100 | "makefile([mode[, buffersize]]) -> file object\n\ |
| 1101 | \n\ |
| 1102 | Return a regular file object corresponding to the socket.\n\ |
| 1103 | The mode and buffersize arguments are as for the built-in open() function."; |
| 1104 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1105 | #endif /* NO_DUP */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1106 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1107 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1108 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1109 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1110 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1111 | PySocketSock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1112 | { |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1113 | int len, n, flags = 0; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1114 | PyObject *buf; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1115 | if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1116 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1117 | buf = PyString_FromStringAndSize((char *) 0, len); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1118 | if (buf == NULL) |
| 1119 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1120 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 1121 | n = recv(s->sock_fd, PyString_AsString(buf), len, flags); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1122 | Py_END_ALLOW_THREADS |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 1123 | if (n < 0) { |
| 1124 | Py_DECREF(buf); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1125 | return PySocket_Err(); |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 1126 | } |
Guido van Rossum | 53a9bf8 | 1996-06-11 18:35:24 +0000 | [diff] [blame] | 1127 | if (n != len && _PyString_Resize(&buf, n) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1128 | return NULL; |
| 1129 | return buf; |
| 1130 | } |
| 1131 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1132 | static char recv_doc[] = |
| 1133 | "recv(buffersize[, flags]) -> data\n\ |
| 1134 | \n\ |
| 1135 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 1136 | argument, see the Unix manual. When no data is available, block until\n\ |
| 1137 | at least one byte is available or until the remote end is closed. When\n\ |
| 1138 | the remote end is closed and all data is read, return the empty string."; |
| 1139 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1140 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1141 | /* s.recvfrom(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1142 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1143 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1144 | PySocketSock_recvfrom(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1145 | { |
| 1146 | char addrbuf[256]; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1147 | PyObject *buf = NULL; |
| 1148 | PyObject *addr = NULL; |
| 1149 | PyObject *ret = NULL; |
| 1150 | |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1151 | int len, n, flags = 0; |
| 1152 | socklen_t addrlen; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1153 | if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1154 | return NULL; |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 1155 | if (!getsockaddrlen(s, &addrlen)) |
| 1156 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1157 | buf = PyString_FromStringAndSize((char *) 0, len); |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 1158 | if (buf == NULL) |
| 1159 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1160 | Py_BEGIN_ALLOW_THREADS |
| 1161 | n = recvfrom(s->sock_fd, PyString_AsString(buf), len, flags, |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1162 | #ifndef MS_WINDOWS |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 1163 | #if defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1164 | (struct sockaddr *)addrbuf, &addrlen |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 1165 | #else |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1166 | (ANY *)addrbuf, &addrlen |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 1167 | #endif |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1168 | #else |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1169 | (struct sockaddr *)addrbuf, &addrlen |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1170 | #endif |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1171 | ); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1172 | Py_END_ALLOW_THREADS |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 1173 | if (n < 0) { |
| 1174 | Py_DECREF(buf); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1175 | return PySocket_Err(); |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 1176 | } |
Guido van Rossum | 53a9bf8 | 1996-06-11 18:35:24 +0000 | [diff] [blame] | 1177 | if (n != len && _PyString_Resize(&buf, n) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1178 | return NULL; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1179 | |
| 1180 | if (!(addr = makesockaddr((struct sockaddr *)addrbuf, addrlen))) |
| 1181 | goto finally; |
| 1182 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1183 | ret = Py_BuildValue("OO", buf, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1184 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1185 | Py_XDECREF(addr); |
| 1186 | Py_XDECREF(buf); |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 1187 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1190 | static char recvfrom_doc[] = |
| 1191 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 1192 | \n\ |
| 1193 | Like recv(buffersize, flags) but also return the sender's address info."; |
| 1194 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1195 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1196 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1197 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1198 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1199 | PySocketSock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1200 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1201 | char *buf; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1202 | int len, n, flags = 0; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1203 | if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1204 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1205 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1206 | n = send(s->sock_fd, buf, len, flags); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1207 | Py_END_ALLOW_THREADS |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1208 | if (n < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1209 | return PySocket_Err(); |
| 1210 | return PyInt_FromLong((long)n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1213 | static char send_doc[] = |
| 1214 | "send(data[, flags])\n\ |
| 1215 | \n\ |
| 1216 | Send a data string to the socket. For the optional flags\n\ |
| 1217 | argument, see the Unix manual."; |
| 1218 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1219 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1220 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1221 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1222 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1223 | PySocketSock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1224 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1225 | PyObject *addro; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1226 | char *buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1227 | struct sockaddr *addr; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1228 | int addrlen, len, n, flags; |
| 1229 | flags = 0; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1230 | if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1231 | PyErr_Clear(); |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1232 | if (!PyArg_ParseTuple(args, "s#iO:sendto", |
| 1233 | &buf, &len, &flags, &addro)) |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1234 | return NULL; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1235 | } |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1236 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1237 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1238 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1239 | n = sendto(s->sock_fd, buf, len, flags, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1240 | Py_END_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1241 | if (n < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1242 | return PySocket_Err(); |
| 1243 | return PyInt_FromLong((long)n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1246 | static char sendto_doc[] = |
| 1247 | "sendto(data[, flags], address)\n\ |
| 1248 | \n\ |
| 1249 | Like send(data, flags) but allows specifying the destination address.\n\ |
| 1250 | For IP sockets, the address is a pair (hostaddr, port)."; |
| 1251 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1252 | |
| 1253 | /* s.shutdown(how) method */ |
| 1254 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1255 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1256 | PySocketSock_shutdown(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1257 | { |
| 1258 | int how; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1259 | int res; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1260 | if (!PyArg_ParseTuple(args, "i:shutdown", &how)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1261 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1262 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1263 | res = shutdown(s->sock_fd, how); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1264 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1265 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1266 | return PySocket_Err(); |
| 1267 | Py_INCREF(Py_None); |
| 1268 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1271 | static char shutdown_doc[] = |
| 1272 | "shutdown(flag)\n\ |
| 1273 | \n\ |
| 1274 | Shut down the reading side of the socket (flag == 0), the writing side\n\ |
| 1275 | of the socket (flag == 1), or both ends (flag == 2)."; |
| 1276 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1277 | |
| 1278 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1279 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1280 | static PyMethodDef PySocketSock_methods[] = { |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1281 | {"accept", (PyCFunction)PySocketSock_accept, 1, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1282 | accept_doc}, |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1283 | {"bind", (PyCFunction)PySocketSock_bind, 1, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1284 | bind_doc}, |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1285 | {"close", (PyCFunction)PySocketSock_close, 1, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1286 | close_doc}, |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1287 | {"connect", (PyCFunction)PySocketSock_connect, 1, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1288 | connect_doc}, |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1289 | {"connect_ex", (PyCFunction)PySocketSock_connect_ex, 1, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1290 | connect_ex_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1291 | #ifndef NO_DUP |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1292 | {"dup", (PyCFunction)PySocketSock_dup, 1, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1293 | dup_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1294 | #endif |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1295 | {"fileno", (PyCFunction)PySocketSock_fileno, 1, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1296 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1297 | #ifdef HAVE_GETPEERNAME |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1298 | {"getpeername", (PyCFunction)PySocketSock_getpeername, 1, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1299 | getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 1300 | #endif |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1301 | {"getsockname", (PyCFunction)PySocketSock_getsockname, 1, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1302 | getsockname_doc}, |
| 1303 | {"getsockopt", (PyCFunction)PySocketSock_getsockopt, 1, |
| 1304 | getsockopt_doc}, |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1305 | {"listen", (PyCFunction)PySocketSock_listen, 1, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1306 | listen_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1307 | #ifndef NO_DUP |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1308 | {"makefile", (PyCFunction)PySocketSock_makefile, 1, |
| 1309 | makefile_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1310 | #endif |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1311 | {"recv", (PyCFunction)PySocketSock_recv, 1, |
| 1312 | recv_doc}, |
| 1313 | {"recvfrom", (PyCFunction)PySocketSock_recvfrom, 1, |
| 1314 | recvfrom_doc}, |
| 1315 | {"send", (PyCFunction)PySocketSock_send, 1, |
| 1316 | send_doc}, |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1317 | {"sendto", (PyCFunction)PySocketSock_sendto, 1, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1318 | sendto_doc}, |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1319 | {"setblocking", (PyCFunction)PySocketSock_setblocking, 1, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1320 | setblocking_doc}, |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1321 | {"setsockopt", (PyCFunction)PySocketSock_setsockopt, 1, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1322 | setsockopt_doc}, |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1323 | {"shutdown", (PyCFunction)PySocketSock_shutdown, 1, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1324 | shutdown_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1325 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1326 | }; |
| 1327 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1328 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1329 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1330 | First close the file description. */ |
| 1331 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1332 | static void |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1333 | PySocketSock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1334 | { |
Guido van Rossum | fa972c9 | 2000-04-10 12:45:45 +0000 | [diff] [blame] | 1335 | if (s->sock_fd != -1) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1336 | (void) SOCKETCLOSE(s->sock_fd); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1337 | PyObject_Del(s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1340 | |
| 1341 | /* Return a socket object's named attribute. */ |
| 1342 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1343 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1344 | PySocketSock_getattr(PySocketSockObject *s, char *name) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1345 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1346 | return Py_FindMethod(PySocketSock_methods, (PyObject *) s, name); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1349 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1350 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1351 | PySocketSock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1352 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1353 | char buf[512]; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1354 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
| 1355 | if (s->sock_fd > LONG_MAX) { |
| 1356 | /* this can occur on Win64, and actually there is a special |
| 1357 | ugly printf formatter for decimal pointer length integer |
| 1358 | printing, only bother if necessary*/ |
| 1359 | PyErr_SetString(PyExc_OverflowError, |
| 1360 | "no printf formatter to display the socket descriptor in decimal"); |
| 1361 | return NULL; |
| 1362 | } |
| 1363 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1364 | sprintf(buf, |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1365 | "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>", |
| 1366 | (long)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] | 1367 | return PyString_FromString(buf); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1371 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1372 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1373 | static PyTypeObject PySocketSock_Type = { |
Guido van Rossum | 5555854 | 1996-05-23 22:54:50 +0000 | [diff] [blame] | 1374 | PyObject_HEAD_INIT(0) /* Must fill in type value later */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1375 | 0, |
| 1376 | "socket", |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1377 | sizeof(PySocketSockObject), |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1378 | 0, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1379 | (destructor)PySocketSock_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1380 | 0, /*tp_print*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1381 | (getattrfunc)PySocketSock_getattr, /*tp_getattr*/ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1382 | 0, /*tp_setattr*/ |
| 1383 | 0, /*tp_compare*/ |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1384 | (reprfunc)PySocketSock_repr, /*tp_repr*/ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1385 | 0, /*tp_as_number*/ |
| 1386 | 0, /*tp_as_sequence*/ |
| 1387 | 0, /*tp_as_mapping*/ |
| 1388 | }; |
| 1389 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1390 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1391 | /* Python interface to gethostname(). */ |
| 1392 | |
| 1393 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1394 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1395 | PySocket_gethostname(PyObject *self, PyObject *args) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1396 | { |
| 1397 | char buf[1024]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1398 | int res; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1399 | if (!PyArg_ParseTuple(args, ":gethostname")) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1400 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1401 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1402 | res = gethostname(buf, (int) sizeof buf - 1); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1403 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1404 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1405 | return PySocket_Err(); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1406 | buf[sizeof buf - 1] = '\0'; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1407 | return PyString_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1408 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1409 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1410 | static char gethostname_doc[] = |
| 1411 | "gethostname() -> string\n\ |
| 1412 | \n\ |
| 1413 | Return the current host name."; |
| 1414 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1415 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1416 | /* Python interface to gethostbyname(name). */ |
| 1417 | |
| 1418 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1419 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1420 | PySocket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1421 | { |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1422 | char *name; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1423 | struct sockaddr_in addrbuf; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1424 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1425 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1426 | if (setipaddr(name, &addrbuf) < 0) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1427 | return NULL; |
| 1428 | return makeipaddr(&addrbuf); |
| 1429 | } |
| 1430 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1431 | static char gethostbyname_doc[] = |
| 1432 | "gethostbyname(host) -> address\n\ |
| 1433 | \n\ |
| 1434 | Return the IP address (a string of the form '255.255.255.255') for a host."; |
| 1435 | |
| 1436 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1437 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 1438 | |
| 1439 | static PyObject * |
| 1440 | gethost_common(h, addr) |
| 1441 | struct hostent *h; |
| 1442 | struct sockaddr_in *addr; |
| 1443 | { |
| 1444 | char **pch; |
| 1445 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 1446 | PyObject *name_list = (PyObject *)NULL; |
| 1447 | PyObject *addr_list = (PyObject *)NULL; |
| 1448 | PyObject *tmp; |
| 1449 | if (h == NULL) { |
| 1450 | #ifdef HAVE_HSTRERROR |
| 1451 | /* Let's get real error message to return */ |
| 1452 | extern int h_errno; |
| 1453 | PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno)); |
| 1454 | #else |
| 1455 | PyErr_SetString(PySocket_Error, "host not found"); |
| 1456 | #endif |
| 1457 | return NULL; |
| 1458 | } |
| 1459 | if ((name_list = PyList_New(0)) == NULL) |
| 1460 | goto err; |
| 1461 | if ((addr_list = PyList_New(0)) == NULL) |
| 1462 | goto err; |
| 1463 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 1464 | int status; |
| 1465 | tmp = PyString_FromString(*pch); |
| 1466 | if (tmp == NULL) |
| 1467 | goto err; |
| 1468 | status = PyList_Append(name_list, tmp); |
| 1469 | Py_DECREF(tmp); |
| 1470 | if (status) |
| 1471 | goto err; |
| 1472 | } |
| 1473 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 1474 | int status; |
| 1475 | memcpy((char *) &addr->sin_addr, *pch, h->h_length); |
| 1476 | tmp = makeipaddr(addr); |
| 1477 | if (tmp == NULL) |
| 1478 | goto err; |
| 1479 | status = PyList_Append(addr_list, tmp); |
| 1480 | Py_DECREF(tmp); |
| 1481 | if (status) |
| 1482 | goto err; |
| 1483 | } |
| 1484 | rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list); |
| 1485 | err: |
| 1486 | Py_XDECREF(name_list); |
| 1487 | Py_XDECREF(addr_list); |
| 1488 | return rtn_tuple; |
| 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | /* Python interface to gethostbyname_ex(name). */ |
| 1493 | |
| 1494 | /*ARGSUSED*/ |
| 1495 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1496 | PySocket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1497 | { |
| 1498 | char *name; |
| 1499 | struct hostent *h; |
| 1500 | struct sockaddr_in addr; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 1501 | PyObject *ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1502 | #ifdef HAVE_GETHOSTBYNAME_R |
| 1503 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1504 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 1505 | struct hostent_data data; |
| 1506 | #else |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1507 | char buf[16384]; |
| 1508 | int buf_len = (sizeof buf) - 1; |
| 1509 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1510 | #endif |
| 1511 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 1512 | int result; |
| 1513 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1514 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1515 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name)) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1516 | return NULL; |
| 1517 | if (setipaddr(name, &addr) < 0) |
| 1518 | return NULL; |
| 1519 | Py_BEGIN_ALLOW_THREADS |
| 1520 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1521 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 1522 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1523 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1524 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1525 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 1526 | memset((void *) &data, '\0', sizeof(data)); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1527 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 1528 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 1529 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1530 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 1531 | #ifdef USE_GETHOSTBYNAME_LOCK |
| 1532 | PyThread_acquire_lock(gethostbyname_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1533 | #endif |
| 1534 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1535 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 1536 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 1537 | ret = gethost_common(h, &addr); |
| 1538 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 1539 | PyThread_release_lock(gethostbyname_lock); |
| 1540 | #endif |
| 1541 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | static char ghbn_ex_doc[] = |
| 1545 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 1546 | \n\ |
| 1547 | Return the true host name, a list of aliases, and a list of IP addresses,\n\ |
| 1548 | for a host. The host argument is a string giving a host name or IP number."; |
| 1549 | |
| 1550 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1551 | /* Python interface to gethostbyaddr(IP). */ |
| 1552 | |
| 1553 | /*ARGSUSED*/ |
| 1554 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1555 | PySocket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1556 | { |
| 1557 | struct sockaddr_in addr; |
| 1558 | char *ip_num; |
| 1559 | struct hostent *h; |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 1560 | PyObject *ret; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1561 | #ifdef HAVE_GETHOSTBYNAME_R |
| 1562 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1563 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 1564 | struct hostent_data data; |
| 1565 | #else |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1566 | char buf[16384]; |
| 1567 | int buf_len = (sizeof buf) - 1; |
| 1568 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1569 | #endif |
| 1570 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 1571 | int result; |
| 1572 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1573 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1574 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1575 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num)) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1576 | return NULL; |
| 1577 | if (setipaddr(ip_num, &addr) < 0) |
| 1578 | return NULL; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1579 | Py_BEGIN_ALLOW_THREADS |
| 1580 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1581 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 1582 | result = gethostbyaddr_r((char *)&addr.sin_addr, |
| 1583 | sizeof(addr.sin_addr), |
| 1584 | AF_INET, &hp_allocated, buf, buf_len, |
| 1585 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1586 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1587 | h = gethostbyaddr_r((char *)&addr.sin_addr, |
| 1588 | sizeof(addr.sin_addr), |
| 1589 | AF_INET, |
| 1590 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1591 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 1592 | memset((void *) &data, '\0', sizeof(data)); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1593 | result = gethostbyaddr_r((char *)&addr.sin_addr, |
| 1594 | sizeof(addr.sin_addr), |
| 1595 | AF_INET, &hp_allocated, &data); |
| 1596 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 1597 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1598 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 1599 | #ifdef USE_GETHOSTBYNAME_LOCK |
| 1600 | PyThread_acquire_lock(gethostbyname_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1601 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1602 | h = gethostbyaddr((char *)&addr.sin_addr, |
| 1603 | sizeof(addr.sin_addr), |
| 1604 | AF_INET); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1605 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 1606 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 1607 | ret = gethost_common(h, &addr); |
| 1608 | #ifdef USE_GETHOSTBYNAME_LOCK |
| 1609 | PyThread_release_lock(gethostbyname_lock); |
| 1610 | #endif |
| 1611 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1614 | static char gethostbyaddr_doc[] = |
| 1615 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 1616 | \n\ |
| 1617 | Return the true host name, a list of aliases, and a list of IP addresses,\n\ |
| 1618 | for a host. The host argument is a string giving a host name or IP number."; |
| 1619 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1620 | |
| 1621 | /* Python interface to getservbyname(name). |
| 1622 | This only returns the port number, since the other info is already |
| 1623 | known or not useful (like the list of aliases). */ |
| 1624 | |
| 1625 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1626 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1627 | PySocket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1628 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1629 | char *name, *proto; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1630 | struct servent *sp; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1631 | if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1632 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1633 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1634 | sp = getservbyname(name, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1635 | Py_END_ALLOW_THREADS |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1636 | if (sp == NULL) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1637 | PyErr_SetString(PySocket_Error, "service/proto not found"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1638 | return NULL; |
| 1639 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1640 | return PyInt_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1643 | static char getservbyname_doc[] = |
| 1644 | "getservbyname(servicename, protocolname) -> integer\n\ |
| 1645 | \n\ |
| 1646 | Return a port number from a service name and protocol name.\n\ |
| 1647 | The protocol name should be 'tcp' or 'udp'."; |
| 1648 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1649 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 1650 | /* Python interface to getprotobyname(name). |
| 1651 | This only returns the protocol number, since the other info is |
| 1652 | already known or not useful (like the list of aliases). */ |
| 1653 | |
| 1654 | /*ARGSUSED*/ |
| 1655 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1656 | PySocket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 1657 | { |
| 1658 | char *name; |
| 1659 | struct protoent *sp; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1660 | #ifdef __BEOS__ |
| 1661 | /* Not available in BeOS yet. - [cjh] */ |
| 1662 | PyErr_SetString( PySocket_Error, "getprotobyname not supported" ); |
| 1663 | return NULL; |
| 1664 | #else |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1665 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 1666 | return NULL; |
| 1667 | Py_BEGIN_ALLOW_THREADS |
| 1668 | sp = getprotobyname(name); |
| 1669 | Py_END_ALLOW_THREADS |
| 1670 | if (sp == NULL) { |
| 1671 | PyErr_SetString(PySocket_Error, "protocol not found"); |
| 1672 | return NULL; |
| 1673 | } |
| 1674 | return PyInt_FromLong((long) sp->p_proto); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1675 | #endif |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1678 | static char getprotobyname_doc[] = |
| 1679 | "getprotobyname(name) -> integer\n\ |
| 1680 | \n\ |
| 1681 | Return the protocol number for the named protocol. (Rarely used.)"; |
| 1682 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 1683 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1684 | /* Python interface to socket(family, type, proto). |
| 1685 | The third (protocol) argument is optional. |
| 1686 | Return a new socket object. */ |
| 1687 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1688 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1689 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1690 | PySocket_socket(PyObject *self, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1691 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1692 | PySocketSockObject *s; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1693 | SOCKET_T fd; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1694 | int family, type, proto = 0; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1695 | if (!PyArg_ParseTuple(args, "ii|i:socket", &family, &type, &proto)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1696 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1697 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1698 | fd = socket(family, type, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1699 | Py_END_ALLOW_THREADS |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 1700 | #ifdef MS_WINDOWS |
Guido van Rossum | d639d4d | 1996-06-12 04:04:22 +0000 | [diff] [blame] | 1701 | if (fd == INVALID_SOCKET) |
| 1702 | #else |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1703 | if (fd < 0) |
Guido van Rossum | d639d4d | 1996-06-12 04:04:22 +0000 | [diff] [blame] | 1704 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1705 | return PySocket_Err(); |
| 1706 | s = PySocketSock_New(fd, family, type, proto); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1707 | /* If the object can't be created, don't forget to close the |
| 1708 | file descriptor again! */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1709 | if (s == NULL) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1710 | (void) SOCKETCLOSE(fd); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1711 | /* From now on, ignore SIGPIPE and let the error checking |
| 1712 | do the work. */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1713 | #ifdef SIGPIPE |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1714 | (void) signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1715 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1716 | return (PyObject *) s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1719 | static char socket_doc[] = |
| 1720 | "socket(family, type[, proto]) -> socket object\n\ |
| 1721 | \n\ |
| 1722 | Open a socket of the given type. The family argument specifies the\n\ |
| 1723 | address family; it is normally AF_INET, sometimes AF_UNIX.\n\ |
| 1724 | The type argument specifies whether this is a stream (SOCK_STREAM)\n\ |
| 1725 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 1726 | specifying the default protocol."; |
| 1727 | |
| 1728 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1729 | #ifndef NO_DUP |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1730 | /* Create a socket object from a numeric file description. |
| 1731 | Useful e.g. if stdin is a socket. |
| 1732 | Additional arguments as for socket(). */ |
| 1733 | |
| 1734 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1735 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1736 | PySocket_fromfd(PyObject *self, PyObject *args) |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1737 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1738 | PySocketSockObject *s; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1739 | SOCKET_T fd; |
| 1740 | int family, type, proto = 0; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1741 | if (!PyArg_ParseTuple(args, "iii|i:fromfd", |
| 1742 | &fd, &family, &type, &proto)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1743 | return NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1744 | /* Dup the fd so it and the socket can be closed independently */ |
| 1745 | fd = dup(fd); |
| 1746 | if (fd < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1747 | return PySocket_Err(); |
| 1748 | s = PySocketSock_New(fd, family, type, proto); |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1749 | /* From now on, ignore SIGPIPE and let the error checking |
| 1750 | do the work. */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1751 | #ifdef SIGPIPE |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1752 | (void) signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1753 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1754 | return (PyObject *) s; |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1755 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1756 | |
| 1757 | static char fromfd_doc[] = |
| 1758 | "fromfd(fd, family, type[, proto]) -> socket object\n\ |
| 1759 | \n\ |
| 1760 | Create a socket object from the given file descriptor.\n\ |
| 1761 | The remaining arguments are the same as for socket()."; |
| 1762 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1763 | #endif /* NO_DUP */ |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1764 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1765 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1766 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1767 | PySocket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1768 | { |
| 1769 | int x1, x2; |
| 1770 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1771 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1772 | return NULL; |
| 1773 | } |
| 1774 | x2 = (int)ntohs((short)x1); |
| 1775 | return PyInt_FromLong(x2); |
| 1776 | } |
| 1777 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1778 | static char ntohs_doc[] = |
| 1779 | "ntohs(integer) -> integer\n\ |
| 1780 | \n\ |
| 1781 | Convert a 16-bit integer from network to host byte order."; |
| 1782 | |
| 1783 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1784 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1785 | PySocket_ntohl(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1786 | { |
| 1787 | int x1, x2; |
| 1788 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1789 | if (!PyArg_ParseTuple(args, "i:ntohl", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1790 | return NULL; |
| 1791 | } |
| 1792 | x2 = ntohl(x1); |
| 1793 | return PyInt_FromLong(x2); |
| 1794 | } |
| 1795 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1796 | static char ntohl_doc[] = |
| 1797 | "ntohl(integer) -> integer\n\ |
| 1798 | \n\ |
| 1799 | Convert a 32-bit integer from network to host byte order."; |
| 1800 | |
| 1801 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1802 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1803 | PySocket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1804 | { |
| 1805 | int x1, x2; |
| 1806 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1807 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1808 | return NULL; |
| 1809 | } |
| 1810 | x2 = (int)htons((short)x1); |
| 1811 | return PyInt_FromLong(x2); |
| 1812 | } |
| 1813 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1814 | static char htons_doc[] = |
| 1815 | "htons(integer) -> integer\n\ |
| 1816 | \n\ |
| 1817 | Convert a 16-bit integer from host to network byte order."; |
| 1818 | |
| 1819 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1820 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1821 | PySocket_htonl(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1822 | { |
| 1823 | int x1, x2; |
| 1824 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1825 | if (!PyArg_ParseTuple(args, "i:htonl", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1826 | return NULL; |
| 1827 | } |
| 1828 | x2 = htonl(x1); |
| 1829 | return PyInt_FromLong(x2); |
| 1830 | } |
| 1831 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1832 | static char htonl_doc[] = |
| 1833 | "htonl(integer) -> integer\n\ |
| 1834 | \n\ |
| 1835 | Convert a 32-bit integer from host to network byte order."; |
| 1836 | |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1837 | /* |
| 1838 | * socket.inet_aton() and socket.inet_ntoa() functions |
| 1839 | * |
| 1840 | * written 20 Aug 1999 by Ben Gertzfield <che@debian.org> <- blame him! |
| 1841 | * |
| 1842 | */ |
| 1843 | |
| 1844 | static char inet_aton_doc[] = |
| 1845 | "inet_aton(string) -> packed 32-bit IP representation\n\ |
| 1846 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 1847 | Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1848 | binary format used in low-level network functions."; |
| 1849 | |
| 1850 | static PyObject* |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1851 | PySocket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1852 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 1853 | #ifndef INADDR_NONE |
| 1854 | #define INADDR_NONE (-1) |
| 1855 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1856 | |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 1857 | /* Have to use inet_addr() instead */ |
| 1858 | char *ip_addr; |
| 1859 | long packed_addr; |
| 1860 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1861 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) { |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 1862 | return NULL; |
| 1863 | } |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1864 | #ifdef USE_GUSI1 |
Guido van Rossum | 1b6e463 | 1999-11-04 18:22:29 +0000 | [diff] [blame] | 1865 | packed_addr = (long)inet_addr(ip_addr).s_addr; |
| 1866 | #else |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 1867 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | 1b6e463 | 1999-11-04 18:22:29 +0000 | [diff] [blame] | 1868 | #endif |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 1869 | |
| 1870 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
| 1871 | PyErr_SetString(PySocket_Error, |
| 1872 | "illegal IP address string passed to inet_aton"); |
| 1873 | return NULL; |
| 1874 | } |
| 1875 | |
| 1876 | return PyString_FromStringAndSize((char *) &packed_addr, |
| 1877 | sizeof(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | static char inet_ntoa_doc[] = |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 1881 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1882 | \n\ |
| 1883 | Convert an IP address from 32-bit packed binary format to string format"; |
| 1884 | |
| 1885 | static PyObject* |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1886 | PySocket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1887 | { |
| 1888 | char *packed_str; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 1889 | int addr_len; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1890 | struct in_addr packed_addr; |
| 1891 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1892 | if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) { |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1893 | return NULL; |
| 1894 | } |
| 1895 | |
| 1896 | if (addr_len != sizeof(packed_addr)) { |
| 1897 | PyErr_SetString(PySocket_Error, |
| 1898 | "packed IP wrong length for inet_ntoa"); |
| 1899 | return NULL; |
| 1900 | } |
| 1901 | |
| 1902 | memcpy(&packed_addr, packed_str, addr_len); |
| 1903 | |
| 1904 | return PyString_FromString(inet_ntoa(packed_addr)); |
| 1905 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1906 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 1907 | |
| 1908 | #ifdef USE_SSL |
| 1909 | |
| 1910 | /* This is a C function to be called for new object initialization */ |
| 1911 | static SSLObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1912 | newSSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 1913 | { |
| 1914 | SSLObject *self; |
| 1915 | char *str; |
| 1916 | |
| 1917 | #if 0 |
| 1918 | meth=SSLv23_client_method(); |
| 1919 | meth=SSLv3_client_method(); |
| 1920 | meth=SSLv2_client_method(); |
| 1921 | #endif |
| 1922 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1923 | self = PyObject_New(SSLObject, &SSL_Type); /* Create new object */ |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 1924 | if (self == NULL){ |
| 1925 | PyErr_SetObject(SSLErrorObject, |
| 1926 | PyString_FromString("newSSLObject error")); |
| 1927 | return NULL; |
| 1928 | } |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1929 | memset(self->server, '\0', sizeof(char) * 256); |
| 1930 | memset(self->issuer, '\0', sizeof(char) * 256); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 1931 | |
| 1932 | self->x_attr = PyDict_New(); |
| 1933 | self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */ |
| 1934 | if (self->ctx == NULL) { |
| 1935 | PyErr_SetObject(SSLErrorObject, |
| 1936 | PyString_FromString("SSL_CTX_new error")); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1937 | PyObject_Del(self); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 1938 | return NULL; |
| 1939 | } |
| 1940 | |
| 1941 | if ( (key_file && !cert_file) || (!key_file && cert_file) ) |
| 1942 | { |
| 1943 | PyErr_SetObject(SSLErrorObject, |
| 1944 | PyString_FromString( |
| 1945 | "Both the key & certificate files must be specified")); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1946 | PyObject_Del(self); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 1947 | return NULL; |
| 1948 | } |
| 1949 | |
| 1950 | if (key_file && cert_file) |
| 1951 | { |
| 1952 | if (SSL_CTX_use_PrivateKey_file(self->ctx, key_file, |
| 1953 | SSL_FILETYPE_PEM) < 1) |
| 1954 | { |
| 1955 | PyErr_SetObject(SSLErrorObject, |
| 1956 | PyString_FromString( |
| 1957 | "SSL_CTX_use_PrivateKey_file error")); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1958 | PyObject_Del(self); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 1959 | return NULL; |
| 1960 | } |
| 1961 | |
| 1962 | if (SSL_CTX_use_certificate_chain_file(self->ctx, |
| 1963 | cert_file) < 1) |
| 1964 | { |
| 1965 | PyErr_SetObject(SSLErrorObject, |
| 1966 | PyString_FromString( |
| 1967 | "SSL_CTX_use_certificate_chain_file error")); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1968 | PyObject_Del(self); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 1969 | return NULL; |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | SSL_CTX_set_verify(self->ctx, |
| 1974 | SSL_VERIFY_NONE, NULL); /* set verify lvl */ |
| 1975 | self->ssl = SSL_new(self->ctx); /* New ssl struct */ |
| 1976 | SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */ |
| 1977 | SSL_set_connect_state(self->ssl); |
| 1978 | |
| 1979 | if ((SSL_connect(self->ssl)) == -1) { |
| 1980 | /* Actually negotiate SSL connection */ |
| 1981 | PyErr_SetObject(SSLErrorObject, |
| 1982 | PyString_FromString("SSL_connect error")); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1983 | PyObject_Del(self); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 1984 | return NULL; |
| 1985 | } |
| 1986 | self->ssl->debug = 1; |
| 1987 | |
| 1988 | if ((self->server_cert = SSL_get_peer_certificate(self->ssl))) { |
| 1989 | X509_NAME_oneline(X509_get_subject_name(self->server_cert), |
| 1990 | self->server, 256); |
| 1991 | X509_NAME_oneline(X509_get_issuer_name(self->server_cert), |
| 1992 | self->issuer, 256); |
| 1993 | } |
| 1994 | self->x_attr = NULL; |
| 1995 | self->Socket = Sock; |
| 1996 | Py_INCREF(self->Socket); |
| 1997 | return self; |
| 1998 | } |
| 1999 | |
| 2000 | /* This is the Python function called for new object initialization */ |
| 2001 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2002 | PySocket_ssl(PyObject *self, PyObject *args) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2003 | { |
| 2004 | SSLObject *rv; |
| 2005 | PySocketSockObject *Sock; |
| 2006 | char *key_file; |
| 2007 | char *cert_file; |
| 2008 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2009 | if (!PyArg_ParseTuple(args, "O!zz:ssl", |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2010 | &PySocketSock_Type, (PyObject*)&Sock, |
| 2011 | &key_file, &cert_file) ) |
| 2012 | return NULL; |
| 2013 | |
| 2014 | rv = newSSLObject(Sock, key_file, cert_file); |
| 2015 | if ( rv == NULL ) |
| 2016 | return NULL; |
| 2017 | return (PyObject *)rv; |
| 2018 | } |
| 2019 | |
| 2020 | static char ssl_doc[] = |
| 2021 | "ssl(socket, keyfile, certfile) -> sslobject"; |
| 2022 | |
| 2023 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2024 | SSL_server(SSLObject *self, PyObject *args) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2025 | { |
| 2026 | return PyString_FromString(self->server); |
| 2027 | } |
| 2028 | |
| 2029 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2030 | SSL_issuer(SSLObject *self, PyObject *args) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2031 | { |
| 2032 | return PyString_FromString(self->issuer); |
| 2033 | } |
| 2034 | |
| 2035 | |
| 2036 | /* SSL object methods */ |
| 2037 | |
| 2038 | static PyMethodDef SSLMethods[] = { |
| 2039 | { "write", (PyCFunction)SSL_SSLwrite, 1 }, |
| 2040 | { "read", (PyCFunction)SSL_SSLread, 1 }, |
| 2041 | { "server", (PyCFunction)SSL_server, 1 }, |
| 2042 | { "issuer", (PyCFunction)SSL_issuer, 1 }, |
| 2043 | { NULL, NULL} |
| 2044 | }; |
| 2045 | |
| 2046 | static void SSL_dealloc(SSLObject *self) |
| 2047 | { |
| 2048 | if (self->server_cert) /* Possible not to have one? */ |
| 2049 | X509_free (self->server_cert); |
| 2050 | SSL_CTX_free(self->ctx); |
| 2051 | SSL_free(self->ssl); |
| 2052 | Py_XDECREF(self->x_attr); |
| 2053 | Py_XDECREF(self->Socket); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2054 | PyObject_Del(self); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | static PyObject *SSL_getattr(SSLObject *self, char *name) |
| 2058 | { |
| 2059 | return Py_FindMethod(SSLMethods, (PyObject *)self, name); |
| 2060 | } |
| 2061 | |
| 2062 | staticforward PyTypeObject SSL_Type = { |
| 2063 | PyObject_HEAD_INIT(&PyType_Type) |
| 2064 | 0, /*ob_size*/ |
| 2065 | "SSL", /*tp_name*/ |
| 2066 | sizeof(SSLObject), /*tp_basicsize*/ |
| 2067 | 0, /*tp_itemsize*/ |
| 2068 | /* methods */ |
| 2069 | (destructor)SSL_dealloc, /*tp_dealloc*/ |
| 2070 | 0, /*tp_print*/ |
| 2071 | (getattrfunc)SSL_getattr, /*tp_getattr*/ |
| 2072 | 0, /*tp_setattr*/ |
| 2073 | 0, /*tp_compare*/ |
| 2074 | 0, /*tp_repr*/ |
| 2075 | 0, /*tp_as_number*/ |
| 2076 | 0, /*tp_as_sequence*/ |
| 2077 | 0, /*tp_as_mapping*/ |
| 2078 | 0, /*tp_hash*/ |
| 2079 | }; |
| 2080 | |
| 2081 | |
| 2082 | |
| 2083 | static PyObject *SSL_SSLwrite(SSLObject *self, PyObject *args) |
| 2084 | { |
| 2085 | char *data; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2086 | size_t len = 0; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2087 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2088 | if (!PyArg_ParseTuple(args, "s|i:write", &data, &len)) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2089 | return NULL; |
| 2090 | |
| 2091 | if (!len) |
| 2092 | len = strlen(data); |
| 2093 | |
| 2094 | len = SSL_write(self->ssl, data, len); |
| 2095 | return PyInt_FromLong((long)len); |
| 2096 | } |
| 2097 | |
| 2098 | static PyObject *SSL_SSLread(SSLObject *self, PyObject *args) |
| 2099 | { |
| 2100 | PyObject *buf; |
| 2101 | int count = 0; |
| 2102 | int len = 1024; |
| 2103 | int res; |
| 2104 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2105 | PyArg_ParseTuple(args, "|i:read", &len); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2106 | |
| 2107 | if (!(buf = PyString_FromStringAndSize((char *) 0, len))) |
| 2108 | return NULL; /* Error object should already be set */ |
| 2109 | |
| 2110 | count = SSL_read(self->ssl, PyString_AsString(buf), len); |
| 2111 | res = SSL_get_error(self->ssl, count); |
| 2112 | |
| 2113 | switch (res) { |
| 2114 | case 0: /* Good return value! */ |
| 2115 | break; |
| 2116 | case 6: |
| 2117 | PyErr_SetString(SSLErrorObject, "EOF"); |
| 2118 | Py_DECREF(buf); |
| 2119 | return NULL; |
| 2120 | break; |
| 2121 | case 5: |
| 2122 | default: |
| 2123 | return PyErr_SetFromErrno(SSLErrorObject); |
| 2124 | break; |
| 2125 | } |
| 2126 | |
| 2127 | fflush(stderr); |
| 2128 | |
| 2129 | if (count < 0) { |
| 2130 | Py_DECREF(buf); |
| 2131 | return PyErr_SetFromErrno(SSLErrorObject); |
| 2132 | } |
| 2133 | |
| 2134 | if (count != len && _PyString_Resize(&buf, count) < 0) |
| 2135 | return NULL; |
| 2136 | return buf; |
| 2137 | } |
| 2138 | |
| 2139 | #endif /* USE_SSL */ |
| 2140 | |
| 2141 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2142 | /* List of functions exported by this module. */ |
| 2143 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2144 | static PyMethodDef PySocket_methods[] = { |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2145 | {"gethostbyname", PySocket_gethostbyname, 1, gethostbyname_doc}, |
| 2146 | {"gethostbyname_ex", PySocket_gethostbyname_ex, 1, ghbn_ex_doc}, |
| 2147 | {"gethostbyaddr", PySocket_gethostbyaddr, 1, gethostbyaddr_doc}, |
| 2148 | {"gethostname", PySocket_gethostname, 1, gethostname_doc}, |
| 2149 | {"getservbyname", PySocket_getservbyname, 1, getservbyname_doc}, |
| 2150 | {"getprotobyname", PySocket_getprotobyname, 1,getprotobyname_doc}, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2151 | {"socket", PySocket_socket, 1, socket_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2152 | #ifndef NO_DUP |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2153 | {"fromfd", PySocket_fromfd, 1, fromfd_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2154 | #endif |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2155 | {"ntohs", PySocket_ntohs, 1, ntohs_doc}, |
| 2156 | {"ntohl", PySocket_ntohl, 1, ntohl_doc}, |
| 2157 | {"htons", PySocket_htons, 1, htons_doc}, |
| 2158 | {"htonl", PySocket_htonl, 1, htonl_doc}, |
| 2159 | {"inet_aton", PySocket_inet_aton, 1, inet_aton_doc}, |
| 2160 | {"inet_ntoa", PySocket_inet_ntoa, 1, inet_ntoa_doc}, |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2161 | #ifdef USE_SSL |
| 2162 | {"ssl", PySocket_ssl, 1, ssl_doc}, |
| 2163 | #endif /* USE_SSL */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2164 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2165 | }; |
| 2166 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2167 | |
| 2168 | /* Convenience routine to export an integer value. |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2169 | * |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2170 | * Errors are silently ignored, for better or for worse... |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2171 | */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2172 | static void |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2173 | insint(PyObject *d, char *name, int value) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2174 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2175 | PyObject *v = PyInt_FromLong((long) value); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2176 | if (!v || PyDict_SetItemString(d, name, v)) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2177 | PyErr_Clear(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2178 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2179 | Py_XDECREF(v); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2182 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2183 | #ifdef MS_WINDOWS |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2184 | |
| 2185 | /* Additional initialization and cleanup for NT/Windows */ |
| 2186 | |
| 2187 | static void |
| 2188 | NTcleanup() |
| 2189 | { |
| 2190 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | static int |
| 2194 | NTinit() |
| 2195 | { |
| 2196 | WSADATA WSAData; |
| 2197 | int ret; |
| 2198 | char buf[100]; |
| 2199 | ret = WSAStartup(0x0101, &WSAData); |
| 2200 | switch (ret) { |
| 2201 | case 0: /* no error */ |
| 2202 | atexit(NTcleanup); |
| 2203 | return 1; |
| 2204 | case WSASYSNOTREADY: |
| 2205 | PyErr_SetString(PyExc_ImportError, |
| 2206 | "WSAStartup failed: network not ready"); |
| 2207 | break; |
| 2208 | case WSAVERNOTSUPPORTED: |
| 2209 | case WSAEINVAL: |
| 2210 | PyErr_SetString(PyExc_ImportError, |
| 2211 | "WSAStartup failed: requested version not supported"); |
| 2212 | break; |
| 2213 | default: |
| 2214 | sprintf(buf, "WSAStartup failed: error code %d", ret); |
| 2215 | PyErr_SetString(PyExc_ImportError, buf); |
| 2216 | break; |
| 2217 | } |
| 2218 | return 0; |
| 2219 | } |
| 2220 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2221 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2222 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2223 | #if defined(PYOS_OS2) |
| 2224 | |
| 2225 | /* Additional initialization and cleanup for OS/2 */ |
| 2226 | |
| 2227 | static void |
| 2228 | OS2cleanup() |
| 2229 | { |
| 2230 | /* No cleanup is necessary for OS/2 Sockets */ |
| 2231 | } |
| 2232 | |
| 2233 | static int |
| 2234 | OS2init() |
| 2235 | { |
| 2236 | char reason[64]; |
| 2237 | int rc = sock_init(); |
| 2238 | |
| 2239 | if (rc == 0) { |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2240 | atexit(OS2cleanup); |
| 2241 | return 1; /* Indicate Success */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | sprintf(reason, "OS/2 TCP/IP Error# %d", sock_errno()); |
| 2245 | PyErr_SetString(PyExc_ImportError, reason); |
| 2246 | |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2247 | return 0; /* Indicate Failure */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | #endif /* PYOS_OS2 */ |
| 2251 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2252 | /* Initialize this module. |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2253 | * This is called when the first 'import socket' is done, |
| 2254 | * via a table in config.c, if config.c is compiled with USE_SOCKET |
| 2255 | * defined. |
| 2256 | * |
| 2257 | * For MS_WINDOWS (which means any Windows variant), this module |
| 2258 | * is actually called "_socket", and there's a wrapper "socket.py" |
| 2259 | * which implements some missing functionality (such as makefile(), |
| 2260 | * dup() and fromfd()). The import of "_socket" may fail with an |
| 2261 | * ImportError exception if initialization of WINSOCK fails. When |
| 2262 | * WINSOCK is initialized succesfully, a call to WSACleanup() is |
| 2263 | * scheduled to be made at exit time. |
| 2264 | * |
| 2265 | * For OS/2, this module is also called "_socket" and uses a wrapper |
| 2266 | * "socket.py" which implements that functionality that is missing |
| 2267 | * when PC operating systems don't put socket descriptors in the |
| 2268 | * operating system's filesystem layer. |
| 2269 | */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2270 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2271 | static char module_doc[] = |
| 2272 | "This module provides socket operations and some related functions.\n\ |
| 2273 | On Unix, it supports IP (Internet Protocol) and Unix domain sockets.\n\ |
| 2274 | On other systems, it only supports IP.\n\ |
| 2275 | \n\ |
| 2276 | Functions:\n\ |
| 2277 | \n\ |
| 2278 | socket() -- create a new socket object\n\ |
| 2279 | fromfd() -- create a socket object from an open file descriptor (*)\n\ |
| 2280 | gethostname() -- return the current hostname\n\ |
| 2281 | gethostbyname() -- map a hostname to its IP number\n\ |
| 2282 | gethostbyaddr() -- map an IP number or hostname to DNS info\n\ |
| 2283 | getservbyname() -- map a service name and a protocol name to a port number\n\ |
| 2284 | getprotobyname() -- mape a protocol name (e.g. 'tcp') to a number\n\ |
| 2285 | ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order\n\ |
| 2286 | htons(), htonl() -- convert 16, 32 bit int from host to network byte order\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 2287 | inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format\n\ |
| 2288 | inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)\n\ |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2289 | ssl() -- secure socket layer support (only available if configured)\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2290 | \n\ |
| 2291 | (*) not available on all platforms!)\n\ |
| 2292 | \n\ |
| 2293 | Special objects:\n\ |
| 2294 | \n\ |
| 2295 | SocketType -- type object for socket objects\n\ |
| 2296 | error -- exception raised for I/O errors\n\ |
| 2297 | \n\ |
| 2298 | Integer constants:\n\ |
| 2299 | \n\ |
| 2300 | AF_INET, AF_UNIX -- socket domains (first argument to socket() call)\n\ |
| 2301 | SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)\n\ |
| 2302 | \n\ |
| 2303 | Many other constants may be defined; these may be used in calls to\n\ |
| 2304 | the setsockopt() and getsockopt() methods.\n\ |
| 2305 | "; |
| 2306 | |
| 2307 | static char sockettype_doc[] = |
| 2308 | "A socket represents one endpoint of a network connection.\n\ |
| 2309 | \n\ |
| 2310 | Methods:\n\ |
| 2311 | \n\ |
| 2312 | accept() -- accept a connection, returning new socket and client address\n\ |
| 2313 | bind() -- bind the socket to a local address\n\ |
| 2314 | close() -- close the socket\n\ |
| 2315 | connect() -- connect the socket to a remote address\n\ |
| 2316 | connect_ex() -- connect, return an error code instead of an exception \n\ |
| 2317 | dup() -- return a new socket object identical to the current one (*)\n\ |
| 2318 | fileno() -- return underlying file descriptor\n\ |
| 2319 | getpeername() -- return remote address (*)\n\ |
| 2320 | getsockname() -- return local address\n\ |
| 2321 | getsockopt() -- get socket options\n\ |
| 2322 | listen() -- start listening for incoming connections\n\ |
| 2323 | makefile() -- return a file object corresponding tot the socket (*)\n\ |
| 2324 | recv() -- receive data\n\ |
| 2325 | recvfrom() -- receive data and sender's address\n\ |
| 2326 | send() -- send data\n\ |
| 2327 | sendto() -- send data to a given address\n\ |
| 2328 | setblocking() -- set or clear the blocking I/O flag\n\ |
| 2329 | setsockopt() -- set socket options\n\ |
| 2330 | shutdown() -- shut down traffic in one or both directions\n\ |
| 2331 | \n\ |
| 2332 | (*) not available on all platforms!)"; |
| 2333 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 2334 | DL_EXPORT(void) |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2335 | #if defined(MS_WINDOWS) || defined(PYOS_OS2) || defined(__BEOS__) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2336 | init_socket() |
| 2337 | #else |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2338 | initsocket() |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2339 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2340 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2341 | PyObject *m, *d; |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2342 | #ifdef MS_WINDOWS |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2343 | if (!NTinit()) |
| 2344 | return; |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2345 | m = Py_InitModule3("_socket", PySocket_methods, module_doc); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2346 | #else |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2347 | #if defined(__TOS_OS2__) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2348 | if (!OS2init()) |
| 2349 | return; |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2350 | m = Py_InitModule3("_socket", PySocket_methods, module_doc); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2351 | #else |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2352 | #if defined(__BEOS__) |
| 2353 | m = Py_InitModule3("_socket", PySocket_methods, module_doc); |
| 2354 | #else |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2355 | m = Py_InitModule3("socket", PySocket_methods, module_doc); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2356 | #endif /* __BEOS__ */ |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2357 | #endif |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2358 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2359 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2360 | PySocket_Error = PyErr_NewException("socket.error", NULL, NULL); |
| 2361 | if (PySocket_Error == NULL) |
| 2362 | return; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2363 | #ifdef USE_SSL |
| 2364 | SSL_load_error_strings(); |
| 2365 | SSLeay_add_ssl_algorithms(); |
| 2366 | SSLErrorObject = PyErr_NewException("socket.sslerror", NULL, NULL); |
| 2367 | if (SSLErrorObject == NULL) |
| 2368 | return; |
| 2369 | PyDict_SetItemString(d, "sslerror", SSLErrorObject); |
| 2370 | Py_INCREF(&SSL_Type); |
| 2371 | if (PyDict_SetItemString(d, "SSLType", |
| 2372 | (PyObject *)&SSL_Type) != 0) |
| 2373 | return; |
| 2374 | #endif /* USE_SSL */ |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2375 | PyDict_SetItemString(d, "error", PySocket_Error); |
Guido van Rossum | 3b4b6fc | 1997-05-21 14:37:37 +0000 | [diff] [blame] | 2376 | PySocketSock_Type.ob_type = &PyType_Type; |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2377 | PySocketSock_Type.tp_doc = sockettype_doc; |
Guido van Rossum | 3b4b6fc | 1997-05-21 14:37:37 +0000 | [diff] [blame] | 2378 | Py_INCREF(&PySocketSock_Type); |
Guido van Rossum | 91ba64d | 1997-06-02 22:18:09 +0000 | [diff] [blame] | 2379 | if (PyDict_SetItemString(d, "SocketType", |
Guido van Rossum | 3b4b6fc | 1997-05-21 14:37:37 +0000 | [diff] [blame] | 2380 | (PyObject *)&PySocketSock_Type) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2381 | return; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 2382 | |
| 2383 | /* Address families (we only support AF_INET and AF_UNIX) */ |
| 2384 | #ifdef AF_UNSPEC |
| 2385 | insint(d, "AF_UNSPEC", AF_UNSPEC); |
| 2386 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2387 | insint(d, "AF_INET", AF_INET); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2388 | #ifdef AF_UNIX |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2389 | insint(d, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2390 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 2391 | #ifdef AF_AX25 |
| 2392 | insint(d, "AF_AX25", AF_AX25); /* Amateur Radio AX.25 */ |
| 2393 | #endif |
| 2394 | #ifdef AF_IPX |
| 2395 | insint(d, "AF_IPX", AF_IPX); /* Novell IPX */ |
| 2396 | #endif |
| 2397 | #ifdef AF_APPLETALK |
| 2398 | insint(d, "AF_APPLETALK", AF_APPLETALK); /* Appletalk DDP */ |
| 2399 | #endif |
| 2400 | #ifdef AF_NETROM |
| 2401 | insint(d, "AF_NETROM", AF_NETROM); /* Amateur radio NetROM */ |
| 2402 | #endif |
| 2403 | #ifdef AF_BRIDGE |
| 2404 | insint(d, "AF_BRIDGE", AF_BRIDGE); /* Multiprotocol bridge */ |
| 2405 | #endif |
| 2406 | #ifdef AF_AAL5 |
| 2407 | insint(d, "AF_AAL5", AF_AAL5); /* Reserved for Werner's ATM */ |
| 2408 | #endif |
| 2409 | #ifdef AF_X25 |
| 2410 | insint(d, "AF_X25", AF_X25); /* Reserved for X.25 project */ |
| 2411 | #endif |
| 2412 | #ifdef AF_INET6 |
| 2413 | insint(d, "AF_INET6", AF_INET6); /* IP version 6 */ |
| 2414 | #endif |
| 2415 | #ifdef AF_ROSE |
| 2416 | insint(d, "AF_ROSE", AF_ROSE); /* Amateur Radio X.25 PLP */ |
| 2417 | #endif |
| 2418 | |
| 2419 | /* Socket types */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2420 | insint(d, "SOCK_STREAM", SOCK_STREAM); |
| 2421 | insint(d, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2422 | #ifndef __BEOS__ |
| 2423 | /* We have incomplete socket support. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2424 | insint(d, "SOCK_RAW", SOCK_RAW); |
| 2425 | insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
| 2426 | insint(d, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2427 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2428 | |
| 2429 | #ifdef SO_DEBUG |
| 2430 | insint(d, "SO_DEBUG", SO_DEBUG); |
| 2431 | #endif |
| 2432 | #ifdef SO_ACCEPTCONN |
| 2433 | insint(d, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
| 2434 | #endif |
| 2435 | #ifdef SO_REUSEADDR |
| 2436 | insint(d, "SO_REUSEADDR", SO_REUSEADDR); |
| 2437 | #endif |
| 2438 | #ifdef SO_KEEPALIVE |
| 2439 | insint(d, "SO_KEEPALIVE", SO_KEEPALIVE); |
| 2440 | #endif |
| 2441 | #ifdef SO_DONTROUTE |
| 2442 | insint(d, "SO_DONTROUTE", SO_DONTROUTE); |
| 2443 | #endif |
| 2444 | #ifdef SO_BROADCAST |
| 2445 | insint(d, "SO_BROADCAST", SO_BROADCAST); |
| 2446 | #endif |
| 2447 | #ifdef SO_USELOOPBACK |
| 2448 | insint(d, "SO_USELOOPBACK", SO_USELOOPBACK); |
| 2449 | #endif |
| 2450 | #ifdef SO_LINGER |
| 2451 | insint(d, "SO_LINGER", SO_LINGER); |
| 2452 | #endif |
| 2453 | #ifdef SO_OOBINLINE |
| 2454 | insint(d, "SO_OOBINLINE", SO_OOBINLINE); |
| 2455 | #endif |
| 2456 | #ifdef SO_REUSEPORT |
| 2457 | insint(d, "SO_REUSEPORT", SO_REUSEPORT); |
| 2458 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2459 | #ifdef SO_SNDBUF |
| 2460 | insint(d, "SO_SNDBUF", SO_SNDBUF); |
| 2461 | #endif |
| 2462 | #ifdef SO_RCVBUF |
| 2463 | insint(d, "SO_RCVBUF", SO_RCVBUF); |
| 2464 | #endif |
| 2465 | #ifdef SO_SNDLOWAT |
| 2466 | insint(d, "SO_SNDLOWAT", SO_SNDLOWAT); |
| 2467 | #endif |
| 2468 | #ifdef SO_RCVLOWAT |
| 2469 | insint(d, "SO_RCVLOWAT", SO_RCVLOWAT); |
| 2470 | #endif |
| 2471 | #ifdef SO_SNDTIMEO |
| 2472 | insint(d, "SO_SNDTIMEO", SO_SNDTIMEO); |
| 2473 | #endif |
| 2474 | #ifdef SO_RCVTIMEO |
| 2475 | insint(d, "SO_RCVTIMEO", SO_RCVTIMEO); |
| 2476 | #endif |
| 2477 | #ifdef SO_ERROR |
| 2478 | insint(d, "SO_ERROR", SO_ERROR); |
| 2479 | #endif |
| 2480 | #ifdef SO_TYPE |
| 2481 | insint(d, "SO_TYPE", SO_TYPE); |
| 2482 | #endif |
| 2483 | |
| 2484 | /* Maximum number of connections for "listen" */ |
| 2485 | #ifdef SOMAXCONN |
| 2486 | insint(d, "SOMAXCONN", SOMAXCONN); |
| 2487 | #else |
| 2488 | insint(d, "SOMAXCONN", 5); /* Common value */ |
| 2489 | #endif |
| 2490 | |
| 2491 | /* Flags for send, recv */ |
| 2492 | #ifdef MSG_OOB |
| 2493 | insint(d, "MSG_OOB", MSG_OOB); |
| 2494 | #endif |
| 2495 | #ifdef MSG_PEEK |
| 2496 | insint(d, "MSG_PEEK", MSG_PEEK); |
| 2497 | #endif |
| 2498 | #ifdef MSG_DONTROUTE |
| 2499 | insint(d, "MSG_DONTROUTE", MSG_DONTROUTE); |
| 2500 | #endif |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 2501 | #ifdef MSG_DONTWAIT |
Guido van Rossum | 14f3f59 | 2000-04-25 21:53:58 +0000 | [diff] [blame] | 2502 | insint(d, "MSG_DONTWAIT", MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 2503 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2504 | #ifdef MSG_EOR |
| 2505 | insint(d, "MSG_EOR", MSG_EOR); |
| 2506 | #endif |
| 2507 | #ifdef MSG_TRUNC |
| 2508 | insint(d, "MSG_TRUNC", MSG_TRUNC); |
| 2509 | #endif |
| 2510 | #ifdef MSG_CTRUNC |
| 2511 | insint(d, "MSG_CTRUNC", MSG_CTRUNC); |
| 2512 | #endif |
| 2513 | #ifdef MSG_WAITALL |
| 2514 | insint(d, "MSG_WAITALL", MSG_WAITALL); |
| 2515 | #endif |
| 2516 | #ifdef MSG_BTAG |
| 2517 | insint(d, "MSG_BTAG", MSG_BTAG); |
| 2518 | #endif |
| 2519 | #ifdef MSG_ETAG |
| 2520 | insint(d, "MSG_ETAG", MSG_ETAG); |
| 2521 | #endif |
| 2522 | |
| 2523 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 2524 | #ifdef SOL_SOCKET |
| 2525 | insint(d, "SOL_SOCKET", SOL_SOCKET); |
| 2526 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 2527 | #ifdef SOL_IP |
| 2528 | insint(d, "SOL_IP", SOL_IP); |
| 2529 | #else |
| 2530 | insint(d, "SOL_IP", 0); |
| 2531 | #endif |
| 2532 | #ifdef SOL_IPX |
| 2533 | insint(d, "SOL_IPX", SOL_IPX); |
| 2534 | #endif |
| 2535 | #ifdef SOL_AX25 |
| 2536 | insint(d, "SOL_AX25", SOL_AX25); |
| 2537 | #endif |
| 2538 | #ifdef SOL_ATALK |
| 2539 | insint(d, "SOL_ATALK", SOL_ATALK); |
| 2540 | #endif |
| 2541 | #ifdef SOL_NETROM |
| 2542 | insint(d, "SOL_NETROM", SOL_NETROM); |
| 2543 | #endif |
| 2544 | #ifdef SOL_ROSE |
| 2545 | insint(d, "SOL_ROSE", SOL_ROSE); |
| 2546 | #endif |
| 2547 | #ifdef SOL_TCP |
| 2548 | insint(d, "SOL_TCP", SOL_TCP); |
| 2549 | #else |
| 2550 | insint(d, "SOL_TCP", 6); |
| 2551 | #endif |
| 2552 | #ifdef SOL_UDP |
| 2553 | insint(d, "SOL_UDP", SOL_UDP); |
| 2554 | #else |
| 2555 | insint(d, "SOL_UDP", 17); |
| 2556 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2557 | #ifdef IPPROTO_IP |
| 2558 | insint(d, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 2559 | #else |
| 2560 | insint(d, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2561 | #endif |
| 2562 | #ifdef IPPROTO_ICMP |
| 2563 | insint(d, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 2564 | #else |
| 2565 | insint(d, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2566 | #endif |
| 2567 | #ifdef IPPROTO_IGMP |
| 2568 | insint(d, "IPPROTO_IGMP", IPPROTO_IGMP); |
| 2569 | #endif |
| 2570 | #ifdef IPPROTO_GGP |
| 2571 | insint(d, "IPPROTO_GGP", IPPROTO_GGP); |
| 2572 | #endif |
| 2573 | #ifdef IPPROTO_TCP |
| 2574 | insint(d, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 2575 | #else |
| 2576 | insint(d, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2577 | #endif |
| 2578 | #ifdef IPPROTO_EGP |
| 2579 | insint(d, "IPPROTO_EGP", IPPROTO_EGP); |
| 2580 | #endif |
| 2581 | #ifdef IPPROTO_PUP |
| 2582 | insint(d, "IPPROTO_PUP", IPPROTO_PUP); |
| 2583 | #endif |
| 2584 | #ifdef IPPROTO_UDP |
| 2585 | insint(d, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 2586 | #else |
| 2587 | insint(d, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2588 | #endif |
| 2589 | #ifdef IPPROTO_IDP |
| 2590 | insint(d, "IPPROTO_IDP", IPPROTO_IDP); |
| 2591 | #endif |
| 2592 | #ifdef IPPROTO_HELLO |
| 2593 | insint(d, "IPPROTO_HELLO", IPPROTO_HELLO); |
| 2594 | #endif |
| 2595 | #ifdef IPPROTO_ND |
| 2596 | insint(d, "IPPROTO_ND", IPPROTO_ND); |
| 2597 | #endif |
| 2598 | #ifdef IPPROTO_TP |
| 2599 | insint(d, "IPPROTO_TP", IPPROTO_TP); |
| 2600 | #endif |
| 2601 | #ifdef IPPROTO_XTP |
| 2602 | insint(d, "IPPROTO_XTP", IPPROTO_XTP); |
| 2603 | #endif |
| 2604 | #ifdef IPPROTO_EON |
| 2605 | insint(d, "IPPROTO_EON", IPPROTO_EON); |
| 2606 | #endif |
| 2607 | #ifdef IPPROTO_BIP |
| 2608 | insint(d, "IPPROTO_BIP", IPPROTO_BIP); |
| 2609 | #endif |
| 2610 | /**/ |
| 2611 | #ifdef IPPROTO_RAW |
| 2612 | insint(d, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 2613 | #else |
| 2614 | insint(d, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2615 | #endif |
| 2616 | #ifdef IPPROTO_MAX |
| 2617 | insint(d, "IPPROTO_MAX", IPPROTO_MAX); |
| 2618 | #endif |
| 2619 | |
| 2620 | /* Some port configuration */ |
| 2621 | #ifdef IPPORT_RESERVED |
| 2622 | insint(d, "IPPORT_RESERVED", IPPORT_RESERVED); |
| 2623 | #else |
| 2624 | insint(d, "IPPORT_RESERVED", 1024); |
| 2625 | #endif |
| 2626 | #ifdef IPPORT_USERRESERVED |
| 2627 | insint(d, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
| 2628 | #else |
| 2629 | insint(d, "IPPORT_USERRESERVED", 5000); |
| 2630 | #endif |
| 2631 | |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 2632 | /* Some reserved IP v.4 addresses */ |
| 2633 | #ifdef INADDR_ANY |
| 2634 | insint(d, "INADDR_ANY", INADDR_ANY); |
| 2635 | #else |
| 2636 | insint(d, "INADDR_ANY", 0x00000000); |
| 2637 | #endif |
| 2638 | #ifdef INADDR_BROADCAST |
| 2639 | insint(d, "INADDR_BROADCAST", INADDR_BROADCAST); |
| 2640 | #else |
| 2641 | insint(d, "INADDR_BROADCAST", 0xffffffff); |
| 2642 | #endif |
| 2643 | #ifdef INADDR_LOOPBACK |
| 2644 | insint(d, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
| 2645 | #else |
| 2646 | insint(d, "INADDR_LOOPBACK", 0x7F000001); |
| 2647 | #endif |
| 2648 | #ifdef INADDR_UNSPEC_GROUP |
| 2649 | insint(d, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
| 2650 | #else |
| 2651 | insint(d, "INADDR_UNSPEC_GROUP", 0xe0000000); |
| 2652 | #endif |
| 2653 | #ifdef INADDR_ALLHOSTS_GROUP |
| 2654 | insint(d, "INADDR_ALLHOSTS_GROUP", INADDR_ALLHOSTS_GROUP); |
| 2655 | #else |
| 2656 | insint(d, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
| 2657 | #endif |
| 2658 | #ifdef INADDR_MAX_LOCAL_GROUP |
| 2659 | insint(d, "INADDR_MAX_LOCAL_GROUP", INADDR_MAX_LOCAL_GROUP); |
| 2660 | #else |
| 2661 | insint(d, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
| 2662 | #endif |
| 2663 | #ifdef INADDR_NONE |
| 2664 | insint(d, "INADDR_NONE", INADDR_NONE); |
| 2665 | #else |
| 2666 | insint(d, "INADDR_NONE", 0xffffffff); |
| 2667 | #endif |
| 2668 | |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2669 | /* IP [gs]etsockopt options */ |
| 2670 | #ifdef IP_OPTIONS |
| 2671 | insint(d, "IP_OPTIONS", IP_OPTIONS); |
| 2672 | #endif |
| 2673 | #ifdef IP_HDRINCL |
| 2674 | insint(d, "IP_HDRINCL", IP_HDRINCL); |
| 2675 | #endif |
| 2676 | #ifdef IP_TOS |
| 2677 | insint(d, "IP_TOS", IP_TOS); |
| 2678 | #endif |
| 2679 | #ifdef IP_TTL |
| 2680 | insint(d, "IP_TTL", IP_TTL); |
| 2681 | #endif |
| 2682 | #ifdef IP_RECVOPTS |
| 2683 | insint(d, "IP_RECVOPTS", IP_RECVOPTS); |
| 2684 | #endif |
| 2685 | #ifdef IP_RECVRETOPTS |
| 2686 | insint(d, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
| 2687 | #endif |
| 2688 | #ifdef IP_RECVDSTADDR |
| 2689 | insint(d, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
| 2690 | #endif |
| 2691 | #ifdef IP_RETOPTS |
| 2692 | insint(d, "IP_RETOPTS", IP_RETOPTS); |
| 2693 | #endif |
| 2694 | #ifdef IP_MULTICAST_IF |
| 2695 | insint(d, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
| 2696 | #endif |
| 2697 | #ifdef IP_MULTICAST_TTL |
| 2698 | insint(d, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
| 2699 | #endif |
| 2700 | #ifdef IP_MULTICAST_LOOP |
| 2701 | insint(d, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
| 2702 | #endif |
| 2703 | #ifdef IP_ADD_MEMBERSHIP |
| 2704 | insint(d, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
| 2705 | #endif |
| 2706 | #ifdef IP_DROP_MEMBERSHIP |
| 2707 | insint(d, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
| 2708 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 2709 | #ifdef IP_DEFAULT_MULTICAST_TTL |
| 2710 | insint(d, "IP_DEFAULT_MULTICAST_TTL", IP_DEFAULT_MULTICAST_TTL); |
| 2711 | #endif |
| 2712 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
| 2713 | insint(d, "IP_DEFAULT_MULTICAST_LOOP", IP_DEFAULT_MULTICAST_LOOP); |
| 2714 | #endif |
| 2715 | #ifdef IP_MAX_MEMBERSHIPS |
| 2716 | insint(d, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
| 2717 | #endif |
| 2718 | |
| 2719 | /* TCP options */ |
| 2720 | #ifdef TCP_NODELAY |
| 2721 | insint(d, "TCP_NODELAY", TCP_NODELAY); |
| 2722 | #endif |
| 2723 | #ifdef TCP_MAXSEG |
| 2724 | insint(d, "TCP_MAXSEG", TCP_MAXSEG); |
| 2725 | #endif |
| 2726 | |
| 2727 | /* IPX options */ |
| 2728 | #ifdef IPX_TYPE |
| 2729 | insint(d, "IPX_TYPE", IPX_TYPE); |
| 2730 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2731 | |
| 2732 | /* Initialize gethostbyname lock */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2733 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 2734 | gethostbyname_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2735 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2736 | } |