-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker.c
268 lines (222 loc) · 9.8 KB
/
broker.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <time.h>
#include <unistd.h>
/** Para usar o mkfifo() **/
#include <sys/stat.h>
/** Para usar readdir() e listar os arquivos FIFO nos diretórios **/
#include <dirent.h>
/** Para usar o open e conseguir abrir o pipe **/
#include <fcntl.h>
/** Para usar prctl() e matar o processo filho quando o processo pai é morto **/
#include <sys/prctl.h>
/** Para usar signal() e ignorar o código de retorno do filho (sem um processo zumbi) **/
#include <signal.h>
#define LISTENQ 1
#define MAXBUFFER 4096
#define MAXPATH 65536+101
/* Função que ajuda a fazer o parse dos bytes de "msg len" para int */
int get_msg_len(FILE* connfile) {
unsigned char cur_byte = 0;
int shifts = 0, total = 0;
do {
cur_byte = getc(connfile);
total += (cur_byte & 0x7F) << shifts;
shifts += 7;
} while (cur_byte & 0x80);
return total;
}
int main(int argc, char** argv) {
if (argc != 2) {
fprintf(stderr,"Uso: %s <Porta>\n",argv[0]);
fprintf(stderr,"Vai rodar um servidor broker MQTT na porta <Porta> TCP\n");
exit(1);
}
int listenfd, connfd;
struct sockaddr_in servaddr;
/* Criação de um socket */
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket :(\n");
exit(2);
}
/* Inicialização do endereço e associação dele ao socket */
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(atoi(argv[1]));
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
perror("bind :(\n");
exit(3);
}
/* Socket passivo */
if (listen(listenfd, LISTENQ) == -1) {
perror("listen :(\n");
exit(4);
}
/* O diretório temporário será utilizado como um banco de dados, na qual cada
* subdiretório representará um tópico distinto e conterá todos os arquivos FIFO de
* processos que cuidam de clientes inscritos naquele tópico. Mais detalhes na parte
* do código que processa os bytes do tópico. */
char path[MAXPATH] = "/tmp/ep1-willian_wang.XXXXXX";
int path_len = strlen(path);
if (mkdtemp(path) == NULL) {
perror("mkdtemp :(\n");
exit(5);
}
while (1) {
if ((connfd = accept(listenfd, (struct sockaddr *) NULL, NULL)) == -1 ) {
perror("accept :(\n");
exit(6);
}
/* Esse fork é para "liberar" o socket do processo principal para novos clientes.
* O tratamento do sinal SIGCHLD faz com que o status de saída dos filhos possam ser
* ignorados, evitando processos zumbis ou a necessidade de um tratamento mais
* detalhado usando wait. */
signal(SIGCHLD, SIG_IGN);
int childpid = fork();
/* processo pai apenas fecha o socket que está sendo tratado pelo filho e continua
* com o loop para tratar conexões de novos clientes. */
if (childpid != 0) {
close(connfd);
continue;
}
/* O socket listenfd é fechado no processo filho, pois não é mais necessário.
* connfile é utilizado para possibilitar o uso de getc e facilitar o processamento
* de cada byte da mensagem. */
close(listenfd);
FILE* connfile = fdopen(connfd, "r");
/* Variáveis auxiliares para as "header flags", "msg len" e remain_bytes ajuda a
* determinar quando a mensagem chega ao fim. */
unsigned char header = 0;
int msg_len = 0, remain_bytes = 0;
/* Lê o header da mensagem, que deveria ser de CONNECT.
* O corpo inteiro desta mensagem é ignorado. */
header = getc(connfile);
msg_len = remain_bytes = get_msg_len(connfile);
while (remain_bytes--) getc(connfile);
/* Manda de volta um CONNACK padrão */
write(connfd, (char []) {0x20, 0x02, 0x00, 0x00}, 4);
/* Lê o header do e o tamanho da mensagem, que deveria ser de PUBLISH ou SUBSCRIBE */
header = getc(connfile);
msg_len = remain_bytes = get_msg_len(connfile);
/* O tipo SUBSCRIBE lê dois bytes de identifier que serão ignorados */
if (header >> 4 == 8) {
getc(connfile);
getc(connfile);
remain_bytes -= 2;
}
/* Lê o tamanho do tópico, o tratamento manual tira a necessidade do ntohl aqui */
int topic_len = getc(connfile) << 8;
topic_len += getc(connfile);
remain_bytes -= 2;
/* Lê o tópico e atualiza a path com o caminho correspondente ao diretório que irá
* administrar os arquivos pipe temporários dos clientes inscritos
*
* Exemplo de diretório para o tópico "temperatura":
* /ep1-willian_wang.hIIGh4/temperatura
*/
char topic[topic_len + 1];
for (int i = 0; i < topic_len; i++) topic[i] = getc(connfile);
topic[topic_len] = '\0';
snprintf(path+path_len, topic_len+2, "/%s", topic);
remain_bytes -= topic_len;
path_len += 1+topic_len;
/* Para tratar o PUBLISH, o payload a ser enviada é lida, todos os arquvivos FIFO
* no diretório temporário correspondente ao tópico são listados, e escrever
* a mensagem de PUBLISH completo será escrito em cada um deles, byte por byte.*/
if (header >> 4 == 3) {
/* packet mantém a reconstrução byte por byte da mensagem que o cliente do tipo SUB espera receber. */
unsigned char packet[msg_len + 10];
int packet_len = 0;
packet[packet_len++] = header;
int aux = msg_len;
do {
unsigned char cur_byte = aux & 0x7F;
aux >>= 7;
if (aux) cur_byte |= 0x80;
packet[packet_len++] = cur_byte;
} while (aux);
packet[packet_len++] = topic_len >> 8;
packet[packet_len++] = topic_len & 0xFF;
memcpy(packet+packet_len, topic, topic_len);
packet_len += topic_len;
while(remain_bytes--) packet[packet_len++] = getc(connfile);
/* Itera por todos os potenciais arquivos pipe de processos cuidando de clientes
* inscritos naquele tópico e escreve todos os bytes que esses outros processos
* deverão enviar aos clientes que eles estão servindo. */
DIR* dir = opendir(path);
struct dirent *file;
if (dir) {
while ((file = readdir(dir)) != NULL) {
/* Ignora os arquviso que não são FIFO */
if (file->d_type != DT_FIFO) continue;
snprintf(path+path_len, 257, "/%s", file->d_name);
int pipefd = open(path, O_WRONLY);
if (pipefd == -1) continue;
/* Escreve no arquivo FIFO a mensagem PUBLISH completo */
write(pipefd, packet, packet_len);
close(pipefd);
}
closedir(dir);
}
}
/* Para tratar o SUBSCRIBE, o arquivo FIFO correspondente deve ser lido e repassado
* para o socket do jeito que se encontra. Ao mesmo tempo, as mensagens PINGREQ e
* DISCONNECT devem ser tratados para manter a conexão ativa e controlar o fim da
* leitura do FIFO. Para isso o fork() é utilizado, com o processo filho cuidando
* apenas do repasse dos bytes encontrados no arquivo FIFO. */
else if (header >> 4 == 8) {
/* Lê a flag de QoS e ignora ela */
getc(connfile);
/* Cria o diretório do tópico temporário caso não exista e cria o arquivo FIFO
* dentro deste diretório. O nome do arquivo FIFO é o PID do processo escutando.
*
* Exemplo de arquivo FIFO:
* /ep1-willian_wang.hIIGh4/temperatura/27534
*/
mkdir(path, 0700);
snprintf(path+path_len, 16, "/%d.fifo", getpid());
mkfifo(path, 0600);
int childpid2 = fork();
/* Processo pai cuidando apenas das mensagens do tipo PINGREQ e DISCONNECT.
* Note que o DISCONNECT acaba matando o processo, forçando o processo filho
* cuidando do arquivo FIFO a morrer também. */
if (childpid2 != 0) {
while (1) {
header = getc(connfile);
get_msg_len(connfile);
if (header >> 4 == 12) write(connfd, (char []) {0xD0, 0x00}, 2);
else if (header >> 4 == 14) break;
}
/* Remove o arquivo FIFO para efeitos de organização. */
unlink(path);
}
/* processo filho cuidando de novos bytes encontrados no arquivo FIFO */
else {
/* Manda de voltar um SUBACK padrão */
write(connfd, (char []) {0x90, 0x03, 0x00, 0x01, 0x00}, 5);
/* prctl garantirá que a morte do processo pai matará este processo também */
prctl(PR_SET_PDEATHSIG, SIGTERM);
int pipefd, n = 0;
unsigned char buf[MAXBUFFER+1];
/* O while é necessário pois todo novo PUBLISH acaba fechando o pipefd. */
while (1) {
pipefd = open(path, O_RDONLY);
while ((n=read(pipefd, buf, MAXBUFFER)) > 0) write(connfd, buf, n);
if (n < 0) break;
}
}
}
fclose(connfile);
close(connfd);
exit(0);
}
}