File: abstract.c

package info (click to toggle)
libwebsockets 4.3.5-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 31,404 kB
  • sloc: ansic: 194,409; javascript: 1,550; sh: 1,387; cpp: 505; java: 461; perl: 405; xml: 118; makefile: 76; awk: 5
file content (355 lines) | stat: -rw-r--r-- 8,610 bytes parent folder | download | duplicates (4)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * libwebsockets - small server side websockets and web server implementation
 *
 * Copyright (C) 2010 - 2019 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.
 */

#include <private-lib-core.h>
#include <private-lib-abstract.h>

extern const lws_abs_transport_t lws_abs_transport_cli_raw_skt,
				 lws_abs_transport_cli_unit_test;
#if defined(LWS_WITH_SMTP)
extern const lws_abs_protocol_t lws_abs_protocol_smtp;
#endif
#if defined(LWS_WITH_MQTT)
extern const lws_abs_protocol_t lws_abs_protocol_mqttc;
#endif

static const lws_abs_transport_t * const available_abs_transports[] = {
	&lws_abs_transport_cli_raw_skt,
	&lws_abs_transport_cli_unit_test,
};

#if defined(LWS_WITH_ABSTRACT)
static const lws_abs_protocol_t * const available_abs_protocols[] = {
#if defined(LWS_WITH_SMTP)
	&lws_abs_protocol_smtp,
#endif
#if defined(LWS_WITH_MQTT)
	&lws_abs_protocol_mqttc,
#endif
};
#endif

const lws_abs_transport_t *
lws_abs_transport_get_by_name(const char *name)
{
	int n;

	for (n = 0; n < (int)LWS_ARRAY_SIZE(available_abs_transports); n++)
		if (!strcmp(name, available_abs_transports[n]->name))
			return available_abs_transports[n];

	lwsl_err("%s: cannot find '%s'\n", __func__, name);

	return NULL;
}

const lws_abs_protocol_t *
lws_abs_protocol_get_by_name(const char *name)
{
#if defined(LWS_WITH_ABSTRACT)
	int n;

	for (n = 0; n < (int)LWS_ARRAY_SIZE(available_abs_protocols); n++)
		if (!strcmp(name, available_abs_protocols[n]->name))
			return available_abs_protocols[n];
#endif
	lwsl_err("%s: cannot find '%s'\n", __func__, name);

	return NULL;
}

const lws_token_map_t *
lws_abs_get_token(const lws_token_map_t *token_map, short name_index)
{
	if (!token_map)
		return NULL;

	do {
		if (token_map->name_index == name_index)
			return token_map;
		token_map++;
	} while (token_map->name_index);

	return NULL;
}

static int
lws_abstract_compare_connection(lws_abs_t *abs1, lws_abs_t *abs2)
{
	/* it has to be using the same protocol */
	if (abs1->ap != abs2->ap)
		return 1;

	/* protocol has to allow some kind of binding */
	if (!abs1->ap->flags)
		return 1;

	/* it has to be using the same transport */
	if (abs1->at != abs2->at)
		return 1;

	/*
	 * The transport must feel the endpoint and conditions in use match the
	 * requested endpoint and conditions... and the transport type must be
	 * willing to allow it
	 */
	if (abs1->at->compare(abs1, abs2))
		return 1;

	/*
	 * The protocol must feel they are in compatible modes if any
	 * (and the protocol type must be willing to allow it)
	 */
	if (abs1->ap->compare(abs1, abs2))
		return 1;

	/*
	 * If no objection by now, we can say there's already a comparable
	 * connection and both the protocol and transport feel we can make
	 * use of it.
	 */

	return 0;
}

static int
find_compatible(struct lws_dll2 *d, void *user)
{
	lws_abs_t *ai1 = (lws_abs_t *)user,
		  *ai2 = lws_container_of(d, lws_abs_t, abstract_instances);

	if (!lws_abstract_compare_connection(ai1, ai2)) {
		/* we can bind to it */
		lws_dll2_add_tail(&ai1->bound, &ai2->children_owner);

		return 1;
	}

	return 0;
}

