servlib/naming/DTNScheme.cc
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2004-2006 Intel Corporation
       
     3  * 
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  * 
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  * 
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 #ifdef HAVE_CONFIG_H
       
    18 #  include <dtn-config.h>
       
    19 #endif
       
    20 
       
    21 #include <ctype.h>
       
    22 #include <oasys/debug/Log.h>
       
    23 #include <oasys/util/Glob.h>
       
    24 
       
    25 #include "DTNScheme.h"
       
    26 #include "EndpointID.h"
       
    27 
       
    28 namespace dtn {
       
    29 
       
    30 template <>
       
    31 DTNScheme* oasys::Singleton<DTNScheme>::instance_ = 0;
       
    32 
       
    33 //----------------------------------------------------------------------
       
    34 bool
       
    35 DTNScheme::validate(const URI& uri, bool is_pattern)
       
    36 {
       
    37     (void)is_pattern;
       
    38 
       
    39     if (!uri.valid()) {
       
    40         log_debug_p("/dtn/scheme/dtn", "DTNScheme::validate: invalid URI");
       
    41         return false;
       
    42     }
       
    43 
       
    44     // a valid dtn scheme uri must have a host component in the
       
    45     // authority unless it's the special "dtn:none" uri
       
    46     if (uri.host().length() == 0 && uri.ssp() != "none") {
       
    47         return false;
       
    48     }
       
    49 
       
    50     return true;
       
    51 }
       
    52 
       
    53 //----------------------------------------------------------------------
       
    54 bool
       
    55 DTNScheme::match(const EndpointIDPattern& pattern, const EndpointID& eid)
       
    56 {
       
    57     // sanity check
       
    58     ASSERT(pattern.scheme() == this);
       
    59 
       
    60     // we only match endpoint ids of the same scheme
       
    61     if (!eid.known_scheme() || (eid.scheme() != this)) {
       
    62         return false;
       
    63     }
       
    64     
       
    65     // if the ssp of either string is "none", then nothing should
       
    66     // match it (ever)
       
    67     if (pattern.ssp() == "none" || eid.ssp() == "none") {
       
    68         return false;
       
    69     }
       
    70     
       
    71     // check for a wildcard host specifier e.g dtn://*
       
    72     if (pattern.uri().host() == "*" && pattern.uri().path() == "")
       
    73     {
       
    74         return true;
       
    75     }
       
    76     
       
    77     // use glob rules to match the hostname part -- note that we don't
       
    78     // distinguish between which one has the glob patterns so we run
       
    79     // glob in both directions looking for a match
       
    80     if (! (oasys::Glob::fixed_glob(pattern.uri().host().c_str(),
       
    81                                    eid.uri().host().c_str()) ||
       
    82            oasys::Glob::fixed_glob(eid.uri().host().c_str(),
       
    83                                    pattern.uri().host().c_str())) )
       
    84     {
       
    85         log_debug_p("/dtn/scheme/dtn",
       
    86                     "match(%s, %s) failed: uri hosts don't glob ('%s' != '%s')",
       
    87                     eid.uri().c_str(), pattern.uri().c_str(),
       
    88                     pattern.uri().host().c_str(), eid.uri().host().c_str());
       
    89         return false;
       
    90     }
       
    91 
       
    92     // make sure the ports are equal (or unspecified in which case they're 0)
       
    93     if (pattern.uri().port_num() != eid.uri().port_num())
       
    94     {
       
    95         log_debug_p("/dtn/scheme/dtn",
       
    96                     "match(%s, %s) failed: uri ports not equal (%d != %d)",
       
    97                     eid.uri().c_str(), pattern.uri().c_str(),
       
    98                     pattern.uri().port_num(), eid.uri().port_num());
       
    99         return false;
       
   100     }
       
   101 
       
   102     // XXX/demmer I don't understand why this is needed...
       
   103     std::string pattern_path(pattern.uri().path());
       
   104     if ((pattern_path.length() >= 2) &&
       
   105         (pattern_path.substr((pattern_path.length() - 2), 2) == "/*")) {
       
   106         pattern_path.replace((pattern_path.length() - 2), 2, 1, '*');
       
   107     }
       
   108 
       
   109     // check for a glob match of the path strings
       
   110     if (! (oasys::Glob::fixed_glob(pattern_path.c_str(),
       
   111                                    eid.uri().path().c_str()) ||
       
   112            oasys::Glob::fixed_glob(eid.uri().path().c_str(),
       
   113                                    pattern_path.c_str())) )
       
   114     {
       
   115         log_debug_p("/dtn/scheme/dtn",
       
   116                     "match(%s, %s) failed: paths don't glob ('%s' != '%s')",
       
   117                     eid.uri().c_str(), pattern.uri().c_str(),
       
   118                     pattern.uri().path().c_str(), eid.uri().path().c_str());
       
   119         return false;
       
   120     }
       
   121 
       
   122     // XXX/demmer: maybe if the pattern has any query parameters, then
       
   123     // they should match or glob to the equivalent ones in the eid.
       
   124     // parameters present in the eid but not in the pattern should be
       
   125     // ignored
       
   126     
       
   127     // ignore the query parameter strings, so they still match the glob'd routes
       
   128     return true;
       
   129 }
       
   130 
       
   131 //----------------------------------------------------------------------
       
   132 bool
       
   133 DTNScheme::append_service_tag(URI* uri, const char* tag)
       
   134 {
       
   135     if (tag[0] != '/') {
       
   136         uri->set_path(std::string("/") + tag);
       
   137     } else {
       
   138         uri->set_path(tag);
       
   139     }
       
   140     return true;
       
   141 }
       
   142 
       
   143 //----------------------------------------------------------------------
       
   144 bool
       
   145 DTNScheme::append_service_wildcard(URI* uri)
       
   146 {
       
   147     if (uri == NULL) return false;
       
   148 
       
   149     // only append wildcard if path is empty
       
   150     if (! uri->path().empty())
       
   151         return false;
       
   152 
       
   153     uri->set_path("/*");
       
   154     return true;
       
   155 }
       
   156 
       
   157 //----------------------------------------------------------------------
       
   158 bool
       
   159 DTNScheme::remove_service_tag(URI* uri)
       
   160 {
       
   161     if (uri == NULL) return false;
       
   162     uri->set_path("");
       
   163     return true;
       
   164 }
       
   165 
       
   166 //----------------------------------------------------------------------
       
   167 Scheme::singleton_info_t
       
   168 DTNScheme::is_singleton(const URI& uri)
       
   169 {
       
   170     // if there's a * in the hostname part of the URI, then it's not a
       
   171     // singleton endpoint
       
   172     if (uri.host().find('*') != std::string::npos) {
       
   173         log_debug_p("/dtn/scheme/dtn",
       
   174                     "URI host %s contains a wildcard, so is MULTINODE",
       
   175                     uri.host().c_str());
       
   176         return EndpointID::MULTINODE;
       
   177     }
       
   178     
       
   179     log_debug_p("/dtn/scheme/dtn",
       
   180                 "URI host %s does not contain a wildcard, so is SINGLETON",
       
   181                 uri.host().c_str());
       
   182     return EndpointID::SINGLETON;
       
   183 }
       
   184 
       
   185 } // namespace dtn