servlib/conv_layers/IPConvergenceLayerUtils.cc
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 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 <oasys/io/NetUtils.h>
       
    22 #include "IPConvergenceLayerUtils.h"
       
    23 
       
    24 namespace dtn {
       
    25 
       
    26 /**
       
    27  * Parse a next hop address specification of the form
       
    28  * HOST[:PORT?].
       
    29  *
       
    30  * @return true if the conversion was successful, false
       
    31  */
       
    32 bool
       
    33 IPConvergenceLayerUtils::parse_nexthop(const char* logpath, const char* nexthop,
       
    34                                        in_addr_t* addr, u_int16_t* port)
       
    35 {
       
    36     const char* host;
       
    37     std::string tmp;
       
    38                         
       
    39     *addr = INADDR_NONE;
       
    40     *port = 0;
       
    41 
       
    42     // first see if there's a colon in the string -- if so, it should
       
    43     // separate the hostname and port, if not, then the whole string
       
    44     // should be a hostname.
       
    45     //
       
    46     // note that it is invalid to have more than one colon in the
       
    47     // string, as caught by the check after calling strtoul
       
    48     const char* colon = strchr(nexthop, ':');
       
    49     if (colon != NULL) {
       
    50         char* endstr;
       
    51         u_int32_t portval = strtoul(colon + 1, &endstr, 10);
       
    52         
       
    53         if (*endstr != '\0' || portval > 65535) {
       
    54             log_warn_p(logpath, "invalid port %s in next hop '%s'",
       
    55                        colon + 1, nexthop);
       
    56             return false;
       
    57         }
       
    58 
       
    59         *port = (u_int16_t)portval;
       
    60         
       
    61         tmp.assign(nexthop, colon - nexthop);
       
    62         host = tmp.c_str();
       
    63     } else {
       
    64         host = nexthop;
       
    65     }
       
    66 
       
    67     // now look up the hostname
       
    68     if (oasys::gethostbyname(host, addr) != 0) {
       
    69         log_warn_p(logpath, "invalid hostname '%s' in next hop %s",
       
    70                    host, nexthop);
       
    71         return false;
       
    72     }
       
    73     
       
    74     return true;
       
    75 }
       
    76     
       
    77 
       
    78 } // namespace dtn