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 #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
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
00127
00128
00129
00130
00131
00132
00133
00134 }
00135 else
00136 {
00137
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
00146
00147
00148
00149
00150
00151
00152
00153 }
00154 sendDown(p, 0);
00155 }
00156 }