-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathudpr-negotiate.c
337 lines (289 loc) · 8.77 KB
/
udpr-negotiate.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include "log.h"
#include "socklib.h"
#include "udpcast.h"
#include "udpc-protoc.h"
#include "fifo.h"
#include "udp-receiver.h"
#include "util.h"
#include "produconsum.h"
#include "statistics.h"
#ifndef O_BINARY
# define O_BINARY 0
#endif
#ifndef O_SYNC
# define O_SYNC 0
#endif
#ifndef O_TRUNC
# define O_TRUNC 0
#endif
static int sendConnectReq(struct client_config *client_config,
struct net_config *net_config,
int haveServerAddress) {
struct connectReq connectReq;
if(net_config->flags & FLAG_PASSIVE)
return 0;
connectReq.opCode = htons(CMD_CONNECT_REQ);
connectReq.reserved = 0;
connectReq.capabilities = htonl(RECEIVER_CAPABILITIES);
connectReq.rcvbuf = htonl(getRcvBuf(client_config->S_UCAST));
if(haveServerAddress)
return SSEND(connectReq);
else
return BCAST_CONTROL(client_config->S_UCAST, connectReq);
}
int sendGo(struct client_config *client_config) {
struct go go;
go.opCode = htons(CMD_GO);
go.reserved = 0;
return SSEND(go);
}
struct client_config *global_client_config=NULL;
static void fixConsole(void) {
if(global_client_config)
restoreConsole(&global_client_config->console,0);
}
static void sendDisconnectWrapper(void) {
if(global_client_config)
sendDisconnect(0, global_client_config);
}
void sendDisconnect(int exitStatus,
struct client_config *client_config) {
struct disconnect disconnect;
disconnect.opCode = htons(CMD_DISCONNECT);
disconnect.reserved = 0;
SSEND(disconnect);
if (exitStatus == 0)
udpc_flprintf("Transfer complete.\007\n");
}
struct startTransferArgs {
int fd;
int pipeFd;
struct client_config *client_config;
int doWarn;
};
static int openOutFile(struct disk_config *disk_config)
{
int outFile=1;
if(disk_config->fileName != NULL) {
int oflags = O_CREAT | O_WRONLY | O_TRUNC;
if((disk_config->flags & FLAG_SYNC)) {
oflags |= O_SYNC;
}
outFile = open(disk_config->fileName, oflags | O_BINARY, 0644);
if(outFile < 0) {
#ifdef NO_BB
#ifndef errno
extern int errno;
#endif
#endif
udpc_fatal(1, "open outfile %s: %s\n",
disk_config->fileName, strerror(errno));
}
} else {
#ifdef __MINGW32__
_setmode(1, O_BINARY);
#endif
}
return outFile;
}
int startReceiver(int doWarn,
struct disk_config *disk_config,
struct net_config *net_config,
struct stat_config *stat_config,
const char *ifName)
{
char ipBuffer[16];
union serverControlMsg Msg;
int connectReqSent=0;
struct client_config client_config;
int outFile=1;
int pipedOutFile;
struct sockaddr_in myIp;
int pipePid = 0;
int origOutFile;
int haveServerAddress;
int ret=0;
client_config.sender_is_newgen = 0;
net_config->net_if = getNetIf(ifName);
zeroSockArray(client_config.socks, NR_CLIENT_SOCKS);
client_config.S_UCAST = makeSocket(ADDR_TYPE_UCAST,
net_config->net_if,
0, RECEIVER_PORT(net_config->portBase));
client_config.S_BCAST = makeSocket(ADDR_TYPE_BCAST,
net_config->net_if,
0, RECEIVER_PORT(net_config->portBase));
if(net_config->ttl == 1 && net_config->mcastRdv == NULL) {
getBroadCastAddress(net_config->net_if,
&net_config->controlMcastAddr,
SENDER_PORT(net_config->portBase));
setSocketToBroadcast(client_config.S_UCAST);
} else {
getMcastAllAddress(&net_config->controlMcastAddr,
net_config->mcastRdv,
SENDER_PORT(net_config->portBase));
if(isMcastAddress(&net_config->controlMcastAddr)) {
setMcastDestination(client_config.S_UCAST, net_config->net_if,
&net_config->controlMcastAddr);
setTtl(client_config.S_UCAST, net_config->ttl);
client_config.S_MCAST_CTRL =
makeSocket(ADDR_TYPE_MCAST,
net_config->net_if,
&net_config->controlMcastAddr,
RECEIVER_PORT(net_config->portBase));
// TODO: subscribe address as receiver to!
}
}
clearIp(&net_config->dataMcastAddr);
udpc_flprintf("%sUDP receiver for %s at ",
disk_config->pipeName == NULL ? "" : "Compressed ",
disk_config->fileName == NULL ? "(stdout)":disk_config->fileName);
printMyIp(net_config->net_if);
udpc_flprintf(" on %s\n", net_config->net_if->name);
connectReqSent = 0;
haveServerAddress = 0;
client_config.clientNumber= 0; /*default number for asynchronous transfer*/
while(1) {
// int len;
int msglen;
int sock;
if (!connectReqSent) {
if (sendConnectReq(&client_config, net_config,
haveServerAddress) < 0) {
perror("sendto to locate server");
}
connectReqSent = 1;
}
haveServerAddress=0;
sock = udpc_selectSock(client_config.socks, NR_CLIENT_SOCKS,
net_config->startTimeout);
if(sock < 0) {
return -1;
}
// len = sizeof(server);
msglen=RECV(sock,
Msg, client_config.serverAddr, net_config->portBase);
if (msglen < 0) {
perror("recvfrom to locate server");
exit(1);
}
if(getPort(&client_config.serverAddr) !=
SENDER_PORT(net_config->portBase))
/* not from the right port */
continue;
switch(ntohs(Msg.opCode)) {
case CMD_CONNECT_REPLY:
client_config.clientNumber = ntohl(Msg.connectReply.clNr);
net_config->blockSize = ntohl(Msg.connectReply.blockSize);
udpc_flprintf("received message, cap=%08lx\n",
(long) ntohl(Msg.connectReply.capabilities));
if(ntohl(Msg.connectReply.capabilities) & CAP_NEW_GEN) {
client_config.sender_is_newgen = 1;
copyFromMessage(&net_config->dataMcastAddr,
Msg.connectReply.mcastAddr);
}
if (client_config.clientNumber == -1) {
udpc_fatal(1, "Too many clients already connected\n");
}
goto break_loop;
case CMD_HELLO_STREAMING:
case CMD_HELLO_NEW:
case CMD_HELLO:
connectReqSent = 0;
if(ntohs(Msg.opCode) == CMD_HELLO_STREAMING)
net_config->flags |= FLAG_STREAMING;
if(ntohl(Msg.hello.capabilities) & CAP_NEW_GEN) {
client_config.sender_is_newgen = 1;
copyFromMessage(&net_config->dataMcastAddr,
Msg.hello.mcastAddr);
net_config->blockSize = ntohs(Msg.hello.blockSize);
if(ntohl(Msg.hello.capabilities) & CAP_ASYNC)
net_config->flags |= FLAG_PASSIVE;
if(net_config->flags & FLAG_PASSIVE)
goto break_loop;
}
haveServerAddress=1;
continue;
case CMD_CONNECT_REQ:
case CMD_DATA:
case CMD_FEC:
continue;
default:
break;
}
udpc_fatal(1,
"Bad server reply %04x. Other transfer in progress?\n",
(unsigned short) ntohs(Msg.opCode));
}
break_loop:
udpc_flprintf("Connected as #%d to %s\n",
client_config.clientNumber,
getIpString(&client_config.serverAddr, ipBuffer));
getMyAddress(net_config->net_if, &myIp);
if(!ipIsZero(&net_config->dataMcastAddr) &&
!ipIsEqual(&net_config->dataMcastAddr, &myIp) &&
(ipIsZero(&net_config->controlMcastAddr) ||
!ipIsEqual(&net_config->dataMcastAddr, &net_config->controlMcastAddr)
)) {
udpc_flprintf("Listening to multicast on %s\n",
getIpString(&net_config->dataMcastAddr, ipBuffer));
client_config.S_MCAST_DATA =
makeSocket(ADDR_TYPE_MCAST, net_config->net_if,
&net_config->dataMcastAddr,
RECEIVER_PORT(net_config->portBase));
}
if(net_config->requestedBufSize) {
int i;
for(i=0; i<NR_CLIENT_SOCKS; i++)
if(client_config.socks[i] != -1)
setRcvBuf(client_config.socks[i],net_config->requestedBufSize);
}
outFile=openOutFile(disk_config);
origOutFile = outFile;
pipedOutFile = openPipe(outFile, disk_config, &pipePid);
global_client_config= &client_config;
atexit(sendDisconnectWrapper);
{
struct fifo fifo;
int printUncompressedPos =
udpc_shouldPrintUncompressedPos(stat_config->printUncompressedPos,
origOutFile, pipedOutFile);
receiver_stats_t stats = allocReadStats(origOutFile,
stat_config->statPeriod,
printUncompressedPos,
stat_config->noProgress);
udpc_initFifo(&fifo, net_config->blockSize);
fifo.data = pc_makeProduconsum(fifo.dataBufSize, "receive");
client_config.isStarted = 0;
if((net_config->flags & (FLAG_PASSIVE|FLAG_NOKBD))) {
/* No console used */
client_config.console = NULL;
} else {
if(doWarn)
udpc_flprintf("WARNING: This will overwrite the hard disk of this machine\n");
client_config.console = prepareConsole(0);
atexit(fixConsole);
}
spawnNetReceiver(&fifo,&client_config, net_config, stats);
writer(&fifo, pipedOutFile);
if(pipePid) {
close(pipedOutFile);
}
pthread_join(client_config.thread, NULL);
/* if we have a pipe, now wait for that too */
if(pipePid) {
ret=udpc_waitForProcess(pipePid, "Pipe");
}
#ifndef __MINGW32__
fsync(origOutFile);
#endif /* __MINGW32__ */
displayReceiverStats(stats, 1);
}
fixConsole();
sendDisconnectWrapper();
global_client_config= NULL;
return ret;
}