(root)/
Python-3.12.0/
Modules/
socketmodule.h
       1  /* Socket module header file */
       2  
       3  /* Includes needed for the sockaddr_* symbols below */
       4  #ifndef MS_WINDOWS
       5  #ifdef __VMS
       6  #   include <socket.h>
       7  # else
       8  #   include <sys/socket.h>
       9  # endif
      10  # include <netinet/in.h>
      11  # include <netinet/tcp.h>
      12  
      13  #else /* MS_WINDOWS */
      14  # include <winsock2.h>
      15  
      16  /*
      17   * If Windows has bluetooth support, include bluetooth constants.
      18   */
      19  #ifdef AF_BTH
      20  # include <ws2bth.h>
      21  # include <pshpack1.h>
      22  
      23  /*
      24   * The current implementation assumes the bdaddr in the sockaddr structs
      25   * will be a bdaddr_t. We treat this as an opaque type: on *nix systems, it
      26   * will be a struct with a single member (an array of six bytes). On windows,
      27   * we typedef this to ULONGLONG to match the Windows definition.
      28   */
      29  typedef ULONGLONG bdaddr_t;
      30  
      31  /*
      32   * Redefine SOCKADDR_BTH to provide names compatible with _BT_RC_MEMB() macros.
      33   */
      34  struct SOCKADDR_BTH_REDEF {
      35      union {
      36          USHORT    addressFamily;
      37          USHORT    family;
      38      };
      39  
      40      union {
      41          ULONGLONG btAddr;
      42          bdaddr_t  bdaddr;
      43      };
      44  
      45      GUID      serviceClassId;
      46  
      47      union {
      48          ULONG     port;
      49          ULONG     channel;
      50      };
      51  
      52  };
      53  # include <poppack.h>
      54  #endif
      55  
      56  /* Windows 'supports' CMSG_LEN, but does not follow the POSIX standard
      57   * interface at all, so there is no point including the code that
      58   * attempts to use it.
      59   */
      60  # ifdef PySocket_BUILDING_SOCKET
      61  #  undef CMSG_LEN
      62  # endif
      63  # include <ws2tcpip.h>
      64  /* VC6 is shipped with old platform headers, and does not have MSTcpIP.h
      65   * Separate SDKs have all the functions we want, but older ones don't have
      66   * any version information.
      67   * I use SIO_GET_MULTICAST_FILTER to detect a decent SDK.
      68   */
      69  # ifdef SIO_GET_MULTICAST_FILTER
      70  #  include <mstcpip.h> /* for SIO_RCVALL */
      71  #  define HAVE_ADDRINFO
      72  #  define HAVE_SOCKADDR_STORAGE
      73  #  define HAVE_GETADDRINFO
      74  #  define HAVE_GETNAMEINFO
      75  #  define ENABLE_IPV6
      76  # else
      77  typedef int socklen_t;
      78  # endif /* IPPROTO_IPV6 */
      79  
      80  /* Remove ifdef once Py_WINVER >= 0x0604
      81   * socket.h only defines AF_HYPERV if _WIN32_WINNT is at that level or higher
      82   * so for now it's just manually defined.
      83   */
      84  # ifndef AF_HYPERV
      85  #  define AF_HYPERV 34
      86  # endif
      87  # include <hvsocket.h>
      88  #endif /* MS_WINDOWS */
      89  
      90  #ifdef HAVE_SYS_UN_H
      91  # include <sys/un.h>
      92  #else
      93  # undef AF_UNIX
      94  #endif
      95  
      96  #ifdef HAVE_LINUX_NETLINK_H
      97  # ifdef HAVE_ASM_TYPES_H
      98  #  include <asm/types.h>
      99  # endif
     100  # include <linux/netlink.h>
     101  #else
     102  #  undef AF_NETLINK
     103  #endif
     104  
     105  #ifdef HAVE_LINUX_QRTR_H
     106  # ifdef HAVE_ASM_TYPES_H
     107  #  include <asm/types.h>
     108  # endif
     109  # include <linux/qrtr.h>
     110  #else
     111  #  undef AF_QIPCRTR
     112  #endif
     113  
     114  #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
     115  #include <bluetooth/bluetooth.h>
     116  #include <bluetooth/rfcomm.h>
     117  #include <bluetooth/l2cap.h>
     118  #include <bluetooth/sco.h>
     119  #include <bluetooth/hci.h>
     120  #endif
     121  
     122  #ifdef HAVE_BLUETOOTH_H
     123  #include <bluetooth.h>
     124  #endif
     125  
     126  #ifdef HAVE_NET_IF_H
     127  # include <net/if.h>
     128  #endif
     129  
     130  #ifdef HAVE_NETPACKET_PACKET_H
     131  # include <sys/ioctl.h>
     132  # include <netpacket/packet.h>
     133  #endif
     134  
     135  #ifdef HAVE_LINUX_TIPC_H
     136  # include <linux/tipc.h>
     137  #endif
     138  
     139  #ifdef HAVE_LINUX_CAN_H
     140  # include <linux/can.h>
     141  #elif defined(HAVE_NETCAN_CAN_H)
     142  # include <netcan/can.h>
     143  #else
     144  # undef AF_CAN
     145  # undef PF_CAN
     146  #endif
     147  
     148  #ifdef HAVE_LINUX_CAN_RAW_H
     149  #include <linux/can/raw.h>
     150  #endif
     151  
     152  #ifdef HAVE_LINUX_CAN_BCM_H
     153  #include <linux/can/bcm.h>
     154  #endif
     155  
     156  #ifdef HAVE_LINUX_CAN_J1939_H
     157  #include <linux/can/j1939.h>
     158  #endif
     159  
     160  #ifdef HAVE_SYS_SYS_DOMAIN_H
     161  #include <sys/sys_domain.h>
     162  #endif
     163  #ifdef HAVE_SYS_KERN_CONTROL_H
     164  #include <sys/kern_control.h>
     165  #endif
     166  
     167  #ifdef HAVE_LINUX_VM_SOCKETS_H
     168  # include <linux/vm_sockets.h>
     169  #else
     170  # undef AF_VSOCK
     171  #endif
     172  
     173  #ifdef HAVE_SOCKADDR_ALG
     174  
     175  # include <linux/if_alg.h>
     176  # ifndef AF_ALG
     177  #  define AF_ALG 38
     178  # endif
     179  # ifndef SOL_ALG
     180  #  define SOL_ALG 279
     181  # endif
     182  
     183  /* Linux 3.19 */
     184  # ifndef ALG_SET_AEAD_ASSOCLEN
     185  #  define ALG_SET_AEAD_ASSOCLEN           4
     186  # endif
     187  # ifndef ALG_SET_AEAD_AUTHSIZE
     188  #  define ALG_SET_AEAD_AUTHSIZE           5
     189  # endif
     190  /* Linux 4.8 */
     191  # ifndef ALG_SET_PUBKEY
     192  #  define ALG_SET_PUBKEY                  6
     193  # endif
     194  
     195  # ifndef ALG_OP_SIGN
     196  #  define ALG_OP_SIGN                     2
     197  # endif
     198  # ifndef ALG_OP_VERIFY
     199  #  define ALG_OP_VERIFY                   3
     200  # endif
     201  
     202  #endif /* HAVE_SOCKADDR_ALG */
     203  
     204  #ifdef __EMSCRIPTEN__
     205  // wasm32-emscripten sockets only support subset of IPv4 and IPv6.
     206  // SCTP protocol crashes runtime.
     207  #ifdef IPPROTO_SCTP
     208  #  undef IPPROTO_SCTP
     209  #endif
     210  // setsockopt() fails with ENOPROTOOPT, getsockopt only supports SO_ERROR.
     211  // undef SO_REUSEADDR and SO_REUSEPORT so they cannot be used.
     212  #ifdef SO_REUSEADDR
     213  #  undef SO_REUSEADDR
     214  #endif
     215  #ifdef SO_REUSEPORT
     216  #  undef SO_REUSEPORT
     217  #endif
     218  #endif // __EMSCRIPTEN__
     219  
     220  #ifndef Py__SOCKET_H
     221  #define Py__SOCKET_H
     222  #ifdef __cplusplus
     223  extern "C" {
     224  #endif
     225  
     226  /* Python module and C API name */
     227  #define PySocket_MODULE_NAME    "_socket"
     228  #define PySocket_CAPI_NAME      "CAPI"
     229  #define PySocket_CAPSULE_NAME   PySocket_MODULE_NAME "." PySocket_CAPI_NAME
     230  
     231  /* Abstract the socket file descriptor type */
     232  #ifdef MS_WINDOWS
     233  typedef SOCKET SOCKET_T;
     234  #       ifdef MS_WIN64
     235  #               define SIZEOF_SOCKET_T 8
     236  #       else
     237  #               define SIZEOF_SOCKET_T 4
     238  #       endif
     239  #else
     240  typedef int SOCKET_T;
     241  #       define SIZEOF_SOCKET_T SIZEOF_INT
     242  #endif
     243  
     244  #if SIZEOF_SOCKET_T <= SIZEOF_LONG
     245  #define PyLong_FromSocket_t(fd) PyLong_FromLong((SOCKET_T)(fd))
     246  #define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLong(fd)
     247  #else
     248  #define PyLong_FromSocket_t(fd) PyLong_FromLongLong((SOCKET_T)(fd))
     249  #define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLongLong(fd)
     250  #endif
     251  
     252  // AF_HYPERV is only supported on Windows
     253  #if defined(AF_HYPERV) && defined(MS_WINDOWS)
     254  #  define HAVE_AF_HYPERV
     255  #endif
     256  
     257  /* Socket address */
     258  typedef union sock_addr {
     259      struct sockaddr_in in;
     260      struct sockaddr sa;
     261  #ifdef AF_UNIX
     262      struct sockaddr_un un;
     263  #endif
     264  #ifdef AF_NETLINK
     265      struct sockaddr_nl nl;
     266  #endif
     267  #ifdef ENABLE_IPV6
     268      struct sockaddr_in6 in6;
     269      struct sockaddr_storage storage;
     270  #endif
     271  #if defined(HAVE_BLUETOOTH_H) && defined(__FreeBSD__)
     272      struct sockaddr_l2cap bt_l2;
     273      struct sockaddr_rfcomm bt_rc;
     274      struct sockaddr_sco bt_sco;
     275      struct sockaddr_hci bt_hci;
     276  #elif defined(HAVE_BLUETOOTH_BLUETOOTH_H)
     277      struct sockaddr_l2 bt_l2;
     278      struct sockaddr_rc bt_rc;
     279      struct sockaddr_sco bt_sco;
     280      struct sockaddr_hci bt_hci;
     281  #elif defined(MS_WINDOWS)
     282      struct SOCKADDR_BTH_REDEF bt_rc;
     283  #endif
     284  #ifdef HAVE_NETPACKET_PACKET_H
     285      struct sockaddr_ll ll;
     286  #endif
     287  #if defined(HAVE_LINUX_CAN_H) || defined(HAVE_NETCAN_CAN_H)
     288      struct sockaddr_can can;
     289  #endif
     290  #ifdef HAVE_SYS_KERN_CONTROL_H
     291      struct sockaddr_ctl ctl;
     292  #endif
     293  #ifdef HAVE_SOCKADDR_ALG
     294      struct sockaddr_alg alg;
     295  #endif
     296  #ifdef AF_QIPCRTR
     297      struct sockaddr_qrtr sq;
     298  #endif
     299  #ifdef AF_VSOCK
     300      struct sockaddr_vm vm;
     301  #endif
     302  #ifdef HAVE_LINUX_TIPC_H
     303      struct sockaddr_tipc tipc;
     304  #endif
     305  #ifdef HAVE_AF_HYPERV
     306      SOCKADDR_HV hv;
     307  #endif
     308  } sock_addr_t;
     309  
     310  /* The object holding a socket.  It holds some extra information,
     311     like the address family, which is used to decode socket address
     312     arguments properly. */
     313  
     314  typedef struct {
     315      PyObject_HEAD
     316      SOCKET_T sock_fd;           /* Socket file descriptor */
     317      int sock_family;            /* Address family, e.g., AF_INET */
     318      int sock_type;              /* Socket type, e.g., SOCK_STREAM */
     319      int sock_proto;             /* Protocol type, usually 0 */
     320      PyObject *(*errorhandler)(void); /* Error handler; checks
     321                                          errno, returns NULL and
     322                                          sets a Python exception */
     323      _PyTime_t sock_timeout;     /* Operation timeout in seconds;
     324                                          0.0 means non-blocking */
     325      struct _socket_state *state;
     326  } PySocketSockObject;
     327  
     328  /* --- C API ----------------------------------------------------*/
     329  
     330  /* Short explanation of what this C API export mechanism does
     331     and how it works:
     332  
     333      The _ssl module needs access to the type object defined in
     334      the _socket module. Since cross-DLL linking introduces a lot of
     335      problems on many platforms, the "trick" is to wrap the
     336      C API of a module in a struct which then gets exported to
     337      other modules via a PyCapsule.
     338  
     339      The code in socketmodule.c defines this struct (which currently
     340      only contains the type object reference, but could very
     341      well also include other C APIs needed by other modules)
     342      and exports it as PyCapsule via the module dictionary
     343      under the name "CAPI".
     344  
     345      Other modules can now include the socketmodule.h file
     346      which defines the needed C APIs to import and set up
     347      a static copy of this struct in the importing module.
     348  
     349      After initialization, the importing module can then
     350      access the C APIs from the _socket module by simply
     351      referring to the static struct, e.g.
     352  
     353      Load _socket module and its C API; this sets up the global
     354      PySocketModule:
     355  
     356      if (PySocketModule_ImportModuleAndAPI())
     357          return;
     358  
     359  
     360      Now use the C API as if it were defined in the using
     361      module:
     362  
     363      if (!PyArg_ParseTuple(args, "O!|zz:ssl",
     364  
     365                            PySocketModule.Sock_Type,
     366  
     367                            (PyObject*)&Sock,
     368                            &key_file, &cert_file))
     369          return NULL;
     370  
     371      Support could easily be extended to export more C APIs/symbols
     372      this way. Currently, only the type object is exported,
     373      other candidates would be socket constructors and socket
     374      access functions.
     375  
     376  */
     377  
     378  /* C API for usage by other Python modules.
     379   * Always add new things to the end for binary compatibility. */
     380  typedef struct {
     381      PyTypeObject *Sock_Type;
     382      PyObject *error;
     383      PyObject *timeout_error;
     384  } PySocketModule_APIObject;
     385  
     386  #define PySocketModule_ImportModuleAndAPI() PyCapsule_Import(PySocket_CAPSULE_NAME, 1)
     387  
     388  #ifdef __cplusplus
     389  }
     390  #endif
     391  #endif /* !Py__SOCKET_H */