aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ippool.h
blob: e3c1c920a8ee7a8eeb90b306aa0a6272d0d1edbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* 
 * IP address pool functions.
 * Copyright (C) 2003, 2004 Mondru AB.
 * 
 * The contents of this file may be used under the terms of the GNU
 * General Public License Version 2, provided that the above copyright
 * notice and this permission notice is included in all copies or
 * substantial portions of the software.
 * 
 */

#ifndef _IPPOOL_H
#define _IPPOOL_H

#include "../lib/in46_addr.h"

/* Assuming that the address space is fragmented we need a hash table
   in order to return the addresses.

   The list pool should provide for both IPv4 and IPv6 addresses.

   When initialising a new address pool it should be possible to pass
   a string of CIDR format networks: "10.0.0.0/24 10.15.0.0/20" would
   translate to 256 addresses starting at 10.0.0.0 and 1024 addresses
   starting at 10.15.0.0. 

   The above also applies to IPv6 which can be specified as described
   in RFC2373.
*/

#define IPPOOL_NONETWORK   0x01
#define IPPOOL_NOBROADCAST 0x02
#define IPPOOL_NOGATEWAY   0x04

#define IPPOOL_STATSIZE 0x10000

struct ippoolm_t;		/* Forward declaration */

struct ippool_t {
	unsigned int listsize;	/* Total number of addresses */
	int allowdyn;		/* Allow dynamic IP address allocation */
	int allowstat;		/* Allow static IP address allocation */
	struct in46_addr stataddr;	/* Static address range network address */
	size_t stataddrprefixlen;	/* IPv6 prefix length of stataddr */
	struct ippoolm_t *member;	/* Listsize array of members */
	unsigned int hashsize;	/* Size of hash table */
	int hashlog;		/* Log2 size of hash table */
	int hashmask;		/* Bitmask for calculating hash */
	struct ippoolm_t **hash;	/* Hashsize array of pointer to member */
	struct ippoolm_t *firstdyn;	/* Pointer to first free dynamic member */
	struct ippoolm_t *lastdyn;	/* Pointer to last free dynamic member */
	struct ippoolm_t *firststat;	/* Pointer to first free static member */
	struct ippoolm_t *laststat;	/* Pointer to last free static member */
};

struct ippoolm_t {
	struct in46_addr addr;	/* IP address of this member */
	int inuse;		/* 0=available; 1= dynamic; 2 = static */
	struct ippoolm_t *nexthash;	/* Linked list part of hash table */
	struct ippoolm_t *prev, *next;	/* Linked list of free dynamic or static */
	void *peer;		/* Pointer to peer protocol handler */
};

/* The above structures require approximately 20+4 = 24 bytes for
   each address (IPv4). For IPv6 the corresponding value is 32+4 = 36
   bytes for each address. */

/* Hash an IP address using code based on Bob Jenkins lookupa */
extern unsigned long int ippool_hash(struct in46_addr *addr);

/* Create new address pool */
extern int ippool_new(struct ippool_t **this, const char *dyn, const char *stat,
		      int allowdyn, int allowstat, int flags);

/* Delete existing address pool */
extern int ippool_free(struct ippool_t *this);

/* Find an IP address in the pool */
extern int ippool_getip(struct ippool_t *this, struct ippoolm_t **member,
			struct in46_addr *addr);

/* Get an IP address. If addr = 0.0.0.0 get a dynamic IP address. Otherwise
   check to see if the given address is available */
extern int ippool_newip(struct ippool_t *this, struct ippoolm_t **member,
			struct in46_addr *addr, int statip);

/* Return a previously allocated IP address */
extern int ippool_freeip(struct ippool_t *this, struct ippoolm_t *member);

/* Get net and mask based on ascii string */
int ippool_aton(struct in46_addr *addr, size_t *prefixlen, const char *pool, int number);

/* Increase IPv4/IPv6 address by 1 */
extern void in46a_inc(struct in46_addr *addr);

#endif /* !_IPPOOL_H */