src/Network/Address.hh
author terom
Mon, 15 Dec 2008 23:56:42 +0000
changeset 378 5589abf5e61b
parent 284 27ce69fd1e06
child 380 d193dd1d8a7e
permissions -rw-r--r--
break the network code. Too late to set up a branch for this now
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     1
#ifndef NETWORK_ADDRESS_HH
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     2
#define NETWORK_ADDRESS_HH
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     3
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
     4
#include "../Error.hh"
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
     5
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
     6
/*
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
     7
 * Platform-specific includes
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
     8
 */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
     9
#ifndef WIN32
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    10
    // linux
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    11
    #include <sys/types.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    12
    #include <sys/socket.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    13
    #include <netdb.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    14
#else
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    15
    #error "This network code won't compile on win32 :)"
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    16
#endif
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    17
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    18
#include <string>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    19
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    20
// assume...
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    21
#if INET6_ADDRSTRLEN < INET_ADDRSTRLEN
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    22
    #error INET6_ADDRSTRLEN is smaller than INET_ADDRSTRLEN
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    23
#endif
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    24
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    25
/**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    26
 * Length of a network address
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    27
 */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    28
const socklen_t NETWORK_ADDRESS_LENGTH = INET6_ADDRSTRLEN;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    29
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    30
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    31
 * We use ClanLib's IPAddress API, but with our own name
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    32
 */ 
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    33
class NetworkAddress {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    34
    private:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    35
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    36
         * Our human-readable hostname
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    37
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    38
        std::string hostname;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    39
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    40
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    41
         * Our human-readable service
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    42
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    43
        std::string service;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    44
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    45
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    46
         * Our machine-readable address and its length
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    47
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    48
        sockaddr_storage address;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    49
        socklen_t address_length;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    50
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    51
    public:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    52
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    53
         * Construct an empty NetworkAddress
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    54
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    55
        NetworkAddress (void);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    56
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    57
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    58
         * Construct a NetworkAddress with a NULL hostname, and a specific service
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    59
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    60
        NetworkAddress (std::string service);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    61
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    62
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    63
         * Construct a NetworkAddress on a specific hostname and service
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    64
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    65
        NetworkAddress (std::string hostname, std::string service);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    66
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    67
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    68
         * Construct a NetworkAddress from a machine-readable address of the given length
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    69
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    70
        NetworkAddress (const sockaddr *addr, socklen_t len);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    71
   
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    72
    public:    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    73
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    74
         * Get a addrinfo* for this address using the given family/type/protocol/flags.
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    75
         *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    76
         * Remember to free the returned pointer using freeaddrinfo() after use.
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    77
         *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    78
         * @param family the socket family for hints.ai_family
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    79
         * @param socktype the socket type for hints.ai_socktype
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    80
         * @param protoocl the socket protocol for hints.ai_protocol
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    81
         * @param flags the flags for hints.ai_flags
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    82
         * @return linked list of addrinfo's
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    83
         * @throw NetworkAddressError if resolving this address fails
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    84
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    85
        addrinfo* get_addrinfo (int family, int socktype, int protocol = 0, int flags = 0) const;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    86
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    87
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    88
         * Get a sockaddr* for this address. This is only valid for NetworkAddress's which have had set_sockaddr called
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    89
         * on them.
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    90
         *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    91
         * @param len_ref updated to the sockaddr length
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    92
         * @return borrowed sockaddr pointer
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    93
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    94
        const sockaddr* get_sockaddr (socklen_t &len_ref) const;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    95
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    96
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    97
         * Set a sockaddr for this address
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    98
         *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    99
         * @param addr the address to copy, of len bytes
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   100
         * @param len the size of the sockaddr
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   101
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   102
        void 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: 284
diff changeset
   103
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   104
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   105
         * Get the human-readable hostname
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   106
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   107
        std::string get_hostname (void) const { return hostname; }
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   108
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   109
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   110
         * Get the human-readable service name
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   111
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   112
        std::string get_service (void) const { return service; }
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   113
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   114
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   115
         * Equal-to comparison operator. This requires that the machine-readable address be available.
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   116
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   117
        bool operator== (const NetworkAddress &other) const {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   118
            return (address_length == other.address_length) && memcmp(&address, &other.address, address_length) == 0;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   119
        }
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   120
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   121
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   122
         * Not-qqual-to comparison operator. This requires that the machine-readable address be available.
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   123
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   124
        bool operator!= (const NetworkAddress &other) const {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   125
            return (address_length != other.address_length) || memcmp(&address, &other.address, address_length) != 0;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   126
        }
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   127
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   128
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   129
         * Less-than comparison operator. Smaller addresses are always lesser.
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   130
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   131
        bool operator< (const NetworkAddress &other) const {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   132
            return (address_length < other.address_length) || memcmp(&address, &other.address, other.address_length) < 0;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   133
        }
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   134
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   135
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   136
         * Greater-than comparison operator. Bigger addresses are always greater.
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   137
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   138
        bool operator> (const NetworkAddress &other) const {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   139
            return (address_length > other.address_length) || memcmp(&address, &other.address, address_length) > 0;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   140
        }
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   141
};
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   142
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   143
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   144
 * Formatted as  [<addr>:<port>]
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   145
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   146
std::ostream& operator<< (std::ostream &s, const NetworkAddress &addr);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   147
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   148
/**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   149
 *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   150
 */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   151
class NetworkAddressError : public Error {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   152
    protected:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   153
        static std::string 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: 284
diff changeset
   154
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   155
    public:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   156
        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: 284
diff changeset
   157
};
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   158
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   159
#endif /* NETWORK_ADDRESS_HH */