Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Network.h Source File

Network.h

00001 /**************************************************************************************
00002  * Copyright (c) 2016, Tomoaki Yamaguchi
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Tomoaki Yamaguchi - initial API and implementation and/or initial documentation
00015  **************************************************************************************/
00016 
00017 #ifndef NETWORK_H_
00018 #define NETWORK_H_
00019 #include <sys/types.h>
00020 #include <sys/socket.h>
00021 #include <netdb.h>
00022 #include <resolv.h>
00023 #include <netdb.h>
00024 #include <openssl/ssl.h>
00025 #include <openssl/err.h>
00026 
00027 #include "Threading.h"
00028 #include "MQTTSNGWDefines.h"
00029 
00030 using namespace std;
00031 using namespace MQTTSNGW;
00032 
00033 /*========================================
00034  Class TCPStack
00035  =======================================*/
00036 class TCPStack
00037 {
00038 public:
00039     TCPStack();
00040     virtual ~TCPStack();
00041 
00042     // Server initialization
00043     bool bind(const char* service);
00044     bool listen();
00045     bool accept(TCPStack&);
00046 
00047     // Client initialization
00048     bool connect(const char* host, const char* service);
00049 
00050     int send(const uint8_t* buf, int length);
00051     int recv(uint8_t* buf, int len);
00052     void close();
00053 
00054     void setNonBlocking(const bool);
00055 
00056     bool isValid();
00057     int getSock();
00058 
00059 private:
00060     int _sockfd;
00061     addrinfo* _addrinfo;
00062     Mutex _mutex;
00063 };
00064 
00065 /*========================================
00066  Class Network
00067  =======================================*/
00068 class Network: public TCPStack
00069 {
00070 public:
00071     Network(bool secure);
00072     virtual ~Network();
00073 
00074     bool connect(const char* host, const char* port, const char* caPath, const char* caFile, const char* cert, const char* prvkey);
00075     bool connect(const char* host, const char* port);
00076     void close(void);
00077     int  send(const uint8_t* buf, uint16_t length);
00078     int  recv(uint8_t* buf, uint16_t len);
00079 
00080     bool isValid(void);
00081     bool isSecure(void);
00082     int  getSock(void);
00083 
00084 private:
00085     static SSL_CTX* _ctx;
00086     static SSL_SESSION* _session;
00087     static int _numOfInstance;
00088     SSL* _ssl;
00089     bool _secureFlg;
00090     Mutex _mutex;
00091     bool _busy;
00092     bool _sslValid;
00093 };
00094 
00095 #endif /* NETWORK_H_ */