-
Notifications
You must be signed in to change notification settings - Fork 0
/
pftp.c
320 lines (281 loc) · 8.38 KB
/
pftp.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
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
#include <pthread.h>
#include <signal.h>
#include "pftp_common.h"
static int m_run_receiver = 0;
static pthread_mutex_t m_pftp_mutex = PTHREAD_MUTEX_INITIALIZER;
static void print_usage() {
fprintf(stderr, "PFTP Server v0.0.1 by Maarten Vergouwe,\n"
"Copyright Televic NV\n"
"\n"
"Usage : \n"
"pftp [OPTIONS] file-to-send \n"
"\n"
"OPTIONS:\n"
"\t-m multicast-address\t\tSelect multicast address to send on.\n"
"\t-i interface\t\t\tSelect interface to send on.\n"
"\t-p destination port\t\tSelect port to send to.\n");
exit(-1);
}
//static void handle_sigint(int signo) {
// m_run_receiver = 0;
//}
static void *nak_routine(void *arg) {
/* This thread makes sure we get the NAKs from the receivers */
pgm_sock_t *pgm_sock = (pgm_sock_t*)arg;
pgm_error_t* pgm_err = NULL;
int fds = 0;
fd_set readfds;
char recv_buf[PGMBUF_SIZE];
size_t bytes_read = 0;
int run_receiver = m_run_receiver;
while (run_receiver) {
memset(&recv_buf, 0, PGMBUF_SIZE);
bytes_read = 0;
struct timeval tv;
struct pgm_sockaddr_t from;
socklen_t from_sz = sizeof(from);
const int pgm_status = pgm_recvfrom(pgm_sock, recv_buf, PGMBUF_SIZE, MSG_DONTWAIT, &bytes_read, &from, &from_sz, &pgm_err);
//PRINT_ERR("pgm_status: %d", pgm_status);
switch (pgm_status) {
case PGM_IO_STATUS_TIMER_PENDING:
{
socklen_t optlen = sizeof(tv);
pgm_getsockopt (pgm_sock, IPPROTO_PGM, PGM_TIME_REMAIN, &tv, &optlen);
if (0 == (tv.tv_sec * 1000) + ((tv.tv_usec + 500) / 1000))
break;
goto block;
}
case PGM_IO_STATUS_RATE_LIMITED:
{
socklen_t optlen = sizeof(tv);
pgm_getsockopt (pgm_sock, IPPROTO_PGM, PGM_RATE_REMAIN, &tv, &optlen);
if (0 == (tv.tv_sec * 1000) + ((tv.tv_usec + 500) / 1000))
break;
/* No accidental fallthrough! */
}
block:
case PGM_IO_STATUS_WOULD_BLOCK:
FD_ZERO(&readfds);
pgm_select_info(pgm_sock, &readfds, NULL, &fds);
fds = select(fds, &readfds, NULL, NULL, pgm_status == PGM_IO_STATUS_WOULD_BLOCK ? NULL : &tv);
break;
default :
if (pgm_err) {
fprintf(stderr, "%s\n", pgm_err->message);
pgm_error_free(pgm_err);
pgm_err = NULL;
}
break;
}
pthread_mutex_lock(&m_pftp_mutex);
run_receiver = m_run_receiver;
pthread_mutex_unlock(&m_pftp_mutex);
}
pthread_exit(NULL);
}
int main( int argc, char *argv[]) {
int c = 0;
int ret_val = 0;
int port = 0;
char network[128];
char *file = NULL;
char *iface = NULL;
char *mcast_addr = NULL;
int bfd = 0;
network[0] = '\0';
#ifdef UDPTEST
int sockfd = 0;
#else
pgm_error_t* pgm_err = NULL;
pgm_sock_t *pgm_sock;
pthread_t nak_thread;
#endif
while((c = getopt(argc, argv, ":hm:i:p:f:")) != -1) {
switch (c) {
case 'm':
mcast_addr = optarg;
break;
case 'i':
iface = optarg;
break;
case 'p':
port = atoi(optarg);
break;
case 'h':
case '?':
print_usage();
break;
default:
print_usage();
break;
}
}
if(optind != argc-1) {
PRINT_ERR("Please provide the path to a file you want to send!");
ret_val = -1;
goto ret_error;
} else {
file = argv[optind];
}
if (iface) {
char *ifaddr = pftp_inet_iftoa(iface);
if (strlen(ifaddr) > 0) {
strncat(network, ifaddr, 128);
strncat(network, ";", 128);
}
}
if (mcast_addr) {
strncat(network, mcast_addr, 128);
} else {
strncat(network, PFTP_DEFAULT_MCAST_ADDR, 128);
}
#ifdef UDPTEST
struct sockaddr_in servaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(PFTP_DEFAULT_MCAST_ADDR);
servaddr.sin_port = port ? htons(port) : htons(PFTP_UDP_PORT);
#else
if (0 != pftp_create(1, network, port, &pgm_sock)) {
ret_val = -1;
goto ret_error;
}
int pgm_status;
/* Start NAK receiver thread */
m_run_receiver = 1;
pthread_create(&nak_thread, NULL, nak_routine, (void*)pgm_sock);
#endif
char buf[PGMBUF_SIZE];
buf[0] = '\0';
struct stat fstat;
if (-1 == stat(file, &fstat)) {
PRINT_ERR("Couldn't stat file: %s", strerror(errno));
ret_val = -1;
goto ret_error;
}
char *fname = strrchr(file, '/');
if (!fname) {
fname = file;
} else {
fname = fname+1;
}
memset(buf, 0, PGMBUF_SIZE);
snprintf(buf, PGMBUF_SIZE, CMD_SEND_FILE" %ld %s", fstat.st_size, fname);
PRINT_DBG("Sending file with command: %s", buf);
size_t bytes_written;
int bytes_read;
#ifdef UDPTEST
bytes_written = sendto(sockfd, buf, strlen(buf)+1, 0, (struct sockaddr*)&servaddr, sizeof(servaddr));
if (bytes_written != strlen(buf)+1) {
PRINT_ERR("Couldn't send file transfer command! %s", strerror(errno));
ret_val = -1;
goto ret_error;
}
#else
pgm_status = pgm_status = pgm_send(pgm_sock, buf, strlen(buf)+1, &bytes_written);
if (pgm_status != PGM_IO_STATUS_NORMAL) {
PRINT_ERR("Couldn't send file transfer command!");
ret_val = -1;
goto ret_error;
}
#endif
bfd = open(file, O_RDONLY);
if (bfd < 0) {
PRINT_ERR("Couldn't open file for reading! %s", strerror(errno));
ret_val = -1;
goto ret_error;
}
int fds = 0;
fd_set writefds;
fd_set readfds;
bytes_read = read(bfd, buf, PGMBUF_SIZE);
while (bytes_read > 0) {
#ifdef UDPTEST
bytes_written = sendto(sockfd, buf, bytes_read, 0, (struct sockaddr*)&servaddr, sizeof(servaddr));
#else
struct timeval tv;
pgm_status = pgm_send(pgm_sock, buf, bytes_read, &bytes_written);
//PRINT_DBG("pgm_status: %d", pgm_status);
switch (pgm_status) {
case PGM_IO_STATUS_NORMAL :
{
#endif
if (bytes_written != bytes_read) {
PRINT_ERR("Error sending file!");
ret_val = -1;
goto ret_error;
}
bytes_read = read(bfd, buf, PGMBUF_SIZE);
#ifdef UDPTEST
struct timespec ts = {0, 35000};
nanosleep(&ts, NULL);
#else
} break;
case PGM_IO_STATUS_TIMER_PENDING:
{
socklen_t optlen = sizeof(tv);
pgm_getsockopt (pgm_sock, IPPROTO_PGM, PGM_TIME_REMAIN, &tv, &optlen);
if (0 == (tv.tv_sec * 1000) + ((tv.tv_usec + 500) / 1000))
break;
goto block;
}
case PGM_IO_STATUS_RATE_LIMITED :
{
socklen_t optlen = sizeof(tv);
pgm_getsockopt (pgm_sock, IPPROTO_PGM, PGM_RATE_REMAIN, &tv, &optlen);
if (0 == (tv.tv_sec * 1000) + ((tv.tv_usec + 500) / 1000))
break;
/* No accidental fallthrough! */
}
case PGM_IO_STATUS_CONGESTION :
case PGM_IO_STATUS_WOULD_BLOCK :
block:
{
FD_ZERO(&writefds);
pgm_select_info(pgm_sock, NULL, &writefds, &fds);
fds = select(fds, NULL, &writefds, NULL, pgm_status == PGM_IO_STATUS_WOULD_BLOCK ? NULL : &tv);
} break;
default:
PRINT_ERR("Send error!");
ret_val = -1;
goto ret_error;
break;
}
#endif
}
#ifndef UDPTEST
pthread_mutex_lock(&m_pftp_mutex);
m_run_receiver = 0;
pthread_mutex_unlock(&m_pftp_mutex);
pthread_join(nak_thread, NULL);
pthread_mutex_destroy(&m_pftp_mutex);
#endif
goto ret_good;
ret_error:
PRINT_DBG("Exit error");
goto ret;
ret_good:
PRINT_DBG("Exit good");
ret:
if (bfd > 0)
close(bfd);
#ifdef UDPTEST
if (sockfd > 0)
close(sockfd);
#else
if (pgm_sock) {
pftp_stop(pgm_sock);
}
#endif
return ret_val;
}