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 <tomoaki@tomy-tech.com>
This commit is contained in:
tomoaki
2020-02-11 18:30:33 +09:00
parent 768123b76f
commit b8129d6781
7 changed files with 48 additions and 10 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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_ */

View File

@@ -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");

View File

@@ -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;
};
/*===========================================

View File

@@ -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

View File

@@ -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;
};
/*===========================================