forked from d5/node.native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.h
More file actions
43 lines (37 loc) · 871 Bytes
/
net.h
File metadata and controls
43 lines (37 loc) · 871 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
38
39
40
41
42
43
#ifndef __NET_H__
#define __NET_H__
#include "base.h"
#include "callback.h"
namespace native
{
namespace net
{
typedef sockaddr_in ip4_addr;
typedef sockaddr_in6 ip6_addr;
ip4_addr to_ip4_addr(const std::string& ip, int port) { return uv_ip4_addr(ip.c_str(), port); }
ip6_addr to_ip6_addr(const std::string& ip, int port) { return uv_ip6_addr(ip.c_str(), port); }
bool from_ip4_addr(ip4_addr* src, std::string& ip, int& port)
{
char dest[16];
if(uv_ip4_name(src, dest, 16) == 0)
{
ip = dest;
port = static_cast<int>(ntohs(src->sin_port));
return true;
}
return false;
}
bool from_ip6_addr(ip6_addr* src, std::string& ip, int& port)
{
char dest[46];
if(uv_ip6_name(src, dest, 46) == 0)
{
ip = dest;
port = static_cast<int>(ntohs(src->sin6_port));
return true;
}
return false;
}
}
}
#endif