---
title: "在NS2（ns-allione-2.34）中加入bing协议"
author: "chrisongs"
date: "2010-11-29T19:42:46Z"
tags: ["Access", "Bing", "Tcl", "工作", "网络协议"]
categories: ["编程语言"]
slug: "ns2-ns-allione-2-34-bing"
draft: false
lastmod: "2010-12-01T19:32:24Z"
id: 828555
url: "https://chrisongs.iteye.com/blog/828555"
---

今天开始沉下心来好好搞项目，我们小组的任务是完成利用网络编码技术改进传统TCP协议，我主要是负责进行ns仿真。今天上午回顾了一下以前进行的工作，下午试验在ns2中植入了一个小协议Bing（其实就是Ping协议，由于在现有ns源代码中已经有了Ping协议，故改名为Bing协议）。以下是植入的过程： First Step: 编写bing.h文件 \[code="java"]#ifndef ns\_bing\_h #define ns\_bing\_h #include "agent.h" #include "tclcl.h" #include "packet.h" #include "address.h" #include "ip.h" /* * 定义Bing协议的分组头结构 \*/ struct hdr\_bing { char ret; // 用来判断是否发送pktret响应包 double send\_time; // 包传送时间 int seq; // sequence number // Header access methods static int offset\_; // required by PacketHeaderManager // 分组头在整个包头中的偏移量。 疑问：何时确定的这个偏移量的值？？？ inline static int& offset() { return offset\_; } inline static hdr\_bing* access(const Packet* p) { return (hdr\_bing\*) p-&gt;access(offset\_); } }; class BingAgent : public Agent { public: BingAgent(); int seq; // a send sequence number like in real ping virtual int command(int argc, const char\*const* argv); // 重写父类的command函数，在这里实际就是传送包的函数 virtual void recv(Packet\*, Handler\*); }; #endif // ns\_ping\_h\[/code] Second Step: 编写bing.cc文件 \[code="java"]#include "bing.h" #include ; /* * bing协议需要被NS-2所接受并且，可以在OTcl代码中使用，需要定义如下代码： * 以下代码使得OTcl中的bingHead/Ping类与C++中的hdr\_bing绑定在一起,这样定义出的分组头为Bing \*/ int hdr\_bing::offset\_; static class BingHeaderClass : public PacketHeaderClass { public: BingHeaderClass() : PacketHeaderClass("PacketHeader/Bing", sizeof(hdr\_bing)) { bind\_offset(&hdr\_bing::offset\_); } } class\_binghdr; /* * 以下代码从TclCL继承一个BingClass类，其构造函数通过调用基类的构造函数TclClass("Agent/Bing")指定其 * 对应的OTcl对象为Agent/Bing,这实际是做了登记工作。 \*/ static class BingClass : public TclClass { public: BingClass() : TclClass("Agent/Bing") {} TclObject* create(int, const char\*const\*) { return (new BingAgent()); } } class\_bing; BingAgent::BingAgent() : Agent(PT\_BING), seq(0) { bind("packetSize\_", &size\_); } int BingAgent::command(int argc, const char\*const* argv) { if (argc == 2) { if (strcmp(argv\[1], "send") == 0) { // Create a new packet Packet* pkt = allocpkt(); // Access the Ping header for the new packet: hdr\_bing* hdr = hdr\_bing::access(pkt); // Set the 'ret' field to 0, so the receiving node // knows that it has to generate an echo packet hdr-&gt;ret = 0; hdr-&gt;seq = seq++; // Store the current time in the 'send\_time' field hdr-&gt;send\_time = Scheduler::instance().clock(); std::cout &lt;&lt; Agent::size_ &lt;&lt; endl; // Send the packet send(pkt, 0); // return TCL\_OK, so the calling function knows that // the command has been processed return (TCL\_OK); } } // If the command hasn't been processed by PingAgent()::command, // call the command() function for the base class return (Agent::command(argc, argv)); } void BingAgent::recv(Packet* pkt, Handler\*) { // Access the IP header for the received packet: hdr\_ip* hdrip = hdr\_ip::access(pkt); // Access the Ping header for the received packet: hdr\_bing* hdr = hdr\_bing::access(pkt); // Is the 'ret' field = 0 (i.e. the receiving node is being pinged)? if (hdr-&gt;ret == 0) { // Send an 'echo'. First save the old packet's send\_time double stime = hdr-&gt;send\_time; int rcv\_seq = hdr-&gt;seq; // Discard the packet Packet::free(pkt); // Create a new packet Packet* pktret = allocpkt(); // Access the Ping header for the new packet: hdr\_bing* hdrret = hdr\_bing::access(pktret); // Set the 'ret' field to 1, so the receiver won't send // another echo hdrret-&gt;ret = 1; // Set the send\_time field to the correct value hdrret-&gt;send\_time = stime; // Added by Andrei Gurtov for one-way delay measurement. hdrret-&gt;seq = rcv\_seq; // Send the packet send(pktret, 0); } else { // A packet was received. Use tcl.eval to call the Tcl // interpreter with the ping results. // Note: In the Tcl code, a procedure // 'Agent/Ping recv {from rtt}' has to be defined which // allows the user to react to the ping result. char out\[100]; // Prepare the output to the Tcl interpreter. Calculate the // round trip time sprintf(out, "%s recv %d %3.1f", name(), hdrip-&gt;src\_.addr_ &gt;&gt; Address::instance().NodeShift\_\[1], (Scheduler::instance().clock()-hdr-&gt;send\_time) * 1000); Tcl& tcl = Tcl::instance(); tcl.eval(out); // Discard the packet Packet::free(pkt); } } \[/code] Third Step: (1) 在lib/ns-packet.tcl中添加包头名字Bing; (2) 在common/packet.h中新增包类型:PT\_BING; 并定义新增包类型的名字name\_\[PT\_BING]= "bing"; (3)在lib/ns-default.tcl中增加定义变量初始值的语句。这里的变量只包括通过bind函数进行OTcl与C++语言绑定的变量： Agent/Bing set packetSize_ 64; Fourth Step: 最后，修改ns/Makefile文件,并编译。 Bing协议植入过程就此结束！