-
Notifications
You must be signed in to change notification settings - Fork 0
/
icmp.c
92 lines (81 loc) · 2.42 KB
/
icmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*! \file icmp.c \brief ICMP Protocol Library. */
//*****************************************************************************
//
// File Name : 'icmp.c'
// Title : ICMP (Internet Control Message Protocol) Protocol Library
// Version : 1.0
// Target MCU : Atmel AVR series
//
//*****************************************************************************
#include "global.h"
#include "net.h"
#include "nic.h"
#include "arp.h"
#include "icmp.h"
//#include "rprintf.h"
#include "debug.h"
//extern void nicSend(unsigned int len, unsigned char* packet);
// global variables
// functions
void icmpInit(void)
{
}
void icmpIpIn(icmpip_hdr* packet)
{
// check ICMP type
switch(packet->icmp.type)
{
case ICMP_TYPE_ECHOREQUEST:
// echo request
icmpEchoRequest(packet);
break;
default:
break;
}
}
void icmpEchoRequest(icmpip_hdr* packet)
{
uint32_t tempIp;
// change type to reply
packet->icmp.type = ICMP_TYPE_ECHOREPLY;
// recalculate checksum
packet->icmp.icmpchksum = 0;
packet->icmp.icmpchksum = netChecksum((u08*)&packet->icmp, htons(packet->ip.len)-IP_HEADER_LEN);
// printf("CheckSum: %d\r\n",packet->icmp.icmpchksum);
// return to sender
tempIp = packet->ip.destipaddr;
packet->ip.destipaddr = packet->ip.srcipaddr;
packet->ip.srcipaddr = tempIp;
// add ethernet routing
arpIpOut((struct netEthIpHeader*)(((u08*)packet)-ETH_HEADER_LEN), 0);
// debugging
// #if NET_DEBUG >= 2
// icmpPrintHeader(packet);
//debugPrintHexTable(htons(packet->ip.len), (u08*)packet);
// #endif
// send it (packet->ip.len+ETH_HEADER_LEN
nicSend(htons(packet->ip.len)+ETH_HEADER_LEN, (((u08*)packet)-ETH_HEADER_LEN));
}
/*
#ifdef ICMP_DEBUG_PRINT
void icmpPrintHeader(icmpip_hdr* packet)
{
rprintfProgStrM("ICMP Packet:\r\n");
// print source IP address
rprintfProgStrM("SrcIpAddr: "); netPrintIPAddr(htonl(packet->ip.srcipaddr)); rprintfCRLF();
// print dest IP address
rprintfProgStrM("DstIpAddr: "); netPrintIPAddr(htonl(packet->ip.destipaddr)); rprintfCRLF();
// print type
rprintfProgStrM("Type : ");
switch(packet->icmp.type)
{
case ICMP_TYPE_ECHOREQUEST: rprintfProgStrM("ECHO REQUEST"); break;
case ICMP_TYPE_ECHOREPLY: rprintfProgStrM("ECHO REPLY"); break;
default: rprintfProgStrM("UNKNOWN"); break;
}
rprintfCRLF();
// print code
rprintfProgStrM("Code : 0x"); rprintfu08(packet->icmp.icode); rprintfCRLF();
}
#endif
*/