3 * - IPv4 Protcol Handling
10 extern tInterface *gIP_Interfaces;
13 int IPv4_Initialise();
14 void IPv4_int_GetPacket(tAdapter *Interface, tMacAddr From, int Length, void *Buffer);
15 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
16 Uint32 IPv4_Netmask(int FixedBits);
19 tIPCallback gaIPv4_Callbacks[256];
23 * \fn int IPv4_Initialise()
27 Link_RegisterType(IPV4_ETHERNET_ID, IPv4_int_GetPacket);
32 * \fn void IPv4_int_GetPacket(tInterface *Adapter, tMacAddr From, int Length, void *Buffer)
33 * \brief Process an IPv4 Packet
35 void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
37 tIPv4Header *hdr = Buffer;
41 if(Length < sizeof(tIPv4Header)) return;
43 // Check that the version IS IPv4
44 if(hdr->Version != 4) return;
46 // Check Header checksum
49 // Check Packet length
50 if(hdr->TotalLength > Length) return;
52 // Get Interface (allowing broadcasts)
53 iface = IPv4_GetInterface(Adapter, hdr->Source, 1);
54 if(!iface) return; // Not for us? Well, let's ignore it
59 dataLength = hdr->TotalLength - sizeof(tIPv4Header);
60 data = &hdr->Options[0];
63 gaIPv4_Callbacks[hdr->Protocol] (iface, &hdr->Source, dataLength, data);
67 * \fn tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address)
68 * \brief Searches an adapter for a matching address
70 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast)
72 tInterface *iface = NULL;
75 for( iface = gIP_Interfaces; iface; iface = iface->Next)
77 if( iface->Adapter != Adapter ) continue;
78 if( iface->Type != 4 ) continue;
79 if( IP4_EQU(Address, iface->IP4.Address) )
82 if( !Broadcast ) continue;
84 // Check for broadcast
85 netmask = IPv4_Netmask(iface->IP4.SubnetBits);
86 if( (Address.L & netmask) == (iface->IP4.Address.L & netmask)
87 && (Address.L & ~netmask) == (0xFFFFFFFF & ~netmask) )
94 * \brief Convert a network prefix to a netmask
96 * For example /24 will become 255.255.255.0
98 Uint32 IPv4_Netmask(int FixedBits)
100 Uint32 ret = 0xFFFFFFFF;
101 ret >>= (32-FixedBits);
102 ret <<= (32-FixedBits);