/src/mosquitto/lib/net_mosq.c
Line | Count | Source |
1 | | /* |
2 | | Copyright (c) 2009-2021 Roger Light <roger@atchoo.org> |
3 | | |
4 | | All rights reserved. This program and the accompanying materials |
5 | | are made available under the terms of the Eclipse Public License 2.0 |
6 | | and Eclipse Distribution License v1.0 which accompany this distribution. |
7 | | |
8 | | The Eclipse Public License is available at |
9 | | https://www.eclipse.org/legal/epl-2.0/ |
10 | | and the Eclipse Distribution License is available at |
11 | | http://www.eclipse.org/org/documents/edl-v10.php. |
12 | | |
13 | | SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
14 | | |
15 | | Contributors: |
16 | | Roger Light - initial implementation and documentation. |
17 | | */ |
18 | | |
19 | | #define _GNU_SOURCE |
20 | | #include "config.h" |
21 | | |
22 | | #include <assert.h> |
23 | | #include <errno.h> |
24 | | #include <fcntl.h> |
25 | | #include <stdio.h> |
26 | | #include <string.h> |
27 | | #ifndef WIN32 |
28 | | #define _GNU_SOURCE |
29 | | #include <netdb.h> |
30 | | #include <netinet/tcp.h> |
31 | | #include <sys/socket.h> |
32 | | #include <unistd.h> |
33 | | #else |
34 | | #include <winsock2.h> |
35 | | #include <ws2tcpip.h> |
36 | | #endif |
37 | | |
38 | | #ifdef __ANDROID__ |
39 | | #include <linux/in.h> |
40 | | #include <linux/in6.h> |
41 | | #include <sys/endian.h> |
42 | | #endif |
43 | | |
44 | | #ifdef HAVE_NETINET_IN_H |
45 | | # include <netinet/in.h> |
46 | | #endif |
47 | | |
48 | | #ifdef WITH_UNIX_SOCKETS |
49 | | # include <sys/un.h> |
50 | | #endif |
51 | | |
52 | | #ifdef __QNX__ |
53 | | #include <net/netbyte.h> |
54 | | #endif |
55 | | |
56 | | #ifdef WITH_TLS |
57 | | #include <openssl/conf.h> |
58 | | #include <openssl/engine.h> |
59 | | #include <openssl/err.h> |
60 | | #include <openssl/ui.h> |
61 | | #include <tls_mosq.h> |
62 | | #endif |
63 | | |
64 | | #ifdef WITH_BROKER |
65 | | # include "mosquitto_broker_internal.h" |
66 | | # if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_LWS |
67 | | # include <libwebsockets.h> |
68 | | # endif |
69 | | #else |
70 | | # include "read_handle.h" |
71 | | #endif |
72 | | |
73 | | #include "logging_mosq.h" |
74 | | #include "mosquitto/mqtt_protocol.h" |
75 | | #include "net_mosq.h" |
76 | | #include "packet_mosq.h" |
77 | | #include "util_mosq.h" |
78 | | |
79 | | #ifdef WITH_TLS |
80 | | int tls_ex_index_mosq = -1; |
81 | | UI_METHOD *_ui_method = NULL; |
82 | | |
83 | | static bool is_tls_initialized = false; |
84 | | |
85 | | |
86 | | /* Functions taken from OpenSSL s_server/s_client */ |
87 | | static int ui_open(UI *ui) |
88 | 0 | { |
89 | 0 | return UI_method_get_opener(UI_OpenSSL())(ui); |
90 | 0 | } |
91 | | |
92 | | |
93 | | static int ui_read(UI *ui, UI_STRING *uis) |
94 | 0 | { |
95 | 0 | return UI_method_get_reader(UI_OpenSSL())(ui, uis); |
96 | 0 | } |
97 | | |
98 | | |
99 | | static int ui_write(UI *ui, UI_STRING *uis) |
100 | 0 | { |
101 | 0 | return UI_method_get_writer(UI_OpenSSL())(ui, uis); |
102 | 0 | } |
103 | | |
104 | | |
105 | | static int ui_close(UI *ui) |
106 | 0 | { |
107 | 0 | return UI_method_get_closer(UI_OpenSSL())(ui); |
108 | 0 | } |
109 | | |
110 | | |
111 | | static void setup_ui_method(void) |
112 | 0 | { |
113 | 0 | _ui_method = UI_create_method("OpenSSL application user interface"); |
114 | 0 | UI_method_set_opener(_ui_method, ui_open); |
115 | 0 | UI_method_set_reader(_ui_method, ui_read); |
116 | 0 | UI_method_set_writer(_ui_method, ui_write); |
117 | 0 | UI_method_set_closer(_ui_method, ui_close); |
118 | 0 | } |
119 | | |
120 | | |
121 | | static void cleanup_ui_method(void) |
122 | 8.33k | { |
123 | 8.33k | if(_ui_method){ |
124 | 0 | UI_destroy_method(_ui_method); |
125 | 0 | _ui_method = NULL; |
126 | 0 | } |
127 | 8.33k | } |
128 | | |
129 | | |
130 | | UI_METHOD *net__get_ui_method(void) |
131 | 0 | { |
132 | 0 | if(!_ui_method){ |
133 | 0 | setup_ui_method(); |
134 | 0 | } |
135 | 0 | return _ui_method; |
136 | 0 | } |
137 | | |
138 | | #endif |
139 | | |
140 | | |
141 | | int net__init(void) |
142 | 8.33k | { |
143 | | #ifdef WIN32 |
144 | | WSADATA wsaData; |
145 | | if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0){ |
146 | | return MOSQ_ERR_UNKNOWN; |
147 | | } |
148 | | #endif |
149 | | |
150 | | #ifdef WITH_SRV |
151 | | ares_library_init(ARES_LIB_INIT_ALL); |
152 | | #endif |
153 | | |
154 | 8.33k | return MOSQ_ERR_SUCCESS; |
155 | 8.33k | } |
156 | | |
157 | | |
158 | | void net__cleanup(void) |
159 | 8.33k | { |
160 | 8.33k | #ifdef WITH_TLS |
161 | 8.33k | cleanup_ui_method(); |
162 | 8.33k | if(is_tls_initialized){ |
163 | 8.33k | # if !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000 |
164 | 8.33k | ENGINE_cleanup(); |
165 | 8.33k | # endif |
166 | 8.33k | is_tls_initialized = false; |
167 | 8.33k | } |
168 | 8.33k | #endif |
169 | | |
170 | | #ifdef WITH_SRV |
171 | | ares_library_cleanup(); |
172 | | #endif |
173 | | |
174 | | #ifdef WIN32 |
175 | | WSACleanup(); |
176 | | #endif |
177 | 8.33k | } |
178 | | |
179 | | #ifdef WITH_TLS |
180 | | |
181 | | |
182 | | void net__init_tls(void) |
183 | 8.33k | { |
184 | 8.33k | if(is_tls_initialized){ |
185 | 0 | return; |
186 | 0 | } |
187 | | |
188 | 8.33k | #if !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000 |
189 | 8.33k | ENGINE_load_builtin_engines(); |
190 | 8.33k | #endif |
191 | 8.33k | if(tls_ex_index_mosq == -1){ |
192 | 1 | tls_ex_index_mosq = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
193 | 1 | } |
194 | | |
195 | 8.33k | is_tls_initialized = true; |
196 | 8.33k | } |
197 | | #endif |
198 | | |
199 | | |
200 | | bool net__is_connected(struct mosquitto *mosq) |
201 | 11.6k | { |
202 | | #if defined(WITH_BROKER) && defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_LWS |
203 | | return mosq->sock != INVALID_SOCKET || mosq->wsi != NULL; |
204 | | #else |
205 | 11.6k | return mosq->sock != INVALID_SOCKET; |
206 | 11.6k | #endif |
207 | 11.6k | } |
208 | | |
209 | | |
210 | | /* Close a socket associated with a context and set it to -1. |
211 | | * Returns 1 on failure (context is NULL) |
212 | | * Returns 0 on success. |
213 | | */ |
214 | | int net__socket_close(struct mosquitto *mosq) |
215 | 7.62k | { |
216 | 7.62k | int rc = 0; |
217 | 7.62k | #ifdef WITH_BROKER |
218 | 7.62k | struct mosquitto *mosq_found; |
219 | 7.62k | #endif |
220 | | |
221 | 7.62k | assert(mosq); |
222 | 7.62k | #ifdef WITH_TLS |
223 | | #if defined(WITH_BROKER) && defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_LWS |
224 | | if(!mosq->wsi) |
225 | | #endif |
226 | 7.62k | { |
227 | 7.62k | if(mosq->ssl){ |
228 | 0 | if(!SSL_in_init(mosq->ssl)){ |
229 | 0 | SSL_shutdown(mosq->ssl); |
230 | 0 | } |
231 | 0 | SSL_free(mosq->ssl); |
232 | 0 | mosq->ssl = NULL; |
233 | 0 | } |
234 | 7.62k | } |
235 | 7.62k | #endif |
236 | | |
237 | | #if defined(WITH_BROKER) && defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_LWS |
238 | | if(mosq->wsi){ |
239 | | if(mosq->state != mosq_cs_disconnecting){ |
240 | | mosquitto__set_state(mosq, mosq_cs_disconnect_ws); |
241 | | } |
242 | | lws_callback_on_writable(mosq->wsi); |
243 | | if(mosq->listener){ |
244 | | mosq->listener->client_count--; |
245 | | } |
246 | | }else |
247 | | #endif |
248 | 7.62k | { |
249 | 7.62k | if(net__is_connected(mosq)){ |
250 | 0 | #ifdef WITH_BROKER |
251 | 0 | HASH_FIND(hh_sock, db.contexts_by_sock, &mosq->sock, sizeof(mosq->sock), mosq_found); |
252 | 0 | if(mosq_found){ |
253 | 0 | HASH_DELETE(hh_sock, db.contexts_by_sock, mosq_found); |
254 | 0 | } |
255 | 0 | if(mosq->listener && mosq->state != mosq_cs_disused){ |
256 | 0 | mosq->listener->client_count--; |
257 | 0 | } |
258 | 0 | #endif |
259 | 0 | rc = COMPAT_CLOSE(mosq->sock); |
260 | 0 | mosq->sock = INVALID_SOCKET; |
261 | 0 | } |
262 | 7.62k | } |
263 | | |
264 | 7.62k | return rc; |
265 | 7.62k | } |
266 | | |
267 | | |
268 | | /* Shutdown a socket. |
269 | | * Returns 1 on failure (context is NULL) |
270 | | * Returns 0 on success. |
271 | | */ |
272 | | int net__socket_shutdown(struct mosquitto *mosq) |
273 | 0 | { |
274 | 0 | int rc = 0; |
275 | |
|
276 | 0 | assert(mosq); |
277 | |
|
278 | 0 | if(net__is_connected(mosq)){ |
279 | 0 | rc = COMPAT_SHUTDOWN(mosq->sock); |
280 | 0 | } |
281 | |
|
282 | 0 | return rc; |
283 | 0 | } |
284 | | |
285 | | |
286 | | #ifdef FINAL_WITH_TLS_PSK |
287 | | |
288 | | |
289 | | static unsigned int psk_client_callback(SSL *ssl, const char *hint, |
290 | | char *identity, unsigned int max_identity_len, |
291 | | unsigned char *psk, unsigned int max_psk_len) |
292 | 0 | { |
293 | 0 | struct mosquitto *mosq; |
294 | 0 | int len; |
295 | |
|
296 | 0 | UNUSED(hint); |
297 | |
|
298 | 0 | mosq = SSL_get_ex_data(ssl, tls_ex_index_mosq); |
299 | 0 | if(!mosq){ |
300 | 0 | return 0; |
301 | 0 | } |
302 | | |
303 | 0 | snprintf(identity, max_identity_len, "%s", mosq->tls_psk_identity); |
304 | |
|
305 | 0 | len = mosquitto__hex2bin(mosq->tls_psk, psk, (int)max_psk_len); |
306 | 0 | if(len < 0){ |
307 | 0 | return 0; |
308 | 0 | } |
309 | 0 | return (unsigned int)len; |
310 | 0 | } |
311 | | #endif |
312 | | |
313 | | #if defined(WITH_BROKER) && defined(__GLIBC__) && defined(WITH_ADNS) |
314 | | |
315 | | |
316 | | /* Async connect, part 1 (dns lookup) */ |
317 | | int net__try_connect_step1(struct mosquitto *mosq, const char *host) |
318 | | { |
319 | | int s; |
320 | | void *sevp = NULL; |
321 | | struct addrinfo *hints; |
322 | | struct addrinfo *ar_request; |
323 | | |
324 | | if(mosq->adns){ |
325 | | gai_cancel(mosq->adns); |
326 | | |
327 | | ar_request = (struct addrinfo *)mosq->adns->ar_request; |
328 | | mosquitto_FREE(ar_request); |
329 | | mosquitto_FREE(mosq->adns); |
330 | | } |
331 | | mosq->adns = mosquitto_calloc(1, sizeof(struct gaicb)); |
332 | | if(!mosq->adns){ |
333 | | return MOSQ_ERR_NOMEM; |
334 | | } |
335 | | |
336 | | hints = mosquitto_calloc(1, sizeof(struct addrinfo)); |
337 | | if(!hints){ |
338 | | mosquitto_FREE(mosq->adns); |
339 | | return MOSQ_ERR_NOMEM; |
340 | | } |
341 | | |
342 | | hints->ai_family = AF_UNSPEC; |
343 | | hints->ai_socktype = SOCK_STREAM; |
344 | | |
345 | | mosq->adns->ar_name = host; |
346 | | mosq->adns->ar_request = hints; |
347 | | |
348 | | s = getaddrinfo_a(GAI_NOWAIT, &mosq->adns, 1, sevp); |
349 | | if(s){ |
350 | | errno = s; |
351 | | if(mosq->adns){ |
352 | | ar_request = (struct addrinfo *)mosq->adns->ar_request; |
353 | | mosquitto_FREE(ar_request); |
354 | | mosquitto_FREE(mosq->adns); |
355 | | } |
356 | | return MOSQ_ERR_EAI; |
357 | | } |
358 | | |
359 | | return MOSQ_ERR_SUCCESS; |
360 | | } |
361 | | |
362 | | |
363 | | /* Async connect part 2, the connection. */ |
364 | | int net__try_connect_step2(struct mosquitto *mosq, uint16_t port, mosq_sock_t *sock) |
365 | | { |
366 | | struct addrinfo *ainfo, *rp; |
367 | | struct addrinfo *ar_request; |
368 | | int rc; |
369 | | |
370 | | ainfo = mosq->adns->ar_result; |
371 | | |
372 | | for(rp = ainfo; rp != NULL; rp = rp->ai_next){ |
373 | | *sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
374 | | if(*sock == INVALID_SOCKET){ |
375 | | continue; |
376 | | } |
377 | | |
378 | | if(rp->ai_family == AF_INET){ |
379 | | ((struct sockaddr_in *)rp->ai_addr)->sin_port = htons(port); |
380 | | }else if(rp->ai_family == AF_INET6){ |
381 | | ((struct sockaddr_in6 *)rp->ai_addr)->sin6_port = htons(port); |
382 | | }else{ |
383 | | COMPAT_CLOSE(*sock); |
384 | | *sock = INVALID_SOCKET; |
385 | | continue; |
386 | | } |
387 | | |
388 | | /* Set non-blocking */ |
389 | | if(net__socket_nonblock(sock)){ |
390 | | continue; |
391 | | } |
392 | | |
393 | | rc = connect(*sock, rp->ai_addr, rp->ai_addrlen); |
394 | | WINDOWS_SET_ERRNO(); |
395 | | if(rc == 0 || errno == EINPROGRESS || errno == COMPAT_EWOULDBLOCK){ |
396 | | if(rc < 0 && (errno == EINPROGRESS || errno == COMPAT_EWOULDBLOCK)){ |
397 | | rc = MOSQ_ERR_CONN_PENDING; |
398 | | } |
399 | | |
400 | | /* Set non-blocking */ |
401 | | if(net__socket_nonblock(sock)){ |
402 | | continue; |
403 | | } |
404 | | break; |
405 | | } |
406 | | |
407 | | COMPAT_CLOSE(*sock); |
408 | | *sock = INVALID_SOCKET; |
409 | | } |
410 | | freeaddrinfo(mosq->adns->ar_result); |
411 | | mosq->adns->ar_result = NULL; |
412 | | |
413 | | ar_request = (struct addrinfo *)mosq->adns->ar_request; |
414 | | mosquitto_FREE(ar_request); |
415 | | mosquitto_FREE(mosq->adns); |
416 | | |
417 | | if(!rp){ |
418 | | return MOSQ_ERR_ERRNO; |
419 | | } |
420 | | |
421 | | return rc; |
422 | | } |
423 | | |
424 | | #endif |
425 | | |
426 | | |
427 | | static int net__try_connect_tcp(const char *host, uint16_t port, mosq_sock_t *sock, const char *bind_address, bool blocking) |
428 | 0 | { |
429 | 0 | struct addrinfo hints; |
430 | 0 | struct addrinfo *ainfo, *rp; |
431 | 0 | struct addrinfo *ainfo_bind, *rp_bind; |
432 | 0 | int s; |
433 | 0 | int rc = MOSQ_ERR_SUCCESS; |
434 | |
|
435 | 0 | ainfo_bind = NULL; |
436 | |
|
437 | 0 | *sock = INVALID_SOCKET; |
438 | 0 | memset(&hints, 0, sizeof(struct addrinfo)); |
439 | 0 | hints.ai_family = AF_UNSPEC; |
440 | 0 | hints.ai_socktype = SOCK_STREAM; |
441 | |
|
442 | 0 | s = getaddrinfo(host, NULL, &hints, &ainfo); |
443 | 0 | if(s){ |
444 | 0 | errno = s; |
445 | 0 | return MOSQ_ERR_EAI; |
446 | 0 | } |
447 | | |
448 | 0 | if(bind_address){ |
449 | 0 | s = getaddrinfo(bind_address, NULL, &hints, &ainfo_bind); |
450 | 0 | if(s){ |
451 | 0 | freeaddrinfo(ainfo); |
452 | 0 | errno = s; |
453 | 0 | return MOSQ_ERR_EAI; |
454 | 0 | } |
455 | 0 | } |
456 | | |
457 | 0 | for(rp = ainfo; rp != NULL; rp = rp->ai_next){ |
458 | 0 | *sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
459 | 0 | if(*sock == INVALID_SOCKET){ |
460 | 0 | continue; |
461 | 0 | } |
462 | | |
463 | 0 | if(rp->ai_family == AF_INET){ |
464 | 0 | ((struct sockaddr_in *)rp->ai_addr)->sin_port = htons(port); |
465 | 0 | }else if(rp->ai_family == AF_INET6){ |
466 | 0 | ((struct sockaddr_in6 *)rp->ai_addr)->sin6_port = htons(port); |
467 | 0 | }else{ |
468 | 0 | COMPAT_CLOSE(*sock); |
469 | 0 | *sock = INVALID_SOCKET; |
470 | 0 | continue; |
471 | 0 | } |
472 | | |
473 | 0 | if(bind_address){ |
474 | 0 | for(rp_bind = ainfo_bind; rp_bind != NULL; rp_bind = rp_bind->ai_next){ |
475 | 0 | if(bind(*sock, rp_bind->ai_addr, rp_bind->ai_addrlen) == 0){ |
476 | 0 | break; |
477 | 0 | } |
478 | 0 | } |
479 | 0 | if(!rp_bind){ |
480 | 0 | COMPAT_CLOSE(*sock); |
481 | 0 | *sock = INVALID_SOCKET; |
482 | 0 | continue; |
483 | 0 | } |
484 | 0 | } |
485 | | |
486 | 0 | if(!blocking){ |
487 | | /* Set non-blocking */ |
488 | 0 | if(net__socket_nonblock(sock)){ |
489 | 0 | continue; |
490 | 0 | } |
491 | 0 | } |
492 | | |
493 | 0 | rc = connect(*sock, rp->ai_addr, rp->ai_addrlen); |
494 | 0 | WINDOWS_SET_ERRNO(); |
495 | 0 | if(rc == 0 || errno == EINPROGRESS || errno == COMPAT_EWOULDBLOCK){ |
496 | 0 | if(rc < 0 && (errno == EINPROGRESS || errno == COMPAT_EWOULDBLOCK)){ |
497 | 0 | rc = MOSQ_ERR_CONN_PENDING; |
498 | 0 | } |
499 | |
|
500 | 0 | if(blocking){ |
501 | | /* Set non-blocking */ |
502 | 0 | if(net__socket_nonblock(sock)){ |
503 | 0 | continue; |
504 | 0 | } |
505 | 0 | } |
506 | 0 | break; |
507 | 0 | } |
508 | | |
509 | 0 | COMPAT_CLOSE(*sock); |
510 | 0 | *sock = INVALID_SOCKET; |
511 | 0 | } |
512 | 0 | freeaddrinfo(ainfo); |
513 | 0 | if(bind_address){ |
514 | 0 | freeaddrinfo(ainfo_bind); |
515 | 0 | } |
516 | 0 | if(!rp){ |
517 | 0 | return MOSQ_ERR_ERRNO; |
518 | 0 | } |
519 | 0 | return rc; |
520 | 0 | } |
521 | | |
522 | | |
523 | | #ifdef WITH_UNIX_SOCKETS |
524 | | |
525 | | |
526 | | static int net__try_connect_unix(const char *host, mosq_sock_t *sock) |
527 | 0 | { |
528 | 0 | struct sockaddr_un addr; |
529 | 0 | int s; |
530 | 0 | int rc; |
531 | |
|
532 | 0 | if(host == NULL || strlen(host) == 0 || strlen(host) > sizeof(addr.sun_path)-1){ |
533 | 0 | return MOSQ_ERR_INVAL; |
534 | 0 | } |
535 | | |
536 | 0 | memset(&addr, 0, sizeof(struct sockaddr_un)); |
537 | 0 | addr.sun_family = AF_UNIX; |
538 | 0 | strncpy(addr.sun_path, host, sizeof(addr.sun_path)-1); |
539 | |
|
540 | 0 | s = socket(AF_UNIX, SOCK_STREAM, 0); |
541 | 0 | if(s < 0){ |
542 | 0 | return MOSQ_ERR_ERRNO; |
543 | 0 | } |
544 | 0 | rc = net__socket_nonblock(&s); |
545 | 0 | if(rc){ |
546 | 0 | return rc; |
547 | 0 | } |
548 | | |
549 | 0 | rc = connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)); |
550 | 0 | if(rc < 0){ |
551 | 0 | close(s); |
552 | 0 | return MOSQ_ERR_ERRNO; |
553 | 0 | } |
554 | | |
555 | 0 | *sock = s; |
556 | |
|
557 | 0 | return 0; |
558 | 0 | } |
559 | | #endif |
560 | | |
561 | | |
562 | | int net__try_connect(const char *host, uint16_t port, mosq_sock_t *sock, const char *bind_address, bool blocking) |
563 | 0 | { |
564 | 0 | if(port == 0){ |
565 | 0 | #ifdef WITH_UNIX_SOCKETS |
566 | 0 | return net__try_connect_unix(host, sock); |
567 | | #else |
568 | | return MOSQ_ERR_NOT_SUPPORTED; |
569 | | #endif |
570 | 0 | }else{ |
571 | 0 | return net__try_connect_tcp(host, port, sock, bind_address, blocking); |
572 | 0 | } |
573 | 0 | } |
574 | | |
575 | | |
576 | | #ifdef WITH_TLS |
577 | | |
578 | | |
579 | | void net__print_ssl_error(struct mosquitto *mosq, const char *msg) |
580 | 0 | { |
581 | 0 | char ebuf[256]; |
582 | 0 | unsigned long e; |
583 | 0 | int num = 0; |
584 | 0 | const char *msg_disp = msg == NULL ? "" : msg; |
585 | 0 | e = ERR_get_error(); |
586 | 0 | while(e){ |
587 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "OpenSSL Error %s[%d]: %s", msg_disp, num, ERR_error_string(e, ebuf)); |
588 | 0 | e = ERR_get_error(); |
589 | 0 | num++; |
590 | 0 | } |
591 | 0 | } |
592 | | |
593 | | |
594 | | int net__socket_connect_tls(struct mosquitto *mosq) |
595 | 0 | { |
596 | 0 | long res; |
597 | |
|
598 | 0 | ERR_clear_error(); |
599 | 0 | if(mosq->tls_ocsp_required){ |
600 | | /* Note: OCSP is available in all currently supported OpenSSL versions. */ |
601 | 0 | if((res=SSL_set_tlsext_status_type(mosq->ssl, TLSEXT_STATUSTYPE_ocsp)) != 1){ |
602 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Could not activate OCSP (error: %ld)", res); |
603 | 0 | return MOSQ_ERR_OCSP; |
604 | 0 | } |
605 | 0 | if((res=SSL_CTX_set_tlsext_status_cb(mosq->ssl_ctx, mosquitto__verify_ocsp_status_cb)) != 1){ |
606 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Could not activate OCSP (error: %ld)", res); |
607 | 0 | return MOSQ_ERR_OCSP; |
608 | 0 | } |
609 | 0 | if((res=SSL_CTX_set_tlsext_status_arg(mosq->ssl_ctx, mosq)) != 1){ |
610 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Could not activate OCSP (error: %ld)", res); |
611 | 0 | return MOSQ_ERR_OCSP; |
612 | 0 | } |
613 | 0 | } |
614 | 0 | SSL_set_connect_state(mosq->ssl); |
615 | 0 | return MOSQ_ERR_SUCCESS; |
616 | 0 | } |
617 | | #endif |
618 | | |
619 | | |
620 | | #ifdef WITH_TLS |
621 | | |
622 | | |
623 | | static int net__tls_load_ca(struct mosquitto *mosq) |
624 | 0 | { |
625 | 0 | int ret; |
626 | |
|
627 | 0 | if(mosq->tls_use_os_certs){ |
628 | 0 | SSL_CTX_set_default_verify_paths(mosq->ssl_ctx); |
629 | 0 | } |
630 | 0 | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
631 | 0 | if(mosq->tls_cafile || mosq->tls_capath){ |
632 | 0 | ret = SSL_CTX_load_verify_locations(mosq->ssl_ctx, mosq->tls_cafile, mosq->tls_capath); |
633 | 0 | if(ret == 0){ |
634 | 0 | # ifdef WITH_BROKER |
635 | 0 | if(mosq->tls_cafile && mosq->tls_capath){ |
636 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check bridge_cafile \"%s\" and bridge_capath \"%s\".", mosq->tls_cafile, mosq->tls_capath); |
637 | 0 | }else if(mosq->tls_cafile){ |
638 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check bridge_cafile \"%s\".", mosq->tls_cafile); |
639 | 0 | }else{ |
640 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check bridge_capath \"%s\".", mosq->tls_capath); |
641 | 0 | } |
642 | | # else |
643 | | if(mosq->tls_cafile && mosq->tls_capath){ |
644 | | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check cafile \"%s\" and capath \"%s\".", mosq->tls_cafile, mosq->tls_capath); |
645 | | }else if(mosq->tls_cafile){ |
646 | | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check cafile \"%s\".", mosq->tls_cafile); |
647 | | }else{ |
648 | | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check capath \"%s\".", mosq->tls_capath); |
649 | | } |
650 | | # endif |
651 | 0 | return MOSQ_ERR_TLS; |
652 | 0 | } |
653 | 0 | } |
654 | | #else |
655 | | if(mosq->tls_cafile){ |
656 | | ret = SSL_CTX_load_verify_file(mosq->ssl_ctx, mosq->tls_cafile); |
657 | | if(ret == 0){ |
658 | | # ifdef WITH_BROKER |
659 | | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check bridge_cafile \"%s\".", mosq->tls_cafile); |
660 | | # else |
661 | | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check cafile \"%s\".", mosq->tls_cafile); |
662 | | # endif |
663 | | return MOSQ_ERR_TLS; |
664 | | } |
665 | | } |
666 | | if(mosq->tls_capath){ |
667 | | ret = SSL_CTX_load_verify_dir(mosq->ssl_ctx, mosq->tls_capath); |
668 | | if(ret == 0){ |
669 | | # ifdef WITH_BROKER |
670 | | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check bridge_capath \"%s\".", mosq->tls_capath); |
671 | | # else |
672 | | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check capath \"%s\".", mosq->tls_capath); |
673 | | # endif |
674 | | return MOSQ_ERR_TLS; |
675 | | } |
676 | | } |
677 | | #endif |
678 | 0 | return MOSQ_ERR_SUCCESS; |
679 | 0 | } |
680 | | |
681 | | |
682 | | static int net__init_ssl_ctx(struct mosquitto *mosq) |
683 | 0 | { |
684 | 0 | int ret; |
685 | 0 | #if !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000 |
686 | 0 | ENGINE *engine = NULL; |
687 | 0 | EVP_PKEY *pkey; |
688 | 0 | #endif |
689 | 0 | uint8_t tls_alpn_wire[256]; |
690 | 0 | uint8_t tls_alpn_len; |
691 | |
|
692 | 0 | net__init_tls(); |
693 | |
|
694 | | #ifndef WITH_BROKER |
695 | | if(mosq->user_ssl_ctx){ |
696 | | mosq->ssl_ctx = mosq->user_ssl_ctx; |
697 | | if(!mosq->ssl_ctx_defaults){ |
698 | | return MOSQ_ERR_SUCCESS; |
699 | | }else if(!mosq->tls_cafile && !mosq->tls_capath && !mosq->tls_psk){ |
700 | | log__printf(mosq, MOSQ_LOG_ERR, "Error: If you use MOSQ_OPT_SSL_CTX then MOSQ_OPT_SSL_CTX_WITH_DEFAULTS must be true, or at least one of cafile, capath or psk must be specified."); |
701 | | return MOSQ_ERR_INVAL; |
702 | | } |
703 | | } |
704 | | #endif |
705 | | |
706 | | /* Apply default SSL_CTX settings. This is only used if MOSQ_OPT_SSL_CTX |
707 | | * has not been set, or if both of MOSQ_OPT_SSL_CTX and |
708 | | * MOSQ_OPT_SSL_CTX_WITH_DEFAULTS are set. */ |
709 | 0 | if(mosq->tls_cafile || mosq->tls_capath || mosq->tls_psk || mosq->tls_use_os_certs){ |
710 | 0 | if(!mosq->ssl_ctx){ |
711 | |
|
712 | 0 | mosq->ssl_ctx = SSL_CTX_new(TLS_client_method()); |
713 | |
|
714 | 0 | if(!mosq->ssl_ctx){ |
715 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to create TLS context."); |
716 | 0 | net__print_ssl_error(mosq, "while trying to create a SSL ctx"); |
717 | 0 | return MOSQ_ERR_TLS; |
718 | 0 | } |
719 | 0 | } |
720 | | |
721 | 0 | #ifdef SSL_OP_NO_TLSv1_3 |
722 | 0 | if(mosq->tls_psk){ |
723 | 0 | SSL_CTX_set_options(mosq->ssl_ctx, SSL_OP_NO_TLSv1_3); |
724 | 0 | } |
725 | 0 | #endif |
726 | |
|
727 | 0 | if(!mosq->tls_version){ |
728 | 0 | SSL_CTX_set_options(mosq->ssl_ctx, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1); |
729 | 0 | #ifdef SSL_OP_NO_TLSv1_3 |
730 | 0 | }else if(!strcmp(mosq->tls_version, "tlsv1.3")){ |
731 | 0 | SSL_CTX_set_options(mosq->ssl_ctx, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1_2); |
732 | 0 | #endif |
733 | 0 | }else if(!strcmp(mosq->tls_version, "tlsv1.2")){ |
734 | 0 | SSL_CTX_set_options(mosq->ssl_ctx, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1); |
735 | 0 | }else{ |
736 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error: Protocol %s not supported.", mosq->tls_version); |
737 | 0 | return MOSQ_ERR_INVAL; |
738 | 0 | } |
739 | | |
740 | | /* Allow use of DHE ciphers */ |
741 | 0 | SSL_CTX_set_dh_auto(mosq->ssl_ctx, 1); |
742 | | |
743 | | /* Disable compression */ |
744 | 0 | SSL_CTX_set_options(mosq->ssl_ctx, SSL_OP_NO_COMPRESSION); |
745 | | |
746 | | /* Set ALPN */ |
747 | 0 | if(mosq->tls_alpn){ |
748 | 0 | tls_alpn_len = (uint8_t)strnlen(mosq->tls_alpn, 254); |
749 | 0 | tls_alpn_wire[0] = tls_alpn_len; /* first byte is length of string */ |
750 | 0 | memcpy(tls_alpn_wire + 1, mosq->tls_alpn, tls_alpn_len); |
751 | 0 | SSL_CTX_set_alpn_protos(mosq->ssl_ctx, tls_alpn_wire, tls_alpn_len + 1U); |
752 | 0 | } |
753 | |
|
754 | 0 | #ifdef SSL_MODE_RELEASE_BUFFERS |
755 | | /* Use even less memory per SSL connection. */ |
756 | 0 | SSL_CTX_set_mode(mosq->ssl_ctx, SSL_MODE_RELEASE_BUFFERS); |
757 | 0 | #endif |
758 | |
|
759 | 0 | #if !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000 |
760 | 0 | if(mosq->tls_engine){ |
761 | 0 | engine = ENGINE_by_id(mosq->tls_engine); |
762 | 0 | if(!engine){ |
763 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error loading %s engine\n", mosq->tls_engine); |
764 | 0 | return MOSQ_ERR_TLS; |
765 | 0 | } |
766 | 0 | if(!ENGINE_init(engine)){ |
767 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Failed engine initialisation\n"); |
768 | 0 | ENGINE_free(engine); |
769 | 0 | return MOSQ_ERR_TLS; |
770 | 0 | } |
771 | 0 | ENGINE_set_default(engine, ENGINE_METHOD_ALL); |
772 | 0 | ENGINE_free(engine); /* release the structural reference from ENGINE_by_id() */ |
773 | 0 | } |
774 | 0 | #endif |
775 | | |
776 | 0 | if(mosq->tls_ciphers){ |
777 | 0 | ret = SSL_CTX_set_cipher_list(mosq->ssl_ctx, mosq->tls_ciphers); |
778 | 0 | if(ret == 0){ |
779 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to set TLS ciphers. Check cipher list \"%s\".", mosq->tls_ciphers); |
780 | 0 | #if !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000 |
781 | 0 | ENGINE_FINISH(engine); |
782 | 0 | #endif |
783 | 0 | net__print_ssl_error(mosq, "while trying to set the cipher list"); |
784 | 0 | return MOSQ_ERR_TLS; |
785 | 0 | } |
786 | 0 | } |
787 | | |
788 | 0 | if(mosq->tls_13_ciphers){ |
789 | 0 | ret = SSL_CTX_set_ciphersuites(mosq->ssl_ctx, mosq->tls_13_ciphers); |
790 | 0 | if(ret == 0){ |
791 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to set TLS 1.3 ciphersuites. Check cipher_tls13 list \"%s\".", mosq->tls_13_ciphers); |
792 | 0 | return MOSQ_ERR_TLS; |
793 | 0 | } |
794 | 0 | } |
795 | | |
796 | 0 | if(mosq->tls_cafile || mosq->tls_capath || mosq->tls_use_os_certs){ |
797 | 0 | ret = net__tls_load_ca(mosq); |
798 | 0 | if(ret != MOSQ_ERR_SUCCESS){ |
799 | 0 | # if !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000 |
800 | 0 | ENGINE_FINISH(engine); |
801 | 0 | # endif |
802 | 0 | net__print_ssl_error(mosq, "while trying to load the ca"); |
803 | 0 | return MOSQ_ERR_TLS; |
804 | 0 | } |
805 | 0 | if(mosq->tls_cert_reqs == 0){ |
806 | 0 | SSL_CTX_set_verify(mosq->ssl_ctx, SSL_VERIFY_NONE, NULL); |
807 | 0 | }else{ |
808 | 0 | SSL_CTX_set_verify(mosq->ssl_ctx, SSL_VERIFY_PEER, mosquitto__server_certificate_verify); |
809 | 0 | } |
810 | |
|
811 | 0 | if(mosq->tls_pw_callback){ |
812 | 0 | SSL_CTX_set_default_passwd_cb(mosq->ssl_ctx, mosq->tls_pw_callback); |
813 | 0 | SSL_CTX_set_default_passwd_cb_userdata(mosq->ssl_ctx, mosq); |
814 | 0 | } |
815 | |
|
816 | 0 | if(mosq->tls_certfile){ |
817 | 0 | ret = SSL_CTX_use_certificate_chain_file(mosq->ssl_ctx, mosq->tls_certfile); |
818 | 0 | if(ret != 1){ |
819 | 0 | #ifdef WITH_BROKER |
820 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load client certificate, check bridge_certfile \"%s\".", mosq->tls_certfile); |
821 | | #else |
822 | | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load client certificate \"%s\".", mosq->tls_certfile); |
823 | | #endif |
824 | 0 | #if !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000 |
825 | 0 | ENGINE_FINISH(engine); |
826 | 0 | #endif |
827 | 0 | net__print_ssl_error(mosq, "while trying to use the certificate chain file"); |
828 | 0 | return MOSQ_ERR_TLS; |
829 | 0 | } |
830 | 0 | } |
831 | 0 | if(mosq->tls_keyfile){ |
832 | 0 | if(mosq->tls_keyform == mosq_k_engine){ |
833 | 0 | #if !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000 |
834 | 0 | UI_METHOD *ui_method = net__get_ui_method(); |
835 | 0 | if(mosq->tls_engine_kpass_sha1){ |
836 | 0 | if(!ENGINE_ctrl_cmd(engine, ENGINE_SECRET_MODE, ENGINE_SECRET_MODE_SHA, NULL, NULL, 0)){ |
837 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to set engine secret mode sha1"); |
838 | 0 | ENGINE_FINISH(engine); |
839 | 0 | net__print_ssl_error(mosq, "while trying to set the engine ctrl cmd SECRET_MODE"); |
840 | 0 | return MOSQ_ERR_TLS; |
841 | 0 | } |
842 | 0 | if(!ENGINE_ctrl_cmd(engine, ENGINE_PIN, 0, mosq->tls_engine_kpass_sha1, NULL, 0)){ |
843 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to set engine pin"); |
844 | 0 | ENGINE_FINISH(engine); |
845 | 0 | net__print_ssl_error(mosq, "while trying to set the engine ctrl cmd PIN"); |
846 | 0 | return MOSQ_ERR_TLS; |
847 | 0 | } |
848 | 0 | ui_method = NULL; |
849 | 0 | } |
850 | 0 | pkey = ENGINE_load_private_key(engine, mosq->tls_keyfile, ui_method, NULL); |
851 | 0 | if(!pkey){ |
852 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load engine private key file \"%s\".", mosq->tls_keyfile); |
853 | 0 | ENGINE_FINISH(engine); |
854 | 0 | net__print_ssl_error(mosq, "while trying to load the private key"); |
855 | 0 | return MOSQ_ERR_TLS; |
856 | 0 | } |
857 | 0 | if(SSL_CTX_use_PrivateKey(mosq->ssl_ctx, pkey) <= 0){ |
858 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to use engine private key file \"%s\".", mosq->tls_keyfile); |
859 | 0 | ENGINE_FINISH(engine); |
860 | 0 | net__print_ssl_error(mosq, "while trying to use the private key"); |
861 | 0 | return MOSQ_ERR_TLS; |
862 | 0 | } |
863 | 0 | #endif |
864 | 0 | }else{ |
865 | 0 | ret = SSL_CTX_use_PrivateKey_file(mosq->ssl_ctx, mosq->tls_keyfile, SSL_FILETYPE_PEM); |
866 | 0 | if(ret != 1){ |
867 | 0 | #ifdef WITH_BROKER |
868 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load client key file, check bridge_keyfile \"%s\".", mosq->tls_keyfile); |
869 | | #else |
870 | | log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load client key file \"%s\".", mosq->tls_keyfile); |
871 | | #endif |
872 | 0 | #if !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000 |
873 | 0 | ENGINE_FINISH(engine); |
874 | 0 | #endif |
875 | 0 | net__print_ssl_error(mosq, "while trying to use the private key file"); |
876 | 0 | return MOSQ_ERR_TLS; |
877 | 0 | } |
878 | 0 | } |
879 | 0 | ret = SSL_CTX_check_private_key(mosq->ssl_ctx); |
880 | 0 | if(ret != 1){ |
881 | 0 | log__printf(mosq, MOSQ_LOG_ERR, "Error: Client certificate/key are inconsistent."); |
882 | 0 | #if !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000 |
883 | 0 | ENGINE_FINISH(engine); |
884 | 0 | #endif |
885 | 0 | net__print_ssl_error(mosq, "while trying to check the private key"); |
886 | 0 | return MOSQ_ERR_TLS; |
887 | 0 | } |
888 | 0 | } |
889 | 0 | #ifdef FINAL_WITH_TLS_PSK |
890 | 0 | }else if(mosq->tls_psk){ |
891 | 0 | SSL_CTX_set_psk_client_callback(mosq->ssl_ctx, psk_client_callback); |
892 | 0 | if(mosq->tls_ciphers == NULL){ |
893 | 0 | SSL_CTX_set_cipher_list(mosq->ssl_ctx, "PSK"); |
894 | 0 | } |
895 | 0 | #endif |
896 | 0 | } |
897 | 0 | } |
898 | | |
899 | 0 | return MOSQ_ERR_SUCCESS; |
900 | 0 | } |
901 | | #endif |
902 | | |
903 | | |
904 | | int net__socket_connect_step3(struct mosquitto *mosq, const char *host) |
905 | 0 | { |
906 | 0 | #ifdef WITH_TLS |
907 | 0 | BIO *bio; |
908 | |
|
909 | 0 | int rc = net__init_ssl_ctx(mosq); |
910 | 0 | if(rc){ |
911 | 0 | net__socket_close(mosq); |
912 | 0 | return rc; |
913 | 0 | } |
914 | | |
915 | 0 | if(mosq->ssl_ctx){ |
916 | 0 | if(mosq->ssl){ |
917 | 0 | SSL_free(mosq->ssl); |
918 | 0 | } |
919 | 0 | mosq->ssl = SSL_new(mosq->ssl_ctx); |
920 | 0 | if(!mosq->ssl){ |
921 | 0 | net__socket_close(mosq); |
922 | 0 | net__print_ssl_error(mosq, "while creating a SSL object"); |
923 | 0 | return MOSQ_ERR_TLS; |
924 | 0 | } |
925 | | |
926 | 0 | if(!SSL_set_ex_data(mosq->ssl, tls_ex_index_mosq, mosq)){ |
927 | 0 | net__socket_close(mosq); |
928 | 0 | net__print_ssl_error(mosq, "while setting SSL ex data"); |
929 | 0 | return MOSQ_ERR_TLS; |
930 | 0 | } |
931 | 0 | bio = BIO_new_socket(mosq->sock, BIO_NOCLOSE); |
932 | 0 | if(!bio){ |
933 | 0 | net__socket_close(mosq); |
934 | 0 | net__print_ssl_error(mosq, "while trying to create a new socket"); |
935 | 0 | return MOSQ_ERR_TLS; |
936 | 0 | } |
937 | 0 | SSL_set_bio(mosq->ssl, bio, bio); |
938 | | |
939 | | /* |
940 | | * required for the SNI resolving |
941 | | */ |
942 | 0 | if(SSL_set_tlsext_host_name(mosq->ssl, host) != 1){ |
943 | 0 | net__socket_close(mosq); |
944 | 0 | return MOSQ_ERR_TLS; |
945 | 0 | } |
946 | 0 | if(tls__set_verify_hostname(mosq, host)){ |
947 | 0 | return MOSQ_ERR_TLS; |
948 | 0 | } |
949 | | |
950 | 0 | if(net__socket_connect_tls(mosq)){ |
951 | 0 | net__socket_close(mosq); |
952 | 0 | return MOSQ_ERR_TLS; |
953 | 0 | } |
954 | |
|
955 | 0 | } |
956 | | #else |
957 | | UNUSED(mosq); |
958 | | UNUSED(host); |
959 | | #endif |
960 | 0 | return MOSQ_ERR_SUCCESS; |
961 | 0 | } |
962 | | |
963 | | |
964 | | /* Create a socket and connect it to 'ip' on port 'port'. */ |
965 | | int net__socket_connect(struct mosquitto *mosq, const char *host, uint16_t port, const char *bind_address, bool blocking) |
966 | 0 | { |
967 | 0 | int rc, rc2; |
968 | |
|
969 | 0 | if(!mosq || !host){ |
970 | 0 | return MOSQ_ERR_INVAL; |
971 | 0 | } |
972 | | |
973 | 0 | rc = net__try_connect(host, port, &mosq->sock, bind_address, blocking); |
974 | 0 | if(rc > 0){ |
975 | 0 | return rc; |
976 | 0 | } |
977 | | |
978 | 0 | if(mosq->tcp_nodelay && port){ |
979 | 0 | int flag = 1; |
980 | 0 | if(setsockopt(mosq->sock, IPPROTO_TCP, TCP_NODELAY, (const void *)&flag, sizeof(int)) != 0){ |
981 | 0 | log__printf(mosq, MOSQ_LOG_WARNING, "Warning: Unable to set TCP_NODELAY."); |
982 | 0 | } |
983 | 0 | } |
984 | |
|
985 | | #if defined(WITH_SOCKS) && !defined(WITH_BROKER) |
986 | | if(!mosq->socks5_host) |
987 | | #endif |
988 | 0 | { |
989 | 0 | rc2 = net__socket_connect_step3(mosq, host); |
990 | 0 | if(rc2){ |
991 | 0 | return rc2; |
992 | 0 | } |
993 | 0 | } |
994 | | |
995 | 0 | return rc; |
996 | 0 | } |
997 | | |
998 | | |
999 | | #ifdef WITH_TLS |
1000 | | |
1001 | | |
1002 | | static void net__handle_ssl(struct mosquitto *mosq, int ret) |
1003 | 0 | { |
1004 | 0 | int err; |
1005 | |
|
1006 | 0 | err = SSL_get_error(mosq->ssl, ret); |
1007 | 0 | if(err == SSL_ERROR_WANT_READ){ |
1008 | 0 | errno = EAGAIN; |
1009 | 0 | }else if(err == SSL_ERROR_WANT_WRITE){ |
1010 | 0 | #ifdef WITH_BROKER |
1011 | 0 | mux__add_out(mosq); |
1012 | | #else |
1013 | | mosq->want_write = true; |
1014 | | #endif |
1015 | 0 | errno = EAGAIN; |
1016 | 0 | }else if(err == SSL_ERROR_SSL){ |
1017 | 0 | net__print_ssl_error(mosq, "while trying to get the error"); |
1018 | 0 | errno = EPROTO; |
1019 | | /* else if SSL_ERROR_SYSCALL leave errno alone */ |
1020 | 0 | } |
1021 | 0 | ERR_clear_error(); |
1022 | | #ifdef WIN32 |
1023 | | WSASetLastError(errno); |
1024 | | #endif |
1025 | 0 | } |
1026 | | #endif |
1027 | | |
1028 | | |
1029 | | ssize_t net__read(struct mosquitto *mosq, void *buf, size_t count) |
1030 | 0 | { |
1031 | 0 | #ifdef WITH_TLS |
1032 | 0 | int ret; |
1033 | 0 | #endif |
1034 | 0 | assert(mosq); |
1035 | 0 | errno = 0; |
1036 | 0 | #ifdef WITH_TLS |
1037 | 0 | if(mosq->ssl){ |
1038 | 0 | ERR_clear_error(); |
1039 | 0 | ret = SSL_read(mosq->ssl, buf, (int)count); |
1040 | 0 | if(ret <= 0){ |
1041 | 0 | net__handle_ssl(mosq, ret); |
1042 | 0 | } |
1043 | 0 | return (ssize_t )ret; |
1044 | 0 | }else |
1045 | 0 | #endif |
1046 | 0 | { |
1047 | | /* Call normal read/recv */ |
1048 | |
|
1049 | 0 | #ifndef WIN32 |
1050 | 0 | return read(mosq->sock, buf, count); |
1051 | | #else |
1052 | | return recv(mosq->sock, buf, count, 0); |
1053 | | #endif |
1054 | 0 | } |
1055 | 0 | } |
1056 | | |
1057 | | |
1058 | | ssize_t net__write(struct mosquitto *mosq, const void *buf, size_t count) |
1059 | 0 | { |
1060 | 0 | #ifdef WITH_TLS |
1061 | 0 | int ret; |
1062 | 0 | #endif |
1063 | 0 | assert(mosq); |
1064 | |
|
1065 | 0 | errno = 0; |
1066 | 0 | #ifdef WITH_TLS |
1067 | 0 | if(mosq->ssl){ |
1068 | 0 | ERR_clear_error(); |
1069 | 0 | mosq->want_write = false; |
1070 | 0 | ret = SSL_write(mosq->ssl, buf, (int)count); |
1071 | 0 | if(ret < 0){ |
1072 | 0 | net__handle_ssl(mosq, ret); |
1073 | 0 | } |
1074 | 0 | return (ssize_t )ret; |
1075 | 0 | }else |
1076 | | /* Call normal write/send */ |
1077 | 0 | #endif |
1078 | 0 | { |
1079 | 0 | return send(mosq->sock, buf, count, MSG_NOSIGNAL); |
1080 | 0 | } |
1081 | 0 | } |
1082 | | |
1083 | | |
1084 | | int net__socket_nonblock(mosq_sock_t *sock) |
1085 | 0 | { |
1086 | 0 | #ifndef WIN32 |
1087 | 0 | int opt; |
1088 | | /* Set non-blocking */ |
1089 | 0 | opt = fcntl(*sock, F_GETFL, 0); |
1090 | 0 | if(opt == -1){ |
1091 | 0 | COMPAT_CLOSE(*sock); |
1092 | 0 | *sock = INVALID_SOCKET; |
1093 | 0 | return MOSQ_ERR_ERRNO; |
1094 | 0 | } |
1095 | 0 | if(fcntl(*sock, F_SETFL, opt | O_NONBLOCK) == -1){ |
1096 | | /* If either fcntl fails, don't want to allow this client to connect. */ |
1097 | 0 | COMPAT_CLOSE(*sock); |
1098 | 0 | *sock = INVALID_SOCKET; |
1099 | 0 | return MOSQ_ERR_ERRNO; |
1100 | 0 | } |
1101 | | #else |
1102 | | unsigned long opt = 1; |
1103 | | if(ioctlsocket(*sock, FIONBIO, &opt)){ |
1104 | | COMPAT_CLOSE(*sock); |
1105 | | *sock = INVALID_SOCKET; |
1106 | | return MOSQ_ERR_ERRNO; |
1107 | | } |
1108 | | #endif |
1109 | 0 | return MOSQ_ERR_SUCCESS; |
1110 | 0 | } |
1111 | | |
1112 | | |
1113 | | #ifndef WITH_BROKER |
1114 | | |
1115 | | |
1116 | | int net__socketpair(mosq_sock_t *pairR, mosq_sock_t *pairW) |
1117 | | { |
1118 | | #ifdef WIN32 |
1119 | | int family[2] = {AF_INET, AF_INET6}; |
1120 | | int i; |
1121 | | struct sockaddr_storage ss; |
1122 | | struct sockaddr_in *sa = (struct sockaddr_in *)&ss; |
1123 | | struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&ss; |
1124 | | socklen_t ss_len; |
1125 | | mosq_sock_t spR, spW; |
1126 | | |
1127 | | mosq_sock_t listensock; |
1128 | | |
1129 | | *pairR = INVALID_SOCKET; |
1130 | | *pairW = INVALID_SOCKET; |
1131 | | |
1132 | | for(i=0; i<2; i++){ |
1133 | | memset(&ss, 0, sizeof(ss)); |
1134 | | if(family[i] == AF_INET){ |
1135 | | sa->sin_family = family[i]; |
1136 | | sa->sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
1137 | | sa->sin_port = 0; |
1138 | | ss_len = sizeof(struct sockaddr_in); |
1139 | | }else if(family[i] == AF_INET6){ |
1140 | | sa6->sin6_family = family[i]; |
1141 | | sa6->sin6_addr = in6addr_loopback; |
1142 | | sa6->sin6_port = 0; |
1143 | | ss_len = sizeof(struct sockaddr_in6); |
1144 | | }else{ |
1145 | | return MOSQ_ERR_INVAL; |
1146 | | } |
1147 | | |
1148 | | listensock = socket(family[i], SOCK_STREAM, IPPROTO_TCP); |
1149 | | if(listensock == -1){ |
1150 | | continue; |
1151 | | } |
1152 | | |
1153 | | if(bind(listensock, (struct sockaddr *)&ss, ss_len) == -1){ |
1154 | | COMPAT_CLOSE(listensock); |
1155 | | continue; |
1156 | | } |
1157 | | |
1158 | | if(listen(listensock, 1) == -1){ |
1159 | | COMPAT_CLOSE(listensock); |
1160 | | continue; |
1161 | | } |
1162 | | memset(&ss, 0, sizeof(ss)); |
1163 | | ss_len = sizeof(ss); |
1164 | | if(getsockname(listensock, (struct sockaddr *)&ss, &ss_len) < 0){ |
1165 | | COMPAT_CLOSE(listensock); |
1166 | | continue; |
1167 | | } |
1168 | | |
1169 | | if(family[i] == AF_INET){ |
1170 | | sa->sin_family = family[i]; |
1171 | | sa->sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
1172 | | ss_len = sizeof(struct sockaddr_in); |
1173 | | }else if(family[i] == AF_INET6){ |
1174 | | sa6->sin6_family = family[i]; |
1175 | | sa6->sin6_addr = in6addr_loopback; |
1176 | | ss_len = sizeof(struct sockaddr_in6); |
1177 | | } |
1178 | | |
1179 | | spR = socket(family[i], SOCK_STREAM, IPPROTO_TCP); |
1180 | | if(spR == -1){ |
1181 | | COMPAT_CLOSE(listensock); |
1182 | | continue; |
1183 | | } |
1184 | | if(net__socket_nonblock(&spR)){ |
1185 | | COMPAT_CLOSE(listensock); |
1186 | | continue; |
1187 | | } |
1188 | | if(connect(spR, (struct sockaddr *)&ss, ss_len) < 0){ |
1189 | | WINDOWS_SET_ERRNO(); |
1190 | | if(errno != EINPROGRESS && errno != COMPAT_EWOULDBLOCK){ |
1191 | | COMPAT_CLOSE(spR); |
1192 | | COMPAT_CLOSE(listensock); |
1193 | | continue; |
1194 | | } |
1195 | | } |
1196 | | spW = accept(listensock, NULL, 0); |
1197 | | if(spW == -1){ |
1198 | | WINDOWS_SET_ERRNO(); |
1199 | | if(errno != EINPROGRESS && errno != COMPAT_EWOULDBLOCK){ |
1200 | | COMPAT_CLOSE(spR); |
1201 | | COMPAT_CLOSE(listensock); |
1202 | | continue; |
1203 | | } |
1204 | | } |
1205 | | |
1206 | | if(net__socket_nonblock(&spW)){ |
1207 | | COMPAT_CLOSE(spR); |
1208 | | COMPAT_CLOSE(listensock); |
1209 | | continue; |
1210 | | } |
1211 | | COMPAT_CLOSE(listensock); |
1212 | | |
1213 | | *pairR = spR; |
1214 | | *pairW = spW; |
1215 | | return MOSQ_ERR_SUCCESS; |
1216 | | } |
1217 | | return MOSQ_ERR_UNKNOWN; |
1218 | | #else |
1219 | | int sv[2]; |
1220 | | |
1221 | | *pairR = INVALID_SOCKET; |
1222 | | *pairW = INVALID_SOCKET; |
1223 | | |
1224 | | if(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1){ |
1225 | | return MOSQ_ERR_ERRNO; |
1226 | | } |
1227 | | if(net__socket_nonblock(&sv[0])){ |
1228 | | COMPAT_CLOSE(sv[1]); |
1229 | | return MOSQ_ERR_ERRNO; |
1230 | | } |
1231 | | if(net__socket_nonblock(&sv[1])){ |
1232 | | COMPAT_CLOSE(sv[0]); |
1233 | | return MOSQ_ERR_ERRNO; |
1234 | | } |
1235 | | *pairR = sv[0]; |
1236 | | *pairW = sv[1]; |
1237 | | return MOSQ_ERR_SUCCESS; |
1238 | | #endif |
1239 | | } |
1240 | | #endif |
1241 | | |
1242 | | #ifndef WITH_BROKER |
1243 | | |
1244 | | |
1245 | | void *mosquitto_ssl_get(struct mosquitto *mosq) |
1246 | | { |
1247 | | #ifdef WITH_TLS |
1248 | | return mosq->ssl; |
1249 | | #else |
1250 | | UNUSED(mosq); |
1251 | | |
1252 | | return NULL; |
1253 | | #endif |
1254 | | } |
1255 | | #endif |