-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetout_ip.c
More file actions
51 lines (40 loc) · 1.33 KB
/
getout_ip.c
File metadata and controls
51 lines (40 loc) · 1.33 KB
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
/*
* name: getout_ip.c
* this file is part of the "Net-Injector" project. It defines the getout_ip() function
* Adapted from R.Stevens
* license: This software is under GPL license
* date: 04 17 2003
* rev: 0.8
*/
#include "neti.h"
/* Finds the IP address of the output interface from OS routing table */
int getout_ip(struct sockaddr_in *if_addr, struct sockaddr_in *dest){
int sock_rt, len, one=1;
struct sockaddr_in found_if_addr;
memset(&found_if_addr, 0, sizeof(found_if_addr));
sock_rt = socket(AF_INET, SOCK_DGRAM, 0 );
dest->sin_port = htons(53);
if (setsockopt(sock_rt, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one))
== -1) {
fprintf(stderr, "Error : (getout_ip) in setsockopt !\n");
close(sock_rt);
return EXIT_FAILURE;
}
if (connect(sock_rt, (struct sockaddr*)dest, sizeof(struct sockaddr_in))
== -1 ) {
fprintf(stderr, "Error : (getout_ip) bad IP address !\n");
close(sock_rt);
return EXIT_FAILURE;
}
len = sizeof(found_if_addr);
if (getsockname(sock_rt, (struct sockaddr *)&found_if_addr, &len) == -1 ) {
fprintf(stderr, "Error : (getout_ip) in getsockname !\n");
close(sock_rt);
return EXIT_FAILURE;
}
close(sock_rt);
if (found_if_addr.sin_addr.s_addr == 0)
return EXIT_FAILURE;
memcpy(if_addr, &found_if_addr, sizeof(struct sockaddr_in));
return EXIT_SUCCESS;
}