mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-16 08:56:51 +01:00
The purpose of this branch is to share work in process. Change sellect() of UDP to poll() Rewrite UDP6 for DTLS6 Known bug: can't reconnect DTLS Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
1437
MQTTSNGateway/src/linux/dtls/SensorNetwork.cpp
Normal file
1437
MQTTSNGateway/src/linux/dtls/SensorNetwork.cpp
Normal file
File diff suppressed because it is too large
Load Diff
163
MQTTSNGateway/src/linux/dtls/SensorNetwork.h
Normal file
163
MQTTSNGateway/src/linux/dtls/SensorNetwork.h
Normal file
@@ -0,0 +1,163 @@
|
||||
/**************************************************************************************
|
||||
* Copyright (c) 2021, Tomoaki Yamaguchi
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Tomoaki Yamaguchi - initial API and implementation and/or initial documentation
|
||||
**************************************************************************************/
|
||||
|
||||
#ifndef SENSORNETWORK_H_
|
||||
#define SENSORNETWORK_H_
|
||||
|
||||
#include "MQTTSNGWDefines.h"
|
||||
#include "Threading.h"
|
||||
#include <netinet/ip.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
#include <string>
|
||||
#include <poll.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace MQTTSNGW
|
||||
{
|
||||
/*===========================================
|
||||
Class SensorNetAddreess
|
||||
============================================*/
|
||||
typedef struct
|
||||
{
|
||||
int af;
|
||||
union
|
||||
{
|
||||
struct in_addr ad4;
|
||||
struct in6_addr ad6;
|
||||
} addr;
|
||||
} ipAddr_t;
|
||||
|
||||
class SensorNetAddress
|
||||
{
|
||||
public:
|
||||
SensorNetAddress();
|
||||
~SensorNetAddress();
|
||||
void setAddress(ipAddr_t *Address, uint16_t port);
|
||||
int setAddress(string *ipAddrPort);
|
||||
int setIpAddress(string *IpAddress);
|
||||
void setFamily(int type);
|
||||
int getFamily(void);
|
||||
void setPort(in_port_t port);
|
||||
void setSockaddr4(sockaddr_in *sockaddr);
|
||||
void setSockaddr6(sockaddr_in6 *sockaddr);
|
||||
void cpyAddr4(sockaddr_in *sockaddr);
|
||||
void cpyAddr6(sockaddr_in6 *sockaddr);
|
||||
in_port_t getPort(void);
|
||||
ipAddr_t* getIpAddress(void);
|
||||
void setIndex(int index);
|
||||
int getIndex(void);
|
||||
|
||||
void clear(void);
|
||||
|
||||
bool isMatch(SensorNetAddress *addr);
|
||||
SensorNetAddress& operator =(SensorNetAddress &addr);
|
||||
char* sprint(char *buf);
|
||||
private:
|
||||
int _pfdsIndex;
|
||||
in_port_t _portNo;
|
||||
ipAddr_t _ipAddr;
|
||||
};
|
||||
|
||||
/*===========================================
|
||||
Class Connections
|
||||
============================================*/
|
||||
#define POLL_UCAST 0
|
||||
#define POLL_MCAST 1
|
||||
#define POLL_SSL 2
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int af;
|
||||
SSL *ssl;
|
||||
} afSSL_t;
|
||||
|
||||
class Connections
|
||||
{
|
||||
public:
|
||||
Connections();
|
||||
~Connections();
|
||||
void initialize(int maxClient);
|
||||
void close(int index);
|
||||
int poll(int timeout);
|
||||
int addClientSock(int sock);
|
||||
int addClientSSL(SSL *ssl, int sock);
|
||||
void setSockMulticast(int sock);
|
||||
void setSockUnicast(int sock);
|
||||
int getNumOfConnections(void);
|
||||
int getNumOfClients(void);
|
||||
SSL* getClientSSL(int index);
|
||||
int getEventClient(int index);
|
||||
int getSockClient(int index);
|
||||
int getSockMulticast(void);
|
||||
int getSockUnicast(void);
|
||||
int getEventMulticast(void);
|
||||
int getEventUnicast(void);
|
||||
int getEventListen(void);
|
||||
void closeSSL(int index);
|
||||
int searchClient(SensorNetAddress *addr);
|
||||
private:
|
||||
pollfd *_pollfds;
|
||||
SSL **_ssls;
|
||||
SensorNetAddress **_clientAddr;
|
||||
int _maxfds;
|
||||
int _numfds;
|
||||
Mutex _mutex;
|
||||
};
|
||||
|
||||
/*===========================================
|
||||
Class SensorNetwork
|
||||
============================================*/
|
||||
class SensorNetwork
|
||||
{
|
||||
friend class SensorNetSubTask;
|
||||
public:
|
||||
SensorNetwork();
|
||||
~SensorNetwork();
|
||||
|
||||
int unicast(const uint8_t *payload, uint16_t payloadLength, SensorNetAddress *sendto);
|
||||
int broadcast(const uint8_t *payload, uint16_t payloadLength);
|
||||
int read(uint8_t *buf, uint16_t bufLen);
|
||||
void initialize(void);
|
||||
const char* getDescription(void);
|
||||
SensorNetAddress* getSenderAddress(void);
|
||||
void close();
|
||||
|
||||
private:
|
||||
int openV4(string *ipAddress, uint16_t multiPortNo, uint16_t uniPortNo, uint16_t listenPortNo, uint32_t ttl);
|
||||
int openV6(string *ipAddress, string *interface, uint16_t multiPortNo, uint16_t uniPortNo, uint16_t listenPortNo,
|
||||
uint32_t hops);
|
||||
int multicastRecv(uint8_t *buf, uint16_t len);
|
||||
int getSendClient(int index, SensorNetAddress *addr);
|
||||
int getSenderAddress(int sock, SensorNetAddress *addr);
|
||||
int getUnicastClient(SensorNetAddress *addr);
|
||||
void clearRecvData(int sock);
|
||||
|
||||
Mutex _mutex;
|
||||
SensorNetAddress *_senderAddr;
|
||||
SensorNetAddress *_multicastAddr;
|
||||
SensorNetAddress *_unicastAddr;
|
||||
string _description;
|
||||
SSL_CTX *_dtlsctx;
|
||||
Connections *_conns;
|
||||
sockaddr_in _serverAddr4;
|
||||
sockaddr_in6 _serverAddr6;
|
||||
int _af;
|
||||
};
|
||||
|
||||
}
|
||||
#endif /* SENSORNETWORK_H_ */
|
||||
Reference in New Issue
Block a user