-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
123 lines (108 loc) · 2.69 KB
/
client.c
File metadata and controls
123 lines (108 loc) · 2.69 KB
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
#include "client.h"
#include "client_file_processor.h"
volatile serv_conn_info *serv_info_handler;
void quit(serv_conn_info serv_info)
{
ftp_com quit;
quit.type = QUIT;
client_send_block(serv_info, &quit, sizeof(ftp_com));
Close(serv_info.fd);
printf("Your are disconnected\nGoodbye\n");
exit(0);
}
void server_pwd(serv_conn_info serv_info, char *server_id_string)
{
ftp_com pwd;
pwd.type = PWD;
client_send_block(serv_info, &pwd, sizeof(ftp_com));
if (rio_readnb(&serv_info.rio, &pwd, sizeof(ftp_com)) == sizeof(ftp_com) && pwd.type == PWD)
printf("\033[1;31m%s\033[0;37m:\033[1;36m%s\033[0;37m$ ", server_id_string, pwd.content);
}
void client_request(serv_conn_info serv_info, char *host)
{
ftp_com msg;
memset(&msg, 0, sizeof(ftp_com));
while (!serv_info.disconnected)
{
if (Fgets(msg.content, sizeof(msg.content), stdin) == NULL)
{
printf("\n");
break;
}
msg.content[strcspn(msg.content, "\n")] = 0;
if (strlen(msg.content) == 0)
{
server_pwd(serv_info, host);
}
else
{
if (strcmp(msg.content, "bye") == 0)
{
quit(serv_info);
}
else if (strstr(msg.content, "get") == msg.content)
{
get_file(serv_info, msg.content + 4);
}
else if (strstr(msg.content, "resume") == msg.content)
{
resume_get_file(serv_info, msg.content + 7);
}
else
{
if (strcmp(msg.content, "ping") == 0)
{
memset(&msg, 0, sizeof(ftp_com));
msg.type = PING;
client_send_block(serv_info, &msg, sizeof(ftp_com));
}
else
{
msg.type = MSG;
msg.value = strlen(msg.content);
client_send_block(serv_info, &msg, sizeof(ftp_com));
}
memset(&msg, 0, sizeof(ftp_com));
rio_readnb(&serv_info.rio, &msg, sizeof(ftp_com));
Fputs(msg.content, stdout);
}
server_pwd(serv_info, host);
}
memset(&msg, 0, sizeof(ftp_com));
}
}
void sig_int_handler(int sig)
{
printf("\n");
quit(*serv_info_handler);
}
int main(int argc, char **argv)
{
int port;
char *host;
serv_conn_info serv_info;
serv_info_handler = &serv_info;
Signal(SIGINT, sig_int_handler);
if (argc < 2)
{
fprintf(stderr, "usage: %s <host>\n", argv[0]);
exit(0);
}
else if (argc == 2)
{
port = FTP_PORT;
}
else if (argc == 3)
{
port = atoi(argv[2]);
}
chdir(CLIENT_FOLDER);
host = argv[1];
serv_info.fd = Open_clientfd(host, port);
serv_info.disconnected = 0;
printf("client connected to FTP server\n");
Rio_readinitb(&serv_info.rio, serv_info.fd);
server_pwd(serv_info, host);
client_request(serv_info, host);
quit(serv_info);
}