2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/usr.sbin/ppp/iplist.c,v 1.9 1999/08/28 01:18:31 peter Exp $
27 * $DragonFly: src/usr.sbin/ppp/iplist.c,v 1.2 2003/06/17 04:30:00 dillon Exp $
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
43 do_inet_aton(const char *start, const char *end, struct in_addr *ip)
47 if (end - start > 15) {
48 log_Printf(LogWARN, "%.*s: Invalid IP address\n", (int)(end-start), start);
51 strncpy(ipstr, start, end-start);
52 ipstr[end-start] = '\0';
53 return inet_aton(ipstr, ip);
57 iplist_first(struct iplist *list)
63 iplist_setrange(struct iplist *list, char *range)
67 if ((ptr = strpbrk(range, ",-")) == NULL) {
68 if (!inet_aton(range, &list->cur.ip))
70 list->cur.lstart = ntohl(list->cur.ip.s_addr);
73 if (!do_inet_aton(range, ptr, &list->cur.ip))
76 list->cur.lstart = ntohl(list->cur.ip.s_addr);
82 if ((ptr = strpbrk(to, ",-")) == NULL)
83 ptr = to + strlen(to);
86 if (!do_inet_aton(to, ptr, &endip))
88 list->cur.lstart = ntohl(list->cur.ip.s_addr);
89 list->cur.nItems = ntohl(endip.s_addr) - list->cur.lstart + 1;
90 if (list->cur.nItems < 1)
94 list->cur.srcitem = 0;
95 list->cur.srcptr = range;
100 iplist_nextrange(struct iplist *list)
102 char *ptr, *to, *end;
104 ptr = list->cur.srcptr;
105 if (ptr != NULL && (ptr = strchr(ptr, ',')) != NULL)
110 while (*ptr != '\0' && !iplist_setrange(list, ptr)) {
111 if ((end = strchr(ptr, ',')) == NULL)
112 end = ptr + strlen(ptr);
115 log_Printf(LogWARN, "%.*s: Invalid IP range (skipping)\n",
116 (int)(end - ptr), ptr);
120 while (*to++ != '\0');
129 iplist_next(struct iplist *list)
131 if (list->cur.pos == -1) {
132 list->cur.srcptr = NULL;
133 if (!iplist_nextrange(list)) {
134 list->cur.ip.s_addr = INADDR_ANY;
137 } else if (++list->cur.srcitem == list->cur.nItems) {
138 if (!iplist_nextrange(list)) {
139 list->cur.ip.s_addr = INADDR_ANY;
144 list->cur.ip.s_addr = htonl(list->cur.lstart + list->cur.srcitem);
151 iplist_setsrc(struct iplist *list, const char *src)
153 strncpy(list->src, src, sizeof list->src - 1);
154 list->src[sizeof list->src - 1] = '\0';
155 list->cur.srcptr = list->src;
157 if (iplist_nextrange(list))
158 list->nItems += list->cur.nItems;
161 } while (list->cur.srcptr != list->src);
166 iplist_reset(struct iplist *list)
174 iplist_setcurpos(struct iplist *list, long pos)
176 if (pos < 0 || pos >= list->nItems) {
178 list->cur.ip.s_addr = INADDR_ANY;
182 list->cur.srcptr = NULL;
185 iplist_nextrange(list);
186 if (pos < list->cur.nItems) {
188 list->cur.srcitem = pos;
189 list->cur.pos += pos;
190 list->cur.ip.s_addr = htonl(list->cur.lstart + list->cur.srcitem);
194 pos -= list->cur.nItems;
195 list->cur.pos += list->cur.nItems;
202 iplist_setrandpos(struct iplist *list)
205 return iplist_setcurpos(list, random() % list->nItems);
209 iplist_ip2pos(struct iplist *list, struct in_addr ip)
211 struct iplist_cur cur;
216 memcpy(&cur, &list->cur, sizeof cur);
218 for (iplist_first(list), f = 0; f < list->nItems; f++)
219 if (iplist_next(list).s_addr == ip.s_addr) {
220 result = list->cur.pos;
224 memcpy(&list->cur, &cur, sizeof list->cur);