3 * - IPv4 Protcol Handling
10 int IPv4_Initialise();
11 void IPv4_int_GetPacket(tAdapter *Interface, tMacAddr From, int Length, void *Buffer);
12 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
13 Uint32 IPv4_Netmask(int FixedBits);
16 tIPCallback gaIPv4_Callbacks[256];
20 * \fn int IPv4_Initialise()
24 Link_RegisterType(IPV4_ETHERNET_ID, IPv4_int_GetPacket);
29 * \fn void IPv4_int_GetPacket(tInterface *Adapter, tMacAddr From, int Length, void *Buffer)
30 * \brief Process an IPv4 Packet
32 void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
34 tIPv4Header *hdr = Buffer;
38 if(Length < sizeof(tIPv4Header)) return;
40 // Check that the version IS IPv4
41 if(hdr->Version != 4) return;
43 // Check Header checksum
46 // Check Packet length
47 if(hdr->TotalLength > Length) return;
49 // Get Interface (allowing broadcasts)
50 iface = IPv4_GetInterface(Adapter, hdr->Source, 1);
51 if(!iface) return; // Not for us? Well, let's ignore it
56 dataLength = hdr->TotalLength - sizeof(tIPv4Header);
57 data = &hdr->Options[0];
60 gaIPv4_Callbacks[hdr->Protocol] (iface, &hdr->Source, dataLength, data);
64 * \fn tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address)
65 * \brief Searches an adapter for a matching address
67 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast)
69 tInterface *iface = NULL;
72 for( iface = Adapter->Interfaces; iface; iface = iface->Next)
74 if( iface->Type != 4 ) continue;
75 if( IP4_EQU(Address, iface->IP4.Address) )
78 if( !Broadcast ) continue;
80 // Check for broadcast
81 netmask = IPv4_Netmask(iface->IP4.SubnetBits);
82 if( (Address.L & netmask) == (iface->IP4.Address.L & netmask)
83 && (Address.L & ~netmask) == (0xFFFFFFFF & ~netmask) )
90 * \brief Convert a network prefix to a netmask
92 * For example /24 will become 255.255.255.0
94 Uint32 IPv4_Netmask(int FixedBits)
96 Uint32 ret = 0xFFFFFFFF;
97 ret >>= (32-FixedBits);
98 ret <<= (32-FixedBits);