VirtualBox

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

Last change on this file since 60547 was 58438, checked in by vboxsync, 9 years ago

IPRT/net: add RTNetStrIsIPv4AddrAny, RTNetStrIsIPv6AddrAny.

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