summaryrefslogtreecommitdiff
path: root/src/plugins/relay/relay-websocket.h
blob: 6e48569fda0ac892ca57bab52c1767fd521d4683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (C) 2013-2024 Sébastien Helleu <flashcode@flashtux.org>
 *
 * This file is part of WeeChat, the extensible chat client.
 *
 * WeeChat is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * WeeChat is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WeeChat.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef WEECHAT_PLUGIN_RELAY_WEBSOCKET_H
#define WEECHAT_PLUGIN_RELAY_WEBSOCKET_H

#include "relay.h"

#include <zlib.h>

#define WEBSOCKET_FRAME_OPCODE_CONTINUATION 0x00
#define WEBSOCKET_FRAME_OPCODE_TEXT         0x01
#define WEBSOCKET_FRAME_OPCODE_BINARY       0x02
#define WEBSOCKET_FRAME_OPCODE_CLOSE        0x08
#define WEBSOCKET_FRAME_OPCODE_PING         0x09
#define WEBSOCKET_FRAME_OPCODE_PONG         0x0A

struct t_relay_client;
struct t_relay_http_request;

struct t_relay_websocket_deflate
{
    int enabled;                       /* 1 if permessage-deflate is enabled*/
    int server_context_takeover;       /* context takeover for server       */
    int client_context_takeover;       /* context takeover for client       */
    int window_bits_deflate;           /* window bits for server (comp.)    */
                                       /* ("server_max_window_bits")        */
    int window_bits_inflate;           /* window bits for client (decomp.)  */
                                       /* ("client_max_window_bits")        */
    z_stream *strm_deflate;            /* stream for deflate (compression)  */
    z_stream *strm_inflate;            /* stream for inflate (decompression)*/
};

struct t_relay_websocket_frame
{
    int opcode;                        /* frame opcode                      */
    int payload_size;                  /* size of payload                   */
    char *payload;                     /* payload                           */
};

extern struct t_relay_websocket_deflate *relay_websocket_deflate_alloc ();
extern int relay_websocket_deflate_init_stream_deflate (struct t_relay_websocket_deflate *ws_deflate);
extern int relay_websocket_deflate_init_stream_inflate (struct t_relay_websocket_deflate *ws_deflate);
extern void relay_websocket_deflate_free (struct t_relay_websocket_deflate *ws_deflate);
extern int relay_websocket_is_valid_http_get (enum t_relay_protocol protocol,
                                              const char *message);
extern int relay_websocket_client_handshake_valid (struct t_relay_http_request *request);
extern void relay_websocket_parse_extensions (const char *extensions,
                                              struct t_relay_websocket_deflate *ws_deflate);
extern char *relay_websocket_build_handshake (struct t_relay_http_request *request);
extern int relay_websocket_decode_frame (struct t_relay_client *client,
                                         const unsigned char *buffer,
                                         unsigned long long length,
                                         struct t_relay_websocket_frame **frames,
                                         int *num_frames);
extern char *relay_websocket_encode_frame (struct t_relay_client *client,
                                           int opcode,
                                           const char *payload,
                                           unsigned long long payload_size,
                                           unsigned long long *length_frame);
extern void relay_websocket_deflate_print_log (struct t_relay_websocket_deflate *ws_deflate,
                                               const char *prefix);

#endif /* WEECHAT_PLUGIN_RELAY_WEBSOCKET_H */