-
Notifications
You must be signed in to change notification settings - Fork 2
/
synexec_netops.c
335 lines (297 loc) · 8.72 KB
/
synexec_netops.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
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
/*
* ------------------------------------
* synexec - Synchronised Executioner
* ------------------------------------
* synexec_netops.c
* ------------------
* Copyright 2014 (c) Citrix
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Read the README file for the changelog and information on how to
* compile and use this program.
*/
// Header files
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include "synexec_netops.h"
#include "synexec_common.h"
/*
* int
* get_ifdef(char **defif_name, struct in_addr *gw_addr);
* ------------------------------------------------------
* This function will search the route table for the default route and
* fill '*defif_name' with the interface name associated to it. If the
* interface is found and 'gw_addr' is a valid pointer, it is filled
* with the gateway address as well. '*defif_name' should be free()d
* after its use.
*
* Mandatory params: defif_name
* Optional params : gw_addr
*
* NOTES:
* '*defif_name' will be allocated with malloc() and should be free()d.
*
* Return values:
* -1 Error
* 0 Success
*/
int
get_ifdef(char **defif_name, struct in_addr *gw_addr){
// Local variables
char buf[256]; // Buffer for line reading
char *bufp; // Buffer pointer
char ifname[16]; // Temporary interface name buffer
FILE *routefp; // Route table file pointer
char tstr[3]; // Temporary char array
char tgw_ip[15]; // Temporary gw addr as a char array
char *tstrp; // Temporary char array pointer
int i, j; // Temporary integers
int err = 0; // Return code
// TODO: The instructions below can be replaced by an ioctl
// TODO: using SIOCRTMSG, but I couldn't find a way to do it,
// TODO: so I'm just greping /proc for the default route.
// TODO: Using ioctl will increase compatibility and look cleaner.
// Reset defif_name
if (defif_name == NULL){
goto err;
}
*defif_name = NULL;
// Open route table
if ((routefp = fopen(SYNEXEC_PROC_ROUTE, "r")) == NULL){
perror("fopen");
fprintf(stderr, "%s: Error opening route table (ro) at '%s'.\n", __FUNCTION__, SYNEXEC_PROC_ROUTE);
goto err;
}
// Loop searching for default route
while(!feof(routefp)){
// Reads a line from the route table
bufp = buf;
memset(bufp, 0, sizeof(buf));
if (fgets(bufp, sizeof(buf), routefp) == NULL){
// Error reading or eof
break;
}
// Remove trailing line breaks
i = strlen(bufp) - 1;
if (i <= 0){
continue;
}
while ((bufp[i] == '\n') || (bufp[i] == '\r')){
bufp[i] = '\0';
i = strlen(bufp) - 1;
if (i <= 0){
break;
}
}
// Skip empty lines
if (strlen(bufp) < 1){
continue;
}
// Copy first field to temporary interface name buffer
if ((bufp = strchr(bufp, '\t')) == NULL){
// Table fields not separated by '\t'?
fprintf(stderr, "%s: WARNING while reading route table: entries not separated by tabs.\n", __FUNCTION__);
continue;
}
*bufp++ = 0;
memcpy(ifname, buf, sizeof(ifname));
// Test destination address
if ((bufp = strchr(bufp, '\t')) == NULL){
// Corrupt table?
fprintf(stderr, "%s: WARNING while reading route table: entries not separated by tabs.\n", __FUNCTION__);
memset(ifname, 0, sizeof(ifname));
continue;
}
*bufp = 0;
bufp = buf + strlen(buf) + 1;
if (!strncmp((void *)bufp, "00000000", 8)){
// Found it
if (asprintf(defif_name, "%s", ifname) < 0){
perror("asprintf");
fprintf(stderr, "%s: Error allocating memory for interface name.\n", __FUNCTION__);
defif_name = NULL;
break;
}
// Get gateway address as well
// TODO: There's gotta be a more straightforward way to do this
if (gw_addr != NULL){
bufp += 15;
memset(&tgw_ip, 0, sizeof(tgw_ip));
tstrp = tgw_ip;
for (i=0; i<4; i++){
memset(&tstr, 0, sizeof(tstr));
for (j=0; j<2; j++){
tstr[j] = *bufp++;
}
bufp -= 4;
sprintf((void *)tstrp, "%ld", strtol((void *)tstr, NULL, 16));
tstrp = tgw_ip + strlen((char *)&tgw_ip);
if (i < 3){
*tstrp++ = '.';
}
}
inet_aton((void *)tgw_ip, gw_addr);
#if 0
// Failed attempt to do it in a more straightforward way:
bufp += 17;
*bufp = 0;
bufp -= 8;
printf("get_defif: gw = %s\n", bufp); // Ok, at the right position
gw_addr.s_addr = strtol((void *)bufp, NULL, 16);
printf("get_defif: gw = %s\n", inet_ntoa(gw_addr)); // Well, strtol just doesn't do it.
#endif
}
// Stop searching
break;
}
// Reset temporary interface name buffer
memset(ifname, 0, sizeof(ifname));
}
// Close route table
fclose(routefp);
// Verify if we missed it
if ((!*defif_name) || strlen(*defif_name) == 0){
goto err;
}
out:
// Return
return(err);
err:
err = -1;
goto out;
}
/*
* int
* get_ifipaddr(char *if_name, struct in_addr *if_addr);
* -----------------------------------------------------
* This function gets the address of interface named 'if_name' and stores it
* into 'if_addr'. If 'if_name' is "any", it stores '0.0.0.0'.
*
* Mandatory params: if_name, if_addr
* Optional params :
*
* Return values:
* -1 Error
* 0 Success
*/
int
get_ifipaddr(char *if_name, struct in_addr *if_addr){
// Local variables
int sock = -1; // Temporary socket
struct ifreq ifr; // Structure for interface request
int err = 0; // Return code
// Validate arguments
if (!if_name || !if_addr){
fprintf(stderr, "%s: invalid arguments.\n", __FUNCTION__);
goto err;
}
// Check special "any" case
if (!strcmp(if_name, "any")){
memset(&(if_addr->s_addr), 0, sizeof(if_addr->s_addr));
goto out;
}
// Create temporary socket for ioctl request
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
perror("socket");
fprintf(stderr, "%s: error creating temporary socket.\n", __FUNCTION__);
goto err;
}
// Set request structure
memset(&ifr, 0, sizeof(ifr));
memcpy(&ifr.ifr_name, if_name, sizeof(ifr.ifr_name));
// Execute ioctl and fetch result
if (ioctl(sock, SIOCGIFADDR, &ifr) < 0){
perror("ioctl");
fprintf(stderr, "%s: error fetching interface address.\n", __FUNCTION__);
goto err;
}
memcpy(if_addr, (ifr.ifr_addr.sa_data)+2, sizeof(*if_addr));
out:
// Close socket
if (sock >= 0){
close(sock);
}
// Return
return(err);
err:
err = -1;
goto out;
}
/*
* int
* get_ifbroad(char *if_name, struct in_addr *if_broad);
* -----------------------------------------------------
* This function gets the broadcast address of interface named 'if_name' and
* stores it into 'if_broad'. If 'if_name' is set to "any", the broadcast
* address is set to 255.255.255.255.
*
* Mandatory params: if_name, if_broad
* Optional params :
*
* Return values:
* -1 Error
* 0 Success
*/
int
get_ifbroad(char *if_name, struct in_addr *if_broad){
// Local variables
int sock = -1; // Temporary socket
struct ifreq ifr; // Structure for interface request
int err = 0; // Return code
// Validate arguments
if (!if_name || !if_broad){
fprintf(stderr, "%s: invalid arguments.\n", __FUNCTION__);
goto err;
}
// Check special "any" case
if (!strcmp(if_name, "any")){
memset(&(if_broad->s_addr), 0xFF, sizeof(if_broad->s_addr));
goto out;
}
// Create temporary socket for ioctl request
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
fprintf(stderr, "%s: error creating temporary socket.\n", __FUNCTION__);
perror("socket");
goto err;
}
// Set request structure
memset(&ifr, 0, sizeof(ifr));
memcpy(&ifr.ifr_name, if_name, sizeof(ifr.ifr_name));
// Execute ioctl and fetch result
if (ioctl(sock, SIOCGIFBRDADDR, &ifr) < 0){
fprintf(stderr, "%s: error fetching interface netmask.\n", __FUNCTION__);
perror("ioctl");
goto err;
}
memcpy(if_broad, (ifr.ifr_addr.sa_data)+2, sizeof(*if_broad));
out:
// Close socket
if (sock >= 0){
close(sock);
}
// Return
return(err);
err:
err = -1;
goto out;
}