-
Notifications
You must be signed in to change notification settings - Fork 0
/
libsws.c
109 lines (84 loc) · 2.14 KB
/
libsws.c
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <curl/curl.h>
#include <poll.h>
#include <unistd.h>
//defined in RFC 6455
struct base_frame
{
unsigned char byte_a;
/*
* 1 : fin frag
* 2-4: RSV
* 5-8: opcode
*/
unsigned char byte_b;
unsigned char byte_c;
/*
* uhhh can i die plz, this is hella low level
* i prolly need to use union for certain bytes that can be one thing or be another
* i still need to read into the RFC some more so this aint final
*/
};
/*
* use http/https for the host scheme rather than ws/wss
* Example: https://gateway.discord.gg
*/
static int ws_handshake(CURL *curl, const char *host, const char *key, unsigned char version)
{
//prepares headers for websocket handshake
long resp;
int rv;
rv = 0;
curl_slist_append(NULL, "Upgrade: websocket");
curl_slist_append(headers, "Connection: upgrade");
curl_slist_append(headers, "Sec-Websocket-Key: ?");
curl_slist_append(headers, "Sec-Websocket-Version: 13");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_preform(curl);
curl_easy_getinfo(curl, CURLINFO_HTTP_RESPONSECODE, &resp);
if(resp != 101)
goto err;
rv = 1;
err:
return rv;
}
int ws_connect(CURL *curl, const char *host, const char *key, unsigned char version)
{
int rv;
rv = 0;
if(ws_handshake(curl, host, key, version) != 1)
goto err;
curl_easy_pause(curl, CURLPAUSE_ALL);
curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET &rv);
err:
return rv;
}
CURL *ws_init(void)
{
return curl_easy_init();
}
void ws_cleanup(CURL *curl)
{
curl_easy_cleanup(curl);
}
void ws_close(CURL *curl)
{
}
ssize_t ws_read(struct websocket *ws, void *buf, size_t buflen)
{
}
ssize_t ws_write(struct websocket *ws, const void *buf, size_t buflen)
{
}
/*
* the easy variation of ws read/write are similar to the standard ws read/write
* functions except that it automatically de/encapsulates data in packets following
* the base framing protocol defined in RFC 6455
*/
char *ws_easy_read(struct websocket *ws, void *buf, size_t buflen)
{
//decapsulate and return a pointer to the application data
}
ssize_t ws_easy_write(struct websocket *ws, const void *buff, buflen)
{
//encapsulates application data accoridly and sends it
}