aboutsummaryrefslogtreecommitdiff
path: root/src/core/options.c
blob: 7e05c23ea71efb6ecf37c301200edcd28d8b1bd0 (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
//
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
// Copyright 2018 Devolutions <info@devolutions.net>
//
// 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/defs.h"
#include "core/nng_impl.h"
#include "nng/nng.h"

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

int
nni_copyin_ms(nni_duration *dp, const void *v, size_t sz, nni_type t)
{
	nni_duration dur;
	NNI_ARG_UNUSED(sz);

	if (t != NNI_TYPE_DURATION) {
		return (NNG_EBADTYPE);
	}
	dur = *(nng_duration *) v;

	if (dur < -1) {
		return (NNG_EINVAL);
	}
	if (dp != NULL) {
		*dp = dur;
	}
	return (0);
}

int
nni_copyin_bool(bool *bp, const void *v, size_t sz, nni_type t)
{
	NNI_ARG_UNUSED(sz);

	if (t != NNI_TYPE_BOOL) {
		return (NNG_EBADTYPE);
	}
	*bp = *(bool *) v;
	return (0);
}

int
nni_copyin_int(
    int *ip, const void *v, size_t sz, int minv, int maxv, nni_type t)
{
	int i;
	NNI_ARG_UNUSED(sz);

	if (t != NNI_TYPE_INT32) {
		return (NNG_EBADTYPE);
	}
	i = *(int *) v;
	if (i > maxv) {
		return (NNG_EINVAL);
	}
	if (i < minv) {
		return (NNG_EINVAL);
	}
	*ip = i;
	return (0);
}

int
nni_copyin_size(
    size_t *sp, const void *v, size_t sz, size_t minv, size_t maxv, nni_type t)
{
	size_t val;
	NNI_ARG_UNUSED(sz);

	if (t != NNI_TYPE_SIZE) {
		return (NNG_EBADTYPE);
	}

	val = *(size_t *) v;
	if ((val > maxv) || (val < minv)) {
		return (NNG_EINVAL);
	}
	*sp = val;
	return (0);
}

int
nni_copyin_str(char *s, const void *v, size_t maxsz, nni_type t)
{
	size_t z;

	if (t != NNI_TYPE_STRING) {
		return (NNG_EBADTYPE);
	}
	z = nni_strnlen(v, maxsz);
	if (z == maxsz && ((char *) v)[maxsz - 1] != 0) {
		return (NNG_EINVAL); // too long
	}
	memcpy(s, v, z);
	s[z] = 0;
	return (0);
}

int
nni_copyin_sockaddr(nng_sockaddr *ap, const void *v, nni_type t)
{
	if (t != NNI_TYPE_SOCKADDR) {
		return (NNG_EBADTYPE);
	}
	*ap = *(nng_sockaddr *) v;
	return (0);
}

int
nni_copyout_bool(bool b, void *dst, size_t *szp, nni_type t)
{
	NNI_ARG_UNUSED(szp);
	if (t != NNI_TYPE_BOOL) {
		return (NNG_EBADTYPE);
	}
	*(bool *) dst = b;
	return (0);
}

int
nni_copyout_int(int i, void *dst, size_t *szp, nni_type t)
{
	NNI_ARG_UNUSED(szp);
	if (t != NNI_TYPE_INT32) {
		return (NNG_EBADTYPE);
	}
	*(int *) dst = i;
	return (0);
}

int
nni_copyout_ms(nng_duration d, void *dst, size_t *szp, nni_type t)
{
	NNI_ARG_UNUSED(szp);
	if (t != NNI_TYPE_DURATION) {
		return (NNG_EBADTYPE);
	}
	*(nng_duration *) dst = d;
	return (0);
}

int
nni_copyout_size(size_t s, void *dst, size_t *szp, nni_type t)
{
	NNI_ARG_UNUSED(szp);
	if (t != NNI_TYPE_SIZE) {
		return (NNG_EBADTYPE);
	}
	*(size_t *) dst = s;
	return (0);
}

int
nni_copyout_sockaddr(
    const nng_sockaddr *sap, void *dst, size_t *szp, nni_type t)
{
	NNI_ARG_UNUSED(szp);
	if (t != NNI_TYPE_SOCKADDR) {
		return (NNG_EBADTYPE);
	}
	*(nng_sockaddr *) dst = *sap;
	return (0);
}

int
nni_copyout_str(const char *str, void *dst, size_t *szp, nni_type t)
{
	char *s;
	NNI_ARG_UNUSED(szp);
	if (t != NNI_TYPE_STRING) {
		return (NNG_EBADTYPE);
	}

	if ((s = nni_strdup(str)) == NULL) {
		return (NNG_ENOMEM);
	}
	*(char **) dst = s;
	return (0);
}

int
nni_getopt(const nni_option *opts, const char *nm, void *arg, void *buf,
    size_t *szp, nni_type otype)
{
	while (opts->o_name != NULL) {
		if (strcmp(opts->o_name, nm) == 0) {
			if (opts->o_get == NULL) {
				return (NNG_EWRITEONLY);
			}
			return (opts->o_get(arg, buf, szp, otype));
		}
		opts++;
	}
	return (NNG_ENOTSUP);
}

int
nni_setopt(const nni_option *opts, const char *nm, void *arg, const void *buf,
    size_t sz, nni_type otype)
{
	while (opts->o_name != NULL) {
		if (strcmp(opts->o_name, nm) == 0) {
			if (opts->o_set == NULL) {
				return (NNG_EREADONLY);
			}
			return (opts->o_set(arg, buf, sz, otype));
		}
		opts++;
	}
	return (NNG_ENOTSUP);
}