1 #include <openssl/ssl.h>
2 
3 #define BUFSIZE 65536
4 #define DBUFSIZE (BUFSIZE * 3) / 4 - 20
5 
6 #define SERVER_HANDSHAKE_HIXIE "HTTP/1.1 101 Web Socket Protocol Handshake\r\n\
7 Upgrade: WebSocket\r\n\
8 Connection: Upgrade\r\n\
9 %sWebSocket-Origin: %s\r\n\
10 %sWebSocket-Location: %s://%s%s\r\n\
11 %sWebSocket-Protocol: %s\r\n\
12 \r\n%s"
13 
14 #define SERVER_HANDSHAKE_HYBI "HTTP/1.1 101 Switching Protocols\r\n\
15 Upgrade: websocket\r\n\
16 Connection: Upgrade\r\n\
17 Sec-WebSocket-Accept: %s\r\n\
18 Sec-WebSocket-Protocol: %s\r\n\
19 \r\n"
20 
21 #define HYBI_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
22 
23 #define HYBI10_ACCEPTHDRLEN 29
24 
25 #define HIXIE_MD5_DIGEST_LENGTH 16
26 
27 #define POLICY_RESPONSE "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>\n"
28 
29 typedef struct {
30     char path[1024+1];
31     char host[1024+1];
32     char origin[1024+1];
33     char version[1024+1];
34     char connection[1024+1];
35     char protocols[1024+1];
36     char key1[1024+1];
37     char key2[1024+1];
38     char key3[8+1];
39 } headers_t;
40 
41 typedef struct {
42     int        sockfd;
43     SSL_CTX   *ssl_ctx;
44     SSL       *ssl;
45     int        hixie;
46     int        hybi;
47     headers_t *headers;
48     char      *cin_buf;
49     char      *cout_buf;
50     char      *tin_buf;
51     char      *tout_buf;
52 } ws_ctx_t;
53 
54 typedef struct {
55     int verbose;
56     char listen_host[256];
57     int listen_port;
58     void (*handler)(ws_ctx_t*);
59     int handler_id;
60     char *cert;
61     char *key;
62     int ssl_only;
63     int daemon;
64     int run_once;
65 } settings_t;
66 
67 
68 ssize_t ws_recv(ws_ctx_t *ctx, void *buf, size_t len);
69 
70 ssize_t ws_send(ws_ctx_t *ctx, const void *buf, size_t len);
71 
72 /* base64.c declarations */
73 //int b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize);
74 //int b64_pton(char const *src, u_char *target, size_t targsize);
75 
76 #define gen_handler_msg(stream, ...) \
77     if (! settings.daemon) { \
78         fprintf(stream, "  %d: ", settings.handler_id); \
79         fprintf(stream, __VA_ARGS__); \
80     }
81 
82 #define handler_msg(...) gen_handler_msg(stdout, __VA_ARGS__);
83 #define handler_emsg(...) gen_handler_msg(stderr, __VA_ARGS__);
84 
85