-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInetAddress.cpp
More file actions
37 lines (32 loc) · 921 Bytes
/
InetAddress.cpp
File metadata and controls
37 lines (32 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "InetAddress.h"
#include "SocketOps.h"
#include <strings.h>
#include <netinet/in.h>
#include <stdint.h>
#include <boost/static_assert.hpp>
static const in_addr_t kInaddrAny = INADDR_ANY;
BOOST_STATIC_ASSERT(sizeof(InetAddress) == sizeof(struct sockaddr_in));
InetAddress::InetAddress(uint16_t port)
{
bzero(&m_addr, sizeof(m_addr));
m_addr.sin_family = AF_INET;
m_addr.sin_addr.s_addr = SocketOps::hostToNetwork32(kInaddrAny);
m_addr.sin_port = SocketOps::hostToNetwork16(port);
}
InetAddress::InetAddress(const std::string& ip, uint16_t port)
{
bzero(&m_addr, sizeof(m_addr));
SocketOps::fromIpPort(ip.data(), port, &m_addr);
}
std::string InetAddress::toIp() const
{
char buf[32];
SocketOps::toIp(buf, sizeof(buf), m_addr);
return buf;
}
std::string InetAddress::toIpPort() const
{
char buf[32];
SocketOps::toIpPort(buf, sizeof(buf), m_addr);
return buf;
}