UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
port_sock.hpp
Go to the documentation of this file.
1#ifndef UPnPsdk_INCLUDE_PORT_SOCK_HPP
2#define UPnPsdk_INCLUDE_PORT_SOCK_HPP
3// Copyright (C) 2021+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
4// Redistribution only with this Copyright remark. Last modified: 2024-10-07
13// clang-format off
14
15// Make sockets portable
16// ---------------------
17#ifdef _MSC_VER
18 #include <fcntl.h>
19 #include <winsock2.h>
20 #include <iphlpapi.h> // must be after <winsock2.h>
21 #include <ws2tcpip.h> // for getaddrinfo, socklen_t etc.
22 #include <stdint.h> // for uint16_t etc.
23
24 #define CLOSE_SOCKET_P closesocket
25
26 // _MSC_VER has SOCKET defined but unsigned and not a file descriptor.
27 typedef ADDRESS_FAMILY sa_family_t;
28 // socklen_t describes the length of a socket address. This is an integer
29 // type of at least 32 bits.
30 typedef int socklen_t;
31 typedef uint16_t in_port_t;
32 typedef uint32_t in_addr_t;
33
34 // socket() returns INVALID_SOCKET and is defined unsigned:
35 // #define INVALID_SOCKET (0xffff)
36 // some winsock functions return SOCKET_ERROR that is defined:
37 // #define SOCKET_ERROR (-1)
38
39 // For shutdown() send/receive on a socket there are other constant names.
40 #define SHUT_RD SD_RECEIVE
41 #define SHUT_WR SD_SEND
42 #define SHUT_RDWR SD_BOTH
43
44#else // not _MSC_VER
45
46 #include <sys/socket.h>
47 #include <sys/select.h>
48 #include <arpa/inet.h>
49 #include <unistd.h> // Also needed here to use 'close()' for a socket.
50 #include <netdb.h> // for getaddrinfo etc.
51
52 #define CLOSE_SOCKET_P close
53
54 typedef int SOCKET;
55 // Posix has sa_family_t defined.
56 // Posix has socklen_t defined.
57 // Posix has in_port_t defined.
58 // Posix has in_addr_t defined.
59
60 // socket() returns INVALID_SOCKET on win32 and is unsigned.
61 #define INVALID_SOCKET (-1)
62 // some functions return SOCKET_ERROR on win32.
63 #define SOCKET_ERROR (-1)
64#endif // _MSC_VER
65
66
67// This a bit flag to disable SIGPIPE on socket connections. With 0 it do
68// nothing but we need it on platforms that do not support it, to compile
69// without error missing this macro.
70#ifndef MSG_NOSIGNAL
71#define MSG_NOSIGNAL 0
72#endif
73
74// On MS Windows there is 'int' used instead of 'sa_family_t' (unsigned short
75// int) for some variable. To be portable I simply use
76// 'static_cast<sa_family_t>(var)'. This is a compile time guard that the
77// smaler boundaries of 'sa_family_t' are not violated.
78#if (AF_UNSPEC < 0) || (AF_UNSPEC > 65535) || (AF_INET6 < 0) || (AF_INET6 > 65535) || (AF_INET < 0) || (AF_INET > 65535)
79 #error "Constant AF_UNSPEC, or AF_INET6, or AF_INET is negative or bigger than 65535."
80#endif
81
82// clang-format on
83
85#endif // UPnPsdk_INCLUDE_PORT_SOCK_HPP