Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
IP.h
1/*
2* Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3*/
4#ifndef _Stroika_Foundation_IO_Network_InternetProtocol_IP_h_
5#define _Stroika_Foundation_IO_Network_InternetProtocol_IP_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#if qStroika_Foundation_Common_Platform_Linux
10#include <netinet/ip.h>
11#endif
12
13#include "Stroika/Foundation/Common/Common.h"
15#include "Stroika/Foundation/Memory/Common.h"
16
17/**
18*
19* \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
20*
21*
22* TODO:
23*
24* @see https://en.wikipedia.org/w/index.php?title=IPv4
25*
26*/
27
29
30 /**
31 * This type is frequently used to select what versions of IP protocol to use in higher level services.
32 *
33 * @see SupportIPV4
34 * @see SupportIPV6
35 */
36 enum class IPVersionSupport {
37 eIPV4Only,
38 eIPV6Only,
39 eIPV4AndIPV6,
40
41 eDEFAULT = eIPV4AndIPV6,
42
43 Stroika_Define_Enum_Bounds (eIPV4Only, eIPV4AndIPV6)
44 };
45
46 /**
47 * Trivial helper so you can do:
48 *
49 * \par Example Usage
50 * \code
51 * if (InternetProtocol::IP::SupportIPV4 (ipVersion)) {
52 * fSocket_.Bind (SocketAddress{Network::V4::kAddrAny, UPnP::SSDP::V4::kSocketAddress.GetPort ()}, bindFlags);
53 * }
54 * \endcode
55 */
56 bool SupportIPV4 (IPVersionSupport flag);
57
58 /**
59 * Trivial helper so you can do:
60 *
61 * \par Example Usage
62 * \code
63 * if (InternetProtocol::IP::SupportIPV6 (ipVersion)) {
64 * fSocket_.Bind (SocketAddress (Network::V6::kAddrAny, UPnP::SSDP::V6::kSocketAddress.GetPort ()), bindFlags);
65 * }
66 * \endcode
67 */
68 bool SupportIPV6 (IPVersionSupport flag);
69
70 namespace V4 {
71
72 /**
73 * The IPv4 packet header
74 *
75 * @see https://tools.ietf.org/html/rfc760
76 *
77 * copied field names to match http://lxr.free-electrons.com/source/include/uapi/linux/ip.h
78 */
79#if qStroika_Foundation_Common_Platform_Linux
80 using PacketHeader = ::iphdr;
81#else
82 Stroika_Foundation_Common_STRUCT_PACKED (struct iphdr_le_ {
83 uint8_t ihl : 4, // Length of the header in dwords
84 version : 4; // Version of IP
85 uint8_t tos; // Type of service
86 uint16_t tot_len; // Length of the packet in dwords
87 uint16_t id; // unique identifier
88 uint16_t frag_off; // Flags and Fragment Offset
89 uint8_t ttl; // Time to live
90 uint8_t protocol; // Protocol number (TCP, UDP etc)
91 uint16_t check; // IP checksum
92 uint32_t saddr;
93 uint32_t daddr;
94 });
95 Stroika_Foundation_Common_STRUCT_PACKED (struct iphdr_be_ {
96 uint8_t version : 4, // Version of IP
97 ihl : 4; // Length of the header in dwords
98 uint8_t tos; // Type of service
99 uint16_t tot_len; // Length of the packet in dwords
100 uint16_t id; // unique identifier
101 uint16_t frag_off; // Flags and Fragment Offset
102 uint8_t ttl; // Time to live
103 uint8_t protocol; // Protocol number (TCP, UDP etc)
104 uint16_t check; // IP checksum
105 uint32_t saddr;
106 uint32_t daddr;
107 });
108 using PacketHeader = conditional_t<Common::GetEndianness () == Common::Endian::eBig, iphdr_be_, iphdr_le_>;
109#endif
110 static_assert (sizeof (PacketHeader) == 20,
111 "Check Stroika_Foundation_Common_STRUCT_PACKED, or builtin definition of iphdr: iphdr size wrong");
112 }
113
114 /**
115 */
116 uint16_t ip_checksum (const void* packetStart, const void* packetEnd);
117
118}
119
120/*
121********************************************************************************
122***************************** Implementation Details ***************************
123********************************************************************************
124*/
125#include "IP.inl"
126
127#endif /*_Stroika_Foundation_IO_Network_InternetProtocol_IP_h_*/
#define Stroika_Define_Enum_Bounds(FIRST_ITEM, LAST_ITEM)
constexpr Endian GetEndianness()
returns native (std::endian::native) Endianness flag. Can be complicated (mixed, etc)....
Definition Endian.inl:25
bool SupportIPV4(IPVersionSupport flag)
Definition IP.inl:8