EthernetScheme.cc

Go to the documentation of this file.
00001  /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #ifdef __linux__
00019 
00020 #include <oasys/io/IPSocket.h>
00021 #include <oasys/io/NetUtils.h>
00022 #include <oasys/util/URL.h>
00023 
00024 #include "EthernetScheme.h"
00025 #include "EndpointID.h"
00026 
00027 namespace dtn {
00028 
00029 template <>
00030 EthernetScheme* oasys::Singleton<EthernetScheme>::instance_ = 0;
00031 
00032 /*
00033  * Parse out an ethernet address from the ssp.
00034  *
00035  * @return true if the string is a valid ethernet address, false if not.
00036  */
00037 bool
00038 EthernetScheme::parse(const std::string& ssp, eth_addr_t* addr)
00039 {
00040     // XXX/jakob - for now, assume it's a correctly formatted ethernet URI
00041     // eth://00:01:02:03:04:05 or eth:00:01:02:03:04:05
00042 
00043     const char* str = ssp.c_str();
00044     if (str[0] == '/' && str[1] == '/') {
00045         str = str + 2;
00046     }
00047     
00048     // this is a nasty hack. grab the ethernet address out of there,
00049     // assuming everything is correct
00050     int r = sscanf(str, "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx",
00051                    &addr->octet[0],
00052                    &addr->octet[1],
00053                    &addr->octet[2],
00054                    &addr->octet[3],
00055                    &addr->octet[4],
00056                    &addr->octet[5]);
00057     if (r != 6) {
00058         return false;
00059     }
00060 
00061     return true;
00062 }
00063 
00071 bool
00072 EthernetScheme::validate(const std::string& ssp, bool is_pattern)
00073 {
00074     (void)is_pattern;
00075     
00076     // make sure it's a valid url
00077     eth_addr_t addr;
00078     return parse(ssp, &addr);
00079 }
00080 
00086 bool
00087 EthernetScheme::match(const EndpointIDPattern& pattern,
00088                       const EndpointID& eid)
00089 {
00090     // sanity check
00091     ASSERT(pattern.scheme() == this);
00092     
00093     log_debug_p("/dtn/scheme/ethernet",
00094                 "matching %s against %s.", pattern.ssp().c_str(), eid.ssp().c_str());
00095 
00096     size_t patternlen = pattern.ssp().length();
00097     
00098     if (pattern.ssp() == eid.ssp()) 
00099         return true;
00100     
00101     if (patternlen >= 1 && pattern.ssp()[patternlen-1] == '*') {
00102         patternlen--;
00103         
00104         if (pattern.ssp().substr(0, patternlen) ==
00105             eid.ssp().substr(0, patternlen))
00106         {
00107             return true;
00108         }
00109     }
00110 
00111     return false;
00112 }
00113 
00114 char* 
00115 EthernetScheme::to_string(u_int8_t* addr, char* outstring)
00116 {
00117     sprintf(outstring,"eth://%02X:%02X:%02X:%02X:%02X:%02X", 
00118             addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
00119     
00120     return outstring;
00121 }
00122 
00123 
00124 } // namespace dtn
00125 
00126 #endif /* __linux__ */

Generated on Thu Jun 7 12:54:27 2007 for DTN Reference Implementation by  doxygen 1.5.1