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 //extern void nicSend(unsigned int len, unsigned char* packet);
    00025
    00026 // global variables
    00027
    00028
    00029 // functions
    00030 void icmpInit(void)
    00031 {
    00032 }
    00033
    00034 void icmpIpIn(icmpip_hdr* packet)
    00035 {
    00036     // check ICMP type
    00037     switch(packet->icmp.type)
    00038     {
    00039     case ICMP_TYPE_ECHOREQUEST:
    00040         // echo request
    00041         icmpEchoRequest(packet);
    00042         break;
    00043     default:
    00044         break;
    00045     }
    00046 }
    00047
    00048 void icmpEchoRequest(icmpip_hdr* packet)
    00049 {
    00050     uint32_t tempIp;
    00051
    00052     // change type to reply
    00053     packet->icmp.type = ICMP_TYPE_ECHOREPLY;
    00054     // recalculate checksum
    00055     packet->icmp.icmpchksum = 0;
    00056     packet->icmp.icmpchksum = netChecksum((u08*)&packet->icmp, htons(packet->ip.len)-IP_HEADER_LEN);
    00057     // return to sender
    00058     tempIp = packet->ip.destipaddr;
    00059     packet->ip.destipaddr = packet->ip.srcipaddr;
    00060     packet->ip.srcipaddr = tempIp;
    00061     // add ethernet routing
    00062     arpIpOut((struct netEthIpHeader*)(((u08*)packet)-ETH_HEADER_LEN), 0);
    00063
    00064     // debugging
    00065     #if NET_DEBUG >= 2
    00066         icmpPrintHeader(packet);
    00067         //debugPrintHexTable(htons(packet->ip.len), (u08*)packet);
    00068     #endif
    00069
    00070     // send it (packet->ip.len+ETH_HEADER_LEN
    00071     nicSend(htons(packet->ip.len)+ETH_HEADER_LEN, (((u08*)packet)-ETH_HEADER_LEN));
    00072 }
    00073
    00074 #ifdef ICMP_DEBUG_PRINT
    00075 void icmpPrintHeader(icmpip_hdr* packet)
    00076 {
    00077     rprintfProgStrM("ICMP Packet:\r\n");
    00078     // print source IP address
    00079     rprintfProgStrM("SrcIpAddr: "); netPrintIPAddr(htonl(packet->ip.srcipaddr));    rprintfCRLF();
    00080     // print dest IP address
    00081     rprintfProgStrM("DstIpAddr: "); netPrintIPAddr(htonl(packet->ip.destipaddr));   rprintfCRLF();
    00082     // print type
    00083     rprintfProgStrM("Type   : ");
    00084     switch(packet->icmp.type)
    00085     {
    00086     case ICMP_TYPE_ECHOREQUEST:     rprintfProgStrM("ECHO REQUEST"); break;
    00087     case ICMP_TYPE_ECHOREPLY:       rprintfProgStrM("ECHO REPLY"); break;
    00088     default:                        rprintfProgStrM("UNKNOWN"); break;
    00089     }
    00090     rprintfCRLF();
    00091     // print code
    00092     rprintfProgStrM("Code   : 0x"); rprintfu08(packet->icmp.icode);         rprintfCRLF();
    00093 }
    00094 #endif