| 1 | // nanonext - NNG internal structure mirrors ------------------------------------ |
| 2 | |
| 3 | // Mirror structures for NNG HTTP header iteration (matches NNG v1.6+ internals) |
| 4 | // Layout must match: src/nng/src/core/list.h and src/nng/src/supplemental/http/http_msg.c |
| 5 | |
| 6 | // Copyright 2017 Garrett D'Amore <[email protected]> |
| 7 | // Copyright 2019 Staysail Systems, Inc. <[email protected]> |
| 8 | // Copyright 2018 Capitar IT Group BV <[email protected]> |
| 9 | // |
| 10 | // This software is supplied under the terms of the MIT License, a |
| 11 | // copy of which should be located in the distribution where this |
| 12 | // file was obtained (LICENSE.txt). A copy of the license may also be |
| 13 | // found online at https://opensource.org/licenses/MIT. |
| 14 | |
| 15 | #ifndef NANONEXT_NNG_STRUCTS_H |
| 16 | #define NANONEXT_NNG_STRUCTS_H |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | #include <stdint.h> |
| 20 | |
| 21 | typedef struct nano_list_node_s { |
| 22 | struct nano_list_node_s *next; |
| 23 | struct nano_list_node_s *prev; |
| 24 | } nano_list_node_s; |
| 25 | |
| 26 | typedef struct nano_list_s { |
| 27 | nano_list_node_s head; |
| 28 | size_t offset; |
| 29 | } nano_list_s; |
| 30 | |
| 31 | typedef struct nano_http_header_s { |
| 32 | char *name; |
| 33 | char *value; |
| 34 | nano_list_node_s node; |
| 35 | } nano_http_header_s; |
| 36 | |
| 37 | // Mirror of nng_http_req/nng_http_res - only hdrs field needed (first field in both) |
| 38 | typedef struct nano_http_msg_s { |
| 39 | nano_list_s hdrs; |
| 40 | } nano_http_msg_s; |
| 41 | |
| 42 | // Generic header iteration - works for both nng_http_req and nng_http_res |
| 43 | // since both have hdrs as their first field with identical structure |
| 44 | |
| 45 | static inline nano_http_header_s *nano_http_header_first(void *msg) { |
| 46 | nano_http_msg_s *m = (nano_http_msg_s *) msg; |
| 47 | nano_list_node_s *node = m->hdrs.head.next; |
| 48 | if (node == &m->hdrs.head) |
| 49 | return NULL; |
| 50 | return (nano_http_header_s *) ((char *) node - m->hdrs.offset); |
| 51 | } |
| 52 | |
| 53 | static inline nano_http_header_s *nano_http_header_next(void *msg, |
| 54 | nano_http_header_s *h) { |
| 55 | nano_http_msg_s *m = (nano_http_msg_s *) msg; |
| 56 | nano_list_node_s *node = h->node.next; |
| 57 | if (node == &m->hdrs.head) |
| 58 | return NULL; |
| 59 | return (nano_http_header_s *) ((char *) node - m->hdrs.offset); |
| 60 | } |
| 61 | |
| 62 | // Mirror of NNG nng_msg body layout for zero-copy buffer transfer. |
| 63 | // Used by nano_msg_set_body() to install a realloc'd serialization buffer |
| 64 | // directly as the message body, avoiding a redundant allocation + memcpy. |
| 65 | // |
| 66 | // Assumed NNG internal layout (stable since NNG 1.6, required >= 1.11.0): |
| 67 | // |
| 68 | // struct nng_msg { // src/nng/src/core/message.c |
| 69 | // uint32_t m_header_buf[16]; // NNI_MAX_MAX_TTL (15) + 1 |
| 70 | // size_t m_header_len; |
| 71 | // nni_chunk m_body; // <-- we access this |
| 72 | // uint32_t m_pipe; |
| 73 | // nni_atomic_int m_refcnt; |
| 74 | // }; |
| 75 | // |
| 76 | // typedef struct { // nni_chunk |
| 77 | // size_t ch_cap; |
| 78 | // size_t ch_len; |
| 79 | // uint8_t *ch_buf; |
| 80 | // uint8_t *ch_ptr; |
| 81 | // } nni_chunk; |
| 82 | // |
| 83 | // nni_free() is free() on all platforms, so body.buf may be a malloc'd buffer. |
| 84 | |
| 85 | typedef struct nano_nng_chunk_s { |
| 86 | size_t cap; |
| 87 | size_t len; |
| 88 | uint8_t *buf; |
| 89 | uint8_t *ptr; |
| 90 | } nano_nng_chunk; |
| 91 | |
| 92 | typedef struct nano_nng_msg_s { |
| 93 | uint32_t header_buf[16]; |
| 94 | size_t header_len; |
| 95 | nano_nng_chunk body; |
| 96 | } nano_nng_msg; |
| 97 | |
| 98 | #endif // NANONEXT_NNG_STRUCTS_H |