forked from alexpevzner/sane-airscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airscan-ip.c
220 lines (189 loc) · 4.93 KB
/
airscan-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
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
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* Utility function for IP addresses
*/
#include "airscan.h"
#include <string.h>
#include <arpa/inet.h>
/* Format ip_straddr from IP address (struct in_addr or struct in6_addr)
* af must be AF_INET or AF_INET6
*/
ip_straddr
ip_straddr_from_ip (int af, const void *addr)
{
ip_straddr straddr = {""};
inet_ntop(af, addr, straddr.text, sizeof(straddr.text));
return straddr;
}
/* Format struct sockaddr. Both AF_INET and AF_INET6 are supported
*/
ip_straddr
ip_straddr_from_sockaddr(const struct sockaddr *addr)
{
return ip_straddr_from_sockaddr_dport(addr, -1);
}
/* Format ip_straddr from struct sockaddr.
* Port will not be appended, if it matches provided default port
* Both AF_INET and AF_INET6 are supported
*/
ip_straddr
ip_straddr_from_sockaddr_dport(const struct sockaddr *addr, int dport)
{
ip_straddr straddr = {""};
struct sockaddr_in *addr_in = (struct sockaddr_in*) addr;
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6*) addr;
uint16_t port = 0;
switch (addr->sa_family) {
case AF_INET:
inet_ntop(AF_INET, &addr_in->sin_addr,
straddr.text, sizeof(straddr.text));
port = addr_in->sin_port;
break;
case AF_INET6:
straddr.text[0] = '[';
inet_ntop(AF_INET6, &addr_in6->sin6_addr,
straddr.text + 1, sizeof(straddr.text) - 2);
strcat(straddr.text, "]");
port = addr_in6->sin6_port;
break;
}
port = htons(port);
if (port != dport) {
sprintf(straddr.text + strlen(straddr.text), ":%d", port);
}
return straddr;
}
/* Check if address is link-local
* af must be AF_INET or AF_INET6
*/
bool
ip_is_linklocal (int af, const void *addr)
{
if (af == AF_INET) {
/* 169.254.0.0/16 */
const uint32_t *a = addr;
return (ntohl(*a) & 0xffff0000) == 0xa9fe0000;
} else {
const uint8_t *a = addr;
return a[0] == 0xfe && (a[1] & 0xc0) == 0x80;
}
}
/* Check if sockaddr is link-local
*/
bool
ip_sockaddr_is_linklocal (const struct sockaddr *addr)
{
struct sockaddr_in *addr_in = (struct sockaddr_in*) addr;
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6*) addr;
switch (addr->sa_family) {
case AF_INET:
return ip_is_linklocal(AF_INET, &addr_in->sin_addr);
case AF_INET6:
return ip_is_linklocal(AF_INET6, &addr_in6->sin6_addr);
}
return false;
}
/* Check if address is loopback
* af must be AF_INET or AF_INET6
*/
bool
ip_is_loopback (int af, const void *addr)
{
if (af == AF_INET) {
/* 169.254.0.0/16 */
const uint32_t *a = addr;
return ntohl(*a) == 0x7f000001;
} else {
static const uint8_t loopback[16] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
};
return !memcmp(addr, loopback, 16);
}
}
/* ip_addr_set represents a set of IP addresses
*/
struct ip_addrset {
ip_addr *addrs; /* Addresses in the set */
size_t len, cap; /* Set length and capacity */
};
/* Create new ip_addrset
*/
ip_addrset*
ip_addrset_new (void)
{
ip_addrset *addrset = g_new0(ip_addrset, 1);
addrset->cap = 4;
addrset->addrs = g_new(ip_addr, addrset->cap);
return addrset;
}
/* Free ip_addrset
*/
void
ip_addrset_free (ip_addrset *addrset)
{
g_free(addrset->addrs);
g_free(addrset);
}
/* Find address index within a set. Returns -1 if address was not found
*/
static int
ip_addrset_index (const ip_addrset *addrset, ip_addr addr)
{
size_t i;
for (i = 0; i < addrset->len; i ++) {
if (ip_addr_equal(addrset->addrs[i], addr)) {
return (int) i;
}
}
return -1;
}
/* Check if address is in set
*/
bool
ip_addrset_lookup (const ip_addrset *addrset, ip_addr addr)
{
return ip_addrset_index(addrset, addr) >= 0;
}
/* Add address to the set. Returns true, if address was
* actually added, false if it was already in the set
*/
bool
ip_addrset_add (ip_addrset *addrset, ip_addr addr)
{
if (ip_addrset_lookup(addrset, addr)) {
return false;
}
ip_addrset_add_unsafe(addrset, addr);
return true;
}
/* Add address to the set without checking for duplicates
*/
void
ip_addrset_add_unsafe (ip_addrset *addrset, ip_addr addr)
{
if (addrset->len == addrset->cap) {
addrset->cap *= 2;
addrset->addrs = g_renew(ip_addr, addrset->addrs, addrset->cap);
}
addrset->addrs[addrset->len ++] = addr;
}
/* Del address from the set.
*/
void
ip_addrset_del (ip_addrset *addrset, ip_addr addr)
{
int i = ip_addrset_index(addrset, addr);
if (i >= 0) {
size_t tail = addrset->len - (size_t) i - 1;
if (tail != 0) {
tail *= sizeof(*addrset->addrs);
memmove(&addrset->addrs[i], &addrset->addrs[i + 1], tail);
}
addrset->len --;
}
}
/* vim:ts=8:sw=4:et
*/