Annotation of embedaddon/coova-chilli/src/garden.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (C) 2003, 2004, 2005 Mondru AB.
3: * Copyright (c) 2006-2007 David Bird <david@coova.com>
4: *
5: * The contents of this file may be used under the terms of the GNU
6: * General Public License Version 2, provided that the above copyright
7: * notice and this permission notice is included in all copies or
8: * substantial portions of the software.
9: *
10: */
11:
12: #include "system.h"
13: #include "ippool.h"
14: #include "radius.h"
15: #include "radius_wispr.h"
16: #include "radius_chillispot.h"
17: #include "redir.h"
18: #include "syserr.h"
19: #include "dhcp.h"
20: #include "cmdline.h"
21: #include "chilli.h"
22: #include "options.h"
23:
24: int pass_through_add(pass_through *ptlist, size_t ptlen, size_t *ptcnt, pass_through *pt) {
25: size_t cnt = *ptcnt;
26: int i;
27:
28: if (cnt >= ptlen) {
29: if (options.debug)
30: log_dbg("No more room for walled garden entries");
31: return -1;
32: }
33:
34: for (i=0; i < cnt; i++) {
35: if (!memcmp(&ptlist[i],pt,sizeof(pass_through))) {
36: if (options.debug)
37: log_info("Uamallowed already exists #%d:%d: proto=%d host=%s port=%d", i, ptlen,
38: pt->proto, inet_ntoa(pt->host), pt->port);
39: return 0;
40: }
41: }
42:
43: if (options.debug)
44: log_info("Uamallowed IP address #%d:%d: proto=%d host=%s port=%d", cnt, ptlen,
45: pt->proto, inet_ntoa(pt->host), pt->port);
46:
47: memcpy(&ptlist[cnt], pt, sizeof(pass_through));
48: *ptcnt = cnt + 1;
49: return 0;
50: }
51:
52: int pass_throughs_from_string(pass_through *ptlist, size_t ptlen, size_t *ptcnt, char *s) {
53: struct hostent *host;
54: pass_through pt;
55: char *t, *p1 = NULL, *p2 = NULL;
56: char *p3 = malloc(strlen(s)+1);
57: strcpy(p3, s);
58: p1 = p3;
59:
60: if (options.debug)
61: log_dbg("Uamallowed %s", s);
62:
63: for ( ; p1; p1 = p2) {
64:
65: /* save the next entry position */
66: if ((p2 = strchr(p1, ','))) { *p2=0; p2++; }
67:
68: /* clear the pass-through entry in case we partitially filled it already */
69: memset(&pt, 0, sizeof(pass_through));
70:
71: /* eat whitespace */
72: while (isspace(*p1)) p1++;
73:
74: /* look for specific protocols */
75: if ((t = strchr(p1, ':'))) {
76: int pnum = 0;
77:
78: *t = 0;
79:
80: #ifdef HAVE_GETPROTOENT
81: if (1) {
82: struct protoent *proto = getprotobyname(p1);
83:
84: if (!proto && !strchr(p1, '.'))
85: proto = getprotobynumber(atoi(p1));
86:
87: if (proto)
88: pnum = proto->p_proto;
89: }
90: #else
91: if (!strcmp(p1,"tcp")) { pnum = DHCP_IP_TCP; }
92: else if (!strcmp(p1,"udp")) { pnum = DHCP_IP_UDP; }
93: else if (!strcmp(p1,"icmp")) { pnum = DHCP_IP_ICMP; }
94: #endif
95:
96: if (pnum > 0) {
97: /* if a protocol, skip ahead */
98: pt.proto = pnum;
99: p1 = t + 1;
100: } else {
101: /* if not a protocol, put the ':' back */
102: *t = ':';
103: }
104: }
105:
106: /* look for an optional port */
107: if ((t = strchr(p1, ':'))) {
108: pt.port = atoi(t+1);
109: *t = 0;
110: }
111:
112: if (strchr(p1, '/')) { /* parse a network address */
113: if (option_aton(&pt.host, &pt.mask, p1, 0)) {
114: log_err(0, "Invalid uamallowed network address or mask %s!", s);
115: continue;
116: }
117: if (pass_through_add(ptlist, ptlen, ptcnt, &pt))
118: log_err(0, "Too many pass-throughs! skipped %s", s);
119: }
120: else { /* otherwise, parse a host ip or hostname */
121: int j = 0;
122: pt.mask.s_addr = 0xffffffff;
123:
124: if (!(host = gethostbyname(p1))) {
125: log_err(errno, "Invalid uamallowed domain or address: %s!", p1);
126: continue;
127: }
128:
129: while (host->h_addr_list[j] != NULL) {
130: pt.host = *((struct in_addr *) host->h_addr_list[j++]);
131: if (pass_through_add(ptlist, ptlen, ptcnt, &pt))
132: log_err(0, "Too many pass-throughs! skipped %s", s);
133: }
134: }
135: }
136:
137: free(p3);
138: return 0;
139: }
140:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>