-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathsysutil.c
executable file
·666 lines (591 loc) · 15.5 KB
/
sysutil.c
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#include "sysutil.h"
#include "common.h"
static ssize_t recv_peek(int sockfd, void *buf, size_t len);
static int lock_file(int fd, int type);
static struct timeval tv = {0, 0}; //全局变量
//设置socket的选项,使其接收带外数据
void activate_oobinline(int sockfd)
{
int oob_inline = 1;
if(setsockopt(sockfd, SOL_SOCKET, SO_OOBINLINE, &oob_inline, sizeof(oob_inline)) == -1)
ERR_EXIT("setsockopt oobinline");
}
//开启SIGURG信号,当有带外数据到来时,发出该信号
void activate_signal_sigurg(int sockfd)
{
if(fcntl(sockfd, F_SETOWN, getpid()) == -1)
ERR_EXIT("fcntl sigurg");
}
int get_curr_time_sec()
{
if(gettimeofday(&tv, NULL) == -1)
ERR_EXIT("gettimeofday");
return tv.tv_sec;
}
int get_curr_time_usec()
{
return tv.tv_usec;
}
int nano_sleep(double t)
{
int sec = (time_t)t;//取整数部分
int nsec = (t - sec) * 1000000000;
//int nanosleep(const struct timespec *req, struct timespec *rem);
struct timespec ts;
ts.tv_sec = sec;
ts.tv_nsec = nsec;
int ret;
do
{//当睡眠被打断时,剩余时间放到ts里面
ret = nanosleep(&ts, &ts);
}
while(ret == -1 && errno == EINTR)
;
return ret;
}
int lock_file_read(int fd)
{
return lock_file(fd, F_RDLCK);
}
int lock_file_write(int fd)
{
return lock_file(fd, F_WRLCK);
}
int unlock_file(int fd)
{
struct flock lock;
memset(&lock, 0, sizeof lock);
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
lock.l_pid = getpid();
return fcntl(fd, F_SETLK, &lock);
}
static int lock_file(int fd, int type)
{
struct flock lock;
memset(&lock, 0, sizeof lock);
lock.l_type = type;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
lock.l_pid = getpid();
int ret;
do
{
ret = fcntl(fd, F_SETLKW, &lock);
}
while(ret == -1 && errno == EINTR)
;
return ret;
}
/*
*函数功能:创建客户套接字
*参数port: 端口号
*函数返回值:返回客户端套接字
*/
int tcp_client(unsigned int port)
{
int sockfd;
//设置端口复用
if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
ERR_EXIT("socket");
if(port > 0)
{
int on = 1;
if((setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on))) == -1)
ERR_EXIT("setsockopt");
struct sockaddr_in addr;
memset(&addr, 0, sizeof addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
char ip[16] = {0};
get_local_ip(ip);
addr.sin_addr.s_addr = inet_addr(ip);
if(bind(sockfd, (struct sockaddr *)&addr, sizeof addr) == -1)
ERR_EXIT("bind");
}
return sockfd;
}
/*
*函数功能:启动服务器
*参数host:服务器IP地址或者服务器主机名
*参数port:服务器端口
*函数返回值:成功则返回监听套接字
*/
int tcp_server(const char *host, unsigned short port)
{
//建立套接字
int listenfd;
if((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
ERR_EXIT("tcp_server");
struct sockaddr_in seraddr;
memset(&seraddr, 0, sizeof(seraddr));
seraddr.sin_family = AF_INET;
if(host != NULL)
{
/*
*inet_aton函数将const char * 类型的host
*转化为二进制形式
*并存放到in_addr结构体中,sin_addr是in_addr类型的结构体
*/
if(inet_aton(host, &seraddr.sin_addr) == 0)
{
/*
*gethostbyname返回一个hostent结构体类型
*并将host拷贝到hostent结构体下的h_name中,
*以及host的in_addr结构体类型内容拷贝到返回值中hostent结构体类型的h_addr_list[0]中
*其中#define h_addr h_addr_list[0]
*/
struct hostent *hp;
hp = gethostbyname(host);
if(hp == NULL)
ERR_EXIT("gethostbyname");
seraddr.sin_addr = *(struct in_addr*)hp->h_addr;
}
}
else
seraddr.sin_addr.s_addr = htonl(INADDR_ANY);
seraddr.sin_port = htons(port);
//设置端口复用
int on = 1;
if((setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on))) < 0)
ERR_EXIT("setsockopt");
//绑定
if(bind(listenfd, (struct sockaddr*)&seraddr, sizeof(seraddr)) < 0)
ERR_EXIT("bind");
//监听
if(listen(listenfd, SOMAXCONN) < 0)
ERR_EXIT("listen");
return listenfd;
}
/*
*功能:获取当前主机的IP
*ip:
*返回值:
*/
int get_local_ip(char *ip)
{
int sockfd;
if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
{
ERR_EXIT("socket");
}
struct ifreq req;
bzero(&req, sizeof(struct ifreq));
strcpy(req.ifr_name, "eth0");
if(ioctl(sockfd, SIOCGIFADDR, &req) == -1)
ERR_EXIT("ioctl");
struct sockaddr_in *host = (struct sockaddr_in*)&req.ifr_addr;
strcpy(ip, inet_ntoa(host->sin_addr));
close(sockfd);
return 1;
}
/*
*功能:设置I/O为非阻塞模式
*fd:文件描述符
*/
void activate_nonblock(int fd)
{
int ret;
int flags = fcntl(fd, F_GETFL);
if(flags == -1)
ERR_EXIT("fcntl");
flags |= O_NONBLOCK;//
ret = fcntl(fd, F_SETFL, flags);
if(ret == -1)
ERR_EXIT("fcntl");
}
/*
*功能:设置I/O为阻塞模式
*fd:文件描述符
*/
void deactivate_nonblock(int fd)
{
int ret;
int flags = fcntl(fd, F_GETFL);
if(flags == -1)
ERR_EXIT("fcntl");
flags &= ~O_NONBLOCK;
ret = fcntl(fd, F_SETFL, flags);
if(ret == -1)
ERR_EXIT("fcntl");
}
/*
*功能:读操作检测函数,不包含读操作
*fd:文件描述符
*wait_seconds:等待超时时间(s),如果为0表示不检测超时
*返回值:成功返回0,失败返回-1,超时返回-1,且errno = ETIMEDOUT
*/
int read_timeout(int fd, unsigned int wait_seconds)
{
int ret;
if(wait_seconds > 0)
{
fd_set read_fd;
FD_ZERO(&read_fd);
FD_SET(fd, &read_fd);
struct timeval timeout;
timeout.tv_sec = wait_seconds;
timeout.tv_usec = 0;
do
{
ret = select(fd + 1, &read_fd, NULL, NULL, &timeout);
}while(ret < 0 && errno == EINTR);
if(ret == 0)
{
ret = -1;
errno = ETIMEDOUT;//ETIMEDOUT--connection timed out(POSIX.1)
}
else if(ret == 1)
ret = 0;
}
return ret;
}
/*
*功能:写超时检测函数,不包含写操作
*fd:文件描述符
*wait_seconds:等待超时时间(秒),如果为0表示不检测超时
*返回值:成功返回0,失败返回-1,超时返回-1,且errno = ETIMEDOUT
*/
int write_timeout(int fd, unsigned int wait_seconds)
{
int ret;
if(wait_seconds > 0)
{
fd_set write_fd;
FD_ZERO(&write_fd);
FD_SET(fd, &write_fd);
struct timeval timeout;
timeout.tv_sec = wait_seconds;
timeout.tv_usec = 0;
do
{
ret = select(fd + 1, NULL, &write_fd, NULL, &timeout);
}while(ret < 0 && errno == EINTR);
if(ret == 0)
{
ret = -1;
errno = ETIMEDOUT;//ETIMEDOUT--connection timed out(POSIX.1)
}
else if(ret == 1)
ret = 0;
}
return ret;
}
/*
*功能:有超时功能的accept
*fd:套接字
*addr:输出参数,返回对方地址
*wait_seconds:等待超时时间,0表示正常不带有超时功能
*返回值:成功(未超时)返回已连接套接字,超时返回-1,并且errno = ETIMEDOUT
*/
int accept_timeout(int fd, struct sockaddr_in *addr, unsigned int wait_seconds)
{
int ret;
socklen_t addrlen = sizeof(struct sockaddr_in);
if(wait_seconds > 0)
{
fd_set accept_fd;
FD_ZERO(&accept_fd);
FD_SET(fd, &accept_fd);
struct timeval timeout;
timeout.tv_sec = wait_seconds;
timeout.tv_usec = 0;
do
{
ret = select(fd + 1, &accept_fd, NULL, NULL, &timeout);
}while(ret < 0 && errno == EINTR);
if(ret == -1)
return -1;
else if(ret == 0)
{
errno = ETIMEDOUT;
return -1;
}
}
if(addr != NULL)
ret = accept(fd, (struct sockaddr*)addr, &addrlen);
else
ret = accept(fd, NULL, NULL);
return ret;
}
/*
*功能:有超时功能的connect
*fd: 套接字
*addr: 要连接的对方地址
*wait_seconds: 等待的超时时间(秒), 0表示为正常没有超时设置
*返回值:成功(未超时)返回0, 失败返回-1, 超时返回-1并且errno = ETIMEDOUT
*/
//???????????????
int connect_timeout(int fd, struct sockaddr_in *addr, unsigned int wait_seconds)
{
int ret;
socklen_t addrlen = sizeof(struct sockaddr_in);
if(wait_seconds > 0)
activate_nonblock(fd);
ret = connect(fd, (struct sockaddr*)addr, addrlen);
printf("ret %d\n", ret);
if(ret < 0 && errno == EINPROGRESS)
{
fd_set connect_fd;
FD_ZERO(&connect_fd);
FD_SET(fd, &connect_fd);
struct timeval timeout;
timeout.tv_sec = wait_seconds;
timeout.tv_usec = 0;
do
{
printf("a\n");
/*一旦连接建立,套接字就可写*/
ret = select(fd + 1, NULL, &connect_fd, NULL, &timeout);
printf("ret - %d\n", ret);
}while(ret < 0 && errno == EINTR);
if(ret == 0)
{
ret = -1;
errno = ETIMEDOUT;
}
else if(ret < 0)
return -1;
else if(ret == 1)
{
/*
*ret返回为1
*可能有两种情况:一种是连接建立成功,一种是套接字产生错误
*此时错误信息不会保存至errno变量中,因此,需要调用getsockopt来获取
*/
int err;
socklen_t socklen = sizeof(err);
int sockoptret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &socklen);
if(sockoptret == -1)
{
return -1;
}
//else if(err == 0)
if(err == 0)//这里
{
ret = 0;
}
else
{
errno = err;
ret = -1;
}
}
}
if(wait_seconds > 0)
{
deactivate_nonblock(fd);
}
return ret;
}
/*
*功能:读取固定的字节数
*fd:文件描述符
*buf:接受数据的缓冲区
*n:要读取的字节数
*返回值:成功返回n,失败返回-1,读到EOF返回小于n的值
*/
ssize_t readn(int fd, void *buf, size_t n)
{
size_t nleft = n;
ssize_t nread;
char *bufp = (char*)buf;
while(nleft > 0)
{
if((nread = read(fd, bufp, nleft)) < 0)//这里
{
if(errno == EINTR)
continue;
return -1;
}
else if(nread == 0)
return n - nleft;
bufp += nread;
nleft -= nread;
}
return n;
}
/*
*功能:发送固定的字节数
*fd:文件描述符
*buf:发送数据的缓冲区
*n:要发送的字节数
*返回值:成功返回n,失败返回-1
*/
ssize_t writen(int fd, const void *buf, size_t n)
{
size_t nleft = n;
ssize_t nwrite;
char *bufp = (char*)buf;
while(nleft > 0)
{
if((nwrite = write(fd, bufp, nleft)) < 0)//这里
{
if (errno == EINTR)
continue;
return -1;
}
else if (nwrite == 0)
continue;
bufp += nwrite;
nleft -= nwrite;
}
return n;
}
/*
*功能:查看套接字缓冲区数据,但不移除数据
*sockfd:套接字
*buf:接受数据缓冲区
*len: 接受字节数长度
*返回值:成功一个返回大于零(即从recv获得的字节数)的值,失败返回-1
*/
static ssize_t recv_peek(int sockfd, void *buf, size_t len)
{
int nread;
while (1)
{
//这个过程只成功调用一次
nread = recv(sockfd, buf, len, MSG_PEEK);
if (nread < 0 && errno == EINTR)//这里
{
//被中断则继续读取
continue;
}
if(nread < 0)
{
return -1;
}
break;
}
return nread;
}
/*
*功能:按行读取数据
*sockfd:套接字
*buf:接受数据的缓冲区
*maxsize: 一行的最大长度
*返回值:成功返回大于等于零的值, 失败返回-1
*/
ssize_t readline(int sockfd, void *buf, size_t maxsize)
{
int nread; //一次IO读取的数量
int nleft; //还剩余的字节数
char *ptr; //存放数据的指针的位置
int ret; //readn的返回值
int total = 0; //目前总共读取的字节数
nleft = maxsize-1;
ptr = buf;
while (nleft > 0) {
//这一次调用仅仅是预览数据
//并没有真的把数据从缓冲区中取走
ret = recv_peek(sockfd, ptr, nleft);
//注意这里读取的字节不够,绝对不是错> 误!!!
if(ret <= 0)
{
return ret;
}
nread = ret;
int i;
for(i = 0; i < nread; ++i)
{
if (ptr[i] == '\n')
{
//这里才是真正的读取过程
ret = readn(sockfd, ptr, i + 1);
if (ret != i + 1)
{
return -1;
}
total += ret;
ptr += ret;
*ptr = 0;
return total; //返回此行的> 长度 '\n'包含在其中
}
}
//如果没有发现\n,这些数据应全部接收
ret = readn(sockfd, ptr, nread);
if(ret != nread)
{
return -1;
}
nleft -= nread;
total += nread;
ptr += nread;
}
*ptr = 0;
return maxsize - 1;
}
/*
*功能:
*sockfd:
*fd:
*/
void send_fd(int sockfd, int fd)
{
int ret;
struct msghdr msg;
struct cmsghdr *p_cmsg;
struct iovec vec;
char cmsgbuf[CMSG_SPACE(sizeof(fd))];//这里用到宏
int *p_fds;
char sendchar = 0;
msg.msg_control = cmsgbuf;//这里用到cmsgbuf
msg.msg_controllen = sizeof(cmsgbuf);
p_cmsg = CMSG_FIRSTHDR(&msg);
p_cmsg->cmsg_level = SOL_SOCKET;
p_cmsg->cmsg_type = SCM_RIGHTS;
p_cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
p_fds = (int*)CMSG_DATA(p_cmsg);
*p_fds = fd;
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = &vec;//这里用到vec
msg.msg_iovlen = 1;
msg.msg_flags = 0;
vec.iov_base = &sendchar;
vec.iov_len = sizeof(sendchar);
ret = sendmsg(sockfd, &msg, 0);//这里用到msg
if(ret != 1)
ERR_EXIT("sendmsg");
}
/*
*功能:
*sockfd:
*返回值:
*/
int recv_fd(const int sockfd)
{
int ret;
struct msghdr msg;
char recvchar;
struct iovec vec;
int recvfd;
char cmsgbuf[CMSG_SPACE(sizeof(recvfd))];
struct cmsghdr *p_cmsg;
int *p_fd;
vec.iov_base = &recvchar;
vec.iov_len = sizeof(recvchar);
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = &vec;
msg.msg_iovlen = 1;
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
msg.msg_flags = 0;
p_fd = (int*)CMSG_DATA(CMSG_FIRSTHDR(&msg));
*p_fd = -1;
ret = recvmsg(sockfd, &msg, 0);
if(ret != 1)
ERR_EXIT("recvmsg");
p_cmsg = CMSG_FIRSTHDR(&msg);
if(p_cmsg == NULL)
ERR_EXIT("no passed fd");
p_fd = (int *)CMSG_DATA(p_cmsg);
recvfd = *p_fd;
if(recvfd == -1)
ERR_EXIT("no [assed fd");
return recvfd;
}