-
Notifications
You must be signed in to change notification settings - Fork 46
/
vs_recv.c
292 lines (252 loc) · 6.56 KB
/
vs_recv.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
/*
* A simple RUDP receiver to receive files from remote hosts.
* It takes only one argument - local port to be used.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <syslog.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "rudp_api.h"
#include "event.h"
#include "vsftp.h"
/*
* Data structure for keeping track of partially received files
*/
struct rxfile {
struct rxfile *next; /* Next pointer for linked list */
int fileopen; /* True if file is open */
int fd; /* File descriptor */
struct sockaddr_in remote; /* Peer */
char name[VS_FILENAMELENGTH+1]; /* Name of file */
};
/*
* Prototypes
*/
int filesender(int fd, void *arg);
int rudp_receiver(rudp_socket_t rsocket, struct sockaddr_in *remote, char *buf, int len);
int eventhandler(rudp_socket_t rsocket, rudp_event_t event, struct sockaddr_in *remote);
int usage();
/*
* Global variables
*/
int debug = 0; /* Print debug messages */
struct rxfile *rxhead = NULL; /* Pointer to linked list of rxfiles */
/*
* usage: how to use program
*/
int usage() {
fprintf(stderr, "Usage: vs_recv [-d] port\n");
exit(1);
}
int main(int argc, char* argv[]) {
rudp_socket_t rsock;
int port;
int c;
/*
* Parse and collect arguments
*/
opterr = 0;
while ((c = getopt(argc, argv, "d")) != -1) {
if (c == 'd') {
debug = 1;
}
else
usage();
}
if (argc - optind != 1) {
usage();
}
port = atoi(argv[optind]);
if (port <= 0) {
fprintf(stderr, "Bad destination port: %s\n", argv[optind]);
exit(1);
}
if (debug) {
printf("RUDP receiver waiting on port %i.\n",port);
}
/*
* Create RUDP listener socket
*/
if ((rsock = rudp_socket(port)) == NULL) {
fprintf(stderr,"vs_recv: rudp_socket() failed\n");
exit(1);
}
/*
* Register receiver callback function
*/
rudp_recvfrom_handler(rsock, rudp_receiver);
/*
* Register event handler callback function
*/
rudp_event_handler(rsock, eventhandler);
/*
* Hand over control to event manager
*/
eventloop(0);
return (0);
}
/*
* rxfind: helper function to lookup a rxfile descriptor on the linked list.
* Create new if not found
*/
static struct rxfile *rxfind(struct sockaddr_in *addr) {
struct rxfile *rx;
for (rx = rxhead; rx != NULL; rx = rx->next) {
if (memcmp(&rx->remote, addr, sizeof(struct in_addr)) == 0)
return rx;
}
/* Not found, create new */
if ((rx = malloc(sizeof(struct rxfile))) == NULL) {
fprintf(stderr, "vs_receiver: malloc failed\n");
exit(1);
}
rx->fileopen = 0;
rx->remote = *addr;
rx->next = rxhead;
rxhead = rx;
return rx;
}
/*
* rxdel: helper function to unlink and free a rxfile descriptor
*/
static int rxdel(struct rxfile *rx) {
struct rxfile **rxp;
for (rxp = &rxhead; *rxp != NULL && *rxp != rx; rxp = &(*rxp)->next)
;
if (*rxp == NULL) { /* Not found */
fprintf(stderr, "vs_recv: Can't find rx record for peer\n");
return -1;
}
*rxp = rx->next;
free(rx);
return 0;
}
/*
* eventhandler: callback function for RUDP events
*/
int eventhandler(rudp_socket_t rsocket, rudp_event_t event, struct sockaddr_in *remote) {
struct rxfile *rx;
switch (event) {
case RUDP_EVENT_TIMEOUT:
if (remote) {
fprintf(stderr, "vs_recv: time out in communication with %s:%d\n",
inet_ntoa(remote->sin_addr),
ntohs(remote->sin_port));
if ((rx = rxfind(remote))) {
if (rx->fileopen) {
close(rx->fd);
}
rxdel(rx);
}
}
else {
fprintf(stderr, "vs_recv: time out\n");
}
break;
case RUDP_EVENT_CLOSED:
if (remote && (rx = rxfind(remote))) {
if (rx->fileopen) {
fprintf(stderr, "vs_recv: prematurely closed communication with %s:%d\n",
inet_ntoa(remote->sin_addr),
ntohs(remote->sin_port));
close(rx->fd);
}
rxdel(rx);
} /* else ignore */
break;
default:
fprintf(stderr, "vs_recv: unknown event %d\n", event);
break;
}
return 0;
}
/*
* rudp_receiver: callback function for processing data received
* on RUDP socket.
*/
int rudp_receiver(rudp_socket_t rsocket, struct sockaddr_in *remote, char *buf, int len) {
struct rxfile *rx;
int namelen;
int i;
struct vsftp *vs = (struct vsftp *) buf;
if (len < VS_MINLEN) {
fprintf(stderr, "vs_recv: Too short VSFTP packet (%d bytes)\n",
len);
return 0;
}
rx = rxfind(remote);
switch (ntohl(vs->vs_type)) {
case VS_TYPE_BEGIN:
namelen = len - sizeof(vs->vs_type);
if (namelen > VS_FILENAMELENGTH)
namelen = VS_FILENAMELENGTH;
strncpy(rx->name, vs->vs_info.vs_filename, namelen);
rx->name[namelen] = '\0'; /* Null terminated */
/* Verify that file name is valid
* Only alpha-numerical, period, dash and
* underscore are allowed */
for (i = 0; i < namelen; i++) {
char c = rx->name[i];
if (!(isalnum(c) || c == '.' || c == '_' || c == '-')) {
fprintf(stderr, "vs_recv: Illegal file name \"%s\"\n",
rx->name);
rudp_close(rsocket);
return 0;
}
}
if (debug) {
fprintf(stderr, "vs_recv: BEGIN \"%s\" (%d bytes) from %s:%d\n", rx->name, len,
inet_ntoa(remote->sin_addr), ntohs(remote->sin_port));
}
if ((rx->fd = creat(rx->name, 0644)) < 0) {
perror("vs_recv: create");
rudp_close(rsocket);
}
else {
rx->fileopen = 1;
}
break;
case VS_TYPE_DATA:
if (debug) {
fprintf(stderr, "vs_recv: DATA (%d bytes) from %s:%d\n",
len,
inet_ntoa(remote->sin_addr), ntohs(remote->sin_port));
}
len -= sizeof(vs->vs_type);
/* len now is length of payload (data or file name) */
if (rx->fileopen) {
if ((write(rx->fd, vs->vs_info.vs_filename, len)) < 0) {
perror("vs_recv: write");
}
}
else {
fprintf(stderr, "vs_recv: DATA ignored (file not open)\n");
}
break;
case VS_TYPE_END:
if (debug) {
fprintf(stderr, "vs_recv: END (%d bytes) from %s:%d\n",
len, inet_ntoa(remote->sin_addr), ntohs(remote->sin_port));
}
printf("vs_recv: received end of file \"%s\"\n", rx->name);
if (rx->fileopen) {
close(rx->fd);
rxdel(rx);
}
/* else ignore */
break;
default:
fprintf(stderr, "vs_recv: bad vsftp type %d from %s:%d\n",
vs->vs_type, inet_ntoa(remote->sin_addr), ntohs(remote->sin_port));
}
return 0;
}