Skip to content

Commit 595ba25

Browse files
committed
move helpers into helpers.h and helpers.c
1 parent 8d221d6 commit 595ba25

File tree

3 files changed

+247
-218
lines changed

3 files changed

+247
-218
lines changed

server/helpers.c

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
#include <time.h>
2+
#include <sys/types.h>
3+
#include <sys/socket.h>
4+
#include <strings.h>
5+
#include <string.h>
6+
#include <arpa/inet.h>
7+
#include <unistd.h>
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <ctype.h>
11+
#include <netinet/in.h>
12+
#include <stdbool.h>
13+
#include <netdb.h>
14+
#include <errno.h>
15+
#include <fcntl.h>
16+
#include <time.h>
17+
#include <netinet/tcp.h>
18+
#include <sys/stat.h>
19+
#include <sys/types.h>
20+
#include <dirent.h>
21+
22+
#define MAXLINE 4096
23+
#define BACKLOGS 1024
24+
#define TRUE 1
25+
#define FALSE 0
26+
27+
#define GRN "\x1B[32m"
28+
#define RED "\x1B[31m"
29+
#define WHT "\x1B[37m"
30+
#define BLU "\x1B[34m"
31+
#define YEL "\x1B[33m"
32+
#define MAG "\x1B[35m"
33+
#define CYN "\x1B[36m"
34+
#define RESET "\x1B[0m"
35+
36+
void rm_lt_spaces(char *str){
37+
int left = 0, right = strlen(str) - 1;
38+
39+
while (isspace((unsigned char) str[left]))
40+
left++;
41+
42+
while ((right >= left) && isspace((unsigned char) str[right]))
43+
right--;
44+
45+
int i;
46+
47+
for (i = left; i <= right; i++)
48+
str[i - left] = str[i];
49+
50+
str[i - left] = '\0';
51+
52+
return;
53+
}
54+
55+
int get_client_ip_port(char *str, char *client_ip, int *client_port){
56+
printf("Request received\n");
57+
char *n1, *n2, *n3, *n4, *n5, *n6;
58+
int x5, x6;
59+
60+
strtok(str, " ");
61+
n1 = strtok(NULL, ",");
62+
n2 = strtok(NULL, ",");
63+
n3 = strtok(NULL, ",");
64+
n4 = strtok(NULL, ",");
65+
n5 = strtok(NULL, ",");
66+
n6 = strtok(NULL, ",");
67+
68+
sprintf(client_ip, "%s.%s.%s.%s", n1, n2, n3, n4);
69+
70+
x5 = atoi(n5);
71+
x6 = atoi(n6);
72+
*client_port = (256*x5)+x6;
73+
74+
printf("client_ip: %s client_port: %d\n", client_ip, *client_port);
75+
return 1;
76+
}
77+
78+
int setup_data_connection(int *fd, char *client_ip, int client_port, int server_port){
79+
80+
struct sockaddr_in cliaddr, tempaddr;
81+
82+
if ( (*fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
83+
perror("socket error");
84+
return -1;
85+
}
86+
87+
//bind port for data connection to be server port - 1 by using a temporary struct sockaddr_in
88+
memset(&tempaddr, '\0', sizeof(tempaddr));
89+
tempaddr.sin_family = AF_INET;
90+
tempaddr.sin_addr.s_addr = htonl(INADDR_ANY);
91+
tempaddr.sin_port = htons(server_port-1);
92+
93+
while((bind(*fd, (struct sockaddr*) &tempaddr, sizeof(tempaddr))) < 0){
94+
//perror("bind error");
95+
server_port--;
96+
tempaddr.sin_port = htons(server_port);
97+
}
98+
99+
100+
//initiate data connection fd with client ip and client port
101+
memset(&cliaddr, '\0', sizeof(cliaddr));
102+
cliaddr.sin_family = AF_INET;
103+
cliaddr.sin_port = htons(client_port);
104+
if (inet_pton(AF_INET, client_ip, &cliaddr.sin_addr) <= 0){
105+
perror("inet_pton error");
106+
return -1;
107+
}
108+
109+
if (connect(*fd, (struct sockaddr *) &cliaddr, sizeof(cliaddr)) < 0){
110+
perror("connect error");
111+
return -1;
112+
}
113+
114+
return 1;
115+
}
116+
117+
int get_filename(char *input, char *fileptr){
118+
119+
char *filename = NULL;
120+
filename = strtok(input, " ");
121+
filename = strtok(NULL, " ");
122+
if(filename == NULL){
123+
return -1;
124+
}else{
125+
strncpy(fileptr, filename, strlen(filename));
126+
return 1;
127+
}
128+
}
129+
130+
int get_cmd_code(char *command){
131+
char temp_cmd[1024];
132+
strcpy(temp_cmd, command);
133+
char *str = strtok(temp_cmd, " ");
134+
int cmd_code;
135+
136+
// command code
137+
if(strcmp(str, "LIST") == 0) cmd_code = 1;
138+
else if(strcmp(str, "RETR") == 0) cmd_code = 2;
139+
else if(strcmp(str, "SKIP") == 0) cmd_code = 4;
140+
else if(strcmp(str, "ABOR") == 0) cmd_code = 5;
141+
142+
return cmd_code;
143+
}
144+
145+
int _ls(int controlfd, int datafd, char *input){
146+
char filelist[1024], sendline[MAXLINE+1], str[MAXLINE+1];
147+
memset(filelist, '\0', (int)sizeof(filelist));
148+
149+
if(get_filename(input, filelist) > 0){
150+
printf("Filelist Detected\n");
151+
sprintf(str, "ls %s", filelist);
152+
printf("Filelist: %s\n", filelist);
153+
rm_lt_spaces(filelist);
154+
//verify that given input is valid
155+
/*struct stat statbuf;
156+
stat(filelist, &statbuf);
157+
if(!(S_ISDIR(statbuf.st_mode))) {
158+
sprintf(sendline, "550 No Such File or Directory\n");
159+
write(controlfd, sendline, strlen(sendline));
160+
return -1;
161+
}*/
162+
DIR *dir = opendir(filelist);
163+
if(!dir){
164+
sprintf(sendline, "550 No Such File or Directory\n");
165+
write(controlfd, sendline, strlen(sendline));
166+
return -1;
167+
}else{closedir(dir);}
168+
169+
}else{
170+
sprintf(str, "ls");
171+
}
172+
173+
//initiate file pointer for popen()
174+
FILE *in;
175+
extern FILE *popen();
176+
177+
if (!(in = popen(str, "r"))) {
178+
sprintf(sendline, "451 Requested action aborted. Local error in processing\n");
179+
write(controlfd, sendline, strlen(sendline));
180+
return -1;
181+
}
182+
183+
while (fgets(sendline, MAXLINE, in) != NULL) {
184+
write(datafd, sendline, strlen(sendline));
185+
printf("%s", sendline);
186+
memset(sendline, '\0', (int)sizeof(sendline));
187+
}
188+
189+
sprintf(sendline, "200 Command OK");
190+
write(controlfd, sendline, strlen(sendline));
191+
pclose(in);
192+
193+
return 1;
194+
}
195+
196+
int _get(int controlfd, int datafd, char *input){
197+
char filename[1024], sendline[MAXLINE+1], str[MAXLINE+1];
198+
memset(filename, '\0', (int)sizeof(filename));
199+
memset(sendline, '\0', (int)sizeof(sendline));
200+
memset(str, '\0', (int)sizeof(str));
201+
202+
203+
if(get_filename(input, filename) > 0){
204+
sprintf(str, "cat %s", filename);
205+
206+
if((access(filename, F_OK)) != 0){
207+
sprintf(sendline, "550 No Such File or Directory\n");
208+
write(controlfd, sendline, strlen(sendline));
209+
return -1;
210+
}
211+
}else{
212+
printf("Filename Not Detected\n");
213+
sprintf(sendline, "450 Requested file action not taken.\nFilename Not Detected\n");
214+
write(controlfd, sendline, strlen(sendline));
215+
return -1;
216+
}
217+
218+
FILE *in;
219+
extern FILE *popen();
220+
221+
if (!(in = popen(str, "r"))) {
222+
sprintf(sendline, "451 Requested action aborted. Local error in processing\n");
223+
write(controlfd, sendline, strlen(sendline));
224+
return -1;
225+
}
226+
227+
while (fgets(sendline, MAXLINE, in) != NULL) {
228+
write(datafd, sendline, strlen(sendline));
229+
//printf("%s", sendline);
230+
memset(sendline, '\0', (int)sizeof(sendline));
231+
}
232+
233+
sprintf(sendline, "200 Command OK");
234+
write(controlfd, sendline, strlen(sendline));
235+
pclose(in);
236+
return 1;
237+
}

server/helpers.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "helpers.c"
2+
3+
void rm_lt_spaces(char *str);
4+
int get_client_ip_port(char *str, char *client_ip, int *client_port);
5+
int setup_data_connection(int *fd, char *client_ip, int client_port, int server_port);
6+
int get_filename(char *input, char *fileptr);
7+
int get_cmd_code(char *command);
8+
int _ls(int controlfd, int datafd, char *input);
9+
int _get(int controlfd, int datafd, char *input);

0 commit comments

Comments
 (0)