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.3 2008/05/19 10:19:49 corecode Exp $
30 #include <sys/types.h>
31 #include <sys/select.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
44 do_inet_aton(const char *start
, const char *end
, struct in_addr
*ip
)
48 if (end
- start
> 15) {
49 log_Printf(LogWARN
, "%.*s: Invalid IP address\n", (int)(end
-start
), start
);
52 strncpy(ipstr
, start
, end
-start
);
53 ipstr
[end
-start
] = '\0';
54 return inet_aton(ipstr
, ip
);
58 iplist_first(struct iplist
*list
)
64 iplist_setrange(struct iplist
*list
, char *range
)
68 if ((ptr
= strpbrk(range
, ",-")) == NULL
) {
69 if (!inet_aton(range
, &list
->cur
.ip
))
71 list
->cur
.lstart
= ntohl(list
->cur
.ip
.s_addr
);
74 if (!do_inet_aton(range
, ptr
, &list
->cur
.ip
))
77 list
->cur
.lstart
= ntohl(list
->cur
.ip
.s_addr
);
83 if ((ptr
= strpbrk(to
, ",-")) == NULL
)
84 ptr
= to
+ strlen(to
);
87 if (!do_inet_aton(to
, ptr
, &endip
))
89 list
->cur
.lstart
= ntohl(list
->cur
.ip
.s_addr
);
90 list
->cur
.nItems
= ntohl(endip
.s_addr
) - list
->cur
.lstart
+ 1;
91 if (list
->cur
.nItems
< 1)
95 list
->cur
.srcitem
= 0;
96 list
->cur
.srcptr
= range
;
101 iplist_nextrange(struct iplist
*list
)
103 char *ptr
, *to
, *end
;
105 ptr
= list
->cur
.srcptr
;
106 if (ptr
!= NULL
&& (ptr
= strchr(ptr
, ',')) != NULL
)
111 while (*ptr
!= '\0' && !iplist_setrange(list
, ptr
)) {
112 if ((end
= strchr(ptr
, ',')) == NULL
)
113 end
= ptr
+ strlen(ptr
);
116 log_Printf(LogWARN
, "%.*s: Invalid IP range (skipping)\n",
117 (int)(end
- ptr
), ptr
);
121 while (*to
++ != '\0');
130 iplist_next(struct iplist
*list
)
132 if (list
->cur
.pos
== -1) {
133 list
->cur
.srcptr
= NULL
;
134 if (!iplist_nextrange(list
)) {
135 list
->cur
.ip
.s_addr
= INADDR_ANY
;
138 } else if (++list
->cur
.srcitem
== list
->cur
.nItems
) {
139 if (!iplist_nextrange(list
)) {
140 list
->cur
.ip
.s_addr
= INADDR_ANY
;
145 list
->cur
.ip
.s_addr
= htonl(list
->cur
.lstart
+ list
->cur
.srcitem
);
152 iplist_setsrc(struct iplist
*list
, const char *src
)
154 strncpy(list
->src
, src
, sizeof list
->src
- 1);
155 list
->src
[sizeof list
->src
- 1] = '\0';
156 list
->cur
.srcptr
= list
->src
;
158 if (iplist_nextrange(list
))
159 list
->nItems
+= list
->cur
.nItems
;
162 } while (list
->cur
.srcptr
!= list
->src
);
167 iplist_reset(struct iplist
*list
)
175 iplist_setcurpos(struct iplist
*list
, long pos
)
177 if (pos
< 0 || pos
>= list
->nItems
) {
179 list
->cur
.ip
.s_addr
= INADDR_ANY
;
183 list
->cur
.srcptr
= NULL
;
186 iplist_nextrange(list
);
187 if (pos
< list
->cur
.nItems
) {
189 list
->cur
.srcitem
= pos
;
190 list
->cur
.pos
+= pos
;
191 list
->cur
.ip
.s_addr
= htonl(list
->cur
.lstart
+ list
->cur
.srcitem
);
195 pos
-= list
->cur
.nItems
;
196 list
->cur
.pos
+= list
->cur
.nItems
;
203 iplist_setrandpos(struct iplist
*list
)
206 return iplist_setcurpos(list
, random() % list
->nItems
);
210 iplist_ip2pos(struct iplist
*list
, struct in_addr ip
)
212 struct iplist_cur cur
;
217 memcpy(&cur
, &list
->cur
, sizeof cur
);
219 for (iplist_first(list
), f
= 0; f
< list
->nItems
; f
++)
220 if (iplist_next(list
).s_addr
== ip
.s_addr
) {
221 result
= list
->cur
.pos
;
225 memcpy(&list
->cur
, &cur
, sizeof list
->cur
);