blob: 2d3f575a118d4abd7e2523a29aae43e4723745a7 [file] [log] [blame]
jjakoa7cd2492003-04-11 09:40:12 +00001/*
2 * IP address pool functions.
3 * Copyright (C) 2003 Mondru AB.
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 * The initial developer of the original code is
11 * Jens Jakobsen <jj@openggsn.org>
12 *
13 * Contributor(s):
14 *
15 */
16
17#ifndef _IPPOOL_H
18#define _IPPOOL_H
19
20/* Assuming that the address space is fragmented we need a hash table
21 in order to return the addresses.
22
23 The list pool should provide for both IPv4 and IPv6 addresses.
24
25 When initialising a new address pool it should be possible to pass
26 a string of CIDR format networks: "10.0.0.0/24 10.15.0.0/20" would
27 translate to 256 addresses starting at 10.0.0.0 and 1024 addresses
28 starting at 10.15.0.0.
29
30 The above also applies to IPv6 which can be specified as described
31 in RFC2373.
32*/
33
34typedef unsigned long int ub4; /* unsigned 4-byte quantities */
35typedef unsigned char ub1;
36
37#define IPPOOL_NOIP6
38
39#define IPPOOL_NONETWORK 0x01
40#define IPPOOL_NOBROADCAST 0x02
41
42struct ippoolm_t; /* Forward declaration */
43
44struct ippool_t {
45 int listsize; /* Total number of addresses */
46 struct ippoolm_t *member; /* Listsize array of members */
47 int hashsize; /* Size of hash table */
48 int hashlog; /* Log2 size of hash table */
49 int hashmask; /* Bitmask for calculating hash */
50 struct ippoolm_t **hash; /* Hashsize array of pointer to member */
51 struct ippoolm_t *first; /* Pointer to first available member */
52 struct ippoolm_t *last; /* Pointer to last available member */
53};
54
55struct ippoolm_t {
56#ifndef IPPOOL_NOIP6
57 struct in6_addr addr; /* IP address of this member */
58#else
59 struct in_addr addr; /* IP address of this member */
60#endif
61 int inuse; /* 0=available; 1= inuse */
62 struct ippoolm_t *nexthash; /* Linked list part of hash table */
63 struct ippoolm_t *prev, *next; /* Double linked list of available members */
64 struct ippool_t *parent; /* Pointer to parent */
65 void *peer; /* Pointer to peer protocol handler */
66};
67
68
69/* The above structures requires approximately 20+4 = 24 bytes for
70 each address (IPv4). For IPv6 the corresponding value is 32+4 = 36
71 bytes for each address. */
72
73/* Hash an IP address using code based on Bob Jenkins lookupa */
74extern unsigned long int ippool_hash4(struct in_addr *addr);
75
76/* Create new address pool */
77extern int ippool_new(struct ippool_t **this, char *pool, int flags);
78
79/* Delete existing address pool */
80extern int ippool_free(struct ippool_t *this);
81
82/* Find an IP address in the pool */
83extern int ippool_getip(struct ippool_t *this, struct ippoolm_t **member,
84 struct in_addr *addr);
85
86/* Get an IP address. If addr = 0.0.0.0 get a dynamic IP address. Otherwise
87 check to see if the given address is available */
88extern int ippool_newip(struct ippool_t *this, struct ippoolm_t **member,
89 struct in_addr *addr);
90
91/* Return a previously allocated IP address */
92extern int ippool_freeip(struct ippoolm_t *member);
93
94/* Get net and mask based on ascii string */
95extern int ippool_aton(struct in_addr *addr, struct in_addr *mask,
96 char *pool, int number);
97
98
99#ifndef IPPOOL_NOIP6
100extern unsigned long int ippool_hash6(struct in6_addr *addr);
101extern int ippool_getip6(struct ippool_t *this, struct in6_addr *addr);
102extern int ippool_returnip6(struct ippool_t *this, struct in6_addr *addr);
103#endif
104
105#endif /* !_IPPOOL_H */