1 #ifndef _WS_H
2 #define _WS_H
3 
4 //#define WSS_STANDALONE 1
5 
6 #define WEBSOCKET_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
7 #define B64BUFFLEN 1024
8 
9 #include <sys/types.h>
10 #ifndef _MSC_VER
11 #include <arpa/inet.h>
12 #include <sys/wait.h>
13 #include <sys/socket.h>
14 #include <unistd.h>
15 #else
16 #pragma warning(disable:4996)
17 #endif
18 #include <string.h>
19 #include <string.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <assert.h>
24 #include <errno.h>
25 //#include "sha1.h"
26 #include <openssl/ssl.h>
27 
28 #if defined(_MSC_VER) || defined(__APPLE__) || defined(__FreeBSD__) || (defined(__SVR4) && defined(__sun)) || defined(__DragonFly__)
29 #define __bswap_64(x) \
30   x = (x>>56) | \
31     ((x<<40) & 0x00FF000000000000) | \
32     ((x<<24) & 0x0000FF0000000000) | \
33     ((x<<8)  & 0x000000FF00000000) | \
34     ((x>>8)  & 0x00000000FF000000) | \
35     ((x>>24) & 0x0000000000FF0000) | \
36     ((x>>40) & 0x000000000000FF00) | \
37     (x<<56)
38 #endif
39 #ifdef _MSC_VER
40 #ifndef strncasecmp
41 #define strncasecmp _strnicmp
42 #endif
43 #ifdef _WIN64
44 #define WS_SSIZE_T __int64
45 #elif _MSC_VER >= 1400
46 #define WS_SSIZE_T __int32 __w64
47 #else
48 #define WS_SSIZE_T __int32
49 #endif
50 typedef WS_SSIZE_T ssize_t;
51 #endif
52 
53 
54 struct ws_globals_s {
55 	const SSL_METHOD *ssl_method;
56 	SSL_CTX *ssl_ctx;
57 	char cert[512];
58 	char key[512];
59 };
60 
61 //extern struct ws_globals_s ws_globals;
62 
63 #ifndef WIN32
64 typedef int ws_socket_t;
65 #else
66 typedef SOCKET ws_socket_t;
67 #endif
68 #define ws_sock_invalid (ws_socket_t)-1
69 
70 
71 typedef enum {
72 	WS_NONE = 0,
73 	WS_RECV_CLOSE = 1000,
74 	WS_PROTO_ERR = 1002,
75 	WS_DATA_TOO_BIG = 1009
76 } ws_cause_t;
77 
78 typedef enum {
79 	WSOC_CONTINUATION = 0x0,
80 	WSOC_TEXT = 0x1,
81 	WSOC_BINARY = 0x2,
82 	WSOC_CLOSE = 0x8,
83 	WSOC_PING = 0x9,
84 	WSOC_PONG = 0xA
85 } ws_opcode_t;
86 
87 typedef struct wsh_s {
88 	ws_socket_t sock;
89 	char *buffer;
90 	char *bbuffer;
91 	char *body;
92 	char *uri;
93 	size_t buflen;
94 	size_t bbuflen;
95 	ssize_t datalen;
96 	ssize_t wdatalen;
97 	char *payload;
98 	ssize_t plen;
99 	ssize_t rplen;
100 	ssize_t packetlen;
101 	SSL *ssl;
102 	int handshake;
103 	uint8_t down;
104 	int secure;
105 	uint8_t close_sock;
106 	SSL_CTX *ssl_ctx;
107 	int block;
108 	int sanity;
109 	int secure_established;
110 	int logical_established;
111 	int stay_open;
112 	int x;
113 	void *write_buffer;
114 	size_t write_buffer_len;
115 } wsh_t;
116 
117 ssize_t ws_send_buf(wsh_t *wsh, ws_opcode_t oc);
118 ssize_t ws_feed_buf(wsh_t *wsh, void *data, size_t bytes);
119 
120 
121 ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes, int block);
122 ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes);
123 ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data);
124 ssize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes);
125 int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock, int block, int stay_open);
126 ssize_t ws_close(wsh_t *wsh, int16_t reason);
127 void ws_destroy(wsh_t *wsh);
128 void init_ssl(void);
129 void deinit_ssl(void);
130 int xp_errno(void);
131 int xp_is_blocking(int errcode);
132 
133 
134 
135 #ifndef _MSC_VER
get_unaligned_uint64(const void * p)136 static inline uint64_t get_unaligned_uint64(const void *p)
137 {
138     const struct { uint64_t d; } __attribute__((packed)) *pp = p;
139     return pp->d;
140 }
141 #endif
142 
143 #endif
144