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/utils.h"
47 #include "asterisk/lock.h"
48 #include "asterisk/srv.h"
51 char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "eth0", "ppp0", etc. */
52 struct sockaddr_in ifru_addr;
55 /* Free HA structure */
56 void ast_free_ha(struct ast_ha *ha)
66 /* Copy HA structure */
67 static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
69 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
70 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
71 to->sense = from->sense;
74 /* Create duplicate of ha structure */
75 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
77 struct ast_ha *new_ha;
79 if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
80 /* Copy from original to new object */
81 ast_copy_ha(original, new_ha);
87 /* Create duplicate HA link list */
88 /* Used in chan_sip2 templates */
89 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
91 struct ast_ha *start = original;
92 struct ast_ha *ret = NULL;
93 struct ast_ha *link, *prev = NULL;
96 link = ast_duplicate_ha(start); /* Create copy of this object */
98 prev->next = link; /* Link previous to this object */
101 ret = link; /* Save starting point */
103 start = start->next; /* Go to next object */
104 prev = link; /* Save pointer to this object */
106 return ret; /* Return start of list */
109 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
113 struct ast_ha *prev = NULL;
116 char *tmp = ast_strdupa(stuff);
124 ha = ast_malloc(sizeof(*ha));
128 nm = strchr(tmp, '/');
130 /* assume /32. Yes, htonl does not do anything for this particular mask
131 but we better use it to show we remember about byte order */
132 ha->netmask.s_addr = htonl(0xFFFFFFFF);
137 if (!strchr(nm, '.')) {
138 if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32))
139 ha->netmask.s_addr = htonl(0xFFFFFFFF << (32 - x));
141 ast_log(LOG_WARNING, "Invalid CIDR in %s\n", stuff);
147 } else if (!inet_aton(nm, &ha->netmask)) {
148 ast_log(LOG_WARNING, "Invalid mask in %s\n", stuff);
156 if (!inet_aton(tmp, &ha->netaddr)) {
157 ast_log(LOG_WARNING, "Invalid IP address in %s\n", stuff);
164 ha->netaddr.s_addr &= ha->netmask.s_addr;
166 ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
175 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);
180 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
182 /* Start optimistic */
183 int res = AST_SENSE_ALLOW;
185 #if 0 /* debugging code */
186 char iabuf[INET_ADDRSTRLEN];
187 char iabuf2[INET_ADDRSTRLEN];
189 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
190 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
191 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
193 /* For each rule, if this address and the netmask = the net address
194 apply the current rule */
195 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
202 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
205 struct ast_hostent ahp;
208 int tportno = ntohs(sin->sin_port);
210 snprintf(srv, sizeof(srv), "%s.%s", service, value);
211 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
212 sin->sin_port = htons(tportno);
216 hp = ast_gethostbyname(value, &ahp);
218 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
220 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
226 struct dscp_codepoint {
231 /* IANA registered DSCP codepoints */
233 static const struct dscp_codepoint dscp_pool1[] = {
257 int ast_str2cos(const char *value, unsigned int *cos)
261 if (sscanf(value, "%d", &fval) == 1) {
271 int ast_str2tos(const char *value, unsigned int *tos)
276 if (sscanf(value, "%i", &fval) == 1) {
281 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
282 if (!strcasecmp(value, dscp_pool1[x].name)) {
283 *tos = dscp_pool1[x].space << 2;
291 const char *ast_tos2str(unsigned int tos)
295 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
296 if (dscp_pool1[x].space == (tos >> 2))
297 return dscp_pool1[x].name;
303 int ast_get_ip(struct sockaddr_in *sin, const char *value)
305 return ast_get_ip_or_srv(sin, value, NULL);
308 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
311 struct sockaddr_in sin;
314 s = socket(PF_INET, SOCK_DGRAM, 0);
316 ast_log(LOG_ERROR, "Cannot create socket\n");
319 sin.sin_family = AF_INET;
321 sin.sin_addr = *them;
322 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
323 ast_log(LOG_WARNING, "Cannot connect\n");
328 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
329 ast_log(LOG_WARNING, "Cannot get socket name\n");
334 ast_debug(3, "Found IP address for this socket\n");
339 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
341 char ourhost[MAXHOSTNAMELEN] = "";
342 struct ast_hostent ahp;
344 struct in_addr saddr;
346 /* just use the bind address if it is nonzero */
347 if (ntohl(bindaddr.sin_addr.s_addr)) {
348 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
349 ast_debug(3, "Attached to given IP address\n");
352 /* try to use our hostname */
353 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
354 ast_log(LOG_WARNING, "Unable to get hostname\n");
356 hp = ast_gethostbyname(ourhost, &ahp);
358 memcpy(ourip, hp->h_addr, sizeof(*ourip));
359 ast_debug(3, "Found one IP address based on local hostname %s.\n", ourhost);
363 ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
364 /* A.ROOT-SERVERS.NET. */
365 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
367 ast_debug(3, "Failed to find any IP address for us\n");