ip-address-codec.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-codec.h"
00031 #include"ip-address.h"
00032 #include <ip.h>
00033 
00034 static class IPAddressCodecClass : public TclClass {
00035         public:
00036                 IPAddressCodecClass() : TclClass("Module/MrclIpAddressCodec") {}
00037                 TclObject* create(int, const char*const*) {
00038                         return (new IPAddressCodec());
00039                 }
00040 } class_ip_address_codec_module;
00041 
00042 
00043 IPAddressCodec::IPAddressCodec(): up_(TRUE)
00044 {
00045 }
00046 
00047 IPAddressCodec::~IPAddressCodec()
00048 {
00049 }
00050 
00051 int IPAddressCodec::command(int argc, const char*const* argv)
00052 {
00053         Tcl& tcl = Tcl::instance();
00054         if (argc == 2)
00055         {
00056                 if (strcmp(argv[1], "up") == 0)
00057                 {
00058                         up_=TRUE;
00059                         return TCL_OK;
00060                 }else if (strcmp(argv[1], "down") == 0)
00061                 {
00062                         up_=FALSE;
00063                         return TCL_OK;
00064                 }
00065         }
00066         else if (argc == 3)
00067         {
00068                 if(strcmp(argv[1], "ns_addr_t2mrcl_addr") == 0)
00069                 {
00070                         int ip = atoi(argv[2]);
00071                         char addr[20];
00072                         sprintf(addr,"%d.%d.%d.%d", (ip & 0xff000000)>>24,(ip & 0x00ff0000)>>16, (ip & 0x0000ff00)>>8, (ip & 0x000000ff));
00073                         tcl.resultf("%s", addr);
00074                         return TCL_OK;
00075                 }
00076                 else if(strcmp(argv[1], "mrcl_addr2ns_addr_t") == 0)
00077                 {
00078                         IPAddress *addr = (IPAddress *)TclObject::lookup(argv[2]);
00079                         int level[4] = {0,0,0,0};
00080                         char tmp[20];
00081                         strncpy(tmp,addr->getAddr()+sizeof(int),19);
00082                         tmp[19] = '\0';
00083                         char *p = strtok(tmp, ".");
00084                         for(int i = 0; p && i < 4; p = strtok(NULL, "."), i++)
00085                         {
00086                                 level[i] = atoi(p);
00087                                 if(level[i] > 255)
00088                                         level[i] = 255;
00089                                 else if(level[i] < 0)
00090                                         level[i] = 0;
00091                         }
00092                         int ip = 0;
00093                         for(int i = 0; i < 4; i++)
00094                         {
00095                                 ip += (level[i] << 8 * (3 - i));
00096                         }
00097                         memcpy(&ip, addr->getAddr()+sizeof(int), sizeof(int));
00098                         tcl.resultf("%d", ip);
00099                         return TCL_OK;
00100                 }
00101         }
00102         return Module::command(argc, argv);
00103 }
00104 
00105 void IPAddressCodec::recv(Packet *p)
00106 {
00107         hdr_cmn *ch = HDR_CMN(p);
00108         if (ch->direction()==hdr_cmn::UP)
00109         {
00110                 sendUp(p, 0);
00111         }
00112         else
00113         {
00114                 if (up_==TRUE)
00115                 {
00116                         // convert old ip addresses to new one
00117                         struct hdr_ip *ih = HDR_IP(p);
00118                         RoutingHdr *rhdr = HDR_ROUTING(p);
00119                         int source = ih->saddr();
00120                         int addrLen = sizeof(int);
00121                         memcpy(rhdr->saddr(), &addrLen, sizeof(int));
00122                         memcpy(rhdr->saddr() + sizeof(int), &source, addrLen);
00123                         int dest = ih->daddr();
00124                         memcpy(rhdr->daddr(), &addrLen, sizeof(int));
00125                         memcpy(rhdr->daddr() + sizeof(int), &dest, addrLen);
00126 //                      char addr[20];
00127 //                      int ip;
00128 //                      memcpy(&ip, rhdr->saddr() + sizeof(int), sizeof(int));
00129 //                      sprintf(addr,"%d.%d.%d.%d", (ip & 0xff000000)>>24,(ip & 0x00ff0000)>>16, (ip & 0x0000ff00)>>8, (ip & 0x000000ff));
00130 //                      printf("[IPAddressCodecUP(down)]source address %s, old %d\n", addr, source);
00131 //                      memcpy(&ip, rhdr->daddr()+ sizeof(int), sizeof(int));
00132 //                      sprintf(addr,"%d.%d.%d.%d", (ip & 0xff000000)>>24,(ip & 0x00ff0000)>>16, (ip & 0x0000ff00)>>8, (ip & 0x000000ff));
00133 //                      printf("[IPAddressCodecUP]destination address %s, old %d\n", addr, dest);
00134                 }
00135                 else
00136                 {
00137                         // converto from IPAddress -> ns_addr_t
00138                         struct hdr_ip *ih = HDR_IP(p);
00139                         RoutingHdr *rhdr = HDR_ROUTING(p);
00140                         int temp;
00141                         memcpy(&temp, rhdr->saddr() + sizeof(int), sizeof(int));
00142                         ih->saddr() = temp;
00143                         memcpy(&temp, rhdr->daddr() + sizeof(int), sizeof(int));
00144                         ih->daddr() = temp;
00145 //                      char addr[20];
00146 //                      int ip;
00147 //                      memcpy(&ip, rhdr->saddr() + sizeof(int), sizeof(int));
00148 //                      sprintf(addr,"%d.%d.%d.%d", (ip & 0xff000000)>>24,(ip & 0x00ff0000)>>16, (ip & 0x0000ff00)>>8, (ip & 0x000000ff));
00149 //                      printf("[IPAddressCodecDOWN]source address %s, old %d\n", addr, ih->saddr());
00150 //                      memcpy(&ip, rhdr->daddr()+ sizeof(int), sizeof(int));
00151 //                      sprintf(addr,"%d.%d.%d.%d", (ip & 0xff000000)>>24,(ip & 0x00ff0000)>>16, (ip & 0x0000ff00)>>8, (ip & 0x000000ff));
00152 //                      printf("[IPAddressCodecDOWN(down)]destination address %s, old %d\n", addr, ih->daddr());
00153                 }
00154                 sendDown(p, 0);
00155         }
00156 }

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