-
Notifications
You must be signed in to change notification settings - Fork 0
/
sr_rt.c
97 lines (84 loc) · 2.5 KB
/
sr_rt.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
/*-----------------------------------------------------------------------------
* file: sr_rt.c
* date: Mon Oct 07 04:02:12 PDT 2002
* Author: [email protected]
*
* Description:
*
*---------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define __USE_MISC 1 /* force linux to show inet_aton */
#include <arpa/inet.h>
#include "sr_rt.h"
#include "sr_router.h"
/*---------------------------------------------------------------------
* Method:
*
*---------------------------------------------------------------------*/
int sr_load_rt(struct sr_instance* sr,const char* filename)
{
FILE* fp;
char line[BUFSIZ];
char dest[32];
char gw[32];
char mask[32];
char iface[32];
struct in_addr dest_addr;
struct in_addr gw_addr;
struct in_addr mask_addr;
interface_t* intf;
/* -- REQUIRES -- */
assert(filename);
if( access(filename,R_OK) != 0)
{
perror("access");
return -1;
}
fp = fopen(filename,"r");
struct sr_router* subsystem = (struct sr_router*)sr_get_subsystem(sr);
while( fgets(line,BUFSIZ,fp) != 0)
{
printf("%s\n", line);
sscanf(line,"%s %s %s %s",dest,gw,mask,iface);
if(inet_aton(dest,&dest_addr) == 0)
{
fprintf(stderr,
"Error loading routing table, cannot convert %s to valid IP\n",
dest);
return -1;
}
if(inet_aton(gw,&gw_addr) == 0)
{
fprintf(stderr,
"Error loading routing table, cannot convert %s to valid IP\n",
gw);
return -1;
}
if(inet_aton(mask,&mask_addr) == 0)
{
fprintf(stderr,
"Error loading routing table, cannot convert %s to valid IP\n",
mask);
return -1;
}
intf = get_interface_name(subsystem, iface);
if(intf == NULL) {
fprintf(stderr,
"Error! Invalid interface: %s\n", iface);
} else {
rrtable_route_add( subsystem->rtable,
dest_addr.s_addr,
gw_addr.s_addr,
mask_addr.s_addr,
intf,
's' );
}
} /* -- while -- */
return 0; /* -- success -- */
} /* -- sr_load_rt -- */