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 <assert.h>
00031 #include "mrcl-address.h"
00032
00033 packet_t PT_MRCL_ROUTING;
00034
00035 MrclAddress::MrclAddress()
00036 {
00037 }
00038
00039 MrclAddress::MrclAddress(char *addr)
00040 {
00041 memcpy(addr_, addr, MRCL_ADDRESS_MAX_LEN);
00042 }
00043
00044 MrclAddress::~MrclAddress()
00045 {
00046 }
00047
00048 void MrclAddress::setAddr(void *addr, int addrLen)
00049 {
00050 assert(addrLen <= MRCL_ADDRESS_MAX_LEN - sizeof(int));
00051 memset(addr_, 0, MRCL_ADDRESS_MAX_LEN);
00052 memcpy(addr_, &addrLen, sizeof(int));
00053 memcpy(addr_ + sizeof(int), addr, addrLen);
00054 }
00055
00056 void MrclAddress::write(void *addr, int addrLen)
00057 {
00058 int len;
00059 memcpy(&len, addr_, sizeof(int));
00060 memcpy(addr, addr_, addrLen > len + sizeof(int) ? len + sizeof(int): addrLen);
00061 }
00062
00063 void MrclAddress::getAddr(void *addr, int addrLen, int offset)
00064 {
00065 assert(offset + addrLen <= MRCL_ADDRESS_MAX_LEN - sizeof(int));
00066 memcpy(addr, addr_ + sizeof(int) + offset, addrLen);
00067
00068 }
00069
00070 bool MrclAddress::operator==(const MrclAddress &a)
00071 {
00072 return (memcmp(addr_, a.addr_, MRCL_ADDRESS_MAX_LEN) == 0);
00073 }
00074
00075 int MrclAddress::isEqual(char *addr)
00076 {
00077 return (memcmp(addr_, addr, MRCL_ADDRESS_MAX_LEN) == 0);
00078 }
00079
00080 int MrclAddress::areEqual(char *addr1, char *addr2)
00081 {
00082 return (memcmp(addr1, addr2, MRCL_ADDRESS_MAX_LEN) == 0);
00083 }
00084
00085 void MrclAddress::storeAddr(char *dst, char *addr)
00086 {
00087 memcpy(dst, addr, MRCL_ADDRESS_MAX_LEN);
00088 }
00089
00090 int MrclAddress::command(int argc, const char *const *argv)
00091 {
00092 Tcl& tcl = Tcl::instance();
00093 if(argc == 2)
00094 {
00095 if(strcasecmp(argv[1],"addr") == 0)
00096 {
00097 int len = strlen() + 2;
00098 char *str = new char[len];
00099 toString(str, len);
00100 tcl.resultf("%s",str);
00101 delete [] str;
00102 return TCL_OK;
00103 }
00104 }
00105 else if(argc == 3)
00106 {
00107 if(strcasecmp(argv[1],"addr") == 0)
00108 {
00109 setAddress(argv[2]);
00110 return TCL_OK;
00111 }
00112 }
00113 return TclObject::command(argc, argv);
00114 }
00115
00116 char *MrclAddress::getAddr()
00117 {
00118 return addr_;
00119 }
00120