-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsender.cc
196 lines (163 loc) · 3.9 KB
/
sender.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
/*
* This end is the sender. It opens a named pipe from which it receives
* full paths of files to be sent. The named pipe is given as an argument
* to this program.
*/
#include <iostream>
#include <string>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <memory.h>
#include <netdb.h>
#include <udt/udt.h>
#include <endian.h>
using namespace std;
#include "copy.h"
#ifndef htonll
extern "C" {
static uint64_t htonll(uint64_t n);
// static uint64_t ntohll(uint64_t n);
}
#endif
int
main(int argc, char **argv)
{
if(argc != 2) {
fputs("Arg count\n", stderr);
return 1;
}
/*
* Open the named pipe which receives requests
*/
int fin = open(argv[1], O_RDONLY);
if(fin < 0) {
perror(argv[1]);
return errno;
}
switch(fork()) {
case -1:
perror("fork");
return errno;
case 0:
break;
default:
return 0;
}
struct queue q;
const struct protoent *proto = getprotobyname("tcp");
const struct servent *service = getservbyname("udt-copy", "tcp");
int nbytes;
while((nbytes = read(fin, &q, sizeof(struct queue))) >= 0) {
if(nbytes == 0) {
close(fin);
fin = open(argv[1], O_RDONLY);
if(fin < 0) {
perror(argv[1]);
return errno;
}
continue;
}
char *ptr = strrchr(q.q_filename, '/');
if(ptr == NULL) {
cerr << argv[0] << ": filenames must be absolute " <<
q.q_filename << endl;
continue;
}
struct stat statb;
if(stat(q.q_filename, &statb) < 0) {
perror(q.q_filename);
continue;
}
/*
* security risk, race condition:
* The file could now be replaced, but since
* UDT needs C++, and ifstream doesn't allow us to get to the
* file descriptor, the use of fstat() is not possible :-(
*/
fstream ifs(q.q_filename, ios::in|ifstream::binary);
if((!ifs) || ifs.fail()) {
perror(q.q_filename);
continue;
}
sockaddr_in sin;
memset(&sin, '\0', sizeof(struct sockaddr_in));
sin.sin_family = AF_INET;
sin.sin_port = (in_port_t)htons(service ? service->s_port : PORT);
in_addr_t ip = inet_addr(q.q_hostname);
if(ip == INADDR_NONE) {
const struct hostent *h = gethostbyname(q.q_hostname);
if(h == NULL) {
cerr << "Unknown host " << q.q_hostname << endl;
continue;
}
memcpy((char *)&ip, h->h_addr, sizeof(ip));
}
sin.sin_addr.s_addr = ip;
UDTSOCKET s = UDT::socket(AF_INET, SOCK_STREAM, proto ? proto->p_proto : 0);
if(s < 0) {
perror("UDT::socket");
break;
}
if(UDT::connect(s, (sockaddr *)&sin, sizeof(struct sockaddr)) == UDT::ERROR) {
perror(q.q_hostname);
UDT::close(s);
continue;
}
struct request request;
strcpy(request.r_password, "ignored");
strcpy(request.r_filename, ++ptr);
int64_t rlen = (int64_t)statb.st_size;
request.r_len = htonll(rlen);
if(UDT::send(s, (const char *)&request, sizeof(request), 0) == UDT::ERROR) {
cerr << "send" << ": " <<
UDT::getlasterror().getErrorMessage() << '\n';
UDT::close(s);
continue;
}
char status[24];
if(UDT::recv(s, status, sizeof(status) - 1, 0) != 3) {
cerr << q.q_hostname << " rejected the request " <<
status << endl;
UDT::close(s);
continue;
}
shutdown(s, SHUT_RD);
printf("sending %s, %lld bytes\n", request.r_filename, (long long int)rlen);
int64_t offset = 0;
if(UDT::sendfile(s, ifs, offset, rlen) == UDT::ERROR)
cerr << request.r_filename << ": " <<
UDT::getlasterror().getErrorMessage() << '\n';
UDT::close(s);
}
return close(fin);
}
#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