Skip to content

Commit

Permalink
Process IPv6 scope id
Browse files Browse the repository at this point in the history
Send data to the same interface we originally received from. This
allows the use of TAP devices for communication.
  • Loading branch information
HendrikVE committed Feb 3, 2022
1 parent 5979712 commit ffd7df6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
21 changes: 15 additions & 6 deletions MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#include "SensorNetwork.h"
#include "MQTTSNGWProcess.h"

//enough space for IPv6 + % + scopeId + : + port + brackets + \0
#define ADDRESS_SPRINT_BUFFER_SIZE (INET6_ADDRSTRLEN + 1 + 10 + 1 + 5 + 2 + 1)

//using namespace std;
using namespace MQTTSNGW;

Expand All @@ -59,6 +62,11 @@ uint16_t SensorNetAddress::getPortNo(void)
return _IpAddr.sin6_port;
}

uint32_t SensorNetAddress::getScopeId(void)
{
return _IpAddr.sin6_scope_id;
}

void SensorNetAddress::setAddress(struct sockaddr_in6 *IpAddr)
{
memcpy((void*) &_IpAddr, IpAddr, sizeof(_IpAddr));
Expand Down Expand Up @@ -122,6 +130,7 @@ char* SensorNetAddress::getAddress(void)
bool SensorNetAddress::isMatch(SensorNetAddress* addr)
{
return (this->_IpAddr.sin6_port == addr->_IpAddr.sin6_port)
&& (this->_IpAddr.sin6_scope_id == addr->_IpAddr.sin6_scope_id)
&& (memcmp(this->_IpAddr.sin6_addr.s6_addr, addr->_IpAddr.sin6_addr.s6_addr,
sizeof(this->_IpAddr.sin6_addr.s6_addr)) == 0);
}
Expand All @@ -135,8 +144,7 @@ SensorNetAddress& SensorNetAddress::operator =(SensorNetAddress& addr)

char* SensorNetAddress::sprint(char* buf)
{
sprintf(buf, "[%s]:", getAddress());
sprintf(buf + strlen(buf), "%d", ntohs(_IpAddr.sin6_port));
sprintf(buf, "[%s%%%d]:%d", getAddress(), getScopeId(), ntohs(getPortNo()));
return buf;
}

Expand Down Expand Up @@ -397,10 +405,11 @@ int UDPPort6::unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* add
memset(&dest, 0, sizeof(dest));
dest.sin6_family = AF_INET6;
dest.sin6_port = addr->getPortNo();
dest.sin6_scope_id = addr->getScopeId();
memcpy(dest.sin6_addr.s6_addr, (const void*) &addr->getIpAddress()->sin6_addr, sizeof(in6_addr));

#ifdef DEBUG_NW
char addrBuf[INET6_ADDRSTRLEN];
char addrBuf[ADDRESS_SPRINT_BUFFER_SIZE];
addr->sprint(addrBuf);
D_NWSTACK("sendto %s\n", addrBuf);
#endif
Expand All @@ -423,7 +432,7 @@ int UDPPort6::broadcast(const uint8_t* buf, uint32_t length)
memcpy(dest.sin6_addr.s6_addr, (const void*) &_grpAddr.getIpAddress()->sin6_addr, sizeof(in6_addr));

#ifdef DEBUG_NW
char addrBuf[INET6_ADDRSTRLEN];
char addrBuf[ADDRESS_SPRINT_BUFFER_SIZE];
addr->sprint(addrBuf);
D_NWSTACK("sendto %s\n", addrBuf);
#endif
Expand Down Expand Up @@ -473,9 +482,9 @@ int UDPPort6::recvfrom(int sockfd, uint8_t* buf, uint16_t len, uint8_t flags, Se
addr->setAddress(&sender);

#ifdef DEBUG_NW
char addrBuf[INET6_ADDRSTRLEN];
char addrBuf[ADDRESS_SPRINT_BUFFER_SIZE];
addr->sprint(addrBuf);
D_NWSTACK("sendto %s length = %d\n", addrBuf, status);
D_NWSTACK("recvfrom %s length = %d\n", addrBuf, status);
#endif
return status;
}
1 change: 1 addition & 0 deletions MQTTSNGateway/src/linux/udp6/SensorNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SensorNetAddress
int setAddress(const char* data);
uint16_t getPortNo(void);
sockaddr_in6* getIpAddress(void);
uint32_t getScopeId(void);
char* getAddress(void);
bool isMatch(SensorNetAddress* addr);
SensorNetAddress& operator =(SensorNetAddress& addr);
Expand Down

0 comments on commit ffd7df6

Please # to comment.