Skip to content

Commit 9891c74

Browse files
author
Richard Aas
committed
websock: added WebSocket client/server module
1 parent 38f4370 commit 9891c74

File tree

8 files changed

+832
-3
lines changed

8 files changed

+832
-3
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ include $(MK)
1818

1919
# List of modules
2020
MODULES += sip sipevent sipreg sipsess
21-
MODULES += uri http httpauth msg
21+
MODULES += uri http httpauth msg websock
2222
MODULES += stun turn ice
2323
MODULES += natbd
2424
MODULES += rtp sdp jbuf telev

docs/README

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Modules:
6161
* turn testing Obtaining Relay Addresses from STUN (TURN)
6262
* udp testing UDP transport
6363
* uri testing Generic URI library
64+
* websock unstable WebSocket Client and Server
6465

6566
legend:
6667
"stable" - Code complete; Stable code and stable API
@@ -111,6 +112,7 @@ Features:
111112
* RFC 5780 - NAT Behaviour Discovery Using STUN
112113
* RFC 6026 - Correct Transaction Handling for 2xx Resp. to SIP INVITE Requests
113114
* RFC 6156 - TURN Extension for IPv6
115+
* RFC 6455 - The WebSocket Protocol
114116
* Symmetric RTP
115117
* ITU-T G.711 Appendix I and Appendix II
116118
* draft-ietf-bfcpbis-rfc4582bis-08

include/re.h

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ extern "C" {
5555
#include "re_tls.h"
5656
#include "re_turn.h"
5757
#include "re_udp.h"
58+
#include "re_websock.h"
5859

5960
#ifdef __cplusplus
6061
}

include/re_http.h

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ int http_client_alloc(struct http_cli **clip, struct dnsc *dnsc);
122122
int http_request(struct http_req **reqp, struct http_cli *cli, const char *met,
123123
const char *uri, http_resp_h *resph, http_data_h *datah,
124124
void *arg, const char *fmt, ...);
125+
struct tcp_conn *http_req_tcp(struct http_req *req);
126+
struct tls_conn *http_req_tls(struct http_req *req);
125127

126128

127129
/* Server */

include/re_websock.h

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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);

src/http/client.c

+16-2
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,14 @@ int http_request(struct http_req **reqp, struct http_cli *cli, const char *met,
338338
&scheme, &host, NULL, &port, &path) || scheme.p != uri)
339339
return EINVAL;
340340

341-
if (!pl_strcasecmp(&scheme, "http")) {
341+
if (!pl_strcasecmp(&scheme, "http") ||
342+
!pl_strcasecmp(&scheme, "ws")) {
342343
secure = false;
343344
defport = 80;
344345
}
345346
#ifdef USE_TLS
346-
else if (!pl_strcasecmp(&scheme, "https")) {
347+
else if (!pl_strcasecmp(&scheme, "https") ||
348+
!pl_strcasecmp(&scheme, "wss")) {
347349
secure = true;
348350
defport = 443;
349351
}
@@ -455,3 +457,15 @@ int http_client_alloc(struct http_cli **clip, struct dnsc *dnsc)
455457

456458
return err;
457459
}
460+
461+
462+
struct tcp_conn *http_req_tcp(struct http_req *req)
463+
{
464+
return req ? req->tc : NULL;
465+
}
466+
467+
468+
struct tls_conn *http_req_tls(struct http_req *req)
469+
{
470+
return req ? req->sc : NULL;
471+
}

src/websock/mod.mk

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
# mod.mk
3+
#
4+
# Copyright (C) 2010 Creytiv.com
5+
#
6+
7+
SRCS += websock/websock.c

0 commit comments

Comments
 (0)