Asterisk - The Open Source Telephony Project  18.5.0
netsock2.h
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 <[email protected]>
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  * \brief Network socket handling
21  */
22 
23 #ifndef _ASTERISK_NETSOCK2_H
24 #define _ASTERISK_NETSOCK2_H
25 
26 #if defined(__cplusplus) || defined(c_plusplus)
27 extern "C" {
28 #endif
29 
30 #include <sys/socket.h>
31 
32 #include <netinet/in.h>
33 
34 #include "asterisk/logger.h"
35 
36 /*
37  * String buffer size that can accommodate a fully stringified representation of a
38  * supported IP address & port:
39  *
40  * - 45 bytes for an IPv6 address
41  * - 2 bytes for brackets around an IPv6 address
42  * - 1 byte for the port separator (a colon)
43  * - 5 bytes for the port
44  * - 1 byte for the zero-terminator
45  */
46 #define AST_SOCKADDR_BUFLEN (45 + 2 + 1 + 5 + 1)
47 
48 /*!
49  * Values for address families that we support. This is reproduced from socket.h
50  * because we do not want users to include that file. Only netsock2.c should
51  * ever include socket.h.
52  */
53 enum {
54  AST_AF_UNSPEC = AF_UNSPEC,
55  AST_AF_INET = AF_INET,
56  AST_AF_INET6 = AF_INET6,
57 };
58 
63  AST_TRANSPORT_WS = 1 << 3,
65 };
66 
67 /*!
68  * \brief
69  * Isolate a 32-bit section of an IPv6 address
70  *
71  * An IPv6 address can be divided into 4 32-bit chunks. This gives
72  * easy access to one of these chunks.
73  *
74  * \param sin6 A pointer to a struct sockaddr_in6
75  * \param index Which 32-bit chunk to operate on. Must be in the range 0-3.
76  */
77 #define V6_WORD(sin6, index) ((uint32_t *)&((sin6)->sin6_addr))[(index)]
78 
79 /*!
80  * \brief Socket address structure.
81  *
82  * \details
83  * The first member is big enough to contain addresses of any
84  * family. The second member contains the length (in bytes) used
85  * in the first member.
86  *
87  * \note
88  * Some BSDs have the length embedded in sockaddr structs. We
89  * ignore them. (This is the right thing to do.)
90  *
91  * \note
92  * It is important to always initialize ast_sockaddr before use
93  * -- even if they are passed to ast_sockaddr_copy() as the
94  * underlying storage could be bigger than what ends up being
95  * copied -- leaving part of the data unitialized.
96  */
97 struct ast_sockaddr {
98  struct sockaddr_storage ss;
99  socklen_t len;
100 };
101 
102 /*!
103  * \brief
104  * Convert an IPv4-mapped IPv6 address into an IPv4 address.
105  *
106  * \warning You should rarely need this function. Only call this
107  * if you know what you're doing.
108  *
109  * \param addr The IPv4-mapped address to convert
110  * \param ast_mapped The resulting IPv4 address
111  * \retval 0 Unable to make the conversion
112  * \retval 1 Successful conversion
113  */
114 int ast_sockaddr_ipv4_mapped(const struct ast_sockaddr *addr, struct ast_sockaddr *ast_mapped);
115 
116 /*!
117  * \since 1.8
118  *
119  * \brief
120  * Checks if the ast_sockaddr is null. "null" in this sense essentially
121  * means uninitialized, or having a 0 length.
122  *
123  * \param addr Pointer to the ast_sockaddr we wish to check
124  * \retval 1 \a addr is null
125  * \retval 0 \a addr is non-null.
126  */
127 static inline int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
128 {
129  return !addr || addr->len == 0;
130 }
131 
132 /*!
133  * \since 1.8
134  *
135  * \brief
136  * Sets address \a addr to null.
137  *
138  * \retval void
139  */
140 static inline void ast_sockaddr_setnull(struct ast_sockaddr *addr)
141 {
142  addr->len = 0;
143 }
144 
145 /*!
146  * \brief
147  * Copies the data from a sockaddr to an ast_sockaddr
148  *
149  * \param dst The destination ast_sockaddr
150  * \param src The source sockaddr
151  * \param len Length of the value stored in sockaddr
152  * \retval void
153  */
154 static inline void ast_sockaddr_copy_sockaddr(struct ast_sockaddr *dst,
155  struct sockaddr *src, socklen_t len)
156 {
157  memcpy(dst, src, len);
158  dst->len = len;
159 }
160 
161 /*!
162  * \since 1.8
163  *
164  * \brief
165  * Copies the data from one ast_sockaddr to another
166  *
167  * \param dst The destination ast_sockaddr
168  * \param src The source ast_sockaddr
169  * \retval void
170  */
171 static inline void ast_sockaddr_copy(struct ast_sockaddr *dst,
172  const struct ast_sockaddr *src)
173 {
174  memcpy(dst, src, src->len);
175  dst->len = src->len;
176 };
177 
178 /*!
179  * \since 1.8
180  *
181  * \brief
182  * Compares two ast_sockaddr structures
183  *
184  * \retval -1 \a a is lexicographically smaller than \a b
185  * \retval 0 \a a is equal to \a b
186  * \retval 1 \a b is lexicographically smaller than \a a
187  */
188 int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b);
189 
190 /*!
191  * \since 1.8
192  *
193  * \brief
194  * Compares the addresses of two ast_sockaddr structures.
195  *
196  * \retval -1 \a a is lexicographically smaller than \a b
197  * \retval 0 \a a is equal to \a b
198  * \retval 1 \a b is lexicographically smaller than \a a
199  */
200 int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b);
201 
202 #define AST_SOCKADDR_STR_ADDR (1 << 0)
203 #define AST_SOCKADDR_STR_PORT (1 << 1)
204 #define AST_SOCKADDR_STR_BRACKETS (1 << 2)
205 #define AST_SOCKADDR_STR_REMOTE (1 << 3)
206 #define AST_SOCKADDR_STR_HOST (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_BRACKETS)
207 #define AST_SOCKADDR_STR_DEFAULT (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT)
208 #define AST_SOCKADDR_STR_ADDR_REMOTE (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_REMOTE)
209 #define AST_SOCKADDR_STR_HOST_REMOTE (AST_SOCKADDR_STR_HOST | AST_SOCKADDR_STR_REMOTE)
210 #define AST_SOCKADDR_STR_DEFAULT_REMOTE (AST_SOCKADDR_STR_DEFAULT | AST_SOCKADDR_STR_REMOTE)
211 #define AST_SOCKADDR_STR_FORMAT_MASK (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT | AST_SOCKADDR_STR_BRACKETS)
212 
213 /*!
214  * \since 1.8
215  *
216  * \brief
217  * Convert a socket address to a string.
218  *
219  * \details
220  * This will be of the form a.b.c.d:xyz
221  * for IPv4 and [a:b:c:...:d]:xyz for IPv6.
222  *
223  * This function is thread-safe. The returned string is on static
224  * thread-specific storage.
225  *
226  * \param addr The input to be stringified
227  * \param format one of the following:
228  * AST_SOCKADDR_STR_DEFAULT:
229  * a.b.c.d:xyz for IPv4
230  * [a:b:c:...:d]:xyz for IPv6.
231  * AST_SOCKADDR_STR_ADDR: address only
232  * a.b.c.d for IPv4
233  * a:b:c:...:d for IPv6.
234  * AST_SOCKADDR_STR_HOST: address only, suitable for a URL
235  * a.b.c.d for IPv4
236  * [a:b:c:...:d] for IPv6.
237  * AST_SOCKADDR_STR_PORT: port only
238  *
239  * \note The string pointer returned by this function will point to a string that
240  * will be changed whenever any form of ast_sockaddr_stringify_fmt is called on that
241  * thread. Because of this, it is important that if you use this function, you use the
242  * string before another use of this function is made elsewhere in the same thread.
243  * The easiest way to accomplish this is by immediately copying the string to a buffer
244  * with something like ast_strdupa.
245  *
246  * \retval "(null)" \a addr is null
247  * \retval "" An error occurred during processing
248  * \retval string The stringified form of the address
249  */
250 char *ast_sockaddr_stringify_fmt(const struct ast_sockaddr *addr, int format);
251 
252 /*!
253  * \since 1.8
254  *
255  * \brief
256  * Wrapper around ast_sockaddr_stringify_fmt() with default format
257  *
258  * \return same as ast_sockaddr_stringify_fmt()
259  */
260 static inline char *ast_sockaddr_stringify(const struct ast_sockaddr *addr)
261 {
263 }
264 
265 /*!
266  * \since 1.8
267  *
268  * \brief
269  * Wrapper around ast_sockaddr_stringify_fmt() with default format
270  *
271  * \note This address will be suitable for passing to a remote machine via the
272  * application layer. For example, the scope-id on a link-local IPv6 address
273  * will be stripped.
274  *
275  * \return same as ast_sockaddr_stringify_fmt()
276  */
277 static inline char *ast_sockaddr_stringify_remote(const struct ast_sockaddr *addr)
278 {
280 }
281 
282 /*!
283  * \since 1.8
284  *
285  * \brief
286  * Wrapper around ast_sockaddr_stringify_fmt() to return an address only
287  *
288  * \return same as ast_sockaddr_stringify_fmt()
289  */
290 static inline char *ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
291 {
293 }
294 
295 /*!
296  * \since 12.4
297  *
298  * \brief
299  * Count the 1 bits in a netmask
300  *
301  * \return number of 1 bits
302  */
303 int ast_sockaddr_cidr_bits(const struct ast_sockaddr *sa);
304 
305 /*!
306  * \since 1.8
307  *
308  * \brief
309  * Wrapper around ast_sockaddr_stringify_fmt() to return an address only
310  *
311  * \note This address will be suitable for passing to a remote machine via the
312  * application layer. For example, the scope-id on a link-local IPv6 address
313  * will be stripped.
314  *
315  * \return same as ast_sockaddr_stringify_fmt()
316  */
317 static inline char *ast_sockaddr_stringify_addr_remote(const struct ast_sockaddr *addr)
318 {
320 }
321 
322 /*!
323  * \since 1.8
324  *
325  * \brief
326  * Wrapper around ast_sockaddr_stringify_fmt() to return an address only,
327  * suitable for a URL (with brackets for IPv6).
328  *
329  * \return same as ast_sockaddr_stringify_fmt()
330  */
331 static inline char *ast_sockaddr_stringify_host(const struct ast_sockaddr *addr)
332 {
334 }
335 
336 /*!
337  * \since 1.8
338  *
339  * \brief
340  * Wrapper around ast_sockaddr_stringify_fmt() to return an address only,
341  * suitable for a URL (with brackets for IPv6).
342  *
343  * \note This address will be suitable for passing to a remote machine via the
344  * application layer. For example, the scope-id on a link-local IPv6 address
345  * will be stripped.
346  *
347  * \return same as ast_sockaddr_stringify_fmt()
348  */
349 static inline char *ast_sockaddr_stringify_host_remote(const struct ast_sockaddr *addr)
350 {
352 }
353 
354 /*!
355  * \since 1.8
356  *
357  * \brief
358  * Wrapper around ast_sockaddr_stringify_fmt() to return a port only
359  *
360  * \return same as ast_sockaddr_stringify_fmt()
361  */
362 static inline char *ast_sockaddr_stringify_port(const struct ast_sockaddr *addr)
363 {
365 }
366 
367 /*!
368  * \since 1.8
369  *
370  * \brief
371  * Splits a string into its host and port components
372  *
373  * \param[in] str The string to parse. May be modified by writing a NUL at the end of
374  * the host part.
375  * \param[out] host Pointer to the host component within \a str.
376  * \param[out] port Pointer to the port component within \a str.
377  * \param flags If set to zero, a port MAY be present. If set to PARSE_PORT_IGNORE, a
378  * port MAY be present but will be ignored. If set to PARSE_PORT_REQUIRE,
379  * a port MUST be present. If set to PARSE_PORT_FORBID, a port MUST NOT
380  * be present.
381  *
382  * \retval 1 Success
383  * \retval 0 Failure
384  */
385 int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags);
386 
387 /*!
388  * \since 1.8
389  *
390  * \brief
391  * Parse an IPv4 or IPv6 address string.
392  *
393  * \details
394  * Parses a string containing an IPv4 or IPv6 address followed by an optional
395  * port (separated by a colon) into a struct ast_sockaddr. The allowed formats
396  * are the following:
397  *
398  * a.b.c.d
399  * a.b.c.d:port
400  * a:b:c:...:d
401  * [a:b:c:...:d]
402  * [a:b:c:...:d]:port
403  *
404  * Host names are NOT allowed.
405  *
406  * \param[out] addr The resulting ast_sockaddr. This MAY be NULL from
407  * functions that are performing validity checks only, e.g. ast_parse_arg().
408  * \param str The string to parse
409  * \param flags If set to zero, a port MAY be present. If set to
410  * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
411  * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
412  * port MUST NOT be present.
413  *
414  * \retval 1 Success
415  * \retval 0 Failure
416  */
417 int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags);
418 
419 /*!
420  * \since 1.8
421  *
422  * \brief
423  * Parses a string with an IPv4 or IPv6 address and place results into an array
424  *
425  * \details
426  * Parses a string containing a host name or an IPv4 or IPv6 address followed
427  * by an optional port (separated by a colon). The result is returned into a
428  * array of struct ast_sockaddr. Allowed formats for str are the following:
429  *
430  * hostname:port
431  * host.example.com:port
432  * a.b.c.d
433  * a.b.c.d:port
434  * a:b:c:...:d
435  * [a:b:c:...:d]
436  * [a:b:c:...:d]:port
437  *
438  * \param[out] addrs The resulting array of ast_sockaddrs
439  * \param str The string to parse
440  * \param flags If set to zero, a port MAY be present. If set to
441  * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
442  * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
443  * port MUST NOT be present.
444  *
445  * \param family Only addresses of the given family will be returned. Use 0 or
446  * AST_AF_UNSPEC to get addresses of all families.
447  *
448  * \retval 0 Failure
449  * \retval non-zero The number of elements in addrs array.
450  */
451 int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str,
452  int flags, int family);
453 
454 /*!
455  * \since 16.0
456  *
457  * \brief
458  * Return the first entry from ast_sockaddr_resolve filtered by address family
459  *
460  * \details
461  * Parses a string containing a host name or an IPv4 or IPv6 address followed
462  * by an optional port (separated by a colon). This function only returns the
463  * first address into the ast_sockaddr. Allowed formats for name are the following:
464  *
465  * hostname:port
466  * host.example.com:port
467  * a.b.c.d
468  * a.b.c.d:port
469  * a:b:c:...:d
470  * [a:b:c:...:d]
471  * [a:b:c:...:d]:port
472  *
473  * \param[out] addr The resulting ast_sockaddr
474  * \param name The string to parse
475  * \param flags If set to zero, a port MAY be present. If set to
476  * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
477  * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
478  * port MUST NOT be present.
479  *
480  * \param family Only addresses of the given family will be returned. Use 0 or
481  * AST_AF_UNSPEC to specify any address family. Behavior is ultimately determined
482  * by getaddrinfo in how it orders return results. First result is selected to
483  * be returned.
484  *
485  * \retval 0 Success
486  * \retval non-zero Failure
487  * \warning Using this function potentially means you have a faulty design.
488  */
490  const char* name, int flag, int family);
491 
492 /*!
493  * \brief
494  * Apply a netmask to an address and store the result in a separate structure.
495  *
496  * When dealing with IPv6 addresses, one cannot apply a netmask with a simple
497  * logical AND operation. Futhermore, the incoming address may be an IPv4
498  * address and needs to be mapped properly before attempting to apply a rule.
499  *
500  * \param addr The IP address to apply the mask to.
501  * \param netmask The netmask configured in the host access rule.
502  * \param result The resultant address after applying the netmask to the given address
503  * \retval 0 Successfully applied netmask
504  * \retval -1 Failed to apply netmask
505  */
506 int ast_sockaddr_apply_netmask(const struct ast_sockaddr *addr, const struct ast_sockaddr *netmask,
507  struct ast_sockaddr *result);
508 
509 /*!
510  * \since 1.8
511  *
512  * \brief
513  * Get the port number of a socket address.
514  *
515  * \warning Do not use this function unless you really know what you are doing.
516  * And "I want the port number" is not knowing what you are doing.
517  *
518  * \retval 0 Address is null
519  * \retval non-zero The port number of the ast_sockaddr
520  */
521 #define ast_sockaddr_port(addr) _ast_sockaddr_port(addr, __FILE__, __LINE__, __PRETTY_FUNCTION__)
522 uint16_t _ast_sockaddr_port(const struct ast_sockaddr *addr, const char *file, int line, const char *func);
523 
524 /*!
525  * \since 1.8
526  *
527  * \brief
528  * Sets the port number of a socket address.
529  *
530  * \warning Do not use this function unless you really know what you are doing.
531  * And "I want the port number" is not knowing what you are doing.
532  *
533  * \param addr Address on which to set the port
534  * \param port The port you wish to set the address to use
535  * \retval void
536  */
537 #define ast_sockaddr_set_port(addr,port) _ast_sockaddr_set_port(addr,port,__FILE__,__LINE__,__PRETTY_FUNCTION__)
538 void _ast_sockaddr_set_port(struct ast_sockaddr *addr, uint16_t port, const char *file, int line, const char *func);
539 
540 /*!
541  * \since 1.8
542  *
543  * \brief
544  * Get an IPv4 address of an ast_sockaddr
545  *
546  * \warning You should rarely need this function. Only use if you know what
547  * you're doing.
548  * \return IPv4 address in network byte order
549  */
550 uint32_t ast_sockaddr_ipv4(const struct ast_sockaddr *addr);
551 
552 /*!
553  * \since 1.8
554  *
555  * \brief
556  * Determine if the address is an IPv4 address
557  *
558  * \warning You should rarely need this function. Only use if you know what
559  * you're doing.
560  * \retval 1 This is an IPv4 address
561  * \retval 0 This is an IPv6 or IPv4-mapped IPv6 address
562  */
563 int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr);
564 
565 /*!
566  * \since 1.8
567  *
568  * \brief
569  * Determine if this is an IPv4-mapped IPv6 address
570  *
571  * \warning You should rarely need this function. Only use if you know what
572  * you're doing.
573  *
574  * \retval 1 This is an IPv4-mapped IPv6 address.
575  * \retval 0 This is not an IPv4-mapped IPv6 address.
576  */
577 int ast_sockaddr_is_ipv4_mapped(const struct ast_sockaddr *addr);
578 
579 /*!
580  * \since 10.0
581  *
582  * \brief
583  * Determine if an IPv4 address is a multicast address
584  *
585  * \param addr the address to check
586  *
587  * This function checks if an address is in the 224.0.0.0/4 network block.
588  *
589  * \return non-zero if this is a multicast address
590  */
591 int ast_sockaddr_is_ipv4_multicast(const struct ast_sockaddr *addr);
592 
593 /*!
594  * \since 1.8
595  *
596  * \brief
597  * Determine if this is a link-local IPv6 address
598  *
599  * \warning You should rarely need this function. Only use if you know what
600  * you're doing.
601  *
602  * \retval 1 This is a link-local IPv6 address.
603  * \retval 0 This is link-local IPv6 address.
604  */
605 int ast_sockaddr_is_ipv6_link_local(const struct ast_sockaddr *addr);
606 
607 /*!
608  * \since 1.8
609  *
610  * \brief
611  * Determine if this is an IPv6 address
612  *
613  * \warning You should rarely need this function. Only use if you know what
614  * you're doing.
615  *
616  * \retval 1 This is an IPv6 or IPv4-mapped IPv6 address.
617  * \retval 0 This is an IPv4 address.
618  */
619 int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr);
620 
621 /*!
622  * \since 1.8
623  *
624  * \brief
625  * Determine if the address type is unspecified, or "any" address.
626  *
627  * \details
628  * For IPv4, this would be the address 0.0.0.0, and for IPv6,
629  * this would be the address ::. The port number is ignored.
630  *
631  * \retval 1 This is an "any" address
632  * \retval 0 This is not an "any" address
633  */
634 int ast_sockaddr_is_any(const struct ast_sockaddr *addr);
635 
636 /*!
637  * \since 1.8
638  *
639  * \brief
640  * Computes a hash value from the address. The port is ignored.
641  *
642  * \retval 0 Unknown address family
643  * \retval other A 32-bit hash derived from the address
644  */
645 int ast_sockaddr_hash(const struct ast_sockaddr *addr);
646 
647 /*!
648  * \since 12.3
649  *
650  * \brief
651  * Returns a string representation of an ast_transport
652  *
653  * \retval Name of the tranpsort if it is defined
654  * \retval Undefined if the transport is undefined
655  */
656 const char *ast_transport2str(enum ast_transport transport);
657 
658 /*!
659  * \since 1.8
660  *
661  * \brief
662  * Wrapper around accept(2) that uses struct ast_sockaddr.
663  *
664  * \details
665  * For parameter and return information, see the man page for
666  * accept(2).
667  */
668 int ast_accept(int sockfd, struct ast_sockaddr *addr);
669 
670 /*!
671  * \since 1.8
672  *
673  * \brief
674  * Wrapper around bind(2) that uses struct ast_sockaddr.
675  *
676  * \details
677  * For parameter and return information, see the man page for
678  * bind(2).
679  */
680 int ast_bind(int sockfd, const struct ast_sockaddr *addr);
681 
682 /*!
683  * \since 1.8
684  *
685  * \brief
686  * Wrapper around connect(2) that uses struct ast_sockaddr.
687  *
688  * \details
689  * For parameter and return information, see the man page for
690  * connect(2).
691  */
692 int ast_connect(int sockfd, const struct ast_sockaddr *addr);
693 
694 /*!
695  * \since 1.8
696  *
697  * \brief
698  * Wrapper around getsockname(2) that uses struct ast_sockaddr.
699  *
700  * \details
701  * For parameter and return information, see the man page for
702  * getsockname(2).
703  */
704 int ast_getsockname(int sockfd, struct ast_sockaddr *addr);
705 
706 /*!
707  * \since 1.8
708  *
709  * \brief
710  * Wrapper around recvfrom(2) that uses struct ast_sockaddr.
711  *
712  * \details
713  * For parameter and return information, see the man page for
714  * recvfrom(2).
715  */
716 ssize_t ast_recvfrom(int sockfd, void *buf, size_t len, int flags,
717  struct ast_sockaddr *src_addr);
718 
719 /*!
720  * \since 1.8
721  *
722  * \brief
723  * Wrapper around sendto(2) that uses ast_sockaddr.
724  *
725  * \details
726  * For parameter and
727  * return information, see the man page for sendto(2)
728  */
729 ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags,
730  const struct ast_sockaddr *dest_addr);
731 
732 /*!
733  * \since 1.8
734  *
735  * \brief
736  * Set type of service
737  *
738  * \details
739  * Set ToS ("Type of Service for IPv4 and "Traffic Class for IPv6) and
740  * CoS (Linux's SO_PRIORITY)
741  *
742  * \param sockfd File descriptor for socket on which to set the parameters
743  * \param tos The type of service for the socket
744  * \param cos The cost of service for the socket
745  * \param desc A text description of the socket in question.
746  * \retval 0 Success
747  * \retval -1 Error, with errno set to an appropriate value
748  */
749 int ast_set_qos(int sockfd, int tos, int cos, const char *desc);
750 
751 /*!
752  * These are backward compatibility functions that may be used by subsystems
753  * that have not yet been converted to IPv6. They will be removed when all
754  * subsystems are IPv6-ready.
755  */
756 /*@{*/
757 
758 /*!
759  * \since 1.8
760  *
761  * \brief
762  * Converts a struct ast_sockaddr to a struct sockaddr_in.
763  *
764  * \param addr The ast_sockaddr to convert
765  * \param[out] sin The resulting sockaddr_in struct
766  * \retval nonzero Success
767  * \retval zero Failure
768  */
769 #define ast_sockaddr_to_sin(addr,sin) _ast_sockaddr_to_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)
770 int _ast_sockaddr_to_sin(const struct ast_sockaddr *addr,
771  struct sockaddr_in *sin, const char *file, int line, const char *func);
772 
773 /*!
774  * \since 1.8
775  *
776  * \brief Converts a struct sockaddr_in to a struct ast_sockaddr.
777  *
778  * \param addr
779  * \param sin The sockaddr_in to convert
780  * \return an ast_sockaddr structure
781  */
782 #define ast_sockaddr_from_sin(addr,sin) _ast_sockaddr_from_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)
783 void _ast_sockaddr_from_sin(struct ast_sockaddr *addr, const struct sockaddr_in *sin,
784  const char *file, int line, const char *func);
785 
786 /*!
787  * \since 13.31.0, 16.8.0, 17.2.0
788  *
789  * \brief Takes an AF_XXX value as input and returns the size of the underlying
790  * sockaddr structure if known, or zero if not.
791  *
792  * \param family The AF_XXX value to determine the size of
793  * \return Size of the applicable struct sockaddr.
794  */
795 #define ast_addressfamily_to_sockaddrsize(family) _ast_addressfamily_to_sockaddrsize(family, __FILE__, __LINE__, __PRETTY_FUNCTION__)
796 static inline int _ast_addressfamily_to_sockaddrsize(int af, const char *file, int line, const char *func)
797 {
798  switch (af) {
799  case AF_INET:
800  return sizeof(struct sockaddr_in);
801  case AF_INET6:
802  return sizeof(struct sockaddr_in6);
803  default:
804  ast_log(__LOG_WARNING, file, line, func, "Unknown address family %d encountered.\n", af);
805  return 0;
806  }
807 }
808 
809 /*!
810  * \since 13.31.0, 16.8.0, 17.2.0
811  *
812  * \brief Converts a struct sockaddr to a struct ast_sockaddr.
813  *
814  * Note that there is an underlying assumption that sockaddr data is valid, more specifically,
815  * if sa_family is set to AF_INET that it's actually a sockaddr_in, and in the case of AF_INET6
816  * a valid sockaddr_in6 structure.
817  *
818  * You can check for failure with ast_sockaddr_isnull.
819  *
820  * \param[out] addr The address of the ast_sockaddr to store into
821  * \param sa The sockaddr structure (sockaddr_in or sockaddr_in6) to convert
822  * \return Nothing
823  */
824 #define ast_sockaddr_from_sockaddr(addr,sa) ast_sockaddr_copy_sockaddr(addr, sa, ast_addressfamily_to_sockaddrsize(((const struct sockaddr*)(sa))->sa_family))
825 
826 /*@}*/
827 
828 #if defined(__cplusplus) || defined(c_plusplus)
829 }
830 #endif
831 
832 #endif /* _ASTERISK_NETSOCK2_H */
static char * ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only.
Definition: netsock2.h:290
struct sockaddr_storage ss
Definition: netsock2.h:98
unsigned int cos
Definition: chan_iax2.c:352
static int _ast_addressfamily_to_sockaddrsize(int af, const char *file, int line, const char *func)
Definition: netsock2.h:796
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:614
static char * ast_sockaddr_stringify_addr_remote(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only.
Definition: netsock2.h:317
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
Parse an IPv4 or IPv6 address string.
Definition: netsock2.c:230
static void ast_sockaddr_copy(struct ast_sockaddr *dst, const struct ast_sockaddr *src)
Copies the data from one ast_sockaddr to another.
Definition: netsock2.h:171
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
ast_transport
Definition: netsock2.h:59
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:37
socklen_t len
Definition: netsock2.h:99
static const char desc[]
Definition: cdr_mysql.c:73
static char * ast_sockaddr_stringify_remote(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:277
uint32_t ast_sockaddr_ipv4(const struct ast_sockaddr *addr)
Get an IPv4 address of an ast_sockaddr.
Definition: netsock2.c:491
#define __LOG_WARNING
Definition: logger.h:273
const char * str
Definition: app_jack.c:147
int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares two ast_sockaddr structures.
Definition: netsock2.c:388
Socket address structure.
Definition: netsock2.h:97
int ast_bind(int sockfd, const struct ast_sockaddr *addr)
Wrapper around bind(2) that uses struct ast_sockaddr.
Definition: netsock2.c:590
#define AST_SOCKADDR_STR_DEFAULT_REMOTE
Definition: netsock2.h:210
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:413
#define AST_SOCKADDR_STR_DEFAULT
Definition: netsock2.h:207
static void ast_sockaddr_setnull(struct ast_sockaddr *addr)
Sets address addr to null.
Definition: netsock2.h:140
unsigned int tos
Definition: chan_iax2.c:351
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized, or having a 0 length.
Definition: netsock2.h:127
static char * ast_sockaddr_stringify_port(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return a port only.
Definition: netsock2.h:362
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:668
#define ast_log
Definition: astobj2.c:42
int ast_sockaddr_is_any(const struct ast_sockaddr *addr)
Determine if the address type is unspecified, or "any" address.
Definition: netsock2.c:534
const char * ast_transport2str(enum ast_transport transport)
Returns a string representation of an ast_transport.
Definition: netsock2.c:566
static char host[256]
Definition: muted.c:77
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:164
int ast_accept(int sockfd, struct ast_sockaddr *addr)
Wrapper around accept(2) that uses struct ast_sockaddr.
Definition: netsock2.c:584
#define AST_SOCKADDR_STR_HOST_REMOTE
Definition: netsock2.h:209
static void ast_sockaddr_copy_sockaddr(struct ast_sockaddr *dst, struct sockaddr *src, socklen_t len)
Copies the data from a sockaddr to an ast_sockaddr.
Definition: netsock2.h:154
int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr, const char *name, int flag, int family)
Return the first entry from ast_sockaddr_resolve filtered by address family.
Definition: netsock2.c:337
int ast_sockaddr_hash(const struct ast_sockaddr *addr)
Computes a hash value from the address. The port is ignored.
Definition: netsock2.c:548
int ast_sockaddr_apply_netmask(const struct ast_sockaddr *addr, const struct ast_sockaddr *netmask, struct ast_sockaddr *result)
Apply a netmask to an address and store the result in a separate structure.
Definition: netsock2.c:357
#define AST_SOCKADDR_STR_PORT
Definition: netsock2.h:203
int ast_sockaddr_is_ipv6_link_local(const struct ast_sockaddr *addr)
Determine if this is a link-local IPv6 address.
Definition: netsock2.c:518
uint16_t _ast_sockaddr_port(const struct ast_sockaddr *addr, const char *file, int line, const char *func)
Definition: netsock2.c:453
#define AST_SOCKADDR_STR_HOST
Definition: netsock2.h:206
int ast_set_qos(int sockfd, int tos, int cos, const char *desc)
Set type of service.
Definition: netsock2.c:621
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:260
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:606
char * ast_sockaddr_stringify_fmt(const struct ast_sockaddr *addr, int format)
Convert a socket address to a string.
Definition: netsock2.c:65
void _ast_sockaddr_set_port(struct ast_sockaddr *addr, uint16_t port, const char *file, int line, const char *func)
Definition: netsock2.c:473
long int flag
Definition: f2c.h:83
int ast_sockaddr_is_ipv4_multicast(const struct ast_sockaddr *addr)
Determine if an IPv4 address is a multicast address.
Definition: netsock2.c:513
static char * ast_sockaddr_stringify_host_remote(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only, suitable for a URL (with brack...
Definition: netsock2.h:349
static const char name[]
Definition: cdr_mysql.c:74
#define AST_SOCKADDR_STR_ADDR_REMOTE
Definition: netsock2.h:208
int ast_sockaddr_is_ipv4_mapped(const struct ast_sockaddr *addr)
Determine if this is an IPv4-mapped IPv6 address.
Definition: netsock2.c:507
int ast_sockaddr_cidr_bits(const struct ast_sockaddr *sa)
Count the 1 bits in a netmask.
Definition: netsock2.c:130
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:689
Support for logging to various files, console and syslog Configuration in file logger.conf.
#define AST_SOCKADDR_STR_ADDR
Definition: netsock2.h:202
static PGresult * result
Definition: cel_pgsql.c:88
int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr)
Determine if the address is an IPv4 address.
Definition: netsock2.c:497
static struct test_val b
int ast_getsockname(int sockfd, struct ast_sockaddr *addr)
Wrapper around getsockname(2) that uses struct ast_sockaddr.
Definition: netsock2.c:600
static snd_pcm_format_t format
Definition: chan_alsa.c:102
int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr)
Determine if this is an IPv6 address.
Definition: netsock2.c:524
int ast_connect(int sockfd, const struct ast_sockaddr *addr)
Wrapper around connect(2) that uses struct ast_sockaddr.
Definition: netsock2.c:595
static char * ast_sockaddr_stringify_host(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only, suitable for a URL (with brack...
Definition: netsock2.h:331
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:280
static struct test_val a