src/Network/Address.hh
changeset 381 9b35bc329d23
parent 380 d193dd1d8a7e
equal deleted inserted replaced
380:d193dd1d8a7e 381:9b35bc329d23
     7  * Platform-specific includes
     7  * Platform-specific includes
     8  */
     8  */
     9 #ifndef WIN32
     9 #ifndef WIN32
    10     // linux
    10     // linux
    11     #include <sys/types.h>
    11     #include <sys/types.h>
    12     #include <sys/socket.h>
       
    13     #include <netdb.h>
    12     #include <netdb.h>
    14 #else
    13 #else
    15     #error "This network code won't compile on win32 :)"
    14     #error "This network code won't compile on win32 :)"
    16 #endif
    15 #endif
    17 
    16 
    28 const socklen_t NETWORK_ADDRESS_LENGTH = INET6_ADDRSTRLEN;
    27 const socklen_t NETWORK_ADDRESS_LENGTH = INET6_ADDRSTRLEN;
    29 
    28 
    30 /**
    29 /**
    31  * We use ClanLib's IPAddress API, but with our own name
    30  * We use ClanLib's IPAddress API, but with our own name
    32  */ 
    31  */ 
    33 class NetworkAddress {
    32 class NetworkEndpoint {
    34     private:
    33     protected:
    35         /**
    34         /**
    36          * Our human-readable hostname
    35          * Our human-readable hostname
    37          */
    36          */
    38         std::string hostname;
    37         std::string hostname;
    39 
    38 
    40         /**
    39         /**
    41          * Our human-readable service
    40          * Our human-readable service
    42          */
    41          */
    43         std::string service;
    42         std::string service;
    44 
    43 
    45         /**
       
    46          * Our machine-readable address and its length
       
    47          */
       
    48         sockaddr_storage address;
       
    49         socklen_t address_length;
       
    50 
       
    51     public:
    44     public:
    52         /**
    45         /**
    53          * Construct an empty NetworkAddress
    46          * Construct an empty NetworkEndpoint
    54          */
    47          */
    55         NetworkAddress (void);
    48         NetworkEndpoint (void);
    56 
    49 
    57         /**
    50         /**
    58          * Construct a NetworkAddress with a NULL hostname, and a specific service
    51          * Construct a NetworkEndpoint with a NULL hostname, and a specific service
    59          */
    52          */
    60         NetworkAddress (std::string service);
    53         explicit NetworkEndpoint (std::string service);
    61 
    54 
    62         /**
    55         /**
    63          * Construct a NetworkAddress on a specific hostname and service
    56          * Construct a NetworkEndpoint on a specific hostname and service
    64          */
    57          */
    65         NetworkAddress (std::string hostname, std::string service);
    58         NetworkEndpoint (std::string hostname, std::string service);
    66         
    59         
    67         /**
       
    68          * Construct a NetworkAddress from a machine-readable address of the given length
       
    69          */
       
    70         NetworkAddress (const sockaddr *addr, socklen_t len);
       
    71 
       
    72         /*
    60         /*
    73          * We can use the default copy-constructor and assignment operator
    61          * We can use the default copy-constructor and assignment operator
    74          */
    62          */
    75    
    63    
    76     public:    
    64     public:    
    84          * @param protoocl the socket protocol for hints.ai_protocol
    72          * @param protoocl the socket protocol for hints.ai_protocol
    85          * @param flags the flags for hints.ai_flags
    73          * @param flags the flags for hints.ai_flags
    86          * @return linked list of addrinfo's
    74          * @return linked list of addrinfo's
    87          * @throw NetworkAddressError if resolving this address fails
    75          * @throw NetworkAddressError if resolving this address fails
    88          */
    76          */
    89         addrinfo* get_addrinfo (int family, int socktype, int protocol = 0, int flags = 0) const;
    77         virtual addrinfo* get_addrinfo (int family, int socktype, int protocol = 0, int flags = 0) const;
    90 
    78         
    91         /**
    79         /**
    92          * Get a sockaddr* for this address. This is only valid for NetworkAddress's which have had set_sockaddr called
    80          * Free an addrinfo returned by get_addrinfo
    93          * on them.
       
    94          *
       
    95          * @param len_ref updated to the sockaddr length
       
    96          * @return borrowed sockaddr pointer
       
    97          */
    81          */
    98         const sockaddr* get_sockaddr (socklen_t &len_ref) const;
    82         virtual void free_addrinfo (addrinfo *info) const;
    99 
       
   100         /**
       
   101          * Set a sockaddr for this address
       
   102          *
       
   103          * @param addr the address to copy, of len bytes
       
   104          * @param len the size of the sockaddr
       
   105          */
       
   106         void set_sockaddr (const sockaddr *addr, socklen_t len);
       
   107 
    83 
   108         /**
    84         /**
   109          * Get the human-readable hostname
    85          * Get the human-readable hostname
   110          */
    86          */
   111         std::string get_hostname (void) const { return hostname; }
    87         virtual std::string get_hostname (void) const { return hostname; }
   112 
    88 
   113         /**
    89         /**
   114          * Get the human-readable service name
    90          * Get the human-readable service name
   115          */
    91          */
   116         std::string get_service (void) const { return service; }
    92         virtual std::string get_service (void) const { return service; }
   117 
       
   118         /**
       
   119          * Equal-to comparison operator. This requires that the machine-readable address be available.
       
   120          */
       
   121         bool operator== (const NetworkAddress &other) const {
       
   122             return (address_length == other.address_length) && memcmp(&address, &other.address, address_length) == 0;
       
   123         }
       
   124 
       
   125         /**
       
   126          * Not-qqual-to comparison operator. This requires that the machine-readable address be available.
       
   127          */
       
   128         bool operator!= (const NetworkAddress &other) const {
       
   129             return (address_length != other.address_length) || memcmp(&address, &other.address, address_length) != 0;
       
   130         }
       
   131 
       
   132         /**
       
   133          * Less-than comparison operator. Smaller addresses are always lesser.
       
   134          */
       
   135         bool operator< (const NetworkAddress &other) const {
       
   136             return (address_length < other.address_length) || memcmp(&address, &other.address, other.address_length) < 0;
       
   137         }
       
   138 
       
   139         /**
       
   140          * Greater-than comparison operator. Bigger addresses are always greater.
       
   141          */
       
   142         bool operator> (const NetworkAddress &other) const {
       
   143             return (address_length > other.address_length) || memcmp(&address, &other.address, address_length) > 0;
       
   144         }
       
   145 };
    93 };
   146 
    94 
   147 /**
    95 /**
   148  * Formatted as  [<addr>:<port>]
    96  * Formatted as  [<addr>:<port>]
   149  */
    97  */
   150 std::ostream& operator<< (std::ostream &s, const NetworkAddress &addr);
    98 std::ostream& operator<< (std::ostream &s, const NetworkEndpoint &addr);
   151 
    99 
   152 /**
   100 /**
   153  *
   101  *
   154  */
   102  */
   155 class NetworkAddressError : public Error {
   103 class NetworkAddressError : public Error {
   156     protected:
   104     protected:
   157         static std::string build_str (const NetworkAddress &addr, const char *op, const char *msg);
   105         static std::string build_str (const NetworkEndpoint &addr, const char *op, const char *msg);
   158 
   106 
   159     public:
   107     public:
   160         NetworkAddressError (const NetworkAddress &addr, const char *op, const char *msg);
   108         NetworkAddressError (const NetworkEndpoint &addr, const char *op, const char *msg);
   161 };
   109 };
   162 
   110 
   163 #endif /* NETWORK_ADDRESS_HH */
   111 #endif /* NETWORK_ADDRESS_HH */