-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
153 lines (129 loc) · 2.7 KB
/
server.c
File metadata and controls
153 lines (129 loc) · 2.7 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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <dirent.h>
#include <stddef.h>
#include <sys/utsname.h>
char LS = '1';
char FILES_NUMBER = '2';
char OS_INFO = '3';
int sock;
char buf[1024];
const char * getLS(char * path) {
int length = 0;
DIR * d;
struct dirent * dir;
char * res = (char *)malloc(1024);
d = opendir(path);
if (d) {
while ((dir = readdir(d)) != NULL) {
if (!strcmp(dir->d_name, "."))
continue;
if (!strcmp(dir->d_name, ".."))
continue;
if (!dir->d_type == DT_REG || !dir->d_type == DT_DIR)
continue;
strcat(res, dir->d_name);
strcat(res, "\n");
}
printf("%s\n%s\n", "Sending folder info", path);
}
else {
strcat(res, "There is no file or directory:\n");
strcat(res, path);
printf("%s\n%s\n", "There is no file or directory:", path);
}
send(sock, res, strlen(res), 0);
closedir(d);
return res;
}
void getFilesNumber(char * path) {
int count = 0;
char append[5];
DIR * d;
struct dirent * dir;
char * res = (char *)malloc(256);
d = opendir(path);
if (d) {
while ((dir = readdir(d)) != NULL) {
if (dir->d_type == DT_REG) {
count++;
printf("%s\n", dir->d_name);
}
}
strcat(res, "Files in directory:\n");
strcat(res, path);
strcat(res, "\n");
sprintf(append, "%d", count);
strcat(res, append);
printf("%s\n%s\n%d\n", "Files in directory:", path, count);
}
else {
strcat(res, "There is no file or directory:\n");
strcat(res, path);
printf("%s\n%s\n", "There is no file or directory:", path);
}
send(sock, res, strlen(res), 0);
closedir(d);
}
void getOSInfo() {
char * res = (char *)malloc(256);
struct utsname sysinfo;
uname(&sysinfo);
strcat(res, "System name:\n");
strcat(res, sysinfo.sysname);
printf("%s\n%s\n", "System name:", sysinfo.sysname);
send(sock, res, strlen(res), 0);
}
void manager(char * buf) {
char task = '0';
task = buf[0];
buf++;
printf("%c\n", task);
if (task == LS)
getLS(buf);
if (task == FILES_NUMBER)
getFilesNumber(buf);
if (task == OS_INFO)
getOSInfo();
}
int main()
{
int listener;
struct sockaddr_in addr;
int bytes_read;
listener = socket(AF_INET, SOCK_STREAM, 0);
if (listener < 0)
{
perror("socket");
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(3425);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(listener, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
exit(2);
}
listen(listener, 1);
while (1)
{
sock = accept(listener, 0, 0);
if (sock < 0)
{
perror("accept");
exit(3);
}
while (1)
{
memset(buf, 0, sizeof buf);
//buf[0] = 0;
bytes_read = recv(sock, buf, 1024, 0);
if (bytes_read <= 0) break;
manager(buf);
}
close(sock);
}
return 0;
}