FreeRDP-WebConnect WebSockets gateway  1.0.0.167
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
wscommon.hpp
1 /*
2  * This file has been derived from the WebSockets++ project at
3  * https://github.com/zaphoyd/websocketpp which is licensed under a BSD-license.
4  *
5  * Copyright (c) 2011, Peter Thorson. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the WebSocket++ Project nor the
15  * names of its contributors may be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  */
30 
31 #ifndef WEBSOCKET_CONSTANTS_HPP
32 #define WEBSOCKET_CONSTANTS_HPP
33 
34 #ifndef __STDC_LIMIT_MACROS
35 #define __STDC_LIMIT_MACROS
36 #endif
37 #include <boost/cstdint.hpp>
38 
39 // SIZE_MAX appears to be a compiler thing not an OS header thing.
40 // make sure it is defined.
41 #ifndef SIZE_MAX
42 #define SIZE_MAX ((size_t)(-1))
43 #endif
44 
45 #include <exception>
46 #include <string>
47 #include <vector>
48 
49 #include <boost/shared_ptr.hpp>
50 
57 namespace wspp {
58 
59  namespace session {
60  namespace state {
61  enum value {
62  CONNECTING = 0,
63  OPEN = 1,
64  CLOSING = 2,
65  CLOSED = 3
66  };
67  }
68  }
69 
70  namespace close {
71  namespace status {
72  enum value {
73  INVALID_END = 999,
74  NORMAL = 1000,
75  GOING_AWAY = 1001,
76  PROTOCOL_ERROR = 1002,
77  UNSUPPORTED_DATA = 1003,
78  RSV_ADHOC_1 = 1004,
79  NO_STATUS = 1005,
80  ABNORMAL_CLOSE = 1006,
81  INVALID_PAYLOAD = 1007,
82  POLICY_VIOLATION = 1008,
83  MESSAGE_TOO_BIG = 1009,
84  EXTENSION_REQUIRE = 1010,
85  INTERNAL_ENDPOINT_ERROR = 1011,
86  RSV_ADHOC_2 = 1012,
87  RSV_ADHOC_3 = 1013,
88  RSV_ADHOC_4 = 1014,
89  TLS_HANDSHAKE = 1015,
90  RSV_START = 1016,
91  RSV_END = 2999,
92  INVALID_START = 5000
93  };
94 
95  inline bool reserved(value s) {
96  return ((s >= RSV_START && s <= RSV_END) || s == RSV_ADHOC_1
97  || s == RSV_ADHOC_2 || s == RSV_ADHOC_3 || s == RSV_ADHOC_4);
98  }
99 
100  // Codes invalid on the wire
101  inline bool invalid(value s) {
102  return ((s <= INVALID_END || s >= INVALID_START) ||
103  s == NO_STATUS ||
104  s == ABNORMAL_CLOSE ||
105  s == TLS_HANDSHAKE);
106  }
107 
108  // TODO functions for application ranges?
109  } // namespace status
110  } // namespace close
111 
112  namespace frame {
113  // Opcodes are 4 bits
114  // See spec section 5.2
115  namespace opcode {
116  enum value {
117  CONTINUATION = 0x0,
118  TEXT = 0x1,
119  BINARY = 0x2,
120  RSV3 = 0x3,
121  RSV4 = 0x4,
122  RSV5 = 0x5,
123  RSV6 = 0x6,
124  RSV7 = 0x7,
125  CLOSE = 0x8,
126  PING = 0x9,
127  PONG = 0xA,
128  CONTROL_RSVB = 0xB,
129  CONTROL_RSVC = 0xC,
130  CONTROL_RSVD = 0xD,
131  CONTROL_RSVE = 0xE,
132  CONTROL_RSVF = 0xF
133  };
134 
135  inline bool reserved(value v) {
136  return (v >= RSV3 && v <= RSV7) ||
137  (v >= CONTROL_RSVB && v <= CONTROL_RSVF);
138  }
139 
140  inline bool invalid(value v) {
141  return (v > 0xF || v < 0);
142  }
143 
144  inline bool is_control(value v) {
145  return v >= 0x8;
146  }
147  }
148 
149  namespace limits {
150  static const uint8_t PAYLOAD_SIZE_BASIC = 125;
151  static const uint16_t PAYLOAD_SIZE_EXTENDED = 0xFFFF; // 2^16, 65535
152  static const uint64_t PAYLOAD_SIZE_JUMBO = 0x7FFFFFFFFFFFFFFFLL;//2^63
153  }
154  } // namespace frame
155 }
156 
157 #endif // WEBSOCKET_CONSTANTS_HPP