-
Notifications
You must be signed in to change notification settings - Fork 1
/
prog5cli.c
307 lines (236 loc) · 5.38 KB
/
prog5cli.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
/********************************************************************
prog5cli.c
Class: CSCI 631 Network Applications Programming
Program: Assignment 5
Author: Vishrant K Gupta
Z-number: z1815637
Date Due: 04/28/17
Purpose: FTP
Execution: Make execute N=5 T=1
*********************************************************************/
#include "prog5cli.h"
#include "wrap5cli.h"
#include "util5cli.h"
/*
* Name: close_connection
*
* Prototype:
* void close_connection(int socket_fd)
*
* Description:
* Close connection
*
* Header files:
* prog5cli.c
*
*/
void close_connection(int socket_fd);
/*
* Name: client
*
* Prototype:
* bool client(char* ip_addr, char* server_port, bool prompt)
*
* Description:
* Executes command on client and server
*
* Header files:
* prog5cli.c
*
*/
bool client(char* ip_addr, char* server_port, bool prompt) {
// removing backslash
trim(ip_addr);
trim(server_port);
// socket fd
int socket_fd = -1;
// port number
int port_no = -1;
// connection return value
int conn = 0;
// inet pton return value
int inet_msg = 0;
// sock addr struct
struct sockaddr_in servaddr;
// resetting values
bzero(&servaddr, sizeof(servaddr));
// ip v4 family type
servaddr.sin_family = AF_INET;
// taking default port if no port is provided
if (server_port[0] == BACKSLASH_ZERO || server_port[0] == BACKSLASH_N) {
port_no = DEFAULT_PORT;
} else {
// if port is provided
port_no = atoi(server_port);
}
// converting to network byte order
servaddr.sin_port = htons(port_no);
// converting to machine understandable format
inet_msg = Inet_pton(AF_INET, ip_addr, &servaddr.sin_addr);
// if in correct address and return control to FTP
if (inet_msg < 0) {
return false;
}
// creating socket
socket_fd = Socket(AF_INET, SOCK_STREAM, 0);
// if socket not created return control to FTP
if (socket_fd < 0) {
return false;
}
// creating connection
conn = Connect(socket_fd, (struct sockaddr*) &servaddr, sizeof(servaddr));
// if connection failed return control to FTP
if (conn < 0) {
return false;
}
// print connection successful message
printf(CONNECTED_MSG, ip_addr);
return str_cli(socket_fd, ip_addr, prompt);
}
/*
* Name: str_cli
*
* Prototype:
* bool str_cli(int sockfd, char* saddr, bool prompt)
*
* Description:
* Executes command on client and server
*
* Header files:
* prog5cli.c
*
*/
bool str_cli(int sockfd, char* saddr, bool prompt) {
// user input
char buffer[MAXLINE];
// command
char command[MAXLINE];
// argument 1
char arg1[MAXLINE];
// argument 2
char arg2[MAXLINE];
// command id
cmd_id id;
// quit flag
bool quit = false;
// return value to FTP
bool return_val = true;
// printing prompt
printf(PROMPT);
while (!quit) {
// resetting quit
quit = false;
// initializing variables
memset(command, 0, MAXLINE);
memset(arg1, 0, MAXLINE);
memset(arg2, 0, MAXLINE);
// taking user nput
Fgets(buffer, MAXLINE, stdin);
// parsing user input
id = get_args(buffer, command, arg1, arg2);
// switch
switch (id) {
case NEG_: // invalid command
fprintf(stderr, INVALID_CMD);
break;
case QUIT_: // quit and bye command
case BYE_:
// closing connection
close_connection(sockfd);
// no prompt
prompt = false;
// quit true
quit = true;
// quitting ftp from FTP main
return_val = true;
break;
case HELP_: // help and ? command
case QM_:
// print whole list
if (arg1[0] == BACKSLASH_ZERO) {
prnt_cmd_list();
} else {
// print for perticular command
prnt_cmd_help(arg1);
}
break;
case LCD_: // lcd, cd and cdup command
case CD_:
case CDUP_:
// calling new dir for handling these command
new_dir(arg1, id, sockfd);
break;
case OPEN_: // open command, already connected
printf(ALREADY_CONNECTED, saddr);
break;
case CLOSE_: // close and disconnect command
case DISCONNECT_:
close_connection(sockfd);
// no prompt
prompt = false;
// return value false to exit
return_val = false;
// quit from whle loop
quit = true;
break;
case PWD_: // pwd command
// send to server
str_echo(sockfd, buffer);
break;
case ZERO_: // enter key pressed
default:
break;
}
// print prompt based on prompt flag
if (prompt) {
printf(PROMPT);
}
}
return return_val;
}
/*
* Name: close_connection
*
* Prototype:
* void close_connection(int socket_fd)
*
* Description:
* close connection
*
* Header files:
* prog5cli.c
*
*/
void close_connection(int socket_fd) {
Close(socket_fd);
printf(GOODBYE);
}
/*
* Name: str_echo
*
* Prototype:
* void str_echo(int sockfd, char* command)
*
* Description:
* send to server
*
* Header files:
* prog5cli.c
*/
void str_echo(int sockfd, char* command) {
// received data
char received_data[MAXLINE];
// writing to server
Writen(sockfd, command, strlen(command));
Writen(sockfd, NEWLINE, strlen(NEWLINE));
// reading from server
Readline(sockfd, received_data, MAXLINE);
// if value at index 0 is 0 then its error
// then print strerror
if (received_data[0] != ZERO) {
fprintf(stderr, "%s\n", strerror(atoi(received_data)));
} else {
// command executed successfully in server, ignoring success code
printf("%s", (received_data + IGNORE_SUCCUESS_CODE));
}
}