-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
376 lines (282 loc) · 7.91 KB
/
client.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <getopt.h>
#include <signal.h>
#include <histedit.h>
#include "smdp.h"
#define DEFAULT_PORT 3535
char buf[1024];
char* line;
int sockfd;
int verbose = 0;
int port_no = DEFAULT_PORT;
const struct option long_options[] = {
{"verbose", no_argument, 0, 'v'},
{"port", required_argument, 0, 'p'},
{0, 0, 0, 0}
};
int running = 1;
void do_echo(){
fgets(buf, 1023, stdin);
smdp_write_int(sockfd, SMDP_ECHO);
smdp_write_str(sockfd, buf);
memset(buf, 0, 1024);
smdp_read_int(sockfd); // ignore type, assume to be echo
smdp_read_str(sockfd, buf, 1024);
printf("%s\n", buf);
}
void do_list(){
smdp_write_int(sockfd, SMDP_LIST);
smdp_read_int(sockfd); // ignore type, assume to be list
int rows = smdp_read_int(sockfd);
int i;
for(i=0;i<rows;i++){
smdp_read_int(sockfd); // ignore type, assume to be row
memset(buf, 0, 1024);
smdp_read_str(sockfd, buf, 1024);
printf("%s ", buf);
memset(buf, 0, 1024);
smdp_read_str(sockfd, buf, 1024);
printf("%s ", buf);
memset(buf, 0, 1024);
smdp_read_str(sockfd, buf, 1024);
printf("%s\n", buf);
}
}
void do_user(char* username){
smdp_write_int(sockfd, SMDP_USER);
smdp_write_str(sockfd, username);
}
void do_pass(char* password){
smdp_write_int(sockfd, SMDP_PASS);
smdp_write_str(sockfd, password);
uint32_t type = smdp_read_int(sockfd);
if(type == SMDP_ACCEPT){
printf("Successfully logged in\n");
} else {
printf("Invalid username/password\n");
}
}
/* reads a file from given path and sends it through the socket connection
by reading the file into the buffer */
void send_file(const char* path){
if(verbose){
printf("Sending file %s\n", path);
}
uint32_t len;
struct stat st;
/* stat returns -1 when the given file does not exist */
if(stat(path, &st) < 0){
fprintf(stderr, "File not found: %s\n", path);
return;
}
/* send the file command and file size */
len = st.st_size;
smdp_write_int(sockfd, len);
/* open the file */
FILE* fp;
fp = fopen(path, "rb");
uint32_t counter = 0;
/* read into the buffer and send part by part until end of file */
while(counter < len){
uint32_t to_read = (len-counter<1024)?(len-counter):1024;
memset(buf, 0, 1024);
fread(buf, sizeof(char), to_read, fp);
int n = write(sockfd, buf, to_read);
if(n < 0){
error("Error writing to socket");
}
counter += to_read;
}
fclose(fp);
}
void receive_file(char* path){
FILE* fp;
fp = fopen(path, "wb");
uint32_t len = smdp_read_int(sockfd);
uint32_t counter = 0;
/* read into the buffer and send part by part until end of file */
while(counter < len){
uint32_t to_read = (len-counter<1024)?(len-counter):1024;
int n = read(sockfd, buf, to_read);
if(n < 0){
error("Error reading from socket");
}
fwrite(buf, sizeof(char), to_read, fp);
counter += to_read;
}
fclose(fp);
}
void do_download(int mid, char* path){
smdp_write_int(sockfd, SMDP_FILE);
smdp_write_int(sockfd, mid);
int resp = smdp_read_int(sockfd);
if(resp == SMDP_DENY){
printf("Access denied\n");
} else if(resp == SMDP_NOFILE){
printf("No such file\n");
} else {
receive_file(path);
}
}
void do_random(char* path){
smdp_write_int(sockfd, SMDP_RANDOM);
int resp = smdp_read_int(sockfd);
if(resp == SMDP_DENY){
printf("Access denied\n");
} else if(resp == SMDP_NOFILE){
printf("No such file\n");
} else {
receive_file(path);
}
}
void do_upload(char* name, char* path){
struct stat st;
/* stat returns -1 when the given file does not exist */
if(stat(path, &st) < 0){
fprintf(stderr, "File not found: %s\n", path);
return;
}
smdp_write_int(sockfd, SMDP_UPLOAD);
int res = smdp_read_int(sockfd);
if(res == SMDP_DENY){
printf("Access denied\n");
return;
}
smdp_write_str(sockfd, name);
send_file(path);
}
void signal_handler(int sig){
smdp_write_int(sockfd, SMDP_CLOSE);
close(sockfd);
}
void setup(char* hostname){
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGSEGV, signal_handler);
sockfd = 0;
struct sockaddr_in server_addr;
struct hostent* server;
memset(&server_addr, 0, sizeof(server_addr));
memset(buf, 0, 1024);
server = gethostbyname(hostname);
if(server == NULL){
printf("ERROR: No such host\n");
exit(0);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port_no);
memcpy(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
error("Error creating socket");
}
if(connect(sockfd, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0){
error("Error connecting to server");
}
}
void parse_opts(int argc, char** argv){
int c;
for(;;){
int option_index = 0;
c = getopt_long(argc, argv, "p:v", long_options, &option_index);
if(c == -1) break;
switch(c){
case 'p':
port_no = atoi(optarg);
break;
case 'v':
verbose = 1;
printf("Verbose mode\n");
break;
default:
abort();
}
}
}
void help_message(){
printf("Commands: \n");
printf("* list \n");
printf("* user <username> \n");
printf("* pass <password> \n");
printf("* download <mid> <filename> \n");
printf("* random <filename>\n");
printf("* upload <name> <path>\n");
printf("* exit\n");
}
void do_command(char* command){
char* tok;
tok = strtok(command, " \n");
if(strcmp(tok, "list")==0){
do_list();
} else if(strcmp(tok, "user")==0){
tok = strtok(NULL, " \n");
do_user(tok);
} else if(strcmp(tok, "pass")==0){
tok = strtok(NULL, " \n");
do_pass(tok);
} else if(strcmp(tok, "download")==0){
tok = strtok(NULL, " \n");
int mid = atoi(tok);
tok = strtok(NULL, " \n");
do_download(mid, tok);
} else if(strcmp(tok, "random")==0){
tok = strtok(NULL, " \n");
do_random(tok);
} else if(strcmp(tok, "upload")==0){
tok = strtok(NULL, " \n");
char name[256];
strcpy(name, tok);
tok = strtok(NULL, " \n");
do_upload(name, tok);
}else if(strcmp(tok, "exit")==0){
running = 0;
} else {
printf("Invalid command %s\n", tok);
}
}
char* prompt(EditLine* e) {
return "> ";
}
int main(int argc, char** argv){
parse_opts(argc, argv);
if(argc < 2){
printf("USAGE: client hostname\n");
return 0;
}
printf("%s\n", argv[optind]);
setup(argv[optind]);
help_message();
/* disable tab completion */
/* rl_bind_key('\t', rl_abort); */
EditLine* el;
History* hist;
HistEvent ev;
int linelen;
el = el_init(argv[0], stdin, stdout, stderr);
el_set(el, EL_PROMPT, &prompt);
el_set(el, EL_EDITOR, "emacs");
hist = history_init();
history(hist, &ev, H_SETSIZE, 800);
el_set(el, EL_HIST, history, hist);
while(running && (line = (char*)el_gets(el, &linelen)) != NULL){
do_command(line);
if(linelen!=0){
history(hist, &ev, H_ENTER, line);
}
}
free(line);
history_end(hist);
el_end(el);
smdp_write_int(sockfd, SMDP_CLOSE);
close(sockfd);
return 0;
}