mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-15 08:26:51 +01:00
Two clients works fine. Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
@@ -207,10 +207,13 @@ int LGwProxy::getConnectResponce(void)
|
|||||||
if (_network.sslConnect() > 0)
|
if (_network.sslConnect() > 0)
|
||||||
{
|
{
|
||||||
_status = GW_CONNECTING;
|
_status = GW_CONNECTING;
|
||||||
DISPLAY(
|
DISPLAY("\033[0m\033[0;32m\n\nLGwProxy::getConnectResponce SSL connection established.\033[0m\033[0;37m\n\n");
|
||||||
"\033[0m\033[0;32m\n\nLGwProxy::getConnectResponce Can't connect the Gateway via SSL.\033[0m\033[0;37m\n\n");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DISPLAY("\033[0m\033[0;32m\n\nLGwProxy::getConnectResponce SSL connection failed.\033[0m\033[0;37m\n\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
_status = GW_CONNECTING;
|
_status = GW_CONNECTING;
|
||||||
|
|||||||
@@ -290,7 +290,6 @@ void SensorNetAddress::clear(void)
|
|||||||
Connections::Connections()
|
Connections::Connections()
|
||||||
{
|
{
|
||||||
_pollfds = nullptr;
|
_pollfds = nullptr;
|
||||||
_clientAddr = nullptr;
|
|
||||||
_ssls = nullptr;
|
_ssls = nullptr;
|
||||||
_maxfds = 0;
|
_maxfds = 0;
|
||||||
_numfds = 2;
|
_numfds = 2;
|
||||||
@@ -322,13 +321,8 @@ Connections::~Connections()
|
|||||||
}
|
}
|
||||||
free(_pollfds);
|
free(_pollfds);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < _maxfds; i++)
|
|
||||||
{
|
|
||||||
delete _clientAddr[i];
|
|
||||||
}
|
|
||||||
free(_clientAddr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connections::initialize(int maxClient)
|
void Connections::initialize(int maxClient)
|
||||||
{
|
{
|
||||||
_maxfds = maxClient + POLL_SSL;
|
_maxfds = maxClient + POLL_SSL;
|
||||||
@@ -340,13 +334,6 @@ void Connections::initialize(int maxClient)
|
|||||||
{
|
{
|
||||||
throw EXCEPTION("Can't allocate ssls.", 0);
|
throw EXCEPTION("Can't allocate ssls.", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
_clientAddr = (SensorNetAddress**) malloc(_maxfds * sizeof(unsigned long int));
|
|
||||||
for (int i = 0; i < _maxfds; i++)
|
|
||||||
{
|
|
||||||
_clientAddr[i] = new SensorNetAddress();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connections::closeSSL(int index)
|
void Connections::closeSSL(int index)
|
||||||
@@ -409,17 +396,14 @@ void Connections::close(int index)
|
|||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
int sock = _pollfds[idx].fd;
|
int sock = _pollfds[idx].fd;
|
||||||
SSL *ssl = _ssls[idx];
|
SSL *ssl = _ssls[idx];
|
||||||
SensorNetAddress *addr = _clientAddr[idx];
|
|
||||||
|
|
||||||
for (; idx < _numfds; idx++)
|
for (; idx < _numfds; idx++)
|
||||||
{
|
{
|
||||||
_ssls[idx] = _ssls[idx + 1];
|
_ssls[idx] = _ssls[idx + 1];
|
||||||
_pollfds[idx] = _pollfds[idx + 1];
|
_pollfds[idx] = _pollfds[idx + 1];
|
||||||
_clientAddr[idx] = _clientAddr[idx + 1];
|
|
||||||
|
|
||||||
if (_ssls[idx + 1] == 0)
|
if (_ssls[idx + 1] == 0)
|
||||||
{
|
{
|
||||||
_clientAddr[idx + 1] = new SensorNetAddress();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -434,10 +418,6 @@ void Connections::close(int index)
|
|||||||
{
|
{
|
||||||
::close(sock);
|
::close(sock);
|
||||||
}
|
}
|
||||||
if (addr != nullptr)
|
|
||||||
{
|
|
||||||
delete addr;
|
|
||||||
}
|
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -473,18 +453,6 @@ SSL* Connections::getClientSSL(int index)
|
|||||||
return _ssls[index + POLL_SSL];
|
return _ssls[index + POLL_SSL];
|
||||||
}
|
}
|
||||||
|
|
||||||
int Connections::searchClient(SensorNetAddress *addr)
|
|
||||||
{
|
|
||||||
for (int i = POLL_SSL; i < _numfds; i++)
|
|
||||||
{
|
|
||||||
if (_clientAddr[i]->isMatch(addr) == true)
|
|
||||||
{
|
|
||||||
return i - POLL_SSL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*================================================================
|
/*================================================================
|
||||||
Class SensorNetwork
|
Class SensorNetwork
|
||||||
|
|
||||||
@@ -597,7 +565,6 @@ int SensorNetwork::read(uint8_t *buf, uint16_t bufLen)
|
|||||||
int optval;
|
int optval;
|
||||||
int clientIndex = -1;
|
int clientIndex = -1;
|
||||||
int sockListen = 0;
|
int sockListen = 0;
|
||||||
SensorNetAddress client;
|
|
||||||
char errmsg[256];
|
char errmsg[256];
|
||||||
union
|
union
|
||||||
{
|
{
|
||||||
@@ -605,27 +572,22 @@ int SensorNetwork::read(uint8_t *buf, uint16_t bufLen)
|
|||||||
struct sockaddr_in6 s6;
|
struct sockaddr_in6 s6;
|
||||||
} client_addr;
|
} client_addr;
|
||||||
|
|
||||||
|
SensorNetAddress client;
|
||||||
|
client.clear();
|
||||||
|
client.setFamily(_af);
|
||||||
|
|
||||||
// Check POLL_IN
|
// Check POLL_IN
|
||||||
int cnt = _conns->poll(2000); // Timeout 2secs
|
int cnt = _conns->poll(2000); // Timeout 2secs
|
||||||
if (cnt == 0)
|
if (cnt == 0)
|
||||||
{
|
{
|
||||||
|
// Timeout
|
||||||
return cnt;
|
return cnt;
|
||||||
}
|
}
|
||||||
|
else if (cnt < 0)
|
||||||
if (cnt < 0)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
/* ToDo: close socket */
|
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int numfds = _conns->getNumOfConnections();
|
|
||||||
|
|
||||||
client.clear();
|
|
||||||
client.setFamily(_af);
|
|
||||||
size_t recvlen = 0;
|
|
||||||
SSL *ssl = 0;
|
|
||||||
|
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
|
|
||||||
@@ -643,7 +605,6 @@ int SensorNetwork::read(uint8_t *buf, uint16_t bufLen)
|
|||||||
getUnicastClient(&client);
|
getUnicastClient(&client);
|
||||||
sockListen = _conns->getSockUnicast();
|
sockListen = _conns->getSockUnicast();
|
||||||
|
|
||||||
ListenClient_hello:
|
|
||||||
// Listen Connection
|
// Listen Connection
|
||||||
SSL *ssl = SSL_new(_dtlsctx);
|
SSL *ssl = SSL_new(_dtlsctx);
|
||||||
BIO *bio = BIO_new_dgram(sockListen, BIO_NOCLOSE);
|
BIO *bio = BIO_new_dgram(sockListen, BIO_NOCLOSE);
|
||||||
@@ -725,11 +686,15 @@ ListenClient_hello:
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Check SSL packet from clients
|
// Check SSL packet from clients
|
||||||
|
size_t recvlen = 0;
|
||||||
|
SSL *ssl = 0;
|
||||||
|
int numfds = _conns->getNumOfConnections();
|
||||||
|
|
||||||
for (int i = 0; i < numfds - POLL_SSL; i++)
|
for (int i = 0; i < numfds - POLL_SSL; i++)
|
||||||
{
|
{
|
||||||
if (_conns->getEventClient(i) == POLLIN)
|
if (_conns->getEventClient(i) == POLLIN)
|
||||||
{
|
{
|
||||||
D_NWSTACK("SSL RECV\n");
|
D_NWSTACK("SSL Packet RECV\n");
|
||||||
int dtls = getSendClient(i, &client);
|
int dtls = getSendClient(i, &client);
|
||||||
|
|
||||||
if (dtls > 0)
|
if (dtls > 0)
|
||||||
@@ -740,20 +705,15 @@ ListenClient_hello:
|
|||||||
#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 A packet is ClientHello. Client reconnected. Close connection. SSL_connection will timeout.\n", clientaddrBuf);
|
||||||
#endif
|
#endif
|
||||||
// Delete current connection.
|
|
||||||
clientIndex = i;
|
clientIndex = i;
|
||||||
D_NWSTACK("Close current connections\n");
|
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
_conns->close(clientIndex); // DEBUG
|
_conns->close(clientIndex);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
sockListen = _conns->getSockClient(i);
|
|
||||||
goto ListenClient_hello;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recv a MQTT-SN message from a client
|
// The packet is a MQTT-SN message
|
||||||
ssl = _conns->getClientSSL(i);
|
ssl = _conns->getClientSSL(i);
|
||||||
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)
|
||||||
@@ -1214,6 +1174,7 @@ int SensorNetwork::getSenderAddress(int sock, SensorNetAddress *addr)
|
|||||||
}
|
}
|
||||||
addr->setFamily(AF_INET);
|
addr->setFamily(AF_INET);
|
||||||
addr->getIpAddress()->addr.ad4 = sender4.sin_addr;
|
addr->getIpAddress()->addr.ad4 = sender4.sin_addr;
|
||||||
|
addr->setPort(sender4.sin_port);
|
||||||
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);
|
||||||
|
|
||||||
@@ -1241,6 +1202,7 @@ int SensorNetwork::getSenderAddress(int sock, SensorNetAddress *addr)
|
|||||||
|
|
||||||
addr->setFamily(AF_INET6);
|
addr->setFamily(AF_INET6);
|
||||||
addr->setSockaddr6(&sender6);
|
addr->setSockaddr6(&sender6);
|
||||||
|
addr->setPort(sender4.sin_port);
|
||||||
|
|
||||||
#ifdef DEBUG_NW
|
#ifdef DEBUG_NW
|
||||||
char senderstr[INET6_ADDRSTRLEN];
|
char senderstr[INET6_ADDRSTRLEN];
|
||||||
|
|||||||
@@ -109,11 +109,9 @@ public:
|
|||||||
int getEventUnicast(void);
|
int getEventUnicast(void);
|
||||||
int getEventListen(void);
|
int getEventListen(void);
|
||||||
void closeSSL(int index);
|
void closeSSL(int index);
|
||||||
int searchClient(SensorNetAddress *addr);
|
|
||||||
private:
|
private:
|
||||||
pollfd *_pollfds;
|
pollfd *_pollfds;
|
||||||
SSL **_ssls;
|
SSL **_ssls;
|
||||||
SensorNetAddress **_clientAddr;
|
|
||||||
int _maxfds;
|
int _maxfds;
|
||||||
int _numfds;
|
int _numfds;
|
||||||
Mutex _mutex;
|
Mutex _mutex;
|
||||||
|
|||||||
Reference in New Issue
Block a user