forked from rovinbhandari/FTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_ftp.c
178 lines (166 loc) · 4.33 KB
/
client_ftp.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
#include <client_ftp.h>
int main(int argc, char* argv[])
{
//BEGIN: initialization
struct sockaddr_in sin_server;
int sfd_client, x;
size_t size_sockaddr = sizeof(struct sockaddr), size_packet = sizeof(struct packet);
short int connection_id;
struct packet* chp = (struct packet*) malloc(size_packet); // client host packet
set0(chp);
struct packet* data; // network packet
if((x = sfd_client = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
er("socket()", x);
memset((char*) &sin_server, 0, sizeof(struct sockaddr_in));
sin_server.sin_family = AF_INET;
sin_server.sin_addr.s_addr = inet_addr(IPSERVER);
sin_server.sin_port = htons(PORTSERVER);
if((x = connect(sfd_client, (struct sockaddr*) &sin_server, size_sockaddr)) < 0)
er("connect()", x);
printf(ID "FTP Client started up. Attempting communication with server @ %s:%d...\n\n", IPSERVER, PORTSERVER);
//END: initialization
struct command* cmd;
char lpwd[LENBUFFER], pwd[LENBUFFER];
char userinput[LENUSERINPUT];
while(1)
{
printf("\t> ");
fgets(userinput, LENUSERINPUT, stdin); // in order to give \
a filename with spaces, put ':' \
instead of ' '. If a command needs \
x paths, and y (y > x) paths are \
provided, y - x paths will be \
ignored.
cmd = userinputtocommand(userinput);
if(!cmd)
continue;
//printcommand(cmd);
switch(cmd->id)
{
case GET:
if(cmd->npaths)
command_get(chp, data, sfd_client, *cmd->paths);
else
fprintf(stderr, "No path to file given.\n");
break;
case PUT:
if(cmd->npaths)
command_put(chp, data, sfd_client, *cmd->paths);
else
fprintf(stderr, "No path to file given.\n");
break;
case MGET:
if(cmd->npaths)
command_mget(chp, data, sfd_client, cmd->npaths, cmd->paths);
else
fprintf(stderr, "No path to file given.\n");
break;
case MPUT:
if(cmd->npaths)
command_mput(chp, data, sfd_client, cmd->npaths, cmd->paths);
else
fprintf(stderr, "No path to file given.\n");
break;
case MGETWILD:
command_mgetwild(chp, data, sfd_client);
break;
case MPUTWILD:
if(!getcwd(lpwd, sizeof lpwd))
er("getcwd()", 0);
command_mputwild(chp, data, sfd_client, lpwd);
break;
case CD:
if(cmd->npaths)
command_cd(chp, data, sfd_client, *cmd->paths);
else
fprintf(stderr, "No path given.\n");
break;
case LCD:
if(cmd->npaths)
command_lcd(*cmd->paths);
else
fprintf(stderr, "No path given.\n");
break;
case PWD:
command_pwd(chp, data, sfd_client);
break;
case LPWD:
if(!getcwd(lpwd, sizeof lpwd))
er("getcwd()", 0);
printf("\t%s\n", lpwd);
break;
case DIR_:
case LS:
command_ls(chp, data, sfd_client);
break;
case LDIR:
case LLS:
if(!getcwd(lpwd, sizeof lpwd))
er("getcwd()", 0);
command_lls(lpwd);
break;
case MKDIR:
if(cmd->npaths)
command_mkdir(chp, data, sfd_client, *cmd->paths);
else
fprintf(stderr, "No path to directory given.\n");
break;
case LMKDIR:
if(cmd->npaths)
command_lmkdir(*cmd->paths);
else
fprintf(stderr, "No path to directory given.\n");
break;
case RGET:
if(!getcwd(lpwd, sizeof lpwd))
er("getcwd()", 0);
command_rget(chp, data, sfd_client);
if((x = chdir(lpwd)) == -1)
fprintf(stderr, "Wrong path.\n");
break;
case RPUT:
if(!getcwd(lpwd, sizeof lpwd))
er("getcwd()", 0);
command_rput(chp, data, sfd_client);
if((x = chdir(lpwd)) == -1)
fprintf(stderr, "Wrong path.\n");
break;
case EXIT:
goto outside_client_command_loop;
default:
// display error
break;
}
}
outside_client_command_loop:
/*
chp->type = REQU;
chp->conid = -1;
strcpy(path, argv[1]);
strcpy(chp->buffer, argv[1]);
//printpacket(chp, HP);
data = htonp(chp);
if((x = send(sfd_client, data, size_packet, 0)) != size_packet)
er("send()", x);
set0(data);
do
{
if((x = recv(sfd_client, data, size_packet, 0)) <= 0)
er("recv()", x);
chp = htonp(data);
if(chp->type == INFO)
printf(ID "Server says: %s\n", chp->buffer);
else if(chp->type == DATA)
{
//printpacket(chp, HP);
receive_file(extract_filename(path), sfd_client, chp);
}
}
while(chp->type != TERM);
fprintf(stderr, "TERM received; exiting...\n");
*/
close(sfd_client);
printf(ID "Done.\n");
fflush(stdout);
return 0;
}