src/Network/Address.hh
author terom
Tue, 16 Dec 2008 23:21:26 +0000
changeset 380 d193dd1d8a7e
parent 378 5589abf5e61b
child 381 9b35bc329d23
permissions -rw-r--r--
new NetworkReactor, fix everything to actually work
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);
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    71
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    72
        /*
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    73
         * We can use the default copy-constructor and assignment operator
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    74
         */
378
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
    public:    
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
         * 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
    79
         *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    80
         * 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
    81
         *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    82
         * @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
    83
         * @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
    84
         * @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
    85
         * @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
    86
         * @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
    87
         * @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
    88
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    89
        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
    90
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    91
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    92
         * 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
    93
         * on them.
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    94
         *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    95
         * @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
    96
         * @return borrowed sockaddr pointer
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    97
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    98
        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
    99
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   100
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   101
         * 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
   102
         *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   103
         * @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
   104
         * @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
   105
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   106
        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
   107
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
         * 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
   110
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   111
        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
   112
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
         * 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
   115
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   116
        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
   117
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   118
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   119
         * 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
   120
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   121
        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
   122
            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
   123
        }
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   124
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   125
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   126
         * 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
   127
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   128
        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
   129
            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
   130
        }
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   131
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   132
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   133
         * 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
   134
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   135
        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
   136
            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
   137
        }
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   138
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   139
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   140
         * 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
   141
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   142
        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
   143
            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
   144
        }
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   145
};
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   146
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   147
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   148
 * Formatted as  [<addr>:<port>]
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   149
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   150
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
   151
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   152
/**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   153
 *
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
class NetworkAddressError : public Error {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   156
    protected:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   157
        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
   158
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   159
    public:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   160
        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
   161
};
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   162
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   163
#endif /* NETWORK_ADDRESS_HH */