Commit 2e1d7779 authored by Mickaël Turnel's avatar Mickaël Turnel

Add TTL information in mDNS

parent dec86577
......@@ -49,11 +49,12 @@ BELLESIP_EXPORT int belle_sip_mdns_register_available(void);
* @param port the port of the service.
* @param priority the priority of the service, lower value means more preferred.
* @param weight a relative weight for services within the same local domain that have the same priority, higher value means more preferred.
* @param ttl the time to live of the resulted srv
* @param cb a callback function that will be called to notify the results.
* @param data a user pointer passed through the callback as first argument.
* @return a #belle_sip_register_t that can be used to cancel the registration if needed. The context must have been ref'd with belle_sip_object_ref().
**/
BELLESIP_EXPORT belle_sip_mdns_register_t *belle_sip_mdns_register(const char *service, const char *transport, const char *domain, const char* name, int port, int prio, int weight, belle_sip_mdns_register_callback_t cb, void *data);
BELLESIP_EXPORT belle_sip_mdns_register_t *belle_sip_mdns_register(const char *service, const char *transport, const char *domain, const char* name, int port, int prio, int weight, int ttl, belle_sip_mdns_register_callback_t cb, void *data);
/**
* Cancels the mdns registration.
......
......@@ -129,7 +129,7 @@ int belle_sip_mdns_register_available(void) {
#endif
}
belle_sip_mdns_register_t *belle_sip_mdns_register(const char *service, const char *transport, const char *domain, const char *name, int port, int prio, int weight, belle_sip_mdns_register_callback_t cb, void *data) {
belle_sip_mdns_register_t *belle_sip_mdns_register(const char *service, const char *transport, const char *domain, const char *name, int port, int prio, int weight, int ttl, belle_sip_mdns_register_callback_t cb, void *data) {
#ifdef HAVE_MDNS
belle_sip_mdns_register_t *reg = belle_sip_mdns_register_create(cb, data);
DNSServiceErrorType error;
......@@ -146,6 +146,9 @@ belle_sip_mdns_register_t *belle_sip_mdns_register(const char *service, const ch
n = snprintf(number, sizeof(number), "%d", weight);
TXTRecordSetValue(&txt_ref, "weight\0", n, number);
n = snprintf(number, sizeof(number), "%d", ttl);
TXTRecordSetValue(&txt_ref, "ttl\0", n, number);
prefix = srv_prefix_from_service_and_transport(service, transport);
error = DNSServiceRegister(&reg->service_ref
......
......@@ -785,43 +785,50 @@ static void resolver_process_mdns_resolve(DNSServiceRef service_ref
if (error_code != kDNSServiceErr_NoError) {
belle_sip_error("%s error while resolving [%s]: code %d", __FUNCTION__, source->ctx->name, error_code);
} else {
uint8_t prio_size, weight_size;
uint8_t prio_size, weight_size, ttl_size;
const char *prio_buf = TXTRecordGetValuePtr(txt_len, txt_record, "prio", &prio_size);
const char *weight_buf = TXTRecordGetValuePtr(txt_len, txt_record, "weight", &weight_size);
const char *ttl_buf = TXTRecordGetValuePtr(txt_len, txt_record, "weight", &ttl_size);
/* If the buffer is non-NULL then the key exist and if the result size is > 0 then the value is not empty */
if (prio_buf && prio_size > 0 && weight_buf && weight_size > 0) {
short unsigned int prio, weight;
if (prio_buf && prio_size > 0 && weight_buf && weight_size > 0 && ttl_buf && ttl_size > 0) {
short unsigned int prio, weight, ttl;
/* Don't use the VLAs since it doesn't work on Windows */
char *prio_value = belle_sip_malloc(prio_size + 1);
char *weight_value = belle_sip_malloc(weight_size + 1);
char *ttl_value = belle_sip_malloc(ttl_size + 1);
memcpy(prio_value, prio_buf, prio_size);
memcpy(weight_value, weight_buf, weight_size);
memcpy(ttl_value, ttl_buf, ttl_size);
prio_value[prio_size] = '\0';
weight_value[weight_size] = '\0';
ttl_value[ttl_size] = '\0';
prio = atoi(prio_value);
weight = atoi(weight_value);
ttl = atoi(ttl_value);
belle_sip_dns_srv_t *b_srv = belle_sip_dns_srv_create_raw(prio, weight, port, hosttarget);
if (!belle_sip_list_find_custom(source->ctx->srv_list, srv_compare_host_and_port, b_srv)) {
source->ctx->srv_list = belle_sip_list_insert_sorted(source->ctx->srv_list, belle_sip_object_ref(b_srv), srv_compare_prio);
if (ttl < BELLE_SIP_RESOLVER_CONTEXT(source->ctx)->min_ttl) BELLE_SIP_RESOLVER_CONTEXT(source->ctx)->min_ttl = ttl;
} else {
belle_sip_object_unref(b_srv);
}
belle_sip_free(prio_value);
belle_sip_free(weight_value);
belle_sip_free(ttl_value);
belle_sip_message("mDNS %s resolved to [target:%s port:%d prio:%d weight:%d]", source->ctx->name, hosttarget, port, prio, weight);
belle_sip_message("mDNS %s resolved to [target:%s port:%d prio:%d weight:%d ttl:%d]", source->ctx->name, hosttarget, port, prio, weight, ttl);
source->resolve_finished = BELLE_SIP_STOP;
} else {
belle_sip_warning("%s TXT record of %s does not contain a priority or weight key!", __FUNCTION__, hosttarget);
belle_sip_warning("%s TXT record of %s does not contain a priority, weight or ttl key!", __FUNCTION__, hosttarget);
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment