forked from baresip/baresip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.c
819 lines (629 loc) · 14.1 KB
/
net.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/**
* @file src/net.c Networking code
*
* Copyright (C) 2010 - 2016 Alfred E. Heggestad
*/
#include <re.h>
#include <baresip.h>
#include "core.h"
struct network {
struct config_net cfg;
struct list laddrs; /**< List of local addresses */
struct dnsc *dnsc;
struct sa nsv[NET_MAX_NS];/**< Configured name servers */
uint32_t nsn; /**< Number of configured name servers */
struct sa nsvf[NET_MAX_NS];/**< Configured fallback name servers */
uint32_t nsnf; /**< Number of configured fallback name servers */
};
struct laddr {
struct le le;
char *ifname;
struct sa sa;
};
static void net_destructor(void *data)
{
struct network *net = data;
mem_deref(net->dnsc);
list_flush(&net->laddrs);
}
static void laddr_destructor(void *data)
{
struct laddr *laddr = data;
list_unlink(&laddr->le);
mem_deref(laddr->ifname);
}
static int net_dns_srv_add(struct network *net, const struct sa *sa,
bool fallback)
{
if (!net)
return EINVAL;
if (!fallback && net->nsn >= RE_ARRAY_SIZE(net->nsv))
return E2BIG;
if (fallback && net->nsnf >= RE_ARRAY_SIZE(net->nsvf))
return E2BIG;
if (fallback)
sa_cpy(&net->nsvf[net->nsnf++], sa);
else
sa_cpy(&net->nsv[net->nsn++], sa);
return 0;
}
static int net_dns_srv_get(const struct network *net,
struct sa *srvv, uint32_t *n, bool *from_sys)
{
struct sa nsv[NET_MAX_NS];
uint32_t i, nsn = RE_ARRAY_SIZE(nsv);
uint32_t offset;
uint32_t limit = *n;
int err;
if (net->nsn) {
if (net->nsn > limit)
return E2BIG;
/* Use any configured nameservers */
for (i=0; i<net->nsn; i++) {
srvv[i] = net->nsv[i];
}
*n = net->nsn;
if (from_sys)
*from_sys = false;
}
else {
err = dns_srv_get(NULL, 0, nsv, &nsn);
if (err)
nsn = 0;
if (nsn > limit)
return E2BIG;
for (i=0; i<nsn; i++)
srvv[i] = nsv[i];
*n = nsn;
if (from_sys)
*from_sys = true;
}
/* Add Fallback nameservers */
if (net->nsnf) {
offset = *n;
if ((offset + net->nsnf) > limit) {
debug("net: too many DNS nameservers, "
"fallback DNS ignored\n");
return 0;
}
for (i=0; i<net->nsnf; i++) {
srvv[offset+i] = net->nsvf[i];
}
*n = offset + net->nsnf;
}
return 0;
}
/*
* Check for DNS Server updates
*/
void net_dns_refresh(struct network *net)
{
struct sa nsv[NET_MAX_NS];
uint32_t nsn;
int err;
nsn = RE_ARRAY_SIZE(nsv);
err = net_dns_srv_get(net, nsv, &nsn, NULL);
if (err)
return;
(void)dnsc_srv_set(net->dnsc, nsv, nsn);
}
/**
* Check if address family is enabled
*
* @param net Network instance
* @param af AF_INET or AF_INET6
*
* @return True if enabled, false if disabled
*/
bool net_af_enabled(const struct network *net, int af)
{
if (!net || af == AF_UNSPEC)
return false;
switch (net->cfg.af) {
case AF_UNSPEC:
return true;
default:
return af == net->cfg.af;
}
}
static int dns_init(struct network *net)
{
struct sa nsv[NET_MAX_NS];
uint32_t nsn = RE_ARRAY_SIZE(nsv);
int err;
err = net_dns_srv_get(net, nsv, &nsn, NULL);
if (err)
return err;
return dnsc_alloc(&net->dnsc, NULL, nsv, nsn);
}
/**
* Return TRUE if libre supports IPv6
*/
static bool check_ipv6(void)
{
struct sa sa;
return 0 == sa_set_str(&sa, "::1", 2000);
}
/**
* Add a local IP address with given interface name
*
* @param net Network instance
* @param sa IP address
* @param ifname Interface name
*
* @return 0 if success, otherwise errorcode
*/
int net_add_address_ifname(struct network *net, const struct sa *sa,
const char *ifname)
{
struct le *le;
struct laddr *laddr;
int err = 0;
if (!net || !str_isset(ifname) || !sa)
return EINVAL;
LIST_FOREACH(&net->laddrs, le) {
laddr = le->data;
if (sa_cmp(&laddr->sa, sa, SA_ADDR))
goto out;
}
laddr = mem_zalloc(sizeof(*laddr), laddr_destructor);
if (!laddr)
return ENOMEM;
laddr->sa = *sa;
err = str_dup(&laddr->ifname, ifname);
if (err)
goto out;
list_append(&net->laddrs, &laddr->le, laddr);
out:
if (err)
mem_deref(laddr);
return err;
}
static bool add_laddr_filter(const char *ifname, const struct sa *sa,
void *arg)
{
struct network *net = arg;
if (!net_ifaddr_filter(net, ifname, sa))
return false;
(void)net_add_address_ifname(net, sa, ifname);
return false;
}
static bool if_debug_handler(const char *ifname, const struct sa *sa,
void *arg)
{
void **argv = arg;
struct re_printf *pf = argv[0];
struct network *net = argv[1];
bool def;
int err = 0;
def = sa_cmp(net_laddr_af(baresip_network(), sa_af(sa)), sa, SA_ADDR);
if (net_af_enabled(net, sa_af(sa)))
err = re_hprintf(pf, " %10s: %j %s\n", ifname, sa,
def ? "(default)" : "");
return err != 0;
}
static int check_route(const struct sa *src, const struct sa *dst)
{
struct sa ip;
int err;
err = net_dst_source_addr_get(dst, &ip);
if (err)
return err;
if (!sa_cmp(src, &ip, SA_ADDR))
return ECONNREFUSED;
return 0;
}
enum laddr_check {
LADDR_NOLINKLOCAL = 1,
LADDR_INTERNET = 2
};
static const struct sa *find_laddr_af(const struct network *net, int af,
enum laddr_check lc)
{
struct le *le;
struct sa dst;
int err;
if (!net)
return NULL;
sa_init(&dst, af);
if (af == AF_INET6)
err = sa_set_str(&dst, "1::1", 53);
else
err = sa_set_str(&dst, "1.1.1.1", 53);
if (err)
return NULL;
LIST_FOREACH(&net->laddrs, le) {
struct laddr *laddr = le->data;
if (sa_af(&laddr->sa) != af)
continue;
if ((lc & LADDR_NOLINKLOCAL) && sa_is_linklocal(&laddr->sa))
continue;
if ((lc & LADDR_INTERNET) && check_route(&laddr->sa, &dst))
continue;
return &laddr->sa;
}
return NULL;
}
static bool print_addr(const char *ifname, const struct sa *sa, void *arg)
{
(void) arg;
info(" %10s: %j\n", ifname, sa);
return false;
}
/**
* Initialise networking
*
* @param netp Pointer to allocated network instance
* @param cfg Network configuration
*
* @return 0 if success, otherwise errorcode
*/
int net_alloc(struct network **netp, const struct config_net *cfg)
{
struct network *net;
int err;
if (!netp || !cfg)
return EINVAL;
/*
* baresip/libre must be built with matching HAVE_INET6 value.
* if different the size of `struct sa' will not match and the
* application is very likely to crash.
*/
#ifdef HAVE_INET6
if (!check_ipv6()) {
warning("libre was compiled without IPv6-support"
", but baresip was compiled with\n");
return EAFNOSUPPORT;
}
#else
if (check_ipv6()) {
warning("libre was compiled with IPv6-support"
", but baresip was compiled without\n");
return EAFNOSUPPORT;
}
#endif
net = mem_zalloc(sizeof(*net), net_destructor);
if (!net)
return ENOMEM;
net->cfg = *cfg;
if (cfg->nsc) {
size_t i;
for (i=0; i<cfg->nsc; i++) {
const char *ns = cfg->nsv[i].addr;
struct sa sa;
err = sa_decode(&sa, ns, str_len(ns));
if (err) {
warning("net: dns_server:"
" could not decode `%s' (%m)\n",
ns, err);
goto out;
}
err = net_dns_srv_add(net, &sa, cfg->nsv[i].fallback);
if (err) {
warning("net: failed to add nameserver: %m\n",
err);
goto out;
}
}
}
/* Initialise DNS resolver */
err = dns_init(net);
if (err) {
warning("net: dns_init: %m\n", err);
goto out;
}
if (cfg->use_getaddrinfo)
dnsc_getaddrinfo(net->dnsc, true);
else
dnsc_getaddrinfo(net->dnsc, false);
net_if_apply(add_laddr_filter, net);
info("Local network addresses:\n");
if (!list_count(&net->laddrs))
info(" None available for net_interface: %s\n",
str_isset(cfg->ifname) ? cfg->ifname : "-");
else
net_laddr_apply(net, print_addr, NULL);
out:
if (err)
mem_deref(net);
else
*netp = net;
return err;
}
/**
* Use a specific DNS server
*
* @param net Network instance
* @param srvv DNS Nameservers
* @param srvc Number of nameservers
*
* @return 0 if success, otherwise errorcode
*/
int net_use_nameserver(struct network *net, const struct sa *srvv, size_t srvc)
{
size_t i;
if (!net)
return EINVAL;
net->nsn = (uint32_t)min(RE_ARRAY_SIZE(net->nsv), srvc);
if (srvv) {
for (i=0; i<srvc; i++) {
net->nsv[i] = srvv[i];
}
}
net_dns_refresh(net);
return 0;
}
/**
* Set network IP address
*
* @param net Network instance
* @param ip IP address
*
* @return 0 if success, otherwise errorcode
*/
int net_set_address(struct network *net, const struct sa *ip)
{
struct config_net *cfg = &net->cfg;
if (!net)
return EINVAL;
re_snprintf(cfg->ifname, sizeof(cfg->ifname), "%j", ip);
net_flush_addresses(net);
net_if_apply(add_laddr_filter, net);
return 0;
}
/**
* Add a local IP address
*
* @param net Network instance
* @param ip IP address
*
* @return 0 if success, otherwise errorcode
*/
int net_add_address(struct network *net, const struct sa *ip)
{
char ifname[256] = "???";
int err;
if (!net || !sa_isset(ip, SA_ADDR))
return EINVAL;
err = net_if_getname(ifname, sizeof(ifname), sa_af(ip), ip);
if (err)
goto out;
err = net_add_address_ifname(net, ip, ifname);
out:
return err;
}
/**
* Remove a local IP address
*
* @param net Network instance
* @param ip IP address
*
* @return 0 if success, otherwise errorcode
*/
int net_rm_address(struct network *net, const struct sa *ip)
{
struct le *le;
if (!net)
return EINVAL;
LIST_FOREACH(&net->laddrs, le) {
struct laddr *laddr = le->data;
if (sa_cmp(&laddr->sa, ip, SA_ADDR)) {
mem_deref(laddr);
break;
}
}
return 0;
}
/**
* Remove all local IP addresses
*
* @param net Network instance
*
* @return 0 if success, otherwise errorcode
*/
int net_flush_addresses(struct network *net)
{
struct le *le;
if (!net)
return EINVAL;
le = list_head(&net->laddrs);
while (le) {
struct laddr *laddr = le->data;
le = le->next;
mem_deref(laddr);
}
return 0;
}
/**
* Print DNS server debug information
*
* @param pf Print handler for debug output
* @param net Network instance
*
* @return 0 if success, otherwise errorcode
*/
int net_dns_debug(struct re_printf *pf, const struct network *net)
{
struct sa nsv[NET_MAX_NS];
uint32_t i, nsn = RE_ARRAY_SIZE(nsv);
bool from_sys = false;
int err;
if (!net)
return 0;
err = net_dns_srv_get(net, nsv, &nsn, &from_sys);
if (err)
nsn = 0;
err = re_hprintf(pf, " DNS Servers from %s%s: (%u)\n",
from_sys ? "System" : "Config",
net->cfg.use_getaddrinfo ? "(+getaddrinfo)" : "",
nsn);
for (i=0; i<nsn; i++)
err |= re_hprintf(pf, " %u: %J\n", i, &nsv[i]);
return err;
}
/**
* Set the enabled address family (AF)
*
* @param net Network instance
* @param af Enabled address family
*
* @return 0 if success, otherwise errorcode
*/
int net_set_af(struct network *net, int af)
{
if (af != AF_INET && af != AF_INET6 && af != AF_UNSPEC)
return EAFNOSUPPORT;
if (net)
net->cfg.af = af;
return 0;
}
bool net_ifaddr_filter(const struct network *net, const char *ifname,
const struct sa *sa)
{
const struct config_net *cfg = &net->cfg;
struct sa ip;
if (!sa_isset(sa, SA_ADDR))
return false;
if (sa_is_linklocal(sa) && !cfg->use_linklocal)
return false;
if (str_isset(cfg->ifname) && 0 == sa_set_str(&ip, cfg->ifname, 0) &&
sa_cmp(&ip, sa, SA_ADDR))
return true;
if (str_isset(cfg->ifname) && str_cmp(cfg->ifname, ifname))
return false;
if (!net_af_enabled(net, sa_af(sa)))
return false;
if (sa_is_loopback(sa))
return false;
return true;
}
/**
* Get the local IP Address for a specific Address Family (AF)
*
* @param net Network instance
* @param af Address Family
*
* @return Local IP Address
*/
const struct sa *net_laddr_af(const struct network *net, int af)
{
const struct sa *sa;
sa = find_laddr_af(net, af, LADDR_NOLINKLOCAL | LADDR_INTERNET);
if (sa)
return sa;
sa = find_laddr_af(net, af, LADDR_NOLINKLOCAL);
if (sa)
return sa;
sa = find_laddr_af(net, af, 0);
return sa;
}
const struct sa *net_laddr_for(const struct network *net,
const struct sa *dst)
{
struct le *le;
if (!net || !sa_isset(dst, SA_ADDR))
return NULL;
LIST_FOREACH(&net->laddrs, le) {
struct laddr *laddr = le->data;
if (sa_af(&laddr->sa) != sa_af(dst))
continue;
if (check_route(&laddr->sa, dst))
continue;
return &laddr->sa;
}
return NULL;
}
static bool laddr_cmp(struct le *le, void *arg)
{
struct laddr *laddr = le->data;
struct sa *sa = arg;
return sa_cmp(&laddr->sa, sa, SA_ADDR);
}
static int net_dst_is_source_addr(const struct sa *dst, const struct sa *ip)
{
struct sa src;
int err;
err = net_dst_source_addr_get(dst, &src);
if (err)
return err;
if (!sa_cmp(ip, &src, SA_ADDR))
return ECONNREFUSED;
return 0;
}
/**
* Checks if given IP address is a local address.
*
* @param net Network instance
* @param sa IP address to check
*
* @return true if sa is a local address, false if not
*/
bool net_is_laddr(const struct network *net, struct sa *sa)
{
return NULL != list_apply(&net->laddrs, true, laddr_cmp, sa);
}
int net_set_dst_scopeid(const struct network *net, struct sa *dst)
{
struct le *le;
struct sa dstc;
if (!net || !dst)
return EINVAL;
sa_cpy(&dstc, dst);
LIST_FOREACH(&net->laddrs, le) {
struct laddr *laddr = le->data;
struct sa *sa = &laddr->sa;
if (sa_af(sa) != AF_INET6 || !sa_is_linklocal(sa))
continue;
sa_set_scopeid(&dstc, sa_scopeid(&laddr->sa));
if (!net_dst_is_source_addr(&dstc, &laddr->sa)) {
sa_cpy(dst, &dstc);
return 0;
}
}
return ECONNREFUSED;
}
/**
* Get the DNS Client
*
* @param net Network instance
*
* @return DNS Client
*/
struct dnsc *net_dnsc(const struct network *net)
{
if (!net)
return NULL;
return net->dnsc;
}
bool net_laddr_apply(const struct network *net, net_ifaddr_h *ifh, void *arg)
{
struct le *le;
if (!net || !ifh)
return true;
LIST_FOREACH(&net->laddrs, le) {
struct laddr *laddr = le->data;
if (ifh(laddr->ifname, &laddr->sa, arg))
return true;
}
return false;
}
/**
* Print networking debug information
*
* @param pf Print handler for debug output
* @param net Network instance
*
* @return 0 if success, otherwise errorcode
*/
int net_debug(struct re_printf *pf, const struct network *net)
{
void *argv[2] = {pf, (void *)net};
int err;
if (!net)
return 0;
err = re_hprintf(pf, "--- Network debug ---\n");
err |= re_hprintf(pf, "enabled interfaces:\n");
net_laddr_apply(net, if_debug_handler, argv);
err |= net_dns_debug(pf, net);
return err;
}