Debian Codegraph @

Xpjproject/pjlib/src/pj/sock_common.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pj/sock.h>
20 #include <pj/assert.h>
21 #include <pj/ctype.h>
22 #include <pj/errno.h>
23 #include <pj/ip_helper.h>
24 #include <pj/os.h>
25 #include <pj/pool.h>
26 #include <pj/addr_resolv.h>
27 #include <pj/rand.h>
28 #include <pj/string.h>
29 #include <pj/compat/socket.h>
30
31 #if 0
32 /* Enable some tracing */
33 #include <pj/log.h>
34 #define THIS_FILE "sock_common.c"
35 #define TRACE_(arg) PJ_LOG(4,arg)
36 #else
37 #define TRACE_(arg)
38 #endif
39
40
41 /*
42 * Convert address string with numbers and dots to binary IP address.
43 */
44 PJ_DEF(pj_in_addr) pj_inet_addr(const pj_str_t *cp)
45 {
46 pj_in_addr addr;
47
48 pj_inet_aton(cp, &addr);
49 return addr;
50 }
51
52 /*
53 * Convert address string with numbers and dots to binary IP address.
54 */
55 PJ_DEF(pj_in_addr) pj_inet_addr2(const char *cp)
56 {
57 pj_str_t str = pj_str((char*)cp);
58 return pj_inet_addr(&str);
59 }
60
61 /*
62 * Get text representation.
63 */
64 PJ_DEF(char*) pj_inet_ntop2( int af, const void *src,
65 char *dst, int size)
66 {
67 pj_status_t status;
68
69 status = pj_inet_ntop(af, src, dst, size);
70 return (status==PJ_SUCCESS)? dst : NULL;
71 }
72
73 /*
74 * Print socket address.
75 */
76 PJ_DEF(char*) pj_sockaddr_print( const pj_sockaddr_t *addr,
77 char *buf, int size,
78 unsigned flags)
79 {
80 enum {
81 WITH_PORT = 1,
82 WITH_BRACKETS = 2
83 };
84
85 char txt[PJ_INET6_ADDRSTRLEN];
86 char port[32];
87 const pj_addr_hdr *h = (const pj_addr_hdr*)addr;
88 char *bquote, *equote;
89 pj_status_t status;
90
91 status = pj_inet_ntop(h->sa_family, pj_sockaddr_get_addr(addr),
92 txt, sizeof(txt));
93 if (status != PJ_SUCCESS)
94 return "";
95
96 if (h->sa_family != PJ_AF_INET6 || (flags & WITH_BRACKETS)==0) {
97 bquote = ""; equote = "";
98 } else {
99 bquote = "["; equote = "]";
100 }
101
102 if (flags & WITH_PORT) {
103 pj_ansi_snprintf(port, sizeof(port), ":%d",
104 pj_sockaddr_get_port(addr));
105 } else {
106 port[0] = '\0';
107 }
108
109 pj_ansi_snprintf(buf, size, "%s%s%s%s",
110 bquote, txt, equote, port);
111
112 return buf;
113 }
114
115 /*
116 * Set the IP address of an IP socket address from string address,
117 * with resolving the host if necessary. The string address may be in a
118 * standard numbers and dots notation or may be a hostname. If hostname
119 * is specified, then the function will resolve the host into the IP
120 * address.
121 */
122 PJ_DEF(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,
123 const pj_str_t *str_addr)
124 {
125 PJ_CHECK_STACK();
126
127 PJ_ASSERT_RETURN(!str_addr || str_addr->slen < PJ_MAX_HOSTNAME,
128 (addr->sin_addr.s_addr=PJ_INADDR_NONE, PJ_EINVAL));
129
130 PJ_SOCKADDR_RESET_LEN(addr);
131 addr->sin_family = PJ_AF_INET;
132 pj_bzero(addr->sin_zero_pad, sizeof(addr->sin_zero_pad));
133
134 if (str_addr && str_addr->slen) {
135 addr->sin_addr = pj_inet_addr(str_addr);
136 if (addr->sin_addr.s_addr == PJ_INADDR_NONE) {
137 pj_addrinfo ai;
138 unsigned count = 1;
139 pj_status_t status;
140
141 status = pj_getaddrinfo(pj_AF_INET(), str_addr, &count, &ai);
142 if (status==PJ_SUCCESS) {
143 pj_memcpy(&addr->sin_addr, &ai.ai_addr.ipv4.sin_addr,
144 sizeof(addr->sin_addr));
145 } else {
146 return status;
147 }
148 }
149
150 } else {
151 addr->sin_addr.s_addr = 0;
152 }
153
154 return PJ_SUCCESS;
155 }
156
157 /* Set address from a name */
158 PJ_DEF(pj_status_t) pj_sockaddr_set_str_addr(int af,
159 pj_sockaddr *addr,
160 const pj_str_t *str_addr)
161 {
162 pj_status_t status;
163
164 if (af == PJ_AF_INET) {
165 return pj_sockaddr_in_set_str_addr(&addr->ipv4, str_addr);
166 }
167
168 PJ_ASSERT_RETURN(af==PJ_AF_INET6, PJ_EAFNOTSUP);
169
170 /* IPv6 specific */
171
172 addr->ipv6.sin6_family = PJ_AF_INET6;
173 PJ_SOCKADDR_RESET_LEN(addr);
174
175 if (str_addr && str_addr->slen) {
176 #if defined(PJ_SOCKADDR_USE_GETADDRINFO) && PJ_SOCKADDR_USE_GETADDRINFO!=0
177 if (1) {
178 #else
179 status = pj_inet_pton(PJ_AF_INET6, str_addr, &addr->ipv6.sin6_addr);
180 if (status != PJ_SUCCESS) {
181 #endif
182 pj_addrinfo ai;
183 unsigned count = 1;
184
185 status = pj_getaddrinfo(PJ_AF_INET6, str_addr, &count, &ai);
186 if (status==PJ_SUCCESS) {
187 pj_memcpy(&addr->ipv6.sin6_addr, &ai.ai_addr.ipv6.sin6_addr,
188 sizeof(addr->ipv6.sin6_addr));
189 addr->ipv6.sin6_scope_id = ai.ai_addr.ipv6.sin6_scope_id;
190 }
191 }
192 } else {
193 status = PJ_SUCCESS;
194 }
195
196 return status;
197 }
198
199 /*
200 * Set the IP address and port of an IP socket address.
201 * The string address may be in a standard numbers and dots notation or
202 * may be a hostname. If hostname is specified, then the function will
203 * resolve the host into the IP address.
204 */
205 PJ_DEF(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,
206 const pj_str_t *str_addr,
207 pj_uint16_t port)
208 {
209 PJ_ASSERT_RETURN(addr, PJ_EINVAL);
210
211 PJ_SOCKADDR_RESET_LEN(addr);
212 addr->sin_family = PJ_AF_INET;
213 pj_bzero(addr->sin_zero_pad, sizeof(addr->sin_zero_pad));
214 pj_sockaddr_in_set_port(addr, port);
215 return pj_sockaddr_in_set_str_addr(addr, str_addr);
216 }
217
218 /*
219 * Initialize IP socket address based on the address and port info.
220 */
221 PJ_DEF(pj_status_t) pj_sockaddr_init(int af,
222 pj_sockaddr *addr,
223 const pj_str_t *cp,
224 pj_uint16_t port)
225 {
226 pj_status_t status;
227
228 if (af == PJ_AF_INET) {
229 return pj_sockaddr_in_init(&addr->ipv4, cp, port);
230 }
231
232 /* IPv6 specific */
233 PJ_ASSERT_RETURN(af==PJ_AF_INET6, PJ_EAFNOTSUP);
234
235 pj_bzero(addr, sizeof(pj_sockaddr_in6));
236 addr->addr.sa_family = PJ_AF_INET6;
237
238 status = pj_sockaddr_set_str_addr(af, addr, cp);
239 if (status != PJ_SUCCESS)
240 return status;
241
242 addr->ipv6.sin6_port = pj_htons(port);
243 return PJ_SUCCESS;
244 }
245
246 /*
247 * Compare two socket addresses.
248 */
249 PJ_DEF(int) pj_sockaddr_cmp( const pj_sockaddr_t *addr1,
250 const pj_sockaddr_t *addr2)
251 {
252 const pj_sockaddr *a1 = (const pj_sockaddr*) addr1;
253 const pj_sockaddr *a2 = (const pj_sockaddr*) addr2;
254 int port1, port2;
255 int result;
256
257 /* Compare address family */
258 if (a1->addr.sa_family < a2->addr.sa_family)
259 return -1;
260 else if (a1->addr.sa_family > a2->addr.sa_family)
261 return 1;
262
263 /* Compare addresses */
264 result = pj_memcmp(pj_sockaddr_get_addr(a1),
265 pj_sockaddr_get_addr(a2),
266 pj_sockaddr_get_addr_len(a1));
267 if (result != 0)
268 return result;
269
270 /* Compare port number */
271 port1 = pj_sockaddr_get_port(a1);
272 port2 = pj_sockaddr_get_port(a2);
273
274 if (port1 < port2)
275 return -1;
276 else if (port1 > port2)
277 return 1;
278
279 /* TODO:
280 * Do we need to compare flow label and scope id in IPv6?
281 */
282
283 /* Looks equal */
284 return 0;
285 }
286
287 /*
288 * Get first IP address associated with the hostname.
289 */
290 PJ_DEF(pj_in_addr) pj_gethostaddr(void)
291 {
292 pj_sockaddr_in addr;
293 const pj_str_t *hostname = pj_gethostname();
294
295 pj_sockaddr_in_set_str_addr(&addr, hostname);
296 return addr.sin_addr;
297 }
298
299 /*
300 * Get port number of a pj_sockaddr_in
301 */
302 PJ_DEF(pj_uint16_t) pj_sockaddr_in_get_port(const pj_sockaddr_in *addr)
303 {
304 return pj_ntohs(addr->sin_port);
305 }
306
307 /*
308 * Get the address part
309 */
310 PJ_DEF(void*) pj_sockaddr_get_addr(const pj_sockaddr_t *addr)
311 {
312 const pj_sockaddr *a = (const pj_sockaddr*)addr;
313
314 PJ_ASSERT_RETURN(a->addr.sa_family == PJ_AF_INET ||
315 a->addr.sa_family == PJ_AF_INET6, NULL);
316
317 if (a->addr.sa_family == PJ_AF_INET6)
318 return (void*) &a->ipv6.sin6_addr;
319 else
320 return (void*) &a->ipv4.sin_addr;
321 }
322
323 /*
324 * Check if sockaddr contains a non-zero address
325 */
326 PJ_DEF(pj_bool_t) pj_sockaddr_has_addr(const pj_sockaddr_t *addr)
327 {
328 const pj_sockaddr *a = (const pj_sockaddr*)addr;
329
330 /* It's probably not wise to raise assertion here if
331 * the address doesn't contain a valid address family, and
332 * just return PJ_FALSE instead.
333 *
334 * The reason is because application may need to distinguish
335 * these three conditions with sockaddr:
336 * a) sockaddr is not initialized. This is by convention
337 * indicated by sa_family==0.
338 * b) sockaddr is initialized with zero address. This is
339 * indicated with the address field having zero address.
340 * c) sockaddr is initialized with valid address/port.
341 *
342 * If we enable this assertion, then application will loose
343 * the capability to specify condition a), since it will be
344 * forced to always initialize sockaddr (even with zero address).
345 * This may break some parts of upper layer libraries.
346 */
347 //PJ_ASSERT_RETURN(a->addr.sa_family == PJ_AF_INET ||
348 // a->addr.sa_family == PJ_AF_INET6, PJ_FALSE);
349
350 if (a->addr.sa_family!=PJ_AF_INET && a->addr.sa_family!=PJ_AF_INET6) {
351 return PJ_FALSE;
352 } else if (a->addr.sa_family == PJ_AF_INET6) {
353 pj_uint8_t zero[24];
354 pj_bzero(zero, sizeof(zero));
355 return pj_memcmp(a->ipv6.sin6_addr.s6_addr, zero,
356 sizeof(pj_in6_addr)) != 0;
357 } else
358 return a->ipv4.sin_addr.s_addr != PJ_INADDR_ANY;
359 }
360
361 /*
362 * Get port number
363 */
364 PJ_DEF(pj_uint16_t) pj_sockaddr_get_port(const pj_sockaddr_t *addr)
365 {
366 const pj_sockaddr *a = (const pj_sockaddr*) addr;
367
368 PJ_ASSERT_RETURN(a->addr.sa_family == PJ_AF_INET ||
369 a->addr.sa_family == PJ_AF_INET6, (pj_uint16_t)0xFFFF);
370
371 return pj_ntohs((pj_uint16_t)(a->addr.sa_family == PJ_AF_INET6 ?
372 a->ipv6.sin6_port : a->ipv4.sin_port));
373 }
374
375 /*
376 * Get the length of the address part.
377 */
378 PJ_DEF(unsigned) pj_sockaddr_get_addr_len(const pj_sockaddr_t *addr)
379 {
380 const pj_sockaddr *a = (const pj_sockaddr*) addr;
381 PJ_ASSERT_RETURN(a->addr.sa_family == PJ_AF_INET ||
382 a->addr.sa_family == PJ_AF_INET6, 0);
383 return a->addr.sa_family == PJ_AF_INET6 ?
384 sizeof(pj_in6_addr) : sizeof(pj_in_addr);
385 }
386
387 /*
388 * Get socket address length.
389 */
390 PJ_DEF(unsigned) pj_sockaddr_get_len(const pj_sockaddr_t *addr)
391 {
392 const pj_sockaddr *a = (const pj_sockaddr*) addr;
393 PJ_ASSERT_RETURN(a->addr.sa_family == PJ_AF_INET ||
394 a->addr.sa_family == PJ_AF_INET6, 0);
395 return a->addr.sa_family == PJ_AF_INET6 ?
396 sizeof(pj_sockaddr_in6) : sizeof(pj_sockaddr_in);
397 }
398
399 /*
400 * Copy only the address part (sin_addr/sin6_addr) of a socket address.
401 */
402 PJ_DEF(void) pj_sockaddr_copy_addr( pj_sockaddr *dst,
403 const pj_sockaddr *src)
404 {
405 /* Destination sockaddr might not be initialized */
406 const char *srcbuf = (char*)pj_sockaddr_get_addr(src);
407 char *dstbuf = ((char*)dst) + (srcbuf - (char*)src);
408 pj_memcpy(dstbuf, srcbuf, pj_sockaddr_get_addr_len(src));
409 }
410
411 /*
412 * Copy socket address.
413 */
414 PJ_DEF(void) pj_sockaddr_cp(pj_sockaddr_t *dst, const pj_sockaddr_t *src)
415 {
416 pj_memcpy(dst, src, pj_sockaddr_get_len(src));
417 }
418
419 /*
420 * Synthesize address.
421 */
422 PJ_DEF(pj_status_t) pj_sockaddr_synthesize(int dst_af,
423 pj_sockaddr_t *dst,
424 const pj_sockaddr_t *src)
425 {
426 char ip_addr_buf[PJ_INET6_ADDRSTRLEN];
427 unsigned int count = 1;
428 pj_addrinfo ai[1];
429 pj_str_t ip_addr;
430 pj_status_t status;
431
432 /* Validate arguments */
433 PJ_ASSERT_RETURN(src && dst, PJ_EINVAL);
434
435 if (dst_af == ((const pj_sockaddr *)src)->addr.sa_family) {
436 pj_sockaddr_cp(dst, src);
437 return PJ_SUCCESS;
438 }
439
440 pj_sockaddr_print(src, ip_addr_buf, sizeof(ip_addr_buf), 0);
441 ip_addr = pj_str(ip_addr_buf);
442
443 /* Try to synthesize address using pj_getaddrinfo(). */
444 status = pj_getaddrinfo(dst_af, &ip_addr, &count, ai);
445 if (status == PJ_SUCCESS && count > 0) {
446 pj_sockaddr_cp(dst, &ai[0].ai_addr);
447 pj_sockaddr_set_port(dst, pj_sockaddr_get_port(src));
448 }
449
450 return status;
451 }
452
453 /*
454 * Set port number of pj_sockaddr_in
455 */
456 PJ_DEF(void) pj_sockaddr_in_set_port(pj_sockaddr_in *addr,
457 pj_uint16_t hostport)
458 {
459 addr->sin_port = pj_htons(hostport);
460 }
461
462 /*
463 * Set port number of pj_sockaddr
464 */
465 PJ_DEF(pj_status_t) pj_sockaddr_set_port(pj_sockaddr *addr,
466 pj_uint16_t hostport)
467 {
468 int af = addr->addr.sa_family;
469
470 PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL);
471
472 if (af == PJ_AF_INET6)
473 addr->ipv6.sin6_port = pj_htons(hostport);
474 else
475 addr->ipv4.sin_port = pj_htons(hostport);
476
477 return PJ_SUCCESS;
478 }
479
480 /*
481 * Get IPv4 address
482 */
483 PJ_DEF(pj_in_addr) pj_sockaddr_in_get_addr(const pj_sockaddr_in *addr)
484 {
485 pj_in_addr in_addr;
486 in_addr.s_addr = pj_ntohl(addr->sin_addr.s_addr);
487 return in_addr;
488 }
489
490 /*
491 * Set IPv4 address
492 */
493 PJ_DEF(void) pj_sockaddr_in_set_addr(pj_sockaddr_in *addr,
494 pj_uint32_t hostaddr)
495 {
496 addr->sin_addr.s_addr = pj_htonl(hostaddr);
497 }
498
499 /*
500 * Parse address
501 */
502 PJ_DEF(pj_status_t) pj_sockaddr_parse2(int af, unsigned options,
503 const pj_str_t *str,
504 pj_str_t *p_hostpart,
505 pj_uint16_t *p_port,
506 int *raf)
507 {
508 const char *end = str->ptr + str->slen;
509 const char *last_colon_pos = NULL;
510 unsigned colon_cnt = 0;
511 const char *p;
512
513 PJ_ASSERT_RETURN((af==PJ_AF_INET || af==PJ_AF_INET6 || af==PJ_AF_UNSPEC) &&
514 options==0 &&
515 str!=NULL, PJ_EINVAL);
516
517 /* Special handling for empty input */
518 if (str->slen==0 || str->ptr==NULL) {
519 if (p_hostpart)
520 p_hostpart->slen = 0;
521 if (p_port)
522 *p_port = 0;
523 if (raf)
524 *raf = PJ_AF_INET;
525 return PJ_SUCCESS;
526 }
527
528 /* Count the colon and get the last colon */
529 for (p=str->ptr; p!=end; ++p) {
530 if (*p == ':') {
531 ++colon_cnt;
532 last_colon_pos = p;
533 }
534 }
535
536 /* Deduce address family if it's not given */
537 if (af == PJ_AF_UNSPEC) {
538 if (colon_cnt > 1)
539 af = PJ_AF_INET6;
540 else
541 af = PJ_AF_INET;
542 } else if (af == PJ_AF_INET && colon_cnt > 1)
543 return PJ_EINVAL;
544
545 if (raf)
546 *raf = af;
547
548 if (af == PJ_AF_INET) {
549 /* Parse as IPv4. Supported formats:
550 * - "10.0.0.1:80"
551 * - "10.0.0.1"
552 * - "10.0.0.1:"
553 * - ":80"
554 * - ":"
555 */
556 pj_str_t hostpart;
557 unsigned long port;
558
559 hostpart.ptr = (char*)str->ptr;
560
561 if (last_colon_pos) {
562 pj_str_t port_part;
563 int i;
564
565 hostpart.slen = last_colon_pos - str->ptr;
566
567 port_part.ptr = (char*)last_colon_pos + 1;
568 port_part.slen = end - port_part.ptr;
569
570 /* Make sure port number is valid */
571 for (i=0; i<port_part.slen; ++i) {
572 if (!pj_isdigit(port_part.ptr[i]))
573 return PJ_EINVAL;
574 }
575 port = pj_strtoul(&port_part);
576 if (port > 65535)
577 return PJ_EINVAL;
578 } else {
579 hostpart.slen = str->slen;
580 port = 0;
581 }
582
583 if (p_hostpart)
584 *p_hostpart = hostpart;
585 if (p_port)
586 *p_port = (pj_uint16_t)port;
587
588 return PJ_SUCCESS;
589
590 } else if (af == PJ_AF_INET6) {
591
592 /* Parse as IPv6. Supported formats:
593 * - "fe::01:80" ==> note: port number is zero in this case, not 80!
594 * - "[fe::01]:80"
595 * - "fe::01"
596 * - "fe::01:"
597 * - "[fe::01]"
598 * - "[fe::01]:"
599 * - "[::]:80"
600 * - ":::80"
601 * - "[::]"
602 * - "[::]:"
603 * - ":::"
604 * - "::"
605 */
606 pj_str_t hostpart, port_part;
607
608 if (*str->ptr == '[') {
609 char *end_bracket;
610 int i;
611 unsigned long port;
612
613 if (last_colon_pos == NULL)
614 return PJ_EINVAL;
615
616 end_bracket = pj_strchr(str, ']');
617 if (end_bracket == NULL)
618 return PJ_EINVAL;
619
620 hostpart.ptr = (char*)str->ptr + 1;
621 hostpart.slen = end_bracket - hostpart.ptr;
622
623 if (last_colon_pos < end_bracket) {
624 port_part.ptr = NULL;
625 port_part.slen = 0;
626 } else {
627 port_part.ptr = (char*)last_colon_pos + 1;
628 port_part.slen = end - port_part.ptr;
629 }
630
631 /* Make sure port number is valid */
632 for (i=0; i<port_part.slen; ++i) {
633 if (!pj_isdigit(port_part.ptr[i]))
634 return PJ_EINVAL;
635 }
636 port = pj_strtoul(&port_part);
637 if (port > 65535)
638 return PJ_EINVAL;
639
640 if (p_hostpart)
641 *p_hostpart = hostpart;
642 if (p_port)
643 *p_port = (pj_uint16_t)port;
644
645 return PJ_SUCCESS;
646
647 } else {
648 /* Treat everything as part of the IPv6 IP address */
649 if (p_hostpart)
650 *p_hostpart = *str;
651 if (p_port)
652 *p_port = 0;
653
654 return PJ_SUCCESS;
655 }
656
657 } else {
658 return PJ_EAFNOTSUP;
659 }
660
661 }
662
663 /*
664 * Parse address
665 */
666 PJ_DEF(pj_status_t) pj_sockaddr_parse( int af, unsigned options,
667 const pj_str_t *str,
668 pj_sockaddr *addr)
669 {
670 pj_str_t hostpart;
671 pj_uint16_t port;
672 pj_status_t status;
673
674 PJ_ASSERT_RETURN(addr, PJ_EINVAL);
675 PJ_ASSERT_RETURN(af==PJ_AF_UNSPEC ||
676 af==PJ_AF_INET ||
677 af==PJ_AF_INET6, PJ_EINVAL);
678 PJ_ASSERT_RETURN(options == 0, PJ_EINVAL);
679
680 status = pj_sockaddr_parse2(af, options, str, &hostpart, &port, &af);
681 if (status != PJ_SUCCESS)
682 return status;
683
684 #if !defined(PJ_HAS_IPV6) || !PJ_HAS_IPV6
685 if (af==PJ_AF_INET6)
686 return PJ_EIPV6NOTSUP;
687 #endif
688
689 status = pj_sockaddr_init(af, addr, &hostpart, port);
690 #if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6
691 if (status != PJ_SUCCESS && af == PJ_AF_INET6) {
692 /* Parsing does not yield valid address. Try to treat the last
693 * portion after the colon as port number.
694 */
695 const char *last_colon_pos=NULL, *p;
696 const char *end = str->ptr + str->slen;
697 unsigned long long_port;
698 pj_str_t port_part;
699 int i;
700
701 /* Parse as IPv6:port */
702 for (p=str->ptr; p!=end; ++p) {
703 if (*p == ':')
704 last_colon_pos = p;
705 }
706
707 if (last_colon_pos == NULL)
708 return status;
709
710 hostpart.ptr = (char*)str->ptr;
711 hostpart.slen = last_colon_pos - str->ptr;
712
713 port_part.ptr = (char*)last_colon_pos + 1;
714 port_part.slen = end - port_part.ptr;
715
716 /* Make sure port number is valid */
717 for (i=0; i<port_part.slen; ++i) {
718 if (!pj_isdigit(port_part.ptr[i]))
719 return status;
720 }
721 long_port = pj_strtoul(&port_part);
722 if (long_port > 65535)
723 return status;
724
725 port = (pj_uint16_t)long_port;
726
727 status = pj_sockaddr_init(PJ_AF_INET6, addr, &hostpart, port);
728 }
729 #endif
730
731 return status;
732 }
733
734
735 /* Resolve the IP address of local machine */
736 PJ_DEF(pj_status_t) pj_gethostip(int af, pj_sockaddr *addr)
737 {
738 unsigned i, count, cand_cnt;
739 enum {
740 CAND_CNT = 8,
741
742 /* Weighting to be applied to found addresses */
743 WEIGHT_HOSTNAME = 1, /* hostname IP is not always valid! */
744 WEIGHT_DEF_ROUTE = 2,
745 WEIGHT_INTERFACE = 1,
746 WEIGHT_LOOPBACK = -5,
747 WEIGHT_LINK_LOCAL = -4,
748 WEIGHT_DISABLED = -50,
749
750 MIN_WEIGHT = WEIGHT_DISABLED+1 /* minimum weight to use */
751 };
752 /* candidates: */
753 pj_sockaddr cand_addr[CAND_CNT];
754 int cand_weight[CAND_CNT];
755 int selected_cand;
756 char strip[PJ_INET6_ADDRSTRLEN+10];
757 /* Special IPv4 addresses. */
758 struct spec_ipv4_t
759 {
760 pj_uint32_t addr;
761 pj_uint32_t mask;
762 int weight;
763 } spec_ipv4[] =
764 {
765 /* 127.0.0.0/8, loopback addr will be used if there is no other
766 * addresses.
767 */
768 { 0x7f000000, 0xFF000000, WEIGHT_LOOPBACK },
769
770 /* 0.0.0.0/8, special IP that doesn't seem to be practically useful */
771 { 0x00000000, 0xFF000000, WEIGHT_DISABLED },
772
773 /* 169.254.0.0/16, a zeroconf/link-local address, which has higher
774 * priority than loopback and will be used if there is no other
775 * valid addresses.
776 */
777 { 0xa9fe0000, 0xFFFF0000, WEIGHT_LINK_LOCAL }
778 };
779 /* Special IPv6 addresses */
780 struct spec_ipv6_t
781 {
782 pj_uint8_t addr[16];
783 pj_uint8_t mask[16];
784 int weight;
785 } spec_ipv6[] =
786 {
787 /* Loopback address, ::1/128 */
788 { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
789 {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
790 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
791 WEIGHT_LOOPBACK
792 },
793
794 /* Link local, fe80::/10 */
795 { {0xfe,0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
796 {0xff,0xc0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
797 WEIGHT_LINK_LOCAL
798 },
799
800 /* Disabled, ::/128 */
801 { {0x0,0x0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
802 { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
803 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
804 WEIGHT_DISABLED
805 }
806 };
807 pj_addrinfo ai;
808 pj_status_t status;
809
810 /* May not be used if TRACE_ is disabled */
811 PJ_UNUSED_ARG(strip);
812
813 #ifdef _MSC_VER
814 /* Get rid of "uninitialized he variable" with MS compilers */
815 pj_bzero(&ai, sizeof(ai));
816 #endif
817
818 cand_cnt = 0;
819 pj_bzero(cand_addr, sizeof(cand_addr));
820 pj_bzero(cand_weight, sizeof(cand_weight));
821 for (i=0; i<PJ_ARRAY_SIZE(cand_addr); ++i) {
822 cand_addr[i].addr.sa_family = (pj_uint16_t)af;
823 PJ_SOCKADDR_RESET_LEN(&cand_addr[i]);
824 }
825
826 addr->addr.sa_family = (pj_uint16_t)af;
827 PJ_SOCKADDR_RESET_LEN(addr);
828
829 #if !defined(PJ_GETHOSTIP_DISABLE_LOCAL_RESOLUTION) || \
830 PJ_GETHOSTIP_DISABLE_LOCAL_RESOLUTION == 0
831 /* Get hostname's IP address */
832 {
833 const pj_str_t *hostname = pj_gethostname();
834 count = 1;
835
836 if (hostname->slen > 0)
837 status = pj_getaddrinfo(af, hostname, &count, &ai);
838 else
839 status = PJ_ERESOLVE;
840
841 if (status == PJ_SUCCESS) {
842 pj_assert(ai.ai_addr.addr.sa_family == (pj_uint16_t)af);
843 pj_sockaddr_copy_addr(&cand_addr[cand_cnt], &ai.ai_addr);
844 pj_sockaddr_set_port(&cand_addr[cand_cnt], 0);
845 cand_weight[cand_cnt] += WEIGHT_HOSTNAME;
846 ++cand_cnt;
847
848 TRACE_((THIS_FILE, "hostname IP is %s",
849 pj_sockaddr_print(&ai.ai_addr, strip, sizeof(strip), 3)));
850 }
851 }
852 #else
853 PJ_UNUSED_ARG(ai);
854 #endif
855
856 /* Get default interface (interface for default route) */
857 if (cand_cnt < PJ_ARRAY_SIZE(cand_addr)) {
858 status = pj_getdefaultipinterface(af, addr);
859 if (status == PJ_SUCCESS) {
860 TRACE_((THIS_FILE, "default IP is %s",
861 pj_sockaddr_print(addr, strip, sizeof(strip), 3)));
862
863 pj_sockaddr_set_port(addr, 0);
864 for (i=0; i<cand_cnt; ++i) {
865 if (pj_sockaddr_cmp(&cand_addr[i], addr)==0)
866 break;
867 }
868
869 cand_weight[i] += WEIGHT_DEF_ROUTE;
870 if (i >= cand_cnt) {
871 pj_sockaddr_copy_addr(&cand_addr[i], addr);
872 ++cand_cnt;
873 }
874 }
875 }
876
877
878 /* Enumerate IP interfaces */
879 if (cand_cnt < PJ_ARRAY_SIZE(cand_addr)) {
880 unsigned start_if = cand_cnt;
881 count = PJ_ARRAY_SIZE(cand_addr) - start_if;
882
883 status = pj_enum_ip_interface(af, &count, &cand_addr[start_if]);
884 if (status == PJ_SUCCESS && count) {
885 /* Clear the port number */
886 for (i=0; i<count; ++i)
887 pj_sockaddr_set_port(&cand_addr[start_if+i], 0);
888
889 /* For each candidate that we found so far (that is the hostname
890 * address and default interface address, check if they're found
891 * in the interface list. If found, add the weight, and if not,
892 * decrease the weight.
893 */
894 for (i=0; i<cand_cnt; ++i) {
895 unsigned j;
896 for (j=0; j<count; ++j) {
897 if (pj_sockaddr_cmp(&cand_addr[i],
898 &cand_addr[start_if+j])==0)
899 break;
900 }
901
902 if (j == count) {
903 /* Not found */
904 cand_weight[i] -= WEIGHT_INTERFACE;
905 } else {
906 cand_weight[i] += WEIGHT_INTERFACE;
907 }
908 }
909
910 /* Add remaining interface to candidate list. */
911 for (i=0; i<count; ++i) {
912 unsigned j;
913 for (j=0; j<cand_cnt; ++j) {
914 if (pj_sockaddr_cmp(&cand_addr[start_if+i],
915 &cand_addr[j])==0)
916 break;
917 }
918
919 if (j == cand_cnt) {
920 if (cand_cnt != (start_if + i)) {
921 pj_sockaddr_copy_addr(&cand_addr[cand_cnt],
922 &cand_addr[start_if + i]);
923 }
924 cand_weight[cand_cnt] += WEIGHT_INTERFACE;
925 ++cand_cnt;
926 }
927 }
928 }
929 }
930
931 /* Apply weight adjustment for special IPv4/IPv6 addresses
932 * See https://github.com/pjsip/pjproject/issues/1046
933 */
934 if (af == PJ_AF_INET) {
935 for (i=0; i<cand_cnt; ++i) {
936 unsigned j;
937 for (j=0; j<PJ_ARRAY_SIZE(spec_ipv4); ++j) {
938 pj_uint32_t a = pj_ntohl(cand_addr[i].ipv4.sin_addr.s_addr);
939 pj_uint32_t pa = spec_ipv4[j].addr;
940 pj_uint32_t pm = spec_ipv4[j].mask;
941
942 if ((a & pm) == pa) {
943 cand_weight[i] += spec_ipv4[j].weight;
944 break;
945 }
946 }
947 }
948 } else if (af == PJ_AF_INET6) {
949 for (i=0; i<PJ_ARRAY_SIZE(spec_ipv6); ++i) {
950 unsigned j;
951 for (j=0; j<cand_cnt; ++j) {
952 pj_uint8_t *a = cand_addr[j].ipv6.sin6_addr.s6_addr;
953 pj_uint8_t am[16];
954 pj_uint8_t *pa = spec_ipv6[i].addr;
955 pj_uint8_t *pm = spec_ipv6[i].mask;
956 unsigned k;
957
958 for (k=0; k<16; ++k) {
959 am[k] = (pj_uint8_t)((a[k] & pm[k]) & 0xFF);
960 }
961
962 if (pj_memcmp(am, pa, 16)==0) {
963 cand_weight[j] += spec_ipv6[i].weight;
964 }
965 }
966 }
967 } else {
968 return PJ_EAFNOTSUP;
969 }
970
971 /* Enumerate candidates to get the best IP address to choose */
972 selected_cand = -1;
973 for (i=0; i<cand_cnt; ++i) {
974 TRACE_((THIS_FILE, "Checking candidate IP %s, weight=%d",
975 pj_sockaddr_print(&cand_addr[i], strip, sizeof(strip), 3),
976 cand_weight[i]));
977
978 if (cand_weight[i] < MIN_WEIGHT) {
979 continue;
980 }
981
982 if (selected_cand == -1)
983 selected_cand = i;
984 else if (cand_weight[i] > cand_weight[selected_cand])
985 selected_cand = i;
986 }
987
988 /* If else fails, returns loopback interface as the last resort */
989 if (selected_cand == -1) {
990 if (af==PJ_AF_INET) {
991 addr->ipv4.sin_addr.s_addr = pj_htonl (0x7f000001);
992 } else {
993 pj_in6_addr *s6_addr_;
994
995 s6_addr_ = (pj_in6_addr*) pj_sockaddr_get_addr(addr);
996 pj_bzero(s6_addr_, sizeof(pj_in6_addr));
997 s6_addr_->s6_addr[15] = 1;
998 }
999 TRACE_((THIS_FILE, "Loopback IP %s returned",
1000 pj_sockaddr_print(addr, strip, sizeof(strip), 3)));
1001 } else {
1002 pj_sockaddr_copy_addr(addr, &cand_addr[selected_cand]);
1003 TRACE_((THIS_FILE, "Candidate %s selected",
1004 pj_sockaddr_print(addr, strip, sizeof(strip), 3)));
1005 }
1006
1007 return PJ_SUCCESS;
1008 }
1009
1010 /* Get IP interface for sending to the specified destination */
1011 PJ_DEF(pj_status_t) pj_getipinterface(int af,
1012 const pj_str_t *dst,
1013 pj_sockaddr *itf_addr,
1014 pj_bool_t allow_resolve,
1015 pj_sockaddr *p_dst_addr)
1016 {
1017 pj_sockaddr dst_addr;
1018 pj_sock_t fd;
1019 int len;
1020 pj_uint8_t zero[64];
1021 pj_status_t status;
1022
1023 pj_sockaddr_init(af, &dst_addr, NULL, 53);
1024 status = pj_inet_pton(af, dst, pj_sockaddr_get_addr(&dst_addr));
1025 if (status != PJ_SUCCESS) {
1026 /* "dst" is not an IP address. */
1027 if (allow_resolve) {
1028 status = pj_sockaddr_init(af, &dst_addr, dst, 53);
1029 } else {
1030 pj_str_t cp;
1031
1032 if (af == PJ_AF_INET) {
1033 cp = pj_str("1.1.1.1");
1034 } else {
1035 cp = pj_str("1::1");
1036 }
1037 status = pj_sockaddr_init(af, &dst_addr, &cp, 53);
1038 }
1039
1040 if (status != PJ_SUCCESS)
1041 return status;
1042 }
1043
1044 /* Create UDP socket and connect() to the destination IP */
1045 status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &fd);
1046 if (status != PJ_SUCCESS) {
1047 return status;
1048 }
1049
1050 status = pj_sock_connect(fd, &dst_addr, pj_sockaddr_get_len(&dst_addr));
1051 if (status != PJ_SUCCESS) {
1052 pj_sock_close(fd);
1053 return status;
1054 }
1055
1056 len = sizeof(*itf_addr);
1057 status = pj_sock_getsockname(fd, itf_addr, &len);
1058 if (status != PJ_SUCCESS) {
1059 pj_sock_close(fd);
1060 return status;
1061 }
1062
1063 pj_sock_close(fd);
1064
1065 /* Check that the address returned is not zero */
1066 pj_bzero(zero, sizeof(zero));
1067 if (pj_memcmp(pj_sockaddr_get_addr(itf_addr), zero,
1068 pj_sockaddr_get_addr_len(itf_addr))==0)
1069 {
1070 return PJ_ENOTFOUND;
1071 }
1072
1073 #if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6
1074 /* Check if the local address is link-local but the destination
1075 * is not link-local.
1076 */
1077 if (af == pj_AF_INET6() &&
1078 IN6_IS_ADDR_LINKLOCAL(&itf_addr->ipv6.sin6_addr) &&
1079 (!IN6_IS_ADDR_LINKLOCAL(&dst_addr.ipv6.sin6_addr)))
1080 {
1081 TRACE_((THIS_FILE, "Local interface address is link-local and "
1082 "the destination host is external"));
1083
1084 return PJ_ENOTFOUND;
1085 }
1086 #endif
1087
1088 if (p_dst_addr)
1089 *p_dst_addr = dst_addr;
1090
1091 return PJ_SUCCESS;
1092 }
1093
1094 /* Get the default IP interface */
1095 PJ_DEF(pj_status_t) pj_getdefaultipinterface(int af, pj_sockaddr *addr)
1096 {
1097 pj_str_t cp;
1098
1099 if (af == PJ_AF_INET) {
1100 cp = pj_str("1.1.1.1");
1101 } else {
1102 cp = pj_str("1::1");
1103 }
1104
1105 return pj_getipinterface(af, &cp, addr, PJ_FALSE, NULL);
1106 }
1107
1108
1109 /*
1110 * Bind socket at random port.
1111 */
1112 PJ_DEF(pj_status_t) pj_sock_bind_random( pj_sock_t sockfd,
1113 const pj_sockaddr_t *addr,
1114 pj_uint16_t port_range,
1115 pj_uint16_t max_try)
1116 {
1117 pj_sockaddr bind_addr;
1118 int addr_len;
1119 pj_uint16_t base_port;
1120 pj_status_t status = PJ_SUCCESS;
1121
1122 PJ_CHECK_STACK();
1123
1124 PJ_ASSERT_RETURN(addr, PJ_EINVAL);
1125
1126 pj_sockaddr_cp(&bind_addr, addr);
1127 addr_len = pj_sockaddr_get_len(addr);
1128 base_port = pj_sockaddr_get_port(addr);
1129
1130 if (base_port == 0 || port_range == 0) {
1131 return pj_sock_bind(sockfd, &bind_addr, addr_len);
1132 }
1133
1134 for (; max_try; --max_try) {
1135 pj_uint16_t port;
1136 port = (pj_uint16_t)(base_port + pj_rand() % (port_range + 1));
1137 pj_sockaddr_set_port(&bind_addr, port);
1138 status = pj_sock_bind(sockfd, &bind_addr, addr_len);
1139 if (status == PJ_SUCCESS)
1140 break;
1141 }
1142
1143 return status;
1144 }
1145
1146
1147 /*
1148 * Adjust socket send/receive buffer size.
1149 */
1150 PJ_DEF(pj_status_t) pj_sock_setsockopt_sobuf( pj_sock_t sockfd,
1151 pj_uint16_t optname,
1152 pj_bool_t auto_retry,
1153 unsigned *buf_size)
1154 {
1155 pj_status_t status;
1156 int try_size, cur_size, i, step, size_len;
1157 enum { MAX_TRY = 20 };
1158
1159 PJ_CHECK_STACK();
1160
1161 PJ_ASSERT_RETURN(sockfd != PJ_INVALID_SOCKET &&
1162 buf_size &&
1163 *buf_size > 0 &&
1164 (optname == pj_SO_RCVBUF() ||
1165 optname == pj_SO_SNDBUF()),
1166 PJ_EINVAL);
1167
1168 size_len = sizeof(cur_size);
1169 status = pj_sock_getsockopt(sockfd, pj_SOL_SOCKET(), optname,
1170 &cur_size, &size_len);
1171 if (status != PJ_SUCCESS)
1172 return status;
1173
1174 try_size = *buf_size;
1175 step = (try_size - cur_size) / MAX_TRY;
1176 if (step < 4096)
1177 step = 4096;
1178
1179 for (i = 0; i < (MAX_TRY-1); ++i) {
1180 if (try_size <= cur_size) {
1181 /* Done, return current size */
1182 *buf_size = cur_size;
1183 break;
1184 }
1185
1186 status = pj_sock_setsockopt(sockfd, pj_SOL_SOCKET(), optname,
1187 &try_size, sizeof(try_size));
1188 if (status == PJ_SUCCESS) {
1189 status = pj_sock_getsockopt(sockfd, pj_SOL_SOCKET(), optname,
1190 &cur_size, &size_len);
1191 if (status != PJ_SUCCESS) {
1192 /* Ops! No info about current size, just return last try size
1193 * and quit.
1194 */
1195 *buf_size = try_size;
1196 break;
1197 }
1198 }
1199
1200 if (!auto_retry)
1201 break;
1202
1203 try_size -= step;
1204 }
1205
1206 return status;
1207 }
1208
1209
1210 PJ_DEF(pj_status_t) pj_sockopt_params_clone(pj_pool_t *pool,
1211 pj_sockopt_params *dst,
1212 const pj_sockopt_params *src)
1213 {
1214 unsigned int i;
1215
1216 PJ_ASSERT_RETURN(pool && src && dst, PJ_EINVAL);
1217
1218 pj_memcpy(dst, src, sizeof(pj_sockopt_params));
1219 for (i = 0; i < dst->cnt && i < PJ_MAX_SOCKOPT_PARAMS; ++i) {
1220 if (dst->options[i].optlen == 0) {
1221 dst->options[i].optval = NULL;
1222 continue;
1223 }
1224 dst->options[i].optval = pj_pool_alloc(pool, dst->options[i].optlen);
1225 pj_memcpy(dst->options[i].optval, src->options[i].optval,
1226 dst->options[i].optlen);
1227 }
1228
1229 return PJ_SUCCESS;
1230 }
1231
1232
1233 PJ_DEF(char *) pj_addr_str_print( const pj_str_t *host_str, int port,
1234 char *buf, int size, unsigned flag)
1235 {
1236 enum {
1237 WITH_PORT = 1
1238 };
1239 char *bquote, *equote;
1240 int af = pj_AF_UNSPEC();
1241 pj_in6_addr dummy6;
1242
1243 /* Check if this is an IPv6 address */
1244 if (pj_inet_pton(pj_AF_INET6(), host_str, &dummy6) == PJ_SUCCESS)
1245 af = pj_AF_INET6();
1246
1247 if (af == pj_AF_INET6()) {
1248 bquote = "[";
1249 equote = "]";
1250 } else {
1251 bquote = "";
1252 equote = "";
1253 }
1254
1255 if (flag & WITH_PORT) {
1256 pj_ansi_snprintf(buf, size, "%s%.*s%s:%d",
1257 bquote, (int)host_str->slen, host_str->ptr, equote,
1258 port);
1259 } else {
1260 pj_ansi_snprintf(buf, size, "%s%.*s%s",
1261 bquote, (int)host_str->slen, host_str->ptr, equote);
1262 }
1263 return buf;
1264 }
1265
1266 /*
1267 * Simulate unix-like socketpair()
1268 * Use a pair of IPv4/IPv6 local sockets (listening on 127.0.0.1/::1)
1269 */
1270 static pj_status_t socketpair_imp(int family,
1271 int type,
1272 int protocol,
1273 pj_sock_t sv[2])
1274 {
1275 pj_status_t status;
1276 pj_sock_t lfd = PJ_INVALID_SOCKET;
1277 pj_sock_t cfd = PJ_INVALID_SOCKET;
1278 pj_str_t loopback;
1279 pj_sockaddr sa;
1280 int salen;
1281 int type0 = type;
1282
1283 #if PJ_HAS_TCP
1284 PJ_ASSERT_RETURN((type & 0xF) == pj_SOCK_DGRAM() ||
1285 (type & 0xF) == pj_SOCK_STREAM(),
1286 PJ_EINVAL);
1287 #else
1288 PJ_ASSERT_RETURN((type & 0xF) == pj_SOCK_DGRAM(), PJ_EINVAL);
1289 #endif
1290
1291 PJ_ASSERT_RETURN(family == pj_AF_INET() || family == pj_AF_INET6(),
1292 PJ_EINVAL);
1293
1294 loopback = family == pj_AF_INET() ? pj_str("127.0.0.1") : pj_str("::1");
1295
1296 #if !defined(SOCK_CLOEXEC)
1297 if ((type0 & pj_SOCK_CLOEXEC()) == pj_SOCK_CLOEXEC())
1298 type &= ~pj_SOCK_CLOEXEC();
1299 #else
1300 PJ_UNUSED_ARG(type0);
1301 #endif
1302
1303 /* listen */
1304 status = pj_sock_socket(family, type, protocol, &lfd);
1305 if (status != PJ_SUCCESS)
1306 goto on_error;
1307
1308 pj_sockaddr_init(family, &sa, &loopback, 0);
1309 salen = pj_sockaddr_get_len(&sa);
1310 status = pj_sock_bind(lfd, &sa, salen);
1311 if (status != PJ_SUCCESS)
1312 goto on_error;
1313
1314 status = pj_sock_getsockname(lfd, &sa, &salen);
1315 if (status != PJ_SUCCESS)
1316 goto on_error;
1317
1318 #if PJ_HAS_TCP
1319 if ((type & 0xF) == pj_SOCK_STREAM()) {
1320 status = pj_sock_listen(lfd, 1);
1321 if (status != PJ_SUCCESS)
1322 goto on_error;
1323 }
1324 #endif
1325
1326 /* connect to listen fd */
1327 status = pj_sock_socket(family, type, protocol, &cfd);
1328 if (status != PJ_SUCCESS)
1329 goto on_error;
1330 status = pj_sock_connect(cfd, &sa, salen);
1331 if (status != PJ_SUCCESS)
1332 goto on_error;
1333
1334 if ((type & 0xF) == pj_SOCK_DGRAM()) {
1335 status = pj_sock_getsockname(cfd, &sa, &salen);
1336 if (status != PJ_SUCCESS)
1337 goto on_error;
1338 status = pj_sock_connect(lfd, &sa, salen);
1339 if (status != PJ_SUCCESS)
1340 goto on_error;
1341 sv[0] = lfd;
1342 sv[1] = cfd;
1343 }
1344 #if PJ_HAS_TCP
1345 else if ((type & 0xF) == pj_SOCK_STREAM()) {
1346 pj_sock_t newfd = PJ_INVALID_SOCKET;
1347 status = pj_sock_accept(lfd, &newfd, NULL, NULL);
1348 if (status != PJ_SUCCESS)
1349 goto on_error;
1350 pj_sock_close(lfd);
1351 sv[0] = newfd;
1352 sv[1] = cfd;
1353 }
1354 #endif
1355
1356 #if !defined(SOCK_CLOEXEC)
1357 if ((type0 & pj_SOCK_CLOEXEC()) == pj_SOCK_CLOEXEC()) {
1358 pj_set_cloexec_flag((int)sv[0]);
1359 pj_set_cloexec_flag((int)sv[1]);
1360 }
1361 #endif
1362
1363 return PJ_SUCCESS;
1364
1365 on_error:
1366 if (lfd != PJ_INVALID_SOCKET)
1367 pj_sock_close(lfd);
1368 if (cfd != PJ_INVALID_SOCKET)
1369 pj_sock_close(cfd);
1370 return status;
1371 }
1372
1373 #if defined(PJ_SOCK_HAS_SOCKETPAIR) && PJ_SOCK_HAS_SOCKETPAIR != 0
1374 PJ_DEF(pj_status_t) pj_sock_socketpair(int family,
1375 int type,
1376 int protocol,
1377 pj_sock_t sv[2])
1378 {
1379 int status;
1380 int tmp_sv[2];
1381 int type0 = type;
1382
1383 PJ_CHECK_STACK();
1384
1385 #if !defined(SOCK_CLOEXEC)
1386 if ((type0 & pj_SOCK_CLOEXEC()) == pj_SOCK_CLOEXEC())
1387 type &= ~pj_SOCK_CLOEXEC();
1388 #endif
1389
1390 status = socketpair(family, type, protocol, tmp_sv);
1391 if (status != PJ_SUCCESS) {
1392 if (errno == EOPNOTSUPP) {
1393 return socketpair_imp(family, type0, protocol, sv);
1394 }
1395 status = PJ_RETURN_OS_ERROR(pj_get_native_netos_error());
1396 return status;
1397 }
1398 sv[0] = tmp_sv[0];
1399 sv[1] = tmp_sv[1];
1400 return PJ_SUCCESS;
1401 }
1402 #else
1403 PJ_DEF(pj_status_t) pj_sock_socketpair(int family,
1404 int type,
1405 int protocol,
1406 pj_sock_t sv[2])
1407 {
1408 PJ_CHECK_STACK();
1409 return socketpair_imp(family, type, protocol, sv);
1410 }
1411 #endif
1412
1413
1414 /* Check IP address type. */
1415 PJ_DEF(pj_bool_t) pj_check_addr_type(const pj_sockaddr *addr, unsigned type)
1416 {
1417 int af;
1418
1419 PJ_ASSERT_RETURN(addr && type, PJ_EINVAL);
1420 PJ_ASSERT_RETURN(addr->addr.sa_family == PJ_AF_INET ||
1421 addr->addr.sa_family == PJ_AF_INET6, PJ_EINVAL);
1422
1423 af = addr->addr.sa_family;
1424
1425 if (af == PJ_AF_INET) {
1426 enum {
1427 /* Disabled: 0.0.0.0/8 */
1428 ADDR4_DISABLED = 0x00000000,
1429 MASK4_DISABLED = 0xFF000000,
1430 /* Loopback: 127.0.0.0/8 */
1431 ADDR4_LOOPBACK = 0x7f000000,
1432 MASK4_LOOPBACK = 0xFF000000,
1433 /* Link-local: 169.254.0.0/16 */
1434 ADDR4_LINKLOCAL = 0xa9fe0000,
1435 MASK4_LINKLOCAL = 0xFFFF0000,
1436 /* Multicast: 224.0.0.0/3 */
1437 ADDR4_MULTICAST = 0xE0000000,
1438 MASK4_MULTICAST = 0xF0000000,
1439 };
1440
1441 pj_uint32_t a = pj_ntohl(addr->ipv4.sin_addr.s_addr);
1442
1443 if ((type & PJ_ADDR_TYPE_DISABLED) &&
1444 (a & (pj_uint32_t)MASK4_DISABLED)==(pj_uint32_t)ADDR4_DISABLED)
1445 {
1446 return PJ_TRUE;
1447 }
1448
1449 if ((type & PJ_ADDR_TYPE_LOOPBACK) &&
1450 (a & (pj_uint32_t)MASK4_LOOPBACK)==(pj_uint32_t)ADDR4_LOOPBACK)
1451 {
1452 return PJ_TRUE;
1453 }
1454
1455 if ((type & PJ_ADDR_TYPE_LINK_LOCAL) &&
1456 (a & (pj_uint32_t)MASK4_LINKLOCAL)==(pj_uint32_t)ADDR4_LINKLOCAL)
1457 {
1458 return PJ_TRUE;
1459 }
1460
1461 if (type & PJ_ADDR_TYPE_PRIVATE) {
1462 pj_uint8_t b1 = (pj_uint8_t)(a >> 24);
1463 pj_uint8_t b2 = (pj_uint8_t)((a >> 16) & 0x0ff);;
1464
1465 /* 10.0.0.0/8 */
1466 if (b1 == 10)
1467 return PJ_TRUE;
1468
1469 /* 172.16.0.0/12 or 172.16.0.0-172.31.255.255 */
1470 if ((b1 == 172) && (b2 >= 16) && (b2 <= 31))
1471 return PJ_TRUE;
1472
1473 /* 192.168.0.0/16 */
1474 if ((b1 == 192) && (b2 == 168))
1475 return PJ_TRUE;
1476 }
1477
1478 if ((type & PJ_ADDR_TYPE_MULTICAST) &&
1479 (a & (pj_uint32_t)MASK4_MULTICAST)==(pj_uint32_t)ADDR4_MULTICAST)
1480 {
1481 return PJ_TRUE;
1482 }
1483
1484 } else /* if (af == PJ_AF_INET6) */ {
1485
1486 if (type & PJ_ADDR_TYPE_DISABLED) {
1487 /* ::/128 */
1488 pj_uint8_t saddr[16] = {0x0,0x0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
1489 if (pj_memcmp(saddr, &addr->ipv6.sin6_addr, 16) == 0)
1490 return PJ_TRUE;
1491 }
1492
1493 if (type & PJ_ADDR_TYPE_LOOPBACK) {
1494 /* ::1/128 */
1495 pj_uint8_t saddr[16] = {0x0,0x0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
1496 if (pj_memcmp(saddr, &addr->ipv6.sin6_addr, 16) == 0)
1497 return PJ_TRUE;
1498 }
1499
1500 if (type & PJ_ADDR_TYPE_LINK_LOCAL) {
1501 /* fe80::/64 */
1502 pj_uint8_t saddr[16] = {0xfe,0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
1503 if (pj_memcmp(saddr, &addr->ipv6.sin6_addr, 8) == 0)
1504 return PJ_TRUE;
1505 }
1506
1507 if (type & PJ_ADDR_TYPE_PRIVATE) {
1508 /* fc00::/7 */
1509 if ((addr->ipv6.sin6_addr.s6_addr[0] & 0xfe) == 0xfc)
1510 return PJ_TRUE;
1511 }
1512
1513 if (type & PJ_ADDR_TYPE_MULTICAST) {
1514 /* ff00::/8 */
1515 if (addr->ipv6.sin6_addr.s6_addr[0] == 0xff)
1516 return PJ_TRUE;
1517 }
1518 }
1519
1520 return PJ_FALSE;
1521 }
1522
1523
1524 /* Only need to implement these in DLL build */
1525 #if defined(PJ_DLL)
1526
1527 PJ_DEF(pj_uint16_t) pj_AF_UNSPEC(void)
1528 {
1529 return PJ_AF_UNSPEC;
1530 }
1531
1532 PJ_DEF(pj_uint16_t) pj_AF_UNIX(void)
1533 {
1534 return PJ_AF_UNIX;
1535 }
1536
1537 PJ_DEF(pj_uint16_t) pj_AF_INET(void)
1538 {
1539 return PJ_AF_INET;
1540 }
1541
1542 PJ_DEF(pj_uint16_t) pj_AF_INET6(void)
1543 {
1544 return PJ_AF_INET6;
1545 }
1546
1547 PJ_DEF(pj_uint16_t) pj_AF_PACKET(void)
1548 {
1549 return PJ_AF_PACKET;
1550 }
1551
1552 PJ_DEF(pj_uint16_t) pj_AF_IRDA(void)
1553 {
1554 return PJ_AF_IRDA;
1555 }
1556
1557 PJ_DEF(int) pj_SOCK_STREAM(void)
1558 {
1559 return PJ_SOCK_STREAM;
1560 }
1561
1562 PJ_DEF(int) pj_SOCK_DGRAM(void)
1563 {
1564 return PJ_SOCK_DGRAM;
1565 }
1566
1567 PJ_DEF(int) pj_SOCK_RAW(void)
1568 {
1569 return PJ_SOCK_RAW;
1570 }
1571
1572 PJ_DEF(int) pj_SOCK_RDM(void)
1573 {
1574 return PJ_SOCK_RDM;
1575 }
1576
1577 PJ_DEF(int) pj_SOCK_CLOEXEC(void)
1578 {
1579 return PJ_SOCK_CLOEXEC;
1580 }
1581
1582 PJ_DEF(pj_uint16_t) pj_SOL_SOCKET(void)
1583 {
1584 return PJ_SOL_SOCKET;
1585 }
1586
1587 PJ_DEF(pj_uint16_t) pj_SOL_IP(void)
1588 {
1589 return PJ_SOL_IP;
1590 }
1591
1592 PJ_DEF(pj_uint16_t) pj_SOL_TCP(void)
1593 {
1594 return PJ_SOL_TCP;
1595 }
1596
1597 PJ_DEF(pj_uint16_t) pj_SOL_UDP(void)
1598 {
1599 return PJ_SOL_UDP;
1600 }
1601
1602 PJ_DEF(pj_uint16_t) pj_SOL_IPV6(void)
1603 {
1604 return PJ_SOL_IPV6;
1605 }
1606
1607 PJ_DEF(int) pj_IP_TOS(void)
1608 {
1609 return PJ_IP_TOS;
1610 }
1611
1612 PJ_DEF(int) pj_IPTOS_LOWDELAY(void)
1613 {
1614 return PJ_IPTOS_LOWDELAY;
1615 }
1616
1617 PJ_DEF(int) pj_IPTOS_THROUGHPUT(void)
1618 {
1619 return PJ_IPTOS_THROUGHPUT;
1620 }
1621
1622 PJ_DEF(int) pj_IPTOS_RELIABILITY(void)
1623 {
1624 return PJ_IPTOS_RELIABILITY;
1625 }
1626
1627 PJ_DEF(int) pj_IPTOS_MINCOST(void)
1628 {
1629 return PJ_IPTOS_MINCOST;
1630 }
1631
1632 PJ_DEF(int) pj_IPV6_TCLASS(void)
1633 {
1634 return PJ_IPV6_TCLASS;
1635 }
1636
1637 PJ_DEF(pj_uint16_t) pj_SO_TYPE(void)
1638 {
1639 return PJ_SO_TYPE;
1640 }
1641
1642 PJ_DEF(pj_uint16_t) pj_SO_RCVBUF(void)
1643 {
1644 return PJ_SO_RCVBUF;
1645 }
1646
1647 PJ_DEF(pj_uint16_t) pj_SO_SNDBUF(void)
1648 {
1649 return PJ_SO_SNDBUF;
1650 }
1651
1652 PJ_DEF(pj_uint16_t) pj_TCP_NODELAY(void)
1653 {
1654 return PJ_TCP_NODELAY;
1655 }
1656
1657 PJ_DEF(pj_uint16_t) pj_SO_REUSEADDR(void)
1658 {
1659 return PJ_SO_REUSEADDR;
1660 }
1661
1662 PJ_DEF(pj_uint16_t) pj_SO_NOSIGPIPE(void)
1663 {
1664 return PJ_SO_NOSIGPIPE;
1665 }
1666
1667 PJ_DEF(pj_uint16_t) pj_SO_PRIORITY(void)
1668 {
1669 return PJ_SO_PRIORITY;
1670 }
1671
1672 PJ_DEF(pj_uint16_t) pj_IP_MULTICAST_IF(void)
1673 {
1674 return PJ_IP_MULTICAST_IF;
1675 }
1676
1677 PJ_DEF(pj_uint16_t) pj_IP_MULTICAST_TTL(void)
1678 {
1679 return PJ_IP_MULTICAST_TTL;
1680 }
1681
1682 PJ_DEF(pj_uint16_t) pj_IP_MULTICAST_LOOP(void)
1683 {
1684 return PJ_IP_MULTICAST_LOOP;
1685 }
1686
1687 PJ_DEF(pj_uint16_t) pj_IP_ADD_MEMBERSHIP(void)
1688 {
1689 return PJ_IP_ADD_MEMBERSHIP;
1690 }
1691
1692 PJ_DEF(pj_uint16_t) pj_IP_DROP_MEMBERSHIP(void)
1693 {
1694 return PJ_IP_DROP_MEMBERSHIP;
1695 }
1696
1697 PJ_DEF(int) pj_MSG_OOB(void)
1698 {
1699 return PJ_MSG_OOB;
1700 }
1701
1702 PJ_DEF(int) pj_MSG_PEEK(void)
1703 {
1704 return PJ_MSG_PEEK;
1705 }
1706
1707 PJ_DEF(int) pj_MSG_DONTROUTE(void)
1708 {
1709 return PJ_MSG_DONTROUTE;
1710 }
1711
1712 #endif /* PJ_DLL */
1713