The Reception
To the reception of an IP packet, the operations to follow are simples enough, and are implemented in the method processIP.
First of all must verify that the packet recipient is the IP address assigned to the server; this control is made confronting the variable MyIP wiht
the field Destination Address of the packet.
The variable MyIp is defined in file ip.c and is initialized in the method encInit:
// in ip.c IPAddr MyIP; // in define.h #define MY_IP1 10 #define MY_IP2 0 #define MY_IP3 0 #define MY_IP4 7 // in enc28j60.c, encInit() MyIP.b[0] = MY_IP1; MyIP.b[1] = MY_IP2; MyIP.b[2] = MY_IP3; MyIP.b[3] = MY_IP4;
The IP heading integrity control is constituted from the verifying of the checksum field; this operation is made from hte method DMAChecksum that we'll see a little bit later.
Let's see this method processIP:
void processIP(){
IP_Header header;
u16 chksum;
u8 optlen;
encGetArray((u8*)&header, sizeof(header));
if (!ipMatch(header.destIP,MyIP)) return;
// controlla il checksum
if (DMAChecksum(0, (header.verlen & 0x0F)*4, FALSE)) return;
// scarta eventuali campi option
optlen = (header.verlen & 0x0F)*4 - 20;
if (optlen)
encDiscard(optlen);
// big endian --> little endian
swapIPHeader(&header);
switch (header.protocol){
case IPPROTO_ICMP: processICMP(header);
break;
/* case IPPROTO_UDP :
break;
case IPPROTO_TCP:
break; */
default: break;
}
}The method ipMatch compares two IP addresses:
u8 ipMatch(IPAddr ip1, IPAddr ip2){
return (ip1.b[0] == ip2.b[0] && ip1.b[1] == ip2.b[1] && ip1.b[2] == ip2.b[2] && ip1.b[3] == ip2.b[3]);
}
The sending
To send an IP packet is necessary to prepare a MAC heading and an IP heading to write inthe transmission buffer;
at these will follow the dates field that will contain a 4th level packet. This is the duty of this method IPPutHeader,
where are passed some parameters:
Here is the method:
void IPPutHeader(IPAddr target, u8 protocol, u8* data, u16 datalen, u16 totalLength){
IP_Header header;
u16 tmp;
header.verlen = 0x45;
header.typeOfService = 0x00;
header.totalLength = 20+totalLength;
header.id = ++id;
header.fragmentInfo = 0x00;
header.TTL = 128;
header.protocol = protocol;
header.checksum = 0x00;;
header.sourceIP = MyIP;
header.destIP = target;
swapIPHeader(&header);
MACPutHeader(remoteAddr, TYPE_IP);
encPutArray((u8*)&header, sizeof(header));
encPutArray(data, datalen);
putChecksum(IP_Offset+10, DMAChecksum(IP_Offset, sizeof(header), TRUE));
}First of all is prepared the heading in the variable header using some parameters and others constance; then is called the function MACPutHeader which beyond to write in the buffer the MAC heading, prepares also the writing and sending oeration.Then are send to the controller the IP heading and the dates, at last is calculated the packet checksum and this also is wrote in the buffer (to the location IPOffset+10).
To find the checksum, the relative field in the packet mus be set at zero, then through the method DMAChecksum is calculated
directly from the ENC28J60 buffer.
|