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
|
/*##############################################################################
# Copyright (c) 2017 ZTE Coreporation and others.
# hu.zhijiang@zte.com.cn
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "buffer.h"
#include "udp-common.h"
#include "tcp-common.h"
#include "misc.h"
/* Global statistics */
long tot_dups = 0;
long tot_reps = 0;
long tot_pkts = 0;
long long tot_size = 0;
void recv_mcinfo(int tcp_socket)
{
int res;
res = read_all(tcp_socket, &mcinfo, sizeof(struct mc_info));
if (res != sizeof(struct mc_info)) {
crit("Error while reading initial data");
}
}
void recv_buffctl(int tcp_socket)
{
int res;
res = read_all(tcp_socket, &buffctl, sizeof(struct buffer_ctl));
if (res != sizeof(struct buffer_ctl)) {
crit("Error reading buffer control");
}
}
void send_client_ready(int tcp_socket)
{
uint8_t cmd;
/* Tell TCP server we are ready to receive */
cmd = CLIENT_READY;
write(tcp_socket, &cmd, 1);
}
/* Read out the SERVER_SENT signal */
void recv_server_sent(int tcp_socket)
{
uint8_t cmd;
int res;
res = read_all(tcp_socket, &cmd, 1);
if (res != 1) {
//if (read(tcp_socket, &cmd, 1) < 1) {
crit("Error reading SERVER_SENT");
}
if (cmd != SERVER_SENT) {
crit("Error reading SERVER_SENT %d", cmd);
}
}
void send_client_request(int tcp_socket, struct request_ctl *req)
{
uint8_t cmd;
/* Tell TCP server we are ready to receive */
cmd = CLIENT_REQ;
write(tcp_socket, &cmd, 1);
write(tcp_socket, req,
sizeof(struct request_ctl) + (req->req_count) * sizeof(uint32_t));
}
void recv_server_ack(int tcp_socket, struct packet_ctl *pkt)
{
int res;
res = read_all(tcp_socket, pkt, sizeof(struct packet_ctl));
if (res != sizeof(struct packet_ctl)) {
crit("Error on tcp socket");
}
res = read_all(tcp_socket,
((char *)pkt) + sizeof(struct packet_ctl),
pkt->data_size);
if (res != pkt->data_size) {
crit("Error on tcp socket received %d of %d",
res, pkt->data_size);
}
}
void tcp_retransmition(int tcp_socket,
struct packet_ctl **curr_pkt,
struct packet_ctl **freed_pkt)
{
struct request_ctl *reqctl;
uint32_t *reqbody;
uint8_t rqbuf[MAX_REQ_SIZE];
uint32_t l;
reqctl = (struct request_ctl *)rqbuf;
reqbody = (uint32_t *)(rqbuf + sizeof(struct request_ctl));
reqctl->req_count = 0;
for (l = 0; l < buffctl.pkt_count; l++) {
if (!packetctl[l]->data_size) {
log(6, "Requesting packet %u", l + buffctl.packet_id_base);
reqbody[reqctl->req_count] = l + buffctl.packet_id_base;
reqctl->req_count++;
}
}
if (reqctl->req_count > 0) {
send_client_request(tcp_socket, reqctl);
/* read retransmitted blocks via TCP */
for (l = 0; l < reqctl->req_count; l++) {
if (*freed_pkt) {
*curr_pkt = *freed_pkt;
}
recv_server_ack(tcp_socket, *curr_pkt);
*freed_pkt = packet_put(*curr_pkt);
if (!(*freed_pkt)) {
crit("Malformed packet on tcp socket");
}
if ((*freed_pkt)->data_size != 0) {
crit("Malformed free packet slot or TCP data");
}
log(6, "Received retran packet %u", (*curr_pkt)->seq);
}
tot_reps += reqctl->req_count;
}
}
/* Returns how many good packets received from UDP */
int recv_mcast(int tcp_socket, int udp_socket,
struct packet_ctl **curr_pkt,
struct packet_ctl **freed_pkt)
{
int rcv_pkt_count;
int got_sent;
int maxfd;
fd_set rfds;
struct timeval tv;
int res;
maxfd = tcp_socket;
if (maxfd < udp_socket) {
maxfd = udp_socket;
}
maxfd++;
FD_ZERO(&rfds);
rcv_pkt_count = 0;
got_sent = 0;
if (buffctl.pkt_count != 0) {
do {
FD_SET(tcp_socket, &rfds);
FD_SET(udp_socket, &rfds);
tv.tv_sec = 20;
tv.tv_usec = 0;
res = select(maxfd, &rfds, 0, 0, &tv);
if (res < 0) {
crit("select error");
}
if (res == 0) {
crit("select timed out");
}
/* Read multicast packet */
if (FD_ISSET(udp_socket, &rfds)) {
log(7, "Reading multicast packet");
if (*freed_pkt) {
*curr_pkt = *freed_pkt;
}
res = recv(udp_socket, *curr_pkt, PACKET_SIZE, 0);
if (res <= 0) {
crit("error on multicast socket");
}
if (res < sizeof(struct packet_ctl) ) {
log(7, "Truncated packet received (%d bytes)", res);
} else if (res != (*curr_pkt)->data_size + sizeof(struct packet_ctl)) {
log(7,
"Truncated packet received (%d of %ld bytes)",
res, (*curr_pkt)->data_size + sizeof(struct packet_ctl));
} else {
log(9, "Normal packet seq:%d", (*curr_pkt)->seq);
(*freed_pkt) = packet_put((*curr_pkt));
if (!(*freed_pkt)) {
log(5, "Malformed packet");
} else {
if ((*freed_pkt)->data_size == 0) {
rcv_pkt_count++;
} else {
log(6, "Duplicated packet");
tot_dups++;
}
}
}
} else if (FD_ISSET(tcp_socket, &rfds)) {
/* Check TCP, only if there was no more data from UDP */
log(6, "No more data path1");
recv_server_sent(tcp_socket);
got_sent = 1;
break;
}
} while (rcv_pkt_count < buffctl.pkt_count);
}
if (got_sent == 0) {
log(6, "No more data path2");
recv_server_sent(tcp_socket);
}
return rcv_pkt_count;
}
void send_client_done(int tcp_socket)
{
uint8_t cmd;
/* Tell TCP server we are ready to receive */
cmd = CLIENT_DONE;
write(tcp_socket, &cmd, 1);
}
int main(int argc, char *argv[])
{
int ms, ts;
struct packet_ctl *alloc_pkt, *curr_pkt, *freed_pkt;
int udp_rcv_count;
u_short port = DEF_PORT;
struct in_addr local_addr;
if (argc < 3) {
printf("Usage: %s <local_ip> <server_ip> [port]\n", argv[0]);
return -1;
}
if (!inet_aton(argv[1], &local_addr)) {
crit("can not resolve address: %s", argv[1]);
}
if (argc > 3) {
port = atoi(argv[3]);
if (!port) {
port = DEF_PORT;
}
}
buffer_init();
/* Init first time packet slot */
alloc_pkt = (struct packet_ctl *)wrapper_malloc(PACKET_SIZE);
memset(alloc_pkt, 0, PACKET_SIZE);
freed_pkt = curr_pkt = alloc_pkt;
ts = init_tcp_client_socket(argv[2], port);
recv_mcinfo(ts);
ms = init_mcast_socket(&local_addr, &mcinfo.group);
/* Do we need set_nonblock(ms)??? ; */
/* Will do dummy run even if buffctl.pkt_count is zero at the first round */
do { /* one buffer a round */
packetctl_precheck();
recv_buffctl(ts);
send_client_ready(ts);
udp_rcv_count = recv_mcast(ts, ms, &curr_pkt,&freed_pkt);
if (udp_rcv_count == buffctl.pkt_count) {
log(6, "All packets of current buffer received from UDP");
} else {
tcp_retransmition(ts, &curr_pkt, &freed_pkt);
}
tot_pkts += buffctl.pkt_count;
tot_size += buffctl.buffer_size;
log(1, "\rBuffer received %lld Bytes, %ld Packets(%ld Repeats %ld Dups)",
tot_size, tot_pkts, tot_reps, tot_dups);
if (buffctl.pkt_count) {
buffer_flush(STDOUT_FILENO);
}
send_client_done(ts);
} while (buffctl.pkt_count != 0);
shutdown(ts, 2);
close(ts);
close(ms);
log(1, "All buffers receive done.\n");
return 0;
}
|