Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1 | /* 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 */ |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 12 | #if _MSC_VER >= 1300 |
| 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 |
| 19 | # define ENABLE_IPV6 |
| 20 | #else |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 21 | # include <winsock.h> |
| 22 | #endif |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 23 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 24 | |
| 25 | #ifdef HAVE_SYS_UN_H |
| 26 | # include <sys/un.h> |
| 27 | #else |
| 28 | # undef AF_UNIX |
| 29 | #endif |
| 30 | |
| 31 | #ifdef HAVE_NETPACKET_PACKET_H |
| 32 | # include <sys/ioctl.h> |
| 33 | # include <net/if.h> |
| 34 | # include <netpacket/packet.h> |
| 35 | #endif |
| 36 | |
| 37 | #ifndef Py__SOCKET_H |
| 38 | #define Py__SOCKET_H |
| 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
| 43 | /* Python module and C API name */ |
| 44 | #define PySocket_MODULE_NAME "_socket" |
| 45 | #define PySocket_CAPI_NAME "CAPI" |
| 46 | |
| 47 | /* Abstract the socket file descriptor type */ |
| 48 | #ifdef MS_WINDOWS |
| 49 | typedef SOCKET SOCKET_T; |
| 50 | # ifdef MS_WIN64 |
| 51 | # define SIZEOF_SOCKET_T 8 |
| 52 | # else |
| 53 | # define SIZEOF_SOCKET_T 4 |
| 54 | # endif |
| 55 | #else |
| 56 | typedef int SOCKET_T; |
| 57 | # define SIZEOF_SOCKET_T SIZEOF_INT |
| 58 | #endif |
| 59 | |
| 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. */ |
| 63 | |
| 64 | typedef struct { |
| 65 | PyObject_HEAD |
| 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 */ |
| 70 | union sock_addr { |
| 71 | struct sockaddr_in in; |
| 72 | #ifdef AF_UNIX |
| 73 | struct sockaddr_un un; |
| 74 | #endif |
| 75 | #ifdef ENABLE_IPV6 |
| 76 | struct sockaddr_in6 in6; |
| 77 | struct sockaddr_storage storage; |
| 78 | #endif |
| 79 | #ifdef HAVE_NETPACKET_PACKET_H |
| 80 | struct sockaddr_ll ll; |
| 81 | #endif |
| 82 | } sock_addr; |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 83 | PyObject *(*errorhandler)(void); /* Error handler; checks |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 84 | errno, returns NULL and |
| 85 | sets a Python exception */ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 86 | double sock_timeout; /* Operation timeout in seconds; |
| 87 | 0.0 means non-blocking */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 88 | } PySocketSockObject; |
| 89 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 90 | /* --- C API ----------------------------------------------------*/ |
| 91 | |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 92 | /* Short explanation of what this C API export mechanism does |
| 93 | and how it works: |
| 94 | |
| 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. |
| 100 | |
| 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". |
| 106 | |
| 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. |
| 110 | |
| 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. |
| 114 | |
| 115 | Load _socket module and its C API; this sets up the global |
| 116 | PySocketModule: |
| 117 | |
| 118 | if (PySocketModule_ImportModuleAndAPI()) |
| 119 | return; |
| 120 | |
| 121 | |
| 122 | Now use the C API as if it were defined in the using |
| 123 | module: |
| 124 | |
| 125 | if (!PyArg_ParseTuple(args, "O!|zz:ssl", |
| 126 | |
| 127 | PySocketModule.Sock_Type, |
| 128 | |
| 129 | (PyObject*)&Sock, |
| 130 | &key_file, &cert_file)) |
| 131 | return NULL; |
| 132 | |
| 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 |
| 136 | access functions. |
| 137 | |
| 138 | */ |
| 139 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 140 | /* C API for usage by other Python modules */ |
| 141 | typedef struct { |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 142 | PyTypeObject *Sock_Type; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 143 | } PySocketModule_APIObject; |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 144 | |
| 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 |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 147 | XXX defined there directly. |
| 148 | |
| 149 | >>> It's defined here because other modules might also want to use |
| 150 | >>> the C API. |
| 151 | |
| 152 | */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 153 | #ifndef PySocket_BUILDING_SOCKET |
| 154 | |
| 155 | /* --- C API ----------------------------------------------------*/ |
| 156 | |
| 157 | /* Interfacestructure to C API for other modules. |
Guido van Rossum | be8db07 | 2002-06-07 02:27:50 +0000 | [diff] [blame] | 158 | Call PySocketModule_ImportModuleAndAPI() to initialize this |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 159 | structure. After that usage is simple: |
| 160 | |
| 161 | if (!PyArg_ParseTuple(args, "O!|zz:ssl", |
| 162 | &PySocketModule.Sock_Type, (PyObject*)&Sock, |
| 163 | &key_file, &cert_file)) |
| 164 | return NULL; |
| 165 | ... |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 166 | */ |
| 167 | |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 168 | static |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 169 | PySocketModule_APIObject PySocketModule; |
| 170 | |
| 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. */ |
| 174 | |
| 175 | #ifndef DPRINTF |
| 176 | # define DPRINTF if (0) printf |
| 177 | #endif |
| 178 | |
| 179 | static |
| 180 | int PySocketModule_ImportModuleAndAPI(void) |
| 181 | { |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 182 | PyObject *mod = 0, *v = 0; |
| 183 | char *apimodule = PySocket_MODULE_NAME; |
| 184 | char *apiname = PySocket_CAPI_NAME; |
| 185 | void *api; |
| 186 | |
| 187 | DPRINTF("Importing the %s C API...\n", apimodule); |
| 188 | mod = PyImport_ImportModule(apimodule); |
| 189 | if (mod == NULL) |
| 190 | goto onError; |
| 191 | DPRINTF(" %s package found\n", apimodule); |
| 192 | v = PyObject_GetAttrString(mod, apiname); |
| 193 | if (v == NULL) |
| 194 | goto onError; |
| 195 | Py_DECREF(mod); |
| 196 | DPRINTF(" API object %s found\n", apiname); |
| 197 | api = PyCObject_AsVoidPtr(v); |
| 198 | if (api == NULL) |
| 199 | goto onError; |
| 200 | Py_DECREF(v); |
| 201 | memcpy(&PySocketModule, api, sizeof(PySocketModule)); |
| 202 | DPRINTF(" API object loaded and initialized.\n"); |
| 203 | return 0; |
| 204 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 205 | onError: |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 206 | DPRINTF(" not found.\n"); |
| 207 | Py_XDECREF(mod); |
| 208 | Py_XDECREF(v); |
| 209 | return -1; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 212 | #endif /* !PySocket_BUILDING_SOCKET */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 213 | |
| 214 | #ifdef __cplusplus |
| 215 | } |
| 216 | #endif |
| 217 | #endif /* !Py__SOCKET_H */ |