File: net.c

package info (click to toggle)
ofono 2.18-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,064 kB
  • sloc: ansic: 224,979; sh: 5,012; python: 4,040; makefile: 956
file content (406 lines) | stat: -rw-r--r-- 7,538 bytes parent folder | download | duplicates (6)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * Embedded Linux library
 * Copyright (C) 2018  Intel Corporation
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/socket.h>
#include <linux/if_arp.h>
#include <linux/if_addr.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <netinet/ip.h>
#include <ifaddrs.h>

#include "net.h"
#include "net-private.h"
#include "string.h"
#include "utf8.h"
#include "useful.h"
#include "private.h"

/**
 * SECTION:net
 * @short_description: Network Interface related utilities
 *
 * Network Interface utilities
 */

/**
 * l_net_get_mac_address:
 * @ifindex: Interface index to query
 * @out_addr: Buffer to copy the mac address to.  Must be able to hold 6 bytes
 *
 * Obtains the mac address of the network interface given by @ifindex
 *
 * Returns: #true on success and #false on failure
 **/
LIB_EXPORT bool l_net_get_mac_address(uint32_t ifindex, uint8_t *out_addr)
{
	struct ifreq ifr;
	int sk, err;

	sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
	if (sk < 0)
		return false;

	memset(&ifr, 0, sizeof(ifr));
	ifr.ifr_ifindex = ifindex;

	err = ioctl(sk, SIOCGIFNAME, &ifr);
	if (err < 0)
		goto error;

	err = ioctl(sk, SIOCGIFHWADDR, &ifr);
	if (err < 0)
		goto error;

	close(sk);

	if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER)
		return false;

	memcpy(out_addr, ifr.ifr_hwaddr.sa_data, 6);
	return true;

error:
	close(sk);
	return false;
}

/**
 * l_net_get_name:
 * @ifindex: Interface index to query
 *
 * Obtains the name of the network inderface given by @ifindex
 *
 * Returns: A newly allocated string with the name or NULL on failure
 **/
LIB_EXPORT char *l_net_get_name(uint32_t ifindex)
{
	struct ifreq ifr;
	int sk, err;

	sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
	if (sk < 0)
		return NULL;

	memset(&ifr, 0, sizeof(ifr));
	ifr.ifr_ifindex = ifindex;

	err = ioctl(sk, SIOCGIFNAME, &ifr);
	close(sk);

	if (err < 0)
		return NULL;

	return l_strdup(ifr.ifr_name);
}

/**
 * l_net_hostname_is_root:
 * @hostname: Hostname to validate
 *
 * Identifies if the hostname given by @hostname is root domain name or
 * not.
 *
 * Returns: #true if the given hostname is root and #false otherwise.
 **/
LIB_EXPORT bool l_net_hostname_is_root(const char *hostname)
{
	if (unlikely(!hostname))
		return false;

	if (!strcmp(hostname, ""))
		return true;

	if (!strcmp(hostname, "."))
		return true;

	return false;
}

static bool str_has_suffix(const char *str, const char *suffix)
{
	size_t str_len;
	size_t suffix_len;
	size_t len_diff;

	str_len = strlen(str);
	suffix_len = strlen(suffix);

	if (str_len < suffix_len)
		return false;

	len_diff = str_len - suffix_len;

	return !strcasecmp(&str[len_diff], suffix);
}

/**
 * l_net_hostname_is_localhost:
 * @hostname: Hostname to validate
 *
 * Identifies if the hostname given by @hostname is localhost or not.
 *
 * Returns: #true if the given hostname is localhost and #false otherwise.
 **/
LIB_EXPORT bool l_net_hostname_is_localhost(const char *hostname)
{
	if (unlikely(!hostname))
		return false;

	if (!strcasecmp(hostname, "localhost") ||
			!strcasecmp(hostname, "localhost.") ||
			!strcasecmp(hostname, "localhost.localdomain") ||
			!strcasecmp(hostname, "localhost.localdomain."))
		return true;

	if (str_has_suffix(hostname, ".localhost") ||
			str_has_suffix(hostname, ".localhost.") ||
			str_has_suffix(hostname, ".localhost.localdomain") ||
			str_has_suffix(hostname, ".localhost.localdomain."))
		return true;

	return false;
}

static const char *domain_name_escape(const uint8_t *label, uint8_t l)
{
	/* RFC 1035, Section 3.1: "...limit the label to 63 octets or less." */
	static char buf[63 * 4 + 1];
	unsigned int i;
	unsigned int j;

	for (i = 0, j = 0; i < l; i++) {
		if (l_ascii_isalnum(label[i]) ||
				label[i] == '_' || label[i] == '-') {
			buf[j++] = label[i];
			continue;
		}

		if (label[i] == '.' || label[i] == '\\') {
			buf[j++] = '\\';
			buf[j++] = label[i];
			continue;
		}

		buf[j++] = '\\';
		buf[j++] = '0' + label[i] / 100;
		buf[j++] = '0' + (label[i] / 10) % 10;
		buf[j++] = '0' + label[i] % 10;
	}

	buf[j] = '\0';

	return buf;
}

