My Project
 All Classes Functions Variables Pages
Socket.h
1 /*
2  * Socket.h
3  *
4  * Created on: Mar 5, 2017
5  * Author: kolban
6  */
7 
8 #ifndef COMPONENTS_CPP_UTILS_SOCKET_H_
9 #define COMPONENTS_CPP_UTILS_SOCKET_H_
10 #include "sdkconfig.h"
11 #include <mbedtls/platform.h>
12 
13 #include <mbedtls/ctr_drbg.h>
14 #include <mbedtls/debug.h>
15 #include <mbedtls/entropy.h>
16 #include <mbedtls/error.h>
17 #include <mbedtls/net.h>
18 #include <mbedtls/ssl.h>
19 
20 #include <lwip/inet.h>
21 #include <lwip/sockets.h>
22 
23 #undef accept
24 #undef bind
25 #undef close
26 #undef connect
27 #undef listen
28 #undef read
29 #undef recv
30 #undef send
31 #undef write
32 
33 #include <string>
34 #include <iostream>
35 #include <streambuf>
36 #include <cstdio>
37 #include <cstring>
38 #include <exception>
39 
40 
41 #if CONFIG_CXX_EXCEPTIONS != 1
42 #error "C++ exception handling must be enabled within make menuconfig. See Compiler Options > Enable C++ Exceptions."
43 #endif
44 
45 class SocketException: public std::exception {
46 public:
47  SocketException(int myErrno);
48 
49 private:
50  int m_errno;
51 
52 };
53 
54 
62 class Socket {
63 public:
64  Socket();
65  virtual ~Socket();
66 
67  Socket accept();
68  static std::string addressToString(struct sockaddr* addr);
69  int bind(uint16_t port, uint32_t address);
70  int close();
71  int connect(struct in_addr address, uint16_t port);
72  int connect(char* address, uint16_t port);
73  int createSocket(bool isDatagram = false);
74  void setReuseAddress(bool value);
75  int setSocketOption(int option, void* value, size_t len);
76  int setTimeout(uint32_t seconds);
77  void getBind(struct sockaddr* pAddr);
78  int getFD() const;
79  bool getSSL() const;
80  bool isValid();
81  int listen(uint16_t port, bool isDatagram = false, bool reuseAddress = false);
82  bool operator<(const Socket& other) const;
83  std::string readToDelim(std::string delim);
84  size_t receive(uint8_t* data, size_t length, bool exact = false);
85  int receiveFrom(uint8_t* data, size_t length, struct sockaddr* pAddr);
86  int send(std::string value) const;
87  int send(const uint8_t* data, size_t length) const;
88  int send(uint16_t value);
89  int send(uint32_t value);
90  void sendTo(const uint8_t* data, size_t length, struct sockaddr* pAddr);
91  void setSSL(bool sslValue = true);
92  std::string toString();
93 
94 private:
95  int m_sock; // The underlying TCP/IP socket
96  bool m_useSSL; // Should we use SSL
97  mbedtls_net_context m_sslSock;
98  mbedtls_entropy_context m_entropy;
99  mbedtls_ctr_drbg_context m_ctr_drbg;
100  mbedtls_ssl_context m_sslContext;
101  mbedtls_ssl_config m_conf;
102  mbedtls_x509_crt m_srvcert;
103  mbedtls_pk_context m_pkey;
104  void sslHandshake();
105 
106 };
107 
108 class SocketInputRecordStreambuf : public std::streambuf {
109 public:
110  SocketInputRecordStreambuf(Socket socket, size_t dataLength, size_t bufferSize = 512);
112  int_type underflow();
113 
114 private:
115  char* m_buffer;
116  Socket m_socket;
117  size_t m_dataLength;
118  size_t m_bufferSize;
119  size_t m_sizeRead;
120 
121 };
122 
123 
124 #endif /* COMPONENTS_CPP_UTILS_SOCKET_H_ */
Definition: Socket.h:108
Encapsulate a socket.
Definition: Socket.h:62
void getBind(struct sockaddr *pAddr)
Get the bound address.
Definition: Socket.cpp:225
Definition: Socket.h:45
int listen(uint16_t port, bool isDatagram=false, bool reuseAddress=false)
Create a listening socket.
Definition: Socket.cpp:259
void setSSL(bool sslValue=true)
Flag the socket as using SSL.
Definition: Socket.cpp:510
SocketInputRecordStreambuf(Socket socket, size_t dataLength, size_t bufferSize=512)
Create a socket input record streambuf.
Definition: Socket.cpp:627
void sendTo(const uint8_t *data, size_t length, struct sockaddr *pAddr)
Send data to a specific address.
Definition: Socket.cpp:481
int connect(struct in_addr address, uint16_t port)
Connect to a partner.
Definition: Socket.cpp:164
int setTimeout(uint32_t seconds)
Socket timeout.
Definition: Socket.cpp:303
size_t receive(uint8_t *data, size_t length, bool exact=false)
Receive data from the partner. Receive data from the socket partner. If exact = false, we read as much data as is available without blocking up to length. If exact = true, we will block until we have received exactly length bytes or there are no more bytes to read.
Definition: Socket.cpp:349
static std::string addressToString(struct sockaddr *addr)
Convert a socket address to a string representation.
Definition: Socket.cpp:92
int close()
Close the socket.
Definition: Socket.cpp:135
Socket accept()
Accept a new socket.
Definition: Socket.cpp:57
int bind(uint16_t port, uint32_t address)
Bind an address/port to a socket. Specify a port of 0 to have a local port allocated. Specify an address of INADDR_ANY to use the local server IP.
Definition: Socket.cpp:110
void setReuseAddress(bool value)
Flag the socket address as re-usable.
Definition: Socket.cpp:498
int send(std::string value) const
Send a string to the partner.
Definition: Socket.cpp:457
int createSocket(bool isDatagram=false)
Create the socket.
Definition: Socket.cpp:204
int receiveFrom(uint8_t *data, size_t length, struct sockaddr *pAddr)
Receive data with the address.
Definition: Socket.cpp:400
int_type underflow()
Handle the request to read data from the stream but we need more data from the source.
Definition: Socket.cpp:650
int getFD() const
Get the underlying socket file descriptor.
Definition: Socket.cpp:241
std::string toString()
Get the string representation of this socket.
Definition: Socket.cpp:614
int setSocketOption(int option, void *value, size_t len)
Set the socket option.
Definition: Socket.cpp:290