blob: 616318030fe15a5d0349ce661aceeac45ed737ea [file] [log] [blame]
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001/* Socket module */
2
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00003/* SSL support based on patches by Brian E Gallew and Laszlo Kovacs */
4
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005/*
6This module provides an interface to Berkeley socket IPC.
7
8Limitations:
9
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000010- only AF_INET, AF_INET6 and AF_UNIX address families are supported in a
Jeremy Hylton22308652001-02-02 03:23:09 +000011 portable manner, though AF_PACKET is supported under Linux.
Guido van Rossum81194471991-07-27 21:42:02 +000012- no read/write operations (use send/recv or makefile instead)
Guido van Rossumbe32c891996-06-20 16:25:29 +000013- additional restrictions apply on Windows
Guido van Rossum6574b3e1991-06-25 21:36:08 +000014
Guido van Rossum27e177d1995-03-16 15:43:47 +000015Module interface:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000016
Guido van Rossum27e177d1995-03-16 15:43:47 +000017- socket.error: exception raised for socket specific errors
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000018- socket.gaierror: exception raised for getaddrinfo/getnameinfo errors,
19 a subclass of socket.error
20- socket.herror: exception raised for gethostby* errors,
21 a subclass of socket.error
Guido van Rossum30a685f1991-06-27 15:51:29 +000022- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000023- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000024- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000025- socket.getprotobyname(protocolname) --> protocol number
Guido van Rossum27e177d1995-03-16 15:43:47 +000026- socket.getservbyname(servicename, protocolname) --> port number
Guido van Rossum4dd2a7e1991-07-01 18:51:33 +000027- socket.socket(family, type [, proto]) --> new socket object
Guido van Rossum006bf911996-06-12 04:04:55 +000028- socket.ntohs(16 bit value) --> new int object
29- socket.ntohl(32 bit value) --> new int object
30- socket.htons(16 bit value) --> new int object
31- socket.htonl(32 bit value) --> new int object
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000032- socket.getaddrinfo(host, port [, family, socktype, proto, flags])
33 --> List of (family, socktype, proto, canonname, sockaddr)
34- socket.getnameinfo(sockaddr, flags) --> (host, port)
Guido van Rossum27e177d1995-03-16 15:43:47 +000035- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +000036- socket.inet_aton(IP address) -> 32-bit packed IP representation
37- socket.inet_ntoa(packed IP) -> IP address string
Guido van Rossum09c8b6c1999-12-07 21:37:17 +000038- socket.ssl(socket, keyfile, certfile) -> new ssl object
Guido van Rossum6574b3e1991-06-25 21:36:08 +000039- an Internet socket address is a pair (hostname, port)
40 where hostname can be anything recognized by gethostbyname()
41 (including the dd.dd.dd.dd notation) and port is in host byte order
42- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000043- a UNIX domain socket address is a string specifying the pathname
Jeremy Hylton22308652001-02-02 03:23:09 +000044- an AF_PACKET socket address is a tuple containing a string
45 specifying the ethernet interface and an integer specifying
46 the Ethernet protocol number to be received. For example:
Jeremy Hyltondbfb6622001-02-02 19:55:17 +000047 ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple
48 specify packet-type and ha-type/addr -- these are ignored by
Jeremy Hylton22308652001-02-02 03:23:09 +000049 networking code, but accepted since they are returned by the
50 getsockname() method.
Guido van Rossum6574b3e1991-06-25 21:36:08 +000051
Guido van Rossum30a685f1991-06-27 15:51:29 +000052Socket methods:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000053
Guido van Rossum81194471991-07-27 21:42:02 +000054- s.accept() --> new socket object, sockaddr
Fred Drake728819a2000-07-01 03:40:12 +000055- s.bind(sockaddr) --> None
Guido van Rossum27e177d1995-03-16 15:43:47 +000056- s.close() --> None
Fred Drake728819a2000-07-01 03:40:12 +000057- s.connect(sockaddr) --> None
58- s.connect_ex(sockaddr) --> 0 or errno (handy for e.g. async connect)
Guido van Rossum27e177d1995-03-16 15:43:47 +000059- s.fileno() --> file descriptor
Guido van Rossumbe32c891996-06-20 16:25:29 +000060- s.dup() --> same as socket.fromfd(os.dup(s.fileno(), ...)
Guido van Rossumc89705d1992-11-26 08:54:07 +000061- s.getpeername() --> sockaddr
Guido van Rossum27e177d1995-03-16 15:43:47 +000062- s.getsockname() --> sockaddr
63- s.getsockopt(level, optname[, buflen]) --> int or string
64- s.listen(backlog) --> None
Guido van Rossum6b144911995-03-14 15:05:13 +000065- s.makefile([mode[, bufsize]]) --> file object
Guido van Rossum27e177d1995-03-16 15:43:47 +000066- s.recv(buflen [,flags]) --> string
67- s.recvfrom(buflen [,flags]) --> string, sockaddr
Guido van Rossumb6775db1994-08-01 11:34:53 +000068- s.send(string [,flags]) --> nbytes
69- s.sendto(string, [flags,] sockaddr) --> nbytes
Guido van Rossum27e177d1995-03-16 15:43:47 +000070- s.setblocking(0 | 1) --> None
71- s.setsockopt(level, optname, value) --> None
72- s.shutdown(how) --> None
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000073- repr(s) --> "<socket object, fd=%d, family=%d, type=%d, protocol=%d>"
Guido van Rossum30a685f1991-06-27 15:51:29 +000074
Guido van Rossum6574b3e1991-06-25 21:36:08 +000075*/
76
Guido van Rossum73624e91994-10-10 17:59:00 +000077#include "Python.h"
Guido van Rossum3baaa131999-03-22 21:44:51 +000078
Guido van Rossum9376b741999-09-15 22:01:40 +000079/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
80 script doesn't get this right, so we hardcode some platform checks below.
81 On the other hand, not all Linux versions agree, so there the settings
82 computed by the configure script are needed! */
83
84#ifndef linux
Guido van Rossume7de2061999-03-24 17:24:33 +000085#undef HAVE_GETHOSTBYNAME_R_3_ARG
86#undef HAVE_GETHOSTBYNAME_R_5_ARG
87#undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +000088#endif
Guido van Rossume7de2061999-03-24 17:24:33 +000089
Guido van Rossum7a122991999-04-13 04:07:32 +000090#ifndef WITH_THREAD
91#undef HAVE_GETHOSTBYNAME_R
92#endif
93
Guido van Rossume7de2061999-03-24 17:24:33 +000094#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +000095#if defined(_AIX) || defined(__osf__)
Guido van Rossume7de2061999-03-24 17:24:33 +000096#define HAVE_GETHOSTBYNAME_R_3_ARG
97#elif defined(__sun__) || defined(__sgi)
98#define HAVE_GETHOSTBYNAME_R_5_ARG
99#elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +0000100/* Rely on the configure script */
Guido van Rossume7de2061999-03-24 17:24:33 +0000101#else
102#undef HAVE_GETHOSTBYNAME_R
103#endif
104#endif
105
Guido van Rossum3baaa131999-03-22 21:44:51 +0000106#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && !defined(MS_WINDOWS)
107#define USE_GETHOSTBYNAME_LOCK
108#endif
109
110#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum49b56061998-10-01 20:42:43 +0000111#include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000112#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000113
Guido van Rossuma376cc51996-12-05 23:43:35 +0000114#ifdef HAVE_UNISTD_H
115#include <unistd.h>
116#endif
117
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000118#if defined(PYCC_VACPP)
119#include <types.h>
120#include <io.h>
121#include <sys/ioctl.h>
122#include <utils.h>
123#include <ctype.h>
124#endif
125
126#if defined(PYOS_OS2)
127#define INCL_DOS
128#define INCL_DOSERRORS
129#define INCL_NOPMAPI
130#include <os2.h>
131#endif
132
Guido van Rossum48a680c2001-03-02 06:34:14 +0000133#ifdef RISCOS
134#define NO_DUP
135#undef off_t
136#undef uid_t
137#undef gid_t
138#undef errno
139#include <signal.h>
140#include "socklib.h"
141#include "inetlib.h"
142#include "netdb.h"
143#include "unixlib.h"
144#include "netinet/in.h"
145#include "sys/ioctl.h"
146#else /*RISCOS*/
147
Guido van Rossumb6775db1994-08-01 11:34:53 +0000148#include <sys/types.h>
Guido van Rossumed233a51992-06-23 09:07:03 +0000149
Guido van Rossum81194471991-07-27 21:42:02 +0000150#include <signal.h>
Guido van Rossum6f489d91996-06-28 20:15:15 +0000151#ifndef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152#include <netdb.h>
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000153#include <sys/socket.h>
154#include <netinet/in.h>
Fred Drake51d90362000-10-06 15:37:06 +0000155#if !(defined(__BEOS__) || defined(__CYGWIN__))
Andrew M. Kuchling52a644c2000-03-31 17:24:30 +0000156#include <netinet/tcp.h>
Fred Drake11b09362000-05-16 13:30:12 +0000157#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000158
Guido van Rossum9376b741999-09-15 22:01:40 +0000159/* Headers needed for inet_ntoa() and inet_addr() */
160#ifdef __BEOS__
161#include <net/netdb.h>
162#else
Guido van Rossumff3ab422000-04-24 15:16:03 +0000163#ifndef USE_GUSI1
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000164#include <arpa/inet.h>
Guido van Rossum9376b741999-09-15 22:01:40 +0000165#endif
Guido van Rossum1b6e4631999-11-04 18:22:29 +0000166#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000167
Guido van Rossume4485b01994-09-07 14:32:49 +0000168#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000169#else
170#include <winsock.h>
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000171#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000172#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000173
174#endif /*RISCOS*/
175
Guido van Rossumb6775db1994-08-01 11:34:53 +0000176#ifdef HAVE_SYS_UN_H
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000177#include <sys/un.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000178#else
179#undef AF_UNIX
Guido van Rossum9575a441993-04-07 14:06:14 +0000180#endif
181
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +0000182#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +0000183#include <sys/ioctl.h>
184#include <net/if.h>
185#include <netpacket/packet.h>
186#endif
187
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000188#ifdef HAVE_STDDEF_H
189#include <stddef.h>
190#endif
191
192#ifndef offsetof
193#define offsetof(type, member) ((size_t)(&((type *)0)->member))
194#endif
195
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000196#ifndef O_NDELAY
197#define O_NDELAY O_NONBLOCK /* For QNX only? */
198#endif
199
Guido van Rossumff3ab422000-04-24 15:16:03 +0000200#ifdef USE_GUSI1
Jack Jansen508537b1996-02-14 15:57:45 +0000201/* fdopen() isn't declared in stdio.h (sigh) */
202#include <GUSI.h>
203#endif
204
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000205#include "addrinfo.h"
206
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000207#ifdef USE_SSL
Andrew M. Kuchling697a0b02001-01-18 17:41:41 +0000208#include "openssl/rsa.h"
209#include "openssl/crypto.h"
210#include "openssl/x509.h"
211#include "openssl/pem.h"
212#include "openssl/ssl.h"
213#include "openssl/err.h"
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000214#endif /* USE_SSL */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000215
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000216#ifndef HAVE_INET_PTON
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000217int inet_pton (int af, const char *src, void *dst);
218const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000219#endif
220
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000221#ifdef __APPLE__
222/* On OS X, getaddrinfo returns no error indication of lookup
223 failure, so we must use the emulation instead of the libinfo
224 implementation. Unfortunately, performing an autoconf test
225 for this bug would require DNS access for the machine performing
226 the configuration, which is not acceptable. Therefore, we
227 determine the bug just by checking for __APPLE__. If this bug
228 gets ever fixed, perhaps checking for sys/version.h would be
229 appropriate, which is 10/0 on the system with the bug. */
230#undef HAVE_GETADDRINFO
231/* avoid clashes with the C library definition of the symbol. */
232#define getaddrinfo fake_getaddrinfo
233#endif
234
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000235/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000236#if !defined(HAVE_GETADDRINFO)
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000237#include "getaddrinfo.c"
238#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000239#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000240#include "getnameinfo.c"
241#endif
242
Guido van Rossumbcc20741998-08-04 22:53:56 +0000243#if defined(MS_WINDOWS) || defined(__BEOS__)
244/* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000245/* seem to be a few differences in the API */
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000246#define SOCKETCLOSE closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000247#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000248#endif
249
Fred Drakea04eaad2000-06-30 02:46:07 +0000250/* abstract the socket file descriptor type */
251#ifdef MS_WINDOWS
252typedef SOCKET SOCKET_T;
253# ifdef MS_WIN64
254# define SIZEOF_SOCKET_T 8
255# else
256# define SIZEOF_SOCKET_T 4
257# endif
258#else
259typedef int SOCKET_T;
260# define SIZEOF_SOCKET_T SIZEOF_INT
261#endif
262
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000263#ifdef MS_WIN32
264# define EAFNOSUPPORT WSAEAFNOSUPPORT
265# define snprintf _snprintf
266#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000267
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000268#if defined(PYOS_OS2)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000269#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000270#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000271#endif
272
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000273#ifndef SOCKETCLOSE
274#define SOCKETCLOSE close
275#endif
276
Guido van Rossum30a685f1991-06-27 15:51:29 +0000277/* Global variable holding the exception type for errors detected
278 by this module (but not argument type or memory errors, etc.). */
279
Guido van Rossum73624e91994-10-10 17:59:00 +0000280static PyObject *PySocket_Error;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000281static PyObject *PyH_Error;
282static PyObject *PyGAI_Error;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000283
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000284#ifdef USE_SSL
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +0000285static PyObject *PySSLErrorObject;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000286#endif /* USE_SSL */
287
Guido van Rossum30a685f1991-06-27 15:51:29 +0000288
Guido van Rossum48a680c2001-03-02 06:34:14 +0000289#ifdef RISCOS
290/* Global variable which is !=0 if Python is running in a RISC OS taskwindow */
291static int taskwindow;
292#endif
293
294
Guido van Rossum30a685f1991-06-27 15:51:29 +0000295/* Convenience function to raise an error according to errno
296 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000297
Guido van Rossum73624e91994-10-10 17:59:00 +0000298static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000299PySocket_Err(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000300{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000301#ifdef MS_WINDOWS
Mark Hammond46a733d2000-07-24 01:45:11 +0000302 int err_no = WSAGetLastError();
303 if (err_no) {
304 static struct { int no; const char *msg; } *msgp, msgs[] = {
305 { WSAEINTR, "Interrupted system call" },
306 { WSAEBADF, "Bad file descriptor" },
307 { WSAEACCES, "Permission denied" },
308 { WSAEFAULT, "Bad address" },
309 { WSAEINVAL, "Invalid argument" },
310 { WSAEMFILE, "Too many open files" },
Guido van Rossum48a680c2001-03-02 06:34:14 +0000311 { WSAEWOULDBLOCK,
Mark Hammond46a733d2000-07-24 01:45:11 +0000312 "The socket operation could not complete "
313 "without blocking" },
314 { WSAEINPROGRESS, "Operation now in progress" },
315 { WSAEALREADY, "Operation already in progress" },
316 { WSAENOTSOCK, "Socket operation on non-socket" },
317 { WSAEDESTADDRREQ, "Destination address required" },
318 { WSAEMSGSIZE, "Message too long" },
319 { WSAEPROTOTYPE, "Protocol wrong type for socket" },
320 { WSAENOPROTOOPT, "Protocol not available" },
321 { WSAEPROTONOSUPPORT, "Protocol not supported" },
322 { WSAESOCKTNOSUPPORT, "Socket type not supported" },
323 { WSAEOPNOTSUPP, "Operation not supported" },
324 { WSAEPFNOSUPPORT, "Protocol family not supported" },
325 { WSAEAFNOSUPPORT, "Address family not supported" },
326 { WSAEADDRINUSE, "Address already in use" },
327 { WSAEADDRNOTAVAIL,
328 "Can't assign requested address" },
329 { WSAENETDOWN, "Network is down" },
330 { WSAENETUNREACH, "Network is unreachable" },
Guido van Rossum48a680c2001-03-02 06:34:14 +0000331 { WSAENETRESET,
Mark Hammond46a733d2000-07-24 01:45:11 +0000332 "Network dropped connection on reset" },
Guido van Rossum48a680c2001-03-02 06:34:14 +0000333 { WSAECONNABORTED,
Mark Hammond46a733d2000-07-24 01:45:11 +0000334 "Software caused connection abort" },
335 { WSAECONNRESET, "Connection reset by peer" },
336 { WSAENOBUFS, "No buffer space available" },
337 { WSAEISCONN, "Socket is already connected" },
338 { WSAENOTCONN, "Socket is not connected" },
339 { WSAESHUTDOWN, "Can't send after socket shutdown" },
340 { WSAETOOMANYREFS,
341 "Too many references: can't splice" },
342 { WSAETIMEDOUT, "Operation timed out" },
343 { WSAECONNREFUSED, "Connection refused" },
344 { WSAELOOP, "Too many levels of symbolic links" },
345 { WSAENAMETOOLONG, "File name too long" },
346 { WSAEHOSTDOWN, "Host is down" },
347 { WSAEHOSTUNREACH, "No route to host" },
348 { WSAENOTEMPTY, "Directory not empty" },
349 { WSAEPROCLIM, "Too many processes" },
350 { WSAEUSERS, "Too many users" },
351 { WSAEDQUOT, "Disc quota exceeded" },
352 { WSAESTALE, "Stale NFS file handle" },
353 { WSAEREMOTE, "Too many levels of remote in path" },
354 { WSASYSNOTREADY,
355 "Network subsystem is unvailable" },
356 { WSAVERNOTSUPPORTED,
357 "WinSock version is not supported" },
Guido van Rossum48a680c2001-03-02 06:34:14 +0000358 { WSANOTINITIALISED,
Mark Hammond46a733d2000-07-24 01:45:11 +0000359 "Successful WSAStartup() not yet performed" },
360 { WSAEDISCON, "Graceful shutdown in progress" },
361 /* Resolver errors */
362 { WSAHOST_NOT_FOUND, "No such host is known" },
363 { WSATRY_AGAIN, "Host not found, or server failed" },
364 { WSANO_RECOVERY,
365 "Unexpected server error encountered" },
366 { WSANO_DATA, "Valid name without requested data" },
367 { WSANO_ADDRESS, "No address, look for MX record" },
368 { 0, NULL }
369 };
Guido van Rossum73624e91994-10-10 17:59:00 +0000370 PyObject *v;
Mark Hammond46a733d2000-07-24 01:45:11 +0000371 const char *msg = "winsock error";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000372
Mark Hammond46a733d2000-07-24 01:45:11 +0000373 for (msgp = msgs; msgp->msg; msgp++) {
374 if (err_no == msgp->no) {
375 msg = msgp->msg;
376 break;
377 }
378 }
379
380 v = Py_BuildValue("(is)", err_no, msg);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000381 if (v != NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000382 PyErr_SetObject(PySocket_Error, v);
383 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000384 }
385 return NULL;
386 }
387 else
388#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000389
390#if defined(PYOS_OS2)
391 if (sock_errno() != NO_ERROR) {
392 APIRET rc;
393 ULONG msglen;
394 char outbuf[100];
395 int myerrorcode = sock_errno();
396
397 /* Retrieve Socket-Related Error Message from MPTN.MSG File */
398 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
399 myerrorcode - SOCBASEERR + 26, "mptn.msg", &msglen);
400 if (rc == NO_ERROR) {
401 PyObject *v;
402
403 outbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
404 if (strlen(outbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
405 char *lastc = &outbuf[ strlen(outbuf)-1 ];
406 while (lastc > outbuf && isspace(*lastc))
407 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
408 }
409 v = Py_BuildValue("(is)", myerrorcode, outbuf);
410 if (v != NULL) {
411 PyErr_SetObject(PySocket_Error, v);
412 Py_DECREF(v);
413 }
414 return NULL;
415 }
416 }
417#endif
418
Guido van Rossum73624e91994-10-10 17:59:00 +0000419 return PyErr_SetFromErrno(PySocket_Error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000420}
421
Guido van Rossum30a685f1991-06-27 15:51:29 +0000422
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000423static PyObject *
424PyH_Err(int h_error)
425{
426 PyObject *v;
427
428#ifdef HAVE_HSTRERROR
429 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
430#else
431 v = Py_BuildValue("(is)", h_error, "host not found");
432#endif
433 if (v != NULL) {
Martin v. Löwis864e9ff2001-08-04 22:32:03 +0000434 PyErr_SetObject(PyH_Error, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000435 Py_DECREF(v);
436 }
437
438 return NULL;
439}
440
441
442static PyObject *
443PyGAI_Err(int error)
444{
445 PyObject *v;
446
447 if (error == EAI_SYSTEM)
448 return PySocket_Err();
449
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000450#ifdef HAVE_GAI_STRERROR
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000451 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000452#else
453 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
454#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000455 if (v != NULL) {
456 PyErr_SetObject(PyGAI_Error, v);
457 Py_DECREF(v);
458 }
459
460 return NULL;
461}
462
463
Guido van Rossum30a685f1991-06-27 15:51:29 +0000464/* The object holding a socket. It holds some extra information,
465 like the address family, which is used to decode socket address
466 arguments properly. */
467
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000468typedef struct {
Guido van Rossum73624e91994-10-10 17:59:00 +0000469 PyObject_HEAD
Fred Drakea04eaad2000-06-30 02:46:07 +0000470 SOCKET_T sock_fd; /* Socket file descriptor */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000471 int sock_family; /* Address family, e.g., AF_INET */
472 int sock_type; /* Socket type, e.g., SOCK_STREAM */
473 int sock_proto; /* Protocol type, usually 0 */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000474 union sock_addr {
475 struct sockaddr_in in;
476#ifdef AF_UNIX
477 struct sockaddr_un un;
478#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000479#ifdef INET6
480 struct sockaddr_in6 in6;
481 struct sockaddr_storage storage;
482#endif
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +0000483#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +0000484 struct sockaddr_ll ll;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000485#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000486 } sock_addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000487} PySocketSockObject;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000488
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000489#ifdef USE_SSL
Guido van Rossum30a685f1991-06-27 15:51:29 +0000490
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000491typedef struct {
492 PyObject_HEAD
493 PySocketSockObject *Socket; /* Socket on which we're layered */
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000494 SSL_CTX* ctx;
495 SSL* ssl;
496 X509* server_cert;
497 BIO* sbio;
498 char server[256];
499 char issuer[256];
500
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +0000501} PySSLObject;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000502
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +0000503staticforward PyTypeObject PySSL_Type;
504staticforward PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args);
505staticforward PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000506
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +0000507#define PySSLObject_Check(v) ((v)->ob_type == &PySSL_Type)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000508
509#endif /* USE_SSL */
Guido van Rossum48a680c2001-03-02 06:34:14 +0000510
Guido van Rossum30a685f1991-06-27 15:51:29 +0000511/* A forward reference to the Socktype type object.
512 The Socktype variable contains pointers to various functions,
Guido van Rossum73624e91994-10-10 17:59:00 +0000513 some of which call newsockobject(), which uses Socktype, so
Guido van Rossum54ba21b1991-09-10 14:57:12 +0000514 there has to be a circular reference. */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000515
Guido van Rossum73624e91994-10-10 17:59:00 +0000516staticforward PyTypeObject PySocketSock_Type;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000517
Guido van Rossum30a685f1991-06-27 15:51:29 +0000518
519/* Create a new socket object.
520 This just creates the object and initializes it.
521 If the creation fails, return NULL and set an exception (implicit
522 in NEWOBJ()). */
523
Guido van Rossum73624e91994-10-10 17:59:00 +0000524static PySocketSockObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000525PySocketSock_New(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000526{
Guido van Rossum48a680c2001-03-02 06:34:14 +0000527#ifdef RISCOS
528 int block = 1;
529#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000530 PySocketSockObject *s;
Guido van Rossum55558541996-05-23 22:54:50 +0000531 PySocketSock_Type.ob_type = &PyType_Type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000532 s = PyObject_New(PySocketSockObject, &PySocketSock_Type);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000533 if (s != NULL) {
534 s->sock_fd = fd;
535 s->sock_family = family;
536 s->sock_type = type;
537 s->sock_proto = proto;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000538#ifdef RISCOS
539 if(taskwindow) {
540 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
541 }
542#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000543 }
544 return s;
545}
546
Guido van Rossum30a685f1991-06-27 15:51:29 +0000547
Guido van Rossum48a680c2001-03-02 06:34:14 +0000548/* Lock to allow python interpreter to continue, but only allow one
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000549 thread to be in gethostbyname */
Guido van Rossum3baaa131999-03-22 21:44:51 +0000550#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum65d5b571998-12-21 19:32:43 +0000551PyThread_type_lock gethostbyname_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000552#endif
553
554
Guido van Rossum30a685f1991-06-27 15:51:29 +0000555/* Convert a string specifying a host name or one of a few symbolic
556 names to a numeric IP address. This usually calls gethostbyname()
557 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000558 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000559 an error occurred; then an exception is raised. */
560
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000561static int
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000562setipaddr(char* name, struct sockaddr * addr_ret, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000563{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000564 struct addrinfo hints, *res;
565 int error;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000566
Guido van Rossuma376cc51996-12-05 23:43:35 +0000567 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000568 if (name[0] == '\0') {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000569 int siz;
570 memset(&hints, 0, sizeof(hints));
571 hints.ai_family = af;
572 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
573 hints.ai_flags = AI_PASSIVE;
574 error = getaddrinfo(NULL, "0", &hints, &res);
575 if (error) {
576 PyGAI_Err(error);
577 return -1;
578 }
579 switch (res->ai_family) {
580 case AF_INET:
581 siz = 4;
582 break;
583#ifdef INET6
584 case AF_INET6:
585 siz = 16;
586 break;
587#endif
588 default:
589 freeaddrinfo(res);
590 PyErr_SetString(PySocket_Error,
591 "unsupported address family");
592 return -1;
593 }
594 if (res->ai_next) {
595 PyErr_SetString(PySocket_Error,
596 "wildcard resolved to multiple address");
597 return -1;
598 }
599 memcpy(addr_ret, res->ai_addr, res->ai_addrlen);
600 freeaddrinfo(res);
601 return siz;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000602 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000603 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000604 struct sockaddr_in *sin;
605 if (af != PF_INET && af != PF_UNSPEC) {
606 PyErr_SetString(PySocket_Error,
607 "address family mismatched");
608 return -1;
609 }
610 sin = (struct sockaddr_in *)addr_ret;
611 memset((void *) sin, '\0', sizeof(*sin));
612 sin->sin_family = AF_INET;
613#ifdef HAVE_SOCKADDR_SA_LEN
614 sin->sin_len = sizeof(*sin);
615#endif
616 sin->sin_addr.s_addr = INADDR_BROADCAST;
617 return sizeof(sin->sin_addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000618 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000619 memset(&hints, 0, sizeof(hints));
620 hints.ai_family = af;
621 error = getaddrinfo(name, NULL, &hints, &res);
622 if (error) {
623 PyGAI_Err(error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000624 return -1;
625 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000626 memcpy((char *) addr_ret, res->ai_addr, res->ai_addrlen);
627 freeaddrinfo(res);
628 switch (addr_ret->sa_family) {
629 case AF_INET:
630 return 4;
631#ifdef INET6
632 case AF_INET6:
633 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000634#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000635 default:
636 PyErr_SetString(PySocket_Error, "unknown address family");
637 return -1;
638 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000639}
640
Guido van Rossum30a685f1991-06-27 15:51:29 +0000641
Guido van Rossum30a685f1991-06-27 15:51:29 +0000642/* Create a string object representing an IP address.
643 This is always a string of the form 'dd.dd.dd.dd' (with variable
644 size numbers). */
645
Guido van Rossum73624e91994-10-10 17:59:00 +0000646static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000647makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000648{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000649 char buf[NI_MAXHOST];
650 int error;
651
652 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
653 NI_NUMERICHOST);
654 if (error) {
655 PyGAI_Err(error);
656 return NULL;
657 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000658 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000659}
660
661
662/* Create an object representing the given socket address,
663 suitable for passing it back to bind(), connect() etc.
664 The family field of the sockaddr structure is inspected
665 to determine what kind of address it really is. */
666
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000667/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000668static PyObject *
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000669makesockaddr(int sockfd, struct sockaddr *addr, int addrlen)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000670{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000671 if (addrlen == 0) {
672 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000673 Py_INCREF(Py_None);
674 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000675 }
676
Guido van Rossumbcc20741998-08-04 22:53:56 +0000677#ifdef __BEOS__
Guido van Rossum2c8bcb82000-04-25 21:34:53 +0000678 /* XXX: BeOS version of accept() doesn't set family correctly */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000679 addr->sa_family = AF_INET;
680#endif
681
Guido van Rossum30a685f1991-06-27 15:51:29 +0000682 switch (addr->sa_family) {
683
684 case AF_INET:
685 {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000686 struct sockaddr_in *a;
687 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
Barry Warsaw752300b1997-01-03 17:18:10 +0000688 PyObject *ret = NULL;
689 if (addrobj) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000690 a = (struct sockaddr_in *)addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000691 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
692 Py_DECREF(addrobj);
693 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000694 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000695 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000696
Guido van Rossumb6775db1994-08-01 11:34:53 +0000697#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000698 case AF_UNIX:
699 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000700 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000701 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000702 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000703#endif /* AF_UNIX */
704
705#ifdef INET6
706 case AF_INET6:
707 {
708 struct sockaddr_in6 *a;
709 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
710 PyObject *ret = NULL;
711 if (addrobj) {
712 a = (struct sockaddr_in6 *)addr;
713 ret = Py_BuildValue("Oiii", addrobj, ntohs(a->sin6_port),
714 a->sin6_flowinfo, a->sin6_scope_id);
715 Py_DECREF(addrobj);
716 }
717 return ret;
718 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000719#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +0000720
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +0000721#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +0000722 case AF_PACKET:
723 {
724 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
725 char *ifname = "";
726 struct ifreq ifr;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000727 /* need to look up interface name give index */
728 if (a->sll_ifindex) {
Jeremy Hylton22308652001-02-02 03:23:09 +0000729 ifr.ifr_ifindex = a->sll_ifindex;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000730 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
Jeremy Hylton22308652001-02-02 03:23:09 +0000731 ifname = ifr.ifr_name;
Jeremy Hylton22308652001-02-02 03:23:09 +0000732 }
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000733 return Py_BuildValue("shbhs#", ifname, ntohs(a->sll_protocol),
Guido van Rossum48a680c2001-03-02 06:34:14 +0000734 a->sll_pkttype, a->sll_hatype,
735 a->sll_addr, a->sll_halen);
Jeremy Hylton22308652001-02-02 03:23:09 +0000736 }
737#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000738
Guido van Rossum30a685f1991-06-27 15:51:29 +0000739 /* More cases here... */
740
741 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +0000742 /* If we don't know the address family, don't raise an
743 exception -- return it as a tuple. */
744 return Py_BuildValue("is#",
745 addr->sa_family,
746 addr->sa_data,
747 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000748
Guido van Rossum30a685f1991-06-27 15:51:29 +0000749 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000750}
751
Guido van Rossum30a685f1991-06-27 15:51:29 +0000752
753/* Parse a socket address argument according to the socket object's
754 address family. Return 1 if the address was in the proper format,
755 0 of not. The address is returned through addr_ret, its length
756 through len_ret. */
757
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000758static int
Guido van Rossum48a680c2001-03-02 06:34:14 +0000759getsockaddrarg(PySocketSockObject *s, PyObject *args,
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000760 struct sockaddr **addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000761{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000762 switch (s->sock_family) {
763
Guido van Rossumb6775db1994-08-01 11:34:53 +0000764#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000765 case AF_UNIX:
766 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000767 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000768 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000769 int len;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000770 addr = (struct sockaddr_un* )&(s->sock_addr).un;
Guido van Rossum7e488981998-10-08 02:25:24 +0000771 if (!PyArg_Parse(args, "t#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000772 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000773 if (len > sizeof addr->sun_path) {
Barry Warsaw752300b1997-01-03 17:18:10 +0000774 PyErr_SetString(PySocket_Error,
775 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000776 return 0;
777 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000778 addr->sun_family = s->sock_family;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000779 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000780 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000781 *addr_ret = (struct sockaddr *) addr;
Guido van Rossum65af28a1996-06-11 18:36:33 +0000782 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000783 return 1;
784 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000785#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000786
Guido van Rossum30a685f1991-06-27 15:51:29 +0000787 case AF_INET:
788 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000789 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000790 char *host;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000791 int port;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000792 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossume4dad902000-12-01 13:13:11 +0000793 if (!PyTuple_Check(args)) {
794 PyErr_Format(PyExc_TypeError,
795 "getsockaddrarg: AF_INET address must be tuple, not %.500s",
796 args->ob_type->tp_name);
797 return 0;
798 }
799 if (!PyArg_ParseTuple(args, "si:getsockaddrarg", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000800 return 0;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000801 if (setipaddr(host, (struct sockaddr *)addr, AF_INET) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000802 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000803 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000804 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000805 *addr_ret = (struct sockaddr *) addr;
806 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000807 return 1;
808 }
809
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000810#ifdef INET6
811 case AF_INET6:
812 {
813 struct sockaddr_in6* addr;
814 char *host;
815 int port, flowinfo, scope_id;
816 addr = (struct sockaddr_in6*)&(s->sock_addr).in6;
817 flowinfo = scope_id = 0;
818 if (!PyArg_ParseTuple(args, "si|ii", &host, &port, &flowinfo,
819 &scope_id)) {
820 return 0;
821 }
822 if (setipaddr(host, (struct sockaddr *)addr, AF_INET6) < 0)
823 return 0;
824 addr->sin6_family = s->sock_family;
825 addr->sin6_port = htons((short)port);
826 addr->sin6_flowinfo = flowinfo;
827 addr->sin6_scope_id = scope_id;
828 *addr_ret = (struct sockaddr *) addr;
829 *len_ret = sizeof *addr;
830 return 1;
831 }
832#endif
833
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +0000834#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +0000835 case AF_PACKET:
836 {
837 struct sockaddr_ll* addr;
838 struct ifreq ifr;
839 char *interfaceName;
840 int protoNumber;
841 int hatype = 0;
842 int pkttype = 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000843 char *haddr;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000844
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000845 if (!PyArg_ParseTuple(args, "si|iis", &interfaceName,
846 &protoNumber, &pkttype, &hatype, &haddr))
Jeremy Hylton22308652001-02-02 03:23:09 +0000847 return 0;
848 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
849 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000850 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
851 PyErr_SetFromErrno(PySocket_Error);
Jeremy Hylton22308652001-02-02 03:23:09 +0000852 return 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000853 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000854 addr = &(s->sock_addr.ll);
855 addr->sll_family = AF_PACKET;
856 addr->sll_protocol = htons((short)protoNumber);
857 addr->sll_ifindex = ifr.ifr_ifindex;
858 addr->sll_pkttype = pkttype;
859 addr->sll_hatype = hatype;
860 *addr_ret = (struct sockaddr *) addr;
861 *len_ret = sizeof *addr;
862 return 1;
863 }
Guido van Rossum48a680c2001-03-02 06:34:14 +0000864#endif
865
Guido van Rossum30a685f1991-06-27 15:51:29 +0000866 /* More cases here... */
867
868 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000869 PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000870 return 0;
871
872 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000873}
874
Guido van Rossum30a685f1991-06-27 15:51:29 +0000875
Guido van Rossum48a680c2001-03-02 06:34:14 +0000876/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +0000877 Return 1 if the family is known, 0 otherwise. The length is returned
878 through len_ret. */
879
880static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +0000881getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +0000882{
883 switch (s->sock_family) {
884
Guido van Rossumb6775db1994-08-01 11:34:53 +0000885#ifdef AF_UNIX
Guido van Rossum710e1df1992-06-12 10:39:36 +0000886 case AF_UNIX:
887 {
888 *len_ret = sizeof (struct sockaddr_un);
889 return 1;
890 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000891#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +0000892
893 case AF_INET:
894 {
895 *len_ret = sizeof (struct sockaddr_in);
896 return 1;
897 }
898
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000899#ifdef INET6
900 case AF_INET6:
901 {
902 *len_ret = sizeof (struct sockaddr_in6);
903 return 1;
904 }
905#endif
906
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +0000907#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +0000908 case AF_PACKET:
909 {
910 *len_ret = sizeof (struct sockaddr_ll);
911 return 1;
912 }
913#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000914
Guido van Rossum710e1df1992-06-12 10:39:36 +0000915 /* More cases here... */
916
917 default:
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000918 PyErr_SetString(PySocket_Error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +0000919 return 0;
920
921 }
922}
923
924
Guido van Rossum30a685f1991-06-27 15:51:29 +0000925/* s.accept() method */
926
Guido van Rossum73624e91994-10-10 17:59:00 +0000927static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000928PySocketSock_accept(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000929{
930 char addrbuf[256];
Fred Drakea04eaad2000-06-30 02:46:07 +0000931 SOCKET_T newfd;
Guido van Rossumff3ab422000-04-24 15:16:03 +0000932 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +0000933 PyObject *sock = NULL;
934 PyObject *addr = NULL;
935 PyObject *res = NULL;
936
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000937 if (!PyArg_ParseTuple(args, ":accept"))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000938 return NULL;
Guido van Rossum710e1df1992-06-12 10:39:36 +0000939 if (!getsockaddrlen(s, &addrlen))
940 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +0000941 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000942 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000943 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000944 Py_END_ALLOW_THREADS
Fred Drakea04eaad2000-06-30 02:46:07 +0000945#ifdef MS_WINDOWS
946 if (newfd == INVALID_SOCKET)
947#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000948 if (newfd < 0)
Fred Drakea04eaad2000-06-30 02:46:07 +0000949#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000950 return PySocket_Err();
Barry Warsaw752300b1997-01-03 17:18:10 +0000951
Guido van Rossum30a685f1991-06-27 15:51:29 +0000952 /* Create the new object with unspecified family,
953 to avoid calls to bind() etc. on it. */
Guido van Rossum73624e91994-10-10 17:59:00 +0000954 sock = (PyObject *) PySocketSock_New(newfd,
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000955 s->sock_family,
956 s->sock_type,
957 s->sock_proto);
Barry Warsaw752300b1997-01-03 17:18:10 +0000958 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000959 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +0000960 goto finally;
961 }
Guido van Rossum48a680c2001-03-02 06:34:14 +0000962 addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000963 addrlen);
964 if (addr == NULL)
Barry Warsaw752300b1997-01-03 17:18:10 +0000965 goto finally;
966
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000967 res = Py_BuildValue("OO", sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +0000968
969 finally:
Guido van Rossum73624e91994-10-10 17:59:00 +0000970 Py_XDECREF(sock);
971 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000972 return res;
973}
974
Guido van Rossum82a5c661998-07-07 20:45:43 +0000975static char accept_doc[] =
976"accept() -> (socket object, address info)\n\
977\n\
978Wait for an incoming connection. Return a new socket representing the\n\
979connection, and the address of the client. For IP sockets, the address\n\
980info is a pair (hostaddr, port).";
981
Guido van Rossum30a685f1991-06-27 15:51:29 +0000982
Guido van Rossume4485b01994-09-07 14:32:49 +0000983/* s.setblocking(1 | 0) method */
984
Guido van Rossum73624e91994-10-10 17:59:00 +0000985static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000986PySocketSock_setblocking(PySocketSockObject *s, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000987{
988 int block;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000989#ifndef RISCOS
Guido van Rossum644a12b1997-04-09 19:24:53 +0000990#ifndef MS_WINDOWS
Guido van Rossume4485b01994-09-07 14:32:49 +0000991 int delay_flag;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000992#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000993#endif
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000994 if (!PyArg_ParseTuple(args, "i:setblocking", &block))
Guido van Rossume4485b01994-09-07 14:32:49 +0000995 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000996 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000997#ifdef __BEOS__
998 block = !block;
999 setsockopt( s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
1000 (void *)(&block), sizeof( int ) );
1001#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00001002#ifndef RISCOS
Guido van Rossum6f489d91996-06-28 20:15:15 +00001003#ifndef MS_WINDOWS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001004#ifdef PYOS_OS2
1005 block = !block;
1006 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
1007#else /* !PYOS_OS2 */
Guido van Rossume4485b01994-09-07 14:32:49 +00001008 delay_flag = fcntl (s->sock_fd, F_GETFL, 0);
1009 if (block)
1010 delay_flag &= (~O_NDELAY);
1011 else
1012 delay_flag |= O_NDELAY;
1013 fcntl (s->sock_fd, F_SETFL, delay_flag);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001014#endif /* !PYOS_OS2 */
1015#else /* MS_WINDOWS */
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001016 block = !block;
1017 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001018#endif /* MS_WINDOWS */
Guido van Rossumbcc20741998-08-04 22:53:56 +00001019#endif /* __BEOS__ */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001020#endif /* RISCOS */
Guido van Rossum73624e91994-10-10 17:59:00 +00001021 Py_END_ALLOW_THREADS
Guido van Rossume4485b01994-09-07 14:32:49 +00001022
Guido van Rossum73624e91994-10-10 17:59:00 +00001023 Py_INCREF(Py_None);
1024 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001025}
Guido van Rossume4485b01994-09-07 14:32:49 +00001026
Guido van Rossum82a5c661998-07-07 20:45:43 +00001027static char setblocking_doc[] =
1028"setblocking(flag)\n\
1029\n\
1030Set the socket to blocking (flag is true) or non-blocking (false).\n\
1031This uses the FIONBIO ioctl with the O_NDELAY flag.";
1032
Guido van Rossume4485b01994-09-07 14:32:49 +00001033
Guido van Rossum48a680c2001-03-02 06:34:14 +00001034#ifdef RISCOS
1035/* s.sleeptaskw(1 | 0) method */
1036
1037static PyObject *
1038PySocketSock_sleeptaskw(PySocketSockObject *s,PyObject *args)
1039{
1040 int block;
1041 int delay_flag;
1042 if (!PyArg_GetInt(args, &block))
1043 return NULL;
1044 Py_BEGIN_ALLOW_THREADS
1045 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
1046 Py_END_ALLOW_THREADS
1047
1048 Py_INCREF(Py_None);
1049 return Py_None;
1050}
1051static char sleeptaskw_doc[] =
1052"sleeptaskw(flag)\n\
1053\n\
1054Allow sleeps in taskwindows.";
1055#endif
1056
1057
Guido van Rossumaee08791992-09-08 09:05:33 +00001058/* s.setsockopt() method.
1059 With an integer third argument, sets an integer option.
1060 With a string third argument, sets an option from a buffer;
1061 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001062
Guido van Rossum73624e91994-10-10 17:59:00 +00001063static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001064PySocketSock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001065{
1066 int level;
1067 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001068 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +00001069 char *buf;
1070 int buflen;
1071 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001072
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001073 if (PyArg_ParseTuple(args, "iii:setsockopt",
1074 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001075 buf = (char *) &flag;
1076 buflen = sizeof flag;
1077 }
1078 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001079 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001080 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
1081 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +00001082 return NULL;
1083 }
Thomas Wouters334fb892000-07-25 12:56:38 +00001084 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001085 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001086 return PySocket_Err();
1087 Py_INCREF(Py_None);
1088 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001089}
1090
Guido van Rossum82a5c661998-07-07 20:45:43 +00001091static char setsockopt_doc[] =
1092"setsockopt(level, option, value)\n\
1093\n\
1094Set a socket option. See the Unix manual for level and option.\n\
1095The value argument can either be an integer or a string.";
1096
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001097
Guido van Rossumaee08791992-09-08 09:05:33 +00001098/* s.getsockopt() method.
1099 With two arguments, retrieves an integer option.
1100 With a third integer argument, retrieves a string buffer of that size;
1101 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001102
Guido van Rossum73624e91994-10-10 17:59:00 +00001103static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001104PySocketSock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001105{
1106 int level;
1107 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001108 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001109 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001110 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001111
Guido van Rossumbcc20741998-08-04 22:53:56 +00001112#ifdef __BEOS__
Fred Drake564a6cc2001-05-11 20:12:26 +00001113 /* We have incomplete socket support. */
1114 PyErr_SetString(PySocket_Error, "getsockopt not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00001115 return NULL;
1116#else
1117
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001118 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1119 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001120 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001121
Guido van Rossumbe32c891996-06-20 16:25:29 +00001122 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001123 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001124 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001125 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +00001126 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +00001127 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001128 return PySocket_Err();
1129 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +00001130 }
Guido van Rossumaee08791992-09-08 09:05:33 +00001131 if (buflen <= 0 || buflen > 1024) {
Barry Warsaw752300b1997-01-03 17:18:10 +00001132 PyErr_SetString(PySocket_Error,
1133 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +00001134 return NULL;
1135 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001136 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001137 if (buf == NULL)
1138 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001139 res = getsockopt(s->sock_fd, level, optname,
Fred Drake564a6cc2001-05-11 20:12:26 +00001140 (void *)PyString_AS_STRING(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001141 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001142 Py_DECREF(buf);
1143 return PySocket_Err();
Guido van Rossumaee08791992-09-08 09:05:33 +00001144 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001145 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001146 return buf;
Guido van Rossumbcc20741998-08-04 22:53:56 +00001147#endif /* __BEOS__ */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001148}
1149
Guido van Rossum82a5c661998-07-07 20:45:43 +00001150static char getsockopt_doc[] =
1151"getsockopt(level, option[, buffersize]) -> value\n\
1152\n\
1153Get a socket option. See the Unix manual for level and option.\n\
1154If a nonzero buffersize argument is given, the return value is a\n\
1155string of that length; otherwise it is an integer.";
1156
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001157
Fred Drake728819a2000-07-01 03:40:12 +00001158/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001159
Guido van Rossum73624e91994-10-10 17:59:00 +00001160static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001161PySocketSock_bind(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001162{
1163 struct sockaddr *addr;
1164 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001165 int res;
Fred Drake728819a2000-07-01 03:40:12 +00001166 PyObject *addro;
1167 if (!PyArg_ParseTuple(args, "O:bind", &addro))
1168 return NULL;
1169 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001170 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001171 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001172 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001173 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001174 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001175 return PySocket_Err();
1176 Py_INCREF(Py_None);
1177 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001178}
1179
Guido van Rossum82a5c661998-07-07 20:45:43 +00001180static char bind_doc[] =
1181"bind(address)\n\
1182\n\
1183Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001184pair (host, port); the host must refer to the local host. For raw packet\n\
1185sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])";
Guido van Rossum82a5c661998-07-07 20:45:43 +00001186
Guido van Rossum30a685f1991-06-27 15:51:29 +00001187
1188/* s.close() method.
1189 Set the file descriptor to -1 so operations tried subsequently
1190 will surely fail. */
1191
Guido van Rossum73624e91994-10-10 17:59:00 +00001192static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001193PySocketSock_close(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001194{
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001195 SOCKET_T fd;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001196 if (!PyArg_ParseTuple(args, ":close"))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001197 return NULL;
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001198 if ((fd = s->sock_fd) != -1) {
1199 s->sock_fd = -1;
Guido van Rossum08481461996-10-12 14:07:22 +00001200 Py_BEGIN_ALLOW_THREADS
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001201 (void) SOCKETCLOSE(fd);
Guido van Rossum08481461996-10-12 14:07:22 +00001202 Py_END_ALLOW_THREADS
1203 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001204 Py_INCREF(Py_None);
1205 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001206}
1207
Guido van Rossum82a5c661998-07-07 20:45:43 +00001208static char close_doc[] =
1209"close()\n\
1210\n\
1211Close the socket. It cannot be used after this call.";
1212
Guido van Rossum30a685f1991-06-27 15:51:29 +00001213
Fred Drake728819a2000-07-01 03:40:12 +00001214/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001215
Guido van Rossum73624e91994-10-10 17:59:00 +00001216static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001217PySocketSock_connect(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001218{
1219 struct sockaddr *addr;
1220 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001221 int res;
Fred Drake728819a2000-07-01 03:40:12 +00001222 PyObject *addro;
1223 if (!PyArg_ParseTuple(args, "O:connect", &addro))
1224 return NULL;
1225 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001226 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001227 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001228 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001229 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001230 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001231 return PySocket_Err();
1232 Py_INCREF(Py_None);
1233 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001234}
1235
Guido van Rossum82a5c661998-07-07 20:45:43 +00001236static char connect_doc[] =
1237"connect(address)\n\
1238\n\
1239Connect the socket to a remote address. For IP sockets, the address\n\
1240is a pair (host, port).";
1241
Guido van Rossum30a685f1991-06-27 15:51:29 +00001242
Fred Drake728819a2000-07-01 03:40:12 +00001243/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001244
1245static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001246PySocketSock_connect_ex(PySocketSockObject *s, PyObject *args)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001247{
1248 struct sockaddr *addr;
1249 int addrlen;
1250 int res;
Fred Drake728819a2000-07-01 03:40:12 +00001251 PyObject *addro;
1252 if (!PyArg_ParseTuple(args, "O:connect_ex", &addro))
1253 return NULL;
1254 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001255 return NULL;
1256 Py_BEGIN_ALLOW_THREADS
1257 res = connect(s->sock_fd, addr, addrlen);
1258 Py_END_ALLOW_THREADS
1259 if (res != 0)
1260 res = errno;
1261 return PyInt_FromLong((long) res);
1262}
1263
Guido van Rossum82a5c661998-07-07 20:45:43 +00001264static char connect_ex_doc[] =
1265"connect_ex(address)\n\
1266\n\
1267This is like connect(address), but returns an error code (the errno value)\n\
1268instead of raising an exception when an error occurs.";
1269
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001270
Guido van Rossumed233a51992-06-23 09:07:03 +00001271/* s.fileno() method */
1272
Guido van Rossum73624e91994-10-10 17:59:00 +00001273static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001274PySocketSock_fileno(PySocketSockObject *s, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +00001275{
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001276 if (!PyArg_ParseTuple(args, ":fileno"))
Guido van Rossumed233a51992-06-23 09:07:03 +00001277 return NULL;
Fred Drakea04eaad2000-06-30 02:46:07 +00001278#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +00001279 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001280#else
1281 return PyLong_FromLongLong((LONG_LONG)s->sock_fd);
1282#endif
Guido van Rossumed233a51992-06-23 09:07:03 +00001283}
1284
Guido van Rossum82a5c661998-07-07 20:45:43 +00001285static char fileno_doc[] =
1286"fileno() -> integer\n\
1287\n\
1288Return the integer file descriptor of the socket.";
1289
Guido van Rossumed233a51992-06-23 09:07:03 +00001290
Guido van Rossumbe32c891996-06-20 16:25:29 +00001291#ifndef NO_DUP
1292/* s.dup() method */
1293
1294static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001295PySocketSock_dup(PySocketSockObject *s, PyObject *args)
Guido van Rossumbe32c891996-06-20 16:25:29 +00001296{
Fred Drakea04eaad2000-06-30 02:46:07 +00001297 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001298 PyObject *sock;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001299 if (!PyArg_ParseTuple(args, ":dup"))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001300 return NULL;
1301 newfd = dup(s->sock_fd);
1302 if (newfd < 0)
1303 return PySocket_Err();
1304 sock = (PyObject *) PySocketSock_New(newfd,
Barry Warsaw752300b1997-01-03 17:18:10 +00001305 s->sock_family,
1306 s->sock_type,
1307 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001308 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001309 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001310 return sock;
1311}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001312
1313static char dup_doc[] =
1314"dup() -> socket object\n\
1315\n\
1316Return a new socket object connected to the same system resource.";
1317
Guido van Rossumbe32c891996-06-20 16:25:29 +00001318#endif
1319
1320
Guido van Rossumc89705d1992-11-26 08:54:07 +00001321/* s.getsockname() method */
1322
Guido van Rossum73624e91994-10-10 17:59:00 +00001323static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001324PySocketSock_getsockname(PySocketSockObject *s, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001325{
1326 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001327 int res;
1328 socklen_t addrlen;
1329
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001330 if (!PyArg_ParseTuple(args, ":getsockname"))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001331 return NULL;
1332 if (!getsockaddrlen(s, &addrlen))
1333 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +00001334 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001335 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001336 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001337 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001338 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001339 return PySocket_Err();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001340 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001341}
1342
Guido van Rossum82a5c661998-07-07 20:45:43 +00001343static char getsockname_doc[] =
1344"getsockname() -> address info\n\
1345\n\
1346Return the address of the local endpoint. For IP sockets, the address\n\
1347info is a pair (hostaddr, port).";
1348
Guido van Rossumc89705d1992-11-26 08:54:07 +00001349
Guido van Rossumb6775db1994-08-01 11:34:53 +00001350#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001351/* s.getpeername() method */
1352
Guido van Rossum73624e91994-10-10 17:59:00 +00001353static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001354PySocketSock_getpeername(PySocketSockObject *s, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001355{
1356 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001357 int res;
1358 socklen_t addrlen;
1359
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001360 if (!PyArg_ParseTuple(args, ":getpeername"))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001361 return NULL;
1362 if (!getsockaddrlen(s, &addrlen))
1363 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001364 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001365 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001366 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001367 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001368 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001369 return PySocket_Err();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001370 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001371}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001372
1373static char getpeername_doc[] =
1374"getpeername() -> address info\n\
1375\n\
1376Return the address of the remote endpoint. For IP sockets, the address\n\
1377info is a pair (hostaddr, port).";
1378
Guido van Rossumb6775db1994-08-01 11:34:53 +00001379#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001380
1381
Guido van Rossum30a685f1991-06-27 15:51:29 +00001382/* s.listen(n) method */
1383
Guido van Rossum73624e91994-10-10 17:59:00 +00001384static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001385PySocketSock_listen(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001386{
1387 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001388 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001389 if (!PyArg_ParseTuple(args, "i:listen", &backlog))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001390 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001391 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001392 if (backlog < 1)
1393 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001394 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001395 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001396 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001397 return PySocket_Err();
1398 Py_INCREF(Py_None);
1399 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001400}
1401
Guido van Rossum82a5c661998-07-07 20:45:43 +00001402static char listen_doc[] =
1403"listen(backlog)\n\
1404\n\
1405Enable a server to accept connections. The backlog argument must be at\n\
1406least 1; it specifies the number of unaccepted connection that the system\n\
1407will allow before refusing new connections.";
1408
1409
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001410#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00001411/* s.makefile(mode) method.
1412 Create a new open file object referring to a dupped version of
1413 the socket's file descriptor. (The dup() call is necessary so
1414 that the open file and socket objects may be closed independent
1415 of each other.)
1416 The mode argument specifies 'r' or 'w' passed to fdopen(). */
1417
Guido van Rossum73624e91994-10-10 17:59:00 +00001418static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001419PySocketSock_makefile(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001420{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001421 extern int fclose(FILE *);
Guido van Rossum6b144911995-03-14 15:05:13 +00001422 char *mode = "r";
1423 int bufsize = -1;
Fred Drakea04eaad2000-06-30 02:46:07 +00001424#ifdef MS_WIN32
Tim Peters79248aa2001-08-29 21:37:10 +00001425 Py_intptr_t fd;
Fred Drakea04eaad2000-06-30 02:46:07 +00001426#else
Guido van Rossum30a685f1991-06-27 15:51:29 +00001427 int fd;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001428#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001429 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00001430 PyObject *f;
1431
Guido van Rossum43713e52000-02-29 13:59:29 +00001432 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001433 return NULL;
Guido van Rossum8d665e61996-06-26 18:22:49 +00001434#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00001435 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
1436 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001437#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00001438 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001439#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00001440 {
Guido van Rossum6b144911995-03-14 15:05:13 +00001441 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001442 SOCKETCLOSE(fd);
Guido van Rossum73624e91994-10-10 17:59:00 +00001443 return PySocket_Err();
Guido van Rossum6b144911995-03-14 15:05:13 +00001444 }
1445 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
1446 if (f != NULL)
1447 PyFile_SetBufSize(f, bufsize);
1448 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001449}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001450
1451static char makefile_doc[] =
1452"makefile([mode[, buffersize]]) -> file object\n\
1453\n\
1454Return a regular file object corresponding to the socket.\n\
1455The mode and buffersize arguments are as for the built-in open() function.";
1456
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001457#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001458
Guido van Rossum48a680c2001-03-02 06:34:14 +00001459
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001460/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001461
Guido van Rossum73624e91994-10-10 17:59:00 +00001462static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001463PySocketSock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001464{
Guido van Rossumbe32c891996-06-20 16:25:29 +00001465 int len, n, flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +00001466 PyObject *buf;
Guido van Rossum43713e52000-02-29 13:59:29 +00001467 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001468 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001469 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001470 if (buf == NULL)
1471 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001472 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton1ba6bad2001-08-20 19:06:36 +00001473 n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001474 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +00001475 if (n < 0) {
1476 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +00001477 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001478 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001479 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001480 return NULL;
1481 return buf;
1482}
1483
Guido van Rossum82a5c661998-07-07 20:45:43 +00001484static char recv_doc[] =
1485"recv(buffersize[, flags]) -> data\n\
1486\n\
1487Receive up to buffersize bytes from the socket. For the optional flags\n\
1488argument, see the Unix manual. When no data is available, block until\n\
1489at least one byte is available or until the remote end is closed. When\n\
1490the remote end is closed and all data is read, return the empty string.";
1491
Guido van Rossum30a685f1991-06-27 15:51:29 +00001492
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001493/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001494
Guido van Rossum73624e91994-10-10 17:59:00 +00001495static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001496PySocketSock_recvfrom(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001497{
1498 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00001499 PyObject *buf = NULL;
1500 PyObject *addr = NULL;
1501 PyObject *ret = NULL;
1502
Guido van Rossumff3ab422000-04-24 15:16:03 +00001503 int len, n, flags = 0;
1504 socklen_t addrlen;
Guido van Rossum43713e52000-02-29 13:59:29 +00001505 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001506 return NULL;
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001507 if (!getsockaddrlen(s, &addrlen))
1508 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001509 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001510 if (buf == NULL)
1511 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001512 Py_BEGIN_ALLOW_THREADS
Fred Drakefd168342001-05-09 19:11:33 +00001513 memset(addrbuf, 0, addrlen);
1514 n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00001515#ifndef MS_WINDOWS
Guido van Rossum32c575d1997-12-02 20:37:32 +00001516#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001517 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001518#else
Thomas Wouters334fb892000-07-25 12:56:38 +00001519 (void *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001520#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001521#else
Barry Warsaw752300b1997-01-03 17:18:10 +00001522 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001523#endif
Barry Warsaw752300b1997-01-03 17:18:10 +00001524 );
Guido van Rossum73624e91994-10-10 17:59:00 +00001525 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +00001526 if (n < 0) {
1527 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +00001528 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001529 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001530 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001531 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001532
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001533 if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf, addrlen)))
Barry Warsaw752300b1997-01-03 17:18:10 +00001534 goto finally;
1535
Guido van Rossum73624e91994-10-10 17:59:00 +00001536 ret = Py_BuildValue("OO", buf, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001537 finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001538 Py_XDECREF(addr);
1539 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00001540 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001541}
1542
Guido van Rossum82a5c661998-07-07 20:45:43 +00001543static char recvfrom_doc[] =
1544"recvfrom(buffersize[, flags]) -> (data, address info)\n\
1545\n\
1546Like recv(buffersize, flags) but also return the sender's address info.";
1547
Guido van Rossum30a685f1991-06-27 15:51:29 +00001548
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001549/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001550
Guido van Rossum73624e91994-10-10 17:59:00 +00001551static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001552PySocketSock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001553{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001554 char *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001555 int len, n, flags = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +00001556 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001557 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001558 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001559 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001560 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00001561 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001562 return PySocket_Err();
1563 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001564}
1565
Guido van Rossum82a5c661998-07-07 20:45:43 +00001566static char send_doc[] =
1567"send(data[, flags])\n\
1568\n\
1569Send a data string to the socket. For the optional flags\n\
1570argument, see the Unix manual.";
1571
Guido van Rossum30a685f1991-06-27 15:51:29 +00001572
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001573/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001574
Guido van Rossum73624e91994-10-10 17:59:00 +00001575static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001576PySocketSock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001577{
Guido van Rossum73624e91994-10-10 17:59:00 +00001578 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001579 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001580 struct sockaddr *addr;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001581 int addrlen, len, n, flags;
1582 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001583 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001584 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001585 if (!PyArg_ParseTuple(args, "s#iO:sendto",
1586 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001587 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001588 }
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001589 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001590 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001591 Py_BEGIN_ALLOW_THREADS
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001592 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001593 Py_END_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001594 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001595 return PySocket_Err();
1596 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001597}
1598
Guido van Rossum82a5c661998-07-07 20:45:43 +00001599static char sendto_doc[] =
1600"sendto(data[, flags], address)\n\
1601\n\
1602Like send(data, flags) but allows specifying the destination address.\n\
1603For IP sockets, the address is a pair (hostaddr, port).";
1604
Guido van Rossum30a685f1991-06-27 15:51:29 +00001605
1606/* s.shutdown(how) method */
1607
Guido van Rossum73624e91994-10-10 17:59:00 +00001608static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001609PySocketSock_shutdown(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001610{
1611 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001612 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001613 if (!PyArg_ParseTuple(args, "i:shutdown", &how))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001614 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001615 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001616 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00001617 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001618 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001619 return PySocket_Err();
1620 Py_INCREF(Py_None);
1621 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001622}
1623
Guido van Rossum82a5c661998-07-07 20:45:43 +00001624static char shutdown_doc[] =
1625"shutdown(flag)\n\
1626\n\
1627Shut down the reading side of the socket (flag == 0), the writing side\n\
1628of the socket (flag == 1), or both ends (flag == 2).";
1629
Guido van Rossum30a685f1991-06-27 15:51:29 +00001630
1631/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001632
Guido van Rossum73624e91994-10-10 17:59:00 +00001633static PyMethodDef PySocketSock_methods[] = {
Guido van Rossum48a680c2001-03-02 06:34:14 +00001634 {"accept", (PyCFunction)PySocketSock_accept, METH_VARARGS,
1635 accept_doc},
1636 {"bind", (PyCFunction)PySocketSock_bind, METH_VARARGS,
1637 bind_doc},
1638 {"close", (PyCFunction)PySocketSock_close, METH_VARARGS,
1639 close_doc},
1640 {"connect", (PyCFunction)PySocketSock_connect, METH_VARARGS,
1641 connect_doc},
1642 {"connect_ex", (PyCFunction)PySocketSock_connect_ex, METH_VARARGS,
1643 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001644#ifndef NO_DUP
Guido van Rossum48a680c2001-03-02 06:34:14 +00001645 {"dup", (PyCFunction)PySocketSock_dup, METH_VARARGS,
1646 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001647#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001648 {"fileno", (PyCFunction)PySocketSock_fileno, METH_VARARGS,
1649 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001650#ifdef HAVE_GETPEERNAME
Guido van Rossum48a680c2001-03-02 06:34:14 +00001651 {"getpeername", (PyCFunction)PySocketSock_getpeername, METH_VARARGS,
1652 getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00001653#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001654 {"getsockname", (PyCFunction)PySocketSock_getsockname, METH_VARARGS,
1655 getsockname_doc},
1656 {"getsockopt", (PyCFunction)PySocketSock_getsockopt, METH_VARARGS,
1657 getsockopt_doc},
1658 {"listen", (PyCFunction)PySocketSock_listen, METH_VARARGS,
1659 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001660#ifndef NO_DUP
Guido van Rossum48a680c2001-03-02 06:34:14 +00001661 {"makefile", (PyCFunction)PySocketSock_makefile, METH_VARARGS,
1662 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001663#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001664 {"recv", (PyCFunction)PySocketSock_recv, METH_VARARGS,
1665 recv_doc},
1666 {"recvfrom", (PyCFunction)PySocketSock_recvfrom, METH_VARARGS,
1667 recvfrom_doc},
1668 {"send", (PyCFunction)PySocketSock_send, METH_VARARGS,
1669 send_doc},
1670 {"sendto", (PyCFunction)PySocketSock_sendto, METH_VARARGS,
1671 sendto_doc},
1672 {"setblocking", (PyCFunction)PySocketSock_setblocking, METH_VARARGS,
1673 setblocking_doc},
1674 {"setsockopt", (PyCFunction)PySocketSock_setsockopt, METH_VARARGS,
1675 setsockopt_doc},
1676 {"shutdown", (PyCFunction)PySocketSock_shutdown, METH_VARARGS,
1677 shutdown_doc},
1678#ifdef RISCOS
1679 {"sleeptaskw", (PyCFunction)PySocketSock_sleeptaskw, METH_VARARGS,
1680 sleeptaskw_doc},
1681#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00001682 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001683};
1684
Guido van Rossum30a685f1991-06-27 15:51:29 +00001685
Guido van Rossum73624e91994-10-10 17:59:00 +00001686/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00001687 First close the file description. */
1688
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001689static void
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001690PySocketSock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001691{
Guido van Rossumfa972c92000-04-10 12:45:45 +00001692 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001693 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001694 PyObject_Del(s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001695}
1696
Guido van Rossum30a685f1991-06-27 15:51:29 +00001697
1698/* Return a socket object's named attribute. */
1699
Guido van Rossum73624e91994-10-10 17:59:00 +00001700static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001701PySocketSock_getattr(PySocketSockObject *s, char *name)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001702{
Guido van Rossum73624e91994-10-10 17:59:00 +00001703 return Py_FindMethod(PySocketSock_methods, (PyObject *) s, name);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001704}
1705
Guido van Rossum30a685f1991-06-27 15:51:29 +00001706
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001707static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001708PySocketSock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001709{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001710 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00001711#if SIZEOF_SOCKET_T > SIZEOF_LONG
1712 if (s->sock_fd > LONG_MAX) {
1713 /* this can occur on Win64, and actually there is a special
1714 ugly printf formatter for decimal pointer length integer
1715 printing, only bother if necessary*/
1716 PyErr_SetString(PyExc_OverflowError,
1717 "no printf formatter to display the socket descriptor in decimal");
1718 return NULL;
1719 }
1720#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001721 sprintf(buf,
1722 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
Fred Drakea04eaad2000-06-30 02:46:07 +00001723 (long)s->sock_fd, s->sock_family, s->sock_type, s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00001724 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001725}
1726
1727
Guido van Rossumb6775db1994-08-01 11:34:53 +00001728/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001729
Guido van Rossum73624e91994-10-10 17:59:00 +00001730static PyTypeObject PySocketSock_Type = {
Guido van Rossum55558541996-05-23 22:54:50 +00001731 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001732 0,
1733 "socket",
Guido van Rossum73624e91994-10-10 17:59:00 +00001734 sizeof(PySocketSockObject),
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001735 0,
Guido van Rossum73624e91994-10-10 17:59:00 +00001736 (destructor)PySocketSock_dealloc, /*tp_dealloc*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001737 0, /*tp_print*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001738 (getattrfunc)PySocketSock_getattr, /*tp_getattr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001739 0, /*tp_setattr*/
1740 0, /*tp_compare*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001741 (reprfunc)PySocketSock_repr, /*tp_repr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001742 0, /*tp_as_number*/
1743 0, /*tp_as_sequence*/
1744 0, /*tp_as_mapping*/
1745};
1746
Guido van Rossum30a685f1991-06-27 15:51:29 +00001747
Guido van Rossum81194471991-07-27 21:42:02 +00001748/* Python interface to gethostname(). */
1749
1750/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001751static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001752PySocket_gethostname(PyObject *self, PyObject *args)
Guido van Rossum81194471991-07-27 21:42:02 +00001753{
1754 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001755 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001756 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00001757 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001758 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001759 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00001760 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001761 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001762 return PySocket_Err();
Guido van Rossum81194471991-07-27 21:42:02 +00001763 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00001764 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00001765}
Guido van Rossumff4949e1992-08-05 19:58:53 +00001766
Guido van Rossum82a5c661998-07-07 20:45:43 +00001767static char gethostname_doc[] =
1768"gethostname() -> string\n\
1769\n\
1770Return the current host name.";
1771
Guido van Rossumff4949e1992-08-05 19:58:53 +00001772
Guido van Rossum30a685f1991-06-27 15:51:29 +00001773/* Python interface to gethostbyname(name). */
1774
1775/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001776static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001777PySocket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001778{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001779 char *name;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001780 struct sockaddr_storage addrbuf;
1781
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001782 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001783 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001784 if (setipaddr(name, (struct sockaddr *)&addrbuf, AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001785 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001786 return makeipaddr((struct sockaddr *)&addrbuf,
1787 sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00001788}
1789
Guido van Rossum82a5c661998-07-07 20:45:43 +00001790static char gethostbyname_doc[] =
1791"gethostbyname(host) -> address\n\
1792\n\
1793Return the IP address (a string of the form '255.255.255.255') for a host.";
1794
1795
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001796/* Convenience function common to gethostbyname_ex and gethostbyaddr */
1797
1798static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001799gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001800{
1801 char **pch;
1802 PyObject *rtn_tuple = (PyObject *)NULL;
1803 PyObject *name_list = (PyObject *)NULL;
1804 PyObject *addr_list = (PyObject *)NULL;
1805 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001806
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001807 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00001808 /* Let's get real error message to return */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001809 PyH_Err(h_errno);
1810 return NULL;
1811 }
1812 if (h->h_addrtype != af) {
1813#ifdef HAVE_STRERROR
1814 /* Let's get real error message to return */
1815 PyErr_SetString(PySocket_Error, (char *)strerror(EAFNOSUPPORT));
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001816#else
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001817 PyErr_SetString(PySocket_Error,
1818 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001819#endif
1820 return NULL;
1821 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001822 switch (af) {
1823 case AF_INET:
1824 if (alen < sizeof(struct sockaddr_in))
1825 return NULL;
1826 break;
1827#ifdef INET6
1828 case AF_INET6:
1829 if (alen < sizeof(struct sockaddr_in6))
1830 return NULL;
1831 break;
1832#endif
1833 }
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001834 if ((name_list = PyList_New(0)) == NULL)
1835 goto err;
1836 if ((addr_list = PyList_New(0)) == NULL)
1837 goto err;
1838 for (pch = h->h_aliases; *pch != NULL; pch++) {
1839 int status;
1840 tmp = PyString_FromString(*pch);
1841 if (tmp == NULL)
1842 goto err;
1843 status = PyList_Append(name_list, tmp);
1844 Py_DECREF(tmp);
1845 if (status)
1846 goto err;
1847 }
1848 for (pch = h->h_addr_list; *pch != NULL; pch++) {
1849 int status;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001850 switch (af) {
1851 case AF_INET:
1852 {
1853 struct sockaddr_in sin;
1854 memset(&sin, 0, sizeof(sin));
1855 sin.sin_family = af;
1856#ifdef HAVE_SOCKADDR_SA_LEN
1857 sin.sin_len = sizeof(sin);
1858#endif
1859 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
1860 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
1861 if (pch == h->h_addr_list && alen >= sizeof(sin))
1862 memcpy((char *) addr, &sin, sizeof(sin));
1863 break;
1864 }
1865#ifdef INET6
1866 case AF_INET6:
1867 {
1868 struct sockaddr_in6 sin6;
1869 memset(&sin6, 0, sizeof(sin6));
1870 sin6.sin6_family = af;
1871#ifdef HAVE_SOCKADDR_SA_LEN
1872 sin6.sin6_len = sizeof(sin6);
1873#endif
1874 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
1875 tmp = makeipaddr((struct sockaddr *)&sin6,
1876 sizeof(sin6));
1877 if (pch == h->h_addr_list && alen >= sizeof(sin6))
1878 memcpy((char *) addr, &sin6, sizeof(sin6));
1879 break;
1880 }
1881#endif
1882 default: /* can't happen */
1883 PyErr_SetString(PySocket_Error,
1884 "unsupported address family");
1885 return NULL;
1886 }
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001887 if (tmp == NULL)
1888 goto err;
1889 status = PyList_Append(addr_list, tmp);
1890 Py_DECREF(tmp);
1891 if (status)
1892 goto err;
1893 }
1894 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
1895 err:
1896 Py_XDECREF(name_list);
1897 Py_XDECREF(addr_list);
1898 return rtn_tuple;
1899}
1900
1901
1902/* Python interface to gethostbyname_ex(name). */
1903
1904/*ARGSUSED*/
1905static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001906PySocket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001907{
1908 char *name;
1909 struct hostent *h;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001910 struct sockaddr_storage addr;
Martin v. Löwis9db2f572001-07-23 01:30:10 +00001911 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00001912 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001913#ifdef HAVE_GETHOSTBYNAME_R
1914 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001915#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
1916 struct hostent_data data;
1917#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001918 char buf[16384];
1919 int buf_len = (sizeof buf) - 1;
1920 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001921#endif
1922#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001923 int result;
1924#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001925#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001926
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001927 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001928 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001929 if (setipaddr(name, (struct sockaddr *)&addr, PF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001930 return NULL;
1931 Py_BEGIN_ALLOW_THREADS
1932#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001933#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001934 result = gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001935#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001936 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001937#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00001938 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001939 result = gethostbyname_r(name, &hp_allocated, &data);
1940 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001941#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001942#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00001943#ifdef USE_GETHOSTBYNAME_LOCK
1944 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001945#endif
1946 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001947#endif /* HAVE_GETHOSTBYNAME_R */
1948 Py_END_ALLOW_THREADS
Martin v. Löwis9db2f572001-07-23 01:30:10 +00001949 /* Some C libraries would require addr.__ss_family instead of addr.ss_family.
1950 Therefore, we cast the sockaddr_storage into sockaddr to access sa_family. */
1951 sa = (struct sockaddr*)&addr;
1952 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00001953#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum955becc1999-03-22 20:14:53 +00001954 PyThread_release_lock(gethostbyname_lock);
1955#endif
1956 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001957}
1958
1959static char ghbn_ex_doc[] =
1960"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
1961\n\
1962Return the true host name, a list of aliases, and a list of IP addresses,\n\
1963for a host. The host argument is a string giving a host name or IP number.";
1964
1965
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001966/* Python interface to gethostbyaddr(IP). */
1967
1968/*ARGSUSED*/
1969static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001970PySocket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001971{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001972#ifdef INET6
1973 struct sockaddr_storage addr;
1974#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00001975 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001976#endif
1977 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001978 char *ip_num;
1979 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00001980 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001981#ifdef HAVE_GETHOSTBYNAME_R
1982 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001983#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
1984 struct hostent_data data;
1985#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001986 char buf[16384];
1987 int buf_len = (sizeof buf) - 1;
1988 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001989#endif
1990#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001991 int result;
1992#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001993#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001994 char *ap;
1995 int al;
1996 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001997
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001998 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001999 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002000 af = PF_UNSPEC;
2001 if (setipaddr(ip_num, sa, af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002002 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002003 af = sa->sa_family;
2004 ap = NULL;
2005 al = 0;
2006 switch (af) {
2007 case AF_INET:
2008 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
2009 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2010 break;
2011#ifdef INET6
2012 case AF_INET6:
2013 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
2014 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2015 break;
2016#endif
2017 default:
2018 PyErr_SetString(PySocket_Error, "unsupported address family");
2019 return NULL;
2020 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002021 Py_BEGIN_ALLOW_THREADS
2022#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002023#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002024 result = gethostbyaddr_r(ap, al, af,
2025 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002026 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002027#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002028 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002029 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002030#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002031 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002032 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002033 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002034#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002035#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002036#ifdef USE_GETHOSTBYNAME_LOCK
2037 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002038#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002039 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002040#endif /* HAVE_GETHOSTBYNAME_R */
2041 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002042 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002043#ifdef USE_GETHOSTBYNAME_LOCK
2044 PyThread_release_lock(gethostbyname_lock);
2045#endif
2046 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002047}
2048
Guido van Rossum82a5c661998-07-07 20:45:43 +00002049static char gethostbyaddr_doc[] =
2050"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
2051\n\
2052Return the true host name, a list of aliases, and a list of IP addresses,\n\
2053for a host. The host argument is a string giving a host name or IP number.";
2054
Guido van Rossum30a685f1991-06-27 15:51:29 +00002055
2056/* Python interface to getservbyname(name).
2057 This only returns the port number, since the other info is already
2058 known or not useful (like the list of aliases). */
2059
2060/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002061static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002062PySocket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002063{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002064 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002065 struct servent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002066 if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002067 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002068 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002069 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00002070 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00002071 if (sp == NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +00002072 PyErr_SetString(PySocket_Error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00002073 return NULL;
2074 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002075 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002076}
2077
Guido van Rossum82a5c661998-07-07 20:45:43 +00002078static char getservbyname_doc[] =
2079"getservbyname(servicename, protocolname) -> integer\n\
2080\n\
2081Return a port number from a service name and protocol name.\n\
2082The protocol name should be 'tcp' or 'udp'.";
2083
Guido van Rossum30a685f1991-06-27 15:51:29 +00002084
Guido van Rossum3901d851996-12-19 16:35:04 +00002085/* Python interface to getprotobyname(name).
2086 This only returns the protocol number, since the other info is
2087 already known or not useful (like the list of aliases). */
2088
2089/*ARGSUSED*/
2090static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002091PySocket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00002092{
2093 char *name;
2094 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00002095#ifdef __BEOS__
2096/* Not available in BeOS yet. - [cjh] */
2097 PyErr_SetString( PySocket_Error, "getprotobyname not supported" );
2098 return NULL;
2099#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002100 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00002101 return NULL;
2102 Py_BEGIN_ALLOW_THREADS
2103 sp = getprotobyname(name);
2104 Py_END_ALLOW_THREADS
2105 if (sp == NULL) {
2106 PyErr_SetString(PySocket_Error, "protocol not found");
2107 return NULL;
2108 }
2109 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002110#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00002111}
2112
Guido van Rossum82a5c661998-07-07 20:45:43 +00002113static char getprotobyname_doc[] =
2114"getprotobyname(name) -> integer\n\
2115\n\
2116Return the protocol number for the named protocol. (Rarely used.)";
2117
Guido van Rossum3901d851996-12-19 16:35:04 +00002118
Guido van Rossum30a685f1991-06-27 15:51:29 +00002119/* Python interface to socket(family, type, proto).
2120 The third (protocol) argument is optional.
2121 Return a new socket object. */
2122
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002123/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002124static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002125PySocket_socket(PyObject *self, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002126{
Guido van Rossum73624e91994-10-10 17:59:00 +00002127 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00002128 SOCKET_T fd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00002129 int family, type, proto = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +00002130 if (!PyArg_ParseTuple(args, "ii|i:socket", &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002131 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002132 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002133 fd = socket(family, type, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00002134 Py_END_ALLOW_THREADS
Guido van Rossum6f489d91996-06-28 20:15:15 +00002135#ifdef MS_WINDOWS
Guido van Rossumd639d4d1996-06-12 04:04:22 +00002136 if (fd == INVALID_SOCKET)
2137#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002138 if (fd < 0)
Guido van Rossumd639d4d1996-06-12 04:04:22 +00002139#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002140 return PySocket_Err();
2141 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum30a685f1991-06-27 15:51:29 +00002142 /* If the object can't be created, don't forget to close the
2143 file descriptor again! */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002144 if (s == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002145 (void) SOCKETCLOSE(fd);
Guido van Rossum81194471991-07-27 21:42:02 +00002146 /* From now on, ignore SIGPIPE and let the error checking
2147 do the work. */
Guido van Rossum48a680c2001-03-02 06:34:14 +00002148#ifdef SIGPIPE
Guido van Rossum81194471991-07-27 21:42:02 +00002149 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002150#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002151 return (PyObject *) s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002152}
2153
Guido van Rossum82a5c661998-07-07 20:45:43 +00002154static char socket_doc[] =
2155"socket(family, type[, proto]) -> socket object\n\
2156\n\
2157Open a socket of the given type. The family argument specifies the\n\
2158address family; it is normally AF_INET, sometimes AF_UNIX.\n\
2159The type argument specifies whether this is a stream (SOCK_STREAM)\n\
2160or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
2161specifying the default protocol.";
2162
2163
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002164#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002165/* Create a socket object from a numeric file description.
2166 Useful e.g. if stdin is a socket.
2167 Additional arguments as for socket(). */
2168
2169/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002170static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002171PySocket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002172{
Guido van Rossum73624e91994-10-10 17:59:00 +00002173 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00002174 SOCKET_T fd;
2175 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002176 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
2177 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002178 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00002179 /* Dup the fd so it and the socket can be closed independently */
2180 fd = dup(fd);
2181 if (fd < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00002182 return PySocket_Err();
2183 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002184 /* From now on, ignore SIGPIPE and let the error checking
2185 do the work. */
Guido van Rossum48a680c2001-03-02 06:34:14 +00002186#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002187 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002188#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002189 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002190}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002191
2192static char fromfd_doc[] =
2193"fromfd(fd, family, type[, proto]) -> socket object\n\
2194\n\
2195Create a socket object from the given file descriptor.\n\
2196The remaining arguments are the same as for socket().";
2197
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002198#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002199
Guido van Rossum82a5c661998-07-07 20:45:43 +00002200
Guido van Rossum006bf911996-06-12 04:04:55 +00002201static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002202PySocket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002203{
2204 int x1, x2;
2205
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002206 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002207 return NULL;
2208 }
2209 x2 = (int)ntohs((short)x1);
2210 return PyInt_FromLong(x2);
2211}
2212
Guido van Rossum82a5c661998-07-07 20:45:43 +00002213static char ntohs_doc[] =
2214"ntohs(integer) -> integer\n\
2215\n\
2216Convert a 16-bit integer from network to host byte order.";
2217
2218
Guido van Rossum006bf911996-06-12 04:04:55 +00002219static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002220PySocket_ntohl(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002221{
2222 int x1, x2;
2223
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002224 if (!PyArg_ParseTuple(args, "i:ntohl", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002225 return NULL;
2226 }
2227 x2 = ntohl(x1);
2228 return PyInt_FromLong(x2);
2229}
2230
Guido van Rossum82a5c661998-07-07 20:45:43 +00002231static char ntohl_doc[] =
2232"ntohl(integer) -> integer\n\
2233\n\
2234Convert a 32-bit integer from network to host byte order.";
2235
2236
Guido van Rossum006bf911996-06-12 04:04:55 +00002237static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002238PySocket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002239{
2240 int x1, x2;
2241
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002242 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002243 return NULL;
2244 }
2245 x2 = (int)htons((short)x1);
2246 return PyInt_FromLong(x2);
2247}
2248
Guido van Rossum82a5c661998-07-07 20:45:43 +00002249static char htons_doc[] =
2250"htons(integer) -> integer\n\
2251\n\
2252Convert a 16-bit integer from host to network byte order.";
2253
2254
Guido van Rossum006bf911996-06-12 04:04:55 +00002255static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002256PySocket_htonl(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002257{
2258 int x1, x2;
2259
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002260 if (!PyArg_ParseTuple(args, "i:htonl", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002261 return NULL;
2262 }
2263 x2 = htonl(x1);
2264 return PyInt_FromLong(x2);
2265}
2266
Guido van Rossum82a5c661998-07-07 20:45:43 +00002267static char htonl_doc[] =
2268"htonl(integer) -> integer\n\
2269\n\
2270Convert a 32-bit integer from host to network byte order.";
2271
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002272/*
2273 * socket.inet_aton() and socket.inet_ntoa() functions
2274 *
2275 * written 20 Aug 1999 by Ben Gertzfield <che@debian.org> <- blame him!
2276 *
2277 */
2278
Guido van Rossum48a680c2001-03-02 06:34:14 +00002279static char inet_aton_doc[] =
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002280"inet_aton(string) -> packed 32-bit IP representation\n\
2281\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002282Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002283binary format used in low-level network functions.";
2284
2285static PyObject*
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002286PySocket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002287{
Guido van Rossuma2e48551999-09-09 15:42:59 +00002288#ifndef INADDR_NONE
2289#define INADDR_NONE (-1)
2290#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002291
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002292 /* Have to use inet_addr() instead */
2293 char *ip_addr;
Fred Drakee5065292001-07-19 21:16:41 +00002294 unsigned long packed_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002295
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002296 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) {
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002297 return NULL;
2298 }
Guido van Rossumff3ab422000-04-24 15:16:03 +00002299#ifdef USE_GUSI1
Fred Drakee5065292001-07-19 21:16:41 +00002300 packed_addr = inet_addr(ip_addr).s_addr;
Guido van Rossum1b6e4631999-11-04 18:22:29 +00002301#else
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002302 packed_addr = inet_addr(ip_addr);
Guido van Rossum1b6e4631999-11-04 18:22:29 +00002303#endif
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002304
2305 if (packed_addr == INADDR_NONE) { /* invalid address */
2306 PyErr_SetString(PySocket_Error,
2307 "illegal IP address string passed to inet_aton");
2308 return NULL;
2309 }
2310
2311 return PyString_FromStringAndSize((char *) &packed_addr,
2312 sizeof(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002313}
2314
Guido van Rossum48a680c2001-03-02 06:34:14 +00002315static char inet_ntoa_doc[] =
Fred Drakee0661342000-03-07 14:05:16 +00002316"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002317\n\
2318Convert an IP address from 32-bit packed binary format to string format";
2319
2320static PyObject*
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002321PySocket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002322{
2323 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002324 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002325 struct in_addr packed_addr;
2326
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002327 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002328 return NULL;
2329 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00002330
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002331 if (addr_len != sizeof(packed_addr)) {
2332 PyErr_SetString(PySocket_Error,
2333 "packed IP wrong length for inet_ntoa");
2334 return NULL;
2335 }
2336
2337 memcpy(&packed_addr, packed_str, addr_len);
2338
2339 return PyString_FromString(inet_ntoa(packed_addr));
2340}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002341
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002342/* Python interface to getaddrinfo(host, port). */
2343
2344/*ARGSUSED*/
2345static PyObject *
2346PySocket_getaddrinfo(PyObject *self, PyObject *args)
2347{
2348 struct addrinfo hints, *res0, *res;
2349 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00002350 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002351 char *hptr, *pptr;
2352 int family, socktype, protocol, flags;
2353 int error;
2354 PyObject *all = (PyObject *)NULL;
2355 PyObject *single = (PyObject *)NULL;
2356
2357 family = socktype = protocol = flags = 0;
2358 family = PF_UNSPEC;
2359 if (!PyArg_ParseTuple(args, "zO|iiii:getaddrinfo",
2360 &hptr, &pobj, &family, &socktype,
2361 &protocol, &flags)) {
2362 return NULL;
2363 }
2364 if (PyInt_Check(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00002365 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002366 pptr = pbuf;
2367 } else if (PyString_Check(pobj)) {
2368 pptr = PyString_AsString(pobj);
2369 } else if (pobj == Py_None) {
2370 pptr = (char *)NULL;
2371 } else {
2372 PyErr_SetString(PySocket_Error, "Int or String expected");
2373 return NULL;
2374 }
2375 memset(&hints, 0, sizeof(hints));
2376 hints.ai_family = family;
2377 hints.ai_socktype = socktype;
2378 hints.ai_protocol = protocol;
2379 hints.ai_flags = flags;
2380 error = getaddrinfo(hptr, pptr, &hints, &res0);
2381 if (error) {
2382 PyGAI_Err(error);
2383 return NULL;
2384 }
2385
2386 if ((all = PyList_New(0)) == NULL)
2387 goto err;
2388 for (res = res0; res; res = res->ai_next) {
2389 single = Py_BuildValue("iiisO", res->ai_family,
2390 res->ai_socktype, res->ai_protocol,
2391 res->ai_canonname ? res->ai_canonname : "",
2392 makesockaddr(-1, res->ai_addr, res->ai_addrlen));
2393 if (single == NULL)
2394 goto err;
2395
2396 if (PyList_Append(all, single))
2397 goto err;
2398 Py_XDECREF(single);
2399 }
2400 Py_XDECREF(pobj);
2401 return all;
2402 err:
2403 Py_XDECREF(single);
2404 Py_XDECREF(all);
2405 Py_XDECREF(pobj);
2406 return (PyObject *)NULL;
2407}
2408
2409static char getaddrinfo_doc[] =
2410"socket.getaddrinfo(host, port [, family, socktype, proto, flags])\n\
2411 --> List of (family, socktype, proto, canonname, sockaddr)\n\
2412\n\
2413Resolve host and port into addrinfo struct.";
2414
2415/* Python interface to getnameinfo(sa, flags). */
2416
2417/*ARGSUSED*/
2418static PyObject *
2419PySocket_getnameinfo(PyObject *self, PyObject *args)
2420{
2421 PyObject *sa = (PyObject *)NULL;
2422 int flags;
2423 char *hostp;
2424 int n, port, flowinfo, scope_id;
2425 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
2426 struct addrinfo hints, *res = NULL;
2427 int error;
2428 PyObject *ret = (PyObject *)NULL;
2429
2430 flags = flowinfo = scope_id = 0;
2431 if (PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags) == 0)
2432 return NULL;
2433 n = PyArg_ParseTuple(sa, "si|ii", &hostp, &port, &flowinfo, scope_id);
2434 if (n == 0)
2435 goto fail;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00002436 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002437 memset(&hints, 0, sizeof(hints));
2438 hints.ai_family = PF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00002439 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002440 error = getaddrinfo(hostp, pbuf, &hints, &res);
2441 if (error) {
2442 PyGAI_Err(error);
2443 goto fail;
2444 }
2445 if (res->ai_next) {
2446 PyErr_SetString(PySocket_Error,
2447 "sockaddr resolved to multiple addresses");
2448 goto fail;
2449 }
2450 switch (res->ai_family) {
2451 case AF_INET:
2452 {
2453 char *t1;
2454 int t2;
2455 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
2456 PyErr_SetString(PySocket_Error,
2457 "IPv4 sockaddr must be 2 tuple");
2458 goto fail;
2459 }
2460 break;
2461 }
2462#ifdef INET6
2463 case AF_INET6:
2464 {
2465 struct sockaddr_in6 *sin6;
2466 sin6 = (struct sockaddr_in6 *)res->ai_addr;
2467 sin6->sin6_flowinfo = flowinfo;
2468 sin6->sin6_scope_id = scope_id;
2469 break;
2470 }
2471#endif
2472 }
2473 error = getnameinfo(res->ai_addr, res->ai_addrlen,
2474 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
2475 if (error) {
2476 PyGAI_Err(error);
2477 goto fail;
2478 }
2479 ret = Py_BuildValue("ss", hbuf, pbuf);
2480
2481fail:
2482 if (res)
2483 freeaddrinfo(res);
2484 Py_XDECREF(sa);
2485 return ret;
2486}
2487
2488static char getnameinfo_doc[] =
2489"socket.getnameinfo(sockaddr, flags) --> (host, port)\n\
2490\n\
2491Get host and port for a sockaddr.";
2492
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002493/* XXX It might be helpful to augment the error message generated
2494 below with the name of the SSL function that generated the error.
2495 I expect it's obvious most of the time.
2496*/
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002497
2498#ifdef USE_SSL
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002499static PyObject *
2500PySSL_SetError(SSL *ssl, int ret)
2501{
2502 PyObject *v, *n, *s;
2503 char *errstr;
2504 int err;
2505
2506 assert(ret <= 0);
2507
2508 err = SSL_get_error(ssl, ret);
2509 n = PyInt_FromLong(err);
2510 if (n == NULL)
2511 return NULL;
2512 v = PyTuple_New(2);
2513 if (v == NULL) {
2514 Py_DECREF(n);
2515 return NULL;
2516 }
2517
2518 switch (SSL_get_error(ssl, ret)) {
2519 case SSL_ERROR_ZERO_RETURN:
2520 errstr = "TLS/SSL connection has been closed";
2521 break;
2522 case SSL_ERROR_WANT_READ:
2523 errstr = "The operation did not complete (read)";
2524 break;
2525 case SSL_ERROR_WANT_WRITE:
2526 errstr = "The operation did not complete (write)";
2527 break;
2528 case SSL_ERROR_WANT_X509_LOOKUP:
2529 errstr = "The operation did not complete (X509 lookup)";
2530 break;
2531 case SSL_ERROR_SYSCALL:
2532 case SSL_ERROR_SSL:
2533 {
2534 unsigned long e = ERR_get_error();
2535 if (e == 0) {
2536 /* an EOF was observed that violates the protocol */
2537 errstr = "EOF occurred in violation of protocol";
2538 } else if (e == -1) {
2539 /* the underlying BIO reported an I/O error */
2540 Py_DECREF(v);
2541 Py_DECREF(n);
2542 PyErr_SetFromErrno(PyExc_IOError);
2543 return NULL;
2544 } else {
2545 /* XXX Protected by global interpreter lock */
2546 errstr = ERR_error_string(e, NULL);
2547 }
2548 break;
2549 }
2550 default:
2551 errstr = "Invalid error code";
2552 }
2553 s = PyString_FromString(errstr);
2554 if (s == NULL) {
2555 Py_DECREF(v);
2556 Py_DECREF(n);
2557 }
2558 PyTuple_SET_ITEM(v, 0, n);
2559 PyTuple_SET_ITEM(v, 1, s);
2560 PyErr_SetObject(PySSLErrorObject, v);
2561 return NULL;
2562}
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002563
2564/* This is a C function to be called for new object initialization */
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002565static PySSLObject *
2566newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002567{
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002568 PySSLObject *self;
Jeremy Hyltonb0b0bd62001-10-10 22:33:32 +00002569 char *errstr = NULL;
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002570 int ret;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002571
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002572 self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002573 if (self == NULL){
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002574 errstr = "newPySSLObject error";
Jeremy Hyltonf86d63e2001-10-10 03:19:39 +00002575 goto fail;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002576 }
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002577 memset(self->server, '\0', sizeof(char) * 256);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002578 memset(self->issuer, '\0', sizeof(char) * 256);
Jeremy Hyltonec4b5452001-10-10 03:37:05 +00002579 self->server_cert = NULL;
2580 self->ssl = NULL;
2581 self->ctx = NULL;
2582 self->Socket = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002583
Jeremy Hylton22738b92001-10-10 22:37:48 +00002584 if ((key_file && !cert_file) || (!key_file && cert_file)) {
2585 errstr = "Both the key & certificate files must be specified";
Jeremy Hyltonf86d63e2001-10-10 03:19:39 +00002586 goto fail;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002587 }
2588
Jeremy Hylton22738b92001-10-10 22:37:48 +00002589 self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */
2590 if (self->ctx == NULL) {
2591 errstr = "SSL_CTX_new error";
Jeremy Hyltonf86d63e2001-10-10 03:19:39 +00002592 goto fail;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002593 }
2594
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002595 if (key_file) {
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002596 if (SSL_CTX_use_PrivateKey_file(self->ctx, key_file,
Jeremy Hyltonf86d63e2001-10-10 03:19:39 +00002597 SSL_FILETYPE_PEM) < 1) {
Jeremy Hyltonb0b0bd62001-10-10 22:33:32 +00002598 errstr = "SSL_CTX_use_PrivateKey_file error";
Jeremy Hyltonf86d63e2001-10-10 03:19:39 +00002599 goto fail;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002600 }
2601
2602 if (SSL_CTX_use_certificate_chain_file(self->ctx,
Jeremy Hyltonf86d63e2001-10-10 03:19:39 +00002603 cert_file) < 1) {
Jeremy Hyltonb0b0bd62001-10-10 22:33:32 +00002604 errstr = "SSL_CTX_use_certificate_chain_file error";
Jeremy Hyltonf86d63e2001-10-10 03:19:39 +00002605 goto fail;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002606 }
2607 }
2608
2609 SSL_CTX_set_verify(self->ctx,
2610 SSL_VERIFY_NONE, NULL); /* set verify lvl */
2611 self->ssl = SSL_new(self->ctx); /* New ssl struct */
2612 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */
2613 SSL_set_connect_state(self->ssl);
2614
Jeremy Hyltonf86d63e2001-10-10 03:19:39 +00002615 /* Actually negotiate SSL connection */
2616 /* XXX If SSL_connect() returns 0, it's also a failure. */
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002617 ret = SSL_connect(self->ssl);
2618 if (ret <= 0) {
2619 PySSL_SetError(self->ssl, ret);
Jeremy Hyltonf86d63e2001-10-10 03:19:39 +00002620 goto fail;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002621 }
2622 self->ssl->debug = 1;
2623
2624 if ((self->server_cert = SSL_get_peer_certificate(self->ssl))) {
2625 X509_NAME_oneline(X509_get_subject_name(self->server_cert),
2626 self->server, 256);
2627 X509_NAME_oneline(X509_get_issuer_name(self->server_cert),
2628 self->issuer, 256);
2629 }
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002630 self->Socket = Sock;
2631 Py_INCREF(self->Socket);
2632 return self;
Jeremy Hyltonf86d63e2001-10-10 03:19:39 +00002633 fail:
Jeremy Hyltonb0b0bd62001-10-10 22:33:32 +00002634 if (errstr)
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002635 PyErr_SetString(PySSLErrorObject, errstr);
Jeremy Hyltonf86d63e2001-10-10 03:19:39 +00002636 Py_DECREF(self);
2637 return NULL;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002638}
2639
2640/* This is the Python function called for new object initialization */
2641static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002642PySocket_ssl(PyObject *self, PyObject *args)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002643{
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002644 PySSLObject *rv;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002645 PySocketSockObject *Sock;
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002646 char *key_file = NULL;
2647 char *cert_file = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002648
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002649 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002650 &PySocketSock_Type, (PyObject*)&Sock,
Jeremy Hyltonab006452001-10-10 03:33:24 +00002651 &key_file, &cert_file))
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002652 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002653
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002654 rv = newPySSLObject(Sock, key_file, cert_file);
Jeremy Hyltonab006452001-10-10 03:33:24 +00002655 if (rv == NULL)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002656 return NULL;
2657 return (PyObject *)rv;
2658}
2659
2660static char ssl_doc[] =
2661"ssl(socket, keyfile, certfile) -> sslobject";
2662
2663static PyObject *
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002664PySSL_server(PySSLObject *self, PyObject *args)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002665{
2666 return PyString_FromString(self->server);
2667}
2668
2669static PyObject *
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002670PySSL_issuer(PySSLObject *self, PyObject *args)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002671{
2672 return PyString_FromString(self->issuer);
2673}
2674
2675
2676/* SSL object methods */
2677
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002678static PyMethodDef PySSLMethods[] = {
2679 {"write", (PyCFunction)PySSL_SSLwrite, 1},
2680 {"read", (PyCFunction)PySSL_SSLread, 1},
2681 {"server", (PyCFunction)PySSL_server, 1},
2682 {"issuer", (PyCFunction)PySSL_issuer, 1},
Jeremy Hyltonab006452001-10-10 03:33:24 +00002683 {NULL, NULL}
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002684};
2685
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002686static void PySSL_dealloc(PySSLObject *self)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002687{
2688 if (self->server_cert) /* Possible not to have one? */
2689 X509_free (self->server_cert);
Jeremy Hyltonec4b5452001-10-10 03:37:05 +00002690 if (self->ssl)
2691 SSL_free(self->ssl);
2692 if (self->ctx)
2693 SSL_CTX_free(self->ctx);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002694 Py_XDECREF(self->Socket);
Guido van Rossumb18618d2000-05-03 23:44:39 +00002695 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002696}
2697
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002698static PyObject *PySSL_getattr(PySSLObject *self, char *name)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002699{
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002700 return Py_FindMethod(PySSLMethods, (PyObject *)self, name);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002701}
2702
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002703staticforward PyTypeObject PySSL_Type = {
Guido van Rossuma120ffc2001-01-22 15:29:14 +00002704 PyObject_HEAD_INIT(NULL)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002705 0, /*ob_size*/
2706 "SSL", /*tp_name*/
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002707 sizeof(PySSLObject), /*tp_basicsize*/
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002708 0, /*tp_itemsize*/
2709 /* methods */
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002710 (destructor)PySSL_dealloc, /*tp_dealloc*/
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002711 0, /*tp_print*/
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002712 (getattrfunc)PySSL_getattr, /*tp_getattr*/
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002713 0, /*tp_setattr*/
2714 0, /*tp_compare*/
2715 0, /*tp_repr*/
2716 0, /*tp_as_number*/
2717 0, /*tp_as_sequence*/
2718 0, /*tp_as_mapping*/
2719 0, /*tp_hash*/
2720};
2721
2722
2723
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002724static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002725{
2726 char *data;
Andrew M. Kuchlingb38175e2001-02-07 20:41:17 +00002727 size_t len;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002728
Andrew M. Kuchlingb38175e2001-02-07 20:41:17 +00002729 if (!PyArg_ParseTuple(args, "s#:write", &data, &len))
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002730 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002731
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002732 len = SSL_write(self->ssl, data, len);
2733 return PyInt_FromLong((long)len);
2734}
2735
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002736static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002737{
2738 PyObject *buf;
2739 int count = 0;
2740 int len = 1024;
2741 int res;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002742
Guido van Rossum43713e52000-02-29 13:59:29 +00002743 PyArg_ParseTuple(args, "|i:read", &len);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002744
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002745 if (!(buf = PyString_FromStringAndSize((char *) 0, len)))
2746 return NULL; /* Error object should already be set */
Guido van Rossum48a680c2001-03-02 06:34:14 +00002747
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002748 count = SSL_read(self->ssl, PyString_AsString(buf), len);
2749 res = SSL_get_error(self->ssl, count);
2750
2751 switch (res) {
Jeremy Hylton42dd01a2001-02-01 23:35:20 +00002752 case SSL_ERROR_NONE:
2753 assert(count > 0);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002754 break;
Jeremy Hylton42dd01a2001-02-01 23:35:20 +00002755 case SSL_ERROR_ZERO_RETURN: /* normal EOF */
2756 assert(count == 0);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002757 break;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002758 default:
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002759 return PyErr_SetFromErrno(PySSLErrorObject);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002760 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00002761
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002762 fflush(stderr);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002763
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002764 if (count < 0) {
2765 Py_DECREF(buf);
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002766 return PyErr_SetFromErrno(PySSLErrorObject);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002767 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00002768
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002769 if (count != len && _PyString_Resize(&buf, count) < 0)
2770 return NULL;
2771 return buf;
2772}
2773
2774#endif /* USE_SSL */
2775
2776
Guido van Rossum30a685f1991-06-27 15:51:29 +00002777/* List of functions exported by this module. */
2778
Guido van Rossum73624e91994-10-10 17:59:00 +00002779static PyMethodDef PySocket_methods[] = {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002780 {"gethostbyname", PySocket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002781 METH_VARARGS, gethostbyname_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002782 {"gethostbyname_ex", PySocket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002783 METH_VARARGS, ghbn_ex_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002784 {"gethostbyaddr", PySocket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002785 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002786 {"gethostname", PySocket_gethostname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002787 METH_VARARGS, gethostname_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002788 {"getservbyname", PySocket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002789 METH_VARARGS, getservbyname_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002790 {"getprotobyname", PySocket_getprotobyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002791 METH_VARARGS,getprotobyname_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002792 {"socket", PySocket_socket,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002793 METH_VARARGS, socket_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002794#ifndef NO_DUP
Guido van Rossum48a680c2001-03-02 06:34:14 +00002795 {"fromfd", PySocket_fromfd,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002796 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002797#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00002798 {"ntohs", PySocket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002799 METH_VARARGS, ntohs_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002800 {"ntohl", PySocket_ntohl,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002801 METH_VARARGS, ntohl_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002802 {"htons", PySocket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002803 METH_VARARGS, htons_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002804 {"htonl", PySocket_htonl,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002805 METH_VARARGS, htonl_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002806 {"inet_aton", PySocket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002807 METH_VARARGS, inet_aton_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002808 {"inet_ntoa", PySocket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002809 METH_VARARGS, inet_ntoa_doc},
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002810 {"getaddrinfo", PySocket_getaddrinfo,
2811 METH_VARARGS, getaddrinfo_doc},
2812 {"getnameinfo", PySocket_getnameinfo,
2813 METH_VARARGS, getnameinfo_doc},
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002814#ifdef USE_SSL
Guido van Rossum48a680c2001-03-02 06:34:14 +00002815 {"ssl", PySocket_ssl,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002816 METH_VARARGS, ssl_doc},
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002817#endif /* USE_SSL */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002818 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002819};
2820
Guido van Rossum30a685f1991-06-27 15:51:29 +00002821
2822/* Convenience routine to export an integer value.
Barry Warsaw752300b1997-01-03 17:18:10 +00002823 *
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002824 * Errors are silently ignored, for better or for worse...
Barry Warsaw752300b1997-01-03 17:18:10 +00002825 */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002826static void
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002827insint(PyObject *d, char *name, int value)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002828{
Guido van Rossum73624e91994-10-10 17:59:00 +00002829 PyObject *v = PyInt_FromLong((long) value);
Barry Warsaw752300b1997-01-03 17:18:10 +00002830 if (!v || PyDict_SetItemString(d, name, v))
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002831 PyErr_Clear();
Barry Warsaw752300b1997-01-03 17:18:10 +00002832
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002833 Py_XDECREF(v);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002834}
2835
Guido van Rossum30a685f1991-06-27 15:51:29 +00002836
Guido van Rossum8d665e61996-06-26 18:22:49 +00002837#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00002838
2839/* Additional initialization and cleanup for NT/Windows */
2840
2841static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002842NTcleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002843{
2844 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00002845}
2846
2847static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002848NTinit(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002849{
2850 WSADATA WSAData;
2851 int ret;
2852 char buf[100];
2853 ret = WSAStartup(0x0101, &WSAData);
2854 switch (ret) {
2855 case 0: /* no error */
2856 atexit(NTcleanup);
2857 return 1;
2858 case WSASYSNOTREADY:
2859 PyErr_SetString(PyExc_ImportError,
2860 "WSAStartup failed: network not ready");
2861 break;
2862 case WSAVERNOTSUPPORTED:
2863 case WSAEINVAL:
2864 PyErr_SetString(PyExc_ImportError,
2865 "WSAStartup failed: requested version not supported");
2866 break;
2867 default:
2868 sprintf(buf, "WSAStartup failed: error code %d", ret);
2869 PyErr_SetString(PyExc_ImportError, buf);
2870 break;
2871 }
2872 return 0;
2873}
2874
Guido van Rossum8d665e61996-06-26 18:22:49 +00002875#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002876
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002877#if defined(PYOS_OS2)
2878
2879/* Additional initialization and cleanup for OS/2 */
2880
2881static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002882OS2cleanup(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002883{
2884 /* No cleanup is necessary for OS/2 Sockets */
2885}
2886
2887static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002888OS2init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002889{
2890 char reason[64];
2891 int rc = sock_init();
2892
2893 if (rc == 0) {
Guido van Rossum32c575d1997-12-02 20:37:32 +00002894 atexit(OS2cleanup);
2895 return 1; /* Indicate Success */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002896 }
2897
2898 sprintf(reason, "OS/2 TCP/IP Error# %d", sock_errno());
2899 PyErr_SetString(PyExc_ImportError, reason);
2900
Guido van Rossum32c575d1997-12-02 20:37:32 +00002901 return 0; /* Indicate Failure */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002902}
2903
2904#endif /* PYOS_OS2 */
2905
Guido van Rossum30a685f1991-06-27 15:51:29 +00002906/* Initialize this module.
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002907 * This is called when the first 'import socket' is done,
2908 * via a table in config.c, if config.c is compiled with USE_SOCKET
2909 * defined.
2910 *
2911 * For MS_WINDOWS (which means any Windows variant), this module
2912 * is actually called "_socket", and there's a wrapper "socket.py"
2913 * which implements some missing functionality (such as makefile(),
2914 * dup() and fromfd()). The import of "_socket" may fail with an
2915 * ImportError exception if initialization of WINSOCK fails. When
2916 * WINSOCK is initialized succesfully, a call to WSACleanup() is
2917 * scheduled to be made at exit time.
2918 *
2919 * For OS/2, this module is also called "_socket" and uses a wrapper
2920 * "socket.py" which implements that functionality that is missing
2921 * when PC operating systems don't put socket descriptors in the
2922 * operating system's filesystem layer.
2923 */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002924
Guido van Rossum82a5c661998-07-07 20:45:43 +00002925static char module_doc[] =
Fred Drakea136d492000-08-16 14:18:30 +00002926"Implementation module for socket operations. See the socket module\n\
2927for documentation.";
Guido van Rossum82a5c661998-07-07 20:45:43 +00002928
2929static char sockettype_doc[] =
2930"A socket represents one endpoint of a network connection.\n\
2931\n\
2932Methods:\n\
2933\n\
2934accept() -- accept a connection, returning new socket and client address\n\
2935bind() -- bind the socket to a local address\n\
2936close() -- close the socket\n\
2937connect() -- connect the socket to a remote address\n\
2938connect_ex() -- connect, return an error code instead of an exception \n\
2939dup() -- return a new socket object identical to the current one (*)\n\
2940fileno() -- return underlying file descriptor\n\
2941getpeername() -- return remote address (*)\n\
2942getsockname() -- return local address\n\
2943getsockopt() -- get socket options\n\
2944listen() -- start listening for incoming connections\n\
2945makefile() -- return a file object corresponding tot the socket (*)\n\
2946recv() -- receive data\n\
2947recvfrom() -- receive data and sender's address\n\
2948send() -- send data\n\
2949sendto() -- send data to a given address\n\
2950setblocking() -- set or clear the blocking I/O flag\n\
2951setsockopt() -- set socket options\n\
2952shutdown() -- shut down traffic in one or both directions\n\
2953\n\
2954(*) not available on all platforms!)";
2955
Guido van Rossum3886bb61998-12-04 18:50:17 +00002956DL_EXPORT(void)
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00002957init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002958{
Guido van Rossum73624e91994-10-10 17:59:00 +00002959 PyObject *m, *d;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002960#ifdef RISCOS
2961 _kernel_swi_regs r;
2962 r.r[0]=0;
2963 _kernel_swi(0x43380, &r, &r);
2964 taskwindow = r.r[0];
2965#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002966#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00002967 if (!NTinit())
2968 return;
Guido van Rossumbe32c891996-06-20 16:25:29 +00002969#else
Guido van Rossum32c575d1997-12-02 20:37:32 +00002970#if defined(__TOS_OS2__)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002971 if (!OS2init())
2972 return;
Fred Drakea136d492000-08-16 14:18:30 +00002973#endif /* __TOS_OS2__ */
2974#endif /* MS_WINDOWS */
Guido van Rossum48a680c2001-03-02 06:34:14 +00002975#endif /* RISCOS */
Guido van Rossuma120ffc2001-01-22 15:29:14 +00002976#ifdef USE_SSL
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002977 PySSL_Type.ob_type = &PyType_Type;
Guido van Rossuma120ffc2001-01-22 15:29:14 +00002978#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00002979 m = Py_InitModule3("_socket", PySocket_methods, module_doc);
Guido van Rossum73624e91994-10-10 17:59:00 +00002980 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002981 PySocket_Error = PyErr_NewException("socket.error", NULL, NULL);
2982 if (PySocket_Error == NULL)
2983 return;
Martin v. Löwis864e9ff2001-08-04 22:32:03 +00002984 PyDict_SetItemString(d, "error", PySocket_Error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002985 PyH_Error = PyErr_NewException("socket.herror", PySocket_Error, NULL);
2986 if (PyH_Error == NULL)
2987 return;
Martin v. Löwis864e9ff2001-08-04 22:32:03 +00002988 PyDict_SetItemString(d, "herror", PyH_Error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002989 PyGAI_Error = PyErr_NewException("socket.gaierror", PySocket_Error,
2990 NULL);
2991 if (PyGAI_Error == NULL)
2992 return;
Martin v. Löwis864e9ff2001-08-04 22:32:03 +00002993 PyDict_SetItemString(d, "gaierror", PyGAI_Error);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002994#ifdef USE_SSL
2995 SSL_load_error_strings();
2996 SSLeay_add_ssl_algorithms();
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00002997 PySSLErrorObject = PyErr_NewException("socket.sslerror", NULL, NULL);
2998 if (PySSLErrorObject == NULL)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002999 return;
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00003000 PyDict_SetItemString(d, "sslerror", PySSLErrorObject);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00003001 if (PyDict_SetItemString(d, "SSLType",
Jeremy Hylton5b6ce5a2001-10-10 23:55:43 +00003002 (PyObject *)&PySSL_Type) != 0)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00003003 return;
3004#endif /* USE_SSL */
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00003005 PySocketSock_Type.ob_type = &PyType_Type;
Guido van Rossum82a5c661998-07-07 20:45:43 +00003006 PySocketSock_Type.tp_doc = sockettype_doc;
Guido van Rossum91ba64d1997-06-02 22:18:09 +00003007 if (PyDict_SetItemString(d, "SocketType",
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00003008 (PyObject *)&PySocketSock_Type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003009 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00003010
3011 /* Address families (we only support AF_INET and AF_UNIX) */
3012#ifdef AF_UNSPEC
3013 insint(d, "AF_UNSPEC", AF_UNSPEC);
3014#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003015 insint(d, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003016#ifdef AF_INET6
3017 insint(d, "AF_INET6", AF_INET6);
3018#endif /* AF_INET6 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003019#ifdef AF_UNIX
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003020 insint(d, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003021#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003022#ifdef AF_AX25
3023 insint(d, "AF_AX25", AF_AX25); /* Amateur Radio AX.25 */
3024#endif
3025#ifdef AF_IPX
3026 insint(d, "AF_IPX", AF_IPX); /* Novell IPX */
3027#endif
3028#ifdef AF_APPLETALK
3029 insint(d, "AF_APPLETALK", AF_APPLETALK); /* Appletalk DDP */
3030#endif
3031#ifdef AF_NETROM
3032 insint(d, "AF_NETROM", AF_NETROM); /* Amateur radio NetROM */
3033#endif
3034#ifdef AF_BRIDGE
3035 insint(d, "AF_BRIDGE", AF_BRIDGE); /* Multiprotocol bridge */
3036#endif
3037#ifdef AF_AAL5
3038 insint(d, "AF_AAL5", AF_AAL5); /* Reserved for Werner's ATM */
3039#endif
3040#ifdef AF_X25
3041 insint(d, "AF_X25", AF_X25); /* Reserved for X.25 project */
3042#endif
3043#ifdef AF_INET6
3044 insint(d, "AF_INET6", AF_INET6); /* IP version 6 */
3045#endif
3046#ifdef AF_ROSE
3047 insint(d, "AF_ROSE", AF_ROSE); /* Amateur Radio X.25 PLP */
3048#endif
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00003049#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00003050 insint(d, "AF_PACKET", AF_PACKET);
3051 insint(d, "PF_PACKET", PF_PACKET);
3052 insint(d, "PACKET_HOST", PACKET_HOST);
3053 insint(d, "PACKET_BROADCAST", PACKET_BROADCAST);
3054 insint(d, "PACKET_MULTICAST", PACKET_MULTICAST);
3055 insint(d, "PACKET_OTHERHOST", PACKET_OTHERHOST);
3056 insint(d, "PACKET_OUTGOING", PACKET_OUTGOING);
3057 insint(d, "PACKET_LOOPBACK", PACKET_LOOPBACK);
3058 insint(d, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00003059#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003060
3061 /* Socket types */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003062 insint(d, "SOCK_STREAM", SOCK_STREAM);
3063 insint(d, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003064#ifndef __BEOS__
3065/* We have incomplete socket support. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003066 insint(d, "SOCK_RAW", SOCK_RAW);
3067 insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET);
3068 insint(d, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003069#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003070
3071#ifdef SO_DEBUG
3072 insint(d, "SO_DEBUG", SO_DEBUG);
3073#endif
3074#ifdef SO_ACCEPTCONN
3075 insint(d, "SO_ACCEPTCONN", SO_ACCEPTCONN);
3076#endif
3077#ifdef SO_REUSEADDR
3078 insint(d, "SO_REUSEADDR", SO_REUSEADDR);
3079#endif
3080#ifdef SO_KEEPALIVE
3081 insint(d, "SO_KEEPALIVE", SO_KEEPALIVE);
3082#endif
3083#ifdef SO_DONTROUTE
3084 insint(d, "SO_DONTROUTE", SO_DONTROUTE);
3085#endif
3086#ifdef SO_BROADCAST
3087 insint(d, "SO_BROADCAST", SO_BROADCAST);
3088#endif
3089#ifdef SO_USELOOPBACK
3090 insint(d, "SO_USELOOPBACK", SO_USELOOPBACK);
3091#endif
3092#ifdef SO_LINGER
3093 insint(d, "SO_LINGER", SO_LINGER);
3094#endif
3095#ifdef SO_OOBINLINE
3096 insint(d, "SO_OOBINLINE", SO_OOBINLINE);
3097#endif
3098#ifdef SO_REUSEPORT
3099 insint(d, "SO_REUSEPORT", SO_REUSEPORT);
3100#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003101#ifdef SO_SNDBUF
3102 insint(d, "SO_SNDBUF", SO_SNDBUF);
3103#endif
3104#ifdef SO_RCVBUF
3105 insint(d, "SO_RCVBUF", SO_RCVBUF);
3106#endif
3107#ifdef SO_SNDLOWAT
3108 insint(d, "SO_SNDLOWAT", SO_SNDLOWAT);
3109#endif
3110#ifdef SO_RCVLOWAT
3111 insint(d, "SO_RCVLOWAT", SO_RCVLOWAT);
3112#endif
3113#ifdef SO_SNDTIMEO
3114 insint(d, "SO_SNDTIMEO", SO_SNDTIMEO);
3115#endif
3116#ifdef SO_RCVTIMEO
3117 insint(d, "SO_RCVTIMEO", SO_RCVTIMEO);
3118#endif
3119#ifdef SO_ERROR
3120 insint(d, "SO_ERROR", SO_ERROR);
3121#endif
3122#ifdef SO_TYPE
3123 insint(d, "SO_TYPE", SO_TYPE);
3124#endif
3125
3126 /* Maximum number of connections for "listen" */
3127#ifdef SOMAXCONN
3128 insint(d, "SOMAXCONN", SOMAXCONN);
3129#else
3130 insint(d, "SOMAXCONN", 5); /* Common value */
3131#endif
3132
3133 /* Flags for send, recv */
3134#ifdef MSG_OOB
3135 insint(d, "MSG_OOB", MSG_OOB);
3136#endif
3137#ifdef MSG_PEEK
3138 insint(d, "MSG_PEEK", MSG_PEEK);
3139#endif
3140#ifdef MSG_DONTROUTE
3141 insint(d, "MSG_DONTROUTE", MSG_DONTROUTE);
3142#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003143#ifdef MSG_DONTWAIT
Guido van Rossum14f3f592000-04-25 21:53:58 +00003144 insint(d, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003145#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003146#ifdef MSG_EOR
3147 insint(d, "MSG_EOR", MSG_EOR);
3148#endif
3149#ifdef MSG_TRUNC
3150 insint(d, "MSG_TRUNC", MSG_TRUNC);
3151#endif
3152#ifdef MSG_CTRUNC
3153 insint(d, "MSG_CTRUNC", MSG_CTRUNC);
3154#endif
3155#ifdef MSG_WAITALL
3156 insint(d, "MSG_WAITALL", MSG_WAITALL);
3157#endif
3158#ifdef MSG_BTAG
3159 insint(d, "MSG_BTAG", MSG_BTAG);
3160#endif
3161#ifdef MSG_ETAG
3162 insint(d, "MSG_ETAG", MSG_ETAG);
3163#endif
3164
3165 /* Protocol level and numbers, usable for [gs]etsockopt */
3166#ifdef SOL_SOCKET
3167 insint(d, "SOL_SOCKET", SOL_SOCKET);
3168#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003169#ifdef SOL_IP
3170 insint(d, "SOL_IP", SOL_IP);
3171#else
3172 insint(d, "SOL_IP", 0);
3173#endif
3174#ifdef SOL_IPX
3175 insint(d, "SOL_IPX", SOL_IPX);
3176#endif
3177#ifdef SOL_AX25
3178 insint(d, "SOL_AX25", SOL_AX25);
3179#endif
3180#ifdef SOL_ATALK
3181 insint(d, "SOL_ATALK", SOL_ATALK);
3182#endif
3183#ifdef SOL_NETROM
3184 insint(d, "SOL_NETROM", SOL_NETROM);
3185#endif
3186#ifdef SOL_ROSE
3187 insint(d, "SOL_ROSE", SOL_ROSE);
3188#endif
3189#ifdef SOL_TCP
3190 insint(d, "SOL_TCP", SOL_TCP);
3191#else
3192 insint(d, "SOL_TCP", 6);
3193#endif
3194#ifdef SOL_UDP
3195 insint(d, "SOL_UDP", SOL_UDP);
3196#else
3197 insint(d, "SOL_UDP", 17);
3198#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003199#ifdef IPPROTO_IP
3200 insint(d, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00003201#else
3202 insint(d, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003203#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003204#ifdef IPPROTO_HOPOPTS
3205 insint(d, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
3206#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003207#ifdef IPPROTO_ICMP
3208 insint(d, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00003209#else
3210 insint(d, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003211#endif
3212#ifdef IPPROTO_IGMP
3213 insint(d, "IPPROTO_IGMP", IPPROTO_IGMP);
3214#endif
3215#ifdef IPPROTO_GGP
3216 insint(d, "IPPROTO_GGP", IPPROTO_GGP);
3217#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003218#ifdef IPPROTO_IPV4
3219 insint(d, "IPPROTO_IPV4", IPPROTO_IPV4);
3220#endif
3221#ifdef IPPROTO_IPIP
3222 insint(d, "IPPROTO_IPIP", IPPROTO_IPIP);
3223#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003224#ifdef IPPROTO_TCP
3225 insint(d, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00003226#else
3227 insint(d, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003228#endif
3229#ifdef IPPROTO_EGP
3230 insint(d, "IPPROTO_EGP", IPPROTO_EGP);
3231#endif
3232#ifdef IPPROTO_PUP
3233 insint(d, "IPPROTO_PUP", IPPROTO_PUP);
3234#endif
3235#ifdef IPPROTO_UDP
3236 insint(d, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00003237#else
3238 insint(d, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003239#endif
3240#ifdef IPPROTO_IDP
3241 insint(d, "IPPROTO_IDP", IPPROTO_IDP);
3242#endif
3243#ifdef IPPROTO_HELLO
3244 insint(d, "IPPROTO_HELLO", IPPROTO_HELLO);
3245#endif
3246#ifdef IPPROTO_ND
3247 insint(d, "IPPROTO_ND", IPPROTO_ND);
3248#endif
3249#ifdef IPPROTO_TP
3250 insint(d, "IPPROTO_TP", IPPROTO_TP);
3251#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003252#ifdef IPPROTO_IPV6
3253 insint(d, "IPPROTO_IPV6", IPPROTO_IPV6);
3254#endif
3255#ifdef IPPROTO_ROUTING
3256 insint(d, "IPPROTO_ROUTING", IPPROTO_ROUTING);
3257#endif
3258#ifdef IPPROTO_FRAGMENT
3259 insint(d, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
3260#endif
3261#ifdef IPPROTO_RSVP
3262 insint(d, "IPPROTO_RSVP", IPPROTO_RSVP);
3263#endif
3264#ifdef IPPROTO_GRE
3265 insint(d, "IPPROTO_GRE", IPPROTO_GRE);
3266#endif
3267#ifdef IPPROTO_ESP
3268 insint(d, "IPPROTO_ESP", IPPROTO_ESP);
3269#endif
3270#ifdef IPPROTO_AH
3271 insint(d, "IPPROTO_AH", IPPROTO_AH);
3272#endif
3273#ifdef IPPROTO_MOBILE
3274 insint(d, "IPPROTO_MOBILE", IPPROTO_MOBILE);
3275#endif
3276#ifdef IPPROTO_ICMPV6
3277 insint(d, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
3278#endif
3279#ifdef IPPROTO_NONE
3280 insint(d, "IPPROTO_NONE", IPPROTO_NONE);
3281#endif
3282#ifdef IPPROTO_DSTOPTS
3283 insint(d, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
3284#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003285#ifdef IPPROTO_XTP
3286 insint(d, "IPPROTO_XTP", IPPROTO_XTP);
3287#endif
3288#ifdef IPPROTO_EON
3289 insint(d, "IPPROTO_EON", IPPROTO_EON);
3290#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003291#ifdef IPPROTO_PIM
3292 insint(d, "IPPROTO_PIM", IPPROTO_PIM);
3293#endif
3294#ifdef IPPROTO_IPCOMP
3295 insint(d, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
3296#endif
3297#ifdef IPPROTO_VRRP
3298 insint(d, "IPPROTO_VRRP", IPPROTO_VRRP);
3299#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003300#ifdef IPPROTO_BIP
3301 insint(d, "IPPROTO_BIP", IPPROTO_BIP);
3302#endif
3303/**/
3304#ifdef IPPROTO_RAW
3305 insint(d, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00003306#else
3307 insint(d, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003308#endif
3309#ifdef IPPROTO_MAX
3310 insint(d, "IPPROTO_MAX", IPPROTO_MAX);
3311#endif
3312
3313 /* Some port configuration */
3314#ifdef IPPORT_RESERVED
3315 insint(d, "IPPORT_RESERVED", IPPORT_RESERVED);
3316#else
3317 insint(d, "IPPORT_RESERVED", 1024);
3318#endif
3319#ifdef IPPORT_USERRESERVED
3320 insint(d, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
3321#else
3322 insint(d, "IPPORT_USERRESERVED", 5000);
3323#endif
3324
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003325 /* Some reserved IP v.4 addresses */
3326#ifdef INADDR_ANY
3327 insint(d, "INADDR_ANY", INADDR_ANY);
3328#else
3329 insint(d, "INADDR_ANY", 0x00000000);
3330#endif
3331#ifdef INADDR_BROADCAST
3332 insint(d, "INADDR_BROADCAST", INADDR_BROADCAST);
3333#else
3334 insint(d, "INADDR_BROADCAST", 0xffffffff);
3335#endif
3336#ifdef INADDR_LOOPBACK
3337 insint(d, "INADDR_LOOPBACK", INADDR_LOOPBACK);
3338#else
3339 insint(d, "INADDR_LOOPBACK", 0x7F000001);
3340#endif
3341#ifdef INADDR_UNSPEC_GROUP
3342 insint(d, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
3343#else
3344 insint(d, "INADDR_UNSPEC_GROUP", 0xe0000000);
3345#endif
3346#ifdef INADDR_ALLHOSTS_GROUP
3347 insint(d, "INADDR_ALLHOSTS_GROUP", INADDR_ALLHOSTS_GROUP);
3348#else
3349 insint(d, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
3350#endif
3351#ifdef INADDR_MAX_LOCAL_GROUP
3352 insint(d, "INADDR_MAX_LOCAL_GROUP", INADDR_MAX_LOCAL_GROUP);
3353#else
3354 insint(d, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
3355#endif
3356#ifdef INADDR_NONE
3357 insint(d, "INADDR_NONE", INADDR_NONE);
3358#else
3359 insint(d, "INADDR_NONE", 0xffffffff);
3360#endif
3361
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003362 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003363#ifdef IP_OPTIONS
3364 insint(d, "IP_OPTIONS", IP_OPTIONS);
3365#endif
3366#ifdef IP_HDRINCL
3367 insint(d, "IP_HDRINCL", IP_HDRINCL);
3368#endif
3369#ifdef IP_TOS
3370 insint(d, "IP_TOS", IP_TOS);
3371#endif
3372#ifdef IP_TTL
3373 insint(d, "IP_TTL", IP_TTL);
3374#endif
3375#ifdef IP_RECVOPTS
3376 insint(d, "IP_RECVOPTS", IP_RECVOPTS);
3377#endif
3378#ifdef IP_RECVRETOPTS
3379 insint(d, "IP_RECVRETOPTS", IP_RECVRETOPTS);
3380#endif
3381#ifdef IP_RECVDSTADDR
3382 insint(d, "IP_RECVDSTADDR", IP_RECVDSTADDR);
3383#endif
3384#ifdef IP_RETOPTS
3385 insint(d, "IP_RETOPTS", IP_RETOPTS);
3386#endif
3387#ifdef IP_MULTICAST_IF
3388 insint(d, "IP_MULTICAST_IF", IP_MULTICAST_IF);
3389#endif
3390#ifdef IP_MULTICAST_TTL
3391 insint(d, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
3392#endif
3393#ifdef IP_MULTICAST_LOOP
3394 insint(d, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
3395#endif
3396#ifdef IP_ADD_MEMBERSHIP
3397 insint(d, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
3398#endif
3399#ifdef IP_DROP_MEMBERSHIP
3400 insint(d, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
3401#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003402#ifdef IP_DEFAULT_MULTICAST_TTL
3403 insint(d, "IP_DEFAULT_MULTICAST_TTL", IP_DEFAULT_MULTICAST_TTL);
3404#endif
3405#ifdef IP_DEFAULT_MULTICAST_LOOP
3406 insint(d, "IP_DEFAULT_MULTICAST_LOOP", IP_DEFAULT_MULTICAST_LOOP);
3407#endif
3408#ifdef IP_MAX_MEMBERSHIPS
3409 insint(d, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
3410#endif
3411
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003412 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
3413#ifdef IPV6_JOIN_GROUP
3414 insint(d, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
3415#endif
3416#ifdef IPV6_LEAVE_GROUP
3417 insint(d, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
3418#endif
3419#ifdef IPV6_MULTICAST_HOPS
3420 insint(d, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
3421#endif
3422#ifdef IPV6_MULTICAST_IF
3423 insint(d, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
3424#endif
3425#ifdef IPV6_MULTICAST_LOOP
3426 insint(d, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
3427#endif
3428#ifdef IPV6_UNICAST_HOPS
3429 insint(d, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
3430#endif
3431
Guido van Rossum09be4091999-08-09 14:40:40 +00003432 /* TCP options */
3433#ifdef TCP_NODELAY
3434 insint(d, "TCP_NODELAY", TCP_NODELAY);
3435#endif
3436#ifdef TCP_MAXSEG
3437 insint(d, "TCP_MAXSEG", TCP_MAXSEG);
3438#endif
3439
3440 /* IPX options */
3441#ifdef IPX_TYPE
3442 insint(d, "IPX_TYPE", IPX_TYPE);
3443#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003444
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003445 /* get{addr,name}info parameters */
3446#ifdef EAI_ADDRFAMILY
3447 insint(d, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
3448#endif
3449#ifdef EAI_AGAIN
3450 insint(d, "EAI_AGAIN", EAI_AGAIN);
3451#endif
3452#ifdef EAI_BADFLAGS
3453 insint(d, "EAI_BADFLAGS", EAI_BADFLAGS);
3454#endif
3455#ifdef EAI_FAIL
3456 insint(d, "EAI_FAIL", EAI_FAIL);
3457#endif
3458#ifdef EAI_FAMILY
3459 insint(d, "EAI_FAMILY", EAI_FAMILY);
3460#endif
3461#ifdef EAI_MEMORY
3462 insint(d, "EAI_MEMORY", EAI_MEMORY);
3463#endif
3464#ifdef EAI_NODATA
3465 insint(d, "EAI_NODATA", EAI_NODATA);
3466#endif
3467#ifdef EAI_NONAME
3468 insint(d, "EAI_NONAME", EAI_NONAME);
3469#endif
3470#ifdef EAI_SERVICE
3471 insint(d, "EAI_SERVICE", EAI_SERVICE);
3472#endif
3473#ifdef EAI_SOCKTYPE
3474 insint(d, "EAI_SOCKTYPE", EAI_SOCKTYPE);
3475#endif
3476#ifdef EAI_SYSTEM
3477 insint(d, "EAI_SYSTEM", EAI_SYSTEM);
3478#endif
3479#ifdef EAI_BADHINTS
3480 insint(d, "EAI_BADHINTS", EAI_BADHINTS);
3481#endif
3482#ifdef EAI_PROTOCOL
3483 insint(d, "EAI_PROTOCOL", EAI_PROTOCOL);
3484#endif
3485#ifdef EAI_MAX
3486 insint(d, "EAI_MAX", EAI_MAX);
3487#endif
3488#ifdef AI_PASSIVE
3489 insint(d, "AI_PASSIVE", AI_PASSIVE);
3490#endif
3491#ifdef AI_CANONNAME
3492 insint(d, "AI_CANONNAME", AI_CANONNAME);
3493#endif
3494#ifdef AI_NUMERICHOST
3495 insint(d, "AI_NUMERICHOST", AI_NUMERICHOST);
3496#endif
3497#ifdef AI_MASK
3498 insint(d, "AI_MASK", AI_MASK);
3499#endif
3500#ifdef AI_ALL
3501 insint(d, "AI_ALL", AI_ALL);
3502#endif
3503#ifdef AI_V4MAPPED_CFG
3504 insint(d, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
3505#endif
3506#ifdef AI_ADDRCONFIG
3507 insint(d, "AI_ADDRCONFIG", AI_ADDRCONFIG);
3508#endif
3509#ifdef AI_V4MAPPED
3510 insint(d, "AI_V4MAPPED", AI_V4MAPPED);
3511#endif
3512#ifdef AI_DEFAULT
3513 insint(d, "AI_DEFAULT", AI_DEFAULT);
3514#endif
3515#ifdef NI_MAXHOST
3516 insint(d, "NI_MAXHOST", NI_MAXHOST);
3517#endif
3518#ifdef NI_MAXSERV
3519 insint(d, "NI_MAXSERV", NI_MAXSERV);
3520#endif
3521#ifdef NI_NOFQDN
3522 insint(d, "NI_NOFQDN", NI_NOFQDN);
3523#endif
3524#ifdef NI_NUMERICHOST
3525 insint(d, "NI_NUMERICHOST", NI_NUMERICHOST);
3526#endif
3527#ifdef NI_NAMEREQD
3528 insint(d, "NI_NAMEREQD", NI_NAMEREQD);
3529#endif
3530#ifdef NI_NUMERICSERV
3531 insint(d, "NI_NUMERICSERV", NI_NUMERICSERV);
3532#endif
3533#ifdef NI_DGRAM
3534 insint(d, "NI_DGRAM", NI_DGRAM);
3535#endif
3536
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003537 /* Initialize gethostbyname lock */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003538#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum65d5b571998-12-21 19:32:43 +00003539 gethostbyname_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003540#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003541}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003542
3543/* Simplistic emulation code for inet_pton that only works for IPv4 */
3544#ifndef HAVE_INET_PTON
Martin v. Löwisa2ca1ae2001-06-24 21:35:43 +00003545int
Martin v. Löwisc925b1532001-07-21 09:42:15 +00003546inet_pton (int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003547{
3548 if(af == AF_INET){
3549 long packed_addr;
3550#ifdef USE_GUSI1
3551 packed_addr = (long)inet_addr(src).s_addr;
3552#else
3553 packed_addr = inet_addr(src);
3554#endif
3555 if (packed_addr == INADDR_NONE)
3556 return 0;
3557 memcpy(dst, &packed_addr, 4);
3558 return 1;
3559 }
3560 /* Should set errno to EAFNOSUPPORT */
3561 return -1;
3562}
3563
Martin v. Löwisc925b1532001-07-21 09:42:15 +00003564const char *
3565inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003566{
3567 if (af == AF_INET) {
3568 struct in_addr packed_addr;
3569 if (size < 16)
3570 /* Should set errno to ENOSPC. */
3571 return NULL;
3572 memcpy(&packed_addr, src, sizeof(packed_addr));
3573 return strncpy(dst, inet_ntoa(packed_addr), size);
3574 }
3575 /* Should set errno to EAFNOSUPPORT */
3576 return NULL;
3577}
3578#endif