mrcl-address.cc

00001 /*
00002  * Copyright (c) 2007 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 <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 

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