terom@185: #ifndef NETWORK_ADDRESS_HH terom@185: #define NETWORK_ADDRESS_HH terom@185: terom@378: #include "../Error.hh" terom@378: terom@378: /* terom@378: * Platform-specific includes terom@378: */ terom@378: #ifndef WIN32 terom@378: // linux terom@378: #include terom@378: #include terom@378: #else terom@378: #error "This network code won't compile on win32 :)" terom@378: #endif terom@378: terom@378: #include terom@378: terom@378: // assume... terom@378: #if INET6_ADDRSTRLEN < INET_ADDRSTRLEN terom@378: #error INET6_ADDRSTRLEN is smaller than INET_ADDRSTRLEN terom@378: #endif terom@378: terom@378: /** terom@378: * Length of a network address terom@378: */ terom@378: const socklen_t NETWORK_ADDRESS_LENGTH = INET6_ADDRSTRLEN; terom@185: terom@284: /** terom@284: * We use ClanLib's IPAddress API, but with our own name terom@284: */ terom@381: class NetworkEndpoint { terom@381: protected: terom@378: /** terom@378: * Our human-readable hostname terom@378: */ terom@378: std::string hostname; terom@378: terom@378: /** terom@378: * Our human-readable service terom@378: */ terom@378: std::string service; terom@378: terom@378: public: terom@378: /** terom@381: * Construct an empty NetworkEndpoint terom@378: */ terom@381: NetworkEndpoint (void); terom@378: terom@378: /** terom@381: * Construct a NetworkEndpoint with a NULL hostname, and a specific service terom@378: */ terom@381: explicit NetworkEndpoint (std::string service); terom@378: terom@378: /** terom@381: * Construct a NetworkEndpoint on a specific hostname and service terom@378: */ terom@381: NetworkEndpoint (std::string hostname, std::string service); terom@378: terom@380: /* terom@380: * We can use the default copy-constructor and assignment operator terom@380: */ terom@378: terom@378: public: terom@378: /** terom@378: * Get a addrinfo* for this address using the given family/type/protocol/flags. terom@378: * terom@378: * Remember to free the returned pointer using freeaddrinfo() after use. terom@378: * terom@378: * @param family the socket family for hints.ai_family terom@378: * @param socktype the socket type for hints.ai_socktype terom@378: * @param protoocl the socket protocol for hints.ai_protocol terom@378: * @param flags the flags for hints.ai_flags terom@378: * @return linked list of addrinfo's terom@378: * @throw NetworkAddressError if resolving this address fails terom@378: */ terom@381: virtual addrinfo* get_addrinfo (int family, int socktype, int protocol = 0, int flags = 0) const; terom@381: terom@378: /** terom@381: * Free an addrinfo returned by get_addrinfo terom@378: */ terom@381: virtual void free_addrinfo (addrinfo *info) const; terom@378: terom@378: /** terom@378: * Get the human-readable hostname terom@378: */ terom@381: virtual std::string get_hostname (void) const { return hostname; } terom@378: terom@378: /** terom@378: * Get the human-readable service name terom@378: */ terom@381: virtual std::string get_service (void) const { return service; } terom@378: }; terom@185: terom@284: /** terom@284: * Formatted as [:] terom@284: */ terom@381: std::ostream& operator<< (std::ostream &s, const NetworkEndpoint &addr); terom@185: terom@378: /** terom@378: * terom@378: */ terom@378: class NetworkAddressError : public Error { terom@378: protected: terom@381: static std::string build_str (const NetworkEndpoint &addr, const char *op, const char *msg); terom@378: terom@378: public: terom@381: NetworkAddressError (const NetworkEndpoint &addr, const char *op, const char *msg); terom@378: }; terom@378: terom@185: #endif /* NETWORK_ADDRESS_HH */