Debian Codegraph @

src/nanonext.h

raw symbols SCIP
1 // nanonext - header file ------------------------------------------------------
2
3 #ifndef NANONEXT_H
4 #define NANONEXT_H
5
6 #include <nng/nng.h>
7 #include <nng/supplemental/util/platform.h>
8 #include <nng/supplemental/tls/tls.h>
9 #include "nng_structs.h"
10
11 #ifdef NANONEXT_PROTOCOLS
12 #include <nng/protocol/bus0/bus.h>
13 #include <nng/protocol/pair0/pair.h>
14 #include <nng/protocol/pair1/pair.h>
15 #include <nng/protocol/pubsub0/pub.h>
16 #include <nng/protocol/pubsub0/sub.h>
17 #include <nng/protocol/pipeline0/pull.h>
18 #include <nng/protocol/pipeline0/push.h>
19 #include <nng/protocol/reqrep0/req.h>
20 #include <nng/protocol/reqrep0/rep.h>
21 #include <nng/protocol/survey0/survey.h>
22 #include <nng/protocol/survey0/respond.h>
23 #endif
24
25 #ifdef NANONEXT_HTTP
26 #include <nng/supplemental/http/http.h>
27 #ifdef _WIN32
28 #include <winsock2.h>
29 #else
30 #include <arpa/inet.h>
31 #endif
32
33 typedef struct nano_handle_s {
34 nng_url *url;
35 nng_http_client *cli;
36 nng_http_req *req;
37 nng_http_res *res;
38 nng_tls_config *cfg;
39 } nano_handle;
40
41 #endif
42
43 #ifdef NANONEXT_IO
44 #include <stdio.h>
45 #endif
46
47 #ifdef NANONEXT_NET
48 #ifdef _WIN32
49 #if !defined(_WIN32_WINNT) || (defined(_WIN32_WINNT) && (_WIN32_WINNT < 0x0600))
50 #define _WIN32_WINNT 0x0600
51 #endif
52 #include <winsock2.h>
53 #include <ws2tcpip.h>
54 #include <iphlpapi.h>
55 #define WIN32_LEAN_AND_MEAN
56 #include <windows.h>
57 #else
58 #include <unistd.h>
59 #include <sys/uio.h>
60 #include <ifaddrs.h>
61 #include <arpa/inet.h>
62 #include <net/if.h>
63 #endif
64 #endif
65
66 #ifdef NANONEXT_SIGNALS
67 #ifndef _WIN32
68 #include <unistd.h>
69 #endif
70 #include <signal.h>
71 #endif
72
73 #ifdef NANONEXT_TLS
74 #include <mbedtls/version.h>
75 #if MBEDTLS_VERSION_MAJOR < 3
76 #include <mbedtls/config.h>
77 #endif
78 #include <mbedtls/platform.h>
79 #include <mbedtls/pk.h>
80 #include <mbedtls/rsa.h>
81 #include <mbedtls/x509_crt.h>
82 #include <mbedtls/x509_csr.h>
83 #include <mbedtls/entropy.h>
84 #include <mbedtls/ctr_drbg.h>
85 #include <mbedtls/md.h>
86 #include <mbedtls/error.h>
87 #ifdef MBEDTLS_PSA_CRYPTO_C
88 #include <psa/crypto.h>
89 #endif
90 #include <errno.h>
91 #endif
92
93 #include <inttypes.h>
94 #ifndef R_NO_REMAP
95 #define R_NO_REMAP
96 #endif
97 #ifndef STRICT_R_HEADERS
98 #define STRICT_R_HEADERS
99 #endif
100 #include <R.h>
101 #include <Rinternals.h>
102 #include <Rversion.h>
103 #include <R_ext/Visibility.h>
104 #if defined(NANONEXT_SIGNALS)
105 #ifdef _WIN32
106 #include <Rembedded.h>
107 #else
108 extern int R_interrupts_pending;
109 #endif
110 #endif
111
112 #define NANO_PTR(x) (void *) CAR(x)
113 #define NANO_PTR_CHECK(x, tag) (TAG(x) != tag || NANO_PTR(x) == NULL)
114 #define NANO_PROT(x) CDR(x)
115 #define NANO_ENCLOS(x) CDR(x)
116 #define NANO_SET_TAG(x, v) SET_TAG(x, v)
117 #define NANO_SET_PROT(x, v) SETCDR(x, v)
118 #define NANO_SET_ENCLOS(x, v) SETCDR(x, v)
119 #define NANO_DATAPTR(x) (void *) DATAPTR_RO(x)
120 #if R_VERSION < R_Version(4, 5, 0)
121 # define VECTOR_PTR_RO(x) ((const SEXP *) DATAPTR_RO(x))
122 #endif
123 #define NANO_INTEGER(x) *(const int *) DATAPTR_RO(x)
124
125 #define ERROR_OUT(xc) Rf_error("%d | %s", xc, nng_strerror(xc))
126 #define ERROR_RET(xc) { Rf_warning("%d | %s", xc, nng_strerror(xc)); return mk_error(xc); }
127 #define NANONEXT_INIT_BUFSIZE 4096
128 #define NANONEXT_SERIAL_VER 3
129 #define NANONEXT_SERIAL_THR 67108864
130 #define NANONEXT_CHUNK_SIZE 67108864 // must be <= INT_MAX
131 #define NANONEXT_STR_SIZE 40
132 #define NANONEXT_WAIT_DUR 1000
133 #define NANONEXT_SLEEP_DUR 200
134 #define NANO_ALLOC(x, sz) \
135 (x)->buf = malloc(sz); \
136 if ((x)->buf == NULL) Rf_error("memory allocation failed"); \
137 (x)->len = sz; \
138 (x)->cur = 0
139 #define NANO_INIT(x, ptr, sz) \
140 (x)->buf = ptr; \
141 (x)->len = 0; \
142 (x)->cur = sz
143 #define NANO_FREE(x) if (x.len) free(x.buf)
144 #define NANO_CLASS2(x, cls1, cls2) \
145 SEXP klass = Rf_allocVector(STRSXP, 2); \
146 Rf_classgets(x, klass); \
147 SET_STRING_ELT(klass, 0, Rf_mkChar(cls1)); \
148 SET_STRING_ELT(klass, 1, Rf_mkChar(cls2))
149 #define NANO_ENSURE_ALLOC(x) if (x == NULL) { xc = 2; goto failmem; }
150 #define NANO_URL_MAX 8192
151
152 typedef union nano_opt_u {
153 char *str;
154 uint64_t u;
155 size_t s;
156 nng_duration d;
157 int i;
158 bool b;
159 } nano_opt;
160
161 typedef struct nano_stream_s {
162 nng_stream *stream;
163 union {
164 nng_stream_dialer *dial;
165 nng_stream_listener *list;
166 } endpoint;
167 nng_tls_config *tls;
168 size_t bufsize;
169 int textframes;
170 int msgmode;
171 enum {
172 NANO_STREAM_DIALER,
173 NANO_STREAM_LISTENER
174 } mode;
175 } nano_stream;
176
177 typedef enum nano_aio_typ {
178 SENDAIO,
179 RECVAIO,
180 REQAIO,
181 IOV_SENDAIO,
182 IOV_RECVAIO,
183 HTTP_AIO,
184 RECVAIOS,
185 REQAIOS,
186 IOV_RECVAIOS
187 } nano_aio_typ;
188
189 typedef struct nano_aio_s {
190 nng_aio *aio;
191 void *data;
192 void *cb;
193 void *next;
194 int result;
195 nano_aio_typ type;
196 uint8_t mode;
197 } nano_aio;
198
199 typedef struct nano_saio_s {
200 nng_aio *aio;
201 void *disp;
202 void *cb;
203 int id;
204 int type;
205 } nano_saio;
206
207 typedef struct nano_cv_s {
208 int condition;
209 int flag;
210 nng_mtx *mtx;
211 nng_cv *cv;
212 } nano_cv;
213
214 typedef struct nano_monitor_s {
215 nano_cv *cv;
216 int *ids;
217 int size;
218 int updates;
219 } nano_monitor;
220
221 typedef struct nano_thread_aio_s {
222 nng_thread *thr;
223 nano_cv *cv;
224 nng_aio *aio;
225 } nano_thread_aio;
226
227 typedef struct nano_thread_duo_s {
228 nng_thread *thr;
229 nano_cv *cv;
230 nano_cv *cv2;
231 } nano_thread_duo;
232
233 typedef struct nano_buf_s {
234 unsigned char *buf;
235 size_t len;
236 size_t cur;
237 } nano_buf;
238
239 typedef struct nano_serial_bundle_s {
240 R_outpstream_t outpstream;
241 R_inpstream_t inpstream;
242 SEXP klass;
243 } nano_serial_bundle;
244
245 typedef enum nano_list_op {
246 INIT,
247 FINALIZE,
248 COMPLETE,
249 FREE,
250 SHUTDOWN
251 } nano_list_op;
252
253 #ifdef NANONEXT_HTTP
254
255 // Forward declarations
256 typedef struct nano_conn_s nano_conn;
257 typedef struct nano_ws_conn_s nano_ws_conn;
258 typedef struct nano_stream_conn_s nano_stream_conn;
259 typedef struct nano_http_server_s nano_http_server;
260 typedef struct nano_http_handler_info_s nano_http_handler_info;
261 typedef struct nano_http_request_s nano_http_request;
262
263 // Connection type discriminator
264 typedef enum {
265 NANO_CONN_WEBSOCKET,
266 NANO_CONN_HTTP_STREAM
267 } nano_conn_type;
268
269 // Connection state machine (prevents races between R and NNG threads)
270 typedef enum {
271 CONN_STATE_OPEN, // Connection active, can send/receive
272 CONN_STATE_CLOSING, // Close initiated, waiting for cleanup
273 CONN_STATE_CLOSED // Fully closed, safe to free
274 } nano_conn_state;
275
276 // Base connection structure - common fields for linked list and lifecycle
277 typedef struct nano_conn_s {
278 nng_aio *send_aio; // For async send (both types)
279 nano_http_handler_info *handler; // Back-reference to handler
280 struct nano_conn_s *next; // Linked list
281 struct nano_conn_s *prev; // Doubly-linked for O(1) removal
282 SEXP xptr; // R external pointer
283 nano_conn_type type; // Connection type (for dispatch)
284 nano_conn_state state; // Connection state
285 int id; // Unique connection ID (server-wide)
286 int onclose_scheduled; // Prevents duplicate on_close callbacks
287 } nano_conn;
288
289 // WebSocket connection (embeds base as first member)
290 typedef struct nano_ws_conn_s {
291 nano_conn conn; // Base - MUST be first member
292 nng_stream *stream; // WebSocket stream with automatic framing
293 nng_aio *recv_aio; // For async receive loop
294 } nano_ws_conn;
295
296 // HTTP streaming connection (embeds base as first member)
297 typedef struct nano_stream_conn_s {
298 nano_conn conn; // Base - MUST be first member
299 nng_http_conn *http; // Hijacked HTTP connection
300 nng_http_req *req; // Request pointer (valid because hijacked)
301 // Response state (set before first send)
302 char **resp_header_names;
303 char **resp_header_values;
304 int resp_header_count;
305 int resp_header_capacity;
306 int status_code; // Default 200
307 int headers_sent; // Whether HTTP headers written
308 } nano_stream_conn;
309
310 // HTTP handler info (links NNG handler to R callback)
311 typedef struct nano_http_handler_info_s {
312 nng_http_handler *handler; // NNG HTTP handler (NULL for WS)
313 SEXP callback; // HTTP callback or WS on_message or stream on_request
314 nano_http_server *server; // Back-reference
315 // Long-lived connection handler fields:
316 nng_stream_listener *listener; // WebSocket listener (NULL for HTTP stream)
317 nng_aio *accept_aio; // Accept AIO for WS handler
318 nano_conn *conns; // Linked list of connections (WS or HTTP stream)
319 SEXP on_open; // R callback: WS open or stream request
320 SEXP on_close; // R callback: connection close
321 int textframes; // WS: text frame mode
322 } nano_http_handler_info;
323
324 typedef struct nano_http_request_s {
325 nng_aio *aio; // The HTTP AIO to complete
326 nng_http_req *req; // The HTTP request (valid until aio finished)
327 SEXP callback; // R callback function
328 nano_http_server *server; // Back-reference to server
329 nano_http_request *next; // Linked list for tracking
330 nano_http_request *prev;
331 int cancelled; // Set when server stops (protected by server->mtx)
332 } nano_http_request;
333
334 typedef enum {
335 SERVER_CREATED,
336 SERVER_STARTED,
337 SERVER_STOPPED
338 } nano_server_state;
339
340 typedef struct nano_http_server_s {
341 nng_http_server *server; // NNG HTTP server
342 nng_tls_config *tls; // TLS configuration
343 nano_http_handler_info *handlers; // Array of handler info
344 nano_http_request *pending_reqs; // Linked list of pending HTTP requests
345 nng_mtx *mtx; // Mutex for thread safety
346 SEXP xptr; // R external pointer for this server
347 SEXP prot; // Pairlist for GC protection of callbacks
348 int handler_count; // Number of handlers
349 int conn_counter; // Server-wide unique connection ID counter
350 nano_server_state state; // Server lifecycle state
351 } nano_http_server;
352
353 typedef struct ws_message_s {
354 nano_ws_conn *conn;
355 nng_msg *msg;
356 } ws_message;
357
358 #endif
359
360 extern void (*eln2)(void (*)(void *), void *, double, int);
361
362 extern SEXP nano_AioSymbol;
363 extern SEXP nano_ContextSymbol;
364 extern SEXP nano_CvSymbol;
365 extern SEXP nano_DataSymbol;
366 extern SEXP nano_DialerSymbol;
367 extern SEXP nano_DotcallSymbol;
368 extern SEXP nano_HeadersSymbol;
369 extern SEXP nano_IdSymbol;
370 extern SEXP nano_ListenerSymbol;
371 extern SEXP nano_MonitorSymbol;
372 extern SEXP nano_ProtocolSymbol;
373 extern SEXP nano_ResolveSymbol;
374 extern SEXP nano_ResponseSymbol;
375 extern SEXP nano_ResultSymbol;
376 extern SEXP nano_SocketSymbol;
377 extern SEXP nano_StateSymbol;
378 extern SEXP nano_StatusSymbol;
379 extern SEXP nano_StreamSymbol;
380 extern SEXP nano_ThreadSymbol;
381 extern SEXP nano_TlsSymbol;
382 extern SEXP nano_UrlSymbol;
383 extern SEXP nano_ValueSymbol;
384 extern SEXP nano_HttpServerSymbol;
385 extern SEXP nano_ConnSymbol;
386
387 extern SEXP nano_aioFuncMsg;
388 extern SEXP nano_aioFuncRes;
389 extern SEXP nano_aioNFuncs;
390 extern SEXP nano_error;
391 extern SEXP nano_precious;
392 extern SEXP nano_recvAio;
393 extern SEXP nano_reqAio;
394 extern SEXP nano_sendAio;
395 extern SEXP nano_success;
396 extern SEXP nano_unresolved;
397
398 #if R_VERSION < R_Version(4, 1, 0)
399
400 static inline SEXP R_NewEnv(SEXP parent, int hash, int size) {
401 (void) parent;
402 (void) hash;
403 (void) size;
404 return Rf_allocSExp(ENVSXP);
405 }
406
407 #endif
408
409 #if R_VERSION < R_Version(4, 5, 0)
410
411 static inline SEXP R_mkClosure(SEXP formals, SEXP body, SEXP env) {
412 SEXP fun = Rf_allocSExp(CLOSXP);
413 SET_FORMALS(fun, formals);
414 SET_BODY(fun, body);
415 SET_CLOENV(fun, env);
416 return fun;
417 }
418
419 #endif
420
421 static inline int nano_integer(const SEXP x) {
422 return (TYPEOF(x) == INTSXP || TYPEOF(x) == LGLSXP) ? NANO_INTEGER(x) : Rf_asInteger(x);
423 }
424
425 static inline void later2(void (*fun)(void *), void *data) {
426 eln2(fun, data, 0, 0);
427 }
428
429 void dialer_finalizer(SEXP);
430 void listener_finalizer(SEXP);
431 void socket_finalizer(SEXP);
432 void raio_invoke_cb(void *);
433 void haio_invoke_cb(void *);
434 SEXP mk_error(const int);
435 SEXP mk_error_data(const int);
436 SEXP nano_raw_char(const unsigned char *, const size_t);
437 void nano_serialize(nano_buf *, const SEXP, SEXP, int);
438 void nano_msg_set_body(nng_msg *, nano_buf *);
439 SEXP nano_unserialize(unsigned char *, const size_t, SEXP);
440 SEXP nano_decode(unsigned char *, const size_t, const uint8_t, SEXP);
441 SEXP nano_url_with_port(nng_url *, int);
442 void nano_encode(nano_buf *, const SEXP);
443 int nano_encode_mode(const SEXP);
444 int nano_matcharg(const SEXP);
445 SEXP nano_aio_result(SEXP);
446 SEXP nano_aio_get_msg(SEXP);
447 SEXP nano_aio_http_status(SEXP);
448
449 void pipe_cb_signal(nng_pipe, nng_pipe_ev, void *);
450 void pipe_cb_monitor(nng_pipe, nng_pipe_ev, void *);
451 void tls_finalizer(SEXP);
452
453 void nano_load_later(void);
454 SEXP nano_findVarInFrame(const SEXP, const SEXP, int *);
455 SEXP nano_PreserveObject(const SEXP);
456 void nano_ReleaseObject(SEXP);
457
458 void nano_list_do(nano_list_op, nano_aio *);
459 void nano_thread_shutdown(void);
460 int dispatch_cancel_direct(void *, int);
461
462 SEXP rnng_advance_rng_state(void);
463 SEXP rnng_aio_call(SEXP);
464 SEXP rnng_aio_collect(SEXP);
465 SEXP rnng_aio_collect_safe(SEXP);
466 SEXP rnng_aio_get_msg(SEXP);
467 SEXP rnng_aio_http_data(SEXP);
468 SEXP rnng_aio_http_headers(SEXP);
469 SEXP rnng_aio_http_status(SEXP);
470 SEXP rnng_aio_result(SEXP);
471 SEXP rnng_aio_stop(SEXP);
472 SEXP rnng_clock(void);
473 SEXP rnng_close(SEXP);
474 SEXP rnng_conn_close(SEXP);
475 SEXP rnng_ctx_close(SEXP);
476 SEXP rnng_ctx_create(SEXP);
477 SEXP rnng_ctx_open(SEXP);
478 SEXP rnng_cv_alloc(void);
479 SEXP rnng_cv_reset(SEXP);
480 SEXP rnng_cv_signal(SEXP);
481 SEXP rnng_cv_until(SEXP, SEXP);
482 SEXP rnng_cv_until_safe(SEXP, SEXP);
483 SEXP rnng_cv_value(SEXP);
484 SEXP rnng_cv_wait(SEXP);
485 SEXP rnng_cv_wait_safe(SEXP);
486 SEXP rnng_dial(SEXP, SEXP, SEXP, SEXP, SEXP);
487 SEXP rnng_dialer_close(SEXP);
488 SEXP rnng_dialer_start(SEXP, SEXP);
489 SEXP rnng_dispatcher_capacity(SEXP);
490 SEXP rnng_dispatcher_gate(SEXP);
491 SEXP rnng_dispatcher_try_gate(SEXP);
492 SEXP rnng_dispatcher_info(SEXP);
493 SEXP rnng_dispatcher_run(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
494 SEXP rnng_dispatcher_start(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
495 SEXP rnng_dispatcher_stop(SEXP);
496 SEXP rnng_dispatcher_wait(SEXP, SEXP);
497 SEXP rnng_eval_safe(SEXP);
498 SEXP rnng_fini(void);
499 SEXP rnng_fini_priors(void);
500 SEXP rnng_get_opt(SEXP, SEXP);
501 SEXP rnng_http_server_close(SEXP);
502 SEXP rnng_http_server_create(SEXP, SEXP, SEXP);
503 SEXP rnng_http_server_start(SEXP);
504 SEXP rnng_http_server_stop(SEXP);
505 SEXP rnng_ip_addr(void);
506 SEXP rnng_is_error_value(SEXP);
507 SEXP rnng_is_nul_byte(SEXP);
508 SEXP rnng_listen(SEXP, SEXP, SEXP, SEXP, SEXP);
509 SEXP rnng_listener_close(SEXP);
510 SEXP rnng_listener_start(SEXP);
511 SEXP rnng_marker_set(SEXP);
512 SEXP rnng_monitor_create(SEXP, SEXP);
513 SEXP rnng_monitor_read(SEXP);
514 SEXP rnng_ncurl(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
515 SEXP rnng_ncurl_aio(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
516 SEXP rnng_ncurl_session(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
517 SEXP rnng_ncurl_session_close(SEXP);
518 SEXP rnng_ncurl_transact(SEXP);
519 SEXP rnng_pipe_notify(SEXP, SEXP, SEXP, SEXP, SEXP);
520 SEXP rnng_protocol_open(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
521 SEXP rnng_random(SEXP, SEXP);
522 SEXP rnng_read_stdin(SEXP);
523 SEXP rnng_reap(SEXP);
524 SEXP rnng_recv(SEXP, SEXP, SEXP);
525 SEXP rnng_recv_aio(SEXP, SEXP, SEXP, SEXP, SEXP);
526 SEXP rnng_request(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
527 SEXP rnng_request_stop(SEXP);
528 SEXP rnng_send(SEXP, SEXP, SEXP, SEXP, SEXP);
529 SEXP rnng_send_aio(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
530 SEXP rnng_serial_config(SEXP, SEXP, SEXP);
531 SEXP rnng_set_opt(SEXP, SEXP, SEXP);
532 SEXP rnng_set_promise_context(SEXP, SEXP);
533 SEXP rnng_signal_thread_create(SEXP, SEXP);
534 SEXP rnng_sleep(SEXP);
535 SEXP rnng_stats_get(SEXP, SEXP);
536 SEXP rnng_status_code(SEXP);
537 SEXP rnng_stream_close(SEXP);
538 SEXP rnng_stream_conn_send(SEXP, SEXP);
539 SEXP rnng_stream_conn_set_header(SEXP, SEXP, SEXP);
540 SEXP rnng_stream_conn_set_status(SEXP, SEXP);
541 SEXP rnng_stream_open(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
542 SEXP rnng_strerror(SEXP);
543 SEXP rnng_subscribe(SEXP, SEXP, SEXP);
544 SEXP rnng_tls_config(SEXP, SEXP, SEXP, SEXP);
545 SEXP rnng_traverse_precious(void);
546 SEXP rnng_unresolved(SEXP);
547 SEXP rnng_unresolved2(SEXP);
548 SEXP rnng_race_aio(SEXP, SEXP);
549 SEXP rnng_url_parse(SEXP);
550 SEXP rnng_version(void);
551 SEXP rnng_wait_thread_create(SEXP);
552 SEXP rnng_write_cert(SEXP, SEXP);
553 SEXP rnng_write_stdout(SEXP);
554 SEXP rnng_ws_close(SEXP);
555 SEXP rnng_ws_send(SEXP, SEXP);
556
557 #endif