Main Page | Class Hierarchy | Class List | File List | Class Members

ipaddr.h

00001 // tacppd common IP address handling
00002 // (c) Copyright in 2000-2005 by tacppd team and contributors
00003 // see http://tacppd.org for more information
00004 
00005 //  This program is free software; you can redistribute it and/or modify
00006 //  it under the terms of the GNU General Public License as published by
00007 //  the Free Software Foundation; either version 2 of the License, or
00008 //  (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program; if not, write to the Free Software
00017 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // all comments which start from /** - this is comments for KDoc
00020 //  for classes
00021 //    @short [short description of the class]
00022 //    @author [class author]
00023 //    @version [class version]
00024 //    @see [references to other classes or methods]
00025 //  for methods
00026 //    @see [references]
00027 //    @return [sentence describing the return value]
00028 //    @exception [list the exeptions that could be thrown]
00029 //    @param [name id] [description]    - can be multiple
00030 
00031 #ifndef __IPADDR_H__
00032 #define __IPADDR_H__
00033 
00034 #ifndef _PTHREADS
00035   #define   _PTHREADS   // enable STL pthread-safe code
00036 #endif //_PTHREADS
00037 
00038 // set pthread-safe stdlibs
00039 #ifndef _REENTRANT
00040   #define       _REENTRANT
00041 #endif //_REENTRANT
00042 
00043 #ifdef FREEBSD
00044 #include <sys/types.h>
00045 #include <netinet/in_systm.h>
00046 #include <netinet/in.h>
00047 #include <netinet/ip.h>
00048 #endif //FREEBSD
00049 
00050 #include <sys/socket.h>
00051 #include <sys/types.h>
00052 #include <netinet/in.h>
00053 #include <arpa/inet.h>
00054 #include <netdb.h>
00055 #include <stdio.h>
00056 #include <unistd.h>
00057 #include <string>
00058 #include <pthread.h>
00059 
00060 #ifdef SOLARIS
00061 #include <strings.h>
00062 #include <netinet/in.h>
00063 #include <netinet/ip.h>
00064 #include <netinet/in_systm.h>
00065 #endif
00066 
00067 using std::string;
00068 #ifndef SOLARIS
00069 using std::snprintf;
00070 #endif
00071 
00072 #define UNKNOWN_IP "0.0.0.0"
00073 
00074 namespace tacpp {
00075 
00081 class ipaddr {
00084 //  pthread_mutex_t mutex_;
00087 //  void lock() {
00088 //    pthread_mutex_lock(&mutex_);
00089 //  }
00092 //  void unlock() {
00093 //    pthread_mutex_unlock(&mutex_);
00094 //  }
00097   char hname_[100];
00100   string getipfromname(const char *name) {
00101     if(!name) return UNKNOWN_IP;
00102     if(strlen(name)==0) return UNKNOWN_IP;
00103     if(strcmp(name,"localhost")==0) return "127.0.0.1";
00104     if(strcmp(name,"0.0.0.0") == 0) return UNKNOWN_IP;
00105     if(strcmp(name,UNKNOWN_IP) == 0) return UNKNOWN_IP;
00106     struct in_addr nas_addr;
00107     struct hostent host;
00108     struct hostent res;
00109     struct hostent *res1 = &res;
00110     char buf[4096];
00111     bzero(buf,sizeof(buf));
00112     char hostaddr[64];
00113     string ostr;
00114     bzero(hostaddr,sizeof(hostaddr));
00115 #ifdef SOLARIS
00116     int error_num = 0;
00117     if((res1 = gethostbyname_r(name,&host,buf,sizeof(buf),&error_num)) != 0) {
00118 #endif
00119 #ifndef SOLARIS
00120 #ifdef FREEBSD
00121     if((res1 = gethostbyname(name)) != 0) {
00122 #else
00123     // Linux
00124     int error_num = 0;
00125     if(gethostbyname_r(name,&host,buf,sizeof(buf),&res1,&error_num) != 0) {
00126 #endif
00127 #endif
00128       // error_num =
00129       //  HOST_NOT_FOUND
00130       //  NO_ADDRESS
00131       //  NO_RECOVERY
00132       //  TRY_AGAIN
00133       snprintf(hostaddr,sizeof(hostaddr)-1,"%s",name);
00134       ostr = hostaddr;
00135       return ostr;
00136     }
00137 //    if(sizeof(struct in_addr) < (unsigned int)host->h_length) {
00138 //std::cerr<<"something wrong in ipaddr.h"<<std::endl;
00139 //    }
00140     bcopy(host.h_addr,(char*)&nas_addr,host.h_length);
00141     strncpy(hostaddr,(char*)inet_ntoa(nas_addr),sizeof(hostaddr)-1);
00142     ostr = hostaddr;
00143 //    freehostent(host);
00144     return ostr;
00145   }
00148   string gethostn() {
00149     char hostname[64];
00150     gethostname((char*)hostname,sizeof(hostname));
00151     string ostr(hostname);
00152     return ostr;
00153   }
00156   unsigned long ipa_;
00157  public:
00160   unsigned long getIpa() {
00161     unsigned long ret;
00162 //    lock();
00163     ret = ipa_;
00164 //    unlock();
00165     return ret;
00166   }
00169   ipaddr(const char *s) {
00170 //    pthread_mutex_init(&mutex_,0);
00171     fill(s);
00172   }
00175   ipaddr(const string s) {
00176 //    pthread_mutex_init(&mutex_,0);
00177     fill(s.c_str());
00178   }
00181   ipaddr() {
00182 //    pthread_mutex_init(&mutex_,0);
00183 //    fill(gethostn().c_str());
00184     fill("localhost");
00185   }
00188   ~ipaddr() {
00189 //    pthread_mutex_destroy(&mutex_);
00190   }
00193   unsigned long ips(string i) {
00194     unsigned long ret;
00195     ret = inet_addr(i.c_str());
00196     return ret;
00197   }
00200   string sip(unsigned long i) {
00201     struct in_addr a;
00202     string str;
00203     a.s_addr = (in_addr_t)i;
00204     str = inet_ntoa(a);
00205     return str;
00206   }
00209   void fill(const char *s) {
00210     if(!s && strlen(s)<1) {
00211       fill("localhost");
00212       return;
00213     }
00214 //    lock();
00215     snprintf(hname_,sizeof(hname_)-1,"%s",s);
00216     char str[100];
00217     snprintf(str,sizeof(str)-1,"%s",getipfromname(s).c_str());
00218     ipa_ = ips(str);
00219 //    unlock();
00220   }
00224   string get() {
00225     string ostr;
00226 //    lock();
00227     ostr = sip(ipa_);
00228 //    unlock();
00229     return ostr;
00230   }
00233   int get(int n) {
00234     char str[100];
00235 //    lock();
00236     snprintf(str,sizeof(str)-1,"%s",sip(ipa_).c_str());
00237 //    unlock();
00238     int o1 = 0;
00239     int o2 = 0;
00240     int o3 = 0;
00241     int o4 = 0;
00242     int k=0, i=0;
00243     char d1[4];
00244     char d2[4];
00245     char d3[4];
00246     char d4[4];
00247     bzero(d1, sizeof(d1));
00248     bzero(d2, sizeof(d2));
00249     bzero(d3, sizeof(d3));
00250     bzero(d4, sizeof(d4));
00251     for(i=0; str[k] && str[k]!='.' && i<3; i++,k++) d1[i] = str[k];
00252     if(str[k]=='.') k++;
00253     for(i=0; str[k] && str[k]!='.' && i<3; i++,k++) d2[i] = str[k];
00254     if(str[k]=='.') k++;
00255     for(i=0; str[k] && str[k]!='.' && i<3; i++,k++) d3[i] = str[k];
00256     if(str[k]=='.') k++;
00257     for(i=0; str[k] && str[k]!='.' && str[k]!=' ' && i<3; i++,k++) d4[i] = str[k];
00258     o1 = atoi(d1);
00259     o2 = atoi(d2);
00260     o3 = atoi(d3);
00261     o4 = atoi(d4);
00262     if(n == 0) return o1;
00263     if(n == 1) return o2;
00264     if(n == 2) return o3;
00265     if(n == 3) return o4;
00266     return 0;
00267   }
00271   string getn() {
00272 //    lock();
00273     string ostr(hname_);
00274 //    unlock();
00275     return ostr;
00276   }
00279   bool operator ==(ipaddr a) {
00280     bool ret;
00281 //    lock();
00282     ret = false;
00283     if(ipa_ == a.ipa_) ret = true;
00284 //    unlock();
00285     return ret;
00286   }
00289   bool operator !=(ipaddr a) {
00290     bool ret;
00291 //    lock();
00292     ret = false;
00293     if(ipa_ != a.ipa_) ret = true;
00294 //    unlock();
00295     return ret;
00296   }
00299   bool operator ==(const char *a) {
00300     bool ret;
00301 //    lock();
00302     ret = false;
00303     ipaddr aaa(a);
00304     if(ipa_ == aaa.ipa_) ret = true;
00305 //    unlock();
00306     return ret;
00307   }
00310   bool operator !=(const char *a) {
00311     bool ret;
00312 //    lock();
00313     ret = false;
00314     ipaddr aaa(a);
00315     if(ipa_ != aaa.ipa_) ret = true;
00316 //    unlock();
00317     return ret;
00318   }
00321   void operator =(ipaddr a) {
00322 //    lock();
00323 //    a.lock();
00324     ipa_ = a.ipa_;
00325     snprintf(hname_,sizeof(hname_),"%s",a.hname_);
00326 //    a.unlock();
00327 //    unlock();
00328   }
00331   void operator <<(const char *a) {
00332     fill(a);
00333   }
00336   void operator =(const char *a) {
00337     fill(a);
00338   }
00339 };
00340 
00341 };
00342 
00343 #endif //__IPADDR_H__

Generated on Thu Jul 21 23:09:45 2005 for tacppd.kdevelop by doxygen 1.3.5