8 #ifndef COMPONENTS_CPP_UTILS_SOCKET_H_
9 #define COMPONENTS_CPP_UTILS_SOCKET_H_
10 #include "sdkconfig.h"
11 #include <mbedtls/platform.h>
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>
20 #include <lwip/inet.h>
21 #include <lwip/sockets.h>
41 #if CONFIG_CXX_EXCEPTIONS != 1
42 #error "C++ exception handling must be enabled within make menuconfig. See Compiler Options > Enable C++ Exceptions."
69 int bind(uint16_t port, uint32_t address);
71 int connect(
struct in_addr address, uint16_t port);
72 int connect(
char* address, uint16_t port);
77 void getBind(
struct sockaddr* pAddr);
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);
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;
Encapsulate a socket.
Definition: Socket.h:62
void getBind(struct sockaddr *pAddr)
Get the bound address.
Definition: Socket.cpp:225
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
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 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