1
/* $Id: sock_common.c 3841 2011-10-24 09:28:13Z ming $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include <pj/assert.h>
24
#include <pj/ip_helper.h>
26
#include <pj/addr_resolv.h>
27
#include <pj/string.h>
28
#include <pj/compat/socket.h>
31
/* Enable some tracing */
33
#define THIS_FILE "sock_common.c"
34
#define TRACE_(arg) PJ_LOG(4,arg)
41
* Convert address string with numbers and dots to binary IP address.
43
PJ_DEF(pj_in_addr) pj_inet_addr(const pj_str_t *cp)
47
pj_inet_aton(cp, &addr);
52
* Convert address string with numbers and dots to binary IP address.
54
PJ_DEF(pj_in_addr) pj_inet_addr2(const char *cp)
56
pj_str_t str = pj_str((char*)cp);
57
return pj_inet_addr(&str);
61
* Get text representation.
63
PJ_DEF(char*) pj_inet_ntop2( int af, const void *src,
68
status = pj_inet_ntop(af, src, dst, size);
69
return (status==PJ_SUCCESS)? dst : NULL;
73
* Print socket address.
75
PJ_DEF(char*) pj_sockaddr_print( const pj_sockaddr_t *addr,
84
char txt[PJ_INET6_ADDRSTRLEN];
86
const pj_addr_hdr *h = (const pj_addr_hdr*)addr;
87
char *bquote, *equote;
90
status = pj_inet_ntop(h->sa_family, pj_sockaddr_get_addr(addr),
92
if (status != PJ_SUCCESS)
95
if (h->sa_family != PJ_AF_INET6 || (flags & WITH_BRACKETS)==0) {
96
bquote = ""; equote = "";
98
bquote = "["; equote = "]";
101
if (flags & WITH_PORT) {
102
pj_ansi_snprintf(port, sizeof(port), ":%d",
103
pj_sockaddr_get_port(addr));
108
pj_ansi_snprintf(buf, size, "%s%s%s%s",
109
bquote, txt, equote, port);
115
* Set the IP address of an IP socket address from string address,
116
* with resolving the host if necessary. The string address may be in a
117
* standard numbers and dots notation or may be a hostname. If hostname
118
* is specified, then the function will resolve the host into the IP
121
PJ_DEF(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,
122
const pj_str_t *str_addr)
126
PJ_ASSERT_RETURN(!str_addr || str_addr->slen < PJ_MAX_HOSTNAME,
127
(addr->sin_addr.s_addr=PJ_INADDR_NONE, PJ_EINVAL));
129
PJ_SOCKADDR_RESET_LEN(addr);
130
addr->sin_family = AF_INET;
131
pj_bzero(addr->sin_zero, sizeof(addr->sin_zero));
133
if (str_addr && str_addr->slen) {
134
addr->sin_addr = pj_inet_addr(str_addr);
135
if (addr->sin_addr.s_addr == PJ_INADDR_NONE) {
139
rc = pj_gethostbyname(str_addr, &he);
141
addr->sin_addr.s_addr = *(pj_uint32_t*)he.h_addr;
143
addr->sin_addr.s_addr = PJ_INADDR_NONE;
149
addr->sin_addr.s_addr = 0;
155
/* Set address from a name */
156
PJ_DEF(pj_status_t) pj_sockaddr_set_str_addr(int af,
158
const pj_str_t *str_addr)
162
if (af == PJ_AF_INET) {
163
return pj_sockaddr_in_set_str_addr(&addr->ipv4, str_addr);
166
PJ_ASSERT_RETURN(af==PJ_AF_INET6, PJ_EAFNOTSUP);
170
addr->ipv6.sin6_family = PJ_AF_INET6;
171
PJ_SOCKADDR_RESET_LEN(addr);
173
if (str_addr && str_addr->slen) {
174
status = pj_inet_pton(PJ_AF_INET6, str_addr, &addr->ipv6.sin6_addr);
175
if (status != PJ_SUCCESS) {
179
status = pj_getaddrinfo(PJ_AF_INET6, str_addr, &count, &ai);
180
if (status==PJ_SUCCESS) {
181
pj_memcpy(&addr->ipv6.sin6_addr, &ai.ai_addr.ipv6.sin6_addr,
182
sizeof(pj_sockaddr_in6));
193
* Set the IP address and port of an IP socket address.
194
* The string address may be in a standard numbers and dots notation or
195
* may be a hostname. If hostname is specified, then the function will
196
* resolve the host into the IP address.
198
PJ_DEF(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,
199
const pj_str_t *str_addr,
202
PJ_ASSERT_RETURN(addr, (addr->sin_addr.s_addr=PJ_INADDR_NONE, PJ_EINVAL));
204
PJ_SOCKADDR_RESET_LEN(addr);
205
addr->sin_family = PJ_AF_INET;
206
pj_bzero(addr->sin_zero, sizeof(addr->sin_zero));
207
pj_sockaddr_in_set_port(addr, port);
208
return pj_sockaddr_in_set_str_addr(addr, str_addr);
212
* Initialize IP socket address based on the address and port info.
214
PJ_DEF(pj_status_t) pj_sockaddr_init(int af,
221
if (af == PJ_AF_INET) {
222
return pj_sockaddr_in_init(&addr->ipv4, cp, port);
226
PJ_ASSERT_RETURN(af==PJ_AF_INET6, PJ_EAFNOTSUP);
228
pj_bzero(addr, sizeof(pj_sockaddr_in6));
229
addr->addr.sa_family = PJ_AF_INET6;
231
status = pj_sockaddr_set_str_addr(af, addr, cp);
232
if (status != PJ_SUCCESS)
235
addr->ipv6.sin6_port = pj_htons(port);
240
* Compare two socket addresses.
242
PJ_DEF(int) pj_sockaddr_cmp( const pj_sockaddr_t *addr1,
243
const pj_sockaddr_t *addr2)
245
const pj_sockaddr *a1 = (const pj_sockaddr*) addr1;
246
const pj_sockaddr *a2 = (const pj_sockaddr*) addr2;
250
/* Compare address family */
251
if (a1->addr.sa_family < a2->addr.sa_family)
253
else if (a1->addr.sa_family > a2->addr.sa_family)
256
/* Compare addresses */
257
result = pj_memcmp(pj_sockaddr_get_addr(a1),
258
pj_sockaddr_get_addr(a2),
259
pj_sockaddr_get_addr_len(a1));
263
/* Compare port number */
264
port1 = pj_sockaddr_get_port(a1);
265
port2 = pj_sockaddr_get_port(a2);
269
else if (port1 > port2)
273
* Do we need to compare flow label and scope id in IPv6?
281
* Get first IP address associated with the hostname.
283
PJ_DEF(pj_in_addr) pj_gethostaddr(void)
286
const pj_str_t *hostname = pj_gethostname();
288
pj_sockaddr_in_set_str_addr(&addr, hostname);
289
return addr.sin_addr;
293
* Get port number of a pj_sockaddr_in
295
PJ_DEF(pj_uint16_t) pj_sockaddr_in_get_port(const pj_sockaddr_in *addr)
297
return pj_ntohs(addr->sin_port);
301
* Get the address part
303
PJ_DEF(void*) pj_sockaddr_get_addr(const pj_sockaddr_t *addr)
305
const pj_sockaddr *a = (const pj_sockaddr*)addr;
307
PJ_ASSERT_RETURN(a->addr.sa_family == PJ_AF_INET ||
308
a->addr.sa_family == PJ_AF_INET6, NULL);
310
if (a->addr.sa_family == PJ_AF_INET6)
311
return (void*) &a->ipv6.sin6_addr;
313
return (void*) &a->ipv4.sin_addr;
317
* Check if sockaddr contains a non-zero address
319
PJ_DEF(pj_bool_t) pj_sockaddr_has_addr(const pj_sockaddr_t *addr)
321
const pj_sockaddr *a = (const pj_sockaddr*)addr;
323
/* It's probably not wise to raise assertion here if
324
* the address doesn't contain a valid address family, and
325
* just return PJ_FALSE instead.
327
* The reason is because application may need to distinguish
328
* these three conditions with sockaddr:
329
* a) sockaddr is not initialized. This is by convention
330
* indicated by sa_family==0.
331
* b) sockaddr is initialized with zero address. This is
332
* indicated with the address field having zero address.
333
* c) sockaddr is initialized with valid address/port.
335
* If we enable this assertion, then application will loose
336
* the capability to specify condition a), since it will be
337
* forced to always initialize sockaddr (even with zero address).
338
* This may break some parts of upper layer libraries.
340
//PJ_ASSERT_RETURN(a->addr.sa_family == PJ_AF_INET ||
341
// a->addr.sa_family == PJ_AF_INET6, PJ_FALSE);
343
if (a->addr.sa_family!=PJ_AF_INET && a->addr.sa_family!=PJ_AF_INET6) {
345
} else if (a->addr.sa_family == PJ_AF_INET6) {
347
pj_bzero(zero, sizeof(zero));
348
return pj_memcmp(a->ipv6.sin6_addr.s6_addr, zero,
349
sizeof(pj_in6_addr)) != 0;
351
return a->ipv4.sin_addr.s_addr != PJ_INADDR_ANY;
357
PJ_DEF(pj_uint16_t) pj_sockaddr_get_port(const pj_sockaddr_t *addr)
359
const pj_sockaddr *a = (const pj_sockaddr*) addr;
361
PJ_ASSERT_RETURN(a->addr.sa_family == PJ_AF_INET ||
362
a->addr.sa_family == PJ_AF_INET6, (pj_uint16_t)0xFFFF);
364
return pj_ntohs((pj_uint16_t)(a->addr.sa_family == PJ_AF_INET6 ?
365
a->ipv6.sin6_port : a->ipv4.sin_port));
369
* Get the length of the address part.
371
PJ_DEF(unsigned) pj_sockaddr_get_addr_len(const pj_sockaddr_t *addr)
373
const pj_sockaddr *a = (const pj_sockaddr*) addr;
374
PJ_ASSERT_RETURN(a->addr.sa_family == PJ_AF_INET ||
375
a->addr.sa_family == PJ_AF_INET6, 0);
376
return a->addr.sa_family == PJ_AF_INET6 ?
377
sizeof(pj_in6_addr) : sizeof(pj_in_addr);
381
* Get socket address length.
383
PJ_DEF(unsigned) pj_sockaddr_get_len(const pj_sockaddr_t *addr)
385
const pj_sockaddr *a = (const pj_sockaddr*) addr;
386
PJ_ASSERT_RETURN(a->addr.sa_family == PJ_AF_INET ||
387
a->addr.sa_family == PJ_AF_INET6, 0);
388
return a->addr.sa_family == PJ_AF_INET6 ?
389
sizeof(pj_sockaddr_in6) : sizeof(pj_sockaddr_in);
393
* Copy only the address part (sin_addr/sin6_addr) of a socket address.
395
PJ_DEF(void) pj_sockaddr_copy_addr( pj_sockaddr *dst,
396
const pj_sockaddr *src)
398
/* Destination sockaddr might not be initialized */
399
const char *srcbuf = (char*)pj_sockaddr_get_addr(src);
400
char *dstbuf = ((char*)dst) + (srcbuf - (char*)src);
401
pj_memcpy(dstbuf, srcbuf, pj_sockaddr_get_addr_len(src));
405
* Copy socket address.
407
PJ_DEF(void) pj_sockaddr_cp(pj_sockaddr_t *dst, const pj_sockaddr_t *src)
409
pj_memcpy(dst, src, pj_sockaddr_get_len(src));
413
* Set port number of pj_sockaddr_in
415
PJ_DEF(void) pj_sockaddr_in_set_port(pj_sockaddr_in *addr,
416
pj_uint16_t hostport)
418
addr->sin_port = pj_htons(hostport);
422
* Set port number of pj_sockaddr
424
PJ_DEF(pj_status_t) pj_sockaddr_set_port(pj_sockaddr *addr,
425
pj_uint16_t hostport)
427
int af = addr->addr.sa_family;
429
PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL);
431
if (af == PJ_AF_INET6)
432
addr->ipv6.sin6_port = pj_htons(hostport);
434
addr->ipv4.sin_port = pj_htons(hostport);
442
PJ_DEF(pj_in_addr) pj_sockaddr_in_get_addr(const pj_sockaddr_in *addr)
445
in_addr.s_addr = pj_ntohl(addr->sin_addr.s_addr);
452
PJ_DEF(void) pj_sockaddr_in_set_addr(pj_sockaddr_in *addr,
453
pj_uint32_t hostaddr)
455
addr->sin_addr.s_addr = pj_htonl(hostaddr);
461
PJ_DEF(pj_status_t) pj_sockaddr_parse2(int af, unsigned options,
463
pj_str_t *p_hostpart,
467
const char *end = str->ptr + str->slen;
468
const char *last_colon_pos = NULL;
469
unsigned colon_cnt = 0;
472
PJ_ASSERT_RETURN((af==PJ_AF_INET || af==PJ_AF_INET6 || af==PJ_AF_UNSPEC) &&
474
str!=NULL, PJ_EINVAL);
476
/* Special handling for empty input */
477
if (str->slen==0 || str->ptr==NULL) {
479
p_hostpart->slen = 0;
487
/* Count the colon and get the last colon */
488
for (p=str->ptr; p!=end; ++p) {
495
/* Deduce address family if it's not given */
496
if (af == PJ_AF_UNSPEC) {
501
} else if (af == PJ_AF_INET && colon_cnt > 1)
507
if (af == PJ_AF_INET) {
508
/* Parse as IPv4. Supported formats:
518
hostpart.ptr = (char*)str->ptr;
520
if (last_colon_pos) {
524
hostpart.slen = last_colon_pos - str->ptr;
526
port_part.ptr = (char*)last_colon_pos + 1;
527
port_part.slen = end - port_part.ptr;
529
/* Make sure port number is valid */
530
for (i=0; i<port_part.slen; ++i) {
531
if (!pj_isdigit(port_part.ptr[i]))
534
port = pj_strtoul(&port_part);
538
hostpart.slen = str->slen;
543
*p_hostpart = hostpart;
545
*p_port = (pj_uint16_t)port;
549
} else if (af == PJ_AF_INET6) {
551
/* Parse as IPv6. Supported formats:
552
* - "fe::01:80" ==> note: port number is zero in this case, not 80!
565
pj_str_t hostpart, port_part;
567
if (*str->ptr == '[') {
572
if (last_colon_pos == NULL)
575
end_bracket = pj_strchr(str, ']');
576
if (end_bracket == NULL)
579
hostpart.ptr = (char*)str->ptr + 1;
580
hostpart.slen = end_bracket - hostpart.ptr;
582
if (last_colon_pos < end_bracket) {
583
port_part.ptr = NULL;
586
port_part.ptr = (char*)last_colon_pos + 1;
587
port_part.slen = end - port_part.ptr;
590
/* Make sure port number is valid */
591
for (i=0; i<port_part.slen; ++i) {
592
if (!pj_isdigit(port_part.ptr[i]))
595
port = pj_strtoul(&port_part);
600
*p_hostpart = hostpart;
602
*p_port = (pj_uint16_t)port;
607
/* Treat everything as part of the IPv6 IP address */
625
PJ_DEF(pj_status_t) pj_sockaddr_parse( int af, unsigned options,
633
PJ_ASSERT_RETURN(addr, PJ_EINVAL);
634
PJ_ASSERT_RETURN(af==PJ_AF_UNSPEC ||
636
af==PJ_AF_INET6, PJ_EINVAL);
637
PJ_ASSERT_RETURN(options == 0, PJ_EINVAL);
639
status = pj_sockaddr_parse2(af, options, str, &hostpart, &port, &af);
640
if (status != PJ_SUCCESS)
643
#if !defined(PJ_HAS_IPV6) || !PJ_HAS_IPV6
645
return PJ_EIPV6NOTSUP;
648
status = pj_sockaddr_init(af, addr, &hostpart, port);
649
#if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6
650
if (status != PJ_SUCCESS && af == PJ_AF_INET6) {
651
/* Parsing does not yield valid address. Try to treat the last
652
* portion after the colon as port number.
654
const char *last_colon_pos=NULL, *p;
655
const char *end = str->ptr + str->slen;
656
unsigned long long_port;
660
/* Parse as IPv6:port */
661
for (p=str->ptr; p!=end; ++p) {
666
if (last_colon_pos == NULL)
669
hostpart.ptr = (char*)str->ptr;
670
hostpart.slen = last_colon_pos - str->ptr;
672
port_part.ptr = (char*)last_colon_pos + 1;
673
port_part.slen = end - port_part.ptr;
675
/* Make sure port number is valid */
676
for (i=0; i<port_part.slen; ++i) {
677
if (!pj_isdigit(port_part.ptr[i]))
680
long_port = pj_strtoul(&port_part);
681
if (long_port > 65535)
684
port = (pj_uint16_t)long_port;
686
status = pj_sockaddr_init(PJ_AF_INET6, addr, &hostpart, port);
693
/* Resolve the IP address of local machine */
694
PJ_DEF(pj_status_t) pj_gethostip(int af, pj_sockaddr *addr)
696
unsigned i, count, cand_cnt;
700
/* Weighting to be applied to found addresses */
701
WEIGHT_HOSTNAME = 1, /* hostname IP is not always valid! */
702
WEIGHT_DEF_ROUTE = 2,
703
WEIGHT_INTERFACE = 1,
704
WEIGHT_LOOPBACK = -5,
705
WEIGHT_LINK_LOCAL = -4,
706
WEIGHT_DISABLED = -50,
708
MIN_WEIGHT = WEIGHT_DISABLED+1 /* minimum weight to use */
711
pj_sockaddr cand_addr[CAND_CNT];
712
int cand_weight[CAND_CNT];
714
char strip[PJ_INET6_ADDRSTRLEN+10];
715
/* Special IPv4 addresses. */
723
/* 127.0.0.0/8, loopback addr will be used if there is no other
726
{ 0x7f000000, 0xFF000000, WEIGHT_LOOPBACK },
728
/* 0.0.0.0/8, special IP that doesn't seem to be practically useful */
729
{ 0x00000000, 0xFF000000, WEIGHT_DISABLED },
731
/* 169.254.0.0/16, a zeroconf/link-local address, which has higher
732
* priority than loopback and will be used if there is no other
735
{ 0xa9fe0000, 0xFFFF0000, WEIGHT_LINK_LOCAL }
737
/* Special IPv6 addresses */
745
/* Loopback address, ::1/128 */
746
{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
747
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
748
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
752
/* Link local, fe80::/10 */
753
{ {0xfe,0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
754
{0xff,0xc0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
758
/* Disabled, ::/128 */
759
{ {0x0,0x0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
760
{ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
761
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
768
/* May not be used if TRACE_ is disabled */
769
PJ_UNUSED_ARG(strip);
772
/* Get rid of "uninitialized he variable" with MS compilers */
773
pj_bzero(&ai, sizeof(ai));
777
pj_bzero(cand_addr, sizeof(cand_addr));
778
pj_bzero(cand_weight, sizeof(cand_weight));
779
for (i=0; i<PJ_ARRAY_SIZE(cand_addr); ++i) {
780
cand_addr[i].addr.sa_family = (pj_uint16_t)af;
781
PJ_SOCKADDR_RESET_LEN(&cand_addr[i]);
784
addr->addr.sa_family = (pj_uint16_t)af;
785
PJ_SOCKADDR_RESET_LEN(addr);
787
#if !defined(PJ_GETHOSTIP_DISABLE_LOCAL_RESOLUTION) || \
788
PJ_GETHOSTIP_DISABLE_LOCAL_RESOLUTION == 0
789
/* Get hostname's IP address */
791
status = pj_getaddrinfo(af, pj_gethostname(), &count, &ai);
792
if (status == PJ_SUCCESS) {
793
pj_assert(ai.ai_addr.addr.sa_family == (pj_uint16_t)af);
794
pj_sockaddr_copy_addr(&cand_addr[cand_cnt], &ai.ai_addr);
795
pj_sockaddr_set_port(&cand_addr[cand_cnt], 0);
796
cand_weight[cand_cnt] += WEIGHT_HOSTNAME;
799
TRACE_((THIS_FILE, "hostname IP is %s",
800
pj_sockaddr_print(&ai.ai_addr, strip, sizeof(strip), 0)));
804
PJ_UNUSED_ARG(count);
807
/* Get default interface (interface for default route) */
808
if (cand_cnt < PJ_ARRAY_SIZE(cand_addr)) {
809
status = pj_getdefaultipinterface(af, addr);
810
if (status == PJ_SUCCESS) {
811
TRACE_((THIS_FILE, "default IP is %s",
812
pj_sockaddr_print(addr, strip, sizeof(strip), 0)));
814
pj_sockaddr_set_port(addr, 0);
815
for (i=0; i<cand_cnt; ++i) {
816
if (pj_sockaddr_cmp(&cand_addr[i], addr)==0)
820
cand_weight[i] += WEIGHT_DEF_ROUTE;
822
pj_sockaddr_copy_addr(&cand_addr[i], addr);
829
/* Enumerate IP interfaces */
830
if (cand_cnt < PJ_ARRAY_SIZE(cand_addr)) {
831
unsigned start_if = cand_cnt;
832
unsigned count = PJ_ARRAY_SIZE(cand_addr) - start_if;
834
status = pj_enum_ip_interface(af, &count, &cand_addr[start_if]);
835
if (status == PJ_SUCCESS && count) {
836
/* Clear the port number */
837
for (i=0; i<count; ++i)
838
pj_sockaddr_set_port(&cand_addr[start_if+i], 0);
840
/* For each candidate that we found so far (that is the hostname
841
* address and default interface address, check if they're found
842
* in the interface list. If found, add the weight, and if not,
843
* decrease the weight.
845
for (i=0; i<cand_cnt; ++i) {
847
for (j=0; j<count; ++j) {
848
if (pj_sockaddr_cmp(&cand_addr[i],
849
&cand_addr[start_if+j])==0)
855
cand_weight[i] -= WEIGHT_INTERFACE;
857
cand_weight[i] += WEIGHT_INTERFACE;
861
/* Add remaining interface to candidate list. */
862
for (i=0; i<count; ++i) {
864
for (j=0; j<cand_cnt; ++j) {
865
if (pj_sockaddr_cmp(&cand_addr[start_if+i],
871
pj_sockaddr_copy_addr(&cand_addr[cand_cnt],
872
&cand_addr[start_if+i]);
873
cand_weight[cand_cnt] += WEIGHT_INTERFACE;
880
/* Apply weight adjustment for special IPv4/IPv6 addresses
881
* See http://trac.pjsip.org/repos/ticket/1046
883
if (af == PJ_AF_INET) {
884
for (i=0; i<cand_cnt; ++i) {
886
for (j=0; j<PJ_ARRAY_SIZE(spec_ipv4); ++j) {
887
pj_uint32_t a = pj_ntohl(cand_addr[i].ipv4.sin_addr.s_addr);
888
pj_uint32_t pa = spec_ipv4[j].addr;
889
pj_uint32_t pm = spec_ipv4[j].mask;
891
if ((a & pm) == pa) {
892
cand_weight[i] += spec_ipv4[j].weight;
897
} else if (af == PJ_AF_INET6) {
898
for (i=0; i<PJ_ARRAY_SIZE(spec_ipv6); ++i) {
900
for (j=0; j<cand_cnt; ++j) {
901
pj_uint8_t *a = cand_addr[j].ipv6.sin6_addr.s6_addr;
903
pj_uint8_t *pa = spec_ipv6[i].addr;
904
pj_uint8_t *pm = spec_ipv6[i].mask;
907
for (k=0; k<16; ++k) {
908
am[k] = (pj_uint8_t)((a[k] & pm[k]) & 0xFF);
911
if (pj_memcmp(am, pa, 16)==0) {
912
cand_weight[j] += spec_ipv6[i].weight;
920
/* Enumerate candidates to get the best IP address to choose */
922
for (i=0; i<cand_cnt; ++i) {
923
TRACE_((THIS_FILE, "Checking candidate IP %s, weight=%d",
924
pj_sockaddr_print(&cand_addr[i], strip, sizeof(strip), 0),
927
if (cand_weight[i] < MIN_WEIGHT) {
931
if (selected_cand == -1)
933
else if (cand_weight[i] > cand_weight[selected_cand])
937
/* If else fails, returns loopback interface as the last resort */
938
if (selected_cand == -1) {
939
if (af==PJ_AF_INET) {
940
addr->ipv4.sin_addr.s_addr = pj_htonl (0x7f000001);
942
pj_in6_addr *s6_addr;
944
s6_addr = (pj_in6_addr*) pj_sockaddr_get_addr(addr);
945
pj_bzero(s6_addr, sizeof(pj_in6_addr));
946
s6_addr->s6_addr[15] = 1;
948
TRACE_((THIS_FILE, "Loopback IP %s returned",
949
pj_sockaddr_print(addr, strip, sizeof(strip), 0)));
951
pj_sockaddr_copy_addr(addr, &cand_addr[selected_cand]);
952
TRACE_((THIS_FILE, "Candidate %s selected",
953
pj_sockaddr_print(addr, strip, sizeof(strip), 0)));
959
/* Get the default IP interface */
960
PJ_DEF(pj_status_t) pj_getdefaultipinterface(int af, pj_sockaddr *addr)
969
addr->addr.sa_family = (pj_uint16_t)af;
971
status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &fd);
972
if (status != PJ_SUCCESS) {
976
if (af == PJ_AF_INET) {
977
cp = pj_str("1.1.1.1");
981
status = pj_sockaddr_init(af, &a, &cp, 53);
982
if (status != PJ_SUCCESS) {
987
status = pj_sock_connect(fd, &a, pj_sockaddr_get_len(&a));
988
if (status != PJ_SUCCESS) {
994
status = pj_sock_getsockname(fd, &a, &len);
995
if (status != PJ_SUCCESS) {
1002
/* Check that the address returned is not zero */
1003
pj_bzero(zero, sizeof(zero));
1004
if (pj_memcmp(pj_sockaddr_get_addr(&a), zero,
1005
pj_sockaddr_get_addr_len(&a))==0)
1007
return PJ_ENOTFOUND;
1010
pj_sockaddr_copy_addr(addr, &a);
1017
/* Only need to implement these in DLL build */
1020
PJ_DEF(pj_uint16_t) pj_AF_UNSPEC(void)
1022
return PJ_AF_UNSPEC;
1025
PJ_DEF(pj_uint16_t) pj_AF_UNIX(void)
1030
PJ_DEF(pj_uint16_t) pj_AF_INET(void)
1035
PJ_DEF(pj_uint16_t) pj_AF_INET6(void)
1040
PJ_DEF(pj_uint16_t) pj_AF_PACKET(void)
1042
return PJ_AF_PACKET;
1045
PJ_DEF(pj_uint16_t) pj_AF_IRDA(void)
1050
PJ_DEF(int) pj_SOCK_STREAM(void)
1052
return PJ_SOCK_STREAM;
1055
PJ_DEF(int) pj_SOCK_DGRAM(void)
1057
return PJ_SOCK_DGRAM;
1060
PJ_DEF(int) pj_SOCK_RAW(void)
1065
PJ_DEF(int) pj_SOCK_RDM(void)
1070
PJ_DEF(pj_uint16_t) pj_SOL_SOCKET(void)
1072
return PJ_SOL_SOCKET;
1075
PJ_DEF(pj_uint16_t) pj_SOL_IP(void)
1080
PJ_DEF(pj_uint16_t) pj_SOL_TCP(void)
1085
PJ_DEF(pj_uint16_t) pj_SOL_UDP(void)
1090
PJ_DEF(pj_uint16_t) pj_SOL_IPV6(void)
1095
PJ_DEF(int) pj_IP_TOS(void)
1100
PJ_DEF(int) pj_IPTOS_LOWDELAY(void)
1102
return PJ_IPTOS_LOWDELAY;
1105
PJ_DEF(int) pj_IPTOS_THROUGHPUT(void)
1107
return PJ_IPTOS_THROUGHPUT;
1110
PJ_DEF(int) pj_IPTOS_RELIABILITY(void)
1112
return PJ_IPTOS_RELIABILITY;
1115
PJ_DEF(int) pj_IPTOS_MINCOST(void)
1117
return PJ_IPTOS_MINCOST;
1120
PJ_DEF(pj_uint16_t) pj_SO_TYPE(void)
1125
PJ_DEF(pj_uint16_t) pj_SO_RCVBUF(void)
1127
return PJ_SO_RCVBUF;
1130
PJ_DEF(pj_uint16_t) pj_SO_SNDBUF(void)
1132
return PJ_SO_SNDBUF;
1135
PJ_DEF(pj_uint16_t) pj_TCP_NODELAY(void)
1137
return PJ_TCP_NODELAY;
1140
PJ_DEF(pj_uint16_t) pj_SO_REUSEADDR(void)
1142
return PJ_SO_REUSEADDR;
1145
PJ_DEF(pj_uint16_t) pj_SO_NOSIGPIPE(void)
1147
return PJ_SO_NOSIGPIPE;
1150
PJ_DEF(pj_uint16_t) pj_SO_PRIORITY(void)
1152
return PJ_SO_PRIORITY;
1155
PJ_DEF(pj_uint16_t) pj_IP_MULTICAST_IF(void)
1157
return PJ_IP_MULTICAST_IF;
1160
PJ_DEF(pj_uint16_t) pj_IP_MULTICAST_TTL(void)
1162
return PJ_IP_MULTICAST_TTL;
1165
PJ_DEF(pj_uint16_t) pj_IP_MULTICAST_LOOP(void)
1167
return PJ_IP_MULTICAST_LOOP;
1170
PJ_DEF(pj_uint16_t) pj_IP_ADD_MEMBERSHIP(void)
1172
return PJ_IP_ADD_MEMBERSHIP;
1175
PJ_DEF(pj_uint16_t) pj_IP_DROP_MEMBERSHIP(void)
1177
return PJ_IP_DROP_MEMBERSHIP;
1180
PJ_DEF(int) pj_MSG_OOB(void)
1185
PJ_DEF(int) pj_MSG_PEEK(void)
1190
PJ_DEF(int) pj_MSG_DONTROUTE(void)
1192
return PJ_MSG_DONTROUTE;