mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-15 16:36:52 +01:00
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:
@@ -73,6 +73,14 @@ KeepAlive=900
|
|||||||
GatewayPortNo=10000
|
GatewayPortNo=10000
|
||||||
MulticastIP=225.1.1.1
|
MulticastIP=225.1.1.1
|
||||||
MulticastPortNo=1883
|
MulticastPortNo=1883
|
||||||
|
MulticastTTL=1
|
||||||
|
|
||||||
|
# UDP6
|
||||||
|
GatewayUDP6Bind=FFFF:FFFE::1
|
||||||
|
GatewayUDP6Port=10000
|
||||||
|
GatewayUDP6Broadcast=FF02::1
|
||||||
|
GatewayUDP6If=wpan0
|
||||||
|
GatewayUDP6Hops=1
|
||||||
|
|
||||||
# XBee
|
# XBee
|
||||||
Baudrate=38400
|
Baudrate=38400
|
||||||
|
|||||||
@@ -49,12 +49,14 @@ KeepAlive=900
|
|||||||
GatewayPortNo=10000
|
GatewayPortNo=10000
|
||||||
MulticastIP=225.1.1.1
|
MulticastIP=225.1.1.1
|
||||||
MulticastPortNo=1883
|
MulticastPortNo=1883
|
||||||
|
MulticastTTL=1
|
||||||
|
|
||||||
# UDP6
|
# UDP6
|
||||||
GatewayUDP6Bind=FFFF:FFFE::1
|
GatewayUDP6Bind=FFFF:FFFE::1
|
||||||
GatewayUDP6Port=10000
|
GatewayUDP6Port=10000
|
||||||
GatewayUDP6Broadcast=FF02::1
|
GatewayUDP6Broadcast=FF02::1
|
||||||
GatewayUDP6If=wpan0
|
GatewayUDP6If=wpan0
|
||||||
|
GatewayUDP6Hops=1
|
||||||
|
|
||||||
# XBee
|
# XBee
|
||||||
Baudrate=38400
|
Baudrate=38400
|
||||||
|
|||||||
@@ -17,6 +17,6 @@
|
|||||||
#ifndef MQTTSNGWVERSION_H_IN_
|
#ifndef MQTTSNGWVERSION_H_IN_
|
||||||
#define 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_ */
|
#endif /* MQTTSNGWVERSION_H_IN_ */
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ int SensorNetwork::initialize(void)
|
|||||||
uint16_t multicastPortNo = 0;
|
uint16_t multicastPortNo = 0;
|
||||||
uint16_t unicastPortNo = 0;
|
uint16_t unicastPortNo = 0;
|
||||||
string ip;
|
string ip;
|
||||||
|
unsigned int ttl = 1;
|
||||||
/*
|
/*
|
||||||
* theProcess->getParam( ) copies
|
* theProcess->getParam( ) copies
|
||||||
* a text specified by "Key" into param[] from the Gateway.conf
|
* a text specified by "Key" into param[] from the Gateway.conf
|
||||||
@@ -216,9 +216,15 @@ int SensorNetwork::initialize(void)
|
|||||||
_description += " Gateway Port ";
|
_description += " Gateway Port ";
|
||||||
_description += param;
|
_description += param;
|
||||||
}
|
}
|
||||||
|
if (theProcess->getParam("MulticastTTL", param) == 0)
|
||||||
|
{
|
||||||
|
ttl = atoi(param);
|
||||||
|
_description += " TTL: ";
|
||||||
|
_description += param;
|
||||||
|
}
|
||||||
|
|
||||||
/* Prepare UDP sockets */
|
/* 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)
|
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;
|
char loopch = 0;
|
||||||
const int reuse = 1;
|
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);
|
uint32_t ip = inet_addr(ipAddress);
|
||||||
_grpAddr.setAddress(ip, htons(multiPortNo));
|
_grpAddr.setAddress(ip, htons(multiPortNo));
|
||||||
_clientAddr.setAddress(ip, htons(uniPortNo));
|
_clientAddr.setAddress(ip, htons(uniPortNo));
|
||||||
|
_ttl = ttl;
|
||||||
|
|
||||||
/*------ Create unicast socket --------*/
|
/*------ Create unicast socket --------*/
|
||||||
_sockfdUnicast = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
_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;
|
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)
|
if (setsockopt(_sockfdUnicast, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
|
||||||
{
|
{
|
||||||
D_NWSTACK("error Unicast IP_ADD_MEMBERSHIP in UDPPort::open\n");
|
D_NWSTACK("error Unicast IP_ADD_MEMBERSHIP in UDPPort::open\n");
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ public:
|
|||||||
UDPPort();
|
UDPPort();
|
||||||
virtual ~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);
|
void close(void);
|
||||||
int unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* sendToAddr);
|
int unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* sendToAddr);
|
||||||
int broadcast(const uint8_t* buf, uint32_t length);
|
int broadcast(const uint8_t* buf, uint32_t length);
|
||||||
@@ -76,7 +76,7 @@ private:
|
|||||||
SensorNetAddress _grpAddr;
|
SensorNetAddress _grpAddr;
|
||||||
SensorNetAddress _clientAddr;
|
SensorNetAddress _clientAddr;
|
||||||
bool _disconReq;
|
bool _disconReq;
|
||||||
|
unsigned int _ttl;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*===========================================
|
/*===========================================
|
||||||
|
|||||||
@@ -175,6 +175,7 @@ int SensorNetwork::initialize(void)
|
|||||||
string ip;
|
string ip;
|
||||||
string broadcast;
|
string broadcast;
|
||||||
string interface;
|
string interface;
|
||||||
|
unsigned int hops = 1;
|
||||||
|
|
||||||
if (theProcess->getParam("GatewayUDP6Bind", param) == 0)
|
if (theProcess->getParam("GatewayUDP6Bind", param) == 0)
|
||||||
{
|
{
|
||||||
@@ -200,8 +201,14 @@ int SensorNetwork::initialize(void)
|
|||||||
_description += " Interface: ";
|
_description += " Interface: ";
|
||||||
_description += param;
|
_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)
|
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;
|
struct addrinfo hints, *res;
|
||||||
int errnu;
|
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));
|
WRITELOG("UDP6::open - limit IPv6: %s",strerror(errnu));
|
||||||
return 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;
|
_uniPortNo = uniPortNo;
|
||||||
|
_hops = hops;
|
||||||
freeaddrinfo(res);
|
freeaddrinfo(res);
|
||||||
|
|
||||||
//init the structs for getaddrinfo
|
//init the structs for getaddrinfo
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ public:
|
|||||||
UDPPort6();
|
UDPPort6();
|
||||||
virtual ~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);
|
void close(void);
|
||||||
int unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* sendToAddr);
|
int unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* sendToAddr);
|
||||||
int broadcast(const uint8_t* buf, uint32_t length);
|
int broadcast(const uint8_t* buf, uint32_t length);
|
||||||
@@ -84,7 +84,7 @@ private:
|
|||||||
SensorNetAddress _clientAddr;
|
SensorNetAddress _clientAddr;
|
||||||
uint16_t _uniPortNo;
|
uint16_t _uniPortNo;
|
||||||
bool _disconReq;
|
bool _disconReq;
|
||||||
|
unsigned int _ttl;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*===========================================
|
/*===========================================
|
||||||
|
|||||||
Reference in New Issue
Block a user