From 1784cd39324449ce8415a765ff93e1c33e8137ce Mon Sep 17 00:00:00 2001 From: Dave Akerman Date: Mon, 11 Mar 2019 17:56:33 +0000 Subject: [PATCH] V1.8.25 - UDP output of hostname, ip address and version number --- README.md | 5 +++++ gateway.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 272bbb2..06efbf9 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,11 @@ Many thanks to David Brooke for coding this feature and the AFC. Change History ============== +11/03/2019 - V1.8.25 +-------------------- + + UDP broadcast output of gateway hostname, IP address and version number. + 27/02/2019 - V1.8.24 -------------------- diff --git a/gateway.c b/gateway.c index 619dfda..cbc5af7 100644 --- a/gateway.c +++ b/gateway.c @@ -23,6 +23,10 @@ #include #include #include +#include +#include +#include +#include #include "urlencode.h" #include "base64.h" @@ -41,7 +45,7 @@ #include "udpclient.h" #include "lifo_buffer.h" -#define VERSION "V1.8.24" +#define VERSION "V1.8.25" bool run = TRUE; // RFM98 @@ -2398,6 +2402,55 @@ void displayChannel (int Channel) { } +char *Hostname(void) +{ + static char Buffer[80]; + + strcpy(Buffer, "PI"); + + gethostname(Buffer, sizeof(Buffer)); + + return Buffer; +} + +char *GetIPAddress(void) +{ + static char IPAddress[100]; + struct ifaddrs *ifap, *ifa; + struct sockaddr_in *sa; + char *addr; + + IPAddress[0] = '\0'; + + if (getifaddrs(&ifap) == 0) + { + for (ifa = ifap; ifa; ifa = ifa->ifa_next) + { + if (ifa->ifa_addr != NULL) + { + // Family is known (which it isn't for a VPN) + if (ifa->ifa_addr->sa_family==AF_INET) + { + // Exclude docker bridges + if (strstr(ifa->ifa_name, "docker") == NULL) + { + sa = (struct sockaddr_in *) ifa->ifa_addr; + addr = inet_ntoa(sa->sin_addr); + if (strcmp(addr, "127.0.0.1") != 0) + { + strcpy(IPAddress, addr); + } + } + } + } + } + } + + freeifaddrs(ifap); + + return IPAddress; +} + int main( int argc, char **argv ) { int ch; @@ -2596,6 +2649,7 @@ int main( int argc, char **argv ) if (LoopPeriod > 1000) { // Every 1 second + static int Seconds=55; time_t now; struct tm *tm; @@ -2681,6 +2735,15 @@ int main( int argc, char **argv ) } } } + + if (++Seconds >= 60) + { + char Message[200]; + + Seconds = 0; + sprintf(Message, "GATEWAY:HOST=%s,IP=%s,VER=%s\n", Hostname(), GetIPAddress(), VERSION); + UDPSend(Message, Config.UDPPort); + } } delay(MSPerLoop);