src/Network/Address.hh
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
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 <netdb.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    13
#else
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    14
    #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
    15
#endif
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    16
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    17
#include <string>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    18
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    19
// assume...
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    20
#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
    21
    #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
    22
#endif
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    23
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
 * 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
    26
 */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    27
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
    28
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    29
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    30
 * We use ClanLib's IPAddress API, but with our own name
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    31
 */ 
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    32
class NetworkEndpoint {
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    33
    protected:
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    34
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    35
         * Our human-readable hostname
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    36
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    37
        std::string hostname;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    38
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
         * Our human-readable service
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    41
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    42
        std::string service;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    43
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    44
    public:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    45
        /**
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    46
         * Construct an empty NetworkEndpoint
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    47
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    48
        NetworkEndpoint (void);
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    49
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    50
        /**
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    51
         * Construct a NetworkEndpoint with a NULL hostname, and a specific service
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    52
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    53
        explicit NetworkEndpoint (std::string service);
378
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
        /**
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    56
         * Construct a NetworkEndpoint on a specific hostname and service
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    57
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    58
        NetworkEndpoint (std::string hostname, std::string service);
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    59
        
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    60
        /*
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    61
         * We can use the default copy-constructor and assignment operator
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    62
         */
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    63
   
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    64
    public:    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    65
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    66
         * 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
    67
         *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    68
         * 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
    69
         *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    70
         * @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
    71
         * @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
    72
         * @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
    73
         * @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
    74
         * @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
    75
         * @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
    76
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    77
        virtual addrinfo* get_addrinfo (int family, int socktype, int protocol = 0, int flags = 0) const;
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    78
        
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    79
        /**
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    80
         * Free an addrinfo returned by get_addrinfo
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    81
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    82
        virtual void free_addrinfo (addrinfo *info) const;
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    83
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
         * 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
    86
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    87
        virtual std::string get_hostname (void) const { return hostname; }
378
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
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    90
         * 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
    91
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    92
        virtual std::string get_service (void) const { return service; }
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    93
};
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    94
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    95
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    96
 * Formatted as  [<addr>:<port>]
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    97
 */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    98
std::ostream& operator<< (std::ostream &s, const NetworkEndpoint &addr);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    99
378
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
 *
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
class NetworkAddressError : public Error {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   104
    protected:
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   105
        static std::string 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: 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
    public:
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   108
        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: 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
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   111
#endif /* NETWORK_ADDRESS_HH */