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