?? icmp.c
字號(hào):
//-----------------------------------------------------------------------------
// Net ICMP.C
//
// This module handles ICMP messages
// Refer to RFC 792, 896, 950, 1122, and 1191
//-----------------------------------------------------------------------------
#include <string.h>
#include <stdlib.h>
#include "net.h"
#include "cksum.h"
#include "ip.h"
#include "icmp.h"
#include "general.h"
#include "timer.h"
#include "serial.h"
//------------------------------------------------------------------------
// This builds a ping response message. It allocates memory for the
// entire outgoing message, including Eth and IP headers. See "TCP/IP
// Illustrated, Volume 1" Sect 7.2 for info on Ping messages
//------------------------------------------------------------------------
void ping_send(UCHAR * inbuf, ULONG ipaddr, UINT len)
{
PING_HEADER * ping_in;
PING_HEADER * ping_out;
UCHAR * outbuf;
ping_in = (PING_HEADER *)(inbuf + 34);
// Allocate memory for entire outgoing message
outbuf = (UCHAR *)malloc(len + 34);
if (outbuf == NULL)
{
#ifdef DEBUF
PrintStr("PING: Oops, out of memory\n");
#endif
return;
}
// Ping response message payload starts at offset 34
ping_out = (PING_HEADER *)(outbuf + 34);
ping_out->msg_type = 0;
ping_out->msg_code = 0;
ping_out->checksum = 0;
ping_out->identifier = ping_in->identifier;
ping_out->sequence = ping_in->sequence;
memcpy(&ping_out->echo_data, &ping_in->echo_data, len - 8);
// Compute checksum over the ICMP header plus
// optional data and insert complement
ping_out->checksum = ~cksum(outbuf + 34, len);
#ifdef DEBUG
PrintStr("ICMP: Sending response to IP layer\n");
#endif
ip_send(outbuf, ipaddr, ICMP_TYPE, len);
}
//------------------------------------------------------------------------
// This builds an outgoing ICMP destination port unreachable response
// message. See See "TCP/IP Illustrated, Volume 1" Sect 6.5. This
// message is typically sent in response to a UDP message directed
// to a port that has no corresponding application running.
// Todo: The spec says we should return all options that were in
// the original incoming IP header. Right now we cut off everything
// after the first 20 bytes.
//------------------------------------------------------------------------
void dest_unreach_send(UCHAR * inbuf, ULONG ipaddr)
{
UCHAR * outbuf;
ICMP_ERR_HEADER * icmp;
// Allocate memory for entire outgoing message
// including eth and IP haders. Always 70 bytes
outbuf = (UCHAR *)malloc(70);
if (outbuf == NULL)
{
#ifdef DEBUG
PrintStr("ICMP: Oops, out of memory\n");
#endif
return;
}
icmp = (ICMP_ERR_HEADER *)(outbuf + 34);
// Fill in ICMP error message header
icmp->msg_type = 3; // destination unreachable
icmp->msg_code = 3; // port unreachable
icmp->checksum = 0;
// Fill in ICMP error message data
icmp->msg_data = 0;
// Copy in 20 byte original incoming IP header
// plus 8 bytes of data
memcpy(&icmp->echo_data, inbuf + 14, 28);
// Compute checksum over the 36 byte long ICMP
// header plus data and insert complement
icmp->checksum = ~cksum(outbuf + 34, 36);
// Forward message to the IP layer
#ifdef DEBUG
PrintStr("ICMP: Sending dest unreach to IP layer\n");
#endif
ip_send(outbuf, ipaddr, ICMP_TYPE, 36);
}
//------------------------------------------------------------------------
// This handles incoming ICMP messages. See "TCP/IP Illustrated,
// Volume 1" Sect 6.2 for discussion of the various ICMP messages
//------------------------------------------------------------------------
void icmp_rcve(UCHAR * inbuf, UINT len)
{
IP_HEADER * ip;
UCHAR msg_type;
// Allow for 14 bytes eth header
ip = (IP_HEADER *)(inbuf + 14);
// IP header has been adjusted if necessary to always be
// 20 bytes so message starts at an offset of 34
// Validate checksum of entire ICMP message incl data
if (cksum(inbuf + 34, len) != 0xFFFF)
{
#ifdef DEBUG
PrintStr("ICMP: Error, cksum bad\n");
#endif
return;
}
// Switch on the message type
msg_type = *(inbuf + 34);
switch(msg_type)
{
case 3:
#ifdef DEBUG
PrintStr("ICMP: Dest unreachable rcvd\n");
#endif
break;
case 8:
#ifdef DEBUG
PrintStr("ICMP: Ping rcvd\n");
#endif
ping_send(inbuf, ip->source_ipaddr, len);
break;
default:
#ifdef DEBUG
PrintStr("ICMP: Error, unknown msg rcvd\n");
#endif
break;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -