NetBurner 3.5.6
PDF Version
ip.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
40#ifndef _NB_IP_H
41#define _NB_IP_H
42
43// NB Definitions
44#include <predef.h>
45
46#ifndef NB_NET_TYPES_H
47#include "nettypes.h"
48#endif
49
50#ifndef _NB_BUFFERS_H
51#include "buffers.h"
52#endif
54// #define IP_DEBUG ( 0 )
55
56/* Definitions */
57
61#define IP_VERSION_MASK (0xF0)
62#define IP_VERSION_IPv4 (0x40)
63#define IP_VERSION_IPv6 (0x60)
69#define IP_HEADER_LENGTH_MASK (0x0F)
70#define IP_HEADER_LENGTH_NO_OPTIONS (0x05)
76#define IP_FLAGS_MASK (0xE000)
77#define IP_FRAGMENT_OFFSET_MASK (0x1FFF)
85#define IP_PROTOCOL_ICMP (0x01)
86#define IP_PROTOCOL_IGMP (0x02)
87#define IP_PROTOCOL_INTERNET (0x04)
88#define IP_PROTOCOL_TCP (0x06)
89#define IP_PROTOCOL_UDP (0x11)
97#define ICMP_TYPE_ECHO_REPLY (0)
98#define ICMP_CODE_ECHO_REPLY (0)
99#define ICMP_TYPE_DEST_UNREACHABLE (3)
100#define ICMP_CODE_PORT_UNREACHABLE (3)
101#define ICMP_TYPE_SOURCE_QUENCH (4)
102#define ICMP_CODE_SOURCE_QUENCH (0)
103#define ICMP_TYPE_REDIRECT_MESSAGE (5)
104#define ICMP_TYPE_ECHO_REQUEST (8)
105#define ICMP_CODE_ECHO_REQUEST (0)
113#define IANA_ECHO_PORT (7)
114#define IANA_DISCARD_PORT (9)
115#define IANA_SSH_PORT (22)
116#define IANA_TELNET_PORT (23)
117//#define IANA_TFTP_PORT (69) /**< @brief Trivial File Transfer Protocol port */
118#define IANA_NETBIOS_NAME_SERVICE_PORT (137)
121#define ICMP_PING_DATA_SIZE (32)
123/* Data Structures */
124
140typedef struct _IpHeaderIPv4
141{
143 uint8_t diffServNEcn;
144 beuint16_t totalLength;
145 beuint16_t identification;
147 uint8_t timeToLive;
148 uint8_t protocol;
150 beuint16_t headerChecksum;
151 beuint32_t sourceAddress;
154} __attribute__((packed)) IpHeaderIPv4;
155
168typedef struct
169{
170 uint8_t bVerHdrLen;
171 uint8_t bTos;
172 beuint16_t wLength;
173 beuint16_t wpktId;
174 beuint16_t wFlags_Frag;
175 uint8_t bTTL;
176 uint8_t proto;
180 uint16_t hCSum;
183 uint8_t DATA[];
184} __attribute__((packed)) IPPKT;
185
186typedef IPPKT *PIPPKT;
187
200typedef struct
201{
202 beuint16_t srcPort;
203 beuint16_t dstPort;
204 beuint16_t UdpLen;
208 uint16_t UdpCSum;
209 uint8_t DATA[];
210} __attribute__((packed)) UDPPKT;
211typedef UDPPKT *PUDPPKT;
212
231typedef struct
232{
233 beuint16_t proto;
234 beuint16_t len;
239typedef PsudeoHeader *PPSUH;
240
247extern uint8_t bTTL_Default;
249extern uint16_t wArpLifetime;
251extern BOOL bQuietStart;
254/* "C" Internal Functions */
255
269void IpProcessEthernetPacket(PoolPtr poolPtr, uint16_t packetSizeInBytes);
270
271extern "C"
272{
293 uint16_t GetSum(puint16_t addr, uint16_t count);
294
312 uint16_t GetSum20(puint32_t addr);
313
336 uint16_t GetSumHdr(PsudeoHeader &hdr, puint16_t addr, uint16_t count);
337}
338
356BOOL IsMyIp4(IPADDR4 ip, int ifc = -1);
357
376
395IPADDR4 GetSrcIpwIf4(int ifn, const IPADDR4 dst);
396
397#ifdef IPV6
414BOOL IsMyIp6(const IPADDR &ip, int ifc = -1);
416inline BOOL IsMyIp(const IPADDR &ip, int ifc = -1)
417{
418 return IsMyIp6(ip, ifc);
419};
420
437
450inline IPADDR GetSrcIp(const IPADDR &dst)
451{
452 return GetSrcIp6(dst);
453};
454
470IPADDR GetSrcIp6wIf(int ifn, const IPADDR &dst);
471
485inline IPADDR GetSrcIpwIf(int ifn, const IPADDR &dst)
486{
487 return GetSrcIp6wIf(ifn, dst);
488};
489
490#else
504inline BOOL IsMyIp(IPADDR4 ip, int ifc = -1)
505{
506 return IsMyIp4(ip, ifc);
507};
508
521inline IPADDR4 GetSrcIp(IPADDR4 dst)
522{
523 return GetSrcIp4(dst);
524};
525
539inline IPADDR4 GetSrcIpwIf(int ifn, IPADDR4 dst)
540{
541 return GetSrcIpwIf4(ifn, dst);
542};
543
544#endif
545
546/* "C++" Internal Functions */
547
566void SendICMPError(PoolPtr pBadPacket, uint8_t type, uint8_t code);
567
589
607void FixHeaderAndSendViaInterface(PoolPtr p, PIPPKT pIp, int Interface);
608
633{
634 return (p == NULL) ? NULL : (PIPPKT)(p->pData + 14);
635};
636
645inline PIPPKT GetIpPkt(PEFRAME pFrame)
646{
647 return (PIPPKT)(pFrame->pData);
648};
649
658inline PIPPKT GetIpPkt(PVLEFRAME pFrame)
659{
660 return (PIPPKT)(pFrame->pData);
661};
662
677{
678 if (pIp == NULL) { return NULL; }
679 if (pIp->bVerHdrLen == 0x45) { return (PUDPPKT)pIp->DATA; }
680 return (PUDPPKT)(pIp->DATA + (((pIp->bVerHdrLen & 0XF) * 4) - 20));
681}
682
685/* "C++" Runtime Library Functions */
686
708
731
755
779
805
826class UDPPacket;
827typedef int(TaskPacketFunc)(UDPPacket &pkt, int ifn);
828
839extern TaskPacketFunc *pTaskPacketFunc;
840
868
907int Ping4(IPADDR4 to, uint16_t id, uint16_t seq, uint16_t maxwaitticks);
908
939int Ping4ViaInterface(IPADDR4 to, uint16_t id, uint16_t seq, uint16_t wait, int interface);
940
941#ifdef IPV6
965int Ping6(const IPADDR &to, uint16_t id, uint16_t seq, uint16_t maxwaitticks, int size = 32);
966
991int Ping6ViaInterface(const IPADDR &to, uint16_t id, uint16_t seq, uint16_t wait, int interface, int size = 32);
1008inline int Ping(const IPADDR &to, uint16_t id, uint16_t seq, uint16_t maxwaitticks, int size = 32)
1009{
1010 return Ping6(to, id, seq, maxwaitticks, size);
1011};
1012
1030inline int PingViaInterface(const IPADDR &to, uint16_t id, uint16_t seq, uint16_t wait, int interface, int size = 32)
1031{
1032 return Ping6ViaInterface(to, id, seq, wait, interface, size);
1033};
1034#else
1050inline int Ping(IPADDR4 to, uint16_t id, uint16_t seq, uint16_t maxwaitticks)
1051{
1052 return Ping4(to, id, seq, maxwaitticks);
1053};
1054
1071inline int PingViaInterface(IPADDR4 to, uint16_t id, uint16_t seq, uint16_t wait, int interface)
1072{
1073 return Ping4ViaInterface(to, id, seq, wait, interface);
1074};
1075#endif
1076
1104BOOL IsLocal4(IPADDR4 ip, int ifc);
1105
1131
1132#if defined MULTIHOME || defined AUTOIP
1168int AddInterface(IPADDR4 addr, IPADDR4 mask, IPADDR4 gateway, int root_if = 0);
1169
1185int GetMultiHomeInterface(IPADDR4 ipa, int norgif);
1186
1201#ifdef IPV6
1216
1229inline int GetLocalIPInterface(const IPADDR &ipa)
1230{
1231 return GetLocalIPInterface6(ipa);
1232};
1233#else
1246inline int GetLocalIPInterface(IPADDR4 ipa)
1247{
1248 return GetLocalIPInterface4(ipa);
1249};
1250#endif
1251#endif
1252
1253 /*
1254 *******************************************************************************
1255 *
1256 * Deprecated and Backward Compatibility Runtime Library Functions
1257 *
1258 *******************************************************************************
1259 */
1260
1261#ifndef IPV6
1262#ifndef IPV4ONLY
1263#error Got to pick an IP version
1264#endif
1265#endif
1266
1267#ifdef IPV6
1268#ifdef IPV4ONLY
1269#error Must select IPV4ONLY or IPV6 but not both
1270#endif
1271#endif
1272
1273
1274
1275
1276
1302
1303#endif /* #ifndef _NB_IP_H */
1304
Used to store and manipulate IPv4 addresses in dual stack mode.
Definition nettypes.h:225
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
UDP Packet Class - Complete UDP packet management.
Definition udp.h:602
int Ping6ViaInterface(const IPADDR &to, uint16_t id, uint16_t seq, uint16_t wait, int interface, int size=32)
Send an IPv6 "ping" packet via a specific network interface.
int AddInterface(IPADDR4 addr, IPADDR4 mask, IPADDR4 gateway, int root_if=0)
Add a new network interface with the specified configuration.
Definition multihome.h:151
int GetProperInterface4(IPADDR4 dst)
Get the appropriate interface number for reaching a destination.
void FixHeaderAndSend(PoolPtr p, PIPPKT pIp)
Complete IP header fields and send packet on primary interface.
IPADDR GetSrcIp(const IPADDR &dst)
Generic function to get source IP address for reaching a destination (IPv6 version).
Definition ip.h:450
BOOL bQuietStart
When TRUE, suppresses startup messages during network initialization. Set before calling init().
void FixHeaderAndSendViaInterface(PoolPtr p, PIPPKT pIp, int Interface)
Complete IP header fields and send packet via specified interface.
int GetLocalIPInterface4(IPADDR4 ipa)
Get the local interface number for a specific IPv4 address.
int Ping4(IPADDR4 to, uint16_t id, uint16_t seq, uint16_t maxwaitticks)
Send a "ping" packet and wait the specified timeout for a response.
uint16_t GetEphemeralPort()
Generate a random ephemeral port number.
struct _IpHeaderIPv4 IpHeaderIPv4
IP Header Structure (IPv4)
void InitializeStack()
Initialize the IP protocol stack.
FragmentProcessFunction * pFragFunc
Global function pointer for fragment processing callback.
PIPPKT GetIpPkt(PoolPtr p)
Get IP packet pointer from network buffer pool buffer.
Definition ip.h:632
uint16_t wArpLifetime
ARP cache entry lifetime in seconds. Entries older than this are refreshed. Typical value is 300-1200...
int Ping6(const IPADDR &to, uint16_t id, uint16_t seq, uint16_t maxwaitticks, int size=32)
Send an IPv6 "ping" packet and wait the specified timeout for a response.
IPADDR GetSrcIp6(const IPADDR &dst)
Get the appropriate source IPv6 address for reaching a destination.
int Ping4ViaInterface(IPADDR4 to, uint16_t id, uint16_t seq, uint16_t wait, int interface)
Send a "ping" packet via a specific network interface and wait for a response.
void SendICMPError(PoolPtr pBadPacket, uint8_t type, uint8_t code)
Send an ICMP error message in response to a bad packet.
IPADDR GetSrcIpwIf(int ifn, const IPADDR &dst)
Generic function to get source IP address for an interface (IPv6 version).
Definition ip.h:485
TaskPacketFunc * pTaskPacketFunc
Global function pointer for UDP task packet processing callback.
BOOL IsMyIp6(const IPADDR &ip, int ifc=-1)
Check if the specified IPv6 address belongs to this device.
IPADDR4 GetSrcIp4(IPADDR4 dst)
Get the appropriate source IPv4 address for reaching a destination.
uint16_t GetSum20(puint32_t addr)
Calculate Internet checksum for exactly 20 bytes of data.
void FragmentProcessFunction(PoolPtr pp)
User-defined function pointer type for processing fragmented IP packets.
Definition ip.h:730
BOOL IsMyIp(const IPADDR &ip, int ifc=-1)
Generic function to check if IP address belongs to this device (uses IPv6 when available)
Definition ip.h:416
int GetLocalIPInterface6(const IPADDR &ipa)
Get the local interface number for a specific IPv6 address.
int GetLocalIPInterface(const IPADDR &ipa)
Generic function to get local interface for an IP address (IPv6 version).
Definition ip.h:1229
int GetMultiHomeInterface(IPADDR4 ipa, int norgif)
Get the multi-home interface for a specific IP address.
IPADDR GetSrcIp6wIf(int ifn, const IPADDR &dst)
Get the source IPv6 address for a specific interface and destination.
int Ping(const IPADDR &to, uint16_t id, uint16_t seq, uint16_t maxwaitticks, int size=32)
Generic ping function that uses IPv6 when IPv6 is enabled.
Definition ip.h:1008
IPADDR4 GetSrcIpwIf4(int ifn, const IPADDR4 dst)
Get the source IPv4 address for a specific interface and destination.
BOOL IsLocal4(IPADDR4 ip, int ifc)
Check if an IPv4 address is on the local network segment.
void ShowIPBuffer(PoolPtr rp)
Display the contents of a network buffer for diagnostic purposes.
uint16_t GetSumHdr(PsudeoHeader &hdr, puint16_t addr, uint16_t count)
Calculate Internet checksum using a pseudo-header.
PUDPPKT GetUdpPkt(PIPPKT pIp)
Get UDP packet pointer from IP packet pointer.
Definition ip.h:676
void IpProcessEthernetPacket(PoolPtr poolPtr, uint16_t packetSizeInBytes)
Process an Ethernet packet for IP layer handling.
PromisciousPacketFunc * pPromisciousPacketFunc
Global function pointer for promiscuous packet processing callback.
int PingViaInterface(const IPADDR &to, uint16_t id, uint16_t seq, uint16_t wait, int interface, int size=32)
Generic ping via interface function that uses IPv6 when IPv6 is enabled.
Definition ip.h:1030
uint16_t GetSum(puint16_t addr, uint16_t count)
Calculate Internet checksum for the specified data.
void PromisciousPacketFunc(PoolPtr pp)
User-defined function pointer type for processing promiscuous packets.
Definition ip.h:778
BOOL IsMyIp4(IPADDR4 ip, int ifc=-1)
Check if the specified IPv4 address belongs to this device.
uint8_t bTTL_Default
Default Time To Live (hop count) for outgoing IP packets. Typical value is 64 or 128.
IP Header Structure (IPv4)
Definition ip.h:141
uint8_t diffServNEcn
Differentiated Services Code Point (6 bits) and Explicit Congestion Notification (2 bits) - used for ...
Definition ip.h:143
uint8_t versionNLength
Version (4 bits) and header length (4 bits). Version=4 for IPv4, length in 32-bit words IPPKT::bVerHd...
Definition ip.h:142
beuint16_t identification
Fragment identification field - same for all fragments of a datagram IPPKT::wpktId.
Definition ip.h:145
beuint32_t destinationAddress
Destination IP address as 4 octets in network byte order IPPKT::ipDst.
Definition ip.h:152
beuint16_t totalLength
Total datagram size in bytes including IP header and data payload IPPKT::wLength.
Definition ip.h:144
beuint32_t sourceAddress
Source IP address as 4 octets in network byte order IPPKT::ipSrc.
Definition ip.h:151
beuint16_t headerChecksum
Header checksum - 16-bit one's complement checksum of IP header only (not data) IPPKT::hCSum.
Definition ip.h:150
uint8_t protocol
IANA protocol number identifying next-level protocol (TCP=6, UDP=17, ICMP=1) IPPKT::proto.
Definition ip.h:148
beuint16_t flagsNFragmentOffset
Flags (3 bits) and fragment offset (13 bits) - offset in 8-byte units IPPKT::wFlags_Frag.
Definition ip.h:146
uint8_t timeToLive
Time To Live - typically used as hop count, decremented by each router IPPKT::bTTL.
Definition ip.h:147
Internal IPv4 Header Representation.
Definition ip.h:169
uint16_t hCSum
Header checksum.
Definition ip.h:180
IPADDR4 ipSrc
Source IP address.
Definition ip.h:181
uint8_t proto
Protocol number (TCP, UDP, ICMP, etc.)
Definition ip.h:176
uint8_t DATA[]
Flexible array member - payload data follows header (actual length varies by packet)
Definition ip.h:183
beuint16_t wLength
Total packet length in bytes including header.
Definition ip.h:172
uint8_t bTTL
Time To Live / hop count.
Definition ip.h:175
uint8_t bTos
Type of Service / Differentiated Services field (same as diffServNEcn)
Definition ip.h:171
beuint16_t wpktId
Packet identification for fragment reassembly.
Definition ip.h:173
uint8_t bVerHdrLen
Version and header length (same as versionNLength in IpHeaderIPv4)
Definition ip.h:170
IPADDR4 ipDst
Destination IP address.
Definition ip.h:182
beuint16_t wFlags_Frag
Flags and fragment offset combined.
Definition ip.h:174
UDP/TCP IPv4 Pseudo-header Structure (bits 0 through 95 of the pseudo-header)
Definition ip.h:232
IPADDR4 dstip
Destination IP address (same as in IP header)
Definition ip.h:236
beuint16_t proto
Protocol number (UDP=17, TCP=6) - note: only 8 bits used, padded to 16 bits.
Definition ip.h:233
beuint16_t len
Length of UDP/TCP header plus data (not including pseudo-header itself)
Definition ip.h:234
IPADDR4 srcip
Source IP address (same as in IP header)
Definition ip.h:235
Internal UDP Packet Header Structure.
Definition ip.h:201
beuint16_t dstPort
Destination port number (0-65535)
Definition ip.h:203
beuint16_t srcPort
Source port number (0-65535)
Definition ip.h:202
uint16_t UdpCSum
UDP checksum (optional in IPv4, mandatory in IPv6)
Definition ip.h:208
beuint16_t UdpLen
Datagram length in bytes including UDP header and data (minimum 8 bytes)
Definition ip.h:204
Main buffer structure for network and serial communication.
Definition buffers.h:90
uint8_t pData[ETHER_BUFFER_SIZE]
Buffer data payload (1548 bytes)
Definition buffers.h:104