forked from pubdigital/simpleftp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myftp_skel.c
203 lines (154 loc) · 4.21 KB
/
myftp_skel.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <err.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFSIZE 512
/**
* function: receive and analize the answer from the server
* sd: socket descriptor
* code: three leter numerical code to check if received
* text: normally NULL but if a pointer if received as parameter
* then a copy of the optional message from the response
* is copied
* return: result of code checking
**/
bool recv_msg(int sd, int code, char *text) {
char buffer[BUFSIZE], message[BUFSIZE];
int recv_s, recv_code;
// receive the answer
// error checking
if (recv_s < 0) warn("error receiving data");
if (recv_s == 0) errx(1, "connection closed by host");
// parsing the code and message receive from the answer
sscanf(buffer, "%d %[^\r\n]\r\n", &recv_code, message);
printf("%d %s\n", recv_code, message);
// optional copy of parameters
if(text) strcpy(text, message);
// boolean test for the code
return (code == recv_code) ? true : false;
}
/**
* function: send command formated to the server
* sd: socket descriptor
* operation: four letters command
* param: command parameters
**/
void send_msg(int sd, char *operation, char *param) {
char buffer[BUFSIZE] = "";
// command formating
if (param != NULL)
sprintf(buffer, "%s %s\r\n", operation, param);
else
sprintf(buffer, "%s\r\n", operation);
// send command and check for errors
}
/**
* function: simple input from keyboard
* return: input without ENTER key
**/
char * read_input() {
char *input = malloc(BUFSIZE);
if (fgets(input, BUFSIZE, stdin)) {
return strtok(input, "\n");
}
return NULL;
}
/**
* function: login process from the client side
* sd: socket descriptor
**/
void authenticate(int sd) {
char *input, desc[100];
int code;
// ask for user
printf("username: ");
input = read_input();
// send the command to the server
// relese memory
free(input);
// wait to receive password requirement and check for errors
// ask for password
printf("passwd: ");
input = read_input();
// send the command to the server
// release memory
free(input);
// wait for answer and process it and check for errors
}
/**
* function: operation get
* sd: socket descriptor
* file_name: file name to get from the server
**/
void get(int sd, char *file_name) {
char desc[BUFSIZE], buffer[BUFSIZE];
int f_size, recv_s, r_size = BUFSIZE;
FILE *file;
// send the RETR command to the server
// check for the response
// parsing the file size from the answer received
// "File %s size %ld bytes"
sscanf(buffer, "File %*s size %d bytes", &f_size);
// open the file to write
file = fopen(file_name, "w");
//receive the file
// close the file
fclose(file);
// receive the OK from the server
}
/**
* function: operation quit
* sd: socket descriptor
**/
void quit(int sd) {
// send command QUIT to the client
// receive the answer from the server
}
/**
* function: make all operations (get|quit)
* sd: socket descriptor
**/
void operate(int sd) {
char *input, *op, *param;
while (true) {
printf("Operation: ");
input = read_input();
if (input == NULL)
continue; // avoid empty input
op = strtok(input, " ");
// free(input);
if (strcmp(op, "get") == 0) {
param = strtok(NULL, " ");
get(sd, param);
}
else if (strcmp(op, "quit") == 0) {
quit(sd);
break;
}
else {
// new operations in the future
printf("TODO: unexpected command\n");
}
free(input);
}
free(input);
}
/**
* Run with
* ./myftp <SERVER_IP> <SERVER_PORT>
**/
int main (int argc, char *argv[]) {
int sd;
struct sockaddr_in addr;
// arguments checking
// create socket and check for errors
// set socket data
// connect and check for errors
// if receive hello proceed with authenticate and operate if not warning
// close socket
return 0;
}