forked from folkertvanheusden/omnisync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
315 lines (250 loc) · 5.36 KB
/
utils.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
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include "error.h"
#include "gen.h"
#include "log.h"
#define incopy(a) *((struct in_addr *)a)
int resolve_host(char *host, struct sockaddr_in *addr)
{
struct hostent *hostdnsentries;
hostdnsentries = gethostbyname(host);
if (hostdnsentries == NULL)
{
switch(h_errno)
{
case HOST_NOT_FOUND:
dolog(LOG_ERR, "The specified host is unknown.\n");
break;
case NO_ADDRESS:
dolog(LOG_ERR, "The requested name is valid but does not have an IP address.\n");
break;
case NO_RECOVERY:
dolog(LOG_ERR, "A non-recoverable name server error occurred.\n");
break;
case TRY_AGAIN:
dolog(LOG_ERR, "A temporary error occurred on an authoritative name server. Try again later.\n");
break;
default:
dolog(LOG_ERR, "Could not resolve %s for an unknown reason (%d)\n", host, h_errno);
}
return -1;
}
/* create address structure */
addr -> sin_family = hostdnsentries -> h_addrtype;
addr -> sin_addr = incopy(hostdnsentries -> h_addr_list[0]);
return 0;
}
int set_tcp_low_latency(int sock)
{
int flag = 1;
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)) < 0)
dolog(LOG_ERR, "could not set TCP_NODELAY on socket");
return 0;
}
int bind_socket_to_address(int fd, char *bindto_in)
{
char *temp = strdup(bindto_in);
struct sockaddr_in from;
char *colon = strchr(temp, ':');
int port = 0;
if (colon)
{
*colon = 0x00;
port = atoi(colon + 1);
}
if (inet_aton(temp, &from.sin_addr) == 0)
{
dolog(LOG_ERR, "bind_socket_to_address: failed converting address");
free(temp);
return -1;
}
from.sin_family = AF_INET;
from.sin_port = htons(port);
if (bind(fd, (struct sockaddr *)&from, sizeof(from)) == -1)
{
dolog(LOG_ERR, "bind_socket_to_address: Cannot bind socket");
free(temp);
return -1;
}
free(temp);
return 0;
}
int connect_to(char *bindto, char *host, int portnr)
{
int fd;
struct sockaddr_in addr;
int keep_alive = 1;
/* resolve */
memset(&addr, 0x00, sizeof(addr));
resolve_host(host, &addr);
addr.sin_port = htons(portnr);
/* connect */
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
dolog(LOG_ERR, "connect_to: problem creating socket");
return -1;
}
if (bind_socket_to_address(fd, bindto) == -1)
{
dolog(LOG_ERR, "connect_to: failed to bind socket");
return -1;
}
if (set_tcp_low_latency(fd) == -1)
{
dolog(LOG_ERR, "connect_to: problem setting low latency on socket");
close(fd);
return -1;
}
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&keep_alive, sizeof(keep_alive)) == -1)
{
dolog(LOG_ERR, "connect_to: problem setting KEEPALIVE");
close(fd);
return -1;
}
/* connect to peer */
if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == 0)
{
/* connection made, return */
return fd;
}
close(fd);
return -1;
}
int udp_socket(char *bindto)
{
int fd = socket(PF_INET, SOCK_DGRAM, 0);
if (bind_socket_to_address(fd, bindto) == -1)
{
dolog(LOG_ERR, "udp_socket: failed to bind socket");
return -1;
}
return fd;
}
ssize_t READ(int fd, char *whereto, size_t len)
{
ssize_t cnt=0;
while(len>0)
{
ssize_t rc;
rc = read(fd, whereto, len);
if (rc == -1)
{
if (errno != EINTR && errno != EAGAIN)
error_exit("READ failed");
}
else if (rc == 0)
{
break;
}
else
{
whereto += rc;
len -= rc;
cnt += rc;
}
}
return cnt;
}
ssize_t WRITE(int fd, char *whereto, size_t len)
{
ssize_t cnt=0;
while(len>0)
{
ssize_t rc;
rc = write(fd, whereto, len);
if (rc == -1)
{
if (errno != EINTR && errno != EINPROGRESS && errno != EAGAIN)
error_exit("WRITE failed");
}
else if (rc == 0)
{
return -1;
}
else
{
whereto += rc;
len -= rc;
cnt += rc;
}
}
return cnt;
}
int write_pidfile(char *fname)
{
FILE *fh = fopen(fname, "w");
if (!fh)
error_exit("write_pidfile::fopen: failed creating file %s", fname);
fprintf(fh, "%i", getpid());
fclose(fh);
return 0;
}
char * mystrdup(char *in)
{
char *copy = strdup(in);
if (!copy)
error_exit("mystrdup: cannot duplicate string - out of memory?");
return copy;
}
int find_string_offset(char *str, char *what)
{
char *dummy = strstr(str, what);
if (!dummy)
return -1;
return (int)(dummy - str);
}
void * myrealloc(void *what, int new_len)
{
void * newp = realloc(what, new_len);
if (!newp)
error_exit("myrealloc: failed to grow memory block %p to %d bytes", what, new_len);
return newp;
}
void mysleep(int sleep_left)
{
do
{
sleep_left = sleep(sleep_left);
if (sleep_left > 0 && verbose > 1)
dolog(LOG_DEBUG, "Early return from sleep (%d) seconds left", sleep_left);
}
while(sleep_left > 0);
}
int wait_for_socket(int fd, double timeout)
{
for(;;)
{
int rc;
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = (int)timeout;
tv.tv_usec = (int)((timeout - (double)tv.tv_sec) * 1000000.0);
rc = select(fd + 1, &rfds, NULL, NULL, &tv);
if (rc == -1)
{
if (errno == EINTR || errno == EAGAIN)
continue;
if (errno == EBADF)
return -1;
error_exit("wait_for_socket: select() failed");
}
if (FD_ISSET(fd, &rfds))
break;
return -1;
}
return 0;
}