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
#ifndef NETWORK_ADDRESS_HH
#define NETWORK_ADDRESS_HH

#include "../Error.hh"

/*
 * Platform-specific includes
 */
#ifndef WIN32
    // linux
    #include <sys/types.h>
    #include <netdb.h>
#else
    #error "This network code won't compile on win32 :)"
#endif

#include <string>

// assume...
#if INET6_ADDRSTRLEN < INET_ADDRSTRLEN
    #error INET6_ADDRSTRLEN is smaller than INET_ADDRSTRLEN
#endif

/**
 * Length of a network address
 */
const socklen_t NETWORK_ADDRESS_LENGTH = INET6_ADDRSTRLEN;

/**
 * We use ClanLib's IPAddress API, but with our own name
 */ 
class NetworkEndpoint {
    protected:
        /**
         * Our human-readable hostname
         */
        std::string hostname;

        /**
         * Our human-readable service
         */
        std::string service;

    public:
        /**
         * Construct an empty NetworkEndpoint
         */
        NetworkEndpoint (void);

        /**
         * Construct a NetworkEndpoint with a NULL hostname, and a specific service
         */
        explicit NetworkEndpoint (std::string service);

        /**
         * Construct a NetworkEndpoint on a specific hostname and service
         */
        NetworkEndpoint (std::string hostname, std::string service);
        
        /*
         * We can use the default copy-constructor and assignment operator
         */
   
    public:    
        /**
         * Get a addrinfo* for this address using the given family/type/protocol/flags.
         *
         * Remember to free the returned pointer using freeaddrinfo() after use.
         *
         * @param family the socket family for hints.ai_family
         * @param socktype the socket type for hints.ai_socktype
         * @param protoocl the socket protocol for hints.ai_protocol
         * @param flags the flags for hints.ai_flags
         * @return linked list of addrinfo's
         * @throw NetworkAddressError if resolving this address fails
         */
        virtual addrinfo* get_addrinfo (int family, int socktype, int protocol = 0, int flags = 0) const;
        
        /**
         * Free an addrinfo returned by get_addrinfo
         */
        virtual void free_addrinfo (addrinfo *info) const;

        /**
         * Get the human-readable hostname
         */
        virtual std::string get_hostname (void) const { return hostname; }

        /**
         * Get the human-readable service name
         */
        virtual std::string get_service (void) const { return service; }
};

/**
 * Formatted as  [<addr>:<port>]
 */
std::ostream& operator<< (std::ostream &s, const NetworkEndpoint &addr);

/**
 *
 */
class NetworkAddressError : public Error {
    protected:
        static std::string build_str (const NetworkEndpoint &addr, const char *op, const char *msg);

    public:
        NetworkAddressError (const NetworkEndpoint &addr, const char *op, const char *msg);
};

#endif /* NETWORK_ADDRESS_HH */