jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * IP address pool functions. |
jjako | c321396 | 2004-09-09 20:23:50 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003, 2004 Mondru AB. |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 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 | * |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #ifndef _IPPOOL_H |
| 13 | #define _IPPOOL_H |
| 14 | |
| 15 | /* Assuming that the address space is fragmented we need a hash table |
| 16 | in order to return the addresses. |
| 17 | |
| 18 | The list pool should provide for both IPv4 and IPv6 addresses. |
| 19 | |
| 20 | When initialising a new address pool it should be possible to pass |
| 21 | a string of CIDR format networks: "10.0.0.0/24 10.15.0.0/20" would |
| 22 | translate to 256 addresses starting at 10.0.0.0 and 1024 addresses |
| 23 | starting at 10.15.0.0. |
| 24 | |
| 25 | The above also applies to IPv6 which can be specified as described |
| 26 | in RFC2373. |
| 27 | */ |
| 28 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 29 | #define IPPOOL_NOIP6 |
| 30 | |
| 31 | #define IPPOOL_NONETWORK 0x01 |
| 32 | #define IPPOOL_NOBROADCAST 0x02 |
jjako | 504ee45 | 2003-08-20 15:25:54 +0000 | [diff] [blame] | 33 | #define IPPOOL_NOGATEWAY 0x04 |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 34 | |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 35 | #define IPPOOL_STATSIZE 0x10000 |
| 36 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 37 | struct ippoolm_t; /* Forward declaration */ |
| 38 | |
| 39 | struct ippool_t { |
| 40 | int listsize; /* Total number of addresses */ |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 41 | int allowdyn; /* Allow dynamic IP address allocation */ |
| 42 | int allowstat; /* Allow static IP address allocation */ |
| 43 | struct in_addr stataddr; /* Static address range network address */ |
| 44 | struct in_addr statmask; /* Static address range network mask */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 45 | struct ippoolm_t *member; /* Listsize array of members */ |
| 46 | int hashsize; /* Size of hash table */ |
| 47 | int hashlog; /* Log2 size of hash table */ |
| 48 | int hashmask; /* Bitmask for calculating hash */ |
| 49 | struct ippoolm_t **hash; /* Hashsize array of pointer to member */ |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 50 | struct ippoolm_t *firstdyn; /* Pointer to first free dynamic member */ |
jjako | ada9ffa | 2004-12-30 16:39:11 +0000 | [diff] [blame] | 51 | struct ippoolm_t *lastdyn; /* Pointer to last free dynamic member */ |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 52 | struct ippoolm_t *firststat; /* Pointer to first free static member */ |
| 53 | struct ippoolm_t *laststat; /* Pointer to last free static member */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | struct ippoolm_t { |
| 57 | #ifndef IPPOOL_NOIP6 |
| 58 | struct in6_addr addr; /* IP address of this member */ |
| 59 | #else |
| 60 | struct in_addr addr; /* IP address of this member */ |
| 61 | #endif |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 62 | int inuse; /* 0=available; 1= dynamic; 2 = static */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 63 | struct ippoolm_t *nexthash; /* Linked list part of hash table */ |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 64 | struct ippoolm_t *prev, *next; /* Linked list of free dynamic or static */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 65 | void *peer; /* Pointer to peer protocol handler */ |
| 66 | }; |
| 67 | |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 68 | /* The above structures require approximately 20+4 = 24 bytes for |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 69 | each address (IPv4). For IPv6 the corresponding value is 32+4 = 36 |
| 70 | bytes for each address. */ |
| 71 | |
| 72 | /* Hash an IP address using code based on Bob Jenkins lookupa */ |
| 73 | extern unsigned long int ippool_hash4(struct in_addr *addr); |
| 74 | |
| 75 | /* Create new address pool */ |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 76 | extern int ippool_new(struct ippool_t **this, char *dyn, char *stat, |
| 77 | int allowdyn, int allowstat, int flags); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 78 | |
| 79 | /* Delete existing address pool */ |
| 80 | extern int ippool_free(struct ippool_t *this); |
| 81 | |
| 82 | /* Find an IP address in the pool */ |
| 83 | extern 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 */ |
| 88 | extern int ippool_newip(struct ippool_t *this, struct ippoolm_t **member, |
jjako | ada9ffa | 2004-12-30 16:39:11 +0000 | [diff] [blame] | 89 | struct in_addr *addr, int statip); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 90 | |
| 91 | /* Return a previously allocated IP address */ |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 92 | extern int ippool_freeip(struct ippool_t *this, struct ippoolm_t *member); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 93 | |
| 94 | /* Get net and mask based on ascii string */ |
| 95 | extern int ippool_aton(struct in_addr *addr, struct in_addr *mask, |
| 96 | char *pool, int number); |
| 97 | |
| 98 | |
| 99 | #ifndef IPPOOL_NOIP6 |
| 100 | extern unsigned long int ippool_hash6(struct in6_addr *addr); |
| 101 | extern int ippool_getip6(struct ippool_t *this, struct in6_addr *addr); |
| 102 | extern int ippool_returnip6(struct ippool_t *this, struct in6_addr *addr); |
| 103 | #endif |
| 104 | |
| 105 | #endif /* !_IPPOOL_H */ |