diff options
Diffstat (limited to 'ctrl/facemgr/src/interfaces')
6 files changed, 386 insertions, 78 deletions
diff --git a/ctrl/facemgr/src/interfaces/android_utility/android_utility.c b/ctrl/facemgr/src/interfaces/android_utility/android_utility.c index bb612507f..a4aa2cbfc 100644 --- a/ctrl/facemgr/src/interfaces/android_utility/android_utility.c +++ b/ctrl/facemgr/src/interfaces/android_utility/android_utility.c @@ -124,7 +124,7 @@ int au_on_event(interface_t * interface, const facelet_t * facelet) DEBUG("sending AU udpate"); facelet_set_event(facelet_new, FACELET_EVENT_UPDATE); - facelet_raise_event(facelet_new, interface); + interface_raise_event(interface, facelet_new); return 0; } diff --git a/ctrl/facemgr/src/interfaces/bonjour/bonjour.c b/ctrl/facemgr/src/interfaces/bonjour/bonjour.c index d7b27b995..4d09d89bb 100644 --- a/ctrl/facemgr/src/interfaces/bonjour/bonjour.c +++ b/ctrl/facemgr/src/interfaces/bonjour/bonjour.c @@ -107,7 +107,9 @@ int bj_initialize(interface_t * interface, void * cfg) WSAStartup(versionWanted, &wsaData); #endif - return data->sock; + interface_register_fd(interface, data->sock, NULL); + + return 0; ERR_BUFFER: #ifndef __ANDROID__ @@ -268,7 +270,7 @@ callback(const struct sockaddr* from, mdns_entry_type_t entry, uint16_t type, //facelet_set_remote_port(facelet, ((struct sockaddr_in*)&addr)->sin_port); facelet_set_event(facelet, FACELET_EVENT_UPDATE); - facelet_raise_event(facelet, interface); + interface_raise_event(interface, facelet); break; } @@ -291,7 +293,7 @@ callback(const struct sockaddr* from, mdns_entry_type_t entry, uint16_t type, //facelet_set_remote_port(facelet, ((struct sockaddr_in6*)&addr)->sin6_port); facelet_set_event(facelet, FACELET_EVENT_UPDATE); - facelet_raise_event(facelet, interface); + interface_raise_event(interface, facelet); break; } @@ -317,7 +319,7 @@ callback(const struct sockaddr* from, mdns_entry_type_t entry, uint16_t type, facelet_set_remote_port(facelet, srv.port); facelet_set_event(facelet, FACELET_EVENT_UPDATE); - facelet_raise_event(facelet, interface); + interface_raise_event(interface, facelet); facelet = facelet_create(); facelet_set_netdevice(facelet, bj_data->cfg.netdevice); @@ -325,7 +327,7 @@ callback(const struct sockaddr* from, mdns_entry_type_t entry, uint16_t type, facelet_set_remote_port(facelet, srv.port); facelet_set_event(facelet, FACELET_EVENT_UPDATE); - facelet_raise_event(facelet, interface); + interface_raise_event(interface, facelet); break; } @@ -374,7 +376,7 @@ callback(const struct sockaddr* from, mdns_entry_type_t entry, uint16_t type, * The fact we use a single fd does not allow us to get user_data associated to * the query. */ -int bj_callback(interface_t * interface) +int bj_callback(interface_t * interface, int fd, void * unused) { bj_data_t * data = (bj_data_t*)interface->data; DEBUG("Got an mDNS reply"); diff --git a/ctrl/facemgr/src/interfaces/hicn_light/hicn_light.c b/ctrl/facemgr/src/interfaces/hicn_light/hicn_light.c index 2fdc3f7c3..1f20177c2 100644 --- a/ctrl/facemgr/src/interfaces/hicn_light/hicn_light.c +++ b/ctrl/facemgr/src/interfaces/hicn_light/hicn_light.c @@ -17,6 +17,7 @@ * \file interfaces/hicn_light/hicn_light.c * \brief hICN light interface */ +#include <assert.h> #include <stdbool.h> #include <stdio.h> // snprintf #include <time.h> // time @@ -32,34 +33,43 @@ #define DEFAULT_ROUTE_COST 0 +#define INTERVAL_MS 1000 + typedef enum { HL_STATE_UNDEFINED, + HL_STATE_CONNECTING, HL_STATE_FACES_SENT, HL_STATE_DONE, } hl_state_t; typedef struct { - hc_sock_t * s; + hc_sock_t * s; /* NULL means no active socket */ hl_state_t state; + int timer_fd; /* 0 means no active timer */ } hl_data_t; +/* Forward declarations */ +int hl_timeout(interface_t * interface, int fd, void * unused); + int hl_process_state(interface_t * interface) { hl_data_t * data = (hl_data_t *)interface->data; - hc_data_t * faces; #if 0 char buf[MAXSZ_FACE]; #endif switch(data->state) { - case HL_STATE_UNDEFINED: - if (hc_face_list(data->s, &faces) < 0) { + case HL_STATE_UNDEFINED: // FIXME + case HL_STATE_CONNECTING: // FIXME + if (hc_face_list_async(data->s) < 0) { /* Blocking call */ printf("Could not retrieve face list\n"); return -1; } + break; +#if 0 foreach_face(f, faces) { #if 0 hc_face_snprintf(buf, MAXSZ_FACE, f); @@ -67,9 +77,10 @@ int hl_process_state(interface_t * interface) #endif facelet_t * facelet = facelet_create_from_face(&f->face); facelet_set_event(facelet, FACELET_EVENT_GET); - facelet_raise_event(facelet, interface); + interface_raise_event(interface, facelet); } break; +#endif case HL_STATE_FACES_SENT: break; @@ -81,36 +92,132 @@ int hl_process_state(interface_t * interface) return 0; } -int hl_initialize(interface_t * interface, void * cfg) + +int +hl_after_connect(interface_t * interface) { - hl_data_t * data = malloc(sizeof(hl_data_t)); - if (!data) { - ERROR("[hicn_light] Out of memory!"); - goto ERR_MALLOC; + hl_data_t * data = interface->data; + // XXX cancel timer + + /* File descriptor for control socket operations */ + if (interface_register_fd(interface, hc_sock_get_fd(data->s), NULL) < 0) { + ERROR("[hc_connect] Error registering fd"); + goto ERR_FD; + } + + data->state = HL_STATE_UNDEFINED; + + hl_process_state(interface); + + return 0; + + //interface_unregister_fd(interface, hc_sock_get_fd(data->s)); +ERR_FD: + return -1; +} + +int _hl_connect(interface_t * interface); + +int +hl_connect_timeout(interface_t * interface, int fd, void * unused) +{ + int rc = _hl_connect(interface); + if (rc < 0) { + ERROR("[hl_initialize] Error during connection reattempt; next attempt in %ds", INTERVAL_MS / 1000); + return -1; } + if (interface_unregister_timer(interface, fd) < 0) { + ERROR("[hl_connect_timeout] Could not cancel timer after successful connect"); + } + + /* Connect success */ + return hl_after_connect(interface); +} + + +int +_hl_connect(interface_t * interface) +{ + hl_data_t * data = interface->data; + assert(!data->s); + data->s = hc_sock_create(); if (data->s <= 0) { - ERROR("[hicn_light] Could not create control socket"); + ERROR("[hc_connect] Could not create control socket"); goto ERR_SOCK; } if (hc_sock_connect(data->s) < 0) { - ERROR("[hicn_light] Could not connect control socket"); + ERROR("[hc_connect] Could not connect control socket"); goto ERR_CONNECT; } - data->state = HL_STATE_UNDEFINED; + return hl_after_connect(interface); + +ERR_CONNECT: + hc_sock_free(data->s); + data->s = NULL; +ERR_SOCK: + return -1; + +} + +int hl_disconnect(interface_t * interface) +{ + hl_data_t * data = (hl_data_t *) interface->data; + if (data->timer_fd > 0) + interface_unregister_timer(interface, data->timer_fd); + + if (data->s) { + interface_unregister_fd(interface, hc_sock_get_fd(data->s)); + hc_sock_free(data->s); + } + + return 0; +} + +int +hl_connect(interface_t * interface) +{ + hl_data_t * data = interface->data; + + if (_hl_connect(interface) >= 0) + return 0; + + /* Timer for managing the connection to the forwarder */ + DEBUG("Connection to forwarder failed... next retry in %ds", INTERVAL_MS / 1000); + data->timer_fd = interface_register_timer(interface, INTERVAL_MS, hl_connect_timeout, NULL); + if (data->timer_fd < 0) { + ERROR("[hc_connect] Could not initialize reattempt timer"); + return -1; + } + + return 0; +} + +int +hl_initialize(interface_t * interface, void * cfg) +{ + hl_data_t * data = malloc(sizeof(hl_data_t)); + if (!data) { + ERROR("[hicn_light] Out of memory!"); + goto ERR_MALLOC; + } + + data->s = NULL; + data->timer_fd = 0; interface->data = data; - hl_process_state(interface); + if (hl_connect(interface) < 0) { + ERROR("[hl_initialize] Error during connection to forwarder"); + goto ERR_CONNECT; + } return 0; ERR_CONNECT: - hc_sock_free(data->s); -ERR_SOCK: free(data); ERR_MALLOC: return -1; @@ -118,8 +225,12 @@ ERR_MALLOC: int hl_finalize(interface_t * interface) { - //hc_data_t * data = interface->data; - //hc_sock_close(data->s); + hl_data_t * data = (hl_data_t *) interface->data; + + hl_disconnect(interface); + + free(data); + return 0; } @@ -131,9 +242,23 @@ int hl_on_event(interface_t * interface, const facelet_t * facelet) hl_data_t * data = (hl_data_t *)interface->data; face_t * face = NULL; - if (facelet_get_face(facelet, &face) < 0) + + /* NOTE + * - One example where this fails (and it is normal) is when we delete a + * face that was not completely created, because for instance bonjour did + * not give any data + */ + if (facelet_get_face(facelet, &face) < 0) { + ERROR("Could not retrieve face from facelet"); + return -1; + } + + if (!data->s) { + /* We are not connected to the forwarder */ return -1; + } + switch(facelet_get_event(facelet)) { case FACELET_EVENT_CREATE: @@ -147,10 +272,10 @@ int hl_on_event(interface_t * interface, const facelet_t * facelet) } INFO("Created face id=%d\n", hc_face.id); -#if 0 - /* Add default route v4 */ + /* Adding default routs e*/ +#if 1 route = (hc_route_t) { - .face_id = face.id, + .face_id = hc_face.id, .family = AF_INET, .remote_addr = IPV4_ANY, .len = 0, @@ -161,12 +286,9 @@ int hl_on_event(interface_t * interface, const facelet_t * facelet) ERROR("Failed to create default hICN/IPv4 route"); goto ERR; } - INFO("Successfully created default hICN/IPv4 route."); -#endif -#if 0 route = (hc_route_t) { - .face_id = face.id, + .face_id = hc_face.id, .family = AF_INET6, .remote_addr = IPV6_ANY, .len = 0, @@ -176,9 +298,8 @@ int hl_on_event(interface_t * interface, const facelet_t * facelet) ERROR("Failed to create default hICN/IPv6 route"); goto ERR; } -#endif - /* Adding default route */ +#else route = (hc_route_t) { .face_id = hc_face.id, .family = AF_INET6, @@ -193,6 +314,8 @@ int hl_on_event(interface_t * interface, const facelet_t * facelet) ERROR("Failed to create hICN/IPv6 route"); goto ERR; } +#endif + INFO("Successfully created default route(s)."); break; @@ -212,6 +335,8 @@ int hl_on_event(interface_t * interface, const facelet_t * facelet) if (facelet_get_admin_state_status(facelet) == FACELET_ATTR_STATUS_DIRTY) { hc_face.face = *face; hc_face_t * face_found; + + printf("hc_face_get\n"); rc = hc_face_get(data->s, &hc_face, &face_found); if (rc < 0) { ERROR("Failed to find face\n"); @@ -224,7 +349,6 @@ int hl_on_event(interface_t * interface, const facelet_t * facelet) char conn_id_or_name[SYMBOLIC_NAME_LEN]; snprintf(conn_id_or_name, SYMBOLIC_NAME_LEN, "%d", face_found->id); free(face_found); - printf("Face id = %d\n", face_found->id); face_state_t admin_state; if (facelet_get_admin_state(facelet, &admin_state) < 0) { @@ -233,6 +357,7 @@ int hl_on_event(interface_t * interface, const facelet_t * facelet) } printf("Setting admin state"); + printf("hc_connection_set_admin_state\n"); if (hc_connection_set_admin_state(data->s, conn_id_or_name, admin_state) < 0) { ERROR("Failed to update admin state"); goto ERR; @@ -247,15 +372,56 @@ int hl_on_event(interface_t * interface, const facelet_t * facelet) goto ERR; } + face_free(face); return 0; ERR: + face_free(face); return -1; } +int hl_callback(interface_t * interface, int fd, void * unused) +{ + hl_data_t * data = (hl_data_t*)interface->data; + + hc_data_t * faces; + if (hc_sock_callback(data->s, &faces) < 0){ + DEBUG("Closing socket... reconnecting..."); + if (interface_unregister_fd(interface, hc_sock_get_fd(data->s)) < 0) { + ERROR("[hl_initialize] Error registering fd"); + } + hc_sock_free(data->s); + data->s = NULL; + hl_connect(interface); + return 0; + } + + if (faces->complete) { + foreach_face(f, faces) { +#if 1 + char buf[MAXSZ_FACE]; + hc_face_snprintf(buf, MAXSZ_FACE, f); + printf("Face: %s\n", buf); +#else + facelet_t * facelet = facelet_create_from_face(&f->face); + facelet_set_event(facelet, FACELET_EVENT_GET); + interface_raise_event(interface, facelet); +#endif + } + } + hc_data_free(faces); + + /* XXX how do we know what object we get back */ + + /* We have a queue of pending data elements per active query */ + + return 0; +} + const interface_ops_t hicn_light_ops = { .type = "hicn_light", .initialize = hl_initialize, .finalize = hl_finalize, .on_event = hl_on_event, + .callback = hl_callback, }; diff --git a/ctrl/facemgr/src/interfaces/netlink/netlink.c b/ctrl/facemgr/src/interfaces/netlink/netlink.c index 08cbe4d3b..babf1c305 100644 --- a/ctrl/facemgr/src/interfaces/netlink/netlink.c +++ b/ctrl/facemgr/src/interfaces/netlink/netlink.c @@ -67,6 +67,7 @@ int nl_process_state(interface_t * interface) switch(data->state) { case NL_STATE_UNDEFINED: { + DEBUG("[nl_process_state] UNDEFINED->LINK_SENT"); struct { struct nlmsghdr header; struct rtgenmsg payload; @@ -93,6 +94,7 @@ int nl_process_state(interface_t * interface) case NL_STATE_LINK_SENT: { + DEBUG("[nl_process_state] LINK_SENT->ADDR_SENT"); /* Issue a first query to receive static state */ struct { struct nlmsghdr header; @@ -120,6 +122,7 @@ int nl_process_state(interface_t * interface) case NL_STATE_ADDR_SENT: { + DEBUG("[nl_process_state] ADDR_SENT->DONE"); data->state = NL_STATE_DONE; break; } @@ -167,9 +170,13 @@ int nl_initialize(interface_t * interface, void * cfg) interface->data = data; + interface_register_fd(interface, data->fd, NULL); + +#if 1 nl_process_state(interface); +#endif - return data->fd; // 0; + return 0; ERR_BIND: close(data->fd); @@ -205,7 +212,7 @@ int parse_link(struct nlmsghdr * h, facelet_t ** facelet, *facelet = facelet_create(); netdevice_t * netdevice = netdevice_create_from_name(interface_name); if (!netdevice) - goto ERROR; + goto ERROR_ND; int rc = facelet_set_netdevice(*facelet, *netdevice); if (rc < 0) goto ERROR; @@ -237,9 +244,12 @@ int parse_link(struct nlmsghdr * h, facelet_t ** facelet, // - ifi_change // - IFLA_PROTINFO + netdevice_free(netdevice); return 0; ERROR: + netdevice_free(netdevice); +ERROR_ND: facelet_free(*facelet); *facelet = NULL; @@ -303,7 +313,7 @@ int parse_addr(struct nlmsghdr * h, facelet_t ** facelet, netdevice_t * netdevice = netdevice_create_from_index(ifa->ifa_index); if (!netdevice) { ERROR("[netlink.parse_addr] error creating netdevice '%s'", interface_name); - goto ERROR; + goto ERROR_ND; } if (interface_name) { @@ -324,16 +334,19 @@ int parse_addr(struct nlmsghdr * h, facelet_t ** facelet, goto ERROR; } + netdevice_free(netdevice); return 0; ERROR: + netdevice_free(netdevice); +ERROR_ND: facelet_free(*facelet); *facelet = NULL; return -1; } -int nl_callback(interface_t * interface) +int nl_callback(interface_t * interface, int fd, void * unused) { nl_data_t * data = (nl_data_t*)interface->data; @@ -403,10 +416,10 @@ int nl_callback(interface_t * interface) break; } - DEBUG("Interface %s: address was removed", interface_name); + //DEBUG("Interface %s: address was removed", interface_name); if (facelet) { facelet_set_event(facelet, FACELET_EVENT_DELETE); - facelet_raise_event(facelet, interface); + interface_raise_event(interface, facelet); } break; } @@ -423,11 +436,11 @@ int nl_callback(interface_t * interface) break; } - DEBUG("Interface %s: new address was assigned: %s", interface_name, interface_address); + //DEBUG("Interface %s: new address was assigned: %s", interface_name, interface_address); if (facelet) { facelet_set_event(facelet, FACELET_EVENT_UPDATE); - facelet_raise_event(facelet, interface); + interface_raise_event(interface, facelet); } break; } @@ -441,12 +454,15 @@ int nl_callback(interface_t * interface) ERROR("Error parsing link message"); break; } - if (facelet) { - facelet_set_event(facelet, FACELET_EVENT_DELETE); - facelet_raise_event(facelet, interface); - } - DEBUG("Network interface %s was removed", interface_name); + //DEBUG("Network interface %s was removed", interface_name); + + if (!facelet) + break; + + facelet_set_event(facelet, FACELET_EVENT_DELETE); + interface_raise_event(interface, facelet); + break; } @@ -464,11 +480,15 @@ int nl_callback(interface_t * interface) // UP RUNNING // UP NOT RUNNING // DOWN NOT RUNNING +#if 0 DEBUG("New network interface %s, state: %s %s", interface_name, up ? "UP" : "DOWN", running ? "RUNNING" : "NOT_RUNNING"); +#endif - if (facelet && up && running) { + if (!facelet) + break; + if (up && running) { facelet_set_event(facelet, FACELET_EVENT_CREATE); facelet_t * facelet6 = facelet_dup(facelet); if (!facelet6) { @@ -477,10 +497,12 @@ int nl_callback(interface_t * interface) } facelet_set_family(facelet, AF_INET); - facelet_raise_event(facelet, interface); + interface_raise_event(interface, facelet); facelet_set_family(facelet6, AF_INET6); - facelet_raise_event(facelet6, interface); + interface_raise_event(interface, facelet6); + } else { + facelet_free(facelet); } break; } @@ -507,6 +529,7 @@ int nl_finalize(interface_t * interface) { nl_data_t * data = (nl_data_t*)interface->data; close(data->fd); + free(interface->data); return 0; } diff --git a/ctrl/facemgr/src/interfaces/network_framework/network_framework.c b/ctrl/facemgr/src/interfaces/network_framework/network_framework.c index ed2f88e9e..f438d34d5 100644 --- a/ctrl/facemgr/src/interfaces/network_framework/network_framework.c +++ b/ctrl/facemgr/src/interfaces/network_framework/network_framework.c @@ -18,6 +18,8 @@ * \brief Implementation of Network framework interface */ +#include "Availability.h" + #include <sys/socket.h> #include <arpa/inet.h> @@ -36,6 +38,11 @@ #include "network_framework.h" +#if !defined(MAC_OS_X_VERSION_10_14) +#error "Network frameork requires MacOSX 10.14+" +#endif /* !defined(MAC_OS_X_VERSION_10_14) */ + + /* * Bonjour service discovery for hICN forwarder * @@ -59,7 +66,7 @@ #define BONJOUR_PROTOCOL udp #define BONJOUR_SERVICE_DOMAIN "local" -#define BONJOUR_SERVICE_NAME "hicn" +#define BONJOUR_SERVICE_NAME "hicn node" /* Generated variables */ #define BONJOUR_SERVICE_TYPE "_hicn._" STRINGIZE(BONJOUR_PROTOCOL) @@ -80,7 +87,7 @@ const char * interface_type_str[] = { "OTHER", "WIFI", "CELLULAR", "WIRED", "LOOPBACK", }; -#if 0 +#if 1 typedef enum { PATH_STATUS_INVALID, PATH_STATUS_SATISTIED, @@ -212,6 +219,28 @@ dump_connection(nw_connection_t connection, int indent) nw_release(path); } +#if defined(MAC_OS_X_VERSION_10_15) +void +dump_browse_result(nw_browse_result_t result, int indent) +{ + /* Endpoint */ + nw_endpoint_t browse_endpoint = nw_browse_result_copy_endpoint(result); + if (!bendpoint) { + ERROR("[network_framework.dump_result] Failed to retrieve endpoint from Bonjour browse result"); + return; + } + printfi(indent + 1, "Endpoint:") + dump_endpoint(browse_endpoint, indent + 2); + + /* Interfaces */ + printfi(indent + 1, "Interfaces:") + nw_browse_result_enumerate_interfaces(result, ^(nw_interface_t interface) { + dump_interface(interface, index + 2); + return true; + }); +} +#endif /* defined(MAC_OS_X_VERSION_10_15) */ + facelet_t * facelet_create_from_connection(nw_connection_t connection) { @@ -302,7 +331,7 @@ void on_connection_state_event(interface_t * interface, nw_interface_t iface, nw_connection_t cnx, nw_connection_state_t state, nw_error_t error) { -#if 0 +#if 1 DEBUG("Connection [new state = %s]:\n", connection_state_str[state]); nw_path_t path = nw_connection_copy_current_path(cnx); nw_path_enumerate_interfaces(path, (nw_path_enumerate_interfaces_block_t)^(nw_interface_t interface) { @@ -331,7 +360,9 @@ on_connection_state_event(interface_t * interface, nw_interface_t iface, case nw_connection_state_ready: { -#if 0 + printf("info:\n"); + warn("connection ready"); +#if 1 WITH_DEBUG({ dump_connection(cnx, 1); }); @@ -340,7 +371,7 @@ on_connection_state_event(interface_t * interface, nw_interface_t iface, if (!facelet) return; facelet_set_event(facelet, FACELET_EVENT_CREATE); - facelet_raise_event(facelet, interface); + interface_raise_event(interface, facelet); break; } case nw_connection_state_failed: @@ -371,10 +402,10 @@ void on_connection_path_event(interface_t * interface, nw_interface_t iface, nw_connection_t cnx, nw_path_t path) { -#if 0 +#if 1 DEBUG("Connection [path changed]:\n"); WITH_DEBUG({ - //dump_connection(cnx, 1); + dump_connection(cnx, 1); }); #endif /* redundant *//* @@ -418,15 +449,7 @@ void on_interface_event(interface_t * interface, nw_interface_t iface) * time, if none is discovered, we cannot do any tunnel face. */ - nw_endpoint_t endpoint; - - endpoint = nw_endpoint_create_bonjour_service( - BONJOUR_SERVICE_NAME, - BONJOUR_SERVICE_TYPE, - BONJOUR_SERVICE_DOMAIN); - - if (!endpoint) - goto ERR; + // OLD CODE /* nw_parameters_create_secure_{udp,tcp} */ nw_parameters_t parameters = nw_parameters_create_fn( @@ -434,14 +457,93 @@ void on_interface_event(interface_t * interface, nw_interface_t iface) NW_PARAMETERS_DEFAULT_CONFIGURATION /* default udp/tcp */); if (!parameters) - goto ERR; + goto ERR_PARAMETERS; nw_parameters_require_interface(parameters, iface); nw_parameters_set_reuse_local_address(parameters, true); +#if defined(MAC_OS_X_VERSION_10_15) + /* + * Before being able to create a bonjour endpoint, we need to browse for + * available services on the local network using the parameters specified + * before. + */ + nw_browse_descriptor_t descriptor = nw_browse_descriptor_create_bonjour_service(BONJOUR_SERVICE_TYPE, BONJOUR_SERVICE_DOMAIN); + if (!descriptor) { + ERROR("[network_framework.on_interface_event] Failed to create a bonjour browse descriptor"); + goto ERR_DESCRIPTOR; + } + + nw_browser_t browser = nw_browser_create(descriptor, parameters); + nw_browser_set_queue(browser, dispatch_get_main_queue()); + nw_browser_set_browse_results_changed_handler(browser, ^(nw_browse_result_t result, nw_browse_result_t result2, bool flag) { + /* Dump result */ + printfi(0, "NEW BROWSE RESULT"); + printfi(1, "Result:"); + dump_browse_result(result, 2); + printfi(1, "Result2:"); + dump_browse_result(result2, 2); + printfi("Flag: %s\n", flag?"ON":"OFF"); + + /* Changes */ + nw_browse_result_change_t change = nw_browse_result_get_changes(result, result2); + switch(change) { + case nw_browse_result_change_identical: + printfi("The compared services are identical."); + break; + case nw_browse_result_change_result_added: + printfi(2, "A new service was discovered."); + break; + + case nw_browse_result_change_result_removed: + printfi(2, "A previously discovered service was removed."); + break; + + case nw_browse_result_change_txt_record_changed: + printfi(2, "The service's associated TXT record changed."); + break; + + case nw_browse_result_change_interface_added: + printfi(2, "The service was discovered over a new interface."); + break; + +nw_browse_result_change_interface_removed + printfi(2, "The service was no longer discovered over a certain interface."); + break; + } + }); + + browser.browseResultsChangedHandler = { browseResults, _ in + for browseResult in browseResults { + print("Discovered \(browseResult.endpoint) over \(browseResult.interfaces)") + } + } + nw_browser_start(browser); +//#else +//#warning "Bonjour discovery only available in MacOS 10.15+" +#endif /* defined(MAC_OS_X_VERSION_10_15) */ + + /* + * Now that we have resolve the name of a bonjour remote, we can create a + * connection to the corresponding endpoint identified by its name. + */ + nw_endpoint_t endpoint; + + DEBUG("Creating bonjour service towards NAME=%s TYPE=%s DOMAIN=%s", + BONJOUR_SERVICE_NAME, BONJOUR_SERVICE_TYPE, BONJOUR_SERVICE_DOMAIN); + endpoint = nw_endpoint_create_bonjour_service( + BONJOUR_SERVICE_NAME, + BONJOUR_SERVICE_TYPE, + BONJOUR_SERVICE_DOMAIN); + + if (!endpoint) { + ERROR("[network_framework.on_interface_event] Failed to create bound Bonjour connection"); + goto ERR_ENDPOINT; + } + nw_connection_t connection = nw_connection_create(endpoint, parameters); if (!connection) - goto ERR; + goto ERR_CONNECTION; nw_release(endpoint); nw_release(parameters); @@ -460,7 +562,7 @@ void on_interface_event(interface_t * interface, nw_interface_t iface) }); nw_connection_set_better_path_available_handler(connection, ^(bool value) { -#if 0 +#if 1 DEBUG("Connection [better path = %s]\n", (value ? "true" : "false")); WITH_DEBUG({ dump_connection(connection, 1); @@ -469,7 +571,7 @@ void on_interface_event(interface_t * interface, nw_interface_t iface) }); nw_connection_set_viability_changed_handler(connection, ^(bool value) { -#if 0 +#if 1 DEBUG("Connection [viable = %s]\n", (value ? "true" : "false")); WITH_DEBUG({ //dump_connection(connection, 1); @@ -484,7 +586,7 @@ void on_interface_event(interface_t * interface, nw_interface_t iface) if (!facelet) return; facelet_set_event(facelet, value ? FACELET_EVENT_CREATE : FACELET_EVENT_DELETE); - facelet_raise_event(facelet, interface); + interface_raise_event(interface, facelet); }); @@ -493,14 +595,26 @@ void on_interface_event(interface_t * interface, nw_interface_t iface) nw_connection_set_queue(connection, dispatch_get_main_queue()); nw_retain(connection); // Hold a reference until cancelled -#if 0 - DEBUG("Created Bonjour cnx on interface:\n"); +#if 1 + DEBUG("Created Bonjour cnx on interface:"); WITH_DEBUG({ dump_interface(iface, 1); }); #endif -ERR: + return; + + nw_release(connection); +ERR_CONNECTION: + nw_release(endpoint); +ERR_ENDPOINT: +#if defined(MAC_OS_X_VERSION_10_15) + nw_release(descriptor); +ERR_DESCRIPTOR: +#endif /* defined(MAC_OS_X_VERSION_10_15) */ + nw_release(parameters); + +ERR_PARAMETERS: return; } @@ -509,7 +623,7 @@ void on_path_event(interface_t * interface, nw_path_t path) /* Simplification: we handle path event only once. * Ideally, test whether we discover new interfaces or not */ -#if 0 +#if 1 DEBUG("Path [event]:\n"); WITH_DEBUG({ dump_path(path, 1); @@ -529,6 +643,9 @@ int nf_initialize(interface_t * interface, void * cfg) if (!data) goto ERR_MALLOC; + if (cfg) + data->cfg = * (network_framework_cfg_t *)cfg; + data->pm = nw_path_monitor_create(); if (!data->pm) goto ERR_PM; diff --git a/ctrl/facemgr/src/interfaces/updown/updown.c b/ctrl/facemgr/src/interfaces/updown/updown.c index f5a813cd4..c864c8c04 100644 --- a/ctrl/facemgr/src/interfaces/updown/updown.c +++ b/ctrl/facemgr/src/interfaces/updown/updown.c @@ -125,7 +125,7 @@ int updown_callback(interface_t * interface) facelet_set_event(facelet, FACELET_EVENT_UPDATE); - facelet_raise_event(facelet, interface); + interface_raise_event(interface, facelet); return 0; } |