-
Notifications
You must be signed in to change notification settings - Fork 2
/
ftpclient.c
321 lines (277 loc) · 9.03 KB
/
ftpclient.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
#include <sys/sendfile.h>
#include <fcntl.h>
void syserr(char* msg) { perror(msg); exit(-1); }
int main(int argc, char* argv[])
{
int sockfd, portno, n, fileSize;
struct hostent* server;
struct sockaddr_in serv_addr;
char buffer[256];
char fileSizeBuffer[256];
DIR *dir;
struct dirent *directory;
dir = opendir("./folder-local");
if(argc != 3) {
fprintf(stderr, "Usage: %s <hostname> <port>\n", argv[0]);
return 1;
}
server = gethostbyname(argv[1]);
if(!server) {
fprintf(stderr, "ERROR: no such host: %s\n", argv[1]);
return 2;
}
portno = atoi(argv[2]);
/*{
struct in_addr **addr_list; int i;
printf("Official name is: %s\n", server->h_name);
printf(" IP addresses: ");
addr_list = (struct in_addr **)server->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++) {
printf("%s ", inet_ntoa(*addr_list[i]));
}
printf("\n");
}*/
//socket file descriptor
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sockfd < 0) syserr("can't open socket");
printf("create socket...\n");
//once socket is created:
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET; //IPV4
serv_addr.sin_addr = *((struct in_addr*)server->h_addr);
serv_addr.sin_port = htons(portno); //port
if(connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
syserr("can't connect to server");
printf("connect...\n");
do
{
printf("\nPLEASE ENTER MESSAGE: ");
fgets(buffer, 255, stdin);
n = strlen(buffer);
if(n > 0 && buffer[n-1] == '\n') //line break
buffer[n-1] = '\0';
//send
n = send(sockfd, buffer, strlen(buffer), 0);
printf("user sent %s\n", buffer);
if(n < 0) //couldn't send
syserr("can't send to server");
//user calls download
if(buffer[0] == 'g' &&
buffer[1] == 'e' &&
buffer[2] == 't' &&
buffer[3] == ' ')
{
printf("User requested a download.\n");
//we catch the file name
char fileName[256];
memset(&fileName, 0, sizeof(fileName));
//parse
int j = 0;
for(int i = 4; i <= strlen(buffer); i++)
{
fileName[j] = buffer[i];
j++;
}
//catch file size:
recv(sockfd, buffer, sizeof(buffer), 0);
fileSize = atoi(buffer);
//send size back as ACK:
send(sockfd, buffer, sizeof(buffer), 0);
//print file name and size:
printf("File: '%s' (%d bytes)\n",fileName, fileSize);
//receive data:
memset(&buffer, 0, sizeof(buffer));
int remainingData = 0;
ssize_t len;
char path[256] = "./folder-local/";
strcat(path, fileName);
printf("path: %s", path);
FILE* fp;
fp = fopen(path, "wb");//overwrite if existing
//create if not
remainingData = fileSize;
//while(((len = recv(sockfd, buffer, 256, 0)) > 0) && (remainingData > 0))
printf("remainingData: %d", remainingData);
while(remainingData != 0)
{
if(remainingData < 256)
{
len = recv(sockfd, buffer, remainingData, 0);
fwrite(buffer, sizeof(char), len, fp);
remainingData -= len;
printf("Received %lu bytes, expecting %d bytes\n", len, remainingData);
break;
}
else
{
len = recv(sockfd, buffer, 256, 0); //256
fwrite(buffer, sizeof(char), len, fp);
remainingData -= len;
printf("Received %lu bytes, expecting: %d bytes\n", len, remainingData);
}
}
fclose(fp);
n = recv(sockfd, buffer, 256, 0); //receive bizarre lingering packet.
//clean buffer
memset(&buffer, 0, sizeof(buffer));
}
//user calls 'put file'
//send file to server! (upload)
else if(buffer[0] == 'p' &&
buffer[1] == 'u' &&
buffer[2] == 't' &&
buffer[3] == ' ')
{
printf("User requested an upload\n");
//we wait for the server's ACK
n = recv(sockfd, buffer, sizeof(buffer), 0);
if(n < 0)
printf("Server didn't acknowledge name");
//parse the string
int j = 0;
for(int i = 4; i <= strlen(buffer); i++)
{
buffer[j] = buffer[i];
j++;
}
char address[256] = "./folder-local/";
strcat(address, buffer); //get file path
//open file path
FILE* fp;
fp = fopen(address, "rb"); //filename, read bytes
if(fp == NULL)
printf("error opening file in: %s\n", buffer);
printf("File opened successfully!\n");
//we will attempt to read the file
//in chunks of 256 bytes and send!
//figure out file size:
int file_size = 0;
if(fseek(fp, 0, SEEK_END) != 0)
printf("Error determining file size\n");
file_size = ftell(fp);
rewind(fp);
printf("File size: %lu bytes\n", file_size);
//pass this size to a buffer in order to send it:
//no need for host to network long, we're passing char array
memset(&fileSizeBuffer, 0, sizeof(fileSizeBuffer));
sprintf(fileSizeBuffer, "%d", file_size);
//memset(&buffer, 0, sizeof(buffer));
//send file size:
n = send(sockfd, fileSizeBuffer, sizeof(fileSizeBuffer), 0);
if(n < 0)
printf("Error sending file size information\n");
//receive ACK for file size:
//give enough time for server to get
//file size we just sent
n = recv(sockfd, fileSizeBuffer, sizeof(fileSizeBuffer), 0);
if(n < 0)
printf("Error receiving handshake");
//we create a byte array:
char byteArray[256];
memset(&byteArray, 0, sizeof(byteArray));
int buffRead = 0;
int bytesRemaining = file_size;
//while there are still bytes to be sent:
while(bytesRemaining != 0)
{
//we fill in the byte array
//with slabs smaller than 256 bytes:
if(bytesRemaining < 256)
{
buffRead = fread(byteArray, 1, bytesRemaining, fp);
bytesRemaining = bytesRemaining - buffRead;
n = send(sockfd, byteArray, 256, 0);
if(n < 0)
printf("Error sending small slab\n");
printf("sent %d slab\n", buffRead);
}
//for slabs of 256 bytes:
else
{
buffRead = fread(byteArray, 1, 256, fp);
bytesRemaining = bytesRemaining - buffRead;
n = send(sockfd, byteArray, 256, 0);
if(n < 0)
printf("Error sending slab\n");
printf("sent %d slab\n", buffRead);
}
}
printf("File sent!\n");
//clean buffers
memset(&buffer, 0, sizeof(buffer));
memset(&byteArray, 0, sizeof(byteArray));
}
//user calls ls-local
else if(strcmp(buffer, "ls-local") == 0)
{
memset(&buffer, 0, sizeof(buffer));
printf("running ls-local function:");
if(dir)//if directory successfully opens
{
while((directory = readdir(dir)) != NULL)//while in dir.
{
if(strcmp(directory->d_name, ".") == 0 ||
strcmp(directory->d_name, "..") == 0)
{
//printf("\n%s", directory->d_name);
}
else
printf("\n%s", directory->d_name);
}
printf("\n");
//rewind
rewinddir(dir);
}
else
printf("could not open directory");
n = recv(sockfd, buffer, sizeof(buffer), 0);
if(n < 0) //couldn't receive
syserr("can't receive from server");
//clean buffer
memset(&buffer, 0, sizeof(buffer));
}
else if(strcmp(buffer, "ls-remote") == 0)
{
n = recv(sockfd, buffer, sizeof(buffer), 0);
if(n < 0) //couldn't receive
syserr("can't receive from server");
printf("running ls-remote function: %s\n", buffer);
//clean buffer
memset(&buffer, 0, sizeof(buffer));
}
//user exits
else if(strcmp(buffer, "exit") == 0)
{
break;
}
else//user sent a normal message
{
//echo (receive)
//n = recv(sockfd, buffer, sizeof(buffer), 0);
//memset(&buffer, 0, sizeof(buffer));
n = recv(sockfd, buffer, sizeof(buffer), 0);
if(n < 0) //couldn't receive
syserr("can't receive from server");
else
buffer[n] = '\0';
printf("Client received message: %s\n", buffer);
//clean buffer
memset(&buffer, 0, sizeof(buffer));
}
//clean buffer here maybe
memset(&buffer, 0, sizeof(buffer));
} while(strcmp(buffer, "exit") != 0);
close(sockfd);
return 0;
}