blob: 46ba447654ae211c7df50d6ef34a81bddffd3ecd [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
jjakoa7cd2492003-04-11 09:40:12 +000034#define IPPOOL_NOIP6
35
36#define IPPOOL_NONETWORK 0x01
37#define IPPOOL_NOBROADCAST 0x02
jjako504ee452003-08-20 15:25:54 +000038#define IPPOOL_NOGATEWAY 0x04
jjakoc6762cf2004-04-28 14:52:58 +000039#define IPPOOL_NODESTADDR 0x08
jjakoa7cd2492003-04-11 09:40:12 +000040
jjako88c22162003-07-06 19:33:18 +000041#define IPPOOL_STATSIZE 0x10000
42
jjakoa7cd2492003-04-11 09:40:12 +000043struct ippoolm_t; /* Forward declaration */
44
45struct ippool_t {
46 int listsize; /* Total number of addresses */
jjako88c22162003-07-06 19:33:18 +000047 int allowdyn; /* Allow dynamic IP address allocation */
48 int allowstat; /* Allow static IP address allocation */
49 struct in_addr stataddr; /* Static address range network address */
50 struct in_addr statmask; /* Static address range network mask */
jjakoa7cd2492003-04-11 09:40:12 +000051 struct ippoolm_t *member; /* Listsize array of members */
52 int hashsize; /* Size of hash table */
53 int hashlog; /* Log2 size of hash table */
54 int hashmask; /* Bitmask for calculating hash */
55 struct ippoolm_t **hash; /* Hashsize array of pointer to member */
jjako88c22162003-07-06 19:33:18 +000056 struct ippoolm_t *firstdyn; /* Pointer to first free dynamic member */
57 struct ippoolm_t *lastdyn; /* Pointer to last free dyanmic member */
58 struct ippoolm_t *firststat; /* Pointer to first free static member */
59 struct ippoolm_t *laststat; /* Pointer to last free static member */
jjakoa7cd2492003-04-11 09:40:12 +000060};
61
62struct ippoolm_t {
63#ifndef IPPOOL_NOIP6
64 struct in6_addr addr; /* IP address of this member */
65#else
66 struct in_addr addr; /* IP address of this member */
67#endif
jjako88c22162003-07-06 19:33:18 +000068 int inuse; /* 0=available; 1= dynamic; 2 = static */
jjakoa7cd2492003-04-11 09:40:12 +000069 struct ippoolm_t *nexthash; /* Linked list part of hash table */
jjako88c22162003-07-06 19:33:18 +000070 struct ippoolm_t *prev, *next; /* Linked list of free dynamic or static */
jjakoa7cd2492003-04-11 09:40:12 +000071 void *peer; /* Pointer to peer protocol handler */
72};
73
jjako88c22162003-07-06 19:33:18 +000074/* The above structures require approximately 20+4 = 24 bytes for
jjakoa7cd2492003-04-11 09:40:12 +000075 each address (IPv4). For IPv6 the corresponding value is 32+4 = 36
76 bytes for each address. */
77
78/* Hash an IP address using code based on Bob Jenkins lookupa */
79extern unsigned long int ippool_hash4(struct in_addr *addr);
80
81/* Create new address pool */
jjako88c22162003-07-06 19:33:18 +000082extern int ippool_new(struct ippool_t **this, char *dyn, char *stat,
83 int allowdyn, int allowstat, int flags);
jjakoa7cd2492003-04-11 09:40:12 +000084
85/* Delete existing address pool */
86extern int ippool_free(struct ippool_t *this);
87
88/* Find an IP address in the pool */
89extern int ippool_getip(struct ippool_t *this, struct ippoolm_t **member,
90 struct in_addr *addr);
91
92/* Get an IP address. If addr = 0.0.0.0 get a dynamic IP address. Otherwise
93 check to see if the given address is available */
94extern int ippool_newip(struct ippool_t *this, struct ippoolm_t **member,
95 struct in_addr *addr);
96
97/* Return a previously allocated IP address */
jjako88c22162003-07-06 19:33:18 +000098extern int ippool_freeip(struct ippool_t *this, struct ippoolm_t *member);
jjakoa7cd2492003-04-11 09:40:12 +000099
100/* Get net and mask based on ascii string */
101extern int ippool_aton(struct in_addr *addr, struct in_addr *mask,
102 char *pool, int number);
103
104
105#ifndef IPPOOL_NOIP6
106extern unsigned long int ippool_hash6(struct in6_addr *addr);
107extern int ippool_getip6(struct ippool_t *this, struct in6_addr *addr);
108extern int ippool_returnip6(struct ippool_t *this, struct in6_addr *addr);
109#endif
110
111#endif /* !_IPPOOL_H */