| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| #include "ppport.h" |
| 5 |
|
| |
| 6 |
|
| #include "EVAPI.h" |
| 7 |
|
| #include "ngx-queue.h" |
| 8 |
|
| |
| 9 |
|
| #include <sys/socket.h> |
| 10 |
|
| #include <sys/un.h> |
| 11 |
|
| #include <netinet/in.h> |
| 12 |
|
| #include <netinet/tcp.h> |
| 13 |
|
| #include <netdb.h> |
| 14 |
|
| #include <fcntl.h> |
| 15 |
|
| #include <errno.h> |
| 16 |
|
| #include <unistd.h> |
| 17 |
|
| #include <string.h> |
| 18 |
|
| #include <stdlib.h> |
| 19 |
|
| #include <time.h> |
| 20 |
|
| #include <arpa/inet.h> |
| 21 |
|
| |
| 22 |
|
| /* Makefile.PL defines this from $EV::Nats::VERSION so the two cannot drift */ |
| 23 |
|
| #ifndef EV_NATS_VERSION |
| 24 |
|
| #define EV_NATS_VERSION "0.00" |
| 25 |
|
| #endif |
| 26 |
|
| |
| 27 |
|
| #ifdef HAVE_OPENSSL |
| 28 |
|
| #include <openssl/ssl.h> |
| 29 |
|
| #include <openssl/err.h> |
| 30 |
|
| #include <openssl/x509v3.h> |
| 31 |
|
| #endif |
| 32 |
|
| |
| 33 |
|
| /* ================================================================ |
| 34 |
|
| * Constants |
| 35 |
|
| * ================================================================ */ |
| 36 |
|
| |
| 37 |
|
| #define NATS_MAGIC_ALIVE 0xCA5E4A75 |
| 38 |
|
| |
| 39 |
|
| #define BUF_INIT_SIZE 16384 |
| 40 |
|
| #define MAX_CONTROL_LINE 4096 |
| 41 |
|
| |
| 42 |
|
| /* snprintf returns the length it WOULD have written; if the control line was |
| 43 |
|
| * truncated (n >= bufsz) the buffer holds only bufsz-1 bytes + NUL. Clamp the |
| 44 |
|
| * length used for the subsequent send so an over-long subject/reply/queue |
| 45 |
|
| * cannot cause a stack over-read. (A truncated control line is malformed and |
| 46 |
|
| * the server will reject it -- but we never read past the buffer.) */ |
| 47 |
|
| #define NATS_CLAMP_LEN(n, bufsz) \ |
| 48 |
|
| do { if ((n) < 0) (n) = 0; else if ((n) >= (int)(bufsz)) (n) = (int)(bufsz) - 1; } while (0) |
| 49 |
|
| |
| 50 |
|
| #define DEFAULT_MAX_PAYLOAD (1024 * 1024) |
| 51 |
|
| #define MAX_SERVER_POOL 64 |
| 52 |
|
| |
| 53 |
|
| #define PARSE_OP 0 |
| 54 |
|
| #define PARSE_MSG_BODY 1 |
| 55 |
|
| |
| 56 |
|
| #define MSG_TYPE_MSG 0 |
| 57 |
|
| #define MSG_TYPE_HMSG 1 |
| 58 |
|
| |
| 59 |
|
| #define CLEAR_HANDLER(field) \ |
| 60 |
|
| do { if (NULL != (field)) { SvREFCNT_dec(field); (field) = NULL; } } while(0) |
| 61 |
|
| |
| 62 |
|
| /* call_sv a stored handler SV, pinned across the call so the callback may |
| 63 |
|
| * clear its own handler (e.g. $nats->on_error(undef)) without freeing the CV |
| 64 |
|
| * mid-call. Args must already be pushed. */ |
| 65 |
|
| #define PINNED_CALL_SV(handler, flags) \ |
| 66 |
|
| STMT_START { SV *_pin = (handler); SvREFCNT_inc_simple_void_NN(_pin); \ |
| 67 |
|
| call_sv(_pin, (flags)); SvREFCNT_dec(_pin); } STMT_END |
| 68 |
|
| |
| 69 |
|
| #define NATS_CROAK_UNLESS_CONNECTED(self) \ |
| 70 |
|
| do { \ |
| 71 |
|
| if (!(self)->connected && !(self)->connecting && \ |
| 72 |
|
| !((self)->reconnect_enabled && (self)->reconnect_timer_active)) \ |
| 73 |
|
| croak("not connected"); \ |
| 74 |
|
| } while(0) |
| 75 |
|
| |
| 76 |
|
| /* ================================================================ |
| 77 |
|
| * Type declarations |
| 78 |
|
| * ================================================================ */ |
| 79 |
|
| |
| 80 |
|
| typedef struct nats_s nats_t; |
| 81 |
|
| typedef struct nats_sub_s nats_sub_t; |
| 82 |
|
| typedef struct nats_pub_s nats_pub_t; |
| 83 |
|
| |
| 84 |
|
| typedef nats_t* EV__Nats; |
| 85 |
|
| typedef struct ev_loop* EV__Loop; |
| 86 |
|
| |
| 87 |
|
| /* ================================================================ |
| 88 |
|
| * Data structures |
| 89 |
|
| * ================================================================ */ |
| 90 |
|
| |
| 91 |
|
| struct nats_sub_s { |
| 92 |
|
| uint64_t sid; |
| 93 |
|
| SV *subject; |
| 94 |
|
| SV *queue_group; |
| 95 |
|
| SV *cb; |
| 96 |
|
| int max_msgs; |
| 97 |
|
| int received; |
| 98 |
|
| ngx_queue_t queue; |
| 99 |
|
| }; |
| 100 |
|
| |
| 101 |
|
| /* PONG callback queue entry, one per client-sent PING (FIFO stays 1:1). |
| 102 |
|
| kind: 0 = user flush cb (NULL cb = cb-less flush placeholder), |
| 103 |
|
| 1 = drain marker, 2 = internal PING placeholder. */ |
| 104 |
|
| typedef struct nats_pong_cb_s { |
| 105 |
|
| SV *cb; |
| 106 |
|
| int kind; |
| 107 |
|
| ngx_queue_t queue; |
| 108 |
|
| } nats_pong_cb_t; |
| 109 |
|
| |
| 110 |
|
| /* Server pool entry (parsed from INFO connect_urls) */ |
| 111 |
|
| typedef struct nats_server_s { |
| 112 |
|
| char *host; |
| 113 |
|
| int port; |
| 114 |
|
| ngx_queue_t queue; |
| 115 |
|
| } nats_server_t; |
| 116 |
|
| |
| 117 |
|
| struct nats_pub_s { |
| 118 |
|
| char *data; |
| 119 |
|
| size_t len; |
| 120 |
|
| ngx_queue_t queue; |
| 121 |
|
| }; |
| 122 |
|
| |
| 123 |
|
| struct nats_s { |
| 124 |
|
| unsigned int magic; |
| 125 |
|
| struct ev_loop *loop; |
| 126 |
|
| int fd; |
| 127 |
|
| int connected; |
| 128 |
|
| int connecting; |
| 129 |
|
| int connect_pending; /* nonblocking connect(2) still in flight */ |
| 130 |
|
| |
| 131 |
|
| ev_io rio, wio; |
| 132 |
|
| int reading, writing; |
| 133 |
|
| |
| 134 |
|
| char *rbuf; |
| 135 |
|
| size_t rbuf_len, rbuf_cap; |
| 136 |
|
| |
| 137 |
|
| char *wbuf; |
| 138 |
|
| size_t wbuf_len, wbuf_off, wbuf_cap; |
| 139 |
|
| |
| 140 |
|
| SV *on_error; |
| 141 |
|
| SV *on_connect; |
| 142 |
|
| SV *on_disconnect; |
| 143 |
|
| char *pending_croak; /* no-on_error error, fired at wrapper exit */ |
| 144 |
|
| |
| 145 |
|
| ngx_queue_t subs; |
| 146 |
|
| HV *sub_map; /* SID -> nats_sub_t* hash for O(1) lookup */ |
| 147 |
|
| uint64_t next_sid; |
| 148 |
|
| |
| 149 |
|
| ngx_queue_t wait_queue; |
| 150 |
|
| int waiting_count; |
| 151 |
|
| size_t wait_bytes; /* queued bytes, capped by reconnect_buf_size */ |
| 152 |
|
| size_t reconnect_buf_size; /* 0 = unlimited */ |
| 153 |
|
| |
| 154 |
|
| /* Write coalescing */ |
| 155 |
|
| ev_prepare prepare_watcher; |
| 156 |
|
| int prepare_active; |
| 157 |
|
| int wbuf_dirty; |
| 158 |
|
| |
| 159 |
|
| char *host; |
| 160 |
|
| int port; |
| 161 |
|
| char *path; /* Unix socket path */ |
| 162 |
|
| |
| 163 |
|
| /* getaddrinfo candidates kept across async connect failures for failover */ |
| 164 |
|
| struct addrinfo *ai_res; /* the list to freeaddrinfo */ |
| 165 |
|
| struct addrinfo *ai_next; /* next candidate to try */ |
| 166 |
|
| |
| 167 |
|
| int reconnect_enabled; |
| 168 |
|
| int reconnect_delay_ms; |
| 169 |
|
| int max_reconnect_delay_ms; |
| 170 |
|
| int max_reconnect_attempts; |
| 171 |
|
| int reconnect_attempts; |
| 172 |
|
| ev_timer reconnect_timer; |
| 173 |
|
| int reconnect_timer_active; |
| 174 |
|
| int intentional_disconnect; |
| 175 |
|
| |
| 176 |
|
| int connect_timeout_ms; |
| 177 |
|
| ev_timer connect_timer; |
| 178 |
|
| int connect_timer_active; |
| 179 |
|
| |
| 180 |
|
| int ping_interval_ms; |
| 181 |
|
| ev_timer ping_timer; |
| 182 |
|
| int ping_timer_active; |
| 183 |
|
| int pings_outstanding; |
| 184 |
|
| int max_pings_outstanding; |
| 185 |
|
| |
| 186 |
|
| int max_payload; |
| 187 |
|
| SV *server_info_json; |
| 188 |
|
| |
| 189 |
|
| char *user; |
| 190 |
|
| char *pass; |
| 191 |
|
| char *token; |
| 192 |
|
| char *name; |
| 193 |
|
| int verbose; |
| 194 |
|
| int pedantic; |
| 195 |
|
| int echo; |
| 196 |
|
| int no_responders; |
| 197 |
|
| |
| 198 |
|
| char inbox_prefix[32]; |
| 199 |
|
| uint64_t next_req_id; |
| 200 |
|
| ngx_queue_t req_queue; |
| 201 |
|
| uint64_t inbox_sub_sid; |
| 202 |
|
| |
| 203 |
|
| int parse_state; |
| 204 |
|
| int msg_type; |
| 205 |
|
| /* MSG/HMSG fields: absolute offsets into rbuf (safe across Renew) */ |
| 206 |
|
| size_t msg_subject_off; |
| 207 |
|
| size_t msg_subject_len; |
| 208 |
|
| size_t msg_reply_off; /* msg_reply_len == 0 means no reply */ |
| 209 |
|
| size_t msg_reply_len; |
| 210 |
|
| uint64_t msg_sid; |
| 211 |
|
| size_t msg_hdr_len; |
| 212 |
|
| size_t msg_total_len; |
| 213 |
|
| |
| 214 |
|
| int priority; |
| 215 |
|
| int keepalive; |
| 216 |
|
| |
| 217 |
|
| /* Stats */ |
| 218 |
|
| UV msgs_in; |
| 219 |
|
| UV msgs_out; |
| 220 |
|
| UV bytes_in; |
| 221 |
|
| UV bytes_out; |
| 222 |
|
| |
| 223 |
|
| /* Server pool for cluster failover */ |
| 224 |
|
| ngx_queue_t server_pool; |
| 225 |
|
| int server_pool_count; |
| 226 |
|
| |
| 227 |
|
| /* Drain state */ |
| 228 |
|
| int draining; |
| 229 |
|
| SV *drain_cb; |
| 230 |
|
| |
| 231 |
|
| /* Slow consumer detection */ |
| 232 |
|
| size_t slow_consumer_bytes; /* wbuf threshold, 0 = disabled */ |
| 233 |
|
| SV *on_slow_consumer; |
| 234 |
|
| int slow_fired; |
| 235 |
|
| |
| 236 |
|
| /* Batch mode */ |
| 237 |
|
| int batch_mode; |
| 238 |
|
| |
| 239 |
|
| /* PONG callback queue (for flush) */ |
| 240 |
|
| ngx_queue_t pong_cbs; |
| 241 |
|
| |
| 242 |
|
| /* NKey auth */ |
| 243 |
|
| char *nkey_seed; /* Ed25519 seed (base32-encoded) */ |
| 244 |
|
| char *jwt; |
| 245 |
|
| char *server_nonce; /* nonce from INFO for signing */ |
| 246 |
|
| |
| 247 |
|
| /* Lame duck mode (leaf node graceful shutdown) */ |
| 248 |
|
| int ldm; |
| 249 |
|
| SV *on_ldm; |
| 250 |
|
| |
| 251 |
|
| /* Deferred destroy: DESTROY inside an event-loop callback only sets |
| 252 |
|
| destroy_pending; the outermost nats_on_* wrapper frees (cb_depth) */ |
| 253 |
|
| int cb_depth; |
| 254 |
|
| int destroy_pending; |
| 255 |
|
| /* Borrowed owner referent; pinned across method-reachable user |
| 256 |
|
| callbacks so DESTROY cannot free self mid-method. */ |
| 257 |
|
| SV *owner_sv; |
| 258 |
|
| |
| 259 |
|
| /* TLS */ |
| 260 |
|
| #ifdef HAVE_OPENSSL |
| 261 |
|
| SSL_CTX *ssl_ctx; |
| 262 |
|
| SSL *ssl; |
| 263 |
|
| int tls; |
| 264 |
|
| int tls_skip_verify; |
| 265 |
|
| char *tls_ca_file; |
| 266 |
|
| int ssl_handshaking; |
| 267 |
|
| int ssl_write_len; |
| 268 |
|
| #endif |
| 269 |
|
| SV *loop_sv; |
| 270 |
|
| }; |
| 271 |
|
| |
| 272 |
|
| typedef struct nats_req_s { |
| 273 |
|
| uint64_t req_id; |
| 274 |
|
| SV *cb; |
| 275 |
|
| ev_timer timer; |
| 276 |
|
| int timer_active; |
| 277 |
|
| ngx_queue_t queue; |
| 278 |
|
| } nats_req_t; |
| 279 |
|
| |
| 280 |
|
| /* ================================================================ |
| 281 |
|
| * Forward declarations |
| 282 |
|
| * ================================================================ */ |
| 283 |
|
| |
| 284 |
|
| static void nats_connect_tcp(nats_t *self); |
| 285 |
|
| static void nats_connect_unix(nats_t *self); |
| 286 |
|
| static void nats_free_addrinfo(nats_t *self); |
| 287 |
|
| static void nats_do_connect(nats_t *self); |
| 288 |
|
| static void nats_schedule_reconnect(nats_t *self); |
| 289 |
|
| static void nats_next_server(nats_t *self); |
| 290 |
|
| static void nats_free_server_pool(nats_t *self); |
| 291 |
|
| static void nats_on_read(struct ev_loop *loop, ev_io *w, int revents); |
| 292 |
|
| static void nats_on_prepare(struct ev_loop *loop, ev_prepare *w, int revents); |
| 293 |
|
| static void nats_parse_connect_urls(nats_t *self, const char *json, size_t len); |
| 294 |
|
| static void nats_on_write(struct ev_loop *loop, ev_io *w, int revents); |
| 295 |
|
| static void nats_on_connect_timeout(struct ev_loop *loop, ev_timer *w, int revents); |
| 296 |
|
| static void nats_on_reconnect_timer(struct ev_loop *loop, ev_timer *w, int revents); |
| 297 |
|
| static void nats_on_ping_timer(struct ev_loop *loop, ev_timer *w, int revents); |
| 298 |
|
| static void nats_send_connect(nats_t *self); |
| 299 |
|
| static void nats_try_write(nats_t *self); |
| 300 |
|
| static void nats_process_line(nats_t *self, char *line, size_t len); |
| 301 |
|
| static void nats_process_msg(nats_t *self, char *payload, size_t len); |
| 302 |
|
| static void nats_emit_error(nats_t *self, const char *err); |
| 303 |
|
| static void nats_cleanup(nats_t *self); |
| 304 |
|
| static void nats_destroy_now(nats_t *self); |
| 305 |
|
| static void nats_start_ping_timer(nats_t *self); |
| 306 |
|
| static void nats_drain_waiting(nats_t *self); |
| 307 |
|
| static void nats_setup_inbox(nats_t *self); |
| 308 |
|
| static nats_sub_t *nats_find_sub(nats_t *self, uint64_t sid); |
| 309 |
|
| static void nats_remove_sub(nats_t *self, nats_sub_t *sub); |
| 310 |
|
| static void nats_cancel_all_requests(nats_t *self, const char *err); |
| 311 |
|
| static void nats_resub_all(nats_t *self); |
| 312 |
|
| |
| 313 |
|
| /* ================================================================ |
| 314 |
|
| * String helpers |
| 315 |
|
| * ================================================================ */ |
| 316 |
|
| |
| 317 |
|
| /* Replace *dst with a fresh copy of NUL-terminated src; frees existing. */ |
| 318 |
138 |
| static void nats_set_str(char **dst, const char *src) |
| 319 |
|
| { |
| 320 |
138 |
▶ | if (*dst) { Safefree(*dst); *dst = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dst) { Safefree(*dst); *dst = NULL; } |
26 |
112 |
138 |
|
| 321 |
138 |
▶ | if (src) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src) { |
37 |
101 |
138 |
|
| 322 |
37 |
| size_t l = strlen(src); |
| 323 |
37 |
| Newx(*dst, l + 1, char); |
| 324 |
37 |
| memcpy(*dst, src, l + 1); |
| 325 |
|
| } |
| 326 |
138 |
| } |
| 327 |
|
| |
| 328 |
|
| /* Replace *dst with a copy of an SV's bytes (handles non-NUL-terminated PV). */ |
| 329 |
57 |
| static void nats_set_str_sv(char **dst, SV *val) |
| 330 |
|
| { |
| 331 |
|
| STRLEN l; |
| 332 |
|
| const char *s; |
| 333 |
57 |
▶ | if (*dst) { Safefree(*dst); *dst = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dst) { Safefree(*dst); *dst = NULL; } |
0 |
57 |
57 |
|
| 334 |
57 |
▶ | if (!SvOK(val)) return; /* undef means "unset": NULL, not an empty string */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(val)) return; /* undef means "unset": NULL, not an empty string */ |
0 |
57 |
57 |
|
| 335 |
57 |
| s = SvPV(val, l); |
| 336 |
57 |
| Newx(*dst, l + 1, char); |
| 337 |
57 |
| memcpy(*dst, s, l); |
| 338 |
57 |
| (*dst)[l] = '\0'; |
| 339 |
|
| } |
| 340 |
|
| |
| 341 |
|
| /* ================================================================ |
| 342 |
|
| * Buffer helpers |
| 343 |
|
| * ================================================================ */ |
| 344 |
|
| |
| 345 |
524 |
| static void buf_ensure(char **buf, size_t *cap, size_t needed) |
| 346 |
|
| { |
| 347 |
524 |
▶ | if (needed <= *cap) |
Branch: 1 branch
| Branch | True | False | Total |
| if (needed <= *cap) |
443 |
81 |
524 |
|
| 348 |
443 |
| return; |
| 349 |
81 |
▶ | size_t newcap = *cap ? *cap : BUF_INIT_SIZE; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t newcap = *cap ? *cap : BUF_INIT_SIZE; |
3 |
78 |
81 |
|
| 350 |
84 |
▶ | while (newcap < needed) |
Branch: 1 branch
| Branch | True | False | Total |
| while (newcap < needed) |
3 |
81 |
84 |
|
| 351 |
3 |
| newcap *= 2; |
| 352 |
81 |
| Renew(*buf, newcap, char); |
| 353 |
81 |
| *cap = newcap; |
| 354 |
|
| } |
| 355 |
|
| |
| 356 |
324 |
| static void wbuf_append(nats_t *self, const char *data, size_t len) |
| 357 |
|
| { |
| 358 |
324 |
| size_t used = self->wbuf_len - self->wbuf_off; |
| 359 |
324 |
▶ | if (self->wbuf_off > 0 && self->wbuf_off > self->wbuf_len / 2) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->wbuf_off > 0 && self->wbuf_off > self->wbuf_len / 2) { |
0 |
324 |
324 |
| if (self->wbuf_off > 0 && self->wbuf_off > self->wbuf_len / 2) { |
0 |
0 |
0 |
|
| 360 |
0 |
▶ | if (used > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (used > 0) |
0 |
0 |
0 |
|
| 361 |
0 |
| memmove(self->wbuf, self->wbuf + self->wbuf_off, used); |
| 362 |
0 |
| self->wbuf_len = used; |
| 363 |
0 |
| self->wbuf_off = 0; |
| 364 |
|
| } |
| 365 |
324 |
| buf_ensure(&self->wbuf, &self->wbuf_cap, self->wbuf_len + len); |
| 366 |
324 |
| memcpy(self->wbuf + self->wbuf_len, data, len); |
| 367 |
324 |
| self->wbuf_len += len; |
| 368 |
324 |
| } |
| 369 |
|
| |
| 370 |
|
| /* Bounded substring search for counted buffers: rbuf slices are not |
| 371 |
|
| NUL-terminated, so strstr() is unsafe (memmem() is not portable). */ |
| 372 |
210 |
| static const char *nats_memfind(const char *hay, size_t hay_len, |
| 373 |
|
| const char *needle, size_t needle_len) |
| 374 |
|
| { |
| 375 |
|
| const char *last; |
| 376 |
|
| const char *p; |
| 377 |
210 |
▶ | if (needle_len == 0) return hay; |
Branch: 1 branch
| Branch | True | False | Total |
| if (needle_len == 0) return hay; |
0 |
210 |
210 |
|
| 378 |
210 |
▶ | if (hay_len < needle_len) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hay_len < needle_len) return NULL; |
4 |
206 |
210 |
|
| 379 |
206 |
| last = hay + (hay_len - needle_len); |
| 380 |
206 |
| p = hay; |
| 381 |
1730 |
▶ | while (p <= last) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p <= last) { |
1724 |
6 |
1730 |
|
| 382 |
1724 |
| p = (const char *)memchr(p, needle[0], (size_t)(last - p) + 1); |
| 383 |
1724 |
▶ | if (!p) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) return NULL; |
154 |
1570 |
1724 |
|
| 384 |
1570 |
▶ | if (memcmp(p, needle, needle_len) == 0) return p; |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(p, needle, needle_len) == 0) return p; |
46 |
1524 |
1570 |
|
| 385 |
1524 |
| p++; |
| 386 |
|
| } |
| 387 |
6 |
| return NULL; |
| 388 |
|
| } |
| 389 |
|
| |
| 390 |
|
| /* ================================================================ |
| 391 |
|
| * NKey helpers (base32 + Ed25519) |
| 392 |
|
| * ================================================================ */ |
| 393 |
|
| |
| 394 |
|
| #ifdef HAVE_OPENSSL |
| 395 |
|
| #include <openssl/evp.h> |
| 396 |
|
| |
| 397 |
|
| /* NATS base32 decode (RFC 4648, no padding). dst_cap bounds the output: |
| 398 |
|
| an over-long input (e.g. a hostile .creds seed) must not smash dst. */ |
| 399 |
10 |
| static int nats_base32_decode(const char *src, size_t src_len, |
| 400 |
|
| unsigned char *dst, size_t dst_cap, |
| 401 |
|
| size_t *dst_len) |
| 402 |
|
| { |
| 403 |
|
| static const int8_t b32_tab[128] = { |
| 404 |
|
| -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 405 |
|
| -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 406 |
|
| -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 407 |
|
| -1,-1,26,27,28,29,30,31,-1,-1,-1,-1,-1,-1,-1,-1, /* 2-7 */ |
| 408 |
|
| -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* A-O */ |
| 409 |
|
| 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* P-Z */ |
| 410 |
|
| -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* a-o */ |
| 411 |
|
| 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* p-z */ |
| 412 |
|
| }; |
| 413 |
10 |
| uint64_t buf = 0; |
| 414 |
10 |
| int bits = 0; |
| 415 |
10 |
| size_t di = 0; |
| 416 |
|
| size_t i; |
| 417 |
|
| |
| 418 |
428 |
▶ | for (i = 0; i < src_len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < src_len; i++) { |
422 |
6 |
428 |
|
| 419 |
422 |
| unsigned char c = (unsigned char)src[i]; |
| 420 |
422 |
▶ | if (c == '=' || c == ' ') continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '=' || c == ' ') continue; |
422 |
0 |
422 |
| if (c == '=' || c == ' ') continue; |
0 |
422 |
422 |
|
| 421 |
422 |
▶ | if (c >= 128 || b32_tab[c] < 0) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 128 || b32_tab[c] < 0) return -1; |
422 |
0 |
422 |
| if (c >= 128 || b32_tab[c] < 0) return -1; |
3 |
419 |
422 |
|
| 422 |
419 |
| buf = (buf << 5) | b32_tab[c]; |
| 423 |
419 |
| bits += 5; |
| 424 |
419 |
▶ | if (bits >= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bits >= 8) { |
259 |
160 |
419 |
|
| 425 |
259 |
| bits -= 8; |
| 426 |
259 |
▶ | if (di >= dst_cap) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (di >= dst_cap) return -1; |
1 |
258 |
259 |
|
| 427 |
258 |
| dst[di++] = (unsigned char)(buf >> bits); |
| 428 |
|
| } |
| 429 |
|
| } |
| 430 |
6 |
| *dst_len = di; |
| 431 |
6 |
| return 0; |
| 432 |
|
| } |
| 433 |
|
| |
| 434 |
|
| /* NATS base32 encode (RFC 4648, no padding). Streams 8-bit input through |
| 435 |
|
| a 5-bit accumulator. Returns number of chars written, or -1 if dst is |
| 436 |
|
| too small. */ |
| 437 |
8 |
| static int nats_base32_encode(const unsigned char *src, size_t src_len, |
| 438 |
|
| char *dst, size_t dst_size) |
| 439 |
|
| { |
| 440 |
|
| static const char b32[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; |
| 441 |
8 |
| size_t di = 0; |
| 442 |
8 |
| uint64_t buf = 0; |
| 443 |
8 |
| int bits = 0; |
| 444 |
|
| size_t i; |
| 445 |
291 |
▶ | for (i = 0; i < src_len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < src_len; i++) { |
283 |
8 |
291 |
|
| 446 |
283 |
| buf = (buf << 8) | src[i]; |
| 447 |
283 |
| bits += 8; |
| 448 |
734 |
▶ | while (bits >= 5) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (bits >= 5) { |
451 |
283 |
734 |
|
| 449 |
451 |
▶ | if (di >= dst_size) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (di >= dst_size) return -1; |
0 |
451 |
451 |
|
| 450 |
451 |
| bits -= 5; |
| 451 |
451 |
| dst[di++] = b32[(buf >> bits) & 0x1F]; |
| 452 |
|
| } |
| 453 |
|
| } |
| 454 |
8 |
▶ | if (bits > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bits > 0) { |
3 |
5 |
8 |
|
| 455 |
3 |
▶ | if (di >= dst_size) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (di >= dst_size) return -1; |
0 |
3 |
3 |
|
| 456 |
3 |
| dst[di++] = b32[(buf << (5 - bits)) & 0x1F]; |
| 457 |
|
| } |
| 458 |
8 |
| return (int)di; |
| 459 |
|
| } |
| 460 |
|
| |
| 461 |
|
| /* NATS base64url encode (no padding) */ |
| 462 |
0 |
| static int nats_base64url_encode(const unsigned char *src, size_t src_len, char *dst, size_t dst_size) |
| 463 |
|
| { |
| 464 |
|
| static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
| 465 |
0 |
| size_t di = 0; |
| 466 |
|
| size_t i; |
| 467 |
0 |
▶ | for (i = 0; i + 2 < src_len; i += 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 2 < src_len; i += 3) { |
0 |
0 |
0 |
|
| 468 |
0 |
▶ | if (di + 4 > dst_size) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (di + 4 > dst_size) return -1; |
0 |
0 |
0 |
|
| 469 |
0 |
| uint32_t v = ((uint32_t)src[i] << 16) | ((uint32_t)src[i+1] << 8) | src[i+2]; |
| 470 |
0 |
| dst[di++] = b64[(v >> 18) & 0x3F]; |
| 471 |
0 |
| dst[di++] = b64[(v >> 12) & 0x3F]; |
| 472 |
0 |
| dst[di++] = b64[(v >> 6) & 0x3F]; |
| 473 |
0 |
| dst[di++] = b64[v & 0x3F]; |
| 474 |
|
| } |
| 475 |
0 |
▶ | if (i < src_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < src_len) { |
0 |
0 |
0 |
|
| 476 |
0 |
▶ | if (di + 4 > dst_size) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (di + 4 > dst_size) return -1; |
0 |
0 |
0 |
|
| 477 |
0 |
| uint32_t v = (uint32_t)src[i] << 16; |
| 478 |
0 |
▶ | if (i + 1 < src_len) v |= (uint32_t)src[i+1] << 8; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i + 1 < src_len) v |= (uint32_t)src[i+1] << 8; |
0 |
0 |
0 |
|
| 479 |
0 |
| dst[di++] = b64[(v >> 18) & 0x3F]; |
| 480 |
0 |
| dst[di++] = b64[(v >> 12) & 0x3F]; |
| 481 |
0 |
▶ | if (i + 1 < src_len) dst[di++] = b64[(v >> 6) & 0x3F]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i + 1 < src_len) dst[di++] = b64[(v >> 6) & 0x3F]; |
0 |
0 |
0 |
|
| 482 |
|
| } |
| 483 |
0 |
▶ | if (di < dst_size) dst[di] = '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (di < dst_size) dst[di] = '\0'; |
0 |
0 |
0 |
|
| 484 |
0 |
| return (int)di; |
| 485 |
|
| } |
| 486 |
|
| |
| 487 |
|
| /* CRC-16/XMODEM (CRC-CCITT, poly 0x1021, init 0x0000) — what NATS NKeys |
| 488 |
|
| use. NOT CRC-16/IBM. Used for seed/pubkey integrity. */ |
| 489 |
13 |
| static uint16_t nats_crc16(const unsigned char *data, size_t len) |
| 490 |
|
| { |
| 491 |
13 |
| uint16_t crc = 0; |
| 492 |
|
| size_t i; |
| 493 |
|
| int j; |
| 494 |
450 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
437 |
13 |
450 |
|
| 495 |
437 |
| crc ^= ((uint16_t)data[i]) << 8; |
| 496 |
3933 |
▶ | for (j = 0; j < 8; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < 8; j++) { |
3496 |
437 |
3933 |
|
| 497 |
3496 |
▶ | if (crc & 0x8000) crc = (uint16_t)((crc << 1) ^ 0x1021); |
Branch: 1 branch
| Branch | True | False | Total |
| if (crc & 0x8000) crc = (uint16_t)((crc << 1) ^ 0x1021); |
1793 |
1703 |
3496 |
|
| 498 |
1703 |
| else crc = (uint16_t)(crc << 1); |
| 499 |
|
| } |
| 500 |
|
| } |
| 501 |
13 |
| return crc; |
| 502 |
|
| } |
| 503 |
|
| |
| 504 |
0 |
| static int nats_nkey_sign(const char *seed_encoded, const char *nonce, size_t nonce_len, |
| 505 |
|
| char *sig_out, size_t sig_out_size) |
| 506 |
|
| { |
| 507 |
|
| unsigned char raw[64]; |
| 508 |
0 |
| size_t raw_len = 0; |
| 509 |
|
| |
| 510 |
0 |
▶ | if (nats_base32_decode(seed_encoded, strlen(seed_encoded), raw, sizeof(raw), &raw_len) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_base32_decode(seed_encoded, strlen(seed_encoded), raw, sizeof(raw), &raw_len) != 0) |
0 |
0 |
0 |
|
| 511 |
0 |
| return -1; |
| 512 |
0 |
▶ | if (raw_len != 36) return -1; /* prefix(2) + seed(32) + CRC(2), exactly */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (raw_len != 36) return -1; /* prefix(2) + seed(32) + CRC(2), exactly */ |
0 |
0 |
0 |
|
| 513 |
|
| |
| 514 |
|
| /* Validate CRC16 */ |
| 515 |
0 |
| uint16_t expected_crc = (uint16_t)raw[raw_len - 2] | ((uint16_t)raw[raw_len - 1] << 8); |
| 516 |
0 |
| uint16_t actual_crc = nats_crc16(raw, raw_len - 2); |
| 517 |
0 |
▶ | if (expected_crc != actual_crc) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (expected_crc != actual_crc) return -1; |
0 |
0 |
0 |
|
| 518 |
|
| |
| 519 |
0 |
| unsigned char *seed = raw + 2; |
| 520 |
|
| |
| 521 |
0 |
| EVP_PKEY *pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, NULL, seed, 32); |
| 522 |
0 |
▶ | if (!pkey) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pkey) return -1; |
0 |
0 |
0 |
|
| 523 |
|
| |
| 524 |
0 |
| EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
| 525 |
0 |
▶ | if (!ctx) { EVP_PKEY_free(pkey); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctx) { EVP_PKEY_free(pkey); return -1; } |
0 |
0 |
0 |
|
| 526 |
|
| |
| 527 |
|
| unsigned char sig[64]; |
| 528 |
0 |
| size_t sig_len = sizeof(sig); |
| 529 |
|
| |
| 530 |
0 |
| int ok = (EVP_DigestSignInit(ctx, NULL, NULL, NULL, pkey) == 1 && |
| 531 |
0 |
| EVP_DigestSign(ctx, sig, &sig_len, (const unsigned char *)nonce, nonce_len) == 1); |
| 532 |
|
| |
| 533 |
0 |
| EVP_MD_CTX_free(ctx); |
| 534 |
0 |
| EVP_PKEY_free(pkey); |
| 535 |
|
| |
| 536 |
0 |
▶ | if (!ok) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) return -1; |
0 |
0 |
0 |
|
| 537 |
|
| |
| 538 |
0 |
| return nats_base64url_encode(sig, sig_len, sig_out, sig_out_size); |
| 539 |
|
| } |
| 540 |
|
| |
| 541 |
10 |
| static int nats_nkey_public(const char *seed_encoded, char *pub_out, size_t pub_out_size) |
| 542 |
|
| { |
| 543 |
|
| unsigned char raw[64]; |
| 544 |
10 |
| size_t raw_len = 0; |
| 545 |
|
| |
| 546 |
10 |
▶ | if (nats_base32_decode(seed_encoded, strlen(seed_encoded), raw, sizeof(raw), &raw_len) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_base32_decode(seed_encoded, strlen(seed_encoded), raw, sizeof(raw), &raw_len) != 0) |
4 |
6 |
10 |
|
| 547 |
4 |
| return -1; |
| 548 |
6 |
▶ | if (raw_len != 36) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (raw_len != 36) return -1; |
1 |
5 |
6 |
|
| 549 |
|
| |
| 550 |
5 |
| uint16_t expected_crc = (uint16_t)raw[raw_len - 2] | ((uint16_t)raw[raw_len - 1] << 8); |
| 551 |
5 |
▶ | if (nats_crc16(raw, raw_len - 2) != expected_crc) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_crc16(raw, raw_len - 2) != expected_crc) return -1; |
0 |
5 |
5 |
|
| 552 |
|
| |
| 553 |
5 |
| unsigned char *seed = raw + 2; |
| 554 |
|
| |
| 555 |
5 |
| EVP_PKEY *pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, NULL, seed, 32); |
| 556 |
5 |
▶ | if (!pkey) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pkey) return -1; |
0 |
5 |
5 |
|
| 557 |
|
| |
| 558 |
|
| unsigned char pub[32]; |
| 559 |
5 |
| size_t pub_len = 32; |
| 560 |
5 |
| int ok = EVP_PKEY_get_raw_public_key(pkey, pub, &pub_len) == 1; |
| 561 |
5 |
| EVP_PKEY_free(pkey); |
| 562 |
5 |
▶ | if (!ok) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) return -1; |
0 |
5 |
5 |
|
| 563 |
|
| |
| 564 |
|
| /* Derive public-key prefix from the role embedded in the seed prefix. |
| 565 |
|
| Seed encoding (NATS Go nkeys EncodeSeed): |
| 566 |
|
| raw[0] = PrefixByteSeed | (role >> 5) |
| 567 |
|
| raw[1] = (role & 31) << 3 |
| 568 |
|
| so role = ((raw[0] & 7) << 5) | (raw[1] >> 3). */ |
| 569 |
5 |
| unsigned char role = (unsigned char)(((raw[0] & 0x07) << 5) | (raw[1] >> 3)); |
| 570 |
|
| unsigned char pub_prefix; |
| 571 |
5 |
| switch (role) { |
| 572 |
5 |
| case 0xA0: pub_prefix = 0xA0; break; /* User */ |
| 573 |
0 |
| case 0x70: pub_prefix = 0x70; break; /* Operator */ |
| 574 |
0 |
| case 0x00: pub_prefix = 0x00; break; /* Account */ |
| 575 |
0 |
| default: pub_prefix = 0xA0; break; /* default to User */ |
| 576 |
|
| } |
| 577 |
|
| |
| 578 |
|
| /* Build: prefix(1) + pubkey(32) + CRC16(2) = 35 bytes */ |
| 579 |
|
| unsigned char full[35]; |
| 580 |
5 |
| full[0] = pub_prefix; |
| 581 |
5 |
| memcpy(full + 1, pub, 32); |
| 582 |
5 |
| uint16_t crc = nats_crc16(full, 33); |
| 583 |
5 |
| full[33] = crc & 0xFF; |
| 584 |
5 |
| full[34] = (crc >> 8) & 0xFF; |
| 585 |
|
| |
| 586 |
|
| /* Base32 encode 35 bytes -> 56 chars (35*8 = 280 bits, 280/5 = 56). */ |
| 587 |
5 |
▶ | int n = nats_base32_encode(full, sizeof(full), pub_out, |
Branch: 1 branch
| Branch | True | False | Total |
| int n = nats_base32_encode(full, sizeof(full), pub_out, |
5 |
0 |
5 |
|
| 588 |
|
| pub_out_size > 0 ? pub_out_size - 1 : 0); |
| 589 |
5 |
▶ | if (n < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) return -1; |
0 |
5 |
5 |
|
| 590 |
5 |
▶ | if ((size_t)n < pub_out_size) pub_out[n] = '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((size_t)n < pub_out_size) pub_out[n] = '\0'; |
5 |
0 |
5 |
|
| 591 |
5 |
| return n; |
| 592 |
|
| } |
| 593 |
|
| #endif |
| 594 |
|
| |
| 595 |
|
| /* ================================================================ |
| 596 |
|
| * I/O helpers (with optional TLS) |
| 597 |
|
| * ================================================================ */ |
| 598 |
|
| |
| 599 |
200 |
| static ssize_t nats_io_read(nats_t *self, void *buf, size_t len) |
| 600 |
|
| { |
| 601 |
|
| #ifdef HAVE_OPENSSL |
| 602 |
200 |
▶ | if (self->ssl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->ssl) { |
0 |
200 |
200 |
|
| 603 |
0 |
▶ | int n = SSL_read(self->ssl, buf, (len > (size_t)INT_MAX) ? INT_MAX : (int)len); |
Branch: 1 branch
| Branch | True | False | Total |
| int n = SSL_read(self->ssl, buf, (len > (size_t)INT_MAX) ? INT_MAX : (int)len); |
0 |
0 |
0 |
|
| 604 |
0 |
▶ | if (n <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) { |
0 |
0 |
0 |
|
| 605 |
0 |
| int err = SSL_get_error(self->ssl, n); |
| 606 |
0 |
▶ | if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { |
0 |
0 |
0 |
| if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { |
0 |
0 |
0 |
|
| 607 |
0 |
| errno = EAGAIN; |
| 608 |
0 |
| return -1; |
| 609 |
|
| } |
| 610 |
0 |
▶ | if (err == SSL_ERROR_ZERO_RETURN) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err == SSL_ERROR_ZERO_RETURN) return 0; |
0 |
0 |
0 |
|
| 611 |
0 |
| errno = EIO; |
| 612 |
0 |
| return -1; |
| 613 |
|
| } |
| 614 |
0 |
| return n; |
| 615 |
|
| } |
| 616 |
|
| #endif |
| 617 |
200 |
| return read(self->fd, buf, len); |
| 618 |
|
| } |
| 619 |
|
| |
| 620 |
78 |
| static ssize_t nats_io_write(nats_t *self, const void *buf, size_t len) |
| 621 |
|
| { |
| 622 |
|
| #ifdef HAVE_OPENSSL |
| 623 |
78 |
▶ | if (self->ssl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->ssl) { |
0 |
78 |
78 |
|
| 624 |
|
| /* Retries keep the original length even if producers append more. */ |
| 625 |
0 |
▶ | if (!self->ssl_write_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->ssl_write_len) |
0 |
0 |
0 |
|
| 626 |
0 |
▶ | self->ssl_write_len = len > (size_t)INT_MAX ? INT_MAX : (int)len; |
Branch: 1 branch
| Branch | True | False | Total |
| self->ssl_write_len = len > (size_t)INT_MAX ? INT_MAX : (int)len; |
0 |
0 |
0 |
|
| 627 |
0 |
| int n = SSL_write(self->ssl, buf, self->ssl_write_len); |
| 628 |
0 |
▶ | if (n <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) { |
0 |
0 |
0 |
|
| 629 |
0 |
| int err = SSL_get_error(self->ssl, n); |
| 630 |
0 |
▶ | if (err == SSL_ERROR_WANT_WRITE || err == SSL_ERROR_WANT_READ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (err == SSL_ERROR_WANT_WRITE || err == SSL_ERROR_WANT_READ) { |
0 |
0 |
0 |
| if (err == SSL_ERROR_WANT_WRITE || err == SSL_ERROR_WANT_READ) { |
0 |
0 |
0 |
|
| 631 |
0 |
| errno = EAGAIN; |
| 632 |
0 |
| return -1; |
| 633 |
|
| } |
| 634 |
0 |
| errno = EIO; |
| 635 |
0 |
| return -1; |
| 636 |
|
| } |
| 637 |
0 |
| self->ssl_write_len = 0; |
| 638 |
0 |
| return n; |
| 639 |
|
| } |
| 640 |
|
| #endif |
| 641 |
|
| #ifdef MSG_NOSIGNAL |
| 642 |
78 |
| return send(self->fd, buf, len, MSG_NOSIGNAL); |
| 643 |
|
| #else |
| 644 |
|
| return write(self->fd, buf, len); |
| 645 |
|
| #endif |
| 646 |
|
| } |
| 647 |
|
| |
| 648 |
|
| #ifdef HAVE_OPENSSL |
| 649 |
|
| #ifdef MSG_NOSIGNAL |
| 650 |
|
| /* OpenSSL's socket BIO writes without MSG_NOSIGNAL. Keep signal suppression |
| 651 |
|
| local to this transport, including writes made during TLS reads. */ |
| 652 |
|
| static BIO_METHOD *nats_socket_bio_method; |
| 653 |
|
| |
| 654 |
0 |
| static int nats_bio_read(BIO *bio, char *buf, int len) |
| 655 |
|
| { |
| 656 |
0 |
| nats_t *self = (nats_t *)BIO_get_data(bio); |
| 657 |
0 |
| BIO_clear_retry_flags(bio); |
| 658 |
0 |
| int n = read(self->fd, buf, len); |
| 659 |
0 |
▶ | if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) |
0 |
0 |
0 |
| if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) |
0 |
0 |
0 |
| if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) |
0 |
0 |
0 |
| if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) |
0 |
0 |
0 |
|
| 660 |
0 |
| BIO_set_retry_read(bio); |
| 661 |
0 |
| return n; |
| 662 |
|
| } |
| 663 |
|
| |
| 664 |
0 |
| static int nats_bio_write(BIO *bio, const char *buf, int len) |
| 665 |
|
| { |
| 666 |
0 |
| nats_t *self = (nats_t *)BIO_get_data(bio); |
| 667 |
0 |
| BIO_clear_retry_flags(bio); |
| 668 |
0 |
| int n = send(self->fd, buf, len, MSG_NOSIGNAL); |
| 669 |
0 |
▶ | if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) |
0 |
0 |
0 |
| if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) |
0 |
0 |
0 |
| if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) |
0 |
0 |
0 |
| if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) |
0 |
0 |
0 |
|
| 670 |
0 |
| BIO_set_retry_write(bio); |
| 671 |
0 |
| return n; |
| 672 |
|
| } |
| 673 |
|
| |
| 674 |
0 |
| static long nats_bio_ctrl(BIO *bio, int cmd, long num, void *ptr) |
| 675 |
|
| { |
| 676 |
0 |
| nats_t *self = (nats_t *)BIO_get_data(bio); |
| 677 |
|
| (void)num; |
| 678 |
0 |
▶ | if (cmd == BIO_CTRL_FLUSH) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cmd == BIO_CTRL_FLUSH) return 1; |
0 |
0 |
0 |
|
| 679 |
0 |
▶ | if (cmd == BIO_C_GET_FD) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cmd == BIO_C_GET_FD) { |
0 |
0 |
0 |
|
| 680 |
0 |
▶ | if (ptr) *(int *)ptr = self->fd; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptr) *(int *)ptr = self->fd; |
0 |
0 |
0 |
|
| 681 |
0 |
| return self->fd; |
| 682 |
|
| } |
| 683 |
0 |
| return 0; |
| 684 |
|
| } |
| 685 |
|
| |
| 686 |
0 |
| static BIO *nats_socket_bio(nats_t *self) |
| 687 |
|
| { |
| 688 |
0 |
▶ | if (!nats_socket_bio_method) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nats_socket_bio_method) { |
0 |
0 |
0 |
|
| 689 |
0 |
| BIO_METHOD *method = BIO_meth_new(BIO_get_new_index() | |
| 690 |
|
| BIO_TYPE_SOURCE_SINK | BIO_TYPE_DESCRIPTOR, "EV::Nats socket"); |
| 691 |
0 |
▶ | if (!method) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!method) return NULL; |
0 |
0 |
0 |
|
| 692 |
0 |
| BIO_meth_set_read(method, nats_bio_read); |
| 693 |
0 |
| BIO_meth_set_write(method, nats_bio_write); |
| 694 |
0 |
| BIO_meth_set_ctrl(method, nats_bio_ctrl); |
| 695 |
0 |
| nats_socket_bio_method = method; |
| 696 |
|
| } |
| 697 |
0 |
| BIO *bio = BIO_new(nats_socket_bio_method); |
| 698 |
0 |
▶ | if (bio) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bio) { |
0 |
0 |
0 |
|
| 699 |
0 |
| BIO_set_data(bio, self); |
| 700 |
0 |
| BIO_set_init(bio, 1); |
| 701 |
|
| } |
| 702 |
0 |
| return bio; |
| 703 |
|
| } |
| 704 |
|
| #endif |
| 705 |
|
| |
| 706 |
0 |
| static int nats_ssl_setup(nats_t *self) |
| 707 |
|
| { |
| 708 |
0 |
▶ | if (!self->ssl_ctx) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->ssl_ctx) { |
0 |
0 |
0 |
|
| 709 |
0 |
| self->ssl_ctx = SSL_CTX_new(TLS_client_method()); |
| 710 |
0 |
▶ | if (!self->ssl_ctx) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->ssl_ctx) return -1; |
0 |
0 |
0 |
|
| 711 |
|
| |
| 712 |
0 |
▶ | if (self->tls_ca_file && *self->tls_ca_file) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->tls_ca_file && *self->tls_ca_file) { |
0 |
0 |
0 |
| if (self->tls_ca_file && *self->tls_ca_file) { |
0 |
0 |
0 |
|
| 713 |
0 |
▶ | if (!SSL_CTX_load_verify_locations(self->ssl_ctx, self->tls_ca_file, NULL)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SSL_CTX_load_verify_locations(self->ssl_ctx, self->tls_ca_file, NULL)) { |
0 |
0 |
0 |
|
| 714 |
0 |
| SSL_CTX_free(self->ssl_ctx); |
| 715 |
0 |
| self->ssl_ctx = NULL; |
| 716 |
0 |
| return -1; |
| 717 |
|
| } |
| 718 |
|
| } else { |
| 719 |
0 |
| SSL_CTX_set_default_verify_paths(self->ssl_ctx); |
| 720 |
|
| } |
| 721 |
|
| |
| 722 |
0 |
▶ | if (!self->tls_skip_verify) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->tls_skip_verify) |
0 |
0 |
0 |
|
| 723 |
0 |
| SSL_CTX_set_verify(self->ssl_ctx, SSL_VERIFY_PEER, NULL); |
| 724 |
|
| } |
| 725 |
|
| |
| 726 |
0 |
| self->ssl = SSL_new(self->ssl_ctx); |
| 727 |
0 |
▶ | if (!self->ssl) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->ssl) return -1; |
0 |
0 |
0 |
|
| 728 |
0 |
| SSL_set_mode(self->ssl, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); |
| 729 |
|
| |
| 730 |
|
| #ifdef MSG_NOSIGNAL |
| 731 |
0 |
| BIO *bio = nats_socket_bio(self); |
| 732 |
0 |
▶ | if (!bio) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bio) { |
0 |
0 |
0 |
|
| 733 |
0 |
| SSL_free(self->ssl); |
| 734 |
0 |
| self->ssl = NULL; |
| 735 |
0 |
| return -1; |
| 736 |
|
| } |
| 737 |
0 |
| SSL_set_bio(self->ssl, bio, bio); |
| 738 |
|
| #else |
| 739 |
|
| SSL_set_fd(self->ssl, self->fd); |
| 740 |
|
| #endif |
| 741 |
0 |
▶ | if (self->host) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->host) { |
0 |
0 |
0 |
|
| 742 |
0 |
| SSL_set_tlsext_host_name(self->ssl, self->host); |
| 743 |
0 |
▶ | if (!self->tls_skip_verify) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->tls_skip_verify) { |
0 |
0 |
0 |
|
| 744 |
0 |
| X509_VERIFY_PARAM *vpm = SSL_get0_param(self->ssl); |
| 745 |
|
| /* set1_ip_asc parses host as a textual IP and returns 0 on |
| 746 |
|
| failure — fall back to set1_host for DNS names. */ |
| 747 |
0 |
▶ | if (vpm && !X509_VERIFY_PARAM_set1_ip_asc(vpm, self->host)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (vpm && !X509_VERIFY_PARAM_set1_ip_asc(vpm, self->host)) { |
0 |
0 |
0 |
| if (vpm && !X509_VERIFY_PARAM_set1_ip_asc(vpm, self->host)) { |
0 |
0 |
0 |
|
| 748 |
0 |
| X509_VERIFY_PARAM_set_hostflags(vpm, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); |
| 749 |
0 |
▶ | if (!X509_VERIFY_PARAM_set1_host(vpm, self->host, 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!X509_VERIFY_PARAM_set1_host(vpm, self->host, 0)) { |
0 |
0 |
0 |
|
| 750 |
0 |
| SSL_free(self->ssl); |
| 751 |
0 |
| self->ssl = NULL; |
| 752 |
0 |
| return -1; |
| 753 |
|
| } |
| 754 |
|
| } |
| 755 |
|
| } |
| 756 |
|
| } |
| 757 |
|
| |
| 758 |
0 |
| return 0; |
| 759 |
|
| } |
| 760 |
|
| |
| 761 |
152 |
| static void nats_ssl_cleanup(nats_t *self) |
| 762 |
|
| { |
| 763 |
152 |
▶ | if (self->ssl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->ssl) { |
0 |
152 |
152 |
|
| 764 |
0 |
| SSL_free(self->ssl); |
| 765 |
0 |
| self->ssl = NULL; |
| 766 |
|
| } |
| 767 |
152 |
| self->ssl_handshaking = 0; |
| 768 |
152 |
| self->ssl_write_len = 0; |
| 769 |
152 |
| } |
| 770 |
|
| |
| 771 |
0 |
| static int nats_ssl_handshake(nats_t *self) |
| 772 |
|
| { |
| 773 |
0 |
| int ret = SSL_connect(self->ssl); |
| 774 |
0 |
▶ | if (ret == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 1) { |
0 |
0 |
0 |
|
| 775 |
0 |
| self->ssl_handshaking = 0; |
| 776 |
0 |
| return 1; |
| 777 |
|
| } |
| 778 |
0 |
| int err = SSL_get_error(self->ssl, ret); |
| 779 |
0 |
▶ | if (err == SSL_ERROR_WANT_READ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (err == SSL_ERROR_WANT_READ) { |
0 |
0 |
0 |
|
| 780 |
0 |
▶ | if (!self->reading) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->reading) { |
0 |
0 |
0 |
|
| 781 |
0 |
| ev_io_start(self->loop, &self->rio); |
| 782 |
0 |
| self->reading = 1; |
| 783 |
|
| } |
| 784 |
|
| /* Stop the write watcher: leaving it armed spins the loop |
| 785 |
|
| calling SSL_connect until the server's next flight arrives. */ |
| 786 |
0 |
▶ | if (self->writing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->writing) { |
0 |
0 |
0 |
|
| 787 |
0 |
| ev_io_stop(self->loop, &self->wio); |
| 788 |
0 |
| self->writing = 0; |
| 789 |
|
| } |
| 790 |
0 |
| return 0; |
| 791 |
|
| } |
| 792 |
0 |
▶ | if (err == SSL_ERROR_WANT_WRITE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (err == SSL_ERROR_WANT_WRITE) { |
0 |
0 |
0 |
|
| 793 |
0 |
▶ | if (!self->writing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->writing) { |
0 |
0 |
0 |
|
| 794 |
0 |
| ev_io_start(self->loop, &self->wio); |
| 795 |
0 |
| self->writing = 1; |
| 796 |
|
| } |
| 797 |
0 |
| return 0; |
| 798 |
|
| } |
| 799 |
0 |
| return -1; |
| 800 |
|
| } |
| 801 |
|
| |
| 802 |
|
| /* Emit "<prefix>: <openssl-error>", clean up the connection. */ |
| 803 |
0 |
| static void nats_ssl_fail(nats_t *self, const char *prefix) |
| 804 |
|
| { |
| 805 |
|
| char errbuf[256]; |
| 806 |
|
| char msg[320]; |
| 807 |
0 |
| unsigned long e = ERR_peek_last_error(); |
| 808 |
0 |
▶ | if (e) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e) { |
0 |
0 |
0 |
|
| 809 |
0 |
| ERR_error_string_n(e, errbuf, sizeof(errbuf)); |
| 810 |
0 |
| ERR_clear_error(); |
| 811 |
|
| } else { |
| 812 |
0 |
| snprintf(errbuf, sizeof(errbuf), "unknown SSL error"); |
| 813 |
|
| } |
| 814 |
0 |
| snprintf(msg, sizeof(msg), "%s: %s", prefix, errbuf); |
| 815 |
0 |
| nats_emit_error(self, msg); |
| 816 |
0 |
| nats_cleanup(self); |
| 817 |
0 |
| } |
| 818 |
|
| #endif |
| 819 |
|
| |
| 820 |
|
| /* ================================================================ |
| 821 |
|
| * JSON string escaper (for CONNECT command) |
| 822 |
|
| * ================================================================ */ |
| 823 |
|
| |
| 824 |
1 |
| static int json_escape_string(char *dst, size_t dst_size, const char *src) |
| 825 |
|
| { |
| 826 |
1 |
| size_t di = 0; |
| 827 |
1 |
| const unsigned char *s = (const unsigned char *)src; |
| 828 |
|
| |
| 829 |
|
| /* Need room for opening quote, at least one char, closing quote, NUL */ |
| 830 |
1 |
▶ | if (dst_size < 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dst_size < 3) { |
0 |
1 |
1 |
|
| 831 |
0 |
▶ | if (dst_size > 0) dst[0] = '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dst_size > 0) dst[0] = '\0'; |
0 |
0 |
0 |
|
| 832 |
0 |
| return 0; |
| 833 |
|
| } |
| 834 |
|
| |
| 835 |
1 |
| dst[di++] = '"'; |
| 836 |
|
| |
| 837 |
8190 |
▶ | for (; *s; s++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; *s; s++) { |
8190 |
0 |
8190 |
|
| 838 |
8190 |
▶ | size_t need = (*s == '"' || *s == '\\') ? 2 : (*s < 0x20) ? 6 : 1; |
Branch: 3 branches
| Branch | True | False | Total |
| size_t need = (*s == '"' || *s == '\\') ? 2 : (*s < 0x20) ? 6 : 1; |
8190 |
0 |
8190 |
| size_t need = (*s == '"' || *s == '\\') ? 2 : (*s < 0x20) ? 6 : 1; |
8190 |
0 |
8190 |
| size_t need = (*s == '"' || *s == '\\') ? 2 : (*s < 0x20) ? 6 : 1; |
0 |
8190 |
8190 |
|
| 839 |
8190 |
▶ | if (di + need + 2 > dst_size) break; /* +2 for closing quote + NUL */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (di + need + 2 > dst_size) break; /* +2 for closing quote + NUL */ |
1 |
8189 |
8190 |
|
| 840 |
|
| |
| 841 |
8189 |
▶ | if (*s == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == '"') { |
0 |
8189 |
8189 |
|
| 842 |
0 |
| dst[di++] = '\\'; dst[di++] = '"'; |
| 843 |
8189 |
▶ | } else if (*s == '\\') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*s == '\\') { |
0 |
8189 |
8189 |
|
| 844 |
0 |
| dst[di++] = '\\'; dst[di++] = '\\'; |
| 845 |
8189 |
▶ | } else if (*s < 0x20) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*s < 0x20) { |
0 |
8189 |
8189 |
|
| 846 |
0 |
| di += snprintf(dst + di, dst_size - di, "\\u%04x", *s); |
| 847 |
|
| } else { |
| 848 |
8189 |
| dst[di++] = *s; |
| 849 |
|
| } |
| 850 |
|
| } |
| 851 |
|
| |
| 852 |
1 |
▶ | if (*s) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s) return -1; |
1 |
0 |
1 |
|
| 853 |
0 |
| dst[di++] = '"'; |
| 854 |
0 |
| dst[di] = '\0'; |
| 855 |
0 |
| return (int)di; |
| 856 |
|
| } |
| 857 |
|
| |
| 858 |
|
| /* ================================================================ |
| 859 |
|
| * Random hex for inbox prefix |
| 860 |
|
| * ================================================================ */ |
| 861 |
|
| |
| 862 |
|
| /* Read nbytes from /dev/urandom; fall back to rand() per byte. */ |
| 863 |
129 |
| static void nats_random_bytes(unsigned char *out, int nbytes) |
| 864 |
|
| { |
| 865 |
129 |
| int got = 0; |
| 866 |
129 |
| int fd = open("/dev/urandom", O_RDONLY); |
| 867 |
129 |
▶ | if (fd >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd >= 0) { |
129 |
0 |
129 |
|
| 868 |
258 |
▶ | while (got < nbytes) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (got < nbytes) { |
129 |
129 |
258 |
|
| 869 |
129 |
| ssize_t n = read(fd, out + got, nbytes - got); |
| 870 |
129 |
▶ | if (n > 0) got += n; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) got += n; |
129 |
0 |
129 |
|
| 871 |
0 |
▶ | else if (n == 0 || (errno != EINTR && errno != EAGAIN)) break; |
Branch: 3 branches
| Branch | True | False | Total |
| else if (n == 0 || (errno != EINTR && errno != EAGAIN)) break; |
0 |
0 |
0 |
| else if (n == 0 || (errno != EINTR && errno != EAGAIN)) break; |
0 |
0 |
0 |
| else if (n == 0 || (errno != EINTR && errno != EAGAIN)) break; |
0 |
0 |
0 |
|
| 872 |
|
| } |
| 873 |
129 |
| close(fd); |
| 874 |
|
| } |
| 875 |
|
| int i; |
| 876 |
129 |
▶ | for (i = got; i < nbytes; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = got; i < nbytes; i++) |
0 |
129 |
129 |
|
| 877 |
0 |
| out[i] = (unsigned char)(rand() & 0xFF); |
| 878 |
129 |
| } |
| 879 |
|
| |
| 880 |
78 |
| static void nats_gen_hex(char *buf, int nbytes) |
| 881 |
|
| { |
| 882 |
|
| static const char hex[] = "0123456789abcdef"; |
| 883 |
|
| unsigned char rnd[16]; |
| 884 |
78 |
▶ | if (nbytes < 0) nbytes = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nbytes < 0) nbytes = 0; |
0 |
78 |
78 |
|
| 885 |
78 |
▶ | if (nbytes > (int)sizeof(rnd)) nbytes = (int)sizeof(rnd); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nbytes > (int)sizeof(rnd)) nbytes = (int)sizeof(rnd); |
0 |
78 |
78 |
|
| 886 |
78 |
| nats_random_bytes(rnd, nbytes); |
| 887 |
|
| int i; |
| 888 |
702 |
▶ | for (i = 0; i < nbytes; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nbytes; i++) { |
624 |
78 |
702 |
|
| 889 |
624 |
| buf[i*2] = hex[rnd[i] >> 4]; |
| 890 |
624 |
| buf[i*2+1] = hex[rnd[i] & 0x0F]; |
| 891 |
|
| } |
| 892 |
78 |
| } |
| 893 |
|
| |
| 894 |
78 |
| static void nats_setup_inbox(nats_t *self) |
| 895 |
|
| { |
| 896 |
|
| char hex[20]; |
| 897 |
78 |
| nats_gen_hex(hex, 8); |
| 898 |
78 |
| snprintf(self->inbox_prefix, sizeof(self->inbox_prefix), |
| 899 |
|
| "_INBOX.%.16s.", hex); |
| 900 |
78 |
| self->next_req_id = 1; |
| 901 |
78 |
| } |
| 902 |
|
| |
| 903 |
|
| /* ================================================================ |
| 904 |
|
| * Subscription management |
| 905 |
|
| * ================================================================ */ |
| 906 |
|
| |
| 907 |
52 |
| static nats_sub_t *nats_find_sub(nats_t *self, uint64_t sid) |
| 908 |
|
| { |
| 909 |
|
| char key[24]; |
| 910 |
52 |
| int klen = snprintf(key, sizeof(key), "%" UVuf, (UV)sid); |
| 911 |
52 |
| SV **svp = hv_fetch(self->sub_map, key, klen, 0); |
| 912 |
52 |
▶ | if (svp && SvIOK(*svp)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (svp && SvIOK(*svp)) |
52 |
0 |
52 |
| if (svp && SvIOK(*svp)) |
52 |
0 |
52 |
|
| 913 |
52 |
| return INT2PTR(nats_sub_t *, SvIVX(*svp)); |
| 914 |
0 |
| return NULL; |
| 915 |
|
| } |
| 916 |
|
| |
| 917 |
22 |
| static void nats_register_sub(nats_t *self, nats_sub_t *sub) |
| 918 |
|
| { |
| 919 |
|
| char key[24]; |
| 920 |
22 |
| int klen = snprintf(key, sizeof(key), "%" UVuf, (UV)sub->sid); |
| 921 |
22 |
| hv_store(self->sub_map, key, klen, newSViv(PTR2IV(sub)), 0); |
| 922 |
22 |
| ngx_queue_insert_tail(&self->subs, &sub->queue); |
| 923 |
22 |
| } |
| 924 |
|
| |
| 925 |
15 |
| static void nats_unregister_sub(nats_t *self, nats_sub_t *sub) |
| 926 |
|
| { |
| 927 |
|
| char key[24]; |
| 928 |
15 |
| int klen = snprintf(key, sizeof(key), "%" UVuf, (UV)sub->sid); |
| 929 |
15 |
| hv_delete(self->sub_map, key, klen, G_DISCARD); |
| 930 |
15 |
| } |
| 931 |
|
| |
| 932 |
15 |
| static void nats_remove_sub(nats_t *self, nats_sub_t *sub) |
| 933 |
|
| { |
| 934 |
15 |
| nats_unregister_sub(self, sub); |
| 935 |
15 |
| ngx_queue_remove(&sub->queue); |
| 936 |
15 |
▶ | CLEAR_HANDLER(sub->subject); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(sub->subject); |
15 |
0 |
15 |
|
| 937 |
15 |
▶ | CLEAR_HANDLER(sub->queue_group); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(sub->queue_group); |
0 |
15 |
15 |
|
| 938 |
15 |
▶ | CLEAR_HANDLER(sub->cb); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(sub->cb); |
14 |
1 |
15 |
|
| 939 |
15 |
| Safefree(sub); |
| 940 |
15 |
| } |
| 941 |
|
| |
| 942 |
36 |
| static void nats_resub_all(nats_t *self) |
| 943 |
|
| { |
| 944 |
|
| ngx_queue_t *q; |
| 945 |
|
| char buf[MAX_CONTROL_LINE]; |
| 946 |
|
| |
| 947 |
36 |
▶ | ngx_queue_foreach(q, &self->subs) { |
Branch: 2 branches
| Branch | True | False | Total |
| ngx_queue_foreach(q, &self->subs) { |
0 |
36 |
36 |
| ngx_queue_foreach(q, &self->subs) { |
0 |
0 |
0 |
|
| 948 |
0 |
| nats_sub_t *sub = ngx_queue_data(q, nats_sub_t, queue); |
| 949 |
|
| STRLEN slen; |
| 950 |
0 |
| const char *subj = SvPV(sub->subject, slen); |
| 951 |
|
| int n; |
| 952 |
|
| |
| 953 |
0 |
▶ | if (sub->queue_group) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sub->queue_group) { |
0 |
0 |
0 |
|
| 954 |
|
| STRLEN glen; |
| 955 |
0 |
| const char *grp = SvPV(sub->queue_group, glen); |
| 956 |
0 |
| n = snprintf(buf, sizeof(buf), "SUB %.*s %.*s %" UVuf "\r\n", |
| 957 |
0 |
| (int)slen, subj, (int)glen, grp, (UV)sub->sid); |
| 958 |
|
| } else { |
| 959 |
0 |
| n = snprintf(buf, sizeof(buf), "SUB %.*s %" UVuf "\r\n", |
| 960 |
0 |
| (int)slen, subj, (UV)sub->sid); |
| 961 |
|
| } |
| 962 |
0 |
▶ | NATS_CLAMP_LEN(n, sizeof(buf)); |
Branch: 2 branches
| Branch | True | False | Total |
| NATS_CLAMP_LEN(n, sizeof(buf)); |
0 |
0 |
0 |
| NATS_CLAMP_LEN(n, sizeof(buf)); |
0 |
0 |
0 |
|
| 963 |
0 |
| wbuf_append(self, buf, n); |
| 964 |
|
| |
| 965 |
|
| /* Restore auto-unsub if partially consumed */ |
| 966 |
0 |
▶ | if (sub->max_msgs > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sub->max_msgs > 0) { |
0 |
0 |
0 |
|
| 967 |
0 |
| int remaining = sub->max_msgs - sub->received; |
| 968 |
0 |
▶ | if (remaining < 1) remaining = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (remaining < 1) remaining = 1; |
0 |
0 |
0 |
|
| 969 |
0 |
| n = snprintf(buf, sizeof(buf), "UNSUB %" UVuf " %d\r\n", |
| 970 |
0 |
| (UV)sub->sid, remaining); |
| 971 |
0 |
| wbuf_append(self, buf, n); |
| 972 |
|
| } |
| 973 |
|
| } |
| 974 |
36 |
| } |
| 975 |
|
| |
| 976 |
|
| /* ================================================================ |
| 977 |
|
| * Connection management |
| 978 |
|
| * ================================================================ */ |
| 979 |
|
| |
| 980 |
152 |
| static void nats_stop_watchers(nats_t *self) |
| 981 |
|
| { |
| 982 |
152 |
▶ | if (self->reading) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->reading) { |
83 |
69 |
152 |
|
| 983 |
83 |
| ev_io_stop(self->loop, &self->rio); |
| 984 |
83 |
| self->reading = 0; |
| 985 |
|
| } |
| 986 |
152 |
▶ | if (self->writing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->writing) { |
45 |
107 |
152 |
|
| 987 |
45 |
| ev_io_stop(self->loop, &self->wio); |
| 988 |
45 |
| self->writing = 0; |
| 989 |
|
| } |
| 990 |
152 |
| } |
| 991 |
|
| |
| 992 |
152 |
| static void nats_stop_timers(nats_t *self) |
| 993 |
|
| { |
| 994 |
152 |
▶ | if (self->connect_timer_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connect_timer_active) { |
0 |
152 |
152 |
|
| 995 |
0 |
| ev_timer_stop(self->loop, &self->connect_timer); |
| 996 |
0 |
| self->connect_timer_active = 0; |
| 997 |
|
| } |
| 998 |
152 |
▶ | if (self->reconnect_timer_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->reconnect_timer_active) { |
2 |
150 |
152 |
|
| 999 |
2 |
| ev_timer_stop(self->loop, &self->reconnect_timer); |
| 1000 |
2 |
| self->reconnect_timer_active = 0; |
| 1001 |
|
| } |
| 1002 |
152 |
▶ | if (self->ping_timer_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->ping_timer_active) { |
34 |
118 |
152 |
|
| 1003 |
34 |
| ev_timer_stop(self->loop, &self->ping_timer); |
| 1004 |
34 |
| self->ping_timer_active = 0; |
| 1005 |
|
| } |
| 1006 |
152 |
▶ | if (self->prepare_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->prepare_active) { |
5 |
147 |
152 |
|
| 1007 |
5 |
| ev_prepare_stop(self->loop, &self->prepare_watcher); |
| 1008 |
5 |
| self->prepare_active = 0; |
| 1009 |
|
| } |
| 1010 |
152 |
| } |
| 1011 |
|
| |
| 1012 |
92 |
| static void nats_cancel_all_requests(nats_t *self, const char *err) |
| 1013 |
|
| { |
| 1014 |
100 |
▶ | while (!ngx_queue_empty(&self->req_queue)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!ngx_queue_empty(&self->req_queue)) { |
8 |
92 |
100 |
|
| 1015 |
8 |
| ngx_queue_t *q = ngx_queue_head(&self->req_queue); |
| 1016 |
8 |
| nats_req_t *req = ngx_queue_data(q, nats_req_t, queue); |
| 1017 |
8 |
| ngx_queue_remove(q); |
| 1018 |
|
| |
| 1019 |
8 |
▶ | if (req->timer_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (req->timer_active) { |
8 |
0 |
8 |
|
| 1020 |
8 |
| ev_timer_stop(self->loop, &req->timer); |
| 1021 |
8 |
| req->timer_active = 0; |
| 1022 |
|
| } |
| 1023 |
|
| |
| 1024 |
8 |
▶ | if (req->cb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (req->cb) { |
8 |
0 |
8 |
|
| 1025 |
8 |
| dSP; |
| 1026 |
8 |
| ENTER; SAVETMPS; |
| 1027 |
8 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
8 |
8 |
|
| 1028 |
8 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
8 |
8 |
|
| 1029 |
8 |
| PUSHs(&PL_sv_undef); |
| 1030 |
8 |
| PUSHs(sv_2mortal(newSVpv(err, 0))); |
| 1031 |
8 |
| PUTBACK; |
| 1032 |
8 |
| call_sv(req->cb, G_DISCARD); |
| 1033 |
8 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
8 |
0 |
8 |
|
| 1034 |
8 |
| SvREFCNT_dec(req->cb); |
| 1035 |
|
| } |
| 1036 |
8 |
| Safefree(req); |
| 1037 |
|
| } |
| 1038 |
92 |
| } |
| 1039 |
|
| |
| 1040 |
92 |
| static void nats_skip_waiting(nats_t *self) |
| 1041 |
|
| { |
| 1042 |
120 |
▶ | while (!ngx_queue_empty(&self->wait_queue)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!ngx_queue_empty(&self->wait_queue)) { |
28 |
92 |
120 |
|
| 1043 |
28 |
| ngx_queue_t *q = ngx_queue_head(&self->wait_queue); |
| 1044 |
28 |
| nats_pub_t *pub = ngx_queue_data(q, nats_pub_t, queue); |
| 1045 |
28 |
| ngx_queue_remove(q); |
| 1046 |
28 |
▶ | if (pub->data) |
Branch: 1 branch
| Branch | True | False | Total |
| if (pub->data) |
28 |
0 |
28 |
|
| 1047 |
28 |
| Safefree(pub->data); |
| 1048 |
28 |
| Safefree(pub); |
| 1049 |
28 |
| self->waiting_count--; |
| 1050 |
|
| } |
| 1051 |
92 |
| self->wait_bytes = 0; |
| 1052 |
92 |
| } |
| 1053 |
|
| |
| 1054 |
92 |
| static void nats_cleanup(nats_t *self) |
| 1055 |
|
| { |
| 1056 |
92 |
| SV *pin = self->owner_sv; |
| 1057 |
92 |
▶ | if (pin) SvREFCNT_inc(pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pin) SvREFCNT_inc(pin); |
92 |
0 |
92 |
|
| 1058 |
92 |
▶ | int was_connected = self->connected || self->connecting; |
Branch: 2 branches
| Branch | True | False | Total |
| int was_connected = self->connected || self->connecting; |
61 |
31 |
92 |
| int was_connected = self->connected || self->connecting; |
47 |
14 |
61 |
|
| 1059 |
|
| |
| 1060 |
92 |
| nats_stop_watchers(self); |
| 1061 |
92 |
| nats_stop_timers(self); |
| 1062 |
|
| |
| 1063 |
92 |
| self->connected = 0; |
| 1064 |
92 |
| self->connecting = 0; |
| 1065 |
92 |
| self->connect_pending = 0; |
| 1066 |
92 |
| self->pings_outstanding = 0; |
| 1067 |
92 |
| self->parse_state = PARSE_OP; |
| 1068 |
92 |
| self->ldm = 0; /* lame-duck flag is per-connection */ |
| 1069 |
92 |
| nats_set_str(&self->server_nonce, NULL); /* stale nonce must not sign */ |
| 1070 |
|
| |
| 1071 |
92 |
| nats_free_addrinfo(self); |
| 1072 |
|
| |
| 1073 |
|
| #ifdef HAVE_OPENSSL |
| 1074 |
92 |
| nats_ssl_cleanup(self); |
| 1075 |
|
| #endif |
| 1076 |
|
| |
| 1077 |
92 |
▶ | if (self->fd >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->fd >= 0) { |
78 |
14 |
92 |
|
| 1078 |
78 |
| close(self->fd); |
| 1079 |
78 |
| self->fd = -1; |
| 1080 |
|
| } |
| 1081 |
|
| |
| 1082 |
92 |
| self->rbuf_len = 0; |
| 1083 |
92 |
| self->wbuf_len = 0; |
| 1084 |
92 |
| self->wbuf_off = 0; |
| 1085 |
|
| |
| 1086 |
92 |
| nats_cancel_all_requests(self, "disconnected"); |
| 1087 |
|
| |
| 1088 |
|
| /* Drain pending PONG callbacks: fire kind-0 flush cbs with an error so |
| 1089 |
|
| callers don't hang; drain markers and placeholders are freed silently. */ |
| 1090 |
109 |
▶ | while (!ngx_queue_empty(&self->pong_cbs)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!ngx_queue_empty(&self->pong_cbs)) { |
17 |
92 |
109 |
|
| 1091 |
17 |
| ngx_queue_t *pq = ngx_queue_head(&self->pong_cbs); |
| 1092 |
17 |
| nats_pong_cb_t *pcb = ngx_queue_data(pq, nats_pong_cb_t, queue); |
| 1093 |
17 |
| ngx_queue_remove(pq); |
| 1094 |
17 |
▶ | if (pcb->kind == 0 && pcb->cb) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pcb->kind == 0 && pcb->cb) { |
1 |
16 |
17 |
| if (pcb->kind == 0 && pcb->cb) { |
1 |
0 |
1 |
|
| 1095 |
1 |
| dSP; |
| 1096 |
1 |
| ENTER; SAVETMPS; |
| 1097 |
1 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
1 |
1 |
|
| 1098 |
1 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
1 |
1 |
|
| 1099 |
1 |
| PUSHs(sv_2mortal(newSVpvn("disconnected", 12))); |
| 1100 |
1 |
| PUTBACK; |
| 1101 |
1 |
| call_sv(pcb->cb, G_DISCARD); |
| 1102 |
1 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 1103 |
1 |
| SvREFCNT_dec(pcb->cb); |
| 1104 |
|
| } |
| 1105 |
17 |
| Safefree(pcb); |
| 1106 |
|
| } |
| 1107 |
|
| /* Drain marker (drain_cb) is a separate field, fired on PONG normally; |
| 1108 |
|
| on cleanup it would otherwise leak. Fire it with error too. |
| 1109 |
|
| Take the callback before firing: a drain_cb that disconnects must |
| 1110 |
|
| not see a still-armed drain and recurse into cleanup forever. */ |
| 1111 |
92 |
| self->draining = 0; |
| 1112 |
92 |
▶ | if (self->drain_cb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->drain_cb) { |
2 |
90 |
92 |
|
| 1113 |
2 |
| SV *dcb = self->drain_cb; |
| 1114 |
2 |
| self->drain_cb = NULL; |
| 1115 |
2 |
| dSP; |
| 1116 |
2 |
| ENTER; SAVETMPS; |
| 1117 |
2 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
2 |
2 |
|
| 1118 |
2 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
2 |
2 |
|
| 1119 |
2 |
| PUSHs(sv_2mortal(newSVpvn("disconnected", 12))); |
| 1120 |
2 |
| PUTBACK; |
| 1121 |
2 |
| PINNED_CALL_SV(dcb, G_DISCARD); |
| 1122 |
2 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
2 |
0 |
2 |
|
| 1123 |
2 |
| SvREFCNT_dec(dcb); |
| 1124 |
|
| } |
| 1125 |
|
| |
| 1126 |
92 |
▶ | if (was_connected && self->on_disconnect) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (was_connected && self->on_disconnect) { |
78 |
14 |
92 |
| if (was_connected && self->on_disconnect) { |
4 |
74 |
78 |
|
| 1127 |
4 |
| dSP; |
| 1128 |
4 |
| ENTER; SAVETMPS; |
| 1129 |
4 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
4 |
4 |
|
| 1130 |
4 |
| PUTBACK; |
| 1131 |
4 |
| PINNED_CALL_SV(self->on_disconnect, G_DISCARD); |
| 1132 |
4 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
4 |
4 |
|
| 1133 |
|
| } |
| 1134 |
|
| |
| 1135 |
92 |
| nats_schedule_reconnect(self); |
| 1136 |
92 |
▶ | if (pin) SvREFCNT_dec(pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pin) SvREFCNT_dec(pin); |
92 |
0 |
92 |
|
| 1137 |
92 |
| } |
| 1138 |
|
| |
| 1139 |
58 |
| static void nats_emit_error(nats_t *self, const char *err) |
| 1140 |
|
| { |
| 1141 |
58 |
▶ | if (self->on_error) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->on_error) { |
57 |
1 |
58 |
|
| 1142 |
57 |
| SV *pin = self->owner_sv; |
| 1143 |
57 |
▶ | if (pin) SvREFCNT_inc(pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pin) SvREFCNT_inc(pin); |
57 |
0 |
57 |
|
| 1144 |
57 |
| dSP; |
| 1145 |
57 |
| ENTER; SAVETMPS; |
| 1146 |
57 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
57 |
57 |
|
| 1147 |
57 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
57 |
57 |
|
| 1148 |
57 |
| PUSHs(sv_2mortal(newSVpv(err, 0))); |
| 1149 |
57 |
| PUTBACK; |
| 1150 |
57 |
| PINNED_CALL_SV(self->on_error, G_DISCARD); |
| 1151 |
57 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
57 |
0 |
57 |
|
| 1152 |
57 |
▶ | if (pin) SvREFCNT_dec(pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pin) SvREFCNT_dec(pin); |
57 |
0 |
57 |
|
| 1153 |
1 |
▶ | } else if (self->cb_depth > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->cb_depth > 0) { |
0 |
1 |
1 |
|
| 1154 |
|
| /* Async context with no handler: stashing the first error and |
| 1155 |
|
| croaking it at the outermost wrapper exit lets the caller's |
| 1156 |
|
| cleanup and reconnect logic run instead of longjmping past it. */ |
| 1157 |
0 |
▶ | if (!self->pending_croak) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->pending_croak) { |
0 |
0 |
0 |
|
| 1158 |
0 |
| size_t l = strlen(err); |
| 1159 |
0 |
| Newx(self->pending_croak, l + 1, char); |
| 1160 |
0 |
| memcpy(self->pending_croak, err, l + 1); |
| 1161 |
|
| } |
| 1162 |
|
| } else { |
| 1163 |
1 |
| croak("EV::Nats: %s", err); |
| 1164 |
|
| } |
| 1165 |
57 |
| } |
| 1166 |
|
| |
| 1167 |
|
| /* Fire a deferred no-on_error croak after bookkeeping has run. |
| 1168 |
|
| Take-before-fire: the croak longjmps. */ |
| 1169 |
|
| static void |
| 1170 |
0 |
| nats_fire_pending_croak(nats_t *self) |
| 1171 |
|
| { |
| 1172 |
0 |
| char *msg = self->pending_croak; |
| 1173 |
0 |
| self->pending_croak = NULL; |
| 1174 |
0 |
| SV *sv = sv_2mortal(newSVpvf("EV::Nats: %s", msg)); |
| 1175 |
0 |
| Safefree(msg); |
| 1176 |
0 |
| croak_sv(sv); |
| 1177 |
|
| } |
| 1178 |
|
| |
| 1179 |
|
| /* Kind-2 placeholder for an internal PING, keeping the pong_cbs FIFO 1:1 with PINGs sent */ |
| 1180 |
49 |
| static void nats_push_pong_marker(nats_t *self) |
| 1181 |
|
| { |
| 1182 |
|
| nats_pong_cb_t *pcb; |
| 1183 |
49 |
| Newxz(pcb, 1, nats_pong_cb_t); |
| 1184 |
49 |
| pcb->kind = 2; |
| 1185 |
49 |
| ngx_queue_insert_tail(&self->pong_cbs, &pcb->queue); |
| 1186 |
49 |
| } |
| 1187 |
|
| |
| 1188 |
|
| /* ================================================================ |
| 1189 |
|
| * CONNECT command builder |
| 1190 |
|
| * ================================================================ */ |
| 1191 |
|
| |
| 1192 |
|
| #define CONNECT_BUF_SIZE 8192 |
| 1193 |
|
| #define CONNECT_APPEND(fmt, ...) \ |
| 1194 |
|
| do { \ |
| 1195 |
|
| if (off >= (int)sizeof(buf)) { overflow = 1; break; } \ |
| 1196 |
|
| int _n = snprintf(buf + off, sizeof(buf) - off, fmt, ##__VA_ARGS__); \ |
| 1197 |
|
| if (_n < 0 || _n >= (int)(sizeof(buf) - off)) { overflow = 1; break; } \ |
| 1198 |
|
| off += _n; \ |
| 1199 |
|
| } while(0) |
| 1200 |
|
| |
| 1201 |
37 |
| static void nats_send_connect(nats_t *self) |
| 1202 |
|
| { |
| 1203 |
|
| char buf[CONNECT_BUF_SIZE]; |
| 1204 |
|
| /* Per-field scratch sized to the whole CONNECT buffer: production |
| 1205 |
|
| JWTs routinely exceed 1KB, and the total is still capped by |
| 1206 |
|
| CONNECT_APPEND below. */ |
| 1207 |
37 |
| int off = 0; |
| 1208 |
37 |
| int overflow = 0; |
| 1209 |
|
| char escaped[CONNECT_BUF_SIZE]; |
| 1210 |
|
| |
| 1211 |
37 |
▶ | CONNECT_APPEND("CONNECT {"); |
Branch: 3 branches
| Branch | True | False | Total |
| CONNECT_APPEND("CONNECT {"); |
0 |
37 |
37 |
| CONNECT_APPEND("CONNECT {"); |
37 |
0 |
37 |
| CONNECT_APPEND("CONNECT {"); |
0 |
37 |
37 |
|
| 1212 |
37 |
▶ | CONNECT_APPEND("\"verbose\":%s,\"pedantic\":%s,\"echo\":%s", |
Branch: 6 branches
| Branch | True | False | Total |
| CONNECT_APPEND("\"verbose\":%s,\"pedantic\":%s,\"echo\":%s", |
0 |
37 |
37 |
| CONNECT_APPEND("\"verbose\":%s,\"pedantic\":%s,\"echo\":%s", |
37 |
0 |
37 |
| CONNECT_APPEND("\"verbose\":%s,\"pedantic\":%s,\"echo\":%s", |
0 |
37 |
37 |
| CONNECT_APPEND("\"verbose\":%s,\"pedantic\":%s,\"echo\":%s", |
0 |
37 |
37 |
| CONNECT_APPEND("\"verbose\":%s,\"pedantic\":%s,\"echo\":%s", |
37 |
0 |
37 |
| CONNECT_APPEND("\"verbose\":%s,\"pedantic\":%s,\"echo\":%s", |
0 |
37 |
37 |
|
| 1213 |
|
| self->verbose ? "true" : "false", |
| 1214 |
|
| self->pedantic ? "true" : "false", |
| 1215 |
|
| self->echo ? "true" : "false"); |
| 1216 |
37 |
▶ | CONNECT_APPEND(",\"protocol\":1"); |
Branch: 3 branches
| Branch | True | False | Total |
| CONNECT_APPEND(",\"protocol\":1"); |
0 |
37 |
37 |
| CONNECT_APPEND(",\"protocol\":1"); |
37 |
0 |
37 |
| CONNECT_APPEND(",\"protocol\":1"); |
0 |
37 |
37 |
|
| 1217 |
37 |
▶ | if (self->no_responders) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->no_responders) |
0 |
37 |
37 |
|
| 1218 |
0 |
▶ | CONNECT_APPEND(",\"no_responders\":true"); |
Branch: 3 branches
| Branch | True | False | Total |
| CONNECT_APPEND(",\"no_responders\":true"); |
0 |
0 |
0 |
| CONNECT_APPEND(",\"no_responders\":true"); |
0 |
0 |
0 |
| CONNECT_APPEND(",\"no_responders\":true"); |
0 |
0 |
0 |
|
| 1219 |
37 |
▶ | CONNECT_APPEND(",\"headers\":true"); |
Branch: 3 branches
| Branch | True | False | Total |
| CONNECT_APPEND(",\"headers\":true"); |
0 |
37 |
37 |
| CONNECT_APPEND(",\"headers\":true"); |
37 |
0 |
37 |
| CONNECT_APPEND(",\"headers\":true"); |
0 |
37 |
37 |
|
| 1220 |
37 |
▶ | CONNECT_APPEND(",\"lang\":\"perl-xs\",\"version\":\"" EV_NATS_VERSION "\""); |
Branch: 3 branches
| Branch | True | False | Total |
| CONNECT_APPEND(",\"lang\":\"perl-xs\",\"version\":\"" EV_NATS_VERSION "\""); |
0 |
37 |
37 |
| CONNECT_APPEND(",\"lang\":\"perl-xs\",\"version\":\"" EV_NATS_VERSION "\""); |
37 |
0 |
37 |
| CONNECT_APPEND(",\"lang\":\"perl-xs\",\"version\":\"" EV_NATS_VERSION "\""); |
0 |
37 |
37 |
|
| 1221 |
|
| |
| 1222 |
37 |
▶ | if (self->user) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->user) { |
0 |
37 |
37 |
|
| 1223 |
0 |
| int elen = json_escape_string(escaped, sizeof(escaped), self->user); |
| 1224 |
0 |
▶ | if (elen < 0) overflow = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (elen < 0) overflow = 1; |
0 |
0 |
0 |
|
| 1225 |
0 |
▶ | else CONNECT_APPEND(",\"user\":%.*s", elen, escaped); |
Branch: 3 branches
| Branch | True | False | Total |
| else CONNECT_APPEND(",\"user\":%.*s", elen, escaped); |
0 |
0 |
0 |
| else CONNECT_APPEND(",\"user\":%.*s", elen, escaped); |
0 |
0 |
0 |
| else CONNECT_APPEND(",\"user\":%.*s", elen, escaped); |
0 |
0 |
0 |
|
| 1226 |
0 |
▶ | if (self->pass) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->pass) { |
0 |
0 |
0 |
|
| 1227 |
0 |
| elen = json_escape_string(escaped, sizeof(escaped), self->pass); |
| 1228 |
0 |
▶ | if (elen < 0) overflow = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (elen < 0) overflow = 1; |
0 |
0 |
0 |
|
| 1229 |
0 |
▶ | else CONNECT_APPEND(",\"pass\":%.*s", elen, escaped); |
Branch: 3 branches
| Branch | True | False | Total |
| else CONNECT_APPEND(",\"pass\":%.*s", elen, escaped); |
0 |
0 |
0 |
| else CONNECT_APPEND(",\"pass\":%.*s", elen, escaped); |
0 |
0 |
0 |
| else CONNECT_APPEND(",\"pass\":%.*s", elen, escaped); |
0 |
0 |
0 |
|
| 1230 |
|
| } |
| 1231 |
|
| } |
| 1232 |
37 |
▶ | if (self->token) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->token) { |
0 |
37 |
37 |
|
| 1233 |
0 |
| int elen = json_escape_string(escaped, sizeof(escaped), self->token); |
| 1234 |
0 |
▶ | if (elen < 0) overflow = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (elen < 0) overflow = 1; |
0 |
0 |
0 |
|
| 1235 |
0 |
▶ | else CONNECT_APPEND(",\"auth_token\":%.*s", elen, escaped); |
Branch: 3 branches
| Branch | True | False | Total |
| else CONNECT_APPEND(",\"auth_token\":%.*s", elen, escaped); |
0 |
0 |
0 |
| else CONNECT_APPEND(",\"auth_token\":%.*s", elen, escaped); |
0 |
0 |
0 |
| else CONNECT_APPEND(",\"auth_token\":%.*s", elen, escaped); |
0 |
0 |
0 |
|
| 1236 |
|
| } |
| 1237 |
37 |
▶ | if (self->name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->name) { |
1 |
36 |
37 |
|
| 1238 |
1 |
| int elen = json_escape_string(escaped, sizeof(escaped), self->name); |
| 1239 |
1 |
▶ | if (elen < 0) overflow = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (elen < 0) overflow = 1; |
1 |
0 |
1 |
|
| 1240 |
0 |
▶ | else CONNECT_APPEND(",\"name\":%.*s", elen, escaped); |
Branch: 3 branches
| Branch | True | False | Total |
| else CONNECT_APPEND(",\"name\":%.*s", elen, escaped); |
0 |
0 |
0 |
| else CONNECT_APPEND(",\"name\":%.*s", elen, escaped); |
0 |
0 |
0 |
| else CONNECT_APPEND(",\"name\":%.*s", elen, escaped); |
0 |
0 |
0 |
|
| 1241 |
|
| } |
| 1242 |
37 |
▶ | if (self->jwt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->jwt) { |
0 |
37 |
37 |
|
| 1243 |
0 |
| int elen = json_escape_string(escaped, sizeof(escaped), self->jwt); |
| 1244 |
0 |
▶ | if (elen < 0) overflow = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (elen < 0) overflow = 1; |
0 |
0 |
0 |
|
| 1245 |
0 |
▶ | else CONNECT_APPEND(",\"jwt\":%.*s", elen, escaped); |
Branch: 3 branches
| Branch | True | False | Total |
| else CONNECT_APPEND(",\"jwt\":%.*s", elen, escaped); |
0 |
0 |
0 |
| else CONNECT_APPEND(",\"jwt\":%.*s", elen, escaped); |
0 |
0 |
0 |
| else CONNECT_APPEND(",\"jwt\":%.*s", elen, escaped); |
0 |
0 |
0 |
|
| 1246 |
|
| } |
| 1247 |
|
| #ifdef HAVE_OPENSSL |
| 1248 |
37 |
▶ | if (self->nkey_seed && self->server_nonce) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->nkey_seed && self->server_nonce) { |
0 |
37 |
37 |
| if (self->nkey_seed && self->server_nonce) { |
0 |
0 |
0 |
|
| 1249 |
|
| /* Sign nonce with NKey */ |
| 1250 |
|
| char sig[128]; |
| 1251 |
|
| char pub[64]; |
| 1252 |
0 |
▶ | if (nats_nkey_sign(self->nkey_seed, self->server_nonce, |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_nkey_sign(self->nkey_seed, self->server_nonce, |
0 |
0 |
0 |
|
| 1253 |
0 |
▶ | strlen(self->server_nonce), sig, sizeof(sig)) > 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| strlen(self->server_nonce), sig, sizeof(sig)) > 0 && |
0 |
0 |
0 |
|
| 1254 |
0 |
| nats_nkey_public(self->nkey_seed, pub, sizeof(pub)) > 0) { |
| 1255 |
0 |
▶ | CONNECT_APPEND(",\"nkey\":\"%s\",\"sig\":\"%s\"", pub, sig); |
Branch: 3 branches
| Branch | True | False | Total |
| CONNECT_APPEND(",\"nkey\":\"%s\",\"sig\":\"%s\"", pub, sig); |
0 |
0 |
0 |
| CONNECT_APPEND(",\"nkey\":\"%s\",\"sig\":\"%s\"", pub, sig); |
0 |
0 |
0 |
| CONNECT_APPEND(",\"nkey\":\"%s\",\"sig\":\"%s\"", pub, sig); |
0 |
0 |
0 |
|
| 1256 |
|
| } |
| 1257 |
|
| } |
| 1258 |
|
| #endif |
| 1259 |
|
| |
| 1260 |
37 |
▶ | CONNECT_APPEND("}\r\n"); |
Branch: 3 branches
| Branch | True | False | Total |
| CONNECT_APPEND("}\r\n"); |
0 |
37 |
37 |
| CONNECT_APPEND("}\r\n"); |
37 |
0 |
37 |
| CONNECT_APPEND("}\r\n"); |
0 |
37 |
37 |
|
| 1261 |
|
| |
| 1262 |
37 |
▶ | if (overflow) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (overflow) { |
1 |
36 |
37 |
|
| 1263 |
|
| /* deterministic local failure: retries cannot help */ |
| 1264 |
1 |
| self->intentional_disconnect = 1; |
| 1265 |
1 |
| nats_emit_error(self, "CONNECT command too long (auth/name fields)"); |
| 1266 |
1 |
| nats_cleanup(self); |
| 1267 |
1 |
| return; |
| 1268 |
|
| } |
| 1269 |
|
| |
| 1270 |
36 |
| wbuf_append(self, buf, off); |
| 1271 |
|
| |
| 1272 |
36 |
| nats_resub_all(self); |
| 1273 |
|
| |
| 1274 |
36 |
| wbuf_append(self, "PING\r\n", 6); |
| 1275 |
36 |
| nats_push_pong_marker(self); |
| 1276 |
|
| } |
| 1277 |
|
| |
| 1278 |
|
| /* ================================================================ |
| 1279 |
|
| * Protocol parser |
| 1280 |
|
| * ================================================================ */ |
| 1281 |
|
| |
| 1282 |
|
| /* Parse a decimal token as size_t. Returns 0 on success, -1 on |
| 1283 |
|
| non-digits, empty input, or overflow. */ |
| 1284 |
93 |
| static int nats_parse_decimal(const char *tok, size_t tok_len, size_t *out) |
| 1285 |
|
| { |
| 1286 |
93 |
| size_t v = 0; |
| 1287 |
|
| size_t i; |
| 1288 |
93 |
▶ | if (tok_len == 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tok_len == 0) return -1; |
1 |
92 |
93 |
|
| 1289 |
214 |
▶ | for (i = 0; i < tok_len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < tok_len; i++) { |
124 |
90 |
214 |
|
| 1290 |
124 |
▶ | if (tok[i] < '0' || tok[i] > '9') return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (tok[i] < '0' || tok[i] > '9') return -1; |
123 |
1 |
124 |
| if (tok[i] < '0' || tok[i] > '9') return -1; |
0 |
123 |
123 |
|
| 1291 |
123 |
| size_t d = (size_t)(tok[i] - '0'); |
| 1292 |
123 |
▶ | if (v > (SIZE_MAX - d) / 10) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v > (SIZE_MAX - d) / 10) return -1; |
1 |
122 |
123 |
|
| 1293 |
122 |
| v = v * 10 + d; |
| 1294 |
|
| } |
| 1295 |
90 |
| *out = v; |
| 1296 |
90 |
| return 0; |
| 1297 |
|
| } |
| 1298 |
|
| |
| 1299 |
42 |
| static int nats_parse_msg_args(nats_t *self, char *line, size_t len) |
| 1300 |
|
| { |
| 1301 |
42 |
| char *p = line; |
| 1302 |
42 |
| char *end = line + len; |
| 1303 |
|
| char *tok_start; |
| 1304 |
|
| |
| 1305 |
42 |
▶ | if (len < 4) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 4) return -1; |
0 |
42 |
42 |
|
| 1306 |
42 |
| p += 4; |
| 1307 |
|
| |
| 1308 |
|
| /* subject */ |
| 1309 |
42 |
| tok_start = p; |
| 1310 |
286 |
▶ | while (p < end && *p != ' ' && *p != '\t') p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && *p != ' ' && *p != '\t') p++; |
285 |
1 |
286 |
| while (p < end && *p != ' ' && *p != '\t') p++; |
244 |
41 |
285 |
| while (p < end && *p != ' ' && *p != '\t') p++; |
244 |
0 |
244 |
|
| 1311 |
42 |
▶ | if (p == tok_start) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == tok_start) return -1; |
1 |
41 |
42 |
|
| 1312 |
41 |
| self->msg_subject_off = tok_start - self->rbuf; |
| 1313 |
41 |
| self->msg_subject_len = p - tok_start; |
| 1314 |
|
| |
| 1315 |
81 |
▶ | while (p < end && (*p == ' ' || *p == '\t')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
80 |
1 |
81 |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
40 |
40 |
80 |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
0 |
40 |
40 |
|
| 1316 |
|
| |
| 1317 |
|
| /* sid */ |
| 1318 |
41 |
| tok_start = p; |
| 1319 |
81 |
▶ | while (p < end && *p != ' ' && *p != '\t') p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && *p != ' ' && *p != '\t') p++; |
80 |
1 |
81 |
| while (p < end && *p != ' ' && *p != '\t') p++; |
40 |
40 |
80 |
| while (p < end && *p != ' ' && *p != '\t') p++; |
40 |
0 |
40 |
|
| 1320 |
|
| { |
| 1321 |
|
| size_t sid; |
| 1322 |
41 |
▶ | if (nats_parse_decimal(tok_start, p - tok_start, &sid) != 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_parse_decimal(tok_start, p - tok_start, &sid) != 0) return -1; |
1 |
40 |
41 |
|
| 1323 |
40 |
| self->msg_sid = (uint64_t)sid; |
| 1324 |
|
| } |
| 1325 |
|
| |
| 1326 |
80 |
▶ | while (p < end && (*p == ' ' || *p == '\t')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
80 |
0 |
80 |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
40 |
40 |
80 |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
0 |
40 |
40 |
|
| 1327 |
|
| |
| 1328 |
|
| /* Remaining tokens: [reply-to] <#bytes> — use token counting like HMSG */ |
| 1329 |
40 |
| int ntokens = 0; |
| 1330 |
|
| char *tokens[3]; |
| 1331 |
|
| size_t token_lens[3]; |
| 1332 |
40 |
| char *tp = p; |
| 1333 |
106 |
▶ | while (tp < end && ntokens < 3) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (tp < end && ntokens < 3) { |
67 |
39 |
106 |
| while (tp < end && ntokens < 3) { |
67 |
0 |
67 |
|
| 1334 |
96 |
▶ | while (tp < end && (*tp == ' ' || *tp == '\t')) tp++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (tp < end && (*tp == ' ' || *tp == '\t')) tp++; |
95 |
1 |
96 |
| while (tp < end && (*tp == ' ' || *tp == '\t')) tp++; |
29 |
66 |
95 |
| while (tp < end && (*tp == ' ' || *tp == '\t')) tp++; |
0 |
66 |
66 |
|
| 1335 |
67 |
▶ | if (tp >= end) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tp >= end) break; |
1 |
66 |
67 |
|
| 1336 |
66 |
| tokens[ntokens] = tp; |
| 1337 |
198 |
▶ | while (tp < end && *tp != ' ' && *tp != '\t') tp++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (tp < end && *tp != ' ' && *tp != '\t') tp++; |
159 |
39 |
198 |
| while (tp < end && *tp != ' ' && *tp != '\t') tp++; |
132 |
27 |
159 |
| while (tp < end && *tp != ' ' && *tp != '\t') tp++; |
132 |
0 |
132 |
|
| 1338 |
66 |
| token_lens[ntokens] = tp - tokens[ntokens]; |
| 1339 |
66 |
| ntokens++; |
| 1340 |
|
| } |
| 1341 |
|
| |
| 1342 |
40 |
▶ | if (ntokens == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ntokens == 1) { |
14 |
26 |
40 |
|
| 1343 |
|
| /* no reply-to, just #bytes */ |
| 1344 |
14 |
| self->msg_reply_off = 0; |
| 1345 |
14 |
| self->msg_reply_len = 0; |
| 1346 |
14 |
▶ | if (nats_parse_decimal(tokens[0], token_lens[0], &self->msg_total_len) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_parse_decimal(tokens[0], token_lens[0], &self->msg_total_len) != 0) |
2 |
12 |
14 |
|
| 1347 |
2 |
| return -1; |
| 1348 |
26 |
▶ | } else if (ntokens == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (ntokens == 2) { |
26 |
0 |
26 |
|
| 1349 |
|
| /* reply-to + #bytes */ |
| 1350 |
26 |
| self->msg_reply_off = tokens[0] - self->rbuf; |
| 1351 |
26 |
| self->msg_reply_len = token_lens[0]; |
| 1352 |
26 |
▶ | if (nats_parse_decimal(tokens[1], token_lens[1], &self->msg_total_len) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_parse_decimal(tokens[1], token_lens[1], &self->msg_total_len) != 0) |
0 |
26 |
26 |
|
| 1353 |
0 |
| return -1; |
| 1354 |
|
| } else { |
| 1355 |
0 |
| return -1; |
| 1356 |
|
| } |
| 1357 |
|
| |
| 1358 |
38 |
| self->msg_hdr_len = 0; |
| 1359 |
38 |
| self->msg_type = MSG_TYPE_MSG; |
| 1360 |
38 |
| return 0; |
| 1361 |
|
| } |
| 1362 |
|
| |
| 1363 |
4 |
| static int nats_parse_hmsg_args(nats_t *self, char *line, size_t len) |
| 1364 |
|
| { |
| 1365 |
4 |
| char *p = line; |
| 1366 |
4 |
| char *end = line + len; |
| 1367 |
|
| char *tok_start; |
| 1368 |
|
| |
| 1369 |
4 |
▶ | if (len < 5) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 5) return -1; |
0 |
4 |
4 |
|
| 1370 |
4 |
| p += 5; |
| 1371 |
|
| |
| 1372 |
|
| /* subject */ |
| 1373 |
4 |
| tok_start = p; |
| 1374 |
16 |
▶ | while (p < end && *p != ' ' && *p != '\t') p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && *p != ' ' && *p != '\t') p++; |
16 |
0 |
16 |
| while (p < end && *p != ' ' && *p != '\t') p++; |
12 |
4 |
16 |
| while (p < end && *p != ' ' && *p != '\t') p++; |
12 |
0 |
12 |
|
| 1375 |
4 |
▶ | if (p == tok_start) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == tok_start) return -1; |
0 |
4 |
4 |
|
| 1376 |
4 |
| self->msg_subject_off = tok_start - self->rbuf; |
| 1377 |
4 |
| self->msg_subject_len = p - tok_start; |
| 1378 |
|
| |
| 1379 |
8 |
▶ | while (p < end && (*p == ' ' || *p == '\t')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
8 |
0 |
8 |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
4 |
4 |
8 |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
0 |
4 |
4 |
|
| 1380 |
|
| |
| 1381 |
|
| /* sid */ |
| 1382 |
4 |
| tok_start = p; |
| 1383 |
8 |
▶ | while (p < end && *p != ' ' && *p != '\t') p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && *p != ' ' && *p != '\t') p++; |
8 |
0 |
8 |
| while (p < end && *p != ' ' && *p != '\t') p++; |
4 |
4 |
8 |
| while (p < end && *p != ' ' && *p != '\t') p++; |
4 |
0 |
4 |
|
| 1384 |
|
| { |
| 1385 |
|
| size_t sid; |
| 1386 |
4 |
▶ | if (nats_parse_decimal(tok_start, p - tok_start, &sid) != 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_parse_decimal(tok_start, p - tok_start, &sid) != 0) return -1; |
0 |
4 |
4 |
|
| 1387 |
4 |
| self->msg_sid = (uint64_t)sid; |
| 1388 |
|
| } |
| 1389 |
|
| |
| 1390 |
8 |
▶ | while (p < end && (*p == ' ' || *p == '\t')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
8 |
0 |
8 |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
4 |
4 |
8 |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
0 |
4 |
4 |
|
| 1391 |
|
| |
| 1392 |
4 |
| int ntokens = 0; |
| 1393 |
|
| char *tokens[4]; |
| 1394 |
|
| size_t token_lens[4]; |
| 1395 |
4 |
| char *tp = p; |
| 1396 |
13 |
▶ | while (tp < end && ntokens < 4) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (tp < end && ntokens < 4) { |
9 |
4 |
13 |
| while (tp < end && ntokens < 4) { |
9 |
0 |
9 |
|
| 1397 |
14 |
▶ | while (tp < end && (*tp == ' ' || *tp == '\t')) tp++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (tp < end && (*tp == ' ' || *tp == '\t')) tp++; |
14 |
0 |
14 |
| while (tp < end && (*tp == ' ' || *tp == '\t')) tp++; |
5 |
9 |
14 |
| while (tp < end && (*tp == ' ' || *tp == '\t')) tp++; |
0 |
9 |
9 |
|
| 1398 |
9 |
▶ | if (tp >= end) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tp >= end) break; |
0 |
9 |
9 |
|
| 1399 |
9 |
| tokens[ntokens] = tp; |
| 1400 |
22 |
▶ | while (tp < end && *tp != ' ' && *tp != '\t') tp++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (tp < end && *tp != ' ' && *tp != '\t') tp++; |
18 |
4 |
22 |
| while (tp < end && *tp != ' ' && *tp != '\t') tp++; |
13 |
5 |
18 |
| while (tp < end && *tp != ' ' && *tp != '\t') tp++; |
13 |
0 |
13 |
|
| 1401 |
9 |
| token_lens[ntokens] = tp - tokens[ntokens]; |
| 1402 |
9 |
| ntokens++; |
| 1403 |
|
| } |
| 1404 |
|
| |
| 1405 |
|
| int hdr_idx, len_idx; |
| 1406 |
4 |
▶ | if (ntokens == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ntokens == 2) { |
3 |
1 |
4 |
|
| 1407 |
3 |
| self->msg_reply_off = 0; |
| 1408 |
3 |
| self->msg_reply_len = 0; |
| 1409 |
3 |
| hdr_idx = 0; len_idx = 1; |
| 1410 |
1 |
▶ | } else if (ntokens == 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (ntokens == 3) { |
1 |
0 |
1 |
|
| 1411 |
1 |
| self->msg_reply_off = tokens[0] - self->rbuf; |
| 1412 |
1 |
| self->msg_reply_len = token_lens[0]; |
| 1413 |
1 |
| hdr_idx = 1; len_idx = 2; |
| 1414 |
|
| } else { |
| 1415 |
0 |
| return -1; |
| 1416 |
|
| } |
| 1417 |
4 |
▶ | if (nats_parse_decimal(tokens[hdr_idx], token_lens[hdr_idx], &self->msg_hdr_len) != 0 |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_parse_decimal(tokens[hdr_idx], token_lens[hdr_idx], &self->msg_hdr_len) != 0 |
4 |
0 |
4 |
|
| 1418 |
4 |
▶ | || nats_parse_decimal(tokens[len_idx], token_lens[len_idx], &self->msg_total_len) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| || nats_parse_decimal(tokens[len_idx], token_lens[len_idx], &self->msg_total_len) != 0) { |
0 |
4 |
4 |
|
| 1419 |
0 |
| return -1; |
| 1420 |
|
| } |
| 1421 |
|
| |
| 1422 |
4 |
| self->msg_type = MSG_TYPE_HMSG; |
| 1423 |
4 |
| return 0; |
| 1424 |
|
| } |
| 1425 |
|
| |
| 1426 |
30 |
| static void nats_process_msg(nats_t *self, char *payload, size_t len) |
| 1427 |
|
| { |
| 1428 |
30 |
| nats_sub_t *sub = nats_find_sub(self, self->msg_sid); |
| 1429 |
30 |
▶ | if (!sub) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sub) return; |
0 |
30 |
30 |
|
| 1430 |
|
| |
| 1431 |
30 |
| self->msgs_in++; |
| 1432 |
30 |
| self->bytes_in += len; |
| 1433 |
30 |
| sub->received++; |
| 1434 |
|
| |
| 1435 |
30 |
| int max_msgs = sub->max_msgs; |
| 1436 |
30 |
| int received = sub->received; |
| 1437 |
30 |
| uint64_t sid = sub->sid; |
| 1438 |
|
| |
| 1439 |
30 |
▶ | if (sub->cb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sub->cb) { |
30 |
0 |
30 |
|
| 1440 |
30 |
| dSP; |
| 1441 |
30 |
| ENTER; SAVETMPS; |
| 1442 |
30 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
30 |
30 |
|
| 1443 |
30 |
▶ | EXTEND(SP, 4); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 4); |
0 |
30 |
30 |
|
| 1444 |
|
| |
| 1445 |
30 |
| PUSHs(sv_2mortal(newSVpvn(self->rbuf + self->msg_subject_off, self->msg_subject_len))); |
| 1446 |
|
| |
| 1447 |
30 |
▶ | if (self->msg_type == MSG_TYPE_HMSG && self->msg_hdr_len > 0 && self->msg_hdr_len <= len) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (self->msg_type == MSG_TYPE_HMSG && self->msg_hdr_len > 0 && self->msg_hdr_len <= len) { |
4 |
26 |
30 |
| if (self->msg_type == MSG_TYPE_HMSG && self->msg_hdr_len > 0 && self->msg_hdr_len <= len) { |
3 |
1 |
4 |
| if (self->msg_type == MSG_TYPE_HMSG && self->msg_hdr_len > 0 && self->msg_hdr_len <= len) { |
2 |
1 |
3 |
|
| 1448 |
2 |
| PUSHs(sv_2mortal(newSVpvn(payload + self->msg_hdr_len, len - self->msg_hdr_len))); |
| 1449 |
|
| } else { |
| 1450 |
28 |
| PUSHs(sv_2mortal(newSVpvn(payload, len))); |
| 1451 |
|
| } |
| 1452 |
|
| |
| 1453 |
30 |
▶ | if (self->msg_reply_len > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->msg_reply_len > 0) { |
22 |
8 |
30 |
|
| 1454 |
22 |
| PUSHs(sv_2mortal(newSVpvn(self->rbuf + self->msg_reply_off, self->msg_reply_len))); |
| 1455 |
|
| } else { |
| 1456 |
8 |
| PUSHs(&PL_sv_undef); |
| 1457 |
|
| } |
| 1458 |
|
| |
| 1459 |
30 |
▶ | if (self->msg_type == MSG_TYPE_HMSG && self->msg_hdr_len > 0 && self->msg_hdr_len <= len) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (self->msg_type == MSG_TYPE_HMSG && self->msg_hdr_len > 0 && self->msg_hdr_len <= len) { |
4 |
26 |
30 |
| if (self->msg_type == MSG_TYPE_HMSG && self->msg_hdr_len > 0 && self->msg_hdr_len <= len) { |
3 |
1 |
4 |
| if (self->msg_type == MSG_TYPE_HMSG && self->msg_hdr_len > 0 && self->msg_hdr_len <= len) { |
2 |
1 |
3 |
|
| 1460 |
2 |
| PUSHs(sv_2mortal(newSVpvn(payload, self->msg_hdr_len))); |
| 1461 |
|
| } |
| 1462 |
|
| |
| 1463 |
30 |
| PUTBACK; |
| 1464 |
|
| /* pin sub->cb: a callback that unsubscribes its own sid clears |
| 1465 |
|
| sub->cb (and frees the sub) mid-call; the pin keeps the CV alive */ |
| 1466 |
30 |
| PINNED_CALL_SV(sub->cb, G_DISCARD); |
| 1467 |
30 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
30 |
0 |
30 |
|
| 1468 |
|
| } |
| 1469 |
|
| |
| 1470 |
30 |
▶ | if (max_msgs > 0 && received >= max_msgs) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (max_msgs > 0 && received >= max_msgs) { |
0 |
30 |
30 |
| if (max_msgs > 0 && received >= max_msgs) { |
0 |
0 |
0 |
|
| 1471 |
0 |
| sub = nats_find_sub(self, sid); |
| 1472 |
0 |
▶ | if (sub) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sub) |
0 |
0 |
0 |
|
| 1473 |
0 |
| nats_remove_sub(self, sub); |
| 1474 |
|
| } |
| 1475 |
|
| } |
| 1476 |
|
| |
| 1477 |
4 |
| static void nats_check_inbox_response(nats_t *self, const char *subject, size_t subject_len, |
| 1478 |
|
| const char *payload, size_t payload_len, |
| 1479 |
|
| const char *headers, size_t headers_len) |
| 1480 |
|
| { |
| 1481 |
4 |
| size_t pfx_len = strlen(self->inbox_prefix); |
| 1482 |
4 |
▶ | if (subject_len <= pfx_len || memcmp(subject, self->inbox_prefix, pfx_len) != 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (subject_len <= pfx_len || memcmp(subject, self->inbox_prefix, pfx_len) != 0) |
4 |
0 |
4 |
| if (subject_len <= pfx_len || memcmp(subject, self->inbox_prefix, pfx_len) != 0) |
0 |
4 |
4 |
|
| 1483 |
0 |
| return; |
| 1484 |
|
| |
| 1485 |
4 |
| const char *id_str = subject + pfx_len; |
| 1486 |
4 |
| size_t id_len = subject_len - pfx_len; |
| 1487 |
4 |
| uint64_t req_id = 0; |
| 1488 |
|
| size_t i; |
| 1489 |
26 |
▶ | for (i = 0; i < id_len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < id_len; i++) { |
23 |
3 |
26 |
|
| 1490 |
23 |
▶ | if (id_str[i] < '0' || id_str[i] > '9') return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (id_str[i] < '0' || id_str[i] > '9') return; |
23 |
0 |
23 |
| if (id_str[i] < '0' || id_str[i] > '9') return; |
0 |
23 |
23 |
|
| 1491 |
23 |
| uint64_t d = (uint64_t)(id_str[i] - '0'); |
| 1492 |
23 |
▶ | if (req_id > (UINT64_MAX - d) / 10) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (req_id > (UINT64_MAX - d) / 10) return; |
1 |
22 |
23 |
|
| 1493 |
22 |
| req_id = req_id * 10 + d; |
| 1494 |
|
| } |
| 1495 |
|
| |
| 1496 |
|
| ngx_queue_t *q; |
| 1497 |
3 |
▶ | ngx_queue_foreach(q, &self->req_queue) { |
Branch: 2 branches
| Branch | True | False | Total |
| ngx_queue_foreach(q, &self->req_queue) { |
3 |
0 |
3 |
| ngx_queue_foreach(q, &self->req_queue) { |
3 |
0 |
3 |
|
| 1498 |
3 |
| nats_req_t *req = ngx_queue_data(q, nats_req_t, queue); |
| 1499 |
3 |
▶ | if (req->req_id == req_id) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (req->req_id == req_id) { |
3 |
0 |
3 |
|
| 1500 |
3 |
| ngx_queue_remove(q); |
| 1501 |
3 |
▶ | if (req->timer_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (req->timer_active) { |
3 |
0 |
3 |
|
| 1502 |
3 |
| ev_timer_stop(self->loop, &req->timer); |
| 1503 |
3 |
| req->timer_active = 0; |
| 1504 |
|
| } |
| 1505 |
3 |
▶ | if (req->cb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (req->cb) { |
3 |
0 |
3 |
|
| 1506 |
3 |
| dSP; |
| 1507 |
3 |
▶ | int is_no_responders = (headers && headers_len >= 12 && |
Branch: 2 branches
| Branch | True | False | Total |
| int is_no_responders = (headers && headers_len >= 12 && |
0 |
3 |
3 |
| int is_no_responders = (headers && headers_len >= 12 && |
0 |
0 |
0 |
|
| 1508 |
0 |
▶ | memcmp(headers, "NATS/1.0 503", 12) == 0); |
Branch: 1 branch
| Branch | True | False | Total |
| memcmp(headers, "NATS/1.0 503", 12) == 0); |
0 |
0 |
0 |
|
| 1509 |
3 |
| ENTER; SAVETMPS; |
| 1510 |
3 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
3 |
3 |
|
| 1511 |
3 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
3 |
3 |
|
| 1512 |
3 |
▶ | if (is_no_responders) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_no_responders) { |
0 |
3 |
3 |
|
| 1513 |
0 |
| PUSHs(&PL_sv_undef); |
| 1514 |
0 |
| PUSHs(sv_2mortal(newSVpvn("no responders", 13))); |
| 1515 |
|
| } else { |
| 1516 |
3 |
| PUSHs(sv_2mortal(newSVpvn(payload, payload_len))); |
| 1517 |
3 |
| PUSHs(&PL_sv_undef); |
| 1518 |
3 |
▶ | if (headers && headers_len > 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (headers && headers_len > 0) |
0 |
3 |
3 |
| if (headers && headers_len > 0) |
0 |
0 |
0 |
|
| 1519 |
0 |
| PUSHs(sv_2mortal(newSVpvn(headers, headers_len))); |
| 1520 |
|
| } |
| 1521 |
3 |
| PUTBACK; |
| 1522 |
3 |
| call_sv(req->cb, G_DISCARD); |
| 1523 |
3 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
3 |
0 |
3 |
|
| 1524 |
3 |
| SvREFCNT_dec(req->cb); |
| 1525 |
|
| } |
| 1526 |
3 |
| Safefree(req); |
| 1527 |
3 |
| return; |
| 1528 |
|
| } |
| 1529 |
|
| } |
| 1530 |
|
| } |
| 1531 |
|
| |
| 1532 |
139 |
| static void nats_process_line(nats_t *self, char *line, size_t len) |
| 1533 |
|
| { |
| 1534 |
139 |
▶ | if (len > 0 && line[len-1] == '\r') |
Branch: 2 branches
| Branch | True | False | Total |
| if (len > 0 && line[len-1] == '\r') |
139 |
0 |
139 |
| if (len > 0 && line[len-1] == '\r') |
139 |
0 |
139 |
|
| 1535 |
139 |
| len--; |
| 1536 |
|
| |
| 1537 |
139 |
▶ | if (len == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 0) return; |
1 |
138 |
139 |
|
| 1538 |
|
| |
| 1539 |
|
| /* INFO */ |
| 1540 |
138 |
▶ | if (len > 5 && (line[0] == 'I' || line[0] == 'i') && |
Branch: 3 branches
| Branch | True | False | Total |
| if (len > 5 && (line[0] == 'I' || line[0] == 'i') && |
91 |
47 |
138 |
| if (len > 5 && (line[0] == 'I' || line[0] == 'i') && |
48 |
43 |
91 |
| if (len > 5 && (line[0] == 'I' || line[0] == 'i') && |
0 |
48 |
48 |
|
| 1541 |
43 |
▶ | (line[1] == 'N' || line[1] == 'n') && |
Branch: 2 branches
| Branch | True | False | Total |
| (line[1] == 'N' || line[1] == 'n') && |
0 |
43 |
43 |
| (line[1] == 'N' || line[1] == 'n') && |
0 |
0 |
0 |
|
| 1542 |
43 |
▶ | (line[2] == 'F' || line[2] == 'f') && |
Branch: 2 branches
| Branch | True | False | Total |
| (line[2] == 'F' || line[2] == 'f') && |
0 |
43 |
43 |
| (line[2] == 'F' || line[2] == 'f') && |
0 |
0 |
0 |
|
| 1543 |
43 |
▶ | (line[3] == 'O' || line[3] == 'o') && |
Branch: 2 branches
| Branch | True | False | Total |
| (line[3] == 'O' || line[3] == 'o') && |
0 |
43 |
43 |
| (line[3] == 'O' || line[3] == 'o') && |
0 |
0 |
0 |
|
| 1544 |
43 |
▶ | line[4] == ' ') { |
Branch: 1 branch
| Branch | True | False | Total |
| line[4] == ' ') { |
43 |
0 |
43 |
|
| 1545 |
|
| |
| 1546 |
43 |
▶ | CLEAR_HANDLER(self->server_info_json); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->server_info_json); |
8 |
35 |
43 |
|
| 1547 |
43 |
| self->server_info_json = newSVpvn(line + 5, len - 5); |
| 1548 |
|
| |
| 1549 |
|
| { |
| 1550 |
43 |
| char *p = line + 5; |
| 1551 |
43 |
| char *e = line + len; |
| 1552 |
|
| char *found; |
| 1553 |
|
| |
| 1554 |
|
| /* max_payload accumulates into a signed int: parse wide and |
| 1555 |
|
| clamp to INT_MAX so a hostile INFO cannot poison it negative */ |
| 1556 |
43 |
| found = (char *)nats_memfind(p, (size_t)(e - p), "\"max_payload\":", 14); |
| 1557 |
43 |
▶ | if (found) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (found) { |
39 |
4 |
43 |
|
| 1558 |
39 |
| found += 14; |
| 1559 |
39 |
▶ | while (found < e && (*found == ' ' || *found == '\t')) found++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (found < e && (*found == ' ' || *found == '\t')) found++; |
39 |
0 |
39 |
| while (found < e && (*found == ' ' || *found == '\t')) found++; |
0 |
39 |
39 |
| while (found < e && (*found == ' ' || *found == '\t')) found++; |
0 |
39 |
39 |
|
| 1560 |
39 |
| uint64_t mp = 0; |
| 1561 |
39 |
| char *digits = found; |
| 1562 |
324 |
▶ | while (found < e && *found >= '0' && *found <= '9') { |
Branch: 3 branches
| Branch | True | False | Total |
| while (found < e && *found >= '0' && *found <= '9') { |
324 |
0 |
324 |
| while (found < e && *found >= '0' && *found <= '9') { |
302 |
22 |
324 |
| while (found < e && *found >= '0' && *found <= '9') { |
285 |
17 |
302 |
|
| 1563 |
285 |
| uint64_t d = (uint64_t)(*found - '0'); |
| 1564 |
285 |
▶ | if (mp <= (UINT64_MAX - d) / 10) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mp <= (UINT64_MAX - d) / 10) |
284 |
1 |
285 |
|
| 1565 |
284 |
| mp = mp * 10 + d; |
| 1566 |
|
| else |
| 1567 |
1 |
| mp = UINT64_MAX; |
| 1568 |
285 |
| found++; |
| 1569 |
|
| } |
| 1570 |
39 |
▶ | if (found > digits && mp > 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (found > digits && mp > 0) |
39 |
0 |
39 |
| if (found > digits && mp > 0) |
39 |
0 |
39 |
|
| 1571 |
39 |
▶ | self->max_payload = mp > (uint64_t)INT_MAX ? INT_MAX : (int)mp; |
Branch: 1 branch
| Branch | True | False | Total |
| self->max_payload = mp > (uint64_t)INT_MAX ? INT_MAX : (int)mp; |
37 |
2 |
39 |
|
| 1572 |
|
| } |
| 1573 |
|
| |
| 1574 |
43 |
| nats_parse_connect_urls(self, p, e - p); |
| 1575 |
|
| |
| 1576 |
|
| /* Parse nonce for NKey auth */ |
| 1577 |
43 |
| found = (char *)nats_memfind(p, (size_t)(e - p), "\"nonce\":", 8); |
| 1578 |
43 |
▶ | if (found) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (found) { |
1 |
42 |
43 |
|
| 1579 |
1 |
| found += 8; |
| 1580 |
1 |
▶ | while (found < e && (*found == ' ' || *found == '\t')) found++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (found < e && (*found == ' ' || *found == '\t')) found++; |
1 |
0 |
1 |
| while (found < e && (*found == ' ' || *found == '\t')) found++; |
0 |
1 |
1 |
| while (found < e && (*found == ' ' || *found == '\t')) found++; |
0 |
1 |
1 |
|
| 1581 |
1 |
▶ | if (found < e && *found == '"') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (found < e && *found == '"') { |
1 |
0 |
1 |
| if (found < e && *found == '"') { |
1 |
0 |
1 |
|
| 1582 |
1 |
| found++; |
| 1583 |
1 |
| const char *nend = memchr(found, '"', e - found); |
| 1584 |
1 |
▶ | if (nend) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nend) { |
0 |
1 |
1 |
|
| 1585 |
0 |
| size_t nlen = nend - found; |
| 1586 |
0 |
▶ | if (self->server_nonce) Safefree(self->server_nonce); |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->server_nonce) Safefree(self->server_nonce); |
0 |
0 |
0 |
|
| 1587 |
0 |
| Newx(self->server_nonce, nlen + 1, char); |
| 1588 |
0 |
| memcpy(self->server_nonce, found, nlen); |
| 1589 |
0 |
| self->server_nonce[nlen] = '\0'; |
| 1590 |
|
| } |
| 1591 |
|
| } |
| 1592 |
|
| } |
| 1593 |
|
| |
| 1594 |
|
| /* Parse ldm (lame duck mode) */ |
| 1595 |
43 |
| found = (char *)nats_memfind(p, (size_t)(e - p), "\"ldm\":", 6); |
| 1596 |
43 |
▶ | if (found) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (found) { |
1 |
42 |
43 |
|
| 1597 |
1 |
| found += 6; |
| 1598 |
1 |
▶ | while (found < e && (*found == ' ' || *found == '\t')) found++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (found < e && (*found == ' ' || *found == '\t')) found++; |
1 |
0 |
1 |
| while (found < e && (*found == ' ' || *found == '\t')) found++; |
0 |
1 |
1 |
| while (found < e && (*found == ' ' || *found == '\t')) found++; |
0 |
1 |
1 |
|
| 1599 |
1 |
▶ | if (found < e && *found == 't') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (found < e && *found == 't') { |
1 |
0 |
1 |
| if (found < e && *found == 't') { |
1 |
0 |
1 |
|
| 1600 |
1 |
▶ | if (!self->ldm) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->ldm) { |
1 |
0 |
1 |
|
| 1601 |
1 |
| self->ldm = 1; |
| 1602 |
1 |
▶ | if (self->on_ldm) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->on_ldm) { |
0 |
1 |
1 |
|
| 1603 |
0 |
| dSP; |
| 1604 |
0 |
| ENTER; SAVETMPS; |
| 1605 |
0 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
0 |
0 |
|
| 1606 |
0 |
| PUTBACK; |
| 1607 |
0 |
| PINNED_CALL_SV(self->on_ldm, G_DISCARD); |
| 1608 |
0 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 1609 |
|
| } |
| 1610 |
|
| } |
| 1611 |
|
| } |
| 1612 |
|
| } |
| 1613 |
|
| } |
| 1614 |
|
| |
| 1615 |
43 |
▶ | if (self->connecting) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connecting) { |
38 |
5 |
43 |
|
| 1616 |
|
| /* tls_required: CONNECT would leak credentials over plaintext |
| 1617 |
|
| before the server could reject it; refuse instead */ |
| 1618 |
|
| { |
| 1619 |
38 |
| const char *tr = nats_memfind(line + 5, len - 5, "\"tls_required\":", 15); |
| 1620 |
38 |
▶ | if (tr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tr) { |
1 |
37 |
38 |
|
| 1621 |
1 |
| const char *tre = line + len; |
| 1622 |
1 |
| int tls_active = 0; |
| 1623 |
1 |
| tr += 15; |
| 1624 |
1 |
▶ | while (tr < tre && (*tr == ' ' || *tr == '\t')) tr++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (tr < tre && (*tr == ' ' || *tr == '\t')) tr++; |
1 |
0 |
1 |
| while (tr < tre && (*tr == ' ' || *tr == '\t')) tr++; |
0 |
1 |
1 |
| while (tr < tre && (*tr == ' ' || *tr == '\t')) tr++; |
0 |
1 |
1 |
|
| 1625 |
|
| #ifdef HAVE_OPENSSL |
| 1626 |
1 |
| tls_active = self->tls; |
| 1627 |
|
| #endif |
| 1628 |
1 |
▶ | if ((size_t)(tre - tr) >= 4 && memcmp(tr, "true", 4) == 0 && !tls_active) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((size_t)(tre - tr) >= 4 && memcmp(tr, "true", 4) == 0 && !tls_active) { |
1 |
0 |
1 |
| if ((size_t)(tre - tr) >= 4 && memcmp(tr, "true", 4) == 0 && !tls_active) { |
1 |
0 |
1 |
| if ((size_t)(tre - tr) >= 4 && memcmp(tr, "true", 4) == 0 && !tls_active) { |
1 |
0 |
1 |
|
| 1629 |
1 |
| self->intentional_disconnect = 1; |
| 1630 |
1 |
| nats_emit_error(self, "server requires TLS but TLS is not enabled"); |
| 1631 |
1 |
| nats_cleanup(self); |
| 1632 |
1 |
| return; |
| 1633 |
|
| } |
| 1634 |
|
| } |
| 1635 |
|
| } |
| 1636 |
|
| #ifdef HAVE_OPENSSL |
| 1637 |
37 |
▶ | if (self->tls && !self->ssl) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->tls && !self->ssl) { |
0 |
37 |
37 |
| if (self->tls && !self->ssl) { |
0 |
0 |
0 |
|
| 1638 |
0 |
▶ | if (nats_ssl_setup(self) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_ssl_setup(self) != 0) { |
0 |
0 |
0 |
|
| 1639 |
0 |
| nats_ssl_fail(self, "SSL setup failed"); |
| 1640 |
0 |
| return; |
| 1641 |
|
| } |
| 1642 |
0 |
| self->ssl_handshaking = 1; |
| 1643 |
0 |
| int hret = nats_ssl_handshake(self); |
| 1644 |
0 |
▶ | if (hret < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hret < 0) { |
0 |
0 |
0 |
|
| 1645 |
0 |
| nats_ssl_fail(self, "SSL handshake failed"); |
| 1646 |
0 |
| return; |
| 1647 |
|
| } |
| 1648 |
0 |
▶ | if (hret == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hret == 1) { |
0 |
0 |
0 |
|
| 1649 |
0 |
| self->ssl_handshaking = 0; |
| 1650 |
0 |
| nats_send_connect(self); |
| 1651 |
0 |
| nats_try_write(self); |
| 1652 |
|
| } |
| 1653 |
|
| /* hret == 0: handshake in progress; nats_on_read will resume. */ |
| 1654 |
0 |
| return; |
| 1655 |
|
| } |
| 1656 |
|
| #endif |
| 1657 |
37 |
| nats_send_connect(self); |
| 1658 |
37 |
| nats_try_write(self); |
| 1659 |
|
| } |
| 1660 |
42 |
| return; |
| 1661 |
|
| } |
| 1662 |
|
| |
| 1663 |
|
| /* MSG */ |
| 1664 |
95 |
▶ | if (len >= 4 && line[0] == 'M' && line[1] == 'S' && line[2] == 'G' && line[3] == ' ') { |
Branch: 5 branches
| Branch | True | False | Total |
| if (len >= 4 && line[0] == 'M' && line[1] == 'S' && line[2] == 'G' && line[3] == ' ') { |
92 |
3 |
95 |
| if (len >= 4 && line[0] == 'M' && line[1] == 'S' && line[2] == 'G' && line[3] == ' ') { |
43 |
49 |
92 |
| if (len >= 4 && line[0] == 'M' && line[1] == 'S' && line[2] == 'G' && line[3] == ' ') { |
43 |
0 |
43 |
| if (len >= 4 && line[0] == 'M' && line[1] == 'S' && line[2] == 'G' && line[3] == ' ') { |
43 |
0 |
43 |
| if (len >= 4 && line[0] == 'M' && line[1] == 'S' && line[2] == 'G' && line[3] == ' ') { |
42 |
1 |
43 |
|
| 1665 |
42 |
▶ | if (nats_parse_msg_args(self, line, len) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_parse_msg_args(self, line, len) == 0) { |
38 |
4 |
42 |
|
| 1666 |
|
| /* max_payload is a signed int a hostile INFO could poison; |
| 1667 |
|
| the +2 check rejects lengths whose trailing CRLF would wrap */ |
| 1668 |
114 |
| size_t limit = self->max_payload > 0 ? (size_t)self->max_payload |
| 1669 |
38 |
▶ | : (size_t)DEFAULT_MAX_PAYLOAD; |
Branch: 1 branch
| Branch | True | False | Total |
| : (size_t)DEFAULT_MAX_PAYLOAD; |
38 |
0 |
38 |
|
| 1670 |
38 |
▶ | if (self->msg_total_len > limit || self->msg_total_len > SIZE_MAX - 2) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->msg_total_len > limit || self->msg_total_len > SIZE_MAX - 2) { |
37 |
1 |
38 |
| if (self->msg_total_len > limit || self->msg_total_len > SIZE_MAX - 2) { |
0 |
37 |
37 |
|
| 1671 |
1 |
| nats_emit_error(self, "server sent message exceeding max_payload"); |
| 1672 |
1 |
| nats_cleanup(self); |
| 1673 |
1 |
| return; |
| 1674 |
|
| } |
| 1675 |
37 |
| self->parse_state = PARSE_MSG_BODY; |
| 1676 |
|
| } |
| 1677 |
|
| /* Unparseable frame: drop it and stay in PARSE_OP. The sender is |
| 1678 |
|
| trusted; killing the connection over one corrupt frame turns a |
| 1679 |
|
| glitch into an outage, and reconnect defaults off. */ |
| 1680 |
41 |
| return; |
| 1681 |
|
| } |
| 1682 |
|
| |
| 1683 |
|
| /* HMSG */ |
| 1684 |
53 |
▶ | if (len >= 5 && line[0] == 'H' && line[1] == 'M' && line[2] == 'S' && line[3] == 'G' && line[4] == ' ') { |
Branch: 6 branches
| Branch | True | False | Total |
| if (len >= 5 && line[0] == 'H' && line[1] == 'M' && line[2] == 'S' && line[3] == 'G' && line[4] == ' ') { |
6 |
47 |
53 |
| if (len >= 5 && line[0] == 'H' && line[1] == 'M' && line[2] == 'S' && line[3] == 'G' && line[4] == ' ') { |
4 |
2 |
6 |
| if (len >= 5 && line[0] == 'H' && line[1] == 'M' && line[2] == 'S' && line[3] == 'G' && line[4] == ' ') { |
4 |
0 |
4 |
| if (len >= 5 && line[0] == 'H' && line[1] == 'M' && line[2] == 'S' && line[3] == 'G' && line[4] == ' ') { |
4 |
0 |
4 |
| if (len >= 5 && line[0] == 'H' && line[1] == 'M' && line[2] == 'S' && line[3] == 'G' && line[4] == ' ') { |
4 |
0 |
4 |
| if (len >= 5 && line[0] == 'H' && line[1] == 'M' && line[2] == 'S' && line[3] == 'G' && line[4] == ' ') { |
4 |
0 |
4 |
|
| 1685 |
4 |
▶ | if (nats_parse_hmsg_args(self, line, len) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_parse_hmsg_args(self, line, len) == 0) { |
4 |
0 |
4 |
|
| 1686 |
12 |
| size_t limit = self->max_payload > 0 ? (size_t)self->max_payload |
| 1687 |
4 |
▶ | : (size_t)DEFAULT_MAX_PAYLOAD; |
Branch: 1 branch
| Branch | True | False | Total |
| : (size_t)DEFAULT_MAX_PAYLOAD; |
4 |
0 |
4 |
|
| 1688 |
4 |
▶ | if (self->msg_total_len > limit || self->msg_total_len > SIZE_MAX - 2) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->msg_total_len > limit || self->msg_total_len > SIZE_MAX - 2) { |
4 |
0 |
4 |
| if (self->msg_total_len > limit || self->msg_total_len > SIZE_MAX - 2) { |
0 |
4 |
4 |
|
| 1689 |
0 |
| nats_emit_error(self, "server sent message exceeding max_payload"); |
| 1690 |
0 |
| nats_cleanup(self); |
| 1691 |
0 |
| return; |
| 1692 |
|
| } |
| 1693 |
4 |
| self->parse_state = PARSE_MSG_BODY; |
| 1694 |
|
| } |
| 1695 |
4 |
| return; |
| 1696 |
|
| } |
| 1697 |
|
| |
| 1698 |
|
| /* PING */ |
| 1699 |
49 |
▶ | if (len == 4 && line[0] == 'P' && line[1] == 'I' && line[2] == 'N' && line[3] == 'G') { |
Branch: 5 branches
| Branch | True | False | Total |
| if (len == 4 && line[0] == 'P' && line[1] == 'I' && line[2] == 'N' && line[3] == 'G') { |
44 |
5 |
49 |
| if (len == 4 && line[0] == 'P' && line[1] == 'I' && line[2] == 'N' && line[3] == 'G') { |
44 |
0 |
44 |
| if (len == 4 && line[0] == 'P' && line[1] == 'I' && line[2] == 'N' && line[3] == 'G') { |
1 |
43 |
44 |
| if (len == 4 && line[0] == 'P' && line[1] == 'I' && line[2] == 'N' && line[3] == 'G') { |
1 |
0 |
1 |
| if (len == 4 && line[0] == 'P' && line[1] == 'I' && line[2] == 'N' && line[3] == 'G') { |
1 |
0 |
1 |
|
| 1700 |
1 |
| wbuf_append(self, "PONG\r\n", 6); |
| 1701 |
1 |
| nats_try_write(self); |
| 1702 |
1 |
| return; |
| 1703 |
|
| } |
| 1704 |
|
| |
| 1705 |
|
| /* PONG */ |
| 1706 |
48 |
▶ | if (len == 4 && line[0] == 'P' && line[1] == 'O' && line[2] == 'N' && line[3] == 'G') { |
Branch: 5 branches
| Branch | True | False | Total |
| if (len == 4 && line[0] == 'P' && line[1] == 'O' && line[2] == 'N' && line[3] == 'G') { |
43 |
5 |
48 |
| if (len == 4 && line[0] == 'P' && line[1] == 'O' && line[2] == 'N' && line[3] == 'G') { |
43 |
0 |
43 |
| if (len == 4 && line[0] == 'P' && line[1] == 'O' && line[2] == 'N' && line[3] == 'G') { |
43 |
0 |
43 |
| if (len == 4 && line[0] == 'P' && line[1] == 'O' && line[2] == 'N' && line[3] == 'G') { |
43 |
0 |
43 |
| if (len == 4 && line[0] == 'P' && line[1] == 'O' && line[2] == 'N' && line[3] == 'G') { |
43 |
0 |
43 |
|
| 1707 |
43 |
▶ | if (self->pings_outstanding > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->pings_outstanding > 0) |
2 |
41 |
43 |
|
| 1708 |
2 |
| self->pings_outstanding--; |
| 1709 |
|
| |
| 1710 |
43 |
▶ | if (self->connecting && !self->connected) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->connecting && !self->connected) { |
36 |
7 |
43 |
| if (self->connecting && !self->connected) { |
36 |
0 |
36 |
|
| 1711 |
36 |
| self->connecting = 0; |
| 1712 |
36 |
| self->connected = 1; |
| 1713 |
36 |
| self->reconnect_attempts = 0; |
| 1714 |
|
| /* connect succeeded: drop the saved list; reconnect re-resolves */ |
| 1715 |
36 |
| nats_free_addrinfo(self); |
| 1716 |
|
| |
| 1717 |
36 |
▶ | if (self->connect_timer_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connect_timer_active) { |
0 |
36 |
36 |
|
| 1718 |
0 |
| ev_timer_stop(self->loop, &self->connect_timer); |
| 1719 |
0 |
| self->connect_timer_active = 0; |
| 1720 |
|
| } |
| 1721 |
|
| |
| 1722 |
36 |
| nats_start_ping_timer(self); |
| 1723 |
|
| |
| 1724 |
|
| /* Drain the outage backlog first: publishes from on_connect |
| 1725 |
|
| must queue behind it, not jump ahead of it. */ |
| 1726 |
36 |
| nats_drain_waiting(self); |
| 1727 |
|
| |
| 1728 |
36 |
▶ | if (self->connected && self->on_connect) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->connected && self->on_connect) { |
36 |
0 |
36 |
| if (self->connected && self->on_connect) { |
32 |
4 |
36 |
|
| 1729 |
32 |
| dSP; |
| 1730 |
32 |
| ENTER; SAVETMPS; |
| 1731 |
32 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
32 |
32 |
|
| 1732 |
32 |
| PUTBACK; |
| 1733 |
32 |
| PINNED_CALL_SV(self->on_connect, G_DISCARD); |
| 1734 |
32 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
32 |
32 |
|
| 1735 |
|
| } |
| 1736 |
|
| } |
| 1737 |
|
| |
| 1738 |
|
| /* One PONG consumes exactly one queue entry (one per PING sent) */ |
| 1739 |
43 |
▶ | if (!ngx_queue_empty(&self->pong_cbs)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ngx_queue_empty(&self->pong_cbs)) { |
42 |
1 |
43 |
|
| 1740 |
42 |
| ngx_queue_t *pq = ngx_queue_head(&self->pong_cbs); |
| 1741 |
42 |
| nats_pong_cb_t *pcb = ngx_queue_data(pq, nats_pong_cb_t, queue); |
| 1742 |
42 |
| ngx_queue_remove(pq); |
| 1743 |
42 |
▶ | if (pcb->kind == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcb->kind == 2) { |
36 |
6 |
42 |
|
| 1744 |
36 |
| Safefree(pcb); |
| 1745 |
6 |
▶ | } else if (pcb->kind == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (pcb->kind == 1) { |
2 |
4 |
6 |
|
| 1746 |
|
| /* Drain marker: fire drain callback and disconnect */ |
| 1747 |
2 |
| self->draining = 0; |
| 1748 |
2 |
▶ | if (self->drain_cb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->drain_cb) { |
2 |
0 |
2 |
|
| 1749 |
2 |
| SV *dcb = self->drain_cb; |
| 1750 |
2 |
| self->drain_cb = NULL; |
| 1751 |
2 |
| dSP; |
| 1752 |
2 |
| ENTER; SAVETMPS; |
| 1753 |
2 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
2 |
2 |
|
| 1754 |
2 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
2 |
2 |
|
| 1755 |
2 |
| PUSHs(&PL_sv_undef); /* success: no error */ |
| 1756 |
2 |
| PUTBACK; |
| 1757 |
2 |
| PINNED_CALL_SV(dcb, G_DISCARD); |
| 1758 |
2 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
2 |
2 |
|
| 1759 |
2 |
| SvREFCNT_dec(dcb); |
| 1760 |
|
| } |
| 1761 |
2 |
| Safefree(pcb); |
| 1762 |
2 |
| self->intentional_disconnect = 1; |
| 1763 |
2 |
| nats_cleanup(self); |
| 1764 |
2 |
| return; |
| 1765 |
|
| } else { |
| 1766 |
4 |
▶ | if (pcb->cb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcb->cb) { |
3 |
1 |
4 |
|
| 1767 |
3 |
| dSP; |
| 1768 |
3 |
| ENTER; SAVETMPS; |
| 1769 |
3 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
3 |
3 |
|
| 1770 |
3 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
3 |
3 |
|
| 1771 |
3 |
| PUSHs(&PL_sv_undef); /* success: no error */ |
| 1772 |
3 |
| PUTBACK; |
| 1773 |
3 |
| call_sv(pcb->cb, G_DISCARD); |
| 1774 |
3 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
3 |
3 |
|
| 1775 |
3 |
| SvREFCNT_dec(pcb->cb); |
| 1776 |
|
| } |
| 1777 |
4 |
| Safefree(pcb); |
| 1778 |
|
| } |
| 1779 |
|
| } |
| 1780 |
|
| |
| 1781 |
41 |
| return; |
| 1782 |
|
| } |
| 1783 |
|
| |
| 1784 |
|
| /* +OK */ |
| 1785 |
5 |
▶ | if (len >= 3 && line[0] == '+' && line[1] == 'O' && line[2] == 'K') { |
Branch: 4 branches
| Branch | True | False | Total |
| if (len >= 3 && line[0] == '+' && line[1] == 'O' && line[2] == 'K') { |
5 |
0 |
5 |
| if (len >= 3 && line[0] == '+' && line[1] == 'O' && line[2] == 'K') { |
1 |
4 |
5 |
| if (len >= 3 && line[0] == '+' && line[1] == 'O' && line[2] == 'K') { |
1 |
0 |
1 |
| if (len >= 3 && line[0] == '+' && line[1] == 'O' && line[2] == 'K') { |
1 |
0 |
1 |
|
| 1786 |
1 |
| return; |
| 1787 |
|
| } |
| 1788 |
|
| |
| 1789 |
|
| /* -ERR */ |
| 1790 |
4 |
▶ | if (len >= 4 && line[0] == '-' && line[1] == 'E' && line[2] == 'R' && line[3] == 'R') { |
Branch: 5 branches
| Branch | True | False | Total |
| if (len >= 4 && line[0] == '-' && line[1] == 'E' && line[2] == 'R' && line[3] == 'R') { |
2 |
2 |
4 |
| if (len >= 4 && line[0] == '-' && line[1] == 'E' && line[2] == 'R' && line[3] == 'R') { |
0 |
2 |
2 |
| if (len >= 4 && line[0] == '-' && line[1] == 'E' && line[2] == 'R' && line[3] == 'R') { |
0 |
0 |
0 |
| if (len >= 4 && line[0] == '-' && line[1] == 'E' && line[2] == 'R' && line[3] == 'R') { |
0 |
0 |
0 |
| if (len >= 4 && line[0] == '-' && line[1] == 'E' && line[2] == 'R' && line[3] == 'R') { |
0 |
0 |
0 |
|
| 1791 |
0 |
| char *msg = line + 4; |
| 1792 |
0 |
| size_t mlen = len - 4; |
| 1793 |
0 |
▶ | while (mlen > 0 && (*msg == ' ' || *msg == '\'')) { msg++; mlen--; } |
Branch: 3 branches
| Branch | True | False | Total |
| while (mlen > 0 && (*msg == ' ' || *msg == '\'')) { msg++; mlen--; } |
0 |
0 |
0 |
| while (mlen > 0 && (*msg == ' ' || *msg == '\'')) { msg++; mlen--; } |
0 |
0 |
0 |
| while (mlen > 0 && (*msg == ' ' || *msg == '\'')) { msg++; mlen--; } |
0 |
0 |
0 |
|
| 1794 |
0 |
▶ | while (mlen > 0 && msg[mlen-1] == '\'') mlen--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (mlen > 0 && msg[mlen-1] == '\'') mlen--; |
0 |
0 |
0 |
| while (mlen > 0 && msg[mlen-1] == '\'') mlen--; |
0 |
0 |
0 |
|
| 1795 |
|
| |
| 1796 |
|
| char errbuf[512]; |
| 1797 |
0 |
| snprintf(errbuf, sizeof(errbuf), "%.*s", (int)mlen, msg); |
| 1798 |
|
| |
| 1799 |
|
| /* nats-server capitalizes these; strcasestr is not portable, so |
| 1800 |
|
| lowercase a bounded copy. Auth errors are fatal: no reconnect. */ |
| 1801 |
|
| { |
| 1802 |
|
| char lc[512]; |
| 1803 |
0 |
| size_t i, n = strlen(errbuf); |
| 1804 |
0 |
▶ | for (i = 0; i < n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) |
0 |
0 |
0 |
|
| 1805 |
0 |
▶ | lc[i] = (errbuf[i] >= 'A' && errbuf[i] <= 'Z') |
Branch: 2 branches
| Branch | True | False | Total |
| lc[i] = (errbuf[i] >= 'A' && errbuf[i] <= 'Z') |
0 |
0 |
0 |
| lc[i] = (errbuf[i] >= 'A' && errbuf[i] <= 'Z') |
0 |
0 |
0 |
|
| 1806 |
0 |
| ? errbuf[i] + ('a' - 'A') : errbuf[i]; |
| 1807 |
0 |
| lc[n] = '\0'; |
| 1808 |
0 |
▶ | if (strncmp(lc, "permissions violation", 21) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp(lc, "permissions violation", 21) == 0) { |
0 |
0 |
0 |
|
| 1809 |
0 |
| nats_emit_error(self, errbuf); |
| 1810 |
0 |
| return; |
| 1811 |
|
| } |
| 1812 |
0 |
▶ | if (strstr(lc, "authorization") || strstr(lc, "authentication")) |
Branch: 2 branches
| Branch | True | False | Total |
| if (strstr(lc, "authorization") || strstr(lc, "authentication")) |
0 |
0 |
0 |
| if (strstr(lc, "authorization") || strstr(lc, "authentication")) |
0 |
0 |
0 |
|
| 1813 |
0 |
| self->intentional_disconnect = 1; |
| 1814 |
|
| } |
| 1815 |
0 |
| nats_emit_error(self, errbuf); |
| 1816 |
0 |
| nats_cleanup(self); |
| 1817 |
0 |
| return; |
| 1818 |
|
| } |
| 1819 |
|
| } |
| 1820 |
|
| |
| 1821 |
|
| /* ================================================================ |
| 1822 |
|
| * IO callbacks |
| 1823 |
|
| * ================================================================ */ |
| 1824 |
|
| |
| 1825 |
|
| /* Fail over to the next getaddrinfo candidate after an async connect failure. */ |
| 1826 |
35 |
| static int nats_failover_next_addr(nats_t *self) |
| 1827 |
|
| { |
| 1828 |
35 |
▶ | if (!self->ai_next) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->ai_next) return 0; |
35 |
0 |
35 |
|
| 1829 |
0 |
| nats_stop_watchers(self); |
| 1830 |
0 |
| close(self->fd); |
| 1831 |
0 |
| self->fd = -1; |
| 1832 |
0 |
| nats_connect_tcp(self); |
| 1833 |
0 |
| return 1; |
| 1834 |
|
| } |
| 1835 |
|
| |
| 1836 |
200 |
| static void nats_on_read_inner(nats_t *self) |
| 1837 |
|
| { |
| 1838 |
|
| |
| 1839 |
|
| #ifdef HAVE_OPENSSL |
| 1840 |
200 |
▶ | if (self->ssl_handshaking) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->ssl_handshaking) { |
0 |
200 |
200 |
|
| 1841 |
0 |
| int hret = nats_ssl_handshake(self); |
| 1842 |
0 |
▶ | if (hret == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hret == 0) return; |
0 |
0 |
0 |
|
| 1843 |
0 |
▶ | if (hret < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hret < 0) { |
0 |
0 |
0 |
|
| 1844 |
0 |
| nats_ssl_fail(self, "SSL handshake failed"); |
| 1845 |
0 |
| return; |
| 1846 |
|
| } |
| 1847 |
0 |
| self->ssl_handshaking = 0; |
| 1848 |
0 |
▶ | if (self->writing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->writing) { |
0 |
0 |
0 |
|
| 1849 |
0 |
| ev_io_stop(self->loop, &self->wio); |
| 1850 |
0 |
| self->writing = 0; |
| 1851 |
|
| } |
| 1852 |
|
| /* Handshake completed after the post-INFO upgrade. Send CONNECT |
| 1853 |
|
| over the now-encrypted channel. */ |
| 1854 |
0 |
▶ | if (self->connecting) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connecting) { |
0 |
0 |
0 |
|
| 1855 |
0 |
| nats_send_connect(self); |
| 1856 |
0 |
| nats_try_write(self); |
| 1857 |
|
| } |
| 1858 |
|
| } |
| 1859 |
|
| #endif |
| 1860 |
|
| |
| 1861 |
200 |
| buf_ensure(&self->rbuf, &self->rbuf_cap, self->rbuf_len + BUF_INIT_SIZE); |
| 1862 |
|
| |
| 1863 |
200 |
| ssize_t n = nats_io_read(self, self->rbuf + self->rbuf_len, |
| 1864 |
200 |
| self->rbuf_cap - self->rbuf_len); |
| 1865 |
|
| |
| 1866 |
200 |
▶ | if (n <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) { |
53 |
147 |
200 |
|
| 1867 |
53 |
▶ | if (n == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) { |
16 |
37 |
53 |
|
| 1868 |
16 |
| nats_cleanup(self); |
| 1869 |
37 |
▶ | } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) { |
37 |
0 |
37 |
| } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) { |
37 |
0 |
37 |
| } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) { |
37 |
0 |
37 |
|
| 1870 |
|
| /* a refused nonblocking connect can surface here as a read |
| 1871 |
|
| error, before nats_on_write's SO_ERROR check — fail over too */ |
| 1872 |
37 |
▶ | if (self->connect_pending && nats_failover_next_addr(self)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->connect_pending && nats_failover_next_addr(self)) |
35 |
2 |
37 |
| if (self->connect_pending && nats_failover_next_addr(self)) |
0 |
35 |
35 |
|
| 1873 |
0 |
| return; |
| 1874 |
37 |
| nats_emit_error(self, strerror(errno)); |
| 1875 |
37 |
| nats_cleanup(self); |
| 1876 |
|
| } |
| 1877 |
53 |
| return; |
| 1878 |
|
| } |
| 1879 |
|
| |
| 1880 |
147 |
| self->rbuf_len += n; |
| 1881 |
|
| |
| 1882 |
147 |
| size_t consumed = 0; |
| 1883 |
|
| |
| 1884 |
313 |
▶ | while (consumed < self->rbuf_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (consumed < self->rbuf_len) { |
189 |
124 |
313 |
|
| 1885 |
189 |
▶ | if (self->fd < 0) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->fd < 0) break; |
0 |
189 |
189 |
|
| 1886 |
|
| #ifdef HAVE_OPENSSL |
| 1887 |
|
| /* If processing INFO triggered a TLS upgrade, stop parsing rbuf as |
| 1888 |
|
| plaintext — subsequent bytes belong to the TLS handshake. */ |
| 1889 |
189 |
▶ | if (self->ssl_handshaking) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->ssl_handshaking) break; |
0 |
189 |
189 |
|
| 1890 |
|
| #endif |
| 1891 |
|
| |
| 1892 |
189 |
▶ | if (self->parse_state == PARSE_OP) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->parse_state == PARSE_OP) { |
155 |
34 |
189 |
|
| 1893 |
155 |
| char *start = self->rbuf + consumed; |
| 1894 |
155 |
| char *nl = (char *)memchr(start, '\n', self->rbuf_len - consumed); |
| 1895 |
155 |
▶ | if (!nl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nl) { |
16 |
139 |
155 |
|
| 1896 |
|
| /* a peer that never sends '\n' would otherwise grow rbuf |
| 1897 |
|
| without bound (MAX_CONTROL_LINE was outbound-only) */ |
| 1898 |
16 |
▶ | if (self->rbuf_len - consumed > MAX_CONTROL_LINE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->rbuf_len - consumed > MAX_CONTROL_LINE) { |
1 |
15 |
16 |
|
| 1899 |
1 |
| nats_emit_error(self, "maximum control line exceeded"); |
| 1900 |
1 |
| nats_cleanup(self); |
| 1901 |
1 |
| return; |
| 1902 |
|
| } |
| 1903 |
15 |
| break; |
| 1904 |
|
| } |
| 1905 |
|
| |
| 1906 |
139 |
| size_t line_off = consumed; |
| 1907 |
139 |
| size_t line_len = nl - start; |
| 1908 |
139 |
| nats_process_line(self, start, line_len); |
| 1909 |
139 |
| consumed += line_len + 1; |
| 1910 |
|
| |
| 1911 |
|
| /* body not fully arrived: subject/reply are offsets into this |
| 1912 |
|
| line, so roll back and re-parse the header once it has */ |
| 1913 |
139 |
▶ | if (self->fd >= 0 && self->parse_state == PARSE_MSG_BODY && |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->fd >= 0 && self->parse_state == PARSE_MSG_BODY && |
133 |
6 |
139 |
| if (self->fd >= 0 && self->parse_state == PARSE_MSG_BODY && |
41 |
92 |
133 |
|
| 1914 |
41 |
▶ | self->rbuf_len - consumed < self->msg_total_len + 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| self->rbuf_len - consumed < self->msg_total_len + 2) { |
7 |
34 |
41 |
|
| 1915 |
7 |
| consumed = line_off; |
| 1916 |
7 |
| self->parse_state = PARSE_OP; |
| 1917 |
7 |
| break; |
| 1918 |
|
| } |
| 1919 |
|
| |
| 1920 |
34 |
▶ | } else if (self->parse_state == PARSE_MSG_BODY) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->parse_state == PARSE_MSG_BODY) { |
34 |
0 |
34 |
|
| 1921 |
34 |
| size_t need = self->msg_total_len + 2; |
| 1922 |
34 |
| size_t avail = self->rbuf_len - consumed; |
| 1923 |
34 |
▶ | if (avail < need) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (avail < need) break; |
0 |
34 |
34 |
|
| 1924 |
|
| |
| 1925 |
34 |
| char *payload = self->rbuf + consumed; |
| 1926 |
|
| |
| 1927 |
|
| /* Dispatch inbox responses, skip normal sub delivery for inbox */ |
| 1928 |
38 |
▶ | if (self->inbox_sub_sid && self->msg_sid == self->inbox_sub_sid) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->inbox_sub_sid && self->msg_sid == self->inbox_sub_sid) { |
4 |
30 |
34 |
| if (self->inbox_sub_sid && self->msg_sid == self->inbox_sub_sid) { |
4 |
0 |
4 |
|
| 1929 |
4 |
| const char *hdrs = NULL; |
| 1930 |
4 |
| size_t hdrs_len = 0; |
| 1931 |
4 |
| const char *body = payload; |
| 1932 |
4 |
| size_t body_len = self->msg_total_len; |
| 1933 |
|
| |
| 1934 |
4 |
▶ | if (self->msg_type == MSG_TYPE_HMSG && self->msg_hdr_len > 0 && |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->msg_type == MSG_TYPE_HMSG && self->msg_hdr_len > 0 && |
0 |
4 |
4 |
| if (self->msg_type == MSG_TYPE_HMSG && self->msg_hdr_len > 0 && |
0 |
0 |
0 |
|
| 1935 |
0 |
▶ | self->msg_hdr_len <= self->msg_total_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| self->msg_hdr_len <= self->msg_total_len) { |
0 |
0 |
0 |
|
| 1936 |
0 |
| hdrs = payload; |
| 1937 |
0 |
| hdrs_len = self->msg_hdr_len; |
| 1938 |
0 |
| body = payload + self->msg_hdr_len; |
| 1939 |
0 |
| body_len = self->msg_total_len - self->msg_hdr_len; |
| 1940 |
|
| } |
| 1941 |
|
| |
| 1942 |
4 |
| self->msgs_in++; |
| 1943 |
4 |
| self->bytes_in += self->msg_total_len; |
| 1944 |
4 |
| nats_check_inbox_response(self, self->rbuf + self->msg_subject_off, self->msg_subject_len, |
| 1945 |
|
| body, body_len, hdrs, hdrs_len); |
| 1946 |
|
| } else { |
| 1947 |
30 |
| nats_process_msg(self, payload, self->msg_total_len); |
| 1948 |
|
| } |
| 1949 |
|
| |
| 1950 |
34 |
| consumed += need; |
| 1951 |
34 |
| self->parse_state = PARSE_OP; |
| 1952 |
|
| } |
| 1953 |
|
| } |
| 1954 |
|
| |
| 1955 |
146 |
▶ | if (self->fd >= 0 && consumed > 0 && consumed <= self->rbuf_len) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (self->fd >= 0 && consumed > 0 && consumed <= self->rbuf_len) { |
140 |
6 |
146 |
| if (self->fd >= 0 && consumed > 0 && consumed <= self->rbuf_len) { |
118 |
22 |
140 |
| if (self->fd >= 0 && consumed > 0 && consumed <= self->rbuf_len) { |
118 |
0 |
118 |
|
| 1956 |
118 |
| self->rbuf_len -= consumed; |
| 1957 |
118 |
▶ | if (self->rbuf_len > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->rbuf_len > 0) |
0 |
118 |
118 |
|
| 1958 |
0 |
| memmove(self->rbuf, self->rbuf + consumed, self->rbuf_len); |
| 1959 |
|
| } |
| 1960 |
|
| } |
| 1961 |
|
| |
| 1962 |
|
| /* cb_depth bracket: DESTROY during the dispatch only sets destroy_pending |
| 1963 |
|
| and the free runs here. nats_destroy_now frees self - touch nothing after. */ |
| 1964 |
200 |
| static void nats_on_read(struct ev_loop *loop, ev_io *w, int revents) |
| 1965 |
|
| { |
| 1966 |
200 |
| nats_t *self = (nats_t *)((char *)w - offsetof(nats_t, rio)); |
| 1967 |
|
| (void)loop; (void)revents; |
| 1968 |
200 |
| self->cb_depth++; |
| 1969 |
200 |
| SV *owner_pin = self->owner_sv; |
| 1970 |
200 |
▶ | if (owner_pin) SvREFCNT_inc(owner_pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (owner_pin) SvREFCNT_inc(owner_pin); |
200 |
0 |
200 |
|
| 1971 |
200 |
| nats_on_read_inner(self); |
| 1972 |
200 |
▶ | if (owner_pin) SvREFCNT_dec(owner_pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (owner_pin) SvREFCNT_dec(owner_pin); |
200 |
0 |
200 |
|
| 1973 |
200 |
▶ | if (--self->cb_depth == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (--self->cb_depth == 0) { |
200 |
0 |
200 |
|
| 1974 |
200 |
▶ | if (self->destroy_pending) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->destroy_pending) |
4 |
196 |
200 |
|
| 1975 |
4 |
| nats_destroy_now(self); |
| 1976 |
196 |
▶ | else if (self->pending_croak) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (self->pending_croak) |
0 |
196 |
196 |
|
| 1977 |
0 |
| nats_fire_pending_croak(self); |
| 1978 |
|
| } |
| 1979 |
200 |
| } |
| 1980 |
|
| |
| 1981 |
117 |
| static void nats_try_write(nats_t *self) |
| 1982 |
|
| { |
| 1983 |
117 |
▶ | if (self->fd < 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->fd < 0) return; |
1 |
116 |
117 |
|
| 1984 |
|
| |
| 1985 |
192 |
▶ | while (self->wbuf_off < self->wbuf_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (self->wbuf_off < self->wbuf_len) { |
78 |
114 |
192 |
|
| 1986 |
78 |
| ssize_t n = nats_io_write(self, self->wbuf + self->wbuf_off, |
| 1987 |
78 |
| self->wbuf_len - self->wbuf_off); |
| 1988 |
78 |
▶ | if (n <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) { |
2 |
76 |
78 |
|
| 1989 |
2 |
▶ | if (n == 0 || errno == EAGAIN || errno == EWOULDBLOCK) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (n == 0 || errno == EAGAIN || errno == EWOULDBLOCK) { |
2 |
0 |
2 |
| if (n == 0 || errno == EAGAIN || errno == EWOULDBLOCK) { |
2 |
0 |
2 |
| if (n == 0 || errno == EAGAIN || errno == EWOULDBLOCK) { |
0 |
2 |
2 |
|
| 1990 |
0 |
▶ | if (!self->writing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->writing) { |
0 |
0 |
0 |
|
| 1991 |
0 |
| ev_io_start(self->loop, &self->wio); |
| 1992 |
0 |
| self->writing = 1; |
| 1993 |
|
| } |
| 1994 |
0 |
| return; |
| 1995 |
|
| } |
| 1996 |
2 |
▶ | if (errno == EINTR) |
Branch: 1 branch
| Branch | True | False | Total |
| if (errno == EINTR) |
0 |
2 |
2 |
|
| 1997 |
0 |
| continue; |
| 1998 |
2 |
| self->slow_fired = 0; |
| 1999 |
2 |
▶ | if (self->owner_sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->owner_sv) |
2 |
0 |
2 |
|
| 2000 |
2 |
| sv_2mortal(SvREFCNT_inc(self->owner_sv)); |
| 2001 |
2 |
| nats_emit_error(self, strerror(errno)); |
| 2002 |
2 |
| nats_cleanup(self); |
| 2003 |
2 |
| return; |
| 2004 |
|
| } |
| 2005 |
76 |
| self->wbuf_off += n; |
| 2006 |
|
| } |
| 2007 |
|
| |
| 2008 |
114 |
| self->wbuf_off = 0; |
| 2009 |
114 |
| self->wbuf_len = 0; |
| 2010 |
114 |
| self->slow_fired = 0; /* buffer drained: re-arm slow-consumer edge */ |
| 2011 |
|
| |
| 2012 |
114 |
▶ | if (self->writing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->writing) { |
39 |
75 |
114 |
|
| 2013 |
39 |
| ev_io_stop(self->loop, &self->wio); |
| 2014 |
39 |
| self->writing = 0; |
| 2015 |
|
| } |
| 2016 |
|
| } |
| 2017 |
|
| |
| 2018 |
38 |
| static void nats_on_write_inner(nats_t *self) |
| 2019 |
|
| { |
| 2020 |
38 |
▶ | if (self->connect_pending) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connect_pending) { |
38 |
0 |
38 |
|
| 2021 |
38 |
| int err = 0; |
| 2022 |
38 |
| socklen_t errlen = sizeof(err); |
| 2023 |
38 |
| getsockopt(self->fd, SOL_SOCKET, SO_ERROR, &err, &errlen); |
| 2024 |
38 |
▶ | if (err) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) { |
0 |
38 |
38 |
|
| 2025 |
0 |
▶ | if (nats_failover_next_addr(self)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_failover_next_addr(self)) |
0 |
0 |
0 |
|
| 2026 |
0 |
| return; |
| 2027 |
0 |
| nats_emit_error(self, strerror(err)); |
| 2028 |
0 |
| nats_cleanup(self); |
| 2029 |
0 |
| return; |
| 2030 |
|
| } |
| 2031 |
|
| /* TCP connect complete. Fall through: wbuf may already hold part |
| 2032 |
|
| of the CONNECT flush; nats_try_write disarms wio once empty. */ |
| 2033 |
38 |
| self->connect_pending = 0; |
| 2034 |
|
| } |
| 2035 |
|
| |
| 2036 |
|
| #ifdef HAVE_OPENSSL |
| 2037 |
|
| /* resume a TLS handshake stalled on SSL_ERROR_WANT_WRITE */ |
| 2038 |
38 |
▶ | if (self->ssl_handshaking) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->ssl_handshaking) { |
0 |
38 |
38 |
|
| 2039 |
0 |
| int hret = nats_ssl_handshake(self); |
| 2040 |
0 |
▶ | if (hret < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hret < 0) { |
0 |
0 |
0 |
|
| 2041 |
0 |
| nats_ssl_fail(self, "SSL handshake failed"); |
| 2042 |
0 |
| return; |
| 2043 |
|
| } |
| 2044 |
0 |
▶ | if (hret == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hret == 0) return; |
0 |
0 |
0 |
|
| 2045 |
0 |
| self->ssl_handshaking = 0; |
| 2046 |
0 |
▶ | if (self->connecting) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connecting) |
0 |
0 |
0 |
|
| 2047 |
0 |
| nats_send_connect(self); |
| 2048 |
|
| } |
| 2049 |
|
| #endif |
| 2050 |
|
| |
| 2051 |
38 |
| nats_try_write(self); |
| 2052 |
|
| } |
| 2053 |
|
| |
| 2054 |
38 |
| static void nats_on_write(struct ev_loop *loop, ev_io *w, int revents) |
| 2055 |
|
| { |
| 2056 |
38 |
| nats_t *self = (nats_t *)((char *)w - offsetof(nats_t, wio)); |
| 2057 |
|
| (void)loop; (void)revents; |
| 2058 |
38 |
| self->cb_depth++; |
| 2059 |
38 |
| SV *owner_pin = self->owner_sv; |
| 2060 |
38 |
▶ | if (owner_pin) SvREFCNT_inc(owner_pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (owner_pin) SvREFCNT_inc(owner_pin); |
38 |
0 |
38 |
|
| 2061 |
38 |
| nats_on_write_inner(self); |
| 2062 |
38 |
▶ | if (owner_pin) SvREFCNT_dec(owner_pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (owner_pin) SvREFCNT_dec(owner_pin); |
38 |
0 |
38 |
|
| 2063 |
38 |
▶ | if (--self->cb_depth == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (--self->cb_depth == 0) { |
38 |
0 |
38 |
|
| 2064 |
38 |
▶ | if (self->destroy_pending) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->destroy_pending) |
0 |
38 |
38 |
|
| 2065 |
0 |
| nats_destroy_now(self); |
| 2066 |
38 |
▶ | else if (self->pending_croak) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (self->pending_croak) |
0 |
38 |
38 |
|
| 2067 |
0 |
| nats_fire_pending_croak(self); |
| 2068 |
|
| } |
| 2069 |
38 |
| } |
| 2070 |
|
| |
| 2071 |
|
| /* ================================================================ |
| 2072 |
|
| * Timer callbacks |
| 2073 |
|
| * ================================================================ */ |
| 2074 |
|
| |
| 2075 |
0 |
| static void nats_on_connect_timeout_inner(nats_t *self) |
| 2076 |
|
| { |
| 2077 |
0 |
| self->connect_timer_active = 0; |
| 2078 |
0 |
| nats_emit_error(self, "connect timeout"); |
| 2079 |
0 |
| nats_cleanup(self); |
| 2080 |
0 |
| } |
| 2081 |
|
| |
| 2082 |
0 |
| static void nats_on_connect_timeout(struct ev_loop *loop, ev_timer *w, int revents) |
| 2083 |
|
| { |
| 2084 |
0 |
| nats_t *self = (nats_t *)((char *)w - offsetof(nats_t, connect_timer)); |
| 2085 |
|
| (void)loop; (void)revents; |
| 2086 |
0 |
| self->cb_depth++; |
| 2087 |
0 |
| SV *owner_pin = self->owner_sv; |
| 2088 |
0 |
▶ | if (owner_pin) SvREFCNT_inc(owner_pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (owner_pin) SvREFCNT_inc(owner_pin); |
0 |
0 |
0 |
|
| 2089 |
0 |
| nats_on_connect_timeout_inner(self); |
| 2090 |
0 |
▶ | if (owner_pin) SvREFCNT_dec(owner_pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (owner_pin) SvREFCNT_dec(owner_pin); |
0 |
0 |
0 |
|
| 2091 |
0 |
▶ | if (--self->cb_depth == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (--self->cb_depth == 0) { |
0 |
0 |
0 |
|
| 2092 |
0 |
▶ | if (self->destroy_pending) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->destroy_pending) |
0 |
0 |
0 |
|
| 2093 |
0 |
| nats_destroy_now(self); |
| 2094 |
0 |
▶ | else if (self->pending_croak) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (self->pending_croak) |
0 |
0 |
0 |
|
| 2095 |
0 |
| nats_fire_pending_croak(self); |
| 2096 |
|
| } |
| 2097 |
0 |
| } |
| 2098 |
|
| |
| 2099 |
34 |
| static void nats_on_reconnect_timer_inner(nats_t *self) |
| 2100 |
|
| { |
| 2101 |
34 |
| self->reconnect_timer_active = 0; |
| 2102 |
34 |
| self->reconnect_attempts++; |
| 2103 |
34 |
| self->intentional_disconnect = 0; |
| 2104 |
34 |
▶ | if (self->server_pool_count > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->server_pool_count > 0) |
19 |
15 |
34 |
|
| 2105 |
19 |
| nats_next_server(self); |
| 2106 |
34 |
| nats_do_connect(self); |
| 2107 |
34 |
| } |
| 2108 |
|
| |
| 2109 |
34 |
| static void nats_on_reconnect_timer(struct ev_loop *loop, ev_timer *w, int revents) |
| 2110 |
|
| { |
| 2111 |
34 |
| nats_t *self = (nats_t *)((char *)w - offsetof(nats_t, reconnect_timer)); |
| 2112 |
|
| (void)loop; (void)revents; |
| 2113 |
34 |
| self->cb_depth++; |
| 2114 |
34 |
| SV *owner_pin = self->owner_sv; |
| 2115 |
34 |
▶ | if (owner_pin) SvREFCNT_inc(owner_pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (owner_pin) SvREFCNT_inc(owner_pin); |
34 |
0 |
34 |
|
| 2116 |
34 |
| nats_on_reconnect_timer_inner(self); |
| 2117 |
34 |
▶ | if (owner_pin) SvREFCNT_dec(owner_pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (owner_pin) SvREFCNT_dec(owner_pin); |
34 |
0 |
34 |
|
| 2118 |
34 |
▶ | if (--self->cb_depth == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (--self->cb_depth == 0) { |
34 |
0 |
34 |
|
| 2119 |
34 |
▶ | if (self->destroy_pending) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->destroy_pending) |
0 |
34 |
34 |
|
| 2120 |
0 |
| nats_destroy_now(self); |
| 2121 |
34 |
▶ | else if (self->pending_croak) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (self->pending_croak) |
0 |
34 |
34 |
|
| 2122 |
0 |
| nats_fire_pending_croak(self); |
| 2123 |
|
| } |
| 2124 |
34 |
| } |
| 2125 |
|
| |
| 2126 |
38 |
| static void nats_start_ping_timer(nats_t *self) |
| 2127 |
|
| { |
| 2128 |
38 |
▶ | if (self->ping_interval_ms <= 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->ping_interval_ms <= 0) return; |
1 |
37 |
38 |
|
| 2129 |
37 |
| double interval = self->ping_interval_ms / 1000.0; |
| 2130 |
37 |
| ev_timer_set(&self->ping_timer, interval, interval); |
| 2131 |
37 |
| ev_timer_start(self->loop, &self->ping_timer); |
| 2132 |
37 |
| self->ping_timer_active = 1; |
| 2133 |
|
| } |
| 2134 |
|
| |
| 2135 |
12 |
| static void nats_on_ping_timer_inner(nats_t *self) |
| 2136 |
|
| { |
| 2137 |
12 |
| self->pings_outstanding++; |
| 2138 |
12 |
▶ | if (self->max_pings_outstanding > 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->max_pings_outstanding > 0 && |
12 |
0 |
12 |
|
| 2139 |
12 |
▶ | self->pings_outstanding > self->max_pings_outstanding) { |
Branch: 1 branch
| Branch | True | False | Total |
| self->pings_outstanding > self->max_pings_outstanding) { |
0 |
12 |
12 |
|
| 2140 |
0 |
| nats_emit_error(self, "stale connection"); |
| 2141 |
0 |
| nats_cleanup(self); |
| 2142 |
0 |
| return; |
| 2143 |
|
| } |
| 2144 |
|
| |
| 2145 |
12 |
| wbuf_append(self, "PING\r\n", 6); |
| 2146 |
12 |
| nats_push_pong_marker(self); |
| 2147 |
12 |
| nats_try_write(self); |
| 2148 |
|
| } |
| 2149 |
|
| |
| 2150 |
12 |
| static void nats_on_ping_timer(struct ev_loop *loop, ev_timer *w, int revents) |
| 2151 |
|
| { |
| 2152 |
12 |
| nats_t *self = (nats_t *)((char *)w - offsetof(nats_t, ping_timer)); |
| 2153 |
|
| (void)loop; (void)revents; |
| 2154 |
12 |
| self->cb_depth++; |
| 2155 |
12 |
| SV *owner_pin = self->owner_sv; |
| 2156 |
12 |
▶ | if (owner_pin) SvREFCNT_inc(owner_pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (owner_pin) SvREFCNT_inc(owner_pin); |
12 |
0 |
12 |
|
| 2157 |
12 |
| nats_on_ping_timer_inner(self); |
| 2158 |
12 |
▶ | if (owner_pin) SvREFCNT_dec(owner_pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (owner_pin) SvREFCNT_dec(owner_pin); |
12 |
0 |
12 |
|
| 2159 |
12 |
▶ | if (--self->cb_depth == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (--self->cb_depth == 0) { |
12 |
0 |
12 |
|
| 2160 |
12 |
▶ | if (self->destroy_pending) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->destroy_pending) |
0 |
12 |
12 |
|
| 2161 |
0 |
| nats_destroy_now(self); |
| 2162 |
12 |
▶ | else if (self->pending_croak) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (self->pending_croak) |
0 |
12 |
12 |
|
| 2163 |
0 |
| nats_fire_pending_croak(self); |
| 2164 |
|
| } |
| 2165 |
12 |
| } |
| 2166 |
|
| |
| 2167 |
|
| /* ================================================================ |
| 2168 |
|
| * Write coalescing via ev_prepare |
| 2169 |
|
| * ================================================================ */ |
| 2170 |
|
| |
| 2171 |
17 |
| static void nats_on_prepare_inner(nats_t *self) |
| 2172 |
|
| { |
| 2173 |
17 |
▶ | if (self->wbuf_dirty && self->fd >= 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->wbuf_dirty && self->fd >= 0) { |
17 |
0 |
17 |
| if (self->wbuf_dirty && self->fd >= 0) { |
17 |
0 |
17 |
|
| 2174 |
17 |
| self->wbuf_dirty = 0; |
| 2175 |
17 |
| nats_try_write(self); |
| 2176 |
|
| } |
| 2177 |
|
| /* Arm-on-demand: every producer either try_writes directly or goes |
| 2178 |
|
| through nats_schedule_write, so an empty wbuf needs no watcher. */ |
| 2179 |
17 |
▶ | if (self->prepare_active && self->wbuf_off >= self->wbuf_len) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->prepare_active && self->wbuf_off >= self->wbuf_len) { |
17 |
0 |
17 |
| if (self->prepare_active && self->wbuf_off >= self->wbuf_len) { |
17 |
0 |
17 |
|
| 2180 |
17 |
| ev_prepare_stop(self->loop, &self->prepare_watcher); |
| 2181 |
17 |
| self->prepare_active = 0; |
| 2182 |
|
| } |
| 2183 |
17 |
| } |
| 2184 |
|
| |
| 2185 |
17 |
| static void nats_on_prepare(struct ev_loop *loop, ev_prepare *w, int revents) |
| 2186 |
|
| { |
| 2187 |
17 |
| nats_t *self = (nats_t *)((char *)w - offsetof(nats_t, prepare_watcher)); |
| 2188 |
|
| (void)loop; (void)revents; |
| 2189 |
17 |
| self->cb_depth++; |
| 2190 |
17 |
| SV *owner_pin = self->owner_sv; |
| 2191 |
17 |
▶ | if (owner_pin) SvREFCNT_inc(owner_pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (owner_pin) SvREFCNT_inc(owner_pin); |
17 |
0 |
17 |
|
| 2192 |
17 |
| nats_on_prepare_inner(self); |
| 2193 |
17 |
▶ | if (owner_pin) SvREFCNT_dec(owner_pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (owner_pin) SvREFCNT_dec(owner_pin); |
17 |
0 |
17 |
|
| 2194 |
17 |
▶ | if (--self->cb_depth == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (--self->cb_depth == 0) { |
17 |
0 |
17 |
|
| 2195 |
17 |
▶ | if (self->destroy_pending) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->destroy_pending) |
0 |
17 |
17 |
|
| 2196 |
0 |
| nats_destroy_now(self); |
| 2197 |
17 |
▶ | else if (self->pending_croak) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (self->pending_croak) |
0 |
17 |
17 |
|
| 2198 |
0 |
| nats_fire_pending_croak(self); |
| 2199 |
|
| } |
| 2200 |
17 |
| } |
| 2201 |
|
| |
| 2202 |
0 |
| static void nats_fire_slow_consumer(nats_t *self) |
| 2203 |
|
| { |
| 2204 |
0 |
| SV *pin = self->owner_sv; |
| 2205 |
0 |
▶ | if (pin) SvREFCNT_inc(pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pin) SvREFCNT_inc(pin); |
0 |
0 |
0 |
|
| 2206 |
0 |
| dSP; |
| 2207 |
0 |
| ENTER; SAVETMPS; |
| 2208 |
0 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
0 |
0 |
|
| 2209 |
0 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
0 |
0 |
|
| 2210 |
0 |
| PUSHs(sv_2mortal(newSVuv(self->wbuf_len - self->wbuf_off))); |
| 2211 |
0 |
| PUTBACK; |
| 2212 |
0 |
| PINNED_CALL_SV(self->on_slow_consumer, G_DISCARD); |
| 2213 |
0 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 2214 |
0 |
▶ | if (pin) SvREFCNT_dec(pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pin) SvREFCNT_dec(pin); |
0 |
0 |
0 |
|
| 2215 |
0 |
| } |
| 2216 |
|
| |
| 2217 |
|
| /* Edge-triggered slow-consumer check, shared by the write path and batch |
| 2218 |
|
| end: fires once when the buffer crosses the threshold and re-arms when |
| 2219 |
|
| a later call finds it drained back below. */ |
| 2220 |
|
| static void |
| 2221 |
99 |
| nats_slow_check(nats_t *self) |
| 2222 |
|
| { |
| 2223 |
99 |
▶ | if (self->slow_consumer_bytes == 0 || !self->on_slow_consumer) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->slow_consumer_bytes == 0 || !self->on_slow_consumer) |
0 |
99 |
99 |
| if (self->slow_consumer_bytes == 0 || !self->on_slow_consumer) |
0 |
0 |
0 |
|
| 2224 |
99 |
| return; |
| 2225 |
0 |
▶ | if ((self->wbuf_len - self->wbuf_off) > self->slow_consumer_bytes) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((self->wbuf_len - self->wbuf_off) > self->slow_consumer_bytes) { |
0 |
0 |
0 |
|
| 2226 |
0 |
▶ | if (!self->slow_fired) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->slow_fired) { |
0 |
0 |
0 |
|
| 2227 |
0 |
| self->slow_fired = 1; |
| 2228 |
0 |
| nats_fire_slow_consumer(self); |
| 2229 |
|
| } |
| 2230 |
|
| } else { |
| 2231 |
0 |
| self->slow_fired = 0; |
| 2232 |
|
| } |
| 2233 |
|
| } |
| 2234 |
|
| |
| 2235 |
99 |
| static void nats_schedule_write(nats_t *self) |
| 2236 |
|
| { |
| 2237 |
99 |
▶ | if (self->batch_mode) return; /* writes batched, flushed on batch end */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->batch_mode) return; /* writes batched, flushed on batch end */ |
0 |
99 |
99 |
|
| 2238 |
|
| |
| 2239 |
99 |
| self->wbuf_dirty = 1; |
| 2240 |
99 |
▶ | if (!self->prepare_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->prepare_active) { |
22 |
77 |
99 |
|
| 2241 |
22 |
| ev_prepare_start(self->loop, &self->prepare_watcher); |
| 2242 |
22 |
| self->prepare_active = 1; |
| 2243 |
|
| } |
| 2244 |
|
| |
| 2245 |
99 |
| nats_slow_check(self); |
| 2246 |
|
| } |
| 2247 |
|
| |
| 2248 |
|
| /* ================================================================ |
| 2249 |
|
| * Request timeout |
| 2250 |
|
| * ================================================================ */ |
| 2251 |
|
| |
| 2252 |
|
| /* Deliberately not cb_depth-guarded: it never dereferences nats_t - the |
| 2253 |
|
| req is already unlinked, so a connection-destroying callback is harmless. */ |
| 2254 |
1 |
| static void nats_on_request_timeout(struct ev_loop *loop, ev_timer *w, int revents) |
| 2255 |
|
| { |
| 2256 |
1 |
| nats_req_t *req = (nats_req_t *)((char *)w - offsetof(nats_req_t, timer)); |
| 2257 |
|
| (void)loop; (void)revents; |
| 2258 |
1 |
| req->timer_active = 0; |
| 2259 |
|
| |
| 2260 |
1 |
| ngx_queue_remove(&req->queue); |
| 2261 |
|
| |
| 2262 |
1 |
▶ | if (req->cb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (req->cb) { |
1 |
0 |
1 |
|
| 2263 |
1 |
| dSP; |
| 2264 |
1 |
| ENTER; SAVETMPS; |
| 2265 |
1 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
1 |
1 |
|
| 2266 |
1 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
1 |
1 |
|
| 2267 |
1 |
| PUSHs(&PL_sv_undef); |
| 2268 |
1 |
| PUSHs(sv_2mortal(newSVpvn("request timeout", 15))); |
| 2269 |
1 |
| PUTBACK; |
| 2270 |
1 |
| call_sv(req->cb, G_DISCARD); |
| 2271 |
1 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 2272 |
1 |
| SvREFCNT_dec(req->cb); |
| 2273 |
|
| } |
| 2274 |
1 |
| Safefree(req); |
| 2275 |
1 |
| } |
| 2276 |
|
| |
| 2277 |
|
| /* ================================================================ |
| 2278 |
|
| * Write queue (for buffering during connect/reconnect) |
| 2279 |
|
| * ================================================================ */ |
| 2280 |
|
| |
| 2281 |
36 |
| static void nats_drain_waiting(nats_t *self) |
| 2282 |
|
| { |
| 2283 |
36 |
▶ | while (!ngx_queue_empty(&self->wait_queue)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!ngx_queue_empty(&self->wait_queue)) { |
0 |
36 |
36 |
|
| 2284 |
0 |
| ngx_queue_t *q = ngx_queue_head(&self->wait_queue); |
| 2285 |
0 |
| nats_pub_t *pub = ngx_queue_data(q, nats_pub_t, queue); |
| 2286 |
0 |
| ngx_queue_remove(q); |
| 2287 |
0 |
| self->waiting_count--; |
| 2288 |
|
| |
| 2289 |
0 |
| wbuf_append(self, pub->data, pub->len); |
| 2290 |
0 |
| Safefree(pub->data); |
| 2291 |
0 |
| Safefree(pub); |
| 2292 |
|
| } |
| 2293 |
36 |
| self->wait_bytes = 0; |
| 2294 |
|
| |
| 2295 |
36 |
▶ | if (self->wbuf_len > self->wbuf_off) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->wbuf_len > self->wbuf_off) |
0 |
36 |
36 |
|
| 2296 |
0 |
| nats_try_write(self); |
| 2297 |
36 |
| } |
| 2298 |
|
| |
| 2299 |
|
| /* Protocol fields are space-delimited: a blank or control byte silently |
| 2300 |
|
| reinterprets the frame (truncated subject, spoofed reply-to), so every |
| 2301 |
|
| caller-supplied subject, reply-to and queue group goes through here. */ |
| 2302 |
|
| static int |
| 2303 |
142 |
| nats_field_ok(const char *p, size_t len) |
| 2304 |
|
| { |
| 2305 |
|
| size_t i; |
| 2306 |
1430 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
1294 |
136 |
1430 |
|
| 2307 |
1294 |
| unsigned char c = (unsigned char)p[i]; |
| 2308 |
1294 |
▶ | if (c <= 0x20 || c == 0x7f) |
Branch: 2 branches
| Branch | True | False | Total |
| if (c <= 0x20 || c == 0x7f) |
1288 |
6 |
1294 |
| if (c <= 0x20 || c == 0x7f) |
0 |
1288 |
1288 |
|
| 2309 |
6 |
| return 0; |
| 2310 |
|
| } |
| 2311 |
136 |
| return 1; |
| 2312 |
|
| } |
| 2313 |
|
| |
| 2314 |
|
| /* Check before allocating queued writes so a cap refusal strands nothing. |
| 2315 |
|
| Connected writes bypass the cap. */ |
| 2316 |
159 |
| static int nats_queue_cap_ok(nats_t *self, size_t len) |
| 2317 |
|
| { |
| 2318 |
159 |
▶ | if (self->connected || self->reconnect_buf_size == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->connected || self->reconnect_buf_size == 0) |
70 |
89 |
159 |
| if (self->connected || self->reconnect_buf_size == 0) |
0 |
70 |
70 |
|
| 2319 |
89 |
| return 1; |
| 2320 |
70 |
| return self->wait_bytes <= self->reconnect_buf_size |
| 2321 |
70 |
▶ | && len <= self->reconnect_buf_size - self->wait_bytes; |
Branch: 2 branches
| Branch | True | False | Total |
| && len <= self->reconnect_buf_size - self->wait_bytes; |
66 |
4 |
70 |
| && len <= self->reconnect_buf_size - self->wait_bytes; |
51 |
15 |
66 |
|
| 2322 |
|
| } |
| 2323 |
|
| |
| 2324 |
73 |
| static void nats_queue_write(nats_t *self, const char *data, size_t len) |
| 2325 |
|
| { |
| 2326 |
73 |
▶ | if (self->connected) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connected) { |
38 |
35 |
73 |
|
| 2327 |
38 |
| wbuf_append(self, data, len); |
| 2328 |
38 |
| nats_schedule_write(self); |
| 2329 |
35 |
▶ | } else if (self->connecting || |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->connecting || |
5 |
30 |
35 |
|
| 2330 |
5 |
▶ | (self->reconnect_enabled && self->reconnect_timer_active)) { |
Branch: 2 branches
| Branch | True | False | Total |
| (self->reconnect_enabled && self->reconnect_timer_active)) { |
5 |
0 |
5 |
| (self->reconnect_enabled && self->reconnect_timer_active)) { |
5 |
0 |
5 |
|
| 2331 |
|
| /* Cap the outage backlog: without it a hot publisher during a long |
| 2332 |
|
| partition queues without bound. */ |
| 2333 |
35 |
▶ | if (!nats_queue_cap_ok(self, len)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nats_queue_cap_ok(self, len)) |
7 |
28 |
35 |
|
| 2334 |
7 |
| croak("reconnect buffer full (%" UVuf " byte limit)", |
| 2335 |
|
| (UV)self->reconnect_buf_size); |
| 2336 |
|
| nats_pub_t *pub; |
| 2337 |
28 |
| Newxz(pub, 1, nats_pub_t); |
| 2338 |
28 |
| Newx(pub->data, len, char); |
| 2339 |
28 |
| memcpy(pub->data, data, len); |
| 2340 |
28 |
| pub->len = len; |
| 2341 |
28 |
| ngx_queue_insert_tail(&self->wait_queue, &pub->queue); |
| 2342 |
28 |
| self->waiting_count++; |
| 2343 |
28 |
| self->wait_bytes += len; |
| 2344 |
|
| } else { |
| 2345 |
0 |
| croak("not connected"); |
| 2346 |
|
| } |
| 2347 |
66 |
| } |
| 2348 |
|
| |
| 2349 |
|
| /* ================================================================ |
| 2350 |
|
| * TCP connection |
| 2351 |
|
| * ================================================================ */ |
| 2352 |
|
| |
| 2353 |
104 |
| static void nats_schedule_reconnect(nats_t *self) |
| 2354 |
|
| { |
| 2355 |
|
| /* If a user callback (on_disconnect/on_error) already kicked off a fresh |
| 2356 |
|
| connect, don't stomp on it. */ |
| 2357 |
104 |
▶ | if (self->fd >= 0 || self->connecting || self->reconnect_timer_active) |
Branch: 3 branches
| Branch | True | False | Total |
| if (self->fd >= 0 || self->connecting || self->reconnect_timer_active) |
103 |
1 |
104 |
| if (self->fd >= 0 || self->connecting || self->reconnect_timer_active) |
103 |
0 |
103 |
| if (self->fd >= 0 || self->connecting || self->reconnect_timer_active) |
0 |
103 |
103 |
|
| 2358 |
1 |
| return; |
| 2359 |
103 |
▶ | if (!self->intentional_disconnect && self->reconnect_enabled) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!self->intentional_disconnect && self->reconnect_enabled) { |
68 |
35 |
103 |
| if (!self->intentional_disconnect && self->reconnect_enabled) { |
50 |
18 |
68 |
|
| 2360 |
50 |
▶ | if (self->max_reconnect_attempts == 0 || |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->max_reconnect_attempts == 0 || |
50 |
0 |
50 |
|
| 2361 |
50 |
▶ | self->reconnect_attempts < self->max_reconnect_attempts) { |
Branch: 1 branch
| Branch | True | False | Total |
| self->reconnect_attempts < self->max_reconnect_attempts) { |
48 |
2 |
50 |
|
| 2362 |
|
| /* Exponential backoff with jitter */ |
| 2363 |
48 |
| int shift = self->reconnect_attempts > 5 ? 5 : self->reconnect_attempts; |
| 2364 |
48 |
| int delay = self->reconnect_delay_ms * (1 << shift); |
| 2365 |
48 |
▶ | if (self->max_reconnect_delay_ms > 0 && delay > self->max_reconnect_delay_ms) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->max_reconnect_delay_ms > 0 && delay > self->max_reconnect_delay_ms) |
48 |
0 |
48 |
| if (self->max_reconnect_delay_ms > 0 && delay > self->max_reconnect_delay_ms) |
0 |
48 |
48 |
|
| 2366 |
0 |
| delay = self->max_reconnect_delay_ms; |
| 2367 |
|
| /* Floor: a zero delay would spin the loop at full tilt. */ |
| 2368 |
48 |
▶ | if (delay < 100) |
Branch: 1 branch
| Branch | True | False | Total |
| if (delay < 100) |
9 |
39 |
48 |
|
| 2369 |
9 |
| delay = 100; |
| 2370 |
48 |
| double base = delay / 1000.0; |
| 2371 |
|
| unsigned char r[2]; |
| 2372 |
48 |
| nats_random_bytes(r, 2); |
| 2373 |
48 |
| unsigned int rv = ((unsigned)r[0] << 8) | r[1]; |
| 2374 |
48 |
| double jitter = base * (0.5 + (double)(rv % 1000) / 2000.0); |
| 2375 |
48 |
| ev_timer_set(&self->reconnect_timer, jitter, 0.0); |
| 2376 |
48 |
| ev_timer_start(self->loop, &self->reconnect_timer); |
| 2377 |
48 |
| self->reconnect_timer_active = 1; |
| 2378 |
|
| } else { |
| 2379 |
2 |
| nats_emit_error(self, "max reconnect attempts reached"); |
| 2380 |
|
| } |
| 2381 |
|
| } |
| 2382 |
|
| } |
| 2383 |
|
| |
| 2384 |
69 |
| static void nats_free_server_pool(nats_t *self) |
| 2385 |
|
| { |
| 2386 |
73 |
▶ | while (!ngx_queue_empty(&self->server_pool)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!ngx_queue_empty(&self->server_pool)) { |
4 |
69 |
73 |
|
| 2387 |
4 |
| ngx_queue_t *q = ngx_queue_head(&self->server_pool); |
| 2388 |
4 |
| nats_server_t *srv = ngx_queue_data(q, nats_server_t, queue); |
| 2389 |
4 |
| ngx_queue_remove(q); |
| 2390 |
4 |
| Safefree(srv->host); |
| 2391 |
4 |
| Safefree(srv); |
| 2392 |
4 |
| self->server_pool_count--; |
| 2393 |
|
| } |
| 2394 |
69 |
| } |
| 2395 |
|
| |
| 2396 |
13 |
| static int nats_pool_contains(nats_t *self, const char *host, size_t hlen, int port) |
| 2397 |
|
| { |
| 2398 |
|
| ngx_queue_t *q; |
| 2399 |
33 |
▶ | ngx_queue_foreach(q, &self->server_pool) { |
Branch: 2 branches
| Branch | True | False | Total |
| ngx_queue_foreach(q, &self->server_pool) { |
20 |
13 |
33 |
| ngx_queue_foreach(q, &self->server_pool) { |
20 |
0 |
20 |
|
| 2400 |
20 |
| nats_server_t *srv = ngx_queue_data(q, nats_server_t, queue); |
| 2401 |
20 |
▶ | if (srv->port == port && strlen(srv->host) == hlen && |
Branch: 2 branches
| Branch | True | False | Total |
| if (srv->port == port && strlen(srv->host) == hlen && |
3 |
17 |
20 |
| if (srv->port == port && strlen(srv->host) == hlen && |
1 |
2 |
3 |
|
| 2402 |
1 |
▶ | memcmp(srv->host, host, hlen) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| memcmp(srv->host, host, hlen) == 0) |
0 |
1 |
1 |
|
| 2403 |
0 |
| return 1; |
| 2404 |
|
| } |
| 2405 |
13 |
| return 0; |
| 2406 |
|
| } |
| 2407 |
|
| |
| 2408 |
43 |
| static void nats_parse_connect_urls(nats_t *self, const char *json, size_t len) |
| 2409 |
|
| { |
| 2410 |
43 |
| const char *key = "\"connect_urls\":["; |
| 2411 |
43 |
| const char *p = nats_memfind(json, len, key, strlen(key)); |
| 2412 |
43 |
▶ | if (!p) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) return; |
39 |
4 |
43 |
|
| 2413 |
|
| |
| 2414 |
4 |
| p += strlen(key); |
| 2415 |
4 |
| const char *end = json + len; |
| 2416 |
|
| |
| 2417 |
|
| /* Merge, never rebuild: a survivor drops dead peers from connect_urls, |
| 2418 |
|
| and rebuilding would forget them so they are never retried. */ |
| 2419 |
|
| |
| 2420 |
13 |
▶ | while (p < end && *p != ']') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != ']') { |
13 |
0 |
13 |
| while (p < end && *p != ']') { |
9 |
4 |
13 |
|
| 2421 |
29 |
▶ | while (p < end && (*p == ' ' || *p == ',' || *p == '"')) p++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (p < end && (*p == ' ' || *p == ',' || *p == '"')) p++; |
29 |
0 |
29 |
| while (p < end && (*p == ' ' || *p == ',' || *p == '"')) p++; |
0 |
29 |
29 |
| while (p < end && (*p == ' ' || *p == ',' || *p == '"')) p++; |
9 |
20 |
29 |
| while (p < end && (*p == ' ' || *p == ',' || *p == '"')) p++; |
11 |
9 |
20 |
|
| 2422 |
9 |
▶ | if (p >= end || *p == ']') break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p >= end || *p == ']') break; |
9 |
0 |
9 |
| if (p >= end || *p == ']') break; |
9 |
0 |
9 |
|
| 2423 |
|
| |
| 2424 |
9 |
| const char *start = p; |
| 2425 |
9 |
▶ | if (p < end && *p == '[') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '[') { |
9 |
0 |
9 |
| if (p < end && *p == '[') { |
3 |
6 |
9 |
|
| 2426 |
|
| /* Bracketed IPv6 literal: consume through the closing bracket |
| 2427 |
|
| so the ']' terminator below doesn't split host from port. |
| 2428 |
|
| Bounded to this entry: an unbalanced '[' must not swallow |
| 2429 |
|
| the entries that follow it. */ |
| 2430 |
3 |
| const char *q = p + 1; |
| 2431 |
20 |
▶ | while (q < end && *q != ']' && *q != '"' && *q != ',') q++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (q < end && *q != ']' && *q != '"' && *q != ',') q++; |
20 |
0 |
20 |
| while (q < end && *q != ']' && *q != '"' && *q != ',') q++; |
18 |
2 |
20 |
| while (q < end && *q != ']' && *q != '"' && *q != ',') q++; |
17 |
1 |
18 |
| while (q < end && *q != ']' && *q != '"' && *q != ',') q++; |
17 |
0 |
17 |
|
| 2432 |
3 |
▶ | if (q < end && *q == ']') p = (char *)q + 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (q < end && *q == ']') p = (char *)q + 1; |
3 |
0 |
3 |
| if (q < end && *q == ']') p = (char *)q + 1; |
2 |
1 |
3 |
|
| 2433 |
|
| } |
| 2434 |
80 |
▶ | while (p < end && *p != '"' && *p != ',' && *p != ']') p++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (p < end && *p != '"' && *p != ',' && *p != ']') p++; |
80 |
0 |
80 |
| while (p < end && *p != '"' && *p != ',' && *p != ']') p++; |
71 |
9 |
80 |
| while (p < end && *p != '"' && *p != ',' && *p != ']') p++; |
71 |
0 |
71 |
| while (p < end && *p != '"' && *p != ',' && *p != ']') p++; |
71 |
0 |
71 |
|
| 2435 |
|
| |
| 2436 |
9 |
| size_t url_len = p - start; |
| 2437 |
9 |
▶ | if (url_len > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (url_len > 0) { |
9 |
0 |
9 |
|
| 2438 |
|
| /* Split host and port. IPv6 arrives bracketed ("[::1]:4222"); |
| 2439 |
|
| strip the brackets, which getaddrinfo rejects. */ |
| 2440 |
9 |
| const char *hstart = start; |
| 2441 |
9 |
| const char *hend = start + url_len; |
| 2442 |
9 |
| const char *pstart = NULL; |
| 2443 |
9 |
| size_t plen = 0; |
| 2444 |
11 |
▶ | if (url_len > 2 && start[0] == '[') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (url_len > 2 && start[0] == '[') { |
7 |
2 |
9 |
| if (url_len > 2 && start[0] == '[') { |
2 |
5 |
7 |
|
| 2445 |
2 |
| const char *rb = memchr(start + 1, ']', url_len - 1); |
| 2446 |
2 |
▶ | if (rb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rb) { |
1 |
1 |
2 |
|
| 2447 |
1 |
| hstart = start + 1; |
| 2448 |
1 |
| hend = rb; |
| 2449 |
1 |
▶ | if (rb + 1 < start + url_len && rb[1] == ':') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (rb + 1 < start + url_len && rb[1] == ':') { |
1 |
0 |
1 |
| if (rb + 1 < start + url_len && rb[1] == ':') { |
1 |
0 |
1 |
|
| 2450 |
1 |
| pstart = rb + 2; |
| 2451 |
1 |
| plen = (start + url_len) - pstart; |
| 2452 |
|
| } |
| 2453 |
|
| } |
| 2454 |
|
| } else { |
| 2455 |
7 |
| const char *colon = NULL; |
| 2456 |
|
| const char *s; |
| 2457 |
41 |
▶ | for (s = start + url_len - 1; s >= start; s--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = start + url_len - 1; s >= start; s--) { |
38 |
3 |
41 |
|
| 2458 |
38 |
▶ | if (*s == ':') { colon = s; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == ':') { colon = s; break; } |
4 |
34 |
38 |
|
| 2459 |
|
| } |
| 2460 |
7 |
▶ | if (colon && colon > start) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (colon && colon > start) { |
4 |
3 |
7 |
| if (colon && colon > start) { |
4 |
0 |
4 |
|
| 2461 |
4 |
| hend = colon; |
| 2462 |
4 |
| pstart = colon + 1; |
| 2463 |
4 |
| plen = (start + url_len) - pstart; |
| 2464 |
|
| } |
| 2465 |
|
| } |
| 2466 |
|
| |
| 2467 |
9 |
| int port = 4222; |
| 2468 |
9 |
▶ | if (pstart && plen > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pstart && plen > 0) { |
5 |
4 |
9 |
| if (pstart && plen > 0) { |
5 |
0 |
5 |
|
| 2469 |
5 |
| long pval = 0; |
| 2470 |
|
| size_t i; |
| 2471 |
25 |
▶ | for (i = 0; i < plen && pstart[i] >= '0' && pstart[i] <= '9'; i++) { |
Branch: 3 branches
| Branch | True | False | Total |
| for (i = 0; i < plen && pstart[i] >= '0' && pstart[i] <= '9'; i++) { |
21 |
4 |
25 |
| for (i = 0; i < plen && pstart[i] >= '0' && pstart[i] <= '9'; i++) { |
21 |
0 |
21 |
| for (i = 0; i < plen && pstart[i] >= '0' && pstart[i] <= '9'; i++) { |
21 |
0 |
21 |
|
| 2472 |
21 |
| pval = pval * 10 + (pstart[i] - '0'); |
| 2473 |
21 |
▶ | if (pval > 65535) { pval = 65535; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (pval > 65535) { pval = 65535; break; } |
1 |
20 |
21 |
|
| 2474 |
|
| } |
| 2475 |
5 |
▶ | if (i > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i > 0) |
5 |
0 |
5 |
|
| 2476 |
5 |
| port = (int)pval; |
| 2477 |
|
| } |
| 2478 |
9 |
| size_t hlen = hend - hstart; |
| 2479 |
18 |
| if (self->server_pool_count < MAX_SERVER_POOL && |
| 2480 |
9 |
| !nats_pool_contains(self, hstart, hlen, port)) { |
| 2481 |
|
| nats_server_t *srv; |
| 2482 |
9 |
| Newxz(srv, 1, nats_server_t); |
| 2483 |
9 |
| Newx(srv->host, hlen + 1, char); |
| 2484 |
9 |
| memcpy(srv->host, hstart, hlen); |
| 2485 |
9 |
| srv->host[hlen] = '\0'; |
| 2486 |
9 |
| srv->port = port; |
| 2487 |
9 |
| ngx_queue_insert_tail(&self->server_pool, &srv->queue); |
| 2488 |
9 |
| self->server_pool_count++; |
| 2489 |
|
| } |
| 2490 |
|
| } |
| 2491 |
|
| |
| 2492 |
18 |
▶ | while (p < end && *p == '"') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p == '"') p++; |
18 |
0 |
18 |
| while (p < end && *p == '"') p++; |
9 |
9 |
18 |
|
| 2493 |
|
| } |
| 2494 |
|
| |
| 2495 |
|
| /* Retain the server we're on in case it omits itself. */ |
| 2496 |
8 |
▶ | if (self->host && self->server_pool_count < MAX_SERVER_POOL && |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->host && self->server_pool_count < MAX_SERVER_POOL && |
4 |
0 |
4 |
|
| 2497 |
4 |
| !nats_pool_contains(self, self->host, strlen(self->host), self->port)) { |
| 2498 |
|
| nats_server_t *srv; |
| 2499 |
4 |
| Newxz(srv, 1, nats_server_t); |
| 2500 |
4 |
| nats_set_str(&srv->host, self->host); |
| 2501 |
4 |
| srv->port = self->port; |
| 2502 |
4 |
| ngx_queue_insert_tail(&self->server_pool, &srv->queue); |
| 2503 |
4 |
| self->server_pool_count++; |
| 2504 |
|
| } |
| 2505 |
|
| } |
| 2506 |
|
| |
| 2507 |
19 |
| static void nats_next_server(nats_t *self) |
| 2508 |
|
| { |
| 2509 |
19 |
▶ | if (ngx_queue_empty(&self->server_pool)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ngx_queue_empty(&self->server_pool)) return; |
0 |
19 |
19 |
|
| 2510 |
|
| |
| 2511 |
|
| /* Rotate: move head to tail, use new head */ |
| 2512 |
19 |
| ngx_queue_t *q = ngx_queue_head(&self->server_pool); |
| 2513 |
19 |
| nats_server_t *srv = ngx_queue_data(q, nats_server_t, queue); |
| 2514 |
|
| |
| 2515 |
19 |
| nats_set_str(&self->host, srv->host); |
| 2516 |
19 |
| self->port = srv->port; |
| 2517 |
|
| /* host changed: any saved getaddrinfo candidates are stale */ |
| 2518 |
19 |
| nats_free_addrinfo(self); |
| 2519 |
|
| |
| 2520 |
|
| /* Rotate this server to end of pool */ |
| 2521 |
19 |
| ngx_queue_remove(q); |
| 2522 |
19 |
| ngx_queue_insert_tail(&self->server_pool, q); |
| 2523 |
|
| } |
| 2524 |
|
| |
| 2525 |
|
| /* Shared post-connect(2) socket setup for the TCP and Unix connect paths. |
| 2526 |
|
| * rv is the connect() return: nonzero means the connection is still in |
| 2527 |
|
| * progress (EINPROGRESS) and we must also wait for EV_WRITE. */ |
| 2528 |
84 |
| static void nats_after_connect(nats_t *self, int fd, int rv) |
| 2529 |
|
| { |
| 2530 |
|
| #ifdef SO_NOSIGPIPE |
| 2531 |
|
| int flag = 1; |
| 2532 |
|
| setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &flag, sizeof(flag)); |
| 2533 |
|
| #endif |
| 2534 |
84 |
| self->fd = fd; |
| 2535 |
84 |
| self->connecting = 1; |
| 2536 |
84 |
| self->connected = 0; |
| 2537 |
|
| /* rv != 0: EINPROGRESS — first EV_WRITE must SO_ERROR-check the connect */ |
| 2538 |
84 |
| self->connect_pending = (rv != 0); |
| 2539 |
|
| |
| 2540 |
84 |
| ev_io_init(&self->rio, nats_on_read, fd, EV_READ); |
| 2541 |
84 |
| ev_io_init(&self->wio, nats_on_write, fd, EV_WRITE); |
| 2542 |
|
| |
| 2543 |
84 |
▶ | if (self->priority) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->priority) { |
0 |
84 |
84 |
|
| 2544 |
0 |
| ev_set_priority(&self->rio, self->priority); |
| 2545 |
0 |
| ev_set_priority(&self->wio, self->priority); |
| 2546 |
|
| } |
| 2547 |
|
| |
| 2548 |
84 |
| ev_io_start(self->loop, &self->rio); |
| 2549 |
84 |
| self->reading = 1; |
| 2550 |
|
| |
| 2551 |
84 |
▶ | if (rv != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != 0) { |
84 |
0 |
84 |
|
| 2552 |
84 |
| ev_io_start(self->loop, &self->wio); |
| 2553 |
84 |
| self->writing = 1; |
| 2554 |
|
| } |
| 2555 |
|
| |
| 2556 |
84 |
▶ | if (self->connect_timeout_ms > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connect_timeout_ms > 0) { |
0 |
84 |
84 |
|
| 2557 |
|
| /* failover re-enters here with the timer still running: ev_timer_set |
| 2558 |
|
| on an active watcher corrupts libev's heap key, so stop it first */ |
| 2559 |
0 |
▶ | if (self->connect_timer_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connect_timer_active) { |
0 |
0 |
0 |
|
| 2560 |
0 |
| ev_timer_stop(self->loop, &self->connect_timer); |
| 2561 |
0 |
| self->connect_timer_active = 0; |
| 2562 |
|
| } |
| 2563 |
0 |
| ev_timer_set(&self->connect_timer, self->connect_timeout_ms / 1000.0, 0.0); |
| 2564 |
0 |
| ev_timer_start(self->loop, &self->connect_timer); |
| 2565 |
0 |
| self->connect_timer_active = 1; |
| 2566 |
|
| } |
| 2567 |
84 |
| } |
| 2568 |
|
| |
| 2569 |
|
| /* Drop the saved getaddrinfo list; NULLs both fields, never frees twice. */ |
| 2570 |
207 |
| static void nats_free_addrinfo(nats_t *self) |
| 2571 |
|
| { |
| 2572 |
207 |
▶ | if (self->ai_res) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->ai_res) |
84 |
123 |
207 |
|
| 2573 |
84 |
| freeaddrinfo(self->ai_res); |
| 2574 |
207 |
| self->ai_res = NULL; |
| 2575 |
207 |
| self->ai_next = NULL; |
| 2576 |
207 |
| } |
| 2577 |
|
| |
| 2578 |
|
| /* Actual teardown behind DESTROY. With cb_depth > 0 DESTROY only sets |
| 2579 |
|
| destroy_pending and the outermost nats_on_* wrapper runs this once the |
| 2580 |
|
| callback returns. Callers must not touch self after this. */ |
| 2581 |
60 |
| static void nats_destroy_now(nats_t *self) |
| 2582 |
|
| { |
| 2583 |
60 |
▶ | CLEAR_HANDLER(self->on_error); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->on_error); |
49 |
11 |
60 |
|
| 2584 |
60 |
▶ | CLEAR_HANDLER(self->on_connect); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->on_connect); |
12 |
48 |
60 |
|
| 2585 |
60 |
▶ | CLEAR_HANDLER(self->on_disconnect); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->on_disconnect); |
2 |
58 |
60 |
|
| 2586 |
|
| |
| 2587 |
60 |
| nats_stop_watchers(self); |
| 2588 |
60 |
| nats_stop_timers(self); |
| 2589 |
|
| |
| 2590 |
60 |
▶ | while (!ngx_queue_empty(&self->req_queue)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!ngx_queue_empty(&self->req_queue)) { |
0 |
60 |
60 |
|
| 2591 |
0 |
| ngx_queue_t *q = ngx_queue_head(&self->req_queue); |
| 2592 |
0 |
| nats_req_t *req = ngx_queue_data(q, nats_req_t, queue); |
| 2593 |
0 |
| ngx_queue_remove(q); |
| 2594 |
0 |
▶ | if (req->timer_active) |
Branch: 1 branch
| Branch | True | False | Total |
| if (req->timer_active) |
0 |
0 |
0 |
|
| 2595 |
0 |
| ev_timer_stop(self->loop, &req->timer); |
| 2596 |
0 |
▶ | CLEAR_HANDLER(req->cb); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(req->cb); |
0 |
0 |
0 |
|
| 2597 |
0 |
| Safefree(req); |
| 2598 |
|
| } |
| 2599 |
60 |
| nats_skip_waiting(self); |
| 2600 |
|
| |
| 2601 |
61 |
▶ | while (!ngx_queue_empty(&self->pong_cbs)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!ngx_queue_empty(&self->pong_cbs)) { |
1 |
60 |
61 |
|
| 2602 |
1 |
| ngx_queue_t *pq = ngx_queue_head(&self->pong_cbs); |
| 2603 |
1 |
| nats_pong_cb_t *pcb = ngx_queue_data(pq, nats_pong_cb_t, queue); |
| 2604 |
1 |
| ngx_queue_remove(pq); |
| 2605 |
1 |
▶ | CLEAR_HANDLER(pcb->cb); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(pcb->cb); |
1 |
0 |
1 |
|
| 2606 |
1 |
| Safefree(pcb); |
| 2607 |
|
| } |
| 2608 |
|
| |
| 2609 |
62 |
▶ | while (!ngx_queue_empty(&self->subs)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!ngx_queue_empty(&self->subs)) { |
2 |
60 |
62 |
|
| 2610 |
2 |
| ngx_queue_t *q = ngx_queue_head(&self->subs); |
| 2611 |
2 |
| nats_sub_t *sub = ngx_queue_data(q, nats_sub_t, queue); |
| 2612 |
2 |
| nats_remove_sub(self, sub); |
| 2613 |
|
| } |
| 2614 |
|
| |
| 2615 |
60 |
▶ | if (self->fd >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->fd >= 0) { |
5 |
55 |
60 |
|
| 2616 |
5 |
| close(self->fd); |
| 2617 |
5 |
| self->fd = -1; |
| 2618 |
|
| } |
| 2619 |
60 |
| nats_free_addrinfo(self); |
| 2620 |
|
| |
| 2621 |
60 |
▶ | CLEAR_HANDLER(self->server_info_json); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->server_info_json); |
17 |
43 |
60 |
|
| 2622 |
60 |
▶ | CLEAR_HANDLER(self->drain_cb); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->drain_cb); |
0 |
60 |
60 |
|
| 2623 |
60 |
| Safefree(self->pending_croak); |
| 2624 |
|
| |
| 2625 |
60 |
| nats_free_server_pool(self); |
| 2626 |
60 |
▶ | if (self->sub_map) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->sub_map) { |
60 |
0 |
60 |
|
| 2627 |
60 |
| SvREFCNT_dec((SV *)self->sub_map); |
| 2628 |
60 |
| self->sub_map = NULL; |
| 2629 |
|
| } |
| 2630 |
|
| #ifdef HAVE_OPENSSL |
| 2631 |
60 |
| nats_ssl_cleanup(self); |
| 2632 |
60 |
▶ | if (self->ssl_ctx) { SSL_CTX_free(self->ssl_ctx); self->ssl_ctx = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->ssl_ctx) { SSL_CTX_free(self->ssl_ctx); self->ssl_ctx = NULL; } |
0 |
60 |
60 |
|
| 2633 |
60 |
| Safefree(self->tls_ca_file); |
| 2634 |
|
| #endif |
| 2635 |
|
| |
| 2636 |
60 |
| Safefree(self->rbuf); |
| 2637 |
60 |
| Safefree(self->wbuf); |
| 2638 |
60 |
| Safefree(self->host); |
| 2639 |
60 |
| Safefree(self->path); |
| 2640 |
60 |
| Safefree(self->user); |
| 2641 |
60 |
| Safefree(self->pass); |
| 2642 |
60 |
| Safefree(self->token); |
| 2643 |
60 |
| Safefree(self->name); |
| 2644 |
60 |
| Safefree(self->nkey_seed); |
| 2645 |
60 |
| Safefree(self->jwt); |
| 2646 |
60 |
| Safefree(self->server_nonce); |
| 2647 |
60 |
▶ | CLEAR_HANDLER(self->on_slow_consumer); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->on_slow_consumer); |
0 |
60 |
60 |
|
| 2648 |
60 |
▶ | CLEAR_HANDLER(self->on_ldm); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->on_ldm); |
0 |
60 |
60 |
|
| 2649 |
60 |
▶ | CLEAR_HANDLER(self->loop_sv); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->loop_sv); |
2 |
58 |
60 |
|
| 2650 |
|
| |
| 2651 |
60 |
| Safefree(self); |
| 2652 |
60 |
| } |
| 2653 |
|
| |
| 2654 |
96 |
| static void nats_connect_tcp(nats_t *self) |
| 2655 |
|
| { |
| 2656 |
|
| struct addrinfo hints, *rp; |
| 2657 |
|
| char port_str[8]; |
| 2658 |
96 |
| int fd = -1; |
| 2659 |
96 |
| int rv = 0; |
| 2660 |
|
| |
| 2661 |
|
| /* Resolve once per host and keep the list: a connect that later fails |
| 2662 |
|
| via SO_ERROR fails over to the remaining candidates. */ |
| 2663 |
96 |
▶ | if (!self->ai_res) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->ai_res) { |
96 |
0 |
96 |
|
| 2664 |
96 |
| memset(&hints, 0, sizeof(hints)); |
| 2665 |
96 |
| hints.ai_family = AF_UNSPEC; |
| 2666 |
96 |
| hints.ai_socktype = SOCK_STREAM; |
| 2667 |
|
| |
| 2668 |
96 |
| snprintf(port_str, sizeof(port_str), "%d", self->port); |
| 2669 |
|
| |
| 2670 |
96 |
| rv = getaddrinfo(self->host, port_str, &hints, &self->ai_res); |
| 2671 |
96 |
▶ | if (rv != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != 0) { |
12 |
84 |
96 |
|
| 2672 |
12 |
| self->ai_res = NULL; |
| 2673 |
12 |
| nats_emit_error(self, gai_strerror(rv)); |
| 2674 |
12 |
| nats_schedule_reconnect(self); |
| 2675 |
96 |
| return; |
| 2676 |
|
| } |
| 2677 |
84 |
| self->ai_next = self->ai_res; |
| 2678 |
|
| } |
| 2679 |
|
| |
| 2680 |
84 |
▶ | for (rp = self->ai_next; rp != NULL; rp = rp->ai_next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (rp = self->ai_next; rp != NULL; rp = rp->ai_next) { |
84 |
0 |
84 |
|
| 2681 |
84 |
| fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
| 2682 |
84 |
▶ | if (fd < 0) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) continue; |
0 |
84 |
84 |
|
| 2683 |
|
| |
| 2684 |
84 |
| fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); |
| 2685 |
|
| |
| 2686 |
84 |
| int flag = 1; |
| 2687 |
84 |
| setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag)); |
| 2688 |
|
| |
| 2689 |
84 |
▶ | if (self->keepalive > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->keepalive > 0) { |
0 |
84 |
84 |
|
| 2690 |
0 |
| setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &flag, sizeof(flag)); |
| 2691 |
|
| #ifdef TCP_KEEPIDLE |
| 2692 |
0 |
| setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &self->keepalive, sizeof(self->keepalive)); |
| 2693 |
|
| #endif |
| 2694 |
|
| } |
| 2695 |
|
| |
| 2696 |
84 |
| rv = connect(fd, rp->ai_addr, rp->ai_addrlen); |
| 2697 |
84 |
▶ | if (rv == 0 || errno == EINPROGRESS) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (rv == 0 || errno == EINPROGRESS) { |
84 |
0 |
84 |
| if (rv == 0 || errno == EINPROGRESS) { |
84 |
0 |
84 |
|
| 2698 |
84 |
| self->ai_next = rp->ai_next; |
| 2699 |
84 |
| nats_after_connect(self, fd, rv); |
| 2700 |
84 |
| return; |
| 2701 |
|
| } |
| 2702 |
|
| |
| 2703 |
0 |
| close(fd); |
| 2704 |
0 |
| fd = -1; |
| 2705 |
|
| } |
| 2706 |
|
| |
| 2707 |
|
| /* Address list exhausted: drop it so the next attempt re-resolves. */ |
| 2708 |
0 |
| nats_free_addrinfo(self); |
| 2709 |
|
| |
| 2710 |
0 |
| nats_emit_error(self, "connection failed"); |
| 2711 |
0 |
| nats_schedule_reconnect(self); |
| 2712 |
|
| } |
| 2713 |
|
| |
| 2714 |
1 |
| static void nats_connect_unix(nats_t *self) |
| 2715 |
|
| { |
| 2716 |
|
| struct sockaddr_un addr; |
| 2717 |
|
| int fd; |
| 2718 |
|
| |
| 2719 |
1 |
▶ | if (!self->path || strlen(self->path) >= sizeof(addr.sun_path)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!self->path || strlen(self->path) >= sizeof(addr.sun_path)) { |
1 |
0 |
1 |
| if (!self->path || strlen(self->path) >= sizeof(addr.sun_path)) { |
0 |
1 |
1 |
|
| 2720 |
0 |
| nats_emit_error(self, "invalid unix socket path"); |
| 2721 |
0 |
| nats_schedule_reconnect(self); |
| 2722 |
0 |
| return; |
| 2723 |
|
| } |
| 2724 |
|
| |
| 2725 |
1 |
| fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 2726 |
1 |
▶ | if (fd < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) { |
0 |
1 |
1 |
|
| 2727 |
0 |
| nats_emit_error(self, strerror(errno)); |
| 2728 |
0 |
| nats_schedule_reconnect(self); |
| 2729 |
0 |
| return; |
| 2730 |
|
| } |
| 2731 |
|
| |
| 2732 |
1 |
| fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); |
| 2733 |
|
| |
| 2734 |
1 |
| memset(&addr, 0, sizeof(addr)); |
| 2735 |
1 |
| addr.sun_family = AF_UNIX; |
| 2736 |
1 |
| strncpy(addr.sun_path, self->path, sizeof(addr.sun_path) - 1); |
| 2737 |
|
| |
| 2738 |
1 |
| int rv = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); |
| 2739 |
1 |
▶ | if (rv != 0 && errno != EINPROGRESS) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (rv != 0 && errno != EINPROGRESS) { |
1 |
0 |
1 |
| if (rv != 0 && errno != EINPROGRESS) { |
1 |
0 |
1 |
|
| 2740 |
1 |
| close(fd); |
| 2741 |
1 |
| nats_emit_error(self, strerror(errno)); |
| 2742 |
0 |
| nats_schedule_reconnect(self); |
| 2743 |
0 |
| return; |
| 2744 |
|
| } |
| 2745 |
|
| |
| 2746 |
0 |
| nats_after_connect(self, fd, rv); |
| 2747 |
|
| } |
| 2748 |
|
| |
| 2749 |
|
| /* Helper: connect via path or host */ |
| 2750 |
88 |
| static void nats_do_connect(nats_t *self) |
| 2751 |
|
| { |
| 2752 |
88 |
▶ | if (self->path) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->path) |
1 |
87 |
88 |
|
| 2753 |
1 |
| nats_connect_unix(self); |
| 2754 |
|
| else |
| 2755 |
87 |
| nats_connect_tcp(self); |
| 2756 |
87 |
| } |
| 2757 |
|
| |
| 2758 |
|
| /* ================================================================ |
| 2759 |
|
| * XS interface |
| 2760 |
|
| * ================================================================ */ |
| 2761 |
|
| |
| 2762 |
|
| MODULE = EV::Nats PACKAGE = EV::Nats |
| 2763 |
|
| |
| 2764 |
|
| PROTOTYPES: DISABLE |
| 2765 |
|
| |
| 2766 |
|
| BOOT: |
| 2767 |
|
| { |
| 2768 |
20 |
▶ | I_EV_API("EV::Nats"); |
Branch: 3 branches
| Branch | True | False | Total |
| I_EV_API("EV::Nats"); |
0 |
20 |
20 |
| I_EV_API("EV::Nats"); |
20 |
0 |
20 |
| I_EV_API("EV::Nats"); |
0 |
20 |
20 |
|
| 2769 |
|
| { |
| 2770 |
20 |
| HV *stash = gv_stashpv("EV::Nats", GV_ADD); |
| 2771 |
|
| #ifdef HAVE_OPENSSL |
| 2772 |
20 |
| newCONSTSUB(stash, "HAS_TLS", newSViv(1)); |
| 2773 |
20 |
| newCONSTSUB(stash, "HAS_NKEY", newSViv(1)); |
| 2774 |
|
| #else |
| 2775 |
|
| newCONSTSUB(stash, "HAS_TLS", newSViv(0)); |
| 2776 |
|
| newCONSTSUB(stash, "HAS_NKEY", newSViv(0)); |
| 2777 |
|
| #endif |
| 2778 |
|
| } |
| 2779 |
|
| } |
| 2780 |
|
| |
| 2781 |
|
| EV::Nats |
| 2782 |
|
| new(class, ...) |
| 2783 |
|
| char *class |
| 2784 |
|
| PREINIT: |
| 2785 |
|
| nats_t *self; |
| 2786 |
|
| int i; |
| 2787 |
|
| CODE: |
| 2788 |
78 |
| Newxz(self, 1, nats_t); |
| 2789 |
78 |
| self->magic = NATS_MAGIC_ALIVE; |
| 2790 |
78 |
| self->fd = -1; |
| 2791 |
78 |
| self->loop = EV_DEFAULT; |
| 2792 |
78 |
| self->max_payload = DEFAULT_MAX_PAYLOAD; |
| 2793 |
78 |
| self->port = 4222; |
| 2794 |
78 |
| self->echo = 1; |
| 2795 |
78 |
| self->ping_interval_ms = 120000; |
| 2796 |
78 |
| self->max_pings_outstanding = 2; |
| 2797 |
78 |
| self->reconnect_delay_ms = 2000; |
| 2798 |
78 |
| self->max_reconnect_delay_ms = 30000; |
| 2799 |
78 |
| self->max_reconnect_attempts = 60; |
| 2800 |
78 |
| self->reconnect_buf_size = 8 * 1024 * 1024; |
| 2801 |
|
| |
| 2802 |
78 |
| ngx_queue_init(&self->subs); |
| 2803 |
78 |
| ngx_queue_init(&self->wait_queue); |
| 2804 |
78 |
| ngx_queue_init(&self->req_queue); |
| 2805 |
|
| |
| 2806 |
78 |
| self->next_sid = 1; |
| 2807 |
|
| |
| 2808 |
78 |
| ev_timer_init(&self->connect_timer, nats_on_connect_timeout, 0., 0.); |
| 2809 |
78 |
| ev_timer_init(&self->reconnect_timer, nats_on_reconnect_timer, 0., 0.); |
| 2810 |
78 |
| ev_timer_init(&self->ping_timer, nats_on_ping_timer, 0., 0.); |
| 2811 |
78 |
| ev_prepare_init(&self->prepare_watcher, nats_on_prepare); |
| 2812 |
|
| |
| 2813 |
78 |
| ngx_queue_init(&self->server_pool); |
| 2814 |
78 |
| ngx_queue_init(&self->pong_cbs); |
| 2815 |
78 |
| self->sub_map = newHV(); |
| 2816 |
|
| |
| 2817 |
78 |
| nats_setup_inbox(self); |
| 2818 |
|
| |
| 2819 |
78 |
| SV *guard = sv_2mortal(newRV_noinc(newSViv(PTR2IV(self)))); |
| 2820 |
78 |
| sv_bless(guard, gv_stashpv(class, GV_ADD)); |
| 2821 |
|
| |
| 2822 |
78 |
▶ | if (items > 1 && (items - 1) % 2 == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (items > 1 && (items - 1) % 2 == 0) { |
72 |
6 |
78 |
| if (items > 1 && (items - 1) % 2 == 0) { |
72 |
0 |
72 |
|
| 2823 |
354 |
▶ | for (i = 1; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i += 2) { |
289 |
65 |
354 |
|
| 2824 |
289 |
| const char *key = SvPV_nolen(ST(i)); |
| 2825 |
289 |
| SV *val = ST(i + 1); |
| 2826 |
|
| |
| 2827 |
289 |
▶ | if (strcmp(key, "host") == 0) nats_set_str_sv(&self->host, val); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(key, "host") == 0) nats_set_str_sv(&self->host, val); |
53 |
236 |
289 |
|
| 2828 |
236 |
▶ | else if (strcmp(key, "port") == 0) self->port = SvIV(val); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "port") == 0) self->port = SvIV(val); |
53 |
183 |
236 |
|
| 2829 |
183 |
▶ | else if (strcmp(key, "path") == 0) nats_set_str_sv(&self->path, val); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "path") == 0) nats_set_str_sv(&self->path, val); |
1 |
182 |
183 |
|
| 2830 |
182 |
▶ | else if (strcmp(key, "user") == 0) nats_set_str_sv(&self->user, val); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "user") == 0) nats_set_str_sv(&self->user, val); |
1 |
181 |
182 |
|
| 2831 |
181 |
▶ | else if (strcmp(key, "pass") == 0) nats_set_str_sv(&self->pass, val); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "pass") == 0) nats_set_str_sv(&self->pass, val); |
1 |
180 |
181 |
|
| 2832 |
180 |
▶ | else if (strcmp(key, "token") == 0) nats_set_str_sv(&self->token, val); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "token") == 0) nats_set_str_sv(&self->token, val); |
0 |
180 |
180 |
|
| 2833 |
180 |
▶ | else if (strcmp(key, "name") == 0) nats_set_str_sv(&self->name, val); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "name") == 0) nats_set_str_sv(&self->name, val); |
1 |
179 |
180 |
|
| 2834 |
179 |
▶ | else if (strcmp(key, "on_error") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "on_error") == 0) |
68 |
111 |
179 |
|
| 2835 |
68 |
▶ | self->on_error = SvOK(val) ? newSVsv(val) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| self->on_error = SvOK(val) ? newSVsv(val) : NULL; |
67 |
1 |
68 |
|
| 2836 |
111 |
▶ | else if (strcmp(key, "on_connect") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "on_connect") == 0) |
31 |
80 |
111 |
|
| 2837 |
31 |
▶ | self->on_connect = SvOK(val) ? newSVsv(val) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| self->on_connect = SvOK(val) ? newSVsv(val) : NULL; |
30 |
1 |
31 |
|
| 2838 |
80 |
▶ | else if (strcmp(key, "on_disconnect") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "on_disconnect") == 0) |
3 |
77 |
80 |
|
| 2839 |
3 |
▶ | self->on_disconnect = SvOK(val) ? newSVsv(val) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| self->on_disconnect = SvOK(val) ? newSVsv(val) : NULL; |
3 |
0 |
3 |
|
| 2840 |
77 |
▶ | else if (strcmp(key, "verbose") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "verbose") == 0) |
0 |
77 |
77 |
|
| 2841 |
0 |
| self->verbose = SvTRUE(val) ? 1 : 0; |
| 2842 |
77 |
▶ | else if (strcmp(key, "pedantic") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "pedantic") == 0) |
0 |
77 |
77 |
|
| 2843 |
0 |
| self->pedantic = SvTRUE(val) ? 1 : 0; |
| 2844 |
77 |
▶ | else if (strcmp(key, "echo") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "echo") == 0) |
0 |
77 |
77 |
|
| 2845 |
0 |
| self->echo = SvTRUE(val) ? 1 : 0; |
| 2846 |
77 |
▶ | else if (strcmp(key, "no_responders") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "no_responders") == 0) |
0 |
77 |
77 |
|
| 2847 |
0 |
| self->no_responders = SvTRUE(val) ? 1 : 0; |
| 2848 |
77 |
▶ | else if (strcmp(key, "reconnect") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "reconnect") == 0) |
38 |
39 |
77 |
|
| 2849 |
38 |
| self->reconnect_enabled = SvTRUE(val) ? 1 : 0; |
| 2850 |
39 |
▶ | else if (strcmp(key, "reconnect_delay") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "reconnect_delay") == 0) |
7 |
32 |
39 |
|
| 2851 |
7 |
| self->reconnect_delay_ms = SvIV(val); |
| 2852 |
32 |
▶ | else if (strcmp(key, "max_reconnect_attempts") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "max_reconnect_attempts") == 0) |
1 |
31 |
32 |
|
| 2853 |
1 |
| self->max_reconnect_attempts = SvIV(val); |
| 2854 |
31 |
▶ | else if (strcmp(key, "max_reconnect_delay") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "max_reconnect_delay") == 0) |
0 |
31 |
31 |
|
| 2855 |
0 |
| self->max_reconnect_delay_ms = SvIV(val); |
| 2856 |
31 |
▶ | else if (strcmp(key, "reconnect_buf_size") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "reconnect_buf_size") == 0) { |
14 |
17 |
31 |
|
| 2857 |
14 |
| IV v = SvIV(val); |
| 2858 |
14 |
| self->reconnect_buf_size = v < 0 ? 0 : (size_t)v; |
| 2859 |
|
| } |
| 2860 |
17 |
▶ | else if (strcmp(key, "connect_timeout") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "connect_timeout") == 0) |
0 |
17 |
17 |
|
| 2861 |
0 |
| self->connect_timeout_ms = SvIV(val); |
| 2862 |
17 |
▶ | else if (strcmp(key, "ping_interval") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "ping_interval") == 0) |
4 |
13 |
17 |
|
| 2863 |
4 |
| self->ping_interval_ms = SvIV(val); |
| 2864 |
13 |
▶ | else if (strcmp(key, "max_pings_outstanding") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "max_pings_outstanding") == 0) |
2 |
11 |
13 |
|
| 2865 |
2 |
| self->max_pings_outstanding = SvIV(val); |
| 2866 |
11 |
▶ | else if (strcmp(key, "priority") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "priority") == 0) { |
3 |
8 |
11 |
|
| 2867 |
3 |
| IV v = SvIV(val); |
| 2868 |
3 |
▶ | if (v < -2 || v > 2) |
Branch: 2 branches
| Branch | True | False | Total |
| if (v < -2 || v > 2) |
2 |
1 |
3 |
| if (v < -2 || v > 2) |
2 |
0 |
2 |
|
| 2869 |
3 |
| croak("EV::Nats::new: 'priority' must be between -2 and +2"); |
| 2870 |
0 |
| self->priority = (int)v; |
| 2871 |
|
| } |
| 2872 |
8 |
▶ | else if (strcmp(key, "keepalive") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "keepalive") == 0) |
0 |
8 |
8 |
|
| 2873 |
0 |
| self->keepalive = SvIV(val); |
| 2874 |
|
| #ifdef HAVE_OPENSSL |
| 2875 |
8 |
▶ | else if (strcmp(key, "tls") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "tls") == 0) |
0 |
8 |
8 |
|
| 2876 |
0 |
| self->tls = SvTRUE(val) ? 1 : 0; |
| 2877 |
8 |
▶ | else if (strcmp(key, "tls_ca_file") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "tls_ca_file") == 0) |
0 |
8 |
8 |
|
| 2878 |
0 |
| nats_set_str_sv(&self->tls_ca_file, val); |
| 2879 |
8 |
▶ | else if (strcmp(key, "tls_skip_verify") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "tls_skip_verify") == 0) |
0 |
8 |
8 |
|
| 2880 |
0 |
| self->tls_skip_verify = SvTRUE(val) ? 1 : 0; |
| 2881 |
8 |
▶ | else if (strcmp(key, "nkey_seed") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "nkey_seed") == 0) { |
2 |
6 |
8 |
|
| 2882 |
2 |
▶ | if (SvOK(val)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(val)) { |
2 |
0 |
2 |
|
| 2883 |
|
| STRLEN slen; |
| 2884 |
2 |
| const char *seed = SvPV(val, slen); |
| 2885 |
|
| char pub[64]; |
| 2886 |
2 |
▶ | if (nats_nkey_public(seed, pub, sizeof(pub)) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_nkey_public(seed, pub, sizeof(pub)) <= 0) |
2 |
0 |
2 |
|
| 2887 |
2 |
| croak("invalid NKey seed"); |
| 2888 |
|
| } |
| 2889 |
0 |
| nats_set_str_sv(&self->nkey_seed, val); |
| 2890 |
|
| } |
| 2891 |
|
| #endif |
| 2892 |
6 |
▶ | else if (strcmp(key, "jwt") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "jwt") == 0) |
0 |
6 |
6 |
|
| 2893 |
0 |
| nats_set_str_sv(&self->jwt, val); |
| 2894 |
6 |
▶ | else if (strcmp(key, "slow_consumer_bytes") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "slow_consumer_bytes") == 0) |
0 |
6 |
6 |
|
| 2895 |
0 |
| self->slow_consumer_bytes = (size_t)SvUV(val); |
| 2896 |
6 |
▶ | else if (strcmp(key, "on_slow_consumer") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "on_slow_consumer") == 0) |
0 |
6 |
6 |
|
| 2897 |
0 |
▶ | self->on_slow_consumer = SvOK(val) ? newSVsv(val) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| self->on_slow_consumer = SvOK(val) ? newSVsv(val) : NULL; |
0 |
0 |
0 |
|
| 2898 |
6 |
▶ | else if (strcmp(key, "on_lame_duck") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "on_lame_duck") == 0) |
0 |
6 |
6 |
|
| 2899 |
0 |
▶ | self->on_ldm = SvOK(val) ? newSVsv(val) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| self->on_ldm = SvOK(val) ? newSVsv(val) : NULL; |
0 |
0 |
0 |
|
| 2900 |
6 |
▶ | else if (strcmp(key, "loop") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(key, "loop") == 0) { |
5 |
1 |
6 |
|
| 2901 |
5 |
▶ | if (!SvOK(val)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(val)) { |
4 |
1 |
5 |
|
| 2902 |
|
| /* undef: keep the default loop */ |
| 2903 |
4 |
▶ | } else if (SvROK(val)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (SvROK(val)) { |
2 |
2 |
4 |
|
| 2904 |
2 |
| self->loop = (struct ev_loop *)SvIVx(SvRV(val)); |
| 2905 |
2 |
▶ | CLEAR_HANDLER(self->loop_sv); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->loop_sv); |
0 |
2 |
2 |
|
| 2906 |
2 |
| self->loop_sv = newSVsv(val); |
| 2907 |
|
| } else { |
| 2908 |
2 |
| croak("EV::Nats::new: 'loop' must be an EV::Loop"); |
| 2909 |
|
| } |
| 2910 |
|
| } |
| 2911 |
|
| else |
| 2912 |
1 |
| warn("EV::Nats::new: unknown option '%s'", key); |
| 2913 |
|
| } |
| 2914 |
|
| } |
| 2915 |
|
| |
| 2916 |
71 |
▶ | if (self->host || self->path) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->host || self->path) |
18 |
53 |
71 |
| if (self->host || self->path) |
1 |
17 |
18 |
|
| 2917 |
54 |
| nats_do_connect(self); |
| 2918 |
|
| |
| 2919 |
70 |
| sv_setiv(SvRV(guard), 0); |
| 2920 |
70 |
| RETVAL = self; |
| 2921 |
|
| OUTPUT: |
| 2922 |
|
| RETVAL |
| 2923 |
|
| CLEANUP: |
| 2924 |
70 |
| self->owner_sv = SvRV(ST(0)); |
| 2925 |
|
| |
| 2926 |
|
| void |
| 2927 |
|
| connect(self, host, port = 4222) |
| 2928 |
|
| EV::Nats self |
| 2929 |
|
| char *host |
| 2930 |
|
| int port |
| 2931 |
|
| CODE: |
| 2932 |
9 |
▶ | if (self->connected || self->connecting) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->connected || self->connecting) |
9 |
0 |
9 |
| if (self->connected || self->connecting) |
0 |
9 |
9 |
|
| 2933 |
0 |
| croak("already connected"); |
| 2934 |
9 |
▶ | if (self->owner_sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->owner_sv) |
9 |
0 |
9 |
|
| 2935 |
9 |
| sv_2mortal(SvREFCNT_inc(self->owner_sv)); |
| 2936 |
|
| |
| 2937 |
9 |
▶ | if (self->reconnect_timer_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->reconnect_timer_active) { |
0 |
9 |
9 |
|
| 2938 |
0 |
| ev_timer_stop(self->loop, &self->reconnect_timer); |
| 2939 |
0 |
| self->reconnect_timer_active = 0; |
| 2940 |
|
| } |
| 2941 |
|
| |
| 2942 |
9 |
| nats_set_str(&self->path, NULL); |
| 2943 |
9 |
| nats_set_str(&self->host, host); |
| 2944 |
9 |
| self->port = port; |
| 2945 |
9 |
| self->intentional_disconnect = 0; |
| 2946 |
9 |
| self->reconnect_attempts = 0; |
| 2947 |
|
| /* An explicit host supersedes any pool discovered from a previous |
| 2948 |
|
| server; the new INFO repopulates it. */ |
| 2949 |
9 |
| nats_free_server_pool(self); |
| 2950 |
|
| |
| 2951 |
9 |
| nats_connect_tcp(self); |
| 2952 |
|
| |
| 2953 |
|
| void |
| 2954 |
|
| connect_unix(self, path) |
| 2955 |
|
| EV::Nats self |
| 2956 |
|
| char *path |
| 2957 |
|
| CODE: |
| 2958 |
0 |
▶ | if (self->connected || self->connecting) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->connected || self->connecting) |
0 |
0 |
0 |
| if (self->connected || self->connecting) |
0 |
0 |
0 |
|
| 2959 |
0 |
| croak("already connected"); |
| 2960 |
0 |
▶ | if (self->owner_sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->owner_sv) |
0 |
0 |
0 |
|
| 2961 |
0 |
| sv_2mortal(SvREFCNT_inc(self->owner_sv)); |
| 2962 |
|
| |
| 2963 |
0 |
▶ | if (self->reconnect_timer_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->reconnect_timer_active) { |
0 |
0 |
0 |
|
| 2964 |
0 |
| ev_timer_stop(self->loop, &self->reconnect_timer); |
| 2965 |
0 |
| self->reconnect_timer_active = 0; |
| 2966 |
|
| } |
| 2967 |
|
| |
| 2968 |
0 |
| nats_set_str(&self->host, NULL); |
| 2969 |
0 |
| nats_set_str(&self->path, path); |
| 2970 |
0 |
| self->intentional_disconnect = 0; |
| 2971 |
0 |
| self->reconnect_attempts = 0; |
| 2972 |
0 |
| nats_free_server_pool(self); |
| 2973 |
|
| |
| 2974 |
0 |
| nats_connect_unix(self); |
| 2975 |
|
| |
| 2976 |
|
| void |
| 2977 |
|
| disconnect(self) |
| 2978 |
|
| EV::Nats self |
| 2979 |
|
| CODE: |
| 2980 |
31 |
| self->intentional_disconnect = 1; |
| 2981 |
31 |
▶ | if (self->reconnect_timer_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->reconnect_timer_active) { |
10 |
21 |
31 |
|
| 2982 |
10 |
| ev_timer_stop(self->loop, &self->reconnect_timer); |
| 2983 |
10 |
| self->reconnect_timer_active = 0; |
| 2984 |
|
| } |
| 2985 |
31 |
| nats_skip_waiting(self); |
| 2986 |
31 |
| nats_cleanup(self); |
| 2987 |
|
| |
| 2988 |
|
| int |
| 2989 |
|
| is_connected(self) |
| 2990 |
|
| EV::Nats self |
| 2991 |
|
| CODE: |
| 2992 |
7 |
▶ | RETVAL = self->connected; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->connected; |
1 |
6 |
7 |
|
| 2993 |
|
| OUTPUT: |
| 2994 |
|
| RETVAL |
| 2995 |
|
| |
| 2996 |
|
| SV * |
| 2997 |
|
| _timer(self, after, repeat, cb) |
| 2998 |
|
| EV::Nats self |
| 2999 |
|
| NV after |
| 3000 |
|
| NV repeat |
| 3001 |
|
| SV *cb |
| 3002 |
|
| CODE: |
| 3003 |
|
| { |
| 3004 |
17 |
| dSP; |
| 3005 |
17 |
| ENTER; SAVETMPS; |
| 3006 |
17 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
17 |
17 |
|
| 3007 |
17 |
▶ | EXTEND(SP, 4); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 4); |
0 |
17 |
17 |
|
| 3008 |
17 |
▶ | if (self->loop_sv) PUSHs(self->loop_sv); |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->loop_sv) PUSHs(self->loop_sv); |
5 |
12 |
17 |
|
| 3009 |
17 |
| PUSHs(sv_2mortal(newSVnv(after))); |
| 3010 |
17 |
| PUSHs(sv_2mortal(newSVnv(repeat))); |
| 3011 |
17 |
| PUSHs(cb); |
| 3012 |
17 |
| PUTBACK; |
| 3013 |
17 |
▶ | call_pv(self->loop_sv ? "EV::Loop::timer" : "EV::timer", G_SCALAR); |
Branch: 1 branch
| Branch | True | False | Total |
| call_pv(self->loop_sv ? "EV::Loop::timer" : "EV::timer", G_SCALAR); |
5 |
12 |
17 |
|
| 3014 |
17 |
| SPAGAIN; |
| 3015 |
17 |
| RETVAL = newSVsv(POPs); |
| 3016 |
17 |
| PUTBACK; |
| 3017 |
17 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
17 |
0 |
17 |
|
| 3018 |
|
| } |
| 3019 |
|
| OUTPUT: |
| 3020 |
|
| RETVAL |
| 3021 |
|
| |
| 3022 |
|
| void |
| 3023 |
|
| publish(self, subject, payload = &PL_sv_undef, reply = NULL) |
| 3024 |
|
| EV::Nats self |
| 3025 |
|
| SV *subject |
| 3026 |
|
| SV *payload |
| 3027 |
|
| const char *reply |
| 3028 |
|
| PREINIT: |
| 3029 |
|
| char hdr[MAX_CONTROL_LINE]; |
| 3030 |
|
| STRLEN subj_len, pay_len; |
| 3031 |
|
| const char *subj_pv, *pay_pv; |
| 3032 |
|
| CODE: |
| 3033 |
77 |
▶ | NATS_CROAK_UNLESS_CONNECTED(self); |
Branch: 4 branches
| Branch | True | False | Total |
| NATS_CROAK_UNLESS_CONNECTED(self); |
22 |
55 |
77 |
| NATS_CROAK_UNLESS_CONNECTED(self); |
0 |
22 |
22 |
| NATS_CROAK_UNLESS_CONNECTED(self); |
0 |
0 |
0 |
| NATS_CROAK_UNLESS_CONNECTED(self); |
0 |
0 |
0 |
|
| 3034 |
|
| |
| 3035 |
77 |
▶ | if (!SvOK(subject)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(subject)) |
1 |
76 |
77 |
|
| 3036 |
1 |
| croak("subject required"); |
| 3037 |
76 |
| subj_pv = SvPV(subject, subj_len); |
| 3038 |
76 |
▶ | if (subj_len == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (subj_len == 0) |
1 |
75 |
76 |
|
| 3039 |
1 |
| croak("subject must not be empty"); |
| 3040 |
75 |
▶ | if (!nats_field_ok(subj_pv, subj_len)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nats_field_ok(subj_pv, subj_len)) |
1 |
74 |
75 |
|
| 3041 |
1 |
| croak("subject must not contain whitespace"); |
| 3042 |
74 |
▶ | if (reply && *reply && !nats_field_ok(reply, strlen(reply))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (reply && *reply && !nats_field_ok(reply, strlen(reply))) |
4 |
70 |
74 |
| if (reply && *reply && !nats_field_ok(reply, strlen(reply))) |
4 |
0 |
4 |
| if (reply && *reply && !nats_field_ok(reply, strlen(reply))) |
1 |
3 |
4 |
|
| 3043 |
1 |
| croak("reply subject must not contain whitespace"); |
| 3044 |
|
| |
| 3045 |
73 |
▶ | if (SvOK(payload)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(payload)) { |
73 |
0 |
73 |
|
| 3046 |
73 |
| pay_pv = SvPV(payload, pay_len); |
| 3047 |
|
| } else { |
| 3048 |
0 |
| pay_pv = ""; |
| 3049 |
0 |
| pay_len = 0; |
| 3050 |
|
| } |
| 3051 |
|
| |
| 3052 |
73 |
▶ | if (self->max_payload > 0 && pay_len > (size_t)self->max_payload) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->max_payload > 0 && pay_len > (size_t)self->max_payload) |
73 |
0 |
73 |
| if (self->max_payload > 0 && pay_len > (size_t)self->max_payload) |
0 |
73 |
73 |
|
| 3053 |
0 |
| croak("payload exceeds max_payload (%d)", self->max_payload); |
| 3054 |
|
| |
| 3055 |
|
| int hdr_len; |
| 3056 |
73 |
▶ | if (reply && *reply) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (reply && *reply) { |
3 |
70 |
73 |
| if (reply && *reply) { |
3 |
0 |
3 |
|
| 3057 |
3 |
| hdr_len = snprintf(hdr, sizeof(hdr), "PUB %.*s %s %lu\r\n", |
| 3058 |
|
| (int)subj_len, subj_pv, reply, (unsigned long)pay_len); |
| 3059 |
|
| } else { |
| 3060 |
70 |
| hdr_len = snprintf(hdr, sizeof(hdr), "PUB %.*s %lu\r\n", |
| 3061 |
|
| (int)subj_len, subj_pv, (unsigned long)pay_len); |
| 3062 |
|
| } |
| 3063 |
73 |
▶ | NATS_CLAMP_LEN(hdr_len, sizeof(hdr)); |
Branch: 2 branches
| Branch | True | False | Total |
| NATS_CLAMP_LEN(hdr_len, sizeof(hdr)); |
0 |
73 |
73 |
| NATS_CLAMP_LEN(hdr_len, sizeof(hdr)); |
0 |
73 |
73 |
|
| 3064 |
|
| |
| 3065 |
73 |
| size_t total = hdr_len + pay_len + 2; |
| 3066 |
73 |
▶ | if (!nats_queue_cap_ok(self, total)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nats_queue_cap_ok(self, total)) |
4 |
69 |
73 |
|
| 3067 |
4 |
| croak("reconnect buffer full (%" UVuf " byte limit)", |
| 3068 |
|
| (UV)self->reconnect_buf_size); |
| 3069 |
|
| |
| 3070 |
69 |
| self->msgs_out++; |
| 3071 |
69 |
| self->bytes_out += pay_len; |
| 3072 |
|
| |
| 3073 |
69 |
▶ | if (self->connected) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connected) { |
55 |
14 |
69 |
|
| 3074 |
55 |
| wbuf_append(self, hdr, hdr_len); |
| 3075 |
55 |
▶ | if (pay_len > 0) wbuf_append(self, pay_pv, pay_len); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pay_len > 0) wbuf_append(self, pay_pv, pay_len); |
55 |
0 |
55 |
|
| 3076 |
55 |
| wbuf_append(self, "\r\n", 2); |
| 3077 |
55 |
| nats_schedule_write(self); |
| 3078 |
|
| } else { |
| 3079 |
|
| char *buf; |
| 3080 |
14 |
| Newx(buf, total, char); |
| 3081 |
14 |
| memcpy(buf, hdr, hdr_len); |
| 3082 |
14 |
▶ | if (pay_len > 0) memcpy(buf + hdr_len, pay_pv, pay_len); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pay_len > 0) memcpy(buf + hdr_len, pay_pv, pay_len); |
14 |
0 |
14 |
|
| 3083 |
14 |
| buf[hdr_len + pay_len] = '\r'; |
| 3084 |
14 |
| buf[hdr_len + pay_len + 1] = '\n'; |
| 3085 |
14 |
| nats_queue_write(self, buf, total); |
| 3086 |
14 |
| Safefree(buf); |
| 3087 |
|
| } |
| 3088 |
|
| |
| 3089 |
|
| void |
| 3090 |
|
| hpublish(self, subject, headers, payload = &PL_sv_undef, reply = NULL) |
| 3091 |
|
| EV::Nats self |
| 3092 |
|
| SV *subject |
| 3093 |
|
| SV *headers |
| 3094 |
|
| SV *payload |
| 3095 |
|
| const char *reply |
| 3096 |
|
| PREINIT: |
| 3097 |
|
| char hdr[MAX_CONTROL_LINE]; |
| 3098 |
|
| STRLEN subj_len, hdr_data_len, pay_len; |
| 3099 |
|
| const char *subj_pv, *hdr_data_pv, *pay_pv; |
| 3100 |
|
| CODE: |
| 3101 |
21 |
▶ | NATS_CROAK_UNLESS_CONNECTED(self); |
Branch: 4 branches
| Branch | True | False | Total |
| NATS_CROAK_UNLESS_CONNECTED(self); |
15 |
6 |
21 |
| NATS_CROAK_UNLESS_CONNECTED(self); |
5 |
10 |
15 |
| NATS_CROAK_UNLESS_CONNECTED(self); |
3 |
2 |
5 |
| NATS_CROAK_UNLESS_CONNECTED(self); |
2 |
1 |
3 |
|
| 3102 |
|
| |
| 3103 |
17 |
▶ | if (!SvOK(subject)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(subject)) |
1 |
16 |
17 |
|
| 3104 |
1 |
| croak("subject required"); |
| 3105 |
16 |
| subj_pv = SvPV(subject, subj_len); |
| 3106 |
16 |
▶ | if (subj_len == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (subj_len == 0) |
0 |
16 |
16 |
|
| 3107 |
0 |
| croak("subject must not be empty"); |
| 3108 |
16 |
▶ | if (!nats_field_ok(subj_pv, subj_len)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nats_field_ok(subj_pv, subj_len)) |
1 |
15 |
16 |
|
| 3109 |
1 |
| croak("subject must not contain whitespace"); |
| 3110 |
15 |
▶ | if (reply && *reply && !nats_field_ok(reply, strlen(reply))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (reply && *reply && !nats_field_ok(reply, strlen(reply))) |
8 |
7 |
15 |
| if (reply && *reply && !nats_field_ok(reply, strlen(reply))) |
8 |
0 |
8 |
| if (reply && *reply && !nats_field_ok(reply, strlen(reply))) |
0 |
8 |
8 |
|
| 3111 |
0 |
| croak("reply subject must not contain whitespace"); |
| 3112 |
|
| |
| 3113 |
15 |
▶ | if (!SvOK(headers)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(headers)) |
1 |
14 |
15 |
|
| 3114 |
1 |
| croak("headers required"); |
| 3115 |
14 |
| hdr_data_pv = SvPV(headers, hdr_data_len); |
| 3116 |
14 |
▶ | if (hdr_data_len == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr_data_len == 0) |
1 |
13 |
14 |
|
| 3117 |
1 |
| croak("headers must not be empty"); |
| 3118 |
13 |
▶ | if (hdr_data_len < 8 || memcmp(hdr_data_pv, "NATS/1.0", 8) != 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (hdr_data_len < 8 || memcmp(hdr_data_pv, "NATS/1.0", 8) != 0) |
13 |
0 |
13 |
| if (hdr_data_len < 8 || memcmp(hdr_data_pv, "NATS/1.0", 8) != 0) |
1 |
12 |
13 |
|
| 3119 |
1 |
| croak("headers must start with NATS/1.0"); |
| 3120 |
|
| |
| 3121 |
12 |
▶ | if (SvOK(payload)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(payload)) { |
12 |
0 |
12 |
|
| 3122 |
12 |
| pay_pv = SvPV(payload, pay_len); |
| 3123 |
|
| } else { |
| 3124 |
0 |
| pay_pv = ""; |
| 3125 |
0 |
| pay_len = 0; |
| 3126 |
|
| } |
| 3127 |
|
| |
| 3128 |
12 |
| size_t total_size = hdr_data_len + pay_len; |
| 3129 |
12 |
▶ | if (self->max_payload > 0 && total_size > (size_t)self->max_payload) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->max_payload > 0 && total_size > (size_t)self->max_payload) |
12 |
0 |
12 |
| if (self->max_payload > 0 && total_size > (size_t)self->max_payload) |
0 |
12 |
12 |
|
| 3130 |
0 |
| croak("message exceeds max_payload (%d)", self->max_payload); |
| 3131 |
|
| |
| 3132 |
|
| int cmd_len; |
| 3133 |
12 |
▶ | if (reply && *reply) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (reply && *reply) { |
8 |
4 |
12 |
| if (reply && *reply) { |
8 |
0 |
8 |
|
| 3134 |
8 |
| cmd_len = snprintf(hdr, sizeof(hdr), "HPUB %.*s %s %lu %lu\r\n", |
| 3135 |
|
| (int)subj_len, subj_pv, reply, |
| 3136 |
|
| (unsigned long)hdr_data_len, (unsigned long)total_size); |
| 3137 |
|
| } else { |
| 3138 |
4 |
| cmd_len = snprintf(hdr, sizeof(hdr), "HPUB %.*s %lu %lu\r\n", |
| 3139 |
|
| (int)subj_len, subj_pv, |
| 3140 |
|
| (unsigned long)hdr_data_len, (unsigned long)total_size); |
| 3141 |
|
| } |
| 3142 |
12 |
▶ | NATS_CLAMP_LEN(cmd_len, sizeof(hdr)); |
Branch: 2 branches
| Branch | True | False | Total |
| NATS_CLAMP_LEN(cmd_len, sizeof(hdr)); |
0 |
12 |
12 |
| NATS_CLAMP_LEN(cmd_len, sizeof(hdr)); |
0 |
12 |
12 |
|
| 3143 |
|
| |
| 3144 |
12 |
| size_t total = cmd_len + total_size + 2; |
| 3145 |
12 |
▶ | if (!nats_queue_cap_ok(self, total)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nats_queue_cap_ok(self, total)) |
3 |
9 |
12 |
|
| 3146 |
3 |
| croak("reconnect buffer full (%" UVuf " byte limit)", |
| 3147 |
|
| (UV)self->reconnect_buf_size); |
| 3148 |
|
| |
| 3149 |
9 |
| self->msgs_out++; |
| 3150 |
9 |
| self->bytes_out += total_size; |
| 3151 |
|
| |
| 3152 |
9 |
▶ | if (self->connected) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connected) { |
6 |
3 |
9 |
|
| 3153 |
6 |
| wbuf_append(self, hdr, cmd_len); |
| 3154 |
6 |
| wbuf_append(self, hdr_data_pv, hdr_data_len); |
| 3155 |
6 |
▶ | if (pay_len > 0) wbuf_append(self, pay_pv, pay_len); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pay_len > 0) wbuf_append(self, pay_pv, pay_len); |
6 |
0 |
6 |
|
| 3156 |
6 |
| wbuf_append(self, "\r\n", 2); |
| 3157 |
6 |
| nats_schedule_write(self); |
| 3158 |
|
| } else { |
| 3159 |
|
| char *buf; |
| 3160 |
3 |
| Newx(buf, total, char); |
| 3161 |
3 |
| memcpy(buf, hdr, cmd_len); |
| 3162 |
3 |
| memcpy(buf + cmd_len, hdr_data_pv, hdr_data_len); |
| 3163 |
3 |
▶ | if (pay_len > 0) memcpy(buf + cmd_len + hdr_data_len, pay_pv, pay_len); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pay_len > 0) memcpy(buf + cmd_len + hdr_data_len, pay_pv, pay_len); |
0 |
3 |
3 |
|
| 3164 |
3 |
| buf[cmd_len + total_size] = '\r'; |
| 3165 |
3 |
| buf[cmd_len + total_size + 1] = '\n'; |
| 3166 |
3 |
| nats_queue_write(self, buf, total); |
| 3167 |
3 |
| Safefree(buf); |
| 3168 |
|
| } |
| 3169 |
|
| |
| 3170 |
|
| UV |
| 3171 |
|
| subscribe(self, subject, cb, queue_group = NULL) |
| 3172 |
|
| EV::Nats self |
| 3173 |
|
| SV *subject |
| 3174 |
|
| SV *cb |
| 3175 |
|
| const char *queue_group |
| 3176 |
|
| PREINIT: |
| 3177 |
|
| nats_sub_t *sub; |
| 3178 |
|
| char buf[MAX_CONTROL_LINE]; |
| 3179 |
|
| STRLEN subj_len; |
| 3180 |
|
| const char *subj_pv; |
| 3181 |
|
| CODE: |
| 3182 |
24 |
▶ | if (!SvOK(subject)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(subject)) |
1 |
23 |
24 |
|
| 3183 |
1 |
| croak("subject required"); |
| 3184 |
23 |
| subj_pv = SvPV(subject, subj_len); |
| 3185 |
23 |
▶ | if (subj_len == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (subj_len == 0) |
1 |
22 |
23 |
|
| 3186 |
1 |
| croak("subject must not be empty"); |
| 3187 |
22 |
▶ | if (!nats_field_ok(subj_pv, subj_len)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nats_field_ok(subj_pv, subj_len)) |
1 |
21 |
22 |
|
| 3188 |
1 |
| croak("subject must not contain whitespace"); |
| 3189 |
21 |
▶ | if (queue_group && *queue_group && !nats_field_ok(queue_group, strlen(queue_group))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (queue_group && *queue_group && !nats_field_ok(queue_group, strlen(queue_group))) |
1 |
20 |
21 |
| if (queue_group && *queue_group && !nats_field_ok(queue_group, strlen(queue_group))) |
1 |
0 |
1 |
| if (queue_group && *queue_group && !nats_field_ok(queue_group, strlen(queue_group))) |
1 |
0 |
1 |
|
| 3190 |
1 |
| croak("queue group must not contain whitespace"); |
| 3191 |
|
| |
| 3192 |
|
| /* Format before allocating: a cap refusal must strand nothing, and an |
| 3193 |
|
| unsent-but-registered sub would ghost-resubscribe on reconnect. */ |
| 3194 |
20 |
| uint64_t sid = self->next_sid; |
| 3195 |
|
| int n; |
| 3196 |
20 |
▶ | if (queue_group && *queue_group) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (queue_group && *queue_group) { |
0 |
20 |
20 |
| if (queue_group && *queue_group) { |
0 |
0 |
0 |
|
| 3197 |
0 |
| n = snprintf(buf, sizeof(buf), "SUB %.*s %s %" UVuf "\r\n", |
| 3198 |
|
| (int)subj_len, subj_pv, queue_group, (UV)sid); |
| 3199 |
|
| } else { |
| 3200 |
20 |
| n = snprintf(buf, sizeof(buf), "SUB %.*s %" UVuf "\r\n", |
| 3201 |
|
| (int)subj_len, subj_pv, (UV)sid); |
| 3202 |
|
| } |
| 3203 |
20 |
▶ | NATS_CLAMP_LEN(n, sizeof(buf)); |
Branch: 2 branches
| Branch | True | False | Total |
| NATS_CLAMP_LEN(n, sizeof(buf)); |
0 |
20 |
20 |
| NATS_CLAMP_LEN(n, sizeof(buf)); |
0 |
20 |
20 |
|
| 3204 |
|
| |
| 3205 |
20 |
▶ | int will_queue = self->connected || self->connecting; |
Branch: 2 branches
| Branch | True | False | Total |
| int will_queue = self->connected || self->connecting; |
7 |
13 |
20 |
| int will_queue = self->connected || self->connecting; |
7 |
0 |
7 |
|
| 3206 |
20 |
▶ | if (will_queue && !nats_queue_cap_ok(self, n)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (will_queue && !nats_queue_cap_ok(self, n)) |
20 |
0 |
20 |
| if (will_queue && !nats_queue_cap_ok(self, n)) |
2 |
18 |
20 |
|
| 3207 |
2 |
| croak("reconnect buffer full (%" UVuf " byte limit)", |
| 3208 |
|
| (UV)self->reconnect_buf_size); |
| 3209 |
|
| |
| 3210 |
18 |
| Newxz(sub, 1, nats_sub_t); |
| 3211 |
18 |
| sub->sid = self->next_sid++; |
| 3212 |
18 |
| sub->subject = newSVpvn(subj_pv, subj_len); |
| 3213 |
18 |
| sub->cb = newSVsv(cb); |
| 3214 |
18 |
▶ | sub->queue_group = (queue_group && *queue_group) ? newSVpv(queue_group, 0) : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| sub->queue_group = (queue_group && *queue_group) ? newSVpv(queue_group, 0) : NULL; |
0 |
18 |
18 |
| sub->queue_group = (queue_group && *queue_group) ? newSVpv(queue_group, 0) : NULL; |
0 |
0 |
0 |
|
| 3215 |
18 |
| sub->max_msgs = 0; |
| 3216 |
18 |
| sub->received = 0; |
| 3217 |
18 |
| nats_register_sub(self, sub); |
| 3218 |
|
| |
| 3219 |
18 |
▶ | if (will_queue) |
Branch: 1 branch
| Branch | True | False | Total |
| if (will_queue) |
18 |
0 |
18 |
|
| 3220 |
18 |
| nats_queue_write(self, buf, n); |
| 3221 |
|
| |
| 3222 |
18 |
▶ | RETVAL = (UV)sid; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)sid; |
10 |
8 |
18 |
|
| 3223 |
|
| OUTPUT: |
| 3224 |
|
| RETVAL |
| 3225 |
|
| |
| 3226 |
|
| void |
| 3227 |
|
| unsubscribe(self, sid, max_msgs = 0) |
| 3228 |
|
| EV::Nats self |
| 3229 |
|
| UV sid |
| 3230 |
|
| int max_msgs |
| 3231 |
|
| PREINIT: |
| 3232 |
|
| char buf[64]; |
| 3233 |
|
| CODE: |
| 3234 |
|
| /* Queue UNSUB while connected/connecting/reconnecting, mirroring |
| 3235 |
|
| subscribe(). When fully disconnected (no reconnect armed) the |
| 3236 |
|
| local-only update is enough; nats_resub_all will not resubscribe |
| 3237 |
|
| a removed sub on the next reconnect. */ |
| 3238 |
12 |
▶ | int can_queue = self->connected || self->connecting |
Branch: 1 branch
| Branch | True | False | Total |
| int can_queue = self->connected || self->connecting |
4 |
8 |
12 |
|
| 3239 |
34 |
▶ | || (self->reconnect_enabled && self->reconnect_timer_active); |
Branch: 3 branches
| Branch | True | False | Total |
| || (self->reconnect_enabled && self->reconnect_timer_active); |
12 |
10 |
22 |
| || (self->reconnect_enabled && self->reconnect_timer_active); |
4 |
0 |
4 |
| || (self->reconnect_enabled && self->reconnect_timer_active); |
4 |
0 |
4 |
|
| 3240 |
|
| |
| 3241 |
22 |
▶ | if (max_msgs > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (max_msgs > 0) { |
9 |
13 |
22 |
|
| 3242 |
9 |
| nats_sub_t *sub = nats_find_sub(self, (uint64_t)sid); |
| 3243 |
9 |
▶ | if (sub) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sub) |
9 |
0 |
9 |
|
| 3244 |
9 |
| sub->max_msgs = max_msgs; |
| 3245 |
|
| |
| 3246 |
9 |
| int n = snprintf(buf, sizeof(buf), "UNSUB %" UVuf " %d\r\n", sid, max_msgs); |
| 3247 |
9 |
▶ | if (can_queue) |
Branch: 1 branch
| Branch | True | False | Total |
| if (can_queue) |
9 |
0 |
9 |
|
| 3248 |
9 |
| nats_queue_write(self, buf, n); |
| 3249 |
|
| } else { |
| 3250 |
13 |
| nats_sub_t *sub = nats_find_sub(self, (uint64_t)sid); |
| 3251 |
13 |
▶ | if (sub) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sub) |
13 |
0 |
13 |
|
| 3252 |
13 |
| nats_remove_sub(self, sub); |
| 3253 |
13 |
| int n = snprintf(buf, sizeof(buf), "UNSUB %" UVuf "\r\n", sid); |
| 3254 |
13 |
▶ | if (can_queue) |
Branch: 1 branch
| Branch | True | False | Total |
| if (can_queue) |
13 |
0 |
13 |
|
| 3255 |
13 |
| nats_queue_write(self, buf, n); |
| 3256 |
|
| } |
| 3257 |
|
| |
| 3258 |
|
| void |
| 3259 |
|
| request(self, subject, payload, cb, timeout_ms = 5000) |
| 3260 |
|
| EV::Nats self |
| 3261 |
|
| SV *subject |
| 3262 |
|
| SV *payload |
| 3263 |
|
| SV *cb |
| 3264 |
|
| int timeout_ms |
| 3265 |
|
| PREINIT: |
| 3266 |
|
| char reply_subj[80]; |
| 3267 |
|
| char hdr[MAX_CONTROL_LINE]; |
| 3268 |
|
| STRLEN subj_len, pay_len; |
| 3269 |
|
| const char *subj_pv, *pay_pv; |
| 3270 |
|
| CODE: |
| 3271 |
19 |
▶ | NATS_CROAK_UNLESS_CONNECTED(self); |
Branch: 4 branches
| Branch | True | False | Total |
| NATS_CROAK_UNLESS_CONNECTED(self); |
7 |
12 |
19 |
| NATS_CROAK_UNLESS_CONNECTED(self); |
1 |
6 |
7 |
| NATS_CROAK_UNLESS_CONNECTED(self); |
0 |
1 |
1 |
| NATS_CROAK_UNLESS_CONNECTED(self); |
0 |
0 |
0 |
|
| 3272 |
|
| |
| 3273 |
18 |
▶ | if (!SvOK(subject)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(subject)) |
1 |
17 |
18 |
|
| 3274 |
1 |
| croak("subject required"); |
| 3275 |
17 |
| subj_pv = SvPV(subject, subj_len); |
| 3276 |
17 |
▶ | if (subj_len == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (subj_len == 0) |
1 |
16 |
17 |
|
| 3277 |
1 |
| croak("subject must not be empty"); |
| 3278 |
16 |
▶ | if (!nats_field_ok(subj_pv, subj_len)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nats_field_ok(subj_pv, subj_len)) |
1 |
15 |
16 |
|
| 3279 |
1 |
| croak("subject must not contain whitespace"); |
| 3280 |
15 |
▶ | if (SvOK(payload)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(payload)) { |
15 |
0 |
15 |
|
| 3281 |
15 |
| pay_pv = SvPV(payload, pay_len); |
| 3282 |
|
| } else { |
| 3283 |
0 |
| pay_pv = ""; |
| 3284 |
0 |
| pay_len = 0; |
| 3285 |
|
| } |
| 3286 |
|
| |
| 3287 |
15 |
▶ | if (self->max_payload > 0 && pay_len > (size_t)self->max_payload) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->max_payload > 0 && pay_len > (size_t)self->max_payload) |
15 |
0 |
15 |
| if (self->max_payload > 0 && pay_len > (size_t)self->max_payload) |
0 |
15 |
15 |
|
| 3288 |
0 |
| croak("payload exceeds max_payload (%d)", self->max_payload); |
| 3289 |
|
| |
| 3290 |
15 |
▶ | if (!self->inbox_sub_sid) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->inbox_sub_sid) { |
6 |
9 |
15 |
|
| 3291 |
|
| char inbox_wild[48]; |
| 3292 |
6 |
| snprintf(inbox_wild, sizeof(inbox_wild), "%s*", self->inbox_prefix); |
| 3293 |
|
| |
| 3294 |
|
| /* Format before allocating: a cap refusal here must leave |
| 3295 |
|
| inbox_sub_sid unset so the next request retries cleanly. */ |
| 3296 |
|
| char sbuf[MAX_CONTROL_LINE]; |
| 3297 |
6 |
| int sn = snprintf(sbuf, sizeof(sbuf), "SUB %s %" UVuf "\r\n", |
| 3298 |
6 |
| inbox_wild, (UV)self->next_sid); |
| 3299 |
6 |
▶ | NATS_CLAMP_LEN(sn, sizeof(sbuf)); |
Branch: 2 branches
| Branch | True | False | Total |
| NATS_CLAMP_LEN(sn, sizeof(sbuf)); |
0 |
6 |
6 |
| NATS_CLAMP_LEN(sn, sizeof(sbuf)); |
0 |
6 |
6 |
|
| 3300 |
6 |
▶ | if (!nats_queue_cap_ok(self, sn)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nats_queue_cap_ok(self, sn)) |
2 |
4 |
6 |
|
| 3301 |
2 |
| croak("reconnect buffer full (%" UVuf " byte limit)", |
| 3302 |
|
| (UV)self->reconnect_buf_size); |
| 3303 |
|
| |
| 3304 |
|
| nats_sub_t *sub; |
| 3305 |
4 |
| Newxz(sub, 1, nats_sub_t); |
| 3306 |
4 |
| sub->sid = self->next_sid++; |
| 3307 |
4 |
| sub->subject = newSVpv(inbox_wild, 0); |
| 3308 |
4 |
| sub->cb = NULL; |
| 3309 |
4 |
| sub->queue_group = NULL; |
| 3310 |
4 |
| nats_register_sub(self, sub); |
| 3311 |
4 |
| self->inbox_sub_sid = sub->sid; |
| 3312 |
|
| |
| 3313 |
4 |
| nats_queue_write(self, sbuf, sn); |
| 3314 |
|
| } |
| 3315 |
|
| |
| 3316 |
13 |
| uint64_t req_id = self->next_req_id++; |
| 3317 |
13 |
| snprintf(reply_subj, sizeof(reply_subj), "%s%" UVuf, self->inbox_prefix, (UV)req_id); |
| 3318 |
|
| |
| 3319 |
13 |
| int hdr_len = snprintf(hdr, sizeof(hdr), "PUB %.*s %s %lu\r\n", |
| 3320 |
|
| (int)subj_len, subj_pv, reply_subj, (unsigned long)pay_len); |
| 3321 |
13 |
▶ | NATS_CLAMP_LEN(hdr_len, sizeof(hdr)); |
Branch: 2 branches
| Branch | True | False | Total |
| NATS_CLAMP_LEN(hdr_len, sizeof(hdr)); |
0 |
13 |
13 |
| NATS_CLAMP_LEN(hdr_len, sizeof(hdr)); |
0 |
13 |
13 |
|
| 3322 |
|
| |
| 3323 |
13 |
| size_t total = hdr_len + pay_len + 2; |
| 3324 |
13 |
▶ | if (!nats_queue_cap_ok(self, total)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nats_queue_cap_ok(self, total)) |
1 |
12 |
13 |
|
| 3325 |
1 |
| croak("reconnect buffer full (%" UVuf " byte limit)", |
| 3326 |
|
| (UV)self->reconnect_buf_size); |
| 3327 |
|
| |
| 3328 |
12 |
| self->msgs_out++; |
| 3329 |
12 |
| self->bytes_out += pay_len; |
| 3330 |
|
| |
| 3331 |
|
| nats_req_t *req; |
| 3332 |
12 |
| Newxz(req, 1, nats_req_t); |
| 3333 |
12 |
| req->req_id = req_id; |
| 3334 |
12 |
| req->cb = newSVsv(cb); |
| 3335 |
12 |
| ngx_queue_insert_tail(&self->req_queue, &req->queue); |
| 3336 |
|
| |
| 3337 |
12 |
▶ | if (timeout_ms > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (timeout_ms > 0) { |
12 |
0 |
12 |
|
| 3338 |
12 |
| ev_timer_init(&req->timer, nats_on_request_timeout, timeout_ms / 1000.0, 0.0); |
| 3339 |
12 |
| ev_timer_start(self->loop, &req->timer); |
| 3340 |
12 |
| req->timer_active = 1; |
| 3341 |
|
| } |
| 3342 |
|
| |
| 3343 |
|
| char *buf; |
| 3344 |
12 |
| Newx(buf, total, char); |
| 3345 |
12 |
| memcpy(buf, hdr, hdr_len); |
| 3346 |
12 |
▶ | if (pay_len > 0) memcpy(buf + hdr_len, pay_pv, pay_len); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pay_len > 0) memcpy(buf + hdr_len, pay_pv, pay_len); |
12 |
0 |
12 |
|
| 3347 |
12 |
| buf[hdr_len + pay_len] = '\r'; |
| 3348 |
12 |
| buf[hdr_len + pay_len + 1] = '\n'; |
| 3349 |
|
| |
| 3350 |
12 |
| nats_queue_write(self, buf, total); |
| 3351 |
12 |
| Safefree(buf); |
| 3352 |
|
| |
| 3353 |
|
| void |
| 3354 |
|
| ping(self) |
| 3355 |
|
| EV::Nats self |
| 3356 |
|
| CODE: |
| 3357 |
|
| /* Unlike publish, a PING cannot be deferred: bytes appended pre-CONNECT |
| 3358 |
|
| would hit the wire before the CONNECT handshake. */ |
| 3359 |
2 |
▶ | if (!self->connected) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->connected) |
1 |
1 |
2 |
|
| 3360 |
1 |
| croak("not connected"); |
| 3361 |
1 |
| wbuf_append(self, "PING\r\n", 6); |
| 3362 |
1 |
| nats_push_pong_marker(self); |
| 3363 |
1 |
| nats_try_write(self); |
| 3364 |
|
| |
| 3365 |
|
| void |
| 3366 |
|
| flush(self, cb = NULL) |
| 3367 |
|
| EV::Nats self |
| 3368 |
|
| SV *cb |
| 3369 |
|
| CODE: |
| 3370 |
9 |
▶ | if (!self->connected) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->connected) |
3 |
6 |
9 |
|
| 3371 |
3 |
| croak("not connected"); |
| 3372 |
6 |
| wbuf_append(self, "PING\r\n", 6); |
| 3373 |
|
| /* Queue before the write, always (a NULL-cb placeholder without a |
| 3374 |
|
| callback): on a synchronous write failure nats_cleanup drains this |
| 3375 |
|
| entry instead of stranding it, and the 1:1 PING/PONG accounting |
| 3376 |
|
| survives a cb-less flush. */ |
| 3377 |
|
| { |
| 3378 |
|
| nats_pong_cb_t *pcb; |
| 3379 |
6 |
| Newxz(pcb, 1, nats_pong_cb_t); |
| 3380 |
6 |
▶ | if (cb && SvOK(cb)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (cb && SvOK(cb)) |
5 |
1 |
6 |
| if (cb && SvOK(cb)) |
5 |
0 |
5 |
|
| 3381 |
5 |
| pcb->cb = newSVsv(cb); |
| 3382 |
6 |
| ngx_queue_insert_tail(&self->pong_cbs, &pcb->queue); |
| 3383 |
|
| } |
| 3384 |
6 |
| nats_try_write(self); |
| 3385 |
|
| |
| 3386 |
|
| SV * |
| 3387 |
|
| server_info(self) |
| 3388 |
|
| EV::Nats self |
| 3389 |
|
| CODE: |
| 3390 |
2 |
▶ | if (self->server_info_json) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->server_info_json) |
2 |
0 |
2 |
|
| 3391 |
2 |
| RETVAL = newSVsv(self->server_info_json); |
| 3392 |
|
| else |
| 3393 |
0 |
| RETVAL = &PL_sv_undef; |
| 3394 |
|
| OUTPUT: |
| 3395 |
|
| RETVAL |
| 3396 |
|
| |
| 3397 |
|
| int |
| 3398 |
|
| max_payload(self, ...) |
| 3399 |
|
| EV::Nats self |
| 3400 |
|
| CODE: |
| 3401 |
4 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
0 |
4 |
4 |
|
| 3402 |
0 |
| IV v = SvIV(ST(1)); |
| 3403 |
|
| /* clamp: a negative limit would wrap to SIZE_MAX in the size_t guards */ |
| 3404 |
0 |
▶ | self->max_payload = v < 0 ? 0 : (v > INT_MAX ? INT_MAX : (int)v); |
Branch: 2 branches
| Branch | True | False | Total |
| self->max_payload = v < 0 ? 0 : (v > INT_MAX ? INT_MAX : (int)v); |
0 |
0 |
0 |
| self->max_payload = v < 0 ? 0 : (v > INT_MAX ? INT_MAX : (int)v); |
0 |
0 |
0 |
|
| 3405 |
|
| } |
| 3406 |
4 |
▶ | RETVAL = self->max_payload; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->max_payload; |
1 |
3 |
4 |
|
| 3407 |
|
| OUTPUT: |
| 3408 |
|
| RETVAL |
| 3409 |
|
| |
| 3410 |
|
| int |
| 3411 |
|
| waiting_count(self) |
| 3412 |
|
| EV::Nats self |
| 3413 |
|
| CODE: |
| 3414 |
5 |
▶ | RETVAL = self->waiting_count; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->waiting_count; |
0 |
5 |
5 |
|
| 3415 |
|
| OUTPUT: |
| 3416 |
|
| RETVAL |
| 3417 |
|
| |
| 3418 |
|
| void |
| 3419 |
|
| skip_waiting(self) |
| 3420 |
|
| EV::Nats self |
| 3421 |
|
| CODE: |
| 3422 |
1 |
| nats_skip_waiting(self); |
| 3423 |
|
| |
| 3424 |
|
| void |
| 3425 |
|
| reconnect(self, enable, ...) |
| 3426 |
|
| EV::Nats self |
| 3427 |
|
| int enable |
| 3428 |
|
| CODE: |
| 3429 |
1 |
| self->reconnect_enabled = enable; |
| 3430 |
1 |
▶ | if (!enable && self->reconnect_timer_active) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!enable && self->reconnect_timer_active) { |
1 |
0 |
1 |
| if (!enable && self->reconnect_timer_active) { |
1 |
0 |
1 |
|
| 3431 |
1 |
| ev_timer_stop(self->loop, &self->reconnect_timer); |
| 3432 |
1 |
| self->reconnect_timer_active = 0; |
| 3433 |
|
| } |
| 3434 |
1 |
▶ | if (items > 2) self->reconnect_delay_ms = SvIV(ST(2)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) self->reconnect_delay_ms = SvIV(ST(2)); |
0 |
1 |
1 |
|
| 3435 |
1 |
▶ | if (items > 3) self->max_reconnect_attempts = SvIV(ST(3)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 3) self->max_reconnect_attempts = SvIV(ST(3)); |
0 |
1 |
1 |
|
| 3436 |
|
| |
| 3437 |
|
| int |
| 3438 |
|
| reconnect_enabled(self) |
| 3439 |
|
| EV::Nats self |
| 3440 |
|
| CODE: |
| 3441 |
0 |
▶ | RETVAL = self->reconnect_enabled; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->reconnect_enabled; |
0 |
0 |
0 |
|
| 3442 |
|
| OUTPUT: |
| 3443 |
|
| RETVAL |
| 3444 |
|
| |
| 3445 |
|
| UV |
| 3446 |
|
| reconnect_buf_size(self, ...) |
| 3447 |
|
| EV::Nats self |
| 3448 |
|
| CODE: |
| 3449 |
6 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
3 |
3 |
6 |
|
| 3450 |
3 |
| IV v = SvIV(ST(1)); |
| 3451 |
3 |
| self->reconnect_buf_size = v < 0 ? 0 : (size_t)v; |
| 3452 |
|
| } |
| 3453 |
6 |
▶ | RETVAL = (UV)self->reconnect_buf_size; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)self->reconnect_buf_size; |
0 |
6 |
6 |
|
| 3454 |
|
| OUTPUT: |
| 3455 |
|
| RETVAL |
| 3456 |
|
| |
| 3457 |
|
| int |
| 3458 |
|
| connect_timeout(self, ...) |
| 3459 |
|
| EV::Nats self |
| 3460 |
|
| CODE: |
| 3461 |
0 |
▶ | if (items > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) |
0 |
0 |
0 |
|
| 3462 |
0 |
| self->connect_timeout_ms = SvIV(ST(1)); |
| 3463 |
0 |
▶ | RETVAL = self->connect_timeout_ms; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->connect_timeout_ms; |
0 |
0 |
0 |
|
| 3464 |
|
| OUTPUT: |
| 3465 |
|
| RETVAL |
| 3466 |
|
| |
| 3467 |
|
| int |
| 3468 |
|
| ping_interval(self, ...) |
| 3469 |
|
| EV::Nats self |
| 3470 |
|
| CODE: |
| 3471 |
2 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
2 |
0 |
2 |
|
| 3472 |
2 |
| self->ping_interval_ms = SvIV(ST(1)); |
| 3473 |
|
| /* Live update: re-arm the running timer (stop first — ev_timer_set |
| 3474 |
|
| on an active watcher corrupts libev's heap). */ |
| 3475 |
2 |
▶ | if (self->ping_timer_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->ping_timer_active) { |
2 |
0 |
2 |
|
| 3476 |
2 |
| ev_timer_stop(self->loop, &self->ping_timer); |
| 3477 |
2 |
| self->ping_timer_active = 0; |
| 3478 |
2 |
| nats_start_ping_timer(self); |
| 3479 |
0 |
▶ | } else if (self->connected) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->connected) { |
0 |
0 |
0 |
|
| 3480 |
|
| /* Enabling on a live connection whose timer never started. */ |
| 3481 |
0 |
| nats_start_ping_timer(self); |
| 3482 |
|
| } |
| 3483 |
|
| } |
| 3484 |
2 |
▶ | RETVAL = self->ping_interval_ms; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->ping_interval_ms; |
0 |
2 |
2 |
|
| 3485 |
|
| OUTPUT: |
| 3486 |
|
| RETVAL |
| 3487 |
|
| |
| 3488 |
|
| int |
| 3489 |
|
| max_pings_outstanding(self, ...) |
| 3490 |
|
| EV::Nats self |
| 3491 |
|
| CODE: |
| 3492 |
0 |
▶ | if (items > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) |
0 |
0 |
0 |
|
| 3493 |
0 |
| self->max_pings_outstanding = SvIV(ST(1)); |
| 3494 |
0 |
▶ | RETVAL = self->max_pings_outstanding; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->max_pings_outstanding; |
0 |
0 |
0 |
|
| 3495 |
|
| OUTPUT: |
| 3496 |
|
| RETVAL |
| 3497 |
|
| |
| 3498 |
|
| int |
| 3499 |
|
| priority(self, ...) |
| 3500 |
|
| EV::Nats self |
| 3501 |
|
| CODE: |
| 3502 |
2 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
2 |
0 |
2 |
|
| 3503 |
2 |
| IV v = SvIV(ST(1)); |
| 3504 |
2 |
▶ | if (v < -2 || v > 2) |
Branch: 2 branches
| Branch | True | False | Total |
| if (v < -2 || v > 2) |
2 |
0 |
2 |
| if (v < -2 || v > 2) |
1 |
1 |
2 |
|
| 3505 |
1 |
| croak("priority must be between -2 and +2"); |
| 3506 |
1 |
| self->priority = (int)v; |
| 3507 |
|
| } |
| 3508 |
1 |
▶ | RETVAL = self->priority; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->priority; |
0 |
1 |
1 |
|
| 3509 |
|
| OUTPUT: |
| 3510 |
|
| RETVAL |
| 3511 |
|
| |
| 3512 |
|
| int |
| 3513 |
|
| keepalive(self, ...) |
| 3514 |
|
| EV::Nats self |
| 3515 |
|
| CODE: |
| 3516 |
0 |
▶ | if (items > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) |
0 |
0 |
0 |
|
| 3517 |
0 |
| self->keepalive = SvIV(ST(1)); |
| 3518 |
0 |
▶ | RETVAL = self->keepalive; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->keepalive; |
0 |
0 |
0 |
|
| 3519 |
|
| OUTPUT: |
| 3520 |
|
| RETVAL |
| 3521 |
|
| |
| 3522 |
|
| void |
| 3523 |
|
| on_error(self, ...) |
| 3524 |
|
| EV::Nats self |
| 3525 |
|
| PPCODE: |
| 3526 |
4 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
4 |
0 |
4 |
|
| 3527 |
4 |
▶ | CLEAR_HANDLER(self->on_error); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->on_error); |
4 |
0 |
4 |
|
| 3528 |
4 |
▶ | if (SvOK(ST(1))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(ST(1))) |
4 |
0 |
4 |
|
| 3529 |
4 |
| self->on_error = newSVsv(ST(1)); |
| 3530 |
|
| } |
| 3531 |
4 |
▶ | if (GIMME_V != G_VOID && self->on_error) |
Branch: 2 branches
| Branch | True | False | Total |
| if (GIMME_V != G_VOID && self->on_error) |
0 |
4 |
4 |
| if (GIMME_V != G_VOID && self->on_error) |
0 |
0 |
0 |
|
| 3532 |
0 |
| PUSHs(sv_2mortal(newSVsv(self->on_error))); |
| 3533 |
|
| |
| 3534 |
|
| void |
| 3535 |
|
| on_connect(self, ...) |
| 3536 |
|
| EV::Nats self |
| 3537 |
|
| PPCODE: |
| 3538 |
0 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
0 |
0 |
0 |
|
| 3539 |
0 |
▶ | CLEAR_HANDLER(self->on_connect); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->on_connect); |
0 |
0 |
0 |
|
| 3540 |
0 |
▶ | if (SvOK(ST(1))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(ST(1))) |
0 |
0 |
0 |
|
| 3541 |
0 |
| self->on_connect = newSVsv(ST(1)); |
| 3542 |
|
| } |
| 3543 |
0 |
▶ | if (GIMME_V != G_VOID && self->on_connect) |
Branch: 2 branches
| Branch | True | False | Total |
| if (GIMME_V != G_VOID && self->on_connect) |
0 |
0 |
0 |
| if (GIMME_V != G_VOID && self->on_connect) |
0 |
0 |
0 |
|
| 3544 |
0 |
| PUSHs(sv_2mortal(newSVsv(self->on_connect))); |
| 3545 |
|
| |
| 3546 |
|
| void |
| 3547 |
|
| on_disconnect(self, ...) |
| 3548 |
|
| EV::Nats self |
| 3549 |
|
| PPCODE: |
| 3550 |
1 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
1 |
0 |
1 |
|
| 3551 |
1 |
▶ | CLEAR_HANDLER(self->on_disconnect); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->on_disconnect); |
1 |
0 |
1 |
|
| 3552 |
1 |
▶ | if (SvOK(ST(1))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(ST(1))) |
0 |
1 |
1 |
|
| 3553 |
0 |
| self->on_disconnect = newSVsv(ST(1)); |
| 3554 |
|
| } |
| 3555 |
1 |
▶ | if (GIMME_V != G_VOID && self->on_disconnect) |
Branch: 2 branches
| Branch | True | False | Total |
| if (GIMME_V != G_VOID && self->on_disconnect) |
0 |
1 |
1 |
| if (GIMME_V != G_VOID && self->on_disconnect) |
0 |
0 |
0 |
|
| 3556 |
0 |
| PUSHs(sv_2mortal(newSVsv(self->on_disconnect))); |
| 3557 |
|
| |
| 3558 |
|
| #ifdef HAVE_OPENSSL |
| 3559 |
|
| |
| 3560 |
|
| void |
| 3561 |
|
| tls(self, enable, ca_file = NULL, skip_verify = 0) |
| 3562 |
|
| EV::Nats self |
| 3563 |
|
| int enable |
| 3564 |
|
| const char *ca_file |
| 3565 |
|
| int skip_verify |
| 3566 |
|
| CODE: |
| 3567 |
0 |
| self->tls = enable; |
| 3568 |
0 |
| self->tls_skip_verify = skip_verify; |
| 3569 |
0 |
▶ | nats_set_str(&self->tls_ca_file, (ca_file && *ca_file) ? ca_file : NULL); |
Branch: 2 branches
| Branch | True | False | Total |
| nats_set_str(&self->tls_ca_file, (ca_file && *ca_file) ? ca_file : NULL); |
0 |
0 |
0 |
| nats_set_str(&self->tls_ca_file, (ca_file && *ca_file) ? ca_file : NULL); |
0 |
0 |
0 |
|
| 3570 |
|
| /* Drop the cached context so the next handshake picks the new CA and |
| 3571 |
|
| verify mode up; an active SSL holds its own reference, so this is |
| 3572 |
|
| safe on a live connection. */ |
| 3573 |
0 |
▶ | if (self->ssl_ctx) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->ssl_ctx) { |
0 |
0 |
0 |
|
| 3574 |
0 |
| SSL_CTX_free(self->ssl_ctx); |
| 3575 |
0 |
| self->ssl_ctx = NULL; |
| 3576 |
|
| } |
| 3577 |
|
| |
| 3578 |
|
| #endif |
| 3579 |
|
| |
| 3580 |
|
| void |
| 3581 |
|
| stats(self) |
| 3582 |
|
| EV::Nats self |
| 3583 |
|
| PPCODE: |
| 3584 |
2 |
▶ | EXTEND(SP, 8); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 8); |
0 |
2 |
2 |
|
| 3585 |
2 |
| PUSHs(sv_2mortal(newSVpvs("msgs_in"))); |
| 3586 |
2 |
| PUSHs(sv_2mortal(newSVuv(self->msgs_in))); |
| 3587 |
2 |
| PUSHs(sv_2mortal(newSVpvs("msgs_out"))); |
| 3588 |
2 |
| PUSHs(sv_2mortal(newSVuv(self->msgs_out))); |
| 3589 |
2 |
| PUSHs(sv_2mortal(newSVpvs("bytes_in"))); |
| 3590 |
2 |
| PUSHs(sv_2mortal(newSVuv(self->bytes_in))); |
| 3591 |
2 |
| PUSHs(sv_2mortal(newSVpvs("bytes_out"))); |
| 3592 |
2 |
| PUSHs(sv_2mortal(newSVuv(self->bytes_out))); |
| 3593 |
|
| |
| 3594 |
|
| void |
| 3595 |
|
| reset_stats(self) |
| 3596 |
|
| EV::Nats self |
| 3597 |
|
| CODE: |
| 3598 |
0 |
| self->msgs_in = 0; |
| 3599 |
0 |
| self->msgs_out = 0; |
| 3600 |
0 |
| self->bytes_in = 0; |
| 3601 |
0 |
| self->bytes_out = 0; |
| 3602 |
|
| |
| 3603 |
|
| SV * |
| 3604 |
|
| new_inbox(self) |
| 3605 |
|
| EV::Nats self |
| 3606 |
|
| CODE: |
| 3607 |
|
| { |
| 3608 |
|
| char inbox[80]; |
| 3609 |
11 |
| int len = snprintf(inbox, sizeof(inbox), "%s%" UVuf, self->inbox_prefix, (UV)self->next_req_id++); |
| 3610 |
11 |
| RETVAL = newSVpvn(inbox, len); |
| 3611 |
|
| } |
| 3612 |
|
| OUTPUT: |
| 3613 |
|
| RETVAL |
| 3614 |
|
| |
| 3615 |
|
| int |
| 3616 |
|
| subscription_count(self) |
| 3617 |
|
| EV::Nats self |
| 3618 |
|
| CODE: |
| 3619 |
16 |
▶ | RETVAL = HvKEYS(self->sub_map); |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = HvKEYS(self->sub_map); |
0 |
16 |
16 |
| RETVAL = HvKEYS(self->sub_map); |
6 |
10 |
16 |
|
| 3620 |
|
| OUTPUT: |
| 3621 |
|
| RETVAL |
| 3622 |
|
| |
| 3623 |
|
| void |
| 3624 |
|
| batch(self, code) |
| 3625 |
|
| EV::Nats self |
| 3626 |
|
| SV *code |
| 3627 |
|
| CODE: |
| 3628 |
2 |
| self->batch_mode = 1; |
| 3629 |
2 |
| SV *pin = self->owner_sv; |
| 3630 |
2 |
▶ | if (pin) SvREFCNT_inc(pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pin) SvREFCNT_inc(pin); |
2 |
0 |
2 |
|
| 3631 |
|
| { |
| 3632 |
2 |
| dSP; |
| 3633 |
2 |
| ENTER; SAVETMPS; |
| 3634 |
2 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
2 |
2 |
|
| 3635 |
2 |
| PUTBACK; |
| 3636 |
|
| /* G_EVAL: a die() must not longjmp past the batch_mode reset below; |
| 3637 |
|
| that would wedge the write path for the connection's lifetime */ |
| 3638 |
2 |
| call_sv(code, G_DISCARD | G_EVAL); |
| 3639 |
2 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
2 |
2 |
|
| 3640 |
|
| } |
| 3641 |
2 |
| self->batch_mode = 0; |
| 3642 |
2 |
▶ | if (self->wbuf_len > self->wbuf_off) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->wbuf_len > self->wbuf_off) { |
0 |
2 |
2 |
|
| 3643 |
0 |
| self->wbuf_dirty = 1; |
| 3644 |
0 |
▶ | if (!self->prepare_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->prepare_active) { |
0 |
0 |
0 |
|
| 3645 |
0 |
| ev_prepare_start(self->loop, &self->prepare_watcher); |
| 3646 |
0 |
| self->prepare_active = 1; |
| 3647 |
|
| } |
| 3648 |
0 |
| nats_slow_check(self); |
| 3649 |
|
| } |
| 3650 |
2 |
▶ | if (pin) SvREFCNT_dec(pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pin) SvREFCNT_dec(pin); |
2 |
0 |
2 |
|
| 3651 |
|
| /* state restored; rethrow so the caller still sees the exception */ |
| 3652 |
2 |
▶ | if (SvTRUE(ERRSV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) |
2 |
0 |
2 |
| if (SvTRUE(ERRSV)) |
1 |
1 |
2 |
|
| 3653 |
1 |
▶ | croak_sv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| croak_sv(ERRSV); |
1 |
0 |
1 |
|
| 3654 |
|
| |
| 3655 |
|
| void |
| 3656 |
|
| slow_consumer(self, bytes_threshold, cb = NULL) |
| 3657 |
|
| EV::Nats self |
| 3658 |
|
| UV bytes_threshold |
| 3659 |
|
| SV *cb |
| 3660 |
|
| CODE: |
| 3661 |
0 |
| self->slow_consumer_bytes = (size_t)bytes_threshold; |
| 3662 |
0 |
| self->slow_fired = 0; |
| 3663 |
0 |
▶ | CLEAR_HANDLER(self->on_slow_consumer); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->on_slow_consumer); |
0 |
0 |
0 |
|
| 3664 |
0 |
▶ | if (cb && SvOK(cb)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (cb && SvOK(cb)) |
0 |
0 |
0 |
| if (cb && SvOK(cb)) |
0 |
0 |
0 |
|
| 3665 |
0 |
| self->on_slow_consumer = newSVsv(cb); |
| 3666 |
|
| |
| 3667 |
|
| void |
| 3668 |
|
| on_lame_duck(self, ...) |
| 3669 |
|
| EV::Nats self |
| 3670 |
|
| PPCODE: |
| 3671 |
0 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
0 |
0 |
0 |
|
| 3672 |
0 |
▶ | CLEAR_HANDLER(self->on_ldm); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->on_ldm); |
0 |
0 |
0 |
|
| 3673 |
0 |
▶ | if (SvOK(ST(1))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(ST(1))) |
0 |
0 |
0 |
|
| 3674 |
0 |
| self->on_ldm = newSVsv(ST(1)); |
| 3675 |
|
| } |
| 3676 |
0 |
▶ | if (GIMME_V != G_VOID && self->on_ldm) |
Branch: 2 branches
| Branch | True | False | Total |
| if (GIMME_V != G_VOID && self->on_ldm) |
0 |
0 |
0 |
| if (GIMME_V != G_VOID && self->on_ldm) |
0 |
0 |
0 |
|
| 3677 |
0 |
| PUSHs(sv_2mortal(newSVsv(self->on_ldm))); |
| 3678 |
|
| |
| 3679 |
|
| #ifdef HAVE_OPENSSL |
| 3680 |
|
| |
| 3681 |
|
| void |
| 3682 |
|
| nkey_seed(self, seed) |
| 3683 |
|
| EV::Nats self |
| 3684 |
|
| const char *seed |
| 3685 |
|
| CODE: |
| 3686 |
4 |
▶ | if (seed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (seed) { |
4 |
0 |
4 |
|
| 3687 |
|
| char pub[64]; |
| 3688 |
4 |
▶ | if (nats_nkey_public(seed, pub, sizeof(pub)) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nats_nkey_public(seed, pub, sizeof(pub)) <= 0) |
1 |
3 |
4 |
|
| 3689 |
1 |
| croak("invalid NKey seed"); |
| 3690 |
|
| } |
| 3691 |
3 |
| nats_set_str(&self->nkey_seed, seed); |
| 3692 |
|
| |
| 3693 |
|
| SV * |
| 3694 |
|
| nkey_public_from_seed(class, seed) |
| 3695 |
|
| SV *class |
| 3696 |
|
| const char *seed |
| 3697 |
|
| CODE: |
| 3698 |
|
| (void)class; |
| 3699 |
|
| { |
| 3700 |
|
| char pub[64]; |
| 3701 |
4 |
| int n = nats_nkey_public(seed, pub, sizeof(pub)); |
| 3702 |
4 |
▶ | if (n <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) |
2 |
2 |
4 |
|
| 3703 |
2 |
| croak("invalid NKey seed"); |
| 3704 |
2 |
| RETVAL = newSVpvn(pub, n); |
| 3705 |
|
| } |
| 3706 |
|
| OUTPUT: |
| 3707 |
|
| RETVAL |
| 3708 |
|
| |
| 3709 |
|
| SV * |
| 3710 |
|
| nkey_generate_user_seed(class) |
| 3711 |
|
| SV *class |
| 3712 |
|
| CODE: |
| 3713 |
|
| (void)class; |
| 3714 |
|
| { |
| 3715 |
|
| unsigned char raw[36]; |
| 3716 |
|
| /* S+User seed: b1 = PrefixByteSeed | (PrefixByteUser >> 5) = 0x95; |
| 3717 |
|
| b2 = (PrefixByteUser & 31) << 3 = 0x00. */ |
| 3718 |
3 |
| raw[0] = 0x95; |
| 3719 |
3 |
| raw[1] = 0x00; |
| 3720 |
3 |
| nats_random_bytes(raw + 2, 32); |
| 3721 |
3 |
| uint16_t crc = nats_crc16(raw, 34); |
| 3722 |
3 |
| raw[34] = crc & 0xFF; |
| 3723 |
3 |
| raw[35] = (crc >> 8) & 0xFF; |
| 3724 |
|
| /* 36 bytes -> 58 base32 chars (288 bits, 57 full chars + 1 flush). */ |
| 3725 |
|
| char enc[60]; |
| 3726 |
3 |
| int n = nats_base32_encode(raw, sizeof(raw), enc, sizeof(enc)); |
| 3727 |
3 |
▶ | if (n < 0) croak("base32 buffer overflow"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) croak("base32 buffer overflow"); |
0 |
3 |
3 |
|
| 3728 |
3 |
| RETVAL = newSVpvn(enc, n); |
| 3729 |
|
| } |
| 3730 |
|
| OUTPUT: |
| 3731 |
|
| RETVAL |
| 3732 |
|
| |
| 3733 |
|
| #endif |
| 3734 |
|
| |
| 3735 |
|
| void |
| 3736 |
|
| jwt(self, jwt_token) |
| 3737 |
|
| EV::Nats self |
| 3738 |
|
| const char *jwt_token |
| 3739 |
|
| CODE: |
| 3740 |
2 |
| nats_set_str(&self->jwt, jwt_token); |
| 3741 |
|
| |
| 3742 |
|
| void |
| 3743 |
|
| drain(self, cb = NULL) |
| 3744 |
|
| EV::Nats self |
| 3745 |
|
| SV *cb |
| 3746 |
|
| CODE: |
| 3747 |
8 |
▶ | if (!self->connected) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->connected) |
1 |
7 |
8 |
|
| 3748 |
1 |
| croak("not connected"); |
| 3749 |
7 |
▶ | if (self->draining) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->draining) { |
2 |
5 |
7 |
|
| 3750 |
2 |
▶ | if (cb && SvOK(cb)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (cb && SvOK(cb)) |
1 |
1 |
2 |
| if (cb && SvOK(cb)) |
1 |
0 |
1 |
|
| 3751 |
1 |
| croak("drain already in progress"); |
| 3752 |
1 |
| return; |
| 3753 |
|
| } |
| 3754 |
5 |
| self->draining = 1; |
| 3755 |
5 |
▶ | CLEAR_HANDLER(self->drain_cb); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_HANDLER(self->drain_cb); |
0 |
5 |
5 |
|
| 3756 |
5 |
▶ | if (cb && SvOK(cb)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (cb && SvOK(cb)) |
4 |
1 |
5 |
| if (cb && SvOK(cb)) |
4 |
0 |
4 |
|
| 3757 |
4 |
| self->drain_cb = newSVsv(cb); |
| 3758 |
|
| |
| 3759 |
|
| /* Send UNSUB for all subscriptions */ |
| 3760 |
|
| { |
| 3761 |
|
| ngx_queue_t *q; |
| 3762 |
|
| char buf[64]; |
| 3763 |
5 |
▶ | ngx_queue_foreach(q, &self->subs) { |
Branch: 2 branches
| Branch | True | False | Total |
| ngx_queue_foreach(q, &self->subs) { |
0 |
5 |
5 |
| ngx_queue_foreach(q, &self->subs) { |
0 |
0 |
0 |
|
| 3764 |
0 |
| nats_sub_t *sub = ngx_queue_data(q, nats_sub_t, queue); |
| 3765 |
0 |
| int n = snprintf(buf, sizeof(buf), "UNSUB %" UVuf "\r\n", (UV)sub->sid); |
| 3766 |
0 |
| wbuf_append(self, buf, n); |
| 3767 |
|
| } |
| 3768 |
|
| } |
| 3769 |
|
| |
| 3770 |
|
| /* Enqueue PING fence via pong_cbs — naturally ordered after any pending flush cbs. |
| 3771 |
|
| kind 1 marks drain completion; the PONG handler fires drain_cb on it. |
| 3772 |
|
| Queued before the write so a synchronous write failure cannot strand |
| 3773 |
|
| a marker that would tear down the next connection. */ |
| 3774 |
5 |
| wbuf_append(self, "PING\r\n", 6); |
| 3775 |
|
| { |
| 3776 |
|
| nats_pong_cb_t *pcb; |
| 3777 |
5 |
| Newxz(pcb, 1, nats_pong_cb_t); |
| 3778 |
5 |
| pcb->kind = 1; /* drain completion marker */ |
| 3779 |
5 |
| ngx_queue_insert_tail(&self->pong_cbs, &pcb->queue); |
| 3780 |
|
| } |
| 3781 |
5 |
| nats_try_write(self); |
| 3782 |
|
| |
| 3783 |
|
| void |
| 3784 |
|
| DESTROY(self) |
| 3785 |
|
| EV::Nats self |
| 3786 |
|
| CODE: |
| 3787 |
148 |
▶ | if (!self || self->magic != NATS_MAGIC_ALIVE) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!self || self->magic != NATS_MAGIC_ALIVE) |
78 |
70 |
148 |
| if (!self || self->magic != NATS_MAGIC_ALIVE) |
0 |
78 |
78 |
|
| 3788 |
70 |
| return; |
| 3789 |
|
| |
| 3790 |
78 |
| self->magic = 0; |
| 3791 |
78 |
| self->intentional_disconnect = 1; |
| 3792 |
|
| |
| 3793 |
78 |
▶ | if (PL_dirty) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_dirty) { |
18 |
60 |
78 |
|
| 3794 |
18 |
▶ | if (self->fd >= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->fd >= 0) |
1 |
17 |
18 |
|
| 3795 |
1 |
| close(self->fd); |
| 3796 |
18 |
| return; |
| 3797 |
|
| } |
| 3798 |
|
| |
| 3799 |
|
| /* DESTROY from inside an event-loop callback: defer the free until the |
| 3800 |
|
| outermost wrapper returns; its C frames still use self */ |
| 3801 |
60 |
▶ | if (self->cb_depth > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->cb_depth > 0) { |
4 |
56 |
60 |
|
| 3802 |
4 |
| self->destroy_pending = 1; |
| 3803 |
4 |
| return; |
| 3804 |
|
| } |
| 3805 |
|
| |
| 3806 |
56 |
| nats_destroy_now(self); |