-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
159 lines (133 loc) · 3.79 KB
/
server.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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include "common.h"
#define PORT "6969"
static const char* prefix = "./server_dir/";
int put(int fd, const char* filename) {
char* filename_local = prefix_it(filename, prefix);
int retval = socket_to_file(fd, filename_local);
free(filename_local);
return retval;
}
int get(int fd, const char* filename) {
char* filename_local = prefix_it(filename, prefix);
int retval = file_to_socket(fd, filename_local);
free(filename_local);
return retval;
}
int main(void) {
struct addrinfo hints;
struct addrinfo *result, *rp;
int sfd, s;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
s = getaddrinfo(NULL, PORT, &hints, &result);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
/* getaddrinfo() returns a list of address structures.
Try each address until we successfully bind(2).
If socket(2) (or bind(2)) fails, we (close the socket
and) try the next address. */
for (rp = result; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (sfd == -1)
continue;
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0) {
fprintf(stderr, "Binding!\n");
break; /* Success */
}
close(sfd);
}
if (rp == NULL) { /* No address succeeded */
fprintf(stderr, "Could not bind\n");
exit(EXIT_FAILURE);
}
if(!(0 == listen(sfd, 0))) {
fprintf(stderr, "Listen failed\n");
exit(EXIT_FAILURE);
}
freeaddrinfo(result); /* No longer needed */
for (;;) {
struct sockaddr_storage their_addr;
socklen_t addr_size;
int sock;
if(-1 == (sock = accept(sfd, (struct sockaddr *)&their_addr, &addr_size))) {
fprintf(stderr, "accept failed\n");
break;
}
char header[6];
if(6 != recv(sock, header, 6, 0)) {
fprintf(stderr, "read failed\n");
close(sock); break;
}
uint16_t op;
memcpy(&op, header, 2);
op = ntohs(op);
uint32_t fn_sz;
memcpy(&fn_sz, header + 2, 4);
fn_sz = ntohl(fn_sz);
char* fn = malloc(fn_sz + 1);
if(fn_sz != recv(sock, fn, fn_sz, 0)) {
fprintf(stderr, "read failed\n");
close(sock); break;
}
fn[fn_sz] = '\0';
printf("Retrieved Op %s (%d) with Filename length %d for %s\n", op_tbl[op], op, fn_sz, fn);
msg_header_t rep;
if(op == REQUEST_UP) {
if(put(sock, fn) == 0) {
make_msg_header(&rep, REPLY_UP, fn);
if(rep.size != write(sock, rep.data, rep.size)) {
fprintf(stderr, "write reply failed\n");
}
} else {
make_msg_header(&rep, ERROR, fn);
if(rep.size != write(sock, rep.data, rep.size)) {
fprintf(stderr, "write reply failed\n");
}
}
} else if(op == REQUEST_DOWN) {
char* check = prefix_it(fn, prefix);
FILE* file = fopen(check, "rb");
if(!file) {
fprintf(stderr, "File error, sending error\n");
make_msg_header(&rep, ERROR, fn);
if(rep.size != write(sock, rep.data, rep.size)) {
fprintf(stderr, "write reply failed\n");
}
} else {
printf("Sending header and file\n");
make_msg_header(&rep, REPLY_DOWN, fn);
if(rep.size != write(sock, rep.data, rep.size)) {
fprintf(stderr, "write header failed\n");
}
fclose(file);
if(file_to_socket(sock, check) != 0) {
fprintf(stderr, "write reply failed\n");
}
}
free(check);
} else {
printf("Bogus opcode, rejecting\n");
}
free_msg_header(&rep);
close(sock);
}
close(sfd);
return EXIT_SUCCESS;
}