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 |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 5 | #ifdef __VMS |
| 6 | # include <socket.h> |
| 7 | # else |
| 8 | # include <sys/socket.h> |
| 9 | # endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 10 | # include <netinet/in.h> |
Christian Heimes | b5daaed | 2007-11-30 23:34:21 +0000 | [diff] [blame] | 11 | # if !(defined(__CYGWIN__) || (defined(PYOS_OS2) && defined(PYCC_VACPP))) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 12 | # include <netinet/tcp.h> |
| 13 | # endif |
| 14 | |
| 15 | #else /* MS_WINDOWS */ |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 16 | #if _MSC_VER >= 1300 |
| 17 | # include <winsock2.h> |
| 18 | # include <ws2tcpip.h> |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame^] | 19 | # include <MSTcpIP.h> /* for SIO_RCVALL */ |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 20 | # define HAVE_ADDRINFO |
| 21 | # define HAVE_SOCKADDR_STORAGE |
| 22 | # define HAVE_GETADDRINFO |
| 23 | # define HAVE_GETNAMEINFO |
| 24 | # define ENABLE_IPV6 |
| 25 | #else |
Christian Heimes | c36625b | 2008-01-04 13:33:00 +0000 | [diff] [blame] | 26 | # define WIN32_LEAN_AND_MEAN |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 27 | # include <winsock.h> |
| 28 | #endif |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 29 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 30 | |
| 31 | #ifdef HAVE_SYS_UN_H |
| 32 | # include <sys/un.h> |
| 33 | #else |
| 34 | # undef AF_UNIX |
| 35 | #endif |
| 36 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 37 | #ifdef HAVE_LINUX_NETLINK_H |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 38 | # ifdef HAVE_ASM_TYPES_H |
| 39 | # include <asm/types.h> |
| 40 | # endif |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 41 | # include <linux/netlink.h> |
| 42 | #else |
| 43 | # undef AF_NETLINK |
| 44 | #endif |
| 45 | |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 46 | #ifdef HAVE_BLUETOOTH_BLUETOOTH_H |
| 47 | #include <bluetooth/bluetooth.h> |
| 48 | #include <bluetooth/rfcomm.h> |
| 49 | #include <bluetooth/l2cap.h> |
| 50 | #include <bluetooth/sco.h> |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 51 | #include <bluetooth/hci.h> |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
Hye-Shik Chang | 96c44658 | 2004-02-02 08:48:45 +0000 | [diff] [blame] | 54 | #ifdef HAVE_BLUETOOTH_H |
| 55 | #include <bluetooth.h> |
| 56 | #endif |
| 57 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 58 | #ifdef HAVE_NETPACKET_PACKET_H |
| 59 | # include <sys/ioctl.h> |
| 60 | # include <net/if.h> |
| 61 | # include <netpacket/packet.h> |
| 62 | #endif |
| 63 | |
| 64 | #ifndef Py__SOCKET_H |
| 65 | #define Py__SOCKET_H |
| 66 | #ifdef __cplusplus |
| 67 | extern "C" { |
| 68 | #endif |
| 69 | |
| 70 | /* Python module and C API name */ |
| 71 | #define PySocket_MODULE_NAME "_socket" |
| 72 | #define PySocket_CAPI_NAME "CAPI" |
| 73 | |
| 74 | /* Abstract the socket file descriptor type */ |
| 75 | #ifdef MS_WINDOWS |
| 76 | typedef SOCKET SOCKET_T; |
| 77 | # ifdef MS_WIN64 |
| 78 | # define SIZEOF_SOCKET_T 8 |
| 79 | # else |
| 80 | # define SIZEOF_SOCKET_T 4 |
| 81 | # endif |
| 82 | #else |
| 83 | typedef int SOCKET_T; |
| 84 | # define SIZEOF_SOCKET_T SIZEOF_INT |
| 85 | #endif |
| 86 | |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 87 | /* Socket address */ |
| 88 | typedef union sock_addr { |
| 89 | struct sockaddr_in in; |
| 90 | #ifdef AF_UNIX |
| 91 | struct sockaddr_un un; |
| 92 | #endif |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 93 | #ifdef AF_NETLINK |
| 94 | struct sockaddr_nl nl; |
| 95 | #endif |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 96 | #ifdef ENABLE_IPV6 |
| 97 | struct sockaddr_in6 in6; |
| 98 | struct sockaddr_storage storage; |
| 99 | #endif |
| 100 | #ifdef HAVE_BLUETOOTH_BLUETOOTH_H |
| 101 | struct sockaddr_l2 bt_l2; |
| 102 | struct sockaddr_rc bt_rc; |
| 103 | struct sockaddr_sco bt_sco; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 104 | struct sockaddr_hci bt_hci; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 105 | #endif |
| 106 | #ifdef HAVE_NETPACKET_PACKET_H |
| 107 | struct sockaddr_ll ll; |
| 108 | #endif |
| 109 | } sock_addr_t; |
| 110 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 111 | /* The object holding a socket. It holds some extra information, |
| 112 | like the address family, which is used to decode socket address |
| 113 | arguments properly. */ |
| 114 | |
| 115 | typedef struct { |
| 116 | PyObject_HEAD |
| 117 | SOCKET_T sock_fd; /* Socket file descriptor */ |
| 118 | int sock_family; /* Address family, e.g., AF_INET */ |
| 119 | int sock_type; /* Socket type, e.g., SOCK_STREAM */ |
| 120 | int sock_proto; /* Protocol type, usually 0 */ |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 121 | PyObject *(*errorhandler)(void); /* Error handler; checks |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 122 | errno, returns NULL and |
| 123 | sets a Python exception */ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 124 | double sock_timeout; /* Operation timeout in seconds; |
| 125 | 0.0 means non-blocking */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 126 | } PySocketSockObject; |
| 127 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 128 | /* --- C API ----------------------------------------------------*/ |
| 129 | |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 130 | /* Short explanation of what this C API export mechanism does |
| 131 | and how it works: |
| 132 | |
| 133 | The _ssl module needs access to the type object defined in |
| 134 | the _socket module. Since cross-DLL linking introduces a lot of |
| 135 | problems on many platforms, the "trick" is to wrap the |
| 136 | C API of a module in a struct which then gets exported to |
| 137 | other modules via a PyCObject. |
| 138 | |
| 139 | The code in socketmodule.c defines this struct (which currently |
| 140 | only contains the type object reference, but could very |
| 141 | well also include other C APIs needed by other modules) |
| 142 | and exports it as PyCObject via the module dictionary |
| 143 | under the name "CAPI". |
| 144 | |
| 145 | Other modules can now include the socketmodule.h file |
| 146 | which defines the needed C APIs to import and set up |
| 147 | a static copy of this struct in the importing module. |
| 148 | |
| 149 | After initialization, the importing module can then |
| 150 | access the C APIs from the _socket module by simply |
| 151 | referring to the static struct, e.g. |
| 152 | |
| 153 | Load _socket module and its C API; this sets up the global |
| 154 | PySocketModule: |
| 155 | |
| 156 | if (PySocketModule_ImportModuleAndAPI()) |
| 157 | return; |
| 158 | |
| 159 | |
| 160 | Now use the C API as if it were defined in the using |
| 161 | module: |
| 162 | |
| 163 | if (!PyArg_ParseTuple(args, "O!|zz:ssl", |
| 164 | |
| 165 | PySocketModule.Sock_Type, |
| 166 | |
| 167 | (PyObject*)&Sock, |
| 168 | &key_file, &cert_file)) |
| 169 | return NULL; |
| 170 | |
| 171 | Support could easily be extended to export more C APIs/symbols |
| 172 | this way. Currently, only the type object is exported, |
| 173 | other candidates would be socket constructors and socket |
| 174 | access functions. |
| 175 | |
| 176 | */ |
| 177 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 178 | /* C API for usage by other Python modules */ |
| 179 | typedef struct { |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 180 | PyTypeObject *Sock_Type; |
Brett Cannon | 06c3479 | 2004-03-23 23:16:54 +0000 | [diff] [blame] | 181 | PyObject *error; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 182 | } PySocketModule_APIObject; |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 183 | |
| 184 | /* XXX The net effect of the following appears to be to define a function |
| 185 | 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] | 186 | XXX defined there directly. |
| 187 | |
| 188 | >>> It's defined here because other modules might also want to use |
| 189 | >>> the C API. |
| 190 | |
| 191 | */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 192 | #ifndef PySocket_BUILDING_SOCKET |
| 193 | |
| 194 | /* --- C API ----------------------------------------------------*/ |
| 195 | |
| 196 | /* Interfacestructure to C API for other modules. |
Guido van Rossum | be8db07 | 2002-06-07 02:27:50 +0000 | [diff] [blame] | 197 | Call PySocketModule_ImportModuleAndAPI() to initialize this |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 198 | structure. After that usage is simple: |
| 199 | |
| 200 | if (!PyArg_ParseTuple(args, "O!|zz:ssl", |
| 201 | &PySocketModule.Sock_Type, (PyObject*)&Sock, |
| 202 | &key_file, &cert_file)) |
| 203 | return NULL; |
| 204 | ... |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 205 | */ |
| 206 | |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 207 | static |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 208 | PySocketModule_APIObject PySocketModule; |
| 209 | |
| 210 | /* You *must* call this before using any of the functions in |
| 211 | PySocketModule and check its outcome; otherwise all accesses will |
| 212 | result in a segfault. Returns 0 on success. */ |
| 213 | |
| 214 | #ifndef DPRINTF |
| 215 | # define DPRINTF if (0) printf |
| 216 | #endif |
| 217 | |
| 218 | static |
| 219 | int PySocketModule_ImportModuleAndAPI(void) |
| 220 | { |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 221 | PyObject *mod = 0, *v = 0; |
| 222 | char *apimodule = PySocket_MODULE_NAME; |
| 223 | char *apiname = PySocket_CAPI_NAME; |
| 224 | void *api; |
| 225 | |
| 226 | DPRINTF("Importing the %s C API...\n", apimodule); |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 227 | mod = PyImport_ImportModuleNoBlock(apimodule); |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 228 | if (mod == NULL) |
| 229 | goto onError; |
| 230 | DPRINTF(" %s package found\n", apimodule); |
| 231 | v = PyObject_GetAttrString(mod, apiname); |
| 232 | if (v == NULL) |
| 233 | goto onError; |
| 234 | Py_DECREF(mod); |
| 235 | DPRINTF(" API object %s found\n", apiname); |
| 236 | api = PyCObject_AsVoidPtr(v); |
| 237 | if (api == NULL) |
| 238 | goto onError; |
| 239 | Py_DECREF(v); |
| 240 | memcpy(&PySocketModule, api, sizeof(PySocketModule)); |
| 241 | DPRINTF(" API object loaded and initialized.\n"); |
| 242 | return 0; |
| 243 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 244 | onError: |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 245 | DPRINTF(" not found.\n"); |
| 246 | Py_XDECREF(mod); |
| 247 | Py_XDECREF(v); |
| 248 | return -1; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 251 | #endif /* !PySocket_BUILDING_SOCKET */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 252 | |
| 253 | #ifdef __cplusplus |
| 254 | } |
| 255 | #endif |
| 256 | #endif /* !Py__SOCKET_H */ |