-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_ip.c
78 lines (65 loc) · 1.92 KB
/
get_ip.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
#include "get_ip.h"
char *find_addr(char *ifname, int is_host)
{
struct ifaddrs *ifaddrs;
getifaddrs(&ifaddrs);
int offset;
switch (is_host)
{
// For custom setup
case -2:
offset = 2;
break;
// Find my own IP
case -1:
offset = 0;
break;
// Find the host IP from BlueField
case 0:
offset = -100;
break;
// Find the BlueField IP from host
case 1:
offset = 100;
break;
}
do
{
if (strcmp(ifaddrs->ifa_name, ifname) == 0 && ifaddrs->ifa_addr->sa_family == AF_INET)
{
struct sockaddr_in *bf_addr = (struct sockaddr_in *) ifaddrs->ifa_addr;
//printf("My address: %s + %d\n", inet_ntoa(bf_addr->sin_addr), offset);
bf_addr->sin_addr.s_addr = htonl(ntohl(bf_addr->sin_addr.s_addr) + offset);
//printf("Transformed: %s + %d\n", inet_ntoa(bf_addr->sin_addr), offset);
return inet_ntoa(bf_addr->sin_addr);
}
else
{
ifaddrs = ifaddrs->ifa_next;
}
}
while (ifaddrs->ifa_next);
return NULL;
}
char *offset_addr(char *ifname, int offset)
{
struct ifaddrs *ifaddrs;
getifaddrs(&ifaddrs);
do
{
if (strcmp(ifaddrs->ifa_name, ifname) == 0 && ifaddrs->ifa_addr->sa_family == AF_INET)
{
struct sockaddr_in *bf_addr = (struct sockaddr_in *) ifaddrs->ifa_addr;
//printf("My address: %s + %d\n", inet_ntoa(bf_addr->sin_addr), offset);
bf_addr->sin_addr.s_addr = htonl(ntohl(bf_addr->sin_addr.s_addr) + offset);
//printf("Transformed: %s + %d\n", inet_ntoa(bf_addr->sin_addr), offset);
return inet_ntoa(bf_addr->sin_addr);
}
else
{
ifaddrs = ifaddrs->ifa_next;
}
}
while (ifaddrs->ifa_next);
return NULL;
}