aboutsummaryrefslogtreecommitdiff
path: root/src/core/options.c
blob: bdbcaababa5e4095d1b5334a1c646eb99a2d5638 (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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
// Copyright 2017 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include "core/nng_impl.h"

#include <stdio.h>
#include <string.h>

// Dynamic options.

typedef struct nni_option nni_option;
struct nni_option {
	nni_list_node o_link;
	char *        o_name;
	int           o_id;
};

static nni_mtx  nni_option_lk;
static nni_list nni_options;
static int      nni_option_nextid;

int
nni_chkopt_usec(const void *v, size_t sz)
{
	nni_duration val;
	if (sz != sizeof(val)) {
		return (NNG_EINVAL);
	}
	memcpy(&val, v, sz);
	if (val < -1) {
		return (NNG_EINVAL);
	}
	return (0);
}

int
nni_chkopt_int(const void *v, size_t sz, int minv, int maxv)
{
	int val;
	if (sz != sizeof(val)) {
		return (NNG_EINVAL);
	}
	memcpy(&val, v, sz);
	if ((val < minv) || (val > maxv)) {
		return (NNG_EINVAL);
	}
	return (0);
}

int
nni_chkopt_size(const void *v, size_t sz, size_t minv, size_t maxv)
{
	size_t val;
	if (sz != sizeof(val)) {
		return (NNG_EINVAL);
	}
	memcpy(&val, v, sz);
	if ((val < minv) || (val > maxv)) {
		return (NNG_EINVAL);
	}
	return (0);
}

int
nni_setopt_usec(nni_duration *dp, const void *v, size_t sz)
{
	nni_duration dur;

	if (sz != sizeof(*dp)) {
		return (NNG_EINVAL);
	}
	memcpy(&dur, v, sizeof(dur));
	if (dur < -1) {
		return (NNG_EINVAL);
	}
	*dp = dur;
	return (0);
}

int
nni_setopt_int(int *ip, const void *v, size_t sz, int minv, int maxv)
{
	int i;

	if (sz != sizeof(i)) {
		return (NNG_EINVAL);
	}
	memcpy(&i, v, sizeof(i));
	if (i > maxv) {
		return (NNG_EINVAL);
	}
	if (i < minv) {
		return (NNG_EINVAL);
	}
	*ip = i;
	return (0);
}

int
nni_setopt_size(size_t *sp, const void *v, size_t sz, size_t minv, size_t maxv)
{
	size_t val;

	if (sz != sizeof(val)) {
		return (NNG_EINVAL);
	}
	memcpy(&val, v, sizeof(val));
	if (val > maxv) {
		return (NNG_EINVAL);
	}
	if (val < minv) {
		return (NNG_EINVAL);
	}
	*sp = val;
	return (0);
}

int
nni_getopt_usec(nni_duration *ptr, void *val, size_t *sizep)
{
	size_t sz = sizeof(*ptr);

	if (sz > *sizep) {
		sz = *sizep;
	}
	*sizep = sizeof(*ptr);
	memcpy(val, ptr, sz);
	return (0);
}

int
nni_getopt_int(int *ptr, void *val, size_t *sizep)
{
	size_t sz = sizeof(*ptr);

	if (sz > *sizep) {
		sz = *sizep;
	}
	*sizep = sizeof(*ptr);
	memcpy(val, ptr, sz);
	return (0);
}

int
nni_getopt_size(size_t *ptr, void *val, size_t *sizep)
{
	size_t sz = sizeof(*ptr);

	if (sz > *sizep) {
		sz = *sizep;
	}
	*sizep = sizeof(*ptr);
	memcpy(val, ptr, sz);
	return (0);
}

int
nni_setopt_buf(nni_msgq *mq, const void *val, size_t sz)
{
	int len;

	if (sz < sizeof(len)) {
		return (NNG_EINVAL);
	}
	memcpy(&len, val, sizeof(len));
	if (len < 0) {
		return (NNG_EINVAL);
	}
	if (len > 8192) {
		// put a reasonable uppper limit on queue depth.
		// This is a count in messages, so the total queue
		// size could be quite large indeed in this case.
		return (NNG_EINVAL);
	}
	return (nni_msgq_resize(mq, len));
}

int
nni_getopt_buf(nni_msgq *mq, void *val, size_t *sizep)
{
	int len = nni_msgq_cap(mq);

	size_t sz = *sizep;

	if (sz > sizeof(len)) {
		sz = sizeof(len);
	}
	memcpy(val, &len, sz);
	*sizep = sizeof(len);
	return (0);
}

static void
nni_notifyfd_push(struct nng_event *ev, void *arg)
{
	nni_notifyfd *fd = arg;

	NNI_ARG_UNUSED(ev);

	nni_plat_pipe_raise(fd->sn_wfd);
}

int
nni_getopt_fd(nni_sock *s, nni_notifyfd *fd, int mask, void *val, size_t *szp)
{
	int      rv;
	uint32_t flags;

	if ((*szp < sizeof(int))) {
		return (NNG_EINVAL);
	}

	flags = nni_sock_flags(s);

	switch (mask) {
	case NNG_EV_CAN_SND:
		if ((flags & NNI_PROTO_FLAG_SND) == 0) {
			return (NNG_ENOTSUP);
		}
		break;
	case NNG_EV_CAN_RCV:
		if ((flags & NNI_PROTO_FLAG_RCV) == 0) {
			return (NNG_ENOTSUP);
		}
		break;
	default:
		return (NNG_ENOTSUP);
	}

	// If we already inited this, just give back the same file descriptor.
	if (fd->sn_init) {
		memcpy(val, &fd->sn_rfd, sizeof(int));
		*szp = sizeof(int);
		return (0);
	}

	if ((rv = nni_plat_pipe_open(&fd->sn_wfd, &fd->sn_rfd)) != 0) {
		return (rv);
	}

	if (nni_sock_notify(s, mask, nni_notifyfd_push, fd) == NULL) {
		nni_plat_pipe_close(fd->sn_wfd, fd->sn_rfd);
		return (NNG_ENOMEM);
	}

	fd->sn_init = 1;
	*szp        = sizeof(int);
	memcpy(val, &fd->sn_rfd, sizeof(int));
	return (0);
}

// nni_option_set_id sets the id for an option, if not already done so.
// (Some options have hard coded values that end-user may depend upon.)
// If the ID passed in is negative, then a new ID is allocated dynamically.
static int
nni_option_set_id(const char *name, int id)
{
	nni_option *opt;
	int         len;
	nni_mtx_lock(&nni_option_lk);
	NNI_LIST_FOREACH (&nni_options, opt) {
		if (strcmp(name, opt->o_name) == 0) {
			nni_mtx_unlock(&nni_option_lk);
			return (0);
		}
	}
	if ((opt = NNI_ALLOC_STRUCT(opt)) == NULL) {
		nni_mtx_unlock(&nni_option_lk);
		return (NNG_ENOMEM);
	}
	len = strlen(name) + 1;
	if ((opt->o_name = nni_alloc(len)) == NULL) {
		nni_mtx_unlock(&nni_option_lk);
		NNI_FREE_STRUCT(opt);
		return (NNG_ENOMEM);
	}
	(void) snprintf(opt->o_name, len, "%s", name);
	if (id < 0) {
		id = nni_option_nextid++;
	}
	opt->o_id = id;
	nni_list_append(&nni_options, opt);
	nni_mtx_unlock(&nni_option_lk);
	return (0);
}

int
nni_option_lookup(const char *name)
{
	nni_option *opt;
	int         id = -1;

	nni_mtx_lock(&nni_option_lk);
	NNI_LIST_FOREACH (&nni_options, opt) {
		if (strcmp(name, opt->o_name) == 0) {
			id = opt->o_id;
			break;
		}
	}
	nni_mtx_unlock(&nni_option_lk);
	return (id);
}

const char *
nni_option_name(int id)
{
	nni_option *opt;
	const char *name = NULL;

	nni_mtx_lock(&nni_option_lk);
	NNI_LIST_FOREACH (&nni_options, opt) {
		if (id == opt->o_id) {
			name = opt->o_name;
			break;
		}
	}
	nni_mtx_unlock(&nni_option_lk);
	return (name);
}

int
nni_option_register(const char *name, int *idp)
{
	int rv;

	// Note that if the id was already in use, we will
	// wind up leaving a gap in the ID space.  That should
	// be inconsequential.
	if ((rv = nni_option_set_id(name, -1)) != 0) {
		return (rv);
	}
	*idp = nni_option_lookup(name);
	return (0);
}

int
nni_option_sys_init(void)
{
	nni_mtx_init(&nni_option_lk);
	NNI_LIST_INIT(&nni_options, nni_option, o_link);
	nni_option_nextid = 0x10000;
	int rv;

	// Register our well-known options.
	if (((rv = nni_option_set_id("raw", NNG_OPT_RAW)) != 0) ||
	    ((rv = nni_option_set_id("linger", NNG_OPT_LINGER)) != 0) ||
	    ((rv = nni_option_set_id("recv-buf", NNG_OPT_RCVBUF)) != 0) ||
	    ((rv = nni_option_set_id("send-buf", NNG_OPT_SNDBUF)) != 0) ||
	    ((rv = nni_option_set_id("recv-timeout", NNG_OPT_RCVTIMEO)) !=
	        0) ||
	    ((rv = nni_option_set_id("send-timeout", NNG_OPT_SNDTIMEO)) !=
	        0) ||
	    ((rv = nni_option_set_id("reconnect-time", NNG_OPT_RECONN_TIME)) !=
	        0) ||
	    ((rv = nni_option_set_id(
	          "reconnect-max-time", NNG_OPT_RECONN_MAXTIME)) != 0) ||
	    ((rv = nni_option_set_id("recv-max-size", NNG_OPT_RCVMAXSZ)) !=
	        0) ||
	    ((rv = nni_option_set_id("max-ttl", NNG_OPT_MAXTTL)) != 0) ||
	    ((rv = nni_option_set_id("protocol", NNG_OPT_PROTOCOL)) != 0) ||
	    ((rv = nni_option_set_id("subscribe", NNG_OPT_SUBSCRIBE)) != 0) ||
	    ((rv = nni_option_set_id("unsubscribe", NNG_OPT_UNSUBSCRIBE)) !=
	        0) ||
	    ((rv = nni_option_set_id("survey-time", NNG_OPT_SURVEYTIME)) !=
	        0) ||
	    ((rv = nni_option_set_id("resend-time", NNG_OPT_RESENDTIME)) !=
	        0) ||
	    ((rv = nni_option_set_id("transport", NNG_OPT_TRANSPORT)) != 0) ||
	    ((rv = nni_option_set_id("local-addr", NNG_OPT_LOCALADDR)) != 0) ||
	    ((rv = nni_option_set_id("remote-addr", NNG_OPT_REMOTEADDR)) !=
	        0) ||
	    ((rv = nni_option_set_id("recv-fd", NNG_OPT_RCVFD)) != 0) ||
	    ((rv = nni_option_set_id("send-fd", NNG_OPT_SNDFD)) != 0)) {
		nni_option_sys_fini();
		return (rv);
	}
	return (0);
}

void
nni_option_sys_fini(void)
{
	if (nni_option_nextid != 0) {
		nni_option *opt;
		while ((opt = nni_list_first(&nni_options)) != NULL) {
			nni_list_remove(&nni_options, opt);
			nni_free(opt->o_name, strlen(opt->o_name) + 1);
			NNI_FREE_STRUCT(opt);
		}
	}
	nni_option_nextid = 0;
}