From 70f19a5596e08b398b91112c2c246dcc6ec6b1dc Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Wed, 23 Jul 2014 18:09:16 +0200 Subject: [PATCH] gluon-neighbour-info: query direct neighbours This is a simple client for gluon-announced. --- gluon/gluon-neighbour-info/Makefile | 39 +++++ gluon/gluon-neighbour-info/src/Makefile | 6 + .../src/gluon-neighbour-info.c | 163 ++++++++++++++++++ 3 files changed, 208 insertions(+) create mode 100644 gluon/gluon-neighbour-info/Makefile create mode 100644 gluon/gluon-neighbour-info/src/Makefile create mode 100644 gluon/gluon-neighbour-info/src/gluon-neighbour-info.c diff --git a/gluon/gluon-neighbour-info/Makefile b/gluon/gluon-neighbour-info/Makefile new file mode 100644 index 0000000..74aa6e8 --- /dev/null +++ b/gluon/gluon-neighbour-info/Makefile @@ -0,0 +1,39 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=gluon-neighbour-info +PKG_VERSION:=1 +PKG_RELEASE:=1 + +PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) + +include $(INCLUDE_DIR)/package.mk + +define Package/gluon-neighbour-info + SECTION:=gluon + CATEGORY:=Gluon + TITLE:=neighbour-info + DEPENDS:= +endef + +define Package/gluon-neighbour-info/description + Gluon community wifi mesh firmware framework: neighbour-info +endef + +define Build/Prepare + mkdir -p $(PKG_BUILD_DIR) + $(CP) ./src/* $(PKG_BUILD_DIR)/ +endef + +define Build/Configure +endef + +define Build/Compile + CFLAGS="$(TARGET_CFLAGS)" CPPFLAGS="$(TARGET_CPPFLAGS)" $(MAKE) -C $(PKG_BUILD_DIR) $(TARGET_CONFIGURE_OPTS) +endef + +define Package/gluon-neighbour-info/install + $(INSTALL_DIR) $(1)/usr/bin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/gluon-neighbour-info $(1)/usr/bin/ +endef + +$(eval $(call BuildPackage,gluon-neighbour-info)) diff --git a/gluon/gluon-neighbour-info/src/Makefile b/gluon/gluon-neighbour-info/src/Makefile new file mode 100644 index 0000000..16a3e95 --- /dev/null +++ b/gluon/gluon-neighbour-info/src/Makefile @@ -0,0 +1,6 @@ +all: gluon-neighbour-info + +gluon-neighbour-info: gluon-neighbour-info.c + +clean: + rm gluon-neighbour-info diff --git a/gluon/gluon-neighbour-info/src/gluon-neighbour-info.c b/gluon/gluon-neighbour-info/src/gluon-neighbour-info.c new file mode 100644 index 0000000..81ce248 --- /dev/null +++ b/gluon/gluon-neighbour-info/src/gluon-neighbour-info.c @@ -0,0 +1,163 @@ +/* + Copyright (c) 2014, Nils Schneider + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * a destination address with interface identifier + * a port + * a requeststring + * a timeout + * a filter + */ + +void usage() { + puts("Usage: gluon-neighbour-info [-h] [-s] -d -p -i -r "); + puts(" -p UDP port"); + puts(" -d multicast group, e.g. ff02:0:0:0:0:0:2:1001"); + puts(" -i interface, e.g. eth0 "); + puts(" -r request, e.g. nodeinfo"); + puts(" -s output as server-sent events"); + puts(" -h this help\n"); +} + +int request(const int sock, const struct sockaddr_in6 *client_addr, const char *request, bool sse) { + ssize_t ret; + struct sockaddr_in6 node_addr; + socklen_t nodelen; + char buffer[8192]; + + ret = sendto(sock, request, strlen(request), 0, (struct sockaddr *)client_addr, sizeof(struct sockaddr_in6)); + + if (ret < 0) { + perror("Error in sendto()"); + exit(EXIT_FAILURE); + } + + nodelen = sizeof(client_addr); + + struct timeval tv; + tv.tv_sec = 2; + tv.tv_usec = 0; + if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) { + perror("Error"); + } + + while (1) { + ret = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&node_addr, &nodelen); + if (ret < 0) + break; + + if (sse) + fputs("event: neighbour\ndata: ", stdout); + + fwrite(buffer, sizeof(char), ret, stdout); + + if (sse) + fputs("\n\n", stdout); + + fflush(stdout); + } + + return 0; +} + +int main(int argc, char **argv) { + int sock; + struct sockaddr_in6 client_addr = {}; + char *request_string = NULL; + struct in6_addr mgroup_addr; + + sock = socket(PF_INET6, SOCK_DGRAM, 0); + + if (sock < 0) { + perror("creating socket"); + exit(EXIT_FAILURE); + } + + client_addr.sin6_family = AF_INET6; + client_addr.sin6_addr = in6addr_any; + + opterr = 0; + + int port_set = 0; + int destination_set = 0; + bool sse = false; + + int c; + while ((c = getopt(argc, argv, "p:d:r:i:sh")) != -1) + switch (c) { + case 'p': + client_addr.sin6_port = htons(atoi(optarg)); + break; + case 'd': + if (!inet_pton(AF_INET6, optarg, &client_addr.sin6_addr)) { + perror("Invalid IPv6 address. This message will probably confuse you"); + exit(EXIT_FAILURE); + } + break; + case 'i': + client_addr.sin6_scope_id = if_nametoindex(optarg); + if (client_addr.sin6_scope_id == 0) { + perror("Can not use interface"); + exit(EXIT_FAILURE); + } + break; + case 'r': + request_string = optarg; + break; + case 's': + sse = true; + break; + case 'h': + usage(); + exit(EXIT_SUCCESS); + break; + default: + fprintf(stderr, "Invalid parameter %c ignored.\n", c); + } + + if (request_string == NULL) + error(EXIT_FAILURE, 0, "No request string supplied"); + + if (sse) + fputs("Content-Type: text/event-stream\n\n", stdout); + + request(sock, &client_addr, request_string, sse); + + if (sse) + fputs("event: eot\ndata: null\n\n", stdout); + + return EXIT_SUCCESS; +}