From: Niklas Hofmann Date: Thu, 31 Jul 2014 01:08:06 +0000 (+0200) Subject: a first mdns discovery implementation X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=e401f4fcb9bf48e4efd04f9671a7c6cf44ff0439;p=meshlink a first mdns discovery implementation --- diff --git a/src/discovery.c b/src/discovery.c index 834822aa..a7b36c48 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -12,6 +12,8 @@ #include #include +#include + #define MESHLINK_MDNS_SERVICE_TYPE "_meshlink._tcp" //#define MESHLINK_MDNS_SERVICE_NAME "Meshlink" #define MESHLINK_MDNS_FINGERPRINT_KEY "fingerprint" @@ -76,10 +78,40 @@ static void discovery_resolve_callback( if( node && fgli && strcmp(avahi_string_list_get_text(fgli)+strlen(MESHLINK_MDNS_FINGERPRINT_KEY)+1, meshlink_get_fingerprint(mesh, node)) == 0 ) { fprintf(stderr, "Node %s is part of the mesh network - updating ip address.\n", node->name); + + struct sockaddr_storage naddr; + memset(&naddr, 0, sizeof(naddr)); + + switch(address->proto) + { + case AVAHI_PROTO_INET: + { + struct sockaddr_in* naddr_in = (struct sockaddr_in*)&naddr; + naddr_in->sin_family = AF_INET; + naddr_in->sin_port = port; + naddr_in->sin_addr.s_addr = address->data.ipv4.address; + } + break; + + case AVAHI_PROTO_INET6: + { + struct sockaddr_in6* naddr_in = (struct sockaddr_in6*)&naddr; + naddr_in->sin6_family = AF_INET6; + naddr_in->sin6_port = port; + memcpy(naddr_in->sin6_addr.s6_addr, address->data.ipv6.address, sizeof(naddr_in->sin6_addr.s6_addr)); + } + break; + + default: + naddr.ss_family = AF_UNKNOWN; + } + + // @TODO: aquire mutex? + meshlink_hint_address(mesh, node->name, &naddr); } else { - fprintf(stderr, "Node %s is not part of the mesh network - ignoring ip address.\n", node->name); + fprintf(stderr, "Node %s is not part of the mesh network - ignoring ip address.\n", node ? node->name : "n/a"); } } } @@ -298,7 +330,7 @@ fail: void discovery_stop(meshlink_handle_t *mesh) { - // @TODO: Shut down + // Shut down avahi_simple_poll_quit(mesh->avahi_poll); // Wait for the discovery thread to finish diff --git a/src/meshlink.c b/src/meshlink.c index 8d120813..c25e08c5 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -1660,7 +1660,7 @@ void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node) { /* Hint that a hostname may be found at an address * See header file for detailed comment. */ -extern void meshlink_hint_address(meshlink_handle_t *mesh, char *hostname, struct sockaddr *addr) { +extern void meshlink_hint_address(meshlink_handle_t *mesh, const char *hostname, struct sockaddr *addr) { if(!mesh || !hostname || !addr) return; diff --git a/src/meshlink.h b/src/meshlink.h index 8a101741..4468fd00 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -574,7 +574,7 @@ extern ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t * given hostname. The caller is free to overwrite or free * this memory once meshlink returns. */ -extern void meshlink_hint_address(meshlink_handle_t *mesh, char *hostname, struct sockaddr *addr); +extern void meshlink_hint_address(meshlink_handle_t *mesh, const char *hostname, struct sockaddr *addr); #ifdef __cplusplus } diff --git a/src/node.c b/src/node.c index b7b39927..edf2c858 100644 --- a/src/node.c +++ b/src/node.c @@ -104,7 +104,7 @@ void node_del(meshlink_handle_t *mesh, node_t *n) { pthread_mutex_unlock(&(mesh->nodes_mutex)); } -node_t *lookup_node(meshlink_handle_t *mesh, char *name) { +node_t *lookup_node(meshlink_handle_t *mesh, const char *name) { node_t n = {NULL}; node_t* result; diff --git a/src/node.h b/src/node.h index 4f0f1308..e9bc0538 100644 --- a/src/node.h +++ b/src/node.h @@ -97,7 +97,7 @@ extern node_t *new_node(void) __attribute__ ((__malloc__)); extern void free_node(node_t *); extern void node_add(struct meshlink_handle *mesh, node_t *); extern void node_del(struct meshlink_handle *mesh, node_t *); -extern node_t *lookup_node(struct meshlink_handle *mesh, char *); +extern node_t *lookup_node(struct meshlink_handle *mesh, const char *); extern node_t *lookup_node_udp(struct meshlink_handle *mesh, const sockaddr_t *); extern void update_node_udp(struct meshlink_handle *mesh, node_t *, const sockaddr_t *);