2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
20 * \brief Access Control of various sorts
23 #ifndef _ASTERISK_ACL_H
24 #define _ASTERISK_ACL_H
27 #if defined(__cplusplus) || defined(c_plusplus)
31 #include "asterisk/network.h"
32 #include "asterisk/netsock2.h"
33 #include "asterisk/io.h"
35 #define AST_SENSE_DENY 0
36 #define AST_SENSE_ALLOW 1
38 /* Host based access control */
40 /*! \brief internal representation of acl entries
41 * In principle user applications would have no need for this,
42 * but there is sometimes a need to extract individual items,
43 * e.g. to print them, and rather than defining iterators to
44 * navigate the list, and an externally visible 'struct ast_ha_entry',
45 * at least in the short term it is more convenient to make the whole
46 * thing public and let users play with them.
49 /* Host access rule */
50 struct ast_sockaddr addr;
51 struct ast_sockaddr netmask;
57 * \brief Free a list of HAs
60 * Given the head of a list of HAs, it and all appended
63 * \param ha The head of the list of HAs to free
66 void ast_free_ha(struct ast_ha *ha);
69 * \brief Copy the contents of one HA to another
72 * This copies the internals of the 'from' HA to the 'to'
73 * HA. It is important that the 'to' HA has been allocated
74 * prior to calling this function
76 * \param from Source HA to copy
77 * \param to Destination HA to copy to
80 void ast_copy_ha(const struct ast_ha *from, struct ast_ha *to);
83 * \brief Add a new rule to a list of HAs
86 * This adds the new host access rule to the end of the list
87 * whose head is specified by the path parameter. Rules are
88 * evaluated in a way such that if multiple rules apply to
89 * a single IP address/subnet mask, then the rule latest
90 * in the list will be used.
92 * \param sense Either "permit" or "deny" (Actually any 'p' word will result
93 * in permission, and any other word will result in denial)
94 * \param stuff The IP address and subnet mask, separated with a '/'. The subnet
95 * mask can either be in dotted-decimal format or in CIDR notation (i.e. 0-32).
96 * \param path The head of the HA list to which we wish to append our new rule. If
97 * NULL is passed, then the new rule will become the head of the list
98 * \param[out] error The integer error points to will be set non-zero if an error occurs
99 * \return The head of the HA list
101 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error);
104 * \brief Apply a set of rules to a given IP address
107 * The list of host access rules is traversed, beginning with the
108 * input rule. If the IP address given matches a rule, the "sense"
109 * of that rule is used as the return value. Note that if an IP
110 * address matches multiple rules that the last one matched will be
111 * the one whose sense will be returned.
113 * \param ha The head of the list of host access rules to follow
114 * \param addr An ast_sockaddr whose address is considered when matching rules
115 * \retval AST_SENSE_ALLOW The IP address passes our ACL
116 * \retval AST_SENSE_DENY The IP address fails our ACL
118 int ast_apply_ha(const struct ast_ha *ha, const struct ast_sockaddr *addr);
121 * \brief Get the IP address given a hostname
124 * Similar in nature to ast_gethostbyname, except that instead
125 * of getting an entire hostent structure, you instead are given
126 * only the IP address inserted into a sockaddr_in structure.
128 * \param[out] addr The IP address is written into sin->sin_addr
129 * \param value The hostname to look up
133 int ast_get_ip(struct ast_sockaddr *addr, const char *value);
136 * \brief Get the IP address given a hostname and optional service
139 * If the service parameter is non-NULL, then an SRV lookup will be made by
140 * prepending the service to the value parameter, separated by a '.'
141 * For example, if value is "example.com" and service is "_sip._udp" then
142 * an SRV lookup will be done for "_sip._udp.example.com". If service is NULL,
143 * then this function acts exactly like a call to ast_get_ip.
145 * \param addr The IP address found. The address family is used as an input parameter to
146 * filter the returned addresses. if it is 0, both IPv4 and IPv6 addresses
149 * \param value The hostname to look up
150 * \param service A specific service provided by the host. A NULL service results
151 * in an A-record lookup instead of an SRV lookup
155 int ast_get_ip_or_srv(struct ast_sockaddr *addr, const char *value, const char *service);
158 * \brief Get our local IP address when contacting a remote host
161 * This function will attempt to connect(2) to them over UDP using a source
162 * port of 5060. If the connect(2) call is successful, then we inspect the
163 * sockaddr_in output parameter of connect(2) to determine the IP address
164 * used to connect to them. This IP address is then copied into us.
166 * \param them The IP address to which we wish to attempt to connect
167 * \param[out] us The source IP address used to connect to them
171 int ast_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us);
174 * \brief Find an IP address associated with a specific interface
177 * Given an interface such as "eth0" we find the primary IP address
178 * associated with it using the SIOCGIFADDR ioctl. If the ioctl call
179 * should fail, we populate address with 0s.
182 * This function is not actually used anywhere
184 * \param iface The interface name whose IP address we wish to find
185 * \param[out] address The interface's IP address is placed into this param
186 * \retval -1 Failure. address is filled with 0s
189 int ast_lookup_iface(char *iface, struct ast_sockaddr *address);
192 * \brief Duplicate the contents of a list of host access rules
195 * A deep copy of all ast_has in the list is made. The returned
196 * value is allocated on the heap and must be freed independently
197 * of the input parameter when finished.
200 * This function is not actually used anywhere.
202 * \param original The ast_ha to copy
203 * \retval The head of the list of duplicated ast_has
205 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original);
208 * \brief Find our IP address
211 * This function goes through many iterations in an attempt to find
212 * our IP address. If any step along the way should fail, we move to the
213 * next item in the list. Here are the steps taken:
214 * - If bindaddr has a non-zero IP address, that is copied into ourip
215 * - We use a combination of gethostname and ast_gethostbyname to find our
217 * - We use ast_ouraddrfor with 198.41.0.4 as the destination IP address
218 * - We try some platform-specific socket operations to find the IP address
220 * \param[out] ourip Our IP address is written here when it is found
221 * \param bindaddr A hint used for finding our IP. See the steps above for
223 * \param family Only addresses of the given family will be returned. Use 0
224 * or AST_SOCKADDR_UNSPEC to get addresses of all families.
228 int ast_find_ourip(struct ast_sockaddr *ourip, const struct ast_sockaddr *bindaddr, int family);
231 * \brief Convert a string to the appropriate COS value
233 * \param value The COS string to convert
234 * \param[out] cos The integer representation of that COS value
238 int ast_str2cos(const char *value, unsigned int *cos);
241 * \brief Convert a string to the appropriate TOS value
243 * \param value The TOS string to convert
244 * \param[out] tos The integer representation of that TOS value
248 int ast_str2tos(const char *value, unsigned int *tos);
251 * \brief Convert a TOS value into its string representation
253 * \param tos The TOS value to look up
254 * \return The string equivalent of the TOS value
256 const char *ast_tos2str(unsigned int tos);
258 #if defined(__cplusplus) || defined(c_plusplus)
262 #endif /* _ASTERISK_ACL_H */