-
Notifications
You must be signed in to change notification settings - Fork 0
/
sr_if.c
executable file
·195 lines (155 loc) · 4.66 KB
/
sr_if.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
/*-----------------------------------------------------------------------------
* file: sr_inface.
* date: Sun Oct 06 14:13:13 PDT 2002
* Contact: [email protected]
*
* Description:
*
* Data structures and methods for handling interfaces
*
*---------------------------------------------------------------------------*/
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#ifdef _DARWIN_
#include <sys/types.h>
#endif /* _DARWIN_ */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "sr_if.h"
#include "sr_router.h"
/*---------------------------------------------------------------------
* Method: sr_get_interface
* Scope: Global
*
* Given an interface name return the interface record or 0 if it doesn't
* exist.
*
*---------------------------------------------------------------------*/
struct sr_if* sr_get_interface(struct sr_instance* sr, const char* name)
{
struct sr_if* if_walker = 0;
/* -- REQUIRES -- */
assert(name);
assert(sr);
if_walker = sr->if_list;
while(if_walker)
{
if(!strncmp(if_walker->name,name,sr_IFACE_NAMELEN))
{ return if_walker; }
if_walker = if_walker->next;
}
return 0;
} /* -- sr_get_interface -- */
/*---------------------------------------------------------------------
* Method: sr_add_interface(..)
* Scope: Global
*
* Add and interface to the router's list
*
*---------------------------------------------------------------------*/
void sr_add_interface(struct sr_instance* sr, const char* name)
{
struct sr_if* if_walker = 0;
/* -- REQUIRES -- */
assert(name);
assert(sr);
/* -- empty list special case -- */
if(sr->if_list == 0)
{
sr->if_list = (struct sr_if*)malloc(sizeof(struct sr_if));
assert(sr->if_list);
sr->if_list->next = 0;
strncpy(sr->if_list->name,name,sr_IFACE_NAMELEN);
return;
}
/* -- find the end of the list -- */
if_walker = sr->if_list;
while(if_walker->next)
{if_walker = if_walker->next; }
if_walker->next = (struct sr_if*)malloc(sizeof(struct sr_if));
assert(if_walker->next);
if_walker = if_walker->next;
strncpy(if_walker->name,name,sr_IFACE_NAMELEN);
if_walker->next = 0;
} /* -- sr_add_interface -- */
/*---------------------------------------------------------------------
* Method: sr_sat_ether_addr(..)
* Scope: Global
*
* set the ethernet address of the LAST interface in the interface list
*
*---------------------------------------------------------------------*/
void sr_set_ether_addr(struct sr_instance* sr, const unsigned char* addr)
{
struct sr_if* if_walker = 0;
/* -- REQUIRES -- */
assert(sr->if_list);
if_walker = sr->if_list;
while(if_walker->next)
{if_walker = if_walker->next; }
/* -- copy address -- */
memcpy(if_walker->addr,addr,6);
} /* -- sr_set_ether_addr -- */
/*---------------------------------------------------------------------
* Method: sr_set_ether_ip(..)
* Scope: Global
*
* set the IP address of the LAST interface in the interface list
*
*---------------------------------------------------------------------*/
void sr_set_ether_ip(struct sr_instance* sr, uint32_t ip_nbo)
{
struct sr_if* if_walker = 0;
/* -- REQUIRES -- */
assert(sr->if_list);
if_walker = sr->if_list;
while(if_walker->next)
{if_walker = if_walker->next; }
/* -- copy address -- */
if_walker->ip = ip_nbo;
} /* -- sr_set_ether_ip -- */
/*---------------------------------------------------------------------
* Method: sr_print_if_list(..)
* Scope: Global
*
* print out the list of interfaces to stdout
*
*---------------------------------------------------------------------*/
void sr_print_if_list(struct sr_instance* sr)
{
struct sr_if* if_walker = 0;
if(sr->if_list == 0)
{
printf(" Interface list empty \n");
return;
}
if_walker = sr->if_list;
sr_print_if(if_walker);
while(if_walker->next)
{
if_walker = if_walker->next;
sr_print_if(if_walker);
}
} /* -- sr_print_if_list -- */
/*---------------------------------------------------------------------
* Method: sr_print_if(..)
* Scope: Global
*
* print out a single interface to stdout
*
*---------------------------------------------------------------------*/
void sr_print_if(struct sr_if* iface)
{
struct in_addr ip_addr;
/* -- REQUIRES --*/
assert(iface);
assert(iface->name);
ip_addr.s_addr = iface->ip;
Debug("%s\tHWaddr",iface->name);
DebugMAC(iface->addr);
Debug("\n");
Debug("\tinet addr %s\n",inet_ntoa(ip_addr));
} /* -- sr_print_if -- */