Pull up CVS idents from FreeBSD to match our current version.
[dragonfly.git] / usr.sbin / ppp / iplist.c
blob457eb9ea0f0fff89d6a8751ae6187200a4f2e706
1 /*-
2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
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>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <termios.h>
38 #include "log.h"
39 #include "defs.h"
40 #include "iplist.h"
42 static int
43 do_inet_aton(const char *start, const char *end, struct in_addr *ip)
45 char ipstr[16];
47 if (end - start > 15) {
48 log_Printf(LogWARN, "%.*s: Invalid IP address\n", (int)(end-start), start);
49 return 0;
51 strncpy(ipstr, start, end-start);
52 ipstr[end-start] = '\0';
53 return inet_aton(ipstr, ip);
56 static void
57 iplist_first(struct iplist *list)
59 list->cur.pos = -1;
62 static int
63 iplist_setrange(struct iplist *list, char *range)
65 char *ptr, *to;
67 if ((ptr = strpbrk(range, ",-")) == NULL) {
68 if (!inet_aton(range, &list->cur.ip))
69 return 0;
70 list->cur.lstart = ntohl(list->cur.ip.s_addr);
71 list->cur.nItems = 1;
72 } else {
73 if (!do_inet_aton(range, ptr, &list->cur.ip))
74 return 0;
75 if (*ptr == ',') {
76 list->cur.lstart = ntohl(list->cur.ip.s_addr);
77 list->cur.nItems = 1;
78 } else {
79 struct in_addr endip;
81 to = ptr+1;
82 if ((ptr = strpbrk(to, ",-")) == NULL)
83 ptr = to + strlen(to);
84 if (*to == '-')
85 return 0;
86 if (!do_inet_aton(to, ptr, &endip))
87 return 0;
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)
91 return 0;
94 list->cur.srcitem = 0;
95 list->cur.srcptr = range;
96 return 1;
99 static int
100 iplist_nextrange(struct iplist *list)
102 char *ptr, *to, *end;
104 ptr = list->cur.srcptr;
105 if (ptr != NULL && (ptr = strchr(ptr, ',')) != NULL)
106 ptr++;
107 else
108 ptr = list->src;
110 while (*ptr != '\0' && !iplist_setrange(list, ptr)) {
111 if ((end = strchr(ptr, ',')) == NULL)
112 end = ptr + strlen(ptr);
113 if (end == ptr)
114 return 0;
115 log_Printf(LogWARN, "%.*s: Invalid IP range (skipping)\n",
116 (int)(end - ptr), ptr);
117 to = ptr;
119 *to = *end++;
120 while (*to++ != '\0');
121 if (*ptr == '\0')
122 ptr = list->src;
125 return 1;
128 struct in_addr
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;
135 return list->cur.ip;
137 } else if (++list->cur.srcitem == list->cur.nItems) {
138 if (!iplist_nextrange(list)) {
139 list->cur.ip.s_addr = INADDR_ANY;
140 list->cur.pos = -1;
141 return list->cur.ip;
143 } else
144 list->cur.ip.s_addr = htonl(list->cur.lstart + list->cur.srcitem);
145 list->cur.pos++;
147 return list->cur.ip;
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;
156 do {
157 if (iplist_nextrange(list))
158 list->nItems += list->cur.nItems;
159 else
160 return 0;
161 } while (list->cur.srcptr != list->src);
162 return 1;
165 void
166 iplist_reset(struct iplist *list)
168 list->src[0] = '\0';
169 list->nItems = 0;
170 list->cur.pos = -1;
173 struct in_addr
174 iplist_setcurpos(struct iplist *list, long pos)
176 if (pos < 0 || pos >= list->nItems) {
177 list->cur.pos = -1;
178 list->cur.ip.s_addr = INADDR_ANY;
179 return list->cur.ip;
182 list->cur.srcptr = NULL;
183 list->cur.pos = 0;
184 while (1) {
185 iplist_nextrange(list);
186 if (pos < list->cur.nItems) {
187 if (pos) {
188 list->cur.srcitem = pos;
189 list->cur.pos += pos;
190 list->cur.ip.s_addr = htonl(list->cur.lstart + list->cur.srcitem);
192 break;
194 pos -= list->cur.nItems;
195 list->cur.pos += list->cur.nItems;
198 return list->cur.ip;
201 struct in_addr
202 iplist_setrandpos(struct iplist *list)
204 randinit();
205 return iplist_setcurpos(list, random() % list->nItems);
209 iplist_ip2pos(struct iplist *list, struct in_addr ip)
211 struct iplist_cur cur;
212 u_long f;
213 int result;
215 result = -1;
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;
221 break;
224 memcpy(&list->cur, &cur, sizeof list->cur);
225 return result;