Tizen RT Libs&Environment  v1.0 D5
websocket.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the License.
16  *
17  ****************************************************************************/
30 
33 #ifndef __APPS_INCLUDE_NETUTILS_WEBSOCKET_H
34 #define __APPS_INCLUDE_NETUTILS_WEBSOCKET_H
35 
36 /****************************************************************************
37  * Included Files
38  ****************************************************************************/
39 
40 #include <stdint.h>
41 #include <netinet/in.h>
42 #include <apps/netutils/wslay/wslay.h>
43 #include <tinyara/wqueue.h>
44 
45 #include "tls/config.h"
46 #include "tls/entropy.h"
47 #include "tls/ctr_drbg.h"
48 #include "tls/certs.h"
49 #include "tls/x509.h"
50 #include "tls/ssl.h"
51 #include "tls/net.h"
52 #include "tls/error.h"
53 #include "tls/debug.h"
54 #include "tls/ssl_cache.h"
55 
56 #define WEBSOCKET_DEBUG_PRINT
57 
58 #if defined(WEBSOCKET_DEBUG_PRINT)
59 #define WEBSOCKET_DEBUG ndbg
60 #else
61 #define WEBSOCKET_DEBUG(...) do { } while (0)
62 #endif
63 
64 /****************************************************************************
65  * Pre-prcessor Definitions
66  ****************************************************************************/
67 
71 #define WS_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
72 
76 #define WEBSOCKET_STACKSIZE (1024 * 10)
77 
80 #define WEBSOCKET_PRI 100
81 
84 #define WEBSOCKET_SCHED_POLICY SCHED_RR
85 
88 #define WEBSOCKET_WQ LPWORK
89 
93 #define WEBSOCKET_CLIENT_KEY_LEN (24)
94 
97 #define WEBSOCKET_ACCEPT_KEY_LEN (29)
98 
101 #define WEBSOCKET_HANDSHAKE_HEADER_SIZE (2048)
102 
108 #ifdef CONFIG_HW_RSA_SIGN
109 #define WEBSOCKET_SERVERNAME "ARTIK051_dev"
110 #else
111 #define WEBSOCKET_SERVERNAME "localhost"
112 #endif
113 
117 #define WEBSOCKET_SOCK_RCV_TIMEOUT (5 * 1000) //mili second
118 
121 #define WEBSOCKET_SERVER_CHECK_INTERVAL (500) //mili second
122 
125 #define WEBSOCKET_SERVER_TIMEOUT (3 * 60 * 1000) //mili second
126 
130 #define WEBSOCKET_HANDLER_TIMEOUT (300) //mili second
131 
135 #define WEBSOCKET_MAX_PING_IGNORE (3) //times
136 
139 #define WEBSOCKET_PING_INTERVAL (20 * 100) //MSEC_PER_TICK
140 
144 #define WEBSOCKET_MAX_LENGTH_QUEUE (100 * 1024)
145 
148 #define WEBSOCKET_MAX_NUMBER_QUEUE (20)
149 
153 #define WEBSOCKET_MAX_CLIENT (3)
154 
155 /*
156  * Pre-definition variables from wslay below.
157  * just not to include wslay.h on application side.
158  */
159 
163 #define websocket_context_ptr wslay_event_context_ptr
164 
168 #define websocket_cb_t struct wslay_event_callbacks
169 
182 #define websocket_frame_t struct wslay_event_msg
183 
196 #define websocket_fragmented_frame_t struct wslay_event_fragmented_msg
197 
212 #define websocket_on_msg_arg struct wslay_event_on_msg_recv_arg
213 
227 #define websocket_recv_start_arg struct wslay_event_on_frame_recv_start_arg
228 
240 #define websocket_recv_chunk_arg struct wslay_event_on_frame_recv_chunk_arg
241 
242 /****************************************************************************
243  * Macros
244  ****************************************************************************/
245 
251 #define WEBSOCKET_CHECK_NOT_CTRL_FRAME(x) (!(((x) >> 3) & 1))
252 
258 #define WEBSOCKET_CHECK_TEXT_FRAME(x) (((uint8_t)(x)) == ((uint8_t)(WEBSOCKET_TEXT_FRAME)))
259 
265 #define WEBSOCKET_CHECK_BINARY_FRAME(x) (((uint8_t)(x)) == ((uint8_t)(WEBSOCKET_BINARY_FRAME)))
266 
272 #define WEBSOCKET_CHECK_CTRL_CLOSE(x) (((uint8_t)(x)) == ((uint8_t)(WEBSOCKET_CONNECTION_CLOSE)))
273 
279 #define WEBSOCKET_CHECK_CTRL_PING(x) (((uint8_t)(x)) == ((uint8_t)(WEBSOCKET_PING)))
280 
286 #define WEBSOCKET_CHECK_CTRL_PONG(x) (((uint8_t)(x)) == ((uint8_t)(WEBSOCKET_PONG)))
287 
288 /****************************************************************************
289  * Public Types
290  ****************************************************************************/
294 typedef enum {
306 
314 };
315 
321 };
322 
338 };
339 
352 };
353 
357 typedef struct {
358  int state;
360  int fd;
364  int ping_cnt;
370  mbedtls_net_context tls_net;
372  mbedtls_ssl_context *tls_ssl;
374  mbedtls_ssl_config *tls_conf;
376  struct work_s time_worker;
382 } websocket_t;
383 
392 };
393 
394 /****************************************************************************
395  * Public Function Prototypes
396  ****************************************************************************/
397 
398 #ifdef __cplusplus
399 #define EXTERN extern "C"
400 extern "C" {
401 #else
402 #define EXTERN extern
403 #endif
404 
416 void websocket_create_accept_key(unsigned char *dst, size_t dlen, const unsigned char *src, size_t slen);
417 
424 int websocket_count_table(void);
425 
436 
450 websocket_return_t websocket_client_open(websocket_t *client, char *addr, char *port, char *path);
451 
464 
476 
488 
489 /*
490  * @brief websocket_queue_msg() queues a message into websocket context.
491  *
492  * Websocket has a event handler thread and it keeps watching the queue to send or recv events.
493  * @param[in] ctx message queue is in websocket context
494  * @param[in] frame message frame to be sent
495  * @return On success return WEBSOCKET_SUCCESS, On failure return values defined in websocket_return_t
496  * @since Tizen RT v1.0
497  */
499 
511 
523 websocket_return_t websocket_queue_close(websocket_t *websocket, const char *close_message);
524 
535 void websocket_update_state(websocket_t *websocket, int state);
536 
547 void websocket_set_error(websocket_t *websocket, int val);
548 
549 #undef EXTERN
550 #undef EXTERN
551 #ifdef __cplusplus
552 }
553 #endif
554 // end of Websocket group
556 
557 #endif /* __APPS_INCLUDE_NETUTILS_WEBSOCKET_H */
websocket number of max state.
Definition: websocket.h:313
websocket_return_t websocket_server_init(websocket_t *server)
websocket_server_init
websocket context init fail
Definition: websocket.h:297
socket connecting fail
Definition: websocket.h:299
a normal websocket frame
Definition: websocket.h:347
websocket_cb_t * cb
Each server of client should define callbacks to use websocket functions.
Definition: websocket.h:368
int ping_cnt
Counter for ping message without receiving pong.
Definition: websocket.h:364
a control frame to ping
Definition: websocket.h:350
websocket_return_t websocket_queue_close(websocket_t *websocket, const char *close_message)
websocket_queue_close() closes the websocket.
message reading fail
Definition: websocket.h:302
TLS context init fail.
Definition: websocket.h:303
void websocket_set_error(websocket_t *websocket, int val)
websocket_set_error() sets an error number to websocket context.
int state
0 - closed 1 - running
Definition: websocket.h:358
int fd
Each server or client has own socket fd.
Definition: websocket.h:360
#define websocket_context_ptr
Websocket context pointer wrapper.
Definition: websocket.h:163
a control frame to close websocket
Definition: websocket.h:349
void websocket_update_state(websocket_t *websocket, int state)
websocket_update_state() changes websocket state.
mbedtls_ssl_config * tls_conf
TLS Initial configuration structure.
Definition: websocket.h:374
void websocket_create_accept_key(unsigned char *dst, size_t dlen, const unsigned char *src, size_t slen)
websocket_create_accept_key
websocket_return_t websocket_queue_ping(websocket_t *websocket)
websocket_queue_ping() is used to send a websocket ping message.
websocket_return_t websocket_queue_msg(websocket_t *websocket, websocket_frame_t *tx_frame)
structure to manage websocket client/server
Definition: websocket.h:357
message sending fail
Definition: websocket.h:301
pthread_attr_t thread_attr
Websocket event handler thread attribute.
Definition: websocket.h:380
data structure to use in callbacks.
Definition: websocket.h:390
websocket_context_ptr ctx
Websocket context to manage event queue, errors and configurations.
Definition: websocket.h:366
stopped
Definition: websocket.h:311
Structure of pthread attr configuration.
Definition: pthread.h:221
#define websocket_frame_t
Websocket structure wrapper to send a frame.
Definition: websocket.h:182
websocket_state
websocket states
Definition: websocket.h:310
int tls_enabled
TLS enable flag (1 - enable, 0 - disable)
Definition: websocket.h:362
int websocket_count_table(void)
websocket_count_table() returns amount of working websocket server.
a control frame to pong
Definition: websocket.h:351
void websocket_register_cb(websocket_t *websocket, websocket_cb_t *cb)
websocket_register_cb() changes a websocket callback structure in a websocket context.
http_server_state_t state
Definition: http_server.h:169
websocket_socket_error
websocket socket error codes
Definition: websocket.h:328
http_cb_t cb[4]
Definition: http_server.h:187
websocket_t * websocket_find_table(void)
websocket_find_table() returns websocket_t * to manage websocket server.
a fragmented frame
Definition: websocket.h:346
websocket_opcode
websocket operation code.
Definition: websocket.h:345
a binary websocket frame
Definition: websocket.h:348
websocket_return_t
websocket return definitions
Definition: websocket.h:294
websocket_t * data
Definition: websocket.h:391
websocket_return_t websocket_server_open(websocket_t *server)
websocket_server_open() initiates websocket server.
#define websocket_cb_t
Websocket structure wrapper to carry call back pointers.
Definition: websocket.h:168
http handshake fail
Definition: websocket.h:300
websocket_return_t websocket_client_open(websocket_t *client, char *addr, char *port, char *path)
websocket_client_open() initiates websocket client ready to send and receive websocket message...
pthread_t thread_id
Websocket event handler thread ID.
Definition: websocket.h:378
mbedtls_net_context tls_net
Contain network file descriptor.
Definition: websocket.h:370
socket setting fail
Definition: websocket.h:298
mbedtls_ssl_context * tls_ssl
Handling all tls context informations.
Definition: websocket.h:372
TLS handshake fail.
Definition: websocket.h:304
pid_t pthread_t
Definition: pthread.h:230
websocket_io_flag
websocket io flags
Definition: websocket.h:319