forked from cleech/open-isns
-
Notifications
You must be signed in to change notification settings - Fork 22
/
sysdep-unix.c
189 lines (156 loc) · 3.88 KB
/
sysdep-unix.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
/*
* System dependent stuff
*
* Copyright (C) 2007 Olaf Kirch <[email protected]>
*/
#include <net/if.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <libisns/isns.h>
#include <libisns/util.h>
int isns_get_nr_portals(void)
{
char buffer[8192], *end, *ptr;
struct ifconf ifc;
unsigned int nportals = 0;
int fd = -1;
if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
isns_error("%s: no socket - %m\n", __FUNCTION__);
return 0;
}
ifc.ifc_buf = buffer;
ifc.ifc_len = sizeof(buffer);
if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
isns_error("ioctl(SIOCGIFCONF): %m\n");
goto out;
}
ptr = buffer;
end = buffer + ifc.ifc_len;
while (ptr < end) {
struct ifreq ifr;
struct sockaddr_storage ifaddr;
int ifflags;
memcpy(&ifr, ptr, sizeof(ifr));
ptr += sizeof(ifr);
/* Get the interface addr */
memcpy(&ifaddr, &ifr.ifr_addr, sizeof(ifr.ifr_addr));
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
isns_error("ioctl(%s, SIOCGIFFLAGS): %m\n",
ifr.ifr_name);
continue;
}
ifflags = ifr.ifr_flags;
if ((ifflags & IFF_UP) == 0)
continue;
if ((ifflags & IFF_LOOPBACK) != 0)
continue;
if (ifaddr.ss_family == AF_INET6 || ifaddr.ss_family == AF_INET)
nportals++;
}
out:
if (fd >= 0)
close(fd);
return nportals;
}
int
isns_enumerate_portals(isns_portal_info_t *result, unsigned int max)
{
char buffer[8192], *end, *ptr;
struct ifconf ifc;
unsigned int nportals = 0;
int fd = -1;
if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
isns_error("%s: no socket - %m\n", __FUNCTION__);
return 0;
}
ifc.ifc_buf = buffer;
ifc.ifc_len = sizeof(buffer);
if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
isns_error("ioctl(SIOCGIFCONF): %m\n");
goto out;
}
ptr = buffer;
end = buffer + ifc.ifc_len;
while (ptr < end) {
struct ifreq ifr;
struct sockaddr_storage ifaddr;
isns_portal_info_t portal;
int ifflags;
memcpy(&ifr, ptr, sizeof(ifr));
ptr += sizeof(ifr);
/* Get the interface addr */
memcpy(&ifaddr, &ifr.ifr_addr, sizeof(ifr.ifr_addr));
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
isns_error("ioctl(%s, SIOCGIFFLAGS): %m\n",
ifr.ifr_name);
continue;
}
ifflags = ifr.ifr_flags;
if ((ifflags & IFF_UP) == 0)
continue;
if ((ifflags & IFF_LOOPBACK) != 0)
continue;
if (!isns_portal_from_sockaddr(&portal, &ifaddr))
continue;
isns_debug_socket("Got interface %u: %s %s\n",
nportals, ifr.ifr_name,
isns_portal_string(&portal));
if (nportals < max)
result[nportals++] = portal;
}
out:
if (fd >= 0)
close(fd);
return nportals;
}
int
isns_portal_from_sockaddr(isns_portal_info_t *portal,
const struct sockaddr_storage *addr)
{
struct sockaddr_in6 *six;
struct sockaddr_in *sin;
memset(portal, 0, sizeof(*portal));
/* May have to convert AF_INET to AF_INET6 */
six = &portal->addr;
switch (addr->ss_family) {
case AF_INET6:
memcpy(six, addr, sizeof(*six));
break;
case AF_INET:
sin = (struct sockaddr_in *) addr;
six->sin6_family = AF_INET6;
six->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
six->sin6_port = sin->sin_port;
break;
default:
isns_error("internal error: unknown address family (%d)\n",
addr->ss_family);
return 0;
}
return 1;
}
int
isns_portal_to_sockaddr(const isns_portal_info_t *portal,
struct sockaddr_storage *addr)
{
const struct sockaddr_in6 *six = &portal->addr;
struct sockaddr_in *sin;
/* Check if this is really a v4 address is disguise.
* If so, explicitly use an AF_INET socket - the
* stack may not support IPv6.
*/
if (IN6_IS_ADDR_V4MAPPED(&six->sin6_addr)
|| IN6_IS_ADDR_V4COMPAT(&six->sin6_addr)) {
sin = (struct sockaddr_in *) addr;
memset(sin, 0, sizeof(*sin));
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = six->sin6_addr.s6_addr32[3];
sin->sin_port = six->sin6_port;
return sizeof(*sin);
}
/* This is the genuine article */
memcpy(addr, six, sizeof(*six));
return sizeof(*six);
}