2016-11-12 17:39:11 +01:00
|
|
|
#ifndef WS_H
|
|
|
|
#define WS_H
|
2017-06-05 19:09:59 +02:00
|
|
|
#include <resolv.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ifaddrs.h>
|
2016-11-12 17:39:11 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
2017-06-05 19:09:59 +02:00
|
|
|
#include<netdb.h> //hostent
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include "utils.h"
|
2017-07-29 23:54:43 +02:00
|
|
|
#include "handle.h"
|
2017-06-05 19:09:59 +02:00
|
|
|
#define CONN_TIME_OUT_S 3
|
2016-11-12 17:39:11 +01:00
|
|
|
#define BITV(v,i) ((v & (1 << i)) >> i)
|
|
|
|
#define WS_TEXT 0x1
|
|
|
|
#define WS_BIN 0x2
|
|
|
|
#define WS_CLOSE 0x8
|
|
|
|
#define WS_PING 0x9
|
|
|
|
#define WS_PONG 0xA
|
2017-06-05 19:09:59 +02:00
|
|
|
#define ws_t(c ,d) (ws_send_text(c,d,0))
|
|
|
|
#define ws_b(c , d,z) (ws_send_binary(c,d,z,0))
|
|
|
|
#define ws_f(c,f) (ws_send_file(c,f,0))
|
|
|
|
#define ws_close(c,r) (ws_send_close(c,r,0))
|
|
|
|
#define MAX_BUFF 1024
|
|
|
|
#define CLIENT_RQ "GET /%s HTTP/1.1\r\nHost: %s\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n"
|
|
|
|
#define SERVER_WS_KEY "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="
|
|
|
|
|
2016-11-12 17:39:11 +01:00
|
|
|
typedef struct{
|
|
|
|
uint8_t fin;
|
|
|
|
uint8_t opcode;
|
|
|
|
unsigned int plen;
|
2017-06-05 19:09:59 +02:00
|
|
|
uint8_t mask;
|
2016-11-12 17:39:11 +01:00
|
|
|
uint8_t mask_key[4];
|
|
|
|
} ws_msg_header_t;
|
|
|
|
|
|
|
|
ws_msg_header_t * ws_read_header(int);
|
2016-12-13 16:34:03 +01:00
|
|
|
void ws_send_frame(int , uint8_t* , ws_msg_header_t );
|
2016-11-12 17:39:11 +01:00
|
|
|
void pong(int client, int len);
|
2017-06-05 19:09:59 +02:00
|
|
|
|
|
|
|
void ws_send_text(int client, const char* data,int mask);
|
|
|
|
void ws_send_close(int client, unsigned int status, int mask);
|
|
|
|
void ws_send_file(int client, const char* file, int mask);
|
|
|
|
void ws_send_binary(int client, uint8_t* data, int l, int mask);
|
|
|
|
|
2016-11-12 17:39:11 +01:00
|
|
|
int ws_read_data(int , ws_msg_header_t*, int, uint8_t*);
|
2017-06-05 19:09:59 +02:00
|
|
|
int request_socket(const char* ip, int port);
|
|
|
|
int ip_from_hostname(const char * hostname , char* ip);
|
|
|
|
int sock_read_buf(int sock, char*buf,int size);
|
|
|
|
int ws_open_hand_shake(const char* host, int port, const char* resource);
|
|
|
|
char* get_ip_address();
|
2016-11-12 17:39:11 +01:00
|
|
|
#endif
|