-
Notifications
You must be signed in to change notification settings - Fork 2
/
receiver.cc
201 lines (172 loc) · 3.78 KB
/
receiver.cc
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
/*
* This end is the receiver
*
* Accepts connections from the sender,
* For each connection
* Validate given password, send OK, receive and store the file
*/
#include <iostream>
#ifdef _MSC_VER
#include <winsock.h>
#include <direct.h>
#else
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <endian.h>
#endif
#include <errno.h>
#include <udt/udt.h>
#include "copy.h"
using namespace std;
using namespace UDT;
#ifdef _MSC_VER
typedef unsigned short in_port_t;
#endif
#ifndef htonll
extern "C" {
static uint64_t htonll(uint64_t n);
static uint64_t ntohll(uint64_t n);
}
#endif
int
main(void)
{
const struct protoent *proto = getprotobyname("tcp");
UDTSOCKET sock;
if((sock = UDT::socket(PF_INET, SOCK_STREAM, proto ? proto->p_proto : 0)) == UDT::ERROR) {
perror("socket");
return errno;
}
struct sockaddr_in sin;
memset(&sin, '\0', sizeof(struct sockaddr_in));
sin.sin_addr.s_addr = htonl(INADDR_ANY);
const struct servent *service = getservbyname("udt-copy", "tcp");
sin.sin_port = (in_port_t)htons(service ? service->s_port : PORT);
sin.sin_family = AF_INET;
if(UDT::bind(sock, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == UDT::ERROR) {
perror("bind");
return errno;
}
if(UDT::listen(sock, 10) == UDT::ERROR) {
perror("listen");
return errno;
}
switch(fork()) {
case -1:
perror("fork");
return errno;
case 0:
break;
default:
return 0;
}
#ifdef _MSC_VER
if(getenv("TEMP")) {
const char *tmp = getenv("TEMP");
if(_chdir(tmp) < 0)
perror(tmp);
}
#else
endprotoent();
endservent();
if(getenv("TMPDIR")) {
const char *tmp = getenv("TMPDIR");
if(chdir(tmp) < 0)
perror(tmp);
else if(chroot(tmp) < 0)
perror(tmp);
}
if(getuid() == 0) {
setgid(99);
setuid(99);
}
#endif
for(;;) {
int len = sizeof(struct sockaddr_in);
UDTSOCKET s = UDT::accept(sock, (struct sockaddr *)&sin, &len);
if(s == UDT::ERROR) {
perror("accept");
return errno;
}
struct request request;
#ifdef _MSC_VER
size_t nbytes = UDT::recv(s, (char *)&request, sizeof(struct request), 0);
#else
ssize_t nbytes = UDT::recv(s, (char *)&request, sizeof(struct request), 0);
#endif
if(nbytes != sizeof(struct request)) {
if(nbytes == UDT::ERROR)
perror("recv");
else {
fputs("Bad request record received\n", stderr);
UDT::send(s, "Bad request", 12, 0);
}
UDT::close(s);
continue;
}
/* FIXME: authenticate the password */
if(UDT::send(s, "OK", 3, 0) != 3) {
perror("send");
UDT::close(s);
continue;
}
#ifdef SHUT_WR
shutdown(s, SHUT_WR);
#endif
request.r_len = ntohll(request.r_len);
/*request.r_rlen = ntohll(request.r_rlen);*/
printf("receiving %s, %llu bytes\n",
request.r_filename, (long long unsigned int)request.r_len);
if(request.r_len) {
fstream ofs(request.r_filename, ios::out|ios::binary|ios::trunc);
if((!ofs) || ofs.bad()) {
perror(request.r_filename);
UDT::close(s);
continue;
}
int64_t offset = 0;
if(UDT::recvfile(s, ofs, offset, request.r_len) == UDT::ERROR) {
cerr << request.r_filename << ": " <<
UDT::getlasterror().getErrorMessage() <<
'\n';
UDT::close(s);
continue;
}
}
UDT::close(s);
/* Also get resource fork if needed */
/*
QFileInfo fiRsrc(fi->absFilePath() + ".rsrc");
if (fiRsrc.exists()) {
addFileEntry(fiRsrc.absFilePath(), strBasePath + "/" + dir.dirName());
}
*/
}
return 0;
}
#ifndef htonll
extern "C" {
static uint64_t
htonll(uint64_t n)
{
#if __BYTE_ORDER == __BIG_ENDIAN
return n;
#else
return (((uint64_t)htonl(n)) << 32) + htonl(n >> 32);
#endif
}
static uint64_t
ntohll(uint64_t n)
{
#if __BYTE_ORDER == __BIG_ENDIAN
return n;
#else
return (((uint64_t)ntohl(n)) << 32) + ntohl(n >> 32);
#endif
}
}
#endif