PageRenderTime 22ms CodeModel.GetById 19ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/asio/ip/detail/impl/endpoint.ipp

http://hadesmem.googlecode.com/
C++ Header | 203 lines | 173 code | 21 blank | 9 comment | 16 complexity | 784749798229f05372c53a62c3441a40 MD5 | raw file
  1//
  2// ip/detail/impl/endpoint.ipp
  3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4//
  5// Copyright (c) 2003-2011 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_IP_DETAIL_IMPL_ENDPOINT_IPP
 12#define BOOST_ASIO_IP_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#include <cstring>
 20#if !defined(BOOST_NO_IOSTREAM)
 21# include <sstream>
 22#endif // !defined(BOOST_NO_IOSTREAM)
 23#include <boost/asio/detail/socket_ops.hpp>
 24#include <boost/asio/detail/throw_error.hpp>
 25#include <boost/asio/error.hpp>
 26#include <boost/asio/ip/detail/endpoint.hpp>
 27
 28#include <boost/asio/detail/push_options.hpp>
 29
 30namespace boost {
 31namespace asio {
 32namespace ip {
 33namespace detail {
 34
 35endpoint::endpoint()
 36  : data_()
 37{
 38  data_.v4.sin_family = AF_INET;
 39  data_.v4.sin_port = 0;
 40  data_.v4.sin_addr.s_addr = INADDR_ANY;
 41}
 42
 43endpoint::endpoint(int family, unsigned short port_num)
 44  : data_()
 45{
 46  using namespace std; // For memcpy.
 47  if (family == PF_INET)
 48  {
 49    data_.v4.sin_family = AF_INET;
 50    data_.v4.sin_port =
 51      boost::asio::detail::socket_ops::host_to_network_short(port_num);
 52    data_.v4.sin_addr.s_addr = INADDR_ANY;
 53  }
 54  else
 55  {
 56    data_.v6.sin6_family = AF_INET6;
 57    data_.v6.sin6_port =
 58      boost::asio::detail::socket_ops::host_to_network_short(port_num);
 59    data_.v6.sin6_flowinfo = 0;
 60    data_.v6.sin6_addr.s6_addr[0] = 0; data_.v6.sin6_addr.s6_addr[1] = 0;
 61    data_.v6.sin6_addr.s6_addr[2] = 0, data_.v6.sin6_addr.s6_addr[3] = 0;
 62    data_.v6.sin6_addr.s6_addr[4] = 0, data_.v6.sin6_addr.s6_addr[5] = 0;
 63    data_.v6.sin6_addr.s6_addr[6] = 0, data_.v6.sin6_addr.s6_addr[7] = 0;
 64    data_.v6.sin6_addr.s6_addr[8] = 0, data_.v6.sin6_addr.s6_addr[9] = 0;
 65    data_.v6.sin6_addr.s6_addr[10] = 0, data_.v6.sin6_addr.s6_addr[11] = 0;
 66    data_.v6.sin6_addr.s6_addr[12] = 0, data_.v6.sin6_addr.s6_addr[13] = 0;
 67    data_.v6.sin6_addr.s6_addr[14] = 0, data_.v6.sin6_addr.s6_addr[15] = 0;
 68    data_.v6.sin6_scope_id = 0;
 69  }
 70}
 71
 72endpoint::endpoint(const boost::asio::ip::address& addr,
 73    unsigned short port_num)
 74  : data_()
 75{
 76  using namespace std; // For memcpy.
 77  if (addr.is_v4())
 78  {
 79    data_.v4.sin_family = AF_INET;
 80    data_.v4.sin_port =
 81      boost::asio::detail::socket_ops::host_to_network_short(port_num);
 82    data_.v4.sin_addr.s_addr =
 83      boost::asio::detail::socket_ops::host_to_network_long(
 84          addr.to_v4().to_ulong());
 85  }
 86  else
 87  {
 88    data_.v6.sin6_family = AF_INET6;
 89    data_.v6.sin6_port =
 90      boost::asio::detail::socket_ops::host_to_network_short(port_num);
 91    data_.v6.sin6_flowinfo = 0;
 92    boost::asio::ip::address_v6 v6_addr = addr.to_v6();
 93    boost::asio::ip::address_v6::bytes_type bytes = v6_addr.to_bytes();
 94    memcpy(data_.v6.sin6_addr.s6_addr, bytes.data(), 16);
 95    data_.v6.sin6_scope_id = v6_addr.scope_id();
 96  }
 97}
 98
 99void endpoint::resize(std::size_t new_size)
100{
101  if (new_size > sizeof(boost::asio::detail::sockaddr_storage_type))
102  {
103    boost::system::error_code ec(boost::asio::error::invalid_argument);
104    boost::asio::detail::throw_error(ec);
105  }
106}
107
108unsigned short endpoint::port() const
109{
110  if (is_v4())
111  {
112    return boost::asio::detail::socket_ops::network_to_host_short(
113        data_.v4.sin_port);
114  }
115  else
116  {
117    return boost::asio::detail::socket_ops::network_to_host_short(
118        data_.v6.sin6_port);
119  }
120}
121
122void endpoint::port(unsigned short port_num)
123{
124  if (is_v4())
125  {
126    data_.v4.sin_port
127      = boost::asio::detail::socket_ops::host_to_network_short(port_num);
128  }
129  else
130  {
131    data_.v6.sin6_port
132      = boost::asio::detail::socket_ops::host_to_network_short(port_num);
133  }
134}
135
136boost::asio::ip::address endpoint::address() const
137{
138  using namespace std; // For memcpy.
139  if (is_v4())
140  {
141    return boost::asio::ip::address_v4(
142        boost::asio::detail::socket_ops::network_to_host_long(
143          data_.v4.sin_addr.s_addr));
144  }
145  else
146  {
147    boost::asio::ip::address_v6::bytes_type bytes;
148#if defined(BOOST_ASIO_HAS_STD_ARRAY)
149    memcpy(bytes.data(), data_.v6.sin6_addr.s6_addr, 16);
150#else // defined(BOOST_ASIO_HAS_STD_ARRAY)
151    memcpy(bytes.elems, data_.v6.sin6_addr.s6_addr, 16);
152#endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
153    return boost::asio::ip::address_v6(bytes, data_.v6.sin6_scope_id);
154  }
155}
156
157void endpoint::address(const boost::asio::ip::address& addr)
158{
159  endpoint tmp_endpoint(addr, port());
160  data_ = tmp_endpoint.data_;
161}
162
163bool operator==(const endpoint& e1, const endpoint& e2)
164{
165  return e1.address() == e2.address() && e1.port() == e2.port();
166}
167
168bool operator<(const endpoint& e1, const endpoint& e2)
169{
170  if (e1.address() < e2.address())
171    return true;
172  if (e1.address() != e2.address())
173    return false;
174  return e1.port() < e2.port();
175}
176
177#if !defined(BOOST_NO_IOSTREAM)
178std::string endpoint::to_string(boost::system::error_code& ec) const
179{
180  std::string a = address().to_string(ec);
181  if (ec)
182    return std::string();
183
184  std::ostringstream tmp_os;
185  tmp_os.imbue(std::locale::classic());
186  if (is_v4())
187    tmp_os << a;
188  else
189    tmp_os << '[' << a << ']';
190  tmp_os << ':' << port();
191
192  return tmp_os.str();
193}
194#endif // !defined(BOOST_NO_IOSTREAM)
195
196} // namespace detail
197} // namespace ip
198} // namespace asio
199} // namespace boost
200
201#include <boost/asio/detail/pop_options.hpp>
202
203#endif // BOOST_ASIO_IP_DETAIL_IMPL_ENDPOINT_IPP