static int validate_next_domain_name(const uint8_t *raw, size_t raw_len)
{
	const uint8_t *p;
	unsigned int r;

	if (raw_len <= 1)
		return -EBADMSG;

	/* Treat NULL domains as invalid */
	if (raw[0] == 0)
		return -EBADMSG;

	p = raw;
	r = raw_len;

	while (r) {
		uint8_t skip = *p;

		r -= 1;
		p += 1;

		if (skip > r)
			return -EBADMSG;

		if (skip > 63)
			return -EINVAL;

		if (skip == 0)
			break;

		r -= skip;
		p += skip;

		/* domains must end with a null label */
		if (!r)
			return -EBADMSG;
	}

	r = p - raw;
	if (r > 255)
		return -EMSGSIZE;

	return r;
}

/*
 * Parse the domain name encoded according to RFC 1035 Section 3.1
 */
char *net_domain_name_parse(const uint8_t *raw, size_t raw_len)
{
	int r;
	struct l_string *growable;
	const uint8_t *p;
	bool first = true;

	r = validate_next_domain_name(raw, raw_len);
	if (r < 0 || (size_t) r != raw_len)
		return NULL;

	growable = l_string_new(r);
	p = raw;

	while (*p) {
		if (first)
			first = false;
		else
			l_string_append_c(growable, '.');

		l_string_append(growable, domain_name_escape(p + 1, *p));
		p += *p + 1;
	}

	return l_string_unwrap(growable);
}

/*
 * Parse list of domain names encoded according to RFC 1035 Section 3.1
 */
char **net_domain_list_parse(const uint8_t *raw, size_t raw_len, bool padded)
{
	size_t remaining = raw_len;
	const uint8_t *p = raw;
	int r;
	char **ret;
	unsigned int nitems = 0;
	struct l_string *growable = NULL;

	while (remaining) {
		if (padded && p[0] == 0)
			break;

		r = validate_next_domain_name(p, remaining);
		if (r < 0)
			return NULL;

		p += r;
		remaining -= r;
		nitems += 1;
	}

	ret = l_new(char *, nitems + 1);
	p = raw;
	remaining = raw_len;
	nitems = 0;

	while (remaining) {
		remaining -= *p + 1;

		if (*p == 0) {
			if (!growable)
				break;

			p += 1;
			ret[nitems++] = l_string_unwrap(growable);
			growable = NULL;
			continue;
		}

		if (!growable)
			growable = l_string_new(128);
		else
			l_string_append_c(growable, '.');

		l_string_append(growable, domain_name_escape(p + 1, *p));
		p += *p + 1;
	}

	return ret;
}

LIB_EXPORT bool l_net_get_address(int ifindex, struct in_addr *out)
{
	struct ifreq ifr;
	int sk, err;
	struct sockaddr_in *server_ip;
	bool ret = false;

	sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
	if (sk < 0)
		return false;

	memset(&ifr, 0, sizeof(ifr));
	ifr.ifr_ifindex = ifindex;

	err = ioctl(sk, SIOCGIFNAME, &ifr);
	if (err < 0)
		goto done;

	err = ioctl(sk, SIOCGIFADDR, &ifr);
	if (err < 0)
		goto done;

	server_ip = (struct sockaddr_in *) &ifr.ifr_addr;
	out->s_addr = server_ip->sin_addr.s_addr;

	ret = true;

done:
	close(sk);

	return ret;
}

LIB_EXPORT bool l_net_get_link_local_address(int ifindex, struct in6_addr *out)
{
	L_AUTO_FREE_VAR(char *, ifname) = l_net_get_name(ifindex);
	struct ifaddrs *ifa;
	struct ifaddrs *cur;
	bool r = false;

	if (!ifname)
		return false;

	if (getifaddrs(&ifa) == -1)
		return false;

	for (cur = ifa; cur; cur = cur->ifa_next) {
		struct sockaddr_in6 *si6;

		if (cur->ifa_addr == NULL)
			continue;

		if (cur->ifa_addr->sa_family != AF_INET6)
			continue;

		if (strcmp(cur->ifa_name, ifname))
			continue;

		si6 = (struct sockaddr_in6 *) cur->ifa_addr;
		if (!IN6_IS_ADDR_LINKLOCAL(&si6->sin6_addr))
			continue;

		memcpy(out, &si6->sin6_addr, sizeof(struct in6_addr));

		r = true;
		goto done;
	}

done:
	freeifaddrs(ifa);
	return r;
}