#pragma implementation /* #Specification: dnsconf / ip range / strategy linuxconf allows one to qualify the different IP numbers of his network. For exemple, one managing a class C network such as 192.168.1.0 may want to organised things this way # 192.168.1.1-20 Servers 192.168.1.21-40 IP for PPP dialin 192.168.1.41-60 Virtual domain 192.168.1.61-254 Reserved # This has two effects. First it documents the network and allows a new operator to have some clues. Next, it allows linuxconf to help you find an unused IP number. The user interface for managing IP number use this and will locate one unused IP number in every range (If possible). */ #include #include #include "dnsconf.h" #include "internal.h" #include "dnsconf.m" #include static DNSCONF_HELP_FILE help_iprange("iprange"); static char IPDB[]="IPDB"; static char RANGE[]="RANGE"; PUBLIC IPMAP *IPMAPS::getitem(int no) { return (IPMAP*)ARRAY::getitem(no); } PUBLIC IPMAPS::IPMAPS() { SSTRINGS lst; int nb = linuxconf_getall (IPDB,RANGE,lst,0); for (int i=0; iget())); } } /* Update /etc/conf.linuxconf */ PUBLIC int IPMAPS::write() { linuxconf_setcursys (subsys_dnsserv); linuxconf_removeall (IPDB,RANGE); int n = getnb(); for (int i=0; iiprange.is_empty()){ char buf[200]; sprintf (buf,"%s %s",e->iprange.get(),e->comment.get()); linuxconf_add (IPDB,RANGE,buf); } } return linuxconf_save(); } /* Compute the available IP number in each IP address range. adrs will be sort by this algoryth. */ PUBLIC void IPMAPS::setuse (IP_ADDRS &adrs) { int n = getnb(); int i; for (i=0; isetup(); } adrs.sort(); int na = adrs.getnb(); for (i=0; i < na; i++){ IP_ADDR *adr = adrs.getitem(i); for (int j=0; jsetuse(adr); } } for (i=0; iavail.reformat(); } } /* Setup the comp field options helping in IP number allocation. */ PUBLIC void IPMAPS::setcombo (class FIELD_COMBO *comb) { int n = getnb(); for (int i=0; iavail.get(); if (e->over){ str = MSG_U(F_RANGEFULL,"None available"); } comb->addopt (str,e->comment.get()); } } PRIVATE void IPMAPS::newdiaf (DIALOG &dia, IPMAP *e) { dia.newf_str (MSG_U(F_IPRANGE,"One IP range"),e->iprange); dia.newf_str (MSG_U(F_RANGECOMMENT,"Id/description"),e->comment); } PUBLIC int IPMAPS::edit() { int no = 0; if (getnb()==0) add (new IPMAP); DIALOG dia; int n = getnb(); for (int i=0; iiprange.is_empty() && e->setup()==-1){ no = j*2; err = 1; xconf_error (MSG_U(E_IVLDIPMAP ,"Invalid IP range definition %s\n" "Expected X.Y.Z.W1-W2\n" "or X.Y.Z1.W1-Z2.W2") ,e->iprange.get()); break; } } if (!err){ write(); break; } }else if (code == MENU_MESSAGE){ if (dialog_testmessage(growmsg)){ dia.remove_del (dia.getnb()-1); // Remove the button IPMAP *e = new IPMAP; add (e); newdiaf(dia,e); // Put the button back dia.new_button (MSG_R(B_GROWFORM),MSG_R(I_GROWFORM) ,growmsg); // Put it back } } } return 0; } void ipmap_edit () { IPMAPS maps; maps.edit(); } /* Return 0 if ok Return -1 if no IP available in the range Return -2 if the range does not exist. */ PUBLIC int IPMAPS::allocateip(const char *range, char *ip, DNS &dns) { int ret = -2; int n = getnb(); ip[0] = '\0'; for (int i=0; icomment.cmp(range)==0){ ret = -1; IP_ADDRS adrs; dns.getalladr(adrs); setuse (adrs); if (!m->over){ m->avail.copy (ip); ret = 0; } } } return ret; } /* Find the first available IP in a range. Return 0 if ok Return -1 if no IP available in the range Return -2 if the range does not exist. */ int ipmap_allocateip (const char *range, char *ip, DNS &dns) { IPMAPS maps; return maps.allocateip(range,ip,dns); } /* Check if one IP number is in one of the ranges. Return false if not. */ PUBLIC bool IPMAPS::inrange(const char *ip) { bool ret = false; IP_ADDR addr (ip); for (int i=0; isetup(); // fprintf (stderr,"compare :%s: :%s: %d :%s: %d\n",ip // ,m->minimum.get(),m->minimum.cmp(&addr) // ,m->maximum.get(),m->maximum.cmp(&addr)); if (m->minimum.cmp(&addr) <= 0 && m->maximum.cmp(&addr) >= 0){ ret = true; break; } } return ret; }