1// 2// generic/detail/impl/endpoint.hpp 3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4// 5// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) 6// 7// Distributed under the Boost Software License, Version 1.0. (See accompanying 8// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 9// 10 11#ifndef ASIO_GENERIC_DETAIL_IMPL_ENDPOINT_IPP 12#define ASIO_GENERIC_DETAIL_IMPL_ENDPOINT_IPP 13 14 15#include "asio/detail/config.hpp" 16 17#include <cstring> 18#include <typeinfo> 19#include "asio/detail/socket_ops.hpp" 20#include "asio/detail/throw_error.hpp" 21#include "asio/detail/throw_exception.hpp" 22#include "asio/error.hpp" 23#include "asio/generic/detail/endpoint.hpp" 24 25#include "asio/detail/push_options.hpp" 26 27namespace asio { 28namespace generic { 29namespace detail { 30 31endpoint::endpoint() 32{ 33 init(0, 0, 0); 34} 35 36endpoint::endpoint(const void* sock_addr, 37 std::size_t sock_addr_size, int sock_protocol) 38{ 39 init(sock_addr, sock_addr_size, sock_protocol); 40} 41 42void endpoint::resize(std::size_t new_size) 43{ 44 if (new_size > sizeof(asio::detail::sockaddr_storage_type)) 45 { 46 asio::error_code ec(asio::error::invalid_argument); 47 asio::detail::throw_error(ec); 48 } 49 else 50 { 51 size_ = new_size; 52 protocol_ = 0; 53 } 54} 55 56bool operator==(const endpoint& e1, const endpoint& e2) 57{ 58 using namespace std; // For memcmp. 59 return e1.size() == e2.size() && memcmp(e1.data(), e2.data(), e1.size()) == 0; 60} 61 62bool operator<(const endpoint& e1, const endpoint& e2) 63{ 64 if (e1.protocol() < e2.protocol()) 65 return true; 66 67 if (e1.protocol() > e2.protocol()) 68 return false; 69 70 using namespace std; // For memcmp. 71 std::size_t compare_size = e1.size() < e2.size() ? e1.size() : e2.size(); 72 int compare_result = memcmp(e1.data(), e2.data(), compare_size); 73 74 if (compare_result < 0) 75 return true; 76 77 if (compare_result > 0) 78 return false; 79 80 return e1.size() < e2.size(); 81} 82 83void endpoint::init(const void* sock_addr, 84 std::size_t sock_addr_size, int sock_protocol) 85{ 86 if (sock_addr_size > sizeof(asio::detail::sockaddr_storage_type)) 87 { 88 asio::error_code ec(asio::error::invalid_argument); 89 asio::detail::throw_error(ec); 90 } 91 92 using namespace std; // For memset and memcpy. 93 memset(&data_.generic, 0, sizeof(asio::detail::sockaddr_storage_type)); 94 memcpy(&data_.generic, sock_addr, sock_addr_size); 95 96 size_ = sock_addr_size; 97 protocol_ = sock_protocol; 98} 99 100} // namespace detail 101} // namespace generic 102} // namespace asio 103 104#include "asio/detail/pop_options.hpp" 105 106#endif // ASIO_GENERIC_DETAIL_IMPL_ENDPOINT_IPP 107