aboutsummaryrefslogtreecommitdiff
path: root/src/core/options.c
blob: 9410de5fd3922f4e718f810126229d8ebb4be636 (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
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 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>

int
nni_chkopt_ms(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_bool(size_t sz)
{
	if (sz != sizeof(bool)) {
		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_ms(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_bool(bool *bp, const void *v, size_t sz)
{
	if (sz != sizeof(*bp)) {
		return (NNG_EINVAL);
	}
	memcpy(bp, v, sizeof(*bp));
	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_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_copyout(const void *src, size_t srcsz, void *dst, size_t *dstszp)
{
	int    rv     = 0;
	size_t copysz = *dstszp;
	// Assumption is that this is type NNI_TYPE_OPAQUE.
	if (copysz > srcsz) {
		copysz = srcsz;
	} else if (srcsz > copysz) {
		// destination too small.
		rv = NNG_EINVAL;
	}
	*dstszp = srcsz;
	memcpy(dst, src, copysz);
	return (rv);
}

int
nni_copyout_bool(bool b, void *dst, size_t *szp, int typ)
{
	switch (typ) {
	case NNI_TYPE_BOOL:
		NNI_ASSERT(*szp == sizeof(b));
		*(bool *) dst = b;
		return (0);
	case NNI_TYPE_OPAQUE:
		return (nni_copyout(&b, sizeof(b), dst, szp));
	default:
		return (NNG_EBADTYPE);
	}
}

int
nni_copyout_int(int i, void *dst, size_t *szp, int typ)
{
	switch (typ) {
	case NNI_TYPE_INT32:
		NNI_ASSERT(*szp == sizeof(i));
		*(int *) dst = i;
		return (0);
	case NNI_TYPE_OPAQUE:
		return (nni_copyout(&i, sizeof(i), dst, szp));
	default:
		return (NNG_EBADTYPE);
	}
}

int
nni_copyout_ms(nng_duration d, void *dst, size_t *szp, int typ)
{
	switch (typ) {
	case NNI_TYPE_DURATION:
		NNI_ASSERT(*szp == sizeof(d));
		*(nng_duration *) dst = d;
		return (0);
	case NNI_TYPE_OPAQUE:
		return (nni_copyout(&d, sizeof(d), dst, szp));
	default:
		return (NNG_EBADTYPE);
	}
}

int
nni_copyout_ptr(void *p, void *dst, size_t *szp, int typ)
{
	switch (typ) {
	case NNI_TYPE_POINTER:
		NNI_ASSERT(*szp == sizeof(p));
		*(void **) dst = p;
		return (0);
	case NNI_TYPE_OPAQUE:
		return (nni_copyout(&p, sizeof(p), dst, szp));
	default:
		return (NNG_EBADTYPE);
	}
}

int
nni_copyout_size(size_t s, void *dst, size_t *szp, int typ)
{
	switch (typ) {
	case NNI_TYPE_SIZE:
		NNI_ASSERT(*szp == sizeof(s));
		*(size_t *) dst = s;
		return (0);
	case NNI_TYPE_OPAQUE:
		return (nni_copyout(&s, sizeof(s), dst, szp));
	default:
		return (NNG_EBADTYPE);
	}
}

int
nni_copyout_sockaddr(const nng_sockaddr *sap, void *dst, size_t *szp, int typ)
{
	switch (typ) {
	case NNI_TYPE_SOCKADDR:
		NNI_ASSERT(*szp == sizeof(*sap));
		*(nng_sockaddr *) dst = *sap;
		return (0);
	case NNI_TYPE_OPAQUE:
		return (nni_copyout(sap, sizeof(*sap), dst, szp));
	default:
		return (NNG_EBADTYPE);
	}
}

int
nni_copyout_u64(uint64_t u, void *dst, size_t *szp, int typ)
{
	switch (typ) {
	case NNI_TYPE_UINT64:
		NNI_ASSERT(*szp == sizeof(u));
		*(uint64_t *) dst = u;
		return (0);
	case NNI_TYPE_OPAQUE:
		return (nni_copyout(&u, sizeof(u), dst, szp));
	default:
		return (NNG_EBADTYPE);
	}
}

int
nni_copyout_str(const char *str, void *dst, size_t *szp, int typ)
{
	char *s;

	switch (typ) {
	case NNI_TYPE_STRING:
		NNI_ASSERT(*szp == sizeof(char *));
		if ((s = nni_strdup(str)) == NULL) {
			return (NNG_ENOMEM);
		}
		*(char **) dst = s;
		return (0);

	case NNI_TYPE_OPAQUE:
		return (nni_copyout(str, strlen(str) + 1, dst, szp));

	default:
		return (NNG_EBADTYPE);
	}
}