#include <dns_sd.h>
#include "dns-sd-util.h"
#include "dns-sd.h"
+#include "dns-sd-internal.h"
#define GET_REG_DATA_P(a) ((dnssd_register_data_s *)((a)->data))
#define GET_BROWSE_DATA_P(a) ((dnssd_browse_data_s *)((a)->data))
{
__DNSSD_LOG_FUNC_ENTER__;
dnssd_handle_s *local_handle;
+ dnssd_register_data_s *reg = NULL;
unsigned int handler;
CHECK_FEATURE_SUPPORTED(NETWORK_SERVICE_DISCOVERY_FEATURE);
g_strlcpy(local_handle->domain, "", sizeof(local_handle->domain));
local_handle->flags = 0;
local_handle->watch_id = 0;
- local_handle->if_index = kDNSServiceInterfaceIndexAny;
+
+ reg = GET_REG_DATA_P(local_handle);
+ reg->if_index = kDNSServiceInterfaceIndexAny;
DNSSD_LOGD("New handle created [%p]->[%u] type %s", local_handle,
*dnssd_service, local_handle->service_type);
return DNSSD_ERROR_NONE;
}
+int dnssd_service_set_interface(dnssd_service_h local_service, const char *interface)
+{
+ __DNSSD_LOG_FUNC_ENTER__;
+ dnssd_handle_s *local_handle = NULL;
+ dnssd_register_data_s *reg = NULL;
+ unsigned int if_index = 0;
+
+ CHECK_FEATURE_SUPPORTED(NETWORK_SERVICE_DISCOVERY_FEATURE);
+
+ if (__dnssd_is_init() == false) {
+ DNSSD_LOGE("Not initialized"); //LCOV_EXCL_LINE
+ __DNSSD_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
+ return DNSSD_ERROR_NOT_INITIALIZED; //LCOV_EXCL_LINE
+ }
+
+ if ((if_index = if_nametoindex(interface)) == 0) {
+ DNSSD_LOGE("Invalid interface name");
+ __DNSSD_LOG_FUNC_EXIT__;
+ return DNSSD_ERROR_INVALID_PARAMETER;
+ }
+ DNSSD_LOGD("Interface index: %u", if_index);
+
+ local_handle = __dnssd_check_handle_validity(local_service);
+ if (local_handle == NULL) {
+ DNSSD_LOGD("Service Handler not found");
+ __DNSSD_LOG_FUNC_EXIT__;
+ return DNSSD_ERROR_SERVICE_NOT_FOUND;
+ }
+
+ if (local_handle->op_type != DNSSD_TYPE_REGISTER) {
+ DNSSD_LOGD("DNSSD service is not a local service"); //LCOV_EXCL_LINE
+ __DNSSD_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
+ return DNSSD_ERROR_SERVICE_NOT_FOUND; //LCOV_EXCL_LINE
+ }
+
+ reg = GET_REG_DATA_P(local_handle);
+ reg->if_index = if_index;
+
+ DNSSD_LOGD("Successfully set interface %s with index %u", interface, if_index);
+
+ __DNSSD_LOG_FUNC_EXIT__;
+ return DNSSD_ERROR_NONE;
+}
+
int dnssd_service_add_txt_record(dnssd_service_h local_service,
const char *key, unsigned short length, const void *value)
{
g_strlcpy(dnssd_handle->domain, domain,
sizeof(dnssd_handle->domain));
dnssd_handle->flags = flags;
- DNSSD_LOGD("Domain/Interface/Flags : %s/%d/0x%x",
+ DNSSD_LOGD("Domain/Interface/Flags : %s/0x%x",
dnssd_handle->domain,
- dnssd_handle->if_index,
dnssd_handle->flags);
reg = GET_REG_DATA_P(dnssd_handle);
DNSSD_LOGD("Service Name is %s", reg->service_name);
DNSSD_LOGD("Domain/Interface/Flags : %s/%d/0x%x", local_handle->domain,
- local_handle->if_index, local_handle->flags);
+ reg->if_index, local_handle->flags);
ret = DNSServiceRegister(sd_ref, local_handle->flags,
- local_handle->if_index, reg->service_name,
+ reg->if_index, reg->service_name,
local_handle->service_type, local_handle->domain,
NULL, reg->port, 1, "", __dnssd_register_reply_cb,
NULL);
local_handle = (dnssd_handle_s *)user_data;
found = GET_FOUND_DATA_P(local_handle);
/* Update interface and flags for any future use */
- local_handle->if_index = if_index;
+ found->if_index = if_index;
local_handle->flags = flags;
DNSSD_LOGD("Domain/Interface/Flags : %s/%d/0x%x",
local_handle->domain,
- local_handle->if_index,
+ found->if_index,
local_handle->flags);
dnssd_handle = __dnssd_check_handle_validity(found->browse_handler);
local_handle->op_type = DNSSD_TYPE_FOUND;
local_handle->service_handler = (uintptr_t)local_handle & 0xffffffff;
g_strlcpy(local_handle->domain, domain, sizeof(local_handle->domain));
- local_handle->if_index = if_index;
+ found->if_index = if_index;
local_handle->flags = flags;
local_handle->watch_id = 0;
if (dnssd_handle->service_type)
//LCOV_EXCL_STOP
//LCOV_EXCL_START
-static void __dnssd_broswe_reply_cb(DNSServiceRef sd_ref, unsigned int flags,
+static void __dnssd_browse_reply_cb(DNSServiceRef sd_ref, unsigned int flags,
unsigned int if_index, DNSServiceErrorType error_code,
const char *service_name, const char *service_type,
const char *domain, void *user_data)
g_strlcpy(dnssd_handle->domain, domain,
sizeof(dnssd_handle->domain));
dnssd_handle->flags = flags;
- dnssd_handle->if_index = if_index;
- DNSSD_LOGD("Domain/Interface/Flags : %s/%d/0x%x",
+ DNSSD_LOGD("Domain/Interface/Flags : %s/:x%x",
dnssd_handle->domain,
- dnssd_handle->if_index,
dnssd_handle->flags);
if (flags & kDNSServiceFlagsAdd) {
g_strlcpy(local_handle->domain, "", sizeof(local_handle->domain));
local_handle->flags = 0;
local_handle->watch_id = 0;
- local_handle->if_index = kDNSServiceInterfaceIndexAny;
browse = GET_BROWSE_DATA_P(local_handle);
sd_ref = &(local_handle->sd_ref);
*dnssd_service, local_handle->service_type);
ret = DNSServiceBrowse(sd_ref, local_handle->flags,
- local_handle->if_index, local_handle->service_type,
- local_handle->domain, __dnssd_broswe_reply_cb,
+ kDNSServiceInterfaceIndexAny, local_handle->service_type,
+ local_handle->domain, __dnssd_browse_reply_cb,
+ NULL);
+ if (ret < 0) {
+ DNSSD_LOGE("Failed to browse for dns service, error[%s]", //LCOV_EXCL_LINE
+ dnssd_error_to_string(ret)); //LCOV_EXCL_LINE
+ g_free(local_handle->service_type); //LCOV_EXCL_LINE
+ g_free(local_handle); //LCOV_EXCL_LINE
+ __DNSSD_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
+ return ret; //LCOV_EXCL_LINE
+ }
+
+ browse->callback = found_cb;
+ browse->user_data = user_data;
+ dnssd_handle_list = g_slist_prepend(dnssd_handle_list, local_handle);
+ __dnssd_handle_events(&(local_handle->watch_id), sd_ref);
+
+ DNSSD_LOGD("Succeeded to browse for dns service");
+ __DNSSD_LOG_FUNC_EXIT__;
+ return DNSSD_ERROR_NONE;
+}
+
+int dnssd_start_browsing_service_on_interface(const char *service_type, const char *interface,
+ dnssd_browser_h *dnssd_service, dnssd_found_cb found_cb,
+ void *user_data)
+{
+ __DNSSD_LOG_FUNC_ENTER__;
+ int ret;
+ dnssd_handle_s *local_handle;
+ dnssd_browse_data_s *browse;
+ DNSServiceRef *sd_ref;
+ unsigned int handler;
+ unsigned int if_index;
+
+ CHECK_FEATURE_SUPPORTED(NETWORK_SERVICE_DISCOVERY_FEATURE);
+
+ if (__dnssd_is_init() == false) {
+ DNSSD_LOGE("Not initialized"); //LCOV_EXCL_LINE
+ __DNSSD_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
+ return DNSSD_ERROR_NOT_INITIALIZED; //LCOV_EXCL_LINE
+ }
+
+ if (dnssd_service == NULL || service_type == NULL ||
+ __dnssd_check_handle_validity(*dnssd_service) != NULL) {
+ DNSSD_LOGE("Invalid Parameter");
+ __DNSSD_LOG_FUNC_EXIT__;
+ return DNSSD_ERROR_INVALID_PARAMETER;
+ }
+
+ if (found_cb == NULL) {
+ DNSSD_LOGE("Callback Not Provided");
+ __DNSSD_LOG_FUNC_EXIT__;
+ return DNSSD_ERROR_INVALID_PARAMETER;
+ }
+
+ local_handle = (dnssd_handle_s *)g_try_malloc0(BROWSE_SIZE);
+ if (local_handle == NULL) {
+ DNSSD_LOGE("Failed to Allocate Memory"); //LCOV_EXCL_LINE
+ __DNSSD_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
+ return DNSSD_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
+ }
+
+ if ((if_index = if_nametoindex(interface)) == 0) {
+ DNSSD_LOGE("Invalid interface name");
+ __DNSSD_LOG_FUNC_EXIT__;
+ return DNSSD_ERROR_INVALID_PARAMETER;
+ }
+ DNSSD_LOGD("Interface index: %u", if_index);
+
+ handler = (uintptr_t)local_handle & 0xffffffff;
+ *dnssd_service = handler;
+ local_handle->service_handler = handler;
+ local_handle->op_type = DNSSD_TYPE_BROWSE;
+ local_handle->service_type = g_strdup(service_type);
+ g_strlcpy(local_handle->domain, "", sizeof(local_handle->domain));
+ local_handle->flags = 0;
+ local_handle->watch_id = 0;
+ browse = GET_BROWSE_DATA_P(local_handle);
+ sd_ref = &(local_handle->sd_ref);
+
+ DNSSD_LOGD("New browse handle created [%p]->[%u] type %s", local_handle,
+ *dnssd_service, local_handle->service_type);
+
+ ret = DNSServiceBrowse(sd_ref, local_handle->flags,
+ if_index, local_handle->service_type,
+ local_handle->domain, __dnssd_browse_reply_cb,
NULL);
if (ret < 0) {
DNSSD_LOGE("Failed to browse for dns service, error[%s]", //LCOV_EXCL_LINE