-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinet_address.h
More file actions
49 lines (39 loc) · 1.34 KB
/
Copy pathinet_address.h
File metadata and controls
49 lines (39 loc) · 1.34 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef __YDX_INET_ADDRESS_H__
#define __YDX_INET_ADDRESS_H__
#include "string_piece.h"
#include <netinet/in.h>
#include <string>
namespace ydx
{
class InetAddress
{
public:
/// Constructs an endpoint with given port number.
/// Mostly used in TcpServer listening.
explicit InetAddress(uint16_t port = 0, bool loopbackOnly = false);
/// Constructs an endpoint with given ip and port.
/// @c ip should be "1.2.3.4"
InetAddress(StringArg ip, uint16_t port);
/// Constructs an endpoint with given struct @c sockaddr_in
/// Mostly used when accepting new connections
InetAddress(const struct sockaddr_in& addr)
: addr_(addr)
{ }
std::string toIp() const;
std::string toIpPort() const;
uint16_t toPort() const;
// default copy/assignment are Okay
const struct sockaddr_in& getSockAddrInet() const { return addr_; }
void setSockAddrInet(const struct sockaddr_in& addr) { addr_ = addr; }
uint32_t ipNetEndian() const { return addr_.sin_addr.s_addr; }
uint16_t portNetEndian() const { return addr_.sin_port; }
// resolve hostname to IP address, not changing port or sin_family
// return true on success.
// thread safe
static bool resolve(StringArg hostname, InetAddress* result);
// static std::vector<InetAddress> resolveAll(const char* hostname, uint16_t port = 0);
private:
struct sockaddr_in addr_;
};
}
#endif