blob: ef4ba38e275bb9192936408716270ac31c53994f [file] [log] [blame]
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001/* Socket module header file */
2
3/* Includes needed for the sockaddr_* symbols below */
4#ifndef MS_WINDOWS
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>
9# endif
10
11#else /* MS_WINDOWS */
12# include <winsock.h>
13#endif
14
15#ifdef HAVE_SYS_UN_H
16# include <sys/un.h>
17#else
18# undef AF_UNIX
19#endif
20
21#ifdef HAVE_NETPACKET_PACKET_H
22# include <sys/ioctl.h>
23# include <net/if.h>
24# include <netpacket/packet.h>
25#endif
26
27#ifndef Py__SOCKET_H
28#define Py__SOCKET_H
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33/* Python module and C API name */
34#define PySocket_MODULE_NAME "_socket"
35#define PySocket_CAPI_NAME "CAPI"
36
37/* Abstract the socket file descriptor type */
38#ifdef MS_WINDOWS
39typedef SOCKET SOCKET_T;
40# ifdef MS_WIN64
41# define SIZEOF_SOCKET_T 8
42# else
43# define SIZEOF_SOCKET_T 4
44# endif
45#else
46typedef int SOCKET_T;
47# define SIZEOF_SOCKET_T SIZEOF_INT
48#endif
49
50/* The object holding a socket. It holds some extra information,
51 like the address family, which is used to decode socket address
52 arguments properly. */
53
54typedef struct {
55 PyObject_HEAD
56 SOCKET_T sock_fd; /* Socket file descriptor */
57 int sock_family; /* Address family, e.g., AF_INET */
58 int sock_type; /* Socket type, e.g., SOCK_STREAM */
59 int sock_proto; /* Protocol type, usually 0 */
60 union sock_addr {
61 struct sockaddr_in in;
62#ifdef AF_UNIX
63 struct sockaddr_un un;
64#endif
65#ifdef ENABLE_IPV6
66 struct sockaddr_in6 in6;
67 struct sockaddr_storage storage;
68#endif
69#ifdef HAVE_NETPACKET_PACKET_H
70 struct sockaddr_ll ll;
71#endif
72 } sock_addr;
Tim Peters6f5505a2002-02-17 03:58:51 +000073 PyObject *(*errorhandler)(void); /* Error handler; checks
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000074 errno, returns NULL and
75 sets a Python exception */
76} PySocketSockObject;
77
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000078/* --- C API ----------------------------------------------------*/
79
80/* C API for usage by other Python modules */
81typedef struct {
Tim Peters6f5505a2002-02-17 03:58:51 +000082 PyTypeObject *Sock_Type;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000083} PySocketModule_APIObject;
Tim Peters6f5505a2002-02-17 03:58:51 +000084
85/* XXX The net effect of the following appears to be to define a function
86 XXX named PySocketModule_APIObject in _ssl.c. It's unclear why it isn't
87 XXX defined there directly. */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000088#ifndef PySocket_BUILDING_SOCKET
89
90/* --- C API ----------------------------------------------------*/
91
92/* Interfacestructure to C API for other modules.
93 Call PySocket_ImportModuleAPI() to initialize this
94 structure. After that usage is simple:
95
96 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
97 &PySocketModule.Sock_Type, (PyObject*)&Sock,
98 &key_file, &cert_file))
99 return NULL;
100 ...
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000101*/
102
Tim Peters6f5505a2002-02-17 03:58:51 +0000103static
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000104PySocketModule_APIObject PySocketModule;
105
106/* You *must* call this before using any of the functions in
107 PySocketModule and check its outcome; otherwise all accesses will
108 result in a segfault. Returns 0 on success. */
109
110#ifndef DPRINTF
111# define DPRINTF if (0) printf
112#endif
113
114static
115int PySocketModule_ImportModuleAndAPI(void)
116{
Tim Peters6f5505a2002-02-17 03:58:51 +0000117 PyObject *mod = 0, *v = 0;
118 char *apimodule = PySocket_MODULE_NAME;
119 char *apiname = PySocket_CAPI_NAME;
120 void *api;
121
122 DPRINTF("Importing the %s C API...\n", apimodule);
123 mod = PyImport_ImportModule(apimodule);
124 if (mod == NULL)
125 goto onError;
126 DPRINTF(" %s package found\n", apimodule);
127 v = PyObject_GetAttrString(mod, apiname);
128 if (v == NULL)
129 goto onError;
130 Py_DECREF(mod);
131 DPRINTF(" API object %s found\n", apiname);
132 api = PyCObject_AsVoidPtr(v);
133 if (api == NULL)
134 goto onError;
135 Py_DECREF(v);
136 memcpy(&PySocketModule, api, sizeof(PySocketModule));
137 DPRINTF(" API object loaded and initialized.\n");
138 return 0;
139
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000140 onError:
Tim Peters6f5505a2002-02-17 03:58:51 +0000141 DPRINTF(" not found.\n");
142 Py_XDECREF(mod);
143 Py_XDECREF(v);
144 return -1;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000145}
146
Tim Peters6f5505a2002-02-17 03:58:51 +0000147#endif /* !PySocket_BUILDING_SOCKET */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000148
149#ifdef __cplusplus
150}
151#endif
152#endif /* !Py__SOCKET_H */