/* aladin.cpp Aladin - Automatic Link Addressing Daemon for wIreless Nets Copyright (C) 2003 Mike Saywell Southampton Open Wireless Network, http://www.sown.org.uk 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 "aladin.h" void usage() { cout << "aladin [-d] [-F] -f " << endl; cout << " -d Debug mode" << endl; cout << " -F Stay in foreground" << endl; } int main(int argc, char *argv[]) { char hostMAC[18]; char configFile[128]; char pidFile[128]; char hostapIf[IF_NAMESIZE]; AddressManager* addrMan = new AddressManager(); IfMan* ifMan = new IfMan(addrMan); strcpy(configFile, ""); int c; int bg = 1; while ((c = getopt(argc, argv, "Fdf:")) != -1) { switch (c) { case 'f': { strcpy(configFile, optarg); break; } case 'F': { bg = 0; break; } case 'd': { DEBUG = 1; break; } default: { usage(); exit(1); } } } if (!strlen(configFile)) { usage(); exit(1); } // Read in config ifstream config; config.open(configFile); if (!config) { cerr << "FATAL: Failed to open config file " << configFile << ".\n"; exit(1); } if(bg) daemon(0,1); char line[256]; bool enabled = false; if (DEBUG) { cout << "Reading Config..." << endl; } while (config.getline(line, 256)) { if ((strncmp(line, "#", 1) == 0) || (strlen(line) < 2)) continue; string keyword(strtok(line, " \t")); // standard interfaces which we should run on if(keyword=="interface") { char* if_name = strtok(NULL, " \t"); while(if_name != NULL) { enabled = true; ifMan->enable(if_name); if_name = strtok(NULL, " \t"); if (if_name && strncmp(if_name, "#", 1) == 0) { if_name=NULL; } } } // HostAP interface, we should run on all its WDS interfaces else if(keyword=="hostap") { char* if_name = strtok(NULL, " \t"); while(if_name != NULL) { enabled = true; ifMan->wdsMonitor(if_name); if_name = strtok(NULL, " \t"); if (if_name && strncmp(if_name, "#", 1) == 0) { if_name=NULL; } } } else if (keyword=="pool") { struct in_addr* network = new struct in_addr; struct in_addr* netmask = new struct in_addr; if (inet_aton(strtok((char *) strtok(NULL, " \t"), "/"), network)==0) { cerr << "Invalid network address." << endl; } // Initial netmask of 255.255.255.255 inet_aton("255.255.255.255", netmask); // Insert 0s from the rhs netmask->s_addr <<= (32-atoi(strtok(NULL, "/"))); // Convert to network byte order netmask->s_addr = ntohl(netmask->s_addr); if (DEBUG) { cout << "Network: " << inet_ntoa(*network) << endl; cout << "Netmask: " << inet_ntoa(*netmask) << endl; } addrMan->setRange(network, netmask); } else if (keyword=="pid") { strcpy(pidFile, strtok(NULL, " \t")); } } config.close (); if(!enabled) { cerr << "No interfaces defined in config file!" << endl; exit(1); } if (strlen(pidFile) != 0) { FILE* fp_pid = fopen(pidFile, "w"); if (!fp_pid) { perror("Unable to open PID file"); } else { fprintf(fp_pid, "%d\n", getpid()); fclose(fp_pid); } } while(1) { sleep(999999); } }