-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnetutil.cpp
110 lines (96 loc) · 2.82 KB
/
netutil.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "netutil.h"
#include "base/log.h"
#define IP_SIZE 16
int NetUtil::Listen(const char * serverIp, uint16_t port)
{
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == fd)
{
ERRLOG("%s %s %d, socket error. %s", __FILE__, __func__, __LINE__, strerror(errno));
return -1;
}
SetReuseAddr(fd);
SetNonblock(fd);
struct sockaddr_in servAddr;
SetAddr(serverIp, port, servAddr);
if (-1 == bind(fd, reinterpret_cast<struct sockaddr *>(&servAddr), static_cast<socklen_t>(sizeof(servAddr))))
{
ERRLOG("%s %s %d, bind error. %s", __FILE__, __func__, __LINE__, strerror(errno));
close(fd);
return -1;
}
if (-1 == listen(fd, 128))
{
ERRLOG("%s %s %d, listen error. %s", __FILE__, __func__, __LINE__, strerror(errno));
close(fd);
return -1;
}
return fd;
}
int NetUtil::Connect(const char * serverIp, uint16_t port)
{
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == fd)
{
ERRLOG("%s %s %d, socket error. %s", __FILE__, __func__, __LINE__, strerror(errno));
return -1;
}
struct sockaddr_in servAddr;
SetAddr(serverIp, port, servAddr);
if (-1 == connect(fd, reinterpret_cast<struct sockaddr *>(&servAddr), static_cast<socklen_t>(sizeof(servAddr))))
{
close(fd);
ERRLOG("%s %s %d, connect error. %s", __FILE__, __func__, __LINE__, strerror(errno));
return -1;
}
return fd;
}
void NetUtil::SetReuseAddr(int fd, int optval /* = 1 */)
{
if (0 != setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, static_cast<socklen_t>(sizeof(optval))))
{
ERRLOG("%s %s %d, set reuse address error. %s", __FILE__, __func__, __LINE__, strerror(errno));
}
}
void NetUtil::SetNonblock(int fd)
{
int ret = fcntl(fd, F_GETFL);
ret = ret | O_NONBLOCK;
if (-1 == fcntl(fd, F_SETFL, ret))
{
ERRLOG("%s %s %d, set fd = %d nonblock error. %s", __FILE__, __func__, __LINE__, fd, strerror(errno));
}
}
void NetUtil::SetNoDelay(int fd, int optval /* = 1 */)
{
if (0 != setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &optval, static_cast<socklen_t>(sizeof(optval))))
{
ERRLOG("%s %s %d, set fd = %d nodelay error. %s", __FILE__, __func__, __LINE__, fd, strerror(errno));
}
}
void NetUtil::SetAddr(const char * serverIp, uint16_t port, struct sockaddr_in & sockAddr)
{
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(port);
if (1 != inet_pton(AF_INET, serverIp, &sockAddr.sin_addr))
{
ERRLOG("%s %s %d, set server address error. %s", __FILE__, __func__, __LINE__, strerror(errno));
}
}
void NetUtil::GetIpByDomain(const char * domain, std::vector<std::string> & vecIp)
{
struct hostent * hptr = gethostbyname(domain);
if (NULL == hptr)
{
ERRLOG("%s %s %d, gethostbyname error. %s", __FILE__, __func__, __LINE__, domain);
return;
}
char ip[IP_SIZE];
for (char ** pptr = hptr->h_addr_list; NULL != *pptr; ++pptr)
{
if (NULL != inet_ntop(hptr->h_addrtype, *pptr, ip, IP_SIZE))
{
vecIp.push_back(std::string(ip));
}
}
}