aboutsummaryrefslogtreecommitdiff
path: root/include/libwebsockets.hxx
blob: 4e395a763faf28c82a5965108c6d6d4498f76f45 (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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * libwebsockets - small server side websockets and web server implementation
 *
 * Copyright (C) 2020 Andy Green <andy@warmcat.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * C++ classes for Secure Streams
 */

#include <map>
#include <set>
#include <list>
#include <string>
#include <vector>
#include <exception>

#include "libwebsockets.h"

class lss;

/*
 * Exception subclass for lss-specific issues
 */

class lssException : public std::exception
{
private:
	std::string details;
public:
	lssException(std::string _details) { details = _details; }
	~lssException() throw() { }
	virtual const char *what() const throw() { return details.c_str(); }
};

typedef struct lssbuf {
	uint8_t				*buf;
	size_t				len;
} lssbuf_t;

class lssAc
{
private:
	struct lwsac			*ac;
	struct lwsac			*iter;
	lssAc() { ac = NULL; }
	~lssAc() { lwsac_free(&ac); }

public:
	void append(lssbuf_t *lb);
	void start(bool atomic);
	int get(lssbuf_t *lb);
};

/*
 * Fixed userdata priv used with ss creation... userdata lives in the lss
 * subclasses' members
 */

class lssPriv
{
public:
	struct lws_ss_handle		*m_ss;
	void				*m_plss;
};

#define userobj_to_lss(uo) ((lss *)(((lssPriv *)userobj)->m_plss))

/*
 * The completion callback... it's called once, and state will be one of
 *
 * LWSSSCS_QOS_ACK_REMOTE:     it completed OK
 * LWSSSCS_DESTROYING:         we didn't complete
 * LWSSSCS_ALL_RETRIES_FAILED:  "
 * LWSSSCS_QOS_NACK_REMOTE:     "
 */

typedef int (*lsscomp_t)(lss *lss, lws_ss_constate_t state, void *arg);

/*
 * Base class for Secure Stream objects
 */

class lss
{
public:
	lss(lws_ctx_t _ctx, std::string _uri, lsscomp_t _comp, bool _psh,
	    lws_sscb_rx rx, lws_sscb_tx tx, lws_sscb_state state);
	virtual ~lss();
	int call_completion(lws_ss_constate_t state);

	lsscomp_t			comp;
	struct lws_ss_handle		*m_ss;
	uint64_t			rxlen;
	lws_usec_t			us_start;

private:
	lws_ctx_t			ctx;
	char				*uri;
	lws_ss_policy_t			pol;
	bool				comp_done;
};

/*
 * Subclass of lss for atomic messages on heap
 */

class lssMsg : public lss
{
public:
	lssMsg(lws_ctx_t _ctx, lsscomp_t _comp, std::string _uri);
	virtual ~lssMsg();
};

/*
 * Subclass of lss for file transactions
 */

class lssFile : public lss
{
public:
	lssFile(lws_ctx_t _ctx, std::string _uri, std::string _path,
		lsscomp_t _comp, bool _psh);
	virtual ~lssFile();
	lws_ss_state_return_t write(const uint8_t *buf, size_t len, int flags);

	std::string			path;

private:
	lws_filefd_type			fd;
	bool				push;
};