From b8129d678109685a3585a23e1728ba2b367f0946 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 11 Feb 2020 18:30:33 +0900 Subject: [PATCH] Add a new function to set TTL/Hops of SensorNetwork(UDP and UDP6) #175 Parameters to set them in the gateway.conf, MulticastTTL=1 for UDP GatewayUDP6Hops=1 for UDP6 Signed-off-by: tomoaki --- MQTTSNGateway/README.md | 8 ++++++++ MQTTSNGateway/gateway.conf | 2 ++ MQTTSNGateway/src/MQTTSNGWVersion.h | 2 +- MQTTSNGateway/src/linux/udp/SensorNetwork.cpp | 20 ++++++++++++++++--- MQTTSNGateway/src/linux/udp/SensorNetwork.h | 4 ++-- .../src/linux/udp6/SensorNetwork.cpp | 18 +++++++++++++++-- MQTTSNGateway/src/linux/udp6/SensorNetwork.h | 4 ++-- 7 files changed, 48 insertions(+), 10 deletions(-) diff --git a/MQTTSNGateway/README.md b/MQTTSNGateway/README.md index 4f2b3c4..819ed0b 100644 --- a/MQTTSNGateway/README.md +++ b/MQTTSNGateway/README.md @@ -73,6 +73,14 @@ KeepAlive=900 GatewayPortNo=10000 MulticastIP=225.1.1.1 MulticastPortNo=1883 +MulticastTTL=1 + +# UDP6 +GatewayUDP6Bind=FFFF:FFFE::1 +GatewayUDP6Port=10000 +GatewayUDP6Broadcast=FF02::1 +GatewayUDP6If=wpan0 +GatewayUDP6Hops=1 # XBee Baudrate=38400 diff --git a/MQTTSNGateway/gateway.conf b/MQTTSNGateway/gateway.conf index d01903a..73f110e 100644 --- a/MQTTSNGateway/gateway.conf +++ b/MQTTSNGateway/gateway.conf @@ -49,12 +49,14 @@ KeepAlive=900 GatewayPortNo=10000 MulticastIP=225.1.1.1 MulticastPortNo=1883 +MulticastTTL=1 # UDP6 GatewayUDP6Bind=FFFF:FFFE::1 GatewayUDP6Port=10000 GatewayUDP6Broadcast=FF02::1 GatewayUDP6If=wpan0 +GatewayUDP6Hops=1 # XBee Baudrate=38400 diff --git a/MQTTSNGateway/src/MQTTSNGWVersion.h b/MQTTSNGateway/src/MQTTSNGWVersion.h index ff9d6e1..9d68241 100644 --- a/MQTTSNGateway/src/MQTTSNGWVersion.h +++ b/MQTTSNGateway/src/MQTTSNGWVersion.h @@ -17,6 +17,6 @@ #ifndef MQTTSNGWVERSION_H_IN_ #define MQTTSNGWVERSION_H_IN_ -#define PAHO_GATEWAY_VERSION "1.3.1" +#define PAHO_GATEWAY_VERSION "1.3.2" #endif /* MQTTSNGWVERSION_H_IN_ */ diff --git a/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp b/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp index 6f08481..5ca191d 100644 --- a/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp +++ b/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp @@ -185,7 +185,7 @@ int SensorNetwork::initialize(void) uint16_t multicastPortNo = 0; uint16_t unicastPortNo = 0; string ip; - + unsigned int ttl = 1; /* * theProcess->getParam( ) copies * a text specified by "Key" into param[] from the Gateway.conf @@ -216,9 +216,15 @@ int SensorNetwork::initialize(void) _description += " Gateway Port "; _description += param; } + if (theProcess->getParam("MulticastTTL", param) == 0) + { + ttl = atoi(param); + _description += " TTL: "; + _description += param; + } /* Prepare UDP sockets */ - return UDPPort::open(ip.c_str(), multicastPortNo, unicastPortNo); + return UDPPort::open(ip.c_str(), multicastPortNo, unicastPortNo, ttl); } const char* SensorNetwork::getDescription(void) @@ -261,7 +267,7 @@ void UDPPort::close(void) } } -int UDPPort::open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortNo) +int UDPPort::open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortNo, unsigned int ttl) { char loopch = 0; const int reuse = 1; @@ -275,6 +281,7 @@ int UDPPort::open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortN uint32_t ip = inet_addr(ipAddress); _grpAddr.setAddress(ip, htons(multiPortNo)); _clientAddr.setAddress(ip, htons(uniPortNo)); + _ttl = ttl; /*------ Create unicast socket --------*/ _sockfdUnicast = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); @@ -342,6 +349,13 @@ int UDPPort::open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortN return -1; } + if (setsockopt(_sockfdMulticast, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,sizeof(ttl)) < 0) + { + D_NWSTACK("error Multicast IP_ADD_MEMBERSHIP in UDPPort::open\n"); + close(); + return -1; + } + if (setsockopt(_sockfdUnicast, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) { D_NWSTACK("error Unicast IP_ADD_MEMBERSHIP in UDPPort::open\n"); diff --git a/MQTTSNGateway/src/linux/udp/SensorNetwork.h b/MQTTSNGateway/src/linux/udp/SensorNetwork.h index 07967f8..735a3c5 100644 --- a/MQTTSNGateway/src/linux/udp/SensorNetwork.h +++ b/MQTTSNGateway/src/linux/udp/SensorNetwork.h @@ -60,7 +60,7 @@ public: UDPPort(); virtual ~UDPPort(); - int open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortNo); + int open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortNo, unsigned int hops); void close(void); int unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* sendToAddr); int broadcast(const uint8_t* buf, uint32_t length); @@ -76,7 +76,7 @@ private: SensorNetAddress _grpAddr; SensorNetAddress _clientAddr; bool _disconReq; - + unsigned int _ttl; }; /*=========================================== diff --git a/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp b/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp index 11affaa..8137a1c 100644 --- a/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp +++ b/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp @@ -175,6 +175,7 @@ int SensorNetwork::initialize(void) string ip; string broadcast; string interface; + unsigned int hops = 1; if (theProcess->getParam("GatewayUDP6Bind", param) == 0) { @@ -200,8 +201,14 @@ int SensorNetwork::initialize(void) _description += " Interface: "; _description += param; } + if (theProcess->getParam("GatewayUDP6Hops", param) == 0) + { + hops = atoi(param); + _description += " Hops: "; + _description += param; + } - return UDPPort6::open(ip.c_str(), unicastPortNo, broadcast.c_str(), interface.c_str()); + return UDPPort6::open(ip.c_str(), unicastPortNo, broadcast.c_str(), interface.c_str(), hops); } const char* SensorNetwork::getDescription(void) @@ -244,7 +251,7 @@ void UDPPort6::close(void) } } -int UDPPort6::open(const char* ipAddress, uint16_t uniPortNo, const char* broadcastAddr, const char* interfaceName) +int UDPPort6::open(const char* ipAddress, uint16_t uniPortNo, const char* broadcastAddr, const char* interfaceName, unsigned int hops) { struct addrinfo hints, *res; int errnu; @@ -291,8 +298,15 @@ int UDPPort6::open(const char* ipAddress, uint16_t uniPortNo, const char* broadc WRITELOG("UDP6::open - limit IPv6: %s",strerror(errnu)); return errnu; } + errnu = setsockopt(_sockfdMulticast, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops,sizeof(hops)); + if(errnu <0) + { + WRITELOG("UDP6::open - limit HOPS: %s",strerror(errnu)); + return errnu; + } _uniPortNo = uniPortNo; + _hops = hops; freeaddrinfo(res); //init the structs for getaddrinfo diff --git a/MQTTSNGateway/src/linux/udp6/SensorNetwork.h b/MQTTSNGateway/src/linux/udp6/SensorNetwork.h index 62c297f..1dbbe5c 100644 --- a/MQTTSNGateway/src/linux/udp6/SensorNetwork.h +++ b/MQTTSNGateway/src/linux/udp6/SensorNetwork.h @@ -66,7 +66,7 @@ public: UDPPort6(); virtual ~UDPPort6(); - int open(const char* ipAddress, uint16_t uniPortNo, const char* broadcastAddr, const char* interfaceName); + int open(const char* ipAddress, uint16_t uniPortNo, const char* broadcastAddr, const char* interfaceName, unsigned int ttl); void close(void); int unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* sendToAddr); int broadcast(const uint8_t* buf, uint32_t length); @@ -84,7 +84,7 @@ private: SensorNetAddress _clientAddr; uint16_t _uniPortNo; bool _disconReq; - + unsigned int _ttl; }; /*===========================================