src/Network/Address.cc
author terom
Wed, 17 Dec 2008 00:40:22 +0000
changeset 381 9b35bc329d23
parent 380 d193dd1d8a7e
permissions -rw-r--r--
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
187
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
     1
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
     2
#include "Address.hh"
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
     3
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
     4
#include <sstream>
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
     5
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
     6
NetworkEndpoint::NetworkEndpoint (void) :
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
     7
    hostname(), service()
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
     8
{
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
     9
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    10
}
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    11
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    12
NetworkEndpoint::NetworkEndpoint (std::string service) :
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    13
    hostname(), service(service)
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    14
{
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    15
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    16
}
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    17
        
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    18
NetworkEndpoint::NetworkEndpoint (std::string hostname, std::string service) :
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    19
    hostname(hostname), service(service)
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    20
{
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    21
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    22
}
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    23
       
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    24
addrinfo* NetworkEndpoint::get_addrinfo (int family, int socktype, int protocol, int flags) const {
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    25
    addrinfo hints, *results;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    26
    int err;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    27
    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    28
    // initialize flags
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    29
    hints.ai_flags = flags;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    30
    hints.ai_family = family;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    31
    hints.ai_socktype = socktype;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    32
    hints.ai_protocol = protocol;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    33
    hints.ai_addrlen = 0;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    34
    hints.ai_canonname = NULL;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    35
    hints.ai_next = NULL;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    36
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    37
    // hostname + service may be NULL
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    38
    const char *hostname = this->hostname.empty() ? NULL : this->hostname.c_str();
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    39
    const char *service = this->service.empty() ? NULL : this->service.c_str();
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    40
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    41
    // do getaddrinfo()
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    42
    if ((err = getaddrinfo(hostname, service, &hints, &results)))
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    43
        throw NetworkAddressError(*this, "getaddrinfo", gai_strerror(err));
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    44
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    45
    // done
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    46
    return results;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    47
}
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    48
        
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    49
void NetworkEndpoint::free_addrinfo (addrinfo *info) const {
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    50
    freeaddrinfo(info);
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    51
}
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    52
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    53
std::ostream& operator<< (std::ostream &s, const NetworkEndpoint &addr) {
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    54
    s << "[" << addr.get_hostname() << ":" << addr.get_service() << "]";
187
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
    55
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
    56
    return s;
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
    57
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
    58
}
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
    59
 
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    60
std::string NetworkAddressError::build_str (const NetworkEndpoint &addr, const char *op, const char *msg) {
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    61
    std::stringstream ss;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    62
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    63
    ss << op << ": " << addr << ": " << msg;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    64
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    65
    return ss.str();
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    66
}
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    67
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    68
NetworkAddressError::NetworkAddressError (const NetworkEndpoint &addr, const char *op, const char *msg) :
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    69
    Error(build_str(addr, op, msg))
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    70
{
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    71
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    72
}
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    73