packet_mosq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. Copyright (c) 2009-2020 Roger Light <roger@atchoo.org>
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License 2.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. https://www.eclipse.org/legal/epl-2.0/
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
  11. Contributors:
  12. Roger Light - initial implementation and documentation.
  13. */
  14. #include "config.h"
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #ifdef WITH_BROKER
  19. # include "mosquitto_broker_internal.h"
  20. # ifdef WITH_WEBSOCKETS
  21. # include <libwebsockets.h>
  22. # endif
  23. #else
  24. # include "read_handle.h"
  25. #endif
  26. #include "memory_mosq.h"
  27. #include "mqtt_protocol.h"
  28. #include "net_mosq.h"
  29. #include "packet_mosq.h"
  30. #include "read_handle.h"
  31. #include "util_mosq.h"
  32. #ifdef WITH_BROKER
  33. # include "sys_tree.h"
  34. # include "send_mosq.h"
  35. #else
  36. # define G_BYTES_RECEIVED_INC(A)
  37. # define G_BYTES_SENT_INC(A)
  38. # define G_MSGS_SENT_INC(A)
  39. # define G_PUB_MSGS_SENT_INC(A)
  40. #endif
  41. int packet__alloc(struct mosquitto__packet *packet)
  42. {
  43. uint8_t remaining_bytes[5], byte;
  44. uint32_t remaining_length;
  45. int i;
  46. assert(packet);
  47. remaining_length = packet->remaining_length;
  48. packet->payload = NULL;
  49. packet->remaining_count = 0;
  50. do{
  51. byte = remaining_length % 128;
  52. remaining_length = remaining_length / 128;
  53. /* If there are more digits to encode, set the top bit of this digit */
  54. if(remaining_length > 0){
  55. byte = byte | 0x80;
  56. }
  57. remaining_bytes[packet->remaining_count] = byte;
  58. packet->remaining_count++;
  59. }while(remaining_length > 0 && packet->remaining_count < 5);
  60. if(packet->remaining_count == 5) return MOSQ_ERR_PAYLOAD_SIZE;
  61. packet->packet_length = packet->remaining_length + 1 + (uint8_t)packet->remaining_count;
  62. #ifdef WITH_WEBSOCKETS
  63. packet->payload = mosquitto__malloc(sizeof(uint8_t)*packet->packet_length + LWS_PRE);
  64. #else
  65. packet->payload = mosquitto__malloc(sizeof(uint8_t)*packet->packet_length);
  66. #endif
  67. if(!packet->payload) return MOSQ_ERR_NOMEM;
  68. packet->payload[0] = packet->command;
  69. for(i=0; i<packet->remaining_count; i++){
  70. packet->payload[i+1] = remaining_bytes[i];
  71. }
  72. packet->pos = 1U + (uint8_t)packet->remaining_count;
  73. return MOSQ_ERR_SUCCESS;
  74. }
  75. void packet__cleanup(struct mosquitto__packet *packet)
  76. {
  77. if(!packet) return;
  78. /* Free data and reset values */
  79. packet->command = 0;
  80. packet->remaining_count = 0;
  81. packet->remaining_mult = 1;
  82. packet->remaining_length = 0;
  83. mosquitto__free(packet->payload);
  84. packet->payload = NULL;
  85. packet->to_process = 0;
  86. packet->pos = 0;
  87. }
  88. void packet__cleanup_all_no_locks(struct mosquitto *mosq)
  89. {
  90. struct mosquitto__packet *packet;
  91. /* Out packet cleanup */
  92. if(mosq->out_packet && !mosq->current_out_packet){
  93. mosq->current_out_packet = mosq->out_packet;
  94. mosq->out_packet = mosq->out_packet->next;
  95. }
  96. while(mosq->current_out_packet){
  97. packet = mosq->current_out_packet;
  98. /* Free data and reset values */
  99. mosq->current_out_packet = mosq->out_packet;
  100. if(mosq->out_packet){
  101. mosq->out_packet = mosq->out_packet->next;
  102. }
  103. packet__cleanup(packet);
  104. mosquitto__free(packet);
  105. }
  106. mosq->out_packet_count = 0;
  107. packet__cleanup(&mosq->in_packet);
  108. }
  109. void packet__cleanup_all(struct mosquitto *mosq)
  110. {
  111. pthread_mutex_lock(&mosq->current_out_packet_mutex);
  112. pthread_mutex_lock(&mosq->out_packet_mutex);
  113. packet__cleanup_all_no_locks(mosq);
  114. pthread_mutex_unlock(&mosq->out_packet_mutex);
  115. pthread_mutex_unlock(&mosq->current_out_packet_mutex);
  116. }
  117. int packet__queue(struct mosquitto *mosq, struct mosquitto__packet *packet)
  118. {
  119. #ifndef WITH_BROKER
  120. char sockpair_data = 0;
  121. #endif
  122. assert(mosq);
  123. assert(packet);
  124. packet->pos = 0;
  125. packet->to_process = packet->packet_length;
  126. packet->next = NULL;
  127. pthread_mutex_lock(&mosq->out_packet_mutex);
  128. if(mosq->out_packet){
  129. mosq->out_packet_last->next = packet;
  130. }else{
  131. mosq->out_packet = packet;
  132. }
  133. mosq->out_packet_last = packet;
  134. mosq->out_packet_count++;
  135. pthread_mutex_unlock(&mosq->out_packet_mutex);
  136. #ifdef WITH_BROKER
  137. # ifdef WITH_WEBSOCKETS
  138. if(mosq->wsi){
  139. lws_callback_on_writable(mosq->wsi);
  140. return MOSQ_ERR_SUCCESS;
  141. }else{
  142. return packet__write(mosq);
  143. }
  144. # else
  145. return packet__write(mosq);
  146. # endif
  147. #else
  148. /* Write a single byte to sockpairW (connected to sockpairR) to break out
  149. * of select() if in threaded mode. */
  150. if(mosq->sockpairW != INVALID_SOCKET){
  151. #ifndef WIN32
  152. if(write(mosq->sockpairW, &sockpair_data, 1)){
  153. }
  154. #else
  155. send(mosq->sockpairW, &sockpair_data, 1, 0);
  156. #endif
  157. }
  158. if(mosq->in_callback == false && mosq->threaded == mosq_ts_none){
  159. return packet__write(mosq);
  160. }else{
  161. return MOSQ_ERR_SUCCESS;
  162. }
  163. #endif
  164. }
  165. int packet__check_oversize(struct mosquitto *mosq, uint32_t remaining_length)
  166. {
  167. uint32_t len;
  168. if(mosq->maximum_packet_size == 0) return MOSQ_ERR_SUCCESS;
  169. len = remaining_length + packet__varint_bytes(remaining_length);
  170. if(len > mosq->maximum_packet_size){
  171. return MOSQ_ERR_OVERSIZE_PACKET;
  172. }else{
  173. return MOSQ_ERR_SUCCESS;
  174. }
  175. }
  176. int packet__write(struct mosquitto *mosq)
  177. {
  178. ssize_t write_length;
  179. struct mosquitto__packet *packet;
  180. enum mosquitto_client_state state;
  181. if(!mosq) return MOSQ_ERR_INVAL;
  182. if(mosq->sock == INVALID_SOCKET) return MOSQ_ERR_NO_CONN;
  183. pthread_mutex_lock(&mosq->current_out_packet_mutex);
  184. pthread_mutex_lock(&mosq->out_packet_mutex);
  185. if(mosq->out_packet && !mosq->current_out_packet){
  186. mosq->current_out_packet = mosq->out_packet;
  187. mosq->out_packet = mosq->out_packet->next;
  188. if(!mosq->out_packet){
  189. mosq->out_packet_last = NULL;
  190. }
  191. mosq->out_packet_count--;
  192. }
  193. pthread_mutex_unlock(&mosq->out_packet_mutex);
  194. #ifdef WITH_BROKER
  195. if(mosq->current_out_packet){
  196. mux__add_out(mosq);
  197. }
  198. #endif
  199. state = mosquitto__get_state(mosq);
  200. #if defined(WITH_TLS) && !defined(WITH_BROKER)
  201. if(state == mosq_cs_connect_pending || mosq->want_connect){
  202. #else
  203. if(state == mosq_cs_connect_pending){
  204. #endif
  205. pthread_mutex_unlock(&mosq->current_out_packet_mutex);
  206. return MOSQ_ERR_SUCCESS;
  207. }
  208. while(mosq->current_out_packet){
  209. packet = mosq->current_out_packet;
  210. while(packet->to_process > 0){
  211. write_length = net__write(mosq, &(packet->payload[packet->pos]), packet->to_process);
  212. if(write_length > 0){
  213. G_BYTES_SENT_INC(write_length);
  214. packet->to_process -= (uint32_t)write_length;
  215. packet->pos += (uint32_t)write_length;
  216. }else{
  217. #ifdef WIN32
  218. errno = WSAGetLastError();
  219. #endif
  220. if(errno == EAGAIN || errno == COMPAT_EWOULDBLOCK
  221. #ifdef WIN32
  222. || errno == WSAENOTCONN
  223. #endif
  224. ){
  225. pthread_mutex_unlock(&mosq->current_out_packet_mutex);
  226. return MOSQ_ERR_SUCCESS;
  227. }else{
  228. pthread_mutex_unlock(&mosq->current_out_packet_mutex);
  229. switch(errno){
  230. case COMPAT_ECONNRESET:
  231. return MOSQ_ERR_CONN_LOST;
  232. case COMPAT_EINTR:
  233. return MOSQ_ERR_SUCCESS;
  234. default:
  235. return MOSQ_ERR_ERRNO;
  236. }
  237. }
  238. }
  239. }
  240. G_MSGS_SENT_INC(1);
  241. if(((packet->command)&0xF6) == CMD_PUBLISH){
  242. G_PUB_MSGS_SENT_INC(1);
  243. #ifndef WITH_BROKER
  244. pthread_mutex_lock(&mosq->callback_mutex);
  245. if(mosq->on_publish){
  246. /* This is a QoS=0 message */
  247. mosq->in_callback = true;
  248. mosq->on_publish(mosq, mosq->userdata, packet->mid);
  249. mosq->in_callback = false;
  250. }
  251. if(mosq->on_publish_v5){
  252. /* This is a QoS=0 message */
  253. mosq->in_callback = true;
  254. mosq->on_publish_v5(mosq, mosq->userdata, packet->mid, 0, NULL);
  255. mosq->in_callback = false;
  256. }
  257. pthread_mutex_unlock(&mosq->callback_mutex);
  258. }else if(((packet->command)&0xF0) == CMD_DISCONNECT){
  259. do_client_disconnect(mosq, MOSQ_ERR_SUCCESS, NULL);
  260. packet__cleanup(packet);
  261. mosquitto__free(packet);
  262. return MOSQ_ERR_SUCCESS;
  263. #endif
  264. }else if(((packet->command)&0xF0) == CMD_PUBLISH){
  265. G_PUB_MSGS_SENT_INC(1);
  266. }
  267. /* Free data and reset values */
  268. pthread_mutex_lock(&mosq->out_packet_mutex);
  269. mosq->current_out_packet = mosq->out_packet;
  270. if(mosq->out_packet){
  271. mosq->out_packet = mosq->out_packet->next;
  272. if(!mosq->out_packet){
  273. mosq->out_packet_last = NULL;
  274. }
  275. mosq->out_packet_count--;
  276. }
  277. pthread_mutex_unlock(&mosq->out_packet_mutex);
  278. packet__cleanup(packet);
  279. mosquitto__free(packet);
  280. #ifdef WITH_BROKER
  281. mosq->next_msg_out = db.now_s + mosq->keepalive;
  282. #else
  283. pthread_mutex_lock(&mosq->msgtime_mutex);
  284. mosq->next_msg_out = mosquitto_time() + mosq->keepalive;
  285. pthread_mutex_unlock(&mosq->msgtime_mutex);
  286. #endif
  287. }
  288. #ifdef WITH_BROKER
  289. if (mosq->current_out_packet == NULL) {
  290. mux__remove_out(mosq);
  291. }
  292. #endif
  293. pthread_mutex_unlock(&mosq->current_out_packet_mutex);
  294. return MOSQ_ERR_SUCCESS;
  295. }
  296. int packet__read(struct mosquitto *mosq)
  297. {
  298. uint8_t byte;
  299. ssize_t read_length;
  300. int rc = 0;
  301. enum mosquitto_client_state state;
  302. if(!mosq){
  303. return MOSQ_ERR_INVAL;
  304. }
  305. if(mosq->sock == INVALID_SOCKET){
  306. return MOSQ_ERR_NO_CONN;
  307. }
  308. state = mosquitto__get_state(mosq);
  309. if(state == mosq_cs_connect_pending){
  310. return MOSQ_ERR_SUCCESS;
  311. }
  312. /* This gets called if pselect() indicates that there is network data
  313. * available - ie. at least one byte. What we do depends on what data we
  314. * already have.
  315. * If we've not got a command, attempt to read one and save it. This should
  316. * always work because it's only a single byte.
  317. * Then try to read the remaining length. This may fail because it is may
  318. * be more than one byte - will need to save data pending next read if it
  319. * does fail.
  320. * Then try to read the remaining payload, where 'payload' here means the
  321. * combined variable header and actual payload. This is the most likely to
  322. * fail due to longer length, so save current data and current position.
  323. * After all data is read, send to mosquitto__handle_packet() to deal with.
  324. * Finally, free the memory and reset everything to starting conditions.
  325. */
  326. if(!mosq->in_packet.command){
  327. read_length = net__read(mosq, &byte, 1);
  328. if(read_length == 1){
  329. mosq->in_packet.command = byte;
  330. #ifdef WITH_BROKER
  331. G_BYTES_RECEIVED_INC(1);
  332. /* Clients must send CONNECT as their first command. */
  333. if(!(mosq->bridge) && state == mosq_cs_connected && (byte&0xF0) != CMD_CONNECT){
  334. return MOSQ_ERR_PROTOCOL;
  335. }
  336. #endif
  337. }else{
  338. if(read_length == 0){
  339. return MOSQ_ERR_CONN_LOST; /* EOF */
  340. }
  341. #ifdef WIN32
  342. errno = WSAGetLastError();
  343. #endif
  344. if(errno == EAGAIN || errno == COMPAT_EWOULDBLOCK){
  345. return MOSQ_ERR_SUCCESS;
  346. }else{
  347. switch(errno){
  348. case COMPAT_ECONNRESET:
  349. return MOSQ_ERR_CONN_LOST;
  350. case COMPAT_EINTR:
  351. return MOSQ_ERR_SUCCESS;
  352. default:
  353. return MOSQ_ERR_ERRNO;
  354. }
  355. }
  356. }
  357. }
  358. /* remaining_count is the number of bytes that the remaining_length
  359. * parameter occupied in this incoming packet. We don't use it here as such
  360. * (it is used when allocating an outgoing packet), but we must be able to
  361. * determine whether all of the remaining_length parameter has been read.
  362. * remaining_count has three states here:
  363. * 0 means that we haven't read any remaining_length bytes
  364. * <0 means we have read some remaining_length bytes but haven't finished
  365. * >0 means we have finished reading the remaining_length bytes.
  366. */
  367. if(mosq->in_packet.remaining_count <= 0){
  368. do{
  369. read_length = net__read(mosq, &byte, 1);
  370. if(read_length == 1){
  371. mosq->in_packet.remaining_count--;
  372. /* Max 4 bytes length for remaining length as defined by protocol.
  373. * Anything more likely means a broken/malicious client.
  374. */
  375. if(mosq->in_packet.remaining_count < -4){
  376. return MOSQ_ERR_MALFORMED_PACKET;
  377. }
  378. G_BYTES_RECEIVED_INC(1);
  379. mosq->in_packet.remaining_length += (byte & 127) * mosq->in_packet.remaining_mult;
  380. mosq->in_packet.remaining_mult *= 128;
  381. }else{
  382. if(read_length == 0){
  383. return MOSQ_ERR_CONN_LOST; /* EOF */
  384. }
  385. #ifdef WIN32
  386. errno = WSAGetLastError();
  387. #endif
  388. if(errno == EAGAIN || errno == COMPAT_EWOULDBLOCK){
  389. return MOSQ_ERR_SUCCESS;
  390. }else{
  391. switch(errno){
  392. case COMPAT_ECONNRESET:
  393. return MOSQ_ERR_CONN_LOST;
  394. case COMPAT_EINTR:
  395. return MOSQ_ERR_SUCCESS;
  396. default:
  397. return MOSQ_ERR_ERRNO;
  398. }
  399. }
  400. }
  401. }while((byte & 128) != 0);
  402. /* We have finished reading remaining_length, so make remaining_count
  403. * positive. */
  404. mosq->in_packet.remaining_count = (int8_t)(mosq->in_packet.remaining_count * -1);
  405. #ifdef WITH_BROKER
  406. switch(mosq->in_packet.command & 0xF0){
  407. case CMD_CONNECT:
  408. if(mosq->in_packet.remaining_length > 100000){ /* Arbitrary limit, make configurable */
  409. return MOSQ_ERR_MALFORMED_PACKET;
  410. }
  411. break;
  412. case CMD_PUBACK:
  413. case CMD_PUBREC:
  414. case CMD_PUBREL:
  415. case CMD_PUBCOMP:
  416. case CMD_UNSUBACK:
  417. if(mosq->protocol != mosq_p_mqtt5 && mosq->in_packet.remaining_length != 2){
  418. return MOSQ_ERR_MALFORMED_PACKET;
  419. }
  420. break;
  421. case CMD_PINGREQ:
  422. case CMD_PINGRESP:
  423. if(mosq->in_packet.remaining_length != 0){
  424. return MOSQ_ERR_MALFORMED_PACKET;
  425. }
  426. break;
  427. case CMD_DISCONNECT:
  428. if(mosq->protocol != mosq_p_mqtt5 && mosq->in_packet.remaining_length != 0){
  429. return MOSQ_ERR_MALFORMED_PACKET;
  430. }
  431. break;
  432. }
  433. if(db.config->max_packet_size > 0 && mosq->in_packet.remaining_length+1 > db.config->max_packet_size){
  434. if(mosq->protocol == mosq_p_mqtt5){
  435. send__disconnect(mosq, MQTT_RC_PACKET_TOO_LARGE, NULL);
  436. }
  437. return MOSQ_ERR_OVERSIZE_PACKET;
  438. }
  439. #else
  440. /* FIXME - client case for incoming message received from broker too large */
  441. #endif
  442. if(mosq->in_packet.remaining_length > 0){
  443. mosq->in_packet.payload = mosquitto__malloc(mosq->in_packet.remaining_length*sizeof(uint8_t));
  444. if(!mosq->in_packet.payload){
  445. return MOSQ_ERR_NOMEM;
  446. }
  447. mosq->in_packet.to_process = mosq->in_packet.remaining_length;
  448. }
  449. }
  450. while(mosq->in_packet.to_process>0){
  451. read_length = net__read(mosq, &(mosq->in_packet.payload[mosq->in_packet.pos]), mosq->in_packet.to_process);
  452. if(read_length > 0){
  453. G_BYTES_RECEIVED_INC(read_length);
  454. mosq->in_packet.to_process -= (uint32_t)read_length;
  455. mosq->in_packet.pos += (uint32_t)read_length;
  456. }else{
  457. #ifdef WIN32
  458. errno = WSAGetLastError();
  459. #endif
  460. if(errno == EAGAIN || errno == COMPAT_EWOULDBLOCK){
  461. if(mosq->in_packet.to_process > 1000){
  462. /* Update last_msg_in time if more than 1000 bytes left to
  463. * receive. Helps when receiving large messages.
  464. * This is an arbitrary limit, but with some consideration.
  465. * If a client can't send 1000 bytes in a second it
  466. * probably shouldn't be using a 1 second keep alive. */
  467. #ifdef WITH_BROKER
  468. keepalive__update(mosq);
  469. #else
  470. pthread_mutex_lock(&mosq->msgtime_mutex);
  471. mosq->last_msg_in = mosquitto_time();
  472. pthread_mutex_unlock(&mosq->msgtime_mutex);
  473. #endif
  474. }
  475. return MOSQ_ERR_SUCCESS;
  476. }else{
  477. switch(errno){
  478. case COMPAT_ECONNRESET:
  479. return MOSQ_ERR_CONN_LOST;
  480. case COMPAT_EINTR:
  481. return MOSQ_ERR_SUCCESS;
  482. default:
  483. return MOSQ_ERR_ERRNO;
  484. }
  485. }
  486. }
  487. }
  488. /* All data for this packet is read. */
  489. mosq->in_packet.pos = 0;
  490. #ifdef WITH_BROKER
  491. G_MSGS_RECEIVED_INC(1);
  492. if(((mosq->in_packet.command)&0xF5) == CMD_PUBLISH){
  493. G_PUB_MSGS_RECEIVED_INC(1);
  494. }
  495. #endif
  496. rc = handle__packet(mosq);
  497. /* Free data and reset values */
  498. packet__cleanup(&mosq->in_packet);
  499. #ifdef WITH_BROKER
  500. keepalive__update(mosq);
  501. #else
  502. pthread_mutex_lock(&mosq->msgtime_mutex);
  503. mosq->last_msg_in = mosquitto_time();
  504. pthread_mutex_unlock(&mosq->msgtime_mutex);
  505. #endif
  506. return rc;
  507. }