network.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2019-12-09 21:30:54
  5. * @LastEditTime: 2020-06-05 17:17:48
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #include "platform_timer.h"
  9. #include "platform_memory.h"
  10. #include "nettype_tcp.h"
  11. #include "string.h"
  12. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  13. #include "nettype_tls.h"
  14. #endif
  15. int network_read(network_t *n, unsigned char *buf, int len, int timeout)
  16. {
  17. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  18. if (n->channel)
  19. return nettype_tls_read(n, buf, len, timeout);
  20. #endif
  21. return nettype_tcp_read(n, buf, len, timeout);
  22. }
  23. int network_write(network_t *n, unsigned char *buf, int len, int timeout)
  24. {
  25. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  26. if (n->channel)
  27. return nettype_tls_write(n, buf, len, timeout);
  28. #endif
  29. return nettype_tcp_write(n, buf, len, timeout);
  30. }
  31. int network_connect(network_t *n)
  32. {
  33. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  34. if (n->channel)
  35. return nettype_tls_connect(n);
  36. #endif
  37. return nettype_tcp_connect(n);
  38. }
  39. void network_disconnect(network_t *n)
  40. {
  41. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  42. if (n->channel)
  43. nettype_tls_disconnect(n);
  44. else
  45. #endif
  46. nettype_tcp_disconnect(n);
  47. }
  48. int network_init(network_t *n, const char *host, const char *port, const char *ca)
  49. {
  50. if (NULL == n)
  51. RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
  52. n->socket = -1;
  53. n->host = host;
  54. n->port = port;
  55. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  56. n->channel = 0;
  57. if (NULL != ca) {
  58. network_set_ca(n, ca);
  59. }
  60. #endif
  61. RETURN_ERROR(MQTT_SUCCESS_ERROR);
  62. }
  63. void network_release(network_t* n)
  64. {
  65. if (n->socket >= 0)
  66. {
  67. network_disconnect(n);
  68. memset(n, 0, sizeof(network_t));
  69. }
  70. }
  71. void network_set_channel(network_t *n, int channel)
  72. {
  73. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  74. n->channel = channel;
  75. #endif
  76. }
  77. int network_set_ca(network_t *n, const char *ca)
  78. {
  79. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  80. if ((NULL == n) || (NULL == ca))
  81. RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
  82. n->ca_crt = ca;
  83. n->ca_crt_len = strlen(ca);
  84. n->channel = NETWORK_CHANNEL_TLS;
  85. n->timeout_ms = MQTT_TLS_HANDSHAKE_TIMEOUT;
  86. #endif
  87. RETURN_ERROR(MQTT_SUCCESS_ERROR);
  88. }
  89. int network_set_host_port(network_t* n, char *host, char *port)
  90. {
  91. if (!(n && host && port))
  92. RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
  93. n->host = host;
  94. n->port = port;
  95. RETURN_ERROR(MQTT_SUCCESS_ERROR);
  96. }