Wed Jan 8 2020 09:49:48

Asterisk developer's documentation


netsock2.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * Viagénie <asteriskv6@viagenie.ca>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  *
21  * \brief Network socket handling
22  *
23  * \author Viagénie <asteriskv6@viagenie.ca>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369108 $")
33 
34 #include "asterisk/config.h"
35 #include "asterisk/netsock2.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/threadstorage.h"
38 
39 int ast_sockaddr_ipv4_mapped(const struct ast_sockaddr *addr, struct ast_sockaddr *ast_mapped)
40 {
41  const struct sockaddr_in6 *sin6;
42  struct sockaddr_in sin4;
43 
44  if (!ast_sockaddr_is_ipv6(addr)) {
45  return 0;
46  }
47 
48  if (!ast_sockaddr_is_ipv4_mapped(addr)) {
49  return 0;
50  }
51 
52  sin6 = (const struct sockaddr_in6*)&addr->ss;
53 
54  memset(&sin4, 0, sizeof(sin4));
55  sin4.sin_family = AF_INET;
56  sin4.sin_port = sin6->sin6_port;
57  sin4.sin_addr.s_addr = ((uint32_t *)&sin6->sin6_addr)[3];
58 
59  ast_sockaddr_from_sin(ast_mapped, &sin4);
60 
61  return 1;
62 }
63 
64 
66 
67 char *ast_sockaddr_stringify_fmt(const struct ast_sockaddr *sa, int format)
68 {
69  struct ast_sockaddr sa_ipv4;
70  const struct ast_sockaddr *sa_tmp;
71  char host[NI_MAXHOST];
72  char port[NI_MAXSERV];
73  struct ast_str *str;
74  int e;
75  static const size_t size = sizeof(host) - 1 + sizeof(port) - 1 + 4;
76 
77 
78  if (ast_sockaddr_isnull(sa)) {
79  return "(null)";
80  }
81 
82  if (!(str = ast_str_thread_get(&ast_sockaddr_stringify_buf, size))) {
83  return "";
84  }
85 
86  if (ast_sockaddr_ipv4_mapped(sa, &sa_ipv4)) {
87  sa_tmp = &sa_ipv4;
88  } else {
89  sa_tmp = sa;
90  }
91 
92  if ((e = getnameinfo((struct sockaddr *)&sa_tmp->ss, sa_tmp->len,
93  format & AST_SOCKADDR_STR_ADDR ? host : NULL,
94  format & AST_SOCKADDR_STR_ADDR ? sizeof(host) : 0,
95  format & AST_SOCKADDR_STR_PORT ? port : 0,
96  format & AST_SOCKADDR_STR_PORT ? sizeof(port): 0,
97  NI_NUMERICHOST | NI_NUMERICSERV))) {
98  ast_log(LOG_ERROR, "getnameinfo(): %s\n", gai_strerror(e));
99  return "";
100  }
101 
102  if ((format & AST_SOCKADDR_STR_REMOTE) == AST_SOCKADDR_STR_REMOTE) {
103  char *p;
104  if (ast_sockaddr_is_ipv6_link_local(sa) && (p = strchr(host, '%'))) {
105  *p = '\0';
106  }
107  }
108 
109  switch ((format & AST_SOCKADDR_STR_FORMAT_MASK)) {
111  ast_str_set(&str, 0, sa_tmp->ss.ss_family == AF_INET6 ?
112  "[%s]:%s" : "%s:%s", host, port);
113  break;
115  ast_str_set(&str, 0, "%s", host);
116  break;
118  ast_str_set(&str, 0,
119  sa_tmp->ss.ss_family == AF_INET6 ? "[%s]" : "%s", host);
120  break;
122  ast_str_set(&str, 0, "%s", port);
123  break;
124  default:
125  ast_log(LOG_ERROR, "Invalid format\n");
126  return "";
127  }
128 
129  return ast_str_buffer(str);
130 }
131 
132 int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags)
133 {
134  char *s = str;
135  char *orig_str = str;/* Original string in case the port presence is incorrect. */
136  char *host_end = NULL;/* Delay terminating the host in case the port presence is incorrect. */
137 
138  ast_debug(5, "Splitting '%s' into...\n", str);
139  *host = NULL;
140  *port = NULL;
141  if (*s == '[') {
142  *host = ++s;
143  for (; *s && *s != ']'; ++s) {
144  }
145  if (*s == ']') {
146  host_end = s;
147  ++s;
148  }
149  if (*s == ':') {
150  *port = s + 1;
151  }
152  } else {
153  *host = s;
154  for (; *s; ++s) {
155  if (*s == ':') {
156  if (*port) {
157  *port = NULL;
158  break;
159  } else {
160  *port = s;
161  }
162  }
163  }
164  if (*port) {
165  host_end = *port;
166  ++*port;
167  }
168  }
169 
170  switch (flags & PARSE_PORT_MASK) {
171  case PARSE_PORT_IGNORE:
172  *port = NULL;
173  break;
174  case PARSE_PORT_REQUIRE:
175  if (*port == NULL) {
176  ast_log(LOG_WARNING, "Port missing in %s\n", orig_str);
177  return 0;
178  }
179  break;
180  case PARSE_PORT_FORBID:
181  if (*port != NULL) {
182  ast_log(LOG_WARNING, "Port disallowed in %s\n", orig_str);
183  return 0;
184  }
185  break;
186  }
187 
188  /* Can terminate the host string now if needed. */
189  if (host_end) {
190  *host_end = '\0';
191  }
192  ast_debug(5, "...host '%s' and port '%s'.\n", *host, *port ? *port : "");
193  return 1;
194 }
195 
196 
197 
198 int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
199 {
200  struct addrinfo hints;
201  struct addrinfo *res;
202  char *s;
203  char *host;
204  char *port;
205  int e;
206 
207  s = ast_strdupa(str);
208  if (!ast_sockaddr_split_hostport(s, &host, &port, flags)) {
209  return 0;
210  }
211 
212  memset(&hints, 0, sizeof(hints));
213  /* Hint to get only one entry from getaddrinfo */
214  hints.ai_socktype = SOCK_DGRAM;
215 
216 #ifdef AI_NUMERICSERV
217  hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
218 #else
219  hints.ai_flags = AI_NUMERICHOST;
220 #endif
221  if ((e = getaddrinfo(host, port, &hints, &res))) {
222  if (e != EAI_NONAME) { /* if this was just a host name rather than a ip address, don't print error */
223  ast_log(LOG_ERROR, "getaddrinfo(\"%s\", \"%s\", ...): %s\n",
224  host, S_OR(port, "(null)"), gai_strerror(e));
225  }
226  return 0;
227  }
228 
229  /*
230  * I don't see how this could be possible since we're not resolving host
231  * names. But let's be careful...
232  */
233  if (res->ai_next != NULL) {
234  ast_log(LOG_WARNING, "getaddrinfo() returned multiple "
235  "addresses. Ignoring all but the first.\n");
236  }
237 
238  if (addr) {
239  addr->len = res->ai_addrlen;
240  memcpy(&addr->ss, res->ai_addr, addr->len);
241  }
242 
243  freeaddrinfo(res);
244 
245  return 1;
246 }
247 
248 int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str,
249  int flags, int family)
250 {
251  struct addrinfo hints, *res, *ai;
252  char *s, *host, *port;
253  int e, i, res_cnt;
254 
255  if (!str) {
256  return 0;
257  }
258 
259  s = ast_strdupa(str);
260  if (!ast_sockaddr_split_hostport(s, &host, &port, flags)) {
261  return 0;
262  }
263 
264  memset(&hints, 0, sizeof(hints));
265  hints.ai_family = family;
266  hints.ai_socktype = SOCK_DGRAM;
267 
268  if ((e = getaddrinfo(host, port, &hints, &res))) {
269  ast_log(LOG_ERROR, "getaddrinfo(\"%s\", \"%s\", ...): %s\n",
270  host, S_OR(port, "(null)"), gai_strerror(e));
271  return 0;
272  }
273 
274  res_cnt = 0;
275  for (ai = res; ai; ai = ai->ai_next) {
276  res_cnt++;
277  }
278 
279  if (res_cnt == 0) {
280  goto cleanup;
281  }
282 
283  if ((*addrs = ast_malloc(res_cnt * sizeof(struct ast_sockaddr))) == NULL) {
284  res_cnt = 0;
285  goto cleanup;
286  }
287 
288  i = 0;
289  for (ai = res; ai; ai = ai->ai_next) {
290  (*addrs)[i].len = ai->ai_addrlen;
291  memcpy(&(*addrs)[i].ss, ai->ai_addr, ai->ai_addrlen);
292  ++i;
293  }
294 
295 cleanup:
296  freeaddrinfo(res);
297  return res_cnt;
298 }
299 
300 int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
301 {
302  const struct ast_sockaddr *a_tmp, *b_tmp;
303  struct ast_sockaddr ipv4_mapped;
304 
305  a_tmp = a;
306  b_tmp = b;
307 
308  if (a_tmp->len != b_tmp->len) {
309  if (ast_sockaddr_ipv4_mapped(a, &ipv4_mapped)) {
310  a_tmp = &ipv4_mapped;
311  } else if (ast_sockaddr_ipv4_mapped(b, &ipv4_mapped)) {
312  b_tmp = &ipv4_mapped;
313  }
314  }
315 
316  if (a_tmp->len < b_tmp->len) {
317  return -1;
318  } else if (a_tmp->len > b_tmp->len) {
319  return 1;
320  }
321 
322  return memcmp(&a_tmp->ss, &b_tmp->ss, a_tmp->len);
323 }
324 
325 int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
326 {
327  const struct ast_sockaddr *a_tmp, *b_tmp;
328  struct ast_sockaddr ipv4_mapped;
329  const struct in_addr *ip4a, *ip4b;
330  const struct in6_addr *ip6a, *ip6b;
331  int ret = -1;
332 
333  a_tmp = a;
334  b_tmp = b;
335 
336  if (a_tmp->len != b_tmp->len) {
337  if (ast_sockaddr_ipv4_mapped(a, &ipv4_mapped)) {
338  a_tmp = &ipv4_mapped;
339  } else if (ast_sockaddr_ipv4_mapped(b, &ipv4_mapped)) {
340  b_tmp = &ipv4_mapped;
341  }
342  }
343 
344  if (a->len < b->len) {
345  ret = -1;
346  } else if (a->len > b->len) {
347  ret = 1;
348  }
349 
350  switch (a_tmp->ss.ss_family) {
351  case AF_INET:
352  ip4a = &((const struct sockaddr_in*)&a_tmp->ss)->sin_addr;
353  ip4b = &((const struct sockaddr_in*)&b_tmp->ss)->sin_addr;
354  ret = memcmp(ip4a, ip4b, sizeof(*ip4a));
355  break;
356  case AF_INET6:
357  ip6a = &((const struct sockaddr_in6*)&a_tmp->ss)->sin6_addr;
358  ip6b = &((const struct sockaddr_in6*)&b_tmp->ss)->sin6_addr;
359  ret = memcmp(ip6a, ip6b, sizeof(*ip6a));
360  break;
361  }
362  return ret;
363 }
364 
365 uint16_t _ast_sockaddr_port(const struct ast_sockaddr *addr, const char *file, int line, const char *func)
366 {
367  if (addr->ss.ss_family == AF_INET &&
368  addr->len == sizeof(struct sockaddr_in)) {
369  return ntohs(((struct sockaddr_in *)&addr->ss)->sin_port);
370  } else if (addr->ss.ss_family == AF_INET6 &&
371  addr->len == sizeof(struct sockaddr_in6)) {
372  return ntohs(((struct sockaddr_in6 *)&addr->ss)->sin6_port);
373  }
374  if (option_debug >= 1) {
375  ast_log(__LOG_DEBUG, file, line, func, "Not an IPv4 nor IPv6 address, cannot get port.\n");
376  }
377  return 0;
378 }
379 
380 void _ast_sockaddr_set_port(struct ast_sockaddr *addr, uint16_t port, const char *file, int line, const char *func)
381 {
382  if (addr->ss.ss_family == AF_INET &&
383  addr->len == sizeof(struct sockaddr_in)) {
384  ((struct sockaddr_in *)&addr->ss)->sin_port = htons(port);
385  } else if (addr->ss.ss_family == AF_INET6 &&
386  addr->len == sizeof(struct sockaddr_in6)) {
387  ((struct sockaddr_in6 *)&addr->ss)->sin6_port = htons(port);
388  } else if (option_debug >= 1) {
389  ast_log(__LOG_DEBUG, file, line, func,
390  "Not an IPv4 nor IPv6 address, cannot set port.\n");
391  }
392 }
393 
394 uint32_t ast_sockaddr_ipv4(const struct ast_sockaddr *addr)
395 {
396  const struct sockaddr_in *sin = (struct sockaddr_in *)&addr->ss;
397  return ntohl(sin->sin_addr.s_addr);
398 }
399 
400 int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr)
401 {
402  return addr->ss.ss_family == AF_INET &&
403  addr->len == sizeof(struct sockaddr_in);
404 }
405 
407 {
408  const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr->ss;
409  return addr->len && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr);
410 }
411 
413 {
414  const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr->ss;
415  return ast_sockaddr_is_ipv6(addr) && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr);
416 }
417 
418 int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr)
419 {
420  return addr->ss.ss_family == AF_INET6 &&
421  addr->len == sizeof(struct sockaddr_in6);
422 }
423 
424 int ast_sockaddr_is_any(const struct ast_sockaddr *addr)
425 {
426  union {
427  struct sockaddr_storage ss;
428  struct sockaddr_in sin;
429  struct sockaddr_in6 sin6;
430  } tmp_addr = {
431  .ss = addr->ss,
432  };
433 
434  return (ast_sockaddr_is_ipv4(addr) && (tmp_addr.sin.sin_addr.s_addr == INADDR_ANY)) ||
435  (ast_sockaddr_is_ipv6(addr) && IN6_IS_ADDR_UNSPECIFIED(&tmp_addr.sin6.sin6_addr));
436 }
437 
438 int ast_sockaddr_hash(const struct ast_sockaddr *addr)
439 {
440  /*
441  * For IPv4, return the IP address as-is. For IPv6, return the last 32
442  * bits.
443  */
444  switch (addr->ss.ss_family) {
445  case AF_INET:
446  return ((const struct sockaddr_in *)&addr->ss)->sin_addr.s_addr;
447  case AF_INET6:
448  return ((uint32_t *)&((const struct sockaddr_in6 *)&addr->ss)->sin6_addr)[3];
449  default:
450  ast_log(LOG_ERROR, "Unknown address family '%d'.\n",
451  addr->ss.ss_family);
452  return 0;
453  }
454 }
455 
456 int ast_accept(int sockfd, struct ast_sockaddr *addr)
457 {
458  addr->len = sizeof(addr->ss);
459  return accept(sockfd, (struct sockaddr *)&addr->ss, &addr->len);
460 }
461 
462 int ast_bind(int sockfd, const struct ast_sockaddr *addr)
463 {
464  return bind(sockfd, (const struct sockaddr *)&addr->ss, addr->len);
465 }
466 
467 int ast_connect(int sockfd, const struct ast_sockaddr *addr)
468 {
469  return connect(sockfd, (const struct sockaddr *)&addr->ss, addr->len);
470 }
471 
472 int ast_getsockname(int sockfd, struct ast_sockaddr *addr)
473 {
474  addr->len = sizeof(addr->ss);
475  return getsockname(sockfd, (struct sockaddr *)&addr->ss, &addr->len);
476 }
477 
478 ssize_t ast_recvfrom(int sockfd, void *buf, size_t len, int flags,
479  struct ast_sockaddr *src_addr)
480 {
481  src_addr->len = sizeof(src_addr->ss);
482  return recvfrom(sockfd, buf, len, flags,
483  (struct sockaddr *)&src_addr->ss, &src_addr->len);
484 }
485 
486 ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags,
487  const struct ast_sockaddr *dest_addr)
488 {
489  return sendto(sockfd, buf, len, flags,
490  (const struct sockaddr *)&dest_addr->ss, dest_addr->len);
491 }
492 
493 int ast_set_qos(int sockfd, int tos, int cos, const char *desc)
494 {
495  int res = 0;
496  int set_tos;
497  int set_tclass;
498  struct ast_sockaddr addr;
499 
500  /* If the sock address is IPv6, the TCLASS field must be set. */
501  set_tclass = !ast_getsockname(sockfd, &addr) && ast_sockaddr_is_ipv6(&addr) ? 1 : 0;
502 
503  /* If the the sock address is IPv4 or (IPv6 set to any address [::]) set TOS bits */
504  set_tos = (!set_tclass || (set_tclass && ast_sockaddr_is_any(&addr))) ? 1 : 0;
505 
506  if (set_tos) {
507  if ((res = setsockopt(sockfd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) {
508  ast_log(LOG_WARNING, "Unable to set %s DSCP TOS value to %d (may be you have no "
509  "root privileges): %s\n", desc, tos, strerror(errno));
510  } else if (tos) {
511  ast_verb(2, "Using %s TOS bits %d\n", desc, tos);
512  }
513  }
514 
515 #if defined(IPV6_TCLASS) && defined(IPPROTO_IPV6)
516  if (set_tclass) {
517  if (!ast_getsockname(sockfd, &addr) && ast_sockaddr_is_ipv6(&addr)) {
518  if ((res = setsockopt(sockfd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)))) {
519  ast_log(LOG_WARNING, "Unable to set %s DSCP TCLASS field to %d (may be you have no "
520  "root privileges): %s\n", desc, tos, strerror(errno));
521  } else if (tos) {
522  ast_verb(2, "Using %s TOS bits %d in TCLASS field.\n", desc, tos);
523  }
524  }
525  }
526 #endif
527 
528 #ifdef linux
529  if (setsockopt(sockfd, SOL_SOCKET, SO_PRIORITY, &cos, sizeof(cos))) {
530  ast_log(LOG_WARNING, "Unable to set %s CoS to %d: %s\n", desc, cos,
531  strerror(errno));
532  } else if (cos) {
533  ast_verb(2, "Using %s CoS mark %d\n", desc, cos);
534  }
535 #endif
536 
537  return res;
538 }
539 
540 int _ast_sockaddr_to_sin(const struct ast_sockaddr *addr,
541  struct sockaddr_in *sin, const char *file, int line, const char *func)
542 {
543  if (ast_sockaddr_isnull(addr)) {
544  memset(sin, 0, sizeof(*sin));
545  return 1;
546  }
547 
548  if (addr->len != sizeof(*sin)) {
549  ast_log(__LOG_ERROR, file, line, func, "Bad address cast to IPv4\n");
550  return 0;
551  }
552 
553  if (addr->ss.ss_family != AF_INET && option_debug >= 1) {
554  ast_log(__LOG_DEBUG, file, line, func, "Address family is not AF_INET\n");
555  }
556 
557  *sin = *(struct sockaddr_in *)&addr->ss;
558  return 1;
559 }
560 
561 void _ast_sockaddr_from_sin(struct ast_sockaddr *addr, const struct sockaddr_in *sin,
562  const char *file, int line, const char *func)
563 {
564  memcpy(&addr->ss, sin, sizeof(*sin));
565 
566  if (addr->ss.ss_family != AF_INET && option_debug >= 1) {
567  ast_log(__LOG_DEBUG, file, line, func, "Address family is not AF_INET\n");
568  }
569 
570  addr->len = sizeof(*sin);
571 }
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:81
struct sockaddr_storage ss
Definition: netsock2.h:64
ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags, const struct ast_sockaddr *dest_addr)
Wrapper around sendto(2) that uses ast_sockaddr.
Definition: netsock2.c:486
Asterisk main include file. File version handling, generic pbx functions.
#define __LOG_DEBUG
Definition: logger.h:121
static unsigned int tos
Definition: chan_h323.c:146
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
Parse an IPv4 or IPv6 address string.
Definition: netsock2.c:198
int option_debug
Definition: asterisk.c:182
#define LOG_WARNING
Definition: logger.h:144
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
int ast_sockaddr_ipv4_mapped(const struct ast_sockaddr *addr, struct ast_sockaddr *ast_mapped)
Convert an IPv4-mapped IPv6 address into an IPv4 address.
Definition: netsock2.c:39
socklen_t len
Definition: netsock2.h:65
uint32_t ast_sockaddr_ipv4(const struct ast_sockaddr *addr)
Get an IPv4 address of an ast_sockaddr.
Definition: netsock2.c:394
#define __LOG_ERROR
Definition: logger.h:154
const char * str
Definition: app_jack.c:144
int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares two ast_sockaddr structures.
Definition: netsock2.c:300
Definitions to aid in the use of thread local storage.
Socket address structure.
Definition: netsock2.h:63
int ast_bind(int sockfd, const struct ast_sockaddr *addr)
Wrapper around bind(2) that uses struct ast_sockaddr.
Definition: netsock2.c:462
#define ast_verb(level,...)
Definition: logger.h:243
int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares the addresses of two ast_sockaddr structures.
Definition: netsock2.c:325
#define AST_SOCKADDR_STR_DEFAULT
Definition: netsock2.h:157
Utility functions.
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. &quot;null&quot; in this sense essentially means uninitialized, or having a 0 length.
Definition: netsock2.h:93
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:874
int _ast_sockaddr_to_sin(const struct ast_sockaddr *addr, struct sockaddr_in *sin, const char *file, int line, const char *func)
Definition: netsock2.c:540
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
int ast_sockaddr_is_any(const struct ast_sockaddr *addr)
Determine if the address type is unspecified, or &quot;any&quot; address.
Definition: netsock2.c:424
int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags)
Splits a string into its host and port components.
Definition: netsock2.c:132
int ast_accept(int sockfd, struct ast_sockaddr *addr)
Wrapper around accept(2) that uses struct ast_sockaddr.
Definition: netsock2.c:456
#define ast_sockaddr_from_sin(addr, sin)
Converts a struct sockaddr_in to a struct ast_sockaddr.
Definition: netsock2.h:642
int ast_sockaddr_hash(const struct ast_sockaddr *addr)
Computes a hash value from the address. The port is ignored.
Definition: netsock2.c:438
Network socket handling.
static const char desc[]
Definition: cdr_radius.c:85
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
#define LOG_ERROR
Definition: logger.h:155
#define AST_SOCKADDR_STR_PORT
Definition: netsock2.h:153
int ast_sockaddr_is_ipv6_link_local(const struct ast_sockaddr *addr)
Determine if this is a link-local IPv6 address.
Definition: netsock2.c:412
uint16_t _ast_sockaddr_port(const struct ast_sockaddr *addr, const char *file, int line, const char *func)
Definition: netsock2.c:365
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:364
#define AST_SOCKADDR_STR_HOST
Definition: netsock2.h:156
int ast_set_qos(int sockfd, int tos, int cos, const char *desc)
Set type of service.
Definition: netsock2.c:493
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
ssize_t ast_recvfrom(int sockfd, void *buf, size_t len, int flags, struct ast_sockaddr *src_addr)
Wrapper around recvfrom(2) that uses struct ast_sockaddr.
Definition: netsock2.c:478
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
char * ast_sockaddr_stringify_fmt(const struct ast_sockaddr *addr, int format)
Convert a socket address to a string.
Definition: netsock2.c:67
void _ast_sockaddr_set_port(struct ast_sockaddr *addr, uint16_t port, const char *file, int line, const char *func)
Definition: netsock2.c:380
int errno
int ast_sockaddr_is_ipv4_mapped(const struct ast_sockaddr *addr)
Determine if this is an IPv4-mapped IPv6 address.
Definition: netsock2.c:406
static void * cleanup(void *unused)
Definition: pbx_realtime.c:125
void _ast_sockaddr_from_sin(struct ast_sockaddr *addr, const struct sockaddr_in *sin, const char *file, int line, const char *func)
Definition: netsock2.c:561
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
#define AST_SOCKADDR_STR_ADDR
Definition: netsock2.h:152
int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr)
Determine if the address is an IPv4 address.
Definition: netsock2.c:400
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
Definition: strings.h:669
int ast_getsockname(int sockfd, struct ast_sockaddr *addr)
Wrapper around getsockname(2) that uses struct ast_sockaddr.
Definition: netsock2.c:472
static unsigned int cos
Definition: chan_h323.c:147
#define AST_SOCKADDR_STR_FORMAT_MASK
Definition: netsock2.h:161
#define ast_malloc(a)
Definition: astmm.h:91
static snd_pcm_format_t format
Definition: chan_alsa.c:93
static struct ast_threadstorage ast_sockaddr_stringify_buf
Definition: netsock2.c:65
int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr)
Determine if this is an IPv6 address.
Definition: netsock2.c:418
int ast_connect(int sockfd, const struct ast_sockaddr *addr)
Wrapper around connect(2) that uses struct ast_sockaddr.
Definition: netsock2.c:467
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str, int flags, int family)
Parses a string with an IPv4 or IPv6 address and place results into an array.
Definition: netsock2.c:248
#define AST_SOCKADDR_STR_REMOTE
Definition: netsock2.h:155