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> |
Steve Dower | 3e96f32 | 2015-03-02 08:01:10 -0800 | [diff] [blame] | 17 | /* Windows 'supports' CMSG_LEN, but does not follow the POSIX standard |
| 18 | * interface at all, so there is no point including the code that |
| 19 | * attempts to use it. |
| 20 | */ |
| 21 | # ifdef PySocket_BUILDING_SOCKET |
| 22 | # undef CMSG_LEN |
| 23 | # endif |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 24 | # include <ws2tcpip.h> |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 25 | /* VC6 is shipped with old platform headers, and does not have MSTcpIP.h |
| 26 | * 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] | 27 | * any version information. |
Martin v. Löwis | b072cf2 | 2008-06-14 11:59:52 +0000 | [diff] [blame] | 28 | * 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] | 29 | */ |
Martin v. Löwis | b072cf2 | 2008-06-14 11:59:52 +0000 | [diff] [blame] | 30 | # ifdef SIO_GET_MULTICAST_FILTER |
erikjanss | 6cf8255 | 2018-07-25 02:41:46 +0200 | [diff] [blame] | 31 | # include <mstcpip.h> /* for SIO_RCVALL */ |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 32 | # define HAVE_ADDRINFO |
| 33 | # define HAVE_SOCKADDR_STORAGE |
| 34 | # define HAVE_GETADDRINFO |
| 35 | # define HAVE_GETNAMEINFO |
| 36 | # define ENABLE_IPV6 |
| 37 | # else |
| 38 | typedef int socklen_t; |
| 39 | # endif /* IPPROTO_IPV6 */ |
| 40 | #endif /* MS_WINDOWS */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 41 | |
| 42 | #ifdef HAVE_SYS_UN_H |
| 43 | # include <sys/un.h> |
| 44 | #else |
| 45 | # undef AF_UNIX |
| 46 | #endif |
| 47 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 48 | #ifdef HAVE_LINUX_NETLINK_H |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 49 | # ifdef HAVE_ASM_TYPES_H |
| 50 | # include <asm/types.h> |
| 51 | # endif |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 52 | # include <linux/netlink.h> |
| 53 | #else |
| 54 | # undef AF_NETLINK |
| 55 | #endif |
| 56 | |
Bjorn Andersson | bb81651 | 2018-09-26 06:47:52 -0700 | [diff] [blame] | 57 | #ifdef HAVE_LINUX_QRTR_H |
| 58 | # ifdef HAVE_ASM_TYPES_H |
| 59 | # include <asm/types.h> |
| 60 | # endif |
| 61 | # include <linux/qrtr.h> |
| 62 | #else |
| 63 | # undef AF_QIPCRTR |
| 64 | #endif |
| 65 | |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 66 | #ifdef HAVE_BLUETOOTH_BLUETOOTH_H |
| 67 | #include <bluetooth/bluetooth.h> |
| 68 | #include <bluetooth/rfcomm.h> |
| 69 | #include <bluetooth/l2cap.h> |
| 70 | #include <bluetooth/sco.h> |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 71 | #include <bluetooth/hci.h> |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
Hye-Shik Chang | 96c44658 | 2004-02-02 08:48:45 +0000 | [diff] [blame] | 74 | #ifdef HAVE_BLUETOOTH_H |
| 75 | #include <bluetooth.h> |
| 76 | #endif |
| 77 | |
Nadeem Vawda | d74b593 | 2011-05-15 13:16:22 +0200 | [diff] [blame] | 78 | #ifdef HAVE_NET_IF_H |
| 79 | # include <net/if.h> |
| 80 | #endif |
| 81 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 82 | #ifdef HAVE_NETPACKET_PACKET_H |
| 83 | # include <sys/ioctl.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 84 | # include <netpacket/packet.h> |
| 85 | #endif |
| 86 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 87 | #ifdef HAVE_LINUX_TIPC_H |
| 88 | # include <linux/tipc.h> |
| 89 | #endif |
| 90 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 91 | #ifdef HAVE_LINUX_CAN_H |
Serhiy Storchaka | d318715 | 2017-11-09 18:00:38 +0200 | [diff] [blame] | 92 | # include <linux/can.h> |
| 93 | #else |
| 94 | # undef AF_CAN |
| 95 | # undef PF_CAN |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 96 | #endif |
| 97 | |
| 98 | #ifdef HAVE_LINUX_CAN_RAW_H |
| 99 | #include <linux/can/raw.h> |
| 100 | #endif |
| 101 | |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 102 | #ifdef HAVE_LINUX_CAN_BCM_H |
| 103 | #include <linux/can/bcm.h> |
| 104 | #endif |
| 105 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 106 | #ifdef HAVE_SYS_SYS_DOMAIN_H |
| 107 | #include <sys/sys_domain.h> |
| 108 | #endif |
| 109 | #ifdef HAVE_SYS_KERN_CONTROL_H |
| 110 | #include <sys/kern_control.h> |
| 111 | #endif |
| 112 | |
caavery | effc12f | 2017-09-06 18:18:10 -0400 | [diff] [blame] | 113 | #ifdef HAVE_LINUX_VM_SOCKETS_H |
| 114 | # include <linux/vm_sockets.h> |
| 115 | #else |
| 116 | # undef AF_VSOCK |
| 117 | #endif |
| 118 | |
Thomas Herzog | 2d7102e | 2018-09-02 00:13:57 +0200 | [diff] [blame] | 119 | #ifdef HAVE_SOCKADDR_ALG |
Christian Heimes | d37c068 | 2017-02-22 12:12:00 +0100 | [diff] [blame] | 120 | |
Thomas Herzog | 2d7102e | 2018-09-02 00:13:57 +0200 | [diff] [blame] | 121 | # include <linux/if_alg.h> |
| 122 | # ifndef AF_ALG |
| 123 | # define AF_ALG 38 |
| 124 | # endif |
| 125 | # ifndef SOL_ALG |
| 126 | # define SOL_ALG 279 |
| 127 | # endif |
| 128 | |
| 129 | /* Linux 3.19 */ |
| 130 | # ifndef ALG_SET_AEAD_ASSOCLEN |
| 131 | # define ALG_SET_AEAD_ASSOCLEN 4 |
| 132 | # endif |
| 133 | # ifndef ALG_SET_AEAD_AUTHSIZE |
| 134 | # define ALG_SET_AEAD_AUTHSIZE 5 |
| 135 | # endif |
| 136 | /* Linux 4.8 */ |
| 137 | # ifndef ALG_SET_PUBKEY |
| 138 | # define ALG_SET_PUBKEY 6 |
| 139 | # endif |
| 140 | |
| 141 | # ifndef ALG_OP_SIGN |
| 142 | # define ALG_OP_SIGN 2 |
| 143 | # endif |
| 144 | # ifndef ALG_OP_VERIFY |
| 145 | # define ALG_OP_VERIFY 3 |
| 146 | # endif |
Christian Heimes | d37c068 | 2017-02-22 12:12:00 +0100 | [diff] [blame] | 147 | |
| 148 | #endif /* HAVE_SOCKADDR_ALG */ |
| 149 | |
| 150 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 151 | #ifndef Py__SOCKET_H |
| 152 | #define Py__SOCKET_H |
| 153 | #ifdef __cplusplus |
| 154 | extern "C" { |
| 155 | #endif |
| 156 | |
| 157 | /* Python module and C API name */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | #define PySocket_MODULE_NAME "_socket" |
| 159 | #define PySocket_CAPI_NAME "CAPI" |
| 160 | #define PySocket_CAPSULE_NAME PySocket_MODULE_NAME "." PySocket_CAPI_NAME |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 161 | |
| 162 | /* Abstract the socket file descriptor type */ |
| 163 | #ifdef MS_WINDOWS |
| 164 | typedef SOCKET SOCKET_T; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | # ifdef MS_WIN64 |
| 166 | # define SIZEOF_SOCKET_T 8 |
| 167 | # else |
| 168 | # define SIZEOF_SOCKET_T 4 |
| 169 | # endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 170 | #else |
| 171 | typedef int SOCKET_T; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | # define SIZEOF_SOCKET_T SIZEOF_INT |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 173 | #endif |
| 174 | |
Antoine Pitrou | 67c7ce4 | 2010-08-28 20:42:55 +0000 | [diff] [blame] | 175 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG |
| 176 | #define PyLong_FromSocket_t(fd) PyLong_FromLong((SOCKET_T)(fd)) |
| 177 | #define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLong(fd) |
| 178 | #else |
| 179 | #define PyLong_FromSocket_t(fd) PyLong_FromLongLong((SOCKET_T)(fd)) |
| 180 | #define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLongLong(fd) |
| 181 | #endif |
| 182 | |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 183 | /* Socket address */ |
| 184 | typedef union sock_addr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | struct sockaddr_in in; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 186 | struct sockaddr sa; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 187 | #ifdef AF_UNIX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 188 | struct sockaddr_un un; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 189 | #endif |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 190 | #ifdef AF_NETLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | struct sockaddr_nl nl; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 192 | #endif |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 193 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | struct sockaddr_in6 in6; |
| 195 | struct sockaddr_storage storage; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 196 | #endif |
| 197 | #ifdef HAVE_BLUETOOTH_BLUETOOTH_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 | struct sockaddr_l2 bt_l2; |
| 199 | struct sockaddr_rc bt_rc; |
| 200 | struct sockaddr_sco bt_sco; |
| 201 | struct sockaddr_hci bt_hci; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 202 | #endif |
| 203 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | struct sockaddr_ll ll; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 205 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 206 | #ifdef HAVE_LINUX_CAN_H |
| 207 | struct sockaddr_can can; |
| 208 | #endif |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 209 | #ifdef HAVE_SYS_KERN_CONTROL_H |
| 210 | struct sockaddr_ctl ctl; |
| 211 | #endif |
Christian Heimes | d37c068 | 2017-02-22 12:12:00 +0100 | [diff] [blame] | 212 | #ifdef HAVE_SOCKADDR_ALG |
| 213 | struct sockaddr_alg alg; |
| 214 | #endif |
Bjorn Andersson | bb81651 | 2018-09-26 06:47:52 -0700 | [diff] [blame] | 215 | #ifdef AF_QIPCRTR |
| 216 | struct sockaddr_qrtr sq; |
| 217 | #endif |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 218 | #ifdef AF_VSOCK |
caavery | effc12f | 2017-09-06 18:18:10 -0400 | [diff] [blame] | 219 | struct sockaddr_vm vm; |
| 220 | #endif |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 221 | } sock_addr_t; |
| 222 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 223 | /* The object holding a socket. It holds some extra information, |
| 224 | like the address family, which is used to decode socket address |
| 225 | arguments properly. */ |
| 226 | |
| 227 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | PyObject_HEAD |
| 229 | SOCKET_T sock_fd; /* Socket file descriptor */ |
| 230 | int sock_family; /* Address family, e.g., AF_INET */ |
| 231 | int sock_type; /* Socket type, e.g., SOCK_STREAM */ |
| 232 | int sock_proto; /* Protocol type, usually 0 */ |
| 233 | PyObject *(*errorhandler)(void); /* Error handler; checks |
| 234 | errno, returns NULL and |
| 235 | sets a Python exception */ |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 236 | _PyTime_t sock_timeout; /* Operation timeout in seconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | 0.0 means non-blocking */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 238 | } PySocketSockObject; |
| 239 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 240 | /* --- C API ----------------------------------------------------*/ |
| 241 | |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 242 | /* Short explanation of what this C API export mechanism does |
| 243 | and how it works: |
| 244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | The _ssl module needs access to the type object defined in |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 246 | the _socket module. Since cross-DLL linking introduces a lot of |
| 247 | problems on many platforms, the "trick" is to wrap the |
| 248 | 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] | 249 | other modules via a PyCapsule. |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 250 | |
| 251 | The code in socketmodule.c defines this struct (which currently |
| 252 | only contains the type object reference, but could very |
| 253 | well also include other C APIs needed by other modules) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 254 | and exports it as PyCapsule via the module dictionary |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 255 | under the name "CAPI". |
| 256 | |
| 257 | Other modules can now include the socketmodule.h file |
| 258 | which defines the needed C APIs to import and set up |
| 259 | a static copy of this struct in the importing module. |
| 260 | |
| 261 | After initialization, the importing module can then |
| 262 | access the C APIs from the _socket module by simply |
| 263 | referring to the static struct, e.g. |
| 264 | |
| 265 | Load _socket module and its C API; this sets up the global |
| 266 | PySocketModule: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | |
| 268 | if (PySocketModule_ImportModuleAndAPI()) |
| 269 | return; |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 270 | |
| 271 | |
| 272 | Now use the C API as if it were defined in the using |
| 273 | module: |
| 274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | if (!PyArg_ParseTuple(args, "O!|zz:ssl", |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | PySocketModule.Sock_Type, |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | (PyObject*)&Sock, |
| 280 | &key_file, &cert_file)) |
| 281 | return NULL; |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 282 | |
| 283 | Support could easily be extended to export more C APIs/symbols |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | this way. Currently, only the type object is exported, |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 285 | other candidates would be socket constructors and socket |
| 286 | access functions. |
| 287 | |
| 288 | */ |
| 289 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 290 | /* C API for usage by other Python modules */ |
| 291 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | PyTypeObject *Sock_Type; |
| 293 | PyObject *error; |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 294 | PyObject *timeout_error; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 295 | } PySocketModule_APIObject; |
Tim Peters | 6f5505a | 2002-02-17 03:58:51 +0000 | [diff] [blame] | 296 | |
Benjamin Peterson | 81c447f | 2009-05-06 02:43:58 +0000 | [diff] [blame] | 297 | #define PySocketModule_ImportModuleAndAPI() PyCapsule_Import(PySocket_CAPSULE_NAME, 1) |
Marc-André Lemburg | 666e70d | 2002-02-25 14:45:40 +0000 | [diff] [blame] | 298 | |
Benjamin Peterson | 2349020 | 2009-08-12 18:11:03 +0000 | [diff] [blame] | 299 | #ifdef __cplusplus |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 300 | } |
| 301 | #endif |
| 302 | #endif /* !Py__SOCKET_H */ |