mosquitto/lib/srv_mosq.c

123 lines
3.0 KiB
C
Raw Normal View History

2014-05-07 22:27:00 +00:00
/*
Copyright (c) 2013-2020 Roger Light <roger@atchoo.org>
2014-05-07 22:27:00 +00:00
All rights reserved. This program and the accompanying materials
2020-11-25 17:34:21 +00:00
are made available under the terms of the Eclipse Public License 2.0
2014-05-07 22:27:00 +00:00
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
2020-11-25 17:34:21 +00:00
https://www.eclipse.org/legal/epl-2.0/
2014-05-07 22:27:00 +00:00
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
2020-12-01 18:21:59 +00:00
2014-05-07 22:27:00 +00:00
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
2014-05-07 22:27:00 +00:00
#ifdef WITH_SRV
# include <ares.h>
# include <arpa/nameser.h>
# include <stdio.h>
# include <string.h>
#endif
#include "logging_mosq.h"
#include "memory_mosq.h"
#include "mosquitto_internal.h"
#include "mosquitto.h"
2019-09-25 11:17:17 +00:00
#include "util_mosq.h"
2014-05-07 22:27:00 +00:00
#ifdef WITH_SRV
static void srv_callback(void *arg, int status, int timeouts, unsigned char *abuf, int alen)
{
struct mosquitto *mosq = arg;
struct ares_srv_reply *reply = NULL;
2021-08-26 09:54:03 +00:00
UNUSED(timeouts);
2014-05-07 22:27:00 +00:00
if(status == ARES_SUCCESS){
status = ares_parse_srv_reply(abuf, alen, &reply);
if(status == ARES_SUCCESS){
// FIXME - choose which answer to use based on rfc2782 page 3. */
mosquitto_connect(mosq, reply->host, reply->port, mosq->keepalive);
}
}else{
2015-05-18 07:53:21 +00:00
log__printf(mosq, MOSQ_LOG_ERR, "Error: SRV lookup failed (%d).", status);
2014-05-20 09:22:06 +00:00
/* FIXME - calling on_disconnect here isn't correct. */
pthread_mutex_lock(&mosq->callback_mutex);
if(mosq->on_disconnect){
mosq->in_callback = true;
mosq->on_disconnect(mosq, mosq->userdata, MOSQ_ERR_LOOKUP);
2014-05-20 09:22:06 +00:00
mosq->in_callback = false;
}
2018-11-20 14:36:18 +00:00
if(mosq->on_disconnect_v5){
mosq->in_callback = true;
mosq->on_disconnect_v5(mosq, mosq->userdata, MOSQ_ERR_LOOKUP, NULL);
mosq->in_callback = false;
}
2014-05-20 09:22:06 +00:00
pthread_mutex_unlock(&mosq->callback_mutex);
2014-05-07 22:27:00 +00:00
}
}
#endif
int mosquitto_connect_srv(struct mosquitto *mosq, const char *host, int keepalive, const char *bind_address)
{
#ifdef WITH_SRV
char *h;
int rc;
if(!mosq) return MOSQ_ERR_INVAL;
2021-08-26 09:54:03 +00:00
UNUSED(bind_address);
if(keepalive < 0 || keepalive > UINT16_MAX){
return MOSQ_ERR_INVAL;
}
2014-05-07 22:27:00 +00:00
rc = ares_init(&mosq->achan);
if(rc != ARES_SUCCESS){
return MOSQ_ERR_UNKNOWN;
}
if(!host){
// get local domain
}else{
#ifdef WITH_TLS
if(mosq->tls_cafile || mosq->tls_capath || mosq->tls_psk){
h = mosquitto__malloc(strlen(host) + strlen("_secure-mqtt._tcp.") + 1);
2014-05-07 22:27:00 +00:00
if(!h) return MOSQ_ERR_NOMEM;
sprintf(h, "_secure-mqtt._tcp.%s", host);
}else{
#endif
h = mosquitto__malloc(strlen(host) + strlen("_mqtt._tcp.") + 1);
2014-05-07 22:27:00 +00:00
if(!h) return MOSQ_ERR_NOMEM;
sprintf(h, "_mqtt._tcp.%s", host);
#ifdef WITH_TLS
}
#endif
ares_search(mosq->achan, h, ns_c_in, ns_t_srv, srv_callback, mosq);
mosquitto__free(h);
2014-05-07 22:27:00 +00:00
}
2019-09-25 11:17:17 +00:00
mosquitto__set_state(mosq, mosq_cs_connect_srv);
2014-05-07 22:27:00 +00:00
mosq->keepalive = (uint16_t)keepalive;
2014-05-07 22:27:00 +00:00
return MOSQ_ERR_SUCCESS;
#else
2019-03-13 14:11:50 +00:00
UNUSED(mosq);
UNUSED(host);
UNUSED(keepalive);
UNUSED(bind_address);
2014-05-07 22:27:00 +00:00
return MOSQ_ERR_NOT_SUPPORTED;
#endif
}