From 6cb7935027e36995527211eb6f589b4ee1121786 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 28 May 2019 16:31:52 +0900 Subject: [PATCH] BugFix of #151 Signed-off-by: tomoaki --- .cproject | 4 +- MQTTSNGateway/clients.conf | 2 +- .../src/linux/udp6/SensorNetwork.cpp | 42 +++++++++++++++---- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/.cproject b/.cproject index 78de229..3966019 100644 --- a/.cproject +++ b/.cproject @@ -133,7 +133,7 @@ - + @@ -277,7 +277,7 @@ - + diff --git a/MQTTSNGateway/clients.conf b/MQTTSNGateway/clients.conf index 0409cdc..a2c005c 100644 --- a/MQTTSNGateway/clients.conf +++ b/MQTTSNGateway/clients.conf @@ -22,7 +22,7 @@ # # Ex: # #Client List -# ClientId1,192.168.10.10:11200@ +# ClientId1,192.168.10.10:11200 # ClientID2,192.168.50.200:35000,unstableLine # ClientID3,192.168.200.50:40000,secureConnection # ClientID4,192.168.200.52:41000,unstableLine,secureConnection diff --git a/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp b/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp index e4f0039..b33525d 100644 --- a/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp +++ b/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp @@ -65,24 +65,52 @@ void SensorNetAddress::setAddress(struct sockaddr_in6 *IpAddr, uint16_t port) /** * convert Text data to SensorNetAddress - * @param buf is pointer of IP_Address:PortNo format text + * @param data is a IP_Address:PortNo format string * @return success = 0, Invalid format = -1 */ int SensorNetAddress::setAddress(string* data) { - const char *cstr = data->c_str(); - inet_pton(AF_INET6, cstr, &(_IpAddr.sin6_addr)); - return 0; + + size_t pos = data->find_last_of(":"); + + if ( pos != string::npos) + { + int portNo = 0; + string port = data->substr(pos + 1); + + if ( ( portNo = atoi(port.c_str()) ) > 0 ) + { + _portNo = htons(portNo); + + string ip = data->substr(1,pos - 1); + const char *cstr = ip.c_str(); + + if (inet_pton(AF_INET6, cstr, &(_IpAddr.sin6_addr)) == 1 ) + { + return 0; + } + } + } + _portNo = 0; + memset((void *)&_IpAddr,0,sizeof(_IpAddr)); + return -1; } + /** * convert Text data to SensorNetAddress - * @param buf is pointer of IP_Address:PortNo format text + * @param data is pointer of IP_Address format text * @return success = 0, Invalid format = -1 */ int SensorNetAddress::setAddress(const char* data) { - inet_pton(AF_INET6, data, &(_IpAddr.sin6_addr)); - return 0; + if ( inet_pton(AF_INET6, data, &(_IpAddr.sin6_addr)) == 1 ) + { + return 0; + } + else + { + return -1; + } } char* SensorNetAddress::getAddress(void)