NS3 network Emulation (a): ICMPV4 protocol

Source: Internet
Author: User

Happy Shrimp

http://blog.csdn.net/lights_joy/

Welcome reprint, but please keep the author information


ICMPthe full name isInternet controlmessage Protocol. The goal is to enable us to detect the connectivity of the network. ICMPmainly through the different categories(Type)with the Code(Code)allow the machine to identify different connection conditions. This section usesNS3learn about this agreement.


1.1 Message Format


ICMP the message format is as follows:


that ICMP message is IP the data of the message, and IPv4 The format of the message is as follows:


catch one on the internet Ping package to see:



this is a192.168.24.1to the192.168.24.129of thePingbag, look again.192.168.24.129The response:


It is easy to correspond to the previous message format.


1.2 Request packet to generate ICMP with NS3


Try it next NS3 generate the above ICMP the request package.


Follow NS3 data Packet generation rules, first you need to prepare ICMP The data part of the request, that is, in the message ABCDE ... :


Populated data content uint8_t buffer[2048] = {0};for (int i = 0; i < M_ncurpacketlen; i++) {Buffer[i] = ' a ' + (i% 23);} ptr<packet> data = ns3::create<packet> (buffer, m_ncurpacketlen); then populate the header of the ICMP echo://Generate a list of packets to be sent ptr< packet> PKT = ns3::create<packet> ();//Add ICMP echo header Icmpv4echo Eh;eh. SetData (data); eh. Setsequencenumber (0x0019/*m_ncurpacketseq*/); eh. Setidentifier (1);p kt->addheader (EH); the seq here is a mutable integer, except that we write a fixed value in accordance with the above packet. Next populate the ICMP header://to add the ICMP header Icmpv4header Ih;ih. Setcode (0); ih. SetType (Icmpv4header::echo); ih. Enablechecksum ();p kt->addheader (IH); the only thing to note here is that enablechecksum must be called before AddHeader, otherwise the checksum will not be generated. Plus IP Header://Add IP headers ipv4header iph;iph. Setdestination ((const char*) dest_ip); iph. SetSource ((const char*) src_ip); iph. Setidentification (0X49FB); iph. Setttl (IPH); Setprotocol (icmpv4l4protocol::P rot_number); iph. Setpayloadsize (Pkt->getsize ()); iph. Enablechecksum ();p kt->addheader (IPH); plus Ethernet Header://Add Ethernet head Ethernetheader Eeh;eeh. Setdestination ((const char*) dest_mac); Eeh. SetsoUrce ((const char*) src_mac); Eeh. Setlengthtype (ns3::ipv4l3protocol::P rot_number);p Kt->addheader (eeh); int len = pkt->copydata (buffer, 2048);

Done! Look at the contents of the packet we generated:



It's the same as the bag you grabbed from the Internet.



1.3 analyzing ICMP Request packets with NS3


The process of analyzing the package and constructing the package are the opposite, from the outermost Ethernet packet to the data:


/* Callback function invoked by LIBPCAP for every incoming packet */void Ccommonicmpsenddlg::p acket_handler (void *_param, const void *_header, const void *_pkt_data) {uint8_t buffer[2048], *p;p = (uint8_t *) _pkt_data + 12;if (p[0]! = 8 | | p[1]! = 0) return;const struct PCAP_PKTHDR *header = (const struct PCAP_PKTHDR *) _header; ptr<packet> PKT = ns3::create<packet> ((uint8_t*) _pkt_data, Header->len);//IP packet ethernetheader Eh;I Pv4header iph;pkt->removeheader (EH);p kt->removeheader (IPH), if (iph. Getprotocol ()! = Icmpv4l4protocol::P rot_number) Return;icmpv4header Ih;icmpv4echo Ieh; sicmppacket* Ppkt;pkt->removeheader (IH); GetType () = = icmpv4header::echo_reply) {pkt->removeheader (Ieh), if (Ieh. Getidentifier ()! = dlg->m_nicmpid) return;int seq = Ieh. Getsequencenumber (); ...... return;}}

1.4 WinPcapissue of the contract


in the Send ICMP package, you use the Pcap_sendpacket function, but the package delay of this function is large, from the function call to catch the packet from the network card can have a delay of hundreds of milliseconds.



Write it down for a follow-up reference.












??

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

NS3 network Emulation (a): ICMPV4 protocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.