1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
28 This module provides an interface to Berkeley socket IPC.
32 - only AF_INET and AF_UNIX address families are supported
33 - no asynchronous I/O (but you can use select() on sockets)
34 - no read/write operations (use send/recv or makefile instead)
35 - setsockopt() and getsockopt() only support integer options
39 - socket.gethostname() --> host name (string)
40 - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
41 - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
42 - socket.getservbyname(servername, protocolname) --> port number
43 - socket.socket(family, type [, proto]) --> new socket object
44 - family and type constants from <socket.h> are accessed as socket.AF_INET etc.
45 - errors are reported as the exception socket.error
46 - an Internet socket address is a pair (hostname, port)
47 where hostname can be anything recognized by gethostbyname()
48 (including the dd.dd.dd.dd notation) and port is in host byte order
49 - where a hostname is returned, the dd.dd.dd.dd notation is used
50 - a UNIX domain socket is a string specifying the pathname
54 - s.accept() --> new socket object, sockaddr
55 - s.setsockopt(level, optname, flag) --> Py_None
56 - s.getsockopt(level, optname) --> flag
57 - s.bind(sockaddr) --> Py_None
58 - s.connect(sockaddr) --> Py_None
59 - s.getsockname() --> sockaddr
60 - s.getpeername() --> sockaddr
61 - s.listen(n) --> Py_None
62 - s.makefile(mode) --> file object
63 - s.recv(nbytes [,flags]) --> string
64 - s.recvfrom(nbytes [,flags]) --> string, sockaddr
65 - s.send(string [,flags]) --> nbytes
66 - s.sendto(string, [flags,] sockaddr) --> nbytes
67 - s.setblocking(1 | 0) --> Py_None
68 - s.shutdown(how) --> Py_None
69 - s.close() --> Py_None
70 - repr(s) --> "<socket object, fd=%d, family=%d, type=%d, protocol=%d>"
76 #include <sys/types.h>
82 #include <sys/socket.h>
83 #include <netinet/in.h>
95 #define O_NDELAY O_NONBLOCK /* For QNX only? */
99 /* Here we have some hacks to choose between K&R or ANSI style function
100 definitions. For NT to build this as an extension module (ie, DLL)
101 it must be compiled by the C++ compiler, as it takes the address of
102 a static data item exported from the main Python DLL.
105 /* seem to be a few differences in the API */
106 #define close closesocket
107 #define NO_DUP /* I wont trust passing a socket to NT's RTL!! */
108 #define FORCE_ANSI_FUNC_DEFS
111 #ifdef FORCE_ANSI_FUNC_DEFS
112 #define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \
113 fnname( arg1type arg1name )
115 #define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \
116 fnname( arg1type arg1name, arg2type arg2name )
118 #define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name ) \
119 fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name )
121 #define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name, arg4type, arg4name ) \
122 fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name, arg4type arg4name )
124 #else /* !FORCE_ANSI_FN_DEFS */
125 #define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \
129 #define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \
130 fnname( arg1name, arg2name ) \
134 #define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name ) \
135 fnname( arg1name, arg2name, arg3name ) \
140 #define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name, arg4type, arg4name ) \
141 fnname( arg1name, arg2name, arg3name, arg4name ) \
147 #endif /* !FORCE_ANSI_FN_DEFS */
149 /* Global variable holding the exception type for errors detected
150 by this module (but not argument type or memory errors, etc.). */
152 static PyObject
*PySocket_Error
;
155 /* Convenience function to raise an error according to errno
156 and return a NULL pointer from a function. */
162 if (WSAGetLastError()) {
164 v
= Py_BuildValue("(is)", WSAGetLastError(), "winsock error");
166 PyErr_SetObject(PySocket_Error
, v
);
173 return PyErr_SetFromErrno(PySocket_Error
);
177 /* The object holding a socket. It holds some extra information,
178 like the address family, which is used to decode socket address
179 arguments properly. */
183 int sock_fd
; /* Socket file descriptor */
184 int sock_family
; /* Address family, e.g., AF_INET */
185 int sock_type
; /* Socket type, e.g., SOCK_STREAM */
186 int sock_proto
; /* Protocol type, usually 0 */
188 struct sockaddr_in in
;
190 struct sockaddr_un un
;
193 } PySocketSockObject
;
196 /* A forward reference to the Socktype type object.
197 The Socktype variable contains pointers to various functions,
198 some of which call newsockobject(), which uses Socktype, so
199 there has to be a circular reference. */
201 staticforward PyTypeObject PySocketSock_Type
;
204 /* Create a new socket object.
205 This just creates the object and initializes it.
206 If the creation fails, return NULL and set an exception (implicit
209 static PySocketSockObject
*
210 BUILD_FUNC_DEF_4(PySocketSock_New
,int,fd
, int,family
, int,type
, int,proto
)
212 PySocketSockObject
*s
;
213 s
= PyObject_NEW(PySocketSockObject
, &PySocketSock_Type
);
216 s
->sock_family
= family
;
218 s
->sock_proto
= proto
;
224 /* Convert a string specifying a host name or one of a few symbolic
225 names to a numeric IP address. This usually calls gethostbyname()
226 to do the work; the names "" and "<broadcast>" are special.
227 Return the length (should always be 4 bytes), or negative if
228 an error occurred; then an exception is raised. */
231 BUILD_FUNC_DEF_2(setipaddr
, char*,name
, struct sockaddr_in
*,addr_ret
)
236 #ifdef HAVE_GETHOSTBYNAME_R
237 struct hostent hp_allocated
;
239 int buf_len
= (sizeof buf
) - 1;
241 #endif /* HAVE_GETHOSTBYNAME_R */
243 if (name
[0] == '\0') {
244 addr_ret
->sin_addr
.s_addr
= INADDR_ANY
;
247 if (name
[0] == '<' && strcmp(name
, "<broadcast>") == 0) {
248 addr_ret
->sin_addr
.s_addr
= INADDR_BROADCAST
;
251 if (sscanf(name
, "%d.%d.%d.%d%c", &d1
, &d2
, &d3
, &d4
, &ch
) == 4 &&
252 0 <= d1
&& d1
<= 255 && 0 <= d2
&& d2
<= 255 &&
253 0 <= d3
&& d3
<= 255 && 0 <= d4
&& d4
<= 255) {
254 addr_ret
->sin_addr
.s_addr
= htonl(
255 ((long) d1
<< 24) | ((long) d2
<< 16) |
256 ((long) d3
<< 8) | ((long) d4
<< 0));
259 #ifdef HAVE_GETHOSTBYNAME_R
260 Py_BEGIN_ALLOW_THREADS
261 hp
= gethostbyname_r(name
, &hp_allocated
, buf
, buf_len
, &errnop
);
263 #else /* not HAVE_GETHOSTBYNAME_R */
264 hp
= gethostbyname(name
);
265 #endif /* HAVE_GETHOSTBYNAME_R */
268 #ifdef HAVE_HSTRERROR
269 /* Let's get real error message to return */
271 PyErr_SetString(PySocket_Error
, (char *)hstrerror(h_errno
));
273 PyErr_SetString(PySocket_Error
, "host not found");
277 memcpy((char *) &addr_ret
->sin_addr
, hp
->h_addr
, hp
->h_length
);
282 /* Create a string object representing an IP address.
283 This is always a string of the form 'dd.dd.dd.dd' (with variable
287 BUILD_FUNC_DEF_1(makeipaddr
, struct sockaddr_in
*,addr
)
289 long x
= ntohl(addr
->sin_addr
.s_addr
);
291 sprintf(buf
, "%d.%d.%d.%d",
292 (int) (x
>>24) & 0xff, (int) (x
>>16) & 0xff,
293 (int) (x
>> 8) & 0xff, (int) (x
>> 0) & 0xff);
294 return PyString_FromString(buf
);
298 /* Create an object representing the given socket address,
299 suitable for passing it back to bind(), connect() etc.
300 The family field of the sockaddr structure is inspected
301 to determine what kind of address it really is. */
305 BUILD_FUNC_DEF_2(makesockaddr
,struct sockaddr
*,addr
, int,addrlen
)
308 /* No address -- may be recvfrom() from known socket */
313 switch (addr
->sa_family
) {
317 struct sockaddr_in
*a
= (struct sockaddr_in
*) addr
;
318 PyObject
*addr
= makeipaddr(a
);
319 PyObject
*ret
= Py_BuildValue("Oi", addr
, ntohs(a
->sin_port
));
327 struct sockaddr_un
*a
= (struct sockaddr_un
*) addr
;
328 return PyString_FromString(a
->sun_path
);
332 /* More cases here... */
335 PyErr_SetString(PySocket_Error
, "return unknown socket address type");
342 /* Parse a socket address argument according to the socket object's
343 address family. Return 1 if the address was in the proper format,
344 0 of not. The address is returned through addr_ret, its length
349 getsockaddrarg
,PySocketSockObject
*,s
, PyObject
*,args
, struct sockaddr
**,addr_ret
, int *,len_ret
)
351 switch (s
->sock_family
) {
356 struct sockaddr_un
* addr
;
359 addr
= (struct sockaddr_un
* )&(s
->sock_addr
).un
;
360 if (!PyArg_Parse(args
, "s#", &path
, &len
))
362 if (len
> sizeof addr
->sun_path
) {
363 PyErr_SetString(PySocket_Error
, "AF_UNIX path too long");
366 addr
->sun_family
= AF_UNIX
;
367 memcpy(addr
->sun_path
, path
, len
);
368 *addr_ret
= (struct sockaddr
*) addr
;
369 *len_ret
= len
+ sizeof addr
->sun_family
;
376 struct sockaddr_in
* addr
;
379 addr
=(struct sockaddr_in
*)&(s
->sock_addr
).in
;
380 if (!PyArg_Parse(args
, "(si)", &host
, &port
))
382 if (setipaddr(host
, addr
) < 0)
384 addr
->sin_family
= AF_INET
;
385 addr
->sin_port
= htons(port
);
386 *addr_ret
= (struct sockaddr
*) addr
;
387 *len_ret
= sizeof *addr
;
391 /* More cases here... */
394 PyErr_SetString(PySocket_Error
, "getsockaddrarg: bad family");
401 /* Get the address length according to the socket object's address family.
402 Return 1 if the family is known, 0 otherwise. The length is returned
406 BUILD_FUNC_DEF_2(getsockaddrlen
,PySocketSockObject
*,s
, int *,len_ret
)
408 switch (s
->sock_family
) {
413 *len_ret
= sizeof (struct sockaddr_un
);
420 *len_ret
= sizeof (struct sockaddr_in
);
424 /* More cases here... */
427 PyErr_SetString(PySocket_Error
, "getsockaddrarg: bad family");
434 /* s.accept() method */
437 BUILD_FUNC_DEF_2(PySocketSock_accept
,PySocketSockObject
*,s
, PyObject
*,args
)
441 PyObject
*sock
, *addr
, *res
;
442 if (!PyArg_NoArgs(args
))
444 if (!getsockaddrlen(s
, &addrlen
))
446 Py_BEGIN_ALLOW_THREADS
447 newfd
= accept(s
->sock_fd
, (struct sockaddr
*) addrbuf
, &addrlen
);
450 return PySocket_Err();
451 /* Create the new object with unspecified family,
452 to avoid calls to bind() etc. on it. */
453 sock
= (PyObject
*) PySocketSock_New(newfd
,
459 addr
= makesockaddr((struct sockaddr
*) addrbuf
, addrlen
);
460 res
= Py_BuildValue("OO", sock
, addr
);
468 /* s.allowbroadcast() method */
469 /* XXX obsolete -- will disappear in next release */
472 BUILD_FUNC_DEF_2(PySocketSock_allowbroadcast
,PySocketSockObject
*,s
, PyObject
*,args
)
476 if (!PyArg_Parse(args
, "i", &flag
))
478 res
= setsockopt(s
->sock_fd
, SOL_SOCKET
, SO_BROADCAST
,
479 (ANY
*)&flag
, sizeof flag
);
481 return PySocket_Err();
490 /* s.setblocking(1 | 0) method */
493 PySocketSock_setblocking(s
, args
)
494 PySocketSockObject
*s
;
499 if (!PyArg_GetInt(args
, &block
))
501 Py_BEGIN_ALLOW_THREADS
502 delay_flag
= fcntl (s
->sock_fd
, F_GETFL
, 0);
504 delay_flag
&= (~O_NDELAY
);
506 delay_flag
|= O_NDELAY
;
507 fcntl (s
->sock_fd
, F_SETFL
, delay_flag
);
516 /* s.setsockopt() method.
517 With an integer third argument, sets an integer option.
518 With a string third argument, sets an option from a buffer;
519 use optional built-in module 'struct' to encode the string. */
522 BUILD_FUNC_DEF_2(PySocketSock_setsockopt
,PySocketSockObject
*,s
, PyObject
*,args
)
531 if (PyArg_Parse(args
, "(iii)", &level
, &optname
, &flag
)) {
532 buf
= (char *) &flag
;
533 buflen
= sizeof flag
;
537 if (!PyArg_Parse(args
, "(iis#)", &level
, &optname
, &buf
, &buflen
))
540 res
= setsockopt(s
->sock_fd
, level
, optname
, (ANY
*)buf
, buflen
);
542 return PySocket_Err();
548 /* s.getsockopt() method.
549 With two arguments, retrieves an integer option.
550 With a third integer argument, retrieves a string buffer of that size;
551 use optional built-in module 'struct' to decode the string. */
554 BUILD_FUNC_DEF_2(PySocketSock_getsockopt
,PySocketSockObject
*,s
, PyObject
*,args
)
563 if (PyArg_Parse(args
, "(ii)", &level
, &optname
)) {
565 int flagsize
= sizeof flag
;
566 res
= getsockopt(s
->sock_fd
, level
, optname
,
567 (ANY
*)&flag
, &flagsize
);
569 return PySocket_Err();
570 return PyInt_FromLong(flag
);
573 if (!PyArg_Parse(args
, "(iii)", &level
, &optname
, &buflen
))
575 if (buflen
<= 0 || buflen
> 1024) {
576 PyErr_SetString(PySocket_Error
, "getsockopt buflen out of range");
579 buf
= PyString_FromStringAndSize((char *)NULL
, buflen
);
582 res
= getsockopt(s
->sock_fd
, level
, optname
,
583 (ANY
*)PyString_AsString(buf
), &buflen
);
586 return PySocket_Err();
588 _PyString_Resize(&buf
, buflen
);
593 /* s.bind(sockaddr) method */
596 BUILD_FUNC_DEF_2(PySocketSock_bind
,PySocketSockObject
*,s
, PyObject
*,args
)
598 struct sockaddr
*addr
;
601 if (!getsockaddrarg(s
, args
, &addr
, &addrlen
))
603 Py_BEGIN_ALLOW_THREADS
604 res
= bind(s
->sock_fd
, addr
, addrlen
);
607 return PySocket_Err();
614 Set the file descriptor to -1 so operations tried subsequently
618 BUILD_FUNC_DEF_2(PySocketSock_close
,PySocketSockObject
*,s
, PyObject
*,args
)
620 if (!PyArg_NoArgs(args
))
622 Py_BEGIN_ALLOW_THREADS
623 (void) close(s
->sock_fd
);
631 /* s.connect(sockaddr) method */
634 BUILD_FUNC_DEF_2(PySocketSock_connect
,PySocketSockObject
*,s
, PyObject
*,args
)
636 struct sockaddr
*addr
;
639 if (!getsockaddrarg(s
, args
, &addr
, &addrlen
))
641 Py_BEGIN_ALLOW_THREADS
642 res
= connect(s
->sock_fd
, addr
, addrlen
);
645 return PySocket_Err();
651 /* s.fileno() method */
654 BUILD_FUNC_DEF_2(PySocketSock_fileno
,PySocketSockObject
*,s
, PyObject
*,args
)
656 if (!PyArg_NoArgs(args
))
658 return PyInt_FromLong((long) s
->sock_fd
);
662 /* s.getsockname() method */
665 BUILD_FUNC_DEF_2(PySocketSock_getsockname
,PySocketSockObject
*,s
, PyObject
*,args
)
669 if (!PyArg_NoArgs(args
))
671 if (!getsockaddrlen(s
, &addrlen
))
673 Py_BEGIN_ALLOW_THREADS
674 res
= getsockname(s
->sock_fd
, (struct sockaddr
*) addrbuf
, &addrlen
);
677 return PySocket_Err();
678 return makesockaddr((struct sockaddr
*) addrbuf
, addrlen
);
682 #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
683 /* s.getpeername() method */
686 BUILD_FUNC_DEF_2(PySocketSock_getpeername
,PySocketSockObject
*,s
, PyObject
*,args
)
690 if (!PyArg_NoArgs(args
))
692 if (!getsockaddrlen(s
, &addrlen
))
694 Py_BEGIN_ALLOW_THREADS
695 res
= getpeername(s
->sock_fd
, (struct sockaddr
*) addrbuf
, &addrlen
);
698 return PySocket_Err();
699 return makesockaddr((struct sockaddr
*) addrbuf
, addrlen
);
701 #endif /* HAVE_GETPEERNAME */
704 /* s.listen(n) method */
707 BUILD_FUNC_DEF_2(PySocketSock_listen
,PySocketSockObject
*,s
, PyObject
*,args
)
711 if (!PyArg_GetInt(args
, &backlog
))
713 Py_BEGIN_ALLOW_THREADS
716 res
= listen(s
->sock_fd
, backlog
);
719 return PySocket_Err();
725 /* s.makefile(mode) method.
726 Create a new open file object referring to a dupped version of
727 the socket's file descriptor. (The dup() call is necessary so
728 that the open file and socket objects may be closed independent
730 The mode argument specifies 'r' or 'w' passed to fdopen(). */
733 BUILD_FUNC_DEF_2(PySocketSock_makefile
,PySocketSockObject
*,s
, PyObject
*,args
)
735 extern int fclose
Py_PROTO((FILE *));
739 if (!PyArg_Parse(args
, "s", &mode
))
741 if ((fd
= dup(s
->sock_fd
)) < 0 ||
742 (fp
= fdopen(fd
, mode
)) == NULL
)
743 return PySocket_Err();
744 return PyFile_FromFile(fp
, "<socket>", mode
, fclose
);
748 /* s.recv(nbytes [,flags]) method */
751 BUILD_FUNC_DEF_2(PySocketSock_recv
,PySocketSockObject
*,s
, PyObject
*,args
)
756 if (!PyArg_Parse(args
, "i", &len
)) {
758 if (!PyArg_Parse(args
, "(ii)", &len
, &flags
))
761 buf
= PyString_FromStringAndSize((char *) 0, len
);
764 Py_BEGIN_ALLOW_THREADS
765 n
= recv(s
->sock_fd
, PyString_AsString(buf
), len
, flags
);
768 return PySocket_Err();
769 if (_PyString_Resize(&buf
, n
) < 0)
775 /* s.recvfrom(nbytes [,flags]) method */
778 BUILD_FUNC_DEF_2(PySocketSock_recvfrom
,PySocketSockObject
*,s
, PyObject
*,args
)
781 PyObject
*buf
, *addr
, *ret
;
782 int addrlen
, len
, n
, flags
;
784 if (!PyArg_Parse(args
, "i", &len
)) {
786 if (!PyArg_Parse(args
, "(ii)", &len
, &flags
))
789 if (!getsockaddrlen(s
, &addrlen
))
791 buf
= PyString_FromStringAndSize((char *) 0, len
);
794 Py_BEGIN_ALLOW_THREADS
795 n
= recvfrom(s
->sock_fd
, PyString_AsString(buf
), len
, flags
,
797 (ANY
*)addrbuf
, &addrlen
);
799 (struct sockaddr
*)addrbuf
, &addrlen
);
803 return PySocket_Err();
804 if (_PyString_Resize(&buf
, n
) < 0)
806 addr
= makesockaddr((struct sockaddr
*)addrbuf
, addrlen
);
807 ret
= Py_BuildValue("OO", buf
, addr
);
814 /* s.send(data [,flags]) method */
817 BUILD_FUNC_DEF_2(PySocketSock_send
,PySocketSockObject
*,s
, PyObject
*,args
)
822 if (!PyArg_Parse(args
, "s#", &buf
, &len
)) {
824 if (!PyArg_Parse(args
, "(s#i)", &buf
, &len
, &flags
))
827 Py_BEGIN_ALLOW_THREADS
828 n
= send(s
->sock_fd
, buf
, len
, flags
);
831 return PySocket_Err();
832 return PyInt_FromLong((long)n
);
836 /* s.sendto(data, [flags,] sockaddr) method */
839 BUILD_FUNC_DEF_2(PySocketSock_sendto
,PySocketSockObject
*,s
, PyObject
*,args
)
843 struct sockaddr
*addr
;
844 int addrlen
, len
, n
, flags
;
846 if (!PyArg_Parse(args
, "(s#O)", &buf
, &len
, &addro
)) {
848 if (!PyArg_Parse(args
, "(s#iO)", &buf
, &len
, &flags
, &addro
))
851 if (!getsockaddrarg(s
, addro
, &addr
, &addrlen
))
853 Py_BEGIN_ALLOW_THREADS
854 n
= sendto(s
->sock_fd
, buf
, len
, flags
, addr
, addrlen
);
857 return PySocket_Err();
858 return PyInt_FromLong((long)n
);
862 /* s.shutdown(how) method */
865 BUILD_FUNC_DEF_2(PySocketSock_shutdown
,PySocketSockObject
*,s
, PyObject
*,args
)
869 if (!PyArg_GetInt(args
, &how
))
871 Py_BEGIN_ALLOW_THREADS
872 res
= shutdown(s
->sock_fd
, how
);
875 return PySocket_Err();
881 /* List of methods for socket objects */
883 static PyMethodDef PySocketSock_methods
[] = {
884 {"accept", (PyCFunction
)PySocketSock_accept
},
886 {"allowbroadcast", (PyCFunction
)PySocketSock_allowbroadcast
},
889 {"setblocking", (PyCFunction
)PySocketSock_setblocking
},
891 {"setsockopt", (PyCFunction
)PySocketSock_setsockopt
},
892 {"getsockopt", (PyCFunction
)PySocketSock_getsockopt
},
893 {"bind", (PyCFunction
)PySocketSock_bind
},
894 {"close", (PyCFunction
)PySocketSock_close
},
895 {"connect", (PyCFunction
)PySocketSock_connect
},
896 {"fileno", (PyCFunction
)PySocketSock_fileno
},
897 {"getsockname", (PyCFunction
)PySocketSock_getsockname
},
898 #ifdef HAVE_GETPEERNAME
899 {"getpeername", (PyCFunction
)PySocketSock_getpeername
},
901 {"listen", (PyCFunction
)PySocketSock_listen
},
903 {"makefile", (PyCFunction
)PySocketSock_makefile
},
905 {"recv", (PyCFunction
)PySocketSock_recv
},
906 {"recvfrom", (PyCFunction
)PySocketSock_recvfrom
},
907 {"send", (PyCFunction
)PySocketSock_send
},
908 {"sendto", (PyCFunction
)PySocketSock_sendto
},
909 {"shutdown", (PyCFunction
)PySocketSock_shutdown
},
910 {NULL
, NULL
} /* sentinel */
914 /* Deallocate a socket object in response to the last Py_DECREF().
915 First close the file description. */
918 BUILD_FUNC_DEF_1(PySocketSock_dealloc
,PySocketSockObject
*,s
)
920 (void) close(s
->sock_fd
);
925 /* Return a socket object's named attribute. */
928 BUILD_FUNC_DEF_2(PySocketSock_getattr
,PySocketSockObject
*,s
, char *,name
)
930 return Py_FindMethod(PySocketSock_methods
, (PyObject
*) s
, name
);
935 BUILD_FUNC_DEF_1(PySocketSock_repr
,PySocketSockObject
*,s
)
938 struct sockaddr
*addr
;
940 PyObject
*t
, *comma
, *v
;
943 "<socket object, fd=%d, family=%d, type=%d, protocol=%d>",
944 s
->sock_fd
, s
->sock_family
, s
->sock_type
, s
->sock_proto
);
945 t
= PyString_FromString(buf
);
950 /* Type object for socket objects. */
952 static PyTypeObject PySocketSock_Type
= {
953 PyObject_HEAD_INIT(&PyType_Type
)
956 sizeof(PySocketSockObject
),
958 (destructor
)PySocketSock_dealloc
, /*tp_dealloc*/
960 (getattrfunc
)PySocketSock_getattr
, /*tp_getattr*/
963 (reprfunc
)PySocketSock_repr
, /*tp_repr*/
965 0, /*tp_as_sequence*/
970 /* Python interface to gethostname(). */
974 BUILD_FUNC_DEF_2(PySocket_gethostname
,PyObject
*,self
, PyObject
*,args
)
978 if (!PyArg_NoArgs(args
))
980 Py_BEGIN_ALLOW_THREADS
981 res
= gethostname(buf
, (int) sizeof buf
- 1);
984 return PySocket_Err();
985 buf
[sizeof buf
- 1] = '\0';
986 return PyString_FromString(buf
);
990 /* Python interface to gethostbyname(name). */
994 BUILD_FUNC_DEF_2(PySocket_gethostbyname
,PyObject
*,self
, PyObject
*,args
)
997 struct sockaddr_in addrbuf
;
998 if (!PyArg_Parse(args
, "s", &name
))
1000 if (setipaddr(name
, &addrbuf
) < 0)
1002 return makeipaddr(&addrbuf
);
1005 /* Python interface to gethostbyaddr(IP). */
1009 BUILD_FUNC_DEF_2(PySocket_gethostbyaddr
,PyObject
*,self
, PyObject
*, args
)
1011 struct sockaddr_in addr
;
1016 PyObject
*rtn_tuple
= (PyObject
*)NULL
;
1017 PyObject
*name_list
= (PyObject
*)NULL
;
1018 PyObject
*addr_list
= (PyObject
*)NULL
;
1021 if (!PyArg_Parse(args
, "s", &ip_num
))
1023 if (setipaddr(ip_num
, &addr
) < 0)
1025 h
= gethostbyaddr((char *)&addr
.sin_addr
,
1026 sizeof(addr
.sin_addr
),
1029 #ifdef HAVE_HSTRERROR
1030 /* Let's get real error message to return */
1032 PyErr_SetString(PySocket_Error
, (char *)hstrerror(h_errno
));
1034 PyErr_SetString(PySocket_Error
, "host not found");
1038 if ((name_list
= PyList_New(0)) == NULL
)
1040 if ((addr_list
= PyList_New(0)) == NULL
)
1042 for (pch
= h
->h_aliases
; *pch
!= NULL
; pch
++) {
1043 tmp
= PyString_FromString(*pch
);
1046 PyList_Append(name_list
, tmp
);
1049 for (pch
= h
->h_addr_list
; *pch
!= NULL
; pch
++) {
1050 memcpy((char *) &addr
.sin_addr
, *pch
, h
->h_length
);
1051 tmp
= makeipaddr(&addr
);
1054 PyList_Append(addr_list
, tmp
);
1057 rtn_tuple
= Py_BuildValue("sOO", h
->h_name
, name_list
, addr_list
);
1059 Py_XDECREF(name_list
);
1060 Py_XDECREF(addr_list
);
1065 /* Python interface to getservbyname(name).
1066 This only returns the port number, since the other info is already
1067 known or not useful (like the list of aliases). */
1071 BUILD_FUNC_DEF_2(PySocket_getservbyname
,PyObject
*,self
, PyObject
*,args
)
1075 if (!PyArg_Parse(args
, "(ss)", &name
, &proto
))
1077 Py_BEGIN_ALLOW_THREADS
1078 sp
= getservbyname(name
, proto
);
1079 Py_END_ALLOW_THREADS
1081 PyErr_SetString(PySocket_Error
, "service/proto not found");
1084 return PyInt_FromLong((long) ntohs(sp
->s_port
));
1088 /* Python interface to socket(family, type, proto).
1089 The third (protocol) argument is optional.
1090 Return a new socket object. */
1094 BUILD_FUNC_DEF_2(PySocket_socket
,PyObject
*,self
, PyObject
*,args
)
1096 PySocketSockObject
*s
;
1097 int fd
, family
, type
, proto
;
1099 if (!PyArg_Parse(args
, "(ii)", &family
, &type
)) {
1101 if (!PyArg_Parse(args
, "(iii)", &family
, &type
, &proto
))
1104 Py_BEGIN_ALLOW_THREADS
1105 fd
= socket(family
, type
, proto
);
1106 Py_END_ALLOW_THREADS
1108 return PySocket_Err();
1109 s
= PySocketSock_New(fd
, family
, type
, proto
);
1110 /* If the object can't be created, don't forget to close the
1111 file descriptor again! */
1114 /* From now on, ignore SIGPIPE and let the error checking
1117 (void) signal(SIGPIPE
, SIG_IGN
);
1119 return (PyObject
*) s
;
1123 /* Create a socket object from a numeric file description.
1124 Useful e.g. if stdin is a socket.
1125 Additional arguments as for socket(). */
1129 BUILD_FUNC_DEF_2(PySocket_fromfd
,PyObject
*,self
, PyObject
*,args
)
1131 PySocketSockObject
*s
;
1132 int fd
, family
, type
, proto
;
1134 if (!PyArg_Parse(args
, "(iii)", &fd
, &family
, &type
)) {
1136 if (!PyArg_Parse(args
, "(iiii)", &fd
, &family
, &type
, &proto
))
1139 /* Dup the fd so it and the socket can be closed independently */
1142 return PySocket_Err();
1143 s
= PySocketSock_New(fd
, family
, type
, proto
);
1144 /* From now on, ignore SIGPIPE and let the error checking
1147 (void) signal(SIGPIPE
, SIG_IGN
);
1149 return (PyObject
*) s
;
1153 /* List of functions exported by this module. */
1155 static PyMethodDef PySocket_methods
[] = {
1156 {"gethostbyname", PySocket_gethostbyname
},
1157 {"gethostbyaddr", PySocket_gethostbyaddr
},
1158 {"gethostname", PySocket_gethostname
},
1159 {"getservbyname", PySocket_getservbyname
},
1160 {"socket", PySocket_socket
},
1162 {"fromfd", PySocket_fromfd
},
1164 {NULL
, NULL
} /* Sentinel */
1168 /* Convenience routine to export an integer value.
1169 For simplicity, errors (which are unlikely anyway) are ignored. */
1172 BUILD_FUNC_DEF_3(insint
,PyObject
*,d
, char *,name
, int,value
)
1174 PyObject
*v
= PyInt_FromLong((long) value
);
1176 /* Don't bother reporting this error */
1180 PyDict_SetItemString(d
, name
, v
);
1186 /* Initialize this module.
1187 This is called when the first 'import socket' is done,
1188 via a table in config.c, if config.c is compiled with USE_SOCKET
1195 m
= Py_InitModule("socket", PySocket_methods
);
1196 d
= PyModule_GetDict(m
);
1197 PySocket_Error
= PyString_FromString("socket.error");
1198 if (PySocket_Error
== NULL
||
1199 PyDict_SetItemString(d
, "error", PySocket_Error
) != 0)
1200 Py_FatalError("can't define socket.error");
1201 insint(d
, "AF_INET", AF_INET
);
1203 insint(d
, "AF_UNIX", AF_UNIX
);
1204 #endif /* AF_UNIX */
1205 insint(d
, "SOCK_STREAM", SOCK_STREAM
);
1206 insint(d
, "SOCK_DGRAM", SOCK_DGRAM
);
1207 insint(d
, "SOCK_RAW", SOCK_RAW
);
1208 insint(d
, "SOCK_SEQPACKET", SOCK_SEQPACKET
);
1209 insint(d
, "SOCK_RDM", SOCK_RDM
);
1213 BOOL WINAPI
DllMain (HANDLE hInst
,
1214 ULONG ul_reason_for_call
,
1217 switch (ul_reason_for_call
)
1219 case DLL_PROCESS_ATTACH
:
1221 if (WSAStartup(MAKEWORD(2,0), &WSAData
)) {
1222 OutputDebugString("Python can't initialize Windows Sockets DLL!");
1226 case DLL_PROCESS_DETACH
: