}
#endif
- Log_Debug("ARP", "Request ID %i", ntohs(req4->Request));
-
switch( ntohs(req4->Request) )
{
case 1: // You want my IP?
- Log_Debug("ARP", "ARP Request Address class %i", req4->SWSize);
// Check what type of IP it is
switch( req4->SWSize )
{
case 4:
- Log_Debug("ARP", "From MAC %02x:%02x:%02x:%02x:%02x:%02x",
- req4->SourceMac.B[0], req4->SourceMac.B[1],
- req4->SourceMac.B[2], req4->SourceMac.B[3],
- req4->SourceMac.B[4], req4->SourceMac.B[5]);
- //Log_Debug("ARP", "to MAC %02x:%02x:%02x:%02x:%02x:%02x",
- // req4->DestMac.B[0], req4->DestMac.B[1],
- // req4->DestMac.B[2], req4->DestMac.B[3],
- // req4->DestMac.B[4], req4->DestMac.B[5]);
Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i",
req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
req4->DestIP.B[3],
req4->SourceIP.B[0], req4->SourceIP.B[1],
req4->SourceIP.B[2], req4->SourceIP.B[3]);
+ Log_Debug("ARP", " from MAC %02x:%02x:%02x:%02x:%02x:%02x",
+ req4->SourceMac.B[0], req4->SourceMac.B[1],
+ req4->SourceMac.B[2], req4->SourceMac.B[3],
+ req4->SourceMac.B[4], req4->SourceMac.B[5]);
iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
if( iface )
{
- Log_Debug("ARP", "Caching sender's IP Address");
ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
req4->DestIP = req4->SourceIP;
{
tICMPHeader *hdr = Buffer;
- Log("[ICMP ] Length = %i", Length);
- Log("[ICMP ] hdr->Type = %i", hdr->Type);
- Log("[ICMP ] hdr->Code = %i", hdr->Code);
- Log("[ICMP ] hdr->Checksum = 0x%x", ntohs(hdr->Checksum));
- Log("[ICMP ] hdr->ID = 0x%x", ntohs(hdr->ID));
- Log("[ICMP ] hdr->Sequence = 0x%x", ntohs(hdr->Sequence));
+ //Log_Debug("ICMPv4", "Length = %i", Length);
+ Log_Debug("ICMPv4", "hdr->Type, hdr->Code = %i, %i", hdr->Type, hdr->Code);
+ //Log_Debug("ICMPv4", "hdr->Checksum = 0x%x", ntohs(hdr->Checksum));
+ Log_Debug("ICMPv4", "hdr->ID = 0x%x", ntohs(hdr->ID));
+ Log_Debug("ICMPv4", "hdr->Sequence = 0x%x", ntohs(hdr->Sequence));
switch(hdr->Type)
{
// -- 0: Echo Reply
case ICMP_ECHOREPLY:
if(hdr->Code != 0) {
- Warning("[ICMP ] Code == %i for ICMP Echo Reply, should be 0", hdr->Code);
+ Log_Warning("ICMPv4", "Code == %i for ICMP Echo Reply, should be 0", hdr->Code);
return ;
}
if(hdr->ID != (Uint16)~hdr->Sequence) {
- Warning("[ICMP ] ID and Sequence values do not match");
+ Log_Warning("ICMPv4", "ID and Sequence values do not match");
//return ;
}
gICMP_PingSlots[hdr->ID].bArrived = 1;
switch(hdr->Code)
{
case 3: // Port Unreachable
- Log("[ICMP ] Destination Unreachable (Port Unreachable)");
+ Log_Debug("ICMPv4", "Destination Unreachable (Port Unreachable)");
break;
default:
- Log("[ICMP ] Destination Unreachable (Code %i)", hdr->Code);
+ Log_Debug("ICMPv4", "Destination Unreachable (Code %i)", hdr->Code);
break;
}
// IPv4_Unreachable( Interface, hdr->Code, htons(hdr->Length)-sizeof(tICMPHeader), hdr->Data );
// -- 8: Echo Request
case ICMP_ECHOREQ:
if(hdr->Code != 0) {
- Warning("[ICMP ] Code == %i for ICMP Echo Request, should be 0", hdr->Code);
+ Log_Warning("ICMPv4", "Code == %i for ICMP Echo Request, should be 0", hdr->Code);
return ;
}
- Log("[ICMP ] Replying");
+ //Log_Debug("ICMPv4", "Replying");
hdr->Type = ICMP_ECHOREPLY;
hdr->Checksum = 0;
hdr->Checksum = htons( IPv4_Checksum(hdr, Length) );
- Log("[ICMP ] Checksum = 0x%04x", hdr->Checksum);
+ //Log_Debug("ICMPv4", "Checksum = 0x%04x", hdr->Checksum);
IPv4_SendPacket(Interface, *(tIPv4*)Address, 1, ntohs(hdr->Sequence), Length, hdr);
break;
default:
hdr->HeaderChecksum = 0; // Will be set later
hdr->Source = *(tIPv4*)Iface->Address;
hdr->Destination = Address;
- hdr->HeaderChecksum = IPv4_Checksum(hdr, sizeof(tIPv4Header));
+ hdr->HeaderChecksum = htons(IPv4_Checksum(hdr, sizeof(tIPv4Header)));
Log_Log("IPv4", "Sending packet to %i.%i.%i.%i",
Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
// Check Header checksum
{
Uint16 hdrVal, compVal;
- hdrVal = hdr->HeaderChecksum;
+ hdrVal = ntohs(hdr->HeaderChecksum);
hdr->HeaderChecksum = 0;
- compVal = IPv4_Checksum(hdr, hdr->HeaderLength);
+ compVal = IPv4_Checksum(hdr, hdr->HeaderLength * 4);
if(hdrVal != compVal) {
Log_Log("IPv4", "Header checksum fails (%04x != %04x)", hdrVal, compVal);
return ;
// Routing
if(!iface)
{
- Log_Debug("IPv4", "Route the packet");
+ //Log_Debug("IPv4", "Route the packet");
// TODO: Parse Routing tables and determine where to send it
{
if( iface->Adapter != Adapter ) continue;
if( iface->Type != 4 ) continue;
+ //Log_Debug("IPv4", "%x == %x?\n", addr, ntohl(((tIPv4*)iface->Address)->L));
if( IP4_EQU(Address, *(tIPv4*)iface->Address) )
return iface;
*/
Uint16 IPv4_Checksum(const void *Buf, int Size)
{
- Uint16 sum = 0;
+ Uint32 sum = 0;
const Uint16 *arr = Buf;
int i;
for(i = 0; i < Size; i++ )
{
Uint16 val = ntohs(arr[i]);
- if((int)sum + val > 0xFFFF)
- sum ++; // Simulate 1's complement
sum += val;
}
- return ~sum ;
+
+ // Apply one's complement
+ while (sum >> 16)
+ sum = (sum & 0xFFFF) + (sum >> 16);
+
+ return ~sum;
}
/**
int i;
void *tmp;
- Type = htons(Type); // Set to network order
-
for( i = giRegisteredTypes; i -- ; )
{
if(gaRegisteredTypes[i].Type == Type) {
- Log_Warning("NET", "Attempt to register 0x%x twice", Type);
+ Log_Warning("Net Link", "Attempt to register 0x%x twice", Type);
return ;
}
// Ooh! Free slot!
giRegisteredTypeSpace += 5;
tmp = realloc(gaRegisteredTypes, giRegisteredTypeSpace*sizeof(*gaRegisteredTypes));
if(!tmp) {
- Log_Warning("NET",
+ Log_Warning("Net Link",
"Out of heap space! (Attempted to allocate %i)",
giRegisteredTypeSpace*sizeof(*gaRegisteredTypes)
);
Uint8 buf[bufSize]; // dynamic stack arrays ftw!
tEthernetHeader *hdr = (void*)buf;
- Log_Log("NET", "Sending %i bytes to %02x:%02x:%02x:%02x:%02x:%02x (Type 0x%x)",
+ Log_Log("Net Link", "Sending %i bytes to %02x:%02x:%02x:%02x:%02x:%02x (Type 0x%x)",
Length, To.B[0], To.B[1], To.B[2], To.B[3], To.B[4], To.B[5], Type);
hdr->Dest = To;
}
if(tid > 0) {
- Log_Log("NET", "Watching '%s' using tid %i", Adapter->Device, tid);
+ Log_Log("Net Link", "Watching '%s' using tid %i", Adapter->Device, tid);
return ;
}
Link_InitCRC();
Threads_SetName(Adapter->Device);
- Log_Log("NET", "Thread %i watching '%s'", Threads_GetTID(), Adapter->Device);
+ Log_Log("Net Link", "Thread %i watching '%s'", Threads_GetTID(), Adapter->Device);
// Child Thread
while(Adapter->DeviceFD != -1)
Uint32 checksum;
// Wait for a packet (Read on a network device is blocking)
- Log_Debug("NET", "Waiting on adapter FD#0x%x", Adapter->DeviceFD);
+ //Log_Debug("NET", "Waiting on adapter FD#0x%x", Adapter->DeviceFD);
ret = VFS_Read(Adapter->DeviceFD, MAX_PACKET_SIZE, buf);
if(ret == -1) break;
continue;
}
- Log_Log("NET", "Packet from %02x:%02x:%02x:%02x:%02x:%02x",
+ Log_Log("Net Link",
+ "Packet from %02x:%02x:%02x:%02x:%02x:%02x"
+ " to %02x:%02x:%02x:%02x:%02x:%02x",
hdr->Src.B[0], hdr->Src.B[1], hdr->Src.B[2],
- hdr->Src.B[3], hdr->Src.B[4], hdr->Src.B[5]
- );
- Log_Log("NET", "to %02x:%02x:%02x:%02x:%02x:%02x",
+ hdr->Src.B[3], hdr->Src.B[4], hdr->Src.B[5],
hdr->Dest.B[0], hdr->Dest.B[1], hdr->Dest.B[2],
hdr->Dest.B[3], hdr->Dest.B[4], hdr->Dest.B[5]
);
checksum = *(Uint32*)&hdr->Data[ret-sizeof(tEthernetHeader)-4];
- Log_Log("NET", "Checksum 0x%08x", checksum);
+ //Log_Log("NET", "Checksum 0x%08x", checksum);
+ // TODO: Check checksum
// Check if there is a registered callback for this packet type
for( i = giRegisteredTypes; i--; )
{
- if(gaRegisteredTypes[i].Type == hdr->Type) break;
+ if(gaRegisteredTypes[i].Type == ntohs(hdr->Type)) break;
}
// No? Ignore it
if( i == -1 ) {
- Log_Log("NET", "Unregistered type 0x%x", ntohs(hdr->Type));
+ Log_Log("Net Link", "Unregistered type 0x%x", ntohs(hdr->Type));
continue;
}
buf[2] = (htons(Length)<<16) | (6<<8) | 0;
Data->Checksum = 0;
memcpy( &buf[3], Data, Length );
- Data->Checksum = IPv4_Checksum( buf, buflen );
+ Data->Checksum = htons( IPv4_Checksum( buf, buflen ) );
free(buf);
IPv4_SendPacket(Conn->Interface, Conn->RemoteIP.v4, IP4PROT_TCP, 0, Length, Data);
break;