00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 # include <dtn-config.h>
00019 #endif
00020
00021 #include <ctype.h>
00022 #include <oasys/debug/Log.h>
00023 #include <oasys/util/Glob.h>
00024
00025 #include "DTNScheme.h"
00026 #include "EndpointID.h"
00027
00028 namespace dtn {
00029
00030 template <>
00031 DTNScheme* oasys::Singleton<DTNScheme>::instance_ = 0;
00032
00033
00034 bool
00035 DTNScheme::validate(const URI& uri, bool is_pattern)
00036 {
00037 (void)is_pattern;
00038
00039 if (!uri.valid()) {
00040 log_debug_p("/dtn/scheme/dtn", "DTNScheme::validate: invalid URI");
00041 return false;
00042 }
00043
00044
00045
00046 if (uri.host().length() == 0 && uri.ssp() != "none") {
00047 return false;
00048 }
00049
00050 return true;
00051 }
00052
00053
00054 bool
00055 DTNScheme::match(const EndpointIDPattern& pattern, const EndpointID& eid)
00056 {
00057
00058 ASSERT(pattern.scheme() == this);
00059
00060
00061 if (!eid.known_scheme() || (eid.scheme() != this)) {
00062 return false;
00063 }
00064
00065
00066
00067 if (pattern.ssp() == "none" || eid.ssp() == "none") {
00068 return false;
00069 }
00070
00071
00072 if (pattern.uri().host() == "*" && pattern.uri().path() == "")
00073 {
00074 return true;
00075 }
00076
00077
00078
00079
00080 if (! (oasys::Glob::fixed_glob(pattern.uri().host().c_str(),
00081 eid.uri().host().c_str()) ||
00082 oasys::Glob::fixed_glob(eid.uri().host().c_str(),
00083 pattern.uri().host().c_str())) )
00084 {
00085 log_debug_p("/dtn/scheme/dtn",
00086 "match(%s, %s) failed: uri hosts don't glob ('%s' != '%s')",
00087 eid.uri().c_str(), pattern.uri().c_str(),
00088 pattern.uri().host().c_str(), eid.uri().host().c_str());
00089 return false;
00090 }
00091
00092
00093 if (pattern.uri().port_num() != eid.uri().port_num())
00094 {
00095 log_debug_p("/dtn/scheme/dtn",
00096 "match(%s, %s) failed: uri ports not equal (%d != %d)",
00097 eid.uri().c_str(), pattern.uri().c_str(),
00098 pattern.uri().port_num(), eid.uri().port_num());
00099 return false;
00100 }
00101
00102
00103 std::string pattern_path(pattern.uri().path());
00104 if ((pattern_path.length() >= 2) &&
00105 (pattern_path.substr((pattern_path.length() - 2), 2) == "/*")) {
00106 pattern_path.replace((pattern_path.length() - 2), 2, 1, '*');
00107 }
00108
00109
00110 if (! (oasys::Glob::fixed_glob(pattern_path.c_str(),
00111 eid.uri().path().c_str()) ||
00112 oasys::Glob::fixed_glob(eid.uri().path().c_str(),
00113 pattern_path.c_str())) )
00114 {
00115 log_debug_p("/dtn/scheme/dtn",
00116 "match(%s, %s) failed: paths don't glob ('%s' != '%s')",
00117 eid.uri().c_str(), pattern.uri().c_str(),
00118 pattern.uri().path().c_str(), eid.uri().path().c_str());
00119 return false;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 return true;
00129 }
00130
00131
00132 bool
00133 DTNScheme::append_service_tag(URI* uri, const char* tag)
00134 {
00135 if (tag[0] != '/') {
00136 uri->set_path(std::string("/") + tag);
00137 } else {
00138 uri->set_path(tag);
00139 }
00140 return true;
00141 }
00142
00143
00144 bool
00145 DTNScheme::append_service_wildcard(URI* uri)
00146 {
00147 if (uri == NULL) return false;
00148
00149
00150 if (! uri->path().empty())
00151 return false;
00152
00153 uri->set_path("/*");
00154 return true;
00155 }
00156
00157
00158 bool
00159 DTNScheme::remove_service_tag(URI* uri)
00160 {
00161 if (uri == NULL) return false;
00162 uri->set_path("");
00163 return true;
00164 }
00165
00166
00167 Scheme::singleton_info_t
00168 DTNScheme::is_singleton(const URI& uri)
00169 {
00170
00171
00172 if (uri.host().find('*') != std::string::npos) {
00173 log_debug_p("/dtn/scheme/dtn",
00174 "URI host %s contains a wildcard, so is MULTINODE",
00175 uri.host().c_str());
00176 return EndpointID::MULTINODE;
00177 }
00178
00179 log_debug_p("/dtn/scheme/dtn",
00180 "URI host %s does not contain a wildcard, so is SINGLETON",
00181 uri.host().c_str());
00182 return EndpointID::SINGLETON;
00183 }
00184
00185 }