forked from eastzone/cs344-sw
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sr_cpu_extension_nf2.c
257 lines (214 loc) · 7.39 KB
/
sr_cpu_extension_nf2.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*-----------------------------------------------------------------------------
* file: sr_cpu_extension_nf2.c
* date: Mon Feb 09 16:58:30 PST 2004
* Author: Martin Casado
*
* 2007-Apr-04 04:57:55 AM - Modified to support NetFPGA v2.1 /mc
*
* Description:
*
*---------------------------------------------------------------------------*/
#include "sr_cpu_extension_nf2.h"
#include "sr_base_internal.h"
#include "sr_vns.h"
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
struct sr_ethernet_hdr
{
#ifndef ETHER_ADDR_LEN
#define ETHER_ADDR_LEN 6
#endif
uint8_t ether_dhost[ETHER_ADDR_LEN]; /* destination ethernet address */
uint8_t ether_shost[ETHER_ADDR_LEN]; /* source ethernet address */
uint16_t ether_type; /* packet type ID */
} __attribute__ ((packed)) ;
static char* copy_next_field(FILE* fp, char* line, char* buf);
static uint32_t asci_to_nboip(const char* ip);
static void asci_to_ether(const char* addr, uint8_t mac[6]);
/*-----------------------------------------------------------------------------
* Method: sr_cpu_init_hardware(..)
* scope: global
*
* Read information for each of the router's interfaces from hwfile
*
* format of the file is (1 interface per line)
*
* <name ip mask hwaddr>
*
* e.g.
*
* eth0 192.168.123.10 255.255.255.0 ca:fe:de:ad:be:ef
*
*---------------------------------------------------------------------------*/
int sr_cpu_init_hardware(struct sr_instance* sr, const char* hwfile)
{
struct sr_vns_if vns_if;
FILE* fp = 0;
char line[1024];
char buf[SR_NAMELEN];
char *tmpptr;
if ( (fp = fopen(hwfile, "r") ) == 0 )
{
fprintf(stderr, "Error: could not open cpu hardware info file: %s\n",
hwfile);
return -1;
}
Debug(" < -- Reading hw info from file %s -- >\n", hwfile);
while ( fgets( line, 1024, fp) )
{
line[1023] = 0; /* -- insurance :) -- */
/* -- read interface name into buf -- */
if(! (tmpptr = copy_next_field(fp, line, buf)) )
{
fclose(fp);
fprintf(stderr, "Bad formatting in cpu hardware file\n");
return 1;
}
Debug(" - Name [%s] ", buf);
strncpy(vns_if.name, buf, SR_NAMELEN);
/* -- read interface ip into buf -- */
if(! (tmpptr = copy_next_field(fp, tmpptr, buf)) )
{
fclose(fp);
fprintf(stderr, "Bad formatting in cpu hardware file\n");
return 1;
}
Debug(" IP [%s] ", buf);
vns_if.ip = asci_to_nboip(buf);
/* -- read interface mask into buf -- */
if(! (tmpptr = copy_next_field(fp, tmpptr, buf)) )
{
fclose(fp);
fprintf(stderr, "Bad formatting in cpu hardware file\n");
return 1;
}
Debug(" Mask [%s] ", buf);
vns_if.mask = asci_to_nboip(buf);
/* -- read interface hw address into buf -- */
if(! (tmpptr = copy_next_field(fp, tmpptr, buf)) )
{
fclose(fp);
fprintf(stderr, "Bad formatting in cpu hardware file\n");
return 1;
}
Debug(" MAC [%s]\n", buf);
asci_to_ether(buf, vns_if.addr);
sr_integ_add_interface(sr, &vns_if);
} /* -- while ( fgets ( .. ) ) -- */
Debug(" < -- -- >\n");
fclose(fp);
return 0;
} /* -- sr_cpu_init_hardware -- */
/*-----------------------------------------------------------------------------
* Method: sr_cpu_input(..)
* Scope: Local
*
*---------------------------------------------------------------------------*/
int sr_cpu_input(struct sr_instance* sr)
{
/* REQUIRES */
assert(sr);
fprintf(stderr, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
fprintf(stderr, "!!! sr_cpu_input(..) (sr_cpu_extension_nf2.c) called while running in cpu mode !!!\n");
fprintf(stderr, "!!! you need to implement this function to read from the hardware !!!\n");
fprintf(stderr, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
assert(0);
/*
* TODO: Read packet from the hardware and pass to sr_integ_input(..)
* e.g.
*
* sr_integ_input(sr,
* packet, * lent *
* len,
* "eth2" ); * lent *
*/
/*
* Note: To log incoming packets, use sr_log_packet from sr_dumper.[c,h]
*/
/* RETURN 1 on success, 0 on failure.
* Note: With a 0 result, the router will shut-down
*/
return 1;
} /* -- sr_cpu_input -- */
/*-----------------------------------------------------------------------------
* Method: sr_cpu_output(..)
* Scope: Global
*
*---------------------------------------------------------------------------*/
int sr_cpu_output(struct sr_instance* sr /* borrowed */,
uint8_t* buf /* borrowed */ ,
unsigned int len,
const char* iface /* borrowed */)
{
/* REQUIRES */
assert(sr);
assert(buf);
assert(iface);
fprintf(stderr, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
fprintf(stderr, "!!! sr_cpu_output(..) (sr_cpu_extension_nf2.c) called while running in cpu mode !!!\n");
fprintf(stderr, "!!! you need to implement this function to write to the hardware !!!\n");
fprintf(stderr, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
assert(0);
/* Return the length of the packet on success, -1 on failure */
return -1;
} /* -- sr_cpu_output -- */
/*-----------------------------------------------------------------------------
* Method: copy_next_field(..)
* Scope: Local
*
*---------------------------------------------------------------------------*/
static
char* copy_next_field(FILE* fp, char* line, char* buf)
{
char* tmpptr = buf;
while ( *line && isspace((int)*line)) /* -- XXX: potential overrun here */
{ line++; }
if(! *line )
{ return 0; }
while ( *line && ! isspace((int)*line) && ((tmpptr - buf) < SR_NAMELEN))
{ *tmpptr++ = *line++; }
*tmpptr = 0;
return line;
} /* -- copy_next_field -- */
/*-----------------------------------------------------------------------------
* Method: asci_to_nboip(..)
* Scope: Local
*
*---------------------------------------------------------------------------*/
static uint32_t asci_to_nboip(const char* ip)
{
struct in_addr addr;
if ( inet_pton(AF_INET, ip, &addr) <= 0 )
{ return 0; } /* -- 0.0.0.0 unsupported so its ok .. yeah .. really -- */
return addr.s_addr;
} /* -- asci_to_nboip -- */
/*-----------------------------------------------------------------------------
* Method: asci_to_ether(..)
* Scope: Local
*
* Look away .. please ... just look away
*
*---------------------------------------------------------------------------*/
static void asci_to_ether(const char* addr, uint8_t mac[6])
{
uint32_t tmpint;
const char* buf = addr;
int i = 0;
for( i = 0; i < 6; ++i )
{
if (i)
{
while (*buf && *buf != ':')
{ buf++; }
buf++;
}
sscanf(buf, "%x", &tmpint);
mac[i] = tmpint & 0x000000ff;
}
} /* -- asci_to_ether -- */