// Send Request
Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, buffer);
-
- // Clean up
- IPStack_Buffer_DestroyBuffer(buffer);
// Wait for a reply
Time_ScheduleTimer(NULL, Interface->TimeoutDelay);
switch( req4->SWSize )
{
case 4:
- Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i",
+ Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i"
+ " (%02x:%02x:%02x:%02x:%02x:%02x)",
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->SourceIP.B[2], req4->SourceIP.B[3],
req4->SourceMac.B[0], req4->SourceMac.B[1],
req4->SourceMac.B[2], req4->SourceMac.B[3],
req4->SourceMac.B[4], req4->SourceMac.B[5]);
// Assumes only a header and footer at link layer
tIPStackBuffer *buffer = IPStack_Buffer_CreateBuffer(3);
- IPStack_Buffer_AppendSubBuffer(buffer, sizeof(struct sArpRequest4), 0, req4, NULL, NULL);
+ IPStack_Buffer_AppendSubBuffer(buffer,
+ sizeof(struct sArpRequest4), 0, req4,
+ NULL, NULL);
Link_SendPacket(Adapter, 0x0806, req4->DestMac, buffer);
- IPStack_Buffer_DestroyBuffer(buffer);
}
break;
#if ARPv6
* buffer.c
* - Scatter-gather handling
*/
+#define DEBUG 0
#include "ipstack.h"
#include "include/buffer.h"
size_t PostLength;
tIPStackBufferCb Cb;
void *CbArg;
- // TODO: Callbacks?
} SubBuffers[];
};
void IPStack_Buffer_DestroyBuffer(tIPStackBuffer *Buffer)
{
+ LOG("Called with %p by %p", Buffer, __builtin_return_address(0));
ASSERT(Buffer);
IPStack_Buffer_ClearBuffer(Buffer);
Buffer->MaxSubBufffers = 0;
tIPStackBuffer *buffer = IPStack_Buffer_CreateBuffer(1 + IPV4_BUFFERS);
IPStack_Buffer_AppendSubBuffer(buffer, Length, 0, hdr, NULL, NULL);
IPv4_SendPacket(Interface, *(tIPv4*)Address, 1, ntohs(hdr->Sequence), buffer);
- IPStack_Buffer_DestroyBuffer(buffer);
break;
default:
break;
tIPStackBuffer *buffer = IPStack_Buffer_CreateBuffer(1 + IPV4_BUFFERS);
IPStack_Buffer_AppendSubBuffer(buffer, sizeof(buf), 0, buf, NULL, NULL);
IPv4_SendPacket(Interface, Addr, 1, i, buffer);
- IPStack_Buffer_DestroyBuffer(buffer);
end = ts + Interface->TimeoutDelay;
while( !gICMP_PingSlots[i].bArrived && now() < end) Threads_Yield();
#define VALIDATE_CHECKSUM 0
#define MAX_PACKET_SIZE 2048
+// === TYPES ===
+typedef struct {
+ Uint16 Type;
+ tPacketCallback Callback;
+} tLink_PktType;
+
+
// === PROTOTYPES ===
void Link_RegisterType(Uint16 Type, tPacketCallback Callback);
void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, tIPStackBuffer *Buffer);
// === GLOBALS ===
int giRegisteredTypes = 0;
int giRegisteredTypeSpace = 0;
-struct {
- Uint16 Type;
- tPacketCallback Callback;
-} *gaRegisteredTypes;
+tLink_PktType *gaRegisteredTypes;
int gbLink_CRCTableGenerated = 0;
Uint32 gaiLink_CRCTable[256];
*/
void Link_RegisterType(Uint16 Type, tPacketCallback Callback)
{
- int i;
- void *tmp;
+ tLink_PktType *typeslot = NULL;
- for( i = giRegisteredTypes; i -- ; )
+ for( int i = 0; i < giRegisteredTypes; i ++)
{
if(gaRegisteredTypes[i].Type == Type) {
Log_Warning("Net Link", "Attempt to register 0x%x twice", Type);
return ;
}
// Ooh! Free slot!
- if(gaRegisteredTypes[i].Callback == NULL) break;
+ if(gaRegisteredTypes[i].Callback == NULL)
+ {
+ typeslot = &gaRegisteredTypes[i];
+ break;
+ }
}
- if(i == -1)
+ if(typeslot == NULL)
{
- giRegisteredTypeSpace += 5;
- tmp = realloc(gaRegisteredTypes, giRegisteredTypeSpace*sizeof(*gaRegisteredTypes));
- if(!tmp) {
- Log_Warning("Net Link",
- "Out of heap space! (Attempted to allocate %i)",
- giRegisteredTypeSpace*sizeof(*gaRegisteredTypes)
- );
- return ;
+ if( giRegisteredTypes == giRegisteredTypeSpace )
+ {
+ giRegisteredTypeSpace += 5;
+ void *tmp = realloc(gaRegisteredTypes, giRegisteredTypeSpace*sizeof(tLink_PktType));
+ if(!tmp) {
+ Log_Warning("Net Link",
+ "Out of heap space! (Attempted to allocate %i)",
+ giRegisteredTypeSpace*sizeof(tLink_PktType)
+ );
+ return ;
+ }
+ gaRegisteredTypes = tmp;
}
- gaRegisteredTypes = tmp;
- i = giRegisteredTypes;
- giRegisteredTypes ++;
+ typeslot = &gaRegisteredTypes[giRegisteredTypes++];
}
- gaRegisteredTypes[i].Callback = Callback;
- gaRegisteredTypes[i].Type = Type;
+ typeslot->Callback = Callback;
+ typeslot->Type = Type;
}
/**
- * \fn void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, int Length, void *Buffer)
* \brief Formats and sends a packet on the specified interface
*/
void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, tIPStackBuffer *Buffer)
// TODO: Check checksum
#endif
+ Uint16 type = ntohs(hdr->Type);
// Check if there is a registered callback for this packet type
- int i;
- for( i = giRegisteredTypes; i--; )
+ for( int i = giRegisteredTypes; i--; )
{
- if(gaRegisteredTypes[i].Type == ntohs(hdr->Type)) break;
+ if(gaRegisteredTypes[i].Type == type)
+ {
+ // Call the callback
+ gaRegisteredTypes[i].Callback(
+ Adapter,
+ hdr->Src,
+ len - sizeof(tEthernetHeader),
+ hdr->Data
+ );
+ free(data);
+ return 0;
+ }
}
// No? Ignore it
- if( i == -1 ) {
- Log_Log("Net Link", "Unregistered type 0x%x", ntohs(hdr->Type));
-
- free(data);
- return 1;
- }
-
- // Call the callback
- gaRegisteredTypes[i].Callback(
- Adapter,
- hdr->Src,
- len - sizeof(tEthernetHeader),
- hdr->Data
- );
-
- free(data);
+ Log_Log("Net Link", "Unregistered type 0x%04x", type);
- return 0;
+ free(data);
+ return 1;
}
// From http://www.cl.cam.ac.uk/research/srg/bluebook/21/crc/node6.html
IPStack_Buffer_AppendSubBuffer(buffer, sizeof(hdr), 0, &hdr, NULL, NULL);
// TODO: What if Channel->Interface is NULL here?
IPv4_SendPacket(Channel->Interface, *(tIPv4*)Address, IP4PROT_UDP, 0, buffer);
- IPStack_Buffer_DestroyBuffer(buffer);
break;
}
}