EthernetScheme.cc

Go to the documentation of this file.
00001  /*
00002   * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003   * downloading, copying, installing or using the software you agree to
00004   * this license. If you do not agree to this license, do not download,
00005   * install, copy or use the software.
00006   * 
00007   * Intel Open Source License 
00008   * 
00009   * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010   * 
00011   * Redistribution and use in source and binary forms, with or without
00012   * modification, are permitted provided that the following conditions are
00013   * met:
00014   * 
00015   *   Redistributions of source code must retain the above copyright
00016   *   notice, this list of conditions and the following disclaimer.
00017   * 
00018   *   Redistributions in binary form must reproduce the above copyright
00019   *   notice, this list of conditions and the following disclaimer in the
00020   *   documentation and/or other materials provided with the distribution.
00021   * 
00022   *   Neither the name of the Intel Corporation nor the names of its
00023   *   contributors may be used to endorse or promote products derived from
00024   *   this software without specific prior written permission.
00025   *  
00026   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027   * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037   */
00038 
00039 #ifdef __linux__
00040 
00041 #include <oasys/io/IPSocket.h>
00042 #include <oasys/io/NetUtils.h>
00043 #include <oasys/util/URL.h>
00044 
00045 #include "EthernetScheme.h"
00046 #include "EndpointID.h"
00047 
00048 namespace dtn {
00049 
00050 template <>
00051 EthernetScheme* oasys::Singleton<EthernetScheme>::instance_ = 0;
00052 
00053 /*
00054  * Parse out an ethernet address from the ssp.
00055  *
00056  * @return true if the string is a valid ethernet address, false if not.
00057  */
00058 bool
00059 EthernetScheme::parse(const std::string& ssp, eth_addr_t* addr)
00060 {
00061     // XXX/jakob - for now, assume it's a correctly formatted ethernet URI
00062     // eth://00:01:02:03:04:05 or eth:00:01:02:03:04:05
00063 
00064     const char* str = ssp.c_str();
00065     if (str[0] == '/' && str[1] == '/') {
00066         str = str + 2;
00067     }
00068     
00069     // this is a nasty hack. grab the ethernet address out of there,
00070     // assuming everything is correct
00071     int r = sscanf(str, "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx",
00072                    &addr->octet[0],
00073                    &addr->octet[1],
00074                    &addr->octet[2],
00075                    &addr->octet[3],
00076                    &addr->octet[4],
00077                    &addr->octet[5]);
00078     if (r != 6) {
00079         return false;
00080     }
00081 
00082     return true;
00083 }
00084 
00092 bool
00093 EthernetScheme::validate(const std::string& ssp, bool is_pattern)
00094 {
00095     (void)is_pattern;
00096     
00097     // make sure it's a valid url
00098     eth_addr_t addr;
00099     return parse(ssp, &addr);
00100 }
00101 
00107 bool
00108 EthernetScheme::match(const EndpointIDPattern& pattern,
00109                       const EndpointID& eid)
00110 {
00111     // sanity check
00112     ASSERT(pattern.scheme() == this);
00113     
00114     log_debug("/dtn/scheme/ethernet",
00115               "matching %s against %s.", pattern.ssp().c_str(), eid.ssp().c_str());
00116 
00117     size_t patternlen = pattern.ssp().length();
00118     
00119     if (pattern.ssp() == eid.ssp()) 
00120         return true;
00121     
00122     if (patternlen >= 1 && pattern.ssp()[patternlen-1] == '*') {
00123         patternlen--;
00124         
00125         if (pattern.ssp().substr(0, patternlen) ==
00126             eid.ssp().substr(0, patternlen))
00127         {
00128             return true;
00129         }
00130     }
00131 
00132     return false;
00133 }
00134 
00135 char* 
00136 EthernetScheme::to_string(u_int8_t* addr, char* outstring)
00137 {
00138     sprintf(outstring,"eth://%02X:%02X:%02X:%02X:%02X:%02X", 
00139             addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
00140     
00141     return outstring;
00142 }
00143 
00144 
00145 } // namespace dtn
00146 
00147 #endif /* __linux__ */

Generated on Fri Dec 22 14:47:59 2006 for DTN Reference Implementation by  doxygen 1.5.1