RTL8710AF_GCC/component/common/network/websocket/libwsclient.h

121 lines
4.3 KiB
C
Raw Permalink Normal View History

2016-09-12 09:16:56 +00:00
#ifndef EASYWSCLIENT_H
#define EASYWSCLIENT_H
#include <platform/platform_stdlib.h>
/****************Define the debug message level*********************/
#define DEBUG_WSCLIENT 1
#define WSCLIENT_LOG(level, fmt, ...) printf("\n\r[WSCLIENT %s] %s: " fmt "\n", level, __FUNCTION__, ##__VA_ARGS__)
#if DEBUG_WSCLIENT == 2
#define WSCLIENT_DEBUG(fmt, ...) WSCLIENT_LOG("DEBUG", fmt, ##__VA_ARGS__)
#else
#define WSCLIENT_DEBUG(fmt, ...)
#endif
#if DEBUG_WSCLIENT
#define WSCLIENT_ERROR(fmt, ...) WSCLIENT_LOG("ERROR", fmt, ##__VA_ARGS__)
#else
#define WSCLIENT_ERROR(fmt, ...)
#endif
/*******************************************************************/
/****************Define the structures used*************************/
typedef enum{
CLOSING,
CLOSED,
CONNECTING,
OPEN
} readyStateValues;
struct wsheader_type{
unsigned header_size;
int fin;
int mask;
enum opcode_type {
CONTINUATION = 0x0,
TEXT_FRAME = 0x1,
BINARY_FRAME = 0x2,
CLOSE = 8,
PING = 9,
PONG = 0xa,
} opcode;
int N0;
uint64_t N;
uint8_t masking_key[4];
};
struct _wsclient_context;
struct _ssl_context;
struct ssl_fun_ops{
int (*memory_set_own)( void * (*malloc_func)( size_t ),void (*free_func)( void * ) );
int (*net_connect)( int *fd, const char *host, int port );
int (*ssl_init)( struct _ssl_context *ssl );
void (*ssl_set_endpoint)( struct _ssl_context *ssl, int endpoint );
void (*ssl_set_authmode)( struct _ssl_context *ssl, int authmode );
void (*ssl_set_rng)( struct _ssl_context *ssl,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
void (*ssl_set_bio)( struct _ssl_context *ssl,
int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
int (*f_send)(void *, const unsigned char *, size_t), void *p_send );
int (*ssl_handshake)( struct _ssl_context *ssl );
void (*net_close)( int fd );
void (*ssl_free)( struct _ssl_context *ssl );
int (*ssl_read)( struct _ssl_context *ssl, unsigned char *buf, size_t len );
int (*ssl_write)( struct _ssl_context *ssl, const unsigned char *buf, size_t len );
const char *(*ssl_get_ciphersuite)( const struct _ssl_context *ssl );
int (*net_recv)( void *ctx, unsigned char *buf, size_t len );
int (*net_send)( void *ctx, const unsigned char *buf, size_t len );
};
struct ws_fun_ops{
int (*hostname_connect)(struct _wsclient_context *wsclient);
void (*client_close)(struct _wsclient_context *wsclient);
int (*client_send)(struct _wsclient_context *wsclient, unsigned char *data, size_t data_len);
int (*client_read)(struct _wsclient_context *wsclient, unsigned char *data, size_t data_len);
struct ssl_fun_ops ssl_fun_ops;
};
typedef struct _wsclient_context{
char host[128];
char path[128];
char origin[200];
int port;
uint8_t use_ssl;
int sockfd;
readyStateValues readyState;
int tx_len;
void *ssl;
uint8_t *txbuf;
uint8_t *rxbuf;
uint8_t *receivedData;
struct ws_fun_ops fun_ops;
}wsclient_context;
/*******************************************************************/
/****************General functions used by wsclient*****************/
void ws_get_random_bytes(void *buf, size_t len);
void* ws_malloc(unsigned int size);
void ws_free(void *buf);
int ws_client_handshake(wsclient_context *wsclient);
int ws_check_handshake(wsclient_context *wsclient);
void ws_sendData(uint8_t type, size_t message_size, uint8_t* message, int useMask, wsclient_context *wsclient);
/*******************************************************************/
/*************Functions used by wsclient without SSL****************/
int ws_hostname_connect(wsclient_context *wsclient);
int ws_client_read(wsclient_context *wsclient, unsigned char *data, size_t data_len);
int ws_client_send(wsclient_context *wsclient, unsigned char *data, size_t data_len);
void ws_client_close(wsclient_context *wsclient);
/*******************************************************************/
/***************Functions used by wsclient with SSL*****************/
int wss_hostname_connect(wsclient_context *wsclient);
int wss_client_read(wsclient_context *wsclient, unsigned char *data, size_t data_len);
int wss_client_send(wsclient_context *wsclient, unsigned char *data, size_t data_len);
void wss_client_close(wsclient_context *wsclient);
/*******************************************************************/
#endif