-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathftutil.c
187 lines (154 loc) · 5.54 KB
/
ftutil.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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Program: ftutil.c
* Author: Nathan Cochran
* Date: 11/17/2013
* Description: Contains simple utility functions
* used in both ftclient.c and ftserve.c.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "ftutil.h"
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Sends a message over the specified socket
* Param: int socket_fd - File descriptor of connection to send message over
* Param: char * message - Message to send
* Return: void
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void send_message(int socket_fd, char * message) {
int length = strlen(message);
if(write(socket_fd, message, length) != length) {
perror("Error writing message");
close(socket_fd);
exit(EXIT_FAILURE);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Creates a new IPv4 TCP socket
* Param: void
* Return: int - File descriptor of the newly created socket
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int create_socket(void) {
int socket_fd;
//Create socket:
if((socket_fd = socket(AF_INET, SOCK_STREAM, PROTOCOL)) == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
return socket_fd;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Binds the socket to the specified port. Used in creating a passive socket
* Param: int socket_fd - File descriptor of the socket to bind
* Param: unsigned short port - The port number to bind the socket to
* Return: void
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void bind_socket(int socket_fd, unsigned short port) {
struct sockaddr_in address;
//Create address:
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
//Bind address to socket:
if(bind(socket_fd, (struct sockaddr *) &address, sizeof(address)) == -1) {
perror("Error binding socket to address");
close(socket_fd);
exit(EXIT_FAILURE);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Makes the specified socket listen for incoming connections
* Param: int socket_fd - Socket to listen for connections
* Return: void
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void listen_socket(int socket_fd) {
if(listen(socket_fd, BACKLOG) == -1) {
perror("Error listening for incoming connections");
close(socket_fd);
exit(EXIT_FAILURE);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Parses the client's command from the buffer, and returns the appropriate command type identifier
* Param: char * buffer - Buffer containing the user's raw command
* Param: char * arg - Buffer to return any arguments passed in with the command
* Return: int - The command type identifier
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int parse_command(char * buffer, char * arg) {
int command = INVALID;
//Skip over whitespace:
while(*buffer == ' ' || *buffer == '\t') {
buffer++;
}
//Determine the command given:
if(strncmp(buffer, "list ", 5) == 0 ||
strncmp(buffer, "list\t", 5) == 0 ||
strncmp(buffer, "list\n", 5) == 0) {
buffer = buffer + 4;
command = LIST;
}
else if(strncmp(buffer, "get ", 4) == 0 ||
strncmp(buffer, "get\t", 4) == 0 ||
strncmp(buffer, "get\n", 4) == 0) {
buffer = buffer + 3;
command = GET;
}
else if(strncmp(buffer, "cd ", 3) == 0 ||
strncmp(buffer, "cd\t", 3) == 0 ||
strncmp(buffer, "cd\n", 3) == 0) {
buffer = buffer + 2;
command = CD;
}
else if(strncmp(buffer, "pwd ", 4) == 0 ||
strncmp(buffer, "pwd\t", 4) == 0 ||
strncmp(buffer, "pwd\n", 4) == 0) {
buffer = buffer + 3;
command = PWD;
}
else if(strncmp(buffer, "exit ", 5) == 0 ||
strncmp(buffer, "exit\t", 5) == 0 ||
strncmp(buffer, "exit\n", 5) == 0) {
buffer = buffer + 4;
command = EXIT;
}
//Get the argument given:
if(arg != NULL) {
//Skip over whitespace:
while(*buffer == ' ' || *buffer == '\t') {
buffer++;
}
//Copy the argument over:
while((*buffer != ' ') && (*buffer != '\n') && (*buffer != '\r') && (*buffer != '\0')) {
*arg = *buffer;
arg++;
buffer++;
}
//Null-terminate:
*arg = '\0';
}
return command;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Prompts the user for a yes/no answer. Returns 1 for yes, 0 for no.
* Param: char * prompt - The prompt to display
* Return: int - 1 for yes, 0 for no
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int input_yn(char * prompt) {
char buf[BUF_SIZE];
printf("%s", prompt);
while(1) {
fgets(buf, BUF_SIZE, stdin);
if(strncmp(buf, "yes\n", 4) == 0 ||
strncmp(buf, "yes ", 4) == 0 ||
strncmp(buf, "y\n", 2) == 0 ||
strncmp(buf, "y ", 2) == 0) {
return 1;
}
if(strncmp(buf, "no\n", 3) == 0 ||
strncmp(buf, "no ", 3) == 0 ||
strncmp(buf, "n\n", 2) == 0 ||
strncmp(buf, "n ", 2) == 0) {
return 0;
}
printf("Invalid input. Please try again.\n");
printf("%s", prompt);
}
return -1;
}