Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

# icmp.c

Go to the documentation of this file.

    00001 /*! \file icmp.c \brief ICMP Protocol Library. */
    00002 //*****************************************************************************
    00003 //
    00004 // File Name    : 'icmp.c'
    00005 // Title        : ICMP (Internet Control Message Protocol) Protocol Library
    00006 // Author       : Pascal Stang
    00007 // Created      : 9/10/2004
    00008 // Revised      : 7/3/2005
    00009 // Version      : 0.1
    00010 // Target MCU   : Atmel AVR series
    00011 // Editor Tabs  : 4
    00012 //
    00013 //*****************************************************************************
    00014
    00015 #include "global.h"
    00016 #include "net.h"
    00017 #include "nic.h"
    00018 #include "arp.h"
    00019 #include "icmp.h"
    00020
    00021 #include "rprintf.h"
    00022 #include "debug.h"
    00023
    00024 // global variables
    00025
    00026
    00027 // functions
    00028 void icmpInit(void)
    00029 {
    00030 }
    00031
    00032 void icmpIpIn(icmpip_hdr* packet)
    00033 {
    00034     // check ICMP type
    00035     switch(packet->icmp.type)
    00036     {
    00037     case ICMP_TYPE_ECHOREQUEST:
    00038         // echo request
    00039         icmpEchoRequest(packet);
    00040         break;
    00041     default:
    00042         break;
    00043     }
    00044 }
    00045
    00046 void icmpEchoRequest(icmpip_hdr* packet)
    00047 {
    00048     uint32_t tempIp;
    00049
    00050     #if NET_DEBUG >= 3
    00051         icmpPrintHeader(packet);
    00052         //debugPrintHexTable(htons(packet->ip.len), (u08*)packet);
    00053     #endif
    00054
    00055     // change type to reply
    00056     packet->icmp.type = ICMP_TYPE_ECHOREPLY;
    00057     // recalculate checksum
    00058     packet->icmp.icmpchksum = 0;
    00059     packet->icmp.icmpchksum = netChecksum((u08*)&packet->icmp, htons(packet->ip.len)-IP_HEADER_LEN);
    00060     // return to sender
    00061     tempIp = packet->ip.destipaddr;
    00062     packet->ip.destipaddr = packet->ip.srcipaddr;
    00063     packet->ip.srcipaddr = tempIp;
    00064     // add ethernet routing
    00065     arpIpOut((struct netEthIpHeader*)(((u08*)packet)-ETH_HEADER_LEN), 0);
    00066
    00067     // debugging
    00068     #if NET_DEBUG >= 2
    00069         icmpPrintHeader(packet);
    00070         //debugPrintHexTable(htons(packet->ip.len), (u08*)packet);
    00071     #endif
    00072
    00073     // send it (packet->ip.len+ETH_HEADER_LEN
    00074     nicSend(htons(packet->ip.len)+ETH_HEADER_LEN, (((u08*)packet)-ETH_HEADER_LEN));
    00075 }
    00076
    00077 #ifdef ICMP_DEBUG_PRINT
    00078 void icmpPrintHeader(icmpip_hdr* packet)
    00079 {
    00080     rprintfProgStrM("ICMP Packet:\r\n");
    00081     // print source IP address
    00082     rprintfProgStrM("SrcIpAddr: "); netPrintIPAddr(htonl(packet->ip.srcipaddr));    rprintfCRLF();
    00083     // print dest IP address
    00084     rprintfProgStrM("DstIpAddr: "); netPrintIPAddr(htonl(packet->ip.destipaddr));   rprintfCRLF();
    00085     // print type
    00086     rprintfProgStrM("Type   : ");
    00087     switch(packet->icmp.type)
    00088     {
    00089     case ICMP_TYPE_ECHOREQUEST:     rprintfProgStrM("ECHO REQUEST"); break;
    00090     case ICMP_TYPE_ECHOREPLY:       rprintfProgStrM("ECHO REPLY"); break;
    00091     default:                        rprintfProgStrM("UNKNOWN"); break;
    00092     }
    00093     rprintfCRLF();
    00094     // print code
    00095     rprintfProgStrM("Code   : 0x"); rprintfu08(packet->icmp.icode);         rprintfCRLF();
    00096 }
    00097 #endif