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