-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutil.cpp
More file actions
213 lines (183 loc) · 4.79 KB
/
util.cpp
File metadata and controls
213 lines (183 loc) · 4.79 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
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
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <stddef.h>
#include <poll.h>
#include <sys/fcntl.h>
#ifdef __linux__
#include <linux/sockios.h> // SIOCOUTQ
#endif
double time_seconds(void)
{
struct timeval tval;
gettimeofday(&tval,NULL);
return tval.tv_sec + (tval.tv_usec*1.0e-6);
}
/*
open a UDP socket on the given port
*/
int open_socket_in_udp(int port)
{
struct sockaddr_in sock;
int res;
int one=1;
memset(&sock,0,sizeof(sock));
#ifdef HAVE_SOCK_SIN_LEN
sock.sin_len = sizeof(sock);
#endif
sock.sin_port = htons(port);
sock.sin_family = AF_INET;
res = socket(AF_INET, SOCK_DGRAM, 0);
if (res == -1) {
fprintf(stderr, "socket failed\n"); return -1;
return -1;
}
setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
if (bind(res, (struct sockaddr *)&sock, sizeof(sock)) < 0) {
return(-1);
}
return res;
}
/*
setup TCP options for a socket
*/
void set_tcp_options(int fd)
{
int one=1;
setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
setsockopt(fd, SOL_TCP, TCP_NODELAY, &one, sizeof(one));
}
/*
open a TCP socket on the given port
*/
int open_socket_in_tcp(int port)
{
struct sockaddr_in sock;
int res;
memset(&sock,0,sizeof(sock));
#ifdef HAVE_SOCK_SIN_LEN
sock.sin_len = sizeof(sock);
#endif
sock.sin_port = htons(port);
sock.sin_family = AF_INET;
res = socket(AF_INET, SOCK_STREAM, 0);
if (res == -1) {
fprintf(stderr, "socket failed\n"); return -1;
return -1;
}
set_tcp_options(res);
if (bind(res, (struct sockaddr *)&sock, sizeof(sock)) < 0) {
return(-1);
}
if (listen(res, 100) != 0) {
return(-1);
}
set_tcp_options(res);
return res;
}
/*
convert address to string, uses a static return buffer
*/
const char *addr_to_str(struct sockaddr_in &addr)
{
static char str[INET_ADDRSTRLEN+1];
inet_ntop(AF_INET, &addr.sin_addr, str, INET_ADDRSTRLEN);
return str;
}
/*
return time as a string, using a static buffer
*/
const char *time_string(void)
{
time_t t = time(nullptr);
struct tm *tm = localtime(&t);
static char str[100] {};
strftime(str, sizeof(str)-1, "%F %T", tm);
return str;
}
/*
Returns the number of bytes that can be written to fd without blocking.
On error returns -1 and sets errno.
*/
ssize_t tcp_writable_bytes(int fd)
{
int outq = 0; // bytes currently queued in the send buffer
#if defined(SIOCOUTQ)
if (ioctl(fd, SIOCOUTQ, &outq) == -1) {
return -1;
}
#else
# error "SIOCOUTQ not available on this platform"
#endif
int sndbuf = 0; // total size of the send buffer
socklen_t optlen = sizeof(sndbuf);
if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) == -1) {
return -1;
}
// Linux returns the kernel's accounting size for SO_SNDBUF (often doubled).
// The available space is the remainder; clamp at 0.
ssize_t avail = (ssize_t)sndbuf - (ssize_t)outq;
if (avail < 0) avail = 0;
return avail;
}
/*
return true if a TCP socket is dead
*/
bool socket_is_dead(int fd)
{
// Verify it's a stream socket (TCP).
int type = 0;
socklen_t tlen = sizeof(type);
if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &tlen) == -1) {
return true;
}
if (type != SOCK_STREAM) {
return true;
}
// Quick, non-blocking health probe.
struct pollfd pfd {};
pfd.fd = fd;
pfd.events = POLLOUT | POLLERR | POLLHUP | POLLNVAL;
int pr = poll(&pfd, 1, 0);
if (pr < 0) {
return true;
}
if (pr == 1 && (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))) {
return true;
}
// Check for a queued asynchronous error even if poll() looked fine.
// (This also catches connect() failures on nonblocking sockets.)
int soerr = 0; socklen_t slen = sizeof(soerr);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &soerr, &slen) == 0 && soerr != 0) {
return true;
}
#ifdef TCP_INFO
// Inspect TCP state. Consider only states that are writable as "alive".
struct tcp_info ti;
socklen_t tilen = sizeof(ti);
if (getsockopt(fd, IPPROTO_TCP, TCP_INFO, &ti, &tilen) == 0) {
// Writable in practice: ESTABLISHED, and often CLOSE_WAIT (peer sent FIN, but we can still write).
if (ti.tcpi_state != TCP_ESTABLISHED && ti.tcpi_state != TCP_CLOSE_WAIT) {
return true;
}
}
#endif
// looks healthy for writing
return false;
}
void set_nonblocking(int fd)
{
unsigned v = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, v | O_NONBLOCK);
}