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