-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnanonext.h
312 lines (283 loc) · 8.94 KB
/
nanonext.h
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
// Copyright (C) 2022-2024 Hibiki AI Limited <[email protected]>
//
// This file is part of nanonext.
//
// nanonext is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// nanonext is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// nanonext. If not, see <https://www.gnu.org/licenses/>.
// nanonext - header file ------------------------------------------------------
#ifndef NANONEXT_H
#define NANONEXT_H
#include <nng/nng.h>
#if NNG_MAJOR_VERSION == 1 && NNG_MINOR_VERSION < 6
#define NANONEXT_LEGACY_NNG
#endif
#ifdef NANONEXT_PROTOCOLS
#include <nng/protocol/bus0/bus.h>
#include <nng/protocol/pair0/pair.h>
#include <nng/protocol/pubsub0/pub.h>
#include <nng/protocol/pubsub0/sub.h>
#include <nng/protocol/pipeline0/pull.h>
#include <nng/protocol/pipeline0/push.h>
#include <nng/protocol/reqrep0/req.h>
#include <nng/protocol/reqrep0/rep.h>
#include <nng/protocol/survey0/survey.h>
#include <nng/protocol/survey0/respond.h>
#endif
#ifdef NANONEXT_HTTP
#include <nng/supplemental/http/http.h>
#endif
#ifdef NANONEXT_SUPPLEMENTALS
#include <nng/supplemental/tls/tls.h>
#include <nng/supplemental/util/platform.h>
#ifdef NANONEXT_LEGACY_NNG
extern nng_mtx *shr_mtx;
#endif
typedef struct nano_listener_s {
nng_listener list;
nng_tls_config *tls;
} nano_listener;
typedef struct nano_dialer_s {
nng_dialer dial;
nng_tls_config *tls;
} nano_dialer;
typedef struct nano_stream_s {
nng_stream *stream;
enum {
NANO_STREAM_DIALER,
NANO_STREAM_LISTENER
} mode;
int textframes;
union {
nng_stream_dialer *dial;
nng_stream_listener *list;
} endpoint;
nng_tls_config *tls;
} nano_stream;
typedef enum nano_aio_typ {
SENDAIO,
RECVAIO,
IOV_SENDAIO,
IOV_RECVAIO,
HTTP_AIO
} nano_aio_typ;
typedef struct nano_aio_s {
nng_aio *aio;
nano_aio_typ type;
int mode;
int result;
void *data;
void *next;
void *cb;
} nano_aio;
typedef struct nano_cv_s {
int condition;
int flag;
nng_mtx *mtx;
nng_cv *cv;
} nano_cv;
#endif
#ifdef NANONEXT_SIGNALS
#ifndef _WIN32
#include <unistd.h>
#endif
#include <signal.h>
#endif
#ifdef NANONEXT_TIME
#include <time.h>
#endif
#ifdef NANONEXT_TLS
#include <mbedtls/base64.h>
#include <mbedtls/md.h>
#include <mbedtls/sha256.h>
#include <mbedtls/sha512.h>
#include <mbedtls/version.h>
#define SHA224_KEY_SIZE 28
#define SHA256_KEY_SIZE 32
#define SHA384_KEY_SIZE 48
#define SHA512_KEY_SIZE 64
#endif
#ifdef NANONEXT_MBED
#include <mbedtls/entropy.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/error.h>
#endif
#ifdef NANONEXT_KEYCERT
#include <mbedtls/version.h>
#if MBEDTLS_VERSION_MAJOR < 3
#include <mbedtls/config.h>
#endif
#include <mbedtls/platform.h>
#include <mbedtls/pk.h>
#include <mbedtls/rsa.h>
#include <mbedtls/x509_crt.h>
#include <mbedtls/x509_csr.h>
#include <mbedtls/entropy.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/md.h>
#include <mbedtls/error.h>
#include <errno.h>
#endif
#define R_NO_REMAP
#define STRICT_R_HEADERS
#include <R.h>
#include <Rinternals.h>
#include <R_ext/Visibility.h>
#define ERROR_OUT(xc) Rf_error("%d | %s", xc, nng_strerror(xc))
#define ERROR_RET(xc) { Rf_warning("%d | %s", xc, nng_strerror(xc)); return mk_error(xc); }
#define NANONEXT_INIT_BUFSIZE 8192
#define NANONEXT_SERIAL_VER 3
#define NANONEXT_SERIAL_HEADERS 6
#define NANONEXT_SERIAL_THR 134217728
#define NANONEXT_LD_STRLEN 21
#define NANO_ALLOC(x, sz) \
(x)->buf = R_Calloc(sz, unsigned char); \
(x)->len = sz; \
(x)->cur = 0
#define NANO_INIT(x, ptr, sz) \
(x)->buf = ptr; \
(x)->len = 0; \
(x)->cur = sz
#define NANO_FREE(x) if (x.len) R_Free(x.buf)
#define NANO_INTEGER(x) (int *) DATAPTR_RO(x)
#define NANO_CLASS(x, cls) \
SEXP klass = Rf_cons(Rf_mkString(cls), R_NilValue); \
SET_TAG(klass, R_ClassSymbol); \
SET_ATTRIB(x, klass); \
SET_OBJECT(x, 1)
#define NANO_CLASS2(x, cls1, cls2) \
SEXP klass = Rf_cons(Rf_allocVector(STRSXP, 2), R_NilValue); \
SET_TAG(klass, R_ClassSymbol); \
SET_ATTRIB(x, klass); \
SET_OBJECT(x, 1); \
SET_STRING_ELT(CAR(klass), 0, Rf_mkChar(cls1)); \
SET_STRING_ELT(CAR(klass), 1, Rf_mkChar(cls2))
typedef struct nano_buf_s {
unsigned char *buf;
size_t len;
size_t cur;
} nano_buf;
SEXP mk_error(const int);
SEXP mk_error_ncurl(const int);
nano_buf nano_char_buf(const SEXP);
SEXP nano_decode(unsigned char *, const size_t, const int);
void nano_encode(nano_buf *, const SEXP);
int nano_encodes(const SEXP);
int nano_matcharg(const SEXP);
int nano_matchargs(const SEXP);
void nano_serialize(nano_buf *, const SEXP);
void nano_serialize_next(nano_buf *, const SEXP);
void nano_serialize_xdr(nano_buf *, const SEXP);
SEXP nano_unserialize(unsigned char *, const size_t);
SEXP rawToChar(const unsigned char *, const size_t);
void dialer_finalizer(SEXP);
void listener_finalizer(SEXP);
void socket_finalizer(SEXP);
SEXP rnng_aio_call(SEXP);
SEXP rnng_aio_get_msg(SEXP);
SEXP rnng_aio_get_msg2(SEXP);
SEXP rnng_aio_get_msg3(SEXP);
SEXP rnng_aio_http(SEXP, SEXP, SEXP);
SEXP rnng_aio_result(SEXP);
SEXP rnng_aio_stop(SEXP);
SEXP rnng_base64dec(SEXP, SEXP);
SEXP rnng_base64enc(SEXP, SEXP);
SEXP rnng_clock(void);
SEXP rnng_close(SEXP);
SEXP rnng_ctx_close(SEXP);
SEXP rnng_ctx_create(SEXP);
SEXP rnng_ctx_open(SEXP);
SEXP rnng_cv_alloc(void);
SEXP rnng_cv_reset(SEXP);
SEXP rnng_cv_signal(SEXP);
SEXP rnng_cv_until(SEXP, SEXP);
SEXP rnng_cv_until_safe(SEXP, SEXP);
SEXP rnng_cv_value(SEXP);
SEXP rnng_cv_wait(SEXP);
SEXP rnng_cv_wait_safe(SEXP);
SEXP rnng_dial(SEXP, SEXP, SEXP, SEXP, SEXP);
SEXP rnng_dialer_close(SEXP);
SEXP rnng_dialer_start(SEXP, SEXP);
SEXP rnng_get_opt(SEXP, SEXP);
SEXP rnng_is_error_value(SEXP);
SEXP rnng_is_nul_byte(SEXP);
SEXP rnng_listen(SEXP, SEXP, SEXP, SEXP, SEXP);
SEXP rnng_listener_close(SEXP);
SEXP rnng_listener_start(SEXP);
SEXP rnng_messenger(SEXP);
SEXP rnng_messenger_thread_create(SEXP);
SEXP rnng_ncurl(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
SEXP rnng_ncurl_aio(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
SEXP rnng_ncurl_session(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
SEXP rnng_ncurl_session_close(SEXP);
SEXP rnng_ncurl_transact(SEXP);
SEXP rnng_next_config(SEXP, SEXP, SEXP, SEXP);
SEXP rnng_pipe_notify(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
SEXP rnng_protocol_open(SEXP, SEXP);
SEXP rnng_random(SEXP, SEXP);
SEXP rnng_reap(SEXP);
SEXP rnng_recv(SEXP, SEXP, SEXP, SEXP);
SEXP rnng_recv_aio(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
SEXP rnng_recv_aio_signal(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
SEXP rnng_request(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
SEXP rnng_request_signal(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
SEXP rnng_send(SEXP, SEXP, SEXP, SEXP);
SEXP rnng_send_aio(SEXP, SEXP, SEXP, SEXP, SEXP);
SEXP rnng_set_opt(SEXP, SEXP, SEXP);
SEXP rnng_signal_thread_create(SEXP, SEXP);
SEXP rnng_sleep(SEXP);
SEXP rnng_socket_lock(SEXP, SEXP);
SEXP rnng_socket_unlock(SEXP);
SEXP rnng_stats_get(SEXP, SEXP);
SEXP rnng_status_code(SEXP);
SEXP rnng_stream_close(SEXP);
SEXP rnng_stream_dial(SEXP, SEXP, SEXP);
SEXP rnng_stream_listen(SEXP, SEXP, SEXP);
SEXP rnng_strcat(SEXP, SEXP);
SEXP rnng_strerror(SEXP);
SEXP rnng_subscribe(SEXP, SEXP, SEXP);
SEXP rnng_tls_config(SEXP, SEXP, SEXP, SEXP);
SEXP rnng_unresolved(SEXP);
SEXP rnng_unresolved2(SEXP);
SEXP rnng_url_parse(SEXP);
SEXP rnng_version(void);
SEXP rnng_wait_thread_create(SEXP);
SEXP rnng_write_cert(SEXP, SEXP, SEXP);
extern SEXP nano_AioSymbol;
extern SEXP nano_ContextSymbol;
extern SEXP nano_CvSymbol;
extern SEXP nano_DataSymbol;
extern SEXP nano_DialerSymbol;
extern SEXP nano_DotcallSymbol;
extern SEXP nano_FdSymbol;
extern SEXP nano_HeadersSymbol;
extern SEXP nano_IdSymbol;
extern SEXP nano_ListenerSymbol;
extern SEXP nano_ProtocolSymbol;
extern SEXP nano_RawSymbol;
extern SEXP nano_ResponseSymbol;
extern SEXP nano_ResultSymbol;
extern SEXP nano_SocketSymbol;
extern SEXP nano_StateSymbol;
extern SEXP nano_StatusSymbol;
extern SEXP nano_StreamSymbol;
extern SEXP nano_TlsSymbol;
extern SEXP nano_UrlSymbol;
extern SEXP nano_ValueSymbol;
extern SEXP nano_aioFormals;
extern SEXP nano_aioFuncs;
extern SEXP nano_aioNFuncs;
extern SEXP nano_error;
extern SEXP nano_klassString;
extern SEXP nano_refHook;
extern SEXP nano_success;
extern SEXP nano_unresolved;
#endif