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> |
Jesus Cea | 14c81ab | 2012-10-05 02:11:36 +0200 | [diff] [blame] | 11 | # if !defined(__CYGWIN__) |
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 | # include <winsock2.h> |
| 17 | # include <ws2tcpip.h> |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 18 | /* VC6 is shipped with old platform headers, and does not have MSTcpIP.h |
| 19 | * Separate SDKs have all the functions we want, but older ones don't have |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 20 | * any version information. |
Martin v. Löwis | b072cf2 | 2008-06-14 11:59:52 +0000 | [diff] [blame] | 21 | * I use SIO_GET_MULTICAST_FILTER to detect a decent SDK. |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 22 | */ |
Martin v. Löwis | b072cf2 | 2008-06-14 11:59:52 +0000 | [diff] [blame] | 23 | # ifdef SIO_GET_MULTICAST_FILTER |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 24 | # include <MSTcpIP.h> /* for SIO_RCVALL */ |
| 25 | # define HAVE_ADDRINFO |
| 26 | # define HAVE_SOCKADDR_STORAGE |
| 27 | # define HAVE_GETADDRINFO |
| 28 | # define HAVE_GETNAMEINFO |
| 29 | # define ENABLE_IPV6 |
| 30 | # else |
| 31 | typedef int socklen_t; |
| 32 | # endif /* IPPROTO_IPV6 */ |
| 33 | #endif /* MS_WINDOWS */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 34 | |
| 35 | #ifdef HAVE_SYS_UN_H |
| 36 | # include <sys/un.h> |
| 37 | #else |
| 38 | # undef AF_UNIX |
| 39 | #endif |
| 40 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 41 | #ifdef HAVE_LINUX_NETLINK_H |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 42 | # ifdef HAVE_ASM_TYPES_H |
| 43 | # include <asm/types.h> |
| 44 | # endif |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 45 | # include <linux/netlink.h> |
| 46 | #else |
| 47 | # undef AF_NETLINK |
| 48 | #endif |
| 49 | |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 50 | #ifdef HAVE_BLUETOOTH_BLUETOOTH_H |
| 51 | #include <bluetooth/bluetooth.h> |
| 52 | #include <bluetooth/rfcomm.h> |
| 53 | #include <bluetooth/l2cap.h> |
| 54 | #include <bluetooth/sco.h> |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 55 | #include <bluetooth/hci.h> |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
Hye-Shik Chang | 96c44658 | 2004-02-02 08:48:45 +0000 | [diff] [blame] | 58 | #ifdef HAVE_BLUETOOTH_H |
| 59 | #include <bluetooth.h> |
| 60 | #endif |
| 61 | |
Nadeem Vawda | d74b593 | 2011-05-15 13:16:22 +0200 | [diff] [blame] | 62 | #ifdef HAVE_NET_IF_H |
| 63 | # include <net/if.h> |
| 64 | #endif |
| 65 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 66 | #ifdef HAVE_NETPACKET_PACKET_H |
| 67 | # include <sys/ioctl.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 68 | # include <netpacket/packet.h> |
| 69 | #endif |
| 70 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 71 | #ifdef HAVE_LINUX_TIPC_H |
| 72 | # include <linux/tipc.h> |
| 73 | #endif |
| 74 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 75 | #ifdef HAVE_LINUX_CAN_H |
| 76 | #include <linux/can.h> |
| 77 | #endif |
| 78 | |
| 79 | #ifdef HAVE_LINUX_CAN_RAW_H |
| 80 | #include <linux/can/raw.h> |
| 81 | #endif |
| 82 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 83 | #ifdef HAVE_SYS_SYS_DOMAIN_H |
| 84 | #include <sys/sys_domain.h> |
| 85 | #endif |
| 86 | #ifdef HAVE_SYS_KERN_CONTROL_H |
| 87 | #include <sys/kern_control.h> |
| 88 | #endif |
| 89 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 90 | #ifndef Py__SOCKET_H |
| 91 | #define Py__SOCKET_H |
| 92 | #ifdef __cplusplus |
| 93 | extern "C" { |
| 94 | #endif |
| 95 | |
| 96 | /* Python module and C API name */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | #define PySocket_MODULE_NAME "_socket" |
| 98 | #define PySocket_CAPI_NAME "CAPI" |
| 99 | #define PySocket_CAPSULE_NAME PySocket_MODULE_NAME "." PySocket_CAPI_NAME |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 100 | |
| 101 | /* Abstract the socket file descriptor type */ |
| 102 | #ifdef MS_WINDOWS |
| 103 | typedef SOCKET SOCKET_T; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | # ifdef MS_WIN64 |
| 105 | # define SIZEOF_SOCKET_T 8 |
| 106 | # else |
| 107 | # define SIZEOF_SOCKET_T 4 |
| 108 | # endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 109 | #else |
| 110 | typedef int SOCKET_T; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | # define SIZEOF_SOCKET_T SIZEOF_INT |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 112 | #endif |
| 113 | |
Antoine Pitrou | 67c7ce4 | 2010-08-28 20:42:55 +0000 | [diff] [blame] | 114 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG |
| 115 | #define PyLong_FromSocket_t(fd) PyLong_FromLong((SOCKET_T)(fd)) |
| 116 | #define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLong(fd) |
| 117 | #else |
| 118 | #define PyLong_FromSocket_t(fd) PyLong_FromLongLong((SOCKET_T)(fd)) |
| 119 | #define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLongLong(fd) |
| 120 | #endif |
| 121 | |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 122 | /* Socket address */ |
| 123 | typedef union sock_addr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | struct sockaddr_in in; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 125 | struct sockaddr sa; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 126 | #ifdef AF_UNIX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | struct sockaddr_un un; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 128 | #endif |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 129 | #ifdef AF_NETLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | struct sockaddr_nl nl; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 131 | #endif |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 132 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | struct sockaddr_in6 in6; |
| 134 | struct sockaddr_storage storage; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 135 | #endif |
| 136 | #ifdef HAVE_BLUETOOTH_BLUETOOTH_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | struct sockaddr_l2 bt_l2; |
| 138 | struct sockaddr_rc bt_rc; |
| 139 | struct sockaddr_sco bt_sco; |
| 140 | struct sockaddr_hci bt_hci; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 141 | #endif |
| 142 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | struct sockaddr_ll ll; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 144 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 145 | #ifdef HAVE_LINUX_CAN_H |
| 146 | struct sockaddr_can can; |
| 147 | #endif |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 148 | #ifdef HAVE_SYS_KERN_CONTROL_H |
| 149 | struct sockaddr_ctl ctl; |
| 150 | #endif |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 151 | } sock_addr_t; |
| 152 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 153 | /* The object holding a socket. It holds some extra information, |
| 154 | like the address family, which is used to decode socket address |
| 155 | arguments properly. */ |
| 156 | |
| 157 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | PyObject_HEAD |
| 159 | SOCKET_T sock_fd; /* Socket file descriptor */ |
| 160 | int sock_family; /* Address family, e.g., AF_INET */ |
| 161 | int sock_type; /* Socket type, e.g., SOCK_STREAM */ |
| 162 | int sock_proto; /* Protocol type, usually 0 */ |
| 163 | PyObject *(*errorhandler)(void); /* Error handler; checks |
| 164 | errno, returns NULL and |
| 165 | sets a Python exception */ |
| 166 | double sock_timeout; /* Operation timeout in seconds; |
| 167 | 0.0 means non-blocking */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 168 | } PySocketSockObject; |
| 169 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 170 | /* --- C API ----------------------------------------------------*/ |
| 171 | |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 172 | /* Short explanation of what this C API export mechanism does |
| 173 | and how it works: |
| 174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | The _ssl module needs access to the type object defined in |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 176 | the _socket module. Since cross-DLL linking introduces a lot of |
| 177 | problems on many platforms, the "trick" is to wrap the |
| 178 | C API of a module in a struct which then gets exported to |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 179 | other modules via a PyCapsule. |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 180 | |
| 181 | The code in socketmodule.c defines this struct (which currently |
| 182 | only contains the type object reference, but could very |
| 183 | well also include other C APIs needed by other modules) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 184 | and exports it as PyCapsule via the module dictionary |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 185 | under the name "CAPI". |
| 186 | |
| 187 | Other modules can now include the socketmodule.h file |
| 188 | which defines the needed C APIs to import and set up |
| 189 | a static copy of this struct in the importing module. |
| 190 | |
| 191 | After initialization, the importing module can then |
| 192 | access the C APIs from the _socket module by simply |
| 193 | referring to the static struct, e.g. |
| 194 | |
| 195 | Load _socket module and its C API; this sets up the global |
| 196 | PySocketModule: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | |
| 198 | if (PySocketModule_ImportModuleAndAPI()) |
| 199 | return; |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 200 | |
| 201 | |
| 202 | Now use the C API as if it were defined in the using |
| 203 | module: |
| 204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 | if (!PyArg_ParseTuple(args, "O!|zz:ssl", |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | PySocketModule.Sock_Type, |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | (PyObject*)&Sock, |
| 210 | &key_file, &cert_file)) |
| 211 | return NULL; |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 212 | |
| 213 | Support could easily be extended to export more C APIs/symbols |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | this way. Currently, only the type object is exported, |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 215 | other candidates would be socket constructors and socket |
| 216 | access functions. |
| 217 | |
| 218 | */ |
| 219 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 220 | /* C API for usage by other Python modules */ |
| 221 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | PyTypeObject *Sock_Type; |
| 223 | PyObject *error; |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 224 | PyObject *timeout_error; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 225 | } PySocketModule_APIObject; |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 226 | |
Benjamin Peterson | 81c447f | 2009-05-06 02:43:58 +0000 | [diff] [blame] | 227 | #define PySocketModule_ImportModuleAndAPI() PyCapsule_Import(PySocket_CAPSULE_NAME, 1) |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 228 | |
Benjamin Peterson | 2349020 | 2009-08-12 18:11:03 +0000 | [diff] [blame] | 229 | #ifdef __cplusplus |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 230 | } |
| 231 | #endif |
| 232 | #endif /* !Py__SOCKET_H */ |