File: icmp.cc

package info (click to toggle)
ns2 2.35%2Bdfsg-2.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 78,780 kB
  • ctags: 27,490
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 816; awk: 525; csh: 355
file content (159 lines) | stat: -rw-r--r-- 5,222 bytes parent folder | download | duplicates (8)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*    
 * Copyright (c) 1998 Regents of the University of California.
 * All rights reserved.
 *    
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the Network Research
 *      Group at Lawrence Berkeley National Laboratory.
 * 4. Neither the name of the University nor of the Laboratory may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *   
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 * SUCH DAMAGE.
 */  

#ifndef lint
static const char rcsid[] =
    "@(#) $Header: /cvsroot/nsnam/ns-2/emulate/icmp.cc,v 1.5 2000/09/01 03:04:10 haoboy Exp $";
#endif

#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>

#include "agent.h"
#include "scheduler.h"
#include "packet.h"
#include "emulate/net.h"
#include "emulate/internet.h"

#ifndef IPPROTO_GGP
#define IPPROTO_GGP 3
#endif // IPPROTO_GGP

//
// icmp.cc -- a very limited-functionality set of icmp routines
// 

class IcmpAgent : public Agent {
public:
	IcmpAgent();
	void recv(Packet*, Handler*) { abort(); }
protected:
	void	sendredirect(in_addr& me, in_addr& target, in_addr& dest, in_addr& gw);
	int	command(int argc, const char*const* argv);

	int	ttl_;
};

static class IcmpAgentClass : public TclClass { 
public:
        IcmpAgentClass() : TclClass("Agent/IcmpAgent") {}
        TclObject* create(int , const char*const*) {
                return (new IcmpAgent());
        } 
} class_icmpsagent;

IcmpAgent::IcmpAgent() : Agent(PT_LIVE)
{
	bind("ttl_", &ttl_);
}

/*
 * sendredirect -- send a packet to "target" containing a redirect
 * for the network specified by "dst", so that the gateway "gw" is used
 * also, forge the source address so as to appear to come from "me"
 */

void
IcmpAgent::sendredirect(in_addr& me, in_addr& target, in_addr& dst, in_addr& gw)
{
	// make a simulator packet to hold the IP packet, which in turn
	// holds: ip header, icmp header, embedded ip header, plus 64 bits
	// data
	int iplen = sizeof(ip) + 8 + sizeof(ip) + 8;
        Packet* p = allocpkt(iplen);
	hdr_cmn* hc = HDR_CMN(p);
	ip* iph = (ip*) p->accessdata();
	hc->size() = iplen;

	// make an IP packet ready to send to target
	// size will be min icmp + a dummy'd-up IP header
	Internet::makeip(iph, iplen, ttl_, IPPROTO_ICMP, me, target);

	// make an ICMP host redirect, set the gwaddr field
	icmp* icp = (icmp*) (iph + 1);
	icp->icmp_gwaddr = gw;

	// make a dummy IP packet to go in the ICMP data, which will
	// be used to indicate to the end host which routing table
	// entry to update

	ip* dummyhdr = (ip*)((u_char*)icp + 8);	// past icmp hdr
		// deprecated protocol inside
	Internet::makeip(dummyhdr, 20, 254, IPPROTO_GGP, target, dst);
	u_short *port = (u_short*) (dummyhdr + 1);	// past ip hdr
	*port++ = htons(9);	// discard port
	*port = htons(9);	// discard port
	icp->icmp_cksum = 0;
	icp->icmp_type = ICMP_REDIRECT;
	icp->icmp_code = ICMP_REDIRECT_HOST;
	icp->icmp_cksum = Internet::in_cksum((u_short*)icp,
		8 + sizeof(ip) + 8);

	send(p, 0);
	return;
}

int
IcmpAgent::command(int argc, const char*const* argv)
{
	if (argc > 5) {
		// $obj send name src dst [...stuff...]
		if (strcmp(argv[1], "send") == 0) {
			if (strcmp(argv[2], "redirect") == 0 &&
			    argc == 7) {
				// $obj send redirect src target dst gwaddr
				// as src, send to targ, so that it changes
				// its route to dst to use gwaddr
				u_long s, t, d, g;
				s = inet_addr(argv[3]);
				t = inet_addr(argv[4]);
				d = inet_addr(argv[5]);
				g = inet_addr(argv[6]);
				in_addr src, targ, dst, gw;
				src.s_addr = s;
				targ.s_addr = t;
				dst.s_addr = d;
				gw.s_addr = g;
				sendredirect(src, targ, dst, gw);
				return (TCL_OK);
			}
		}
	}
	return (Agent::command(argc, argv));
}