lws_abs_t *
lws_abs_bind_and_create_instance(const lws_abs_t *abs)
{
	size_t size = sizeof(lws_abs_t) + abs->ap->alloc + abs->at->alloc;
	lws_abs_t *ai;
	int n;

	/*
	 * since we know we will allocate the lws_abs_t, the protocol's
	 * instance allocation, and the transport's instance allocation,
	 * we merge it into a single heap allocation
	 */
	ai = lws_malloc(size, "abs inst");
	if (!ai)
		return NULL;

	*ai = *abs;
	ai->ati = NULL;

	ai->api = (char *)ai + sizeof(lws_abs_t);

	if (!ai->ap->flags) /* protocol only understands single connections */
		goto fresh;

	lws_vhost_lock(ai->vh); /* ----------------------------------- vh { */

	/*
	 * Let's have a look for any already-connected transport we can use
	 */

	n = lws_dll2_foreach_safe(&ai->vh->abstract_instances_owner, ai,
				  find_compatible);

	lws_vhost_unlock(ai->vh); /* } vh --------------------------------- */

	if (n)
		goto vh_list_add;

	/* there's no existing connection doing what we want */

fresh:

	ai->ati = (char *)ai->api + abs->ap->alloc;
	if (ai->at->create(ai)) {
		ai->ati = NULL;
		goto bail;
	}

vh_list_add:
	/* add us to the vhost's dll2 of instances */

	lws_dll2_clear(&ai->abstract_instances);
	lws_dll2_add_head(&ai->abstract_instances,
			  &ai->vh->abstract_instances_owner);

	if (ai->ap->create(ai)) {
		ai->api = NULL;
		goto bail;
	}

	if (ai->bound.owner) { /* we are a piggybacker */
		lws_abs_t *ai2 = lws_container_of(ai->bound.owner, lws_abs_t,
						  children_owner);
		/*
		 * Provide an 'event' in the parent context to start handling
		 * the bind if it's otherwise idle.  We give the parent abs
		 * because we don't know if we're "next" or whatever.  Just that
		 * a child joined him and he should look into his child
		 * situation in case he was waiting for one to appear.
		 */
		if (ai2->ap->child_bind(ai2)) {
			lwsl_info("%s: anticpated child bind fail\n", __func__);
			lws_dll2_remove(&ai->bound);

			goto bail;
		}
	}

	return ai;

bail:
	lws_abs_destroy_instance(&ai);

	return NULL;
}

/*
 * We get called to clean up each child that was still bound to a parent
 * at the time the parent is getting destroyed.
 */

static void
__lws_abs_destroy_instance2(lws_abs_t **ai)
{
	lws_abs_t *a = *ai;

	if (a->api)
		a->ap->destroy(&a->api);
	if (a->ati)
		a->at->destroy(&a->ati);

	lws_dll2_remove(&a->abstract_instances);

	*ai = NULL;
	free(a);
}

static int
__reap_children(struct lws_dll2 *d, void *user)
{
	lws_abs_t *ac = lws_container_of(d, lws_abs_t, bound);

	lws_dll2_foreach_safe(&ac->children_owner, NULL, __reap_children);

	/* then destroy ourselves */

	__lws_abs_destroy_instance2(&ac);

	return 0;
}

void
lws_abs_destroy_instance(lws_abs_t **ai)
{
	lws_abs_t *a = *ai;

	/* destroy child instances that are bound to us first... */

	lws_vhost_lock(a->vh); /* ----------------------------------- vh { */

	lws_dll2_foreach_safe(&a->children_owner, NULL, __reap_children);

	/* ...then destroy ourselves */

	__lws_abs_destroy_instance2(ai);

	lws_vhost_unlock(a->vh); /* } vh --------------------------------- */
}

lws_abs_t *
lws_abstract_alloc(struct lws_vhost *vhost, void *user,
		   const char *abstract_path, const lws_token_map_t *ap_tokens,
		   const lws_token_map_t *at_tokens, struct lws_sequencer *seq,
		   void *opaque_user_data)
{
	lws_abs_t *abs = lws_zalloc(sizeof(*abs), __func__);
	struct lws_tokenize ts;
	lws_tokenize_elem e;
	char tmp[30];

	if (!abs)
		return NULL;

	lws_tokenize_init(&ts, abstract_path, LWS_TOKENIZE_F_MINUS_NONTERM);

	e = lws_tokenize(&ts);
	if (e != LWS_TOKZE_TOKEN)
		goto abs_path_problem;

	if (lws_tokenize_cstr(&ts, tmp, sizeof(tmp)))
		goto abs_path_problem;

	abs->ap = lws_abs_protocol_get_by_name(tmp);
	if (!abs->ap)
		goto abs_path_problem;

	e = lws_tokenize(&ts);
	if (e != LWS_TOKZE_DELIMITER)
		goto abs_path_problem;

	e = lws_tokenize(&ts);
	if (e != LWS_TOKZE_TOKEN)
		goto abs_path_problem;

	if (lws_tokenize_cstr(&ts, tmp, sizeof(tmp)))
		goto abs_path_problem;

	abs->at = lws_abs_transport_get_by_name(tmp);
	if (!abs->at)
		goto abs_path_problem;

	abs->vh = vhost;
	abs->ap_tokens = ap_tokens;
	abs->at_tokens = at_tokens;
	abs->seq = seq;
	abs->opaque_user_data = opaque_user_data;

	lwsl_info("%s: allocated %s\n", __func__, abstract_path);

	return abs;

abs_path_problem:
	lwsl_err("%s: bad abs path '%s'\n", __func__, abstract_path);
	lws_free_set_NULL(abs);

	return NULL;
}

void
lws_abstract_free(lws_abs_t **pabs)
{
	if (*pabs)
		lws_free_set_NULL(*pabs);
}