Spanish Italian
17456 Users    

Ethernet 6/7

  Download PDF version of the Article

The IP protocol
The IP protocol is the communication base through the internet network, not at all IP is for Internet Protocol; its role is to permit the packets forwarding through the network, so that these can arrive to the recipient.
The fundamental element is the IP address (we already talk about it), through the medium of it can be identify sender and recipient unambiguous at the internal from the same network; attention!not at the world-wide how it is for the MAC address. In a private network, the IP address can be chose at will.

The IP heading
An IP header (IP Header) contains the sender and recipient IP address. Beyond these, there are some informations, more or less, important; let's see some:

  • Version: the IP protocol version in use; we'll use just the 4th version.
  • Header Length: the heading length in words of 4 byte.
  • Total Length: the IP packet total length (heading + dates).
  • Identification: the sequential number used to identify an IP packet during a communication.
  • Time To Live (TTL):contains the passages number (through the router) feasible first that the packet is eliminated (destination unreachable).
  • Protocol: indicates the fourth level protocol type contained in the dates field.
  • Checksum: field for the heading integrity control; concern just the heading not hte dates.

There are then others fields, but aren't used.

IP Header

Which in C becomes:

typedef struct {
   u8    b[4];
} IPAddr;

typedef struct {
	u8	verlen;
	u8	typeOfService;
	u16	totalLength;
	u16	id;
	u16	fragmentInfo;
	u8	TTL;
	u8	protocol;
	u16	checksum;
	IPAddr	sourceIP;	
	IPAddr	destIP;	
} IP_Header;

The Reception
To the reception of an IP packet, the operations to follow are simples enough, and are implemented in the method processIP.

  • Recipient control.
  • Errors control.
  • The passing to the superior level.

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:

  • target: the recipient IP address;
  • protocol: the 4th level protocol contained in the dates field;
  • data: punter to the memory location which contains the dates to insert in the packet;
  • datalen: the punters dates dimension at data;
  • totalLength: the packet total dimension, except the heading (it won't always coincide with the dataLen)

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.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
7 + 12 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Who's new

  • frederickdark
  • fernand
  • Ligrock
  • paolo_0665
  • chanuei
  • JM
  • samsilva77
  • araghube
  • stoll
  • mt

Who's online

There are currently 0 users and 80 guests online.