Changeset 10848 for trunk/FACT++


Ignore:
Timestamp:
05/27/11 11:39:10 (13 years ago)
Author:
tbretz
Message:
Changed GetLocalIP code to boost::asio for better readability.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/Dim.cc

    r10657 r10848  
    88#include "Dim.h"
    99
     10/*
    1011#include <netdb.h>
    1112#include <sys/types.h>
    1213#include <sys/socket.h>
    1314#include <arpa/inet.h>
     15*/
     16#include <boost/asio.hpp>
    1417
    1518#include <iostream>
     
    3134string Dim::GetLocalIp(const string &dns)
    3235{
     36    using namespace boost::asio;
     37    using namespace boost::asio::ip;
     38
     39    cout << "Trying to resolve local IP address..." << endl;
     40
     41    boost::system::error_code ec;
     42
     43    boost::asio::io_service io_service;
     44
     45    udp::socket socket(io_service);
     46
     47    udp::resolver resolver(io_service);
     48    udp::resolver::query query(dns, "0");
     49    udp::resolver::iterator iterator = resolver.resolve(query, ec);
     50    if (ec)
     51    {
     52        cout << "WARNING - Failure in name-resolution of '" << dns << ":0': ";
     53        cout << ec.message() << " (" << ec << ")" << endl;
     54        return dns;
     55    }
     56
     57    for (; iterator != udp::resolver::iterator(); ++iterator)
     58    {
     59        udp::endpoint endpoint = *iterator;
     60        socket.connect(endpoint, ec);
     61        if (ec)
     62        {
     63            cout << "WARNING - connect to '" << dns << ":0' failed: ";
     64            cout << ec.message() << " (" << ec << ")" << endl;
     65            continue;
     66        }
     67
     68        const string addr = socket.local_endpoint().address().to_v4().to_string();
     69        cout << "Setting DIM_HOST_NODE=" << addr << endl;
     70        return addr;
     71    }
     72
     73    return dns;
     74
     75/*
    3376    struct addrinfo hints, *servinfo, *p;
    3477
     
    86129
    87130    return dns;
     131*/
    88132}
    89133
Note: See TracChangeset for help on using the changeset viewer.