src/server/daemon/websocket.h

changeset 127
84e206063b64
equal deleted inserted replaced
126:631aaa01b2b5 127:84e206063b64
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2016 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef WEBSOCKET_H
30 #define WEBSOCKET_H
31
32 #include "../public/nsapi.h"
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 #define WS_FRAMEHEADER_BUFLEN 16
39
40 typedef struct WSParser WSParser;
41 typedef struct WSFrame WSFrame;
42
43 // WSMessage is defined in nsapi.h
44
45 struct WSFrame {
46 uint8_t header_complete;
47 uint8_t fin;
48 uint8_t mask;
49 uint8_t opcode;
50 uint32_t masking_key;
51 uint64_t payload_length;
52 };
53
54 struct WSParser {
55 // input buffer size
56 const char *inbuf;
57 // inbuf data length
58 size_t length;
59 // current buffer position
60 size_t pos;
61
62 // current message or NULL
63 WSMessage *current;
64 // parser state: 0: parse frame, 1: get payload data
65 int state;
66 // current payload length
67 size_t cur_plen;
68 // current message frame
69 WSFrame frame;
70
71 // tmp buffer for incomplete frame header
72 char tmpbuf[WS_FRAMEHEADER_BUFLEN];
73 // length of tmpbuf
74 size_t tmplen;
75
76 pool_handle_t *pool;
77 };
78
79 /*
80 * creates a new websocket parser
81 */
82 WSParser* websocket_parser(Session *sn);
83
84 /*
85 * puts input data into the websocket parser
86 * you can use websocket_get_message() after that
87 */
88 void websocket_input(WSParser *parser, const char *data, size_t length);
89
90 /*
91 * tries to read a complete websocket message
92 * returns a pointer to an allocated WSMessage or NULL
93 * sets error to 1 if an error occurs, otherwise to 0
94 */
95 WSMessage* websocket_get_message(WSParser *parser, int *error);
96
97 /*
98 * reads a websocket frame util begin of the payload data
99 * returns position of payload data or 0 if the frame header is incomplete
100 */
101 ssize_t websocket_get_frameheader(WSFrame *frame, const char *buf, size_t len);
102
103 /*
104 * masks or unmasks data
105 * this is done by a simple xor with each element in buf and mask
106 */
107 void websocket_mask_data(char *buf, size_t len, uint32_t mask);
108
109
110 #ifdef __cplusplus
111 }
112 #endif
113
114 #endif /* WEBSOCKET_H */
115

mercurial