diff --git a/package/gluon-neighbour-info/src/gluon-neighbour-info.c b/package/gluon-neighbour-info/src/gluon-neighbour-info.c index 46e6324d..b2652bcc 100644 --- a/package/gluon-neighbour-info/src/gluon-neighbour-info.c +++ b/package/gluon-neighbour-info/src/gluon-neighbour-info.c @@ -36,13 +36,17 @@ #include void usage() { - puts("Usage: gluon-neighbour-info [-h] [-s] [-t ] -d -p -i -r "); + puts("Usage: gluon-neighbour-info [-h] [-s] [-l] [-c ] [-t ] -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(" -t timeout in seconds (default: 3)"); - puts(" -s output as server-sent events"); + puts(" -s output as server-sent events of type "); + puts(" or without type if is the empty string"); + puts(" -c only wait for at most replies"); + puts(" -l after timeout (or replies if -c is given),"); + puts(" send another request and loop forever"); puts(" -h this help\n"); } @@ -78,9 +82,10 @@ ssize_t recvtimeout(int socket, void *buffer, size_t length, int flags, struct t return ret; } -int request(const int sock, const struct sockaddr_in6 *client_addr, const char *request, bool sse, double timeout) { +int request(const int sock, const struct sockaddr_in6 *client_addr, const char *request, const char *sse, double timeout, unsigned int max_count) { ssize_t ret; char buffer[8192]; + unsigned int count = 0; ret = sendto(sock, request, strlen(request), 0, (struct sockaddr *)client_addr, sizeof(struct sockaddr_in6)); @@ -95,14 +100,17 @@ int request(const int sock, const struct sockaddr_in6 *client_addr, const char * getclock(&tv_offset); - while (1) { + do { ret = recvtimeout(sock, buffer, sizeof(buffer), 0, &tv_timeout, &tv_offset); if (ret < 0) break; - if (sse) - fputs("event: neighbour\ndata: ", stdout); + if (sse) { + if (sse[0] != '\0') + fprintf(stdout, "event: %s\n", sse); + fputs("data: ", stdout); + } fwrite(buffer, sizeof(char), ret, stdout); @@ -112,9 +120,13 @@ int request(const int sock, const struct sockaddr_in6 *client_addr, const char * fputs("\n", stdout); fflush(stdout); - } + count++; + } while (max_count == 0 || count < max_count); - return 0; + if ((max_count == 0 && count == 0) || count < max_count) + return EXIT_FAILURE; + else + return EXIT_SUCCESS; } int main(int argc, char **argv) { @@ -137,11 +149,14 @@ int main(int argc, char **argv) { int port_set = 0; int destination_set = 0; + unsigned int max_count = 0; double timeout = 3.0; - bool sse = false; + char *sse = NULL; + bool loop = false; + int ret = false; int c; - while ((c = getopt(argc, argv, "p:d:r:i:t:sh")) != -1) + while ((c = getopt(argc, argv, "p:d:r:i:t:s:c:lh")) != -1) switch (c) { case 'p': client_addr.sin6_port = htons(atoi(optarg)); @@ -166,7 +181,17 @@ int main(int argc, char **argv) { timeout = atof(optarg); break; case 's': - sse = true; + sse = optarg; + break; + case 'l': + loop = true; + break; + case 'c': + max_count = atoi(optarg); + if (max_count < 0) { + perror("Negative count not supported"); + exit(EXIT_FAILURE); + } break; case 'h': usage(); @@ -184,10 +209,12 @@ int main(int argc, char **argv) { if (sse) fputs("Content-Type: text/event-stream\n\n", stdout); - request(sock, &client_addr, request_string, sse, timeout); + do { + ret = request(sock, &client_addr, request_string, sse, timeout, max_count); + } while(loop); if (sse) fputs("event: eot\ndata: null\n\n", stdout); - return EXIT_SUCCESS; + return ret; } diff --git a/package/gluon-status-page-api/files/lib/gluon/status-page/www/cgi-bin/dyn/neighbours-nodeinfo b/package/gluon-status-page-api/files/lib/gluon/status-page/www/cgi-bin/dyn/neighbours-nodeinfo index 34c1204e..4420570e 100755 --- a/package/gluon-status-page-api/files/lib/gluon/status-page/www/cgi-bin/dyn/neighbours-nodeinfo +++ b/package/gluon-status-page-api/files/lib/gluon/status-page/www/cgi-bin/dyn/neighbours-nodeinfo @@ -4,4 +4,4 @@ echo 'Access-Control-Allow-Origin: *' batctl if | cut -d: -f1 | grep -qxF "$QUERY_STRING" || exit 1 -exec /usr/bin/gluon-neighbour-info -s -i "$QUERY_STRING" -d ff02::2:1001 -p 1001 -r nodeinfo +exec /usr/bin/gluon-neighbour-info -s neighbour -i "$QUERY_STRING" -d ff02::2:1001 -p 1001 -r nodeinfo