socketmodule.h 6.06 KB
Newer Older
1 2 3 4
/* Socket module header file */

/* Includes needed for the sockaddr_* symbols below */
#ifndef MS_WINDOWS
5 6 7 8 9
#ifdef __VMS
#   include <socket.h>
# else
#   include <sys/socket.h>
# endif
10
# include <netinet/in.h>
11
# if !defined(__CYGWIN__)
12 13 14 15
#  include <netinet/tcp.h>
# endif

#else /* MS_WINDOWS */
16 17
# include <winsock2.h>
# include <ws2tcpip.h>
18 19
/* VC6 is shipped with old platform headers, and does not have MSTcpIP.h
 * Separate SDKs have all the functions we want, but older ones don't have
20
 * any version information.
21
 * I use SIO_GET_MULTICAST_FILTER to detect a decent SDK.
22
 */
23
# ifdef SIO_GET_MULTICAST_FILTER
24 25 26 27 28 29 30 31 32 33
#  include <MSTcpIP.h> /* for SIO_RCVALL */
#  define HAVE_ADDRINFO
#  define HAVE_SOCKADDR_STORAGE
#  define HAVE_GETADDRINFO
#  define HAVE_GETNAMEINFO
#  define ENABLE_IPV6
# else
typedef int socklen_t;
# endif /* IPPROTO_IPV6 */
#endif /* MS_WINDOWS */
34 35 36 37 38 39 40

#ifdef HAVE_SYS_UN_H
# include <sys/un.h>
#else
# undef AF_UNIX
#endif

41
#ifdef HAVE_LINUX_NETLINK_H
42 43 44
# ifdef HAVE_ASM_TYPES_H
#  include <asm/types.h>
# endif
45 46 47 48 49
# include <linux/netlink.h>
#else
#  undef AF_NETLINK
#endif

50 51 52 53 54
#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <bluetooth/l2cap.h>
#include <bluetooth/sco.h>
55
#include <bluetooth/hci.h>
56 57
#endif

58 59 60 61
#ifdef HAVE_BLUETOOTH_H
#include <bluetooth.h>
#endif

62 63 64 65
#ifdef HAVE_NET_IF_H
# include <net/if.h>
#endif

66 67 68 69 70
#ifdef HAVE_NETPACKET_PACKET_H
# include <sys/ioctl.h>
# include <netpacket/packet.h>
#endif

71 72 73 74
#ifdef HAVE_LINUX_TIPC_H
# include <linux/tipc.h>
#endif

75 76 77 78 79 80 81 82
#ifdef HAVE_LINUX_CAN_H
#include <linux/can.h>
#endif

#ifdef HAVE_LINUX_CAN_RAW_H
#include <linux/can/raw.h>
#endif

83 84 85 86
#ifdef HAVE_LINUX_CAN_BCM_H
#include <linux/can/bcm.h>
#endif

87 88 89 90 91 92 93
#ifdef HAVE_SYS_SYS_DOMAIN_H
#include <sys/sys_domain.h>
#endif
#ifdef HAVE_SYS_KERN_CONTROL_H
#include <sys/kern_control.h>
#endif

94 95 96 97 98 99 100
#ifndef Py__SOCKET_H
#define Py__SOCKET_H
#ifdef __cplusplus
extern "C" {
#endif

/* Python module and C API name */
101 102 103
#define PySocket_MODULE_NAME    "_socket"
#define PySocket_CAPI_NAME      "CAPI"
#define PySocket_CAPSULE_NAME   PySocket_MODULE_NAME "." PySocket_CAPI_NAME
104 105 106 107

/* Abstract the socket file descriptor type */
#ifdef MS_WINDOWS
typedef SOCKET SOCKET_T;
108 109 110 111 112
#       ifdef MS_WIN64
#               define SIZEOF_SOCKET_T 8
#       else
#               define SIZEOF_SOCKET_T 4
#       endif
113 114
#else
typedef int SOCKET_T;
115
#       define SIZEOF_SOCKET_T SIZEOF_INT
116 117
#endif

118 119 120 121 122 123 124 125
#if SIZEOF_SOCKET_T <= SIZEOF_LONG
#define PyLong_FromSocket_t(fd) PyLong_FromLong((SOCKET_T)(fd))
#define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLong(fd)
#else
#define PyLong_FromSocket_t(fd) PyLong_FromLongLong((SOCKET_T)(fd))
#define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLongLong(fd)
#endif

126 127
/* Socket address */
typedef union sock_addr {
128
    struct sockaddr_in in;
129
    struct sockaddr sa;
130
#ifdef AF_UNIX
131
    struct sockaddr_un un;
132
#endif
133
#ifdef AF_NETLINK
134
    struct sockaddr_nl nl;
135
#endif
136
#ifdef ENABLE_IPV6
137 138
    struct sockaddr_in6 in6;
    struct sockaddr_storage storage;
139 140
#endif
#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
141 142 143 144
    struct sockaddr_l2 bt_l2;
    struct sockaddr_rc bt_rc;
    struct sockaddr_sco bt_sco;
    struct sockaddr_hci bt_hci;
145 146
#endif
#ifdef HAVE_NETPACKET_PACKET_H
147
    struct sockaddr_ll ll;
148
#endif
149 150 151
#ifdef HAVE_LINUX_CAN_H
    struct sockaddr_can can;
#endif
152 153 154
#ifdef HAVE_SYS_KERN_CONTROL_H
    struct sockaddr_ctl ctl;
#endif
155 156
} sock_addr_t;

157 158 159 160 161
/* The object holding a socket.  It holds some extra information,
   like the address family, which is used to decode socket address
   arguments properly. */

typedef struct {
162 163 164 165 166 167 168 169 170 171
    PyObject_HEAD
    SOCKET_T sock_fd;           /* Socket file descriptor */
    int sock_family;            /* Address family, e.g., AF_INET */
    int sock_type;              /* Socket type, e.g., SOCK_STREAM */
    int sock_proto;             /* Protocol type, usually 0 */
    PyObject *(*errorhandler)(void); /* Error handler; checks
                                        errno, returns NULL and
                                        sets a Python exception */
    double sock_timeout;                 /* Operation timeout in seconds;
                                        0.0 means non-blocking */
172 173 174 175
} PySocketSockObject;

/* --- C API ----------------------------------------------------*/

176 177 178
/* Short explanation of what this C API export mechanism does
   and how it works:

179
    The _ssl module needs access to the type object defined in
180 181 182
    the _socket module. Since cross-DLL linking introduces a lot of
    problems on many platforms, the "trick" is to wrap the
    C API of a module in a struct which then gets exported to
183
    other modules via a PyCapsule.
184 185 186 187

    The code in socketmodule.c defines this struct (which currently
    only contains the type object reference, but could very
    well also include other C APIs needed by other modules)
188
    and exports it as PyCapsule via the module dictionary
189 190 191 192 193 194 195 196 197 198 199 200
    under the name "CAPI".

    Other modules can now include the socketmodule.h file
    which defines the needed C APIs to import and set up
    a static copy of this struct in the importing module.

    After initialization, the importing module can then
    access the C APIs from the _socket module by simply
    referring to the static struct, e.g.

    Load _socket module and its C API; this sets up the global
    PySocketModule:
201 202 203

    if (PySocketModule_ImportModuleAndAPI())
        return;
204 205 206 207 208


    Now use the C API as if it were defined in the using
    module:

209
    if (!PyArg_ParseTuple(args, "O!|zz:ssl",
210

211
                          PySocketModule.Sock_Type,
212

213 214 215
                          (PyObject*)&Sock,
                          &key_file, &cert_file))
        return NULL;
216 217

    Support could easily be extended to export more C APIs/symbols
218
    this way. Currently, only the type object is exported,
219 220 221 222 223
    other candidates would be socket constructors and socket
    access functions.

*/

224 225
/* C API for usage by other Python modules */
typedef struct {
226 227
    PyTypeObject *Sock_Type;
    PyObject *error;
228
    PyObject *timeout_error;
229
} PySocketModule_APIObject;
230

231
#define PySocketModule_ImportModuleAndAPI() PyCapsule_Import(PySocket_CAPSULE_NAME, 1)
232

233
#ifdef __cplusplus
234 235 236
}
#endif
#endif /* !Py__SOCKET_H */