EndpointID.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) 2005 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 #include <ctype.h>
00040 
00041 #include "applib/dtn_types.h"
00042 #include "EndpointID.h"
00043 #include "Scheme.h"
00044 #include "SchemeTable.h"
00045 
00046 namespace dtn {
00047 
00053 bool
00054 EndpointID::parse()
00055 {
00056     size_t pos;
00057 
00058     scheme_ = NULL;
00059     scheme_str_.erase();
00060     ssp_.erase();
00061     valid_ = false;
00062     
00063     if ((pos = str_.find(':')) == std::string::npos)
00064         return false; // no :
00065 
00066     if (pos == 0) {
00067         return false; // empty scheme
00068     }
00069     
00070     scheme_str_.assign(str_, 0, pos);
00071 
00072     if (!is_pattern_) {
00073         // validate that scheme is composed of legitimate characters
00074         // according to the URI spec (RFC 3986)
00075         std::string::iterator iter = scheme_str_.begin();
00076         if (! isalpha(*iter)) {
00077             return false;
00078         }
00079         ++iter;
00080         for (; iter != scheme_str_.end(); ++iter) {
00081             char c = *iter;
00082             if (isalnum(c) || (c == '+') || (c == '-') || (c == '.'))
00083                 continue;
00084         
00085             return false; // invalid character
00086         }
00087     }
00088 
00089     // XXX/demmer should really validate the rest of it, but the URI
00090     // validation rules are actually a bit complex...
00091     scheme_ = SchemeTable::instance()->lookup(scheme_str_);
00092     
00093     if (pos == str_.length() - 1) {
00094         return false; // empty scheme or ssp
00095     }
00096     ssp_.assign(str_, pos + 1, str_.length() - pos);
00097 
00098 
00099     if (scheme_) {
00100         valid_ = scheme_->validate(ssp_, is_pattern_);
00101     } else {
00102         valid_ = true;
00103     }
00104     
00105     return valid_;
00106 }
00107 
00115 bool
00116 EndpointID::append_service_tag(const char* tag)
00117 {
00118     if (!scheme_)
00119         return false;
00120 
00121     bool ok = scheme_->append_service_tag(&ssp_, tag);
00122     if (!ok)
00123         return false;
00124 
00125     // rebuild the string
00126     str_ = scheme_str_ + ":" + ssp_;
00127     
00128     return true;
00129 }
00130 
00136 bool
00137 EndpointID::assign(const dtn_endpoint_id_t* eid)
00138 {
00139     str_.assign(eid->uri);
00140     return parse();
00141 }
00142     
00147 void
00148 EndpointID::copyto(dtn_endpoint_id_t* eid) const
00149 {
00150     ASSERT(str_.length() <= DTN_MAX_ENDPOINT_ID + 1);
00151     strcpy(eid->uri, str_.c_str());
00152 }
00153 
00154 
00158 void
00159 EndpointID::serialize(oasys::SerializeAction* a)
00160 {
00161     a->process("uri", &str_);
00162     if (a->action_code() == oasys::Serialize::UNMARSHAL) {
00163         parse();
00164         ASSERT(valid_);
00165     }
00166 }
00167 
00168 
00173 bool
00174 EndpointIDPattern::match(const EndpointID& eid) const
00175 {
00176     if (! known_scheme()) {
00177         return false;
00178     }
00179 
00180     return scheme()->match(*this, eid);
00181 }
00182 
00183 
00184 } // namespace dtn

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