-
Notifications
You must be signed in to change notification settings - Fork 1
/
ethernet.cc
491 lines (378 loc) · 9.78 KB
/
ethernet.cc
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/if_ether.h>
#include <sys/time.h>
#include <sys/types.h>
#include "ethernet.h"
#include "config.h"
#include "error.h"
#include "util.h"
#include <libnet.h>
#include "debug.h"
EthernetAddr MyEthernetAddr(getenv("MINET_ETHERNETADDR") ?
getenv("MINET_ETHERNETADDR") : (char*)Die("MINET_ETHERNETADDR is not set!"));
int ethernet_reader_pair[2];
int ethernet_reader_fd;
int ethernet_reader_pid;
int ethernet_writer_pair[2];
int ethernet_writer_fd;
int ethernet_writer_pid;
static EthernetConfig ethernet_config = { 0, 0, NULL} ;
static int ethernet_inited=0;
static RawEthernetPacket EthernetIncomingPacket;
static int EthernetIncomingPacketsAvailable;
static RawEthernetPacket EthernetOutgoingPacket;
static int EthernetOutgoingPacketsAvailable;
int MAX(int x, int y)
{
return x > y ? x : y;
}
void KillHandler(int sig)
{
EthernetShutdown(ðernet_config);
}
void EthernetInputHandler(int sig)
{
static int n=0;
fd_set read_fds;
struct timeval to;
int rc;
while (1) {
/* either reader, writer, or both have something for us */
FD_ZERO(&read_fds);
FD_SET(ethernet_reader_fd,&read_fds);
FD_SET(ethernet_writer_fd,&read_fds);
to.tv_sec=0;
to.tv_usec=0;
do {
rc = select(MAX(ethernet_reader_fd,ethernet_writer_fd)+1,
&read_fds, 0, 0, &to);
} while (rc<0 && errno==EINTR);
if (rc<0) {
PERROR();
exit(-1);
} else if (rc==0) {
/* nothing to do, time out */
return;
} else if (rc>0) {
if (FD_ISSET(ethernet_reader_fd,&read_fds)) {
EthernetIncomingPacket.Unserialize(ethernet_reader_fd);
EthernetIncomingPacketsAvailable=1;
ethernet_config.ISR(ethernet_config.device,
ETHERNET_SERVICE_PACKET_READY);
DEBUGPRINTF(5,"read packet %d\n",n++);
}
#if ETHERNET_WRITER_ON
if (FD_ISSET(ethernet_writer_fd,&read_fds)) {
int flag;
if (readall(ethernet_writer_fd,
(char*)&flag,sizeof(flag),0,1)!=sizeof(flag)) {
PERROR();
exit(-1);
}
ethernet_config.ISR(ethernet_config.device,flag);
DEBUGPRINTF(5,"write response - %s\n",
flag==ETHERNET_SERVICE_OUTPUT_BUFFER_FULL ? "OUTPUT_BUFFER_FULL" :
flag==ETHERNET_SERVICE_DMA_DONE ? "DMA_DONE" :
flag==ETHERNET_SERVICE_DMA_FAIL ? "DMA_FAIL" :
"unknown");
}
#endif
}
}
}
int EthernetStartInternal()
{
/*
Note the use of socketpair here is because linux does not
support async i/o on pipes. Real OSes do.
*/
struct sigaction sa;
memset(&sa,0,sizeof(sa));
sa.sa_handler=EthernetInputHandler;
sigaction(ETHERNET_SIGNAL,&sa,0);
if (socketpair(AF_UNIX,SOCK_STREAM,0,ethernet_reader_pair)) {
PERROR();
return -1;
}
ethernet_reader_fd=ethernet_reader_pair[0];
ethernet_reader_pid=fork();
if (ethernet_reader_pid<0) {
/* fork failed */
PERROR();
close(ethernet_reader_pair[0]);
close(ethernet_reader_pair[1]);
return -1;
} else if (ethernet_reader_pid==0) {
/* I am child */
//AttachDebuggerHere("/home/pdinda/netclass/stack++/ISR");
//BreakHere();
close(ethernet_reader_pair[0]);
if (dup2(ethernet_reader_pair[1],fileno(stdout))<0) {
PERROR();
exit(-1);
}
if (dup2(ethernet_reader_pair[1],fileno(stdin))<0) {
PERROR();
exit(-1);
}
DEBUGPRINTF(2,"Starting reader - exec '%s %s %s'\n",READER_ARGS[0],READER_ARGS[1],READER_ARGS[2]);
execv(READER_BINARY,READER_ARGS);
PERROR();
exit(-1);
} else {
/* I am parent */
close(ethernet_writer_pair[1]);
if (socketpair(AF_UNIX,SOCK_STREAM,0,ethernet_writer_pair)) {
PERROR();
kill(ethernet_reader_pid,SIGKILL);
close(ethernet_reader_fd);
return -1;
}
ethernet_writer_fd=ethernet_writer_pair[0];
ethernet_writer_pid=fork();
if (ethernet_writer_pid<0) {
/* fork failed */
PERROR();
kill(ethernet_reader_pid,SIGKILL);
close(ethernet_reader_fd);
close(ethernet_writer_pair[0]);
close(ethernet_writer_pair[1]);
return -1;
} else if (ethernet_writer_pid==0) {
/* I am child */
//AttachDebuggerHere("/home/pdinda/netclass/stack++/ISR");
//BreakHere();
close(ethernet_writer_pair[0]);
if (dup2(ethernet_writer_pair[1],fileno(stdout))<0) {
PERROR();
exit(-1);
}
if (dup2(ethernet_writer_pair[1],fileno(stdin))<0) {
PERROR();
exit(-1);
}
DEBUGPRINTF(2,"Starting writer - exec '%s %s %s'\n",WRITER_ARGS[0],WRITER_ARGS[1],WRITER_ARGS[2]);
execv(WRITER_BINARY,WRITER_ARGS);
PERROR();
exit(-1);
} else {
/* I am parent */
close(ethernet_writer_pair[1]);
}
return 0;
}
}
int EthernetShutdownInternal()
{
signal(ETHERNET_SIGNAL,SIG_DFL);
kill(ethernet_reader_pid,SIGKILL);
kill(ethernet_writer_pid,SIGKILL);
close(ethernet_reader_fd);
close(ethernet_writer_fd);
return 0;
}
int EthernetStartup(EthernetConfig *conf)
{
int rc;
assert(ethernet_inited==0);
assert(conf!=NULL);
assert(conf->device==0);
assert(conf->flags==0);
ethernet_config=*conf;
rc=EthernetStartInternal();
if (rc<0) {
return rc;
}
EthernetIncomingPacketsAvailable=0;
EthernetOutgoingPacketsAvailable=0;
ethernet_inited=1;
return rc;
}
int EthernetShutdown(EthernetConfig *conf)
{
assert(ethernet_inited==1);
assert(conf!=NULL);
assert(conf->device==0);
assert(conf->flags==0);
EthernetIncomingPacketsAvailable=0;
EthernetOutgoingPacketsAvailable=0;
return EthernetShutdownInternal();
}
int EthernetGetNextPacket(EthernetConfig *conf, RawEthernetPacket *p)
{
assert(ethernet_inited==1);
assert(conf!=NULL);
assert(conf->device==0);
assert(conf->flags==0);
if (EthernetIncomingPacketsAvailable<=0) {
return -1;
}
*p=EthernetIncomingPacket;
EthernetIncomingPacketsAvailable--;
return p->size;
}
int EthernetWriteNextPacket()
{
int rc=0;
assert(EthernetOutgoingPacketsAvailable>0);
EthernetOutgoingPacket.Serialize(ethernet_writer_fd);
EthernetOutgoingPacketsAvailable--;
DEBUGPRINTF(5,"Finished ethernetwritenextpacket\n");
return rc;
}
int EthernetInitiateSend(EthernetConfig *conf, RawEthernetPacket *p)
{
assert(ethernet_inited==1);
assert(conf!=NULL);
assert(conf->device==0);
assert(conf->flags==0);
assert(EthernetOutgoingPacketsAvailable==0);
EthernetOutgoingPacket=*p;
EthernetOutgoingPacketsAvailable=1;
return EthernetWriteNextPacket();
}
EthernetAddr::EthernetAddr()
{
bzero(addr,6);
}
EthernetAddr::EthernetAddr(const EthernetAddr &rhs)
{
memcpy(addr,rhs.addr,6);
}
EthernetAddr::EthernetAddr(const EthernetAddrString rhs)
{
SetToString(rhs);
}
const EthernetAddr & EthernetAddr::operator=(const EthernetAddr &rhs)
{
memcpy(addr,rhs.addr,6);
return *this;
}
bool EthernetAddr::operator==(const EthernetAddr &rhs) const
{
return (memcmp(addr,rhs.addr,6)==0);
}
void EthernetAddr::SetToString(const EthernetAddrString s)
{
int i,j;
for (i=0,j=0;i<6;i++,j+=3) {
hexbytetobyte(&(s[j]),&(addr[i]));
}
}
void EthernetAddr::GetAsString(EthernetAddrString s) const
{
int i,j;
for (i=0,j=0;i<6;i++,j+=3) {
bytetohexbyte(addr[i],&(s[j]));
if (i<5) {
s[j+2]=':';
} else {
s[j+2]=0;
}
}
}
ostream & EthernetAddr::Print(ostream &os) const
{
EthernetAddrString s;
GetAsString(s);
os << "EthernetAddr(" << (char*)s << ")";
return os;
}
void EthernetAddr::Serialize(const int fd) const
{
if (writeall(fd,addr,6)!=6) {
throw SerializationException();
}
}
void EthernetAddr::Unserialize(const int fd)
{
if (readall(fd,addr,6)!=6) {
throw SerializationException();
}
}
EthernetHeader::EthernetHeader() : Header(Headers::EthernetHeader)
{}
EthernetHeader::EthernetHeader(const Header &rhs) : Header(rhs)
{}
EthernetHeader::EthernetHeader(const Buffer &rhs) : Header(Headers::EthernetHeader,rhs)
{}
const EthernetHeader & EthernetHeader::operator=(const Header &rhs)
{
Header::operator=(rhs);
return *this;
}
void EthernetHeader::GetSrcAddr(EthernetAddr &addr) const
{
GetData((char*)(addr.addr),6,6);
}
void EthernetHeader::SetSrcAddr(const EthernetAddr &addr)
{
SetData((const char*)(addr.addr),6,6);
}
void EthernetHeader::GetDestAddr(EthernetAddr &addr) const
{
GetData((char*)(addr.addr),6,0);
}
void EthernetHeader::SetDestAddr(const EthernetAddr &addr)
{
SetData((const char*)(addr.addr),6,0);
}
void EthernetHeader::GetProtocolType(EthernetProtocol &protocoltype) const
{
GetData((char*)&protocoltype,2,12);
protocoltype=ntohs(protocoltype);
}
void EthernetHeader::SetProtocolType(const EthernetProtocol &protocoltype)
{
short temp = htons(protocoltype);
SetData((const char*)&temp,2,12);
}
ostream & EthernetHeader::Print(ostream &os) const
{
EthernetAddr addr;
EthernetProtocol proto;
os << "EthernetHeader(src=";
GetSrcAddr(addr);
os << addr << ", dst=";
GetDestAddr(addr);
GetProtocolType(proto);
os << addr <<", proto="<<proto<<")";
return os;
}
EthernetTrailer::EthernetTrailer() : Trailer(Trailers::EthernetTrailer)
{}
EthernetTrailer::EthernetTrailer(const Trailer &rhs) : Trailer(rhs)
{}
EthernetTrailer::EthernetTrailer(const Buffer &rhs) : Trailer(Trailers::EthernetTrailer,rhs)
{}
const EthernetTrailer & EthernetTrailer::operator=(const Trailer &rhs)
{
Trailer::operator=(rhs);
return *this;
}
void EthernetTrailer::GetCRC(EthernetCRC &crc) const
{
GetData((char*)&crc,4,0);
crc=ntohl(crc);
}
void EthernetTrailer::SetCRC(const EthernetCRC &crc)
{
EthernetCRC crc2=htonl(crc);
SetData((const char*)&crc2,4,0);
}
ostream & EthernetTrailer::Print(ostream &os) const
{
EthernetCRC crc;
GetCRC(crc);
os<<"EthernetTrailer(CRC="<<*((unsigned*)crc)<<")";
return os;
}