forked from Gance54/ClientServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
279 lines (224 loc) · 6.42 KB
/
server.c
File metadata and controls
279 lines (224 loc) · 6.42 KB
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
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <dirent.h>
#include <stdlib.h>
#include "log.h"
#define INET_PORT_DEFAULT 8080
#define NOT_FOUND_RESPONSE "File or directory not found - "
typedef int (*pfn_sent_response)(int , char *);
static int send_dir_info (int fd, char *path);
static int send_file_content(int fd, char *path);
static int send_not_found (int fd, char *path);
static pfn_sent_response response_funtions[] = {
&send_not_found,
&send_dir_info,
&send_file_content,
};
static void print_usage () {
printf("\nUsage:\n");
printf("./server -U <server_path> startet den Server mit Unix-Sockets,\n"
"./server -u <server_address> startet den Server mit UDP-Sockets,\n"
"./server -t <server_address> startet den Server mit TCP-Sockets,\n\n");
}
static int init_unix_socket (char *addr) {
struct sockaddr_un saddr;
int fd;
saddr.sun_family = AF_UNIX;
strncpy(saddr.sun_path, addr, sizeof(saddr.sun_path));
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
LOGE("Error during socket creation %s'n", strerror(errno));
return -1;
}
if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr))) {
LOGE("Error during socket creation %s'n", strerror(errno));
close(fd);
return -1;
}
return fd;
}
static int init_inet_socket (int type) {
int fd;
struct sockaddr_in addr;
if (type != SOCK_STREAM && type != SOCK_DGRAM) {
LOGE("Illegal socket type");
return -1;
}
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
LOGE("socket error = %s", strerror(errno));
return -1;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(INET_PORT_DEFAULT);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
LOGE("bind error = %s", strerror(errno));
close(fd);
return -1;
}
return fd;
}
static int indicate_path_type(const char *path) {
struct stat statbuf;
if (stat(path, &statbuf) != 0) {
LOGI("File %s not found.", path);
return 0;
}
return S_ISDIR(statbuf.st_mode) ? 1 : 2;
}
static int send_file_content(int fd, char *path) {
FILE *hFile = fopen(path, "rb");
size_t file_size;
unsigned char *file_content;
if(!hFile) {
LOGE("This should not happen");
return -1;
}
fseek(hFile, 0L, SEEK_END);
file_size = (size_t)ftell(hFile);
rewind(hFile);
file_content = malloc(file_size);
if (!file_content) {
LOGE("Run out of memory");
fclose(hFile);
return -1;
}
if (fread(file_content, sizeof(unsigned char), file_size, hFile) != file_size) {
LOGE("Faled to read the file %s path", path);
fclose(hFile);
free(file_content);
return -1;
}
fclose(hFile);
if (write(fd, file_content, file_size) != (ssize_t)file_size) {
LOGE("Failed to send data");
free(file_content);
return -1;
}
free(file_content);
return 0;
}
static int send_dir_info (int fd, char *path) {
DIR *d = opendir(path);
if (d) {
struct dirent *dir = readdir(d);
while (dir!= NULL) {
if (write(fd, dir->d_name, strlen(dir->d_name) != strlen(dir->d_name)))
LOGE("Failed to write data to a socket");
dir = readdir(d);
}
closedir(d);
}
else {
LOGE("This should not happen");
return -1;
}
return 0;
}
static int send_not_found (int fd, char *path) {
size_t data_len = strlen(path) + strlen(NOT_FOUND_RESPONSE);
char *data = malloc(data_len);
if (!data) {
LOGE("Run out of memory");
return -1;
}
sprintf(data, "%s%s", NOT_FOUND_RESPONSE, path);
if (write(fd, data, data_len) != (ssize_t)data_len) {
LOGE("Failed to send response");
return -1;
}
return 0;
}
static int handle_input_connection(int fd) {
char path [PATH_MAX] = { 0, };
ssize_t len = read(fd, path, sizeof(path));
pfn_sent_response response_fn;
if (len <= 0 || len == sizeof(path)) {
LOGE("Something went wrong. Read returned %zu", len);
return -1;
}
response_fn = response_funtions[indicate_path_type(path)];
if (response_fn(fd, path)) {
LOGE("Response function failed");
}
else
LOGI("Response sent");
close(fd);
return 0;
}
int main(int argc, char *argv[]) {
int fd = -1;
if (argc != 3 || argv[1][0] != '-') {
print_usage();
return -1;
}
switch (argv[1][1]) {
case 'U':
fd = init_unix_socket(argv[2]);
break;
case 'u':
LOGI("Starting UDP inet socket on local IP address. Any input address will be ignored...");
fd = init_inet_socket(SOCK_DGRAM);
break;
case 't':
LOGI("Starting TCP inet socket on local IP address. Any input address will be ignored...");
fd = init_inet_socket(SOCK_STREAM);
break;
default:
LOGE("Unknown argument");
print_usage();
}
if (fd < 0) {
LOGE("Error happened. Leaving...");
return -1;
}
if (listen(fd, 1)) {
LOGE("listen err = %s", strerror(errno));
close(fd);
}
while (1) {
int session_fd = accept(fd, NULL, NULL);
if (session_fd <= 0) {
LOGE("Failed to accept. something went wrong...");
break;
}
if (handle_input_connection(session_fd))
close(session_fd);
}
return 0;
}
/*static int init_inet_socket (char *ip, int type) {
struct sockaddr_in sockaddrin;
struct hostent *host;
int fd;
if (type != SOCK_STREAM || type != SOCK_DGRAM) {
LOGE("Illegal socket type");
return -1;
}
if (ip[0] == ' ')
ip++;
fd = socket(AF_INET, type, 0);
if (fd == -1) {
LOGE("Socket Error %s", strerror(errno));
return -1;
}
host = gethostbyname(ip);
if (host == NULL) {
LOGE("|%s| - unknown host.", ip);
return -1;
}
sockaddrin.sin_family = AF_INET;
sockaddrin.sin_port = htons(INET_PORT_DEFAULT);
memcpy(&sockaddrin.sin_addr, host->h_addr, host->h_length);
return 0;
}*/