VirtualBox

source: vbox/trunk/include/iprt/net.h@ 99739

Last change on this file since 99739 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.2 KB
Line 
1/** @file
2 * IPRT - Network Protocols.
3 */
4
5/*
6 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
14 * License.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
23 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
29 *
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef IPRT_INCLUDED_net_h
37#define IPRT_INCLUDED_net_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/cdefs.h>
43#include <iprt/types.h>
44#include <iprt/assert.h>
45
46
47RT_C_DECLS_BEGIN
48
49/** @defgroup grp_rt_net RTNet - Network Protocols
50 * @ingroup grp_rt
51 * @{
52 */
53
54/**
55 * Converts an stringified Ethernet MAC address into the RTMAC representation.
56 *
57 * @todo This should be move to some generic part of the runtime.
58 *
59 * @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
60 * failure.
61 *
62 * @param pszAddr The address string to convert.
63 * @param pMacAddr Where to store the result.
64 */
65RTDECL(int) RTNetStrToMacAddr(const char *pszAddr, PRTMAC pMacAddr);
66
67/**
68 * IPv4 address.
69 */
70typedef RTUINT32U RTNETADDRIPV4;
71AssertCompileSize(RTNETADDRIPV4, 4);
72/** Pointer to a IPv4 address. */
73typedef RTNETADDRIPV4 *PRTNETADDRIPV4;
74/** Pointer to a const IPv4 address. */
75typedef RTNETADDRIPV4 const *PCRTNETADDRIPV4;
76
77/**
78 * Tests if the given string is an IPv4 address.
79 *
80 * @returns boolean.
81 * @param pcszAddr String which may be an IPv4 address.
82 */
83RTDECL(bool) RTNetIsIPv4AddrStr(const char *pcszAddr);
84
85/**
86 * Tests if the given string is a wildcard IPv4 address.
87 *
88 * @returns boolean.
89 * @param pcszAddr String which may be an IPv4 address.
90 */
91RTDECL(bool) RTNetStrIsIPv4AddrAny(const char *pcszAddr);
92
93/**
94 * Parses dotted-decimal IPv4 address into RTNETADDRIPV4 representation.
95 *
96 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
97 * failure.
98 *
99 * @param pcszAddr The value to convert.
100 * @param ppszNext Where to store the pointer to the first char
101 * following the address. (Optional)
102 * @param pAddr Where to store the result.
103 */
104RTDECL(int) RTNetStrToIPv4AddrEx(const char *pcszAddr, PRTNETADDRIPV4 pAddr, char **ppszNext);
105
106/**
107 * Parses dotted-decimal IPv4 address into RTNETADDRIPV4 representation.
108 * Leading and trailing whitespace is ignored.
109 *
110 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
111 * failure.
112 *
113 * @param pcszAddr The value to convert.
114 * @param pAddr Where to store the result.
115 */
116RTDECL(int) RTNetStrToIPv4Addr(const char *pcszAddr, PRTNETADDRIPV4 pAddr);
117
118/**
119 * Parses dotted-decimal IPv4 CIDR notation into RTNETADDRIPV4
120 * representation and prefix length. Missing prefix specification is
121 * treated as exact address specification (prefix length 32). Leading
122 * and trailing whitespace is ignored.
123 *
124 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
125 * failure.
126 *
127 * @param pcszAddr The value to convert.
128 * @param pAddr Where to store the address.
129 * @param piPrefix Where to store the prefix length;
130 */
131RTDECL(int) RTNetStrToIPv4Cidr(const char *pcszAddr, PRTNETADDRIPV4 pAddr, int *piPrefix);
132
133/**
134 * Verifies that RTNETADDRIPV4 is a valid contiguous netmask and
135 * computes its prefix length.
136 *
137 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
138 * failure.
139 *
140 * @param pMask The netmask to verify and convert.
141 * @param piPrefix Where to store the prefix length. (Optional)
142 */
143RTDECL(int) RTNetMaskToPrefixIPv4(PCRTNETADDRIPV4 pMask, int *piPrefix);
144
145/**
146 * Computes netmask corresponding to the prefix length.
147 *
148 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
149 * failure.
150 *
151 * @param iPrefix The prefix to convert.
152 * @param pMask Where to store the netmask.
153 */
154RTDECL(int) RTNetPrefixToMaskIPv4(int iPrefix, PRTNETADDRIPV4 pMask);
155
156
157/**
158 * IPv6 address.
159 */
160typedef RTUINT128U RTNETADDRIPV6;
161AssertCompileSize(RTNETADDRIPV6, 16);
162/** Pointer to a IPv6 address. */
163typedef RTNETADDRIPV6 *PRTNETADDRIPV6;
164/** Pointer to a const IPv6 address. */
165typedef RTNETADDRIPV6 const *PCRTNETADDRIPV6;
166
167/**
168 * Tests if the given string is a valid IPv6 address.
169 *
170 * @returns @c true if it is, @c false if not.
171 * @param pszAddress String which may be an IPv6 address.
172 */
173RTDECL(bool) RTNetIsIPv6AddrStr(const char *pszAddress);
174
175/**
176 * Tests if the given string is a wildcard IPv6 address.
177 *
178 * @returns @c true if it is, @c false if not.
179 * @param pszAddress String which may be an IPv6 address.
180 */
181RTDECL(bool) RTNetStrIsIPv6AddrAny(const char *pszAddress);
182
183/**
184 * Parses IPv6 address into RTNETADDRIPV6 representation.
185 *
186 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
187 * failure.
188 *
189 * @param pcszAddr The value to convert.
190 * @param ppszNext Where to store the pointer to the first char
191 * following the address. (Optional)
192 * @param pAddr Where to store the result.
193 */
194RTDECL(int) RTNetStrToIPv6AddrEx(const char *pcszAddr, PRTNETADDRIPV6 pAddr, char **ppszNext);
195
196/**
197 * Parses IPv6 address into RTNETADDRIPV6 representation.
198 * Leading and trailing whitespace is ignored.
199 *
200 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
201 * failure.
202 *
203 * @param pcszAddr The value to convert.
204 * @param ppszZone Where to store the pointer to the first char
205 * of the zone id. NULL is stored if there is
206 * no zone id.
207 * @param pAddr Where to store the result.
208 */
209RTDECL(int) RTNetStrToIPv6Addr(const char *pcszAddr, PRTNETADDRIPV6 pAddr, char **ppszZone);
210
211/**
212 * Verifies that RTNETADDRIPV6 is a valid contiguous netmask and
213 * computes its prefix length.
214 *
215 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
216 * failure.
217 *
218 * @param pMask The netmask to verify and convert.
219 * @param piPrefix Where to store the prefix length. (Optional)
220 */
221RTDECL(int) RTNetMaskToPrefixIPv6(PCRTNETADDRIPV6 pMask, int *piPrefix);
222
223/**
224 * Computes netmask corresponding to the prefix length.
225 *
226 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
227 * failure.
228 *
229 * @param iPrefix The prefix to convert.
230 * @param pMask Where to store the netmask.
231 */
232RTDECL(int) RTNetPrefixToMaskIPv6(int iPrefix, PRTNETADDRIPV6 pMask);
233
234/**
235 * Parses IPv6 prefix notation into RTNETADDRIPV6 representation and
236 * prefix length. Missing prefix specification is treated as exact
237 * address specification (prefix length 128). Leading and trailing
238 * whitespace is ignored.
239 *
240 * "CIDR" in the name is a misnomer as IPv6 doesn't have network
241 * classes, but is parallel to the IPv4 name (and naming things is
242 * hard).
243 *
244 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
245 * failure.
246 *
247 * @param pcszAddr The value to convert.
248 * @param pAddr Where to store the address.
249 * @param piPrefix Where to store the prefix length;
250 */
251RTDECL(int) RTNetStrToIPv6Cidr(const char *pcszAddr, PRTNETADDRIPV6 pAddr, int *piPrefix);
252
253
254/**
255 * IPX address.
256 */
257#pragma pack(1)
258typedef struct RTNETADDRIPX
259{
260 /** The network ID. */
261 uint32_t Network;
262 /** The node ID. (Defaults to the MAC address apparently.) */
263 RTMAC Node;
264} RTNETADDRIPX;
265#pragma pack()
266AssertCompileSize(RTNETADDRIPX, 4+6);
267/** Pointer to an IPX address. */
268typedef RTNETADDRIPX *PRTNETADDRIPX;
269/** Pointer to a const IPX address. */
270typedef RTNETADDRIPX const *PCRTNETADDRIPX;
271
272/**
273 * Network address union.
274 *
275 * @remarks The size of this structure may change in the future.
276 */
277typedef union RTNETADDRU
278{
279 /** 64-bit view. */
280 uint64_t au64[2];
281 /** 32-bit view. */
282 uint32_t au32[4];
283 /** 16-bit view. */
284 uint16_t au16[8];
285 /** 8-bit view. */
286 uint8_t au8[16];
287 /** IPv4 view. */
288 RTNETADDRIPV4 IPv4;
289#ifndef IPv6 /* Work around X11 and RDP defining IPv6 to 1. */
290 /** IPv6 view. */
291 RTNETADDRIPV6 IPv6;
292#endif
293 /** IPX view. */
294 RTNETADDRIPX Ipx;
295 /** MAC address view. */
296 RTMAC Mac;
297} RTNETADDRU;
298AssertCompileSize(RTNETADDRU, 16);
299/** Pointer to an address union. */
300typedef RTNETADDRU *PRTNETADDRU;
301/** Pointer to a const address union. */
302typedef RTNETADDRU const *PCRTNETADDRU;
303
304/**
305 * Network address type.
306 *
307 * @remarks The value assignments may change in the future.
308 */
309typedef enum RTNETADDRTYPE
310{
311 /** The invalid 0 entry. */
312 RTNETADDRTYPE_INVALID = 0,
313 /** IP version 4. */
314 RTNETADDRTYPE_IPV4,
315 /** IP version 6. */
316 RTNETADDRTYPE_IPV6,
317 /** IPX. */
318 RTNETADDRTYPE_IPX,
319 /** MAC address. */
320 RTNETADDRTYPE_MAC,
321 /** The end of the valid values. */
322 RTNETADDRTYPE_END,
323 /** The usual 32-bit hack. */
324 RTNETADDRTYPE_32_BIT_HACK = 0x7fffffff
325} RTNETADDRTYPE;
326/** Pointer to a network address type. */
327typedef RTNETADDRTYPE *PRTNETADDRTYPE;
328/** Pointer to a const network address type. */
329typedef RTNETADDRTYPE const *PCRTNETADDRTYPE;
330
331/**
332 * Network address.
333 *
334 * @remarks The size and type values may change.
335 */
336typedef struct RTNETADDR
337{
338 /** The address union. */
339 RTNETADDRU uAddr;
340 /** Indicates which view of @a u that is valid. */
341 RTNETADDRTYPE enmType;
342 /** The port number for IPv4 and IPv6 addresses. This is set to
343 * RTNETADDR_NA_PORT if not applicable. */
344 uint32_t uPort;
345} RTNETADDR;
346/** Pointer to a network address. */
347typedef RTNETADDR *PRTNETADDR;
348/** Pointer to a const network address. */
349typedef RTNETADDR const *PCRTNETADDR;
350
351/** The not applicable value of RTNETADDR::uPort value use to inid. */
352#define RTNETADDR_PORT_NA UINT32_MAX
353
354/**
355 * Ethernet header.
356 */
357#pragma pack(1)
358typedef struct RTNETETHERHDR
359{
360 RTMAC DstMac;
361 RTMAC SrcMac;
362 /** Ethernet frame type or frame size, depending on the kind of ethernet.
363 * This is big endian on the wire. */
364 uint16_t EtherType;
365} RTNETETHERHDR;
366#pragma pack()
367AssertCompileSize(RTNETETHERHDR, 14);
368/** Pointer to an ethernet header. */
369typedef RTNETETHERHDR *PRTNETETHERHDR;
370/** Pointer to a const ethernet header. */
371typedef RTNETETHERHDR const *PCRTNETETHERHDR;
372
373/** @name EtherType (RTNETETHERHDR::EtherType)
374 * @{ */
375#define RTNET_ETHERTYPE_IPV4 UINT16_C(0x0800)
376#define RTNET_ETHERTYPE_ARP UINT16_C(0x0806)
377#define RTNET_ETHERTYPE_IPV6 UINT16_C(0x86dd)
378#define RTNET_ETHERTYPE_VLAN UINT16_C(0x8100)
379#define RTNET_ETHERTYPE_IPX_1 UINT16_C(0x8037)
380#define RTNET_ETHERTYPE_IPX_2 UINT16_C(0x8137)
381#define RTNET_ETHERTYPE_IPX_3 UINT16_C(0x8138)
382/** @} */
383
384
385/**
386 * IPv4 header.
387 * All is bigendian on the wire.
388 */
389#pragma pack(1)
390typedef struct RTNETIPV4
391{
392#ifdef RT_BIG_ENDIAN
393 unsigned int ip_v : 4;
394 unsigned int ip_hl : 4;
395 unsigned int ip_tos : 8;
396 unsigned int ip_len : 16;
397#else
398 /** 00:0 - Header length given as a 32-bit word count. */
399 unsigned int ip_hl : 4;
400 /** 00:4 - Header version. */
401 unsigned int ip_v : 4;
402 /** 01 - Type of service. */
403 unsigned int ip_tos : 8;
404 /** 02 - Total length (header + data). */
405 unsigned int ip_len : 16;
406#endif
407 /** 04 - Packet idenficiation. */
408 uint16_t ip_id;
409 /** 06 - Offset if fragmented. */
410 uint16_t ip_off;
411 /** 08 - Time to live. */
412 uint8_t ip_ttl;
413 /** 09 - Protocol. */
414 uint8_t ip_p;
415 /** 0a - Header check sum. */
416 uint16_t ip_sum;
417 /** 0c - Source address. */
418 RTNETADDRIPV4 ip_src;
419 /** 10 - Destination address. */
420 RTNETADDRIPV4 ip_dst;
421 /** 14 - Options (optional). */
422 uint32_t ip_options[1];
423} RTNETIPV4;
424#pragma pack()
425AssertCompileSize(RTNETIPV4, 6 * 4);
426/** Pointer to a IPv4 header. */
427typedef RTNETIPV4 *PRTNETIPV4;
428/** Pointer to a const IPv4 header. */
429typedef RTNETIPV4 const *PCRTNETIPV4;
430
431/** The minimum IPv4 header length (in bytes).
432 * Up to and including RTNETIPV4::ip_dst. */
433#define RTNETIPV4_MIN_LEN (20)
434
435
436/** @name IPv4 Protocol Numbers
437 * @{ */
438/** IPv4: ICMP */
439#define RTNETIPV4_PROT_ICMP (1)
440/** IPv4: TCP */
441#define RTNETIPV4_PROT_TCP (6)
442/** IPv4: UDP */
443#define RTNETIPV4_PROT_UDP (17)
444/** @} */
445
446/** @name Common IPv4 Port Assignments
447 * @{
448 */
449/** Boostrap Protocol / DHCP) Server. */
450#define RTNETIPV4_PORT_BOOTPS (67)
451/** Boostrap Protocol / DHCP) Client. */
452#define RTNETIPV4_PORT_BOOTPC (68)
453/** @} */
454
455/** @name IPv4 Flags
456 * @{ */
457/** IPv4: Don't fragment */
458#define RTNETIPV4_FLAGS_DF (0x4000)
459/** IPv4: More fragments */
460#define RTNETIPV4_FLAGS_MF (0x2000)
461/** @} */
462
463RTDECL(uint16_t) RTNetIPv4HdrChecksum(PCRTNETIPV4 pIpHdr);
464RTDECL(bool) RTNetIPv4IsHdrValid(PCRTNETIPV4 pIpHdr, size_t cbHdrMax, size_t cbPktMax, bool fChecksum);
465RTDECL(uint32_t) RTNetIPv4PseudoChecksum(PCRTNETIPV4 pIpHdr);
466RTDECL(uint32_t) RTNetIPv4PseudoChecksumBits(RTNETADDRIPV4 SrcAddr, RTNETADDRIPV4 DstAddr, uint8_t bProtocol, uint16_t cbPkt);
467RTDECL(uint32_t) RTNetIPv4AddDataChecksum(void const *pvData, size_t cbData, uint32_t u32Sum, bool *pfOdd);
468RTDECL(uint16_t) RTNetIPv4FinalizeChecksum(uint32_t u32Sum);
469
470
471/**
472 * IPv6 header.
473 * All is bigendian on the wire.
474 */
475#pragma pack(1)
476typedef struct RTNETIPV6
477{
478 /** Version (4 bits), Traffic Class (8 bits) and Flow Lable (20 bits).
479 * @todo this is probably mislabeled - ip6_flow vs. ip6_vfc, fix later. */
480 uint32_t ip6_vfc;
481 /** 04 - Payload length, including extension headers. */
482 uint16_t ip6_plen;
483 /** 06 - Next header type (RTNETIPV4_PROT_XXX). */
484 uint8_t ip6_nxt;
485 /** 07 - Hop limit. */
486 uint8_t ip6_hlim;
487 /** xx - Source address. */
488 RTNETADDRIPV6 ip6_src;
489 /** xx - Destination address. */
490 RTNETADDRIPV6 ip6_dst;
491} RTNETIPV6;
492#pragma pack()
493AssertCompileSize(RTNETIPV6, 8 + 16 + 16);
494/** Pointer to a IPv6 header. */
495typedef RTNETIPV6 *PRTNETIPV6;
496/** Pointer to a const IPv6 header. */
497typedef RTNETIPV6 const *PCRTNETIPV6;
498
499/** The minimum IPv6 header length (in bytes).
500 * Up to and including RTNETIPV6::ip6_dst. */
501#define RTNETIPV6_MIN_LEN (40)
502#define RTNETIPV6_ICMPV6_ND_WITH_LLA_OPT_MIN_LEN (32)
503
504RTDECL(uint32_t) RTNetIPv6PseudoChecksum(PCRTNETIPV6 pIpHdr);
505RTDECL(uint32_t) RTNetIPv6PseudoChecksumEx(PCRTNETIPV6 pIpHdr, uint8_t bProtocol, uint16_t cbPkt);
506RTDECL(uint32_t) RTNetIPv6PseudoChecksumBits(PCRTNETADDRIPV6 pSrcAddr, PCRTNETADDRIPV6 pDstAddr,
507 uint8_t bProtocol, uint16_t cbPkt);
508
509
510/**
511 * UDP header.
512 */
513#pragma pack(1)
514typedef struct RTNETUDP
515{
516 /** The source port. */
517 uint16_t uh_sport;
518 /** The destination port. */
519 uint16_t uh_dport;
520 /** The length of the UDP header and associated data. */
521 uint16_t uh_ulen;
522 /** The checksum of the pseudo header, the UDP header and the data. */
523 uint16_t uh_sum;
524} RTNETUDP;
525#pragma pack()
526AssertCompileSize(RTNETUDP, 8);
527/** Pointer to an UDP header. */
528typedef RTNETUDP *PRTNETUDP;
529/** Pointer to a const UDP header. */
530typedef RTNETUDP const *PCRTNETUDP;
531
532/** The minimum UDP packet length (in bytes). (RTNETUDP::uh_ulen) */
533#define RTNETUDP_MIN_LEN (8)
534
535RTDECL(uint16_t) RTNetUDPChecksum(uint32_t u32Sum, PCRTNETUDP pUdpHdr);
536RTDECL(uint32_t) RTNetIPv4AddUDPChecksum(PCRTNETUDP pUdpHdr, uint32_t u32Sum);
537RTDECL(uint16_t) RTNetIPv4UDPChecksum(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData);
538RTDECL(bool) RTNetIPv4IsUDPSizeValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, size_t cbPktMax);
539RTDECL(bool) RTNetIPv4IsUDPValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData, size_t cbPktMax, bool fChecksum);
540
541
542/**
543 * IPv4 BOOTP / DHCP packet.
544 */
545#pragma pack(1)
546typedef struct RTNETBOOTP
547{
548 /** 00 - The packet opcode (RTNETBOOTP_OP_*). */
549 uint8_t bp_op;
550 /** 01 - Hardware address type. Same as RTNETARPHDR::ar_htype. */
551 uint8_t bp_htype;
552 /** 02 - Hardware address length. */
553 uint8_t bp_hlen;
554 /** 03 - Gateway hops. */
555 uint8_t bp_hops;
556 /** 04 - Transaction ID. */
557 uint32_t bp_xid;
558 /** 08 - Seconds since boot started. */
559 uint16_t bp_secs;
560 /** 0a - Unused (BOOTP) / Flags (DHCP) (RTNET_DHCP_FLAGS_*). */
561 uint16_t bp_flags;
562 /** 0c - Client IPv4 address. */
563 RTNETADDRIPV4 bp_ciaddr;
564 /** 10 - Your IPv4 address. */
565 RTNETADDRIPV4 bp_yiaddr;
566 /** 14 - Server IPv4 address. */
567 RTNETADDRIPV4 bp_siaddr;
568 /** 18 - Gateway IPv4 address. */
569 RTNETADDRIPV4 bp_giaddr;
570 /** 1c - Client hardware address. */
571 union
572 {
573 uint8_t au8[16];
574 RTMAC Mac;
575 } bp_chaddr;
576 /** 2c - Server name. */
577 uint8_t bp_sname[64];
578 /** 6c - File name / more DHCP options. */
579 uint8_t bp_file[128];
580 /** ec - Vendor specific area (BOOTP) / Options (DHCP).
581 * @remark This is really 312 bytes in the DHCP version. */
582 union
583 {
584 uint8_t au8[128];
585 struct DHCP
586 {
587 /** ec - The DHCP cookie (RTNET_DHCP_COOKIE). */
588 uint32_t dhcp_cookie;
589 /** f0 - The DHCP options. */
590 uint8_t dhcp_opts[124];
591 } Dhcp;
592 } bp_vend;
593
594} RTNETBOOTP;
595#pragma pack()
596AssertCompileSize(RTNETBOOTP, 0xec + 128);
597/** Pointer to a BOOTP / DHCP packet. */
598typedef RTNETBOOTP *PRTNETBOOTP;
599/** Pointer to a const BOOTP / DHCP packet. */
600typedef RTNETBOOTP const *PCRTNETBOOTP;
601
602/** Minimum BOOTP packet length. For quick validation, no standard thing really. */
603#define RTNETBOOTP_MIN_LEN 0xec
604/** Minimum DHCP packet length. For quick validation, no standard thing really. */
605#define RTNETBOOTP_DHCP_MIN_LEN 0xf1
606
607/** The normal size of the a DHCP packet (i.e. a RTNETBOOTP).
608 * Same as RTNET_DHCP_OPT_SIZE, just expressed differently. */
609#define RTNET_DHCP_NORMAL_SIZE (0xec + 4 + RTNET_DHCP_OPT_SIZE)
610/** The normal size of RTNETBOOTP::bp_vend::Dhcp::dhcp_opts. */
611#define RTNET_DHCP_OPT_SIZE (312 - 4)
612
613/** @name BOOTP packet opcode values
614 * @{ */
615#define RTNETBOOTP_OP_REQUEST 1
616#define RTNETBOOTP_OP_REPLY 2
617/** @} */
618
619/** @name DHCP flags (RTNETBOOTP::bp_flags)
620 * @{ */
621#define RTNET_DHCP_FLAGS_NO_BROADCAST UINT16_C(0x8000) /** @todo check test!!! */
622/** @} */
623
624/** The DHCP cookie (network endian). */
625#define RTNET_DHCP_COOKIE UINT32_C(0x63825363)
626
627/**
628 * An IPv4 DHCP option header.
629 */
630typedef struct RTNETDHCPOPT
631{
632 /** 00 - The DHCP option. */
633 uint8_t dhcp_opt;
634 /** 01 - The data length (excluding this header). */
635 uint8_t dhcp_len;
636 /* 02 - The option data follows here, optional and of variable length. */
637} RTNETDHCPOPT;
638AssertCompileSize(RTNETDHCPOPT, 2);
639/** Pointer to a DHCP option header. */
640typedef RTNETDHCPOPT *PRTNETDHCPOPT;
641/** Pointer to a const DHCP option header. */
642typedef RTNETDHCPOPT const *PCRTNETDHCPOPT;
643
644/** @name DHCP options
645 * @{ */
646/** 1 byte padding, this has no dhcp_len field. */
647#define RTNET_DHCP_OPT_PAD 0
648
649/** The subnet mask. */
650#define RTNET_DHCP_OPT_SUBNET_MASK 1
651/** The time offset. */
652#define RTNET_DHCP_OPT_TIME_OFFSET 2
653/** The routers for the subnet. */
654#define RTNET_DHCP_OPT_ROUTERS 3
655/** Domain Name Server. */
656#define RTNET_DHCP_OPT_DNS 6
657/** Host name. */
658#define RTNET_DHCP_OPT_HOST_NAME 12
659/** Domain name. */
660#define RTNET_DHCP_OPT_DOMAIN_NAME 15
661
662/** The requested address. */
663#define RTNET_DHCP_OPT_REQ_ADDR 50
664/** The lease time in seconds. */
665#define RTNET_DHCP_OPT_LEASE_TIME 51
666/** Option overload.
667 * Indicates that the bp_file and/or bp_sname holds contains DHCP options. */
668#define RTNET_DHCP_OPT_OPTION_OVERLOAD 52
669/** Have a 8-bit message type value as data, see RTNET_DHCP_MT_*. */
670#define RTNET_DHCP_OPT_MSG_TYPE 53
671/** Server ID. */
672#define RTNET_DHCP_OPT_SERVER_ID 54
673/** Parameter request list. */
674#define RTNET_DHCP_OPT_PARAM_REQ_LIST 55
675/** The maximum DHCP message size a client is willing to accept. */
676#define RTNET_DHCP_OPT_MAX_DHCP_MSG_SIZE 57
677/** Client ID. */
678#define RTNET_DHCP_OPT_CLIENT_ID 61
679/** TFTP server name. */
680#define RTNET_DHCP_OPT_TFTP_SERVER_NAME 66
681/** Bootfile name. */
682#define RTNET_DHCP_OPT_BOOTFILE_NAME 67
683
684/** Marks the end of the DHCP options, this has no dhcp_len field. */
685#define RTNET_DHCP_OPT_END 255
686/** @} */
687
688/** @name DHCP Option overload flags (option 52)
689 * @{ */
690#define RTNET_DHCP_OPTION_OVERLOAD_FILE 1
691#define RTNET_DHCP_OPTION_OVERLOAD_SNAME 2
692#define RTNET_DHCP_OPTION_OVERLOAD_MASK 3
693/** @} */
694
695/** @name DHCP Message Types (option 53)
696 * @{ */
697#define RTNET_DHCP_MT_DISCOVER 1
698#define RTNET_DHCP_MT_OFFER 2
699#define RTNET_DHCP_MT_REQUEST 3
700#define RTNET_DHCP_MT_DECLINE 4
701#define RTNET_DHCP_MT_ACK 5
702#define RTNET_DHCP_MT_NAC 6
703#define RTNET_DHCP_MT_RELEASE 7
704#define RTNET_DHCP_MT_INFORM 8
705/** @} */
706
707/** @name DHCP Flags
708 * @{ */
709#define RTNET_DHCP_FLAG_BROADCAST 0x8000
710/** @} */
711
712RTDECL(bool) RTNetIPv4IsDHCPValid(PCRTNETUDP pUdpHdr, PCRTNETBOOTP pDhcp, size_t cbDhcp, uint8_t *pMsgType);
713
714
715/**
716 * IPv4 DHCP packet.
717 * @deprecated Use RTNETBOOTP.
718 */
719#pragma pack(1)
720typedef struct RTNETDHCP
721{
722 /** 00 - The packet opcode. */
723 uint8_t Op;
724 /** Hardware address type. */
725 uint8_t HType;
726 /** Hardware address length. */
727 uint8_t HLen;
728 uint8_t Hops;
729 uint32_t XID;
730 uint16_t Secs;
731 uint16_t Flags;
732 /** Client IPv4 address. */
733 RTNETADDRIPV4 CIAddr;
734 /** Your IPv4 address. */
735 RTNETADDRIPV4 YIAddr;
736 /** Server IPv4 address. */
737 RTNETADDRIPV4 SIAddr;
738 /** Gateway IPv4 address. */
739 RTNETADDRIPV4 GIAddr;
740 /** Client hardware address. */
741 uint8_t CHAddr[16];
742 /** Server name. */
743 uint8_t SName[64];
744 uint8_t File[128];
745 uint8_t abMagic[4];
746 uint8_t DhcpOpt;
747 uint8_t DhcpLen; /* 1 */
748 uint8_t DhcpReq;
749 uint8_t abOptions[57];
750} RTNETDHCP;
751#pragma pack()
752/** @todo AssertCompileSize(RTNETDHCP, ); */
753/** Pointer to a DHCP packet. */
754typedef RTNETDHCP *PRTNETDHCP;
755/** Pointer to a const DHCP packet. */
756typedef RTNETDHCP const *PCRTNETDHCP;
757
758
759/**
760 * TCP packet.
761 */
762#pragma pack(1)
763typedef struct RTNETTCP
764{
765 /** 00 - The source port. */
766 uint16_t th_sport;
767 /** 02 - The destination port. */
768 uint16_t th_dport;
769 /** 04 - The sequence number. */
770 uint32_t th_seq;
771 /** 08 - The acknowledgement number. */
772 uint32_t th_ack;
773#ifdef RT_BIG_ENDIAN
774 unsigned int th_win : 16;
775 unsigned int th_flags : 8;
776 unsigned int th_off : 4;
777 unsigned int th_x2 : 4;
778#else
779 /** 0c:0 - Reserved. */
780 unsigned int th_x2 : 4;
781 /** 0c:4 - The data offset given as a dword count from the start of this header. */
782 unsigned int th_off : 4;
783 /** 0d - flags. */
784 unsigned int th_flags : 8;
785 /** 0e - The window. */
786 unsigned int th_win : 16;
787#endif
788 /** 10 - The checksum of the pseudo header, the TCP header and the data. */
789 uint16_t th_sum;
790 /** 12 - The urgent pointer. */
791 uint16_t th_urp;
792 /* (options follows here and then the data (aka text).) */
793} RTNETTCP;
794#pragma pack()
795AssertCompileSize(RTNETTCP, 20);
796/** Pointer to a TCP packet. */
797typedef RTNETTCP *PRTNETTCP;
798/** Pointer to a const TCP packet. */
799typedef RTNETTCP const *PCRTNETTCP;
800
801/** The minimum TCP header length (in bytes). (RTNETTCP::th_off * 4) */
802#define RTNETTCP_MIN_LEN (20)
803
804/** @name TCP flags (RTNETTCP::th_flags)
805 * @{ */
806#define RTNETTCP_F_FIN 0x01
807#define RTNETTCP_F_SYN 0x02
808#define RTNETTCP_F_RST 0x04
809#define RTNETTCP_F_PSH 0x08
810#define RTNETTCP_F_ACK 0x10
811#define RTNETTCP_F_URG 0x20
812#define RTNETTCP_F_ECE 0x40
813#define RTNETTCP_F_CWR 0x80
814/** @} */
815
816RTDECL(uint16_t) RTNetTCPChecksum(uint32_t u32Sum, PCRTNETTCP pTcpHdr, void const *pvData, size_t cbData);
817RTDECL(uint32_t) RTNetIPv4AddTCPChecksum(PCRTNETTCP pTcpHdr, uint32_t u32Sum);
818RTDECL(uint16_t) RTNetIPv4TCPChecksum(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, void const *pvData);
819RTDECL(bool) RTNetIPv4IsTCPSizeValid(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, size_t cbHdrMax, size_t cbPktMax);
820RTDECL(bool) RTNetIPv4IsTCPValid(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, size_t cbHdrMax, void const *pvData,
821 size_t cbPktMax, bool fChecksum);
822
823
824/**
825 * IPv4 ICMP packet header.
826 */
827#pragma pack(1)
828typedef struct RTNETICMPV4HDR
829{
830 /** 00 - The ICMP message type. */
831 uint8_t icmp_type;
832 /** 01 - Type specific code that further qualifies the message. */
833 uint8_t icmp_code;
834 /** 02 - Checksum of the ICMP message. */
835 uint16_t icmp_cksum;
836} RTNETICMPV4HDR;
837#pragma pack()
838AssertCompileSize(RTNETICMPV4HDR, 4);
839/** Pointer to an ICMP packet header. */
840typedef RTNETICMPV4HDR *PRTNETICMPV4HDR;
841/** Pointer to a const ICMP packet header. */
842typedef RTNETICMPV4HDR const *PCRTNETICMPV4HDR;
843
844/** @name ICMP (v4) message types.
845 * @{ */
846#define RTNETICMPV4_TYPE_ECHO_REPLY 0
847#define RTNETICMPV4_TYPE_ECHO_REQUEST 8
848#define RTNETICMPV4_TYPE_TRACEROUTE 30
849/** @} */
850
851/**
852 * IPv4 ICMP ECHO Reply & Request packet.
853 */
854#pragma pack(1)
855typedef struct RTNETICMPV4ECHO
856{
857 /** 00 - The ICMP header. */
858 RTNETICMPV4HDR Hdr;
859 /** 04 - The identifier to help the requestor match up the reply.
860 * Can be 0. Typically fixed value. */
861 uint16_t icmp_id;
862 /** 06 - The sequence number to help the requestor match up the reply.
863 * Can be 0. Typically incrementing between requests. */
864 uint16_t icmp_seq;
865 /** 08 - Variable length data that is to be returned unmodified in the reply. */
866 uint8_t icmp_data[1];
867} RTNETICMPV4ECHO;
868#pragma pack()
869AssertCompileSize(RTNETICMPV4ECHO, 9);
870/** Pointer to an ICMP ECHO packet. */
871typedef RTNETICMPV4ECHO *PRTNETICMPV4ECHO;
872/** Pointer to a const ICMP ECHO packet. */
873typedef RTNETICMPV4ECHO const *PCRTNETICMPV4ECHO;
874
875/**
876 * IPv4 ICMP TRACEROUTE packet.
877 * This is an reply to an IP packet with the traceroute option set.
878 */
879#pragma pack(1)
880typedef struct RTNETICMPV4TRACEROUTE
881{
882 /** 00 - The ICMP header. */
883 RTNETICMPV4HDR Hdr;
884 /** 04 - Identifier copied from the traceroute option's ID number. */
885 uint16_t icmp_id;
886 /** 06 - Unused. (Possibly an icmp_seq?) */
887 uint16_t icmp_void;
888 /** 08 - Outbound hop count. From the IP packet causing this message. */
889 uint16_t icmp_ohc;
890 /** 0a - Return hop count. From the IP packet causing this message. */
891 uint16_t icmp_rhc;
892 /** 0c - Output link speed, 0 if not known. */
893 uint32_t icmp_speed;
894 /** 10 - Output link MTU, 0 if not known. */
895 uint32_t icmp_mtu;
896} RTNETICMPV4TRACEROUTE;
897#pragma pack()
898AssertCompileSize(RTNETICMPV4TRACEROUTE, 20);
899/** Pointer to an ICMP TRACEROUTE packet. */
900typedef RTNETICMPV4TRACEROUTE *PRTNETICMPV4TRACEROUTE;
901/** Pointer to a const ICMP TRACEROUTE packet. */
902typedef RTNETICMPV4TRACEROUTE const *PCRTNETICMPV4TRACEROUTE;
903
904/** @todo add more ICMPv4 as needed. */
905
906/**
907 * IPv4 ICMP union packet.
908 */
909typedef union RTNETICMPV4
910{
911 RTNETICMPV4HDR Hdr;
912 RTNETICMPV4ECHO Echo;
913 RTNETICMPV4TRACEROUTE Traceroute;
914} RTNETICMPV4;
915/** Pointer to an ICMP union packet. */
916typedef RTNETICMPV4 *PRTNETICMPV4;
917/** Pointer to a const ICMP union packet. */
918typedef RTNETICMPV4 const *PCRTNETICMPV4;
919
920
921/**
922 * IPv6 ICMP packet header.
923 */
924#pragma pack(1)
925typedef struct RTNETICMPV6HDR
926{
927 /** 00 - The ICMPv6 message type. */
928 uint8_t icmp6_type;
929 /** 01 - Type specific code that further qualifies the message. */
930 uint8_t icmp6_code;
931 /** 02 - Checksum of the ICMPv6 message. */
932 uint16_t icmp6_cksum;
933} RTNETICMPV6HDR;
934#pragma pack()
935AssertCompileSize(RTNETICMPV6HDR, 4);
936/** Pointer to an ICMPv6 packet header. */
937typedef RTNETICMPV6HDR *PRTNETICMPV6HDR;
938/** Pointer to a const ICMP packet header. */
939typedef RTNETICMPV6HDR const *PCRTNETICMPV6HDR;
940
941#define RTNETIPV6_PROT_ICMPV6 (58)
942
943/** @name Internet Control Message Protocol version 6 (ICMPv6) message types.
944 * @{ */
945#define RTNETIPV6_ICMP_TYPE_RS 133
946#define RTNETIPV6_ICMP_TYPE_RA 134
947#define RTNETIPV6_ICMP_TYPE_NS 135
948#define RTNETIPV6_ICMP_TYPE_NA 136
949#define RTNETIPV6_ICMP_TYPE_RDR 137
950/** @} */
951
952/** @name Neighbor Discovery option types
953 * @{ */
954#define RTNETIPV6_ICMP_ND_SLLA_OPT (1)
955#define RTNETIPV6_ICMP_ND_TLLA_OPT (2)
956/** @} */
957
958/** ICMPv6 ND Source/Target Link Layer Address option */
959#pragma pack(1)
960typedef struct RTNETNDP_LLA_OPT
961{
962 uint8_t type;
963 uint8_t len;
964 RTMAC lla;
965} RTNETNDP_LLA_OPT;
966#pragma pack()
967
968AssertCompileSize(RTNETNDP_LLA_OPT, 1+1+6);
969
970typedef RTNETNDP_LLA_OPT *PRTNETNDP_LLA_OPT;
971typedef RTNETNDP_LLA_OPT const *PCRTNETNDP_LLA_OPT;
972
973/** ICMPv6 ND Neighbor Sollicitation */
974#pragma pack(1)
975typedef struct RTNETNDP
976{
977 /** 00 - The ICMPv6 header. */
978 RTNETICMPV6HDR Hdr;
979 /** 04 - reserved */
980 uint32_t reserved;
981 /** 08 - target address */
982 RTNETADDRIPV6 target_address;
983} RTNETNDP;
984#pragma pack()
985AssertCompileSize(RTNETNDP, 4+4+16);
986/** Pointer to a NDP ND packet. */
987typedef RTNETNDP *PRTNETNDP;
988/** Pointer to a const NDP NS packet. */
989typedef RTNETNDP const *PCRTNETNDP;
990
991
992/**
993 * Ethernet ARP header.
994 */
995#pragma pack(1)
996typedef struct RTNETARPHDR
997{
998 /** The hardware type. */
999 uint16_t ar_htype;
1000 /** The protocol type (ethertype). */
1001 uint16_t ar_ptype;
1002 /** The hardware address length. */
1003 uint8_t ar_hlen;
1004 /** The protocol address length. */
1005 uint8_t ar_plen;
1006 /** The operation. */
1007 uint16_t ar_oper;
1008} RTNETARPHDR;
1009#pragma pack()
1010AssertCompileSize(RTNETARPHDR, 8);
1011/** Pointer to an ethernet ARP header. */
1012typedef RTNETARPHDR *PRTNETARPHDR;
1013/** Pointer to a const ethernet ARP header. */
1014typedef RTNETARPHDR const *PCRTNETARPHDR;
1015
1016/** ARP hardware type - ethernet. */
1017#define RTNET_ARP_ETHER UINT16_C(1)
1018
1019/** @name ARP operations
1020 * @{ */
1021#define RTNET_ARPOP_REQUEST UINT16_C(1) /**< Request hardware address given a protocol address (ARP). */
1022#define RTNET_ARPOP_REPLY UINT16_C(2)
1023#define RTNET_ARPOP_REVREQUEST UINT16_C(3) /**< Request protocol address given a hardware address (RARP). */
1024#define RTNET_ARPOP_REVREPLY UINT16_C(4)
1025#define RTNET_ARPOP_INVREQUEST UINT16_C(8) /**< Inverse ARP. */
1026#define RTNET_ARPOP_INVREPLY UINT16_C(9)
1027/** Check if an ARP operation is a request or not. */
1028#define RTNET_ARPOP_IS_REQUEST(Op) ((Op) & 1)
1029/** Check if an ARP operation is a reply or not. */
1030#define RTNET_ARPOP_IS_REPLY(Op) (!RTNET_ARPOP_IS_REQUEST(Op))
1031/** @} */
1032
1033
1034/**
1035 * Ethernet IPv4 + 6-byte MAC ARP request packet.
1036 */
1037#pragma pack(1)
1038typedef struct RTNETARPIPV4
1039{
1040 /** ARP header. */
1041 RTNETARPHDR Hdr;
1042 /** The sender hardware address. */
1043 RTMAC ar_sha;
1044 /** The sender protocol address. */
1045 RTNETADDRIPV4 ar_spa;
1046 /** The target hardware address. */
1047 RTMAC ar_tha;
1048 /** The target protocol address. */
1049 RTNETADDRIPV4 ar_tpa;
1050} RTNETARPIPV4;
1051#pragma pack()
1052AssertCompileSize(RTNETARPIPV4, 8+6+4+6+4);
1053/** Pointer to an ethernet IPv4+MAC ARP request packet. */
1054typedef RTNETARPIPV4 *PRTNETARPIPV4;
1055/** Pointer to a const ethernet IPv4+MAC ARP request packet. */
1056typedef RTNETARPIPV4 const *PCRTNETARPIPV4;
1057
1058
1059/** @} */
1060
1061RT_C_DECLS_END
1062
1063#endif /* !IPRT_INCLUDED_net_h */
1064
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette