1 /*!
2  * \file        sccp_netsock.h
3  * \brief       SCCP NetSock Header
4  * \author      Diederik de Groot < ddegroot@users.sourceforge.net >
5  * \note        This program is free software and may be modified and distributed under the terms of the GNU Public License.
6  *              See the LICENSE file at the top of the source tree.
7  * \since       2016-02-02
8  */
9 #pragma once
10 #include "config.h"
11 #include "define.h"
12 #include <netinet/in.h>
13 #if HAVE_SYS_SOCKET_H
14 #include <sys/socket.h>
15 #endif
16 
17 /* forward declaration */
18 struct ast_sockaddr;
19 
20 #define SCCP_SOCKADDR_STR_ADDR           (1 << 0)
21 #define SCCP_SOCKADDR_STR_PORT           (1 << 1)
22 #define SCCP_SOCKADDR_STR_BRACKETS       (1 << 2)
23 #define SCCP_SOCKADDR_STR_REMOTE         (1 << 3)
24 #define SCCP_SOCKADDR_STR_HOST           (SCCP_SOCKADDR_STR_ADDR | SCCP_SOCKADDR_STR_BRACKETS)
25 #define SCCP_SOCKADDR_STR_DEFAULT        (SCCP_SOCKADDR_STR_ADDR | SCCP_SOCKADDR_STR_PORT)
26 #define SCCP_SOCKADDR_STR_ADDR_REMOTE     (SCCP_SOCKADDR_STR_ADDR | SCCP_SOCKADDR_STR_REMOTE)
27 #define SCCP_SOCKADDR_STR_HOST_REMOTE     (SCCP_SOCKADDR_STR_HOST | SCCP_SOCKADDR_STR_REMOTE)
28 #define SCCP_SOCKADDR_STR_DEFAULT_REMOTE  (SCCP_SOCKADDR_STR_DEFAULT | SCCP_SOCKADDR_STR_REMOTE)
29 #define SCCP_SOCKADDR_STR_FORMAT_MASK     (SCCP_SOCKADDR_STR_ADDR | SCCP_SOCKADDR_STR_PORT | SCCP_SOCKADDR_STR_BRACKETS)
30 
31 /*!
32  * \brief SCCP Host Access Rule Structure
33  *
34  * internal representation of acl entries In principle user applications would have no need for this,
35  * but there is sometimes a need to extract individual items, e.g. to print them, and rather than defining iterators to
36  * navigate the list, and an externally visible 'struct ast_ha_entry', at least in the short term it is more convenient to make the whole
37  * thing public and let users play with them.
38  */
39 struct sccp_ha {
40 	struct sockaddr_storage netaddr;
41 	struct sockaddr_storage netmask;
42 	struct sccp_ha *next;
43 	int sense;
44 };
45 
46 __BEGIN_C_EXTERN__
47 SCCP_INLINE struct sockaddr_storage * SCCP_CALL ast_sockaddr2storage(struct ast_sockaddr * src);
48 SCCP_INLINE struct ast_sockaddr * SCCP_CALL storage2ast_sockaddr(struct sockaddr_storage * src, struct ast_sockaddr * dst);
49 SCCP_API boolean_t SCCP_CALL sccp_netsock_ouraddrfor(const struct sockaddr_storage * them, struct sockaddr_storage * us);
50 SCCP_INLINE boolean_t SCCP_CALL sccp_netsock_is_IPv4(const struct sockaddr_storage *sockAddrStorage);
51 SCCP_INLINE boolean_t SCCP_CALL sccp_netsock_is_IPv6(const struct sockaddr_storage *sockAddrStorage);
52 SCCP_API uint16_t __PURE__ SCCP_CALL sccp_netsock_getPort(const struct sockaddr_storage *sockAddrStorage);
53 SCCP_API void SCCP_CALL sccp_netsock_setPort(const struct sockaddr_storage *sockAddrStorage, uint16_t port);
54 SCCP_API int __PURE__ SCCP_CALL sccp_netsock_is_any_addr(const struct sockaddr_storage *sockAddrStorage);
55 SCCP_API boolean_t SCCP_CALL sccp_netsock_getExternalAddr(struct sockaddr_storage *sockAddrStorage, int family);
56 SCCP_API void SCCP_CALL sccp_netsock_flush_externhost(void);
57 SCCP_API size_t __PURE__ SCCP_CALL sccp_netsock_sizeof(const struct sockaddr_storage *sockAddrStorage);
58 SCCP_API boolean_t __PURE__ SCCP_CALL sccp_netsock_is_mapped_IPv4(const struct sockaddr_storage *sockAddrStorage);
59 SCCP_API boolean_t SCCP_CALL sccp_netsock_ipv4_mapped(const struct sockaddr_storage *sockAddrStorage, struct sockaddr_storage *sockAddrStorage_mapped);
60 SCCP_API int SCCP_CALL sccp_netsock_cmp_addr(const struct sockaddr_storage *a, const struct sockaddr_storage *b);
61 SCCP_API int SCCP_CALL sccp_netsock_cmp_port(const struct sockaddr_storage *a, const struct sockaddr_storage *b);
62 SCCP_API int SCCP_CALL sccp_netsock_split_hostport(char *str, char **host, char **port, int flags);
63 
64 /* helper: easy replacement for inet_ntop for use in sccp_log functions (threadsafe) */
65 char *__netsock_stringify_fmt(const struct sockaddr_storage *sockAddrStorage, int format);
66 
67 /* begin sccp_netsock_stringify_fmt short cuts */
sccp_netsock_stringify(const struct sockaddr_storage * sockAddrStorage)68 static inline char * SCCP_CALL sccp_netsock_stringify(const struct sockaddr_storage *sockAddrStorage)
69 {
70 	return __netsock_stringify_fmt(sockAddrStorage, SCCP_SOCKADDR_STR_DEFAULT);
71 }
72 
sccp_netsock_stringify_remote(const struct sockaddr_storage * sockAddrStorage)73 static inline char * SCCP_CALL sccp_netsock_stringify_remote(const struct sockaddr_storage *sockAddrStorage)
74 {
75 	return __netsock_stringify_fmt(sockAddrStorage, SCCP_SOCKADDR_STR_DEFAULT_REMOTE);
76 }
77 
sccp_netsock_stringify_addr(const struct sockaddr_storage * sockAddrStorage)78 static inline char * SCCP_CALL sccp_netsock_stringify_addr(const struct sockaddr_storage *sockAddrStorage)
79 {
80 	return __netsock_stringify_fmt(sockAddrStorage, SCCP_SOCKADDR_STR_ADDR);
81 }
82 
sccp_netsock_stringify_addr_remote(const struct sockaddr_storage * sockAddrStorage)83 static inline char * SCCP_CALL sccp_netsock_stringify_addr_remote(const struct sockaddr_storage *sockAddrStorage)
84 {
85 	return __netsock_stringify_fmt(sockAddrStorage, SCCP_SOCKADDR_STR_ADDR_REMOTE);
86 }
87 
sccp_netsock_stringify_host(const struct sockaddr_storage * sockAddrStorage)88 static inline char * SCCP_CALL sccp_netsock_stringify_host(const struct sockaddr_storage *sockAddrStorage)
89 {
90 	return __netsock_stringify_fmt(sockAddrStorage, SCCP_SOCKADDR_STR_HOST);
91 }
92 
sccp_netsock_stringify_host_remote(const struct sockaddr_storage * sockAddrStorage)93 static inline char * SCCP_CALL sccp_netsock_stringify_host_remote(const struct sockaddr_storage *sockAddrStorage)
94 {
95 	return __netsock_stringify_fmt(sockAddrStorage, SCCP_SOCKADDR_STR_HOST_REMOTE);
96 }
97 
sccp_netsock_stringify_port(const struct sockaddr_storage * sockAddrStorage)98 static inline char * SCCP_CALL sccp_netsock_stringify_port(const struct sockaddr_storage *sockAddrStorage)
99 {
100 	return __netsock_stringify_fmt(sockAddrStorage, SCCP_SOCKADDR_STR_PORT);
101 }
102 
103 /* end sccp_netsock_stringify_fmt short cuts */
104 SCCP_API void SCCP_CALL sccp_netsock_setoptions(int new_socket, int reuse, int linger, int keepalive, int sndtimeout, int rcvtimeout);
105 SCCP_API void * SCCP_CALL sccp_netsock_thread(void *ignore);
106 __END_C_EXTERN__
107 // kate: indent-width 8; replace-tabs off; indent-mode cstyle; auto-insert-doxygen on; line-numbers on; tab-indents on; keep-extra-spaces off; auto-brackets off;
108