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 #if _MSC_VER < 1900
44 #define snprintf _snprintf
45 #endif
46 #ifdef _WIN64
47 #define WS_SSIZE_T __int64
48 #elif _MSC_VER >= 1400
49 #define WS_SSIZE_T __int32 __w64
50 #else
51 #define WS_SSIZE_T __int32
52 #endif
53 typedef WS_SSIZE_T ssize_t;
54 #endif
55
56
57 struct ws_globals_s {
58 const SSL_METHOD *ssl_method;
59 SSL_CTX *ssl_ctx;
60 char cert[512];
61 char key[512];
62 };
63
64 //extern struct ws_globals_s ws_globals;
65
66 #ifndef WIN32
67 typedef int ws_socket_t;
68 #else
69 typedef SOCKET ws_socket_t;
70 #endif
71 #define ws_sock_invalid (ws_socket_t)-1
72
73
74 typedef enum {
75 WS_NONE = 0,
76 WS_NORMAL = 1000,
77 WS_PROTO_ERR = 1002,
78 WS_DATA_TOO_BIG = 1009
79 } ws_cause_t;
80
81 typedef enum {
82 WSOC_CONTINUATION = 0x0,
83 WSOC_TEXT = 0x1,
84 WSOC_BINARY = 0x2,
85 WSOC_CLOSE = 0x8,
86 WSOC_PING = 0x9,
87 WSOC_PONG = 0xA
88 } ws_opcode_t;
89
90 typedef struct wsh_s {
91 ws_socket_t sock;
92 char *buffer;
93 char *bbuffer;
94 char *body;
95 char *uri;
96 size_t buflen;
97 size_t bbuflen;
98 ssize_t datalen;
99 char *payload;
100 ssize_t plen;
101 ssize_t rplen;
102 ssize_t packetlen;
103 SSL *ssl;
104 int handshake;
105 uint8_t down;
106 int secure;
107 uint8_t close_sock;
108 SSL_CTX *ssl_ctx;
109 int block;
110 int sanity;
111 int secure_established;
112 int logical_established;
113 int stay_open;
114 int x;
115 void *write_buffer;
116 size_t write_buffer_len;
117 } wsh_t;
118
119 ssize_t ws_send_buf(wsh_t *wsh, ws_opcode_t oc);
120 ssize_t ws_feed_buf(wsh_t *wsh, void *data, size_t bytes);
121
122 int establish_logical_layer(wsh_t *wsh);
123 ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes, int block);
124 ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes);
125 ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data);
126 ssize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes);
127 int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock, int block, int stay_open);
128 ssize_t ws_close(wsh_t *wsh, int16_t reason);
129 void ws_destroy(wsh_t *wsh);
130 void init_ssl(void);
131 void deinit_ssl(void);
132 int xp_errno(void);
133 int xp_is_blocking(int errcode);
134
135
136
137 #ifndef _MSC_VER
get_unaligned_uint64(const void * p)138 static inline uint64_t get_unaligned_uint64(const void *p)
139 {
140 const struct { uint64_t d; } __attribute__((packed)) *pp = p;
141 return pp->d;
142 }
143 #endif
144
145 #endif
146