/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Copyright 2011 Danny Robson */ #ifndef __NET_ADDRESS_HPP #define __NET_ADDRESS_HPP #include "types.hpp" #include "../ip.hpp" #if defined(HAVE_WINSOCK2_H) #include "ws2tcpip.h" #endif #include #include //----------------------------------------------------------------------------- namespace net { /// Supporting types used for defining addresses in various domains template struct address_types; template <> struct address_types { typedef ipv4::ip ip; typedef ipv4::mask_t mask_t; typedef ipv4::port_t port_t; typedef sockaddr_in sockaddr; }; template <> struct address_types { typedef ipv6::ip ip; typedef ipv6::mask_t mask_t; typedef ipv6::port_t port_t; typedef sockaddr_in6 sockaddr; }; /// A full endpoint specification for a domain. Must be usable for bind/listen and send/recv. template class address { public: typedef typename address_types::ip ip_type; typedef typename address_types::mask_t mask_type; typedef typename address_types::port_t port_type; typedef typename address_types::sockaddr sockaddr_type; protected: ip_type m_ip; mask_type m_mask; port_type m_port; public: static const address LOOPBACK; static const address ANY; explicit address (const sockaddr_type &); address (const std::string &address, port_type); port_type port (void) const; void set_port (const port_type &); ip_type ip (void) const { return m_ip; } sockaddr_type to_sockaddr (void) const; std::string to_string (void) const; static ip_type resolve (const std::string &); bool operator ==(const address &rhs); }; std::ostream& operator<< (std::ostream &os, const net::address &addr); } #endif // __NET_ADDRESS_HPP