forked from folkertvanheusden/omnisync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaytime.c
133 lines (108 loc) · 2.65 KB
/
daytime.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
#define _XOPEN_SOURCE
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include "gen.h"
#include "error.h"
#include "utils.h"
#include "utils2.h"
#include "log.h"
int daytime_to_time_t(char *in, double *out_ts)
{
struct tm stm;
memset(&stm, 0x00, sizeof(stm));
/* Mon Dec 31 12:07:40 2007 */
if (strptime(in, "%a %b %d %H:%M:%S %Y", &stm) == NULL)
{
dolog(LOG_INFO, "daytime_to_time_t/strptime: Error converting time-string '%s'", in);
return -1;
}
*out_ts = (double)mktime(&stm);
if (*out_ts == -1)
{
dolog(LOG_INFO, "daytime_to_time_t/mktime: Error converting time-string '%s'", in);
return -1;
}
return 0;
}
int daytime(char *bind_to, char *host, int host_port, double timeout, char mode, double *ts_start_recv, double *ts)
{
char io_buffer[128] = { 0 };
if (mode == IP_UDP)
{
socklen_t to_len;
struct sockaddr_in to;
int fd = udp_socket(bind_to);
if (fd == -1)
{
dolog(LOG_CRIT, "daytime/UDP: Cannot create UDP socket");
close(fd);
return -1;
}
if (resolve_host(host, &to) == -1)
{
dolog(LOG_ERR, "daytime/UDP: Cannot resolve host %s", host);
close(fd);
return -1;
}
to.sin_port = htons(host_port);
if (sendto(fd, "", 0, 0, (struct sockaddr *)&to, sizeof(to)) != 0)
{
dolog(LOG_ERR, "daytime/UDP: Cannot send UDP packet to %s:%d", host, host_port);
close(fd);
return -1;
}
if (wait_for_socket(fd, timeout) == -1)
{
dolog(LOG_DEBUG, "daytime/UDP: timeout");
return -1;
}
to_len = sizeof(to);
if (recvfrom(fd, io_buffer, sizeof(io_buffer), 0, (struct sockaddr *)&to, &to_len) <= 0)
{
dolog(LOG_ERR, "daytime/UDP: Cannot receive UDP packet from %s", host);
close(fd);
return -1;
}
*ts_start_recv = get_ts();
close(fd);
if (daytime_to_time_t(io_buffer, ts) == -1)
{
dolog(LOG_INFO, "daytime/UDP: error converting timestamp '%s'", io_buffer);
}
return 0;
}
else if (mode == IP_TCP)
{
int fd = connect_to(bind_to, host, host_port);
if (fd == -1)
{
dolog(LOG_ERR, "daytime/TCP: Failed to connect to %s:%d", host, host_port);
return -1;
}
if (wait_for_socket(fd, timeout) == -1)
{
dolog(LOG_DEBUG, "daytime/TCP: timeout");
close(fd);
return -1;
}
if (read(fd, (char *)io_buffer, sizeof(io_buffer)) <= 0)
{
dolog(LOG_ERR, "daytime/TCP: Error receiving data from %s", host);
close(fd);
return -1;
}
*ts_start_recv = get_ts();
close(fd);
if (daytime_to_time_t(io_buffer, ts) == -1)
{
dolog(LOG_INFO, "daytime/UDP: error converting timestamp '%s'", io_buffer);
}
return 0;
}
error_exit("daytime: invalid ip mode %d", mode);
return -1;
}