VirtualBox

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

Last change on this file since 78939 was 76585, checked in by vboxsync, 6 years ago

*: scm --fix-header-guard-endif

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