socketmodule.h 5.65 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__) || (defined(PYOS_OS2) && defined(PYCC_VACPP)))
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
#ifndef Py__SOCKET_H
#define Py__SOCKET_H
#ifdef __cplusplus
extern "C" {
#endif

/* Python module and C API name */
82 83 84
#define PySocket_MODULE_NAME    "_socket"
#define PySocket_CAPI_NAME      "CAPI"
#define PySocket_CAPSULE_NAME   PySocket_MODULE_NAME "." PySocket_CAPI_NAME
85 86 87 88

/* Abstract the socket file descriptor type */
#ifdef MS_WINDOWS
typedef SOCKET SOCKET_T;
89 90 91 92 93
#       ifdef MS_WIN64
#               define SIZEOF_SOCKET_T 8
#       else
#               define SIZEOF_SOCKET_T 4
#       endif
94 95
#else
typedef int SOCKET_T;
96
#       define SIZEOF_SOCKET_T SIZEOF_INT
97 98
#endif

99 100 101 102 103 104 105 106
#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

107 108
/* Socket address */
typedef union sock_addr {
109
    struct sockaddr_in in;
110
#ifdef AF_UNIX
111
    struct sockaddr_un un;
112
#endif
113
#ifdef AF_NETLINK
114
    struct sockaddr_nl nl;
115
#endif
116
#ifdef ENABLE_IPV6
117 118
    struct sockaddr_in6 in6;
    struct sockaddr_storage storage;
119 120
#endif
#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
121 122 123 124
    struct sockaddr_l2 bt_l2;
    struct sockaddr_rc bt_rc;
    struct sockaddr_sco bt_sco;
    struct sockaddr_hci bt_hci;
125 126
#endif
#ifdef HAVE_NETPACKET_PACKET_H
127
    struct sockaddr_ll ll;
128 129 130
#endif
} sock_addr_t;

131 132 133 134 135
/* 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 {
136 137 138 139 140 141 142 143 144 145
    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 */
146 147 148 149
} PySocketSockObject;

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

150 151 152
/* Short explanation of what this C API export mechanism does
   and how it works:

153
    The _ssl module needs access to the type object defined in
154 155 156
    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
157
    other modules via a PyCapsule.
158 159 160 161

    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)
162
    and exports it as PyCapsule via the module dictionary
163 164 165 166 167 168 169 170 171 172 173 174
    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:
175 176 177

    if (PySocketModule_ImportModuleAndAPI())
        return;
178 179 180 181 182


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

183
    if (!PyArg_ParseTuple(args, "O!|zz:ssl",
184

185
                          PySocketModule.Sock_Type,
186

187 188 189
                          (PyObject*)&Sock,
                          &key_file, &cert_file))
        return NULL;
190 191

    Support could easily be extended to export more C APIs/symbols
192
    this way. Currently, only the type object is exported,
193 194 195 196 197
    other candidates would be socket constructors and socket
    access functions.

*/

198 199
/* C API for usage by other Python modules */
typedef struct {
200 201
    PyTypeObject *Sock_Type;
    PyObject *error;
202
    PyObject *timeout_error;
203
} PySocketModule_APIObject;
204

205
#define PySocketModule_ImportModuleAndAPI() PyCapsule_Import(PySocket_CAPSULE_NAME, 1)
206

207
#ifdef __cplusplus
208 209 210
}
#endif
#endif /* !Py__SOCKET_H */