1 /* Socket module header file */
3 /* Includes needed for the sockaddr_* symbols below */
5 # include <sys/socket.h>
6 # include <netinet/in.h>
7 # if !(defined(__BEOS__) || defined(__CYGWIN__) || (defined(PYOS_OS2) && defined(PYCC_VACPP)))
8 # include <netinet/tcp.h>
11 #else /* MS_WINDOWS */
13 # include <winsock2.h>
14 # include <ws2tcpip.h>
15 # define HAVE_ADDRINFO
16 # define HAVE_SOCKADDR_STORAGE
17 # define HAVE_GETADDRINFO
18 # define HAVE_GETNAMEINFO
31 #ifdef HAVE_NETPACKET_PACKET_H
32 # include <sys/ioctl.h>
34 # include <netpacket/packet.h>
43 /* Python module and C API name */
44 #define PySocket_MODULE_NAME "_socket"
45 #define PySocket_CAPI_NAME "CAPI"
47 /* Abstract the socket file descriptor type */
49 typedef SOCKET SOCKET_T
;
51 # define SIZEOF_SOCKET_T 8
53 # define SIZEOF_SOCKET_T 4
57 # define SIZEOF_SOCKET_T SIZEOF_INT
60 /* The object holding a socket. It holds some extra information,
61 like the address family, which is used to decode socket address
62 arguments properly. */
66 SOCKET_T sock_fd
; /* Socket file descriptor */
67 int sock_family
; /* Address family, e.g., AF_INET */
68 int sock_type
; /* Socket type, e.g., SOCK_STREAM */
69 int sock_proto
; /* Protocol type, usually 0 */
71 struct sockaddr_in in
;
73 struct sockaddr_un un
;
76 struct sockaddr_in6 in6
;
77 struct sockaddr_storage storage
;
79 #ifdef HAVE_NETPACKET_PACKET_H
80 struct sockaddr_ll ll
;
83 PyObject
*(*errorhandler
)(void); /* Error handler; checks
84 errno, returns NULL and
85 sets a Python exception */
86 double sock_timeout
; /* Operation timeout in seconds;
87 0.0 means non-blocking */
90 /* --- C API ----------------------------------------------------*/
92 /* Short explanation of what this C API export mechanism does
95 The _ssl module needs access to the type object defined in
96 the _socket module. Since cross-DLL linking introduces a lot of
97 problems on many platforms, the "trick" is to wrap the
98 C API of a module in a struct which then gets exported to
99 other modules via a PyCObject.
101 The code in socketmodule.c defines this struct (which currently
102 only contains the type object reference, but could very
103 well also include other C APIs needed by other modules)
104 and exports it as PyCObject via the module dictionary
105 under the name "CAPI".
107 Other modules can now include the socketmodule.h file
108 which defines the needed C APIs to import and set up
109 a static copy of this struct in the importing module.
111 After initialization, the importing module can then
112 access the C APIs from the _socket module by simply
113 referring to the static struct, e.g.
115 Load _socket module and its C API; this sets up the global
118 if (PySocketModule_ImportModuleAndAPI())
122 Now use the C API as if it were defined in the using
125 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
127 PySocketModule.Sock_Type,
130 &key_file, &cert_file))
133 Support could easily be extended to export more C APIs/symbols
134 this way. Currently, only the type object is exported,
135 other candidates would be socket constructors and socket
140 /* C API for usage by other Python modules */
142 PyTypeObject
*Sock_Type
;
143 } PySocketModule_APIObject
;
145 /* XXX The net effect of the following appears to be to define a function
146 XXX named PySocketModule_APIObject in _ssl.c. It's unclear why it isn't
147 XXX defined there directly.
149 >>> It's defined here because other modules might also want to use
153 #ifndef PySocket_BUILDING_SOCKET
155 /* --- C API ----------------------------------------------------*/
157 /* Interfacestructure to C API for other modules.
158 Call PySocketModule_ImportModuleAndAPI() to initialize this
159 structure. After that usage is simple:
161 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
162 &PySocketModule.Sock_Type, (PyObject*)&Sock,
163 &key_file, &cert_file))
169 PySocketModule_APIObject PySocketModule
;
171 /* You *must* call this before using any of the functions in
172 PySocketModule and check its outcome; otherwise all accesses will
173 result in a segfault. Returns 0 on success. */
176 # define DPRINTF if (0) printf
180 int PySocketModule_ImportModuleAndAPI(void)
182 PyObject
*mod
= 0, *v
= 0;
183 char *apimodule
= PySocket_MODULE_NAME
;
184 char *apiname
= PySocket_CAPI_NAME
;
187 DPRINTF("Importing the %s C API...\n", apimodule
);
188 mod
= PyImport_ImportModule(apimodule
);
191 DPRINTF(" %s package found\n", apimodule
);
192 v
= PyObject_GetAttrString(mod
, apiname
);
196 DPRINTF(" API object %s found\n", apiname
);
197 api
= PyCObject_AsVoidPtr(v
);
201 memcpy(&PySocketModule
, api
, sizeof(PySocketModule
));
202 DPRINTF(" API object loaded and initialized.\n");
206 DPRINTF(" not found.\n");
212 #endif /* !PySocket_BUILDING_SOCKET */
217 #endif /* !Py__SOCKET_H */