pnoltes commented on code in PR #495:
URL: https://github.com/apache/celix/pull/495#discussion_r1131444980


##########
bundles/remote_services/rsa_spi/include/remote_constants.h:
##########
@@ -36,4 +36,6 @@ static const char * const OSGI_RSA_SERVICE_IMPORTED_CONFIGS = 
"service.imported.
 static const char * const OSGI_RSA_SERVICE_EXPORTED_CONFIGS = 
"service.exported.configs";
 static const char * const OSGI_RSA_SERVICE_LOCATION = "service.location";
 
+static const char * const RSA_DISCOVERY_ZEROCONF_SERVICE_ANNOUNCED_IF_INDEX = 
"RSA_DZC_IF_INDEX";

Review Comment:
   This is a ZEROCONF specific option and should be part of the 
disocvey_zeroconf subdir. 
   
   nitpick: I would also like to see some documentation for this option. What 
does it do?



##########
bundles/remote_services/discovery_zeroconf/src/discovery_zeroconf_activator.c:
##########
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <discovery_zeroconf_announcer.h>
+#include <discovery_zeroconf_watcher.h>
+#include <celix_log_helper.h>
+#include <celix_bundle_activator.h>
+#include <celix_types.h>
+#include <celix_errno.h>
+#include <assert.h>
+
+typedef struct discovery_zeroconf_activator {
+    celix_log_helper_t *logHelper;
+    discovery_zeroconf_announcer_t *announcer;
+    discovery_zeroconf_watcher_t  *watcher;
+}discovery_zeroconf_activator_t;
+
+celix_status_t discoveryZeroconfActivator_start(discovery_zeroconf_activator_t 
*act, celix_bundle_context_t *ctx) {
+    celix_status_t status = CELIX_SUCCESS;
+    act->logHelper = 
celix_logHelper_create(ctx,"celix_rsa_zeroconf_discovery");
+    assert(act->logHelper != NULL);

Review Comment:
   I think we only should use assert for precondition on function.
   
   In this case celix_logHelper_create can return NULL (ENOMEM) en this should 
be handled.



##########
bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.c:
##########
@@ -807,6 +817,32 @@ static celix_status_t 
remoteServiceAdmin_getIpAddress(char* interface, char** ip
     return status;
 }
 
+static uint32_t remoteServiceAdmin_getIfIndex(const char *ip) {
+    uint32_t ifIndex = 0;
+
+    struct ifaddrs *ifaddr, *ifa;
+    char host[NI_MAXHOST];
+
+    if (ip != NULL && getifaddrs(&ifaddr) != -1)
+    {
+        for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
+        {
+            if (ifa->ifa_addr == NULL)

Review Comment:
   nitpick: Although we do not yet have a explicit style, I prefer always to 
use curly braces



##########
bundles/remote_services/discovery_zeroconf/src/discovery_zeroconf_constants.h:
##########
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#ifndef CELIX_DISCOVERY_ZEROCONF_CONSTANTS_H
+#define CELIX_DISCOVERY_ZEROCONF_CONSTANTS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#include <dns_sd.h>
+
+#define DZC_SERVICE_ANNOUNCED_IF_INDEX_DEFAULT 
kDNSServiceInterfaceIndexLocalOnly

Review Comment:
   For constants I would prefer some doxygen info



##########
bundles/remote_services/discovery_zeroconf/src/discovery_zeroconf_announcer.c:
##########
@@ -0,0 +1,533 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <discovery_zeroconf_announcer.h>
+#include <discovery_zeroconf_constants.h>
+#include <dns_sd.h>
+#include <endpoint_listener.h>
+#include <remote_constants.h>
+#include <celix_utils.h>
+#include <celix_properties.h>
+#include <celix_constants.h>
+#include <celix_threads.h>
+#include <celix_bundle_context.h>
+#include <celix_string_hash_map.h>
+#include <celix_array_list.h>
+#include <celix_log_helper.h>
+#include <celix_types.h>
+#include <celix_errno.h>
+#include <celix_build_assert.h>
+#include <netinet/in.h>
+#include <net/if.h>
+#include <sys/eventfd.h>
+#include <sys/select.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <string.h>
+#include <errno.h>
+
+
+#define DZC_MAX_CONFLICT_CNT 256
+
+//According to rfc6763, Using TXT records larger than 1300 bytes is NOT 
RECOMMENDED

Review Comment:
   Nice short and descriptive comment :+1: 



##########
bundles/remote_services/discovery_zeroconf/src/discovery_zeroconf_announcer.c:
##########
@@ -0,0 +1,533 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <discovery_zeroconf_announcer.h>
+#include <discovery_zeroconf_constants.h>
+#include <dns_sd.h>
+#include <endpoint_listener.h>
+#include <remote_constants.h>
+#include <celix_utils.h>
+#include <celix_properties.h>
+#include <celix_constants.h>
+#include <celix_threads.h>
+#include <celix_bundle_context.h>
+#include <celix_string_hash_map.h>
+#include <celix_array_list.h>
+#include <celix_log_helper.h>
+#include <celix_types.h>
+#include <celix_errno.h>
+#include <celix_build_assert.h>
+#include <netinet/in.h>
+#include <net/if.h>
+#include <sys/eventfd.h>
+#include <sys/select.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <string.h>
+#include <errno.h>
+
+
+#define DZC_MAX_CONFLICT_CNT 256
+
+//According to rfc6763, Using TXT records larger than 1300 bytes is NOT 
RECOMMENDED
+#define DZC_MAX_TXT_RECORD_SIZE 1300
+
+struct discovery_zeroconf_announcer {
+    celix_bundle_context_t *ctx;
+    celix_log_helper_t *logHelper;
+    char fwUuid[64];
+    endpoint_listener_t epListener;
+    long epListenerSvcId;
+    DNSServiceRef sharedRef;
+    int eventFd;
+    celix_thread_t refreshEPThread;
+    celix_thread_mutex_t mutex;//projects below
+    bool running;
+    celix_string_hash_map_t *endpoints;//key:endpoint id, 
val:announce_endpoint_entry_t*
+    celix_array_list_t *revokedEndpoints;
+};
+
+typedef struct announce_endpoint_entry {
+    celix_properties_t *properties;
+    DNSServiceRef registerRef;
+    unsigned int uid;
+    int ifIndex;
+    const char *serviceName;
+    char serviceType[64];
+    bool announced;
+}announce_endpoint_entry_t;
+
+
+static void 
discoveryZeroconfAnnouncer_eventNotify(discovery_zeroconf_announcer_t 
*announcer);
+static  celix_status_t discoveryZeroconfAnnouncer_endpointAdded(void *handle, 
endpoint_description_t *endpoint, char *matchedFilter);
+static celix_status_t discoveryZeroconfAnnouncer_endpointRemoved(void *handle, 
endpoint_description_t *endpoint, char *matchedFilter);
+static void *discoveryZeroconfAnnouncer_refreshEndpointThread(void *data);
+
+
+celix_status_t discoveryZeroconfAnnouncer_create(celix_bundle_context_t *ctx, 
celix_log_helper_t *logHelper, discovery_zeroconf_announcer_t **announcerOut) {
+    celix_status_t status = CELIX_SUCCESS;
+    discovery_zeroconf_announcer_t *announcer = calloc(1, sizeof(*announcer));
+    assert(announcer != NULL);
+    announcer->ctx = ctx;
+    announcer->logHelper = logHelper;
+    announcer->sharedRef = NULL;
+
+    announcer->eventFd = eventfd(0, 0);
+    if (announcer->eventFd < 0) {
+        status = CELIX_ERROR_MAKE(CELIX_FACILITY_CERRNO, errno);
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to open event fd, 
%d.", errno);
+        goto eventfd_err;
+    }
+
+    status = celixThreadMutex_create(&announcer->mutex, NULL);
+    if (status != CELIX_SUCCESS) {
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to create mutex, 
%d.", status);
+        goto mutex_err;
+    }
+
+    announcer->endpoints = celix_stringHashMap_create();
+    assert(announcer->endpoints != NULL);
+    announcer->revokedEndpoints = celix_arrayList_create();
+    assert(announcer->revokedEndpoints != NULL);
+
+    const char *fwUuid = celix_bundleContext_getProperty(ctx, 
OSGI_FRAMEWORK_FRAMEWORK_UUID, NULL);
+    if (fwUuid == NULL || strlen(fwUuid) >= sizeof(announcer->fwUuid)) {
+        status = CELIX_BUNDLE_EXCEPTION;
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to get framework 
uuid.");
+        goto fw_uuid_err;
+    }
+    strcpy(announcer->fwUuid, fwUuid);
+
+    announcer->epListener.handle = announcer;
+    announcer->epListener.endpointAdded = 
discoveryZeroconfAnnouncer_endpointAdded;
+    announcer->epListener.endpointRemoved = 
discoveryZeroconfAnnouncer_endpointRemoved;
+    char scope[256] = {0};
+    (void)snprintf(scope, sizeof(scope), "(&(%s=*)(%s=%s))", 
OSGI_FRAMEWORK_OBJECTCLASS, OSGI_RSA_ENDPOINT_FRAMEWORK_UUID, fwUuid);
+
+    celix_properties_t *props = celix_properties_create();
+    assert(props != NULL);
+    celix_properties_set(props, "DISCOVERY", "true");
+    celix_properties_set(props, (char *) OSGI_ENDPOINT_LISTENER_SCOPE, scope);
+    celix_service_registration_options_t opt = 
CELIX_EMPTY_SERVICE_REGISTRATION_OPTIONS;
+    opt.serviceName = OSGI_ENDPOINT_LISTENER_SERVICE;
+    opt.properties = props;
+    opt.svc = &announcer->epListener;
+    announcer->epListenerSvcId = 
celix_bundleContext_registerServiceWithOptionsAsync(ctx, &opt);
+    if (announcer->epListenerSvcId < 0) {
+        status = CELIX_BUNDLE_EXCEPTION;
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to register 
endpoint listener.");
+        goto ep_listener_err;
+    }
+
+    announcer->running = true;
+    status = celixThread_create(&announcer->refreshEPThread, NULL, 
discoveryZeroconfAnnouncer_refreshEndpointThread, announcer);
+    if (status != CELIX_SUCCESS) {
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to create 
refreshing endpoint thread, %d.", status);
+        goto announce_ep_thread_err;
+    }
+    celixThread_setName(&announcer->refreshEPThread, "DiscAnnouncer");
+
+    *announcerOut = announcer;
+    return CELIX_SUCCESS;
+announce_ep_thread_err:

Review Comment:
   I do think the usage of goto is better for error handler than lots of 
if/else and I also think it would be nice the document how we prefer to do 
error handling for future code.
   But this is for another pull request.



##########
bundles/remote_services/discovery_zeroconf/src/discovery_zeroconf_announcer.h:
##########
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#ifndef CELIX_DISCOVERY_ZEROCONF_ANNOUNCER_H
+#define CELIX_DISCOVERY_ZEROCONF_ANNOUNCER_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <celix_log_helper.h>

Review Comment:
   For Celix default is still to use `#include ""` for own headers. Maybe 
something to discuss?



##########
bundles/remote_services/discovery_zeroconf/src/discovery_zeroconf_announcer.c:
##########
@@ -0,0 +1,533 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <discovery_zeroconf_announcer.h>
+#include <discovery_zeroconf_constants.h>
+#include <dns_sd.h>
+#include <endpoint_listener.h>
+#include <remote_constants.h>
+#include <celix_utils.h>
+#include <celix_properties.h>
+#include <celix_constants.h>
+#include <celix_threads.h>
+#include <celix_bundle_context.h>
+#include <celix_string_hash_map.h>
+#include <celix_array_list.h>
+#include <celix_log_helper.h>
+#include <celix_types.h>
+#include <celix_errno.h>
+#include <celix_build_assert.h>
+#include <netinet/in.h>
+#include <net/if.h>
+#include <sys/eventfd.h>
+#include <sys/select.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <string.h>
+#include <errno.h>
+
+
+#define DZC_MAX_CONFLICT_CNT 256
+
+//According to rfc6763, Using TXT records larger than 1300 bytes is NOT 
RECOMMENDED
+#define DZC_MAX_TXT_RECORD_SIZE 1300
+
+struct discovery_zeroconf_announcer {
+    celix_bundle_context_t *ctx;
+    celix_log_helper_t *logHelper;
+    char fwUuid[64];
+    endpoint_listener_t epListener;
+    long epListenerSvcId;
+    DNSServiceRef sharedRef;
+    int eventFd;
+    celix_thread_t refreshEPThread;
+    celix_thread_mutex_t mutex;//projects below
+    bool running;
+    celix_string_hash_map_t *endpoints;//key:endpoint id, 
val:announce_endpoint_entry_t*
+    celix_array_list_t *revokedEndpoints;
+};
+
+typedef struct announce_endpoint_entry {
+    celix_properties_t *properties;
+    DNSServiceRef registerRef;
+    unsigned int uid;
+    int ifIndex;
+    const char *serviceName;
+    char serviceType[64];
+    bool announced;
+}announce_endpoint_entry_t;
+
+
+static void 
discoveryZeroconfAnnouncer_eventNotify(discovery_zeroconf_announcer_t 
*announcer);
+static  celix_status_t discoveryZeroconfAnnouncer_endpointAdded(void *handle, 
endpoint_description_t *endpoint, char *matchedFilter);
+static celix_status_t discoveryZeroconfAnnouncer_endpointRemoved(void *handle, 
endpoint_description_t *endpoint, char *matchedFilter);
+static void *discoveryZeroconfAnnouncer_refreshEndpointThread(void *data);
+
+
+celix_status_t discoveryZeroconfAnnouncer_create(celix_bundle_context_t *ctx, 
celix_log_helper_t *logHelper, discovery_zeroconf_announcer_t **announcerOut) {
+    celix_status_t status = CELIX_SUCCESS;
+    discovery_zeroconf_announcer_t *announcer = calloc(1, sizeof(*announcer));
+    assert(announcer != NULL);
+    announcer->ctx = ctx;
+    announcer->logHelper = logHelper;
+    announcer->sharedRef = NULL;
+
+    announcer->eventFd = eventfd(0, 0);

Review Comment:
   I am not really familiar with eventfd, but this seems nice.
   
   Could this also replace the pipe usage in shell_tui? 
   
   In shell_tui a pipe used to provide a fd to stop the text ui thread:
   
https://github.com/apache/celix/blob/master/bundles/shell/shell_tui/src/shell_tui.c#L121-L125
   
https://github.com/apache/celix/blob/master/bundles/shell/shell_tui/src/shell_tui.c#L141



##########
bundles/remote_services/discovery_zeroconf/src/discovery_zeroconf_announcer.c:
##########
@@ -0,0 +1,533 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <discovery_zeroconf_announcer.h>
+#include <discovery_zeroconf_constants.h>
+#include <dns_sd.h>
+#include <endpoint_listener.h>
+#include <remote_constants.h>
+#include <celix_utils.h>
+#include <celix_properties.h>
+#include <celix_constants.h>
+#include <celix_threads.h>
+#include <celix_bundle_context.h>
+#include <celix_string_hash_map.h>
+#include <celix_array_list.h>
+#include <celix_log_helper.h>
+#include <celix_types.h>
+#include <celix_errno.h>
+#include <celix_build_assert.h>
+#include <netinet/in.h>
+#include <net/if.h>
+#include <sys/eventfd.h>
+#include <sys/select.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <string.h>
+#include <errno.h>
+
+
+#define DZC_MAX_CONFLICT_CNT 256
+
+//According to rfc6763, Using TXT records larger than 1300 bytes is NOT 
RECOMMENDED
+#define DZC_MAX_TXT_RECORD_SIZE 1300
+
+struct discovery_zeroconf_announcer {
+    celix_bundle_context_t *ctx;
+    celix_log_helper_t *logHelper;
+    char fwUuid[64];
+    endpoint_listener_t epListener;
+    long epListenerSvcId;
+    DNSServiceRef sharedRef;
+    int eventFd;
+    celix_thread_t refreshEPThread;
+    celix_thread_mutex_t mutex;//projects below
+    bool running;
+    celix_string_hash_map_t *endpoints;//key:endpoint id, 
val:announce_endpoint_entry_t*
+    celix_array_list_t *revokedEndpoints;
+};
+
+typedef struct announce_endpoint_entry {
+    celix_properties_t *properties;
+    DNSServiceRef registerRef;
+    unsigned int uid;
+    int ifIndex;
+    const char *serviceName;
+    char serviceType[64];
+    bool announced;
+}announce_endpoint_entry_t;
+
+
+static void 
discoveryZeroconfAnnouncer_eventNotify(discovery_zeroconf_announcer_t 
*announcer);
+static  celix_status_t discoveryZeroconfAnnouncer_endpointAdded(void *handle, 
endpoint_description_t *endpoint, char *matchedFilter);
+static celix_status_t discoveryZeroconfAnnouncer_endpointRemoved(void *handle, 
endpoint_description_t *endpoint, char *matchedFilter);
+static void *discoveryZeroconfAnnouncer_refreshEndpointThread(void *data);
+
+
+celix_status_t discoveryZeroconfAnnouncer_create(celix_bundle_context_t *ctx, 
celix_log_helper_t *logHelper, discovery_zeroconf_announcer_t **announcerOut) {
+    celix_status_t status = CELIX_SUCCESS;
+    discovery_zeroconf_announcer_t *announcer = calloc(1, sizeof(*announcer));
+    assert(announcer != NULL);
+    announcer->ctx = ctx;
+    announcer->logHelper = logHelper;
+    announcer->sharedRef = NULL;
+
+    announcer->eventFd = eventfd(0, 0);
+    if (announcer->eventFd < 0) {
+        status = CELIX_ERROR_MAKE(CELIX_FACILITY_CERRNO, errno);
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to open event fd, 
%d.", errno);
+        goto eventfd_err;
+    }
+
+    status = celixThreadMutex_create(&announcer->mutex, NULL);
+    if (status != CELIX_SUCCESS) {
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to create mutex, 
%d.", status);
+        goto mutex_err;
+    }
+
+    announcer->endpoints = celix_stringHashMap_create();
+    assert(announcer->endpoints != NULL);
+    announcer->revokedEndpoints = celix_arrayList_create();
+    assert(announcer->revokedEndpoints != NULL);
+
+    const char *fwUuid = celix_bundleContext_getProperty(ctx, 
OSGI_FRAMEWORK_FRAMEWORK_UUID, NULL);
+    if (fwUuid == NULL || strlen(fwUuid) >= sizeof(announcer->fwUuid)) {
+        status = CELIX_BUNDLE_EXCEPTION;
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to get framework 
uuid.");
+        goto fw_uuid_err;
+    }
+    strcpy(announcer->fwUuid, fwUuid);
+
+    announcer->epListener.handle = announcer;
+    announcer->epListener.endpointAdded = 
discoveryZeroconfAnnouncer_endpointAdded;
+    announcer->epListener.endpointRemoved = 
discoveryZeroconfAnnouncer_endpointRemoved;
+    char scope[256] = {0};
+    (void)snprintf(scope, sizeof(scope), "(&(%s=*)(%s=%s))", 
OSGI_FRAMEWORK_OBJECTCLASS, OSGI_RSA_ENDPOINT_FRAMEWORK_UUID, fwUuid);
+
+    celix_properties_t *props = celix_properties_create();
+    assert(props != NULL);
+    celix_properties_set(props, "DISCOVERY", "true");
+    celix_properties_set(props, (char *) OSGI_ENDPOINT_LISTENER_SCOPE, scope);
+    celix_service_registration_options_t opt = 
CELIX_EMPTY_SERVICE_REGISTRATION_OPTIONS;
+    opt.serviceName = OSGI_ENDPOINT_LISTENER_SERVICE;
+    opt.properties = props;
+    opt.svc = &announcer->epListener;
+    announcer->epListenerSvcId = 
celix_bundleContext_registerServiceWithOptionsAsync(ctx, &opt);
+    if (announcer->epListenerSvcId < 0) {
+        status = CELIX_BUNDLE_EXCEPTION;
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to register 
endpoint listener.");
+        goto ep_listener_err;
+    }
+
+    announcer->running = true;
+    status = celixThread_create(&announcer->refreshEPThread, NULL, 
discoveryZeroconfAnnouncer_refreshEndpointThread, announcer);
+    if (status != CELIX_SUCCESS) {
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to create 
refreshing endpoint thread, %d.", status);
+        goto announce_ep_thread_err;
+    }
+    celixThread_setName(&announcer->refreshEPThread, "DiscAnnouncer");
+
+    *announcerOut = announcer;
+    return CELIX_SUCCESS;
+announce_ep_thread_err:
+    celix_bundleContext_unregisterServiceAsync(ctx, 
announcer->epListenerSvcId, NULL, NULL);
+    celix_bundleContext_waitForAsyncUnregistration(ctx, 
announcer->epListenerSvcId);
+ep_listener_err:
+fw_uuid_err:
+    celix_arrayList_destroy(announcer->revokedEndpoints);
+    celix_stringHashMap_destroy(announcer->endpoints);
+    celixThreadMutex_destroy(&announcer->mutex);
+mutex_err:
+    close(announcer->eventFd);
+eventfd_err:
+    free(announcer);
+    return status;
+}
+
+void discoveryZeroconfAnnouncer_destroy(discovery_zeroconf_announcer_t 
*announcer) {
+    celixThreadMutex_lock(&announcer->mutex);
+    announcer->running= false;
+    celixThreadMutex_unlock(&announcer->mutex);
+    discoveryZeroconfAnnouncer_eventNotify(announcer);
+    celixThread_join(announcer->refreshEPThread, NULL);
+    celix_bundleContext_unregisterServiceAsync(announcer->ctx, 
announcer->epListenerSvcId, NULL, NULL);
+    celix_bundleContext_waitForAsyncUnregistration(announcer->ctx, 
announcer->epListenerSvcId);
+
+    announce_endpoint_entry_t *entry = NULL;
+    int size = celix_arrayList_size(announcer->revokedEndpoints);
+    for (int i = 0; i < size; ++i) {
+        entry = (announce_endpoint_entry_t 
*)celix_arrayList_get(announcer->revokedEndpoints, i);
+        celix_properties_destroy(entry->properties);
+        free(entry);
+    }
+    celix_arrayList_destroy(announcer->revokedEndpoints);
+
+    CELIX_STRING_HASH_MAP_ITERATE(announcer->endpoints,iter) {
+        entry = (announce_endpoint_entry_t *) iter.value.ptrValue;
+        celix_properties_destroy(entry->properties);
+        free(entry);
+    }
+    celix_stringHashMap_destroy(announcer->endpoints);
+
+    celixThreadMutex_destroy(&announcer->mutex);
+    close(announcer->eventFd);
+    free(announcer);
+    return;
+}
+
+static void 
discoveryZeroconfAnnouncer_eventNotify(discovery_zeroconf_announcer_t 
*announcer) {
+    eventfd_t val = 1;
+    eventfd_write(announcer->eventFd, val);
+    return;
+}
+
+static bool isLoopBackNetInterface(int ifIndex) {
+    if (ifIndex <= 0) {
+        return false;
+    }
+    bool loopBack = false;
+    int fd = socket(AF_INET, SOCK_DGRAM, 0);
+    if (fd >= 0) {
+        struct ifreq ifr;
+        memset(&ifr, 0, sizeof(ifr));
+        if (if_indextoname((unsigned int)ifIndex, ifr.ifr_name) != NULL) {
+            if (ioctl(fd, SIOCGIFFLAGS, &ifr) == 0) {
+                loopBack = !!(ifr.ifr_ifru.ifru_flags & IFF_LOOPBACK);
+            }
+        }
+        close(fd);
+    }
+    return loopBack;
+}
+
+static  celix_status_t discoveryZeroconfAnnouncer_endpointAdded(void *handle, 
endpoint_description_t *endpoint, char *matchedFilter) {
+    (void)matchedFilter;//unused
+    celix_status_t status = CELIX_SUCCESS;
+    discovery_zeroconf_announcer_t *announcer = 
(discovery_zeroconf_announcer_t *)handle;
+    assert(announcer != NULL);
+    if (endpointDescription_isInvalid(endpoint)) {
+        celix_logHelper_error(announcer->logHelper, "Announcer: Endpoint is 
invalid.");
+        return CELIX_ILLEGAL_ARGUMENT;
+    }
+
+    celix_logHelper_info(announcer->logHelper, "Announcer: Add endpoint for 
%s(%s).", endpoint->serviceName, endpoint->id);
+
+    announce_endpoint_entry_t *entry = calloc(1, sizeof(*entry));
+    assert(entry != NULL);
+    entry->registerRef = NULL;
+    entry->announced = false;
+    entry->uid = celix_utils_stringHash(endpoint->id);

Review Comment:
   hash should not be needed. the endpoint id should be unique and I think it 
is always a UUID.



##########
bundles/remote_services/discovery_zeroconf/src/discovery_zeroconf_announcer.c:
##########
@@ -0,0 +1,533 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <discovery_zeroconf_announcer.h>
+#include <discovery_zeroconf_constants.h>
+#include <dns_sd.h>
+#include <endpoint_listener.h>
+#include <remote_constants.h>
+#include <celix_utils.h>
+#include <celix_properties.h>
+#include <celix_constants.h>
+#include <celix_threads.h>
+#include <celix_bundle_context.h>
+#include <celix_string_hash_map.h>
+#include <celix_array_list.h>
+#include <celix_log_helper.h>
+#include <celix_types.h>
+#include <celix_errno.h>
+#include <celix_build_assert.h>
+#include <netinet/in.h>
+#include <net/if.h>
+#include <sys/eventfd.h>
+#include <sys/select.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <string.h>
+#include <errno.h>
+
+
+#define DZC_MAX_CONFLICT_CNT 256
+
+//According to rfc6763, Using TXT records larger than 1300 bytes is NOT 
RECOMMENDED
+#define DZC_MAX_TXT_RECORD_SIZE 1300
+
+struct discovery_zeroconf_announcer {
+    celix_bundle_context_t *ctx;
+    celix_log_helper_t *logHelper;
+    char fwUuid[64];
+    endpoint_listener_t epListener;
+    long epListenerSvcId;
+    DNSServiceRef sharedRef;
+    int eventFd;
+    celix_thread_t refreshEPThread;
+    celix_thread_mutex_t mutex;//projects below
+    bool running;
+    celix_string_hash_map_t *endpoints;//key:endpoint id, 
val:announce_endpoint_entry_t*
+    celix_array_list_t *revokedEndpoints;
+};
+
+typedef struct announce_endpoint_entry {
+    celix_properties_t *properties;
+    DNSServiceRef registerRef;
+    unsigned int uid;
+    int ifIndex;
+    const char *serviceName;
+    char serviceType[64];
+    bool announced;
+}announce_endpoint_entry_t;
+
+
+static void 
discoveryZeroconfAnnouncer_eventNotify(discovery_zeroconf_announcer_t 
*announcer);
+static  celix_status_t discoveryZeroconfAnnouncer_endpointAdded(void *handle, 
endpoint_description_t *endpoint, char *matchedFilter);
+static celix_status_t discoveryZeroconfAnnouncer_endpointRemoved(void *handle, 
endpoint_description_t *endpoint, char *matchedFilter);
+static void *discoveryZeroconfAnnouncer_refreshEndpointThread(void *data);
+
+
+celix_status_t discoveryZeroconfAnnouncer_create(celix_bundle_context_t *ctx, 
celix_log_helper_t *logHelper, discovery_zeroconf_announcer_t **announcerOut) {
+    celix_status_t status = CELIX_SUCCESS;
+    discovery_zeroconf_announcer_t *announcer = calloc(1, sizeof(*announcer));
+    assert(announcer != NULL);
+    announcer->ctx = ctx;
+    announcer->logHelper = logHelper;
+    announcer->sharedRef = NULL;
+
+    announcer->eventFd = eventfd(0, 0);
+    if (announcer->eventFd < 0) {
+        status = CELIX_ERROR_MAKE(CELIX_FACILITY_CERRNO, errno);
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to open event fd, 
%d.", errno);
+        goto eventfd_err;
+    }
+
+    status = celixThreadMutex_create(&announcer->mutex, NULL);
+    if (status != CELIX_SUCCESS) {
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to create mutex, 
%d.", status);
+        goto mutex_err;
+    }
+
+    announcer->endpoints = celix_stringHashMap_create();
+    assert(announcer->endpoints != NULL);
+    announcer->revokedEndpoints = celix_arrayList_create();
+    assert(announcer->revokedEndpoints != NULL);
+
+    const char *fwUuid = celix_bundleContext_getProperty(ctx, 
OSGI_FRAMEWORK_FRAMEWORK_UUID, NULL);
+    if (fwUuid == NULL || strlen(fwUuid) >= sizeof(announcer->fwUuid)) {
+        status = CELIX_BUNDLE_EXCEPTION;
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to get framework 
uuid.");
+        goto fw_uuid_err;
+    }
+    strcpy(announcer->fwUuid, fwUuid);
+
+    announcer->epListener.handle = announcer;
+    announcer->epListener.endpointAdded = 
discoveryZeroconfAnnouncer_endpointAdded;
+    announcer->epListener.endpointRemoved = 
discoveryZeroconfAnnouncer_endpointRemoved;
+    char scope[256] = {0};
+    (void)snprintf(scope, sizeof(scope), "(&(%s=*)(%s=%s))", 
OSGI_FRAMEWORK_OBJECTCLASS, OSGI_RSA_ENDPOINT_FRAMEWORK_UUID, fwUuid);
+
+    celix_properties_t *props = celix_properties_create();
+    assert(props != NULL);
+    celix_properties_set(props, "DISCOVERY", "true");
+    celix_properties_set(props, (char *) OSGI_ENDPOINT_LISTENER_SCOPE, scope);
+    celix_service_registration_options_t opt = 
CELIX_EMPTY_SERVICE_REGISTRATION_OPTIONS;
+    opt.serviceName = OSGI_ENDPOINT_LISTENER_SERVICE;
+    opt.properties = props;
+    opt.svc = &announcer->epListener;
+    announcer->epListenerSvcId = 
celix_bundleContext_registerServiceWithOptionsAsync(ctx, &opt);
+    if (announcer->epListenerSvcId < 0) {
+        status = CELIX_BUNDLE_EXCEPTION;
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to register 
endpoint listener.");
+        goto ep_listener_err;
+    }
+
+    announcer->running = true;
+    status = celixThread_create(&announcer->refreshEPThread, NULL, 
discoveryZeroconfAnnouncer_refreshEndpointThread, announcer);
+    if (status != CELIX_SUCCESS) {
+        celix_logHelper_fatal(logHelper, "Announcer: Failed to create 
refreshing endpoint thread, %d.", status);
+        goto announce_ep_thread_err;
+    }
+    celixThread_setName(&announcer->refreshEPThread, "DiscAnnouncer");
+
+    *announcerOut = announcer;
+    return CELIX_SUCCESS;
+announce_ep_thread_err:
+    celix_bundleContext_unregisterServiceAsync(ctx, 
announcer->epListenerSvcId, NULL, NULL);
+    celix_bundleContext_waitForAsyncUnregistration(ctx, 
announcer->epListenerSvcId);
+ep_listener_err:
+fw_uuid_err:
+    celix_arrayList_destroy(announcer->revokedEndpoints);
+    celix_stringHashMap_destroy(announcer->endpoints);
+    celixThreadMutex_destroy(&announcer->mutex);
+mutex_err:
+    close(announcer->eventFd);
+eventfd_err:
+    free(announcer);
+    return status;
+}
+
+void discoveryZeroconfAnnouncer_destroy(discovery_zeroconf_announcer_t 
*announcer) {
+    celixThreadMutex_lock(&announcer->mutex);
+    announcer->running= false;
+    celixThreadMutex_unlock(&announcer->mutex);
+    discoveryZeroconfAnnouncer_eventNotify(announcer);
+    celixThread_join(announcer->refreshEPThread, NULL);
+    celix_bundleContext_unregisterServiceAsync(announcer->ctx, 
announcer->epListenerSvcId, NULL, NULL);
+    celix_bundleContext_waitForAsyncUnregistration(announcer->ctx, 
announcer->epListenerSvcId);
+
+    announce_endpoint_entry_t *entry = NULL;
+    int size = celix_arrayList_size(announcer->revokedEndpoints);
+    for (int i = 0; i < size; ++i) {
+        entry = (announce_endpoint_entry_t 
*)celix_arrayList_get(announcer->revokedEndpoints, i);
+        celix_properties_destroy(entry->properties);
+        free(entry);
+    }
+    celix_arrayList_destroy(announcer->revokedEndpoints);
+
+    CELIX_STRING_HASH_MAP_ITERATE(announcer->endpoints,iter) {
+        entry = (announce_endpoint_entry_t *) iter.value.ptrValue;
+        celix_properties_destroy(entry->properties);
+        free(entry);
+    }
+    celix_stringHashMap_destroy(announcer->endpoints);
+
+    celixThreadMutex_destroy(&announcer->mutex);
+    close(announcer->eventFd);
+    free(announcer);
+    return;
+}
+
+static void 
discoveryZeroconfAnnouncer_eventNotify(discovery_zeroconf_announcer_t 
*announcer) {
+    eventfd_t val = 1;
+    eventfd_write(announcer->eventFd, val);
+    return;
+}
+
+static bool isLoopBackNetInterface(int ifIndex) {
+    if (ifIndex <= 0) {
+        return false;
+    }
+    bool loopBack = false;
+    int fd = socket(AF_INET, SOCK_DGRAM, 0);
+    if (fd >= 0) {
+        struct ifreq ifr;
+        memset(&ifr, 0, sizeof(ifr));
+        if (if_indextoname((unsigned int)ifIndex, ifr.ifr_name) != NULL) {
+            if (ioctl(fd, SIOCGIFFLAGS, &ifr) == 0) {
+                loopBack = !!(ifr.ifr_ifru.ifru_flags & IFF_LOOPBACK);
+            }
+        }
+        close(fd);
+    }
+    return loopBack;
+}
+
+static  celix_status_t discoveryZeroconfAnnouncer_endpointAdded(void *handle, 
endpoint_description_t *endpoint, char *matchedFilter) {
+    (void)matchedFilter;//unused
+    celix_status_t status = CELIX_SUCCESS;
+    discovery_zeroconf_announcer_t *announcer = 
(discovery_zeroconf_announcer_t *)handle;
+    assert(announcer != NULL);
+    if (endpointDescription_isInvalid(endpoint)) {
+        celix_logHelper_error(announcer->logHelper, "Announcer: Endpoint is 
invalid.");
+        return CELIX_ILLEGAL_ARGUMENT;
+    }
+
+    celix_logHelper_info(announcer->logHelper, "Announcer: Add endpoint for 
%s(%s).", endpoint->serviceName, endpoint->id);
+
+    announce_endpoint_entry_t *entry = calloc(1, sizeof(*entry));
+    assert(entry != NULL);
+    entry->registerRef = NULL;
+    entry->announced = false;
+    entry->uid = celix_utils_stringHash(endpoint->id);
+    entry->ifIndex = (int)celix_properties_getAsLong(endpoint->properties, 
RSA_DISCOVERY_ZEROCONF_SERVICE_ANNOUNCED_IF_INDEX, 
DZC_SERVICE_ANNOUNCED_IF_INDEX_DEFAULT);
+    // If it is a loopback interface,we will announce the service on the local 
only interface.
+    // Because the mDNSResponder will skip the loopback interface,if it found 
a normal interface.
+    entry->ifIndex = isLoopBackNetInterface(entry->ifIndex) ? 
kDNSServiceInterfaceIndexLocalOnly : entry->ifIndex;
+    const char *serviceSubType = celix_properties_get(endpoint->properties, 
DZC_SERVICE_TYPE_KEY, NULL);
+    if (serviceSubType != NULL) {
+        int bytes = snprintf(entry->serviceType, sizeof(entry->serviceType), 
DZC_SERVICE_PRIMARY_TYPE",%s", serviceSubType);
+        if (bytes >= sizeof(entry->serviceType)) {
+            celix_logHelper_error(announcer->logHelper, "Announcer: Please 
reduce the length of service type for %s.", serviceSubType);
+            status = CELIX_ILLEGAL_ARGUMENT;
+            goto service_type_err;
+        }
+    } else {
+        CELIX_BUILD_ASSERT(sizeof(entry->serviceType) >= 
sizeof(DZC_SERVICE_PRIMARY_TYPE));
+        strcpy(entry->serviceType, DZC_SERVICE_PRIMARY_TYPE);
+    }
+    entry->properties = celix_properties_copy(endpoint->properties);
+
+    //Remove properties that mDNS txt record does not need

Review Comment:
   If the properties are not needed for discovery, I think they should not be 
part of the endpoint description.
   
   IMO on which interfaces zeroconf discovery works 
(RSA_DISCOVERY_ZEROCONF_SERVICE_ANNOUNCED_IF_INDEX) can be a config 
configuration of discovery_zeroconf.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@celix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to