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 BOOST_ASIO_GENERIC_DETAIL_IMPL_ENDPOINT_IPP
12#define BOOST_ASIO_GENERIC_DETAIL_IMPL_ENDPOINT_IPP
13
14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15# pragma once
16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18#include <boost/asio/detail/config.hpp>
19
20#include <cstring>
21#include <typeinfo>
22#include <boost/asio/detail/socket_ops.hpp>
23#include <boost/asio/detail/throw_error.hpp>
24#include <boost/asio/detail/throw_exception.hpp>
25#include <boost/asio/error.hpp>
26#include <boost/asio/generic/detail/endpoint.hpp>
27
28#include <boost/asio/detail/push_options.hpp>
29
30namespace boost {
31namespace asio {
32namespace generic {
33namespace detail {
34
35endpoint::endpoint()
36{
37 init(0, 0, 0);
38}
39
40endpoint::endpoint(const void* sock_addr,
41 std::size_t sock_addr_size, int sock_protocol)
42{
43 init(sock_addr, sock_addr_size, sock_protocol);
44}
45
46void endpoint::resize(std::size_t new_size)
47{
48 if (new_size > sizeof(boost::asio::detail::sockaddr_storage_type))
49 {
50 boost::system::error_code ec(boost::asio::error::invalid_argument);
51 boost::asio::detail::throw_error(ec);
52 }
53 else
54 {
55 size_ = new_size;
56 protocol_ = 0;
57 }
58}
59
60bool operator==(const endpoint& e1, const endpoint& e2)
61{
62 using namespace std; // For memcmp.
63 return e1.size() == e2.size() && memcmp(e1.data(), e2.data(), e1.size()) == 0;
64}
65
66bool operator<(const endpoint& e1, const endpoint& e2)
67{
68 if (e1.protocol() < e2.protocol())
69 return true;
70
71 if (e1.protocol() > e2.protocol())
72 return false;
73
74 using namespace std; // For memcmp.
75 std::size_t compare_size = e1.size() < e2.size() ? e1.size() : e2.size();
76 int compare_result = memcmp(e1.data(), e2.data(), compare_size);
77
78 if (compare_result < 0)
79 return true;
80
81 if (compare_result > 0)
82 return false;
83
84 return e1.size() < e2.size();
85}
86
87void endpoint::init(const void* sock_addr,
88 std::size_t sock_addr_size, int sock_protocol)
89{
90 if (sock_addr_size > sizeof(boost::asio::detail::sockaddr_storage_type))
91 {
92 boost::system::error_code ec(boost::asio::error::invalid_argument);
93 boost::asio::detail::throw_error(ec);
94 }
95
96 using namespace std; // For memset and memcpy.
97 memset(&data_.generic, 0, sizeof(boost::asio::detail::sockaddr_storage_type));
98 memcpy(&data_.generic, sock_addr, sock_addr_size);
99
100 size_ = sock_addr_size;
101 protocol_ = sock_protocol;
102}
103
104} // namespace detail
105} // namespace generic
106} // namespace asio
107} // namespace boost
108
109#include <boost/asio/detail/pop_options.hpp>
110
111#endif // BOOST_ASIO_GENERIC_DETAIL_IMPL_ENDPOINT_IPP
112