mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-15 08:26:51 +01:00
@@ -202,14 +202,19 @@ int LGwProxy::getConnectResponce(void)
|
|||||||
_gwId = _mqttsnMsg[1];
|
_gwId = _mqttsnMsg[1];
|
||||||
|
|
||||||
#if defined(DTLS) || defined(DTLS6)
|
#if defined(DTLS) || defined(DTLS6)
|
||||||
if (_network.sslConnect() < 0)
|
for (int i = 0; i < MQTTSN_RETRY_COUNT; i++)
|
||||||
{
|
{
|
||||||
DISPLAY(
|
if (_network.sslConnect() > 0)
|
||||||
"\033[0m\033[0;32m\n\nLGwProxy::getConnectResponce Can't connect the Gateway via SSL.\033[0m\033[0;37m\n\n");
|
{
|
||||||
return 0;
|
_status = GW_CONNECTING;
|
||||||
|
DISPLAY(
|
||||||
|
"\033[0m\033[0;32m\n\nLGwProxy::getConnectResponce Can't connect the Gateway via SSL.\033[0m\033[0;37m\n\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#else
|
||||||
_status = GW_CONNECTING;
|
_status = GW_CONNECTING;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else if (_mqttsnMsg[0] == MQTTSN_TYPE_WILLTOPICREQ && _status == GW_WAIT_WILLTOPICREQ)
|
else if (_mqttsnMsg[0] == MQTTSN_TYPE_WILLTOPICREQ && _status == GW_WAIT_WILLTOPICREQ)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -52,15 +52,15 @@ int main(int argc, char** argv)
|
|||||||
printf("\n%s", PAHO_COPYRIGHT4);
|
printf("\n%s", PAHO_COPYRIGHT4);
|
||||||
printf("\n%s", PAHO_COPYRIGHT0);
|
printf("\n%s", PAHO_COPYRIGHT0);
|
||||||
#if defined(UDP)
|
#if defined(UDP)
|
||||||
printf(" UDP\n");
|
printf("UDP ClientId:%s PortNo:%d\n", theNetcon.clientId, theNetcon.uPortNo);
|
||||||
#elif defined(UDP6)
|
#elif defined(UDP6)
|
||||||
printf(" UDP6\n");
|
printf("UDP6 ClientId:%s PortNo:%d\n", theNetcon.clientId, theNetcon.uPortNo);
|
||||||
#elif defined(RFCOMM)
|
|
||||||
printf(" RFCOMM\n");
|
|
||||||
#elif defined(DTLS)
|
#elif defined(DTLS)
|
||||||
printf(" DTLS\n");
|
printf("DTLS ClientId:%s PortNo:%d\n", theNetcon.clientId, theNetcon.uPortNo);
|
||||||
#elif defined(DTLS6)
|
#elif defined(DTLS6)
|
||||||
printf(" DTLS6\n");
|
printf("DTLS6 ClientId:%s PortNo:%d\n", theNetcon.clientId, theNetcon.uPortNo);
|
||||||
|
#elif defined(RFCOMM)
|
||||||
|
printf("RFCOMM ClientId:%s channel:%d\n", theNetcon.clientId, theNetcon.channel);
|
||||||
#else
|
#else
|
||||||
printf("\n");
|
printf("\n");
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -471,6 +471,7 @@ int LDtlsPort::sslConnect(uint32_t ipAddress, in_port_t portNo)
|
|||||||
int reuse = 1;
|
int reuse = 1;
|
||||||
if (_ssl != 0)
|
if (_ssl != 0)
|
||||||
{
|
{
|
||||||
|
D_NWLOG("LDtlsPort::sslConnect SSL exists.\n");
|
||||||
SSL_shutdown(_ssl);
|
SSL_shutdown(_ssl);
|
||||||
SSL_free(_ssl);
|
SSL_free(_ssl);
|
||||||
_sockfdSsl = 0;
|
_sockfdSsl = 0;
|
||||||
@@ -489,7 +490,7 @@ int LDtlsPort::sslConnect(uint32_t ipAddress, in_port_t portNo)
|
|||||||
D_NWLOG("LDtlsPort::sslConnect Can't create a socket\n");
|
D_NWLOG("LDtlsPort::sslConnect Can't create a socket\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
setsockopt(_sockfdSsl, SOL_SOCKET, SO_REUSEADDR || SO_REUSEPORT, &reuse, sizeof(reuse));
|
setsockopt(_sockfdSsl, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
|
||||||
|
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
@@ -497,6 +498,8 @@ int LDtlsPort::sslConnect(uint32_t ipAddress, in_port_t portNo)
|
|||||||
addr.sin_addr.s_addr = INADDR_ANY;
|
addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
if (::bind(_sockfdSsl, (struct sockaddr*) &addr, sizeof(addr)) < 0)
|
if (::bind(_sockfdSsl, (struct sockaddr*) &addr, sizeof(addr)) < 0)
|
||||||
{
|
{
|
||||||
|
::close(_sockfdSsl);
|
||||||
|
_sockfdSsl = 0;
|
||||||
D_NWLOG("LDtlsPort::sslConnect Can't bind a socket\n");
|
D_NWLOG("LDtlsPort::sslConnect Can't bind a socket\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -514,6 +517,12 @@ int LDtlsPort::sslConnect(uint32_t ipAddress, in_port_t portNo)
|
|||||||
SSL_set_bio(_ssl, cbio, cbio);
|
SSL_set_bio(_ssl, cbio, cbio);
|
||||||
|
|
||||||
D_NWLOG("LDtlsPort::sslConnect connect to %-15s:%-6u\n", inet_ntoa(dest.sin_addr), htons(dest.sin_port));
|
D_NWLOG("LDtlsPort::sslConnect connect to %-15s:%-6u\n", inet_ntoa(dest.sin_addr), htons(dest.sin_port));
|
||||||
|
|
||||||
|
timeval timeout;
|
||||||
|
timeout.tv_sec = 5;
|
||||||
|
timeout.tv_usec = 0;
|
||||||
|
BIO_ctrl(cbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
|
||||||
|
|
||||||
int stat = SSL_connect(_ssl);
|
int stat = SSL_connect(_ssl);
|
||||||
if (stat != 1)
|
if (stat != 1)
|
||||||
{
|
{
|
||||||
@@ -522,6 +531,7 @@ int LDtlsPort::sslConnect(uint32_t ipAddress, in_port_t portNo)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
rc = 1;
|
||||||
D_NWLOG("SSL connected\n");
|
D_NWLOG("SSL connected\n");
|
||||||
}
|
}
|
||||||
return rc;
|
return rc;
|
||||||
|
|||||||
@@ -77,7 +77,6 @@ MulticastHops=1
|
|||||||
|
|
||||||
DtlsCertsKey=/etc/ssl/certs/gateway.pem
|
DtlsCertsKey=/etc/ssl/certs/gateway.pem
|
||||||
DtlsPrivKey=/etc/ssl/private/privkey.pem
|
DtlsPrivKey=/etc/ssl/private/privkey.pem
|
||||||
DtlsSSLPortNo=10001
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# XBee
|
# XBee
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace MQTTSNGW
|
|||||||
/*=================================
|
/*=================================
|
||||||
* Starting prompt
|
* Starting prompt
|
||||||
==================================*/
|
==================================*/
|
||||||
#define PAHO_COPYRIGHT0 " * MQTT-SN Gateway"
|
#define PAHO_COPYRIGHT0 " * "
|
||||||
#define PAHO_COPYRIGHT1 " * Part of Project Paho in Eclipse"
|
#define PAHO_COPYRIGHT1 " * Part of Project Paho in Eclipse"
|
||||||
#define PAHO_COPYRIGHT2 " * (http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt-sn.embedded-c.git/)"
|
#define PAHO_COPYRIGHT2 " * (http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt-sn.embedded-c.git/)"
|
||||||
#define PAHO_COPYRIGHT3 " * Author : Tomoaki YAMAGUCHI"
|
#define PAHO_COPYRIGHT3 " * Author : Tomoaki YAMAGUCHI"
|
||||||
|
|||||||
@@ -405,8 +405,6 @@ int Connections::getSockClient(int index)
|
|||||||
|
|
||||||
void Connections::close(int index)
|
void Connections::close(int index)
|
||||||
{
|
{
|
||||||
_mutex.lock();
|
|
||||||
|
|
||||||
int idx = index + POLL_SSL;
|
int idx = index + POLL_SSL;
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
int sock = _pollfds[idx].fd;
|
int sock = _pollfds[idx].fd;
|
||||||
@@ -415,9 +413,9 @@ void Connections::close(int index)
|
|||||||
|
|
||||||
for (; idx < _numfds; idx++)
|
for (; idx < _numfds; idx++)
|
||||||
{
|
{
|
||||||
_ssls[index] = _ssls[idx + 1];
|
_ssls[idx] = _ssls[idx + 1];
|
||||||
_pollfds[index] = _pollfds[idx + 1];
|
_pollfds[idx] = _pollfds[idx + 1];
|
||||||
_clientAddr[index] = _clientAddr[idx + 1];
|
_clientAddr[idx] = _clientAddr[idx + 1];
|
||||||
|
|
||||||
if (_ssls[idx + 1] == 0)
|
if (_ssls[idx + 1] == 0)
|
||||||
{
|
{
|
||||||
@@ -434,7 +432,7 @@ void Connections::close(int index)
|
|||||||
}
|
}
|
||||||
if (sock > 0)
|
if (sock > 0)
|
||||||
{
|
{
|
||||||
close(sock);
|
::close(sock);
|
||||||
}
|
}
|
||||||
if (addr != nullptr)
|
if (addr != nullptr)
|
||||||
{
|
{
|
||||||
@@ -498,9 +496,9 @@ int Connections::searchClient(SensorNetAddress *addr)
|
|||||||
read( ) is used by MQTTSNPacket::recv( )
|
read( ) is used by MQTTSNPacket::recv( )
|
||||||
|
|
||||||
================================================================*/
|
================================================================*/
|
||||||
#define PACKET_CLIENTHELLO 10000
|
#define DTLS_CLIENTHELLO 22
|
||||||
#define PACKET_APPL 10001
|
#define DTLS_APPL 23
|
||||||
#define PACKET_OTHERS 10002
|
#define DTLS_OTHERS 100
|
||||||
|
|
||||||
/* Certificate verification. Returns 1 if trusted, else 0 */
|
/* Certificate verification. Returns 1 if trusted, else 0 */
|
||||||
int verify_cert(int ok, X509_STORE_CTX *ctx);
|
int verify_cert(int ok, X509_STORE_CTX *ctx);
|
||||||
@@ -607,7 +605,7 @@ int SensorNetwork::read(uint8_t *buf, uint16_t bufLen)
|
|||||||
struct sockaddr_in6 s6;
|
struct sockaddr_in6 s6;
|
||||||
} client_addr;
|
} client_addr;
|
||||||
|
|
||||||
// Ccheck sockets
|
// Check POLL_IN
|
||||||
int cnt = _conns->poll(2000); // Timeout 2secs
|
int cnt = _conns->poll(2000); // Timeout 2secs
|
||||||
if (cnt == 0)
|
if (cnt == 0)
|
||||||
{
|
{
|
||||||
@@ -668,13 +666,9 @@ ListenClient_hello:
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (clientIndex != -1)
|
// Handle client connection
|
||||||
// {
|
|
||||||
// _conns->close(clientIndex);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// SSL Accept
|
|
||||||
#ifndef DTLS6
|
#ifndef DTLS6
|
||||||
|
// DTLS over IPv4
|
||||||
int client_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
int client_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
setsockopt(client_fd, SOL_SOCKET, SO_REUSEADDR, (const void*) &optval, sizeof(optval));
|
setsockopt(client_fd, SOL_SOCKET, SO_REUSEADDR, (const void*) &optval, sizeof(optval));
|
||||||
// Bind to Dtls PortNo
|
// Bind to Dtls PortNo
|
||||||
@@ -695,8 +689,7 @@ ListenClient_hello:
|
|||||||
BIO_set_fd(cbio, client_fd, BIO_NOCLOSE);
|
BIO_set_fd(cbio, client_fd, BIO_NOCLOSE);
|
||||||
BIO_ctrl(cbio, BIO_CTRL_DGRAM_SET_CONNECTED, 0, &client_addr);
|
BIO_ctrl(cbio, BIO_CTRL_DGRAM_SET_CONNECTED, 0, &client_addr);
|
||||||
|
|
||||||
D_NWSTACK("Accept SSL\n");
|
// Finish handshake
|
||||||
|
|
||||||
int ret = SSL_accept(ssl);
|
int ret = SSL_accept(ssl);
|
||||||
if (ret <= 0)
|
if (ret <= 0)
|
||||||
{
|
{
|
||||||
@@ -708,17 +701,17 @@ ListenClient_hello:
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
||||||
// add ssl & socket to Connections instance
|
// add ssl & socket to Connections instance
|
||||||
int index = _conns->addClientSSL(ssl, client_fd);
|
int index = _conns->addClientSSL(ssl, client_fd);
|
||||||
|
|
||||||
// save SensorNetworkAddress of Client
|
// save SensorNetworkAddress of Client
|
||||||
client.setIndex(index);
|
client.setIndex(index);
|
||||||
_senderAddr = &client;
|
_senderAddr = &client;
|
||||||
|
#ifdef DEBUG_NW
|
||||||
char clientaddrBuf[128];
|
char clientaddrBuf[128];
|
||||||
client.sprint(clientaddrBuf);
|
client.sprint(clientaddrBuf);
|
||||||
WRITELOG("Client %s SSL Accepted. idx=%d\n", clientaddrBuf, index);
|
D_NWSTACK("Client %s SSL Accepted. idx=%d\n", clientaddrBuf, index);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
@@ -741,15 +734,21 @@ ListenClient_hello:
|
|||||||
|
|
||||||
if (dtls > 0)
|
if (dtls > 0)
|
||||||
{
|
{
|
||||||
D_NWSTACK("DTLT type=%d\n", dtls);
|
if (dtls == DTLS_CLIENTHELLO)
|
||||||
if (dtls == PACKET_CLIENTHELLO)
|
|
||||||
{
|
{
|
||||||
|
// Received packet is ClientHello
|
||||||
#ifdef DEBUG_NW
|
#ifdef DEBUG_NW
|
||||||
char clientaddrBuf[128];
|
char clientaddrBuf[128];
|
||||||
client.sprint(clientaddrBuf);
|
client.sprint(clientaddrBuf);
|
||||||
D_NWSTACK("Client %s SSL reconnect. idx=%d\n", clientaddrBuf, i);
|
D_NWSTACK("Client %s SSL reconnect. idx=%d\n", clientaddrBuf, i);
|
||||||
#endif
|
#endif
|
||||||
|
// Delete current connection.
|
||||||
clientIndex = i;
|
clientIndex = i;
|
||||||
|
D_NWSTACK("Close current connections\n");
|
||||||
|
_mutex.unlock();
|
||||||
|
_conns->close(clientIndex); // DEBUG
|
||||||
|
return 0;
|
||||||
|
|
||||||
sockListen = _conns->getSockClient(i);
|
sockListen = _conns->getSockClient(i);
|
||||||
goto ListenClient_hello;
|
goto ListenClient_hello;
|
||||||
}
|
}
|
||||||
@@ -759,12 +758,13 @@ ListenClient_hello:
|
|||||||
int len = SSL_read_ex(ssl, (void*) buf, (size_t) bufLen, &recvlen);
|
int len = SSL_read_ex(ssl, (void*) buf, (size_t) bufLen, &recvlen);
|
||||||
if (SSL_get_error(ssl, len) >= 0)
|
if (SSL_get_error(ssl, len) >= 0)
|
||||||
{
|
{
|
||||||
_senderAddr = &client;
|
#ifdef DEBUG_NW
|
||||||
_senderAddr->setIndex(i);
|
|
||||||
|
|
||||||
char clientaddrBuf[128];
|
char clientaddrBuf[128];
|
||||||
client.sprint(clientaddrBuf);
|
client.sprint(clientaddrBuf);
|
||||||
D_NWSTACK("Client %s SSL Accepted. idx=%d\n", clientaddrBuf, i);
|
D_NWSTACK("Client %s SSL Accepted. idx=%d\n", clientaddrBuf, i);
|
||||||
|
#endif
|
||||||
|
_senderAddr = &client;
|
||||||
|
_senderAddr->setIndex(i);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -787,7 +787,6 @@ void SensorNetwork::initialize(void)
|
|||||||
char errmsg[256];
|
char errmsg[256];
|
||||||
uint16_t multicastPortNo = 0;
|
uint16_t multicastPortNo = 0;
|
||||||
uint16_t unicastPortNo = 0;
|
uint16_t unicastPortNo = 0;
|
||||||
uint16_t dtlsPortNo = 0;
|
|
||||||
|
|
||||||
SensorNetAddress add;
|
SensorNetAddress add;
|
||||||
sockaddr_in6 soadd;
|
sockaddr_in6 soadd;
|
||||||
@@ -815,12 +814,6 @@ void SensorNetwork::initialize(void)
|
|||||||
_description += ", Gateway PortNo:";
|
_description += ", Gateway PortNo:";
|
||||||
_description += param;
|
_description += param;
|
||||||
}
|
}
|
||||||
if (theProcess->getParam("DtlsPortNo", param) == 0)
|
|
||||||
{
|
|
||||||
dtlsPortNo = atoi(param);
|
|
||||||
_description += ", SSL PortNo:";
|
|
||||||
_description += param;
|
|
||||||
}
|
|
||||||
if (theProcess->getParam("MulticastTTL", param) == 0)
|
if (theProcess->getParam("MulticastTTL", param) == 0)
|
||||||
{
|
{
|
||||||
ttl = atoi(param);
|
ttl = atoi(param);
|
||||||
@@ -850,12 +843,6 @@ void SensorNetwork::initialize(void)
|
|||||||
_description += ", Gateway PortNo:";
|
_description += ", Gateway PortNo:";
|
||||||
_description += param;
|
_description += param;
|
||||||
}
|
}
|
||||||
if (theProcess->getParam("DtlsPortNo", param) == 0)
|
|
||||||
{
|
|
||||||
dtlsPortNo = atoi(param);
|
|
||||||
_description += ", SSL PortNo:";
|
|
||||||
_description += param;
|
|
||||||
}
|
|
||||||
if (theProcess->getParam("MulticastIPv6If", param) == 0)
|
if (theProcess->getParam("MulticastIPv6If", param) == 0)
|
||||||
{
|
{
|
||||||
interface = param;
|
interface = param;
|
||||||
@@ -914,12 +901,12 @@ void SensorNetwork::initialize(void)
|
|||||||
|
|
||||||
/* Prepare UDP and UDP6 sockets for Multicasting and unicasting */
|
/* Prepare UDP and UDP6 sockets for Multicasting and unicasting */
|
||||||
#ifndef DTLS6
|
#ifndef DTLS6
|
||||||
if (openV4(&ip, multicastPortNo, unicastPortNo, dtlsPortNo, ttl) < 0)
|
if (openV4(&ip, multicastPortNo, unicastPortNo, ttl) < 0)
|
||||||
{
|
{
|
||||||
throw EXCEPTION("Can't open a UDP4", errno);
|
throw EXCEPTION("Can't open a UDP4", errno);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
if (openV6(&ip6, &interface, multicastPortNo, unicastPortNo, dtlsPortNo, hops) < 0)
|
if (openV6(&ip6, &interface, multicastPortNo, unicastPortNo, hops) < 0)
|
||||||
{
|
{
|
||||||
throw EXCEPTION("Can't open a UDP6", errno);
|
throw EXCEPTION("Can't open a UDP6", errno);
|
||||||
}
|
}
|
||||||
@@ -936,7 +923,7 @@ SensorNetAddress* SensorNetwork::getSenderAddress(void)
|
|||||||
return _senderAddr;
|
return _senderAddr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SensorNetwork::openV4(string *ipAddress, uint16_t multiPortNo, uint16_t uniPortNo, uint16_t dtlsPortNo, uint32_t ttl)
|
int SensorNetwork::openV4(string *ipAddress, uint16_t multiPortNo, uint16_t uniPortNo, uint32_t ttl)
|
||||||
{
|
{
|
||||||
int optval = 0;
|
int optval = 0;
|
||||||
int rc = -1;
|
int rc = -1;
|
||||||
@@ -961,23 +948,17 @@ int SensorNetwork::openV4(string *ipAddress, uint16_t multiPortNo, uint16_t uniP
|
|||||||
optval = 1;
|
optval = 1;
|
||||||
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
|
||||||
|
|
||||||
sockaddr_in addr;
|
_serverAddr4.sin_family = AF_INET;
|
||||||
addr.sin_family = AF_INET;
|
_serverAddr4.sin_port = htons(uniPortNo);
|
||||||
addr.sin_port = htons(uniPortNo);
|
_serverAddr4.sin_addr.s_addr = INADDR_ANY;
|
||||||
addr.sin_addr.s_addr = INADDR_ANY;
|
|
||||||
|
|
||||||
if (::bind(sock, (sockaddr*) &addr, sizeof(addr)) < 0)
|
if (::bind(sock, (sockaddr*) &_serverAddr4, sizeof(_serverAddr4)) < 0)
|
||||||
{
|
{
|
||||||
D_NWSTACK("can't bind unicast socket in UDP4_6Port::openV4 error %d %s\n", errno, strerror(errno));
|
D_NWSTACK("can't bind unicast socket in UDP4_6Port::openV4 error %d %s\n", errno, strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
_conns->setSockUnicast(sock);
|
_conns->setSockUnicast(sock);
|
||||||
|
|
||||||
/*------ Set SSL socket address --------*/
|
|
||||||
_serverAddr4.sin_family = AF_INET;
|
|
||||||
_serverAddr4.sin_port = htons(uniPortNo);
|
|
||||||
_serverAddr4.sin_addr.s_addr = INADDR_ANY;
|
|
||||||
|
|
||||||
/*------ Create Multicast socket --------*/
|
/*------ Create Multicast socket --------*/
|
||||||
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
if (sock < 0)
|
if (sock < 0)
|
||||||
@@ -1032,8 +1013,7 @@ int SensorNetwork::openV4(string *ipAddress, uint16_t multiPortNo, uint16_t uniP
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SensorNetwork::openV6(string *ipAddress, string *interface, uint16_t multiPortNo, uint16_t uniPortNo, uint16_t dtlsPortNo,
|
int SensorNetwork::openV6(string *ipAddress, string *interface, uint16_t multiPortNo, uint16_t uniPortNo, uint32_t hops)
|
||||||
uint32_t hops)
|
|
||||||
{
|
{
|
||||||
int optval = 0;
|
int optval = 0;
|
||||||
int sock = 0;
|
int sock = 0;
|
||||||
@@ -1048,7 +1028,7 @@ int SensorNetwork::openV6(string *ipAddress, string *interface, uint16_t multiPo
|
|||||||
}
|
}
|
||||||
|
|
||||||
_multicastAddr->setPort(multiPortNo);
|
_multicastAddr->setPort(multiPortNo);
|
||||||
_unicastAddr->setPort(dtlsPortNo);
|
_unicastAddr->setPort(uniPortNo);
|
||||||
|
|
||||||
if (_multicastAddr->setIpAddress(ipAddress) < 0)
|
if (_multicastAddr->setIpAddress(ipAddress) < 0)
|
||||||
{
|
{
|
||||||
@@ -1075,13 +1055,12 @@ int SensorNetwork::openV6(string *ipAddress, string *interface, uint16_t multiPo
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
sockaddr_in6 addr;
|
memset(&_serverAddr6, 0, sizeof(_serverAddr6));
|
||||||
memset(&addr, 0, sizeof(addr));
|
_serverAddr6.sin6_family = AF_INET6;
|
||||||
addr.sin6_family = AF_INET6;
|
_serverAddr6.sin6_port = htons(uniPortNo);
|
||||||
addr.sin6_port = htons(uniPortNo);
|
_serverAddr6.sin6_addr = in6addr_any;
|
||||||
addr.sin6_addr = in6addr_any;
|
|
||||||
|
|
||||||
if (::bind(sock, (sockaddr*) &addr, sizeof(addr)) < 0)
|
if (::bind(sock, (sockaddr*) &_serverAddr6, sizeof(_serverAddr6)) < 0)
|
||||||
{
|
{
|
||||||
D_NWSTACK("can't bind unicast socket in SensorNetwork::openV6 error %s\n", strerror(errno));
|
D_NWSTACK("can't bind unicast socket in SensorNetwork::openV6 error %s\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
@@ -1097,27 +1076,6 @@ int SensorNetwork::openV6(string *ipAddress, string *interface, uint16_t multiPo
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------ Set SSL socket address --------*/
|
|
||||||
_serverAddr6.sin6_family = AF_INET6;
|
|
||||||
_serverAddr6.sin6_port = htons(uniPortNo);
|
|
||||||
_serverAddr6.sin6_addr = in6addr_any;
|
|
||||||
|
|
||||||
if (::bind(sock, (sockaddr*) &_serverAddr6, sizeof(_serverAddr6)) < 0)
|
|
||||||
{
|
|
||||||
D_NWSTACK("can't bind listen socket in SensorNetwork::openV6 error %s\n", strerror(errno));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (interface->size() > 0)
|
|
||||||
{
|
|
||||||
#ifdef __APPLE__
|
|
||||||
setsockopt(sock, IPPROTO_IP, IP_BOUND_IF, &ifindex, interface->size());
|
|
||||||
#else
|
|
||||||
setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, interface->c_str(), interface->size());
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Create Multicast socket
|
// Create Multicast socket
|
||||||
sock = socket(AF_INET6, SOCK_DGRAM, 0);
|
sock = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||||
if (sock < 0)
|
if (sock < 0)
|
||||||
@@ -1245,7 +1203,7 @@ int SensorNetwork::getSenderAddress(int sock, SensorNetAddress *addr)
|
|||||||
sockaddr_in sender4 = { 0 };
|
sockaddr_in sender4 = { 0 };
|
||||||
socklen_t addrlen4 = sizeof(sender4);
|
socklen_t addrlen4 = sizeof(sender4);
|
||||||
char buf[16];
|
char buf[16];
|
||||||
int rc = PACKET_OTHERS;
|
int rc = DTLS_OTHERS;
|
||||||
|
|
||||||
len = ::recvfrom(sock, buf, 15, MSG_PEEK, (sockaddr*) &sender4, &addrlen4);
|
len = ::recvfrom(sock, buf, 15, MSG_PEEK, (sockaddr*) &sender4, &addrlen4);
|
||||||
|
|
||||||
@@ -1259,17 +1217,12 @@ int SensorNetwork::getSenderAddress(int sock, SensorNetAddress *addr)
|
|||||||
D_NWSTACK("SensorNetwork::getSenderAddress recved from %s:%d length = %d\n", inet_ntoa(sender4.sin_addr),
|
D_NWSTACK("SensorNetwork::getSenderAddress recved from %s:%d length = %d\n", inet_ntoa(sender4.sin_addr),
|
||||||
ntohs(sender4.sin_port), len);
|
ntohs(sender4.sin_port), len);
|
||||||
|
|
||||||
// if (len >= 13)
|
if (len >= 13)
|
||||||
{
|
{
|
||||||
if (buf[0] == 22)
|
if (buf[0] == DTLS_CLIENTHELLO || buf[0] == DTLS_APPL)
|
||||||
{
|
{
|
||||||
rc = PACKET_CLIENTHELLO;
|
rc = buf[0];
|
||||||
}
|
}
|
||||||
else if (buf[0] == 23)
|
|
||||||
{
|
|
||||||
rc = PACKET_APPL;
|
|
||||||
}
|
|
||||||
D_NWSTACK("getSenderAddress len=%d Packet type=%d\n", len, buf[0]);
|
|
||||||
}
|
}
|
||||||
return rc;
|
return rc;
|
||||||
|
|
||||||
|
|||||||
@@ -138,9 +138,8 @@ public:
|
|||||||
void close();
|
void close();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int openV4(string *ipAddress, uint16_t multiPortNo, uint16_t uniPortNo, uint16_t listenPortNo, uint32_t ttl);
|
int openV4(string *ipAddress, uint16_t multiPortNo, uint16_t uniPortNo, uint32_t ttl);
|
||||||
int openV6(string *ipAddress, string *interface, uint16_t multiPortNo, uint16_t uniPortNo, uint16_t listenPortNo,
|
int openV6(string *ipAddress, string *interface, uint16_t multiPortNo, uint16_t uniPortNo, uint32_t hops);
|
||||||
uint32_t hops);
|
|
||||||
int multicastRecv(uint8_t *buf, uint16_t len);
|
int multicastRecv(uint8_t *buf, uint16_t len);
|
||||||
int getSendClient(int index, SensorNetAddress *addr);
|
int getSendClient(int index, SensorNetAddress *addr);
|
||||||
int getSenderAddress(int sock, SensorNetAddress *addr);
|
int getSenderAddress(int sock, SensorNetAddress *addr);
|
||||||
|
|||||||
Reference in New Issue
Block a user