ip-address.cc

00001 /*
00002  * Copyright (c) 2008 Regents of the SIGNET lab, University of Padova.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University of Padova (SIGNET lab) nor the 
00014  *    names of its contributors may be used to endorse or promote products 
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
00019  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00020  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00021  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00023  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00024  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00025  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00026  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00027  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include "ip-address.h"
00031 
00032 static class IPAddressClass : public TclClass {
00033 public:
00034         IPAddressClass() : TclClass("Address/IP") {}
00035         TclObject* create(int argc, const char *const *argv) 
00036         {
00037                 int i=0;
00038                 while((strcmp(argv[i],"Address/IP"))&&(i<argc))
00039                 {
00040                         i++;
00041                 }
00042                 if(argc - i - 1 == 2)
00043                         {
00044                                 return (new IPAddress(argv[i+2]));
00045                         }
00046 //              else if(argc - i - 1 > 2)
00047 //                      return (new IPAddress(argv[i+2], argv[i+3]));
00048                 else
00049                         return (new IPAddress());
00050         }
00051 } class_ip_address;
00052 
00053 IPAddress::IPAddress() : ipAddr_(0)
00054 {
00055 }
00056 
00057 IPAddress::IPAddress(const char *ip)
00058 {
00059         setAddress(ip);
00060         printf("IPAddress(%s)\n", ip);
00061 //      ipAddr_ = (int*)(ip + sizeof(int));
00062 //      generateNetmask();
00063 }
00064 
00065 IPAddress::~IPAddress()
00066 {
00067 }
00068 
00069 int IPAddress::toString(char *str, int strlen)
00070 {
00071         char addr[20];
00072         int ip;
00073         getAddr(&ip, sizeof(int));
00074         sprintf(addr,"%d.%d.%d.%d", (ip & 0xff000000)>>24,(ip & 0x00ff0000)>>16, (ip & 0x0000ff00)>>8, (ip & 0x000000ff));
00075         strncpy(str, addr, strlen);
00076         if(::strlen(addr)>= strlen)
00077                 return 1;
00078         return 0;
00079 }
00080 
00081 int IPAddress::strlen()
00082 {
00083         char addr[20];
00084         int ip;
00085         getAddr(&ip, sizeof(int));
00086         sprintf(addr,"%d.%d.%d.%d", (ip & 0xff000000)>>24,(ip & 0x00ff0000)>>16, (ip & 0x0000ff00)>>8, (ip & 0x000000ff));
00087         return ::strlen(addr);
00088 }
00089 
00090 void IPAddress::setAddress(const char *addr)
00091 {
00092         int level[4] = {0,0,0,0};
00093         char tmp[20];
00094         strncpy(tmp,addr,19);
00095         tmp[19] = '\0';
00096         char *p = strtok(tmp, ".");
00097         for(int i = 0; p && i < 4; p = strtok(NULL, "."), i++)
00098         {
00099                 level[i] = atoi(p);
00100                 if(level[i] > 255)
00101                         level[i] = 255;
00102                 else if(level[i] < 0)
00103                         level[i] = 0;
00104         }
00105         int ip = 0;
00106         for(int i = 0; i < 4; i++)
00107         {
00108                 ip += (level[i] << 8 * (3 - i));
00109         }
00110         setAddr(&ip, sizeof(int));
00111         ipAddr_ = ip;
00112         printf("ipAddr_ %d\n", ipAddr_);
00113 }
00114 
00115 
00116 int IPAddress::command(int argc, const char * const *argv)
00117 {
00118 //      Tcl& tcl = Tcl::instance();
00119         return MrclAddress::command(argc, argv);
00120 }
00121 
00122 
00123 int IPAddress::INT(char* str)
00124 {
00125         if (str==0)
00126                 return (ipAddr_);
00127         else
00128         {
00129                 int addr;
00130                 memcpy(&addr, str + sizeof(int), sizeof(int));
00131                 return (addr);
00132         }
00133 }
00134 
00135 int IPAddress::AND(char* str1, char *str2)
00136 {
00137         int addr1;
00138         int addr2;
00139         int ret;
00140         memcpy(&addr1, str1 + sizeof(int), sizeof(int));
00141 //      addr->getAddr(&addr2, sizeof(int));
00142 //      ret = (addr & addr2);
00143 //      char *ret1 = new char[MRCL_ADDRESS_MAX_LEN];
00144 //      sprintf(ret1,"%d.%d.%d.%d", (ret & 0xff000000)>>24,(ret & 0x00ff0000)>>16, (ret & 0x0000ff00)>>8, (ret & 0x000000ff));
00145         if (str2==0)
00146                 return(ipAddr_ & addr1);
00147         else
00148         {
00149                 memcpy(&addr2, str2 + sizeof(int), sizeof(int));
00150                 return(addr1 & addr2);
00151         }
00152 }
00153 
00154 int IPAddress::OR(char* str1, char *str2)
00155 {
00156         int addr1;
00157         int addr2;
00158         int ret;
00159         memcpy(&addr1, str1 + sizeof(int), sizeof(int));
00160 //      addr->getAddr(&addr2, sizeof(int));
00161 //      ret = (addr & addr2);
00162 //      char *ret1 = new char[MRCL_ADDRESS_MAX_LEN];
00163 //      sprintf(ret1,"%d.%d.%d.%d", (ret & 0xff000000)>>24,(ret & 0x00ff0000)>>16, (ret & 0x0000ff00)>>8, (ret & 0x000000ff));
00164         if (str2==0)
00165         return(ipAddr_ | addr1);
00166         else
00167         {
00168                 memcpy(&addr2, str2 + sizeof(int), sizeof(int));
00169                 return(addr1 | addr2);
00170         }
00171 }
00172 

Generated on Wed Nov 26 15:47:28 2008 for NS-MIRACLE library by  doxygen 1.5.2