IPConvergenceLayer.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 #include <oasys/io/NetUtils.h>
00019 #include "IPConvergenceLayer.h"
00020 
00021 namespace dtn {
00022 
00029 bool
00030 IPConvergenceLayer::parse_nexthop(const char* nexthop,
00031                                   in_addr_t* addr, u_int16_t* port)
00032 {
00033     const char* host;
00034     std::string tmp;
00035 
00036     *addr = INADDR_NONE;
00037     *port = 0;
00038 
00039     // first see if there's a colon in the string -- if so, it should
00040     // separate the hostname and port, if not, then the whole string
00041     // should be a hostname.
00042     //
00043     // note that it is invalid to have more than one colon in the
00044     // string, as caught by the check after calling strtoul
00045     const char* colon = strchr(nexthop, ':');
00046     if (colon != NULL) {
00047         char* endstr;
00048         u_int32_t portval = strtoul(colon + 1, &endstr, 10);
00049         
00050         if (*endstr != '\0' || portval > 65535) {
00051             log_warn("invalid port %s in next hop '%s'",
00052                      colon + 1, nexthop);
00053             return false;
00054         }
00055 
00056         *port = (u_int16_t)portval;
00057         
00058         tmp.assign(nexthop, colon - nexthop);
00059         host = tmp.c_str();
00060     } else {
00061         host = nexthop;
00062     }
00063 
00064     // now look up the hostname
00065     if (oasys::gethostbyname(host, addr) != 0) {
00066         log_warn("can't lookup hostname '%s' in next hop %s",
00067                  host, nexthop);
00068         return false;
00069     }
00070     
00071     return true;
00072 }
00073     
00074 
00075 } // namespace dtn

Generated on Thu Jun 7 16:56:50 2007 for DTN Reference Implementation by  doxygen 1.5.1