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.
21 * \brief Various sorts of access control
23 * \author Mark Spencer <markster@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/network.h"
33 #include <sys/sockio.h>
36 #include "asterisk/acl.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/srv.h"
43 char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "eth0", "ppp0", etc. */
44 struct sockaddr_in ifru_addr;
47 /* Free HA structure */
48 void ast_free_ha(struct ast_ha *ha)
58 /* Copy HA structure */
59 static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
61 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
62 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
63 to->sense = from->sense;
66 /* Create duplicate of ha structure */
67 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
69 struct ast_ha *new_ha;
71 if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
72 /* Copy from original to new object */
73 ast_copy_ha(original, new_ha);
79 /* Create duplicate HA link list */
80 /* Used in chan_sip2 templates */
81 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
83 struct ast_ha *start = original;
84 struct ast_ha *ret = NULL;
85 struct ast_ha *link, *prev = NULL;
88 link = ast_duplicate_ha(start); /* Create copy of this object */
90 prev->next = link; /* Link previous to this object */
93 ret = link; /* Save starting point */
95 start = start->next; /* Go to next object */
96 prev = link; /* Save pointer to this object */
98 return ret; /* Return start of list */
101 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
105 struct ast_ha *prev = NULL;
108 char *tmp = ast_strdupa(stuff);
116 ha = ast_malloc(sizeof(*ha));
120 nm = strchr(tmp, '/');
122 /* assume /32. Yes, htonl does not do anything for this particular mask
123 but we better use it to show we remember about byte order */
124 ha->netmask.s_addr = htonl(0xFFFFFFFF);
129 if (!strchr(nm, '.')) {
130 if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32))
131 ha->netmask.s_addr = htonl(0xFFFFFFFF << (32 - x));
133 ast_log(LOG_WARNING, "Invalid CIDR in %s\n", stuff);
139 } else if (!inet_aton(nm, &ha->netmask)) {
140 ast_log(LOG_WARNING, "Invalid mask in %s\n", stuff);
148 if (!inet_aton(tmp, &ha->netaddr)) {
149 ast_log(LOG_WARNING, "Invalid IP address in %s\n", stuff);
156 ha->netaddr.s_addr &= ha->netmask.s_addr;
158 ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
167 ast_debug(1, "%s/%s sense %d appended to acl for peer\n", ast_strdupa(ast_inet_ntoa(ha->netaddr)), ast_strdupa(ast_inet_ntoa(ha->netmask)), ha->sense);
172 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
174 /* Start optimistic */
175 int res = AST_SENSE_ALLOW;
177 #if 0 /* debugging code */
178 char iabuf[INET_ADDRSTRLEN];
179 char iabuf2[INET_ADDRSTRLEN];
181 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
182 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
183 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
185 /* For each rule, if this address and the netmask = the net address
186 apply the current rule */
187 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
194 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
197 struct ast_hostent ahp;
200 int tportno = ntohs(sin->sin_port);
202 snprintf(srv, sizeof(srv), "%s.%s", service, value);
203 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
204 sin->sin_port = htons(tportno);
208 hp = ast_gethostbyname(value, &ahp);
210 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
212 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
218 struct dscp_codepoint {
223 /* IANA registered DSCP codepoints */
225 static const struct dscp_codepoint dscp_pool1[] = {
249 int ast_str2cos(const char *value, unsigned int *cos)
253 if (sscanf(value, "%d", &fval) == 1) {
263 int ast_str2tos(const char *value, unsigned int *tos)
268 if (sscanf(value, "%i", &fval) == 1) {
273 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
274 if (!strcasecmp(value, dscp_pool1[x].name)) {
275 *tos = dscp_pool1[x].space << 2;
283 const char *ast_tos2str(unsigned int tos)
287 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
288 if (dscp_pool1[x].space == (tos >> 2))
289 return dscp_pool1[x].name;
295 int ast_get_ip(struct sockaddr_in *sin, const char *value)
297 return ast_get_ip_or_srv(sin, value, NULL);
300 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
303 struct sockaddr_in sin;
306 s = socket(PF_INET, SOCK_DGRAM, 0);
308 ast_log(LOG_ERROR, "Cannot create socket\n");
311 sin.sin_family = AF_INET;
313 sin.sin_addr = *them;
314 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
315 ast_log(LOG_WARNING, "Cannot connect\n");
320 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
321 ast_log(LOG_WARNING, "Cannot get socket name\n");
326 ast_debug(3, "Found IP address for this socket\n");
331 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
333 char ourhost[MAXHOSTNAMELEN] = "";
334 struct ast_hostent ahp;
336 struct in_addr saddr;
338 /* just use the bind address if it is nonzero */
339 if (ntohl(bindaddr.sin_addr.s_addr)) {
340 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
341 ast_debug(3, "Attached to given IP address\n");
344 /* try to use our hostname */
345 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
346 ast_log(LOG_WARNING, "Unable to get hostname\n");
348 hp = ast_gethostbyname(ourhost, &ahp);
350 memcpy(ourip, hp->h_addr, sizeof(*ourip));
351 ast_debug(3, "Found one IP address based on local hostname %s.\n", ourhost);
355 ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
356 /* A.ROOT-SERVERS.NET. */
357 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
359 ast_debug(3, "Failed to find any IP address for us\n");