forked from guilherme-ls/SocketsHandler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsockets.cpp
More file actions
314 lines (258 loc) · 8.55 KB
/
sockets.cpp
File metadata and controls
314 lines (258 loc) · 8.55 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "sockets.hpp"
int SocketHandler::connection_socket;
// Starts main daemon's socket
void SocketHandler::start() {
unlink(SOCKET_NAME);
struct sockaddr_un address;
memset(&address, 0, sizeof(address));
// Creates a local main socket
connection_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (connection_socket == -1) {
perror("socket");
//exit(EXIT_FAILURE);
}
// Binds socket to defined address
address.sun_family = AF_UNIX;
strncpy(address.sun_path, SOCKET_NAME, sizeof(address.sun_path) - 1);
if (bind(connection_socket, (const struct sockaddr *) &address, sizeof(address)) == -1) {
perror("bind");
//exit(EXIT_FAILURE);
}
// Prepares for accepting connections
if (listen(connection_socket, 20) == -1) {
perror("listen");
//exit(EXIT_FAILURE);
}
}
void SocketHandler::openSocket(std::string name) {
struct sockaddr_un address;
memset(&address, 0, sizeof(address));
// Creates local socket
connection_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (connection_socket == -1) {
perror("socket");
//exit(EXIT_FAILURE);
}
printf("local socket created\n");
// Connects to main_daemon's socket adress
address.sun_family = AF_UNIX;
strncpy(address.sun_path, SOCKET_NAME, sizeof(address.sun_path) - 1);
if (connect(connection_socket, (const struct sockaddr *) &address, sizeof(address)) == -1) {
perror("The server is down");
//exit(EXIT_FAILURE);
}
printf("socket connected to main\n");
// Sends socket information to main
write(connection_socket, name.c_str(), name.length());
printf("Identity message sent");
// Confirmation
char buffer[5] = "null";
while(strcmp(buffer, "rcvd") != 0) {
read(connection_socket, buffer, 5);
}
printf("Identity confirmed");
}
void SocketHandler::listenClient(SocketHandler::Message* com) {
// Puts all sockets on list
fd_set fd_reads;
FD_ZERO(&fd_reads);
FD_SET(connection_socket, &fd_reads);
struct timeval time = {1,0};
// Checks wich sockets can be read
ssize_t activity = select(connection_socket + 1, &fd_reads, NULL, NULL, &time);
if ((activity < 0) && (errno!=EINTR)) {
printf("select error");
//exit(EXIT_FAILURE);
}
// Reads message, if present
if (FD_ISSET(connection_socket, &fd_reads)) {
int temp;
char buffer[STD_SIZE];
if((temp = read(connection_socket, buffer, STD_SIZE)) == -1) {
perror("read");
//exit(EXIT_FAILURE);
}
buffer[temp] = '\0';
// Returns message
*com = SocketHandler::strToMsg(buffer);
printf("Passou mensagem\n");
}
}
void SocketHandler::listenServer(int* client_sockets, std::string* connection_list, int size) {
// Puts all sockets on list
int max_fd = connection_socket;
fd_set fd_reads;
FD_ZERO(&fd_reads);
FD_SET(connection_socket, &fd_reads);
for (int i = 0; i < size; i++) {
if (client_sockets[i] != 0) {
FD_SET(client_sockets[i], &fd_reads);
if (client_sockets[i] > max_fd) {
max_fd = client_sockets[i];
}
}
}
struct timeval time = {1,0};
// Checks wich sockets can be read
ssize_t activity = select(max_fd + 1, &fd_reads, NULL, NULL, &time);
if ((activity < 0) && (errno!=EINTR)) {
printf("select error");
//exit(EXIT_FAILURE);
}
// If main socket can be read, accepts new connections
if (FD_ISSET(connection_socket, &fd_reads)) {
int temp;
struct sockaddr_un name;
// Accepts
if ((temp = accept(connection_socket, (struct sockaddr *) &name, (socklen_t *) sizeof(name))) == -1) {
perror("accept");
//exit(EXIT_FAILURE);
}
printf("Accepted new connection\n");
// Gets connected socket's identity
FD_ZERO(&fd_reads);
FD_SET(temp, &fd_reads);
select(1, &fd_reads, NULL, NULL, NULL);
char buffer[STD_SIZE];
read(temp, buffer, STD_SIZE);
for (int i = 0; i < size; i++) {
if (strcmp(buffer, connection_list[i].c_str()) == 0) {
client_sockets[i] = temp;
}
}
write(temp, "rcvd", 5);
printf("Stored new connection\n");
}
// Transfers messages between children
for (int i = 0; i < size; i++) {
if (FD_ISSET(client_sockets[i], &fd_reads)) {
char buffer[STD_SIZE];
if (read(client_sockets[i], buffer, STD_SIZE) == -1) {
perror("read");
//exit(EXIT_FAILURE);
}
SocketHandler::Message com = strToMsg(buffer);
transfer(com, client_sockets, size);
printf("Transfered message\n");
}
}
}
void SocketHandler::sendMessage(SocketHandler::Message com) {
std::string men = com.send_to + "," + com.sent_from + "," + com.message;
if (write(connection_socket, men.c_str(), men.length()) == -1) {
perror("message not sent");
//exit(EXIT_FAILURE);
}
}
void SocketHandler::transfer(SocketHandler::Message com, int* sockets, int size) {
if (std::stoi(com.send_to) < 0 || std::stoi(com.send_to) >= size) {
perror("invalid destination");
//exit(EXIT_FAILURE);
}
std::string men = com.send_to + "," + com.sent_from + "," + com.message;
if (write(sockets[std::stoi(com.send_to)], men.c_str(), men.length()) == -1) {
perror("message not sent");
//exit(EXIT_FAILURE);
}
}
SocketHandler::Message SocketHandler::strToMsg(char* com) {
std::string cut[3];
int i = 0, j = 0;
while (com[i] != '\0' && j < 3) {
if (com[i] != ',') {
cut[j] += com[i];
}
else {
j++;
}
i++;
}
SocketHandler::Message mes {cut[0], cut[1], cut[2]};
return mes;
}
int SocketHandler::getId(std::string name, std::string* name_list, int size) {
for (int i = 0; i < size; i++) {
if (name == name_list[i]) {
return i;
}
}
return -1;
}
void SocketHandler::closeSocket(int dis_socket) {
struct sockaddr_un name;
getpeername(dis_socket, (struct sockaddr *) &name, (socklen_t *) sizeof(name));
close(dis_socket);
unlink(name.sun_path);
}
/*
int NO(int argc, char *argv[]) {
struct sockaddr_un name;
int down_flag = 0;
int connection_socket;
int data_socket;
char buffer[STD_SIZE];
// Create local socket
connection_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (connection_socket == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
memset(&name, 0, sizeof(name));
// Bind socket to socket name
name.sun_family = AF_UNIX;
strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) - 1);
if (bind(connection_socket, (const struct sockaddr *) &name, sizeof(name)) == -1) {
perror("bind");
exit(EXIT_FAILURE);
}
// Prepare for accepting connections
if (listen(connection_socket, 20) == -1) {
perror("listen");
exit(EXIT_FAILURE);
}
while (1) {
// Wait for incoming connection
data_socket = accept(connection_socket, NULL, NULL);
if (data_socket == -1) {
perror("accept");
exit(EXIT_FAILURE);
}
while (1) {
// Wait for next data packet
if (read(data_socket, buffer, sizeof(buffer)) == -1) {
perror("read");
exit(EXIT_FAILURE);
}
// Ensure buffer is 0-terminated
buffer[sizeof(buffer) - 1] = 0;
// Stops reading when receives END signal
if (!strncmp(buffer, "END", sizeof(buffer))) {
break;
}
// Kills server when receives DOWN signal
if (!strncmp(buffer, "DOWN", sizeof(buffer))) {
down_flag = 1;
break;
}
printf("received = %s\n", buffer);
// Send result back
if (write(data_socket, buffer, sizeof(buffer)) == -1) {
perror("write");
exit(EXIT_FAILURE);
}
printf("sent = %s\n", buffer);
}
// Close socket
close(data_socket);
// Quit on DOWN command
if (down_flag) {
printf("Server closed\n");
break;
}
}
close(connection_socket);
// Unlink the socket
unlink(SOCKET_NAME);
exit(EXIT_SUCCESS);
}
*/