3 * - IPv4 Protcol Handling
12 extern tInterface *gIP_Interfaces;
13 extern void ICMP_Initialise();
14 extern int ICMP_Ping(tInterface *Interface, tIPv4 Addr);
15 extern void UDP_Initialise();
16 extern tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address);
19 int IPv4_Initialise();
20 int IPv4_RegisterCallback(int ID, tIPCallback Callback);
21 void IPv4_int_GetPacket(tAdapter *Interface, tMacAddr From, int Length, void *Buffer);
22 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
23 Uint32 IPv4_Netmask(int FixedBits);
24 Uint16 IPv4_Checksum(void *Buf, int Size);
25 int IPv4_Ping(tInterface *Iface, tIPv4 Addr);
28 tIPCallback gaIPv4_Callbacks[256];
32 * \fn int IPv4_Initialise()
38 Link_RegisterType(IPV4_ETHERNET_ID, IPv4_int_GetPacket);
43 * \fn int IPv4_RegisterCallback( int ID, tIPCallback Callback )
44 * \brief Registers a callback
46 int IPv4_RegisterCallback(int ID, tIPCallback Callback)
48 if( ID < 0 || ID > 255 ) return 0;
49 if( gaIPv4_Callbacks[ID] ) return 0;
50 gaIPv4_Callbacks[ID] = Callback;
55 * \brief Creates and sends an IPv4 Packet
57 int IPv4_SendPacket(tInterface *Iface, tIPv4 Address, int Protocol, int ID, int Length, void *Data)
59 tMacAddr to = ARP_Resolve4(Iface, Address);
60 int bufSize = sizeof(tIPv4Header) + Length;
62 tIPv4Header *hdr = (void*)buf;
64 memcpy(&hdr->Options[0], Data, Length);
66 hdr->HeaderLength = sizeof(tIPv4Header)/4;
67 hdr->DiffServices = 0; // TODO: Check
68 hdr->TotalLength = htons( bufSize );
69 hdr->Identifcation = htons( ID ); // TODO: Check
70 hdr->TTL = DEFAULT_TTL;
71 hdr->Protocol = Protocol;
72 hdr->HeaderChecksum = 0; // Will be set later
73 hdr->Source = Iface->IP4.Address;
74 hdr->Destination = Address;
75 hdr->HeaderChecksum = htons( IPv4_Checksum(hdr, sizeof(tIPv4Header)) );
77 Log("[IPv4 ] Sending packet to %i.%i.%i.%i",
78 Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
79 Link_SendPacket(Iface->Adapter, IPV4_ETHERNET_ID, to, bufSize, buf);
84 * \fn void IPv4_int_GetPacket(tInterface *Adapter, tMacAddr From, int Length, void *Buffer)
85 * \brief Process an IPv4 Packet
87 void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
89 tIPv4Header *hdr = Buffer;
93 if(Length < sizeof(tIPv4Header)) return;
95 //Log("[IPv4 ] Version = %i", hdr->Version);
96 Log("[IPv4 ] HeaderLength = %i", hdr->HeaderLength);
97 Log("[IPv4 ] DiffServices = %i", hdr->DiffServices);
98 Log("[IPv4 ] TotalLength = %i", ntohs(hdr->TotalLength) );
99 //Log("[IPv4 ] Identifcation = %i", ntohs(hdr->Identifcation) );
100 //Log("[IPv4 ] TTL = %i", hdr->TTL );
101 Log("[IPv4 ] Protocol = %i", hdr->Protocol );
102 //Log("[IPv4 ] HeaderChecksum = 0x%x", ntohs(hdr->HeaderChecksum) );
103 Log("[IPv4 ] Source = %i.%i.%i.%i",
104 hdr->Source.B[0], hdr->Source.B[1], hdr->Source.B[2], hdr->Source.B[3] );
105 Log("[IPv4 ] Destination = %i.%i.%i.%i",
106 hdr->Destination.B[0], hdr->Destination.B[1],
107 hdr->Destination.B[2], hdr->Destination.B[3] );
109 // Check that the version IS IPv4
110 if(hdr->Version != 4) {
111 Log("[IPv4 ] hdr->Version(%i) != 4", hdr->Version);
115 // Check Header checksum
118 // Check Packet length
119 if( ntohs(hdr->TotalLength) > Length) {
120 Log("[IPv4 ] hdr->TotalLength(%i) > Length(%i)", ntohs(hdr->TotalLength), Length);
124 // Get Interface (allowing broadcasts)
125 iface = IPv4_GetInterface(Adapter, hdr->Destination, 1);
127 Log("[IPv4 ] Ignoring Packet (Not for us)");
128 return; // Not for us? Well, let's ignore it
134 dataLength = ntohs(hdr->TotalLength) - sizeof(tIPv4Header);
135 data = &hdr->Options[0];
138 gaIPv4_Callbacks[hdr->Protocol] (iface, &hdr->Source, dataLength, data);
142 * \fn tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address)
143 * \brief Searches an adapter for a matching address
145 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast)
147 tInterface *iface = NULL;
151 addr = ntohl( Address.L );
153 for( iface = gIP_Interfaces; iface; iface = iface->Next)
155 if( iface->Adapter != Adapter ) continue;
156 if( iface->Type != 4 ) continue;
157 if( IP4_EQU(Address, iface->IP4.Address) )
160 if( !Broadcast ) continue;
162 this = ntohl( iface->IP4.Address.L );
164 // Check for broadcast
165 netmask = IPv4_Netmask(iface->IP4.SubnetBits);
167 //Log("netmask = 0x%08x", netmask);
168 //Log("addr = 0x%08x", addr);
169 //Log("this = 0x%08x", this);
170 //Log("%08x == %08x && %08x == %08x",
171 // (addr & netmask), (this & netmask),
172 // (addr & ~netmask), (0xFFFFFFFF & ~netmask)
174 if( (addr & netmask) == (this & netmask)
175 && (addr & ~netmask) == (0xFFFFFFFF & ~netmask) )
182 * \brief Convert a network prefix to a netmask
184 * For example /24 will become 255.255.255.0
186 Uint32 IPv4_Netmask(int FixedBits)
188 Uint32 ret = 0xFFFFFFFF;
189 ret >>= (32-FixedBits);
190 ret <<= (32-FixedBits);
191 // Returs a little endian netmask
196 * \brief Calculate the IPv4 Checksum
198 Uint16 IPv4_Checksum(void *Buf, int Size)
204 Log("IPv4_Checksum: (%p, %i)", Buf, Size);
206 Size = (Size + 1) >> 1;
207 for(i = 0; i < Size; i++ )
209 if((int)sum + arr[i] > 0xFFFF)
210 sum ++; // Simulate 1's complement
213 return htons( ~sum );
217 * \brief Sends an ICMP Echo and waits for a reply
219 int IPv4_Ping(tInterface *Iface, tIPv4 Addr)
221 return ICMP_Ping(Iface, Addr);