-
Notifications
You must be signed in to change notification settings - Fork 17
/
net.cpp
345 lines (302 loc) · 7.85 KB
/
net.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "unp.h"
/*
* get peer ipv4 (network order)
*/
uint32_t getpeerip(int fd)
{
sockaddr_in peeraddr;
socklen_t addrlen;
if(getpeername(fd, (sockaddr *)&peeraddr, &addrlen) < 0)
{
err_msg("getpeername error");
return -1;
}
return peeraddr.sin_addr.s_addr;
}
int Select(int nfds, fd_set * readfds, fd_set * writefds, fd_set * execpfds, struct timeval *timeout)
{
int n;
for(;;)
{
if((n = select(nfds, readfds, writefds, execpfds, timeout)) < 0)
{
if(errno == EINTR) continue;
else err_quit("select error");
}
else break;
}
return n; //can return 0 on timeout
}
ssize_t Readn(int fd, void * buf, size_t size)
{
ssize_t lefttoread = size, hasread = 0;
char *ptr = (char *)buf;
while(lefttoread > 0)
{
if((hasread = read(fd, ptr, lefttoread))<0)
{
if(errno == EINTR)
{
hasread = 0;
}
else
{
return -1;
}
}
else if(hasread == 0) //eof
{
break;
}
lefttoread -= hasread;
ptr += hasread;
}
return size - lefttoread;
}
ssize_t writen(int fd, const void * buf, size_t n)
{
ssize_t lefttowrite = n, haswrite = 0;
char *ptr = (char *)buf;
while(lefttowrite > 0)
{
if((haswrite = write(fd, ptr, lefttowrite)) < 0)
{
if(haswrite < 0 && errno == EINTR)
{
haswrite = 0;
}
else
{
return -1; //error
}
}
lefttowrite -= haswrite;
ptr += haswrite;
}
return n;
}
char *Sock_ntop(char * str, int size ,const sockaddr * sa, socklen_t salen)
{
switch (sa->sa_family)
{
case AF_INET:
{
struct sockaddr_in *sin = (struct sockaddr_in *) sa;
if(inet_ntop(AF_INET, &sin->sin_addr, str, size) == NULL)
{
err_msg("inet_ntop error");
return NULL;
}
if(ntohs(sin->sin_port) > 0)
{
snprintf(str + strlen(str), size - strlen(str), ":%d", ntohs(sin->sin_port));
}
return str;
}
case AF_INET6:
{
struct sockaddr_in6 *sin = (struct sockaddr_in6 *) sa;
if(inet_ntop(AF_INET6, &sin->sin6_addr, str, size) == NULL)
{
err_msg("inet_ntop error");
return NULL;
}
if(ntohs(sin->sin6_port) > 0)
{
snprintf(str + strlen(str), size - strlen(str), ":%d", ntohs(sin->sin6_port));
}
return str;
}
default:
return "server error";
}
return NULL;
}
void Setsockopt(int fd, int level, int optname, const void * optval, socklen_t optlen)
{
if(setsockopt(fd, level, optname, optval, optlen) < 0)
{
err_msg("setsockopt error");
}
}
void Close(int fd)
{
if(close(fd) < 0)
{
err_msg("Close error");
}
}
void Listen(int fd, int backlog)
{
if(listen(fd, backlog) < 0)
{
err_quit("listen error");
}
}
int Tcp_connect(const char * host, const char * serv)
{
int sockfd, n;
struct addrinfo hints, *res, *ressave;
bzero(&hints, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if((n = getaddrinfo(host, serv, &hints, &res)) != 0)
{
err_quit("tcp_connect error for %s, %s: %s", host, serv, gai_strerror(n));
}
ressave = res;
do
{
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if(sockfd < 0) continue;
if(connect(sockfd, res->ai_addr, res->ai_addrlen) == 0) break;
close(sockfd);
}while((res = res->ai_next) != NULL);
if(res == NULL)
{
err_quit("tcp_connect error for %s,%s", host, serv);
}
freeaddrinfo(ressave);
return sockfd;
}
int Tcp_listen(const char * host, const char * service, socklen_t * addrlen)
{
int listenfd, n;
const int on = 1;
struct addrinfo hints, *res, *ressave;
bzero(&hints, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE; //设置了AI_PASSIVE标志,但没有指定主机名,那么return ipv6和ipv4通配地址
hints.ai_family = AF_UNSPEC; //返回的是适用于指定主机名和服务名且适合任意协议族的地址
hints.ai_socktype = SOCK_STREAM;
char addr[MAXSOCKADDR];
if((n = getaddrinfo(host, service, &hints, &res)) > 0)
{
err_quit("tcp listen error for %s %s: %s", host, service, gai_strerror(n));
}
ressave = res;
do
{
listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if(listenfd < 0)
{
continue; //error try again
}
Setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if(bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
{
printf("server address: %s\n", Sock_ntop(addr, MAXSOCKADDR, res->ai_addr, res->ai_addrlen));
break; //success
}
Close(listenfd);
}while((res = res->ai_next) != NULL);
freeaddrinfo(ressave); //free
if(res == NULL)
{
err_quit("tcp listen error for %s: %s", host, service);
}
Listen(listenfd, LISTENQ);
if(addrlen)
{
*addrlen = res->ai_addrlen;
}
return listenfd;
}
int Accept(int listenfd, SA * addr, socklen_t *addrlen)
{
int n;
for(;;)
{
if((n = accept(listenfd, addr, addrlen)) < 0)
{
if(errno == EINTR)
continue;
else
err_quit("accept error");
}
else
{
return n;
}
}
}
void Socketpair(int family, int type, int protocol, int * sockfd)
{
if(socketpair(family, type, protocol, sockfd) < 0)
{
err_quit("socketpair error");
}
}
ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
{
struct msghdr msg;
struct iovec iov[1];
union{
struct cmsghdr cm;
char control[CMSG_SPACE(sizeof(int))];
}control_un;
struct cmsghdr *cmptr;
msg.msg_control = control_un.control;
msg.msg_controllen = sizeof(control_un.control);
cmptr = CMSG_FIRSTHDR(&msg);
cmptr->cmsg_len = CMSG_LEN(sizeof(int));
cmptr->cmsg_level = SOL_SOCKET;
cmptr->cmsg_type = SCM_RIGHTS;
*((int *) CMSG_DATA(cmptr)) = sendfd;
msg.msg_name = NULL;
msg.msg_namelen = 0;
iov[0].iov_base = ptr;
iov[0].iov_len = nbytes;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
return (sendmsg(fd, &msg, 0));
}
ssize_t Write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
{
ssize_t n;
if((n = write_fd(fd, ptr, nbytes, sendfd)) < 0)
{
err_quit("write fd error");
}
return n;
}
ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
{
struct msghdr msg;
struct iovec iov[1];
ssize_t n;
union {
struct cmsghdr cm;
char control[CMSG_SPACE(sizeof(int))];
}control_un;
struct cmsghdr *cmptr;
msg.msg_control = control_un.control;
msg.msg_controllen = sizeof(control_un.control);
//-------------------------
msg.msg_name = NULL;
msg.msg_namelen = 0;
iov[0].iov_base = ptr;
iov[0].iov_len = nbytes;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
if((n = recvmsg(fd, &msg, MSG_WAITALL)) < 0)
{
return n;
}
if((cmptr = CMSG_FIRSTHDR(&msg)) != NULL && cmptr->cmsg_len == CMSG_LEN(sizeof(int)))
{
if(cmptr->cmsg_level != SOL_SOCKET)
{
err_msg("control level != SOL_SOCKET");
}
if(cmptr->cmsg_type != SCM_RIGHTS)
{
err_msg("control type != SCM_RIGHTS");
}
*recvfd = *((int *) CMSG_DATA(cmptr));
}
else
{
*recvfd = -1; // descroptor was not passed
}
return n;
}