-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdiffd.c
255 lines (205 loc) · 6.54 KB
/
rdiffd.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
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
#include <getopt.h>
#include <arpa/inet.h>
#include "fs.h"
#include "mem.h"
#include "num.h"
#include "stat.h"
#include "str.h"
#include "time.h"
#include "vec.h"
#include "lsdir.h"
#include "ai.h"
#include "net.h"
int main(int argc, char *argv[]) {
bool detach = false;
bool safe = false;
bool keep = false;
bool ipv4 = false;
bool ipv6 = false;
char *rootdir = NULL;
char *host = NULL;
char *port = NULL;
int net_fam = AF_UNSPEC;
/* socket fds */
AutoSock lfd = -1;
AutoSock cfd = -1;
if (argc <= 1) {
usage:
errx(EX_USAGE, "usage: rdiffd [-46dsk] -r DIR [-a HOST] -p PORT");
}
for (int ch = 0; (ch = getopt(argc, argv, ":46dskr:a:p:")) != -1;) {
switch (ch) {
case '4':
if (ipv4) warnx("multiple -4 flags have no extra effect");
if (ipv6) errx(EX_USAGE, "flags -4 and -6 are incompatible");
ipv4 = true;
break;
case '6':
if (ipv6) warnx("multiple -6 flags have no extra effect");
if (ipv4) errx(EX_USAGE, "flags -6 and -4 are incompatible");
ipv6 = true;
break;
case 'd':
if (detach) warnx("multiple -d flags have no extra effect");
detach = true;
break;
case 's':
if (safe) warnx("multiple -s flags have no extra effect");
safe = true;
break;
case 'k':
if (keep) warnx("multiple -k flags have no extra effect");
keep = true;
break;
case 'r':
if (rootdir) errx(EX_USAGE, "multiple -r arguments are not supported");
rootdir = optarg;
break;
case 'a':
if (host) errx(EX_USAGE, "multiple -a arguments are not supported");
host = optarg;
break;
case 'p':
if (port) errx(EX_USAGE, "multiple -p arguments are not supported");
port = optarg;
break;
case 'h': default: goto usage;
}
}
if (!rootdir || !port)
goto usage;
assert(!(ipv4 && ipv6));
net_fam = ipv4 ? AF_INET : ipv6 ? AF_INET6 : AF_UNSPEC;
{ /* print local hostname */
char hostname[HOST_NAME_MAX + 1];
if (gethostname(hostname, sizeof(hostname)) == -1)
warn("cannot get local hostname");
else
warnx("hostname: %s", hostname);
}
{ /* TCP listen */
AutoAddrInfo *ai = NULL;
const AddrInfo *p = NULL;
int gai_err = xgetai(host, port, net_fam, SOCK_STREAM, AI_PASSIVE, &ai);
if (gai_err != 0) {
assert(ai == NULL);
assert(gai_err != EAI_BADFLAGS);
assert(gai_err != EAI_SOCKTYPE);
const char fmt[] = "cannot get local address info";
if (gai_err == EAI_SYSTEM)
warn("%s for %s:%s", fmt, host, port);
else
warnx("%s for %s:%s: %s", fmt, host, port, gai_strerror(gai_err));
return (EX_OSERR);
}
assert(ai != NULL);
for (p = ai; p != NULL; p = p->ai_next) {
if ((lfd = xbindlisten(p)) == -1)
ai_print("skip", p->ai_family, p->ai_addr, port, true);
else
break;
}
assert((lfd == -1 && p == NULL) || (lfd != -1 && p != NULL));
if (lfd == -1) {
const char *const _host = host == NULL ? "*" : host;
warnx("cannot listen on port %s (on host %s)", port, _host);
return (EX_OSERR);
}
ai_print("listen on", p->ai_family, p->ai_addr, port, false);
}
if (detach) { /* detach process */
pid_t pid = fork();
switch (pid) {
case -1: err(EX_OSERR, "cannot detach/fork process");
case 0: break; /* child */
default: /* parent */
warnx("forked child %d", pid);
return (EX_OK);
}
}
{ /* start loading directory */
AutoVec dlist = vec_init(sizeof(char), 2048, NULL);
AutoStr estr = NULL;
LsDir lsdir_info = {
.name = rootdir,
.res = &dlist,
.nents = 0,
.estr = &estr,
.sys_errno = 0
};
AutoThread *lsdir_thread = (Thread *) &lsdir_info;
if (thread_init(lsdir_thread, (ThreadCB) lsdir, &lsdir_info))
warnx("started loading %s", rootdir);
{ /* accept TCP connection */
typedef struct sockaddr_storage SockAddrStorage;
SockAddrStorage addr;
socklen_t addrlen = 0;
int gni_err = 0;
char chn[NI_MAXHOST + 1];
retry:
addrlen = sizeof(SockAddrStorage);
warnx("waiting to accept connection...");
if ((cfd = accept(lfd, (SockAddr *) &addr, &addrlen)) == -1 &&
(errno == ENETDOWN || errno == ENOPROTOOPT || errno == EPROTO ||
errno == EHOSTDOWN || errno == EHOSTUNREACH || errno == ENONET ||
errno == EOPNOTSUPP || errno == ENETUNREACH)) {
warnx("%s, trying again...", strerror(errno));
goto retry;
} else if (cfd == -1) {
warn("could not accept connection");
return (EX_NOHOST);
}
ai_print("accepted connection from", addr.ss_family,
(SockAddr *) &addr, "*", false);
sock_close(&lfd);
lfd = -1;
addrlen = sizeof(SockAddrStorage);
if ((gni_err = getnameinfo((SockAddr *) &addr, addrlen,
chn, sizeof(chn), NULL, 0,
NI_NAMEREQD)) != 0) {
assert(gni_err != EAI_BADFLAGS);
assert(gni_err != EAI_FAMILY);
assert(gni_err != EAI_OVERFLOW);
warnx("cannot get client hostname: %s", gai_strerror(gni_err));
} else {
warnx("client hostname: %s", chn);
}
}
if (!lsdir_thread->thread_valid)
warnx("loading %s", rootdir);
if (thread_result(lsdir_thread) == NULL) {
errno = lsdir_info.sys_errno;
if (lsdir_info.sys_errno == ENOMEM ||
lsdir_info.sys_errno == EOVERFLOW)
warn("cannot load %s", rootdir);
else
warn("cannot load %s", estr);
return (EX_OSERR);
}
warnx("finished loading %s", rootdir);
{
AutoStr hsize = humansize(dlist.len);
if (hsize != NULL) {
warnx("loaded %zu entries (%s) in " TI_FMT,
lsdir_info.nents, hsize, TI_FMT_PARAMS(lsdir_info.ti));
} else {
warnx("loaded %zu entries (%zu bytes) in " TI_FMT,
lsdir_info.nents, dlist.len, TI_FMT_PARAMS(lsdir_info.ti));
}
}
warnx("sending directory listing...");
if (!netsend(cfd, lsdir_info.res->ptr, lsdir_info.res->len)) {
warn("error sending directory listing");
return (EX_OSERR);
}
}
return (EX_OK);
}