srv_mosq.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Copyright (c) 2013-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. #ifdef WITH_SRV
  16. # include <ares.h>
  17. # include <arpa/nameser.h>
  18. # include <stdio.h>
  19. # include <string.h>
  20. #endif
  21. #include "logging_mosq.h"
  22. #include "memory_mosq.h"
  23. #include "mosquitto_internal.h"
  24. #include "mosquitto.h"
  25. #include "util_mosq.h"
  26. #ifdef WITH_SRV
  27. static void srv_callback(void *arg, int status, int timeouts, unsigned char *abuf, int alen)
  28. {
  29. struct mosquitto *mosq = arg;
  30. struct ares_srv_reply *reply = NULL;
  31. UNUSED(timeouts);
  32. if(status == ARES_SUCCESS){
  33. status = ares_parse_srv_reply(abuf, alen, &reply);
  34. if(status == ARES_SUCCESS){
  35. // FIXME - choose which answer to use based on rfc2782 page 3. */
  36. mosquitto_connect(mosq, reply->host, reply->port, mosq->keepalive);
  37. }
  38. }else{
  39. log__printf(mosq, MOSQ_LOG_ERR, "Error: SRV lookup failed (%d).", status);
  40. /* FIXME - calling on_disconnect here isn't correct. */
  41. pthread_mutex_lock(&mosq->callback_mutex);
  42. if(mosq->on_disconnect){
  43. mosq->in_callback = true;
  44. mosq->on_disconnect(mosq, mosq->userdata, MOSQ_ERR_LOOKUP);
  45. mosq->in_callback = false;
  46. }
  47. if(mosq->on_disconnect_v5){
  48. mosq->in_callback = true;
  49. mosq->on_disconnect_v5(mosq, mosq->userdata, MOSQ_ERR_LOOKUP, NULL);
  50. mosq->in_callback = false;
  51. }
  52. pthread_mutex_unlock(&mosq->callback_mutex);
  53. }
  54. }
  55. #endif
  56. int mosquitto_connect_srv(struct mosquitto *mosq, const char *host, int keepalive, const char *bind_address)
  57. {
  58. #ifdef WITH_SRV
  59. char *h;
  60. int rc;
  61. if(!mosq) return MOSQ_ERR_INVAL;
  62. UNUSED(bind_address);
  63. if(keepalive < 0 || keepalive > UINT16_MAX){
  64. return MOSQ_ERR_INVAL;
  65. }
  66. rc = ares_init(&mosq->achan);
  67. if(rc != ARES_SUCCESS){
  68. return MOSQ_ERR_UNKNOWN;
  69. }
  70. if(!host){
  71. // get local domain
  72. }else{
  73. #ifdef WITH_TLS
  74. if(mosq->tls_cafile || mosq->tls_capath || mosq->tls_psk){
  75. h = mosquitto__malloc(strlen(host) + strlen("_secure-mqtt._tcp.") + 1);
  76. if(!h) return MOSQ_ERR_NOMEM;
  77. sprintf(h, "_secure-mqtt._tcp.%s", host);
  78. }else{
  79. #endif
  80. h = mosquitto__malloc(strlen(host) + strlen("_mqtt._tcp.") + 1);
  81. if(!h) return MOSQ_ERR_NOMEM;
  82. sprintf(h, "_mqtt._tcp.%s", host);
  83. #ifdef WITH_TLS
  84. }
  85. #endif
  86. ares_search(mosq->achan, h, ns_c_in, ns_t_srv, srv_callback, mosq);
  87. mosquitto__free(h);
  88. }
  89. mosquitto__set_state(mosq, mosq_cs_connect_srv);
  90. mosq->keepalive = (uint16_t)keepalive;
  91. return MOSQ_ERR_SUCCESS;
  92. #else
  93. UNUSED(mosq);
  94. UNUSED(host);
  95. UNUSED(keepalive);
  96. UNUSED(bind_address);
  97. return MOSQ_ERR_NOT_SUPPORTED;
  98. #endif
  99. }