mqtt_client.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. #include "mqtt_client.h"
  2. #include "hbase.h"
  3. #include "hlog.h"
  4. #include "hendian.h"
  5. static unsigned short mqtt_next_mid() {
  6. static unsigned short s_mid = 0;
  7. return ++s_mid;
  8. }
  9. static int mqtt_send_head(hio_t* io, int type, int length) {
  10. mqtt_head_t head;
  11. memset(&head, 0, sizeof(head));
  12. head.type = type;
  13. head.length = length;
  14. unsigned char headbuf[8] = { 0 };
  15. int headlen = mqtt_head_pack(&head, headbuf);
  16. return hio_write(io, headbuf, headlen);
  17. }
  18. static int mqtt_send_head_with_mid(hio_t* io, int type, unsigned short mid) {
  19. mqtt_head_t head;
  20. memset(&head, 0, sizeof(head));
  21. head.type = type;
  22. if (head.type == MQTT_TYPE_PUBREL) {
  23. head.qos = 1;
  24. }
  25. head.length = 2;
  26. unsigned char headbuf[8] = { 0 };
  27. unsigned char* p = headbuf;
  28. int headlen = mqtt_head_pack(&head, p);
  29. p += headlen;
  30. PUSH16(p, mid);
  31. return hio_write(io, headbuf, headlen + 2);
  32. }
  33. static void mqtt_send_ping(hio_t* io) {
  34. mqtt_send_head(io, MQTT_TYPE_PINGREQ, 0);
  35. }
  36. static void mqtt_send_pong(hio_t* io) {
  37. mqtt_send_head(io, MQTT_TYPE_PINGRESP, 0);
  38. }
  39. static void mqtt_send_disconnect(hio_t* io) {
  40. mqtt_send_head(io, MQTT_TYPE_DISCONNECT, 0);
  41. }
  42. static int mqtt_client_login(mqtt_client_t* cli) {
  43. int len = MQTT_CONN_HEAD_LEN;
  44. unsigned short cid_len = 0,
  45. will_topic_len = 0,
  46. will_payload_len = 0,
  47. username_len = 0,
  48. password_len = 0;
  49. unsigned char conn_flags = 0;
  50. if (*cli->client_id) {
  51. cid_len = strlen(cli->client_id);
  52. len += cid_len;
  53. }
  54. if (cid_len == 0) cli->clean_session = 1;
  55. if (cli->clean_session) {
  56. conn_flags |= MQTT_CONN_CLEAN_SESSION;
  57. }
  58. if (cli->will && cli->will->topic && cli->will->payload) {
  59. will_topic_len = cli->will->topic_len ? cli->will->topic_len : strlen(cli->will->topic);
  60. will_payload_len = cli->will->payload_len ? cli->will->payload_len : strlen(cli->will->payload);
  61. if (will_topic_len && will_payload_len) {
  62. conn_flags |= MQTT_CONN_HAS_WILL;
  63. conn_flags |= ((cli->will->qos & 3) << 3);
  64. if (cli->will->retain) {
  65. conn_flags |= MQTT_CONN_WILL_RETAIN;
  66. }
  67. len += 2 + will_topic_len;
  68. len += 2 + will_payload_len;
  69. }
  70. }
  71. if (*cli->username) {
  72. username_len = strlen(cli->username);
  73. if (username_len) {
  74. conn_flags |= MQTT_CONN_HAS_USERNAME;
  75. len += 2 + username_len;
  76. }
  77. }
  78. if (*cli->password) {
  79. password_len = strlen(cli->password);
  80. if (password_len) {
  81. conn_flags |= MQTT_CONN_HAS_PASSWORD;
  82. len += 2 + password_len;
  83. }
  84. }
  85. mqtt_head_t head;
  86. memset(&head, 0, sizeof(head));
  87. head.type = MQTT_TYPE_CONNECT;
  88. head.length = len;
  89. int buflen = mqtt_estimate_length(&head);
  90. unsigned char* buf = NULL;
  91. HV_STACK_ALLOC(buf, buflen);
  92. unsigned char* p = buf;
  93. int headlen = mqtt_head_pack(&head, p);
  94. p += headlen;
  95. // TODO: just implement MQTT_PROTOCOL_V311
  96. PUSH16(p, 4);
  97. PUSH_N(p, "MQTT", 4);
  98. PUSH8(p, MQTT_PROTOCOL_V311);
  99. PUSH8(p, conn_flags);
  100. PUSH16(p, cli->keepalive);
  101. PUSH16(p, cid_len);
  102. if (cid_len > 0) {
  103. PUSH_N(p, cli->client_id, cid_len);
  104. }
  105. if (conn_flags & MQTT_CONN_HAS_WILL) {
  106. PUSH16(p, will_topic_len);
  107. PUSH_N(p, cli->will->topic, will_topic_len);
  108. PUSH16(p, will_payload_len);
  109. PUSH_N(p, cli->will->payload, will_payload_len);
  110. }
  111. if (conn_flags & MQTT_CONN_HAS_USERNAME) {
  112. PUSH16(p, username_len);
  113. PUSH_N(p, cli->username, username_len);
  114. }
  115. if (conn_flags & MQTT_CONN_HAS_PASSWORD) {
  116. PUSH16(p, password_len);
  117. PUSH_N(p, cli->password, password_len);
  118. }
  119. int nwrite = hio_write(cli->io, buf, p - buf);
  120. HV_STACK_FREE(buf);
  121. return nwrite < 0 ? nwrite : 0;
  122. }
  123. static void reconnect_timer_cb(htimer_t* timer) {
  124. mqtt_client_t* cli = (mqtt_client_t*)hevent_userdata(timer);
  125. if (cli == NULL) return;
  126. cli->reconn_timer = NULL;
  127. mqtt_client_reconnect(cli);
  128. }
  129. static void on_close(hio_t* io) {
  130. mqtt_client_t* cli = (mqtt_client_t*)hevent_userdata(io);
  131. if (cli->cb) {
  132. cli->head.type = MQTT_TYPE_DISCONNECT;
  133. cli->cb(cli, cli->head.type);
  134. }
  135. // reconnect
  136. if (cli->reconn_setting && reconn_setting_can_retry(cli->reconn_setting)) {
  137. uint32_t delay = reconn_setting_calc_delay(cli->reconn_setting);
  138. cli->reconn_timer = htimer_add(cli->loop, reconnect_timer_cb, delay, 1);
  139. hevent_set_userdata(cli->reconn_timer, cli);
  140. }
  141. }
  142. static void on_packet(hio_t* io, void* buf, int len) {
  143. mqtt_client_t* cli = (mqtt_client_t*)hevent_userdata(io);
  144. unsigned char* p = (unsigned char*)buf;
  145. unsigned char* end = p + len;
  146. memset(&cli->head, 0, sizeof(mqtt_head_t));
  147. int headlen = mqtt_head_unpack(&cli->head, p, len);
  148. if (headlen <= 0) return;
  149. p += headlen;
  150. switch (cli->head.type) {
  151. // case MQTT_TYPE_CONNECT:
  152. case MQTT_TYPE_CONNACK:
  153. {
  154. if (cli->head.length < 2) {
  155. hloge("MQTT CONNACK malformed!");
  156. hio_close(io);
  157. return;
  158. }
  159. unsigned char conn_flags = 0, rc = 0;
  160. POP8(p, conn_flags);
  161. POP8(p, rc);
  162. if (rc != MQTT_CONNACK_ACCEPTED) {
  163. cli->error = rc;
  164. hloge("MQTT CONNACK error=%d", cli->error);
  165. hio_close(io);
  166. return;
  167. }
  168. if (cli->keepalive) {
  169. hio_set_heartbeat(io, cli->keepalive * 1000, mqtt_send_ping);
  170. }
  171. }
  172. break;
  173. case MQTT_TYPE_PUBLISH:
  174. {
  175. if (cli->head.length < 2) {
  176. hloge("MQTT PUBLISH malformed!");
  177. hio_close(io);
  178. return;
  179. }
  180. memset(&cli->message, 0, sizeof(mqtt_message_t));
  181. POP16(p, cli->message.topic_len);
  182. if (end - p < cli->message.topic_len) {
  183. hloge("MQTT PUBLISH malformed!");
  184. hio_close(io);
  185. return;
  186. }
  187. // NOTE: Not deep copy
  188. cli->message.topic = (char*)p;
  189. p += cli->message.topic_len;
  190. if (cli->head.qos > 0) {
  191. if (end - p < 2) {
  192. hloge("MQTT PUBLISH malformed!");
  193. hio_close(io);
  194. return;
  195. }
  196. POP16(p, cli->mid);
  197. }
  198. cli->message.payload_len = end - p;
  199. if (cli->message.payload_len > 0) {
  200. // NOTE: Not deep copy
  201. cli->message.payload = (char*)p;
  202. }
  203. cli->message.qos = cli->head.qos;
  204. if (cli->message.qos == 0) {
  205. // Do nothing
  206. } else if (cli->message.qos == 1) {
  207. mqtt_send_head_with_mid(io, MQTT_TYPE_PUBACK, cli->mid);
  208. } else if (cli->message.qos == 2) {
  209. mqtt_send_head_with_mid(io, MQTT_TYPE_PUBREC, cli->mid);
  210. }
  211. }
  212. break;
  213. case MQTT_TYPE_PUBACK:
  214. case MQTT_TYPE_PUBREC:
  215. case MQTT_TYPE_PUBREL:
  216. case MQTT_TYPE_PUBCOMP:
  217. {
  218. if (cli->head.length < 2) {
  219. hloge("MQTT PUBACK malformed!");
  220. hio_close(io);
  221. return;
  222. }
  223. POP16(p, cli->mid);
  224. if (cli->head.type == MQTT_TYPE_PUBREC) {
  225. mqtt_send_head_with_mid(io, MQTT_TYPE_PUBREL, cli->mid);
  226. } else if (cli->head.type == MQTT_TYPE_PUBREL) {
  227. mqtt_send_head_with_mid(io, MQTT_TYPE_PUBCOMP, cli->mid);
  228. }
  229. }
  230. break;
  231. // case MQTT_TYPE_SUBSCRIBE:
  232. // break;
  233. case MQTT_TYPE_SUBACK:
  234. {
  235. if (cli->head.length < 2) {
  236. hloge("MQTT SUBACK malformed!");
  237. hio_close(io);
  238. return;
  239. }
  240. POP16(p, cli->mid);
  241. }
  242. break;
  243. // case MQTT_TYPE_UNSUBSCRIBE:
  244. // break;
  245. case MQTT_TYPE_UNSUBACK:
  246. {
  247. if (cli->head.length < 2) {
  248. hloge("MQTT UNSUBACK malformed!");
  249. hio_close(io);
  250. return;
  251. }
  252. POP16(p, cli->mid);
  253. }
  254. break;
  255. case MQTT_TYPE_PINGREQ:
  256. mqtt_send_pong(io);
  257. return;
  258. case MQTT_TYPE_PINGRESP:
  259. return;
  260. case MQTT_TYPE_DISCONNECT:
  261. hio_close(io);
  262. return;
  263. default:
  264. hloge("MQTT client received wrong type=%d", (int)cli->head.type);
  265. hio_close(io);
  266. return;
  267. }
  268. if (cli->cb) {
  269. cli->cb(cli, cli->head.type);
  270. }
  271. }
  272. static void on_connect(hio_t* io) {
  273. mqtt_client_t* cli = (mqtt_client_t*)hevent_userdata(io);
  274. if (cli->cb) {
  275. cli->head.type = MQTT_TYPE_CONNECT;
  276. cli->cb(cli, cli->head.type);
  277. }
  278. if (cli->reconn_setting) {
  279. reconn_setting_reset(cli->reconn_setting);
  280. }
  281. static unpack_setting_t mqtt_unpack_setting;
  282. mqtt_unpack_setting.mode = UNPACK_BY_LENGTH_FIELD;
  283. mqtt_unpack_setting.package_max_length = DEFAULT_MQTT_PACKAGE_MAX_LENGTH;
  284. mqtt_unpack_setting.body_offset = 2;
  285. mqtt_unpack_setting.length_field_offset = 1;
  286. mqtt_unpack_setting.length_field_bytes = 1;
  287. mqtt_unpack_setting.length_field_coding = ENCODE_BY_VARINT;
  288. hio_set_unpack(io, &mqtt_unpack_setting);
  289. // start recv packet
  290. hio_setcb_read(io, on_packet);
  291. hio_read(io);
  292. mqtt_client_login(cli);
  293. }
  294. mqtt_client_t* mqtt_client_new(hloop_t* loop) {
  295. if (loop == NULL) {
  296. loop = hloop_new(HLOOP_FLAG_AUTO_FREE);
  297. if (loop == NULL) return NULL;
  298. }
  299. mqtt_client_t* cli = NULL;
  300. HV_ALLOC_SIZEOF(cli);
  301. if (cli == NULL) return NULL;
  302. cli->loop = loop;
  303. cli->keepalive = DEFAULT_MQTT_KEEPALIVE;
  304. hmutex_init(&cli->mutex_);
  305. return cli;
  306. }
  307. void mqtt_client_free(mqtt_client_t* cli) {
  308. if (!cli) return;
  309. hmutex_destroy(&cli->mutex_);
  310. if (cli->reconn_timer) {
  311. htimer_del(cli->reconn_timer);
  312. cli->reconn_timer = NULL;
  313. }
  314. if (cli->ssl_ctx && cli->alloced_ssl_ctx) {
  315. hssl_ctx_free(cli->ssl_ctx);
  316. cli->ssl_ctx = NULL;
  317. }
  318. HV_FREE(cli->reconn_setting);
  319. HV_FREE(cli->will);
  320. HV_FREE(cli);
  321. }
  322. void mqtt_client_run (mqtt_client_t* cli) {
  323. if (!cli || !cli->loop) return;
  324. hloop_run(cli->loop);
  325. }
  326. void mqtt_client_stop(mqtt_client_t* cli) {
  327. if (!cli || !cli->loop) return;
  328. hloop_stop(cli->loop);
  329. }
  330. void mqtt_client_set_id(mqtt_client_t* cli, const char* id) {
  331. if (!cli || !id) return;
  332. safe_strncpy(cli->client_id, id, sizeof(cli->client_id));
  333. }
  334. void mqtt_client_set_will(mqtt_client_t* cli, mqtt_message_t* will) {
  335. if (!cli || !will) return;
  336. if (cli->will == NULL) {
  337. HV_ALLOC_SIZEOF(cli->will);
  338. }
  339. memcpy(cli->will, will, sizeof(mqtt_message_t));
  340. }
  341. void mqtt_client_set_auth(mqtt_client_t* cli, const char* username, const char* password) {
  342. if (!cli) return;
  343. if (username) {
  344. safe_strncpy(cli->username, username, sizeof(cli->username));
  345. }
  346. if (password) {
  347. safe_strncpy(cli->password, password, sizeof(cli->password));
  348. }
  349. }
  350. void mqtt_client_set_callback(mqtt_client_t* cli, mqtt_client_cb cb) {
  351. if (!cli) return;
  352. cli->cb = cb;
  353. }
  354. void mqtt_client_set_userdata(mqtt_client_t* cli, void* userdata) {
  355. if (!cli) return;
  356. cli->userdata = userdata;
  357. }
  358. void* mqtt_client_get_userdata(mqtt_client_t* cli) {
  359. if (!cli) return NULL;
  360. return cli->userdata;
  361. }
  362. int mqtt_client_get_last_error(mqtt_client_t* cli) {
  363. if (!cli) return -1;
  364. return cli->error;
  365. }
  366. int mqtt_client_set_ssl_ctx(mqtt_client_t* cli, hssl_ctx_t ssl_ctx) {
  367. cli->ssl_ctx = ssl_ctx;
  368. return 0;
  369. }
  370. int mqtt_client_new_ssl_ctx(mqtt_client_t* cli, hssl_ctx_opt_t* opt) {
  371. opt->endpoint = HSSL_CLIENT;
  372. hssl_ctx_t ssl_ctx = hssl_ctx_new(opt);
  373. if (ssl_ctx == NULL) return HSSL_ERROR;
  374. cli->alloced_ssl_ctx = true;
  375. return mqtt_client_set_ssl_ctx(cli, ssl_ctx);
  376. }
  377. int mqtt_client_set_reconnect(mqtt_client_t* cli, reconn_setting_t* reconn) {
  378. if (reconn == NULL) {
  379. HV_FREE(cli->reconn_setting);
  380. return 0;
  381. }
  382. if (cli->reconn_setting == NULL) {
  383. HV_ALLOC_SIZEOF(cli->reconn_setting);
  384. }
  385. *cli->reconn_setting = *reconn;
  386. return 0;
  387. }
  388. int mqtt_client_reconnect(mqtt_client_t* cli) {
  389. mqtt_client_connect(cli, cli->host, cli->port, cli->ssl);
  390. return 0;
  391. }
  392. int mqtt_client_connect(mqtt_client_t* cli, const char* host, int port, int ssl) {
  393. if (!cli) return -1;
  394. safe_strncpy(cli->host, host, sizeof(cli->host));
  395. cli->port = port;
  396. cli->ssl = ssl;
  397. hio_t* io = hio_create_socket(cli->loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
  398. if (io == NULL) return -1;
  399. if (ssl) {
  400. if (cli->ssl_ctx) {
  401. hio_set_ssl_ctx(io, cli->ssl_ctx);
  402. }
  403. hio_enable_ssl(io);
  404. }
  405. cli->io = io;
  406. hevent_set_userdata(io, cli);
  407. hio_setcb_connect(io, on_connect);
  408. hio_setcb_close(io, on_close);
  409. return hio_connect(io);
  410. }
  411. int mqtt_client_disconnect(mqtt_client_t* cli) {
  412. if (!cli || !cli->io) return -1;
  413. // cancel reconnect first
  414. mqtt_client_set_reconnect(cli, NULL);
  415. mqtt_send_disconnect(cli->io);
  416. return hio_close(cli->io);
  417. }
  418. int mqtt_client_publish(mqtt_client_t* cli, mqtt_message_t* msg) {
  419. if (!cli || !cli->io || !msg) return -1;
  420. int topic_len = msg->topic_len ? msg->topic_len : strlen(msg->topic);
  421. int payload_len = msg->payload_len ? msg->payload_len : strlen(msg->payload);
  422. int len = 2 + topic_len + payload_len;
  423. if (msg->qos > 0) len += 2; // mid
  424. unsigned short mid = 0;
  425. mqtt_head_t head;
  426. memset(&head, 0, sizeof(head));
  427. head.type = MQTT_TYPE_PUBLISH;
  428. head.qos = msg->qos & 3;
  429. head.retain = msg->retain;
  430. head.length = len;
  431. int buflen = mqtt_estimate_length(&head);
  432. // NOTE: send payload alone
  433. buflen -= payload_len;
  434. unsigned char* buf = NULL;
  435. HV_STACK_ALLOC(buf, buflen);
  436. unsigned char* p = buf;
  437. int headlen = mqtt_head_pack(&head, p);
  438. p += headlen;
  439. PUSH16(p, topic_len);
  440. PUSH_N(p, msg->topic, topic_len);
  441. if (msg->qos) {
  442. mid = mqtt_next_mid();
  443. PUSH16(p, mid);
  444. }
  445. hmutex_lock(&cli->mutex_);
  446. // send head + topic + mid
  447. int nwrite = hio_write(cli->io, buf, p - buf);
  448. HV_STACK_FREE(buf);
  449. if (nwrite < 0) {
  450. goto unlock;
  451. }
  452. // send payload
  453. nwrite = hio_write(cli->io, msg->payload, payload_len);
  454. unlock:
  455. hmutex_unlock(&cli->mutex_);
  456. return nwrite < 0 ? nwrite : mid;
  457. }
  458. int mqtt_client_subscribe(mqtt_client_t* cli, const char* topic, int qos) {
  459. if (!cli || !cli->io || !topic) return -1;
  460. int topic_len = strlen(topic);
  461. int len = 2 + 2 + topic_len + 1;
  462. mqtt_head_t head;
  463. memset(&head, 0, sizeof(head));
  464. head.type = MQTT_TYPE_SUBSCRIBE;
  465. head.qos = 1;
  466. head.length = len;
  467. int buflen = mqtt_estimate_length(&head);
  468. unsigned char* buf = NULL;
  469. HV_STACK_ALLOC(buf, buflen);
  470. unsigned char* p = buf;
  471. int headlen = mqtt_head_pack(&head, p);
  472. p += headlen;
  473. unsigned short mid = mqtt_next_mid();
  474. PUSH16(p, mid);
  475. PUSH16(p, topic_len);
  476. PUSH_N(p, topic, topic_len);
  477. PUSH8(p, qos & 3);
  478. // send head + mid + topic + qos
  479. int nwrite = hio_write(cli->io, buf, p - buf);
  480. HV_STACK_FREE(buf);
  481. return nwrite < 0 ? nwrite : mid;
  482. }
  483. int mqtt_client_unsubscribe(mqtt_client_t* cli, const char* topic) {
  484. if (!cli || !cli->io || !topic) return -1;
  485. int topic_len = strlen(topic);
  486. int len = 2 + 2 + topic_len;
  487. mqtt_head_t head;
  488. memset(&head, 0, sizeof(head));
  489. head.type = MQTT_TYPE_UNSUBSCRIBE;
  490. head.qos = 1;
  491. head.length = len;
  492. int buflen = mqtt_estimate_length(&head);
  493. unsigned char* buf = NULL;
  494. HV_STACK_ALLOC(buf, buflen);
  495. unsigned char* p = buf;
  496. int headlen = mqtt_head_pack(&head, p);
  497. p += headlen;
  498. unsigned short mid = mqtt_next_mid();
  499. PUSH16(p, mid);
  500. PUSH16(p, topic_len);
  501. PUSH_N(p, topic, topic_len);
  502. // send head + mid + topic
  503. int nwrite = hio_write(cli->io, buf, p - buf);
  504. HV_STACK_FREE(buf);
  505. return nwrite < 0 ? nwrite : mid;
  506. }