]>
Commit | Line | Data |
---|---|---|
7eb75bcc DM |
1 | /* Socket module */\r |
2 | \r | |
3 | /*\r | |
4 | \r | |
5 | This module provides an interface to Berkeley socket IPC.\r | |
6 | \r | |
7 | Limitations:\r | |
8 | \r | |
9 | - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a\r | |
10 | portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported\r | |
11 | under Linux.\r | |
12 | - No read/write operations (use sendall/recv or makefile instead).\r | |
13 | - Additional restrictions apply on some non-Unix platforms (compensated\r | |
14 | for by socket.py).\r | |
15 | \r | |
16 | Module interface:\r | |
17 | \r | |
18 | - socket.error: exception raised for socket specific errors\r | |
19 | - socket.gaierror: exception raised for getaddrinfo/getnameinfo errors,\r | |
20 | a subclass of socket.error\r | |
21 | - socket.herror: exception raised for gethostby* errors,\r | |
22 | a subclass of socket.error\r | |
23 | - socket.fromfd(fd, family, type[, proto]) --> new socket object (created\r | |
24 | from an existing file descriptor)\r | |
25 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')\r | |
26 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])\r | |
27 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')\r | |
28 | - socket.getprotobyname(protocolname) --> protocol number\r | |
29 | - socket.getservbyname(servicename[, protocolname]) --> port number\r | |
30 | - socket.getservbyport(portnumber[, protocolname]) --> service name\r | |
31 | - socket.socket([family[, type [, proto]]]) --> new socket object\r | |
32 | - socket.socketpair([family[, type [, proto]]]) --> (socket, socket)\r | |
33 | - socket.ntohs(16 bit value) --> new int object\r | |
34 | - socket.ntohl(32 bit value) --> new int object\r | |
35 | - socket.htons(16 bit value) --> new int object\r | |
36 | - socket.htonl(32 bit value) --> new int object\r | |
37 | - socket.getaddrinfo(host, port [, family, socktype, proto, flags])\r | |
38 | --> List of (family, socktype, proto, canonname, sockaddr)\r | |
39 | - socket.getnameinfo(sockaddr, flags) --> (host, port)\r | |
40 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>\r | |
41 | - socket.has_ipv6: boolean value indicating if IPv6 is supported\r | |
42 | - socket.inet_aton(IP address) -> 32-bit packed IP representation\r | |
43 | - socket.inet_ntoa(packed IP) -> IP address string\r | |
44 | - socket.getdefaulttimeout() -> None | float\r | |
45 | - socket.setdefaulttimeout(None | float)\r | |
46 | - an Internet socket address is a pair (hostname, port)\r | |
47 | where hostname can be anything recognized by gethostbyname()\r | |
48 | (including the dd.dd.dd.dd notation) and port is in host byte order\r | |
49 | - where a hostname is returned, the dd.dd.dd.dd notation is used\r | |
50 | - a UNIX domain socket address is a string specifying the pathname\r | |
51 | - an AF_PACKET socket address is a tuple containing a string\r | |
52 | specifying the ethernet interface and an integer specifying\r | |
53 | the Ethernet protocol number to be received. For example:\r | |
54 | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple\r | |
55 | specify packet-type and ha-type/addr.\r | |
56 | - an AF_TIPC socket address is expressed as\r | |
57 | (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of:\r | |
58 | TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID;\r | |
59 | and scope can be one of:\r | |
60 | TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE.\r | |
61 | The meaning of v1, v2 and v3 depends on the value of addr_type:\r | |
62 | if addr_type is TIPC_ADDR_NAME:\r | |
63 | v1 is the server type\r | |
64 | v2 is the port identifier\r | |
65 | v3 is ignored\r | |
66 | if addr_type is TIPC_ADDR_NAMESEQ:\r | |
67 | v1 is the server type\r | |
68 | v2 is the lower port number\r | |
69 | v3 is the upper port number\r | |
70 | if addr_type is TIPC_ADDR_ID:\r | |
71 | v1 is the node\r | |
72 | v2 is the ref\r | |
73 | v3 is ignored\r | |
74 | \r | |
75 | \r | |
76 | Local naming conventions:\r | |
77 | \r | |
78 | - names starting with sock_ are socket object methods\r | |
79 | - names starting with socket_ are module-level functions\r | |
80 | - names starting with PySocket are exported through socketmodule.h\r | |
81 | \r | |
82 | */\r | |
83 | \r | |
84 | #ifdef __APPLE__\r | |
85 | /*\r | |
86 | * inet_aton is not available on OSX 10.3, yet we want to use a binary\r | |
87 | * that was build on 10.4 or later to work on that release, weak linking\r | |
88 | * comes to the rescue.\r | |
89 | */\r | |
90 | # pragma weak inet_aton\r | |
91 | #endif\r | |
92 | \r | |
93 | #include "Python.h"\r | |
94 | #include "structmember.h"\r | |
95 | #include "timefuncs.h"\r | |
96 | \r | |
97 | #ifndef INVALID_SOCKET /* MS defines this */\r | |
98 | #define INVALID_SOCKET (-1)\r | |
99 | #endif\r | |
100 | \r | |
101 | #undef MAX\r | |
102 | #define MAX(x, y) ((x) < (y) ? (y) : (x))\r | |
103 | \r | |
104 | /* Socket object documentation */\r | |
105 | PyDoc_STRVAR(sock_doc,\r | |
106 | "socket([family[, type[, proto]]]) -> socket object\n\\r | |
107 | \n\\r | |
108 | Open a socket of the given type. The family argument specifies the\n\\r | |
109 | address family; it defaults to AF_INET. The type argument specifies\n\\r | |
110 | whether this is a stream (SOCK_STREAM, this is the default)\n\\r | |
111 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\\r | |
112 | specifying the default protocol. Keyword arguments are accepted.\n\\r | |
113 | \n\\r | |
114 | A socket object represents one endpoint of a network connection.\n\\r | |
115 | \n\\r | |
116 | Methods of socket objects (keyword arguments not allowed):\n\\r | |
117 | \n\\r | |
118 | accept() -- accept a connection, returning new socket and client address\n\\r | |
119 | bind(addr) -- bind the socket to a local address\n\\r | |
120 | close() -- close the socket\n\\r | |
121 | connect(addr) -- connect the socket to a remote address\n\\r | |
122 | connect_ex(addr) -- connect, return an error code instead of an exception\n\\r | |
123 | dup() -- return a new socket object identical to the current one [*]\n\\r | |
124 | fileno() -- return underlying file descriptor\n\\r | |
125 | getpeername() -- return remote address [*]\n\\r | |
126 | getsockname() -- return local address\n\\r | |
127 | getsockopt(level, optname[, buflen]) -- get socket options\n\\r | |
128 | gettimeout() -- return timeout or None\n\\r | |
129 | listen(n) -- start listening for incoming connections\n\\r | |
130 | makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\\r | |
131 | recv(buflen[, flags]) -- receive data\n\\r | |
132 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\\r | |
133 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\\r | |
134 | recvfrom_into(buffer[, nbytes, [, flags])\n\\r | |
135 | -- receive data and sender\'s address (into a buffer)\n\\r | |
136 | sendall(data[, flags]) -- send all data\n\\r | |
137 | send(data[, flags]) -- send data, may not send all of it\n\\r | |
138 | sendto(data[, flags], addr) -- send data to a given address\n\\r | |
139 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\\r | |
140 | setsockopt(level, optname, value) -- set socket options\n\\r | |
141 | settimeout(None | float) -- set or clear the timeout\n\\r | |
142 | shutdown(how) -- shut down traffic in one or both directions\n\\r | |
143 | \n\\r | |
144 | [*] not available on all platforms!");\r | |
145 | \r | |
146 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks.\r | |
147 | I hope some day someone can clean this up please... */\r | |
148 | \r | |
149 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure\r | |
150 | script doesn't get this right, so we hardcode some platform checks below.\r | |
151 | On the other hand, not all Linux versions agree, so there the settings\r | |
152 | computed by the configure script are needed! */\r | |
153 | \r | |
154 | #ifndef linux\r | |
155 | # undef HAVE_GETHOSTBYNAME_R_3_ARG\r | |
156 | # undef HAVE_GETHOSTBYNAME_R_5_ARG\r | |
157 | # undef HAVE_GETHOSTBYNAME_R_6_ARG\r | |
158 | #endif\r | |
159 | \r | |
160 | #ifndef WITH_THREAD\r | |
161 | # undef HAVE_GETHOSTBYNAME_R\r | |
162 | #endif\r | |
163 | \r | |
164 | #ifdef HAVE_GETHOSTBYNAME_R\r | |
165 | # if defined(_AIX) || defined(__osf__)\r | |
166 | # define HAVE_GETHOSTBYNAME_R_3_ARG\r | |
167 | # elif defined(__sun) || defined(__sgi)\r | |
168 | # define HAVE_GETHOSTBYNAME_R_5_ARG\r | |
169 | # elif defined(linux)\r | |
170 | /* Rely on the configure script */\r | |
171 | # else\r | |
172 | # undef HAVE_GETHOSTBYNAME_R\r | |
173 | # endif\r | |
174 | #endif\r | |
175 | \r | |
176 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \\r | |
177 | !defined(MS_WINDOWS)\r | |
178 | # define USE_GETHOSTBYNAME_LOCK\r | |
179 | #endif\r | |
180 | \r | |
181 | /* To use __FreeBSD_version */\r | |
182 | #ifdef HAVE_SYS_PARAM_H\r | |
183 | #include <sys/param.h>\r | |
184 | #endif\r | |
185 | /* On systems on which getaddrinfo() is believed to not be thread-safe,\r | |
186 | (this includes the getaddrinfo emulation) protect access with a lock. */\r | |
187 | #if defined(WITH_THREAD) && (defined(__APPLE__) || \\r | |
188 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \\r | |
189 | defined(__OpenBSD__) || defined(__NetBSD__) || \\r | |
190 | defined(__VMS) || !defined(HAVE_GETADDRINFO))\r | |
191 | #define USE_GETADDRINFO_LOCK\r | |
192 | #endif\r | |
193 | \r | |
194 | #ifdef USE_GETADDRINFO_LOCK\r | |
195 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1);\r | |
196 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock);\r | |
197 | #else\r | |
198 | #define ACQUIRE_GETADDRINFO_LOCK\r | |
199 | #define RELEASE_GETADDRINFO_LOCK\r | |
200 | #endif\r | |
201 | \r | |
202 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)\r | |
203 | # include "pythread.h"\r | |
204 | #endif\r | |
205 | \r | |
206 | #if defined(PYCC_VACPP)\r | |
207 | # include <types.h>\r | |
208 | # include <io.h>\r | |
209 | # include <sys/ioctl.h>\r | |
210 | # include <utils.h>\r | |
211 | # include <ctype.h>\r | |
212 | #endif\r | |
213 | \r | |
214 | #if defined(__VMS)\r | |
215 | # include <ioctl.h>\r | |
216 | #endif\r | |
217 | \r | |
218 | #if defined(PYOS_OS2)\r | |
219 | # define INCL_DOS\r | |
220 | # define INCL_DOSERRORS\r | |
221 | # define INCL_NOPMAPI\r | |
222 | # include <os2.h>\r | |
223 | #endif\r | |
224 | \r | |
225 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI\r | |
226 | /* make sure that the reentrant (gethostbyaddr_r etc)\r | |
227 | functions are declared correctly if compiling with\r | |
228 | MIPSPro 7.x in ANSI C mode (default) */\r | |
229 | \r | |
230 | /* XXX Using _SGIAPI is the wrong thing,\r | |
231 | but I don't know what the right thing is. */\r | |
232 | #undef _SGIAPI /* to avoid warning */\r | |
233 | #define _SGIAPI 1\r | |
234 | \r | |
235 | #undef _XOPEN_SOURCE\r | |
236 | #include <sys/socket.h>\r | |
237 | #include <sys/types.h>\r | |
238 | #include <netinet/in.h>\r | |
239 | #ifdef _SS_ALIGNSIZE\r | |
240 | #define HAVE_GETADDRINFO 1\r | |
241 | #define HAVE_GETNAMEINFO 1\r | |
242 | #endif\r | |
243 | \r | |
244 | #define HAVE_INET_PTON\r | |
245 | #include <netdb.h>\r | |
246 | #endif\r | |
247 | \r | |
248 | /* Irix 6.5 fails to define this variable at all. This is needed\r | |
249 | for both GCC and SGI's compiler. I'd say that the SGI headers\r | |
250 | are just busted. Same thing for Solaris. */\r | |
251 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN)\r | |
252 | #define INET_ADDRSTRLEN 16\r | |
253 | #endif\r | |
254 | \r | |
255 | /* Generic includes */\r | |
256 | #ifdef HAVE_SYS_TYPES_H\r | |
257 | #include <sys/types.h>\r | |
258 | #endif\r | |
259 | \r | |
260 | /* Generic socket object definitions and includes */\r | |
261 | #define PySocket_BUILDING_SOCKET\r | |
262 | #include "socketmodule.h"\r | |
263 | \r | |
264 | /* Addressing includes */\r | |
265 | \r | |
266 | #ifndef MS_WINDOWS\r | |
267 | \r | |
268 | /* Non-MS WINDOWS includes */\r | |
269 | # include <netdb.h>\r | |
270 | \r | |
271 | /* Headers needed for inet_ntoa() and inet_addr() */\r | |
272 | # ifdef __BEOS__\r | |
273 | # include <net/netdb.h>\r | |
274 | # elif defined(PYOS_OS2) && defined(PYCC_VACPP)\r | |
275 | # include <netdb.h>\r | |
276 | typedef size_t socklen_t;\r | |
277 | # else\r | |
278 | # include <arpa/inet.h>\r | |
279 | # endif\r | |
280 | \r | |
281 | # ifndef RISCOS\r | |
282 | # include <fcntl.h>\r | |
283 | # else\r | |
284 | # include <sys/ioctl.h>\r | |
285 | # include <socklib.h>\r | |
286 | # define NO_DUP\r | |
287 | int h_errno; /* not used */\r | |
288 | # define INET_ADDRSTRLEN 16\r | |
289 | # endif\r | |
290 | \r | |
291 | #else\r | |
292 | \r | |
293 | /* MS_WINDOWS includes */\r | |
294 | # ifdef HAVE_FCNTL_H\r | |
295 | # include <fcntl.h>\r | |
296 | # endif\r | |
297 | \r | |
298 | #endif\r | |
299 | \r | |
300 | #include <stddef.h>\r | |
301 | \r | |
302 | #ifndef offsetof\r | |
303 | # define offsetof(type, member) ((size_t)(&((type *)0)->member))\r | |
304 | #endif\r | |
305 | \r | |
306 | #ifndef O_NONBLOCK\r | |
307 | # define O_NONBLOCK O_NDELAY\r | |
308 | #endif\r | |
309 | \r | |
310 | /* include Python's addrinfo.h unless it causes trouble */\r | |
311 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE)\r | |
312 | /* Do not include addinfo.h on some newer IRIX versions.\r | |
313 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21,\r | |
314 | * for example, but not by 6.5.10.\r | |
315 | */\r | |
316 | #elif defined(_MSC_VER) && _MSC_VER>1201\r | |
317 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and\r | |
318 | * EAI_* constants are defined in (the already included) ws2tcpip.h.\r | |
319 | */\r | |
320 | #else\r | |
321 | # include "addrinfo.h"\r | |
322 | #endif\r | |
323 | \r | |
324 | #ifndef HAVE_INET_PTON\r | |
325 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)\r | |
326 | int inet_pton(int af, const char *src, void *dst);\r | |
327 | const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);\r | |
328 | #endif\r | |
329 | #endif\r | |
330 | \r | |
331 | #ifdef __APPLE__\r | |
332 | /* On OS X, getaddrinfo returns no error indication of lookup\r | |
333 | failure, so we must use the emulation instead of the libinfo\r | |
334 | implementation. Unfortunately, performing an autoconf test\r | |
335 | for this bug would require DNS access for the machine performing\r | |
336 | the configuration, which is not acceptable. Therefore, we\r | |
337 | determine the bug just by checking for __APPLE__. If this bug\r | |
338 | gets ever fixed, perhaps checking for sys/version.h would be\r | |
339 | appropriate, which is 10/0 on the system with the bug. */\r | |
340 | #ifndef HAVE_GETNAMEINFO\r | |
341 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could\r | |
342 | Find to check for Jaguar is that it has getnameinfo(), which\r | |
343 | older releases don't have */\r | |
344 | #undef HAVE_GETADDRINFO\r | |
345 | #endif\r | |
346 | \r | |
347 | #ifdef HAVE_INET_ATON\r | |
348 | #define USE_INET_ATON_WEAKLINK\r | |
349 | #endif\r | |
350 | \r | |
351 | #endif\r | |
352 | \r | |
353 | /* I know this is a bad practice, but it is the easiest... */\r | |
354 | #if !defined(HAVE_GETADDRINFO)\r | |
355 | /* avoid clashes with the C library definition of the symbol. */\r | |
356 | #define getaddrinfo fake_getaddrinfo\r | |
357 | #define gai_strerror fake_gai_strerror\r | |
358 | #define freeaddrinfo fake_freeaddrinfo\r | |
359 | #include "getaddrinfo.c"\r | |
360 | #endif\r | |
361 | #if !defined(HAVE_GETNAMEINFO)\r | |
362 | #define getnameinfo fake_getnameinfo\r | |
363 | #include "getnameinfo.c"\r | |
364 | #endif\r | |
365 | \r | |
366 | #if defined(MS_WINDOWS) || defined(__BEOS__)\r | |
367 | /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */\r | |
368 | /* seem to be a few differences in the API */\r | |
369 | #define SOCKETCLOSE closesocket\r | |
370 | #define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */\r | |
371 | #endif\r | |
372 | \r | |
373 | #ifdef MS_WIN32\r | |
374 | #define EAFNOSUPPORT WSAEAFNOSUPPORT\r | |
375 | #define snprintf _snprintf\r | |
376 | #endif\r | |
377 | \r | |
378 | #if defined(PYOS_OS2) && !defined(PYCC_GCC)\r | |
379 | #define SOCKETCLOSE soclose\r | |
380 | #define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */\r | |
381 | #endif\r | |
382 | \r | |
383 | #ifndef SOCKETCLOSE\r | |
384 | #define SOCKETCLOSE close\r | |
385 | #endif\r | |
386 | \r | |
387 | #if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__)\r | |
388 | #define USE_BLUETOOTH 1\r | |
389 | #if defined(__FreeBSD__)\r | |
390 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP\r | |
391 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM\r | |
392 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI\r | |
393 | #define SOL_HCI SOL_HCI_RAW\r | |
394 | #define HCI_FILTER SO_HCI_RAW_FILTER\r | |
395 | #define sockaddr_l2 sockaddr_l2cap\r | |
396 | #define sockaddr_rc sockaddr_rfcomm\r | |
397 | #define hci_dev hci_node\r | |
398 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)\r | |
399 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)\r | |
400 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)\r | |
401 | #elif defined(__NetBSD__) || defined(__DragonFly__)\r | |
402 | #define sockaddr_l2 sockaddr_bt\r | |
403 | #define sockaddr_rc sockaddr_bt\r | |
404 | #define sockaddr_hci sockaddr_bt\r | |
405 | #define sockaddr_sco sockaddr_bt\r | |
406 | #define SOL_HCI BTPROTO_HCI\r | |
407 | #define HCI_DATA_DIR SO_HCI_DIRECTION\r | |
408 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)\r | |
409 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)\r | |
410 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)\r | |
411 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)\r | |
412 | #else\r | |
413 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)\r | |
414 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)\r | |
415 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)\r | |
416 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)\r | |
417 | #endif\r | |
418 | #endif\r | |
419 | \r | |
420 | #ifdef __VMS\r | |
421 | /* TCP/IP Services for VMS uses a maximum send/recv buffer length */\r | |
422 | #define SEGMENT_SIZE (32 * 1024 -1)\r | |
423 | #endif\r | |
424 | \r | |
425 | #define SAS2SA(x) ((struct sockaddr *)(x))\r | |
426 | \r | |
427 | /*\r | |
428 | * Constants for getnameinfo()\r | |
429 | */\r | |
430 | #if !defined(NI_MAXHOST)\r | |
431 | #define NI_MAXHOST 1025\r | |
432 | #endif\r | |
433 | #if !defined(NI_MAXSERV)\r | |
434 | #define NI_MAXSERV 32\r | |
435 | #endif\r | |
436 | \r | |
437 | /* XXX There's a problem here: *static* functions are not supposed to have\r | |
438 | a Py prefix (or use CapitalizedWords). Later... */\r | |
439 | \r | |
440 | /* Global variable holding the exception type for errors detected\r | |
441 | by this module (but not argument type or memory errors, etc.). */\r | |
442 | static PyObject *socket_error;\r | |
443 | static PyObject *socket_herror;\r | |
444 | static PyObject *socket_gaierror;\r | |
445 | static PyObject *socket_timeout;\r | |
446 | \r | |
447 | #ifdef RISCOS\r | |
448 | /* Global variable which is !=0 if Python is running in a RISC OS taskwindow */\r | |
449 | static int taskwindow;\r | |
450 | #endif\r | |
451 | \r | |
452 | /* A forward reference to the socket type object.\r | |
453 | The sock_type variable contains pointers to various functions,\r | |
454 | some of which call new_sockobject(), which uses sock_type, so\r | |
455 | there has to be a circular reference. */\r | |
456 | static PyTypeObject sock_type;\r | |
457 | \r | |
458 | #if defined(HAVE_POLL_H)\r | |
459 | #include <poll.h>\r | |
460 | #elif defined(HAVE_SYS_POLL_H)\r | |
461 | #include <sys/poll.h>\r | |
462 | #endif\r | |
463 | \r | |
464 | #ifdef HAVE_POLL\r | |
465 | /* Instead of select(), we'll use poll() since poll() works on any fd. */\r | |
466 | #define IS_SELECTABLE(s) 1\r | |
467 | /* Can we call select() with this socket without a buffer overrun? */\r | |
468 | #else\r | |
469 | /* If there's no timeout left, we don't have to call select, so it's a safe,\r | |
470 | * little white lie. */\r | |
471 | #define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0.0)\r | |
472 | #endif\r | |
473 | \r | |
474 | static PyObject*\r | |
475 | select_error(void)\r | |
476 | {\r | |
477 | PyErr_SetString(socket_error, "unable to select on socket");\r | |
478 | return NULL;\r | |
479 | }\r | |
480 | \r | |
481 | #ifdef MS_WINDOWS\r | |
482 | #ifndef WSAEAGAIN\r | |
483 | #define WSAEAGAIN WSAEWOULDBLOCK\r | |
484 | #endif\r | |
485 | #define CHECK_ERRNO(expected) \\r | |
486 | (WSAGetLastError() == WSA ## expected)\r | |
487 | #else\r | |
488 | #define CHECK_ERRNO(expected) \\r | |
489 | (errno == expected)\r | |
490 | #endif\r | |
491 | \r | |
492 | /* Convenience function to raise an error according to errno\r | |
493 | and return a NULL pointer from a function. */\r | |
494 | \r | |
495 | static PyObject *\r | |
496 | set_error(void)\r | |
497 | {\r | |
498 | #ifdef MS_WINDOWS\r | |
499 | int err_no = WSAGetLastError();\r | |
500 | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which\r | |
501 | recognizes the error codes used by both GetLastError() and\r | |
502 | WSAGetLastError */\r | |
503 | if (err_no)\r | |
504 | return PyErr_SetExcFromWindowsErr(socket_error, err_no);\r | |
505 | #endif\r | |
506 | \r | |
507 | #if defined(PYOS_OS2) && !defined(PYCC_GCC)\r | |
508 | if (sock_errno() != NO_ERROR) {\r | |
509 | APIRET rc;\r | |
510 | ULONG msglen;\r | |
511 | char outbuf[100];\r | |
512 | int myerrorcode = sock_errno();\r | |
513 | \r | |
514 | /* Retrieve socket-related error message from MPTN.MSG file */\r | |
515 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),\r | |
516 | myerrorcode - SOCBASEERR + 26,\r | |
517 | "mptn.msg",\r | |
518 | &msglen);\r | |
519 | if (rc == NO_ERROR) {\r | |
520 | PyObject *v;\r | |
521 | \r | |
522 | /* OS/2 doesn't guarantee a terminator */\r | |
523 | outbuf[msglen] = '\0';\r | |
524 | if (strlen(outbuf) > 0) {\r | |
525 | /* If non-empty msg, trim CRLF */\r | |
526 | char *lastc = &outbuf[ strlen(outbuf)-1 ];\r | |
527 | while (lastc > outbuf &&\r | |
528 | isspace(Py_CHARMASK(*lastc))) {\r | |
529 | /* Trim trailing whitespace (CRLF) */\r | |
530 | *lastc-- = '\0';\r | |
531 | }\r | |
532 | }\r | |
533 | v = Py_BuildValue("(is)", myerrorcode, outbuf);\r | |
534 | if (v != NULL) {\r | |
535 | PyErr_SetObject(socket_error, v);\r | |
536 | Py_DECREF(v);\r | |
537 | }\r | |
538 | return NULL;\r | |
539 | }\r | |
540 | }\r | |
541 | #endif\r | |
542 | \r | |
543 | #if defined(RISCOS)\r | |
544 | if (_inet_error.errnum != NULL) {\r | |
545 | PyObject *v;\r | |
546 | v = Py_BuildValue("(is)", errno, _inet_err());\r | |
547 | if (v != NULL) {\r | |
548 | PyErr_SetObject(socket_error, v);\r | |
549 | Py_DECREF(v);\r | |
550 | }\r | |
551 | return NULL;\r | |
552 | }\r | |
553 | #endif\r | |
554 | \r | |
555 | return PyErr_SetFromErrno(socket_error);\r | |
556 | }\r | |
557 | \r | |
558 | \r | |
559 | static PyObject *\r | |
560 | set_herror(int h_error)\r | |
561 | {\r | |
562 | PyObject *v;\r | |
563 | \r | |
564 | #ifdef HAVE_HSTRERROR\r | |
565 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));\r | |
566 | #else\r | |
567 | v = Py_BuildValue("(is)", h_error, "host not found");\r | |
568 | #endif\r | |
569 | if (v != NULL) {\r | |
570 | PyErr_SetObject(socket_herror, v);\r | |
571 | Py_DECREF(v);\r | |
572 | }\r | |
573 | \r | |
574 | return NULL;\r | |
575 | }\r | |
576 | \r | |
577 | \r | |
578 | static PyObject *\r | |
579 | set_gaierror(int error)\r | |
580 | {\r | |
581 | PyObject *v;\r | |
582 | \r | |
583 | #ifdef EAI_SYSTEM\r | |
584 | /* EAI_SYSTEM is not available on Windows XP. */\r | |
585 | if (error == EAI_SYSTEM)\r | |
586 | return set_error();\r | |
587 | #endif\r | |
588 | \r | |
589 | #ifdef HAVE_GAI_STRERROR\r | |
590 | v = Py_BuildValue("(is)", error, gai_strerror(error));\r | |
591 | #else\r | |
592 | v = Py_BuildValue("(is)", error, "getaddrinfo failed");\r | |
593 | #endif\r | |
594 | if (v != NULL) {\r | |
595 | PyErr_SetObject(socket_gaierror, v);\r | |
596 | Py_DECREF(v);\r | |
597 | }\r | |
598 | \r | |
599 | return NULL;\r | |
600 | }\r | |
601 | \r | |
602 | #ifdef __VMS\r | |
603 | /* Function to send in segments */\r | |
604 | static int\r | |
605 | sendsegmented(int sock_fd, char *buf, int len, int flags)\r | |
606 | {\r | |
607 | int n = 0;\r | |
608 | int remaining = len;\r | |
609 | \r | |
610 | while (remaining > 0) {\r | |
611 | unsigned int segment;\r | |
612 | \r | |
613 | segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);\r | |
614 | n = send(sock_fd, buf, segment, flags);\r | |
615 | if (n < 0) {\r | |
616 | return n;\r | |
617 | }\r | |
618 | remaining -= segment;\r | |
619 | buf += segment;\r | |
620 | } /* end while */\r | |
621 | \r | |
622 | return len;\r | |
623 | }\r | |
624 | #endif\r | |
625 | \r | |
626 | /* Function to perform the setting of socket blocking mode\r | |
627 | internally. block = (1 | 0). */\r | |
628 | static int\r | |
629 | internal_setblocking(PySocketSockObject *s, int block)\r | |
630 | {\r | |
631 | #ifndef RISCOS\r | |
632 | #ifndef MS_WINDOWS\r | |
633 | int delay_flag;\r | |
634 | #endif\r | |
635 | #endif\r | |
636 | \r | |
637 | Py_BEGIN_ALLOW_THREADS\r | |
638 | #ifdef __BEOS__\r | |
639 | block = !block;\r | |
640 | setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK,\r | |
641 | (void *)(&block), sizeof(int));\r | |
642 | #else\r | |
643 | #ifndef RISCOS\r | |
644 | #ifndef MS_WINDOWS\r | |
645 | #if defined(PYOS_OS2) && !defined(PYCC_GCC)\r | |
646 | block = !block;\r | |
647 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));\r | |
648 | #elif defined(__VMS)\r | |
649 | block = !block;\r | |
650 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);\r | |
651 | #else /* !PYOS_OS2 && !__VMS */\r | |
652 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0);\r | |
653 | if (block)\r | |
654 | delay_flag &= (~O_NONBLOCK);\r | |
655 | else\r | |
656 | delay_flag |= O_NONBLOCK;\r | |
657 | fcntl(s->sock_fd, F_SETFL, delay_flag);\r | |
658 | #endif /* !PYOS_OS2 */\r | |
659 | #else /* MS_WINDOWS */\r | |
660 | block = !block;\r | |
661 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);\r | |
662 | #endif /* MS_WINDOWS */\r | |
663 | #else /* RISCOS */\r | |
664 | block = !block;\r | |
665 | socketioctl(s->sock_fd, FIONBIO, (u_long*)&block);\r | |
666 | #endif /* RISCOS */\r | |
667 | #endif /* __BEOS__ */\r | |
668 | Py_END_ALLOW_THREADS\r | |
669 | \r | |
670 | /* Since these don't return anything */\r | |
671 | return 1;\r | |
672 | }\r | |
673 | \r | |
674 | /* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).\r | |
675 | The argument writing indicates the direction.\r | |
676 | This does not raise an exception; we'll let our caller do that\r | |
677 | after they've reacquired the interpreter lock.\r | |
678 | Returns 1 on timeout, -1 on error, 0 otherwise. */\r | |
679 | static int\r | |
680 | internal_select_ex(PySocketSockObject *s, int writing, double interval)\r | |
681 | {\r | |
682 | int n;\r | |
683 | \r | |
684 | /* Nothing to do unless we're in timeout mode (not non-blocking) */\r | |
685 | if (s->sock_timeout <= 0.0)\r | |
686 | return 0;\r | |
687 | \r | |
688 | /* Guard against closed socket */\r | |
689 | if (s->sock_fd < 0)\r | |
690 | return 0;\r | |
691 | \r | |
692 | /* Handling this condition here simplifies the select loops */\r | |
693 | if (interval < 0.0)\r | |
694 | return 1;\r | |
695 | \r | |
696 | /* Prefer poll, if available, since you can poll() any fd\r | |
697 | * which can't be done with select(). */\r | |
698 | #ifdef HAVE_POLL\r | |
699 | {\r | |
700 | struct pollfd pollfd;\r | |
701 | int timeout;\r | |
702 | \r | |
703 | pollfd.fd = s->sock_fd;\r | |
704 | pollfd.events = writing ? POLLOUT : POLLIN;\r | |
705 | \r | |
706 | /* s->sock_timeout is in seconds, timeout in ms */\r | |
707 | timeout = (int)(interval * 1000 + 0.5);\r | |
708 | n = poll(&pollfd, 1, timeout);\r | |
709 | }\r | |
710 | #else\r | |
711 | {\r | |
712 | /* Construct the arguments to select */\r | |
713 | fd_set fds;\r | |
714 | struct timeval tv;\r | |
715 | tv.tv_sec = (int)interval;\r | |
716 | tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6);\r | |
717 | FD_ZERO(&fds);\r | |
718 | FD_SET(s->sock_fd, &fds);\r | |
719 | \r | |
720 | /* See if the socket is ready */\r | |
721 | if (writing)\r | |
722 | n = select(s->sock_fd+1, NULL, &fds, NULL, &tv);\r | |
723 | else\r | |
724 | n = select(s->sock_fd+1, &fds, NULL, NULL, &tv);\r | |
725 | }\r | |
726 | #endif\r | |
727 | \r | |
728 | if (n < 0)\r | |
729 | return -1;\r | |
730 | if (n == 0)\r | |
731 | return 1;\r | |
732 | return 0;\r | |
733 | }\r | |
734 | \r | |
735 | static int\r | |
736 | internal_select(PySocketSockObject *s, int writing)\r | |
737 | {\r | |
738 | return internal_select_ex(s, writing, s->sock_timeout);\r | |
739 | }\r | |
740 | \r | |
741 | /*\r | |
742 | Two macros for automatic retry of select() in case of false positives\r | |
743 | (for example, select() could indicate a socket is ready for reading\r | |
744 | but the data then discarded by the OS because of a wrong checksum).\r | |
745 | Here is an example of use:\r | |
746 | \r | |
747 | BEGIN_SELECT_LOOP(s)\r | |
748 | Py_BEGIN_ALLOW_THREADS\r | |
749 | timeout = internal_select_ex(s, 0, interval);\r | |
750 | if (!timeout)\r | |
751 | outlen = recv(s->sock_fd, cbuf, len, flags);\r | |
752 | Py_END_ALLOW_THREADS\r | |
753 | if (timeout == 1) {\r | |
754 | PyErr_SetString(socket_timeout, "timed out");\r | |
755 | return -1;\r | |
756 | }\r | |
757 | END_SELECT_LOOP(s)\r | |
758 | */\r | |
759 | #define BEGIN_SELECT_LOOP(s) \\r | |
760 | { \\r | |
761 | double deadline, interval = s->sock_timeout; \\r | |
762 | int has_timeout = s->sock_timeout > 0.0; \\r | |
763 | if (has_timeout) { \\r | |
764 | deadline = _PyTime_FloatTime() + s->sock_timeout; \\r | |
765 | } \\r | |
766 | while (1) { \\r | |
767 | errno = 0;\r | |
768 | \r | |
769 | #define END_SELECT_LOOP(s) \\r | |
770 | if (!has_timeout || \\r | |
771 | (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \\r | |
772 | break; \\r | |
773 | interval = deadline - _PyTime_FloatTime(); \\r | |
774 | } \\r | |
775 | }\r | |
776 | \r | |
777 | /* Initialize a new socket object. */\r | |
778 | \r | |
779 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */\r | |
780 | \r | |
781 | PyMODINIT_FUNC\r | |
782 | init_sockobject(PySocketSockObject *s,\r | |
783 | SOCKET_T fd, int family, int type, int proto)\r | |
784 | {\r | |
785 | #ifdef RISCOS\r | |
786 | int block = 1;\r | |
787 | #endif\r | |
788 | s->sock_fd = fd;\r | |
789 | s->sock_family = family;\r | |
790 | s->sock_type = type;\r | |
791 | s->sock_proto = proto;\r | |
792 | s->sock_timeout = defaulttimeout;\r | |
793 | \r | |
794 | s->errorhandler = &set_error;\r | |
795 | \r | |
796 | if (defaulttimeout >= 0.0)\r | |
797 | internal_setblocking(s, 0);\r | |
798 | \r | |
799 | #ifdef RISCOS\r | |
800 | if (taskwindow)\r | |
801 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);\r | |
802 | #endif\r | |
803 | }\r | |
804 | \r | |
805 | \r | |
806 | /* Create a new socket object.\r | |
807 | This just creates the object and initializes it.\r | |
808 | If the creation fails, return NULL and set an exception (implicit\r | |
809 | in NEWOBJ()). */\r | |
810 | \r | |
811 | static PySocketSockObject *\r | |
812 | new_sockobject(SOCKET_T fd, int family, int type, int proto)\r | |
813 | {\r | |
814 | PySocketSockObject *s;\r | |
815 | s = (PySocketSockObject *)\r | |
816 | PyType_GenericNew(&sock_type, NULL, NULL);\r | |
817 | if (s != NULL)\r | |
818 | init_sockobject(s, fd, family, type, proto);\r | |
819 | return s;\r | |
820 | }\r | |
821 | \r | |
822 | \r | |
823 | /* Lock to allow python interpreter to continue, but only allow one\r | |
824 | thread to be in gethostbyname or getaddrinfo */\r | |
825 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)\r | |
826 | static PyThread_type_lock netdb_lock;\r | |
827 | #endif\r | |
828 | \r | |
829 | \r | |
830 | /* Convert a string specifying a host name or one of a few symbolic\r | |
831 | names to a numeric IP address. This usually calls gethostbyname()\r | |
832 | to do the work; the names "" and "<broadcast>" are special.\r | |
833 | Return the length (IPv4 should be 4 bytes), or negative if\r | |
834 | an error occurred; then an exception is raised. */\r | |
835 | \r | |
836 | static int\r | |
837 | setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)\r | |
838 | {\r | |
839 | struct addrinfo hints, *res;\r | |
840 | int error;\r | |
841 | int d1, d2, d3, d4;\r | |
842 | char ch;\r | |
843 | \r | |
844 | memset((void *) addr_ret, '\0', sizeof(*addr_ret));\r | |
845 | if (name[0] == '\0') {\r | |
846 | int siz;\r | |
847 | memset(&hints, 0, sizeof(hints));\r | |
848 | hints.ai_family = af;\r | |
849 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/\r | |
850 | hints.ai_flags = AI_PASSIVE;\r | |
851 | Py_BEGIN_ALLOW_THREADS\r | |
852 | ACQUIRE_GETADDRINFO_LOCK\r | |
853 | error = getaddrinfo(NULL, "0", &hints, &res);\r | |
854 | Py_END_ALLOW_THREADS\r | |
855 | /* We assume that those thread-unsafe getaddrinfo() versions\r | |
856 | *are* safe regarding their return value, ie. that a\r | |
857 | subsequent call to getaddrinfo() does not destroy the\r | |
858 | outcome of the first call. */\r | |
859 | RELEASE_GETADDRINFO_LOCK\r | |
860 | if (error) {\r | |
861 | set_gaierror(error);\r | |
862 | return -1;\r | |
863 | }\r | |
864 | switch (res->ai_family) {\r | |
865 | case AF_INET:\r | |
866 | siz = 4;\r | |
867 | break;\r | |
868 | #ifdef ENABLE_IPV6\r | |
869 | case AF_INET6:\r | |
870 | siz = 16;\r | |
871 | break;\r | |
872 | #endif\r | |
873 | default:\r | |
874 | freeaddrinfo(res);\r | |
875 | PyErr_SetString(socket_error,\r | |
876 | "unsupported address family");\r | |
877 | return -1;\r | |
878 | }\r | |
879 | if (res->ai_next) {\r | |
880 | freeaddrinfo(res);\r | |
881 | PyErr_SetString(socket_error,\r | |
882 | "wildcard resolved to multiple address");\r | |
883 | return -1;\r | |
884 | }\r | |
885 | if (res->ai_addrlen < addr_ret_size)\r | |
886 | addr_ret_size = res->ai_addrlen;\r | |
887 | memcpy(addr_ret, res->ai_addr, addr_ret_size);\r | |
888 | freeaddrinfo(res);\r | |
889 | return siz;\r | |
890 | }\r | |
891 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {\r | |
892 | struct sockaddr_in *sin;\r | |
893 | if (af != AF_INET && af != AF_UNSPEC) {\r | |
894 | PyErr_SetString(socket_error,\r | |
895 | "address family mismatched");\r | |
896 | return -1;\r | |
897 | }\r | |
898 | sin = (struct sockaddr_in *)addr_ret;\r | |
899 | memset((void *) sin, '\0', sizeof(*sin));\r | |
900 | sin->sin_family = AF_INET;\r | |
901 | #ifdef HAVE_SOCKADDR_SA_LEN\r | |
902 | sin->sin_len = sizeof(*sin);\r | |
903 | #endif\r | |
904 | sin->sin_addr.s_addr = INADDR_BROADCAST;\r | |
905 | return sizeof(sin->sin_addr);\r | |
906 | }\r | |
907 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&\r | |
908 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&\r | |
909 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {\r | |
910 | struct sockaddr_in *sin;\r | |
911 | sin = (struct sockaddr_in *)addr_ret;\r | |
912 | sin->sin_addr.s_addr = htonl(\r | |
913 | ((long) d1 << 24) | ((long) d2 << 16) |\r | |
914 | ((long) d3 << 8) | ((long) d4 << 0));\r | |
915 | sin->sin_family = AF_INET;\r | |
916 | #ifdef HAVE_SOCKADDR_SA_LEN\r | |
917 | sin->sin_len = sizeof(*sin);\r | |
918 | #endif\r | |
919 | return 4;\r | |
920 | }\r | |
921 | memset(&hints, 0, sizeof(hints));\r | |
922 | hints.ai_family = af;\r | |
923 | Py_BEGIN_ALLOW_THREADS\r | |
924 | ACQUIRE_GETADDRINFO_LOCK\r | |
925 | error = getaddrinfo(name, NULL, &hints, &res);\r | |
926 | #if defined(__digital__) && defined(__unix__)\r | |
927 | if (error == EAI_NONAME && af == AF_UNSPEC) {\r | |
928 | /* On Tru64 V5.1, numeric-to-addr conversion fails\r | |
929 | if no address family is given. Assume IPv4 for now.*/\r | |
930 | hints.ai_family = AF_INET;\r | |
931 | error = getaddrinfo(name, NULL, &hints, &res);\r | |
932 | }\r | |
933 | #endif\r | |
934 | Py_END_ALLOW_THREADS\r | |
935 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */\r | |
936 | if (error) {\r | |
937 | set_gaierror(error);\r | |
938 | return -1;\r | |
939 | }\r | |
940 | if (res->ai_addrlen < addr_ret_size)\r | |
941 | addr_ret_size = res->ai_addrlen;\r | |
942 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);\r | |
943 | freeaddrinfo(res);\r | |
944 | switch (addr_ret->sa_family) {\r | |
945 | case AF_INET:\r | |
946 | return 4;\r | |
947 | #ifdef ENABLE_IPV6\r | |
948 | case AF_INET6:\r | |
949 | return 16;\r | |
950 | #endif\r | |
951 | default:\r | |
952 | PyErr_SetString(socket_error, "unknown address family");\r | |
953 | return -1;\r | |
954 | }\r | |
955 | }\r | |
956 | \r | |
957 | \r | |
958 | /* Create a string object representing an IP address.\r | |
959 | This is always a string of the form 'dd.dd.dd.dd' (with variable\r | |
960 | size numbers). */\r | |
961 | \r | |
962 | static PyObject *\r | |
963 | makeipaddr(struct sockaddr *addr, int addrlen)\r | |
964 | {\r | |
965 | char buf[NI_MAXHOST];\r | |
966 | int error;\r | |
967 | \r | |
968 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,\r | |
969 | NI_NUMERICHOST);\r | |
970 | if (error) {\r | |
971 | set_gaierror(error);\r | |
972 | return NULL;\r | |
973 | }\r | |
974 | return PyString_FromString(buf);\r | |
975 | }\r | |
976 | \r | |
977 | \r | |
978 | #ifdef USE_BLUETOOTH\r | |
979 | /* Convert a string representation of a Bluetooth address into a numeric\r | |
980 | address. Returns the length (6), or raises an exception and returns -1 if\r | |
981 | an error occurred. */\r | |
982 | \r | |
983 | static int\r | |
984 | setbdaddr(char *name, bdaddr_t *bdaddr)\r | |
985 | {\r | |
986 | unsigned int b0, b1, b2, b3, b4, b5;\r | |
987 | char ch;\r | |
988 | int n;\r | |
989 | \r | |
990 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",\r | |
991 | &b5, &b4, &b3, &b2, &b1, &b0, &ch);\r | |
992 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {\r | |
993 | bdaddr->b[0] = b0;\r | |
994 | bdaddr->b[1] = b1;\r | |
995 | bdaddr->b[2] = b2;\r | |
996 | bdaddr->b[3] = b3;\r | |
997 | bdaddr->b[4] = b4;\r | |
998 | bdaddr->b[5] = b5;\r | |
999 | return 6;\r | |
1000 | } else {\r | |
1001 | PyErr_SetString(socket_error, "bad bluetooth address");\r | |
1002 | return -1;\r | |
1003 | }\r | |
1004 | }\r | |
1005 | \r | |
1006 | /* Create a string representation of the Bluetooth address. This is always a\r | |
1007 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal\r | |
1008 | value (zero padded if necessary). */\r | |
1009 | \r | |
1010 | static PyObject *\r | |
1011 | makebdaddr(bdaddr_t *bdaddr)\r | |
1012 | {\r | |
1013 | char buf[(6 * 2) + 5 + 1];\r | |
1014 | \r | |
1015 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",\r | |
1016 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],\r | |
1017 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);\r | |
1018 | return PyString_FromString(buf);\r | |
1019 | }\r | |
1020 | #endif\r | |
1021 | \r | |
1022 | \r | |
1023 | /* Create an object representing the given socket address,\r | |
1024 | suitable for passing it back to bind(), connect() etc.\r | |
1025 | The family field of the sockaddr structure is inspected\r | |
1026 | to determine what kind of address it really is. */\r | |
1027 | \r | |
1028 | /*ARGSUSED*/\r | |
1029 | static PyObject *\r | |
1030 | makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)\r | |
1031 | {\r | |
1032 | if (addrlen == 0) {\r | |
1033 | /* No address -- may be recvfrom() from known socket */\r | |
1034 | Py_INCREF(Py_None);\r | |
1035 | return Py_None;\r | |
1036 | }\r | |
1037 | \r | |
1038 | #ifdef __BEOS__\r | |
1039 | /* XXX: BeOS version of accept() doesn't set family correctly */\r | |
1040 | addr->sa_family = AF_INET;\r | |
1041 | #endif\r | |
1042 | \r | |
1043 | switch (addr->sa_family) {\r | |
1044 | \r | |
1045 | case AF_INET:\r | |
1046 | {\r | |
1047 | struct sockaddr_in *a;\r | |
1048 | PyObject *addrobj = makeipaddr(addr, sizeof(*a));\r | |
1049 | PyObject *ret = NULL;\r | |
1050 | if (addrobj) {\r | |
1051 | a = (struct sockaddr_in *)addr;\r | |
1052 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));\r | |
1053 | Py_DECREF(addrobj);\r | |
1054 | }\r | |
1055 | return ret;\r | |
1056 | }\r | |
1057 | \r | |
1058 | #if defined(AF_UNIX)\r | |
1059 | case AF_UNIX:\r | |
1060 | {\r | |
1061 | struct sockaddr_un *a = (struct sockaddr_un *) addr;\r | |
1062 | #ifdef linux\r | |
1063 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */\r | |
1064 | addrlen -= offsetof(struct sockaddr_un, sun_path);\r | |
1065 | return PyString_FromStringAndSize(a->sun_path,\r | |
1066 | addrlen);\r | |
1067 | }\r | |
1068 | else\r | |
1069 | #endif /* linux */\r | |
1070 | {\r | |
1071 | /* regular NULL-terminated string */\r | |
1072 | return PyString_FromString(a->sun_path);\r | |
1073 | }\r | |
1074 | }\r | |
1075 | #endif /* AF_UNIX */\r | |
1076 | \r | |
1077 | #if defined(AF_NETLINK)\r | |
1078 | case AF_NETLINK:\r | |
1079 | {\r | |
1080 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr;\r | |
1081 | return Py_BuildValue("II", a->nl_pid, a->nl_groups);\r | |
1082 | }\r | |
1083 | #endif /* AF_NETLINK */\r | |
1084 | \r | |
1085 | #ifdef ENABLE_IPV6\r | |
1086 | case AF_INET6:\r | |
1087 | {\r | |
1088 | struct sockaddr_in6 *a;\r | |
1089 | PyObject *addrobj = makeipaddr(addr, sizeof(*a));\r | |
1090 | PyObject *ret = NULL;\r | |
1091 | if (addrobj) {\r | |
1092 | a = (struct sockaddr_in6 *)addr;\r | |
1093 | ret = Py_BuildValue("OiII",\r | |
1094 | addrobj,\r | |
1095 | ntohs(a->sin6_port),\r | |
1096 | ntohl(a->sin6_flowinfo),\r | |
1097 | a->sin6_scope_id);\r | |
1098 | Py_DECREF(addrobj);\r | |
1099 | }\r | |
1100 | return ret;\r | |
1101 | }\r | |
1102 | #endif\r | |
1103 | \r | |
1104 | #ifdef USE_BLUETOOTH\r | |
1105 | case AF_BLUETOOTH:\r | |
1106 | switch (proto) {\r | |
1107 | \r | |
1108 | case BTPROTO_L2CAP:\r | |
1109 | {\r | |
1110 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;\r | |
1111 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));\r | |
1112 | PyObject *ret = NULL;\r | |
1113 | if (addrobj) {\r | |
1114 | ret = Py_BuildValue("Oi",\r | |
1115 | addrobj,\r | |
1116 | _BT_L2_MEMB(a, psm));\r | |
1117 | Py_DECREF(addrobj);\r | |
1118 | }\r | |
1119 | return ret;\r | |
1120 | }\r | |
1121 | \r | |
1122 | case BTPROTO_RFCOMM:\r | |
1123 | {\r | |
1124 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr;\r | |
1125 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));\r | |
1126 | PyObject *ret = NULL;\r | |
1127 | if (addrobj) {\r | |
1128 | ret = Py_BuildValue("Oi",\r | |
1129 | addrobj,\r | |
1130 | _BT_RC_MEMB(a, channel));\r | |
1131 | Py_DECREF(addrobj);\r | |
1132 | }\r | |
1133 | return ret;\r | |
1134 | }\r | |
1135 | \r | |
1136 | case BTPROTO_HCI:\r | |
1137 | {\r | |
1138 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr;\r | |
1139 | #if defined(__NetBSD__) || defined(__DragonFly__)\r | |
1140 | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr));\r | |
1141 | #else\r | |
1142 | PyObject *ret = NULL;\r | |
1143 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));\r | |
1144 | return ret;\r | |
1145 | #endif\r | |
1146 | }\r | |
1147 | \r | |
1148 | #if !defined(__FreeBSD__)\r | |
1149 | case BTPROTO_SCO:\r | |
1150 | {\r | |
1151 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr;\r | |
1152 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));\r | |
1153 | }\r | |
1154 | #endif\r | |
1155 | \r | |
1156 | default:\r | |
1157 | PyErr_SetString(PyExc_ValueError,\r | |
1158 | "Unknown Bluetooth protocol");\r | |
1159 | return NULL;\r | |
1160 | }\r | |
1161 | #endif\r | |
1162 | \r | |
1163 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME)\r | |
1164 | case AF_PACKET:\r | |
1165 | {\r | |
1166 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr;\r | |
1167 | char *ifname = "";\r | |
1168 | struct ifreq ifr;\r | |
1169 | /* need to look up interface name give index */\r | |
1170 | if (a->sll_ifindex) {\r | |
1171 | ifr.ifr_ifindex = a->sll_ifindex;\r | |
1172 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)\r | |
1173 | ifname = ifr.ifr_name;\r | |
1174 | }\r | |
1175 | return Py_BuildValue("shbhs#",\r | |
1176 | ifname,\r | |
1177 | ntohs(a->sll_protocol),\r | |
1178 | a->sll_pkttype,\r | |
1179 | a->sll_hatype,\r | |
1180 | a->sll_addr,\r | |
1181 | a->sll_halen);\r | |
1182 | }\r | |
1183 | #endif\r | |
1184 | \r | |
1185 | #ifdef HAVE_LINUX_TIPC_H\r | |
1186 | case AF_TIPC:\r | |
1187 | {\r | |
1188 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;\r | |
1189 | if (a->addrtype == TIPC_ADDR_NAMESEQ) {\r | |
1190 | return Py_BuildValue("IIIII",\r | |
1191 | a->addrtype,\r | |
1192 | a->addr.nameseq.type,\r | |
1193 | a->addr.nameseq.lower,\r | |
1194 | a->addr.nameseq.upper,\r | |
1195 | a->scope);\r | |
1196 | } else if (a->addrtype == TIPC_ADDR_NAME) {\r | |
1197 | return Py_BuildValue("IIIII",\r | |
1198 | a->addrtype,\r | |
1199 | a->addr.name.name.type,\r | |
1200 | a->addr.name.name.instance,\r | |
1201 | a->addr.name.name.instance,\r | |
1202 | a->scope);\r | |
1203 | } else if (a->addrtype == TIPC_ADDR_ID) {\r | |
1204 | return Py_BuildValue("IIIII",\r | |
1205 | a->addrtype,\r | |
1206 | a->addr.id.node,\r | |
1207 | a->addr.id.ref,\r | |
1208 | 0,\r | |
1209 | a->scope);\r | |
1210 | } else {\r | |
1211 | PyErr_SetString(PyExc_ValueError,\r | |
1212 | "Invalid address type");\r | |
1213 | return NULL;\r | |
1214 | }\r | |
1215 | }\r | |
1216 | #endif\r | |
1217 | \r | |
1218 | /* More cases here... */\r | |
1219 | \r | |
1220 | default:\r | |
1221 | /* If we don't know the address family, don't raise an\r | |
1222 | exception -- return it as a tuple. */\r | |
1223 | return Py_BuildValue("is#",\r | |
1224 | addr->sa_family,\r | |
1225 | addr->sa_data,\r | |
1226 | sizeof(addr->sa_data));\r | |
1227 | \r | |
1228 | }\r | |
1229 | }\r | |
1230 | \r | |
1231 | \r | |
1232 | /* Parse a socket address argument according to the socket object's\r | |
1233 | address family. Return 1 if the address was in the proper format,\r | |
1234 | 0 of not. The address is returned through addr_ret, its length\r | |
1235 | through len_ret. */\r | |
1236 | \r | |
1237 | static int\r | |
1238 | getsockaddrarg(PySocketSockObject *s, PyObject *args,\r | |
1239 | struct sockaddr *addr_ret, int *len_ret)\r | |
1240 | {\r | |
1241 | switch (s->sock_family) {\r | |
1242 | \r | |
1243 | #if defined(AF_UNIX)\r | |
1244 | case AF_UNIX:\r | |
1245 | {\r | |
1246 | struct sockaddr_un* addr;\r | |
1247 | char *path;\r | |
1248 | int len;\r | |
1249 | if (!PyArg_Parse(args, "t#", &path, &len))\r | |
1250 | return 0;\r | |
1251 | \r | |
1252 | addr = (struct sockaddr_un*)addr_ret;\r | |
1253 | #ifdef linux\r | |
1254 | if (len > 0 && path[0] == 0) {\r | |
1255 | /* Linux abstract namespace extension */\r | |
1256 | if (len > sizeof addr->sun_path) {\r | |
1257 | PyErr_SetString(socket_error,\r | |
1258 | "AF_UNIX path too long");\r | |
1259 | return 0;\r | |
1260 | }\r | |
1261 | }\r | |
1262 | else\r | |
1263 | #endif /* linux */\r | |
1264 | {\r | |
1265 | /* regular NULL-terminated string */\r | |
1266 | if (len >= sizeof addr->sun_path) {\r | |
1267 | PyErr_SetString(socket_error,\r | |
1268 | "AF_UNIX path too long");\r | |
1269 | return 0;\r | |
1270 | }\r | |
1271 | addr->sun_path[len] = 0;\r | |
1272 | }\r | |
1273 | addr->sun_family = s->sock_family;\r | |
1274 | memcpy(addr->sun_path, path, len);\r | |
1275 | #if defined(PYOS_OS2)\r | |
1276 | *len_ret = sizeof(*addr);\r | |
1277 | #else\r | |
1278 | *len_ret = len + offsetof(struct sockaddr_un, sun_path);\r | |
1279 | #endif\r | |
1280 | return 1;\r | |
1281 | }\r | |
1282 | #endif /* AF_UNIX */\r | |
1283 | \r | |
1284 | #if defined(AF_NETLINK)\r | |
1285 | case AF_NETLINK:\r | |
1286 | {\r | |
1287 | struct sockaddr_nl* addr;\r | |
1288 | int pid, groups;\r | |
1289 | addr = (struct sockaddr_nl *)addr_ret;\r | |
1290 | if (!PyTuple_Check(args)) {\r | |
1291 | PyErr_Format(\r | |
1292 | PyExc_TypeError,\r | |
1293 | "getsockaddrarg: "\r | |
1294 | "AF_NETLINK address must be tuple, not %.500s",\r | |
1295 | Py_TYPE(args)->tp_name);\r | |
1296 | return 0;\r | |
1297 | }\r | |
1298 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))\r | |
1299 | return 0;\r | |
1300 | addr->nl_family = AF_NETLINK;\r | |
1301 | addr->nl_pid = pid;\r | |
1302 | addr->nl_groups = groups;\r | |
1303 | *len_ret = sizeof(*addr);\r | |
1304 | return 1;\r | |
1305 | }\r | |
1306 | #endif\r | |
1307 | \r | |
1308 | case AF_INET:\r | |
1309 | {\r | |
1310 | struct sockaddr_in* addr;\r | |
1311 | char *host;\r | |
1312 | int port, result;\r | |
1313 | if (!PyTuple_Check(args)) {\r | |
1314 | PyErr_Format(\r | |
1315 | PyExc_TypeError,\r | |
1316 | "getsockaddrarg: "\r | |
1317 | "AF_INET address must be tuple, not %.500s",\r | |
1318 | Py_TYPE(args)->tp_name);\r | |
1319 | return 0;\r | |
1320 | }\r | |
1321 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",\r | |
1322 | "idna", &host, &port))\r | |
1323 | return 0;\r | |
1324 | addr=(struct sockaddr_in*)addr_ret;\r | |
1325 | result = setipaddr(host, (struct sockaddr *)addr,\r | |
1326 | sizeof(*addr), AF_INET);\r | |
1327 | PyMem_Free(host);\r | |
1328 | if (result < 0)\r | |
1329 | return 0;\r | |
1330 | if (port < 0 || port > 0xffff) {\r | |
1331 | PyErr_SetString(\r | |
1332 | PyExc_OverflowError,\r | |
1333 | "getsockaddrarg: port must be 0-65535.");\r | |
1334 | return 0;\r | |
1335 | }\r | |
1336 | addr->sin_family = AF_INET;\r | |
1337 | addr->sin_port = htons((short)port);\r | |
1338 | *len_ret = sizeof *addr;\r | |
1339 | return 1;\r | |
1340 | }\r | |
1341 | \r | |
1342 | #ifdef ENABLE_IPV6\r | |
1343 | case AF_INET6:\r | |
1344 | {\r | |
1345 | struct sockaddr_in6* addr;\r | |
1346 | char *host;\r | |
1347 | int port, result;\r | |
1348 | unsigned int flowinfo, scope_id;\r | |
1349 | flowinfo = scope_id = 0;\r | |
1350 | if (!PyTuple_Check(args)) {\r | |
1351 | PyErr_Format(\r | |
1352 | PyExc_TypeError,\r | |
1353 | "getsockaddrarg: "\r | |
1354 | "AF_INET6 address must be tuple, not %.500s",\r | |
1355 | Py_TYPE(args)->tp_name);\r | |
1356 | return 0;\r | |
1357 | }\r | |
1358 | if (!PyArg_ParseTuple(args, "eti|II",\r | |
1359 | "idna", &host, &port, &flowinfo,\r | |
1360 | &scope_id)) {\r | |
1361 | return 0;\r | |
1362 | }\r | |
1363 | addr = (struct sockaddr_in6*)addr_ret;\r | |
1364 | result = setipaddr(host, (struct sockaddr *)addr,\r | |
1365 | sizeof(*addr), AF_INET6);\r | |
1366 | PyMem_Free(host);\r | |
1367 | if (result < 0)\r | |
1368 | return 0;\r | |
1369 | if (port < 0 || port > 0xffff) {\r | |
1370 | PyErr_SetString(\r | |
1371 | PyExc_OverflowError,\r | |
1372 | "getsockaddrarg: port must be 0-65535.");\r | |
1373 | return 0;\r | |
1374 | }\r | |
1375 | if (flowinfo > 0xfffff) {\r | |
1376 | PyErr_SetString(\r | |
1377 | PyExc_OverflowError,\r | |
1378 | "getsockaddrarg: flowinfo must be 0-1048575.");\r | |
1379 | return 0;\r | |
1380 | }\r | |
1381 | addr->sin6_family = s->sock_family;\r | |
1382 | addr->sin6_port = htons((short)port);\r | |
1383 | addr->sin6_flowinfo = htonl(flowinfo);\r | |
1384 | addr->sin6_scope_id = scope_id;\r | |
1385 | *len_ret = sizeof *addr;\r | |
1386 | return 1;\r | |
1387 | }\r | |
1388 | #endif\r | |
1389 | \r | |
1390 | #ifdef USE_BLUETOOTH\r | |
1391 | case AF_BLUETOOTH:\r | |
1392 | {\r | |
1393 | switch (s->sock_proto) {\r | |
1394 | case BTPROTO_L2CAP:\r | |
1395 | {\r | |
1396 | struct sockaddr_l2 *addr;\r | |
1397 | char *straddr;\r | |
1398 | \r | |
1399 | addr = (struct sockaddr_l2 *)addr_ret;\r | |
1400 | memset(addr, 0, sizeof(struct sockaddr_l2));\r | |
1401 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;\r | |
1402 | if (!PyArg_ParseTuple(args, "si", &straddr,\r | |
1403 | &_BT_L2_MEMB(addr, psm))) {\r | |
1404 | PyErr_SetString(socket_error, "getsockaddrarg: "\r | |
1405 | "wrong format");\r | |
1406 | return 0;\r | |
1407 | }\r | |
1408 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)\r | |
1409 | return 0;\r | |
1410 | \r | |
1411 | *len_ret = sizeof *addr;\r | |
1412 | return 1;\r | |
1413 | }\r | |
1414 | case BTPROTO_RFCOMM:\r | |
1415 | {\r | |
1416 | struct sockaddr_rc *addr;\r | |
1417 | char *straddr;\r | |
1418 | \r | |
1419 | addr = (struct sockaddr_rc *)addr_ret;\r | |
1420 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;\r | |
1421 | if (!PyArg_ParseTuple(args, "si", &straddr,\r | |
1422 | &_BT_RC_MEMB(addr, channel))) {\r | |
1423 | PyErr_SetString(socket_error, "getsockaddrarg: "\r | |
1424 | "wrong format");\r | |
1425 | return 0;\r | |
1426 | }\r | |
1427 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)\r | |
1428 | return 0;\r | |
1429 | \r | |
1430 | *len_ret = sizeof *addr;\r | |
1431 | return 1;\r | |
1432 | }\r | |
1433 | case BTPROTO_HCI:\r | |
1434 | {\r | |
1435 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;\r | |
1436 | #if defined(__NetBSD__) || defined(__DragonFly__)\r | |
1437 | char *straddr = PyBytes_AS_STRING(args);\r | |
1438 | \r | |
1439 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;\r | |
1440 | if (straddr == NULL) {\r | |
1441 | PyErr_SetString(socket_error, "getsockaddrarg: "\r | |
1442 | "wrong format");\r | |
1443 | return 0;\r | |
1444 | }\r | |
1445 | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0)\r | |
1446 | return 0;\r | |
1447 | #else\r | |
1448 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;\r | |
1449 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {\r | |
1450 | PyErr_SetString(socket_error, "getsockaddrarg: "\r | |
1451 | "wrong format");\r | |
1452 | return 0;\r | |
1453 | }\r | |
1454 | #endif\r | |
1455 | *len_ret = sizeof *addr;\r | |
1456 | return 1;\r | |
1457 | }\r | |
1458 | #if !defined(__FreeBSD__)\r | |
1459 | case BTPROTO_SCO:\r | |
1460 | {\r | |
1461 | struct sockaddr_sco *addr;\r | |
1462 | char *straddr;\r | |
1463 | \r | |
1464 | addr = (struct sockaddr_sco *)addr_ret;\r | |
1465 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;\r | |
1466 | straddr = PyString_AsString(args);\r | |
1467 | if (straddr == NULL) {\r | |
1468 | PyErr_SetString(socket_error, "getsockaddrarg: "\r | |
1469 | "wrong format");\r | |
1470 | return 0;\r | |
1471 | }\r | |
1472 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)\r | |
1473 | return 0;\r | |
1474 | \r | |
1475 | *len_ret = sizeof *addr;\r | |
1476 | return 1;\r | |
1477 | }\r | |
1478 | #endif\r | |
1479 | default:\r | |
1480 | PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");\r | |
1481 | return 0;\r | |
1482 | }\r | |
1483 | }\r | |
1484 | #endif\r | |
1485 | \r | |
1486 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX)\r | |
1487 | case AF_PACKET:\r | |
1488 | {\r | |
1489 | struct sockaddr_ll* addr;\r | |
1490 | struct ifreq ifr;\r | |
1491 | char *interfaceName;\r | |
1492 | int protoNumber;\r | |
1493 | int hatype = 0;\r | |
1494 | int pkttype = 0;\r | |
1495 | char *haddr = NULL;\r | |
1496 | unsigned int halen = 0;\r | |
1497 | \r | |
1498 | if (!PyTuple_Check(args)) {\r | |
1499 | PyErr_Format(\r | |
1500 | PyExc_TypeError,\r | |
1501 | "getsockaddrarg: "\r | |
1502 | "AF_PACKET address must be tuple, not %.500s",\r | |
1503 | Py_TYPE(args)->tp_name);\r | |
1504 | return 0;\r | |
1505 | }\r | |
1506 | if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName,\r | |
1507 | &protoNumber, &pkttype, &hatype,\r | |
1508 | &haddr, &halen))\r | |
1509 | return 0;\r | |
1510 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));\r | |
1511 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';\r | |
1512 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {\r | |
1513 | s->errorhandler();\r | |
1514 | return 0;\r | |
1515 | }\r | |
1516 | if (halen > 8) {\r | |
1517 | PyErr_SetString(PyExc_ValueError,\r | |
1518 | "Hardware address must be 8 bytes or less");\r | |
1519 | return 0;\r | |
1520 | }\r | |
1521 | if (protoNumber < 0 || protoNumber > 0xffff) {\r | |
1522 | PyErr_SetString(\r | |
1523 | PyExc_OverflowError,\r | |
1524 | "getsockaddrarg: protoNumber must be 0-65535.");\r | |
1525 | return 0;\r | |
1526 | }\r | |
1527 | addr = (struct sockaddr_ll*)addr_ret;\r | |
1528 | addr->sll_family = AF_PACKET;\r | |
1529 | addr->sll_protocol = htons((short)protoNumber);\r | |
1530 | addr->sll_ifindex = ifr.ifr_ifindex;\r | |
1531 | addr->sll_pkttype = pkttype;\r | |
1532 | addr->sll_hatype = hatype;\r | |
1533 | if (halen != 0) {\r | |
1534 | memcpy(&addr->sll_addr, haddr, halen);\r | |
1535 | }\r | |
1536 | addr->sll_halen = halen;\r | |
1537 | *len_ret = sizeof *addr;\r | |
1538 | return 1;\r | |
1539 | }\r | |
1540 | #endif\r | |
1541 | \r | |
1542 | #ifdef HAVE_LINUX_TIPC_H\r | |
1543 | case AF_TIPC:\r | |
1544 | {\r | |
1545 | unsigned int atype, v1, v2, v3;\r | |
1546 | unsigned int scope = TIPC_CLUSTER_SCOPE;\r | |
1547 | struct sockaddr_tipc *addr;\r | |
1548 | \r | |
1549 | if (!PyTuple_Check(args)) {\r | |
1550 | PyErr_Format(\r | |
1551 | PyExc_TypeError,\r | |
1552 | "getsockaddrarg: "\r | |
1553 | "AF_TIPC address must be tuple, not %.500s",\r | |
1554 | Py_TYPE(args)->tp_name);\r | |
1555 | return 0;\r | |
1556 | }\r | |
1557 | \r | |
1558 | if (!PyArg_ParseTuple(args,\r | |
1559 | "IIII|I;Invalid TIPC address format",\r | |
1560 | &atype, &v1, &v2, &v3, &scope))\r | |
1561 | return 0;\r | |
1562 | \r | |
1563 | addr = (struct sockaddr_tipc *) addr_ret;\r | |
1564 | memset(addr, 0, sizeof(struct sockaddr_tipc));\r | |
1565 | \r | |
1566 | addr->family = AF_TIPC;\r | |
1567 | addr->scope = scope;\r | |
1568 | addr->addrtype = atype;\r | |
1569 | \r | |
1570 | if (atype == TIPC_ADDR_NAMESEQ) {\r | |
1571 | addr->addr.nameseq.type = v1;\r | |
1572 | addr->addr.nameseq.lower = v2;\r | |
1573 | addr->addr.nameseq.upper = v3;\r | |
1574 | } else if (atype == TIPC_ADDR_NAME) {\r | |
1575 | addr->addr.name.name.type = v1;\r | |
1576 | addr->addr.name.name.instance = v2;\r | |
1577 | } else if (atype == TIPC_ADDR_ID) {\r | |
1578 | addr->addr.id.node = v1;\r | |
1579 | addr->addr.id.ref = v2;\r | |
1580 | } else {\r | |
1581 | /* Shouldn't happen */\r | |
1582 | PyErr_SetString(PyExc_TypeError, "Invalid address type");\r | |
1583 | return 0;\r | |
1584 | }\r | |
1585 | \r | |
1586 | *len_ret = sizeof(*addr);\r | |
1587 | \r | |
1588 | return 1;\r | |
1589 | }\r | |
1590 | #endif\r | |
1591 | \r | |
1592 | /* More cases here... */\r | |
1593 | \r | |
1594 | default:\r | |
1595 | PyErr_SetString(socket_error, "getsockaddrarg: bad family");\r | |
1596 | return 0;\r | |
1597 | \r | |
1598 | }\r | |
1599 | }\r | |
1600 | \r | |
1601 | \r | |
1602 | /* Get the address length according to the socket object's address family.\r | |
1603 | Return 1 if the family is known, 0 otherwise. The length is returned\r | |
1604 | through len_ret. */\r | |
1605 | \r | |
1606 | static int\r | |
1607 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)\r | |
1608 | {\r | |
1609 | switch (s->sock_family) {\r | |
1610 | \r | |
1611 | #if defined(AF_UNIX)\r | |
1612 | case AF_UNIX:\r | |
1613 | {\r | |
1614 | *len_ret = sizeof (struct sockaddr_un);\r | |
1615 | return 1;\r | |
1616 | }\r | |
1617 | #endif /* AF_UNIX */\r | |
1618 | #if defined(AF_NETLINK)\r | |
1619 | case AF_NETLINK:\r | |
1620 | {\r | |
1621 | *len_ret = sizeof (struct sockaddr_nl);\r | |
1622 | return 1;\r | |
1623 | }\r | |
1624 | #endif\r | |
1625 | \r | |
1626 | case AF_INET:\r | |
1627 | {\r | |
1628 | *len_ret = sizeof (struct sockaddr_in);\r | |
1629 | return 1;\r | |
1630 | }\r | |
1631 | \r | |
1632 | #ifdef ENABLE_IPV6\r | |
1633 | case AF_INET6:\r | |
1634 | {\r | |
1635 | *len_ret = sizeof (struct sockaddr_in6);\r | |
1636 | return 1;\r | |
1637 | }\r | |
1638 | #endif\r | |
1639 | \r | |
1640 | #ifdef USE_BLUETOOTH\r | |
1641 | case AF_BLUETOOTH:\r | |
1642 | {\r | |
1643 | switch(s->sock_proto)\r | |
1644 | {\r | |
1645 | \r | |
1646 | case BTPROTO_L2CAP:\r | |
1647 | *len_ret = sizeof (struct sockaddr_l2);\r | |
1648 | return 1;\r | |
1649 | case BTPROTO_RFCOMM:\r | |
1650 | *len_ret = sizeof (struct sockaddr_rc);\r | |
1651 | return 1;\r | |
1652 | case BTPROTO_HCI:\r | |
1653 | *len_ret = sizeof (struct sockaddr_hci);\r | |
1654 | return 1;\r | |
1655 | #if !defined(__FreeBSD__)\r | |
1656 | case BTPROTO_SCO:\r | |
1657 | *len_ret = sizeof (struct sockaddr_sco);\r | |
1658 | return 1;\r | |
1659 | #endif\r | |
1660 | default:\r | |
1661 | PyErr_SetString(socket_error, "getsockaddrlen: "\r | |
1662 | "unknown BT protocol");\r | |
1663 | return 0;\r | |
1664 | \r | |
1665 | }\r | |
1666 | }\r | |
1667 | #endif\r | |
1668 | \r | |
1669 | #ifdef HAVE_NETPACKET_PACKET_H\r | |
1670 | case AF_PACKET:\r | |
1671 | {\r | |
1672 | *len_ret = sizeof (struct sockaddr_ll);\r | |
1673 | return 1;\r | |
1674 | }\r | |
1675 | #endif\r | |
1676 | \r | |
1677 | #ifdef HAVE_LINUX_TIPC_H\r | |
1678 | case AF_TIPC:\r | |
1679 | {\r | |
1680 | *len_ret = sizeof (struct sockaddr_tipc);\r | |
1681 | return 1;\r | |
1682 | }\r | |
1683 | #endif\r | |
1684 | \r | |
1685 | /* More cases here... */\r | |
1686 | \r | |
1687 | default:\r | |
1688 | PyErr_SetString(socket_error, "getsockaddrlen: bad family");\r | |
1689 | return 0;\r | |
1690 | \r | |
1691 | }\r | |
1692 | }\r | |
1693 | \r | |
1694 | \r | |
1695 | /* s.accept() method */\r | |
1696 | \r | |
1697 | static PyObject *\r | |
1698 | sock_accept(PySocketSockObject *s)\r | |
1699 | {\r | |
1700 | sock_addr_t addrbuf;\r | |
1701 | SOCKET_T newfd;\r | |
1702 | socklen_t addrlen;\r | |
1703 | PyObject *sock = NULL;\r | |
1704 | PyObject *addr = NULL;\r | |
1705 | PyObject *res = NULL;\r | |
1706 | int timeout;\r | |
1707 | \r | |
1708 | if (!getsockaddrlen(s, &addrlen))\r | |
1709 | return NULL;\r | |
1710 | memset(&addrbuf, 0, addrlen);\r | |
1711 | \r | |
1712 | newfd = INVALID_SOCKET;\r | |
1713 | \r | |
1714 | if (!IS_SELECTABLE(s))\r | |
1715 | return select_error();\r | |
1716 | \r | |
1717 | BEGIN_SELECT_LOOP(s)\r | |
1718 | Py_BEGIN_ALLOW_THREADS\r | |
1719 | timeout = internal_select_ex(s, 0, interval);\r | |
1720 | if (!timeout)\r | |
1721 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);\r | |
1722 | Py_END_ALLOW_THREADS\r | |
1723 | \r | |
1724 | if (timeout == 1) {\r | |
1725 | PyErr_SetString(socket_timeout, "timed out");\r | |
1726 | return NULL;\r | |
1727 | }\r | |
1728 | END_SELECT_LOOP(s)\r | |
1729 | \r | |
1730 | if (newfd == INVALID_SOCKET)\r | |
1731 | return s->errorhandler();\r | |
1732 | \r | |
1733 | /* Create the new object with unspecified family,\r | |
1734 | to avoid calls to bind() etc. on it. */\r | |
1735 | sock = (PyObject *) new_sockobject(newfd,\r | |
1736 | s->sock_family,\r | |
1737 | s->sock_type,\r | |
1738 | s->sock_proto);\r | |
1739 | \r | |
1740 | if (sock == NULL) {\r | |
1741 | SOCKETCLOSE(newfd);\r | |
1742 | goto finally;\r | |
1743 | }\r | |
1744 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),\r | |
1745 | addrlen, s->sock_proto);\r | |
1746 | if (addr == NULL)\r | |
1747 | goto finally;\r | |
1748 | \r | |
1749 | res = PyTuple_Pack(2, sock, addr);\r | |
1750 | \r | |
1751 | finally:\r | |
1752 | Py_XDECREF(sock);\r | |
1753 | Py_XDECREF(addr);\r | |
1754 | return res;\r | |
1755 | }\r | |
1756 | \r | |
1757 | PyDoc_STRVAR(accept_doc,\r | |
1758 | "accept() -> (socket object, address info)\n\\r | |
1759 | \n\\r | |
1760 | Wait for an incoming connection. Return a new socket representing the\n\\r | |
1761 | connection, and the address of the client. For IP sockets, the address\n\\r | |
1762 | info is a pair (hostaddr, port).");\r | |
1763 | \r | |
1764 | /* s.setblocking(flag) method. Argument:\r | |
1765 | False -- non-blocking mode; same as settimeout(0)\r | |
1766 | True -- blocking mode; same as settimeout(None)\r | |
1767 | */\r | |
1768 | \r | |
1769 | static PyObject *\r | |
1770 | sock_setblocking(PySocketSockObject *s, PyObject *arg)\r | |
1771 | {\r | |
1772 | long block;\r | |
1773 | \r | |
1774 | block = PyInt_AsLong(arg);\r | |
1775 | if (block == -1 && PyErr_Occurred())\r | |
1776 | return NULL;\r | |
1777 | \r | |
1778 | s->sock_timeout = block ? -1.0 : 0.0;\r | |
1779 | internal_setblocking(s, block);\r | |
1780 | \r | |
1781 | Py_INCREF(Py_None);\r | |
1782 | return Py_None;\r | |
1783 | }\r | |
1784 | \r | |
1785 | PyDoc_STRVAR(setblocking_doc,\r | |
1786 | "setblocking(flag)\n\\r | |
1787 | \n\\r | |
1788 | Set the socket to blocking (flag is true) or non-blocking (false).\n\\r | |
1789 | setblocking(True) is equivalent to settimeout(None);\n\\r | |
1790 | setblocking(False) is equivalent to settimeout(0.0).");\r | |
1791 | \r | |
1792 | /* s.settimeout(timeout) method. Argument:\r | |
1793 | None -- no timeout, blocking mode; same as setblocking(True)\r | |
1794 | 0.0 -- non-blocking mode; same as setblocking(False)\r | |
1795 | > 0 -- timeout mode; operations time out after timeout seconds\r | |
1796 | < 0 -- illegal; raises an exception\r | |
1797 | */\r | |
1798 | static PyObject *\r | |
1799 | sock_settimeout(PySocketSockObject *s, PyObject *arg)\r | |
1800 | {\r | |
1801 | double timeout;\r | |
1802 | \r | |
1803 | if (arg == Py_None)\r | |
1804 | timeout = -1.0;\r | |
1805 | else {\r | |
1806 | timeout = PyFloat_AsDouble(arg);\r | |
1807 | if (timeout < 0.0) {\r | |
1808 | if (!PyErr_Occurred())\r | |
1809 | PyErr_SetString(PyExc_ValueError,\r | |
1810 | "Timeout value out of range");\r | |
1811 | return NULL;\r | |
1812 | }\r | |
1813 | }\r | |
1814 | \r | |
1815 | s->sock_timeout = timeout;\r | |
1816 | internal_setblocking(s, timeout < 0.0);\r | |
1817 | \r | |
1818 | Py_INCREF(Py_None);\r | |
1819 | return Py_None;\r | |
1820 | }\r | |
1821 | \r | |
1822 | PyDoc_STRVAR(settimeout_doc,\r | |
1823 | "settimeout(timeout)\n\\r | |
1824 | \n\\r | |
1825 | Set a timeout on socket operations. 'timeout' can be a float,\n\\r | |
1826 | giving in seconds, or None. Setting a timeout of None disables\n\\r | |
1827 | the timeout feature and is equivalent to setblocking(1).\n\\r | |
1828 | Setting a timeout of zero is the same as setblocking(0).");\r | |
1829 | \r | |
1830 | /* s.gettimeout() method.\r | |
1831 | Returns the timeout associated with a socket. */\r | |
1832 | static PyObject *\r | |
1833 | sock_gettimeout(PySocketSockObject *s)\r | |
1834 | {\r | |
1835 | if (s->sock_timeout < 0.0) {\r | |
1836 | Py_INCREF(Py_None);\r | |
1837 | return Py_None;\r | |
1838 | }\r | |
1839 | else\r | |
1840 | return PyFloat_FromDouble(s->sock_timeout);\r | |
1841 | }\r | |
1842 | \r | |
1843 | PyDoc_STRVAR(gettimeout_doc,\r | |
1844 | "gettimeout() -> timeout\n\\r | |
1845 | \n\\r | |
1846 | Returns the timeout in seconds (float) associated with socket \n\\r | |
1847 | operations. A timeout of None indicates that timeouts on socket \n\\r | |
1848 | operations are disabled.");\r | |
1849 | \r | |
1850 | #ifdef RISCOS\r | |
1851 | /* s.sleeptaskw(1 | 0) method */\r | |
1852 | \r | |
1853 | static PyObject *\r | |
1854 | sock_sleeptaskw(PySocketSockObject *s,PyObject *arg)\r | |
1855 | {\r | |
1856 | int block;\r | |
1857 | block = PyInt_AsLong(arg);\r | |
1858 | if (block == -1 && PyErr_Occurred())\r | |
1859 | return NULL;\r | |
1860 | Py_BEGIN_ALLOW_THREADS\r | |
1861 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);\r | |
1862 | Py_END_ALLOW_THREADS\r | |
1863 | \r | |
1864 | Py_INCREF(Py_None);\r | |
1865 | return Py_None;\r | |
1866 | }\r | |
1867 | PyDoc_STRVAR(sleeptaskw_doc,\r | |
1868 | "sleeptaskw(flag)\n\\r | |
1869 | \n\\r | |
1870 | Allow sleeps in taskwindows.");\r | |
1871 | #endif\r | |
1872 | \r | |
1873 | \r | |
1874 | /* s.setsockopt() method.\r | |
1875 | With an integer third argument, sets an integer option.\r | |
1876 | With a string third argument, sets an option from a buffer;\r | |
1877 | use optional built-in module 'struct' to encode the string. */\r | |
1878 | \r | |
1879 | static PyObject *\r | |
1880 | sock_setsockopt(PySocketSockObject *s, PyObject *args)\r | |
1881 | {\r | |
1882 | int level;\r | |
1883 | int optname;\r | |
1884 | int res;\r | |
1885 | char *buf;\r | |
1886 | int buflen;\r | |
1887 | int flag;\r | |
1888 | \r | |
1889 | if (PyArg_ParseTuple(args, "iii:setsockopt",\r | |
1890 | &level, &optname, &flag)) {\r | |
1891 | buf = (char *) &flag;\r | |
1892 | buflen = sizeof flag;\r | |
1893 | }\r | |
1894 | else {\r | |
1895 | PyErr_Clear();\r | |
1896 | if (!PyArg_ParseTuple(args, "iis#:setsockopt",\r | |
1897 | &level, &optname, &buf, &buflen))\r | |
1898 | return NULL;\r | |
1899 | }\r | |
1900 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);\r | |
1901 | if (res < 0)\r | |
1902 | return s->errorhandler();\r | |
1903 | Py_INCREF(Py_None);\r | |
1904 | return Py_None;\r | |
1905 | }\r | |
1906 | \r | |
1907 | PyDoc_STRVAR(setsockopt_doc,\r | |
1908 | "setsockopt(level, option, value)\n\\r | |
1909 | \n\\r | |
1910 | Set a socket option. See the Unix manual for level and option.\n\\r | |
1911 | The value argument can either be an integer or a string.");\r | |
1912 | \r | |
1913 | \r | |
1914 | /* s.getsockopt() method.\r | |
1915 | With two arguments, retrieves an integer option.\r | |
1916 | With a third integer argument, retrieves a string buffer of that size;\r | |
1917 | use optional built-in module 'struct' to decode the string. */\r | |
1918 | \r | |
1919 | static PyObject *\r | |
1920 | sock_getsockopt(PySocketSockObject *s, PyObject *args)\r | |
1921 | {\r | |
1922 | int level;\r | |
1923 | int optname;\r | |
1924 | int res;\r | |
1925 | PyObject *buf;\r | |
1926 | socklen_t buflen = 0;\r | |
1927 | \r | |
1928 | #ifdef __BEOS__\r | |
1929 | /* We have incomplete socket support. */\r | |
1930 | PyErr_SetString(socket_error, "getsockopt not supported");\r | |
1931 | return NULL;\r | |
1932 | #else\r | |
1933 | \r | |
1934 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt",\r | |
1935 | &level, &optname, &buflen))\r | |
1936 | return NULL;\r | |
1937 | \r | |
1938 | if (buflen == 0) {\r | |
1939 | int flag = 0;\r | |
1940 | socklen_t flagsize = sizeof flag;\r | |
1941 | res = getsockopt(s->sock_fd, level, optname,\r | |
1942 | (void *)&flag, &flagsize);\r | |
1943 | if (res < 0)\r | |
1944 | return s->errorhandler();\r | |
1945 | return PyInt_FromLong(flag);\r | |
1946 | }\r | |
1947 | #ifdef __VMS\r | |
1948 | /* socklen_t is unsigned so no negative test is needed,\r | |
1949 | test buflen == 0 is previously done */\r | |
1950 | if (buflen > 1024) {\r | |
1951 | #else\r | |
1952 | if (buflen <= 0 || buflen > 1024) {\r | |
1953 | #endif\r | |
1954 | PyErr_SetString(socket_error,\r | |
1955 | "getsockopt buflen out of range");\r | |
1956 | return NULL;\r | |
1957 | }\r | |
1958 | buf = PyString_FromStringAndSize((char *)NULL, buflen);\r | |
1959 | if (buf == NULL)\r | |
1960 | return NULL;\r | |
1961 | res = getsockopt(s->sock_fd, level, optname,\r | |
1962 | (void *)PyString_AS_STRING(buf), &buflen);\r | |
1963 | if (res < 0) {\r | |
1964 | Py_DECREF(buf);\r | |
1965 | return s->errorhandler();\r | |
1966 | }\r | |
1967 | _PyString_Resize(&buf, buflen);\r | |
1968 | return buf;\r | |
1969 | #endif /* __BEOS__ */\r | |
1970 | }\r | |
1971 | \r | |
1972 | PyDoc_STRVAR(getsockopt_doc,\r | |
1973 | "getsockopt(level, option[, buffersize]) -> value\n\\r | |
1974 | \n\\r | |
1975 | Get a socket option. See the Unix manual for level and option.\n\\r | |
1976 | If a nonzero buffersize argument is given, the return value is a\n\\r | |
1977 | string of that length; otherwise it is an integer.");\r | |
1978 | \r | |
1979 | \r | |
1980 | /* s.bind(sockaddr) method */\r | |
1981 | \r | |
1982 | static PyObject *\r | |
1983 | sock_bind(PySocketSockObject *s, PyObject *addro)\r | |
1984 | {\r | |
1985 | sock_addr_t addrbuf;\r | |
1986 | int addrlen;\r | |
1987 | int res;\r | |
1988 | \r | |
1989 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))\r | |
1990 | return NULL;\r | |
1991 | Py_BEGIN_ALLOW_THREADS\r | |
1992 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);\r | |
1993 | Py_END_ALLOW_THREADS\r | |
1994 | if (res < 0)\r | |
1995 | return s->errorhandler();\r | |
1996 | Py_INCREF(Py_None);\r | |
1997 | return Py_None;\r | |
1998 | }\r | |
1999 | \r | |
2000 | PyDoc_STRVAR(bind_doc,\r | |
2001 | "bind(address)\n\\r | |
2002 | \n\\r | |
2003 | Bind the socket to a local address. For IP sockets, the address is a\n\\r | |
2004 | pair (host, port); the host must refer to the local host. For raw packet\n\\r | |
2005 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");\r | |
2006 | \r | |
2007 | \r | |
2008 | /* s.close() method.\r | |
2009 | Set the file descriptor to -1 so operations tried subsequently\r | |
2010 | will surely fail. */\r | |
2011 | \r | |
2012 | static PyObject *\r | |
2013 | sock_close(PySocketSockObject *s)\r | |
2014 | {\r | |
2015 | SOCKET_T fd;\r | |
2016 | \r | |
2017 | if ((fd = s->sock_fd) != -1) {\r | |
2018 | s->sock_fd = -1;\r | |
2019 | Py_BEGIN_ALLOW_THREADS\r | |
2020 | (void) SOCKETCLOSE(fd);\r | |
2021 | Py_END_ALLOW_THREADS\r | |
2022 | }\r | |
2023 | Py_INCREF(Py_None);\r | |
2024 | return Py_None;\r | |
2025 | }\r | |
2026 | \r | |
2027 | PyDoc_STRVAR(close_doc,\r | |
2028 | "close()\n\\r | |
2029 | \n\\r | |
2030 | Close the socket. It cannot be used after this call.");\r | |
2031 | \r | |
2032 | static int\r | |
2033 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,\r | |
2034 | int *timeoutp)\r | |
2035 | {\r | |
2036 | int res, timeout;\r | |
2037 | \r | |
2038 | timeout = 0;\r | |
2039 | res = connect(s->sock_fd, addr, addrlen);\r | |
2040 | \r | |
2041 | #ifdef MS_WINDOWS\r | |
2042 | \r | |
2043 | if (s->sock_timeout > 0.0) {\r | |
2044 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&\r | |
2045 | IS_SELECTABLE(s)) {\r | |
2046 | /* This is a mess. Best solution: trust select */\r | |
2047 | fd_set fds;\r | |
2048 | fd_set fds_exc;\r | |
2049 | struct timeval tv;\r | |
2050 | tv.tv_sec = (int)s->sock_timeout;\r | |
2051 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);\r | |
2052 | FD_ZERO(&fds);\r | |
2053 | FD_SET(s->sock_fd, &fds);\r | |
2054 | FD_ZERO(&fds_exc);\r | |
2055 | FD_SET(s->sock_fd, &fds_exc);\r | |
2056 | res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv);\r | |
2057 | if (res == 0) {\r | |
2058 | res = WSAEWOULDBLOCK;\r | |
2059 | timeout = 1;\r | |
2060 | } else if (res > 0) {\r | |
2061 | if (FD_ISSET(s->sock_fd, &fds))\r | |
2062 | /* The socket is in the writeable set - this\r | |
2063 | means connected */\r | |
2064 | res = 0;\r | |
2065 | else {\r | |
2066 | /* As per MS docs, we need to call getsockopt()\r | |
2067 | to get the underlying error */\r | |
2068 | int res_size = sizeof res;\r | |
2069 | /* It must be in the exception set */\r | |
2070 | assert(FD_ISSET(s->sock_fd, &fds_exc));\r | |
2071 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,\r | |
2072 | (char *)&res, &res_size))\r | |
2073 | /* getsockopt also clears WSAGetLastError,\r | |
2074 | so reset it back. */\r | |
2075 | WSASetLastError(res);\r | |
2076 | else\r | |
2077 | res = WSAGetLastError();\r | |
2078 | }\r | |
2079 | }\r | |
2080 | /* else if (res < 0) an error occurred */\r | |
2081 | }\r | |
2082 | }\r | |
2083 | \r | |
2084 | if (res < 0)\r | |
2085 | res = WSAGetLastError();\r | |
2086 | \r | |
2087 | #else\r | |
2088 | \r | |
2089 | if (s->sock_timeout > 0.0) {\r | |
2090 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {\r | |
2091 | timeout = internal_select(s, 1);\r | |
2092 | if (timeout == 0) {\r | |
2093 | /* Bug #1019808: in case of an EINPROGRESS,\r | |
2094 | use getsockopt(SO_ERROR) to get the real\r | |
2095 | error. */\r | |
2096 | socklen_t res_size = sizeof res;\r | |
2097 | (void)getsockopt(s->sock_fd, SOL_SOCKET,\r | |
2098 | SO_ERROR, &res, &res_size);\r | |
2099 | if (res == EISCONN)\r | |
2100 | res = 0;\r | |
2101 | errno = res;\r | |
2102 | }\r | |
2103 | else if (timeout == -1) {\r | |
2104 | res = errno; /* had error */\r | |
2105 | }\r | |
2106 | else\r | |
2107 | res = EWOULDBLOCK; /* timed out */\r | |
2108 | }\r | |
2109 | }\r | |
2110 | \r | |
2111 | if (res < 0)\r | |
2112 | res = errno;\r | |
2113 | \r | |
2114 | #endif\r | |
2115 | *timeoutp = timeout;\r | |
2116 | \r | |
2117 | return res;\r | |
2118 | }\r | |
2119 | \r | |
2120 | /* s.connect(sockaddr) method */\r | |
2121 | \r | |
2122 | static PyObject *\r | |
2123 | sock_connect(PySocketSockObject *s, PyObject *addro)\r | |
2124 | {\r | |
2125 | sock_addr_t addrbuf;\r | |
2126 | int addrlen;\r | |
2127 | int res;\r | |
2128 | int timeout;\r | |
2129 | \r | |
2130 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))\r | |
2131 | return NULL;\r | |
2132 | \r | |
2133 | Py_BEGIN_ALLOW_THREADS\r | |
2134 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);\r | |
2135 | Py_END_ALLOW_THREADS\r | |
2136 | \r | |
2137 | if (timeout == 1) {\r | |
2138 | PyErr_SetString(socket_timeout, "timed out");\r | |
2139 | return NULL;\r | |
2140 | }\r | |
2141 | if (res != 0)\r | |
2142 | return s->errorhandler();\r | |
2143 | Py_INCREF(Py_None);\r | |
2144 | return Py_None;\r | |
2145 | }\r | |
2146 | \r | |
2147 | PyDoc_STRVAR(connect_doc,\r | |
2148 | "connect(address)\n\\r | |
2149 | \n\\r | |
2150 | Connect the socket to a remote address. For IP sockets, the address\n\\r | |
2151 | is a pair (host, port).");\r | |
2152 | \r | |
2153 | \r | |
2154 | /* s.connect_ex(sockaddr) method */\r | |
2155 | \r | |
2156 | static PyObject *\r | |
2157 | sock_connect_ex(PySocketSockObject *s, PyObject *addro)\r | |
2158 | {\r | |
2159 | sock_addr_t addrbuf;\r | |
2160 | int addrlen;\r | |
2161 | int res;\r | |
2162 | int timeout;\r | |
2163 | \r | |
2164 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))\r | |
2165 | return NULL;\r | |
2166 | \r | |
2167 | Py_BEGIN_ALLOW_THREADS\r | |
2168 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);\r | |
2169 | Py_END_ALLOW_THREADS\r | |
2170 | \r | |
2171 | /* Signals are not errors (though they may raise exceptions). Adapted\r | |
2172 | from PyErr_SetFromErrnoWithFilenameObject(). */\r | |
2173 | #ifdef EINTR\r | |
2174 | if (res == EINTR && PyErr_CheckSignals())\r | |
2175 | return NULL;\r | |
2176 | #endif\r | |
2177 | \r | |
2178 | return PyInt_FromLong((long) res);\r | |
2179 | }\r | |
2180 | \r | |
2181 | PyDoc_STRVAR(connect_ex_doc,\r | |
2182 | "connect_ex(address) -> errno\n\\r | |
2183 | \n\\r | |
2184 | This is like connect(address), but returns an error code (the errno value)\n\\r | |
2185 | instead of raising an exception when an error occurs.");\r | |
2186 | \r | |
2187 | \r | |
2188 | /* s.fileno() method */\r | |
2189 | \r | |
2190 | static PyObject *\r | |
2191 | sock_fileno(PySocketSockObject *s)\r | |
2192 | {\r | |
2193 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG\r | |
2194 | return PyInt_FromLong((long) s->sock_fd);\r | |
2195 | #else\r | |
2196 | return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd);\r | |
2197 | #endif\r | |
2198 | }\r | |
2199 | \r | |
2200 | PyDoc_STRVAR(fileno_doc,\r | |
2201 | "fileno() -> integer\n\\r | |
2202 | \n\\r | |
2203 | Return the integer file descriptor of the socket.");\r | |
2204 | \r | |
2205 | \r | |
2206 | #ifndef NO_DUP\r | |
2207 | /* s.dup() method */\r | |
2208 | \r | |
2209 | static PyObject *\r | |
2210 | sock_dup(PySocketSockObject *s)\r | |
2211 | {\r | |
2212 | SOCKET_T newfd;\r | |
2213 | PyObject *sock;\r | |
2214 | \r | |
2215 | newfd = dup(s->sock_fd);\r | |
2216 | if (newfd < 0)\r | |
2217 | return s->errorhandler();\r | |
2218 | sock = (PyObject *) new_sockobject(newfd,\r | |
2219 | s->sock_family,\r | |
2220 | s->sock_type,\r | |
2221 | s->sock_proto);\r | |
2222 | if (sock == NULL)\r | |
2223 | SOCKETCLOSE(newfd);\r | |
2224 | return sock;\r | |
2225 | }\r | |
2226 | \r | |
2227 | PyDoc_STRVAR(dup_doc,\r | |
2228 | "dup() -> socket object\n\\r | |
2229 | \n\\r | |
2230 | Return a new socket object connected to the same system resource.");\r | |
2231 | \r | |
2232 | #endif\r | |
2233 | \r | |
2234 | \r | |
2235 | /* s.getsockname() method */\r | |
2236 | \r | |
2237 | static PyObject *\r | |
2238 | sock_getsockname(PySocketSockObject *s)\r | |
2239 | {\r | |
2240 | sock_addr_t addrbuf;\r | |
2241 | int res;\r | |
2242 | socklen_t addrlen;\r | |
2243 | \r | |
2244 | if (!getsockaddrlen(s, &addrlen))\r | |
2245 | return NULL;\r | |
2246 | memset(&addrbuf, 0, addrlen);\r | |
2247 | Py_BEGIN_ALLOW_THREADS\r | |
2248 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);\r | |
2249 | Py_END_ALLOW_THREADS\r | |
2250 | if (res < 0)\r | |
2251 | return s->errorhandler();\r | |
2252 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,\r | |
2253 | s->sock_proto);\r | |
2254 | }\r | |
2255 | \r | |
2256 | PyDoc_STRVAR(getsockname_doc,\r | |
2257 | "getsockname() -> address info\n\\r | |
2258 | \n\\r | |
2259 | Return the address of the local endpoint. For IP sockets, the address\n\\r | |
2260 | info is a pair (hostaddr, port).");\r | |
2261 | \r | |
2262 | \r | |
2263 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */\r | |
2264 | /* s.getpeername() method */\r | |
2265 | \r | |
2266 | static PyObject *\r | |
2267 | sock_getpeername(PySocketSockObject *s)\r | |
2268 | {\r | |
2269 | sock_addr_t addrbuf;\r | |
2270 | int res;\r | |
2271 | socklen_t addrlen;\r | |
2272 | \r | |
2273 | if (!getsockaddrlen(s, &addrlen))\r | |
2274 | return NULL;\r | |
2275 | memset(&addrbuf, 0, addrlen);\r | |
2276 | Py_BEGIN_ALLOW_THREADS\r | |
2277 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);\r | |
2278 | Py_END_ALLOW_THREADS\r | |
2279 | if (res < 0)\r | |
2280 | return s->errorhandler();\r | |
2281 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,\r | |
2282 | s->sock_proto);\r | |
2283 | }\r | |
2284 | \r | |
2285 | PyDoc_STRVAR(getpeername_doc,\r | |
2286 | "getpeername() -> address info\n\\r | |
2287 | \n\\r | |
2288 | Return the address of the remote endpoint. For IP sockets, the address\n\\r | |
2289 | info is a pair (hostaddr, port).");\r | |
2290 | \r | |
2291 | #endif /* HAVE_GETPEERNAME */\r | |
2292 | \r | |
2293 | \r | |
2294 | /* s.listen(n) method */\r | |
2295 | \r | |
2296 | static PyObject *\r | |
2297 | sock_listen(PySocketSockObject *s, PyObject *arg)\r | |
2298 | {\r | |
2299 | int backlog;\r | |
2300 | int res;\r | |
2301 | \r | |
2302 | backlog = _PyInt_AsInt(arg);\r | |
2303 | if (backlog == -1 && PyErr_Occurred())\r | |
2304 | return NULL;\r | |
2305 | Py_BEGIN_ALLOW_THREADS\r | |
2306 | /* To avoid problems on systems that don't allow a negative backlog\r | |
2307 | * (which doesn't make sense anyway) we force a minimum value of 0. */\r | |
2308 | if (backlog < 0)\r | |
2309 | backlog = 0;\r | |
2310 | res = listen(s->sock_fd, backlog);\r | |
2311 | Py_END_ALLOW_THREADS\r | |
2312 | if (res < 0)\r | |
2313 | return s->errorhandler();\r | |
2314 | Py_INCREF(Py_None);\r | |
2315 | return Py_None;\r | |
2316 | }\r | |
2317 | \r | |
2318 | PyDoc_STRVAR(listen_doc,\r | |
2319 | "listen(backlog)\n\\r | |
2320 | \n\\r | |
2321 | Enable a server to accept connections. The backlog argument must be at\n\\r | |
2322 | least 0 (if it is lower, it is set to 0); it specifies the number of\n\\r | |
2323 | unaccepted connections that the system will allow before refusing new\n\\r | |
2324 | connections.");\r | |
2325 | \r | |
2326 | \r | |
2327 | #ifndef NO_DUP\r | |
2328 | /* s.makefile(mode) method.\r | |
2329 | Create a new open file object referring to a dupped version of\r | |
2330 | the socket's file descriptor. (The dup() call is necessary so\r | |
2331 | that the open file and socket objects may be closed independent\r | |
2332 | of each other.)\r | |
2333 | The mode argument specifies 'r' or 'w' passed to fdopen(). */\r | |
2334 | \r | |
2335 | static PyObject *\r | |
2336 | sock_makefile(PySocketSockObject *s, PyObject *args)\r | |
2337 | {\r | |
2338 | extern int fclose(FILE *);\r | |
2339 | char *mode = "r";\r | |
2340 | int bufsize = -1;\r | |
2341 | #ifdef MS_WIN32\r | |
2342 | Py_intptr_t fd;\r | |
2343 | #else\r | |
2344 | int fd;\r | |
2345 | #endif\r | |
2346 | FILE *fp;\r | |
2347 | PyObject *f;\r | |
2348 | #ifdef __VMS\r | |
2349 | char *mode_r = "r";\r | |
2350 | char *mode_w = "w";\r | |
2351 | #endif\r | |
2352 | \r | |
2353 | if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))\r | |
2354 | return NULL;\r | |
2355 | #ifdef __VMS\r | |
2356 | if (strcmp(mode,"rb") == 0) {\r | |
2357 | mode = mode_r;\r | |
2358 | }\r | |
2359 | else {\r | |
2360 | if (strcmp(mode,"wb") == 0) {\r | |
2361 | mode = mode_w;\r | |
2362 | }\r | |
2363 | }\r | |
2364 | #endif\r | |
2365 | #ifdef MS_WIN32\r | |
2366 | if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||\r | |
2367 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))\r | |
2368 | #else\r | |
2369 | if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)\r | |
2370 | #endif\r | |
2371 | {\r | |
2372 | if (fd >= 0)\r | |
2373 | SOCKETCLOSE(fd);\r | |
2374 | return s->errorhandler();\r | |
2375 | }\r | |
2376 | f = PyFile_FromFile(fp, "<socket>", mode, fclose);\r | |
2377 | if (f != NULL)\r | |
2378 | PyFile_SetBufSize(f, bufsize);\r | |
2379 | return f;\r | |
2380 | }\r | |
2381 | \r | |
2382 | PyDoc_STRVAR(makefile_doc,\r | |
2383 | "makefile([mode[, buffersize]]) -> file object\n\\r | |
2384 | \n\\r | |
2385 | Return a regular file object corresponding to the socket.\n\\r | |
2386 | The mode and buffersize arguments are as for the built-in open() function.");\r | |
2387 | \r | |
2388 | #endif /* NO_DUP */\r | |
2389 | \r | |
2390 | /*\r | |
2391 | * This is the guts of the recv() and recv_into() methods, which reads into a\r | |
2392 | * char buffer. If you have any inc/dec ref to do to the objects that contain\r | |
2393 | * the buffer, do it in the caller. This function returns the number of bytes\r | |
2394 | * successfully read. If there was an error, it returns -1. Note that it is\r | |
2395 | * also possible that we return a number of bytes smaller than the request\r | |
2396 | * bytes.\r | |
2397 | */\r | |
2398 | static ssize_t\r | |
2399 | sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags)\r | |
2400 | {\r | |
2401 | ssize_t outlen = -1;\r | |
2402 | int timeout;\r | |
2403 | #ifdef __VMS\r | |
2404 | int remaining;\r | |
2405 | char *read_buf;\r | |
2406 | #endif\r | |
2407 | \r | |
2408 | if (!IS_SELECTABLE(s)) {\r | |
2409 | select_error();\r | |
2410 | return -1;\r | |
2411 | }\r | |
2412 | \r | |
2413 | #ifndef __VMS\r | |
2414 | BEGIN_SELECT_LOOP(s)\r | |
2415 | Py_BEGIN_ALLOW_THREADS\r | |
2416 | timeout = internal_select_ex(s, 0, interval);\r | |
2417 | if (!timeout)\r | |
2418 | outlen = recv(s->sock_fd, cbuf, len, flags);\r | |
2419 | Py_END_ALLOW_THREADS\r | |
2420 | \r | |
2421 | if (timeout == 1) {\r | |
2422 | PyErr_SetString(socket_timeout, "timed out");\r | |
2423 | return -1;\r | |
2424 | }\r | |
2425 | END_SELECT_LOOP(s)\r | |
2426 | if (outlen < 0) {\r | |
2427 | /* Note: the call to errorhandler() ALWAYS indirectly returned\r | |
2428 | NULL, so ignore its return value */\r | |
2429 | s->errorhandler();\r | |
2430 | return -1;\r | |
2431 | }\r | |
2432 | #else\r | |
2433 | read_buf = cbuf;\r | |
2434 | remaining = len;\r | |
2435 | while (remaining != 0) {\r | |
2436 | unsigned int segment;\r | |
2437 | int nread = -1;\r | |
2438 | \r | |
2439 | segment = remaining /SEGMENT_SIZE;\r | |
2440 | if (segment != 0) {\r | |
2441 | segment = SEGMENT_SIZE;\r | |
2442 | }\r | |
2443 | else {\r | |
2444 | segment = remaining;\r | |
2445 | }\r | |
2446 | \r | |
2447 | BEGIN_SELECT_LOOP(s)\r | |
2448 | Py_BEGIN_ALLOW_THREADS\r | |
2449 | timeout = internal_select_ex(s, 0, interval);\r | |
2450 | if (!timeout)\r | |
2451 | nread = recv(s->sock_fd, read_buf, segment, flags);\r | |
2452 | Py_END_ALLOW_THREADS\r | |
2453 | \r | |
2454 | if (timeout == 1) {\r | |
2455 | PyErr_SetString(socket_timeout, "timed out");\r | |
2456 | return -1;\r | |
2457 | }\r | |
2458 | END_SELECT_LOOP(s)\r | |
2459 | \r | |
2460 | if (nread < 0) {\r | |
2461 | s->errorhandler();\r | |
2462 | return -1;\r | |
2463 | }\r | |
2464 | if (nread != remaining) {\r | |
2465 | read_buf += nread;\r | |
2466 | break;\r | |
2467 | }\r | |
2468 | \r | |
2469 | remaining -= segment;\r | |
2470 | read_buf += segment;\r | |
2471 | }\r | |
2472 | outlen = read_buf - cbuf;\r | |
2473 | #endif /* !__VMS */\r | |
2474 | \r | |
2475 | return outlen;\r | |
2476 | }\r | |
2477 | \r | |
2478 | \r | |
2479 | /* s.recv(nbytes [,flags]) method */\r | |
2480 | \r | |
2481 | static PyObject *\r | |
2482 | sock_recv(PySocketSockObject *s, PyObject *args)\r | |
2483 | {\r | |
2484 | int recvlen, flags = 0;\r | |
2485 | ssize_t outlen;\r | |
2486 | PyObject *buf;\r | |
2487 | \r | |
2488 | if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags))\r | |
2489 | return NULL;\r | |
2490 | \r | |
2491 | if (recvlen < 0) {\r | |
2492 | PyErr_SetString(PyExc_ValueError,\r | |
2493 | "negative buffersize in recv");\r | |
2494 | return NULL;\r | |
2495 | }\r | |
2496 | \r | |
2497 | /* Allocate a new string. */\r | |
2498 | buf = PyString_FromStringAndSize((char *) 0, recvlen);\r | |
2499 | if (buf == NULL)\r | |
2500 | return NULL;\r | |
2501 | \r | |
2502 | /* Call the guts */\r | |
2503 | outlen = sock_recv_guts(s, PyString_AS_STRING(buf), recvlen, flags);\r | |
2504 | if (outlen < 0) {\r | |
2505 | /* An error occurred, release the string and return an\r | |
2506 | error. */\r | |
2507 | Py_DECREF(buf);\r | |
2508 | return NULL;\r | |
2509 | }\r | |
2510 | if (outlen != recvlen) {\r | |
2511 | /* We did not read as many bytes as we anticipated, resize the\r | |
2512 | string if possible and be successful. */\r | |
2513 | if (_PyString_Resize(&buf, outlen) < 0)\r | |
2514 | /* Oopsy, not so successful after all. */\r | |
2515 | return NULL;\r | |
2516 | }\r | |
2517 | \r | |
2518 | return buf;\r | |
2519 | }\r | |
2520 | \r | |
2521 | PyDoc_STRVAR(recv_doc,\r | |
2522 | "recv(buffersize[, flags]) -> data\n\\r | |
2523 | \n\\r | |
2524 | Receive up to buffersize bytes from the socket. For the optional flags\n\\r | |
2525 | argument, see the Unix manual. When no data is available, block until\n\\r | |
2526 | at least one byte is available or until the remote end is closed. When\n\\r | |
2527 | the remote end is closed and all data is read, return the empty string.");\r | |
2528 | \r | |
2529 | \r | |
2530 | /* s.recv_into(buffer, [nbytes [,flags]]) method */\r | |
2531 | \r | |
2532 | static PyObject*\r | |
2533 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)\r | |
2534 | {\r | |
2535 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0};\r | |
2536 | \r | |
2537 | int recvlen = 0, flags = 0;\r | |
2538 | ssize_t readlen;\r | |
2539 | Py_buffer buf;\r | |
2540 | Py_ssize_t buflen;\r | |
2541 | \r | |
2542 | /* Get the buffer's memory */\r | |
2543 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recv_into", kwlist,\r | |
2544 | &buf, &recvlen, &flags))\r | |
2545 | return NULL;\r | |
2546 | buflen = buf.len;\r | |
2547 | assert(buf.buf != 0 && buflen > 0);\r | |
2548 | \r | |
2549 | if (recvlen < 0) {\r | |
2550 | PyErr_SetString(PyExc_ValueError,\r | |
2551 | "negative buffersize in recv_into");\r | |
2552 | goto error;\r | |
2553 | }\r | |
2554 | if (recvlen == 0) {\r | |
2555 | /* If nbytes was not specified, use the buffer's length */\r | |
2556 | recvlen = buflen;\r | |
2557 | }\r | |
2558 | \r | |
2559 | /* Check if the buffer is large enough */\r | |
2560 | if (buflen < recvlen) {\r | |
2561 | PyErr_SetString(PyExc_ValueError,\r | |
2562 | "buffer too small for requested bytes");\r | |
2563 | goto error;\r | |
2564 | }\r | |
2565 | \r | |
2566 | /* Call the guts */\r | |
2567 | readlen = sock_recv_guts(s, buf.buf, recvlen, flags);\r | |
2568 | if (readlen < 0) {\r | |
2569 | /* Return an error. */\r | |
2570 | goto error;\r | |
2571 | }\r | |
2572 | \r | |
2573 | PyBuffer_Release(&buf);\r | |
2574 | /* Return the number of bytes read. Note that we do not do anything\r | |
2575 | special here in the case that readlen < recvlen. */\r | |
2576 | return PyInt_FromSsize_t(readlen);\r | |
2577 | \r | |
2578 | error:\r | |
2579 | PyBuffer_Release(&buf);\r | |
2580 | return NULL;\r | |
2581 | }\r | |
2582 | \r | |
2583 | PyDoc_STRVAR(recv_into_doc,\r | |
2584 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\\r | |
2585 | \n\\r | |
2586 | A version of recv() that stores its data into a buffer rather than creating \n\\r | |
2587 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\\r | |
2588 | is not specified (or 0), receive up to the size available in the given buffer.\n\\r | |
2589 | \n\\r | |
2590 | See recv() for documentation about the flags.");\r | |
2591 | \r | |
2592 | \r | |
2593 | /*\r | |
2594 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads\r | |
2595 | * into a char buffer. If you have any inc/def ref to do to the objects that\r | |
2596 | * contain the buffer, do it in the caller. This function returns the number\r | |
2597 | * of bytes successfully read. If there was an error, it returns -1. Note\r | |
2598 | * that it is also possible that we return a number of bytes smaller than the\r | |
2599 | * request bytes.\r | |
2600 | *\r | |
2601 | * 'addr' is a return value for the address object. Note that you must decref\r | |
2602 | * it yourself.\r | |
2603 | */\r | |
2604 | static ssize_t\r | |
2605 | sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags,\r | |
2606 | PyObject** addr)\r | |
2607 | {\r | |
2608 | sock_addr_t addrbuf;\r | |
2609 | int timeout;\r | |
2610 | ssize_t n = -1;\r | |
2611 | socklen_t addrlen;\r | |
2612 | \r | |
2613 | *addr = NULL;\r | |
2614 | \r | |
2615 | if (!getsockaddrlen(s, &addrlen))\r | |
2616 | return -1;\r | |
2617 | \r | |
2618 | if (!IS_SELECTABLE(s)) {\r | |
2619 | select_error();\r | |
2620 | return -1;\r | |
2621 | }\r | |
2622 | \r | |
2623 | BEGIN_SELECT_LOOP(s)\r | |
2624 | Py_BEGIN_ALLOW_THREADS\r | |
2625 | memset(&addrbuf, 0, addrlen);\r | |
2626 | timeout = internal_select_ex(s, 0, interval);\r | |
2627 | if (!timeout) {\r | |
2628 | #ifndef MS_WINDOWS\r | |
2629 | #if defined(PYOS_OS2) && !defined(PYCC_GCC)\r | |
2630 | n = recvfrom(s->sock_fd, cbuf, len, flags,\r | |
2631 | SAS2SA(&addrbuf), &addrlen);\r | |
2632 | #else\r | |
2633 | n = recvfrom(s->sock_fd, cbuf, len, flags,\r | |
2634 | (void *) &addrbuf, &addrlen);\r | |
2635 | #endif\r | |
2636 | #else\r | |
2637 | n = recvfrom(s->sock_fd, cbuf, len, flags,\r | |
2638 | SAS2SA(&addrbuf), &addrlen);\r | |
2639 | #endif\r | |
2640 | }\r | |
2641 | Py_END_ALLOW_THREADS\r | |
2642 | \r | |
2643 | if (timeout == 1) {\r | |
2644 | PyErr_SetString(socket_timeout, "timed out");\r | |
2645 | return -1;\r | |
2646 | }\r | |
2647 | END_SELECT_LOOP(s)\r | |
2648 | if (n < 0) {\r | |
2649 | s->errorhandler();\r | |
2650 | return -1;\r | |
2651 | }\r | |
2652 | \r | |
2653 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),\r | |
2654 | addrlen, s->sock_proto)))\r | |
2655 | return -1;\r | |
2656 | \r | |
2657 | return n;\r | |
2658 | }\r | |
2659 | \r | |
2660 | /* s.recvfrom(nbytes [,flags]) method */\r | |
2661 | \r | |
2662 | static PyObject *\r | |
2663 | sock_recvfrom(PySocketSockObject *s, PyObject *args)\r | |
2664 | {\r | |
2665 | PyObject *buf = NULL;\r | |
2666 | PyObject *addr = NULL;\r | |
2667 | PyObject *ret = NULL;\r | |
2668 | int recvlen, flags = 0;\r | |
2669 | ssize_t outlen;\r | |
2670 | \r | |
2671 | if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags))\r | |
2672 | return NULL;\r | |
2673 | \r | |
2674 | if (recvlen < 0) {\r | |
2675 | PyErr_SetString(PyExc_ValueError,\r | |
2676 | "negative buffersize in recvfrom");\r | |
2677 | return NULL;\r | |
2678 | }\r | |
2679 | \r | |
2680 | buf = PyString_FromStringAndSize((char *) 0, recvlen);\r | |
2681 | if (buf == NULL)\r | |
2682 | return NULL;\r | |
2683 | \r | |
2684 | outlen = sock_recvfrom_guts(s, PyString_AS_STRING(buf),\r | |
2685 | recvlen, flags, &addr);\r | |
2686 | if (outlen < 0) {\r | |
2687 | goto finally;\r | |
2688 | }\r | |
2689 | \r | |
2690 | if (outlen != recvlen) {\r | |
2691 | /* We did not read as many bytes as we anticipated, resize the\r | |
2692 | string if possible and be successful. */\r | |
2693 | if (_PyString_Resize(&buf, outlen) < 0)\r | |
2694 | /* Oopsy, not so successful after all. */\r | |
2695 | goto finally;\r | |
2696 | }\r | |
2697 | \r | |
2698 | ret = PyTuple_Pack(2, buf, addr);\r | |
2699 | \r | |
2700 | finally:\r | |
2701 | Py_XDECREF(buf);\r | |
2702 | Py_XDECREF(addr);\r | |
2703 | return ret;\r | |
2704 | }\r | |
2705 | \r | |
2706 | PyDoc_STRVAR(recvfrom_doc,\r | |
2707 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\\r | |
2708 | \n\\r | |
2709 | Like recv(buffersize, flags) but also return the sender's address info.");\r | |
2710 | \r | |
2711 | \r | |
2712 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */\r | |
2713 | \r | |
2714 | static PyObject *\r | |
2715 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)\r | |
2716 | {\r | |
2717 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0};\r | |
2718 | \r | |
2719 | int recvlen = 0, flags = 0;\r | |
2720 | ssize_t readlen;\r | |
2721 | Py_buffer buf;\r | |
2722 | int buflen;\r | |
2723 | \r | |
2724 | PyObject *addr = NULL;\r | |
2725 | \r | |
2726 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recvfrom_into",\r | |
2727 | kwlist, &buf,\r | |
2728 | &recvlen, &flags))\r | |
2729 | return NULL;\r | |
2730 | buflen = buf.len;\r | |
2731 | \r | |
2732 | if (recvlen < 0) {\r | |
2733 | PyErr_SetString(PyExc_ValueError,\r | |
2734 | "negative buffersize in recvfrom_into");\r | |
2735 | goto error;\r | |
2736 | }\r | |
2737 | if (recvlen == 0) {\r | |
2738 | /* If nbytes was not specified, use the buffer's length */\r | |
2739 | recvlen = buflen;\r | |
2740 | } else if (recvlen > buflen) {\r | |
2741 | PyErr_SetString(PyExc_ValueError,\r | |
2742 | "nbytes is greater than the length of the buffer");\r | |
2743 | goto error;\r | |
2744 | }\r | |
2745 | \r | |
2746 | readlen = sock_recvfrom_guts(s, buf.buf, recvlen, flags, &addr);\r | |
2747 | if (readlen < 0) {\r | |
2748 | /* Return an error */\r | |
2749 | goto error;\r | |
2750 | }\r | |
2751 | \r | |
2752 | PyBuffer_Release(&buf);\r | |
2753 | /* Return the number of bytes read and the address. Note that we do\r | |
2754 | not do anything special here in the case that readlen < recvlen. */\r | |
2755 | return Py_BuildValue("lN", readlen, addr);\r | |
2756 | \r | |
2757 | error:\r | |
2758 | Py_XDECREF(addr);\r | |
2759 | PyBuffer_Release(&buf);\r | |
2760 | return NULL;\r | |
2761 | }\r | |
2762 | \r | |
2763 | PyDoc_STRVAR(recvfrom_into_doc,\r | |
2764 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\\r | |
2765 | \n\\r | |
2766 | Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");\r | |
2767 | \r | |
2768 | \r | |
2769 | /* s.send(data [,flags]) method */\r | |
2770 | \r | |
2771 | static PyObject *\r | |
2772 | sock_send(PySocketSockObject *s, PyObject *args)\r | |
2773 | {\r | |
2774 | char *buf;\r | |
2775 | int len, n = -1, flags = 0, timeout;\r | |
2776 | Py_buffer pbuf;\r | |
2777 | \r | |
2778 | if (!PyArg_ParseTuple(args, "s*|i:send", &pbuf, &flags))\r | |
2779 | return NULL;\r | |
2780 | \r | |
2781 | if (!IS_SELECTABLE(s)) {\r | |
2782 | PyBuffer_Release(&pbuf);\r | |
2783 | return select_error();\r | |
2784 | }\r | |
2785 | buf = pbuf.buf;\r | |
2786 | len = pbuf.len;\r | |
2787 | \r | |
2788 | BEGIN_SELECT_LOOP(s)\r | |
2789 | Py_BEGIN_ALLOW_THREADS\r | |
2790 | timeout = internal_select_ex(s, 1, interval);\r | |
2791 | if (!timeout)\r | |
2792 | #ifdef __VMS\r | |
2793 | n = sendsegmented(s->sock_fd, buf, len, flags);\r | |
2794 | #else\r | |
2795 | n = send(s->sock_fd, buf, len, flags);\r | |
2796 | #endif\r | |
2797 | Py_END_ALLOW_THREADS\r | |
2798 | if (timeout == 1) {\r | |
2799 | PyBuffer_Release(&pbuf);\r | |
2800 | PyErr_SetString(socket_timeout, "timed out");\r | |
2801 | return NULL;\r | |
2802 | }\r | |
2803 | END_SELECT_LOOP(s)\r | |
2804 | \r | |
2805 | PyBuffer_Release(&pbuf);\r | |
2806 | if (n < 0)\r | |
2807 | return s->errorhandler();\r | |
2808 | return PyInt_FromLong((long)n);\r | |
2809 | }\r | |
2810 | \r | |
2811 | PyDoc_STRVAR(send_doc,\r | |
2812 | "send(data[, flags]) -> count\n\\r | |
2813 | \n\\r | |
2814 | Send a data string to the socket. For the optional flags\n\\r | |
2815 | argument, see the Unix manual. Return the number of bytes\n\\r | |
2816 | sent; this may be less than len(data) if the network is busy.");\r | |
2817 | \r | |
2818 | \r | |
2819 | /* s.sendall(data [,flags]) method */\r | |
2820 | \r | |
2821 | static PyObject *\r | |
2822 | sock_sendall(PySocketSockObject *s, PyObject *args)\r | |
2823 | {\r | |
2824 | char *buf;\r | |
2825 | int len, n = -1, flags = 0, timeout, saved_errno;\r | |
2826 | Py_buffer pbuf;\r | |
2827 | \r | |
2828 | if (!PyArg_ParseTuple(args, "s*|i:sendall", &pbuf, &flags))\r | |
2829 | return NULL;\r | |
2830 | buf = pbuf.buf;\r | |
2831 | len = pbuf.len;\r | |
2832 | \r | |
2833 | if (!IS_SELECTABLE(s)) {\r | |
2834 | PyBuffer_Release(&pbuf);\r | |
2835 | return select_error();\r | |
2836 | }\r | |
2837 | \r | |
2838 | do {\r | |
2839 | BEGIN_SELECT_LOOP(s)\r | |
2840 | Py_BEGIN_ALLOW_THREADS\r | |
2841 | timeout = internal_select_ex(s, 1, interval);\r | |
2842 | n = -1;\r | |
2843 | if (!timeout) {\r | |
2844 | #ifdef __VMS\r | |
2845 | n = sendsegmented(s->sock_fd, buf, len, flags);\r | |
2846 | #else\r | |
2847 | n = send(s->sock_fd, buf, len, flags);\r | |
2848 | #endif\r | |
2849 | }\r | |
2850 | Py_END_ALLOW_THREADS\r | |
2851 | if (timeout == 1) {\r | |
2852 | PyBuffer_Release(&pbuf);\r | |
2853 | PyErr_SetString(socket_timeout, "timed out");\r | |
2854 | return NULL;\r | |
2855 | }\r | |
2856 | END_SELECT_LOOP(s)\r | |
2857 | /* PyErr_CheckSignals() might change errno */\r | |
2858 | saved_errno = errno;\r | |
2859 | /* We must run our signal handlers before looping again.\r | |
2860 | send() can return a successful partial write when it is\r | |
2861 | interrupted, so we can't restrict ourselves to EINTR. */\r | |
2862 | if (PyErr_CheckSignals()) {\r | |
2863 | PyBuffer_Release(&pbuf);\r | |
2864 | return NULL;\r | |
2865 | }\r | |
2866 | if (n < 0) {\r | |
2867 | /* If interrupted, try again */\r | |
2868 | if (saved_errno == EINTR)\r | |
2869 | continue;\r | |
2870 | else\r | |
2871 | break;\r | |
2872 | }\r | |
2873 | buf += n;\r | |
2874 | len -= n;\r | |
2875 | } while (len > 0);\r | |
2876 | PyBuffer_Release(&pbuf);\r | |
2877 | \r | |
2878 | if (n < 0)\r | |
2879 | return s->errorhandler();\r | |
2880 | \r | |
2881 | Py_INCREF(Py_None);\r | |
2882 | return Py_None;\r | |
2883 | }\r | |
2884 | \r | |
2885 | PyDoc_STRVAR(sendall_doc,\r | |
2886 | "sendall(data[, flags])\n\\r | |
2887 | \n\\r | |
2888 | Send a data string to the socket. For the optional flags\n\\r | |
2889 | argument, see the Unix manual. This calls send() repeatedly\n\\r | |
2890 | until all data is sent. If an error occurs, it's impossible\n\\r | |
2891 | to tell how much data has been sent.");\r | |
2892 | \r | |
2893 | \r | |
2894 | /* s.sendto(data, [flags,] sockaddr) method */\r | |
2895 | \r | |
2896 | static PyObject *\r | |
2897 | sock_sendto(PySocketSockObject *s, PyObject *args)\r | |
2898 | {\r | |
2899 | Py_buffer pbuf;\r | |
2900 | PyObject *addro;\r | |
2901 | char *buf;\r | |
2902 | Py_ssize_t len;\r | |
2903 | sock_addr_t addrbuf;\r | |
2904 | int addrlen, flags, timeout;\r | |
2905 | long n = -1;\r | |
2906 | int arglen;\r | |
2907 | \r | |
2908 | flags = 0;\r | |
2909 | arglen = PyTuple_Size(args);\r | |
2910 | switch(arglen) {\r | |
2911 | case 2:\r | |
2912 | PyArg_ParseTuple(args, "s*O:sendto", &pbuf, &addro);\r | |
2913 | break;\r | |
2914 | case 3:\r | |
2915 | PyArg_ParseTuple(args, "s*iO:sendto", &pbuf, &flags, &addro);\r | |
2916 | break;\r | |
2917 | default:\r | |
2918 | PyErr_Format(PyExc_TypeError, "sendto() takes 2 or 3"\r | |
2919 | " arguments (%d given)", arglen);\r | |
2920 | }\r | |
2921 | if (PyErr_Occurred())\r | |
2922 | return NULL;\r | |
2923 | \r | |
2924 | buf = pbuf.buf;\r | |
2925 | len = pbuf.len;\r | |
2926 | \r | |
2927 | if (!IS_SELECTABLE(s)) {\r | |
2928 | PyBuffer_Release(&pbuf);\r | |
2929 | return select_error();\r | |
2930 | }\r | |
2931 | \r | |
2932 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {\r | |
2933 | PyBuffer_Release(&pbuf);\r | |
2934 | return NULL;\r | |
2935 | }\r | |
2936 | \r | |
2937 | BEGIN_SELECT_LOOP(s)\r | |
2938 | Py_BEGIN_ALLOW_THREADS\r | |
2939 | timeout = internal_select_ex(s, 1, interval);\r | |
2940 | if (!timeout)\r | |
2941 | n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);\r | |
2942 | Py_END_ALLOW_THREADS\r | |
2943 | \r | |
2944 | if (timeout == 1) {\r | |
2945 | PyBuffer_Release(&pbuf);\r | |
2946 | PyErr_SetString(socket_timeout, "timed out");\r | |
2947 | return NULL;\r | |
2948 | }\r | |
2949 | END_SELECT_LOOP(s)\r | |
2950 | PyBuffer_Release(&pbuf);\r | |
2951 | if (n < 0)\r | |
2952 | return s->errorhandler();\r | |
2953 | return PyInt_FromLong((long)n);\r | |
2954 | }\r | |
2955 | \r | |
2956 | PyDoc_STRVAR(sendto_doc,\r | |
2957 | "sendto(data[, flags], address) -> count\n\\r | |
2958 | \n\\r | |
2959 | Like send(data, flags) but allows specifying the destination address.\n\\r | |
2960 | For IP sockets, the address is a pair (hostaddr, port).");\r | |
2961 | \r | |
2962 | \r | |
2963 | /* s.shutdown(how) method */\r | |
2964 | \r | |
2965 | static PyObject *\r | |
2966 | sock_shutdown(PySocketSockObject *s, PyObject *arg)\r | |
2967 | {\r | |
2968 | int how;\r | |
2969 | int res;\r | |
2970 | \r | |
2971 | how = _PyInt_AsInt(arg);\r | |
2972 | if (how == -1 && PyErr_Occurred())\r | |
2973 | return NULL;\r | |
2974 | Py_BEGIN_ALLOW_THREADS\r | |
2975 | res = shutdown(s->sock_fd, how);\r | |
2976 | Py_END_ALLOW_THREADS\r | |
2977 | if (res < 0)\r | |
2978 | return s->errorhandler();\r | |
2979 | Py_INCREF(Py_None);\r | |
2980 | return Py_None;\r | |
2981 | }\r | |
2982 | \r | |
2983 | PyDoc_STRVAR(shutdown_doc,\r | |
2984 | "shutdown(flag)\n\\r | |
2985 | \n\\r | |
2986 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\\r | |
2987 | of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");\r | |
2988 | \r | |
2989 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL)\r | |
2990 | static PyObject*\r | |
2991 | sock_ioctl(PySocketSockObject *s, PyObject *arg)\r | |
2992 | {\r | |
2993 | unsigned long cmd = SIO_RCVALL;\r | |
2994 | PyObject *argO;\r | |
2995 | DWORD recv;\r | |
2996 | \r | |
2997 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO))\r | |
2998 | return NULL;\r | |
2999 | \r | |
3000 | switch (cmd) {\r | |
3001 | case SIO_RCVALL: {\r | |
3002 | unsigned int option = RCVALL_ON;\r | |
3003 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))\r | |
3004 | return NULL;\r | |
3005 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),\r | |
3006 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {\r | |
3007 | return set_error();\r | |
3008 | }\r | |
3009 | return PyLong_FromUnsignedLong(recv); }\r | |
3010 | case SIO_KEEPALIVE_VALS: {\r | |
3011 | struct tcp_keepalive ka;\r | |
3012 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd,\r | |
3013 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval))\r | |
3014 | return NULL;\r | |
3015 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka),\r | |
3016 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {\r | |
3017 | return set_error();\r | |
3018 | }\r | |
3019 | return PyLong_FromUnsignedLong(recv); }\r | |
3020 | default:\r | |
3021 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd);\r | |
3022 | return NULL;\r | |
3023 | }\r | |
3024 | }\r | |
3025 | PyDoc_STRVAR(sock_ioctl_doc,\r | |
3026 | "ioctl(cmd, option) -> long\n\\r | |
3027 | \n\\r | |
3028 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\\r | |
3029 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\\r | |
3030 | SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).");\r | |
3031 | \r | |
3032 | #endif\r | |
3033 | \r | |
3034 | /* List of methods for socket objects */\r | |
3035 | \r | |
3036 | static PyMethodDef sock_methods[] = {\r | |
3037 | {"accept", (PyCFunction)sock_accept, METH_NOARGS,\r | |
3038 | accept_doc},\r | |
3039 | {"bind", (PyCFunction)sock_bind, METH_O,\r | |
3040 | bind_doc},\r | |
3041 | {"close", (PyCFunction)sock_close, METH_NOARGS,\r | |
3042 | close_doc},\r | |
3043 | {"connect", (PyCFunction)sock_connect, METH_O,\r | |
3044 | connect_doc},\r | |
3045 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,\r | |
3046 | connect_ex_doc},\r | |
3047 | #ifndef NO_DUP\r | |
3048 | {"dup", (PyCFunction)sock_dup, METH_NOARGS,\r | |
3049 | dup_doc},\r | |
3050 | #endif\r | |
3051 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,\r | |
3052 | fileno_doc},\r | |
3053 | #ifdef HAVE_GETPEERNAME\r | |
3054 | {"getpeername", (PyCFunction)sock_getpeername,\r | |
3055 | METH_NOARGS, getpeername_doc},\r | |
3056 | #endif\r | |
3057 | {"getsockname", (PyCFunction)sock_getsockname,\r | |
3058 | METH_NOARGS, getsockname_doc},\r | |
3059 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,\r | |
3060 | getsockopt_doc},\r | |
3061 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL)\r | |
3062 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,\r | |
3063 | sock_ioctl_doc},\r | |
3064 | #endif\r | |
3065 | {"listen", (PyCFunction)sock_listen, METH_O,\r | |
3066 | listen_doc},\r | |
3067 | #ifndef NO_DUP\r | |
3068 | {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,\r | |
3069 | makefile_doc},\r | |
3070 | #endif\r | |
3071 | {"recv", (PyCFunction)sock_recv, METH_VARARGS,\r | |
3072 | recv_doc},\r | |
3073 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,\r | |
3074 | recv_into_doc},\r | |
3075 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,\r | |
3076 | recvfrom_doc},\r | |
3077 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,\r | |
3078 | recvfrom_into_doc},\r | |
3079 | {"send", (PyCFunction)sock_send, METH_VARARGS,\r | |
3080 | send_doc},\r | |
3081 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,\r | |
3082 | sendall_doc},\r | |
3083 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,\r | |
3084 | sendto_doc},\r | |
3085 | {"setblocking", (PyCFunction)sock_setblocking, METH_O,\r | |
3086 | setblocking_doc},\r | |
3087 | {"settimeout", (PyCFunction)sock_settimeout, METH_O,\r | |
3088 | settimeout_doc},\r | |
3089 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,\r | |
3090 | gettimeout_doc},\r | |
3091 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,\r | |
3092 | setsockopt_doc},\r | |
3093 | {"shutdown", (PyCFunction)sock_shutdown, METH_O,\r | |
3094 | shutdown_doc},\r | |
3095 | #ifdef RISCOS\r | |
3096 | {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O,\r | |
3097 | sleeptaskw_doc},\r | |
3098 | #endif\r | |
3099 | {NULL, NULL} /* sentinel */\r | |
3100 | };\r | |
3101 | \r | |
3102 | /* SockObject members */\r | |
3103 | static PyMemberDef sock_memberlist[] = {\r | |
3104 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},\r | |
3105 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},\r | |
3106 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},\r | |
3107 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},\r | |
3108 | {0},\r | |
3109 | };\r | |
3110 | \r | |
3111 | /* Deallocate a socket object in response to the last Py_DECREF().\r | |
3112 | First close the file description. */\r | |
3113 | \r | |
3114 | static void\r | |
3115 | sock_dealloc(PySocketSockObject *s)\r | |
3116 | {\r | |
3117 | if (s->sock_fd != -1)\r | |
3118 | (void) SOCKETCLOSE(s->sock_fd);\r | |
3119 | if (s->weakreflist != NULL)\r | |
3120 | PyObject_ClearWeakRefs((PyObject *)s);\r | |
3121 | Py_TYPE(s)->tp_free((PyObject *)s);\r | |
3122 | }\r | |
3123 | \r | |
3124 | \r | |
3125 | static PyObject *\r | |
3126 | sock_repr(PySocketSockObject *s)\r | |
3127 | {\r | |
3128 | char buf[512];\r | |
3129 | long sock_fd;\r | |
3130 | /* On Windows, this test is needed because SOCKET_T is unsigned */\r | |
3131 | if (s->sock_fd == INVALID_SOCKET) {\r | |
3132 | sock_fd = -1;\r | |
3133 | }\r | |
3134 | #if SIZEOF_SOCKET_T > SIZEOF_LONG\r | |
3135 | else if (s->sock_fd > LONG_MAX) {\r | |
3136 | /* this can occur on Win64, and actually there is a special\r | |
3137 | ugly printf formatter for decimal pointer length integer\r | |
3138 | printing, only bother if necessary*/\r | |
3139 | PyErr_SetString(PyExc_OverflowError,\r | |
3140 | "no printf formatter to display "\r | |
3141 | "the socket descriptor in decimal");\r | |
3142 | return NULL;\r | |
3143 | }\r | |
3144 | #endif\r | |
3145 | else\r | |
3146 | sock_fd = (long)s->sock_fd;\r | |
3147 | PyOS_snprintf(\r | |
3148 | buf, sizeof(buf),\r | |
3149 | "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",\r | |
3150 | sock_fd, s->sock_family,\r | |
3151 | s->sock_type,\r | |
3152 | s->sock_proto);\r | |
3153 | return PyString_FromString(buf);\r | |
3154 | }\r | |
3155 | \r | |
3156 | \r | |
3157 | /* Create a new, uninitialized socket object. */\r | |
3158 | \r | |
3159 | static PyObject *\r | |
3160 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r | |
3161 | {\r | |
3162 | PyObject *new;\r | |
3163 | \r | |
3164 | new = type->tp_alloc(type, 0);\r | |
3165 | if (new != NULL) {\r | |
3166 | ((PySocketSockObject *)new)->sock_fd = -1;\r | |
3167 | ((PySocketSockObject *)new)->sock_timeout = -1.0;\r | |
3168 | ((PySocketSockObject *)new)->errorhandler = &set_error;\r | |
3169 | ((PySocketSockObject *)new)->weakreflist = NULL;\r | |
3170 | }\r | |
3171 | return new;\r | |
3172 | }\r | |
3173 | \r | |
3174 | \r | |
3175 | /* Initialize a new socket object. */\r | |
3176 | \r | |
3177 | /*ARGSUSED*/\r | |
3178 | static int\r | |
3179 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)\r | |
3180 | {\r | |
3181 | PySocketSockObject *s = (PySocketSockObject *)self;\r | |
3182 | SOCKET_T fd;\r | |
3183 | int family = AF_INET, type = SOCK_STREAM, proto = 0;\r | |
3184 | static char *keywords[] = {"family", "type", "proto", 0};\r | |
3185 | \r | |
3186 | if (!PyArg_ParseTupleAndKeywords(args, kwds,\r | |
3187 | "|iii:socket", keywords,\r | |
3188 | &family, &type, &proto))\r | |
3189 | return -1;\r | |
3190 | \r | |
3191 | Py_BEGIN_ALLOW_THREADS\r | |
3192 | fd = socket(family, type, proto);\r | |
3193 | Py_END_ALLOW_THREADS\r | |
3194 | \r | |
3195 | if (fd == INVALID_SOCKET) {\r | |
3196 | set_error();\r | |
3197 | return -1;\r | |
3198 | }\r | |
3199 | init_sockobject(s, fd, family, type, proto);\r | |
3200 | \r | |
3201 | return 0;\r | |
3202 | \r | |
3203 | }\r | |
3204 | \r | |
3205 | \r | |
3206 | /* Type object for socket objects. */\r | |
3207 | \r | |
3208 | static PyTypeObject sock_type = {\r | |
3209 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */\r | |
3210 | "_socket.socket", /* tp_name */\r | |
3211 | sizeof(PySocketSockObject), /* tp_basicsize */\r | |
3212 | 0, /* tp_itemsize */\r | |
3213 | (destructor)sock_dealloc, /* tp_dealloc */\r | |
3214 | 0, /* tp_print */\r | |
3215 | 0, /* tp_getattr */\r | |
3216 | 0, /* tp_setattr */\r | |
3217 | 0, /* tp_compare */\r | |
3218 | (reprfunc)sock_repr, /* tp_repr */\r | |
3219 | 0, /* tp_as_number */\r | |
3220 | 0, /* tp_as_sequence */\r | |
3221 | 0, /* tp_as_mapping */\r | |
3222 | 0, /* tp_hash */\r | |
3223 | 0, /* tp_call */\r | |
3224 | 0, /* tp_str */\r | |
3225 | PyObject_GenericGetAttr, /* tp_getattro */\r | |
3226 | 0, /* tp_setattro */\r | |
3227 | 0, /* tp_as_buffer */\r | |
3228 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */\r | |
3229 | sock_doc, /* tp_doc */\r | |
3230 | 0, /* tp_traverse */\r | |
3231 | 0, /* tp_clear */\r | |
3232 | 0, /* tp_richcompare */\r | |
3233 | offsetof(PySocketSockObject, weakreflist), /* tp_weaklistoffset */\r | |
3234 | 0, /* tp_iter */\r | |
3235 | 0, /* tp_iternext */\r | |
3236 | sock_methods, /* tp_methods */\r | |
3237 | sock_memberlist, /* tp_members */\r | |
3238 | 0, /* tp_getset */\r | |
3239 | 0, /* tp_base */\r | |
3240 | 0, /* tp_dict */\r | |
3241 | 0, /* tp_descr_get */\r | |
3242 | 0, /* tp_descr_set */\r | |
3243 | 0, /* tp_dictoffset */\r | |
3244 | sock_initobj, /* tp_init */\r | |
3245 | PyType_GenericAlloc, /* tp_alloc */\r | |
3246 | sock_new, /* tp_new */\r | |
3247 | PyObject_Del, /* tp_free */\r | |
3248 | };\r | |
3249 | \r | |
3250 | \r | |
3251 | /* Python interface to gethostname(). */\r | |
3252 | \r | |
3253 | /*ARGSUSED*/\r | |
3254 | static PyObject *\r | |
3255 | socket_gethostname(PyObject *self, PyObject *unused)\r | |
3256 | {\r | |
3257 | char buf[1024];\r | |
3258 | int res;\r | |
3259 | Py_BEGIN_ALLOW_THREADS\r | |
3260 | res = gethostname(buf, (int) sizeof buf - 1);\r | |
3261 | Py_END_ALLOW_THREADS\r | |
3262 | if (res < 0)\r | |
3263 | return set_error();\r | |
3264 | buf[sizeof buf - 1] = '\0';\r | |
3265 | return PyString_FromString(buf);\r | |
3266 | }\r | |
3267 | \r | |
3268 | PyDoc_STRVAR(gethostname_doc,\r | |
3269 | "gethostname() -> string\n\\r | |
3270 | \n\\r | |
3271 | Return the current host name.");\r | |
3272 | \r | |
3273 | \r | |
3274 | /* Python interface to gethostbyname(name). */\r | |
3275 | \r | |
3276 | /*ARGSUSED*/\r | |
3277 | static PyObject *\r | |
3278 | socket_gethostbyname(PyObject *self, PyObject *args)\r | |
3279 | {\r | |
3280 | char *name;\r | |
3281 | sock_addr_t addrbuf;\r | |
3282 | \r | |
3283 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))\r | |
3284 | return NULL;\r | |
3285 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)\r | |
3286 | return NULL;\r | |
3287 | return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));\r | |
3288 | }\r | |
3289 | \r | |
3290 | PyDoc_STRVAR(gethostbyname_doc,\r | |
3291 | "gethostbyname(host) -> address\n\\r | |
3292 | \n\\r | |
3293 | Return the IP address (a string of the form '255.255.255.255') for a host.");\r | |
3294 | \r | |
3295 | \r | |
3296 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */\r | |
3297 | \r | |
3298 | static PyObject *\r | |
3299 | gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)\r | |
3300 | {\r | |
3301 | char **pch;\r | |
3302 | PyObject *rtn_tuple = (PyObject *)NULL;\r | |
3303 | PyObject *name_list = (PyObject *)NULL;\r | |
3304 | PyObject *addr_list = (PyObject *)NULL;\r | |
3305 | PyObject *tmp;\r | |
3306 | \r | |
3307 | if (h == NULL) {\r | |
3308 | /* Let's get real error message to return */\r | |
3309 | #ifndef RISCOS\r | |
3310 | set_herror(h_errno);\r | |
3311 | #else\r | |
3312 | PyErr_SetString(socket_error, "host not found");\r | |
3313 | #endif\r | |
3314 | return NULL;\r | |
3315 | }\r | |
3316 | \r | |
3317 | if (h->h_addrtype != af) {\r | |
3318 | /* Let's get real error message to return */\r | |
3319 | PyErr_SetString(socket_error,\r | |
3320 | (char *)strerror(EAFNOSUPPORT));\r | |
3321 | \r | |
3322 | return NULL;\r | |
3323 | }\r | |
3324 | \r | |
3325 | switch (af) {\r | |
3326 | \r | |
3327 | case AF_INET:\r | |
3328 | if (alen < sizeof(struct sockaddr_in))\r | |
3329 | return NULL;\r | |
3330 | break;\r | |
3331 | \r | |
3332 | #ifdef ENABLE_IPV6\r | |
3333 | case AF_INET6:\r | |
3334 | if (alen < sizeof(struct sockaddr_in6))\r | |
3335 | return NULL;\r | |
3336 | break;\r | |
3337 | #endif\r | |
3338 | \r | |
3339 | }\r | |
3340 | \r | |
3341 | if ((name_list = PyList_New(0)) == NULL)\r | |
3342 | goto err;\r | |
3343 | \r | |
3344 | if ((addr_list = PyList_New(0)) == NULL)\r | |
3345 | goto err;\r | |
3346 | \r | |
3347 | /* SF #1511317: h_aliases can be NULL */\r | |
3348 | if (h->h_aliases) {\r | |
3349 | for (pch = h->h_aliases; *pch != NULL; pch++) {\r | |
3350 | int status;\r | |
3351 | tmp = PyString_FromString(*pch);\r | |
3352 | if (tmp == NULL)\r | |
3353 | goto err;\r | |
3354 | \r | |
3355 | status = PyList_Append(name_list, tmp);\r | |
3356 | Py_DECREF(tmp);\r | |
3357 | \r | |
3358 | if (status)\r | |
3359 | goto err;\r | |
3360 | }\r | |
3361 | }\r | |
3362 | \r | |
3363 | for (pch = h->h_addr_list; *pch != NULL; pch++) {\r | |
3364 | int status;\r | |
3365 | \r | |
3366 | switch (af) {\r | |
3367 | \r | |
3368 | case AF_INET:\r | |
3369 | {\r | |
3370 | struct sockaddr_in sin;\r | |
3371 | memset(&sin, 0, sizeof(sin));\r | |
3372 | sin.sin_family = af;\r | |
3373 | #ifdef HAVE_SOCKADDR_SA_LEN\r | |
3374 | sin.sin_len = sizeof(sin);\r | |
3375 | #endif\r | |
3376 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));\r | |
3377 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));\r | |
3378 | \r | |
3379 | if (pch == h->h_addr_list && alen >= sizeof(sin))\r | |
3380 | memcpy((char *) addr, &sin, sizeof(sin));\r | |
3381 | break;\r | |
3382 | }\r | |
3383 | \r | |
3384 | #ifdef ENABLE_IPV6\r | |
3385 | case AF_INET6:\r | |
3386 | {\r | |
3387 | struct sockaddr_in6 sin6;\r | |
3388 | memset(&sin6, 0, sizeof(sin6));\r | |
3389 | sin6.sin6_family = af;\r | |
3390 | #ifdef HAVE_SOCKADDR_SA_LEN\r | |
3391 | sin6.sin6_len = sizeof(sin6);\r | |
3392 | #endif\r | |
3393 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));\r | |
3394 | tmp = makeipaddr((struct sockaddr *)&sin6,\r | |
3395 | sizeof(sin6));\r | |
3396 | \r | |
3397 | if (pch == h->h_addr_list && alen >= sizeof(sin6))\r | |
3398 | memcpy((char *) addr, &sin6, sizeof(sin6));\r | |
3399 | break;\r | |
3400 | }\r | |
3401 | #endif\r | |
3402 | \r | |
3403 | default: /* can't happen */\r | |
3404 | PyErr_SetString(socket_error,\r | |
3405 | "unsupported address family");\r | |
3406 | return NULL;\r | |
3407 | }\r | |
3408 | \r | |
3409 | if (tmp == NULL)\r | |
3410 | goto err;\r | |
3411 | \r | |
3412 | status = PyList_Append(addr_list, tmp);\r | |
3413 | Py_DECREF(tmp);\r | |
3414 | \r | |
3415 | if (status)\r | |
3416 | goto err;\r | |
3417 | }\r | |
3418 | \r | |
3419 | rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);\r | |
3420 | \r | |
3421 | err:\r | |
3422 | Py_XDECREF(name_list);\r | |
3423 | Py_XDECREF(addr_list);\r | |
3424 | return rtn_tuple;\r | |
3425 | }\r | |
3426 | \r | |
3427 | \r | |
3428 | /* Python interface to gethostbyname_ex(name). */\r | |
3429 | \r | |
3430 | /*ARGSUSED*/\r | |
3431 | static PyObject *\r | |
3432 | socket_gethostbyname_ex(PyObject *self, PyObject *args)\r | |
3433 | {\r | |
3434 | char *name;\r | |
3435 | struct hostent *h;\r | |
3436 | #ifdef ENABLE_IPV6\r | |
3437 | struct sockaddr_storage addr;\r | |
3438 | #else\r | |
3439 | struct sockaddr_in addr;\r | |
3440 | #endif\r | |
3441 | struct sockaddr *sa;\r | |
3442 | PyObject *ret;\r | |
3443 | #ifdef HAVE_GETHOSTBYNAME_R\r | |
3444 | struct hostent hp_allocated;\r | |
3445 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG\r | |
3446 | struct hostent_data data;\r | |
3447 | #else\r | |
3448 | char buf[16384];\r | |
3449 | int buf_len = (sizeof buf) - 1;\r | |
3450 | int errnop;\r | |
3451 | #endif\r | |
3452 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)\r | |
3453 | int result;\r | |
3454 | #endif\r | |
3455 | #endif /* HAVE_GETHOSTBYNAME_R */\r | |
3456 | \r | |
3457 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))\r | |
3458 | return NULL;\r | |
3459 | if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)\r | |
3460 | return NULL;\r | |
3461 | Py_BEGIN_ALLOW_THREADS\r | |
3462 | #ifdef HAVE_GETHOSTBYNAME_R\r | |
3463 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG)\r | |
3464 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len,\r | |
3465 | &h, &errnop);\r | |
3466 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)\r | |
3467 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);\r | |
3468 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */\r | |
3469 | memset((void *) &data, '\0', sizeof(data));\r | |
3470 | result = gethostbyname_r(name, &hp_allocated, &data);\r | |
3471 | h = (result != 0) ? NULL : &hp_allocated;\r | |
3472 | #endif\r | |
3473 | #else /* not HAVE_GETHOSTBYNAME_R */\r | |
3474 | #ifdef USE_GETHOSTBYNAME_LOCK\r | |
3475 | PyThread_acquire_lock(netdb_lock, 1);\r | |
3476 | #endif\r | |
3477 | h = gethostbyname(name);\r | |
3478 | #endif /* HAVE_GETHOSTBYNAME_R */\r | |
3479 | Py_END_ALLOW_THREADS\r | |
3480 | /* Some C libraries would require addr.__ss_family instead of\r | |
3481 | addr.ss_family.\r | |
3482 | Therefore, we cast the sockaddr_storage into sockaddr to\r | |
3483 | access sa_family. */\r | |
3484 | sa = (struct sockaddr*)&addr;\r | |
3485 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),\r | |
3486 | sa->sa_family);\r | |
3487 | #ifdef USE_GETHOSTBYNAME_LOCK\r | |
3488 | PyThread_release_lock(netdb_lock);\r | |
3489 | #endif\r | |
3490 | return ret;\r | |
3491 | }\r | |
3492 | \r | |
3493 | PyDoc_STRVAR(ghbn_ex_doc,\r | |
3494 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\\r | |
3495 | \n\\r | |
3496 | Return the true host name, a list of aliases, and a list of IP addresses,\n\\r | |
3497 | for a host. The host argument is a string giving a host name or IP number.");\r | |
3498 | \r | |
3499 | \r | |
3500 | /* Python interface to gethostbyaddr(IP). */\r | |
3501 | \r | |
3502 | /*ARGSUSED*/\r | |
3503 | static PyObject *\r | |
3504 | socket_gethostbyaddr(PyObject *self, PyObject *args)\r | |
3505 | {\r | |
3506 | #ifdef ENABLE_IPV6\r | |
3507 | struct sockaddr_storage addr;\r | |
3508 | #else\r | |
3509 | struct sockaddr_in addr;\r | |
3510 | #endif\r | |
3511 | struct sockaddr *sa = (struct sockaddr *)&addr;\r | |
3512 | char *ip_num;\r | |
3513 | struct hostent *h;\r | |
3514 | PyObject *ret;\r | |
3515 | #ifdef HAVE_GETHOSTBYNAME_R\r | |
3516 | struct hostent hp_allocated;\r | |
3517 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG\r | |
3518 | struct hostent_data data;\r | |
3519 | #else\r | |
3520 | /* glibcs up to 2.10 assume that the buf argument to\r | |
3521 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc\r | |
3522 | does not ensure. The attribute below instructs the compiler\r | |
3523 | to maintain this alignment. */\r | |
3524 | char buf[16384] Py_ALIGNED(8);\r | |
3525 | int buf_len = (sizeof buf) - 1;\r | |
3526 | int errnop;\r | |
3527 | #endif\r | |
3528 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)\r | |
3529 | int result;\r | |
3530 | #endif\r | |
3531 | #endif /* HAVE_GETHOSTBYNAME_R */\r | |
3532 | char *ap;\r | |
3533 | int al;\r | |
3534 | int af;\r | |
3535 | \r | |
3536 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))\r | |
3537 | return NULL;\r | |
3538 | af = AF_UNSPEC;\r | |
3539 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)\r | |
3540 | return NULL;\r | |
3541 | af = sa->sa_family;\r | |
3542 | ap = NULL;\r | |
3543 | switch (af) {\r | |
3544 | case AF_INET:\r | |
3545 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;\r | |
3546 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr);\r | |
3547 | break;\r | |
3548 | #ifdef ENABLE_IPV6\r | |
3549 | case AF_INET6:\r | |
3550 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;\r | |
3551 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);\r | |
3552 | break;\r | |
3553 | #endif\r | |
3554 | default:\r | |
3555 | PyErr_SetString(socket_error, "unsupported address family");\r | |
3556 | return NULL;\r | |
3557 | }\r | |
3558 | Py_BEGIN_ALLOW_THREADS\r | |
3559 | #ifdef HAVE_GETHOSTBYNAME_R\r | |
3560 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG)\r | |
3561 | result = gethostbyaddr_r(ap, al, af,\r | |
3562 | &hp_allocated, buf, buf_len,\r | |
3563 | &h, &errnop);\r | |
3564 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)\r | |
3565 | h = gethostbyaddr_r(ap, al, af,\r | |
3566 | &hp_allocated, buf, buf_len, &errnop);\r | |
3567 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */\r | |
3568 | memset((void *) &data, '\0', sizeof(data));\r | |
3569 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);\r | |
3570 | h = (result != 0) ? NULL : &hp_allocated;\r | |
3571 | #endif\r | |
3572 | #else /* not HAVE_GETHOSTBYNAME_R */\r | |
3573 | #ifdef USE_GETHOSTBYNAME_LOCK\r | |
3574 | PyThread_acquire_lock(netdb_lock, 1);\r | |
3575 | #endif\r | |
3576 | h = gethostbyaddr(ap, al, af);\r | |
3577 | #endif /* HAVE_GETHOSTBYNAME_R */\r | |
3578 | Py_END_ALLOW_THREADS\r | |
3579 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);\r | |
3580 | #ifdef USE_GETHOSTBYNAME_LOCK\r | |
3581 | PyThread_release_lock(netdb_lock);\r | |
3582 | #endif\r | |
3583 | return ret;\r | |
3584 | }\r | |
3585 | \r | |
3586 | PyDoc_STRVAR(gethostbyaddr_doc,\r | |
3587 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\\r | |
3588 | \n\\r | |
3589 | Return the true host name, a list of aliases, and a list of IP addresses,\n\\r | |
3590 | for a host. The host argument is a string giving a host name or IP number.");\r | |
3591 | \r | |
3592 | \r | |
3593 | /* Python interface to getservbyname(name).\r | |
3594 | This only returns the port number, since the other info is already\r | |
3595 | known or not useful (like the list of aliases). */\r | |
3596 | \r | |
3597 | /*ARGSUSED*/\r | |
3598 | static PyObject *\r | |
3599 | socket_getservbyname(PyObject *self, PyObject *args)\r | |
3600 | {\r | |
3601 | char *name, *proto=NULL;\r | |
3602 | struct servent *sp;\r | |
3603 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))\r | |
3604 | return NULL;\r | |
3605 | Py_BEGIN_ALLOW_THREADS\r | |
3606 | sp = getservbyname(name, proto);\r | |
3607 | Py_END_ALLOW_THREADS\r | |
3608 | if (sp == NULL) {\r | |
3609 | PyErr_SetString(socket_error, "service/proto not found");\r | |
3610 | return NULL;\r | |
3611 | }\r | |
3612 | return PyInt_FromLong((long) ntohs(sp->s_port));\r | |
3613 | }\r | |
3614 | \r | |
3615 | PyDoc_STRVAR(getservbyname_doc,\r | |
3616 | "getservbyname(servicename[, protocolname]) -> integer\n\\r | |
3617 | \n\\r | |
3618 | Return a port number from a service name and protocol name.\n\\r | |
3619 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\\r | |
3620 | otherwise any protocol will match.");\r | |
3621 | \r | |
3622 | \r | |
3623 | /* Python interface to getservbyport(port).\r | |
3624 | This only returns the service name, since the other info is already\r | |
3625 | known or not useful (like the list of aliases). */\r | |
3626 | \r | |
3627 | /*ARGSUSED*/\r | |
3628 | static PyObject *\r | |
3629 | socket_getservbyport(PyObject *self, PyObject *args)\r | |
3630 | {\r | |
3631 | int port;\r | |
3632 | char *proto=NULL;\r | |
3633 | struct servent *sp;\r | |
3634 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))\r | |
3635 | return NULL;\r | |
3636 | if (port < 0 || port > 0xffff) {\r | |
3637 | PyErr_SetString(\r | |
3638 | PyExc_OverflowError,\r | |
3639 | "getservbyport: port must be 0-65535.");\r | |
3640 | return NULL;\r | |
3641 | }\r | |
3642 | Py_BEGIN_ALLOW_THREADS\r | |
3643 | sp = getservbyport(htons((short)port), proto);\r | |
3644 | Py_END_ALLOW_THREADS\r | |
3645 | if (sp == NULL) {\r | |
3646 | PyErr_SetString(socket_error, "port/proto not found");\r | |
3647 | return NULL;\r | |
3648 | }\r | |
3649 | return PyString_FromString(sp->s_name);\r | |
3650 | }\r | |
3651 | \r | |
3652 | PyDoc_STRVAR(getservbyport_doc,\r | |
3653 | "getservbyport(port[, protocolname]) -> string\n\\r | |
3654 | \n\\r | |
3655 | Return the service name from a port number and protocol name.\n\\r | |
3656 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\\r | |
3657 | otherwise any protocol will match.");\r | |
3658 | \r | |
3659 | /* Python interface to getprotobyname(name).\r | |
3660 | This only returns the protocol number, since the other info is\r | |
3661 | already known or not useful (like the list of aliases). */\r | |
3662 | \r | |
3663 | /*ARGSUSED*/\r | |
3664 | static PyObject *\r | |
3665 | socket_getprotobyname(PyObject *self, PyObject *args)\r | |
3666 | {\r | |
3667 | char *name;\r | |
3668 | struct protoent *sp;\r | |
3669 | #ifdef __BEOS__\r | |
3670 | /* Not available in BeOS yet. - [cjh] */\r | |
3671 | PyErr_SetString(socket_error, "getprotobyname not supported");\r | |
3672 | return NULL;\r | |
3673 | #else\r | |
3674 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))\r | |
3675 | return NULL;\r | |
3676 | Py_BEGIN_ALLOW_THREADS\r | |
3677 | sp = getprotobyname(name);\r | |
3678 | Py_END_ALLOW_THREADS\r | |
3679 | if (sp == NULL) {\r | |
3680 | PyErr_SetString(socket_error, "protocol not found");\r | |
3681 | return NULL;\r | |
3682 | }\r | |
3683 | return PyInt_FromLong((long) sp->p_proto);\r | |
3684 | #endif\r | |
3685 | }\r | |
3686 | \r | |
3687 | PyDoc_STRVAR(getprotobyname_doc,\r | |
3688 | "getprotobyname(name) -> integer\n\\r | |
3689 | \n\\r | |
3690 | Return the protocol number for the named protocol. (Rarely used.)");\r | |
3691 | \r | |
3692 | \r | |
3693 | #ifdef HAVE_SOCKETPAIR\r | |
3694 | /* Create a pair of sockets using the socketpair() function.\r | |
3695 | Arguments as for socket() except the default family is AF_UNIX if\r | |
3696 | defined on the platform; otherwise, the default is AF_INET. */\r | |
3697 | \r | |
3698 | /*ARGSUSED*/\r | |
3699 | static PyObject *\r | |
3700 | socket_socketpair(PyObject *self, PyObject *args)\r | |
3701 | {\r | |
3702 | PySocketSockObject *s0 = NULL, *s1 = NULL;\r | |
3703 | SOCKET_T sv[2];\r | |
3704 | int family, type = SOCK_STREAM, proto = 0;\r | |
3705 | PyObject *res = NULL;\r | |
3706 | \r | |
3707 | #if defined(AF_UNIX)\r | |
3708 | family = AF_UNIX;\r | |
3709 | #else\r | |
3710 | family = AF_INET;\r | |
3711 | #endif\r | |
3712 | if (!PyArg_ParseTuple(args, "|iii:socketpair",\r | |
3713 | &family, &type, &proto))\r | |
3714 | return NULL;\r | |
3715 | /* Create a pair of socket fds */\r | |
3716 | if (socketpair(family, type, proto, sv) < 0)\r | |
3717 | return set_error();\r | |
3718 | s0 = new_sockobject(sv[0], family, type, proto);\r | |
3719 | if (s0 == NULL)\r | |
3720 | goto finally;\r | |
3721 | s1 = new_sockobject(sv[1], family, type, proto);\r | |
3722 | if (s1 == NULL)\r | |
3723 | goto finally;\r | |
3724 | res = PyTuple_Pack(2, s0, s1);\r | |
3725 | \r | |
3726 | finally:\r | |
3727 | if (res == NULL) {\r | |
3728 | if (s0 == NULL)\r | |
3729 | SOCKETCLOSE(sv[0]);\r | |
3730 | if (s1 == NULL)\r | |
3731 | SOCKETCLOSE(sv[1]);\r | |
3732 | }\r | |
3733 | Py_XDECREF(s0);\r | |
3734 | Py_XDECREF(s1);\r | |
3735 | return res;\r | |
3736 | }\r | |
3737 | \r | |
3738 | PyDoc_STRVAR(socketpair_doc,\r | |
3739 | "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\\r | |
3740 | \n\\r | |
3741 | Create a pair of socket objects from the sockets returned by the platform\n\\r | |
3742 | socketpair() function.\n\\r | |
3743 | The arguments are the same as for socket() except the default family is\n\\r | |
3744 | AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");\r | |
3745 | \r | |
3746 | #endif /* HAVE_SOCKETPAIR */\r | |
3747 | \r | |
3748 | \r | |
3749 | #ifndef NO_DUP\r | |
3750 | /* Create a socket object from a numeric file description.\r | |
3751 | Useful e.g. if stdin is a socket.\r | |
3752 | Additional arguments as for socket(). */\r | |
3753 | \r | |
3754 | /*ARGSUSED*/\r | |
3755 | static PyObject *\r | |
3756 | socket_fromfd(PyObject *self, PyObject *args)\r | |
3757 | {\r | |
3758 | PySocketSockObject *s;\r | |
3759 | SOCKET_T fd;\r | |
3760 | int family, type, proto = 0;\r | |
3761 | if (!PyArg_ParseTuple(args, "iii|i:fromfd",\r | |
3762 | &fd, &family, &type, &proto))\r | |
3763 | return NULL;\r | |
3764 | /* Dup the fd so it and the socket can be closed independently */\r | |
3765 | fd = dup(fd);\r | |
3766 | if (fd < 0)\r | |
3767 | return set_error();\r | |
3768 | s = new_sockobject(fd, family, type, proto);\r | |
3769 | return (PyObject *) s;\r | |
3770 | }\r | |
3771 | \r | |
3772 | PyDoc_STRVAR(fromfd_doc,\r | |
3773 | "fromfd(fd, family, type[, proto]) -> socket object\n\\r | |
3774 | \n\\r | |
3775 | Create a socket object from a duplicate of the given\n\\r | |
3776 | file descriptor.\n\\r | |
3777 | The remaining arguments are the same as for socket().");\r | |
3778 | \r | |
3779 | #endif /* NO_DUP */\r | |
3780 | \r | |
3781 | \r | |
3782 | static PyObject *\r | |
3783 | socket_ntohs(PyObject *self, PyObject *args)\r | |
3784 | {\r | |
3785 | int x1, x2;\r | |
3786 | \r | |
3787 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {\r | |
3788 | return NULL;\r | |
3789 | }\r | |
3790 | if (x1 < 0) {\r | |
3791 | PyErr_SetString(PyExc_OverflowError,\r | |
3792 | "can't convert negative number to unsigned long");\r | |
3793 | return NULL;\r | |
3794 | }\r | |
3795 | x2 = (unsigned int)ntohs((unsigned short)x1);\r | |
3796 | return PyInt_FromLong(x2);\r | |
3797 | }\r | |
3798 | \r | |
3799 | PyDoc_STRVAR(ntohs_doc,\r | |
3800 | "ntohs(integer) -> integer\n\\r | |
3801 | \n\\r | |
3802 | Convert a 16-bit integer from network to host byte order.");\r | |
3803 | \r | |
3804 | \r | |
3805 | static PyObject *\r | |
3806 | socket_ntohl(PyObject *self, PyObject *arg)\r | |
3807 | {\r | |
3808 | unsigned long x;\r | |
3809 | \r | |
3810 | if (PyInt_Check(arg)) {\r | |
3811 | x = PyInt_AS_LONG(arg);\r | |
3812 | if (x == (unsigned long) -1 && PyErr_Occurred())\r | |
3813 | return NULL;\r | |
3814 | if ((long)x < 0) {\r | |
3815 | PyErr_SetString(PyExc_OverflowError,\r | |
3816 | "can't convert negative number to unsigned long");\r | |
3817 | return NULL;\r | |
3818 | }\r | |
3819 | }\r | |
3820 | else if (PyLong_Check(arg)) {\r | |
3821 | x = PyLong_AsUnsignedLong(arg);\r | |
3822 | if (x == (unsigned long) -1 && PyErr_Occurred())\r | |
3823 | return NULL;\r | |
3824 | #if SIZEOF_LONG > 4\r | |
3825 | {\r | |
3826 | unsigned long y;\r | |
3827 | /* only want the trailing 32 bits */\r | |
3828 | y = x & 0xFFFFFFFFUL;\r | |
3829 | if (y ^ x)\r | |
3830 | return PyErr_Format(PyExc_OverflowError,\r | |
3831 | "long int larger than 32 bits");\r | |
3832 | x = y;\r | |
3833 | }\r | |
3834 | #endif\r | |
3835 | }\r | |
3836 | else\r | |
3837 | return PyErr_Format(PyExc_TypeError,\r | |
3838 | "expected int/long, %s found",\r | |
3839 | Py_TYPE(arg)->tp_name);\r | |
3840 | if (x == (unsigned long) -1 && PyErr_Occurred())\r | |
3841 | return NULL;\r | |
3842 | return PyLong_FromUnsignedLong(ntohl(x));\r | |
3843 | }\r | |
3844 | \r | |
3845 | PyDoc_STRVAR(ntohl_doc,\r | |
3846 | "ntohl(integer) -> integer\n\\r | |
3847 | \n\\r | |
3848 | Convert a 32-bit integer from network to host byte order.");\r | |
3849 | \r | |
3850 | \r | |
3851 | static PyObject *\r | |
3852 | socket_htons(PyObject *self, PyObject *args)\r | |
3853 | {\r | |
3854 | int x1, x2;\r | |
3855 | \r | |
3856 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) {\r | |
3857 | return NULL;\r | |
3858 | }\r | |
3859 | if (x1 < 0) {\r | |
3860 | PyErr_SetString(PyExc_OverflowError,\r | |
3861 | "can't convert negative number to unsigned long");\r | |
3862 | return NULL;\r | |
3863 | }\r | |
3864 | x2 = (unsigned int)htons((unsigned short)x1);\r | |
3865 | return PyInt_FromLong(x2);\r | |
3866 | }\r | |
3867 | \r | |
3868 | PyDoc_STRVAR(htons_doc,\r | |
3869 | "htons(integer) -> integer\n\\r | |
3870 | \n\\r | |
3871 | Convert a 16-bit integer from host to network byte order.");\r | |
3872 | \r | |
3873 | \r | |
3874 | static PyObject *\r | |
3875 | socket_htonl(PyObject *self, PyObject *arg)\r | |
3876 | {\r | |
3877 | unsigned long x;\r | |
3878 | \r | |
3879 | if (PyInt_Check(arg)) {\r | |
3880 | x = PyInt_AS_LONG(arg);\r | |
3881 | if (x == (unsigned long) -1 && PyErr_Occurred())\r | |
3882 | return NULL;\r | |
3883 | if ((long)x < 0) {\r | |
3884 | PyErr_SetString(PyExc_OverflowError,\r | |
3885 | "can't convert negative number to unsigned long");\r | |
3886 | return NULL;\r | |
3887 | }\r | |
3888 | }\r | |
3889 | else if (PyLong_Check(arg)) {\r | |
3890 | x = PyLong_AsUnsignedLong(arg);\r | |
3891 | if (x == (unsigned long) -1 && PyErr_Occurred())\r | |
3892 | return NULL;\r | |
3893 | #if SIZEOF_LONG > 4\r | |
3894 | {\r | |
3895 | unsigned long y;\r | |
3896 | /* only want the trailing 32 bits */\r | |
3897 | y = x & 0xFFFFFFFFUL;\r | |
3898 | if (y ^ x)\r | |
3899 | return PyErr_Format(PyExc_OverflowError,\r | |
3900 | "long int larger than 32 bits");\r | |
3901 | x = y;\r | |
3902 | }\r | |
3903 | #endif\r | |
3904 | }\r | |
3905 | else\r | |
3906 | return PyErr_Format(PyExc_TypeError,\r | |
3907 | "expected int/long, %s found",\r | |
3908 | Py_TYPE(arg)->tp_name);\r | |
3909 | return PyLong_FromUnsignedLong(htonl((unsigned long)x));\r | |
3910 | }\r | |
3911 | \r | |
3912 | PyDoc_STRVAR(htonl_doc,\r | |
3913 | "htonl(integer) -> integer\n\\r | |
3914 | \n\\r | |
3915 | Convert a 32-bit integer from host to network byte order.");\r | |
3916 | \r | |
3917 | /* socket.inet_aton() and socket.inet_ntoa() functions. */\r | |
3918 | \r | |
3919 | PyDoc_STRVAR(inet_aton_doc,\r | |
3920 | "inet_aton(string) -> packed 32-bit IP representation\n\\r | |
3921 | \n\\r | |
3922 | Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\\r | |
3923 | binary format used in low-level network functions.");\r | |
3924 | \r | |
3925 | static PyObject*\r | |
3926 | socket_inet_aton(PyObject *self, PyObject *args)\r | |
3927 | {\r | |
3928 | #ifndef INADDR_NONE\r | |
3929 | #define INADDR_NONE (-1)\r | |
3930 | #endif\r | |
3931 | #ifdef HAVE_INET_ATON\r | |
3932 | struct in_addr buf;\r | |
3933 | #endif\r | |
3934 | \r | |
3935 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)\r | |
3936 | #if (SIZEOF_INT != 4)\r | |
3937 | #error "Not sure if in_addr_t exists and int is not 32-bits."\r | |
3938 | #endif\r | |
3939 | /* Have to use inet_addr() instead */\r | |
3940 | unsigned int packed_addr;\r | |
3941 | #endif\r | |
3942 | char *ip_addr;\r | |
3943 | \r | |
3944 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))\r | |
3945 | return NULL;\r | |
3946 | \r | |
3947 | \r | |
3948 | #ifdef HAVE_INET_ATON\r | |
3949 | \r | |
3950 | #ifdef USE_INET_ATON_WEAKLINK\r | |
3951 | if (inet_aton != NULL) {\r | |
3952 | #endif\r | |
3953 | if (inet_aton(ip_addr, &buf))\r | |
3954 | return PyString_FromStringAndSize((char *)(&buf),\r | |
3955 | sizeof(buf));\r | |
3956 | \r | |
3957 | PyErr_SetString(socket_error,\r | |
3958 | "illegal IP address string passed to inet_aton");\r | |
3959 | return NULL;\r | |
3960 | \r | |
3961 | #ifdef USE_INET_ATON_WEAKLINK\r | |
3962 | } else {\r | |
3963 | #endif\r | |
3964 | \r | |
3965 | #endif\r | |
3966 | \r | |
3967 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)\r | |
3968 | \r | |
3969 | /* special-case this address as inet_addr might return INADDR_NONE\r | |
3970 | * for this */\r | |
3971 | if (strcmp(ip_addr, "255.255.255.255") == 0) {\r | |
3972 | packed_addr = 0xFFFFFFFF;\r | |
3973 | } else {\r | |
3974 | \r | |
3975 | packed_addr = inet_addr(ip_addr);\r | |
3976 | \r | |
3977 | if (packed_addr == INADDR_NONE) { /* invalid address */\r | |
3978 | PyErr_SetString(socket_error,\r | |
3979 | "illegal IP address string passed to inet_aton");\r | |
3980 | return NULL;\r | |
3981 | }\r | |
3982 | }\r | |
3983 | return PyString_FromStringAndSize((char *) &packed_addr,\r | |
3984 | sizeof(packed_addr));\r | |
3985 | \r | |
3986 | #ifdef USE_INET_ATON_WEAKLINK\r | |
3987 | }\r | |
3988 | #endif\r | |
3989 | \r | |
3990 | #endif\r | |
3991 | }\r | |
3992 | \r | |
3993 | PyDoc_STRVAR(inet_ntoa_doc,\r | |
3994 | "inet_ntoa(packed_ip) -> ip_address_string\n\\r | |
3995 | \n\\r | |
3996 | Convert an IP address from 32-bit packed binary format to string format");\r | |
3997 | \r | |
3998 | static PyObject*\r | |
3999 | socket_inet_ntoa(PyObject *self, PyObject *args)\r | |
4000 | {\r | |
4001 | char *packed_str;\r | |
4002 | int addr_len;\r | |
4003 | struct in_addr packed_addr;\r | |
4004 | \r | |
4005 | if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {\r | |
4006 | return NULL;\r | |
4007 | }\r | |
4008 | \r | |
4009 | if (addr_len != sizeof(packed_addr)) {\r | |
4010 | PyErr_SetString(socket_error,\r | |
4011 | "packed IP wrong length for inet_ntoa");\r | |
4012 | return NULL;\r | |
4013 | }\r | |
4014 | \r | |
4015 | memcpy(&packed_addr, packed_str, addr_len);\r | |
4016 | \r | |
4017 | return PyString_FromString(inet_ntoa(packed_addr));\r | |
4018 | }\r | |
4019 | \r | |
4020 | #ifdef HAVE_INET_PTON\r | |
4021 | \r | |
4022 | PyDoc_STRVAR(inet_pton_doc,\r | |
4023 | "inet_pton(af, ip) -> packed IP address string\n\\r | |
4024 | \n\\r | |
4025 | Convert an IP address from string format to a packed string suitable\n\\r | |
4026 | for use with low-level network functions.");\r | |
4027 | \r | |
4028 | static PyObject *\r | |
4029 | socket_inet_pton(PyObject *self, PyObject *args)\r | |
4030 | {\r | |
4031 | int af;\r | |
4032 | char* ip;\r | |
4033 | int retval;\r | |
4034 | #ifdef ENABLE_IPV6\r | |
4035 | char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];\r | |
4036 | #else\r | |
4037 | char packed[sizeof(struct in_addr)];\r | |
4038 | #endif\r | |
4039 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {\r | |
4040 | return NULL;\r | |
4041 | }\r | |
4042 | \r | |
4043 | #if !defined(ENABLE_IPV6) && defined(AF_INET6)\r | |
4044 | if(af == AF_INET6) {\r | |
4045 | PyErr_SetString(socket_error,\r | |
4046 | "can't use AF_INET6, IPv6 is disabled");\r | |
4047 | return NULL;\r | |
4048 | }\r | |
4049 | #endif\r | |
4050 | \r | |
4051 | retval = inet_pton(af, ip, packed);\r | |
4052 | if (retval < 0) {\r | |
4053 | PyErr_SetFromErrno(socket_error);\r | |
4054 | return NULL;\r | |
4055 | } else if (retval == 0) {\r | |
4056 | PyErr_SetString(socket_error,\r | |
4057 | "illegal IP address string passed to inet_pton");\r | |
4058 | return NULL;\r | |
4059 | } else if (af == AF_INET) {\r | |
4060 | return PyString_FromStringAndSize(packed,\r | |
4061 | sizeof(struct in_addr));\r | |
4062 | #ifdef ENABLE_IPV6\r | |
4063 | } else if (af == AF_INET6) {\r | |
4064 | return PyString_FromStringAndSize(packed,\r | |
4065 | sizeof(struct in6_addr));\r | |
4066 | #endif\r | |
4067 | } else {\r | |
4068 | PyErr_SetString(socket_error, "unknown address family");\r | |
4069 | return NULL;\r | |
4070 | }\r | |
4071 | }\r | |
4072 | \r | |
4073 | PyDoc_STRVAR(inet_ntop_doc,\r | |
4074 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\\r | |
4075 | \n\\r | |
4076 | Convert a packed IP address of the given family to string format.");\r | |
4077 | \r | |
4078 | static PyObject *\r | |
4079 | socket_inet_ntop(PyObject *self, PyObject *args)\r | |
4080 | {\r | |
4081 | int af;\r | |
4082 | char* packed;\r | |
4083 | int len;\r | |
4084 | const char* retval;\r | |
4085 | #ifdef ENABLE_IPV6\r | |
4086 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];\r | |
4087 | #else\r | |
4088 | char ip[INET_ADDRSTRLEN + 1];\r | |
4089 | #endif\r | |
4090 | \r | |
4091 | /* Guarantee NUL-termination for PyString_FromString() below */\r | |
4092 | memset((void *) &ip[0], '\0', sizeof(ip));\r | |
4093 | \r | |
4094 | if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) {\r | |
4095 | return NULL;\r | |
4096 | }\r | |
4097 | \r | |
4098 | if (af == AF_INET) {\r | |
4099 | if (len != sizeof(struct in_addr)) {\r | |
4100 | PyErr_SetString(PyExc_ValueError,\r | |
4101 | "invalid length of packed IP address string");\r | |
4102 | return NULL;\r | |
4103 | }\r | |
4104 | #ifdef ENABLE_IPV6\r | |
4105 | } else if (af == AF_INET6) {\r | |
4106 | if (len != sizeof(struct in6_addr)) {\r | |
4107 | PyErr_SetString(PyExc_ValueError,\r | |
4108 | "invalid length of packed IP address string");\r | |
4109 | return NULL;\r | |
4110 | }\r | |
4111 | #endif\r | |
4112 | } else {\r | |
4113 | PyErr_Format(PyExc_ValueError,\r | |
4114 | "unknown address family %d", af);\r | |
4115 | return NULL;\r | |
4116 | }\r | |
4117 | \r | |
4118 | retval = inet_ntop(af, packed, ip, sizeof(ip));\r | |
4119 | if (!retval) {\r | |
4120 | PyErr_SetFromErrno(socket_error);\r | |
4121 | return NULL;\r | |
4122 | } else {\r | |
4123 | return PyString_FromString(retval);\r | |
4124 | }\r | |
4125 | \r | |
4126 | /* NOTREACHED */\r | |
4127 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");\r | |
4128 | return NULL;\r | |
4129 | }\r | |
4130 | \r | |
4131 | #endif /* HAVE_INET_PTON */\r | |
4132 | \r | |
4133 | /* Python interface to getaddrinfo(host, port). */\r | |
4134 | \r | |
4135 | /*ARGSUSED*/\r | |
4136 | static PyObject *\r | |
4137 | socket_getaddrinfo(PyObject *self, PyObject *args)\r | |
4138 | {\r | |
4139 | struct addrinfo hints, *res;\r | |
4140 | struct addrinfo *res0 = NULL;\r | |
4141 | PyObject *hobj = NULL;\r | |
4142 | PyObject *pobj = (PyObject *)NULL;\r | |
4143 | char pbuf[30];\r | |
4144 | char *hptr, *pptr;\r | |
4145 | int family, socktype, protocol, flags;\r | |
4146 | int error;\r | |
4147 | PyObject *all = (PyObject *)NULL;\r | |
4148 | PyObject *single = (PyObject *)NULL;\r | |
4149 | PyObject *idna = NULL;\r | |
4150 | \r | |
4151 | family = socktype = protocol = flags = 0;\r | |
4152 | family = AF_UNSPEC;\r | |
4153 | if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",\r | |
4154 | &hobj, &pobj, &family, &socktype,\r | |
4155 | &protocol, &flags)) {\r | |
4156 | return NULL;\r | |
4157 | }\r | |
4158 | if (hobj == Py_None) {\r | |
4159 | hptr = NULL;\r | |
4160 | } else if (PyUnicode_Check(hobj)) {\r | |
4161 | idna = PyObject_CallMethod(hobj, "encode", "s", "idna");\r | |
4162 | if (!idna)\r | |
4163 | return NULL;\r | |
4164 | hptr = PyString_AsString(idna);\r | |
4165 | } else if (PyString_Check(hobj)) {\r | |
4166 | hptr = PyString_AsString(hobj);\r | |
4167 | } else {\r | |
4168 | PyErr_SetString(PyExc_TypeError,\r | |
4169 | "getaddrinfo() argument 1 must be string or None");\r | |
4170 | return NULL;\r | |
4171 | }\r | |
4172 | if (PyInt_Check(pobj) || PyLong_Check(pobj)) {\r | |
4173 | long value = PyLong_AsLong(pobj);\r | |
4174 | if (value == -1 && PyErr_Occurred())\r | |
4175 | return NULL;\r | |
4176 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);\r | |
4177 | pptr = pbuf;\r | |
4178 | } else if (PyString_Check(pobj)) {\r | |
4179 | pptr = PyString_AsString(pobj);\r | |
4180 | } else if (pobj == Py_None) {\r | |
4181 | pptr = (char *)NULL;\r | |
4182 | } else {\r | |
4183 | PyErr_SetString(socket_error,\r | |
4184 | "getaddrinfo() argument 2 must be integer or string");\r | |
4185 | goto err;\r | |
4186 | }\r | |
4187 | #if defined(__APPLE__) && defined(AI_NUMERICSERV)\r | |
4188 | if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) {\r | |
4189 | /* On OSX upto at least OSX 10.8 getaddrinfo crashes\r | |
4190 | * if AI_NUMERICSERV is set and the servname is NULL or "0".\r | |
4191 | * This workaround avoids a segfault in libsystem.\r | |
4192 | */\r | |
4193 | pptr = "00";\r | |
4194 | }\r | |
4195 | #endif\r | |
4196 | memset(&hints, 0, sizeof(hints));\r | |
4197 | hints.ai_family = family;\r | |
4198 | hints.ai_socktype = socktype;\r | |
4199 | hints.ai_protocol = protocol;\r | |
4200 | hints.ai_flags = flags;\r | |
4201 | Py_BEGIN_ALLOW_THREADS\r | |
4202 | ACQUIRE_GETADDRINFO_LOCK\r | |
4203 | error = getaddrinfo(hptr, pptr, &hints, &res0);\r | |
4204 | Py_END_ALLOW_THREADS\r | |
4205 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */\r | |
4206 | if (error) {\r | |
4207 | set_gaierror(error);\r | |
4208 | goto err;\r | |
4209 | }\r | |
4210 | \r | |
4211 | all = PyList_New(0);\r | |
4212 | if (all == NULL)\r | |
4213 | goto err;\r | |
4214 | for (res = res0; res; res = res->ai_next) {\r | |
4215 | PyObject *addr =\r | |
4216 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);\r | |
4217 | if (addr == NULL)\r | |
4218 | goto err;\r | |
4219 | single = Py_BuildValue("iiisO", res->ai_family,\r | |
4220 | res->ai_socktype, res->ai_protocol,\r | |
4221 | res->ai_canonname ? res->ai_canonname : "",\r | |
4222 | addr);\r | |
4223 | Py_DECREF(addr);\r | |
4224 | if (single == NULL)\r | |
4225 | goto err;\r | |
4226 | \r | |
4227 | if (PyList_Append(all, single))\r | |
4228 | goto err;\r | |
4229 | Py_XDECREF(single);\r | |
4230 | }\r | |
4231 | Py_XDECREF(idna);\r | |
4232 | if (res0)\r | |
4233 | freeaddrinfo(res0);\r | |
4234 | return all;\r | |
4235 | err:\r | |
4236 | Py_XDECREF(single);\r | |
4237 | Py_XDECREF(all);\r | |
4238 | Py_XDECREF(idna);\r | |
4239 | if (res0)\r | |
4240 | freeaddrinfo(res0);\r | |
4241 | return (PyObject *)NULL;\r | |
4242 | }\r | |
4243 | \r | |
4244 | PyDoc_STRVAR(getaddrinfo_doc,\r | |
4245 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\\r | |
4246 | -> list of (family, socktype, proto, canonname, sockaddr)\n\\r | |
4247 | \n\\r | |
4248 | Resolve host and port into addrinfo struct.");\r | |
4249 | \r | |
4250 | /* Python interface to getnameinfo(sa, flags). */\r | |
4251 | \r | |
4252 | /*ARGSUSED*/\r | |
4253 | static PyObject *\r | |
4254 | socket_getnameinfo(PyObject *self, PyObject *args)\r | |
4255 | {\r | |
4256 | PyObject *sa = (PyObject *)NULL;\r | |
4257 | int flags;\r | |
4258 | char *hostp;\r | |
4259 | int port;\r | |
4260 | unsigned int flowinfo, scope_id;\r | |
4261 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];\r | |
4262 | struct addrinfo hints, *res = NULL;\r | |
4263 | int error;\r | |
4264 | PyObject *ret = (PyObject *)NULL;\r | |
4265 | \r | |
4266 | flags = flowinfo = scope_id = 0;\r | |
4267 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))\r | |
4268 | return NULL;\r | |
4269 | if (!PyTuple_Check(sa)) {\r | |
4270 | PyErr_SetString(PyExc_TypeError,\r | |
4271 | "getnameinfo() argument 1 must be a tuple");\r | |
4272 | return NULL;\r | |
4273 | }\r | |
4274 | if (!PyArg_ParseTuple(sa, "si|II",\r | |
4275 | &hostp, &port, &flowinfo, &scope_id))\r | |
4276 | return NULL;\r | |
4277 | if (flowinfo > 0xfffff) {\r | |
4278 | PyErr_SetString(PyExc_OverflowError,\r | |
4279 | "getsockaddrarg: flowinfo must be 0-1048575.");\r | |
4280 | return NULL;\r | |
4281 | }\r | |
4282 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);\r | |
4283 | memset(&hints, 0, sizeof(hints));\r | |
4284 | hints.ai_family = AF_UNSPEC;\r | |
4285 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */\r | |
4286 | Py_BEGIN_ALLOW_THREADS\r | |
4287 | ACQUIRE_GETADDRINFO_LOCK\r | |
4288 | error = getaddrinfo(hostp, pbuf, &hints, &res);\r | |
4289 | Py_END_ALLOW_THREADS\r | |
4290 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */\r | |
4291 | if (error) {\r | |
4292 | set_gaierror(error);\r | |
4293 | goto fail;\r | |
4294 | }\r | |
4295 | if (res->ai_next) {\r | |
4296 | PyErr_SetString(socket_error,\r | |
4297 | "sockaddr resolved to multiple addresses");\r | |
4298 | goto fail;\r | |
4299 | }\r | |
4300 | switch (res->ai_family) {\r | |
4301 | case AF_INET:\r | |
4302 | {\r | |
4303 | if (PyTuple_GET_SIZE(sa) != 2) {\r | |
4304 | PyErr_SetString(socket_error,\r | |
4305 | "IPv4 sockaddr must be 2 tuple");\r | |
4306 | goto fail;\r | |
4307 | }\r | |
4308 | break;\r | |
4309 | }\r | |
4310 | #ifdef ENABLE_IPV6\r | |
4311 | case AF_INET6:\r | |
4312 | {\r | |
4313 | struct sockaddr_in6 *sin6;\r | |
4314 | sin6 = (struct sockaddr_in6 *)res->ai_addr;\r | |
4315 | sin6->sin6_flowinfo = htonl(flowinfo);\r | |
4316 | sin6->sin6_scope_id = scope_id;\r | |
4317 | break;\r | |
4318 | }\r | |
4319 | #endif\r | |
4320 | }\r | |
4321 | error = getnameinfo(res->ai_addr, res->ai_addrlen,\r | |
4322 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);\r | |
4323 | if (error) {\r | |
4324 | set_gaierror(error);\r | |
4325 | goto fail;\r | |
4326 | }\r | |
4327 | ret = Py_BuildValue("ss", hbuf, pbuf);\r | |
4328 | \r | |
4329 | fail:\r | |
4330 | if (res)\r | |
4331 | freeaddrinfo(res);\r | |
4332 | return ret;\r | |
4333 | }\r | |
4334 | \r | |
4335 | PyDoc_STRVAR(getnameinfo_doc,\r | |
4336 | "getnameinfo(sockaddr, flags) --> (host, port)\n\\r | |
4337 | \n\\r | |
4338 | Get host and port for a sockaddr.");\r | |
4339 | \r | |
4340 | \r | |
4341 | /* Python API to getting and setting the default timeout value. */\r | |
4342 | \r | |
4343 | static PyObject *\r | |
4344 | socket_getdefaulttimeout(PyObject *self)\r | |
4345 | {\r | |
4346 | if (defaulttimeout < 0.0) {\r | |
4347 | Py_INCREF(Py_None);\r | |
4348 | return Py_None;\r | |
4349 | }\r | |
4350 | else\r | |
4351 | return PyFloat_FromDouble(defaulttimeout);\r | |
4352 | }\r | |
4353 | \r | |
4354 | PyDoc_STRVAR(getdefaulttimeout_doc,\r | |
4355 | "getdefaulttimeout() -> timeout\n\\r | |
4356 | \n\\r | |
4357 | Returns the default timeout in seconds (float) for new socket objects.\n\\r | |
4358 | A value of None indicates that new socket objects have no timeout.\n\\r | |
4359 | When the socket module is first imported, the default is None.");\r | |
4360 | \r | |
4361 | static PyObject *\r | |
4362 | socket_setdefaulttimeout(PyObject *self, PyObject *arg)\r | |
4363 | {\r | |
4364 | double timeout;\r | |
4365 | \r | |
4366 | if (arg == Py_None)\r | |
4367 | timeout = -1.0;\r | |
4368 | else {\r | |
4369 | timeout = PyFloat_AsDouble(arg);\r | |
4370 | if (timeout < 0.0) {\r | |
4371 | if (!PyErr_Occurred())\r | |
4372 | PyErr_SetString(PyExc_ValueError,\r | |
4373 | "Timeout value out of range");\r | |
4374 | return NULL;\r | |
4375 | }\r | |
4376 | }\r | |
4377 | \r | |
4378 | defaulttimeout = timeout;\r | |
4379 | \r | |
4380 | Py_INCREF(Py_None);\r | |
4381 | return Py_None;\r | |
4382 | }\r | |
4383 | \r | |
4384 | PyDoc_STRVAR(setdefaulttimeout_doc,\r | |
4385 | "setdefaulttimeout(timeout)\n\\r | |
4386 | \n\\r | |
4387 | Set the default timeout in seconds (float) for new socket objects.\n\\r | |
4388 | A value of None indicates that new socket objects have no timeout.\n\\r | |
4389 | When the socket module is first imported, the default is None.");\r | |
4390 | \r | |
4391 | \r | |
4392 | /* List of functions exported by this module. */\r | |
4393 | \r | |
4394 | static PyMethodDef socket_methods[] = {\r | |
4395 | {"gethostbyname", socket_gethostbyname,\r | |
4396 | METH_VARARGS, gethostbyname_doc},\r | |
4397 | {"gethostbyname_ex", socket_gethostbyname_ex,\r | |
4398 | METH_VARARGS, ghbn_ex_doc},\r | |
4399 | {"gethostbyaddr", socket_gethostbyaddr,\r | |
4400 | METH_VARARGS, gethostbyaddr_doc},\r | |
4401 | {"gethostname", socket_gethostname,\r | |
4402 | METH_NOARGS, gethostname_doc},\r | |
4403 | {"getservbyname", socket_getservbyname,\r | |
4404 | METH_VARARGS, getservbyname_doc},\r | |
4405 | {"getservbyport", socket_getservbyport,\r | |
4406 | METH_VARARGS, getservbyport_doc},\r | |
4407 | {"getprotobyname", socket_getprotobyname,\r | |
4408 | METH_VARARGS, getprotobyname_doc},\r | |
4409 | #ifndef NO_DUP\r | |
4410 | {"fromfd", socket_fromfd,\r | |
4411 | METH_VARARGS, fromfd_doc},\r | |
4412 | #endif\r | |
4413 | #ifdef HAVE_SOCKETPAIR\r | |
4414 | {"socketpair", socket_socketpair,\r | |
4415 | METH_VARARGS, socketpair_doc},\r | |
4416 | #endif\r | |
4417 | {"ntohs", socket_ntohs,\r | |
4418 | METH_VARARGS, ntohs_doc},\r | |
4419 | {"ntohl", socket_ntohl,\r | |
4420 | METH_O, ntohl_doc},\r | |
4421 | {"htons", socket_htons,\r | |
4422 | METH_VARARGS, htons_doc},\r | |
4423 | {"htonl", socket_htonl,\r | |
4424 | METH_O, htonl_doc},\r | |
4425 | {"inet_aton", socket_inet_aton,\r | |
4426 | METH_VARARGS, inet_aton_doc},\r | |
4427 | {"inet_ntoa", socket_inet_ntoa,\r | |
4428 | METH_VARARGS, inet_ntoa_doc},\r | |
4429 | #ifdef HAVE_INET_PTON\r | |
4430 | {"inet_pton", socket_inet_pton,\r | |
4431 | METH_VARARGS, inet_pton_doc},\r | |
4432 | {"inet_ntop", socket_inet_ntop,\r | |
4433 | METH_VARARGS, inet_ntop_doc},\r | |
4434 | #endif\r | |
4435 | {"getaddrinfo", socket_getaddrinfo,\r | |
4436 | METH_VARARGS, getaddrinfo_doc},\r | |
4437 | {"getnameinfo", socket_getnameinfo,\r | |
4438 | METH_VARARGS, getnameinfo_doc},\r | |
4439 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,\r | |
4440 | METH_NOARGS, getdefaulttimeout_doc},\r | |
4441 | {"setdefaulttimeout", socket_setdefaulttimeout,\r | |
4442 | METH_O, setdefaulttimeout_doc},\r | |
4443 | {NULL, NULL} /* Sentinel */\r | |
4444 | };\r | |
4445 | \r | |
4446 | \r | |
4447 | #ifdef RISCOS\r | |
4448 | #define OS_INIT_DEFINED\r | |
4449 | \r | |
4450 | static int\r | |
4451 | os_init(void)\r | |
4452 | {\r | |
4453 | _kernel_swi_regs r;\r | |
4454 | \r | |
4455 | r.r[0] = 0;\r | |
4456 | _kernel_swi(0x43380, &r, &r);\r | |
4457 | taskwindow = r.r[0];\r | |
4458 | \r | |
4459 | return 1;\r | |
4460 | }\r | |
4461 | \r | |
4462 | #endif /* RISCOS */\r | |
4463 | \r | |
4464 | \r | |
4465 | #ifdef MS_WINDOWS\r | |
4466 | #define OS_INIT_DEFINED\r | |
4467 | \r | |
4468 | /* Additional initialization and cleanup for Windows */\r | |
4469 | \r | |
4470 | static void\r | |
4471 | os_cleanup(void)\r | |
4472 | {\r | |
4473 | WSACleanup();\r | |
4474 | }\r | |
4475 | \r | |
4476 | static int\r | |
4477 | os_init(void)\r | |
4478 | {\r | |
4479 | WSADATA WSAData;\r | |
4480 | int ret;\r | |
4481 | char buf[100];\r | |
4482 | ret = WSAStartup(0x0101, &WSAData);\r | |
4483 | switch (ret) {\r | |
4484 | case 0: /* No error */\r | |
4485 | Py_AtExit(os_cleanup);\r | |
4486 | return 1; /* Success */\r | |
4487 | case WSASYSNOTREADY:\r | |
4488 | PyErr_SetString(PyExc_ImportError,\r | |
4489 | "WSAStartup failed: network not ready");\r | |
4490 | break;\r | |
4491 | case WSAVERNOTSUPPORTED:\r | |
4492 | case WSAEINVAL:\r | |
4493 | PyErr_SetString(\r | |
4494 | PyExc_ImportError,\r | |
4495 | "WSAStartup failed: requested version not supported");\r | |
4496 | break;\r | |
4497 | default:\r | |
4498 | PyOS_snprintf(buf, sizeof(buf),\r | |
4499 | "WSAStartup failed: error code %d", ret);\r | |
4500 | PyErr_SetString(PyExc_ImportError, buf);\r | |
4501 | break;\r | |
4502 | }\r | |
4503 | return 0; /* Failure */\r | |
4504 | }\r | |
4505 | \r | |
4506 | #endif /* MS_WINDOWS */\r | |
4507 | \r | |
4508 | \r | |
4509 | #ifdef PYOS_OS2\r | |
4510 | #define OS_INIT_DEFINED\r | |
4511 | \r | |
4512 | /* Additional initialization for OS/2 */\r | |
4513 | \r | |
4514 | static int\r | |
4515 | os_init(void)\r | |
4516 | {\r | |
4517 | #ifndef PYCC_GCC\r | |
4518 | char reason[64];\r | |
4519 | int rc = sock_init();\r | |
4520 | \r | |
4521 | if (rc == 0) {\r | |
4522 | return 1; /* Success */\r | |
4523 | }\r | |
4524 | \r | |
4525 | PyOS_snprintf(reason, sizeof(reason),\r | |
4526 | "OS/2 TCP/IP Error# %d", sock_errno());\r | |
4527 | PyErr_SetString(PyExc_ImportError, reason);\r | |
4528 | \r | |
4529 | return 0; /* Failure */\r | |
4530 | #else\r | |
4531 | /* No need to initialize sockets with GCC/EMX */\r | |
4532 | return 1; /* Success */\r | |
4533 | #endif\r | |
4534 | }\r | |
4535 | \r | |
4536 | #endif /* PYOS_OS2 */\r | |
4537 | \r | |
4538 | \r | |
4539 | #ifndef OS_INIT_DEFINED\r | |
4540 | static int\r | |
4541 | os_init(void)\r | |
4542 | {\r | |
4543 | return 1; /* Success */\r | |
4544 | }\r | |
4545 | #endif\r | |
4546 | \r | |
4547 | \r | |
4548 | /* C API table - always add new things to the end for binary\r | |
4549 | compatibility. */\r | |
4550 | static\r | |
4551 | PySocketModule_APIObject PySocketModuleAPI =\r | |
4552 | {\r | |
4553 | &sock_type,\r | |
4554 | NULL\r | |
4555 | };\r | |
4556 | \r | |
4557 | \r | |
4558 | /* Initialize the _socket module.\r | |
4559 | \r | |
4560 | This module is actually called "_socket", and there's a wrapper\r | |
4561 | "socket.py" which implements some additional functionality. On some\r | |
4562 | platforms (e.g. Windows and OS/2), socket.py also implements a\r | |
4563 | wrapper for the socket type that provides missing functionality such\r | |
4564 | as makefile(), dup() and fromfd(). The import of "_socket" may fail\r | |
4565 | with an ImportError exception if os-specific initialization fails.\r | |
4566 | On Windows, this does WINSOCK initialization. When WINSOCK is\r | |
4567 | initialized successfully, a call to WSACleanup() is scheduled to be\r | |
4568 | made at exit time.\r | |
4569 | */\r | |
4570 | \r | |
4571 | PyDoc_STRVAR(socket_doc,\r | |
4572 | "Implementation module for socket operations.\n\\r | |
4573 | \n\\r | |
4574 | See the socket module for documentation.");\r | |
4575 | \r | |
4576 | PyMODINIT_FUNC\r | |
4577 | init_socket(void)\r | |
4578 | {\r | |
4579 | PyObject *m, *has_ipv6;\r | |
4580 | \r | |
4581 | if (!os_init())\r | |
4582 | return;\r | |
4583 | \r | |
4584 | Py_TYPE(&sock_type) = &PyType_Type;\r | |
4585 | m = Py_InitModule3(PySocket_MODULE_NAME,\r | |
4586 | socket_methods,\r | |
4587 | socket_doc);\r | |
4588 | if (m == NULL)\r | |
4589 | return;\r | |
4590 | \r | |
4591 | socket_error = PyErr_NewException("socket.error",\r | |
4592 | PyExc_IOError, NULL);\r | |
4593 | if (socket_error == NULL)\r | |
4594 | return;\r | |
4595 | PySocketModuleAPI.error = socket_error;\r | |
4596 | Py_INCREF(socket_error);\r | |
4597 | PyModule_AddObject(m, "error", socket_error);\r | |
4598 | socket_herror = PyErr_NewException("socket.herror",\r | |
4599 | socket_error, NULL);\r | |
4600 | if (socket_herror == NULL)\r | |
4601 | return;\r | |
4602 | Py_INCREF(socket_herror);\r | |
4603 | PyModule_AddObject(m, "herror", socket_herror);\r | |
4604 | socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,\r | |
4605 | NULL);\r | |
4606 | if (socket_gaierror == NULL)\r | |
4607 | return;\r | |
4608 | Py_INCREF(socket_gaierror);\r | |
4609 | PyModule_AddObject(m, "gaierror", socket_gaierror);\r | |
4610 | socket_timeout = PyErr_NewException("socket.timeout",\r | |
4611 | socket_error, NULL);\r | |
4612 | if (socket_timeout == NULL)\r | |
4613 | return;\r | |
4614 | Py_INCREF(socket_timeout);\r | |
4615 | PyModule_AddObject(m, "timeout", socket_timeout);\r | |
4616 | Py_INCREF((PyObject *)&sock_type);\r | |
4617 | if (PyModule_AddObject(m, "SocketType",\r | |
4618 | (PyObject *)&sock_type) != 0)\r | |
4619 | return;\r | |
4620 | Py_INCREF((PyObject *)&sock_type);\r | |
4621 | if (PyModule_AddObject(m, "socket",\r | |
4622 | (PyObject *)&sock_type) != 0)\r | |
4623 | return;\r | |
4624 | \r | |
4625 | #ifdef ENABLE_IPV6\r | |
4626 | has_ipv6 = Py_True;\r | |
4627 | #else\r | |
4628 | has_ipv6 = Py_False;\r | |
4629 | #endif\r | |
4630 | Py_INCREF(has_ipv6);\r | |
4631 | PyModule_AddObject(m, "has_ipv6", has_ipv6);\r | |
4632 | \r | |
4633 | /* Export C API */\r | |
4634 | if (PyModule_AddObject(m, PySocket_CAPI_NAME,\r | |
4635 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)\r | |
4636 | ) != 0)\r | |
4637 | return;\r | |
4638 | \r | |
4639 | /* Address families (we only support AF_INET and AF_UNIX) */\r | |
4640 | #ifdef AF_UNSPEC\r | |
4641 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);\r | |
4642 | #endif\r | |
4643 | PyModule_AddIntConstant(m, "AF_INET", AF_INET);\r | |
4644 | #ifdef AF_INET6\r | |
4645 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);\r | |
4646 | #endif /* AF_INET6 */\r | |
4647 | #if defined(AF_UNIX)\r | |
4648 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);\r | |
4649 | #endif /* AF_UNIX */\r | |
4650 | #ifdef AF_AX25\r | |
4651 | /* Amateur Radio AX.25 */\r | |
4652 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);\r | |
4653 | #endif\r | |
4654 | #ifdef AF_IPX\r | |
4655 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */\r | |
4656 | #endif\r | |
4657 | #ifdef AF_APPLETALK\r | |
4658 | /* Appletalk DDP */\r | |
4659 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);\r | |
4660 | #endif\r | |
4661 | #ifdef AF_NETROM\r | |
4662 | /* Amateur radio NetROM */\r | |
4663 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);\r | |
4664 | #endif\r | |
4665 | #ifdef AF_BRIDGE\r | |
4666 | /* Multiprotocol bridge */\r | |
4667 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);\r | |
4668 | #endif\r | |
4669 | #ifdef AF_ATMPVC\r | |
4670 | /* ATM PVCs */\r | |
4671 | PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);\r | |
4672 | #endif\r | |
4673 | #ifdef AF_AAL5\r | |
4674 | /* Reserved for Werner's ATM */\r | |
4675 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);\r | |
4676 | #endif\r | |
4677 | #ifdef AF_X25\r | |
4678 | /* Reserved for X.25 project */\r | |
4679 | PyModule_AddIntConstant(m, "AF_X25", AF_X25);\r | |
4680 | #endif\r | |
4681 | #ifdef AF_INET6\r | |
4682 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */\r | |
4683 | #endif\r | |
4684 | #ifdef AF_ROSE\r | |
4685 | /* Amateur Radio X.25 PLP */\r | |
4686 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);\r | |
4687 | #endif\r | |
4688 | #ifdef AF_DECnet\r | |
4689 | /* Reserved for DECnet project */\r | |
4690 | PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);\r | |
4691 | #endif\r | |
4692 | #ifdef AF_NETBEUI\r | |
4693 | /* Reserved for 802.2LLC project */\r | |
4694 | PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);\r | |
4695 | #endif\r | |
4696 | #ifdef AF_SECURITY\r | |
4697 | /* Security callback pseudo AF */\r | |
4698 | PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);\r | |
4699 | #endif\r | |
4700 | #ifdef AF_KEY\r | |
4701 | /* PF_KEY key management API */\r | |
4702 | PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);\r | |
4703 | #endif\r | |
4704 | #ifdef AF_NETLINK\r | |
4705 | /* */\r | |
4706 | PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);\r | |
4707 | PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);\r | |
4708 | #ifdef NETLINK_SKIP\r | |
4709 | PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);\r | |
4710 | #endif\r | |
4711 | #ifdef NETLINK_W1\r | |
4712 | PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);\r | |
4713 | #endif\r | |
4714 | PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);\r | |
4715 | PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);\r | |
4716 | #ifdef NETLINK_TCPDIAG\r | |
4717 | PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);\r | |
4718 | #endif\r | |
4719 | #ifdef NETLINK_NFLOG\r | |
4720 | PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);\r | |
4721 | #endif\r | |
4722 | #ifdef NETLINK_XFRM\r | |
4723 | PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);\r | |
4724 | #endif\r | |
4725 | #ifdef NETLINK_ARPD\r | |
4726 | PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);\r | |
4727 | #endif\r | |
4728 | #ifdef NETLINK_ROUTE6\r | |
4729 | PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);\r | |
4730 | #endif\r | |
4731 | PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);\r | |
4732 | #ifdef NETLINK_DNRTMSG\r | |
4733 | PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);\r | |
4734 | #endif\r | |
4735 | #ifdef NETLINK_TAPBASE\r | |
4736 | PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);\r | |
4737 | #endif\r | |
4738 | #endif /* AF_NETLINK */\r | |
4739 | #ifdef AF_ROUTE\r | |
4740 | /* Alias to emulate 4.4BSD */\r | |
4741 | PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);\r | |
4742 | #endif\r | |
4743 | #ifdef AF_ASH\r | |
4744 | /* Ash */\r | |
4745 | PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);\r | |
4746 | #endif\r | |
4747 | #ifdef AF_ECONET\r | |
4748 | /* Acorn Econet */\r | |
4749 | PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);\r | |
4750 | #endif\r | |
4751 | #ifdef AF_ATMSVC\r | |
4752 | /* ATM SVCs */\r | |
4753 | PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);\r | |
4754 | #endif\r | |
4755 | #ifdef AF_SNA\r | |
4756 | /* Linux SNA Project (nutters!) */\r | |
4757 | PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);\r | |
4758 | #endif\r | |
4759 | #ifdef AF_IRDA\r | |
4760 | /* IRDA sockets */\r | |
4761 | PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);\r | |
4762 | #endif\r | |
4763 | #ifdef AF_PPPOX\r | |
4764 | /* PPPoX sockets */\r | |
4765 | PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);\r | |
4766 | #endif\r | |
4767 | #ifdef AF_WANPIPE\r | |
4768 | /* Wanpipe API Sockets */\r | |
4769 | PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);\r | |
4770 | #endif\r | |
4771 | #ifdef AF_LLC\r | |
4772 | /* Linux LLC */\r | |
4773 | PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);\r | |
4774 | #endif\r | |
4775 | \r | |
4776 | #ifdef USE_BLUETOOTH\r | |
4777 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);\r | |
4778 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);\r | |
4779 | PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI);\r | |
4780 | PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI);\r | |
4781 | #if !defined(__NetBSD__) && !defined(__DragonFly__)\r | |
4782 | PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER);\r | |
4783 | #endif\r | |
4784 | #if !defined(__FreeBSD__)\r | |
4785 | #if !defined(__NetBSD__) && !defined(__DragonFly__)\r | |
4786 | PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP);\r | |
4787 | #endif\r | |
4788 | PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR);\r | |
4789 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);\r | |
4790 | #endif\r | |
4791 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);\r | |
4792 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");\r | |
4793 | PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");\r | |
4794 | #endif\r | |
4795 | \r | |
4796 | #ifdef AF_PACKET\r | |
4797 | PyModule_AddIntMacro(m, AF_PACKET);\r | |
4798 | #endif\r | |
4799 | #ifdef PF_PACKET\r | |
4800 | PyModule_AddIntMacro(m, PF_PACKET);\r | |
4801 | #endif\r | |
4802 | #ifdef PACKET_HOST\r | |
4803 | PyModule_AddIntMacro(m, PACKET_HOST);\r | |
4804 | #endif\r | |
4805 | #ifdef PACKET_BROADCAST\r | |
4806 | PyModule_AddIntMacro(m, PACKET_BROADCAST);\r | |
4807 | #endif\r | |
4808 | #ifdef PACKET_MULTICAST\r | |
4809 | PyModule_AddIntMacro(m, PACKET_MULTICAST);\r | |
4810 | #endif\r | |
4811 | #ifdef PACKET_OTHERHOST\r | |
4812 | PyModule_AddIntMacro(m, PACKET_OTHERHOST);\r | |
4813 | #endif\r | |
4814 | #ifdef PACKET_OUTGOING\r | |
4815 | PyModule_AddIntMacro(m, PACKET_OUTGOING);\r | |
4816 | #endif\r | |
4817 | #ifdef PACKET_LOOPBACK\r | |
4818 | PyModule_AddIntMacro(m, PACKET_LOOPBACK);\r | |
4819 | #endif\r | |
4820 | #ifdef PACKET_FASTROUTE\r | |
4821 | PyModule_AddIntMacro(m, PACKET_FASTROUTE);\r | |
4822 | #endif\r | |
4823 | \r | |
4824 | #ifdef HAVE_LINUX_TIPC_H\r | |
4825 | PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC);\r | |
4826 | \r | |
4827 | /* for addresses */\r | |
4828 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ);\r | |
4829 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME);\r | |
4830 | PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID);\r | |
4831 | \r | |
4832 | PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE);\r | |
4833 | PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE);\r | |
4834 | PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE);\r | |
4835 | \r | |
4836 | /* for setsockopt() */\r | |
4837 | PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC);\r | |
4838 | PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE);\r | |
4839 | PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE);\r | |
4840 | PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE",\r | |
4841 | TIPC_DEST_DROPPABLE);\r | |
4842 | PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT);\r | |
4843 | \r | |
4844 | PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE",\r | |
4845 | TIPC_LOW_IMPORTANCE);\r | |
4846 | PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE",\r | |
4847 | TIPC_MEDIUM_IMPORTANCE);\r | |
4848 | PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE",\r | |
4849 | TIPC_HIGH_IMPORTANCE);\r | |
4850 | PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE",\r | |
4851 | TIPC_CRITICAL_IMPORTANCE);\r | |
4852 | \r | |
4853 | /* for subscriptions */\r | |
4854 | PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS);\r | |
4855 | PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE);\r | |
4856 | #ifdef TIPC_SUB_CANCEL\r | |
4857 | /* doesn't seem to be available everywhere */\r | |
4858 | PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL);\r | |
4859 | #endif\r | |
4860 | PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER);\r | |
4861 | PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED);\r | |
4862 | PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN);\r | |
4863 | PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT);\r | |
4864 | PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV);\r | |
4865 | PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV);\r | |
4866 | #endif\r | |
4867 | \r | |
4868 | /* Socket types */\r | |
4869 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);\r | |
4870 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);\r | |
4871 | #ifndef __BEOS__\r | |
4872 | /* We have incomplete socket support. */\r | |
4873 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);\r | |
4874 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);\r | |
4875 | #if defined(SOCK_RDM)\r | |
4876 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);\r | |
4877 | #endif\r | |
4878 | #endif\r | |
4879 | \r | |
4880 | #ifdef SO_DEBUG\r | |
4881 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);\r | |
4882 | #endif\r | |
4883 | #ifdef SO_ACCEPTCONN\r | |
4884 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);\r | |
4885 | #endif\r | |
4886 | #ifdef SO_REUSEADDR\r | |
4887 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);\r | |
4888 | #endif\r | |
4889 | #ifdef SO_EXCLUSIVEADDRUSE\r | |
4890 | PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);\r | |
4891 | #endif\r | |
4892 | \r | |
4893 | #ifdef SO_KEEPALIVE\r | |
4894 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);\r | |
4895 | #endif\r | |
4896 | #ifdef SO_DONTROUTE\r | |
4897 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);\r | |
4898 | #endif\r | |
4899 | #ifdef SO_BROADCAST\r | |
4900 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);\r | |
4901 | #endif\r | |
4902 | #ifdef SO_USELOOPBACK\r | |
4903 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);\r | |
4904 | #endif\r | |
4905 | #ifdef SO_LINGER\r | |
4906 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);\r | |
4907 | #endif\r | |
4908 | #ifdef SO_OOBINLINE\r | |
4909 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);\r | |
4910 | #endif\r | |
4911 | #ifdef SO_REUSEPORT\r | |
4912 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);\r | |
4913 | #endif\r | |
4914 | #ifdef SO_SNDBUF\r | |
4915 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);\r | |
4916 | #endif\r | |
4917 | #ifdef SO_RCVBUF\r | |
4918 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);\r | |
4919 | #endif\r | |
4920 | #ifdef SO_SNDLOWAT\r | |
4921 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);\r | |
4922 | #endif\r | |
4923 | #ifdef SO_RCVLOWAT\r | |
4924 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);\r | |
4925 | #endif\r | |
4926 | #ifdef SO_SNDTIMEO\r | |
4927 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);\r | |
4928 | #endif\r | |
4929 | #ifdef SO_RCVTIMEO\r | |
4930 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);\r | |
4931 | #endif\r | |
4932 | #ifdef SO_ERROR\r | |
4933 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);\r | |
4934 | #endif\r | |
4935 | #ifdef SO_TYPE\r | |
4936 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);\r | |
4937 | #endif\r | |
4938 | #ifdef SO_SETFIB\r | |
4939 | PyModule_AddIntConstant(m, "SO_SETFIB", SO_SETFIB);\r | |
4940 | #endif\r | |
4941 | \r | |
4942 | /* Maximum number of connections for "listen" */\r | |
4943 | #ifdef SOMAXCONN\r | |
4944 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);\r | |
4945 | #else\r | |
4946 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */\r | |
4947 | #endif\r | |
4948 | \r | |
4949 | /* Flags for send, recv */\r | |
4950 | #ifdef MSG_OOB\r | |
4951 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);\r | |
4952 | #endif\r | |
4953 | #ifdef MSG_PEEK\r | |
4954 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);\r | |
4955 | #endif\r | |
4956 | #ifdef MSG_DONTROUTE\r | |
4957 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);\r | |
4958 | #endif\r | |
4959 | #ifdef MSG_DONTWAIT\r | |
4960 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);\r | |
4961 | #endif\r | |
4962 | #ifdef MSG_EOR\r | |
4963 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);\r | |
4964 | #endif\r | |
4965 | #ifdef MSG_TRUNC\r | |
4966 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);\r | |
4967 | #endif\r | |
4968 | #ifdef MSG_CTRUNC\r | |
4969 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);\r | |
4970 | #endif\r | |
4971 | #ifdef MSG_WAITALL\r | |
4972 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);\r | |
4973 | #endif\r | |
4974 | #ifdef MSG_BTAG\r | |
4975 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);\r | |
4976 | #endif\r | |
4977 | #ifdef MSG_ETAG\r | |
4978 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);\r | |
4979 | #endif\r | |
4980 | \r | |
4981 | /* Protocol level and numbers, usable for [gs]etsockopt */\r | |
4982 | #ifdef SOL_SOCKET\r | |
4983 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);\r | |
4984 | #endif\r | |
4985 | #ifdef SOL_IP\r | |
4986 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);\r | |
4987 | #else\r | |
4988 | PyModule_AddIntConstant(m, "SOL_IP", 0);\r | |
4989 | #endif\r | |
4990 | #ifdef SOL_IPX\r | |
4991 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);\r | |
4992 | #endif\r | |
4993 | #ifdef SOL_AX25\r | |
4994 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);\r | |
4995 | #endif\r | |
4996 | #ifdef SOL_ATALK\r | |
4997 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);\r | |
4998 | #endif\r | |
4999 | #ifdef SOL_NETROM\r | |
5000 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);\r | |
5001 | #endif\r | |
5002 | #ifdef SOL_ROSE\r | |
5003 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);\r | |
5004 | #endif\r | |
5005 | #ifdef SOL_TCP\r | |
5006 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);\r | |
5007 | #else\r | |
5008 | PyModule_AddIntConstant(m, "SOL_TCP", 6);\r | |
5009 | #endif\r | |
5010 | #ifdef SOL_UDP\r | |
5011 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);\r | |
5012 | #else\r | |
5013 | PyModule_AddIntConstant(m, "SOL_UDP", 17);\r | |
5014 | #endif\r | |
5015 | #ifdef IPPROTO_IP\r | |
5016 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);\r | |
5017 | #else\r | |
5018 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0);\r | |
5019 | #endif\r | |
5020 | #ifdef IPPROTO_HOPOPTS\r | |
5021 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);\r | |
5022 | #endif\r | |
5023 | #ifdef IPPROTO_ICMP\r | |
5024 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);\r | |
5025 | #else\r | |
5026 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);\r | |
5027 | #endif\r | |
5028 | #ifdef IPPROTO_IGMP\r | |
5029 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);\r | |
5030 | #endif\r | |
5031 | #ifdef IPPROTO_GGP\r | |
5032 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);\r | |
5033 | #endif\r | |
5034 | #ifdef IPPROTO_IPV4\r | |
5035 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);\r | |
5036 | #endif\r | |
5037 | #ifdef IPPROTO_IPV6\r | |
5038 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);\r | |
5039 | #endif\r | |
5040 | #ifdef IPPROTO_IPIP\r | |
5041 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);\r | |
5042 | #endif\r | |
5043 | #ifdef IPPROTO_TCP\r | |
5044 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);\r | |
5045 | #else\r | |
5046 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);\r | |
5047 | #endif\r | |
5048 | #ifdef IPPROTO_EGP\r | |
5049 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);\r | |
5050 | #endif\r | |
5051 | #ifdef IPPROTO_PUP\r | |
5052 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);\r | |
5053 | #endif\r | |
5054 | #ifdef IPPROTO_UDP\r | |
5055 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);\r | |
5056 | #else\r | |
5057 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);\r | |
5058 | #endif\r | |
5059 | #ifdef IPPROTO_IDP\r | |
5060 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);\r | |
5061 | #endif\r | |
5062 | #ifdef IPPROTO_HELLO\r | |
5063 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);\r | |
5064 | #endif\r | |
5065 | #ifdef IPPROTO_ND\r | |
5066 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);\r | |
5067 | #endif\r | |
5068 | #ifdef IPPROTO_TP\r | |
5069 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);\r | |
5070 | #endif\r | |
5071 | #ifdef IPPROTO_IPV6\r | |
5072 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);\r | |
5073 | #endif\r | |
5074 | #ifdef IPPROTO_ROUTING\r | |
5075 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);\r | |
5076 | #endif\r | |
5077 | #ifdef IPPROTO_FRAGMENT\r | |
5078 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);\r | |
5079 | #endif\r | |
5080 | #ifdef IPPROTO_RSVP\r | |
5081 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);\r | |
5082 | #endif\r | |
5083 | #ifdef IPPROTO_GRE\r | |
5084 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);\r | |
5085 | #endif\r | |
5086 | #ifdef IPPROTO_ESP\r | |
5087 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);\r | |
5088 | #endif\r | |
5089 | #ifdef IPPROTO_AH\r | |
5090 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);\r | |
5091 | #endif\r | |
5092 | #ifdef IPPROTO_MOBILE\r | |
5093 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);\r | |
5094 | #endif\r | |
5095 | #ifdef IPPROTO_ICMPV6\r | |
5096 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);\r | |
5097 | #endif\r | |
5098 | #ifdef IPPROTO_NONE\r | |
5099 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);\r | |
5100 | #endif\r | |
5101 | #ifdef IPPROTO_DSTOPTS\r | |
5102 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);\r | |
5103 | #endif\r | |
5104 | #ifdef IPPROTO_XTP\r | |
5105 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);\r | |
5106 | #endif\r | |
5107 | #ifdef IPPROTO_EON\r | |
5108 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);\r | |
5109 | #endif\r | |
5110 | #ifdef IPPROTO_PIM\r | |
5111 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);\r | |
5112 | #endif\r | |
5113 | #ifdef IPPROTO_IPCOMP\r | |
5114 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);\r | |
5115 | #endif\r | |
5116 | #ifdef IPPROTO_VRRP\r | |
5117 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);\r | |
5118 | #endif\r | |
5119 | #ifdef IPPROTO_BIP\r | |
5120 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);\r | |
5121 | #endif\r | |
5122 | /**/\r | |
5123 | #ifdef IPPROTO_RAW\r | |
5124 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);\r | |
5125 | #else\r | |
5126 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);\r | |
5127 | #endif\r | |
5128 | #ifdef IPPROTO_MAX\r | |
5129 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);\r | |
5130 | #endif\r | |
5131 | \r | |
5132 | /* Some port configuration */\r | |
5133 | #ifdef IPPORT_RESERVED\r | |
5134 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);\r | |
5135 | #else\r | |
5136 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);\r | |
5137 | #endif\r | |
5138 | #ifdef IPPORT_USERRESERVED\r | |
5139 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);\r | |
5140 | #else\r | |
5141 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);\r | |
5142 | #endif\r | |
5143 | \r | |
5144 | /* Some reserved IP v.4 addresses */\r | |
5145 | #ifdef INADDR_ANY\r | |
5146 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);\r | |
5147 | #else\r | |
5148 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);\r | |
5149 | #endif\r | |
5150 | #ifdef INADDR_BROADCAST\r | |
5151 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);\r | |
5152 | #else\r | |
5153 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);\r | |
5154 | #endif\r | |
5155 | #ifdef INADDR_LOOPBACK\r | |
5156 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);\r | |
5157 | #else\r | |
5158 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);\r | |
5159 | #endif\r | |
5160 | #ifdef INADDR_UNSPEC_GROUP\r | |
5161 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);\r | |
5162 | #else\r | |
5163 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);\r | |
5164 | #endif\r | |
5165 | #ifdef INADDR_ALLHOSTS_GROUP\r | |
5166 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",\r | |
5167 | INADDR_ALLHOSTS_GROUP);\r | |
5168 | #else\r | |
5169 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);\r | |
5170 | #endif\r | |
5171 | #ifdef INADDR_MAX_LOCAL_GROUP\r | |
5172 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",\r | |
5173 | INADDR_MAX_LOCAL_GROUP);\r | |
5174 | #else\r | |
5175 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);\r | |
5176 | #endif\r | |
5177 | #ifdef INADDR_NONE\r | |
5178 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);\r | |
5179 | #else\r | |
5180 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);\r | |
5181 | #endif\r | |
5182 | \r | |
5183 | /* IPv4 [gs]etsockopt options */\r | |
5184 | #ifdef IP_OPTIONS\r | |
5185 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);\r | |
5186 | #endif\r | |
5187 | #ifdef IP_HDRINCL\r | |
5188 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);\r | |
5189 | #endif\r | |
5190 | #ifdef IP_TOS\r | |
5191 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);\r | |
5192 | #endif\r | |
5193 | #ifdef IP_TTL\r | |
5194 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);\r | |
5195 | #endif\r | |
5196 | #ifdef IP_RECVOPTS\r | |
5197 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);\r | |
5198 | #endif\r | |
5199 | #ifdef IP_RECVRETOPTS\r | |
5200 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);\r | |
5201 | #endif\r | |
5202 | #ifdef IP_RECVDSTADDR\r | |
5203 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);\r | |
5204 | #endif\r | |
5205 | #ifdef IP_RETOPTS\r | |
5206 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);\r | |
5207 | #endif\r | |
5208 | #ifdef IP_MULTICAST_IF\r | |
5209 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);\r | |
5210 | #endif\r | |
5211 | #ifdef IP_MULTICAST_TTL\r | |
5212 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);\r | |
5213 | #endif\r | |
5214 | #ifdef IP_MULTICAST_LOOP\r | |
5215 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);\r | |
5216 | #endif\r | |
5217 | #ifdef IP_ADD_MEMBERSHIP\r | |
5218 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);\r | |
5219 | #endif\r | |
5220 | #ifdef IP_DROP_MEMBERSHIP\r | |
5221 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);\r | |
5222 | #endif\r | |
5223 | #ifdef IP_DEFAULT_MULTICAST_TTL\r | |
5224 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",\r | |
5225 | IP_DEFAULT_MULTICAST_TTL);\r | |
5226 | #endif\r | |
5227 | #ifdef IP_DEFAULT_MULTICAST_LOOP\r | |
5228 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",\r | |
5229 | IP_DEFAULT_MULTICAST_LOOP);\r | |
5230 | #endif\r | |
5231 | #ifdef IP_MAX_MEMBERSHIPS\r | |
5232 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);\r | |
5233 | #endif\r | |
5234 | \r | |
5235 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */\r | |
5236 | #ifdef IPV6_JOIN_GROUP\r | |
5237 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);\r | |
5238 | #endif\r | |
5239 | #ifdef IPV6_LEAVE_GROUP\r | |
5240 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);\r | |
5241 | #endif\r | |
5242 | #ifdef IPV6_MULTICAST_HOPS\r | |
5243 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);\r | |
5244 | #endif\r | |
5245 | #ifdef IPV6_MULTICAST_IF\r | |
5246 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);\r | |
5247 | #endif\r | |
5248 | #ifdef IPV6_MULTICAST_LOOP\r | |
5249 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);\r | |
5250 | #endif\r | |
5251 | #ifdef IPV6_UNICAST_HOPS\r | |
5252 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);\r | |
5253 | #endif\r | |
5254 | /* Additional IPV6 socket options, defined in RFC 3493 */\r | |
5255 | #ifdef IPV6_V6ONLY\r | |
5256 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);\r | |
5257 | #endif\r | |
5258 | /* Advanced IPV6 socket options, from RFC 3542 */\r | |
5259 | #ifdef IPV6_CHECKSUM\r | |
5260 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);\r | |
5261 | #endif\r | |
5262 | #ifdef IPV6_DONTFRAG\r | |
5263 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);\r | |
5264 | #endif\r | |
5265 | #ifdef IPV6_DSTOPTS\r | |
5266 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);\r | |
5267 | #endif\r | |
5268 | #ifdef IPV6_HOPLIMIT\r | |
5269 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);\r | |
5270 | #endif\r | |
5271 | #ifdef IPV6_HOPOPTS\r | |
5272 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);\r | |
5273 | #endif\r | |
5274 | #ifdef IPV6_NEXTHOP\r | |
5275 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);\r | |
5276 | #endif\r | |
5277 | #ifdef IPV6_PATHMTU\r | |
5278 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);\r | |
5279 | #endif\r | |
5280 | #ifdef IPV6_PKTINFO\r | |
5281 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);\r | |
5282 | #endif\r | |
5283 | #ifdef IPV6_RECVDSTOPTS\r | |
5284 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);\r | |
5285 | #endif\r | |
5286 | #ifdef IPV6_RECVHOPLIMIT\r | |
5287 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);\r | |
5288 | #endif\r | |
5289 | #ifdef IPV6_RECVHOPOPTS\r | |
5290 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);\r | |
5291 | #endif\r | |
5292 | #ifdef IPV6_RECVPKTINFO\r | |
5293 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);\r | |
5294 | #endif\r | |
5295 | #ifdef IPV6_RECVRTHDR\r | |
5296 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);\r | |
5297 | #endif\r | |
5298 | #ifdef IPV6_RECVTCLASS\r | |
5299 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);\r | |
5300 | #endif\r | |
5301 | #ifdef IPV6_RTHDR\r | |
5302 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);\r | |
5303 | #endif\r | |
5304 | #ifdef IPV6_RTHDRDSTOPTS\r | |
5305 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);\r | |
5306 | #endif\r | |
5307 | #ifdef IPV6_RTHDR_TYPE_0\r | |
5308 | PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);\r | |
5309 | #endif\r | |
5310 | #ifdef IPV6_RECVPATHMTU\r | |
5311 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);\r | |
5312 | #endif\r | |
5313 | #ifdef IPV6_TCLASS\r | |
5314 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);\r | |
5315 | #endif\r | |
5316 | #ifdef IPV6_USE_MIN_MTU\r | |
5317 | PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);\r | |
5318 | #endif\r | |
5319 | \r | |
5320 | /* TCP options */\r | |
5321 | #ifdef TCP_NODELAY\r | |
5322 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);\r | |
5323 | #endif\r | |
5324 | #ifdef TCP_MAXSEG\r | |
5325 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);\r | |
5326 | #endif\r | |
5327 | #ifdef TCP_CORK\r | |
5328 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);\r | |
5329 | #endif\r | |
5330 | #ifdef TCP_KEEPIDLE\r | |
5331 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);\r | |
5332 | #endif\r | |
5333 | #ifdef TCP_KEEPINTVL\r | |
5334 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);\r | |
5335 | #endif\r | |
5336 | #ifdef TCP_KEEPCNT\r | |
5337 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);\r | |
5338 | #endif\r | |
5339 | #ifdef TCP_SYNCNT\r | |
5340 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);\r | |
5341 | #endif\r | |
5342 | #ifdef TCP_LINGER2\r | |
5343 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);\r | |
5344 | #endif\r | |
5345 | #ifdef TCP_DEFER_ACCEPT\r | |
5346 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);\r | |
5347 | #endif\r | |
5348 | #ifdef TCP_WINDOW_CLAMP\r | |
5349 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);\r | |
5350 | #endif\r | |
5351 | #ifdef TCP_INFO\r | |
5352 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);\r | |
5353 | #endif\r | |
5354 | #ifdef TCP_QUICKACK\r | |
5355 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);\r | |
5356 | #endif\r | |
5357 | \r | |
5358 | \r | |
5359 | /* IPX options */\r | |
5360 | #ifdef IPX_TYPE\r | |
5361 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);\r | |
5362 | #endif\r | |
5363 | \r | |
5364 | /* get{addr,name}info parameters */\r | |
5365 | #ifdef EAI_ADDRFAMILY\r | |
5366 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);\r | |
5367 | #endif\r | |
5368 | #ifdef EAI_AGAIN\r | |
5369 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);\r | |
5370 | #endif\r | |
5371 | #ifdef EAI_BADFLAGS\r | |
5372 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);\r | |
5373 | #endif\r | |
5374 | #ifdef EAI_FAIL\r | |
5375 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);\r | |
5376 | #endif\r | |
5377 | #ifdef EAI_FAMILY\r | |
5378 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);\r | |
5379 | #endif\r | |
5380 | #ifdef EAI_MEMORY\r | |
5381 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);\r | |
5382 | #endif\r | |
5383 | #ifdef EAI_NODATA\r | |
5384 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);\r | |
5385 | #endif\r | |
5386 | #ifdef EAI_NONAME\r | |
5387 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);\r | |
5388 | #endif\r | |
5389 | #ifdef EAI_OVERFLOW\r | |
5390 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);\r | |
5391 | #endif\r | |
5392 | #ifdef EAI_SERVICE\r | |
5393 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);\r | |
5394 | #endif\r | |
5395 | #ifdef EAI_SOCKTYPE\r | |
5396 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);\r | |
5397 | #endif\r | |
5398 | #ifdef EAI_SYSTEM\r | |
5399 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);\r | |
5400 | #endif\r | |
5401 | #ifdef EAI_BADHINTS\r | |
5402 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);\r | |
5403 | #endif\r | |
5404 | #ifdef EAI_PROTOCOL\r | |
5405 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);\r | |
5406 | #endif\r | |
5407 | #ifdef EAI_MAX\r | |
5408 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);\r | |
5409 | #endif\r | |
5410 | #ifdef AI_PASSIVE\r | |
5411 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);\r | |
5412 | #endif\r | |
5413 | #ifdef AI_CANONNAME\r | |
5414 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);\r | |
5415 | #endif\r | |
5416 | #ifdef AI_NUMERICHOST\r | |
5417 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);\r | |
5418 | #endif\r | |
5419 | #ifdef AI_NUMERICSERV\r | |
5420 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);\r | |
5421 | #endif\r | |
5422 | #ifdef AI_MASK\r | |
5423 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);\r | |
5424 | #endif\r | |
5425 | #ifdef AI_ALL\r | |
5426 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);\r | |
5427 | #endif\r | |
5428 | #ifdef AI_V4MAPPED_CFG\r | |
5429 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);\r | |
5430 | #endif\r | |
5431 | #ifdef AI_ADDRCONFIG\r | |
5432 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);\r | |
5433 | #endif\r | |
5434 | #ifdef AI_V4MAPPED\r | |
5435 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);\r | |
5436 | #endif\r | |
5437 | #ifdef AI_DEFAULT\r | |
5438 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);\r | |
5439 | #endif\r | |
5440 | #ifdef NI_MAXHOST\r | |
5441 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);\r | |
5442 | #endif\r | |
5443 | #ifdef NI_MAXSERV\r | |
5444 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);\r | |
5445 | #endif\r | |
5446 | #ifdef NI_NOFQDN\r | |
5447 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);\r | |
5448 | #endif\r | |
5449 | #ifdef NI_NUMERICHOST\r | |
5450 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);\r | |
5451 | #endif\r | |
5452 | #ifdef NI_NAMEREQD\r | |
5453 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);\r | |
5454 | #endif\r | |
5455 | #ifdef NI_NUMERICSERV\r | |
5456 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);\r | |
5457 | #endif\r | |
5458 | #ifdef NI_DGRAM\r | |
5459 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);\r | |
5460 | #endif\r | |
5461 | \r | |
5462 | /* shutdown() parameters */\r | |
5463 | #ifdef SHUT_RD\r | |
5464 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);\r | |
5465 | #elif defined(SD_RECEIVE)\r | |
5466 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);\r | |
5467 | #else\r | |
5468 | PyModule_AddIntConstant(m, "SHUT_RD", 0);\r | |
5469 | #endif\r | |
5470 | #ifdef SHUT_WR\r | |
5471 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);\r | |
5472 | #elif defined(SD_SEND)\r | |
5473 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);\r | |
5474 | #else\r | |
5475 | PyModule_AddIntConstant(m, "SHUT_WR", 1);\r | |
5476 | #endif\r | |
5477 | #ifdef SHUT_RDWR\r | |
5478 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);\r | |
5479 | #elif defined(SD_BOTH)\r | |
5480 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);\r | |
5481 | #else\r | |
5482 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2);\r | |
5483 | #endif\r | |
5484 | \r | |
5485 | #ifdef SIO_RCVALL\r | |
5486 | {\r | |
5487 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS};\r | |
5488 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"};\r | |
5489 | int i;\r | |
5490 | for(i = 0; i<sizeof(codes)/sizeof(*codes); ++i) {\r | |
5491 | PyObject *tmp;\r | |
5492 | tmp = PyLong_FromUnsignedLong(codes[i]);\r | |
5493 | if (tmp == NULL)\r | |
5494 | return;\r | |
5495 | PyModule_AddObject(m, names[i], tmp);\r | |
5496 | }\r | |
5497 | }\r | |
5498 | PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF);\r | |
5499 | PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON);\r | |
5500 | PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY);\r | |
5501 | #ifdef RCVALL_IPLEVEL\r | |
5502 | PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL);\r | |
5503 | #endif\r | |
5504 | #ifdef RCVALL_MAX\r | |
5505 | PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX);\r | |
5506 | #endif\r | |
5507 | #endif /* _MSTCPIP_ */\r | |
5508 | \r | |
5509 | /* Initialize gethostbyname lock */\r | |
5510 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)\r | |
5511 | netdb_lock = PyThread_allocate_lock();\r | |
5512 | #endif\r | |
5513 | }\r | |
5514 | \r | |
5515 | \r | |
5516 | #ifndef HAVE_INET_PTON\r | |
5517 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)\r | |
5518 | \r | |
5519 | /* Simplistic emulation code for inet_pton that only works for IPv4 */\r | |
5520 | /* These are not exposed because they do not set errno properly */\r | |
5521 | \r | |
5522 | int\r | |
5523 | inet_pton(int af, const char *src, void *dst)\r | |
5524 | {\r | |
5525 | if (af == AF_INET) {\r | |
5526 | #if (SIZEOF_INT != 4)\r | |
5527 | #error "Not sure if in_addr_t exists and int is not 32-bits."\r | |
5528 | #endif\r | |
5529 | unsigned int packed_addr;\r | |
5530 | packed_addr = inet_addr(src);\r | |
5531 | if (packed_addr == INADDR_NONE)\r | |
5532 | return 0;\r | |
5533 | memcpy(dst, &packed_addr, 4);\r | |
5534 | return 1;\r | |
5535 | }\r | |
5536 | /* Should set errno to EAFNOSUPPORT */\r | |
5537 | return -1;\r | |
5538 | }\r | |
5539 | \r | |
5540 | const char *\r | |
5541 | inet_ntop(int af, const void *src, char *dst, socklen_t size)\r | |
5542 | {\r | |
5543 | if (af == AF_INET) {\r | |
5544 | struct in_addr packed_addr;\r | |
5545 | if (size < 16)\r | |
5546 | /* Should set errno to ENOSPC. */\r | |
5547 | return NULL;\r | |
5548 | memcpy(&packed_addr, src, sizeof(packed_addr));\r | |
5549 | return strncpy(dst, inet_ntoa(packed_addr), size);\r | |
5550 | }\r | |
5551 | /* Should set errno to EAFNOSUPPORT */\r | |
5552 | return NULL;\r | |
5553 | }\r | |
5554 | \r | |
5555 | #endif\r | |
5556 | #endif\r |