src/Network/Address.cc
author terom
Tue, 16 Dec 2008 20:30:35 +0000
changeset 379 2a8e780844d2
parent 378 5589abf5e61b
child 380 d193dd1d8a7e
permissions -rw-r--r--
fix some NetworkAddress omissions
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
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
     6
NetworkAddress::NetworkAddress (void) :
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
     7
    hostname(), service(), address_length(0)
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
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    12
NetworkAddress::NetworkAddress (std::string service) :
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    13
    hostname(), service(service), address_length(0)
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
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    18
NetworkAddress::NetworkAddress (std::string hostname, std::string service) :
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    19
    hostname(hostname), service(service), address_length(0)
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
}
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    23
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    24
NetworkAddress::NetworkAddress (const sockaddr *addr, socklen_t len) :
379
2a8e780844d2 fix some NetworkAddress omissions
terom
parents: 378
diff changeset
    25
    hostname(), service(), address_length(0)
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    26
{
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    27
    // proxy to set_sockaddr
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    28
    set_sockaddr(addr, len);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    29
}
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    30
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    31
addrinfo* NetworkAddress::get_addrinfo (int family, int socktype, int protocol, int flags) const {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    32
    addrinfo hints, *results;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    33
    int err;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    34
    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    35
    // initialize flags
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    36
    hints.ai_flags = flags;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    37
    hints.ai_family = family;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    38
    hints.ai_socktype = socktype;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    39
    hints.ai_protocol = protocol;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    40
    hints.ai_addrlen = 0;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    41
    hints.ai_canonname = NULL;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    42
    hints.ai_next = NULL;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    43
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    44
    // 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
    45
    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
    46
    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
    47
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    48
    // do getaddrinfo()
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    49
    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
    50
        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
    51
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    52
    // done
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    53
    return results;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    54
}
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    55
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    56
const sockaddr* NetworkAddress::get_sockaddr (socklen_t &len_ref) const {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    57
    // we can't figure this out ourselves...
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    58
    if (address_length == 0)
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    59
        throw Error("NetworkAddress::get_sockaddr called without a valid sockaddr set");
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    60
    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    61
    // update len
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    62
    len_ref = address_length;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    63
    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    64
    // and return a pointer to our internal address
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    65
    return (const sockaddr*) &address;
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
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    68
void NetworkAddress::set_sockaddr (const sockaddr *addr, socklen_t len) {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    69
    char host_buf[NI_MAXHOST], serv_buf[NI_MAXSERV];
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    70
    int err;
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
    // invalid length?
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    73
    if (len > sizeof(this->address))
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    74
        throw NetworkAddressError(*this, "set_sockaddr", "invalid sockaddr length");
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    75
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    76
    // do getnameinfo()
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    77
    if ((err = getnameinfo(addr, len, host_buf, NI_MAXHOST, serv_buf, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV)))
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    78
        throw NetworkAddressError(*this, "getnameinfo", gai_strerror(err));
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    79
    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    80
    // copy over to address
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    81
    memcpy(&this->address, addr, len);
379
2a8e780844d2 fix some NetworkAddress omissions
terom
parents: 378
diff changeset
    82
2a8e780844d2 fix some NetworkAddress omissions
terom
parents: 378
diff changeset
    83
    // and the hostname + service
2a8e780844d2 fix some NetworkAddress omissions
terom
parents: 378
diff changeset
    84
    hostname = host_buf;
2a8e780844d2 fix some NetworkAddress omissions
terom
parents: 378
diff changeset
    85
    service = serv_buf;
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    86
}
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    87
187
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
    88
std::ostream& operator<< (std::ostream &s, const NetworkAddress &addr) {
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    89
    s << "[" << addr.get_hostname() << ":" << addr.get_service() << "]";
187
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
    90
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
    91
    return s;
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
    92
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
    93
}
f41f894213ca restructure network code a bit
terom
parents:
diff changeset
    94
 
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    95
std::string NetworkAddressError::build_str (const NetworkAddress &addr, const char *op, const char *msg) {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    96
    std::stringstream ss;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    97
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    98
    ss << op << ": " << addr << ": " << msg;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
    99
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
   100
    return ss.str();
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
   101
}
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
   102
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
   103
NetworkAddressError::NetworkAddressError (const NetworkAddress &addr, const char *op, const char *msg) :
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
   104
    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
   105
{
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
   106
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
   107
}
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
   108
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
   109
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 187
diff changeset
   110