-
Notifications
You must be signed in to change notification settings - Fork 0
/
libnetfiles.c
286 lines (244 loc) · 7.54 KB
/
libnetfiles.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
#include "libnetfiles.h"
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
int file_mode = -1;
int port = 19210;
struct hostent *server = NULL;
// Helper functions//
/*
Turns a string into an int (string needs to have numbers in it)
*/
int string_to_int(char *string) {
char *temp;
return strtol(string, &temp, 10);
}
/*
Converts an int to a string
*/
char *int_to_string(int integer) {
int maxNumDigits = 12;
char *return_string = (char *)malloc(maxNumDigits + 1);
sprintf(return_string, "%d", integer);
return return_string;
}
/*
Returns -1 on failure and sets errno appropriately
Otherwise returns the created network socket.
*/
int createsocket() {
int sockfd = 0;
struct sockaddr_in serv_addr;
// char buffer[256];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
return -1;
}
// Erase data in n bytes
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET; // Type of address
// Copy net address to sockaddr_in.s_addr (struct)
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,
server->h_length);
// Set sockaddr_in port. Convert port integer to network short
serv_addr.sin_port = htons(port);
// Connect takes socket descriptor, sockaddr_in, and size of sockaddr_in
// struct It returns 0 if sucessful.
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
return -1; // Takes care of ETIMEOUT
}
return sockfd;
}
int netserverinit(char *hostname, int filemode) {
if (filemode < 0 || filemode > 2) {
errno = EACCES;
return -1;
}
file_mode = filemode;
server = gethostbyname(hostname);
if (server == NULL) {
h_errno = HOST_NOT_FOUND;
return -1;
}
return 0;
}
int netopen(const char *pathname, int flags) {
if (server == NULL) {
h_errno = HOST_NOT_FOUND;
return -1;
}
if (flags < 0 || flags > 2) {
errno = EACCES;
return -1;
}
int sockfd = createsocket();
if(sockfd == -1){
return -1;
}
// convert flags variable to string
char *flags_string = int_to_string(flags);
// Open + pathname + flags_string + 2 spaces
char *buffer = malloc((5 + strlen(pathname) + 1 + strlen(flags_string) + 3));
strcat(buffer, "open^");
strcat(buffer, pathname);
strcat(buffer, "^");
strcat(buffer, flags_string);
strcat(buffer, "^");
strcat(buffer, int_to_string(file_mode));
int numbytesWritten = write(sockfd, buffer, strlen(buffer));
// Write up to strlen(buffer) bytes from buffer into sockfd
// Returns numbytesWritten
if (numbytesWritten == -1) {
return -1;
}
// Get server's response
char response[256];
int numbytesRead = read(sockfd, response, 255); // read from server
if (numbytesRead == -1) {
return -1;
}
// printf("[%s], Message to server: %s\n", __func__, buffer);
// printf("[%s], Response from server: %s\n", __func__, response);
char *first_word_of_message = strtok(response, "^");
if (strcmp(first_word_of_message, "error") == 0) {
// set errno
char *errnostring = strtok(NULL, " ");
errno = string_to_int(errnostring);
return -1;
}
// Convert the string FD into a int and return it
int filedescriptor = string_to_int(first_word_of_message);
close(sockfd);
return filedescriptor;
}
ssize_t netread(int fildes, void *buf, size_t nbyte) {
if (server == NULL) {
h_errno = HOST_NOT_FOUND;
return -1;
}
int sockfd = createsocket();
if (sockfd == -1) {
return -1;
}
char *fd_string = int_to_string(fildes);
char *numbytes_string = int_to_string(nbyte);
// "read" + fildes + #bytes + 4 (2 spaces + null terminator)
char *msg_to_server = (char *)malloc(5 + strlen(fd_string) + 1 + strlen(numbytes_string) + 1);
strcat(msg_to_server, "read^");
strcat(msg_to_server, fd_string);
strcat(msg_to_server, "^");
strcat(msg_to_server, numbytes_string);
strcat(msg_to_server, "\0");
int numbytesWritten = write(sockfd, msg_to_server, strlen(msg_to_server));
if (numbytesWritten < 0) {
return -1;
}
// response from server
char *response = (char *)malloc(nbyte + 16);
int numbytesRead = read(sockfd, response, nbyte + 16); // read from server
if (numbytesRead == -1) {
return -1;
}
// printf("[%s], Message to server: %s\n", __func__, msg_to_server);
// printf("[%s], Response from server: %s\n", __func__, response);
char delimiter = '^';
char *first_word_of_message = (char *)malloc(strlen(response));
strncpy(first_word_of_message, response, strlen(response));
int i = 0;
first_word_of_message++; // move one past the delimiter
char *servermsg = first_word_of_message;
while (servermsg[i] != delimiter) {
servermsg += sizeof(char);
}
servermsg[i] = '\0';
// Move servermsg to after second occurance of delimiter. That is where the
// server message starts
servermsg = &servermsg[i + 1];
if (strcmp(first_word_of_message, "error") == 0) {
errno = string_to_int(servermsg);
return -1;
}
if(strcmp(response, "")){
errno = ECONNRESET;
return -1;
}
// Copy message into buffer
strncpy(buf, servermsg, strlen(servermsg));
return numbytesRead;
}
ssize_t netwrite(int fildes, const void *buf, size_t nbyte) {
if (server == NULL) {
h_errno = HOST_NOT_FOUND;
return -1;
}
int sockfd = createsocket();
if (sockfd == -1) {
return -1;
}
char *fd_string = int_to_string(fildes);
char *numbytes_string = int_to_string(nbyte);
// "write" + fildes + buffer + #bytes + 3 (2 spaces + null terminator)
char *msg_to_server = (char *)malloc(9 + strlen(fd_string) + strlen(buf) +
strlen(numbytes_string));
strcat(msg_to_server, "write^");
strcat(msg_to_server, fd_string);
strcat(msg_to_server, "^");
strcat(msg_to_server, buf);
strcat(msg_to_server, "^");
strcat(msg_to_server, numbytes_string);
strcat(msg_to_server, "\0");
int numbytesWritten = write(sockfd, msg_to_server, strlen(msg_to_server));
if (numbytesWritten == -1) {
return -1;
}
char *response = (char *)malloc(strlen("error") + 1 + 14);
int numbytesRead = read(sockfd, response, 20); // read from server
// printf("[%s], Message to server: %s\n", __func__, msg_to_server);
// printf("[%s], Response from server: %s\n", __func__, response);
if(numbytesRead == -1){
return -1;
}
char *first_word_of_message = strtok(response, "^");
if (strcmp(first_word_of_message, "error") == 0) {
char *errnostr = strtok(NULL, "^");
errno = string_to_int(errnostr);
return -1;
}
if(strcmp(response, "") == 0){
errno = ECONNRESET;
return -1;
}
return string_to_int(first_word_of_message);
}
int netclose(int fd) {
if (server == NULL) {
h_errno = HOST_NOT_FOUND;
return -1;
}
int sockfd = createsocket();
if(sockfd == -1){
return -1;
}
char *fd_string = int_to_string(fd);
char *msg_to_server = (char *)malloc(7 + strlen(fd_string));
strcat(msg_to_server, "close^");
strcat(msg_to_server, fd_string);
write(sockfd, msg_to_server, strlen(msg_to_server));
char *response = (char *)malloc(strlen("error") + 15);
read(sockfd, response, 16); // read from server
// printf("[%s], Message to server: %s\n", __func__, msg_to_server);
// printf("[%s], Response from server: %s\n", __func__, response);
char *first_word_of_message = strtok(response, "^");
if (strcmp(first_word_of_message, "error") == 0) {
char *errnostr = strtok(NULL, "^");
errno = string_to_int(errnostr);
return -1;
}
return string_to_int(first_word_of_message);
}