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"
34 #include <sys/ioctl.h>
36 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
41 #include <sys/sockio.h>
44 #include "asterisk/acl.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/options.h"
47 #include "asterisk/utils.h"
48 #include "asterisk/lock.h"
49 #include "asterisk/srv.h"
52 char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "eth0", "ppp0", etc. */
53 struct sockaddr_in ifru_addr;
56 /* Free HA structure */
57 void ast_free_ha(struct ast_ha *ha)
67 /* Copy HA structure */
68 static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
70 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
71 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
72 to->sense = from->sense;
75 /* Create duplicate of ha structure */
76 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
78 struct ast_ha *new_ha;
80 if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
81 /* Copy from original to new object */
82 ast_copy_ha(original, new_ha);
88 /* Create duplicate HA link list */
89 /* Used in chan_sip2 templates */
90 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
92 struct ast_ha *start = original;
93 struct ast_ha *ret = NULL;
94 struct ast_ha *link, *prev = NULL;
97 link = ast_duplicate_ha(start); /* Create copy of this object */
99 prev->next = link; /* Link previous to this object */
102 ret = link; /* Save starting point */
104 start = start->next; /* Go to next object */
105 prev = link; /* Save pointer to this object */
107 return ret; /* Return start of list */
110 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
114 struct ast_ha *prev = NULL;
117 char *tmp = ast_strdupa(stuff);
125 ha = ast_malloc(sizeof(*ha));
129 nm = strchr(tmp, '/');
131 /* assume /32. Yes, htonl does not do anything for this particular mask
132 but we better use it to show we remember about byte order */
133 ha->netmask.s_addr = htonl(0xFFFFFFFF);
138 if (!strchr(nm, '.')) {
139 if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32))
140 ha->netmask.s_addr = htonl(0xFFFFFFFF << (32 - x));
142 ast_log(LOG_WARNING, "Invalid CIDR in %s\n", stuff);
148 } else if (!inet_aton(nm, &ha->netmask)) {
149 ast_log(LOG_WARNING, "Invalid mask in %s\n", stuff);
157 if (!inet_aton(tmp, &ha->netaddr)) {
158 ast_log(LOG_WARNING, "Invalid IP address in %s\n", stuff);
165 ha->netaddr.s_addr &= ha->netmask.s_addr;
167 ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
176 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);
181 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
183 /* Start optimistic */
184 int res = AST_SENSE_ALLOW;
186 #if 0 /* debugging code */
187 char iabuf[INET_ADDRSTRLEN];
188 char iabuf2[INET_ADDRSTRLEN];
190 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
191 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
192 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
194 /* For each rule, if this address and the netmask = the net address
195 apply the current rule */
196 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
203 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
206 struct ast_hostent ahp;
209 int tportno = ntohs(sin->sin_port);
211 snprintf(srv, sizeof(srv), "%s.%s", service, value);
212 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
213 sin->sin_port = htons(tportno);
217 hp = ast_gethostbyname(value, &ahp);
219 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
221 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
227 struct dscp_codepoint {
232 /* IANA registered DSCP codepoints */
234 static const struct dscp_codepoint dscp_pool1[] = {
258 int ast_str2cos(const char *value, unsigned int *cos)
262 if (sscanf(value, "%d", &fval) == 1) {
272 int ast_str2tos(const char *value, unsigned int *tos)
277 if (sscanf(value, "%i", &fval) == 1) {
282 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
283 if (!strcasecmp(value, dscp_pool1[x].name)) {
284 *tos = dscp_pool1[x].space << 2;
292 const char *ast_tos2str(unsigned int tos)
296 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
297 if (dscp_pool1[x].space == (tos >> 2))
298 return dscp_pool1[x].name;
304 int ast_get_ip(struct sockaddr_in *sin, const char *value)
306 return ast_get_ip_or_srv(sin, value, NULL);
309 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
312 struct sockaddr_in sin;
315 s = socket(PF_INET, SOCK_DGRAM, 0);
317 ast_log(LOG_ERROR, "Cannot create socket\n");
320 sin.sin_family = AF_INET;
322 sin.sin_addr = *them;
323 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
324 ast_log(LOG_WARNING, "Cannot connect\n");
329 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
330 ast_log(LOG_WARNING, "Cannot get socket name\n");
335 ast_debug(3, "Found IP address for this socket\n");
340 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
342 char ourhost[MAXHOSTNAMELEN] = "";
343 struct ast_hostent ahp;
345 struct in_addr saddr;
347 /* just use the bind address if it is nonzero */
348 if (ntohl(bindaddr.sin_addr.s_addr)) {
349 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
350 ast_debug(3, "Attached to given IP address\n");
353 /* try to use our hostname */
354 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
355 ast_log(LOG_WARNING, "Unable to get hostname\n");
357 hp = ast_gethostbyname(ourhost, &ahp);
359 memcpy(ourip, hp->h_addr, sizeof(*ourip));
360 ast_debug(3, "Found one IP address based on local hostname %s.\n", ourhost);
364 ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
365 /* A.ROOT-SERVERS.NET. */
366 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
368 ast_debug(3, "Failed to find any IP address for us\n");