00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
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 
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 
00087 
00088 
00089 
00092 
00093 
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     
00124     int error_num = 0;
00125     if(gethostbyname_r(name,&host,buf,sizeof(buf),&res1,&error_num) != 0) {
00126 #endif
00127 #endif
00128       
00129       
00130       
00131       
00132       
00133       snprintf(hostaddr,sizeof(hostaddr)-1,"%s",name);
00134       ostr = hostaddr;
00135       return ostr;
00136     }
00137 
00138 
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 
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 
00163     ret = ipa_;
00164 
00165     return ret;
00166   }
00169   ipaddr(const char *s) {
00170 
00171     fill(s);
00172   }
00175   ipaddr(const string s) {
00176 
00177     fill(s.c_str());
00178   }
00181   ipaddr() {
00182 
00183 
00184     fill("localhost");
00185   }
00188   ~ipaddr() {
00189 
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 
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 
00220   }
00224   string get() {
00225     string ostr;
00226 
00227     ostr = sip(ipa_);
00228 
00229     return ostr;
00230   }
00233   int get(int n) {
00234     char str[100];
00235 
00236     snprintf(str,sizeof(str)-1,"%s",sip(ipa_).c_str());
00237 
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 
00273     string ostr(hname_);
00274 
00275     return ostr;
00276   }
00279   bool operator ==(ipaddr a) {
00280     bool ret;
00281 
00282     ret = false;
00283     if(ipa_ == a.ipa_) ret = true;
00284 
00285     return ret;
00286   }
00289   bool operator !=(ipaddr a) {
00290     bool ret;
00291 
00292     ret = false;
00293     if(ipa_ != a.ipa_) ret = true;
00294 
00295     return ret;
00296   }
00299   bool operator ==(const char *a) {
00300     bool ret;
00301 
00302     ret = false;
00303     ipaddr aaa(a);
00304     if(ipa_ == aaa.ipa_) ret = true;
00305 
00306     return ret;
00307   }
00310   bool operator !=(const char *a) {
00311     bool ret;
00312 
00313     ret = false;
00314     ipaddr aaa(a);
00315     if(ipa_ != aaa.ipa_) ret = true;
00316 
00317     return ret;
00318   }
00321   void operator =(ipaddr a) {
00322 
00323 
00324     ipa_ = a.ipa_;
00325     snprintf(hname_,sizeof(hname_),"%s",a.hname_);
00326 
00327 
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__