PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/bridge.c

https://bitbucket.org/oojah/mosquitto/
C | 173 lines | 123 code | 21 blank | 29 comment | 25 complexity | 593a8404fe04002713be488c6eb1d282 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. Copyright (c) 2009-2012 Roger Light <roger@atchoo.org>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. 3. Neither the name of mosquitto nor the names of its
  12. contributors may be used to endorse or promote products derived from
  13. this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <assert.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <config.h>
  30. #include <mosquitto.h>
  31. #include <mosquitto_internal.h>
  32. #include <net_mosq.h>
  33. #include <mqtt3.h>
  34. #include <memory_mosq.h>
  35. #include <send_mosq.h>
  36. #include <util_mosq.h>
  37. #include <will_mosq.h>
  38. #ifdef WITH_BRIDGE
  39. int mqtt3_bridge_new(mosquitto_db *db, struct _mqtt3_bridge *bridge)
  40. {
  41. int i;
  42. struct mosquitto *new_context = NULL;
  43. struct mosquitto **tmp_contexts;
  44. char hostname[256];
  45. int len;
  46. assert(db);
  47. assert(bridge);
  48. new_context = mqtt3_context_init(-1);
  49. if(!new_context){
  50. return MOSQ_ERR_NOMEM;
  51. }
  52. new_context->bridge = bridge;
  53. for(i=0; i<db->context_count; i++){
  54. if(db->contexts[i] == NULL){
  55. db->contexts[i] = new_context;
  56. break;
  57. }
  58. }
  59. if(i==db->context_count){
  60. db->context_count++;
  61. tmp_contexts = _mosquitto_realloc(db->contexts, sizeof(struct mosquitto*)*db->context_count);
  62. if(tmp_contexts){
  63. db->contexts = tmp_contexts;
  64. db->contexts[db->context_count-1] = new_context;
  65. }else{
  66. _mosquitto_free(new_context);
  67. return MOSQ_ERR_NOMEM;
  68. }
  69. }
  70. /* FIXME - need to check that this name isn't already in use. */
  71. if(bridge->clientid){
  72. new_context->id = _mosquitto_strdup(bridge->clientid);
  73. }else{
  74. if(!gethostname(hostname, 256)){
  75. len = strlen(hostname) + strlen(bridge->name) + 2;
  76. new_context->id = _mosquitto_malloc(len);
  77. if(!new_context->id){
  78. return MOSQ_ERR_NOMEM;
  79. }
  80. snprintf(new_context->id, len, "%s.%s", hostname, bridge->name);
  81. }else{
  82. return 1;
  83. }
  84. }
  85. if(!new_context->id){
  86. _mosquitto_free(new_context);
  87. return MOSQ_ERR_NOMEM;
  88. }
  89. new_context->username = new_context->bridge->username;
  90. new_context->password = new_context->bridge->password;
  91. return mqtt3_bridge_connect(db, new_context);
  92. }
  93. int mqtt3_bridge_connect(mosquitto_db *db, struct mosquitto *context)
  94. {
  95. int rc;
  96. int i;
  97. char *notification_topic;
  98. int notification_topic_len;
  99. uint8_t notification_payload[2];
  100. if(!context || !context->bridge) return MOSQ_ERR_INVAL;
  101. context->state = mosq_cs_new;
  102. context->sock = -1;
  103. context->last_msg_in = time(NULL);
  104. context->last_msg_out = time(NULL);
  105. context->keepalive = context->bridge->keepalive;
  106. context->clean_session = context->bridge->clean_session;
  107. context->in_packet.payload = NULL;
  108. mqtt3_bridge_packet_cleanup(context);
  109. for(i=0; i<context->bridge->topic_count; i++){
  110. if(context->bridge->topics[i].direction == bd_out || context->bridge->topics[i].direction == bd_both){
  111. if(mqtt3_sub_add(context, context->bridge->topics[i].topic, context->bridge->topics[i].qos, &db->subs)) return 1;
  112. }
  113. }
  114. _mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Connecting bridge %s", context->bridge->name);
  115. rc = _mosquitto_socket_connect(context, context->bridge->address, context->bridge->port);
  116. if(rc != MOSQ_ERR_SUCCESS){
  117. _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error creating bridge.");
  118. return rc;
  119. }
  120. if(context->bridge->notifications){
  121. notification_topic_len = strlen(context->id)+strlen("$SYS/broker/connection//state");
  122. notification_topic = _mosquitto_malloc(sizeof(char)*(notification_topic_len+1));
  123. if(!notification_topic) return MOSQ_ERR_NOMEM;
  124. snprintf(notification_topic, notification_topic_len+1, "$SYS/broker/connection/%s/state", context->id);
  125. notification_payload[0] = '0';
  126. notification_payload[1] = '\0';
  127. mqtt3_db_messages_easy_queue(db, context, notification_topic, 1, 2, (uint8_t *)&notification_payload, 1);
  128. rc = _mosquitto_will_set(context, true, notification_topic, 2, (uint8_t *)&notification_payload, 1, true);
  129. if(rc != MOSQ_ERR_SUCCESS){
  130. _mosquitto_free(notification_topic);
  131. return rc;
  132. }
  133. _mosquitto_free(notification_topic);
  134. }
  135. return _mosquitto_send_connect(context, context->keepalive, context->clean_session);
  136. }
  137. void mqtt3_bridge_packet_cleanup(struct mosquitto *context)
  138. {
  139. struct _mosquitto_packet *packet;
  140. if(!context) return;
  141. while(context->out_packet){
  142. _mosquitto_packet_cleanup(context->out_packet);
  143. packet = context->out_packet;
  144. context->out_packet = context->out_packet->next;
  145. _mosquitto_free(packet);
  146. }
  147. _mosquitto_packet_cleanup(&(context->in_packet));
  148. }
  149. #endif