/* AddressManager.cpp IP Address Manager for Aladin Copyright (C) 2003 Mike Saywell Southampton Open Wireless Network, http://www.sown.org.uk ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ This class manages the IP addresses available for links. It takes a range of IPs (network address+netmask), splitting it into as many /30 subnets as possible. These are allocated on demmand. ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "AddressManager.h" AddressManager::AddressManager() { this->list = NULL; if(DEBUG) { cout << "New Allocation Manager" << endl; } } void AddressManager::setRange(struct in_addr* net, struct in_addr* mask) { this->netmask = mask; this->network = net; if(DEBUG) { cout << "Allocating addresses from " << inet_ntoa(*net); cout << "/" << inet_ntoa(*mask) << endl; } this->list = new struct AllocationList; struct AllocationList* list = this->list; struct AddressAllocation* current = &(list->alloc); struct in_addr* link_mask = new struct in_addr; inet_aton("255.255.255.252", link_mask); // Create the first entry list->next = 0; current->allocated = false; memcpy(&(current->network), net, sizeof(struct in_addr)); memcpy(&(current->netmask), link_mask, sizeof(struct in_addr)); struct in_addr* net_inc = new struct in_addr; memcpy(net_inc, net, sizeof(struct in_addr)); net_inc->s_addr = htonl(4+ntohl(net_inc->s_addr)); if (DEBUG) { cout << "Allocation list:" << endl; cout << "Network: " << inet_ntoa(current->network) << endl; } while ((net_inc->s_addr & mask->s_addr) == (net->s_addr & mask->s_addr)) { // Add an item to the linked list list->next = new AllocationList(); list = list->next; // Populate the item current = &(list->alloc); current->allocated = false; list->next = NULL; memcpy(&(current->network), net_inc, 4); memcpy(&(current->netmask), link_mask, 4); if (DEBUG) { cout << "Network: " << inet_ntoa(current->network) << endl; } net_inc->s_addr = htonl(4+ntohl(net_inc->s_addr)); } } AddressAllocation* AddressManager::allocate() { struct AllocationList* current = this->list; while(current== NULL) { sleep(1); } while (current->alloc.allocated == true) { if(!current->next) return NULL; current = current ->next; } current->alloc.allocated = true; return ¤t->alloc; } void AddressManager::deallocate(AddressAllocation* alloc) { alloc->allocated = false; }