2 Copyright (c) 2013 Roger Light <roger@atchoo.org>
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. Neither the name of mosquitto nor the names of its
14 contributors may be used to endorse or promote products derived from
15 this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
33 # include <arpa/nameser.h>
38 #include "logging_mosq.h"
39 #include "memory_mosq.h"
40 #include "mosquitto_internal.h"
41 #include "mosquitto.h"
44 static void srv_callback(void *arg, int status, int timeouts, unsigned char *abuf, int alen)
46 struct mosquitto *mosq = arg;
47 struct ares_srv_reply *reply = NULL;
48 if(status == ARES_SUCCESS){
49 status = ares_parse_srv_reply(abuf, alen, &reply);
50 if(status == ARES_SUCCESS){
51 // FIXME - choose which answer to use based on rfc2782 page 3. */
52 mosquitto_connect(mosq, reply->host, reply->port, mosq->keepalive);
55 _mosquitto_log_printf(mosq, MOSQ_LOG_ERR, "Error: SRV lookup failed (%d).", status);
61 int mosquitto_connect_srv(struct mosquitto *mosq, const char *host, int keepalive, const char *bind_address)
66 if(!mosq) return MOSQ_ERR_INVAL;
68 rc = ares_init(&mosq->achan);
69 if(rc != ARES_SUCCESS){
70 return MOSQ_ERR_UNKNOWN;
77 if(mosq->tls_cafile || mosq->tls_capath || mosq->tls_psk){
78 h = _mosquitto_malloc(strlen(host) + strlen("_secure-mqtt._tcp.") + 1);
79 if(!h) return MOSQ_ERR_NOMEM;
80 sprintf(h, "_secure-mqtt._tcp.%s", host);
83 h = _mosquitto_malloc(strlen(host) + strlen("_mqtt._tcp.") + 1);
84 if(!h) return MOSQ_ERR_NOMEM;
85 sprintf(h, "_mqtt._tcp.%s", host);
89 ares_search(mosq->achan, h, ns_c_in, ns_t_srv, srv_callback, mosq);
93 pthread_mutex_lock(&mosq->state_mutex);
94 mosq->state = mosq_cs_connect_srv;
95 pthread_mutex_unlock(&mosq->state_mutex);
97 mosq->keepalive = keepalive;
99 return MOSQ_ERR_SUCCESS;
102 return MOSQ_ERR_NOT_SUPPORTED;