libcruft-util/net/address.cpp

168 lines
4.5 KiB
C++
Raw Normal View History

/*
2015-04-13 18:05:28 +10:00
* 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
*
2015-04-13 18:05:28 +10:00
* 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 <danny@nerdcruft.net>
*/
#include "address.hpp"
#include "except.hpp"
#include "../debug.hpp"
#include "../endian.hpp"
#include "../types.hpp"
2012-07-30 16:33:12 +10:00
#include "../raii.hpp"
#ifdef __WIN32
#else
#include <netdb.h>
#include <arpa/inet.h>
#endif
//-----------------------------------------------------------------------------
using namespace net;
using namespace std;
//-----------------------------------------------------------------------------
#ifdef __WIN32
const char* inet_ntop(int af, const void* src, char* dst, int size){
struct sockaddr_in srcaddr;
memset(&srcaddr, 0, sizeof(struct sockaddr_in));
memcpy(&(srcaddr.sin_addr), src, sizeof(srcaddr.sin_addr));
srcaddr.sin_family = af;
if (WSAAddressToString ((struct sockaddr*) &srcaddr, sizeof (struct sockaddr_in), 0, dst, (LPDWORD) &size) != 0)
net::error::throw_code ();
return dst;
}
#endif
2012-07-30 16:33:12 +10:00
//-----------------------------------------------------------------------------
template <domain D>
typename address<D>::port_type
2015-04-13 18:06:08 +10:00
address<D>::port (void) const
2012-07-30 16:33:12 +10:00
{ return m_port; }
template <domain D>
void
address<D>::set_port (const port_type &_port)
{ m_port = _port; }
template <domain D>
typename address<D>::ip_type
address<D>::resolve (const std::string &str) {
addrinfo hint;
memset (&hint, 0, sizeof (hint));
hint.ai_family = static_cast<int> (D);
addrinfo* resolved;
int err = getaddrinfo (str.c_str (), nullptr, nullptr, &resolved);
if (err)
net::error::throw_code ();
auto deletor = [] (addrinfo *a) { freeaddrinfo (a); };
std::unique_ptr<addrinfo, decltype(deletor)> raii(resolved, deletor);
return ip_type (reinterpret_cast<sockaddr_type*> (resolved->ai_addr)->sin_addr.s_addr);
}
//-----------------------------------------------------------------------------
namespace net {
template <>
address<domain::INET>::address (const sockaddr_type &addr):
m_ip (addr.sin_addr.s_addr),
m_mask (0),
m_port (ntoh (addr.sin_port))
{
CHECK (addr.sin_family == (int)domain::INET);
}
template <domain D>
2012-07-30 16:33:12 +10:00
address<D>::address (const std::string &_addr,
port_type _port):
2012-07-30 16:33:12 +10:00
m_ip (resolve (_addr)),
m_mask ( 0),
m_port (_port)
{ ; }
template <>
address<domain::INET>::sockaddr_type
address<domain::INET>::to_sockaddr (void) const {
sockaddr_type addr;
addr.sin_family = (int)domain::INET;
addr.sin_port = hton (m_port);
addr.sin_addr.s_addr = m_ip.m_integer;
return addr;
}
template <>
std::string
address<domain::INET>::to_string (void) const {
char dest[INET_ADDRSTRLEN + 1];
sockaddr_type addr = to_sockaddr ();
if (NULL == inet_ntop ((int)domain::INET, &addr.sin_addr, dest, sizeof (dest)))
net::error::throw_code ();
return dest;
}
template <>
bool
address<domain::INET>::operator ==(const address<domain::INET> &rhs) {
return m_ip == rhs.m_ip &&
m_mask == rhs.m_mask &&
m_port == rhs.m_port;
}
}
//-----------------------------------------------------------------------------
std::ostream&
net::operator<< (std::ostream &os, const address<net::domain::INET> &addr) {
os << addr.to_string () << ":" << addr.port ();
return os;
}
//-----------------------------------------------------------------------------
2012-07-30 16:33:12 +10:00
template <> const address<domain::INET>
address<domain::INET>::LOOPBACK ("127.0.0.1", 0);
2012-07-30 16:33:12 +10:00
template <> const address<domain::INET>
address<domain::INET>::ANY ("0.0.0.0", 0);
namespace net {
template class address<domain::INET>;
}
2012-07-30 16:33:12 +10:00
//-----------------------------------------------------------------------------
//template <> const address<domain::INET6>
//address<domain::INET6>::LOOPBACK ("::1", 0);
//
//template <> const address<domain::INET6>
//address<domain::INET6>::ANY ("::0", 0);
//
//template class address<domain::INET6>;