James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file re_websock.h The WebSocket Protocol |
| 3 | * |
| 4 | * Copyright (C) 2010 Creytiv.com |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | enum { |
| 9 | WEBSOCK_VERSION = 13, |
| 10 | }; |
| 11 | |
| 12 | enum websock_opcode { |
| 13 | /* Data frames */ |
| 14 | WEBSOCK_CONT = 0x0, |
| 15 | WEBSOCK_TEXT = 0x1, |
| 16 | WEBSOCK_BIN = 0x2, |
| 17 | /* Control frames */ |
| 18 | WEBSOCK_CLOSE = 0x8, |
| 19 | WEBSOCK_PING = 0x9, |
| 20 | WEBSOCK_PONG = 0xa, |
| 21 | }; |
| 22 | |
| 23 | enum websock_scode { |
| 24 | WEBSOCK_NORMAL_CLOSURE = 1000, |
| 25 | WEBSOCK_GOING_AWAY = 1001, |
| 26 | WEBSOCK_PROTOCOL_ERROR = 1002, |
| 27 | WEBSOCK_UNSUPPORTED_DATA = 1003, |
| 28 | WEBSOCK_INVALID_PAYLOAD = 1007, |
| 29 | WEBSOCK_POLICY_VIOLATION = 1008, |
| 30 | WEBSOCK_MESSAGE_TOO_BIG = 1009, |
| 31 | WEBSOCK_EXTENSION_ERROR = 1010, |
| 32 | WEBSOCK_INTERNAL_ERROR = 1011, |
| 33 | }; |
| 34 | |
| 35 | struct websock_hdr { |
| 36 | unsigned fin:1; |
| 37 | unsigned rsv1:1; |
| 38 | unsigned rsv2:1; |
| 39 | unsigned rsv3:1; |
| 40 | unsigned opcode:4; |
| 41 | unsigned mask:1; |
| 42 | uint64_t len; |
| 43 | uint8_t mkey[4]; |
| 44 | }; |
| 45 | |
| 46 | struct websock; |
| 47 | struct websock_conn; |
| 48 | |
| 49 | typedef void (websock_estab_h)(void *arg); |
| 50 | typedef void (websock_recv_h)(const struct websock_hdr *hdr, struct mbuf *mb, |
| 51 | void *arg); |
| 52 | typedef void (websock_close_h)(int err, void *arg); |
| 53 | |
| 54 | |
| 55 | int websock_connect(struct websock_conn **connp, struct websock *sock, |
| 56 | struct http_cli *cli, const char *uri, unsigned kaint, |
| 57 | websock_estab_h *estabh, websock_recv_h *recvh, |
| 58 | websock_close_h *closeh, void *arg, |
| 59 | const char *fmt, ...); |
| 60 | int websock_accept(struct websock_conn **connp, struct websock *sock, |
| 61 | struct http_conn *htconn, const struct http_msg *msg, |
| 62 | unsigned kaint, websock_recv_h *recvh, |
| 63 | websock_close_h *closeh, void *arg); |
| 64 | int websock_send(struct websock_conn *conn, enum websock_opcode opcode, |
| 65 | const char *fmt, ...); |
| 66 | int websock_close(struct websock_conn *conn, enum websock_scode scode, |
| 67 | const char *fmt, ...); |
| 68 | const struct sa *websock_peer(const struct websock_conn *conn); |
| 69 | |
| 70 | typedef void (websock_shutdown_h)(void *arg); |
| 71 | |
| 72 | int websock_alloc(struct websock **sockp, websock_shutdown_h *shuth, |
| 73 | void *arg); |
| 74 | void websock_shutdown(struct websock *sock); |