-
Notifications
You must be signed in to change notification settings - Fork 0
/
sr_rt.c
171 lines (141 loc) · 4.42 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
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
/*-----------------------------------------------------------------------------
* 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;
/* -- REQUIRES -- */
assert(filename);
if( access(filename,R_OK) != 0)
{
perror("access");
return -1;
}
fp = fopen(filename,"r");
while( fgets(line,BUFSIZ,fp) != 0)
{
if (EOF == sscanf(line,"%s %s %s %s",dest,gw,mask,iface)) break;
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;
}
sr_add_rt_entry(sr,dest_addr,gw_addr,mask_addr,iface);
} /* -- while -- */
return 0; /* -- success -- */
} /* -- sr_load_rt -- */
/*---------------------------------------------------------------------
* Method:
*
*---------------------------------------------------------------------*/
void sr_add_rt_entry(struct sr_instance* sr, struct in_addr dest,
struct in_addr gw, struct in_addr mask,char* if_name)
{
struct sr_rt* rt_walker = 0;
/* -- REQUIRES -- */
assert(if_name);
assert(sr);
/* -- empty list special case -- */
if(sr->routing_table == 0)
{
sr->routing_table = (struct sr_rt*)malloc(sizeof(struct sr_rt));
assert(sr->routing_table);
sr->routing_table->next = 0;
sr->routing_table->dest = dest;
sr->routing_table->gw = gw;
sr->routing_table->mask = mask;
strncpy(sr->routing_table->interface,if_name,sr_IFACE_NAMELEN);
return;
}
/* -- find the end of the list -- */
rt_walker = sr->routing_table;
while(rt_walker->next)
{rt_walker = rt_walker->next; }
rt_walker->next = (struct sr_rt*)malloc(sizeof(struct sr_rt));
assert(rt_walker->next);
rt_walker = rt_walker->next;
rt_walker->next = 0;
rt_walker->dest = dest;
rt_walker->gw = gw;
rt_walker->mask = mask;
strncpy(rt_walker->interface,if_name,sr_IFACE_NAMELEN);
} /* -- sr_add_entry -- */
/*---------------------------------------------------------------------
* Method:
*
*---------------------------------------------------------------------*/
void sr_print_routing_table(struct sr_instance* sr)
{
struct sr_rt* rt_walker = 0;
if(sr->routing_table == 0)
{
printf(" *warning* Routing table empty \n");
return;
}
printf("Destination\tGateway\t\tMask\tIface\n");
rt_walker = sr->routing_table;
sr_print_routing_entry(rt_walker);
while(rt_walker->next)
{
rt_walker = rt_walker->next;
sr_print_routing_entry(rt_walker);
}
} /* -- sr_print_routing_table -- */
/*---------------------------------------------------------------------
* Method:
*
*---------------------------------------------------------------------*/
void sr_print_routing_entry(struct sr_rt* entry)
{
/* -- REQUIRES --*/
assert(entry);
assert(entry->interface);
printf("%s\t\t",inet_ntoa(entry->dest));
printf("%s\t",inet_ntoa(entry->gw));
printf("%s\t",inet_ntoa(entry->mask));
printf("%s\n",entry->interface);
} /* -- sr_print_routing_entry -- */