-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterface.c
executable file
·243 lines (206 loc) · 6.14 KB
/
interface.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <interface.h>
#include <linux/if.h>
#include <asm/env.h>
#include <asm/eth.h>
#include <linux/sockios.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
interface_t* alloc_interface()
{
interface_t* interface = malloc(sizeof(interface_t));
interface->address.s_addr = 0;
return interface;
}
/*int get_interface_index(char *ifname)
{
int err, sock = lkl_sys_socket(PF_INET, SOCK_DGRAM, 0);
struct ifreq ifr;
int ifname_siz;
memset(&ifr,0,sizeof(struct ifreq));
if (sock < 0){
printf("LKL init :: change interface name negative value for \n");
return -sock;
}
ifname_siz = (IFNAMSIZ > strlen(ifname))?strlen(ifname):IFNAMSIZ;
memcpy(ifr.ifr_name,ifname,ifname_siz);
err=lkl_sys_ioctl(sock, SIOCGIFINDEX, (long)&ifr);
if (err < 0){
printf("LKL init :: could not get if index NO SUCK INTERFACE NAME %s\n",strerror(-err));
return -err;
}
lkl_sys_close(sock);
return ifr.ifr_ifindex;
}*/
#ifdef ISLKL
void lkl_change_ifname(int ifindex, char *newname)
{
int err, sock = lkl_sys_socket(PF_INET, SOCK_DGRAM, 0);
int ifname_siz;
char *oldname;
struct ifreq ifr;
memset(&ifr,0,sizeof(struct ifreq));
if (sock < 0){
printf("LKL init :: change interface name negative value for \n");
return;
}
ifr.ifr_ifindex = ifindex;
err=lkl_sys_ioctl(sock, SIOCGIFNAME, (long)&ifr);
if (err < 0){
printf("LKL init :: could not find out interface name\n");
return;
}
oldname=(char*)malloc(strlen(ifr.ifr_name));
memcpy(oldname,ifr.ifr_name,strlen(ifr.ifr_name));
memset(&ifr,0,sizeof(struct ifreq));
ifname_siz = (IFNAMSIZ > strlen(newname))?strlen(newname):IFNAMSIZ;
memcpy(ifr.ifr_name,oldname,strlen(oldname));
memcpy(ifr.ifr_newname,newname, ifname_siz);
lkl_printf("oldname =%s, newname=%s\n",oldname,newname);
err=lkl_sys_ioctl(sock, SIOCSIFNAME, (long)&ifr);
if (err < 0){
printf("LKL init :: could not change if name %s\n",strerror(-err));
return;
}
lkl_sys_close(sock);
}
int lkl_init_interface(const interface_t* interface)
{
int ifindex;
struct tun_device* td = malloc(sizeof(*td));
/* TODO: initializa from interface_t */
td->type = TUN_HUB;
td->port = interface->port;
td->address = interface->gateway.s_addr;
printf("LKL init :: Adding interface %s\n", interface->dev);
if ((ifindex=lkl_add_eth_tun(interface->dev, (char*) interface->mac, 64, td)) < 0) {
printf("LKL init :: could not bring up interface %s\n",interface->dev);
return -1;
}
lkl_change_ifname(ifindex, interface->dev);
if (interface->address.s_addr == 0) {
printf("LKL init :: warning! interface address not defined\n");
} else {
if (lkl_if_set_ipv4(ifindex, interface->address.s_addr, interface->netmask_len) < 0) {
printf("LKL init :: could not set IPv4 address\n");
return -1;
}
}
if (lkl_if_up(ifindex) < 0) {
printf("LKL init :: could not bring up interface\n");
return -1;
}
return ifindex;
}
int lkl_init_interface_short(const interface_t* interface)
{
int ifindex;
struct tun_device* td = malloc(sizeof(*td));
/* TODO: initializa from interface_t */
td->type = TUN_HUB;
td->port = interface->port;
td->address = interface->gateway.s_addr;
printf("LKL init :: Adding interface %s\n", interface->dev);
if ((ifindex=lkl_add_eth_tun(interface->dev, (char*) interface->mac, 64, td)) < 0) {
printf("LKL init :: could not bring up interface %s\n",interface->dev);
return -1;
}
lkl_change_ifname(ifindex, interface->dev);
if (interface->address.s_addr == 0) {
printf("LKL init :: warning! interface address not defined\n");
} else {
if (lkl_if_set_ipv4(ifindex, interface->address.s_addr, interface->netmask_len) < 0) {
printf("LKL init :: could not set IPv4 address\n");
return -1;
}
}
return ifindex;
}
int lkl_list_interfaces( int max_if_no)
{
int err, sock = lkl_sys_socket(PF_INET, SOCK_DGRAM, 0);
struct ifreq *ifr;
struct ifreq ifr2;
struct ifconf ifc;
struct sockaddr_in* inaddr;
int i,ifno;
if (sock < 0){
printf("LKL init :: change interface name negative value for \n");
return sock;
}
ifr = (struct ifreq*)malloc(max_if_no*sizeof(struct ifreq));
ifc.ifc_len = max_if_no * sizeof(struct ifreq);
ifc.ifc_req = ifr;
err=lkl_sys_ioctl(sock, SIOCGIFCONF, (long)&ifc);
if (err < 0){
printf("LKL init :: could not get interfaces\n");
return -err;
}
if (ifc.ifc_len >= max_if_no * sizeof(struct ifconf)){
free(ifr);
return lkl_list_interfaces(2*max_if_no);
}
ifno=ifc.ifc_len/sizeof(struct ifreq);
for(i=0;i<ifno;i++){
memset(&ifr2,0,sizeof(struct ifreq));
memcpy(ifr2.ifr_name,ifr[i].ifr_name,strlen(ifr[i].ifr_name));
err = lkl_sys_ioctl(sock,SIOCGIFFLAGS,(long)&ifr2);
inaddr = (struct sockaddr_in *) &(ifr[i].ifr_addr);
lkl_printf("%s %s %s\n",ifr[i].ifr_name,inet_ntoa(inaddr->sin_addr),((ifr2.ifr_flags &IFF_UP)?"UP":"DOWN"));
}
lkl_sys_close(sock);
free(ifr);
return 0;
}
#endif
void dump_interface(int fd, interface_t *interface)
{
char address[32];
char buffer[256];
memset(buffer, 0, 256);
sprintf(buffer,"interface {\n");
if (write(fd, buffer, strlen(buffer)) < 0) {
perror("write:");
}
if (interface->dev) {
memset(buffer, 0, 256);
sprintf(buffer,"\tdev %s;\n", interface->dev);
if (write(fd, buffer, strlen(buffer)) < 0) {
perror("write:");
}
}
if (interface->address.s_addr) {
memset(buffer, 0, 256);
memset(address, 0, 32);
inet_ntop(AF_INET, &interface->address, address, 32);
sprintf(buffer, "\tipaddress %s;\n", address);
if (write(fd, buffer, strlen(buffer)) < 0) {
perror("write:");
}
}
if (interface->mac) {
memset(buffer, 0, 256);
sprintf(buffer, "\tmac %s\n", ether_ntoa(interface->mac));
if (write(fd, buffer, strlen(buffer)) < 0) {
perror("write:");
}
}
if (interface->gateway.s_addr) {
memset(buffer, 0, 256);
memset(address, 0, 32);
inet_ntop(AF_INET, &interface->gateway, address, 32);
sprintf(buffer, "\tgateway %s;\n", address);
if (write(fd, buffer, strlen(buffer)) < 0) {
perror("write:");
}
}
memset(buffer, 0, 256);
sprintf(buffer,"\tnetmask %d;\n\tport %d;\n\ttype hub;\n}\n", interface->netmask_len, interface->port);
if (write(fd, buffer, strlen(buffer)) < 0) {
perror("write:");
}
}