VirtualBox

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

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

IPRT: Add RTNetStrToIPv4Cidr() that is intended as a better API
replacement for RTCidrStrToIPv4(). Existing call sites for the latter
are not converted to the new function yet to avoid risky churn right
before the release.

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