HBBR Basic

  • Increase font size
  • Default font size
  • Decrease font size

Part 2: IP Protocol Basics

E-mail Print PDF
User Rating: / 0
PoorBest 

HBBR Basic TCPIP

IP Protocol Basics (Part2)

IP acronym stands for Internet Protocol and is the most fundamental protocol from a TCP/IP suite of protocols. IP is concerned with handling packets of data being sent between computers.

Handling IP packets

IP packets are sequence of bytes with a defined header and payload. The fundamental function of the IP protocol is to deliver packets from the source identified by "Source Address" field in the header to the destination identified by the "Destination Address".

To be able to handle the packets this code is using two arrays of bytes, one for incoming packet and another for outgoing packet. Using arrays of bytes as a packet data storage has great advantages, data is stored in only one place and arrays can be passed as arguments to subs or functions.

The TCPIP.bas file has declaration of the two arrays of bytes.

Dim inPacket ( 0 To 1500 ) As Byte
Dim outPacket( 0 To 1500 ) As Byte

To make it easier to access fields in the packet header or data code implements Function to GET the value (return type matches the data type) and Sub to SET the value. Naming convention is "PROTOCOL_get_FIELD_NAME" and "PROTOCOL_set_FIELD_NAME".

Function IP_get_FIELD_NAME
Sub IP_set_FIELD_NAME

For example to get the destination address there is a function "IP_get_destination_address" in IP.bas file. Setting the the destination address field is done with sub "IP_set_destination_address". In both cases the first argument is a reference to the packet array which is a convention used in this project.

Source code

There are number of new files as compared to the Part 1:

TCPIP.bas

The main loop implements processing packets as follows:

  • SLIP Establish connection

  • SLIP Receive incoming packet

  • IP Validate incoming packet

  • IP Handle IP packet by calling "IP_handle_packet"

  • SLIP Send reply packet if necessary

IP.bas

The "IP_handle_packet" implements handling packets as follows:

  • IP Check if my IP address matches the destination IP address

  • IP Build reply packet header

  • IP Call handler based on the protocol

Code in Part 2 implements debugging by dumping content of the packets. It is turned on by defining "TCP_LOG" preprocessor constant, 3 will set the maximum level of debugging.

#Const TCP_LOG=3

Testing

To test the code, build the project and upload it to the microcontroller. Make sure that the board is connected as in Part 1 Making SLIP connection. Initiate SLIP connection on the host computer. Once the SLIP connection is established the host computer will start sending packets to the microcontroller. If the code has been compiled with logging enabled then incoming and outgoing packets will be dumped to the IDE Terminal window.

Here is the example of the SLIP session.

IP packet dump

Below is a description of all the fields in an IP packet header.

' IP Header

' 0                   1                   2                   3
' 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
'+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'|Version| HLEN  |Type of Service|         Total Length          | 0 - 3
'+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'|        Identification         |Flags|       Fragment Offset   | 4 - 7
'+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'| Time to Live  |   Protocol    |        Header Checksum        | 8 - 11
'+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'|                       Source Address                          | 12 - 15
'+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'|                    Destination Address                        | 16 - 19
'+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'|                         Options               |    Padding    | 20 - 23
'+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

' Byte
' 0     - Version, HLEN - header length in 32 bit words
' 1     - Type of Service
' 2-3   - Total length
' 4-5   - Identification
' 6-7   - Flags, Fragment Offset
' 8     - Time to Live TTL
' 9     - Protocol
' 10-11 - Header Checksum
' 12-15 - Source Address
' 16-19 - Destination Address
' 20-22 - Options
' 23    - Padding

Additional resources

Wikipedia IP protocol page

There are many web resources with detailed description of the TCP/IP protocol please check TCPIP links page for a selection of links relevant to this tutorial.

 

Last Updated on Sunday, 08 June 2008 01:14  

Little Bird

twitter Bird more info...!