ICMP Packet Format
Download
Report
Transcript ICMP Packet Format
SWE 344
Internet Protocols & Client Server
Programming
ICMP, SMTP & HTTP
ICMP
The Internet Control Message Protocol (ICMP) is used to communicate
with remote hosts on the network. Many popular network utilities, such
as ping and traceroute, are based on ICMP.
ICMP was defined in RFC 792 to allow network devices to report errors
in datagram processing. ICMP is a robust means of communicating
errors and network information among hosts.
ICMP uses IP to communicate across the network. The entire ICMP
packet is then contained within the data section of the IP packet.
Figure shows how the ICMP packet fields are placed in an IP packet.
2
ICMP Packet Format
ICMP uses a specific packet format to identify information in the packet.
the ICMP packet contains the following fields:
Type The 1-byte Type element defines what kind of ICMP message is in
the packet. Many types of ICMP packets are used to send control
request messages to remote hosts. Each message type has its own
format and data requirements.
Code The 1-byte Code element further defines the Type field. The
various ICMP message types require specific control and data options.
These options are defined in the Code field.
Checksum The 2-byte Checksum element ensures that the ICMP
packet has arrived without corruption or tampering. The checksum is
computed on only the ICMP portion of the packet, using a specific
algorithm defined in RFC 792. When computing the checksum value,
the Checksum field is set to zero.
Message The multibyte Message element contains various other data
elements that are unique to each ICMP message type. The Message
data fields are often used to contain information sent to and from the
remote host. Many of the ICMP message types define the first two fields
in the Message element as an Identifier and a Sequence number. Both
of these fields are used to uniquely identify the ICMP packet to the
3
hosts.
ICMP Packet Types
The ICMP Packet Types
Type Code
Description
0
Echo reply
3
Destination unreachable
4
Source quench
5
Redirect
8
Echo request
11
Time exceeded
12
Parameter problem
13
Timestamp request
14
Timestamp reply
15
Information request
16
Information reply
4
Echo Request and Echo Reply Packets
Two of the ICMP packets used most often are the Echo Request and
Echo Reply. These packets allow a device to request an ICMP response
from a remote device on the network—the core of the ping utility that
has become a universal staple for network administration.
The Echo Request packet uses ICMP Type 8, with a Code value of 0. The
Message data area contains three elements:
•A 1-byte Identifier that uniquely identifies the Echo Request packet
•A 1-byte Sequence number providing additional identification for the
ICMP packet in a stream of A multibyte data element containing data
that should be returned by the receiving host
When a device receives an Echo Request packet, it must respond with
an Echo Reply packet, ICMP Type 0. The Echo Reply packet must
contain the same Identifier and Sequence number values as the Echo
Request packet to which it is responding.
Also, the data element value must be the same as received in the Echo
Request packet
5
Destination Unreachable Packet
The Destination Unreachable ICMP packet (Type 3) is usually returned
by a router device after it receives an IP packet that it cannot forward
to the appropriate destination.
The data portion of the Destination Unreachable packet contains the IP
header plus the first 64 bits of the datagram.
In this packet, the Code field identifies the reason the packet could not
be forwarded by the router
Time Exceeded Packet
The Time Exceeded (ICMP Type 11) packet has become an important
tool that is used for network troubleshooting. It reports that an IP
packet has exceeded the time to live (TTL) value defined in the IP
header.
Each time an IP packet traverses a network router, the TTL value is
decreased by 1. If the TTL value reaches 0 before the IP packet reaches
the intended destination, the last receiving router must send a Time
Exceeded ICMP packet to the sending host. As you will see, this
procedure is exploited in the traceroute program.
6
Creating an ICMP Class in C#
The raw socket does not automatically format your ICMP packet, so you
must do this yourself. C# is an object-oriented language. It makes
sense to create a C# ICMP class that you can use to format an ICMP
packet and manipulate the packet contents as necessary.
ICMP Class Constructors
The ICMP class should define a data variable for each element in the
ICMP packet.
The ICMP Class Generic Data Elements
Data Variable
Size
Type
Type
1 byte
Byte
Code
1 byte
Byte
Checksum
2 bytes
Unsigned 16-bit integer
Message
multibyte
Byte array
Here is the format for the ICMP class default constructor:
class ICMP {
public byte Type;
public byte Code;
public UInt16 Checksum;
public int MessageSize;
public byte[] Message = new byte[1024];
public ICMP() { }
}
7
SMTP
The System.Web.Mail namespace contains classes that are used for
creating and sending e-mail messages to remote hosts. This is done
using either the default Windows Simple Mail Transfer Protocol (SMTP)
service on Windows 2000 and XP machines, or an external mail server.
The SmtpMail Class
The SmtpMail class, found in the System.Web.Mail namespace, allows
you to send SMTP messages in your C# network programs.
The Send() method is overloaded, using two separate formats:
Send(MailMessage message)
Send(string from, string to, string subject, string body)
The first format allows you to send a MailMessage object. The
MailMessage class is a self-contained e-mail message, created by
populating the properties of the class with information related to the
message, and the destination address(es).
8
The second format allows you to send a raw message, manually
specifying the typical e-mail message header fields:
From specifies the e-mail address of the sender.
To specifies the e-mail address of one or more recipients, separated by
commas.
Subject specifies the topic of the e-mail message.
The final parameter of the Send() method is the actual body of the
message. The body can be in either a plain text or HTML format.
The sole property of the SmtpMail class is SmtpServer.
If you are using a relay mail server, you must set the SmtpServer
property before you attempt to send messages:
SmtpMail.SmtpServer = "mailsrvr.myisp.net";
9
This program demonstrates how to create a simple mail message and
send it through a remote mail relay to the recipient.
using System;
using System.Net;
using System.Web.Mail;
class MailTest
{ public static void Main()
{ string from = “[email protected]";
string to = “[email protected]";
string subject = "This is a test mail message";
string body ="Hi there, I hope things are going well today.";
SmtpMail.SmtpServer = "192.168.1.150";
SmtpMail.Send(from, to, subject, body);
}
}
10
HTTP
(Hyper text transfer protocol)
The .NET WebClient class—the easiest way to communicate via HTTP
from your C# programs. The WebRequest and WebResponse classes
have many features available to help you create full-featured web
programs.
The WebClient Class
The easiest way to communicate with websites from a C# network
program is to use the WebClient class, found in the System.Net
namespace. This class provides methods to send requests and receive
responses from a web server within your program.
Downloading Web Data
The WebClient class provides three methods that can download
information from a web server:
DownloadData() Downloads data to a byte array from a specified URI
DownloadFile() Downloads data to a local file from a specified URI
OpenRead() Opens a read-only stream to download data from a
specified URI.
11
Uploading Web Data
The WebClient class provides four ways to upload information to the
web server:
OpenWrite() Sends a stream of data to the web server
UploadData() Sends a byte array of data to the web server
UploadFile() Sends a local file to the web server
UploadValues() Sends a NameValueCollection of data names and
values to the web server
The NetworkCredential Class
The NetworkCredential class (found in the System.Net namespace)
authenticates a client to the web server using a simple
username/password combination (and a specified domain name for
Windows web servers).
The NetworkCredential object is created using one of three constructor
formats:
NetworkCredential()
NetworkCredential(string username, string password)
NetworkCredential(string username, string password, string domain)
12
This program to reads the URL content from webserver.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace Lab3c
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//to read URL Content from webserver
try
{
StreamReader inStream;
WebRequest webRequest;
WebResponse webresponse;
webRequest = WebRequest.Create(textBox1.Text);
webresponse = webRequest.GetResponse();
inStream = new StreamReader(webresponse.GetResponseStream());
textBox2.Text = inStream.ReadToEnd();
}
catch { MessageBox.Show("URL error"); }
}
}
}
13
END
14