Kafka.xs

filter: Statementstmt 74.2% 2182 / 2939 Branchbran 47.2% 1004 / 2126 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 62.9% 3186 / 5065 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/EV/Kafka.pm ↑ Summary
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 <netinet/in.h>
11 #include <netinet/tcp.h>
12 #include <netdb.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <arpa/inet.h>
18 #include <sys/time.h>
19  
20 #ifdef HAVE_OPENSSL
21 #include <openssl/ssl.h>
22 #include <openssl/err.h>
23 #include <openssl/x509v3.h>
24 #include <openssl/hmac.h>
25 #include <openssl/sha.h>
26 #include <openssl/rand.h>
27 #include <openssl/bio.h>
28 #include <openssl/buffer.h>
29 #include <openssl/evp.h>
30 #endif
31  
32 #ifdef HAVE_LZ4
33 #include <lz4.h>
34 #include <lz4frame.h> /* Kafka's wire format is LZ4 FRAME (KIP-57) */
35 #endif
36  
37 #ifdef HAVE_ZLIB
38 #include <zlib.h>
39 #endif
40  
41 #ifdef HAVE_ZSTD
42 #include <zstd.h>
43 #endif
44  
45 #ifdef HAVE_SNAPPY
46 #include <snappy-c.h>
47 #endif
48  
49 /* ================================================================
50 * Constants
51 * ================================================================ */
52  
53 #define KF_MAGIC_ALIVE 0xCAFEBEEF
54 #define KF_MAGIC_FREED 0xDEADCAFE
55  
56 #define KF_BUF_INIT 16384
57  
58 /* True during Perl global destruction — calling user callbacks then is unsafe
59 * because closed-over SVs may already have been freed. */
60 #ifdef PERL_PHASE_DESTRUCT
61 # define KF_IN_GLOBAL_DESTRUCT() (PL_phase == PERL_PHASE_DESTRUCT)
62 #else
63 # define KF_IN_GLOBAL_DESTRUCT() (PL_dirty)
64 #endif
65  
66 /* Connection states */
67 #define CONN_DISCONNECTED 0
68 #define CONN_CONNECTING 1
69 #define CONN_TLS_HANDSHAKE 2
70 #define CONN_SASL_HANDSHAKE 3
71 #define CONN_SASL_AUTH 4
72 #define CONN_API_VERSIONS 5
73 #define CONN_READY 6
74  
75 /* Kafka API keys */
76 #define API_PRODUCE 0
77 #define API_FETCH 1
78 #define API_LIST_OFFSETS 2
79 #define API_METADATA 3
80 #define API_OFFSET_COMMIT 8
81 #define API_OFFSET_FETCH 9
82 #define API_FIND_COORDINATOR 10
83 #define API_JOIN_GROUP 11
84 #define API_HEARTBEAT 12
85 #define API_LEAVE_GROUP 13
86 #define API_SYNC_GROUP 14
87 #define API_SASL_HANDSHAKE 17
88 #define API_API_VERSIONS 18
89 #define API_CREATE_TOPICS 19
90 #define API_DELETE_TOPICS 20
91 #define API_INIT_PRODUCER_ID 22
92 #define API_ADD_PARTITIONS_TXN 24
93 #define API_END_TXN 26
94 #define API_TXN_OFFSET_COMMIT 28
95 #define API_SASL_AUTHENTICATE 36
96  
97 #define API_VERSIONS_MAX_KEY 64
98  
99 /* Compression types — Kafka wire codes. */
100 #define COMPRESS_NONE 0
101 #define COMPRESS_GZIP 1
102 #define COMPRESS_SNAPPY 2
103 #define COMPRESS_LZ4 3
104 #define COMPRESS_ZSTD 4
105  
106 #define CLEAR_HANDLER(field) \
107 do { if (NULL != (field)) { SvREFCNT_dec(field); (field) = NULL; } } while(0)
108  
109 /* DESTROY nulls the IV in the blessed ref, so every copy of the ref
110 * yields a NULL self; guard every XSUB entry against stale use. */
111 #define KF_REQUIRE_CONN(s) \
112 do { if (!(s)) croak("EV::Kafka::Conn: method called on destroyed connection"); } while (0)
113  
114 /* call_sv a stored handler SV, pinned across the call so the callback may
115 * clear its own handler (e.g. $obj->on_error(undef)) without freeing the CV
116 * mid-call. Args must already be pushed. */
117 #define PINNED_CALL_SV(handler, flags) \
118 STMT_START { SV *_pin = (handler); SvREFCNT_inc_simple_void_NN(_pin); \
119 call_sv(_pin, (flags)); SvREFCNT_dec(_pin); } STMT_END
120  
121 /* ================================================================
122 * Type declarations
123 * ================================================================ */
124  
125 typedef struct kf_buf_s kf_buf_t;
126 typedef struct ev_kafka_conn_s ev_kafka_conn_t;
127 typedef struct ev_kafka_conn_cb_s ev_kafka_conn_cb_t;
128  
129 typedef ev_kafka_conn_t* EV__Kafka__Conn;
130  
131 /* ================================================================
132 * Dynamic buffer
133 * ================================================================ */
134  
135 struct kf_buf_s {
136 char *data;
137 size_t len;
138 size_t cap;
139 };
140  
141 703 static void kf_buf_init(kf_buf_t *b) {
142 703 Newx(b->data, 256, char);
143 703 b->len = 0;
144 703 b->cap = 256;
145 703 }
146  
147 4126 static void kf_buf_grow(kf_buf_t *b, size_t need) {
148 4126 ▶ if (b->cap >= need) return;
Branch: 1 branch
BranchTrueFalseTotal
if (b->cap >= need) return; 4107 19 4126
149 19 size_t newcap = b->cap * 2;
150 19 ▶ if (newcap < need) newcap = need;
Branch: 1 branch
BranchTrueFalseTotal
if (newcap < need) newcap = need; 6 13 19
151 19 Renew(b->data, newcap, char);
152 19 b->cap = newcap;
153 }
154  
155 703 static void kf_buf_free(kf_buf_t *b) {
156 703 ▶ if (b->data) { Safefree(b->data); b->data = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (b->data) { Safefree(b->data); b->data = NULL; } 703 0 703
157 703 b->len = 0;
158 703 b->cap = 0;
159 703 }
160  
161 947 static void kf_buf_append(kf_buf_t *b, const char *data, size_t len) {
162 947 kf_buf_grow(b, b->len + len);
163 947 Copy(data, b->data + b->len, len, char);
164 947 b->len += len;
165 947 }
166  
167 233 static void kf_buf_append_i8(kf_buf_t *b, int8_t val) {
168 233 kf_buf_grow(b, b->len + 1);
169 233 b->data[b->len++] = val;
170 233 }
171  
172 870 static void kf_buf_append_i16(kf_buf_t *b, int16_t val) {
173 870 kf_buf_grow(b, b->len + 2);
174 870 uint16_t v = htons((uint16_t)val);
175 870 memcpy(b->data + b->len, &v, 2);
176 870 b->len += 2;
177 870 }
178  
179 847 static void kf_buf_append_i32(kf_buf_t *b, int32_t val) {
180 847 kf_buf_grow(b, b->len + 4);
181 847 uint32_t v = htonl((uint32_t)val);
182 847 memcpy(b->data + b->len, &v, 4);
183 847 b->len += 4;
184 847 }
185  
186 203 static void kf_buf_append_i64(kf_buf_t *b, int64_t val) {
187 203 kf_buf_grow(b, b->len + 8);
188 203 uint32_t hi = htonl((uint32_t)((uint64_t)val >> 32));
189 203 uint32_t lo = htonl((uint32_t)((uint64_t)val & 0xFFFFFFFF));
190 203 memcpy(b->data + b->len, &hi, 4);
191 203 memcpy(b->data + b->len + 4, &lo, 4);
192 203 b->len += 8;
193 203 }
194  
195 /* Kafka STRING: INT16 length + bytes. NULL → -1 length */
196 139 static void kf_buf_append_string(kf_buf_t *b, const char *s, int16_t len) {
197 139 kf_buf_append_i16(b, len);
198 139 ▶ if (len > 0) kf_buf_append(b, s, len);
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) kf_buf_append(b, s, len); 132 7 139
199 139 }
200  
201 225 static void kf_buf_append_nullable_string(kf_buf_t *b, const char *s, int16_t len) {
202 225 ▶ if (!s) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s) { 23 202 225
203 23 kf_buf_append_i16(b, -1);
204 } else {
205 202 kf_buf_append_i16(b, len);
206 202 ▶ if (len > 0) kf_buf_append(b, s, len);
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) kf_buf_append(b, s, len); 198 4 202
207 }
208 225 }
209  
210 /* Kafka BYTES: INT32 length + bytes */
211 12 static void kf_buf_append_bytes(kf_buf_t *b, const char *s, int32_t len) {
212 12 kf_buf_append_i32(b, len);
213 12 ▶ if (len > 0) kf_buf_append(b, s, len);
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) kf_buf_append(b, s, len); 12 0 12
214 12 }
215  
216 8 static void kf_buf_append_nullable_bytes(kf_buf_t *b, const char *s, int32_t len) {
217 8 ▶ if (!s) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s) { 8 0 8
218 8 kf_buf_append_i32(b, -1);
219 } else {
220 0 kf_buf_append_i32(b, len);
221 0 ▶ if (len > 0) kf_buf_append(b, s, len);
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) kf_buf_append(b, s, len); 0 0 0
222 }
223 8 }
224  
225 /* Unsigned varint (ZigZag for signed is not used in Kafka framing) */
226 1026 static void kf_buf_append_uvarint(kf_buf_t *b, uint64_t val) {
227 1026 kf_buf_grow(b, b->len + 10);
228 1047 ▶ while (val >= 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
while (val >= 0x80) { 21 1026 1047
229 21 b->data[b->len++] = (char)((val & 0x7F) | 0x80);
230 21 val >>= 7;
231 }
232 1026 b->data[b->len++] = (char)val;
233 1026 }
234  
235 /* Signed varint (ZigZag encoding) */
236 1026 static void kf_buf_append_varint(kf_buf_t *b, int64_t val) {
237 1026 uint64_t uval = ((uint64_t)val << 1) ^ (uint64_t)(val >> 63);
238 1026 kf_buf_append_uvarint(b, uval);
239 1026 }
240  
241 /* Compact string: uvarint(len+1) + bytes. NULL → 0 */
242 0 static void kf_buf_append_compact_string(kf_buf_t *b, const char *s, int32_t len) {
243 0 ▶ if (!s) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s) { 0 0 0
244 0 kf_buf_append_uvarint(b, 0);
245 } else {
246 0 kf_buf_append_uvarint(b, (uint64_t)len + 1);
247 0 ▶ if (len > 0) kf_buf_append(b, s, len);
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) kf_buf_append(b, s, len); 0 0 0
248 }
249 0 }
250  
251 /* Tagged fields: just 0 = no tagged fields */
252 0 static void kf_buf_append_tagged_fields(kf_buf_t *b) {
253 0 kf_buf_append_uvarint(b, 0);
254 0 }
255  
256 /* ================================================================
257 * Wire read helpers (big-endian)
258 * ================================================================ */
259  
260 1939 static int16_t kf_read_i16(const char *buf) {
261 uint16_t v;
262 1939 memcpy(&v, buf, 2);
263 1939 return (int16_t)ntohs(v);
264 }
265  
266 53125 static int32_t kf_read_i32(const char *buf) {
267 uint32_t v;
268 53125 memcpy(&v, buf, 4);
269 53125 return (int32_t)ntohl(v);
270 }
271  
272 102 static int64_t kf_read_i64(const char *buf) {
273 uint32_t hi, lo;
274 102 memcpy(&hi, buf, 4);
275 102 memcpy(&lo, buf + 4, 4);
276 102 return ((int64_t)ntohl(hi) << 32) | (uint32_t)ntohl(lo);
277 }
278  
279 /* Read unsigned varint, returns bytes consumed or -1 on error */
280 104892 static int kf_read_uvarint(const char *buf, const char *end, uint64_t *out) {
281 104892 uint64_t val = 0;
282 104892 int shift = 0;
283 104892 const char *p = buf;
284 104921 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 52921 52000 104921
285 52921 uint8_t b = (uint8_t)*p++;
286 52921 val |= ((uint64_t)(b & 0x7F)) << shift;
287 52921 ▶ if (!(b & 0x80)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(b & 0x80)) { 52892 29 52921
288 52892 *out = val;
289 52892 return (int)(p - buf);
290 }
291 29 shift += 7;
292 29 ▶ if (shift >= 64) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (shift >= 64) return -1; 0 29 29
293 }
294 52000 return -1; /* incomplete */
295 }
296  
297 /* Read signed varint (ZigZag) */
298 888 static int kf_read_varint(const char *buf, const char *end, int64_t *out) {
299 uint64_t uval;
300 888 int n = kf_read_uvarint(buf, end, &uval);
301 888 ▶ if (n < 0) return n;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return n; 0 888 888
302 888 *out = (int64_t)((uval >> 1) ^ -(int64_t)(uval & 1));
303 888 return n;
304 }
305  
306 /* Read Kafka STRING: i16 len + bytes. Returns pointer into buf, sets *len. Returns bytes consumed. */
307 214 static int kf_read_string(const char *buf, const char *end, const char **out, int16_t *slen) {
308 214 ▶ if (end - buf < 2) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (end - buf < 2) return -1; 0 214 214
309 214 int16_t len = kf_read_i16(buf);
310 214 ▶ if (len < 0) { /* nullable null */
Branch: 1 branch
BranchTrueFalseTotal
if (len < 0) { /* nullable null */ 50 164 214
311 50 *out = NULL;
312 50 *slen = 0;
313 50 return 2;
314 }
315 164 ▶ if (end - buf < 2 + len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (end - buf < 2 + len) return -1; 0 164 164
316 164 *out = buf + 2;
317 164 *slen = len;
318 164 return 2 + len;
319 }
320  
321 /* Read compact string: uvarint(len+1) + bytes */
322 52002 static int kf_read_compact_string(const char *buf, const char *end, const char **out, int32_t *slen) {
323 uint64_t raw;
324 52002 int n = kf_read_uvarint(buf, end, &raw);
325 52002 ▶ if (n < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return -1; 52000 2 52002
326 2 ▶ if (raw == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (raw == 0) { 0 2 2
327 0 *out = NULL;
328 0 *slen = 0;
329 0 return n;
330 }
331 /* reject lengths that would wrap negative in the int32_t cast below */
332 2 ▶ if (raw - 1 > (uint64_t)INT32_MAX) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (raw - 1 > (uint64_t)INT32_MAX) return -1; 2 0 2
333 0 int32_t len = (int32_t)(raw - 1);
334 0 ▶ if (end - buf - n < len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (end - buf - n < len) return -1; 0 0 0
335 0 *out = buf + n;
336 0 *slen = len;
337 0 return n + len;
338 }
339  
340 /* Skip tagged fields */
341 0 static int kf_skip_tagged_fields(const char *buf, const char *end) {
342 uint64_t count;
343 0 int n = kf_read_uvarint(buf, end, &count);
344 0 ▶ if (n < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return -1; 0 0 0
345 0 const char *p = buf + n;
346 uint64_t i;
347 0 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 0 0 0
348 uint64_t tag;
349 0 int tn = kf_read_uvarint(p, end, &tag);
350 0 ▶ if (tn < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (tn < 0) return -1; 0 0 0
351 0 p += tn;
352 uint64_t dlen;
353 0 int dn = kf_read_uvarint(p, end, &dlen);
354 0 ▶ if (dn < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (dn < 0) return -1; 0 0 0
355 0 p += dn;
356 0 ▶ if ((uint64_t)(end - p) < dlen) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ((uint64_t)(end - p) < dlen) return -1; 0 0 0
357 0 p += dlen;
358 }
359 0 return (int)(p - buf);
360 }
361  
362 /* ================================================================
363 * CRC32C (software implementation)
364 * ================================================================ */
365  
366 static uint32_t crc32c_table[256];
367 static int crc32c_table_inited = 0;
368  
369 34 static void crc32c_init_table(void) {
370 uint32_t i, j;
371 8738 ▶ for (i = 0; i < 256; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; i++) { 8704 34 8738
372 8704 uint32_t crc = i;
373 78336 ▶ for (j = 0; j < 8; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 8; j++) { 69632 8704 78336
374 69632 ▶ if (crc & 1)
Branch: 1 branch
BranchTrueFalseTotal
if (crc & 1) 34816 34816 69632
375 34816 crc = (crc >> 1) ^ 0x82F63B78;
376 else
377 34816 crc >>= 1;
378 }
379 8704 crc32c_table[i] = crc;
380 }
381 34 crc32c_table_inited = 1;
382 34 }
383  
384 66 static uint32_t crc32c(const char *buf, size_t len) {
385 66 uint32_t crc = 0xFFFFFFFF;
386 size_t i;
387 66 ▶ if (!crc32c_table_inited) crc32c_init_table();
Branch: 1 branch
BranchTrueFalseTotal
if (!crc32c_table_inited) crc32c_init_table(); 0 66 66
388 206628 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 206562 66 206628
389 206562 crc = crc32c_table[(crc ^ (uint8_t)buf[i]) & 0xFF] ^ (crc >> 8);
390 66 return crc ^ 0xFFFFFFFF;
391 }
392  
393 /* ================================================================
394 * Connection callback struct
395 * ================================================================ */
396  
397 struct ev_kafka_conn_cb_s {
398 SV *cb;
399 ngx_queue_t queue;
400 int32_t correlation_id;
401 int16_t api_key;
402 int16_t api_version;
403 int internal; /* 1 = internal handshake cb, don't invoke Perl */
404 };
405  
406 /* ================================================================
407 * Broker connection struct (Layer 1)
408 * ================================================================ */
409  
410 struct ev_kafka_conn_s {
411 unsigned int magic;
412 struct ev_loop *loop;
413 SV *loop_sv; /* held so a custom EV::Loop cannot be ev_loop_destroy'd
414 * under live watchers; NULL for EV_DEFAULT */
415 int fd;
416 int state;
417  
418 /* EV watchers */
419 ev_io rio, wio;
420 ev_timer timer;
421 int reading, writing, timing;
422  
423 /* TLS */
424 #ifdef HAVE_OPENSSL
425 SSL_CTX *ssl_ctx;
426 SSL *ssl;
427 #endif
428 int tls_enabled;
429 char *tls_ca_file;
430 int tls_skip_verify;
431  
432 /* Connection params */
433 char *host;
434 int port;
435  
436 /* SASL */
437 char *sasl_mechanism;
438 char *sasl_username;
439 char *sasl_password;
440  
441 /* SCRAM state */
442 int scram_step;
443 char *scram_nonce;
444 char *scram_client_first;
445 size_t scram_client_first_len;
446 /* For server-final-message signature verification (RFC 5802). */
447 unsigned char scram_server_key[64];
448 int scram_server_key_len;
449 char *scram_auth_message;
450 size_t scram_auth_message_len;
451  
452 /* Buffers */
453 char *rbuf;
454 size_t rbuf_len, rbuf_cap;
455 char *wbuf;
456 size_t wbuf_len, wbuf_off, wbuf_cap;
457  
458 /* Request/response correlation */
459 ngx_queue_t cb_queue;
460 int32_t next_correlation_id;
461 int pending_count;
462  
463 /* Client identity */
464 char *client_id;
465 int client_id_len;
466  
467 /* API version negotiation */
468 int16_t api_versions[API_VERSIONS_MAX_KEY];
469 int api_versions_known;
470  
471 /* Event handlers */
472 SV *on_error;
473 SV *on_connect;
474 SV *on_disconnect;
475  
476 /* Reconnection */
477 int auto_reconnect;
478 int reconnect_delay_ms;
479 ev_timer reconnect_timer;
480 int reconnect_timing;
481 int intentional_disconnect;
482 /* Backoff state: attempts since the last successful connect;
483 * delay = reconnect_delay_ms * 2^attempts, capped and jittered. */
484 int reconnect_attempts;
485  
486 /* Connect/handshake timeout (seconds); the single self->timer covers
487 * TCP connect AND all handshake phases until CONN_READY. */
488 double connect_timeout;
489  
490 /* Safety: callback_depth is bumped by the outermost event-loop watcher
491 * frames; DESTROY defers Safefree while depth>0 (free_pending) so a
492 * callback that drops the last ref to the conn cannot free it out from
493 * under the still-running C frames. */
494 int callback_depth;
495 int free_pending;
496 };
497  
498 /* ================================================================
499 * Forward declarations
500 * ================================================================ */
501  
502 static void conn_io_cb(EV_P_ ev_io *w, int revents);
503 static void conn_timer_cb(EV_P_ ev_timer *w, int revents);
504 static void conn_reconnect_timer_cb(EV_P_ ev_timer *w, int revents);
505 static void conn_io_cb_inner(EV_P_ ev_io *w, int revents);
506 static void conn_timer_cb_inner(EV_P_ ev_timer *w, int revents);
507 static void conn_reconnect_timer_cb_inner(EV_P_ ev_timer *w, int revents);
508 static void conn_do_free(pTHX_ ev_kafka_conn_t *self);
509 static void conn_start_reading(ev_kafka_conn_t *self);
510 static void conn_stop_reading(ev_kafka_conn_t *self);
511 static void conn_start_writing(ev_kafka_conn_t *self);
512 static void conn_stop_writing(ev_kafka_conn_t *self);
513 static void conn_emit_error(pTHX_ ev_kafka_conn_t *self, const char *msg);
514 static void conn_cleanup(pTHX_ ev_kafka_conn_t *self);
515 static int conn_check_destroyed(ev_kafka_conn_t *self);
516 static void conn_cancel_pending(pTHX_ ev_kafka_conn_t *self, const char *err);
517 static void conn_on_connect_done(pTHX_ ev_kafka_conn_t *self);
518 static void conn_process_responses(pTHX_ ev_kafka_conn_t *self);
519 static void conn_schedule_reconnect(pTHX_ ev_kafka_conn_t *self);
520 static int32_t conn_send_request(pTHX_ ev_kafka_conn_t *self,
521 int16_t api_key, int16_t api_version, kf_buf_t *body, SV *cb,
522 int internal, int no_response);
523 static void conn_send_api_versions(pTHX_ ev_kafka_conn_t *self);
524 static void conn_start_connect(pTHX_ ev_kafka_conn_t *self,
525 const char *host, int port, double timeout);
526 static void conn_send_sasl_handshake(pTHX_ ev_kafka_conn_t *self);
527 static void conn_parse_sasl_handshake_response(pTHX_ ev_kafka_conn_t *self,
528 const char *data, size_t len);
529 static void conn_send_sasl_authenticate(pTHX_ ev_kafka_conn_t *self);
530 static void conn_parse_sasl_authenticate_response(pTHX_ ev_kafka_conn_t *self,
531 const char *data, size_t len);
532  
533 /* Response parsers */
534 static SV* conn_parse_metadata_response(pTHX_ ev_kafka_conn_t *self,
535 int16_t version, const char *data, size_t len);
536 static SV* conn_parse_produce_response(pTHX_ ev_kafka_conn_t *self,
537 int16_t version, const char *data, size_t len);
538 static SV* conn_parse_fetch_response(pTHX_ ev_kafka_conn_t *self,
539 int16_t version, const char *data, size_t len);
540 static SV* conn_parse_list_offsets_response(pTHX_ ev_kafka_conn_t *self,
541 int16_t version, const char *data, size_t len);
542 static SV* conn_parse_find_coordinator_response(pTHX_ ev_kafka_conn_t *self,
543 int16_t version, const char *data, size_t len);
544 static SV* conn_parse_join_group_response(pTHX_ ev_kafka_conn_t *self,
545 int16_t version, const char *data, size_t len);
546 static SV* conn_parse_sync_group_response(pTHX_ ev_kafka_conn_t *self,
547 int16_t version, const char *data, size_t len);
548 static SV* conn_parse_heartbeat_response(pTHX_ ev_kafka_conn_t *self,
549 int16_t version, const char *data, size_t len);
550 static SV* conn_parse_offset_commit_response(pTHX_ ev_kafka_conn_t *self,
551 int16_t version, const char *data, size_t len);
552 static SV* conn_parse_offset_fetch_response(pTHX_ ev_kafka_conn_t *self,
553 int16_t version, const char *data, size_t len);
554 static SV* conn_parse_leave_group_response(pTHX_ ev_kafka_conn_t *self,
555 int16_t version, const char *data, size_t len);
556 static SV* conn_parse_create_topics_response(pTHX_ ev_kafka_conn_t *self,
557 int16_t version, const char *data, size_t len);
558 static SV* conn_parse_delete_topics_response(pTHX_ ev_kafka_conn_t *self,
559 int16_t version, const char *data, size_t len);
560 static SV* conn_parse_init_producer_id_response(pTHX_ ev_kafka_conn_t *self,
561 int16_t version, const char *data, size_t len);
562 static SV* conn_parse_add_partitions_to_txn_response(pTHX_ ev_kafka_conn_t *self,
563 int16_t version, const char *data, size_t len);
564 static SV* conn_parse_end_txn_response(pTHX_ ev_kafka_conn_t *self,
565 int16_t version, const char *data, size_t len);
566 static SV* conn_parse_txn_offset_commit_response(pTHX_ ev_kafka_conn_t *self,
567 int16_t version, const char *data, size_t len);
568  
569 #ifdef HAVE_OPENSSL
570 static int is_ip_literal(const char *host);
571 #endif
572  
573 /* ================================================================
574 * I/O helpers (with optional TLS)
575 * ================================================================ */
576  
577 187 static ssize_t kf_io_read(ev_kafka_conn_t *self, void *buf, size_t len) {
578 #ifdef HAVE_OPENSSL
579 187 ▶ if (self->ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ssl) { 0 187 187
580 0 ▶ int ssl_len = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
Branch: 1 branch
BranchTrueFalseTotal
int ssl_len = (len > (size_t)INT_MAX) ? INT_MAX : (int)len; 0 0 0
581 0 ERR_clear_error();
582 0 int ret = SSL_read(self->ssl, buf, ssl_len);
583 0 ▶ if (ret <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret <= 0) { 0 0 0
584 0 int err = SSL_get_error(self->ssl, ret);
585 0 ▶ if (err == SSL_ERROR_WANT_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == SSL_ERROR_WANT_READ) { 0 0 0
586 0 errno = EAGAIN;
587 0 return -1;
588 }
589 0 ▶ if (err == SSL_ERROR_WANT_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == SSL_ERROR_WANT_WRITE) { 0 0 0
590 0 conn_start_writing(self);
591 0 errno = EAGAIN;
592 0 return -1;
593 }
594 0 ▶ if (err == SSL_ERROR_ZERO_RETURN) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (err == SSL_ERROR_ZERO_RETURN) return 0; 0 0 0
595 0 ERR_clear_error();
596 0 errno = EIO;
597 0 return -1;
598 }
599 0 return ret;
600 }
601 #endif
602 187 return read(self->fd, buf, len);
603 }
604  
605 180 static ssize_t kf_io_write(ev_kafka_conn_t *self, const void *buf, size_t len) {
606 #ifdef HAVE_OPENSSL
607 180 ▶ if (self->ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ssl) { 0 180 180
608 0 ▶ int ssl_len = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
Branch: 1 branch
BranchTrueFalseTotal
int ssl_len = (len > (size_t)INT_MAX) ? INT_MAX : (int)len; 0 0 0
609 0 ERR_clear_error();
610 0 int ret = SSL_write(self->ssl, buf, ssl_len);
611 0 ▶ if (ret <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret <= 0) { 0 0 0
612 0 int err = SSL_get_error(self->ssl, ret);
613 0 ▶ if (err == SSL_ERROR_WANT_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == SSL_ERROR_WANT_WRITE) { 0 0 0
614 0 errno = EAGAIN;
615 0 return -1;
616 }
617 0 ▶ if (err == SSL_ERROR_WANT_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == SSL_ERROR_WANT_READ) { 0 0 0
618 0 conn_start_reading(self);
619 0 errno = EAGAIN;
620 0 return -1;
621 }
622 0 ERR_clear_error();
623 0 errno = EIO;
624 0 return -1;
625 }
626 0 return ret;
627 }
628 #endif
629 #ifdef MSG_NOSIGNAL
630 /* MSG_NOSIGNAL: a write to an RST socket must fail with EPIPE, not kill
631 * the process with SIGPIPE (BSD/macOS use SO_NOSIGPIPE instead). */
632 180 return send(self->fd, buf, len, MSG_NOSIGNAL);
633 #else
634 return write(self->fd, buf, len);
635 #endif
636 }
637  
638 /* ================================================================
639 * Buffer management
640 * ================================================================ */
641  
642 187 static void conn_ensure_rbuf(ev_kafka_conn_t *self, size_t need) {
643 187 ▶ if (self->rbuf_cap >= need) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_cap >= need) return; 187 0 187
644 0 size_t newcap = self->rbuf_cap * 2;
645 0 ▶ if (newcap < need) newcap = need;
Branch: 1 branch
BranchTrueFalseTotal
if (newcap < need) newcap = need; 0 0 0
646 0 Renew(self->rbuf, newcap, char);
647 0 self->rbuf_cap = newcap;
648 }
649  
650 195 static void conn_ensure_wbuf(ev_kafka_conn_t *self, size_t need) {
651 195 ▶ if (self->wbuf_cap >= need) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->wbuf_cap >= need) return; 195 0 195
652 0 size_t newcap = self->wbuf_cap * 2;
653 0 ▶ if (newcap < need) newcap = need;
Branch: 1 branch
BranchTrueFalseTotal
if (newcap < need) newcap = need; 0 0 0
654 0 Renew(self->wbuf, newcap, char);
655 0 self->wbuf_cap = newcap;
656 }
657  
658 /* ================================================================
659 * Watcher control
660 * ================================================================ */
661  
662 59 static void conn_start_reading(ev_kafka_conn_t *self) {
663 59 ▶ if (!self->reading && self->fd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->reading && self->fd >= 0) { 59 0 59
if (!self->reading && self->fd >= 0) { 59 0 59
664 59 ev_io_start(self->loop, &self->rio);
665 59 self->reading = 1;
666 }
667 59 }
668  
669 128 static void conn_stop_reading(ev_kafka_conn_t *self) {
670 128 ▶ if (self->reading) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reading) { 59 69 128
671 59 ev_io_stop(self->loop, &self->rio);
672 59 self->reading = 0;
673 }
674 128 }
675  
676 264 static void conn_start_writing(ev_kafka_conn_t *self) {
677 264 ▶ if (!self->writing && self->fd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->writing && self->fd >= 0) { 253 11 264
if (!self->writing && self->fd >= 0) { 253 0 253
678 253 ev_io_start(self->loop, &self->wio);
679 253 self->writing = 1;
680 }
681 264 }
682  
683 376 static void conn_stop_writing(ev_kafka_conn_t *self) {
684 376 ▶ if (self->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->writing) { 253 123 376
685 253 ev_io_stop(self->loop, &self->wio);
686 253 self->writing = 0;
687 }
688 376 }
689  
690 /* Arm (or re-arm) the single connect/handshake timer; it stays armed
691 * until CONN_READY so a silent peer fails the conn instead of wedging it. */
692 134 static void conn_arm_timer(ev_kafka_conn_t *self) {
693 134 ▶ if (self->connect_timeout <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->connect_timeout <= 0) return; 0 134 134
694 134 ▶ if (self->timing)
Branch: 1 branch
BranchTrueFalseTotal
if (self->timing) 65 69 134
695 65 ev_timer_stop(self->loop, &self->timer);
696 134 ev_timer_init(&self->timer, conn_timer_cb, self->connect_timeout, 0.0);
697 134 self->timer.data = (void *)self;
698 134 ev_timer_start(self->loop, &self->timer);
699 134 self->timing = 1;
700 }
701  
702 184 static void conn_disarm_timer(ev_kafka_conn_t *self) {
703 184 ▶ if (self->timing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->timing) { 68 116 184
704 68 ev_timer_stop(self->loop, &self->timer);
705 68 self->timing = 0;
706 }
707 184 }
708  
709 /* ================================================================
710 * Callback invocation helpers
711 * ================================================================ */
712  
713 17 static void conn_emit_error(pTHX_ ev_kafka_conn_t *self, const char *msg) {
714 17 ▶ if (!self->on_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->on_error) { 1 16 17
715 /* Never croak in watcher context: a longjmp would skip the
716 * enclosing frame's --callback_depth, stranding the deferred
717 * conn_do_free forever. Warn instead. */
718 1 warn("EV::Kafka::Conn: %s", msg);
719 1 return;
720 }
721  
722 16 self->callback_depth++;
723 {
724 16 dSP;
725 16 ENTER;
726 16 SAVETMPS;
727 16 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 16 16
728 16 ▶ XPUSHs(sv_2mortal(newSVpv(msg, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(msg, 0))); 0 16 16
729 16 PUTBACK;
730 16 PINNED_CALL_SV(self->on_error, G_DISCARD | G_EVAL);
731 16 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 16 0 16
if (SvTRUE(ERRSV)) { 0 16 16
732 0 ▶ warn("EV::Kafka::Conn: on_error callback error: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Kafka::Conn: on_error callback error: %s", SvPV_nolen(ERRSV)); 0 0 0
733 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
734 }
735 16 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 16 0 16
736 16 LEAVE;
737 }
738 16 self->callback_depth--;
739 }
740  
741 56 static void conn_emit_connect(pTHX_ ev_kafka_conn_t *self) {
742 56 ▶ if (!self->on_connect) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->on_connect) return; 2 54 56
743  
744 54 self->callback_depth++;
745 {
746 54 dSP;
747 54 ENTER;
748 54 SAVETMPS;
749 54 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 54 54
750 54 PUTBACK;
751 54 PINNED_CALL_SV(self->on_connect, G_DISCARD | G_EVAL);
752 54 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 54 0 54
if (SvTRUE(ERRSV)) { 0 54 54
753 0 ▶ warn("EV::Kafka::Conn: on_connect callback error: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Kafka::Conn: on_connect callback error: %s", SvPV_nolen(ERRSV)); 0 0 0
754 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
755 }
756 54 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 54 54
757 54 LEAVE;
758 }
759 54 self->callback_depth--;
760 }
761  
762 61 static void conn_emit_disconnect(pTHX_ ev_kafka_conn_t *self) {
763 61 ▶ if (!self->on_disconnect) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->on_disconnect) return; 41 20 61
764  
765 20 self->callback_depth++;
766 {
767 20 dSP;
768 20 ENTER;
769 20 SAVETMPS;
770 20 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 20 20
771 20 PUTBACK;
772 20 PINNED_CALL_SV(self->on_disconnect, G_DISCARD | G_EVAL);
773 20 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 20 0 20
if (SvTRUE(ERRSV)) { 0 20 20
774 0 ▶ warn("EV::Kafka::Conn: on_disconnect callback error: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Kafka::Conn: on_disconnect callback error: %s", SvPV_nolen(ERRSV)); 0 0 0
775 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
776 }
777 20 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 20 20
778 20 LEAVE;
779 }
780 20 self->callback_depth--;
781 }
782  
783 /* Invoke a command callback: cb->(result, error) */
784 133 static void conn_invoke_cb(pTHX_ ev_kafka_conn_t *self, SV *cb, SV *result, SV *error) {
785 133 ▶ if (!cb) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cb) return; 0 133 133
786  
787 133 self->callback_depth++;
788 {
789 133 dSP;
790 133 ENTER;
791 133 SAVETMPS;
792 133 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 133 133
793 133 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 133 133
794 133 ▶ PUSHs(result ? result : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(result ? result : &PL_sv_undef); 115 18 133
795 133 ▶ PUSHs(error ? error : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(error ? error : &PL_sv_undef); 18 115 133
796 133 PUTBACK;
797 133 call_sv(cb, G_DISCARD | G_EVAL);
798 133 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 133 0 133
if (SvTRUE(ERRSV)) { 0 133 133
799 0 ▶ warn("EV::Kafka::Conn: callback error: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Kafka::Conn: callback error: %s", SvPV_nolen(ERRSV)); 0 0 0
800 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
801 }
802 133 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 133 133
803 133 LEAVE;
804 }
805 133 self->callback_depth--;
806 }
807  
808 315 static int conn_check_destroyed(ev_kafka_conn_t *self) {
809 /* free_pending: DESTROY deferred the Safefree to the outermost watcher
810 * frame; treat the conn as gone, but the memory is valid until then. */
811 315 ▶ return self->magic != KF_MAGIC_ALIVE || self->free_pending;
Branch: 2 branches
BranchTrueFalseTotal
return self->magic != KF_MAGIC_ALIVE || self->free_pending; 314 1 315
return self->magic != KF_MAGIC_ALIVE || self->free_pending; 0 314 314
812 }
813  
814 /* The actual teardown: stop the reconnect timer, drop handlers, free all
815 * buffers/strings and the struct. Called by DESTROY when not inside a
816 * callback, or by the outermost event-loop watcher frame once it unwinds to
817 * callback_depth 0 with free_pending set. conn_cleanup() must have run first. */
818 67 static void conn_do_free(pTHX_ ev_kafka_conn_t *self) {
819 /* Defensive: if a future early-return ever strands queue entries,
820 * free them here (callbacks are NOT invoked). */
821 67 ▶ while (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->cb_queue)) { 0 67 67
822 0 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
823 0 ngx_queue_remove(q);
824 0 ev_kafka_conn_cb_t *cbt = ngx_queue_data(q, ev_kafka_conn_cb_t, queue);
825 0 ▶ if (cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->cb) SvREFCNT_dec(cbt->cb); 0 0 0
826 0 Safefree(cbt);
827 }
828  
829 67 ▶ if (self->reconnect_timing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reconnect_timing) { 4 63 67
830 4 ev_timer_stop(self->loop, &self->reconnect_timer);
831 4 self->reconnect_timing = 0;
832 }
833  
834 67 ▶ CLEAR_HANDLER(self->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_error); 26 41 67
835 67 ▶ CLEAR_HANDLER(self->on_connect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_connect); 16 51 67
836 67 ▶ CLEAR_HANDLER(self->on_disconnect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_disconnect); 10 57 67
837  
838 67 ▶ if (self->loop_sv) SvREFCNT_dec(self->loop_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (self->loop_sv) SvREFCNT_dec(self->loop_sv); 1 66 67
839  
840 67 ▶ if (self->host) Safefree(self->host);
Branch: 1 branch
BranchTrueFalseTotal
if (self->host) Safefree(self->host); 59 8 67
841 67 ▶ if (self->client_id) Safefree(self->client_id);
Branch: 1 branch
BranchTrueFalseTotal
if (self->client_id) Safefree(self->client_id); 67 0 67
842 67 ▶ if (self->sasl_mechanism) Safefree(self->sasl_mechanism);
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_mechanism) Safefree(self->sasl_mechanism); 3 64 67
843 67 ▶ if (self->sasl_username) Safefree(self->sasl_username);
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_username) Safefree(self->sasl_username); 1 66 67
844 67 ▶ if (self->sasl_password) Safefree(self->sasl_password);
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_password) Safefree(self->sasl_password); 1 66 67
845 67 ▶ if (self->scram_nonce) Safefree(self->scram_nonce);
Branch: 1 branch
BranchTrueFalseTotal
if (self->scram_nonce) Safefree(self->scram_nonce); 2 65 67
846 67 ▶ if (self->scram_client_first) Safefree(self->scram_client_first);
Branch: 1 branch
BranchTrueFalseTotal
if (self->scram_client_first) Safefree(self->scram_client_first); 0 67 67
847 #ifdef HAVE_OPENSSL
848 67 OPENSSL_cleanse(self->scram_server_key, sizeof(self->scram_server_key));
849 67 ▶ if (self->scram_auth_message) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->scram_auth_message) { 0 67 67
850 0 OPENSSL_cleanse(self->scram_auth_message, self->scram_auth_message_len);
851 0 Safefree(self->scram_auth_message);
852 }
853 #endif
854 67 ▶ if (self->tls_ca_file) Safefree(self->tls_ca_file);
Branch: 1 branch
BranchTrueFalseTotal
if (self->tls_ca_file) Safefree(self->tls_ca_file); 0 67 67
855 67 ▶ if (self->rbuf) Safefree(self->rbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf) Safefree(self->rbuf); 67 0 67
856 67 ▶ if (self->wbuf) Safefree(self->wbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (self->wbuf) Safefree(self->wbuf); 67 0 67
857  
858 67 Safefree(self);
859 67 }
860  
861 /* ================================================================
862 * Connection cleanup
863 * ================================================================ */
864  
865 128 static void conn_cleanup(pTHX_ ev_kafka_conn_t *self) {
866 128 conn_stop_reading(self);
867 128 conn_stop_writing(self);
868 128 conn_disarm_timer(self);
869 #ifdef HAVE_OPENSSL
870 128 ▶ if (self->ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ssl) { 0 128 128
871 0 SSL_free(self->ssl);
872 0 self->ssl = NULL;
873 }
874 128 ▶ if (self->ssl_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ssl_ctx) { 0 128 128
875 0 SSL_CTX_free(self->ssl_ctx);
876 0 self->ssl_ctx = NULL;
877 }
878 #endif
879 128 ▶ if (self->fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->fd >= 0) { 69 59 128
880 69 close(self->fd);
881 69 self->fd = -1;
882 }
883 /* Stale bytes must not leak onto the next connection: a reconnect
884 * must not resume behind leftover output or a partial old response. */
885 128 self->wbuf_len = 0;
886 128 self->wbuf_off = 0;
887 128 self->rbuf_len = 0;
888 128 self->state = CONN_DISCONNECTED;
889 128 }
890  
891 128 static void conn_cancel_pending(pTHX_ ev_kafka_conn_t *self, const char *err) {
892 128 int in_destruct = KF_IN_GLOBAL_DESTRUCT();
893 128 ▶ SV *err_sv = in_destruct ? NULL : newSVpv(err, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV *err_sv = in_destruct ? NULL : newSVpv(err, 0); 123 5 128
894 ngx_queue_t *q;
895  
896 /* Drain the WHOLE queue even when a callback destroys the conn
897 * mid-loop. Safe: each cbt is unlinked BEFORE its callback runs, so
898 * a re-entrant drain cannot invoke it twice, and callback_depth > 0
899 * defers conn_do_free, keeping self valid for the rest of the drain. */
900 146 ▶ while (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->cb_queue)) { 18 128 146
901 18 q = ngx_queue_head(&self->cb_queue);
902 18 ngx_queue_remove(q);
903 18 ev_kafka_conn_cb_t *cbt = ngx_queue_data(q, ev_kafka_conn_cb_t, queue);
904 18 self->pending_count--;
905 18 ▶ if (cbt->cb && !cbt->internal && !in_destruct)
Branch: 3 branches
BranchTrueFalseTotal
if (cbt->cb && !cbt->internal && !in_destruct) 18 0 18
if (cbt->cb && !cbt->internal && !in_destruct) 18 0 18
if (cbt->cb && !cbt->internal && !in_destruct) 18 0 18
906 18 conn_invoke_cb(aTHX_ self, cbt->cb, NULL, sv_2mortal(newSVsv(err_sv)));
907 18 ▶ if (cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->cb) SvREFCNT_dec(cbt->cb); 18 0 18
908 18 Safefree(cbt);
909 }
910 128 ▶ if (err_sv) SvREFCNT_dec(err_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (err_sv) SvREFCNT_dec(err_sv); 123 5 128
911 128 }
912  
913 64 static void conn_handle_disconnect(pTHX_ ev_kafka_conn_t *self, const char *reason) {
914 /* Idempotent: conn_cleanup() below leaves state == CONN_DISCONNECTED,
915 * so a re-entrant disconnect() stops at this guard. Callers already
916 * DISCONNECTED must schedule their own conn_schedule_reconnect. */
917 64 ▶ if (self->state == CONN_DISCONNECTED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state == CONN_DISCONNECTED) return; 3 61 64
918  
919 61 conn_cleanup(aTHX_ self);
920  
921 /* Fail pending requests BEFORE emitting disconnect: requests enqueued
922 * from on_disconnect (e.g. a re-entrant connect()) belong to the NEW
923 * connection and must not be cancelled by the old one's drain. */
924 61 conn_cancel_pending(aTHX_ self, reason);
925 61 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 61 61
926  
927 61 conn_emit_disconnect(aTHX_ self);
928 61 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 61 61
929  
930 61 ▶ if (!self->intentional_disconnect && self->auto_reconnect) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->intentional_disconnect && self->auto_reconnect) { 22 39 61
if (!self->intentional_disconnect && self->auto_reconnect) { 14 8 22
931 14 conn_schedule_reconnect(aTHX_ self);
932 }
933 }
934  
935 /* ================================================================
936 * Reconnection
937 * ================================================================ */
938  
939 /* Outermost watcher frame: owns callback_depth so a callback that drops the
940 * last ref to the conn defers the free to here (after all C frames unwind). */
941 12 static void conn_reconnect_timer_cb(EV_P_ ev_timer *w, int revents) {
942 12 ev_kafka_conn_t *self = (ev_kafka_conn_t *)((char *)w - offsetof(ev_kafka_conn_t, reconnect_timer));
943 dTHX;
944 (void)loop;
945 12 ▶ if (self->magic != KF_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != KF_MAGIC_ALIVE) return; 0 12 12
946 12 self->callback_depth++;
947 {
948 /* Per-event tmps frame: see conn_io_cb. */
949 12 ENTER;
950 12 SAVETMPS;
951 12 conn_reconnect_timer_cb_inner(loop, w, revents);
952 12 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 12 12
953 12 LEAVE;
954 }
955 12 ▶ if (--self->callback_depth == 0 && self->free_pending)
Branch: 2 branches
BranchTrueFalseTotal
if (--self->callback_depth == 0 && self->free_pending) 12 0 12
if (--self->callback_depth == 0 && self->free_pending) 0 12 12
956 0 conn_do_free(aTHX_ self);
957 }
958  
959 12 static void conn_reconnect_timer_cb_inner(EV_P_ ev_timer *w, int revents) {
960 12 ev_kafka_conn_t *self = (ev_kafka_conn_t *)((char *)w - offsetof(ev_kafka_conn_t, reconnect_timer));
961 dTHX;
962 (void)loop;
963 (void)revents;
964  
965 12 self->reconnect_timing = 0;
966 12 ▶ if (self->magic != KF_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != KF_MAGIC_ALIVE) return; 0 12 12
967 12 ▶ if (self->state != CONN_DISCONNECTED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_DISCONNECTED) return; 0 12 12
968 12 ▶ if (!self->host) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->host) return; 0 12 12
969  
970 12 conn_start_connect(aTHX_ self, self->host, self->port, self->connect_timeout);
971 }
972  
973 16 static void conn_schedule_reconnect(pTHX_ ev_kafka_conn_t *self) {
974 16 ▶ if (self->reconnect_timing) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->reconnect_timing) return; 0 16 16
975 /* Capped exponential backoff with jitter: retry N>1 waits
976 * reconnect_delay_ms * 2^(N-1), capped at 30s, jittered +/-25%. */
977 16 int attempt = self->reconnect_attempts;
978 16 double delay = self->reconnect_delay_ms / 1000.0;
979 16 ▶ if (delay < 0.01) delay = 1.0;
Branch: 1 branch
BranchTrueFalseTotal
if (delay < 0.01) delay = 1.0; 0 16 16
980 16 ▶ if (attempt > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (attempt > 0) { 5 11 16
981 5 int shift = attempt > 5 ? 5 : attempt;
982 5 delay *= (double)(1 << shift);
983 5 ▶ if (delay > 30.0) delay = 30.0;
Branch: 1 branch
BranchTrueFalseTotal
if (delay > 30.0) delay = 30.0; 0 5 5
984 5 delay *= 0.75 + 0.5 * ((double)rand() / (double)RAND_MAX);
985 }
986 16 self->reconnect_attempts = attempt + 1;
987 16 ev_timer_init(&self->reconnect_timer, conn_reconnect_timer_cb, delay, 0.0);
988 16 ev_timer_start(self->loop, &self->reconnect_timer);
989 16 self->reconnect_timing = 1;
990 }
991  
992 /* ================================================================
993 * Request framing
994 * ================================================================ */
995  
996 /* Build request header + body, append to wbuf, enqueue callback.
997 * For api_version >= threshold, uses compact header (v2).
998 * ApiVersions v3+ uses flexible (v1) request header.
999 * Returns correlation_id.
1000 */
1001 195 static int32_t conn_send_request(pTHX_ ev_kafka_conn_t *self,
1002 int16_t api_key, int16_t api_version, kf_buf_t *body, SV *cb,
1003 int internal, int no_response)
1004 {
1005 195 int32_t corr_id = self->next_correlation_id++;
1006 kf_buf_t hdr;
1007 195 kf_buf_init(&hdr);
1008  
1009 /* Request header v1 (non-flexible): api_key, api_version, correlation_id, client_id */
1010 /* Request header v2 (flexible): same + tagged_fields */
1011 /* ApiVersions v3+ uses header v2 (flexible) */
1012 195 ▶ int flexible = (api_key == API_API_VERSIONS && api_version >= 3);
Branch: 2 branches
BranchTrueFalseTotal
int flexible = (api_key == API_API_VERSIONS && api_version >= 3); 59 136 195
int flexible = (api_key == API_API_VERSIONS && api_version >= 3); 0 59 59
1013  
1014 195 kf_buf_append_i16(&hdr, api_key);
1015 195 kf_buf_append_i16(&hdr, api_version);
1016 195 kf_buf_append_i32(&hdr, corr_id);
1017  
1018 195 ▶ if (flexible) {
Branch: 1 branch
BranchTrueFalseTotal
if (flexible) { 0 195 195
1019 /* compact string for client_id */
1020 0 kf_buf_append_compact_string(&hdr, self->client_id, self->client_id_len);
1021 0 kf_buf_append_tagged_fields(&hdr);
1022 } else {
1023 195 kf_buf_append_nullable_string(&hdr, self->client_id, self->client_id_len);
1024 }
1025  
1026 /* Total size = header + body */
1027 195 size_t raw_size = hdr.len + body->len;
1028 195 ▶ if (raw_size > (size_t)INT32_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (raw_size > (size_t)INT32_MAX) { 0 195 195
1029 0 kf_buf_free(&hdr);
1030 0 croak("request too large");
1031 }
1032 195 int32_t total_size = (int32_t)raw_size;
1033  
1034 /* Compact wbuf if sent prefix wastes significant space */
1035 195 ▶ if (self->wbuf_off > 0 && self->wbuf_off > self->wbuf_len / 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->wbuf_off > 0 && self->wbuf_off > self->wbuf_len / 2) { 0 195 195
if (self->wbuf_off > 0 && self->wbuf_off > self->wbuf_len / 2) { 0 0 0
1036 0 self->wbuf_len -= self->wbuf_off;
1037 0 ▶ if (self->wbuf_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->wbuf_len > 0) 0 0 0
1038 0 memmove(self->wbuf, self->wbuf + self->wbuf_off, self->wbuf_len);
1039 0 self->wbuf_off = 0;
1040 }
1041  
1042 /* Append to wbuf: [size:i32][header][body] */
1043 195 conn_ensure_wbuf(self, self->wbuf_len + 4 + total_size);
1044 {
1045 195 uint32_t sz = htonl((uint32_t)total_size);
1046 195 memcpy(self->wbuf + self->wbuf_len, &sz, 4);
1047 195 self->wbuf_len += 4;
1048 }
1049 195 Copy(hdr.data, self->wbuf + self->wbuf_len, hdr.len, char);
1050 195 self->wbuf_len += hdr.len;
1051 195 Copy(body->data, self->wbuf + self->wbuf_len, body->len, char);
1052 195 self->wbuf_len += body->len;
1053  
1054 195 kf_buf_free(&hdr);
1055  
1056 /* Enqueue callback (skip for no-response requests like acks=0) */
1057 195 ▶ if (!no_response) {
Branch: 1 branch
BranchTrueFalseTotal
if (!no_response) { 194 1 195
1058 ev_kafka_conn_cb_t *cbt;
1059 194 Newxz(cbt, 1, ev_kafka_conn_cb_t);
1060 194 cbt->correlation_id = corr_id;
1061 194 cbt->api_key = api_key;
1062 194 cbt->api_version = api_version;
1063 194 cbt->internal = internal;
1064 194 ▶ if (cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { 132 62 194
1065 132 cbt->cb = cb;
1066 132 SvREFCNT_inc(cb);
1067 }
1068 194 ngx_queue_insert_tail(&self->cb_queue, &cbt->queue);
1069 194 self->pending_count++;
1070 }
1071  
1072 195 conn_start_writing(self);
1073  
1074 195 ▶ if (no_response && cb) {
Branch: 2 branches
BranchTrueFalseTotal
if (no_response && cb) { 1 194 195
if (no_response && cb) { 1 0 1
1075 /* acks=0: the broker never answers, so fire the callback now with
1076 * an empty success result: it means "handed to the socket", not
1077 * "broker acknowledged". */
1078 1 conn_invoke_cb(aTHX_ self, cb,
1079 1 sv_2mortal(newRV_noinc((SV*)newHV())), NULL);
1080 }
1081  
1082 195 return corr_id;
1083 }
1084  
1085 /* ================================================================
1086 * ApiVersions (API 18)
1087 * ================================================================ */
1088  
1089 59 static void conn_send_api_versions(pTHX_ ev_kafka_conn_t *self) {
1090 kf_buf_t body;
1091 59 kf_buf_init(&body);
1092  
1093 /* ApiVersions v0: empty body, most compatible */
1094 59 self->state = CONN_API_VERSIONS;
1095 59 conn_send_request(aTHX_ self, API_API_VERSIONS, 0, &body, NULL, 1, 0);
1096 59 kf_buf_free(&body);
1097 59 }
1098  
1099 59 static void conn_parse_api_versions_response(pTHX_ ev_kafka_conn_t *self,
1100 const char *data, size_t len)
1101 {
1102 59 const char *p = data;
1103 59 const char *end = data + len;
1104 int i;
1105  
1106 /* Initialize all as unsupported */
1107 3835 ▶ for (i = 0; i < API_VERSIONS_MAX_KEY; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < API_VERSIONS_MAX_KEY; i++) 3776 59 3835
1108 3776 self->api_versions[i] = -1;
1109  
1110 59 ▶ if (end - p < 2) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto err; 0 59 59
1111 59 int16_t error_code = kf_read_i16(p); p += 2;
1112 59 ▶ if (error_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error_code != 0) { 0 59 59
1113 char errbuf[128];
1114 0 snprintf(errbuf, sizeof(errbuf), "ApiVersions error: %d", error_code);
1115 0 conn_emit_error(aTHX_ self, errbuf);
1116 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1117 0 conn_handle_disconnect(aTHX_ self, errbuf);
1118 0 return;
1119 }
1120  
1121 /* v0 response: array(api_key:i16, min_version:i16, max_version:i16) */
1122 59 ▶ if (end - p < 4) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto err; 0 59 59
1123 59 int32_t count = kf_read_i32(p); p += 4;
1124  
1125 793 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 734 59 793
1126 734 ▶ if (end - p < 6) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 6) goto err; 0 734 734
1127 734 int16_t key = kf_read_i16(p); p += 2;
1128 734 /* int16_t min_version = kf_read_i16(p); */ p += 2;
1129 734 int16_t max_version = kf_read_i16(p); p += 2;
1130  
1131 734 ▶ if (key >= 0 && key < API_VERSIONS_MAX_KEY)
Branch: 2 branches
BranchTrueFalseTotal
if (key >= 0 && key < API_VERSIONS_MAX_KEY) 734 0 734
if (key >= 0 && key < API_VERSIONS_MAX_KEY) 734 0 734
1132 734 self->api_versions[key] = max_version;
1133 }
1134  
1135 59 self->api_versions_known = 1;
1136  
1137 /* Handshake complete (or continue to SASL) */
1138 59 ▶ if (self->sasl_mechanism) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_mechanism) { 3 56 59
1139 3 self->state = CONN_SASL_HANDSHAKE;
1140 3 conn_arm_timer(self); /* re-arm for the SASL phase */
1141 3 conn_send_sasl_handshake(aTHX_ self);
1142 } else {
1143 56 self->state = CONN_READY;
1144 56 conn_disarm_timer(self); /* handshake done */
1145 56 conn_emit_connect(aTHX_ self);
1146 }
1147 59 return;
1148  
1149 0 err:
1150 0 conn_emit_error(aTHX_ self, "malformed ApiVersions response");
1151 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1152 0 conn_handle_disconnect(aTHX_ self, "malformed ApiVersions response");
1153 }
1154  
1155 /* ================================================================
1156 * SASL Handshake (API 17) + Authenticate (API 36)
1157 * ================================================================ */
1158  
1159 3 static void conn_send_sasl_handshake(pTHX_ ev_kafka_conn_t *self) {
1160 kf_buf_t body;
1161 3 kf_buf_init(&body);
1162  
1163 /* SaslHandshake v1: mechanism (STRING) */
1164 3 STRLEN mech_len = strlen(self->sasl_mechanism);
1165 3 kf_buf_append_string(&body, self->sasl_mechanism, (int16_t)mech_len);
1166  
1167 3 self->state = CONN_SASL_HANDSHAKE;
1168 3 conn_send_request(aTHX_ self, API_SASL_HANDSHAKE, 1, &body, NULL, 1, 0);
1169 3 kf_buf_free(&body);
1170 3 }
1171  
1172 3 static void conn_parse_sasl_handshake_response(pTHX_ ev_kafka_conn_t *self,
1173 const char *data, size_t len)
1174 {
1175 3 const char *p = data;
1176 3 const char *end = data + len;
1177  
1178 3 ▶ if (end - p < 2) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto err; 0 3 3
1179 3 int16_t error_code = kf_read_i16(p); p += 2;
1180  
1181 /* skip mechanisms array */
1182 3 ▶ if (end - p < 4) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto err; 0 3 3
1183 3 int32_t count = kf_read_i32(p); p += 4;
1184 int32_t i;
1185 6 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 3 3 6
1186 const char *s; int16_t slen;
1187 3 int n = kf_read_string(p, end, &s, &slen);
1188 3 ▶ if (n < 0) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto err; 0 3 3
1189 3 p += n;
1190 }
1191  
1192 3 ▶ if (error_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error_code != 0) { 0 3 3
1193 0 conn_emit_error(aTHX_ self, "SASL handshake failed: mechanism not supported");
1194 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1195 0 conn_handle_disconnect(aTHX_ self, "SASL handshake failed");
1196 0 return;
1197 }
1198  
1199 /* Proceed to authenticate */
1200 3 conn_arm_timer(self);
1201 3 conn_send_sasl_authenticate(aTHX_ self);
1202 3 return;
1203  
1204 0 err:
1205 0 conn_emit_error(aTHX_ self, "malformed SaslHandshake response");
1206 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1207 0 conn_handle_disconnect(aTHX_ self, "malformed SaslHandshake response");
1208 }
1209  
1210 /* SCRAM state for multi-step SASL */
1211 #define SCRAM_STEP_CLIENT_FIRST 0
1212 #define SCRAM_STEP_CLIENT_FINAL 1
1213 #define SCRAM_STEP_DONE 2
1214  
1215 3 static void conn_send_sasl_authenticate(pTHX_ ev_kafka_conn_t *self) {
1216 kf_buf_t body;
1217 3 kf_buf_init(&body);
1218  
1219 3 ▶ if (self->sasl_mechanism && strcmp(self->sasl_mechanism, "PLAIN") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->sasl_mechanism && strcmp(self->sasl_mechanism, "PLAIN") == 0) { 3 0 3
if (self->sasl_mechanism && strcmp(self->sasl_mechanism, "PLAIN") == 0) { 0 3 3
1220 /* PLAIN: \0username\0password */
1221 0 ▶ STRLEN ulen = self->sasl_username ? strlen(self->sasl_username) : 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN ulen = self->sasl_username ? strlen(self->sasl_username) : 0; 0 0 0
1222 0 ▶ STRLEN plen = self->sasl_password ? strlen(self->sasl_password) : 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN plen = self->sasl_password ? strlen(self->sasl_password) : 0; 0 0 0
1223 0 int32_t auth_len = 1 + (int32_t)ulen + 1 + (int32_t)plen;
1224  
1225 0 kf_buf_append_i32(&body, auth_len);
1226 0 kf_buf_append_i8(&body, 0);
1227 0 ▶ if (ulen > 0) kf_buf_append(&body, self->sasl_username, ulen);
Branch: 1 branch
BranchTrueFalseTotal
if (ulen > 0) kf_buf_append(&body, self->sasl_username, ulen); 0 0 0
1228 0 kf_buf_append_i8(&body, 0);
1229 0 ▶ if (plen > 0) kf_buf_append(&body, self->sasl_password, plen);
Branch: 1 branch
BranchTrueFalseTotal
if (plen > 0) kf_buf_append(&body, self->sasl_password, plen); 0 0 0
1230 }
1231 #ifdef HAVE_OPENSSL
1232 3 ▶ else if (self->sasl_mechanism &&
Branch: 1 branch
BranchTrueFalseTotal
else if (self->sasl_mechanism && 3 0 3
1233 3 ▶ (strcmp(self->sasl_mechanism, "SCRAM-SHA-256") == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(strcmp(self->sasl_mechanism, "SCRAM-SHA-256") == 0 || 1 2 3
1234 1 ▶ strcmp(self->sasl_mechanism, "SCRAM-SHA-512") == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
strcmp(self->sasl_mechanism, "SCRAM-SHA-512") == 0)) { 0 1 1
1235 /* SCRAM client-first-message: n,,n=<user>,r=<nonce> */
1236 char nonce[33];
1237 {
1238 unsigned char rnd[16];
1239 int i;
1240 2 RAND_bytes(rnd, 16);
1241 34 ▶ for (i = 0; i < 16; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) 32 2 34
1242 32 snprintf(nonce + i*2, 3, "%02x", rnd[i]);
1243 2 nonce[32] = '\0';
1244 }
1245  
1246 /* save nonce for later steps */
1247 2 ▶ if (self->scram_nonce) Safefree(self->scram_nonce);
Branch: 1 branch
BranchTrueFalseTotal
if (self->scram_nonce) Safefree(self->scram_nonce); 0 2 2
1248 2 self->scram_nonce = savepv(nonce);
1249 2 self->scram_step = SCRAM_STEP_CLIENT_FIRST;
1250  
1251 2 ▶ if (!self->sasl_username) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->sasl_username) { 2 0 2
1252 2 conn_emit_error(aTHX_ self, "SCRAM: username required");
1253 2 kf_buf_free(&body);
1254 4 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 2 2
1255 2 conn_handle_disconnect(aTHX_ self, "SCRAM: username required");
1256 2 return;
1257 }
1258 kf_buf_t msg;
1259 0 kf_buf_init(&msg);
1260 0 kf_buf_append(&msg, "n,,n=", 5);
1261 0 kf_buf_append(&msg, self->sasl_username, strlen(self->sasl_username));
1262 0 kf_buf_append(&msg, ",r=", 3);
1263 0 kf_buf_append(&msg, nonce, 32);
1264  
1265 /* save client-first-message-bare for later binding */
1266 0 ▶ if (self->scram_client_first) Safefree(self->scram_client_first);
Branch: 1 branch
BranchTrueFalseTotal
if (self->scram_client_first) Safefree(self->scram_client_first); 0 0 0
1267 /* bare = after "n,," */
1268 0 self->scram_client_first = savepvn(msg.data + 3, msg.len - 3);
1269 0 self->scram_client_first_len = msg.len - 3;
1270  
1271 0 kf_buf_append_i32(&body, (int32_t)msg.len);
1272 0 kf_buf_append(&body, msg.data, msg.len);
1273 0 kf_buf_free(&msg);
1274 }
1275 #endif
1276 else {
1277 1 conn_emit_error(aTHX_ self, "unsupported SASL mechanism");
1278 1 kf_buf_free(&body);
1279 1 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 1 1
1280 1 conn_handle_disconnect(aTHX_ self, "unsupported SASL mechanism");
1281 1 return;
1282 }
1283  
1284 0 self->state = CONN_SASL_AUTH;
1285 {
1286 0 int16_t ver = self->api_versions[API_SASL_AUTHENTICATE];
1287 0 ▶ if (ver < 0) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 1; 0 0 0
1288 0 ▶ if (ver > 2) ver = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 2) ver = 2; 0 0 0
1289 0 conn_send_request(aTHX_ self, API_SASL_AUTHENTICATE, ver, &body, NULL, 1, 0);
1290 }
1291 0 kf_buf_free(&body);
1292 }
1293  
1294 0 static void conn_parse_sasl_authenticate_response(pTHX_ ev_kafka_conn_t *self,
1295 const char *data, size_t len)
1296 {
1297 0 const char *p = data;
1298 0 const char *end = data + len;
1299  
1300 0 ▶ if (end - p < 2) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto err; 0 0 0
1301 0 int16_t error_code = kf_read_i16(p); p += 2;
1302  
1303 0 const char *errmsg_str = NULL;
1304 0 int16_t errmsg_len = 0;
1305 {
1306 0 int n = kf_read_string(p, end, &errmsg_str, &errmsg_len);
1307 0 ▶ if (n < 0) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto err; 0 0 0
1308 0 p += n;
1309 }
1310  
1311 /* auth_bytes */
1312 0 const char *auth_data = NULL;
1313 0 int32_t auth_data_len = 0;
1314 0 ▶ if (end - p >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (end - p >= 4) { 0 0 0
1315 0 auth_data_len = kf_read_i32(p); p += 4;
1316 0 ▶ if (auth_data_len > 0 && end - p >= auth_data_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (auth_data_len > 0 && end - p >= auth_data_len) { 0 0 0
if (auth_data_len > 0 && end - p >= auth_data_len) { 0 0 0
1317 0 auth_data = p;
1318 0 p += auth_data_len;
1319 }
1320 }
1321  
1322 0 ▶ if (error_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error_code != 0) { 0 0 0
1323 char errbuf[512];
1324 0 ▶ if (errmsg_str && errmsg_len > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (errmsg_str && errmsg_len > 0) 0 0 0
if (errmsg_str && errmsg_len > 0) 0 0 0
1325 0 snprintf(errbuf, sizeof(errbuf), "SASL auth failed: %.*s", (int)errmsg_len, errmsg_str);
1326 else
1327 0 snprintf(errbuf, sizeof(errbuf), "SASL auth failed: error %d", error_code);
1328 0 conn_emit_error(aTHX_ self, errbuf);
1329 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1330 0 conn_handle_disconnect(aTHX_ self, "SASL auth failed");
1331 0 return;
1332 }
1333  
1334 #ifdef HAVE_OPENSSL
1335 /* SCRAM multi-step handling */
1336 0 ▶ if (self->sasl_mechanism && self->scram_step == SCRAM_STEP_CLIENT_FIRST && auth_data) {
Branch: 3 branches
BranchTrueFalseTotal
if (self->sasl_mechanism && self->scram_step == SCRAM_STEP_CLIENT_FIRST && auth_data) { 0 0 0
if (self->sasl_mechanism && self->scram_step == SCRAM_STEP_CLIENT_FIRST && auth_data) { 0 0 0
if (self->sasl_mechanism && self->scram_step == SCRAM_STEP_CLIENT_FIRST && auth_data) { 0 0 0
1337 /* Server-first-message: r=<nonce>,s=<salt>,i=<iterations> */
1338 /* Parse server response, compute proof, send client-final */
1339 0 const char *server_nonce = NULL;
1340 0 size_t server_nonce_len = 0;
1341 0 const char *salt_b64 = NULL;
1342 0 size_t salt_b64_len = 0;
1343 0 int iterations = 0;
1344 {
1345 0 const char *sp = auth_data;
1346 0 const char *se = auth_data + auth_data_len;
1347 0 ▶ while (sp < se) {
Branch: 1 branch
BranchTrueFalseTotal
while (sp < se) { 0 0 0
1348 0 ▶ if (sp + 2 <= se && sp[0] == 'r' && sp[1] == '=') {
Branch: 3 branches
BranchTrueFalseTotal
if (sp + 2 <= se && sp[0] == 'r' && sp[1] == '=') { 0 0 0
if (sp + 2 <= se && sp[0] == 'r' && sp[1] == '=') { 0 0 0
if (sp + 2 <= se && sp[0] == 'r' && sp[1] == '=') { 0 0 0
1349 0 sp += 2; server_nonce = sp;
1350 0 ▶ while (sp < se && *sp != ',') sp++;
Branch: 2 branches
BranchTrueFalseTotal
while (sp < se && *sp != ',') sp++; 0 0 0
while (sp < se && *sp != ',') sp++; 0 0 0
1351 0 server_nonce_len = sp - server_nonce;
1352 0 ▶ } else if (sp + 2 <= se && sp[0] == 's' && sp[1] == '=') {
Branch: 3 branches
BranchTrueFalseTotal
} else if (sp + 2 <= se && sp[0] == 's' && sp[1] == '=') { 0 0 0
} else if (sp + 2 <= se && sp[0] == 's' && sp[1] == '=') { 0 0 0
} else if (sp + 2 <= se && sp[0] == 's' && sp[1] == '=') { 0 0 0
1353 0 sp += 2; salt_b64 = sp;
1354 0 ▶ while (sp < se && *sp != ',') sp++;
Branch: 2 branches
BranchTrueFalseTotal
while (sp < se && *sp != ',') sp++; 0 0 0
while (sp < se && *sp != ',') sp++; 0 0 0
1355 0 salt_b64_len = sp - salt_b64;
1356 0 ▶ } else if (sp + 2 <= se && sp[0] == 'i' && sp[1] == '=') {
Branch: 3 branches
BranchTrueFalseTotal
} else if (sp + 2 <= se && sp[0] == 'i' && sp[1] == '=') { 0 0 0
} else if (sp + 2 <= se && sp[0] == 'i' && sp[1] == '=') { 0 0 0
} else if (sp + 2 <= se && sp[0] == 'i' && sp[1] == '=') { 0 0 0
1357 0 sp += 2;
1358 0 iterations = atoi(sp);
1359 0 ▶ while (sp < se && *sp != ',') sp++;
Branch: 2 branches
BranchTrueFalseTotal
while (sp < se && *sp != ',') sp++; 0 0 0
while (sp < se && *sp != ',') sp++; 0 0 0
1360 }
1361 0 ▶ if (sp < se && *sp == ',') sp++;
Branch: 2 branches
BranchTrueFalseTotal
if (sp < se && *sp == ',') sp++; 0 0 0
if (sp < se && *sp == ',') sp++; 0 0 0
1362 0 else sp++;
1363 }
1364 }
1365  
1366 0 ▶ if (!server_nonce || !salt_b64 || iterations <= 0 || iterations > 1048576) {
Branch: 4 branches
BranchTrueFalseTotal
if (!server_nonce || !salt_b64 || iterations <= 0 || iterations > 1048576) { 0 0 0
if (!server_nonce || !salt_b64 || iterations <= 0 || iterations > 1048576) { 0 0 0
if (!server_nonce || !salt_b64 || iterations <= 0 || iterations > 1048576) { 0 0 0
if (!server_nonce || !salt_b64 || iterations <= 0 || iterations > 1048576) { 0 0 0
1367 0 conn_emit_error(aTHX_ self, "SCRAM: malformed server-first-message");
1368 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1369 0 conn_handle_disconnect(aTHX_ self, "SCRAM auth failed");
1370 0 return;
1371 }
1372  
1373 /* RFC 5802: server nonce must start with client nonce */
1374 0 ▶ if (server_nonce_len < 32 ||
Branch: 1 branch
BranchTrueFalseTotal
if (server_nonce_len < 32 || 0 0 0
1375 0 ▶ memcmp(server_nonce, self->scram_nonce, 32) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
memcmp(server_nonce, self->scram_nonce, 32) != 0) { 0 0 0
1376 0 conn_emit_error(aTHX_ self, "SCRAM: server nonce mismatch");
1377 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1378 0 conn_handle_disconnect(aTHX_ self, "SCRAM auth failed");
1379 0 return;
1380 }
1381  
1382 0 int is_sha512 = (strcmp(self->sasl_mechanism, "SCRAM-SHA-512") == 0);
1383 0 ▶ const EVP_MD *md = is_sha512 ? EVP_sha512() : EVP_sha256();
Branch: 1 branch
BranchTrueFalseTotal
const EVP_MD *md = is_sha512 ? EVP_sha512() : EVP_sha256(); 0 0 0
1384 0 ▶ int digest_len = is_sha512 ? 64 : 32;
Branch: 1 branch
BranchTrueFalseTotal
int digest_len = is_sha512 ? 64 : 32; 0 0 0
1385  
1386 /* Decode salt from base64 */
1387 unsigned char salt[128];
1388 int salt_len;
1389 {
1390 0 BIO *b64 = BIO_new(BIO_f_base64());
1391 0 BIO *bmem = BIO_new_mem_buf(salt_b64, (int)salt_b64_len);
1392 0 bmem = BIO_push(b64, bmem);
1393 0 BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL);
1394 0 salt_len = BIO_read(bmem, salt, sizeof(salt));
1395 0 BIO_free_all(bmem);
1396 0 ▶ if (salt_len <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (salt_len <= 0) { 0 0 0
1397 0 conn_emit_error(aTHX_ self, "SCRAM: bad salt");
1398 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1399 0 conn_handle_disconnect(aTHX_ self, "SCRAM auth failed");
1400 0 return;
1401 }
1402 }
1403  
1404 /* SaltedPassword = Hi(password, salt, iterations) using PBKDF2 */
1405 unsigned char salted_password[64];
1406 0 PKCS5_PBKDF2_HMAC(self->sasl_password, strlen(self->sasl_password),
1407 salt, salt_len, iterations, md, digest_len, salted_password);
1408  
1409 /* ClientKey = HMAC(SaltedPassword, "Client Key") */
1410 unsigned char client_key[64];
1411 0 unsigned int ck_len = digest_len;
1412 0 HMAC(md, salted_password, digest_len,
1413 (unsigned char *)"Client Key", 10, client_key, &ck_len);
1414  
1415 /* ServerKey = HMAC(SaltedPassword, "Server Key") — saved for
1416 * server-final-message verification. */
1417 0 unsigned int sk_hmac_len = digest_len;
1418 0 HMAC(md, salted_password, digest_len,
1419 (unsigned char *)"Server Key", 10,
1420 0 self->scram_server_key, &sk_hmac_len);
1421 0 self->scram_server_key_len = (int)sk_hmac_len;
1422  
1423 /* StoredKey = H(ClientKey) */
1424 unsigned char stored_key[64];
1425 {
1426 0 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1427 unsigned int sk_len;
1428 0 EVP_DigestInit_ex(ctx, md, NULL);
1429 0 EVP_DigestUpdate(ctx, client_key, digest_len);
1430 0 EVP_DigestFinal_ex(ctx, stored_key, &sk_len);
1431 0 EVP_MD_CTX_free(ctx);
1432 }
1433  
1434 /* AuthMessage = client-first-bare + "," + server-first + "," + client-final-without-proof */
1435 0 char channel_binding_b64[] = "biws"; /* base64("n,,") */
1436 kf_buf_t auth_msg;
1437 0 kf_buf_init(&auth_msg);
1438 0 kf_buf_append(&auth_msg, self->scram_client_first, self->scram_client_first_len);
1439 0 kf_buf_append(&auth_msg, ",", 1);
1440 0 kf_buf_append(&auth_msg, auth_data, auth_data_len);
1441 0 kf_buf_append(&auth_msg, ",c=", 3);
1442 0 kf_buf_append(&auth_msg, channel_binding_b64, 4);
1443 0 kf_buf_append(&auth_msg, ",r=", 3);
1444 0 kf_buf_append(&auth_msg, server_nonce, server_nonce_len);
1445  
1446 /* ClientSignature = HMAC(StoredKey, AuthMessage) */
1447 unsigned char client_sig[64];
1448 0 unsigned int cs_len = digest_len;
1449 0 HMAC(md, stored_key, digest_len,
1450 0 (unsigned char *)auth_msg.data, auth_msg.len, client_sig, &cs_len);
1451  
1452 /* ClientProof = ClientKey XOR ClientSignature */
1453 unsigned char proof[64];
1454 int di;
1455 0 ▶ for (di = 0; di < digest_len; di++)
Branch: 1 branch
BranchTrueFalseTotal
for (di = 0; di < digest_len; di++) 0 0 0
1456 0 proof[di] = client_key[di] ^ client_sig[di];
1457  
1458 /* Save AuthMessage for server-signature verification at step 2. */
1459 0 ▶ if (self->scram_auth_message) Safefree(self->scram_auth_message);
Branch: 1 branch
BranchTrueFalseTotal
if (self->scram_auth_message) Safefree(self->scram_auth_message); 0 0 0
1460 0 self->scram_auth_message = savepvn(auth_msg.data, auth_msg.len);
1461 0 self->scram_auth_message_len = auth_msg.len;
1462  
1463 0 kf_buf_free(&auth_msg);
1464  
1465 /* Base64 encode proof */
1466 char proof_b64[256];
1467 {
1468 0 BIO *b64 = BIO_new(BIO_f_base64());
1469 0 BIO *bmem = BIO_new(BIO_s_mem());
1470 0 b64 = BIO_push(b64, bmem);
1471 0 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
1472 0 BIO_write(b64, proof, digest_len);
1473 0 BIO_flush(b64);
1474 BUF_MEM *bptr;
1475 0 BIO_get_mem_ptr(b64, &bptr);
1476 0 ▶ int plen = bptr->length < 255 ? (int)bptr->length : 255;
Branch: 1 branch
BranchTrueFalseTotal
int plen = bptr->length < 255 ? (int)bptr->length : 255; 0 0 0
1477 0 memcpy(proof_b64, bptr->data, plen);
1478 0 proof_b64[plen] = '\0';
1479 0 BIO_free_all(b64);
1480 }
1481  
1482 /* Build client-final-message: c=biws,r=<server_nonce>,p=<proof> */
1483 kf_buf_t final_msg;
1484 0 kf_buf_init(&final_msg);
1485 0 kf_buf_append(&final_msg, "c=", 2);
1486 0 kf_buf_append(&final_msg, channel_binding_b64, 4);
1487 0 kf_buf_append(&final_msg, ",r=", 3);
1488 0 kf_buf_append(&final_msg, server_nonce, server_nonce_len);
1489 0 kf_buf_append(&final_msg, ",p=", 3);
1490 0 kf_buf_append(&final_msg, proof_b64, strlen(proof_b64));
1491  
1492 /* Send client-final via SaslAuthenticate */
1493 kf_buf_t body;
1494 0 kf_buf_init(&body);
1495 0 kf_buf_append_i32(&body, (int32_t)final_msg.len);
1496 0 kf_buf_append(&body, final_msg.data, final_msg.len);
1497  
1498 0 self->scram_step = SCRAM_STEP_CLIENT_FINAL;
1499 {
1500 0 int16_t ver = self->api_versions[API_SASL_AUTHENTICATE];
1501 0 ▶ if (ver < 0) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 1; 0 0 0
1502 0 ▶ if (ver > 2) ver = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 2) ver = 2; 0 0 0
1503 0 conn_send_request(aTHX_ self, API_SASL_AUTHENTICATE, ver, &body, NULL, 1, 0);
1504 }
1505 0 kf_buf_free(&body);
1506 0 kf_buf_free(&final_msg);
1507 0 return;
1508 }
1509  
1510 0 ▶ if (self->sasl_mechanism && self->scram_step == SCRAM_STEP_CLIENT_FINAL) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->sasl_mechanism && self->scram_step == SCRAM_STEP_CLIENT_FINAL) { 0 0 0
if (self->sasl_mechanism && self->scram_step == SCRAM_STEP_CLIENT_FINAL) { 0 0 0
1511 /* Server-final-message: v=<base64(ServerSignature)>
1512 * ServerSignature = HMAC(ServerKey, AuthMessage) — RFC 5802. */
1513 0 ▶ if (!auth_data || auth_data_len < 2 || auth_data[0] != 'v' || auth_data[1] != '=') {
Branch: 4 branches
BranchTrueFalseTotal
if (!auth_data || auth_data_len < 2 || auth_data[0] != 'v' || auth_data[1] != '=') { 0 0 0
if (!auth_data || auth_data_len < 2 || auth_data[0] != 'v' || auth_data[1] != '=') { 0 0 0
if (!auth_data || auth_data_len < 2 || auth_data[0] != 'v' || auth_data[1] != '=') { 0 0 0
if (!auth_data || auth_data_len < 2 || auth_data[0] != 'v' || auth_data[1] != '=') { 0 0 0
1514 0 conn_emit_error(aTHX_ self, "SCRAM: missing server signature");
1515 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1516 0 conn_handle_disconnect(aTHX_ self, "SCRAM verify failed");
1517 0 return;
1518 }
1519 0 const char *recv_b64 = auth_data + 2;
1520 0 int recv_b64_len = (int)(auth_data_len - 2);
1521  
1522 /* Compute expected server signature. */
1523 0 const EVP_MD *md = NULL;
1524 0 ▶ if (strcmp(self->sasl_mechanism, "SCRAM-SHA-256") == 0) md = EVP_sha256();
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(self->sasl_mechanism, "SCRAM-SHA-256") == 0) md = EVP_sha256(); 0 0 0
1525 0 ▶ else if (strcmp(self->sasl_mechanism, "SCRAM-SHA-512") == 0) md = EVP_sha512();
Branch: 1 branch
BranchTrueFalseTotal
else if (strcmp(self->sasl_mechanism, "SCRAM-SHA-512") == 0) md = EVP_sha512(); 0 0 0
1526 0 ▶ if (!md || self->scram_server_key_len <= 0 || !self->scram_auth_message) {
Branch: 3 branches
BranchTrueFalseTotal
if (!md || self->scram_server_key_len <= 0 || !self->scram_auth_message) { 0 0 0
if (!md || self->scram_server_key_len <= 0 || !self->scram_auth_message) { 0 0 0
if (!md || self->scram_server_key_len <= 0 || !self->scram_auth_message) { 0 0 0
1527 0 conn_emit_error(aTHX_ self, "SCRAM: missing state for verification");
1528 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1529 0 conn_handle_disconnect(aTHX_ self, "SCRAM verify failed");
1530 0 return;
1531 }
1532 unsigned char expected[64];
1533 0 unsigned int expected_len = (unsigned int)self->scram_server_key_len;
1534 0 HMAC(md, self->scram_server_key, self->scram_server_key_len,
1535 0 (unsigned char *)self->scram_auth_message,
1536 self->scram_auth_message_len, expected, &expected_len);
1537  
1538 /* Decode the received base64 signature. */
1539 unsigned char recv_sig[64];
1540 0 int recv_sig_len = 0;
1541 {
1542 0 BIO *b64 = BIO_new(BIO_f_base64());
1543 0 BIO *bmem = BIO_new_mem_buf(recv_b64, recv_b64_len);
1544 0 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
1545 0 b64 = BIO_push(b64, bmem);
1546 0 recv_sig_len = BIO_read(b64, recv_sig, (int)sizeof(recv_sig));
1547 0 BIO_free_all(b64);
1548 }
1549  
1550 0 int ok = (recv_sig_len == (int)expected_len)
1551 0 ▶ && (CRYPTO_memcmp(recv_sig, expected, expected_len) == 0);
Branch: 2 branches
BranchTrueFalseTotal
&& (CRYPTO_memcmp(recv_sig, expected, expected_len) == 0); 0 0 0
&& (CRYPTO_memcmp(recv_sig, expected, expected_len) == 0); 0 0 0
1552  
1553 /* Wipe ServerKey/AuthMessage now that verification is done. */
1554 0 OPENSSL_cleanse(self->scram_server_key, sizeof(self->scram_server_key));
1555 0 self->scram_server_key_len = 0;
1556 0 ▶ if (self->scram_auth_message) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->scram_auth_message) { 0 0 0
1557 0 OPENSSL_cleanse(self->scram_auth_message,
1558 self->scram_auth_message_len);
1559 0 Safefree(self->scram_auth_message);
1560 0 self->scram_auth_message = NULL;
1561 0 self->scram_auth_message_len = 0;
1562 }
1563  
1564 0 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 0 0 0
1565 0 conn_emit_error(aTHX_ self, "SCRAM: server signature mismatch");
1566 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1567 0 conn_handle_disconnect(aTHX_ self, "SCRAM verify failed");
1568 0 return;
1569 }
1570 0 self->scram_step = SCRAM_STEP_DONE;
1571 /* fall through to CONN_READY */
1572 }
1573 #endif
1574  
1575 #ifdef HAVE_OPENSSL
1576 /* SCRAM configured but the exchange never completed: no server
1577 * signature was verified — refuse. */
1578 0 ▶ if (self->sasl_mechanism
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_mechanism 0 0 0
1579 0 ▶ && strncmp(self->sasl_mechanism, "SCRAM-", 6) == 0
Branch: 1 branch
BranchTrueFalseTotal
&& strncmp(self->sasl_mechanism, "SCRAM-", 6) == 0 0 0 0
1580 0 ▶ && self->scram_step != SCRAM_STEP_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
&& self->scram_step != SCRAM_STEP_DONE) { 0 0 0
1581 0 conn_emit_error(aTHX_ self, "SASL: incomplete SCRAM exchange");
1582 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1583 0 conn_handle_disconnect(aTHX_ self, "SASL auth failed");
1584 0 return;
1585 }
1586 #endif
1587  
1588 /* Auth success — connection is ready */
1589 0 self->state = CONN_READY;
1590 0 conn_disarm_timer(self); /* handshake done */
1591 0 conn_emit_connect(aTHX_ self);
1592 0 return;
1593  
1594 0 err:
1595 0 conn_emit_error(aTHX_ self, "malformed SaslAuthenticate response");
1596 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1597 0 conn_handle_disconnect(aTHX_ self, "malformed SaslAuthenticate response");
1598 }
1599  
1600 /* ================================================================
1601 * Response dispatch
1602 * ================================================================ */
1603  
1604 176 static void conn_dispatch_response(pTHX_ ev_kafka_conn_t *self,
1605 ev_kafka_conn_cb_t *cbt, const char *data, size_t len)
1606 {
1607 /* Internal handshake responses */
1608 176 ▶ if (cbt->internal) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->internal) { 62 114 176
1609 62 switch (cbt->api_key) {
1610 59 case API_API_VERSIONS:
1611 59 conn_parse_api_versions_response(aTHX_ self, data, len);
1612 59 break;
1613 3 case API_SASL_HANDSHAKE:
1614 3 conn_parse_sasl_handshake_response(aTHX_ self, data, len);
1615 3 break;
1616 0 case API_SASL_AUTHENTICATE:
1617 0 conn_parse_sasl_authenticate_response(aTHX_ self, data, len);
1618 0 break;
1619 0 default: break;
1620 }
1621 62 return;
1622 }
1623  
1624 /* User-facing responses: parse and invoke callback */
1625 114 ▶ if (!cbt->cb) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cbt->cb) return; 0 114 114
1626  
1627 114 switch (cbt->api_key) {
1628 41 case API_METADATA: {
1629 /* Parse metadata response and return as Perl hash */
1630 41 SV *result = conn_parse_metadata_response(aTHX_ self, cbt->api_version, data, len);
1631 41 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1632 41 break;
1633 }
1634 14 case API_PRODUCE: {
1635 14 SV *result = conn_parse_produce_response(aTHX_ self, cbt->api_version, data, len);
1636 14 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1637 14 break;
1638 }
1639 12 case API_FETCH: {
1640 12 SV *result = conn_parse_fetch_response(aTHX_ self, cbt->api_version, data, len);
1641 12 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1642 12 break;
1643 }
1644 4 case API_LIST_OFFSETS: {
1645 4 SV *result = conn_parse_list_offsets_response(aTHX_ self, cbt->api_version, data, len);
1646 4 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1647 4 break;
1648 }
1649 9 case API_FIND_COORDINATOR: {
1650 9 SV *result = conn_parse_find_coordinator_response(aTHX_ self, cbt->api_version, data, len);
1651 9 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1652 9 break;
1653 }
1654 4 case API_JOIN_GROUP: {
1655 4 SV *result = conn_parse_join_group_response(aTHX_ self, cbt->api_version, data, len);
1656 4 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1657 4 break;
1658 }
1659 4 case API_SYNC_GROUP: {
1660 4 SV *result = conn_parse_sync_group_response(aTHX_ self, cbt->api_version, data, len);
1661 4 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1662 4 break;
1663 }
1664 1 case API_HEARTBEAT: {
1665 1 SV *result = conn_parse_heartbeat_response(aTHX_ self, cbt->api_version, data, len);
1666 1 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1667 1 break;
1668 }
1669 2 case API_OFFSET_COMMIT: {
1670 2 SV *result = conn_parse_offset_commit_response(aTHX_ self, cbt->api_version, data, len);
1671 2 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1672 2 break;
1673 }
1674 4 case API_OFFSET_FETCH: {
1675 4 SV *result = conn_parse_offset_fetch_response(aTHX_ self, cbt->api_version, data, len);
1676 4 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1677 4 break;
1678 }
1679 1 case API_LEAVE_GROUP: {
1680 1 SV *result = conn_parse_leave_group_response(aTHX_ self, cbt->api_version, data, len);
1681 1 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1682 1 break;
1683 }
1684 0 case API_CREATE_TOPICS: {
1685 0 SV *result = conn_parse_create_topics_response(aTHX_ self, cbt->api_version, data, len);
1686 0 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1687 0 break;
1688 }
1689 0 case API_DELETE_TOPICS: {
1690 0 SV *result = conn_parse_delete_topics_response(aTHX_ self, cbt->api_version, data, len);
1691 0 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1692 0 break;
1693 }
1694 10 case API_INIT_PRODUCER_ID: {
1695 10 SV *result = conn_parse_init_producer_id_response(aTHX_ self, cbt->api_version, data, len);
1696 10 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1697 10 break;
1698 }
1699 4 case API_ADD_PARTITIONS_TXN: {
1700 4 SV *result = conn_parse_add_partitions_to_txn_response(aTHX_ self, cbt->api_version, data, len);
1701 4 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1702 4 break;
1703 }
1704 2 case API_END_TXN: {
1705 2 SV *result = conn_parse_end_txn_response(aTHX_ self, cbt->api_version, data, len);
1706 2 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1707 2 break;
1708 }
1709 2 case API_TXN_OFFSET_COMMIT: {
1710 2 SV *result = conn_parse_txn_offset_commit_response(aTHX_ self, cbt->api_version, data, len);
1711 2 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1712 2 break;
1713 }
1714 0 default: {
1715 /* Unknown API — return raw bytes */
1716 0 SV *result = newSVpvn(data, len);
1717 0 conn_invoke_cb(aTHX_ self, cbt->cb, sv_2mortal(result), NULL);
1718 0 break;
1719 }
1720 }
1721 }
1722  
1723 /* ================================================================
1724 * RecordBatch encoder (for Produce requests)
1725 * ================================================================ */
1726  
1727 /* Build a single Record (within a RecordBatch).
1728 * Format: length(varint), attributes(i8=0), timestampDelta(varint),
1729 * offsetDelta(varint), key(varint-bytes), value(varint-bytes),
1730 * headers(varint array of {key:varint-str, value:varint-bytes})
1731 */
1732 168 static void kf_encode_record(kf_buf_t *b, int offset_delta, int64_t ts_delta,
1733 const char *key, STRLEN key_len, const char *value, STRLEN value_len,
1734 HV *headers)
1735 {
1736 kf_buf_t rec;
1737 168 kf_buf_init(&rec);
1738  
1739 168 kf_buf_append_i8(&rec, 0); /* attributes */
1740 168 kf_buf_append_varint(&rec, ts_delta);
1741 168 kf_buf_append_varint(&rec, offset_delta);
1742  
1743 /* key */
1744 168 ▶ if (key) {
Branch: 1 branch
BranchTrueFalseTotal
if (key) { 167 1 168
1745 167 kf_buf_append_varint(&rec, (int64_t)key_len);
1746 167 kf_buf_append(&rec, key, key_len);
1747 } else {
1748 1 kf_buf_append_varint(&rec, -1);
1749 }
1750  
1751 /* value */
1752 168 ▶ if (value) {
Branch: 1 branch
BranchTrueFalseTotal
if (value) { 167 1 168
1753 167 kf_buf_append_varint(&rec, (int64_t)value_len);
1754 167 kf_buf_append(&rec, value, value_len);
1755 } else {
1756 1 kf_buf_append_varint(&rec, -1);
1757 }
1758  
1759 /* headers */
1760 169 ▶ if (headers && HvUSEDKEYS(headers) > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (headers && HvUSEDKEYS(headers) > 0) { 1 167 168
if (headers && HvUSEDKEYS(headers) > 0) { 0 1 1
if (headers && HvUSEDKEYS(headers) > 0) { 1 0 1
1761 1 ▶ kf_buf_append_varint(&rec, (int64_t)HvUSEDKEYS(headers));
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_varint(&rec, (int64_t)HvUSEDKEYS(headers)); 0 1 1
1762 HE *entry;
1763 1 hv_iterinit(headers);
1764 3 ▶ while ((entry = hv_iternext(headers))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(headers))) { 2 1 3
1765 I32 hklen;
1766 2 const char *hkey = hv_iterkey(entry, &hklen);
1767 2 SV *hval = hv_iterval(headers, entry);
1768 STRLEN hvlen;
1769 2 const char *hvstr = SvPV(hval, hvlen);
1770  
1771 2 kf_buf_append_varint(&rec, (int64_t)hklen);
1772 2 kf_buf_append(&rec, hkey, hklen);
1773 2 kf_buf_append_varint(&rec, (int64_t)hvlen);
1774 2 kf_buf_append(&rec, hvstr, hvlen);
1775 }
1776 } else {
1777 167 kf_buf_append_varint(&rec, 0); /* 0 headers */
1778 }
1779  
1780 /* Write record: length(varint) + body */
1781 168 kf_buf_append_varint(b, (int64_t)rec.len);
1782 168 kf_buf_append(b, rec.data, rec.len);
1783 168 kf_buf_free(&rec);
1784 168 }
1785  
1786 /* Build a RecordBatch from one or more records, with optional
1787 * compression and producer-ID/transactional state. Caller frees *out. */
1788 40 static void kf_encode_record_batch_multi(pTHX_ kf_buf_t *out,
1789 AV *records_av, int64_t timestamp, int compression,
1790 int64_t producer_id, int16_t producer_epoch, int32_t base_sequence,
1791 int is_transactional)
1792 {
1793 40 SSize_t i, count = av_len(records_av) + 1;
1794  
1795 /* Validate up front so croak() doesn't longjmp over kf_buf_free below. */
1796 208 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 168 40 208
1797 168 SV **elem = av_fetch(records_av, i, 0);
1798 168 ▶ if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 168 0 168
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 168 0 168
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 0 168 168
1799 0 croak("produce_batch: record element must be a hashref");
1800 }
1801  
1802 kf_buf_t records;
1803 40 kf_buf_init(&records);
1804  
1805 208 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 168 40 208
1806 168 SV **elem = av_fetch(records_av, i, 0);
1807 168 HV *rh = (HV*)SvRV(*elem);
1808 168 SV **key_sv = hv_fetch(rh, "key", 3, 0);
1809 168 SV **val_sv = hv_fetch(rh, "value", 5, 0);
1810 168 SV **hdr_sv = hv_fetch(rh, "headers", 7, 0);
1811 168 const char *key = NULL; STRLEN key_len = 0;
1812 168 const char *val = NULL; STRLEN val_len = 0;
1813 168 HV *hdrs = NULL;
1814 335 ▶ if (key_sv && SvOK(*key_sv)) key = SvPV(*key_sv, key_len);
Branch: 2 branches
BranchTrueFalseTotal
if (key_sv && SvOK(*key_sv)) key = SvPV(*key_sv, key_len); 168 0 168
if (key_sv && SvOK(*key_sv)) key = SvPV(*key_sv, key_len); 167 1 168
1815 335 ▶ if (val_sv && SvOK(*val_sv)) val = SvPV(*val_sv, val_len);
Branch: 2 branches
BranchTrueFalseTotal
if (val_sv && SvOK(*val_sv)) val = SvPV(*val_sv, val_len); 168 0 168
if (val_sv && SvOK(*val_sv)) val = SvPV(*val_sv, val_len); 167 1 168
1816 168 ▶ if (hdr_sv && SvROK(*hdr_sv) && SvTYPE(SvRV(*hdr_sv)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (hdr_sv && SvROK(*hdr_sv) && SvTYPE(SvRV(*hdr_sv)) == SVt_PVHV) 1 167 168
if (hdr_sv && SvROK(*hdr_sv) && SvTYPE(SvRV(*hdr_sv)) == SVt_PVHV) 1 0 1
if (hdr_sv && SvROK(*hdr_sv) && SvTYPE(SvRV(*hdr_sv)) == SVt_PVHV) 1 0 1
1817 1 hdrs = (HV*)SvRV(*hdr_sv);
1818 168 kf_encode_record(&records, (int)i, 0, key, key_len, val, val_len, hdrs);
1819 }
1820  
1821 kf_buf_t inner;
1822 40 kf_buf_init(&inner);
1823  
1824 40 int16_t attrs = (int16_t)(compression & 0x07);
1825 40 ▶ if (is_transactional) attrs |= 0x10; /* bit 4 = isTransactional */
Branch: 1 branch
BranchTrueFalseTotal
if (is_transactional) attrs |= 0x10; /* bit 4 = isTransactional */ 3 37 40
1826 40 kf_buf_append_i16(&inner, attrs);
1827 40 kf_buf_append_i32(&inner, (int32_t)(count - 1)); /* lastOffsetDelta */
1828 40 kf_buf_append_i64(&inner, timestamp);
1829 40 kf_buf_append_i64(&inner, timestamp);
1830 40 kf_buf_append_i64(&inner, producer_id);
1831 40 kf_buf_append_i16(&inner, producer_epoch);
1832 40 kf_buf_append_i32(&inner, base_sequence);
1833  
1834 #ifdef HAVE_LZ4
1835 if (compression == COMPRESS_LZ4) {
1836 /* Kafka's wire format is LZ4 FRAME (KIP-57), not the raw block API. */
1837 LZ4F_compressionContext_t cctx;
1838 size_t bound = LZ4F_compressFrameBound(records.len, NULL);
1839 char *compressed;
1840 Newx(compressed, bound, char);
1841 size_t clen = 0;
1842 if (!LZ4F_isError(LZ4F_createCompressionContext(&cctx, LZ4F_VERSION))) {
1843 size_t n = LZ4F_compressBegin(cctx, compressed, bound, NULL);
1844 if (!LZ4F_isError(n)) {
1845 clen += n;
1846 n = LZ4F_compressUpdate(cctx, compressed + clen, bound - clen,
1847 records.data, records.len, NULL);
1848 if (!LZ4F_isError(n)) {
1849 clen += n;
1850 n = LZ4F_compressEnd(cctx, compressed + clen, bound - clen,
1851 NULL);
1852 if (!LZ4F_isError(n))
1853 clen += n;
1854 else
1855 clen = 0;
1856 } else clen = 0;
1857 }
1858 LZ4F_freeCompressionContext(cctx);
1859 }
1860 if (clen > 0) {
1861 kf_buf_append_i32(&inner, (int32_t)count);
1862 kf_buf_append(&inner, compressed, clen);
1863 } else {
1864 /* compression failed: fall back to uncompressed. Clear only the
1865 * compression bits (0-2) in the big-endian attrs (low byte, at
1866 * offset 1), preserving isTransactional (bit 4) and other flags. */
1867 inner.data[1] &= (char)~0x07;
1868 kf_buf_append_i32(&inner, (int32_t)count);
1869 kf_buf_append(&inner, records.data, records.len);
1870 }
1871 Safefree(compressed);
1872 } else
1873 #endif
1874 #ifdef HAVE_ZLIB
1875 40 ▶ if (compression == COMPRESS_GZIP) {
Branch: 1 branch
BranchTrueFalseTotal
if (compression == COMPRESS_GZIP) { 2 38 40
1876 2 size_t dest_cap = compressBound((uLong)records.len) + 32;
1877 char *compressed;
1878 2 Newx(compressed, dest_cap, char);
1879 z_stream zs;
1880 2 Zero(&zs, 1, z_stream);
1881 2 int zinit = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
1882 MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY);
1883 2 int zok = 0;
1884 2 ▶ if (zinit == Z_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (zinit == Z_OK) { 2 0 2
1885 2 zs.next_in = (Bytef *)records.data;
1886 2 zs.avail_in = (uInt)records.len;
1887 2 zs.next_out = (Bytef *)compressed;
1888 2 zs.avail_out = (uInt)dest_cap;
1889 2 ▶ if (deflate(&zs, Z_FINISH) == Z_STREAM_END) zok = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (deflate(&zs, Z_FINISH) == Z_STREAM_END) zok = 1; 2 0 2
1890 2 deflateEnd(&zs);
1891 }
1892 2 ▶ if (zok) {
Branch: 1 branch
BranchTrueFalseTotal
if (zok) { 2 0 2
1893 2 kf_buf_append_i32(&inner, (int32_t)count);
1894 2 kf_buf_append(&inner, compressed, zs.total_out);
1895 } else {
1896 /* compression failed: fall back to uncompressed. Clear only the
1897 * compression bits (0-2) in the big-endian attrs (low byte, at
1898 * offset 1), preserving isTransactional (bit 4) and other flags. */
1899 0 inner.data[1] &= (char)~0x07;
1900 0 kf_buf_append_i32(&inner, (int32_t)count);
1901 0 kf_buf_append(&inner, records.data, records.len);
1902 }
1903 2 Safefree(compressed);
1904 } else
1905 #endif
1906 #ifdef HAVE_ZSTD
1907 if (compression == COMPRESS_ZSTD) {
1908 size_t dest_cap = ZSTD_compressBound(records.len);
1909 char *compressed;
1910 Newx(compressed, dest_cap, char);
1911 size_t clen = ZSTD_compress(compressed, dest_cap,
1912 records.data, records.len, ZSTD_CLEVEL_DEFAULT);
1913 if (!ZSTD_isError(clen)) {
1914 kf_buf_append_i32(&inner, (int32_t)count);
1915 kf_buf_append(&inner, compressed, clen);
1916 } else {
1917 /* compression failed: fall back to uncompressed. Clear only the
1918 * compression bits (0-2) in the big-endian attrs (low byte, at
1919 * offset 1), preserving isTransactional (bit 4) and other flags. */
1920 inner.data[1] &= (char)~0x07;
1921 kf_buf_append_i32(&inner, (int32_t)count);
1922 kf_buf_append(&inner, records.data, records.len);
1923 }
1924 Safefree(compressed);
1925 } else
1926 #endif
1927 #ifdef HAVE_SNAPPY
1928 if (compression == COMPRESS_SNAPPY) {
1929 size_t dest_cap = snappy_max_compressed_length(records.len);
1930 char *compressed;
1931 Newx(compressed, dest_cap, char);
1932 size_t clen = dest_cap;
1933 if (snappy_compress(records.data, records.len, compressed, &clen)
1934 == SNAPPY_OK) {
1935 kf_buf_append_i32(&inner, (int32_t)count);
1936 kf_buf_append(&inner, compressed, clen);
1937 } else {
1938 /* compression failed: fall back to uncompressed. Clear only the
1939 * compression bits (0-2) in the big-endian attrs (low byte, at
1940 * offset 1), preserving isTransactional (bit 4) and other flags. */
1941 inner.data[1] &= (char)~0x07;
1942 kf_buf_append_i32(&inner, (int32_t)count);
1943 kf_buf_append(&inner, records.data, records.len);
1944 }
1945 Safefree(compressed);
1946 } else
1947 #endif
1948 {
1949 /* Requested codec not built in (or COMPRESS_NONE): emit uncompressed
1950 * and clear the compression bits so the batch is not mislabeled. */
1951 38 ▶ if (compression != COMPRESS_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if (compression != COMPRESS_NONE) 8 30 38
1952 8 inner.data[1] &= (char)~0x07;
1953 38 kf_buf_append_i32(&inner, (int32_t)count);
1954 38 kf_buf_append(&inner, records.data, records.len);
1955 }
1956  
1957 40 uint32_t crc_val = crc32c(inner.data, inner.len);
1958 40 int32_t batch_length = 4 + 1 + 4 + (int32_t)inner.len;
1959  
1960 40 kf_buf_init(out);
1961 40 kf_buf_append_i64(out, 0);
1962 40 kf_buf_append_i32(out, batch_length);
1963 40 kf_buf_append_i32(out, 0);
1964 40 kf_buf_append_i8(out, 2);
1965 40 kf_buf_append_i32(out, (int32_t)crc_val);
1966 40 kf_buf_append(out, inner.data, inner.len);
1967  
1968 40 kf_buf_free(&inner);
1969 40 kf_buf_free(&records);
1970 40 }
1971  
1972 /* ================================================================
1973 * Response parsers
1974 *
1975 * Ownership: every HV/AV is mortalized at birth and adopted by its
1976 * parent with newRV_inc; the root is sv_2mortal(newRV_noinc(root)).
1977 * The per-event SAVETMPS frame in the watcher wrappers reclaims all of
1978 * it, including nodes abandoned at "goto done" on malformed input.
1979 * ================================================================ */
1980  
1981 /* Metadata response parser (API 3) */
1982 52045 static SV* conn_parse_metadata_response(pTHX_ ev_kafka_conn_t *self,
1983 int16_t version, const char *data, size_t len)
1984 {
1985 52045 const char *p = data;
1986 52045 const char *end = data + len;
1987 52045 HV *result = newHV();
1988 52045 AV *brokers_av = (AV*)sv_2mortal((SV*)newAV());
1989 52045 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
1990 int n;
1991  
1992 (void)self;
1993  
1994 /* For v9+ (flexible versions), fields use compact encoding */
1995 52045 int flexible = (version >= 9);
1996  
1997 52045 ▶ if (flexible) {
Branch: 1 branch
BranchTrueFalseTotal
if (flexible) { 52002 43 52045
1998 /* throttle_time_ms */
1999 104004 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 52002 52002
2000 52002 /* int32_t throttle = kf_read_i32(p); */ p += 4;
2001  
2002 /* brokers: compact array */
2003 uint64_t raw;
2004 52002 n = kf_read_uvarint(p, end, &raw);
2005 52002 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 52002 52002
2006 52002 p += n;
2007 52002 int32_t broker_count = (int32_t)(raw - 1);
2008 int32_t i;
2009  
2010 52002 ▶ for (i = 0; i < broker_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < broker_count; i++) { 52002 0 52002
2011 52002 HV *bh = (HV*)sv_2mortal((SV*)newHV());
2012 104004 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 52002 52002
2013 52002 int32_t nid = kf_read_i32(p); p += 4;
2014 52002 hv_store(bh, "node_id", 7, newSViv(nid), 0);
2015  
2016 const char *host; int32_t hlen;
2017 52002 n = kf_read_compact_string(p, end, &host, &hlen);
2018 52002 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 52002 0 52002
2019 0 p += n;
2020 0 ▶ hv_store(bh, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(bh, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0); 0 0 0
hv_store(bh, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0); 0 0 0
2021  
2022 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2023 0 int32_t port = kf_read_i32(p); p += 4;
2024 0 hv_store(bh, "port", 4, newSViv(port), 0);
2025  
2026 /* rack: compact nullable string */
2027 const char *rack; int32_t rlen;
2028 0 n = kf_read_compact_string(p, end, &rack, &rlen);
2029 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2030 0 p += n;
2031  
2032 /* tagged fields */
2033 0 n = kf_skip_tagged_fields(p, end);
2034 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2035 0 p += n;
2036  
2037 0 av_push(brokers_av, newRV_inc((SV*)bh));
2038 }
2039  
2040 /* cluster_id */
2041 const char *cid; int32_t cidlen;
2042 0 n = kf_read_compact_string(p, end, &cid, &cidlen);
2043 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2044 0 p += n;
2045  
2046 /* controller_id */
2047 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2048 0 int32_t controller_id = kf_read_i32(p); p += 4;
2049 0 hv_store(result, "controller_id", 13, newSViv(controller_id), 0);
2050  
2051 /* topics: compact array */
2052 0 n = kf_read_uvarint(p, end, &raw);
2053 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2054 0 p += n;
2055 0 int32_t topic_count = (int32_t)(raw - 1);
2056  
2057 0 ▶ for (i = 0; i < topic_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < topic_count; i++) { 0 0 0
2058 0 HV *th = (HV*)sv_2mortal((SV*)newHV());
2059 0 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 0 0
2060 0 int16_t terr = kf_read_i16(p); p += 2;
2061 0 hv_store(th, "error_code", 10, newSViv(terr), 0);
2062  
2063 const char *tname; int32_t tnlen;
2064 0 n = kf_read_compact_string(p, end, &tname, &tnlen);
2065 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2066 0 p += n;
2067 0 ▶ hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 0 0 0
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 0 0 0
2068  
2069 /* topic_id (UUID, 16 bytes) — v10+ */
2070 0 ▶ if (version >= 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 10) { 0 0 0
2071 0 ▶ if (end - p < 16) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 16) goto done; 0 0 0
2072 0 p += 16;
2073 }
2074  
2075 /* is_internal */
2076 0 ▶ if (end - p < 1) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 1) goto done; 0 0 0
2077 0 p += 1;
2078  
2079 /* partitions: compact array */
2080 0 n = kf_read_uvarint(p, end, &raw);
2081 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2082 0 p += n;
2083 0 int32_t part_count = (int32_t)(raw - 1);
2084 0 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
2085 int32_t j;
2086  
2087 0 ▶ for (j = 0; j < part_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < part_count; j++) { 0 0 0
2088 0 HV *ph = (HV*)sv_2mortal((SV*)newHV());
2089 0 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 0 0
2090 0 int16_t perr = kf_read_i16(p); p += 2;
2091 0 hv_store(ph, "error_code", 10, newSViv(perr), 0);
2092  
2093 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2094 0 int32_t pid = kf_read_i32(p); p += 4;
2095 0 hv_store(ph, "partition", 9, newSViv(pid), 0);
2096  
2097 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2098 0 int32_t leader = kf_read_i32(p); p += 4;
2099 0 hv_store(ph, "leader", 6, newSViv(leader), 0);
2100  
2101 /* leader_epoch — v7+ */
2102 0 ▶ if (version >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 7) { 0 0 0
2103 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2104 0 p += 4;
2105 }
2106  
2107 /* replicas: compact array of i32 */
2108 0 n = kf_read_uvarint(p, end, &raw);
2109 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2110 0 p += n;
2111 0 int32_t rcount = (int32_t)(raw - 1);
2112 0 ▶ if (rcount < 0 || rcount > 65536) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
2113 0 ▶ if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done; 0 0 0
2114 0 p += (int64_t)rcount * 4;
2115  
2116 /* isr: compact array of i32 */
2117 0 n = kf_read_uvarint(p, end, &raw);
2118 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2119 0 p += n;
2120 0 rcount = (int32_t)(raw - 1);
2121 0 ▶ if (rcount < 0 || rcount > 65536) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
2122 0 ▶ if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done; 0 0 0
2123 0 p += (int64_t)rcount * 4;
2124  
2125 /* offline_replicas: compact array of i32 — v5+ */
2126 0 ▶ if (version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 5) { 0 0 0
2127 0 n = kf_read_uvarint(p, end, &raw);
2128 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2129 0 p += n;
2130 0 rcount = (int32_t)(raw - 1);
2131 0 ▶ if (rcount < 0 || rcount > 65536) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
2132 0 ▶ if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done; 0 0 0
2133 0 p += (int64_t)rcount * 4;
2134 }
2135  
2136 /* tagged fields */
2137 0 n = kf_skip_tagged_fields(p, end);
2138 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2139 0 p += n;
2140  
2141 0 av_push(parts_av, newRV_inc((SV*)ph));
2142 }
2143 0 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
2144  
2145 /* topic authorized operations — v8+ */
2146 0 ▶ if (version >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 8) { 0 0 0
2147 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2148 0 p += 4;
2149 }
2150  
2151 /* tagged fields */
2152 0 n = kf_skip_tagged_fields(p, end);
2153 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2154 0 p += n;
2155  
2156 0 av_push(topics_av, newRV_inc((SV*)th));
2157 }
2158 } else {
2159 /* Non-flexible (v0-v8) — use classic STRING/ARRAY encoding */
2160 /* throttle_time_ms (v3+) */
2161 43 ▶ if (version >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 3) { 0 43 43
2162 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2163 0 p += 4;
2164 }
2165  
2166 /* brokers array */
2167 43 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 43 43
2168 43 int32_t broker_count = kf_read_i32(p); p += 4;
2169 43 ▶ if (broker_count < 0 || broker_count > 65536) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (broker_count < 0 || broker_count > 65536) goto done; 43 0 43
if (broker_count < 0 || broker_count > 65536) goto done; 0 43 43
2170 int32_t i;
2171 84 ▶ for (i = 0; i < broker_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < broker_count; i++) { 42 42 84
2172 42 HV *bh = (HV*)sv_2mortal((SV*)newHV());
2173 42 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 1 41 42
2174 41 int32_t nid = kf_read_i32(p); p += 4;
2175 41 hv_store(bh, "node_id", 7, newSViv(nid), 0);
2176  
2177 const char *host; int16_t hlen;
2178 41 n = kf_read_string(p, end, &host, &hlen);
2179 41 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 41 41
2180 41 p += n;
2181 41 ▶ hv_store(bh, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(bh, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0); 41 0 41
hv_store(bh, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0); 41 0 41
2182  
2183 41 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 41 41
2184 41 int32_t port = kf_read_i32(p); p += 4;
2185 41 hv_store(bh, "port", 4, newSViv(port), 0);
2186  
2187 /* rack (v1+) */
2188 41 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 40 1 41
2189 const char *r; int16_t rlen;
2190 40 n = kf_read_string(p, end, &r, &rlen);
2191 40 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 40 40
2192 40 p += n;
2193 }
2194  
2195 41 av_push(brokers_av, newRV_inc((SV*)bh));
2196 }
2197  
2198 /* cluster_id (v2+) */
2199 42 ▶ if (version >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 2) { 0 42 42
2200 const char *cid; int16_t cidlen;
2201 0 n = kf_read_string(p, end, &cid, &cidlen);
2202 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2203 0 p += n;
2204 }
2205  
2206 /* controller_id (v1+) */
2207 42 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 41 1 42
2208 41 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 41 41
2209 41 int32_t cid = kf_read_i32(p); p += 4;
2210 41 hv_store(result, "controller_id", 13, newSViv(cid), 0);
2211 }
2212  
2213 /* topics array */
2214 42 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 42 42
2215 42 int32_t topic_count = kf_read_i32(p); p += 4;
2216 42 ▶ if (topic_count < 0 || topic_count > 1000000) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (topic_count < 0 || topic_count > 1000000) goto done; 42 0 42
if (topic_count < 0 || topic_count > 1000000) goto done; 0 42 42
2217 83 ▶ for (i = 0; i < topic_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < topic_count; i++) { 41 42 83
2218 41 HV *th = (HV*)sv_2mortal((SV*)newHV());
2219 41 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 41 41
2220 41 int16_t terr = kf_read_i16(p); p += 2;
2221 41 hv_store(th, "error_code", 10, newSViv(terr), 0);
2222  
2223 const char *tname; int16_t tnlen;
2224 41 n = kf_read_string(p, end, &tname, &tnlen);
2225 41 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 41 41
2226 41 p += n;
2227 41 ▶ hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 41 0 41
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 41 0 41
2228  
2229 /* is_internal (v1+) */
2230 41 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 40 1 41
2231 40 ▶ if (end - p < 1) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 1) goto done; 0 40 40
2232 40 p += 1;
2233 }
2234  
2235 /* partitions */
2236 41 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 41 41
2237 41 int32_t part_count = kf_read_i32(p); p += 4;
2238 41 ▶ if (part_count < 0 || part_count > 1000000) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (part_count < 0 || part_count > 1000000) goto done; 41 0 41
if (part_count < 0 || part_count > 1000000) goto done; 0 41 41
2239 41 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
2240 int32_t j;
2241 83 ▶ for (j = 0; j < part_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < part_count; j++) { 42 41 83
2242 42 HV *ph = (HV*)sv_2mortal((SV*)newHV());
2243 42 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 42 42
2244 42 int16_t perr = kf_read_i16(p); p += 2;
2245 42 hv_store(ph, "error_code", 10, newSViv(perr), 0);
2246  
2247 42 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 42 42
2248 42 int32_t pid = kf_read_i32(p); p += 4;
2249 42 hv_store(ph, "partition", 9, newSViv(pid), 0);
2250  
2251 42 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 42 42
2252 42 int32_t leader = kf_read_i32(p); p += 4;
2253 42 hv_store(ph, "leader", 6, newSViv(leader), 0);
2254  
2255 /* leader_epoch (v7+) */
2256 42 ▶ if (version >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 7) { 0 42 42
2257 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2258 0 p += 4;
2259 }
2260  
2261 /* replicas */
2262 42 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 42 42
2263 42 int32_t rcount = kf_read_i32(p); p += 4;
2264 42 ▶ if (rcount < 0 || rcount > 65536) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (rcount < 0 || rcount > 65536) goto done; 42 0 42
if (rcount < 0 || rcount > 65536) goto done; 0 42 42
2265 42 ▶ if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done; 0 42 42
2266 42 p += (int64_t)rcount * 4;
2267  
2268 /* isr */
2269 42 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 42 42
2270 42 rcount = kf_read_i32(p); p += 4;
2271 42 ▶ if (rcount < 0 || rcount > 65536) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (rcount < 0 || rcount > 65536) goto done; 42 0 42
if (rcount < 0 || rcount > 65536) goto done; 0 42 42
2272 42 ▶ if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done; 0 42 42
2273 42 p += (int64_t)rcount * 4;
2274  
2275 /* offline_replicas (v5+) */
2276 42 ▶ if (version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 5) { 0 42 42
2277 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2278 0 rcount = kf_read_i32(p); p += 4;
2279 0 ▶ if (rcount < 0 || rcount > 65536) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
2280 0 ▶ if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done; 0 0 0
2281 0 p += (int64_t)rcount * 4;
2282 }
2283  
2284 42 av_push(parts_av, newRV_inc((SV*)ph));
2285 }
2286 41 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
2287  
2288 41 av_push(topics_av, newRV_inc((SV*)th));
2289 }
2290 }
2291  
2292 42 done:
2293 52045 hv_store(result, "brokers", 7, newRV_inc((SV*)brokers_av), 0);
2294 52045 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
2295 52045 return sv_2mortal(newRV_noinc((SV*)result));
2296 }
2297  
2298 /* Produce response parser (API 0, v0-v7) */
2299 15 static SV* conn_parse_produce_response(pTHX_ ev_kafka_conn_t *self,
2300 int16_t version, const char *data, size_t len)
2301 {
2302 15 const char *p = data;
2303 15 const char *end = data + len;
2304 15 HV *result = newHV();
2305 15 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
2306 int n;
2307  
2308 (void)self;
2309  
2310 /* responses: ARRAY */
2311 15 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 15 15
2312 15 int32_t topic_count = kf_read_i32(p); p += 4;
2313 int32_t i;
2314  
2315 30 ▶ for (i = 0; i < topic_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < topic_count; i++) { 15 15 30
2316 15 HV *th = (HV*)sv_2mortal((SV*)newHV());
2317 const char *tname; int16_t tnlen;
2318 15 n = kf_read_string(p, end, &tname, &tnlen);
2319 15 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 15 15
2320 15 p += n;
2321 15 ▶ hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 15 0 15
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 15 0 15
2322  
2323 /* partitions: ARRAY */
2324 15 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 15 15
2325 15 int32_t part_count = kf_read_i32(p); p += 4;
2326 15 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
2327 int32_t j;
2328  
2329 30 ▶ for (j = 0; j < part_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < part_count; j++) { 15 15 30
2330 15 HV *ph = (HV*)sv_2mortal((SV*)newHV());
2331 15 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 15 15
2332 15 int32_t pid = kf_read_i32(p); p += 4;
2333 15 hv_store(ph, "partition", 9, newSViv(pid), 0);
2334  
2335 15 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 15 15
2336 15 int16_t err = kf_read_i16(p); p += 2;
2337 15 hv_store(ph, "error_code", 10, newSViv(err), 0);
2338  
2339 15 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 15 15
2340 15 int64_t base_offset = kf_read_i64(p); p += 8;
2341 15 hv_store(ph, "base_offset", 11, newSViv(base_offset), 0);
2342  
2343 /* log_append_time (v2+) */
2344 15 ▶ if (version >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 2) { 14 1 15
2345 14 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 14 14
2346 14 p += 8;
2347 }
2348  
2349 /* log_start_offset (v5+) */
2350 15 ▶ if (version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 5) { 14 1 15
2351 14 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 14 14
2352 14 p += 8;
2353 }
2354  
2355 15 av_push(parts_av, newRV_inc((SV*)ph));
2356 }
2357 15 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
2358 15 av_push(topics_av, newRV_inc((SV*)th));
2359 }
2360  
2361 /* throttle_time_ms (v1+) */
2362 15 ▶ if (version >= 1 && end - p >= 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 1 && end - p >= 4) { 1 14 15
if (version >= 1 && end - p >= 4) { 0 14 14
2363 14 int32_t throttle = kf_read_i32(p); p += 4;
2364 14 hv_store(result, "throttle_time_ms", 16, newSViv(throttle), 0);
2365 }
2366  
2367 1 done:
2368 15 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
2369 15 return sv_2mortal(newRV_noinc((SV*)result));
2370 }
2371  
2372 /* ================================================================
2373 * RecordBatch decoder (for Fetch responses)
2374 * ================================================================ */
2375  
2376 /* Decode records from a RecordBatch, push them as hashrefs onto records_av.
2377 * Returns number of records decoded, or -1 on error. */
2378 21 static int kf_decode_record_batch(pTHX_ const char *data, size_t len,
2379 AV *records_av, int64_t *out_base_offset)
2380 {
2381 21 const char *p = data;
2382 21 const char *end = data + len;
2383 int n;
2384  
2385 21 ▶ if (end - p < 12) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 12) return -1; 0 21 21
2386 21 int64_t base_offset = kf_read_i64(p); p += 8;
2387 21 ▶ if (out_base_offset) *out_base_offset = base_offset;
Branch: 1 branch
BranchTrueFalseTotal
if (out_base_offset) *out_base_offset = base_offset; 21 0 21
2388 21 int32_t batch_length = kf_read_i32(p); p += 4;
2389  
2390 /* compare in 64-bit so a huge batch_length cannot wrap the check */
2391 21 ▶ if (batch_length < 0 || (int64_t)(end - p) < (int64_t)batch_length) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (batch_length < 0 || (int64_t)(end - p) < (int64_t)batch_length) return -1; 21 0 21
if (batch_length < 0 || (int64_t)(end - p) < (int64_t)batch_length) return -1; 1 20 21
2392 20 const char *batch_end = p + batch_length;
2393  
2394 20 ▶ if (batch_end - p < 9) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (batch_end - p < 9) return -1; 0 20 20
2395 20 /* int32_t partition_leader_epoch = kf_read_i32(p); */ p += 4;
2396 20 int8_t magic = (int8_t)*p; p += 1;
2397 20 ▶ if (magic != 2) return -1; /* only support magic=2 (current format) */
Branch: 1 branch
BranchTrueFalseTotal
if (magic != 2) return -1; /* only support magic=2 (current format) */ 1 19 20
2398 19 uint32_t expected_crc = (uint32_t)kf_read_i32(p); p += 4;
2399 /* CRC32C covers the bytes from attributes to end of batch. */
2400 19 ▶ if (crc32c(p, (size_t)(batch_end - p)) != expected_crc) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (crc32c(p, (size_t)(batch_end - p)) != expected_crc) return -1; 1 18 19
2401  
2402 18 ▶ if (batch_end - p < 36) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (batch_end - p < 36) return -1; 0 18 18
2403 18 int16_t attributes = kf_read_i16(p); p += 2;
2404 18 int compression_type = attributes & 0x07;
2405 18 /* int32_t last_offset_delta = kf_read_i32(p); */ p += 4;
2406 18 int64_t first_timestamp = kf_read_i64(p); p += 8;
2407 18 /* int64_t max_timestamp = kf_read_i64(p); */ p += 8;
2408 18 /* int64_t producer_id = kf_read_i64(p); */ p += 8;
2409 18 /* int16_t producer_epoch = kf_read_i16(p); */ p += 2;
2410 18 /* int32_t base_sequence = kf_read_i32(p); */ p += 4;
2411  
2412 18 ▶ if (batch_end - p < 4) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (batch_end - p < 4) return -1; 0 18 18
2413 18 int32_t record_count = kf_read_i32(p); p += 4;
2414  
2415 /* Decompress if needed */
2416 18 const char *rec_data = p;
2417 18 const char *rec_end = batch_end;
2418 18 char *decompressed = NULL;
2419  
2420 18 ▶ if (compression_type != COMPRESS_NONE && batch_end > p) {
Branch: 2 branches
BranchTrueFalseTotal
if (compression_type != COMPRESS_NONE && batch_end > p) { 1 17 18
if (compression_type != COMPRESS_NONE && batch_end > p) { 1 0 1
2421 1 size_t compressed_len = batch_end - p;
2422 1 size_t decomp_cap = compressed_len * 4;
2423 1 ▶ if (decomp_cap < 4096) decomp_cap = 4096;
Branch: 1 branch
BranchTrueFalseTotal
if (decomp_cap < 4096) decomp_cap = 4096; 1 0 1
2424 /* set only by a codec branch that fully succeeded (see below) */
2425 1 int decompressed_ok = 0;
2426  
2427 #ifdef HAVE_ZLIB
2428 1 ▶ if (compression_type == COMPRESS_GZIP) {
Branch: 1 branch
BranchTrueFalseTotal
if (compression_type == COMPRESS_GZIP) { 1 0 1
2429 1 int zok = 0;
2430 2 ▶ while (!zok && decomp_cap < 64 * 1024 * 1024) {
Branch: 2 branches
BranchTrueFalseTotal
while (!zok && decomp_cap < 64 * 1024 * 1024) { 1 1 2
while (!zok && decomp_cap < 64 * 1024 * 1024) { 1 0 1
2431 1 Newx(decompressed, decomp_cap, char);
2432 z_stream zs;
2433 1 Zero(&zs, 1, z_stream);
2434 1 int zinit = inflateInit2(&zs, MAX_WBITS + 16);
2435 1 ▶ if (zinit != Z_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (zinit != Z_OK) { 0 1 1
2436 0 Safefree(decompressed);
2437 0 decompressed = NULL;
2438 0 break;
2439 }
2440 1 zs.next_in = (Bytef *)p;
2441 1 zs.avail_in = (uInt)compressed_len;
2442 1 zs.next_out = (Bytef *)decompressed;
2443 1 zs.avail_out = (uInt)decomp_cap;
2444 1 int zret = inflate(&zs, Z_FINISH);
2445 1 size_t dest_len = zs.total_out;
2446 1 inflateEnd(&zs);
2447 1 ▶ if (zret == Z_STREAM_END) {
Branch: 1 branch
BranchTrueFalseTotal
if (zret == Z_STREAM_END) { 1 0 1
2448 1 rec_data = decompressed;
2449 1 rec_end = decompressed + dest_len;
2450 1 zok = 1;
2451 1 decompressed_ok = 1;
2452 0 ▶ } else if (zret == Z_BUF_ERROR || zret == Z_OK) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (zret == Z_BUF_ERROR || zret == Z_OK) { 0 0 0
} else if (zret == Z_BUF_ERROR || zret == Z_OK) { 0 0 0
2453 0 Safefree(decompressed);
2454 0 decompressed = NULL;
2455 0 decomp_cap *= 2;
2456 } else {
2457 0 Safefree(decompressed);
2458 0 decompressed = NULL;
2459 0 break;
2460 }
2461 }
2462 }
2463 #endif
2464 #ifdef HAVE_LZ4
2465 if (compression_type == COMPRESS_LZ4) {
2466 /* LZ4 FRAME format (KIP-57); the context holds frame state,
2467 * so create a fresh one per attempt. */
2468 while (decomp_cap < 64 * 1024 * 1024) {
2469 LZ4F_decompressionContext_t dctx;
2470 if (LZ4F_isError(LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION)))
2471 break;
2472 Newx(decompressed, decomp_cap, char);
2473 size_t produced = 0, consumed = 0;
2474 int err = 0, done = 0;
2475 while (consumed < compressed_len) {
2476 size_t dst_size = decomp_cap - produced;
2477 size_t src_size = compressed_len - consumed;
2478 size_t r = LZ4F_decompress(dctx,
2479 decompressed + produced, &dst_size,
2480 p + consumed, &src_size, NULL);
2481 produced += dst_size;
2482 consumed += src_size;
2483 if (LZ4F_isError(r)) { err = 1; break; }
2484 if (r == 0) { done = 1; break; } /* frame complete */
2485 if (dst_size == 0 && src_size == 0)
2486 break; /* no progress: output cap hit */
2487 }
2488 LZ4F_freeDecompressionContext(dctx);
2489 if (done) {
2490 /* produced==0 is a validly-empty batch: the record
2491 * loop sees zero records. */
2492 rec_data = decompressed;
2493 rec_end = decompressed + produced;
2494 decompressed_ok = 1;
2495 break;
2496 }
2497 Safefree(decompressed);
2498 decompressed = NULL;
2499 if (err || produced < decomp_cap)
2500 break; /* malformed or truncated frame */
2501 decomp_cap *= 2; /* output cap hit: grow, retry */
2502 }
2503 }
2504 #endif
2505 #ifdef HAVE_ZSTD
2506 if (compression_type == COMPRESS_ZSTD) {
2507 unsigned long long expected =
2508 ZSTD_getFrameContentSize(p, compressed_len);
2509 size_t dlen;
2510 if (expected != ZSTD_CONTENTSIZE_ERROR
2511 && expected != ZSTD_CONTENTSIZE_UNKNOWN
2512 && expected <= 64ULL * 1024 * 1024) {
2513 Newx(decompressed, (size_t)expected, char);
2514 dlen = ZSTD_decompress(decompressed, (size_t)expected,
2515 p, compressed_len);
2516 if (!ZSTD_isError(dlen)) {
2517 rec_data = decompressed;
2518 rec_end = decompressed + dlen;
2519 decompressed_ok = 1;
2520 } else {
2521 Safefree(decompressed);
2522 decompressed = NULL;
2523 }
2524 }
2525 }
2526 #endif
2527 #ifdef HAVE_SNAPPY
2528 if (compression_type == COMPRESS_SNAPPY) {
2529 size_t dlen;
2530 if (snappy_uncompressed_length(p, compressed_len, &dlen)
2531 == SNAPPY_OK
2532 && dlen <= 64UL * 1024 * 1024) {
2533 Newx(decompressed, dlen, char);
2534 if (snappy_uncompress(p, compressed_len,
2535 decompressed, &dlen) == SNAPPY_OK) {
2536 rec_data = decompressed;
2537 rec_end = decompressed + dlen;
2538 decompressed_ok = 1;
2539 } else {
2540 Safefree(decompressed);
2541 decompressed = NULL;
2542 }
2543 }
2544 }
2545 #endif
2546  
2547 1 ▶ if (!decompressed_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!decompressed_ok) { 0 1 1
2548 /* unknown type, codec missing, or decompression failed: do
2549 * NOT parse the compressed bytes as records. */
2550 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
2551 0 return -1;
2552 }
2553 }
2554  
2555 18 const char *rp = rec_data;
2556 int32_t i;
2557 163 ▶ for (i = 0; i < record_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < record_count; i++) { 145 18 163
2558 int64_t rec_len;
2559 145 n = kf_read_varint(rp, rec_end, &rec_len);
2560 145 ▶ if (n < 0) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2561 145 rp += n;
2562 145 ▶ if (rec_end - rp < rec_len) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (rec_end - rp < rec_len) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (rec_end - rp < rec_len) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2563 145 const char *this_rec_end = rp + rec_len;
2564  
2565 145 ▶ if (this_rec_end - rp < 1) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (this_rec_end - rp < 1) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (this_rec_end - rp < 1) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2566 145 /* int8_t rec_attrs = (int8_t)*rp; */ rp += 1;
2567  
2568 int64_t ts_delta;
2569 145 n = kf_read_varint(rp, this_rec_end, &ts_delta);
2570 145 ▶ if (n < 0) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2571 145 rp += n;
2572  
2573 int64_t offset_delta;
2574 145 n = kf_read_varint(rp, this_rec_end, &offset_delta);
2575 145 ▶ if (n < 0) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2576 145 rp += n;
2577  
2578 /* key */
2579 int64_t key_len;
2580 145 n = kf_read_varint(rp, this_rec_end, &key_len);
2581 145 ▶ if (n < 0) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2582 145 rp += n;
2583 145 const char *key_data = NULL;
2584 145 ▶ if (key_len >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (key_len >= 0) { 144 1 145
2585 144 ▶ if (this_rec_end - rp < key_len) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (this_rec_end - rp < key_len) { if (decompressed) Safefree(decompressed); return -1; } 0 144 144
if (this_rec_end - rp < key_len) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2586 144 key_data = rp;
2587 144 rp += key_len;
2588 }
2589  
2590 /* value */
2591 int64_t val_len;
2592 145 n = kf_read_varint(rp, this_rec_end, &val_len);
2593 145 ▶ if (n < 0) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2594 145 rp += n;
2595 145 const char *val_data = NULL;
2596 145 ▶ if (val_len >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (val_len >= 0) { 144 1 145
2597 144 ▶ if (this_rec_end - rp < val_len) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (this_rec_end - rp < val_len) { if (decompressed) Safefree(decompressed); return -1; } 0 144 144
if (this_rec_end - rp < val_len) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2598 144 val_data = rp;
2599 144 rp += val_len;
2600 }
2601  
2602 /* headers */
2603 int64_t hdr_count;
2604 145 n = kf_read_varint(rp, this_rec_end, &hdr_count);
2605 145 ▶ if (n < 0) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2606 145 rp += n;
2607 145 HV *hdr_hv = NULL;
2608 145 ▶ if (hdr_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hdr_count > 0) { 1 144 145
2609 1 hdr_hv = newHV();
2610 int64_t h;
2611 3 ▶ for (h = 0; h < hdr_count; h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < hdr_count; h++) { 2 1 3
2612 int64_t hk_len;
2613 2 n = kf_read_varint(rp, this_rec_end, &hk_len);
2614 2 ▶ if (n < 0) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 2 2
if (n < 0) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2615 2 rp += n;
2616 2 const char *hk_data = rp;
2617 2 ▶ if (hk_len < 0 || this_rec_end - rp < hk_len) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; }
Branch: 3 branches
BranchTrueFalseTotal
if (hk_len < 0 || this_rec_end - rp < hk_len) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 2 0 2
if (hk_len < 0 || this_rec_end - rp < hk_len) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 2 2
if (hk_len < 0 || this_rec_end - rp < hk_len) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2618 2 rp += hk_len;
2619  
2620 int64_t hv_len;
2621 2 n = kf_read_varint(rp, this_rec_end, &hv_len);
2622 2 ▶ if (n < 0) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 2 2
if (n < 0) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2623 2 rp += n;
2624 2 const char *hv_data = rp;
2625 2 ▶ if (hv_len >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_len >= 0) { 2 0 2
2626 2 ▶ if (this_rec_end - rp < hv_len) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (this_rec_end - rp < hv_len) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 2 2
if (this_rec_end - rp < hv_len) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2627 2 rp += hv_len;
2628 }
2629  
2630 2 ▶ hv_store(hdr_hv, hk_data, (I32)hk_len,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(hdr_hv, hk_data, (I32)hk_len, 2 0 2
2631 hv_len >= 0 ? newSVpvn(hv_data, (STRLEN)hv_len) : newSV(0), 0);
2632 }
2633 }
2634  
2635 145 HV *rec_hv = newHV();
2636 145 hv_store(rec_hv, "offset", 6, newSViv(base_offset + offset_delta), 0);
2637 145 hv_store(rec_hv, "timestamp", 9, newSViv(first_timestamp + ts_delta), 0);
2638 145 ▶ hv_store(rec_hv, "key", 3,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(rec_hv, "key", 3, 144 1 145
2639 key_data ? newSVpvn(key_data, (STRLEN)key_len) : newSV(0), 0);
2640 145 ▶ hv_store(rec_hv, "value", 5,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(rec_hv, "value", 5, 144 1 145
2641 val_data ? newSVpvn(val_data, (STRLEN)val_len) : newSV(0), 0);
2642 145 ▶ if (hdr_hv)
Branch: 1 branch
BranchTrueFalseTotal
if (hdr_hv) 1 144 145
2643 1 hv_store(rec_hv, "headers", 7, newRV_noinc((SV*)hdr_hv), 0);
2644  
2645 145 av_push(records_av, newRV_noinc((SV*)rec_hv));
2646  
2647 145 rp = this_rec_end; /* skip any remaining bytes in the record */
2648 }
2649  
2650 18 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 1 17 18
2651 18 return record_count;
2652 }
2653  
2654 /* Fetch response parser (API 1, v4-v7 non-flexible) */
2655 13 static SV* conn_parse_fetch_response(pTHX_ ev_kafka_conn_t *self,
2656 int16_t version, const char *data, size_t len)
2657 {
2658 13 const char *p = data;
2659 13 const char *end = data + len;
2660 13 HV *result = newHV();
2661 13 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
2662 int n;
2663  
2664 (void)self;
2665  
2666 /* throttle_time_ms (v1+) */
2667 13 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 13 0 13
2668 13 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 13 13
2669 13 int32_t throttle = kf_read_i32(p); p += 4;
2670 13 hv_store(result, "throttle_time_ms", 16, newSViv(throttle), 0);
2671 }
2672  
2673 /* error_code (v7+) */
2674 13 ▶ if (version >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 7) { 11 2 13
2675 11 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 11 11
2676 11 p += 2;
2677 }
2678  
2679 /* session_id (v7+) */
2680 13 ▶ if (version >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 7) { 11 2 13
2681 11 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 11 11
2682 11 p += 4;
2683 }
2684  
2685 /* responses: ARRAY */
2686 13 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 13 13
2687 13 int32_t topic_count = kf_read_i32(p); p += 4;
2688 int32_t i;
2689  
2690 25 ▶ for (i = 0; i < topic_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < topic_count; i++) { 12 13 25
2691 12 HV *th = (HV*)sv_2mortal((SV*)newHV());
2692  
2693 const char *tname; int16_t tnlen;
2694 12 n = kf_read_string(p, end, &tname, &tnlen);
2695 12 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 12 12
2696 12 p += n;
2697 12 ▶ hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 12 0 12
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 12 0 12
2698  
2699 /* partitions: ARRAY */
2700 12 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 12 12
2701 12 int32_t part_count = kf_read_i32(p); p += 4;
2702 12 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
2703 int32_t j;
2704  
2705 24 ▶ for (j = 0; j < part_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < part_count; j++) { 12 12 24
2706 12 HV *ph = (HV*)sv_2mortal((SV*)newHV());
2707  
2708 12 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 12 12
2709 12 int32_t pid = kf_read_i32(p); p += 4;
2710 12 hv_store(ph, "partition", 9, newSViv(pid), 0);
2711  
2712 12 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 12 12
2713 12 int16_t err = kf_read_i16(p); p += 2;
2714 12 hv_store(ph, "error_code", 10, newSViv(err), 0);
2715  
2716 12 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 12 12
2717 12 int64_t hw = kf_read_i64(p); p += 8;
2718 12 hv_store(ph, "high_watermark", 14, newSViv(hw), 0);
2719  
2720 /* last_stable_offset (v4+) */
2721 12 ▶ if (version >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 4) { 12 0 12
2722 12 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 12 12
2723 12 int64_t lso = kf_read_i64(p); p += 8;
2724 12 hv_store(ph, "last_stable_offset", 18, newSViv(lso), 0);
2725 }
2726  
2727 /* log_start_offset (v5+) */
2728 12 ▶ if (version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 5) { 11 1 12
2729 11 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 11 11
2730 11 p += 8;
2731 }
2732  
2733 /* aborted_transactions (v4+) */
2734 12 ▶ if (version >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 4) { 12 0 12
2735 12 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 12 12
2736 12 int32_t at_count = kf_read_i32(p); p += 4;
2737 int32_t at;
2738 12 ▶ for (at = 0; at < at_count; at++) {
Branch: 1 branch
BranchTrueFalseTotal
for (at = 0; at < at_count; at++) { 0 12 12
2739 0 ▶ if (end - p < 16) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 16) goto done; 0 0 0
2740 0 p += 16; /* producer_id(i64) + first_offset(i64) */
2741 }
2742 }
2743  
2744 /* record_set: BYTES (records data) */
2745 12 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 12 12
2746 12 int32_t records_size = kf_read_i32(p); p += 4;
2747  
2748 12 AV *records_av = (AV*)sv_2mortal((SV*)newAV());
2749 12 ▶ if (records_size > 0 && end - p >= records_size) {
Branch: 2 branches
BranchTrueFalseTotal
if (records_size > 0 && end - p >= records_size) { 6 6 12
if (records_size > 0 && end - p >= records_size) { 6 0 6
2750 6 const char *rp = p;
2751 6 const char *rend = p + records_size;
2752  
2753 /* May contain multiple RecordBatches */
2754 11 ▶ while (rp < rend && rend - rp >= 12) {
Branch: 2 branches
BranchTrueFalseTotal
while (rp < rend && rend - rp >= 12) { 6 5 11
while (rp < rend && rend - rp >= 12) { 6 0 6
2755 int64_t bo;
2756 6 int32_t bl = kf_read_i32(rp + 8);
2757 /* 12 + bl in 64-bit: a bl near INT32_MAX would wrap */
2758 6 ▶ if (bl < 0 || (int64_t)bl > (int64_t)(rend - rp) - 12) break;
Branch: 2 branches
BranchTrueFalseTotal
if (bl < 0 || (int64_t)bl > (int64_t)(rend - rp) - 12) break; 6 0 6
if (bl < 0 || (int64_t)bl > (int64_t)(rend - rp) - 12) break; 5 1 6
2759 5 kf_decode_record_batch(aTHX_ rp, (size_t)12 + (size_t)bl, records_av, &bo);
2760 5 rp += (ptrdiff_t)12 + (ptrdiff_t)bl;
2761 }
2762  
2763 6 p += records_size;
2764 6 ▶ } else if (records_size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (records_size > 0) { 0 6 6
2765 0 p = end; /* truncated */
2766 }
2767  
2768 12 hv_store(ph, "records", 7, newRV_inc((SV*)records_av), 0);
2769 12 av_push(parts_av, newRV_inc((SV*)ph));
2770 }
2771  
2772 12 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
2773 12 av_push(topics_av, newRV_inc((SV*)th));
2774 }
2775  
2776 13 done:
2777 13 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
2778 13 return sv_2mortal(newRV_noinc((SV*)result));
2779 }
2780  
2781 /* ListOffsets response parser (API 2, v1+) */
2782 4 static SV* conn_parse_list_offsets_response(pTHX_ ev_kafka_conn_t *self,
2783 int16_t version, const char *data, size_t len)
2784 {
2785 4 const char *p = data;
2786 4 const char *end = data + len;
2787 4 HV *result = newHV();
2788 4 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
2789 int n;
2790  
2791 (void)self;
2792  
2793 /* throttle_time_ms (v2+) */
2794 4 ▶ if (version >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 2) { 0 4 4
2795 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2796 0 p += 4;
2797 }
2798  
2799 /* topics: ARRAY */
2800 4 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 4 4
2801 4 int32_t topic_count = kf_read_i32(p); p += 4;
2802 int32_t i;
2803  
2804 8 ▶ for (i = 0; i < topic_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < topic_count; i++) { 4 4 8
2805 4 HV *th = (HV*)sv_2mortal((SV*)newHV());
2806 const char *tname; int16_t tnlen;
2807 4 n = kf_read_string(p, end, &tname, &tnlen);
2808 4 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 4 4
2809 4 p += n;
2810 4 ▶ hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 4 0 4
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 4 0 4
2811  
2812 4 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 4 4
2813 4 int32_t part_count = kf_read_i32(p); p += 4;
2814 4 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
2815 int32_t j;
2816  
2817 8 ▶ for (j = 0; j < part_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < part_count; j++) { 4 4 8
2818 4 HV *ph = (HV*)sv_2mortal((SV*)newHV());
2819 4 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 4 4
2820 4 int32_t pid = kf_read_i32(p); p += 4;
2821 4 hv_store(ph, "partition", 9, newSViv(pid), 0);
2822  
2823 4 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 4 4
2824 4 int16_t err = kf_read_i16(p); p += 2;
2825 4 hv_store(ph, "error_code", 10, newSViv(err), 0);
2826  
2827 4 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 4 0 4
2828 4 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 4 4
2829 4 int64_t ts = kf_read_i64(p); p += 8;
2830 4 hv_store(ph, "timestamp", 9, newSViv(ts), 0);
2831 }
2832  
2833 4 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 4 4
2834 4 int64_t offset = kf_read_i64(p); p += 8;
2835 4 hv_store(ph, "offset", 6, newSViv(offset), 0);
2836  
2837 /* leader_epoch (v4+) */
2838 4 ▶ if (version >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 4) { 0 4 4
2839 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2840 0 p += 4;
2841 }
2842  
2843 4 av_push(parts_av, newRV_inc((SV*)ph));
2844 }
2845 4 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
2846 4 av_push(topics_av, newRV_inc((SV*)th));
2847 }
2848  
2849 4 done:
2850 4 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
2851 4 return sv_2mortal(newRV_noinc((SV*)result));
2852 }
2853  
2854 /* FindCoordinator response parser (API 10, v0-v3) */
2855 10 static SV* conn_parse_find_coordinator_response(pTHX_ ev_kafka_conn_t *self,
2856 int16_t version, const char *data, size_t len)
2857 {
2858 10 const char *p = data;
2859 10 const char *end = data + len;
2860 10 HV *result = newHV();
2861 int n;
2862 (void)self;
2863  
2864 /* throttle_time_ms (v1+) */
2865 10 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 9 1 10
2866 9 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 9 9
2867 9 p += 4;
2868 }
2869  
2870 10 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 10 10
2871 10 int16_t err = kf_read_i16(p); p += 2;
2872 10 hv_store(result, "error_code", 10, newSViv(err), 0);
2873  
2874 /* error_message (v1+) */
2875 10 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 9 1 10
2876 const char *emsg; int16_t elen;
2877 9 n = kf_read_string(p, end, &emsg, &elen);
2878 9 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 9 9
2879 9 p += n;
2880 9 ▶ if (emsg && elen > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (emsg && elen > 0) 0 9 9
if (emsg && elen > 0) 0 0 0
2881 0 hv_store(result, "error_message", 13, newSVpvn(emsg, elen), 0);
2882 }
2883  
2884 10 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 10 10
2885 10 int32_t nid = kf_read_i32(p); p += 4;
2886 10 hv_store(result, "node_id", 7, newSViv(nid), 0);
2887  
2888 const char *host; int16_t hlen;
2889 10 n = kf_read_string(p, end, &host, &hlen);
2890 10 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 10 10
2891 10 p += n;
2892 10 ▶ hv_store(result, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(result, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0); 10 0 10
hv_store(result, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0); 10 0 10
2893  
2894 10 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 10 10
2895 10 int32_t port = kf_read_i32(p); p += 4;
2896 10 hv_store(result, "port", 4, newSViv(port), 0);
2897  
2898 10 done:
2899 10 return sv_2mortal(newRV_noinc((SV*)result));
2900 }
2901  
2902 /* JoinGroup response parser (API 11, v0-v5) */
2903 5 static SV* conn_parse_join_group_response(pTHX_ ev_kafka_conn_t *self,
2904 int16_t version, const char *data, size_t len)
2905 {
2906 5 const char *p = data;
2907 5 const char *end = data + len;
2908 5 HV *result = newHV();
2909 int n;
2910 (void)self;
2911  
2912 /* throttle_time_ms (v2+) */
2913 5 ▶ if (version >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 2) { 0 5 5
2914 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2915 0 p += 4;
2916 }
2917  
2918 5 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 5 5
2919 5 int16_t err = kf_read_i16(p); p += 2;
2920 5 hv_store(result, "error_code", 10, newSViv(err), 0);
2921  
2922 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
2923 5 int32_t gen = kf_read_i32(p); p += 4;
2924 5 hv_store(result, "generation_id", 13, newSViv(gen), 0);
2925  
2926 /* protocol_type (v7+) — skip for now, we use v5 max */
2927  
2928 const char *proto; int16_t plen;
2929 5 n = kf_read_string(p, end, &proto, &plen);
2930 5 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 5 5
2931 5 p += n;
2932 5 ▶ if (proto)
Branch: 1 branch
BranchTrueFalseTotal
if (proto) 5 0 5
2933 5 hv_store(result, "protocol_name", 13, newSVpvn(proto, plen), 0);
2934  
2935 const char *leader; int16_t llen;
2936 5 n = kf_read_string(p, end, &leader, &llen);
2937 5 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 5 5
2938 5 p += n;
2939 5 ▶ if (leader)
Branch: 1 branch
BranchTrueFalseTotal
if (leader) 5 0 5
2940 5 hv_store(result, "leader", 6, newSVpvn(leader, llen), 0);
2941  
2942 /* skip_assignment (v9+) — not applicable */
2943  
2944 const char *member_id; int16_t mlen;
2945 5 n = kf_read_string(p, end, &member_id, &mlen);
2946 5 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 5 5
2947 5 p += n;
2948 5 ▶ if (member_id)
Branch: 1 branch
BranchTrueFalseTotal
if (member_id) 5 0 5
2949 5 hv_store(result, "member_id", 9, newSVpvn(member_id, mlen), 0);
2950  
2951 /* members array */
2952 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
2953 5 int32_t mcount = kf_read_i32(p); p += 4;
2954 5 AV *members_av = (AV*)sv_2mortal((SV*)newAV());
2955 int32_t i;
2956 10 ▶ for (i = 0; i < mcount; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < mcount; i++) { 5 5 10
2957 5 HV *mh = (HV*)sv_2mortal((SV*)newHV());
2958  
2959 const char *mid; int16_t midlen;
2960 5 n = kf_read_string(p, end, &mid, &midlen);
2961 5 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 5 5
2962 5 p += n;
2963 5 ▶ if (mid)
Branch: 1 branch
BranchTrueFalseTotal
if (mid) 5 0 5
2964 5 hv_store(mh, "member_id", 9, newSVpvn(mid, midlen), 0);
2965  
2966 /* group_instance_id (v5+) */
2967 5 ▶ if (version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 5) { 0 5 5
2968 const char *gi; int16_t gilen;
2969 0 n = kf_read_string(p, end, &gi, &gilen);
2970 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2971 0 p += n;
2972 }
2973  
2974 /* metadata: BYTES */
2975 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
2976 5 int32_t mdlen = kf_read_i32(p); p += 4;
2977 5 ▶ if (mdlen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mdlen > 0) { 0 5 5
2978 0 ▶ if (end - p < mdlen) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < mdlen) goto done; 0 0 0
2979 0 hv_store(mh, "metadata", 8, newSVpvn(p, mdlen), 0);
2980 0 p += mdlen;
2981 }
2982  
2983 5 av_push(members_av, newRV_inc((SV*)mh));
2984 }
2985 5 hv_store(result, "members", 7, newRV_inc((SV*)members_av), 0);
2986  
2987 5 done:
2988 5 return sv_2mortal(newRV_noinc((SV*)result));
2989 }
2990  
2991 /* SyncGroup response parser (API 14, v0-v3) */
2992 5 static SV* conn_parse_sync_group_response(pTHX_ ev_kafka_conn_t *self,
2993 int16_t version, const char *data, size_t len)
2994 {
2995 5 const char *p = data;
2996 5 const char *end = data + len;
2997 5 HV *result = newHV();
2998 (void)self;
2999  
3000 /* throttle_time_ms (v1+) */
3001 5 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 0 5 5
3002 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
3003 0 p += 4;
3004 }
3005  
3006 5 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 5 5
3007 5 int16_t err = kf_read_i16(p); p += 2;
3008 5 hv_store(result, "error_code", 10, newSViv(err), 0);
3009  
3010 /* assignment: BYTES */
3011 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
3012 5 int32_t alen = kf_read_i32(p); p += 4;
3013 5 ▶ if (alen > 0 && end - p >= alen) {
Branch: 2 branches
BranchTrueFalseTotal
if (alen > 0 && end - p >= alen) { 0 5 5
if (alen > 0 && end - p >= alen) { 0 5 5
3014 5 hv_store(result, "assignment", 10, newSVpvn(p, alen), 0);
3015 5 p += alen;
3016 }
3017  
3018 0 done:
3019 5 return sv_2mortal(newRV_noinc((SV*)result));
3020 }
3021  
3022 /* Heartbeat response parser (API 12) */
3023 2 static SV* conn_parse_heartbeat_response(pTHX_ ev_kafka_conn_t *self,
3024 int16_t version, const char *data, size_t len)
3025 {
3026 2 const char *p = data;
3027 2 const char *end = data + len;
3028 2 HV *result = newHV();
3029 (void)self;
3030  
3031 2 ▶ if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 2 2
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 0 0
3032 2 ▶ if (end - p >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (end - p >= 2) { 2 0 2
3033 2 int16_t err = kf_read_i16(p); p += 2;
3034 2 hv_store(result, "error_code", 10, newSViv(err), 0);
3035 }
3036  
3037 2 return sv_2mortal(newRV_noinc((SV*)result));
3038 }
3039  
3040 /* OffsetCommit response parser (API 8, v0-v7) */
3041 2 static SV* conn_parse_offset_commit_response(pTHX_ ev_kafka_conn_t *self,
3042 int16_t version, const char *data, size_t len)
3043 {
3044 2 const char *p = data;
3045 2 const char *end = data + len;
3046 2 HV *result = newHV();
3047 2 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
3048 int n;
3049 (void)self;
3050  
3051 2 ▶ if (version >= 3 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 3 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 2 2
if (version >= 3 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 0 0
3052  
3053 2 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 2 2
3054 2 int32_t tc = kf_read_i32(p); p += 4;
3055 int32_t i;
3056 4 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 2 2 4
3057 2 HV *th = (HV*)sv_2mortal((SV*)newHV());
3058 const char *tname; int16_t tnlen;
3059 2 n = kf_read_string(p, end, &tname, &tnlen);
3060 2 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 2 2
3061 2 p += n;
3062 2 ▶ hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 2 0 2
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 2 0 2
3063  
3064 2 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 2 2
3065 2 int32_t pc = kf_read_i32(p); p += 4;
3066 2 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
3067 int32_t j;
3068 4 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 2 2 4
3069 2 HV *ph = (HV*)sv_2mortal((SV*)newHV());
3070 2 ▶ if (end - p < 6) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 6) goto done; 0 2 2
3071 2 int32_t pid = kf_read_i32(p); p += 4;
3072 2 int16_t err = kf_read_i16(p); p += 2;
3073 2 hv_store(ph, "partition", 9, newSViv(pid), 0);
3074 2 hv_store(ph, "error_code", 10, newSViv(err), 0);
3075 2 av_push(parts_av, newRV_inc((SV*)ph));
3076 }
3077 2 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
3078 2 av_push(topics_av, newRV_inc((SV*)th));
3079 }
3080  
3081 2 done:
3082 2 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
3083 2 return sv_2mortal(newRV_noinc((SV*)result));
3084 }
3085  
3086 /* OffsetFetch response parser (API 9, v0-v5) */
3087 5 static SV* conn_parse_offset_fetch_response(pTHX_ ev_kafka_conn_t *self,
3088 int16_t version, const char *data, size_t len)
3089 {
3090 5 const char *p = data;
3091 5 const char *end = data + len;
3092 5 HV *result = newHV();
3093 5 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
3094 int n;
3095 (void)self;
3096  
3097 5 ▶ if (version >= 3 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 3 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 5 5
if (version >= 3 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 0 0
3098  
3099 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
3100 5 int32_t tc = kf_read_i32(p); p += 4;
3101 int32_t i;
3102 10 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 5 5 10
3103 5 HV *th = (HV*)sv_2mortal((SV*)newHV());
3104 const char *tname; int16_t tnlen;
3105 5 n = kf_read_string(p, end, &tname, &tnlen);
3106 5 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 5 5
3107 5 p += n;
3108 5 ▶ hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 5 0 5
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 5 0 5
3109  
3110 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
3111 5 int32_t pc = kf_read_i32(p); p += 4;
3112 5 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
3113 int32_t j;
3114 10 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 5 5 10
3115 5 HV *ph = (HV*)sv_2mortal((SV*)newHV());
3116 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
3117 5 int32_t pid = kf_read_i32(p); p += 4;
3118 5 hv_store(ph, "partition", 9, newSViv(pid), 0);
3119  
3120 5 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 5 5
3121 5 int64_t offset = kf_read_i64(p); p += 8;
3122 5 hv_store(ph, "offset", 6, newSViv(offset), 0);
3123  
3124 /* leader_epoch (v5+) */
3125 5 ▶ if (version >= 5 && end - p >= 4) p += 4;
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 5 && end - p >= 4) p += 4; 0 5 5
if (version >= 5 && end - p >= 4) p += 4; 0 0 0
3126  
3127 const char *meta_str; int16_t meta_len;
3128 5 n = kf_read_string(p, end, &meta_str, &meta_len);
3129 5 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 5 5
3130 5 p += n;
3131  
3132 5 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 5 5
3133 5 int16_t err = kf_read_i16(p); p += 2;
3134 5 hv_store(ph, "error_code", 10, newSViv(err), 0);
3135  
3136 5 av_push(parts_av, newRV_inc((SV*)ph));
3137 }
3138 5 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
3139 5 av_push(topics_av, newRV_inc((SV*)th));
3140 }
3141  
3142 /* error_code (v2+) */
3143 5 ▶ if (version >= 2 && end - p >= 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 2 && end - p >= 2) { 5 0 5
if (version >= 2 && end - p >= 2) { 0 0 0
3144 0 int16_t err = kf_read_i16(p); p += 2;
3145 0 hv_store(result, "error_code", 10, newSViv(err), 0);
3146 }
3147  
3148 5 done:
3149 5 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
3150 5 return sv_2mortal(newRV_noinc((SV*)result));
3151 }
3152  
3153 /* LeaveGroup response parser (API 13, v0-v3) */
3154 2 static SV* conn_parse_leave_group_response(pTHX_ ev_kafka_conn_t *self,
3155 int16_t version, const char *data, size_t len)
3156 {
3157 2 const char *p = data;
3158 2 const char *end = data + len;
3159 2 HV *result = newHV();
3160 (void)self;
3161  
3162 2 ▶ if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 2 2
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 0 0
3163 2 ▶ if (end - p >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (end - p >= 2) { 2 0 2
3164 2 int16_t err = kf_read_i16(p); p += 2;
3165 2 hv_store(result, "error_code", 10, newSViv(err), 0);
3166 }
3167  
3168 2 return sv_2mortal(newRV_noinc((SV*)result));
3169 }
3170  
3171 /* CreateTopics response parser (API 19, v0-v4) */
3172 1 static SV* conn_parse_create_topics_response(pTHX_ ev_kafka_conn_t *self,
3173 int16_t version, const char *data, size_t len)
3174 {
3175 1 const char *p = data;
3176 1 const char *end = data + len;
3177 1 HV *result = newHV();
3178 1 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
3179 int n;
3180 (void)self;
3181  
3182 1 ▶ if (version >= 2 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 2 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 1 1
if (version >= 2 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 0 0
3183  
3184 1 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 1 1
3185 1 int32_t tc = kf_read_i32(p); p += 4;
3186 int32_t i;
3187 2 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 1 1 2
3188 1 HV *th = (HV*)sv_2mortal((SV*)newHV());
3189 const char *tname; int16_t tnlen;
3190 1 n = kf_read_string(p, end, &tname, &tnlen);
3191 1 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 1 1
3192 1 p += n;
3193 1 ▶ hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 1 0 1
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 1 0 1
3194  
3195 1 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 1 1
3196 1 int16_t err = kf_read_i16(p); p += 2;
3197 1 hv_store(th, "error_code", 10, newSViv(err), 0);
3198  
3199 /* error_message (v1+) */
3200 1 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 0 1 1
3201 const char *emsg; int16_t elen;
3202 0 n = kf_read_string(p, end, &emsg, &elen);
3203 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
3204 0 p += n;
3205 0 ▶ if (emsg && elen > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (emsg && elen > 0) 0 0 0
if (emsg && elen > 0) 0 0 0
3206 0 hv_store(th, "error_message", 13, newSVpvn(emsg, elen), 0);
3207 }
3208  
3209 1 av_push(topics_av, newRV_inc((SV*)th));
3210 }
3211  
3212 1 done:
3213 1 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
3214 1 return sv_2mortal(newRV_noinc((SV*)result));
3215 }
3216  
3217 /* DeleteTopics response parser (API 20, v0-v3) */
3218 1 static SV* conn_parse_delete_topics_response(pTHX_ ev_kafka_conn_t *self,
3219 int16_t version, const char *data, size_t len)
3220 {
3221 1 const char *p = data;
3222 1 const char *end = data + len;
3223 1 HV *result = newHV();
3224 1 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
3225 int n;
3226 (void)self;
3227  
3228 1 ▶ if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 1 1
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 0 0
3229  
3230 1 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 1 1
3231 1 int32_t tc = kf_read_i32(p); p += 4;
3232 int32_t i;
3233 2 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 1 1 2
3234 1 HV *th = (HV*)sv_2mortal((SV*)newHV());
3235 const char *tname; int16_t tnlen;
3236 1 n = kf_read_string(p, end, &tname, &tnlen);
3237 1 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 1 1
3238 1 p += n;
3239 1 ▶ hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 1 0 1
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 1 0 1
3240  
3241 1 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 1 1
3242 1 int16_t err = kf_read_i16(p); p += 2;
3243 1 hv_store(th, "error_code", 10, newSViv(err), 0);
3244  
3245 1 av_push(topics_av, newRV_inc((SV*)th));
3246 }
3247  
3248 1 done:
3249 1 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
3250 1 return sv_2mortal(newRV_noinc((SV*)result));
3251 }
3252  
3253 /* InitProducerId response parser (API 22, v0-v2) */
3254 11 static SV* conn_parse_init_producer_id_response(pTHX_ ev_kafka_conn_t *self,
3255 int16_t version, const char *data, size_t len)
3256 {
3257 11 const char *p = data;
3258 11 const char *end = data + len;
3259 11 HV *result = newHV();
3260 (void)self;
3261  
3262 11 ▶ if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 11 0 11
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 11 0 11
3263  
3264 11 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 11 11
3265 11 int16_t err = kf_read_i16(p); p += 2;
3266 11 hv_store(result, "error_code", 10, newSViv(err), 0);
3267  
3268 11 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 11 11
3269 11 int64_t producer_id = kf_read_i64(p); p += 8;
3270 11 hv_store(result, "producer_id", 11, newSViv(producer_id), 0);
3271  
3272 11 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 11 11
3273 11 int16_t producer_epoch = kf_read_i16(p); p += 2;
3274 11 hv_store(result, "producer_epoch", 14, newSViv(producer_epoch), 0);
3275  
3276 11 done:
3277 11 return sv_2mortal(newRV_noinc((SV*)result));
3278 }
3279  
3280 /* AddPartitionsToTxn response parser (API 24, v0-v1) */
3281 5 static SV* conn_parse_add_partitions_to_txn_response(pTHX_ ev_kafka_conn_t *self,
3282 int16_t version, const char *data, size_t len)
3283 {
3284 5 const char *p = data;
3285 5 const char *end = data + len;
3286 5 HV *result = newHV();
3287 5 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
3288 int n;
3289 (void)self; (void)version;
3290  
3291 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
3292 5 p += 4; /* throttle_time_ms */
3293  
3294 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 1 4 5
3295 4 int32_t tc = kf_read_i32(p); p += 4;
3296 int32_t i;
3297 8 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 4 4 8
3298 4 HV *th = (HV*)sv_2mortal((SV*)newHV());
3299 const char *tname; int16_t tnlen;
3300 4 n = kf_read_string(p, end, &tname, &tnlen);
3301 4 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 4 4
3302 4 p += n;
3303 4 ▶ hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 4 0 4
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 4 0 4
3304  
3305 4 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 4 4
3306 4 int32_t pc = kf_read_i32(p); p += 4;
3307 4 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
3308 int32_t j;
3309 8 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 4 4 8
3310 4 HV *ph = (HV*)sv_2mortal((SV*)newHV());
3311 4 ▶ if (end - p < 6) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 6) goto done; 0 4 4
3312 4 int32_t pid = kf_read_i32(p); p += 4;
3313 4 int16_t err = kf_read_i16(p); p += 2;
3314 4 hv_store(ph, "partition", 9, newSViv(pid), 0);
3315 4 hv_store(ph, "error_code", 10, newSViv(err), 0);
3316 4 av_push(parts_av, newRV_inc((SV*)ph));
3317 }
3318 4 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
3319 4 av_push(topics_av, newRV_inc((SV*)th));
3320 }
3321  
3322 4 done:
3323 5 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
3324 5 return sv_2mortal(newRV_noinc((SV*)result));
3325 }
3326  
3327 /* EndTxn response parser (API 26, v0-v1) */
3328 3 static SV* conn_parse_end_txn_response(pTHX_ ev_kafka_conn_t *self,
3329 int16_t version, const char *data, size_t len)
3330 {
3331 3 const char *p = data;
3332 3 const char *end = data + len;
3333 3 HV *result = newHV();
3334 (void)self; (void)version;
3335  
3336 3 ▶ if (end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 1 branch
BranchTrueFalseTotal
if (end - p >= 4) p += 4; /* throttle_time_ms */ 3 0 3
3337 3 ▶ if (end - p >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (end - p >= 2) { 3 0 3
3338 3 int16_t err = kf_read_i16(p); p += 2;
3339 3 hv_store(result, "error_code", 10, newSViv(err), 0);
3340 }
3341  
3342 3 return sv_2mortal(newRV_noinc((SV*)result));
3343 }
3344  
3345 /* TxnOffsetCommit response parser (API 28, v0-v2) */
3346 3 static SV* conn_parse_txn_offset_commit_response(pTHX_ ev_kafka_conn_t *self,
3347 int16_t version, const char *data, size_t len)
3348 {
3349 3 const char *p = data;
3350 3 const char *end = data + len;
3351 3 HV *result = newHV();
3352 3 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
3353 int n;
3354 (void)self;
3355  
3356 3 ▶ if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 3 3
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 0 0
3357  
3358 3 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 3 3
3359 3 int32_t tc = kf_read_i32(p); p += 4;
3360 int32_t i;
3361 4 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 1 3 4
3362 1 HV *th = (HV*)sv_2mortal((SV*)newHV());
3363 const char *tname; int16_t tnlen;
3364 1 n = kf_read_string(p, end, &tname, &tnlen);
3365 1 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 1 1
3366 1 p += n;
3367 1 ▶ hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 1 0 1
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 1 0 1
3368  
3369 1 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 1 1
3370 1 int32_t pc = kf_read_i32(p); p += 4;
3371 1 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
3372 int32_t j;
3373 2 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 1 1 2
3374 1 HV *ph = (HV*)sv_2mortal((SV*)newHV());
3375 1 ▶ if (end - p < 6) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 6) goto done; 0 1 1
3376 1 int32_t pid = kf_read_i32(p); p += 4;
3377 1 int16_t err = kf_read_i16(p); p += 2;
3378 1 hv_store(ph, "partition", 9, newSViv(pid), 0);
3379 1 hv_store(ph, "error_code", 10, newSViv(err), 0);
3380 1 av_push(parts_av, newRV_inc((SV*)ph));
3381 }
3382 1 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
3383 1 av_push(topics_av, newRV_inc((SV*)th));
3384 }
3385  
3386 3 done:
3387 3 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
3388 3 return sv_2mortal(newRV_noinc((SV*)result));
3389 }
3390  
3391 /* ================================================================
3392 * Response processing loop
3393 * ================================================================ */
3394  
3395 177 static void conn_process_responses(pTHX_ ev_kafka_conn_t *self) {
3396 526 ▶ while (self->rbuf_len >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->rbuf_len >= 4) { 178 171 349
3397 178 int32_t msg_size = kf_read_i32(self->rbuf);
3398 178 ▶ if (msg_size < 0 || msg_size > 256 * 1024 * 1024) {
Branch: 2 branches
BranchTrueFalseTotal
if (msg_size < 0 || msg_size > 256 * 1024 * 1024) { 178 0 178
if (msg_size < 0 || msg_size > 256 * 1024 * 1024) { 1 177 178
3399 1 conn_emit_error(aTHX_ self, "invalid response size");
3400 1 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 1 1
3401 1 conn_handle_disconnect(aTHX_ self, "invalid response size");
3402 1 return;
3403 }
3404  
3405 177 ▶ if (self->rbuf_len < (size_t)(4 + msg_size))
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len < (size_t)(4 + msg_size)) 0 177 177
3406 0 break; /* incomplete response */
3407  
3408 177 const char *msg = self->rbuf + 4;
3409  
3410 /* correlation_id is first 4 bytes of message */
3411 177 ▶ if (msg_size < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (msg_size < 4) { 0 177 177
3412 0 conn_emit_error(aTHX_ self, "response too short");
3413 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3414 0 conn_handle_disconnect(aTHX_ self, "response too short");
3415 0 return;
3416 }
3417  
3418 177 int32_t corr_id = kf_read_i32(msg);
3419 177 const char *payload = msg + 4;
3420 177 size_t payload_len = (size_t)msg_size - 4;
3421  
3422 /* Find matching callback (should be head of queue — Kafka guarantees ordering) */
3423 177 ev_kafka_conn_cb_t *cbt = NULL;
3424 177 ▶ if (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->cb_queue)) { 177 0 177
3425 177 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
3426 177 cbt = ngx_queue_data(q, ev_kafka_conn_cb_t, queue);
3427 177 ▶ if (cbt->correlation_id != corr_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->correlation_id != corr_id) { 1 176 177
3428 /* Out of order — shouldn't happen with Kafka, but handle gracefully */
3429 char errbuf[128];
3430 1 snprintf(errbuf, sizeof(errbuf),
3431 "correlation ID mismatch: expected %d, got %d",
3432 cbt->correlation_id, corr_id);
3433 1 conn_emit_error(aTHX_ self, errbuf);
3434 1 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 1 1
3435 1 conn_handle_disconnect(aTHX_ self, "correlation ID mismatch");
3436 1 return;
3437 }
3438 176 ngx_queue_remove(q);
3439 176 self->pending_count--;
3440 }
3441  
3442 /* Dispatch BEFORE compacting — payload points into rbuf */
3443 176 size_t consumed = 4 + (size_t)msg_size;
3444 176 ▶ if (cbt) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt) { 176 0 176
3445 176 conn_dispatch_response(aTHX_ self, cbt, payload, payload_len);
3446 176 ▶ if (cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->cb) SvREFCNT_dec(cbt->cb); 114 62 176
3447 176 Safefree(cbt);
3448 176 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 176 176
3449 }
3450  
3451 /* Compact rbuf after dispatch — unless dispatch tore down the
3452 * conn (conn_cleanup zeroed rbuf_len; subtracting would underflow). */
3453 176 ▶ if (self->rbuf_len < consumed)
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len < consumed) 4 172 176
3454 4 return;
3455 172 self->rbuf_len -= consumed;
3456 172 ▶ if (self->rbuf_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len > 0) 171 1 172
3457 1 memmove(self->rbuf, self->rbuf + consumed, self->rbuf_len);
3458 }
3459 }
3460  
3461 /* ================================================================
3462 * I/O callback
3463 * ================================================================ */
3464  
3465 435 static void conn_io_cb(EV_P_ ev_io *w, int revents) {
3466 435 ev_kafka_conn_t *self = (ev_kafka_conn_t *)w->data;
3467 dTHX;
3468 (void)loop;
3469 435 ▶ if (!self || self->magic != KF_MAGIC_ALIVE) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!self || self->magic != KF_MAGIC_ALIVE) return; 435 0 435
if (!self || self->magic != KF_MAGIC_ALIVE) return; 0 435 435
3470 435 self->callback_depth++;
3471 {
3472 /* Per-event tmps frame: without it, mortals created below would
3473 * pile up in EV::run's frame. FREETMPS runs while callback_depth
3474 * >= 1, so a triggered destructor still defers conn_do_free. */
3475 435 ENTER;
3476 435 SAVETMPS;
3477 435 conn_io_cb_inner(loop, w, revents);
3478 435 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 121 314 435
3479 435 LEAVE;
3480 }
3481 435 ▶ if (--self->callback_depth == 0 && self->free_pending)
Branch: 2 branches
BranchTrueFalseTotal
if (--self->callback_depth == 0 && self->free_pending) 435 0 435
if (--self->callback_depth == 0 && self->free_pending) 1 434 435
3482 1 conn_do_free(aTHX_ self);
3483 }
3484  
3485 435 static void conn_io_cb_inner(EV_P_ ev_io *w, int revents) {
3486 435 ev_kafka_conn_t *self = (ev_kafka_conn_t *)w->data;
3487 dTHX;
3488 (void)loop;
3489  
3490 435 ▶ if (!self || self->magic != KF_MAGIC_ALIVE) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!self || self->magic != KF_MAGIC_ALIVE) return; 435 0 435
if (!self || self->magic != KF_MAGIC_ALIVE) return; 0 435 435
3491  
3492 /* TCP connect in progress */
3493 435 ▶ if (self->state == CONN_CONNECTING) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->state == CONN_CONNECTING) { 68 367 435
3494 68 int err = 0;
3495 68 socklen_t errlen = sizeof(err);
3496  
3497 68 conn_stop_writing(self);
3498  
3499 68 ▶ if (getsockopt(self->fd, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (getsockopt(self->fd, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0) 0 68 68
3500 0 err = errno;
3501 68 ▶ if (err != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (err != 0) { 9 59 68
3502 char errbuf[256];
3503 9 snprintf(errbuf, sizeof(errbuf), "connect: %s", strerror(err));
3504 9 conn_emit_error(aTHX_ self, errbuf);
3505 9 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 1 8 9
3506 8 conn_handle_disconnect(aTHX_ self, errbuf);
3507 8 return;
3508 }
3509  
3510 /* TCP connected — keep the timer armed for the handshake phases. */
3511 59 conn_arm_timer(self);
3512 59 conn_on_connect_done(aTHX_ self);
3513 59 return;
3514 }
3515  
3516 #ifdef HAVE_OPENSSL
3517 /* TLS handshake in progress */
3518 367 ▶ if (self->state == CONN_TLS_HANDSHAKE) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->state == CONN_TLS_HANDSHAKE) { 0 367 367
3519 0 ERR_clear_error();
3520 0 int ret = SSL_connect(self->ssl);
3521 0 ▶ if (ret == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 1) { 0 0 0
3522 0 conn_stop_reading(self);
3523 0 conn_stop_writing(self);
3524  
3525 /* TLS done — proceed to ApiVersions (or SASL if needed) */
3526 0 conn_arm_timer(self);
3527 0 conn_send_api_versions(aTHX_ self);
3528 0 conn_start_reading(self);
3529 0 return;
3530 }
3531 0 int err = SSL_get_error(self->ssl, ret);
3532 0 ▶ if (err == SSL_ERROR_WANT_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == SSL_ERROR_WANT_READ) { 0 0 0
3533 0 conn_stop_writing(self);
3534 0 conn_start_reading(self);
3535 0 ▶ } else if (err == SSL_ERROR_WANT_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (err == SSL_ERROR_WANT_WRITE) { 0 0 0
3536 0 conn_stop_reading(self);
3537 0 conn_start_writing(self);
3538 } else {
3539 char errbuf[256];
3540 0 unsigned long e = ERR_peek_last_error();
3541 0 ▶ if (e) {
Branch: 1 branch
BranchTrueFalseTotal
if (e) { 0 0 0
3542 0 snprintf(errbuf, sizeof(errbuf), "SSL_connect failed: %s",
3543 ERR_reason_error_string(e));
3544 } else {
3545 0 snprintf(errbuf, sizeof(errbuf), "SSL_connect failed (err=%d)", err);
3546 }
3547 0 ERR_clear_error();
3548 0 conn_emit_error(aTHX_ self, errbuf);
3549 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3550 0 conn_handle_disconnect(aTHX_ self, errbuf);
3551 }
3552 0 return;
3553 }
3554 #endif
3555  
3556 /* Write */
3557 367 ▶ if (revents & EV_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_WRITE) { 180 187 367
3558 360 ▶ while (self->wbuf_off < self->wbuf_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->wbuf_off < self->wbuf_len) { 180 180 360
3559 180 ssize_t n = kf_io_write(self, self->wbuf + self->wbuf_off,
3560 180 self->wbuf_len - self->wbuf_off);
3561 180 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 180 180
3562 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
3563 0 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK) break;
Branch: 2 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK) break; 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK) break; 0 0 0
3564 0 conn_emit_error(aTHX_ self, strerror(errno));
3565 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3566 0 conn_handle_disconnect(aTHX_ self, "write error");
3567 0 return;
3568 }
3569 180 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 0 180 180
3570 0 conn_handle_disconnect(aTHX_ self, "connection closed");
3571 0 return;
3572 }
3573 180 self->wbuf_off += n;
3574 }
3575  
3576 180 ▶ if (self->wbuf_off >= self->wbuf_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->wbuf_off >= self->wbuf_len) { 180 0 180
3577 180 self->wbuf_off = 0;
3578 180 self->wbuf_len = 0;
3579 180 conn_stop_writing(self);
3580 }
3581 }
3582  
3583 /* Read */
3584 367 ▶ if (revents & EV_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_READ) { 187 180 367
3585 187 conn_ensure_rbuf(self, self->rbuf_len + 8192);
3586 187 ssize_t n = kf_io_read(self, self->rbuf + self->rbuf_len,
3587 187 self->rbuf_cap - self->rbuf_len);
3588 187 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 187 187
3589 0 ▶ if (errno == EINTR) return; /* level-triggered watcher re-fires */
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) return; /* level-triggered watcher re-fires */ 0 0 0
3590 0 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK) return;
Branch: 2 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK) return; 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK) return; 0 0 0
3591 0 conn_emit_error(aTHX_ self, strerror(errno));
3592 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3593 0 conn_handle_disconnect(aTHX_ self, "read error");
3594 0 return;
3595 }
3596 187 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 10 177 187
3597 10 conn_handle_disconnect(aTHX_ self, "connection closed by broker");
3598 10 return;
3599 }
3600 177 self->rbuf_len += n;
3601  
3602 177 conn_process_responses(aTHX_ self);
3603 }
3604 }
3605  
3606 /* ================================================================
3607 * Connect timer (timeout)
3608 * ================================================================ */
3609  
3610 1 static void conn_timer_cb(EV_P_ ev_timer *w, int revents) {
3611 1 ev_kafka_conn_t *self = (ev_kafka_conn_t *)w->data;
3612 dTHX;
3613 (void)loop;
3614 1 self->timing = 0;
3615 1 ▶ if (self->magic != KF_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != KF_MAGIC_ALIVE) return; 0 1 1
3616 1 self->callback_depth++;
3617 {
3618 /* Per-event tmps frame: see conn_io_cb. */
3619 1 ENTER;
3620 1 SAVETMPS;
3621 1 conn_timer_cb_inner(loop, w, revents);
3622 1 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 1 1
3623 1 LEAVE;
3624 }
3625 1 ▶ if (--self->callback_depth == 0 && self->free_pending)
Branch: 2 branches
BranchTrueFalseTotal
if (--self->callback_depth == 0 && self->free_pending) 1 0 1
if (--self->callback_depth == 0 && self->free_pending) 0 1 1
3626 0 conn_do_free(aTHX_ self);
3627 }
3628  
3629 1 static void conn_timer_cb_inner(EV_P_ ev_timer *w, int revents) {
3630 1 ev_kafka_conn_t *self = (ev_kafka_conn_t *)w->data;
3631 dTHX;
3632 (void)loop;
3633 (void)revents;
3634  
3635 1 self->timing = 0;
3636 1 ▶ if (self->magic != KF_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != KF_MAGIC_ALIVE) return; 0 1 1
3637  
3638 2 const char *msg = self->state == CONN_CONNECTING
3639 1 ▶ ? "connect timeout" : "handshake timeout";
Branch: 1 branch
BranchTrueFalseTotal
? "connect timeout" : "handshake timeout"; 1 0 1
3640 1 conn_emit_error(aTHX_ self, msg);
3641 1 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 1 1
3642 1 conn_handle_disconnect(aTHX_ self, msg);
3643 }
3644  
3645 /* ================================================================
3646 * TCP connect + handshake initiation
3647 * ================================================================ */
3648  
3649 #ifdef HAVE_OPENSSL
3650 0 static int is_ip_literal(const char *host) {
3651 struct in_addr addr4;
3652 struct in6_addr addr6;
3653 0 return (inet_pton(AF_INET, host, &addr4) == 1 ||
3654 0 inet_pton(AF_INET6, host, &addr6) == 1);
3655 }
3656 #endif
3657  
3658 59 static void conn_on_connect_done(pTHX_ ev_kafka_conn_t *self) {
3659 /* TCP connected — set up I/O watchers if not already */
3660  
3661 59 self->reconnect_attempts = 0;
3662  
3663 #ifdef HAVE_OPENSSL
3664 59 ▶ if (self->tls_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->tls_enabled) { 0 59 59
3665 0 self->ssl_ctx = SSL_CTX_new(TLS_client_method());
3666 0 ▶ if (!self->ssl_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->ssl_ctx) { 0 0 0
3667 0 conn_emit_error(aTHX_ self, "SSL_CTX_new failed");
3668 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3669 0 conn_handle_disconnect(aTHX_ self, "SSL_CTX_new failed");
3670 0 return;
3671 }
3672 0 SSL_CTX_set_default_verify_paths(self->ssl_ctx);
3673 0 ▶ if (self->tls_skip_verify)
Branch: 1 branch
BranchTrueFalseTotal
if (self->tls_skip_verify) 0 0 0
3674 0 SSL_CTX_set_verify(self->ssl_ctx, SSL_VERIFY_NONE, NULL);
3675 else
3676 0 SSL_CTX_set_verify(self->ssl_ctx, SSL_VERIFY_PEER, NULL);
3677  
3678 0 ▶ if (self->tls_ca_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->tls_ca_file) { 0 0 0
3679 0 ▶ if (SSL_CTX_load_verify_locations(self->ssl_ctx, self->tls_ca_file, NULL) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (SSL_CTX_load_verify_locations(self->ssl_ctx, self->tls_ca_file, NULL) != 1) { 0 0 0
3680 0 conn_emit_error(aTHX_ self, "SSL_CTX_load_verify_locations failed");
3681 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3682 0 conn_handle_disconnect(aTHX_ self, "SSL_CTX_load_verify_locations failed");
3683 0 return;
3684 }
3685 }
3686  
3687 0 self->ssl = SSL_new(self->ssl_ctx);
3688 0 ▶ if (!self->ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->ssl) { 0 0 0
3689 0 conn_emit_error(aTHX_ self, "SSL_new failed");
3690 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3691 0 conn_handle_disconnect(aTHX_ self, "SSL_new failed");
3692 0 return;
3693 }
3694 0 SSL_set_fd(self->ssl, self->fd);
3695  
3696 0 ▶ if (!is_ip_literal(self->host))
Branch: 1 branch
BranchTrueFalseTotal
if (!is_ip_literal(self->host)) 0 0 0
3697 0 SSL_set_tlsext_host_name(self->ssl, self->host);
3698  
3699 0 ▶ if (!self->tls_skip_verify) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->tls_skip_verify) { 0 0 0
3700 0 X509_VERIFY_PARAM *param = SSL_get0_param(self->ssl);
3701 0 X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
3702 0 ▶ if (is_ip_literal(self->host))
Branch: 1 branch
BranchTrueFalseTotal
if (is_ip_literal(self->host)) 0 0 0
3703 0 X509_VERIFY_PARAM_set1_ip_asc(param, self->host);
3704 else
3705 0 X509_VERIFY_PARAM_set1_host(param, self->host, 0);
3706 }
3707  
3708 0 self->state = CONN_TLS_HANDSHAKE;
3709 0 ERR_clear_error();
3710 0 int ret = SSL_connect(self->ssl);
3711 0 ▶ if (ret == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 1) { 0 0 0
3712 /* Immediate success */
3713 0 conn_send_api_versions(aTHX_ self);
3714 0 conn_start_reading(self);
3715 0 return;
3716 }
3717 0 int err = SSL_get_error(self->ssl, ret);
3718 0 ▶ if (err == SSL_ERROR_WANT_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == SSL_ERROR_WANT_READ) { 0 0 0
3719 0 conn_start_reading(self);
3720 0 ▶ } else if (err == SSL_ERROR_WANT_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (err == SSL_ERROR_WANT_WRITE) { 0 0 0
3721 0 conn_start_writing(self);
3722 } else {
3723 char errbuf[256];
3724 0 unsigned long e = ERR_peek_last_error();
3725 0 ▶ if (e) {
Branch: 1 branch
BranchTrueFalseTotal
if (e) { 0 0 0
3726 0 snprintf(errbuf, sizeof(errbuf), "SSL_connect failed: %s",
3727 ERR_reason_error_string(e));
3728 } else {
3729 0 snprintf(errbuf, sizeof(errbuf), "SSL_connect failed (err=%d)", err);
3730 }
3731 0 ERR_clear_error();
3732 0 conn_emit_error(aTHX_ self, errbuf);
3733 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3734 0 conn_handle_disconnect(aTHX_ self, errbuf);
3735 }
3736 0 return;
3737 }
3738 #endif
3739  
3740 /* No TLS — send ApiVersions directly */
3741 59 conn_send_api_versions(aTHX_ self);
3742 59 conn_start_reading(self);
3743 }
3744  
3745 71 static void conn_start_connect(pTHX_ ev_kafka_conn_t *self,
3746 const char *host, int port, double timeout)
3747 {
3748 struct addrinfo hints, *res, *rp;
3749 char port_str[16];
3750 71 int fd = -1;
3751  
3752 71 ▶ if (self->state != CONN_DISCONNECTED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_DISCONNECTED) { 0 71 71
3753 0 conn_cleanup(aTHX_ self);
3754 /* fail old-conn callbacks: their correlation IDs must not match the new wire */
3755 0 conn_cancel_pending(aTHX_ self, "reconnecting");
3756 71 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3757 }
3758  
3759 /* Save host/port (skip if already pointing to same string, e.g. reconnect) */
3760 71 ▶ if (host != self->host) {
Branch: 1 branch
BranchTrueFalseTotal
if (host != self->host) { 59 12 71
3761 59 ▶ if (self->host) Safefree(self->host);
Branch: 1 branch
BranchTrueFalseTotal
if (self->host) Safefree(self->host); 0 59 59
3762 59 self->host = savepv(host);
3763 }
3764 71 self->port = port;
3765 71 self->intentional_disconnect = 0;
3766 /* default timeout so a silent peer cannot wedge the conn forever */
3767 71 ▶ self->connect_timeout = (timeout > 0) ? timeout : 10.0;
Branch: 1 branch
BranchTrueFalseTotal
self->connect_timeout = (timeout > 0) ? timeout : 10.0; 71 0 71
3768  
3769 71 snprintf(port_str, sizeof(port_str), "%d", port);
3770  
3771 71 memset(&hints, 0, sizeof(hints));
3772 71 hints.ai_family = AF_UNSPEC;
3773 71 hints.ai_socktype = SOCK_STREAM;
3774  
3775 /* Fast path: if host is a numeric IP literal, AI_NUMERICHOST avoids any
3776 * DNS resolver work and returns synchronously without blocking the EV
3777 * loop. For non-literal hostnames getaddrinfo still blocks; users that
3778 * need fully-async DNS should resolve in Perl-land before connecting. */
3779 71 hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
3780 71 int gai_err = getaddrinfo(host, port_str, &hints, &res);
3781 71 ▶ if (gai_err != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gai_err != 0) { 2 69 71
3782 2 hints.ai_flags = 0;
3783 2 gai_err = getaddrinfo(host, port_str, &hints, &res);
3784 }
3785 71 ▶ if (gai_err != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gai_err != 0) { 2 69 71
3786 char errbuf[256];
3787 2 snprintf(errbuf, sizeof(errbuf), "resolve: %s", gai_strerror(gai_err));
3788 2 conn_emit_error(aTHX_ self, errbuf);
3789 2 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 2 2
3790 /* already DISCONNECTED: conn_handle_disconnect would no-op, so
3791 * schedule the retry directly. */
3792 2 ▶ if (!self->intentional_disconnect && self->auto_reconnect)
Branch: 2 branches
BranchTrueFalseTotal
if (!self->intentional_disconnect && self->auto_reconnect) 2 0 2
if (!self->intentional_disconnect && self->auto_reconnect) 2 0 2
3793 2 conn_schedule_reconnect(aTHX_ self);
3794 2 return;
3795 }
3796  
3797 69 ▶ for (rp = res; rp; rp = rp->ai_next) {
Branch: 1 branch
BranchTrueFalseTotal
for (rp = res; rp; rp = rp->ai_next) { 69 0 69
3798 69 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
3799 69 ▶ if (fd < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) continue; 0 69 69
3800  
3801 #ifdef SO_NOSIGPIPE
3802 /* BSD/macOS: no SIGPIPE from writes on this socket */
3803 {
3804 int one = 1;
3805 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one));
3806 }
3807 #endif
3808  
3809 /* Non-blocking */
3810 69 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
3811  
3812 /* TCP_NODELAY */
3813 {
3814 69 int one = 1;
3815 69 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
3816 }
3817  
3818 69 int ret = connect(fd, rp->ai_addr, rp->ai_addrlen);
3819 69 ▶ if (ret == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 0) { 0 69 69
3820 /* Immediate connect */
3821 0 self->fd = fd;
3822 0 self->state = CONN_CONNECTING; /* will be advanced in on_connect_done */
3823 0 freeaddrinfo(res);
3824  
3825 0 ev_io_init(&self->rio, conn_io_cb, fd, EV_READ);
3826 0 self->rio.data = (void *)self;
3827 0 ev_io_init(&self->wio, conn_io_cb, fd, EV_WRITE);
3828 0 self->wio.data = (void *)self;
3829  
3830 0 conn_arm_timer(self); /* cover the handshake phases */
3831 0 conn_on_connect_done(aTHX_ self);
3832 0 return;
3833 }
3834  
3835 69 ▶ if (errno == EINPROGRESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINPROGRESS) { 69 0 69
3836 69 self->fd = fd;
3837 69 self->state = CONN_CONNECTING;
3838 69 freeaddrinfo(res);
3839  
3840 69 ev_io_init(&self->rio, conn_io_cb, fd, EV_READ);
3841 69 self->rio.data = (void *)self;
3842 69 ev_io_init(&self->wio, conn_io_cb, fd, EV_WRITE);
3843 69 self->wio.data = (void *)self;
3844  
3845 69 conn_start_writing(self); /* wait for connect to complete */
3846  
3847 69 conn_arm_timer(self);
3848 69 return;
3849 }
3850  
3851 0 close(fd);
3852 }
3853  
3854 0 freeaddrinfo(res);
3855 0 conn_emit_error(aTHX_ self, "connect: all addresses failed");
3856 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3857 /* See the resolve-failure branch above: schedule the retry directly. */
3858 0 ▶ if (!self->intentional_disconnect && self->auto_reconnect)
Branch: 2 branches
BranchTrueFalseTotal
if (!self->intentional_disconnect && self->auto_reconnect) 0 0 0
if (!self->intentional_disconnect && self->auto_reconnect) 0 0 0
3859 0 conn_schedule_reconnect(aTHX_ self);
3860 }
3861  
3862 /* Pre-validate the topics ARRAY so croak() doesn't longjmp over
3863 * kf_buf_free in the caller; check_part_records also requires
3864 * {partition, offset} hashrefs per partition. */
3865 40408 static void kf_validate_txn_topics(const char *who, SV *topics_sv,
3866 int check_part_records)
3867 {
3868 40408 ▶ if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 40408 0 40408
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 0 40408 40408
3869 0 croak("%s: expected arrayref", who);
3870 40408 AV *topics = (AV*)SvRV(topics_sv);
3871 40408 SSize_t i, tc = av_len(topics) + 1;
3872 40414 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 40408 6 40414
3873 40408 SV **elem = av_fetch(topics, i, 0);
3874 40408 ▶ if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 40408 0 40408
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 40408 0 40408
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 0 40408 40408
3875 0 croak("%s: bad element", who);
3876 40408 HV *th = (HV*)SvRV(*elem);
3877 40408 ▶ if (!hv_fetch(th, "topic", 5, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_fetch(th, "topic", 5, 0)) 20201 20207 40408
3878 20201 croak("%s: missing topic", who);
3879 20207 SV **parts_sv = hv_fetch(th, "partitions", 10, 0);
3880 20207 ▶ if (!parts_sv || !SvROK(*parts_sv) || SvTYPE(SvRV(*parts_sv)) != SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
if (!parts_sv || !SvROK(*parts_sv) || SvTYPE(SvRV(*parts_sv)) != SVt_PVAV) 7 20200 20207
if (!parts_sv || !SvROK(*parts_sv) || SvTYPE(SvRV(*parts_sv)) != SVt_PVAV) 7 0 7
if (!parts_sv || !SvROK(*parts_sv) || SvTYPE(SvRV(*parts_sv)) != SVt_PVAV) 0 7 7
3881 20200 croak("%s: missing partitions", who);
3882 7 ▶ if (check_part_records) {
Branch: 1 branch
BranchTrueFalseTotal
if (check_part_records) { 3 4 7
3883 3 AV *parts = (AV*)SvRV(*parts_sv);
3884 3 SSize_t j, pc = av_len(parts) + 1;
3885 5 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 3 2 5
3886 3 SV **pelem = av_fetch(parts, j, 0);
3887 3 ▶ if (!pelem || !SvROK(*pelem) || SvTYPE(SvRV(*pelem)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!pelem || !SvROK(*pelem) || SvTYPE(SvRV(*pelem)) != SVt_PVHV) 3 0 3
if (!pelem || !SvROK(*pelem) || SvTYPE(SvRV(*pelem)) != SVt_PVHV) 2 1 3
if (!pelem || !SvROK(*pelem) || SvTYPE(SvRV(*pelem)) != SVt_PVHV) 0 2 2
3888 1 croak("%s: bad partition", who);
3889 }
3890 }
3891 }
3892 6 }
3893  
3894 /* ================================================================
3895 * XS INTERFACE
3896 * ================================================================ */
3897  
3898 MODULE = EV::Kafka PACKAGE = EV::Kafka::Conn
3899  
3900 PROTOTYPES: DISABLE
3901  
3902 EV::Kafka::Conn
3903 _new(char *cls, SV *loop_sv)
3904 CODE:
3905 {
3906 struct ev_loop *loop;
3907 ev_kafka_conn_t *self;
3908  
3909 67 ▶ if (SvOK(loop_sv) && sv_derived_from(loop_sv, "EV::Loop")) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(loop_sv) && sv_derived_from(loop_sv, "EV::Loop")) { 1 66 67
if (SvOK(loop_sv) && sv_derived_from(loop_sv, "EV::Loop")) { 1 0 1
3910 1 loop = (struct ev_loop *)SvIV(SvRV(loop_sv));
3911 }
3912 else
3913 66 loop = EV_DEFAULT;
3914  
3915 67 Newxz(self, 1, ev_kafka_conn_t);
3916 67 self->magic = KF_MAGIC_ALIVE;
3917 67 self->loop = loop;
3918 67 ▶ if (loop != EV_DEFAULT)
Branch: 1 branch
BranchTrueFalseTotal
if (loop != EV_DEFAULT) 1 66 67
3919 1 self->loop_sv = newSVsv(loop_sv); /* freed in conn_do_free */
3920 67 self->fd = -1;
3921 67 self->state = CONN_DISCONNECTED;
3922 67 self->next_correlation_id = 1;
3923  
3924 67 Newx(self->rbuf, KF_BUF_INIT, char);
3925 67 self->rbuf_cap = KF_BUF_INIT;
3926 67 self->rbuf_len = 0;
3927 67 Newx(self->wbuf, KF_BUF_INIT, char);
3928 67 self->wbuf_cap = KF_BUF_INIT;
3929 67 self->wbuf_len = 0;
3930 67 self->wbuf_off = 0;
3931  
3932 67 ngx_queue_init(&self->cb_queue);
3933  
3934 /* Default client_id */
3935 67 self->client_id = savepv("ev-kafka");
3936 67 self->client_id_len = 8;
3937  
3938 /* Default: no API versions known */
3939 {
3940 int i;
3941 4355 ▶ for (i = 0; i < API_VERSIONS_MAX_KEY; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < API_VERSIONS_MAX_KEY; i++) 4288 67 4355
3942 4288 self->api_versions[i] = -1;
3943 }
3944  
3945 67 self->reconnect_delay_ms = 1000;
3946  
3947 67 RETVAL = self;
3948 }
3949 OUTPUT:
3950 RETVAL
3951  
3952 void
3953 DESTROY(EV::Kafka::Conn self)
3954 CODE:
3955 {
3956 82 ▶ if (!self) return; /* IV already nulled by an earlier DESTROY */
Branch: 1 branch
BranchTrueFalseTotal
if (!self) return; /* IV already nulled by an earlier DESTROY */ 15 67 82
3957 67 ▶ if (self->magic != KF_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != KF_MAGIC_ALIVE) return; 0 67 67
3958 67 ▶ if (self->free_pending) return; /* already scheduled for deferred free */
Branch: 1 branch
BranchTrueFalseTotal
if (self->free_pending) return; /* already scheduled for deferred free */ 0 67 67
3959  
3960 67 self->intentional_disconnect = 1;
3961 67 conn_cleanup(aTHX_ self);
3962  
3963 /* Mark dead BEFORE firing any callback (conn_cancel_pending fires the
3964 * pending command callbacks): a re-entrant DESTROY then hits the
3965 * free_pending guard, and conn_check_destroyed reports the conn gone. */
3966 67 self->magic = KF_MAGIC_FREED;
3967 67 self->free_pending = 1;
3968  
3969 /* Null the IV in the blessed ref BEFORE the drain: every copy of
3970 * the ref shares it, so teardown callbacks calling methods on the
3971 * dying conn get a clean croak (KF_REQUIRE_CONN). */
3972 67 sv_setiv(SvRV(ST(0)), 0);
3973  
3974 67 conn_cancel_pending(aTHX_ self, "destroyed");
3975  
3976 /* If we are inside an event-loop watcher callback (callback_depth>0),
3977 * that outermost frame runs conn_do_free when it unwinds to depth 0.
3978 * Otherwise free now. conn_cancel_pending only fires callbacks and a
3979 * nested DESTROY is blocked above, so self is still valid here. */
3980 67 ▶ if (self->callback_depth == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->callback_depth == 0) 66 1 67
3981 66 conn_do_free(aTHX_ self);
3982 }
3983  
3984 void
3985 connect(EV::Kafka::Conn self, const char *host, int port, double timeout = 0)
3986 CODE:
3987 {
3988 60 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 1 59 60
3989 59 conn_start_connect(aTHX_ self, host, port, timeout);
3990 }
3991  
3992 void
3993 disconnect(EV::Kafka::Conn self)
3994 CODE:
3995 {
3996 40 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 40 40
3997 40 self->intentional_disconnect = 1;
3998 40 ▶ if (self->reconnect_timing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reconnect_timing) { 0 40 40
3999 0 ev_timer_stop(self->loop, &self->reconnect_timer);
4000 0 self->reconnect_timing = 0;
4001 }
4002 40 conn_handle_disconnect(aTHX_ self, "disconnected");
4003 }
4004  
4005 int
4006 connected(EV::Kafka::Conn self)
4007 CODE:
4008 {
4009 436 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 436 436
4010 436 ▶ RETVAL = (self->state == CONN_READY) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (self->state == CONN_READY) ? 1 : 0; 365 71 436
4011 }
4012 OUTPUT:
4013 RETVAL
4014  
4015 int
4016 state(EV::Kafka::Conn self)
4017 CODE:
4018 {
4019 1 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 1 0 1
4020 0 ▶ RETVAL = self->state;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->state; 0 0 0
4021 }
4022 OUTPUT:
4023 RETVAL
4024  
4025 int
4026 pending(EV::Kafka::Conn self)
4027 CODE:
4028 {
4029 44 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 1 43 44
4030 43 ▶ RETVAL = self->pending_count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->pending_count; 36 7 43
4031 }
4032 OUTPUT:
4033 RETVAL
4034  
4035 void
4036 on_error(EV::Kafka::Conn self, SV *cb = NULL)
4037 CODE:
4038 {
4039 154 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 154 154
4040 154 ▶ CLEAR_HANDLER(self->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_error); 96 58 154
4041 154 ▶ if (cb && SvOK(cb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cb && SvOK(cb)) { 154 0 154
if (cb && SvOK(cb)) { 122 32 154
4042 122 self->on_error = newSVsv(cb);
4043 }
4044 }
4045  
4046 void
4047 on_connect(EV::Kafka::Conn self, SV *cb = NULL)
4048 CODE:
4049 {
4050 80 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 80 80
4051 80 ▶ CLEAR_HANDLER(self->on_connect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_connect); 32 48 80
4052 80 ▶ if (cb && SvOK(cb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cb && SvOK(cb)) { 80 0 80
if (cb && SvOK(cb)) { 48 32 80
4053 48 self->on_connect = newSVsv(cb);
4054 }
4055 }
4056  
4057 void
4058 on_disconnect(EV::Kafka::Conn self, SV *cb = NULL)
4059 CODE:
4060 {
4061 105 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 105 105
4062 105 ▶ CLEAR_HANDLER(self->on_disconnect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_disconnect); 63 42 105
4063 105 ▶ if (cb && SvOK(cb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cb && SvOK(cb)) { 105 0 105
if (cb && SvOK(cb)) { 73 32 105
4064 73 self->on_disconnect = newSVsv(cb);
4065 }
4066 }
4067  
4068 void
4069 client_id(EV::Kafka::Conn self, const char *id = NULL)
4070 CODE:
4071 {
4072 33 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 33 33
4073 33 ▶ if (id) {
Branch: 1 branch
BranchTrueFalseTotal
if (id) { 33 0 33
4074 33 ▶ if (self->client_id) Safefree(self->client_id);
Branch: 1 branch
BranchTrueFalseTotal
if (self->client_id) Safefree(self->client_id); 33 0 33
4075 33 self->client_id = savepv(id);
4076 33 self->client_id_len = strlen(id);
4077 }
4078 }
4079  
4080 void
4081 tls(EV::Kafka::Conn self, int enable, const char *ca_file = NULL, int skip_verify = 0)
4082 CODE:
4083 {
4084 0 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 0 0
4085 0 self->tls_enabled = enable;
4086 0 ▶ if (self->tls_ca_file) { Safefree(self->tls_ca_file); self->tls_ca_file = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (self->tls_ca_file) { Safefree(self->tls_ca_file); self->tls_ca_file = NULL; } 0 0 0
4087 0 ▶ if (ca_file && *ca_file) self->tls_ca_file = savepv(ca_file);
Branch: 2 branches
BranchTrueFalseTotal
if (ca_file && *ca_file) self->tls_ca_file = savepv(ca_file); 0 0 0
if (ca_file && *ca_file) self->tls_ca_file = savepv(ca_file); 0 0 0
4088 0 self->tls_skip_verify = skip_verify;
4089 }
4090  
4091 void
4092 sasl(EV::Kafka::Conn self, const char *mechanism, const char *username = NULL, const char *password = NULL)
4093 CODE:
4094 {
4095 3 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 3 3
4096 3 ▶ if (self->sasl_mechanism) { Safefree(self->sasl_mechanism); self->sasl_mechanism = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_mechanism) { Safefree(self->sasl_mechanism); self->sasl_mechanism = NULL; } 0 3 3
4097 3 ▶ if (self->sasl_username) { Safefree(self->sasl_username); self->sasl_username = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_username) { Safefree(self->sasl_username); self->sasl_username = NULL; } 0 3 3
4098 3 ▶ if (self->sasl_password) { Safefree(self->sasl_password); self->sasl_password = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_password) { Safefree(self->sasl_password); self->sasl_password = NULL; } 0 3 3
4099 3 ▶ if (SvOK(ST(1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(ST(1))) { 3 0 3
4100 3 self->sasl_mechanism = savepv(mechanism);
4101 /* undef arrives as "" through the typemap — store NULL instead. */
4102 3 ▶ if (username && *username) self->sasl_username = savepv(username);
Branch: 2 branches
BranchTrueFalseTotal
if (username && *username) self->sasl_username = savepv(username); 3 0 3
if (username && *username) self->sasl_username = savepv(username); 1 2 3
4103 3 ▶ if (password && *password) self->sasl_password = savepv(password);
Branch: 2 branches
BranchTrueFalseTotal
if (password && *password) self->sasl_password = savepv(password); 3 0 3
if (password && *password) self->sasl_password = savepv(password); 1 2 3
4104 }
4105 }
4106  
4107 void
4108 auto_reconnect(EV::Kafka::Conn self, int enable, int delay_ms = 1000)
4109 CODE:
4110 {
4111 34 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 34 34
4112 34 self->auto_reconnect = enable;
4113 34 self->reconnect_delay_ms = delay_ms;
4114 }
4115  
4116 void
4117 metadata(EV::Kafka::Conn self, SV *topics_sv, SV *cb)
4118 CODE:
4119 {
4120 50 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 1 49 50
4121 49 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 1 48 49
4122 1 croak("not connected");
4123  
4124 kf_buf_t body;
4125 48 kf_buf_init(&body);
4126  
4127 /* Metadata v1-v4 (non-flexible) */
4128 48 int16_t ver = self->api_versions[API_METADATA];
4129 48 ▶ if (ver < 0) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 1; 7 41 48
4130 48 ▶ if (ver > 4) ver = 4;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 4) ver = 4; 0 48 48
4131  
4132 48 ▶ if (SvOK(topics_sv) && SvROK(topics_sv) && SvTYPE(SvRV(topics_sv)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvOK(topics_sv) && SvROK(topics_sv) && SvTYPE(SvRV(topics_sv)) == SVt_PVAV) { 0 48 48
if (SvOK(topics_sv) && SvROK(topics_sv) && SvTYPE(SvRV(topics_sv)) == SVt_PVAV) { 0 0 0
if (SvOK(topics_sv) && SvROK(topics_sv) && SvTYPE(SvRV(topics_sv)) == SVt_PVAV) { 0 0 0
4133 0 AV *topics = (AV*)SvRV(topics_sv);
4134 0 SSize_t i, count = av_len(topics) + 1;
4135 0 kf_buf_append_i32(&body, (int32_t)count);
4136 0 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 0 0 0
4137 0 SV **elem = av_fetch(topics, i, 0);
4138 STRLEN tlen;
4139 0 const char *tname = SvPV(*elem, tlen);
4140 0 kf_buf_append_string(&body, tname, (int16_t)tlen);
4141 }
4142 } else {
4143 48 kf_buf_append_i32(&body, -1); /* null array = all topics */
4144 }
4145  
4146 /* allow_auto_topic_creation (v4+) */
4147 48 ▶ if (ver >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 4) 0 48 48
4148 0 kf_buf_append_i8(&body, 1);
4149  
4150 48 conn_send_request(aTHX_ self, API_METADATA, ver, &body, cb, 0, 0);
4151 48 kf_buf_free(&body);
4152 }
4153  
4154 void
4155 api_versions(EV::Kafka::Conn self)
4156 PPCODE:
4157 {
4158 1 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 1 1
4159 1 ▶ if (!self->api_versions_known)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->api_versions_known) 0 1 1
4160 0 XSRETURN_UNDEF;
4161  
4162 1 HV *hv = newHV();
4163 int i;
4164 65 ▶ for (i = 0; i < API_VERSIONS_MAX_KEY; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < API_VERSIONS_MAX_KEY; i++) { 64 1 65
4165 64 ▶ if (self->api_versions[i] >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->api_versions[i] >= 0) { 16 48 64
4166 char key[8];
4167 16 int klen = snprintf(key, sizeof(key), "%d", i);
4168 16 hv_store(hv, key, klen, newSViv(self->api_versions[i]), 0);
4169 }
4170 }
4171 1 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 1 1
4172 1 mPUSHs(newRV_noinc((SV*)hv));
4173 1 XSRETURN(1);
4174 }
4175  
4176 void
4177 fetch(EV::Kafka::Conn self, const char *topic, int partition, SV *offset_sv, SV *arg1 = NULL, SV *arg2 = NULL)
4178 CODE:
4179 {
4180 2 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 2 2
4181 2 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 1 1 2
4182 1 croak("not connected");
4183  
4184 /* Accept either fetch(..., $cb) or fetch(..., \%opts, $cb).
4185 * $opts may set max_bytes, max_wait_ms, min_bytes. */
4186 1 SV *cb = NULL;
4187 1 HV *opts = NULL;
4188 1 ▶ if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) { 1 0 1
if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) { 1 0 1
if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) { 0 1 1
4189 0 opts = (HV*)SvRV(arg1);
4190 0 cb = arg2;
4191 } else {
4192 1 cb = arg1;
4193 }
4194  
4195 1 int32_t max_bytes = 1048576;
4196 1 int32_t max_wait_ms = 500;
4197 1 int32_t min_bytes = 1;
4198 1 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 0 1 1
4199 SV **v;
4200 0 ▶ if ((v = hv_fetchs(opts, "max_bytes", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "max_bytes", 0)) && SvOK(*v)) 0 0 0
if ((v = hv_fetchs(opts, "max_bytes", 0)) && SvOK(*v)) 0 0 0
4201 0 max_bytes = (int32_t)SvIV(*v);
4202 0 ▶ if ((v = hv_fetchs(opts, "max_wait_ms", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "max_wait_ms", 0)) && SvOK(*v)) 0 0 0
if ((v = hv_fetchs(opts, "max_wait_ms", 0)) && SvOK(*v)) 0 0 0
4203 0 max_wait_ms = (int32_t)SvIV(*v);
4204 0 ▶ if ((v = hv_fetchs(opts, "min_bytes", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "min_bytes", 0)) && SvOK(*v)) 0 0 0
if ((v = hv_fetchs(opts, "min_bytes", 0)) && SvOK(*v)) 0 0 0
4205 0 min_bytes = (int32_t)SvIV(*v);
4206 }
4207  
4208 1 int64_t offset = SvIV(offset_sv);
4209 1 STRLEN topic_len = strlen(topic);
4210  
4211 1 int16_t ver = self->api_versions[API_FETCH];
4212 1 ▶ if (ver < 0) ver = 4;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 4; 0 1 1
4213 1 ▶ if (ver > 7) ver = 7;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 7) ver = 7; 0 1 1
4214  
4215 kf_buf_t body;
4216 1 kf_buf_init(&body);
4217  
4218 1 kf_buf_append_i32(&body, -1); /* replica_id = -1 (consumer) */
4219 1 kf_buf_append_i32(&body, max_wait_ms);
4220 1 kf_buf_append_i32(&body, min_bytes);
4221  
4222 /* max_bytes (v3+) */
4223 1 ▶ if (ver >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 3) 1 0 1
4224 1 kf_buf_append_i32(&body, max_bytes);
4225  
4226 /* isolation_level (v4+) */
4227 1 ▶ if (ver >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 4) 1 0 1
4228 1 kf_buf_append_i8(&body, 0); /* READ_UNCOMMITTED */
4229  
4230 /* session_id + session_epoch (v7+) */
4231 1 ▶ if (ver >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 7) { 1 0 1
4232 1 kf_buf_append_i32(&body, 0); /* session_id */
4233 1 kf_buf_append_i32(&body, -1); /* session_epoch */
4234 }
4235  
4236 /* topics: ARRAY(1) */
4237 1 kf_buf_append_i32(&body, 1);
4238 1 kf_buf_append_string(&body, topic, (int16_t)topic_len);
4239  
4240 /* partitions: ARRAY(1) */
4241 1 kf_buf_append_i32(&body, 1);
4242 1 kf_buf_append_i32(&body, (int32_t)partition);
4243  
4244 /* fetch_offset */
4245 1 kf_buf_append_i64(&body, offset);
4246  
4247 /* log_start_offset (v5+) */
4248 1 ▶ if (ver >= 5)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 5) 1 0 1
4249 1 kf_buf_append_i64(&body, -1);
4250  
4251 /* partition_max_bytes */
4252 1 kf_buf_append_i32(&body, max_bytes);
4253  
4254 /* forgotten_topics_data (v7+) */
4255 1 ▶ if (ver >= 7)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 7) 1 0 1
4256 1 kf_buf_append_i32(&body, 0); /* empty array */
4257  
4258 1 conn_send_request(aTHX_ self, API_FETCH, ver, &body, cb, 0, 0);
4259 1 kf_buf_free(&body);
4260 }
4261  
4262 void
4263 fetch_multi(EV::Kafka::Conn self, SV *topics_sv, SV *arg1 = NULL, SV *arg2 = NULL)
4264 CODE:
4265 {
4266 20214 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 20214 20214
4267 20214 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 20214 20214
4268 0 croak("not connected");
4269  
4270 /* topics_sv: { topic => [{partition => N, offset => N}, ...], ... }
4271 * accept fetch_multi(\%topics, $cb) or fetch_multi(\%topics, \%opts, $cb). */
4272 20214 ▶ if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVHV) 20214 0 20214
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVHV) 0 20214 20214
4273 0 croak("fetch_multi: expected hashref");
4274 20214 HV *topics_hv = (HV*)SvRV(topics_sv);
4275  
4276 20214 SV *cb = NULL;
4277 20214 HV *opts = NULL;
4278 20214 ▶ if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) { 20214 0 20214
if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) { 20214 0 20214
if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) { 11 20203 20214
4279 11 opts = (HV*)SvRV(arg1);
4280 11 cb = arg2;
4281 } else {
4282 20203 cb = arg1;
4283 }
4284  
4285 20214 int32_t max_bytes = 1048576;
4286 20214 int32_t max_wait_ms = 500;
4287 20214 int32_t min_bytes = 1;
4288 20214 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 11 20203 20214
4289 SV **v;
4290 11 ▶ if ((v = hv_fetchs(opts, "max_bytes", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "max_bytes", 0)) && SvOK(*v)) 11 0 11
if ((v = hv_fetchs(opts, "max_bytes", 0)) && SvOK(*v)) 11 0 11
4291 11 max_bytes = (int32_t)SvIV(*v);
4292 11 ▶ if ((v = hv_fetchs(opts, "max_wait_ms", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "max_wait_ms", 0)) && SvOK(*v)) 11 0 11
if ((v = hv_fetchs(opts, "max_wait_ms", 0)) && SvOK(*v)) 11 0 11
4293 11 max_wait_ms = (int32_t)SvIV(*v);
4294 11 ▶ if ((v = hv_fetchs(opts, "min_bytes", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "min_bytes", 0)) && SvOK(*v)) 11 0 11
if ((v = hv_fetchs(opts, "min_bytes", 0)) && SvOK(*v)) 11 0 11
4295 11 min_bytes = (int32_t)SvIV(*v);
4296 }
4297  
4298 20214 int16_t ver = self->api_versions[API_FETCH];
4299 20214 ▶ if (ver < 0) ver = 4;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 4; 20203 11 20214
4300 20214 ▶ if (ver > 7) ver = 7;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 7) ver = 7; 0 20214 20214
4301  
4302 /* Validate up front so croak() doesn't longjmp over kf_buf_free. */
4303 {
4304 20214 hv_iterinit(topics_hv);
4305 HE *entry;
4306 20226 ▶ while ((entry = hv_iternext(topics_hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(topics_hv))) { 20214 12 20226
4307 20214 SV *parts_sv = hv_iterval(topics_hv, entry);
4308 20214 ▶ if (!SvROK(parts_sv) || SvTYPE(SvRV(parts_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(parts_sv) || SvTYPE(SvRV(parts_sv)) != SVt_PVAV) 20213 1 20214
if (!SvROK(parts_sv) || SvTYPE(SvRV(parts_sv)) != SVt_PVAV) 0 20213 20213
4309 1 croak("fetch_multi: value must be an arrayref");
4310 20213 AV *parts_av = (AV*)SvRV(parts_sv);
4311 20213 SSize_t i, pc = av_len(parts_av) + 1;
4312 20225 ▶ for (i = 0; i < pc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pc; i++) { 20213 12 20225
4313 20213 SV **elem = av_fetch(parts_av, i, 0);
4314 20213 ▶ if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 20213 0 20213
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 12 20201 20213
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 0 12 12
4315 20201 croak("fetch_multi: partition entry must be a hashref");
4316 }
4317 }
4318 }
4319  
4320 kf_buf_t body;
4321 12 kf_buf_init(&body);
4322  
4323 12 kf_buf_append_i32(&body, -1); /* replica_id */
4324 12 kf_buf_append_i32(&body, max_wait_ms);
4325 12 kf_buf_append_i32(&body, min_bytes);
4326 12 ▶ if (ver >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 3) 12 0 12
4327 12 kf_buf_append_i32(&body, max_bytes);
4328 12 ▶ if (ver >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 4) 12 0 12
4329 12 kf_buf_append_i8(&body, 0); /* isolation_level */
4330 12 ▶ if (ver >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 7) { 11 1 12
4331 11 kf_buf_append_i32(&body, 0); /* session_id */
4332 11 kf_buf_append_i32(&body, -1); /* session_epoch */
4333 }
4334  
4335 /* topics array */
4336 12 ▶ kf_buf_append_i32(&body, (int32_t)HvUSEDKEYS(topics_hv));
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_i32(&body, (int32_t)HvUSEDKEYS(topics_hv)); 0 12 12
4337  
4338 12 hv_iterinit(topics_hv);
4339 HE *entry;
4340 24 ▶ while ((entry = hv_iternext(topics_hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(topics_hv))) { 12 12 24
4341 I32 tlen;
4342 12 const char *tname = hv_iterkey(entry, &tlen);
4343 12 kf_buf_append_string(&body, tname, (int16_t)tlen);
4344  
4345 12 SV *parts_sv = hv_iterval(topics_hv, entry);
4346 12 AV *parts_av = (AV*)SvRV(parts_sv); /* validated above */
4347 12 SSize_t i, pc = av_len(parts_av) + 1;
4348 12 kf_buf_append_i32(&body, (int32_t)pc);
4349  
4350 24 ▶ for (i = 0; i < pc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pc; i++) { 12 12 24
4351 12 SV **elem = av_fetch(parts_av, i, 0);
4352 12 HV *ph = (HV*)SvRV(*elem); /* validated above */
4353  
4354 12 SV **pid_sv = hv_fetch(ph, "partition", 9, 0);
4355 12 ▶ int32_t pid = pid_sv ? (int32_t)SvIV(*pid_sv) : 0;
Branch: 1 branch
BranchTrueFalseTotal
int32_t pid = pid_sv ? (int32_t)SvIV(*pid_sv) : 0; 12 0 12
4356 12 kf_buf_append_i32(&body, pid);
4357  
4358 12 SV **off_sv = hv_fetch(ph, "offset", 6, 0);
4359 12 ▶ int64_t offset = off_sv ? (int64_t)SvIV(*off_sv) : 0;
Branch: 1 branch
BranchTrueFalseTotal
int64_t offset = off_sv ? (int64_t)SvIV(*off_sv) : 0; 12 0 12
4360 12 kf_buf_append_i64(&body, offset);
4361  
4362 12 ▶ if (ver >= 5)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 5) 11 1 12
4363 11 kf_buf_append_i64(&body, -1); /* log_start_offset */
4364  
4365 12 kf_buf_append_i32(&body, max_bytes); /* partition_max_bytes */
4366 }
4367 }
4368  
4369 12 ▶ if (ver >= 7)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 7) 11 1 12
4370 11 kf_buf_append_i32(&body, 0); /* forgotten_topics_data */
4371  
4372 12 conn_send_request(aTHX_ self, API_FETCH, ver, &body, cb, 0, 0);
4373 12 kf_buf_free(&body);
4374 }
4375  
4376 void
4377 produce_batch(EV::Kafka::Conn self, const char *topic, int partition, SV *records_sv, SV *opts_sv = NULL, SV *cb = NULL)
4378 CODE:
4379 {
4380 16 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 16 16
4381 16 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 16 16
4382 0 croak("not connected");
4383  
4384 16 ▶ if (!SvROK(records_sv) || SvTYPE(SvRV(records_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(records_sv) || SvTYPE(SvRV(records_sv)) != SVt_PVAV) 16 0 16
if (!SvROK(records_sv) || SvTYPE(SvRV(records_sv)) != SVt_PVAV) 0 16 16
4385 0 croak("produce_batch: expected arrayref of records");
4386 16 AV *records_av = (AV*)SvRV(records_sv);
4387  
4388 16 int16_t acks = 1;
4389 16 int compression = COMPRESS_NONE;
4390 16 int64_t producer_id = -1;
4391 16 int16_t producer_epoch = -1;
4392 16 int32_t base_sequence = -1;
4393 16 const char *txn_id = NULL;
4394 16 STRLEN txn_id_len = 0;
4395  
4396 32 ▶ if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 16 0 16
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 16 0 16
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 16 0 16
4397 16 HV *opts = (HV*)SvRV(opts_sv);
4398 SV **tmp;
4399 16 ▶ if ((tmp = hv_fetch(opts, "acks", 4, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "acks", 4, 0))) 16 0 16
4400 16 acks = (int16_t)SvIV(*tmp);
4401 16 ▶ if ((tmp = hv_fetch(opts, "transactional_id", 16, 0)) && SvOK(*tmp))
Branch: 2 branches
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "transactional_id", 16, 0)) && SvOK(*tmp)) 3 13 16
if ((tmp = hv_fetch(opts, "transactional_id", 16, 0)) && SvOK(*tmp)) 3 0 3
4402 6 txn_id = SvPV(*tmp, txn_id_len);
4403 16 ▶ if ((tmp = hv_fetch(opts, "compression", 11, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "compression", 11, 0))) { 0 16 16
4404 STRLEN clen;
4405 0 const char *cstr = SvPV(*tmp, clen);
4406 0 ▶ if (clen == 4 && memcmp(cstr, "none", 4) == 0) compression = COMPRESS_NONE;
Branch: 2 branches
BranchTrueFalseTotal
if (clen == 4 && memcmp(cstr, "none", 4) == 0) compression = COMPRESS_NONE; 0 0 0
if (clen == 4 && memcmp(cstr, "none", 4) == 0) compression = COMPRESS_NONE; 0 0 0
4407 #ifdef HAVE_LZ4
4408 else if (clen == 3 && memcmp(cstr, "lz4", 3) == 0) compression = COMPRESS_LZ4;
4409 #endif
4410 #ifdef HAVE_ZLIB
4411 0 ▶ else if (clen == 4 && memcmp(cstr, "gzip", 4) == 0) compression = COMPRESS_GZIP;
Branch: 2 branches
BranchTrueFalseTotal
else if (clen == 4 && memcmp(cstr, "gzip", 4) == 0) compression = COMPRESS_GZIP; 0 0 0
else if (clen == 4 && memcmp(cstr, "gzip", 4) == 0) compression = COMPRESS_GZIP; 0 0 0
4412 #endif
4413 #ifdef HAVE_ZSTD
4414 else if (clen == 4 && memcmp(cstr, "zstd", 4) == 0) compression = COMPRESS_ZSTD;
4415 #endif
4416 #ifdef HAVE_SNAPPY
4417 else if (clen == 6 && memcmp(cstr, "snappy", 6) == 0) compression = COMPRESS_SNAPPY;
4418 #endif
4419 0 else croak("unsupported compression: %.*s", (int)clen, cstr);
4420 }
4421 16 ▶ if ((tmp = hv_fetch(opts, "producer_id", 11, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "producer_id", 11, 0))) 9 7 16
4422 9 producer_id = (int64_t)SvIV(*tmp);
4423 16 ▶ if ((tmp = hv_fetch(opts, "producer_epoch", 14, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "producer_epoch", 14, 0))) 9 7 16
4424 9 producer_epoch = (int16_t)SvIV(*tmp);
4425 16 ▶ if ((tmp = hv_fetch(opts, "base_sequence", 13, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "base_sequence", 13, 0))) 9 7 16
4426 9 base_sequence = (int32_t)SvIV(*tmp);
4427 0 ▶ } else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) { 0 0 0
} else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) { 0 0 0
} else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) { 0 0 0
4428 0 cb = opts_sv;
4429 0 opts_sv = NULL;
4430 }
4431  
4432 struct timeval tv;
4433 16 gettimeofday(&tv, NULL);
4434 16 int64_t timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
4435  
4436 kf_buf_t batch;
4437 16 kf_encode_record_batch_multi(aTHX_ &batch, records_av, timestamp,
4438 compression, producer_id, producer_epoch, base_sequence,
4439 txn_id != NULL ? 1 : 0);
4440  
4441 16 int16_t ver = self->api_versions[API_PRODUCE];
4442 16 ▶ if (ver < 0) ver = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 3; 0 16 16
4443 16 ▶ if (ver > 7) ver = 7;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 7) ver = 7; 0 16 16
4444  
4445 16 STRLEN topic_len = strlen(topic);
4446  
4447 kf_buf_t body;
4448 16 kf_buf_init(&body);
4449  
4450 16 ▶ if (ver >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 3) 16 0 16
4451 16 ▶ kf_buf_append_nullable_string(&body, txn_id, txn_id ? (int16_t)txn_id_len : 0);
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_nullable_string(&body, txn_id, txn_id ? (int16_t)txn_id_len : 0); 3 13 16
4452  
4453 16 kf_buf_append_i16(&body, acks);
4454 16 kf_buf_append_i32(&body, 30000);
4455  
4456 16 kf_buf_append_i32(&body, 1);
4457 16 kf_buf_append_string(&body, topic, (int16_t)topic_len);
4458 16 kf_buf_append_i32(&body, 1);
4459 16 kf_buf_append_i32(&body, (int32_t)partition);
4460 16 kf_buf_append_i32(&body, (int32_t)batch.len);
4461 16 kf_buf_append(&body, batch.data, batch.len);
4462  
4463 32 ▶ conn_send_request(aTHX_ self, API_PRODUCE, ver, &body,
Branch: 1 branch
BranchTrueFalseTotal
conn_send_request(aTHX_ self, API_PRODUCE, ver, &body, 16 0 16
4464 16 ▶ (cb && SvOK(cb)) ? cb : NULL, 0, (acks == 0) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
(cb && SvOK(cb)) ? cb : NULL, 0, (acks == 0) ? 1 : 0); 16 0 16
4465  
4466 16 kf_buf_free(&body);
4467 16 kf_buf_free(&batch);
4468 }
4469  
4470 void
4471 list_offsets(EV::Kafka::Conn self, const char *topic, int partition, SV *timestamp_sv, SV *cb)
4472 CODE:
4473 {
4474 5 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 5 5
4475 5 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 5 5
4476 0 croak("not connected");
4477  
4478 5 int64_t timestamp = SvIV(timestamp_sv);
4479 5 STRLEN topic_len = strlen(topic);
4480  
4481 /* -2 = earliest, -1 = latest */
4482 5 int16_t ver = self->api_versions[API_LIST_OFFSETS];
4483 5 ▶ if (ver < 0) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 1; 0 5 5
4484 5 ▶ if (ver > 5) ver = 5;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 5) ver = 5; 0 5 5
4485  
4486 kf_buf_t body;
4487 5 kf_buf_init(&body);
4488  
4489 5 kf_buf_append_i32(&body, -1); /* replica_id */
4490  
4491 /* isolation_level (v2+) */
4492 5 ▶ if (ver >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 2) 0 5 5
4493 0 kf_buf_append_i8(&body, 0);
4494  
4495 /* topics: ARRAY(1) */
4496 5 kf_buf_append_i32(&body, 1);
4497 5 kf_buf_append_string(&body, topic, (int16_t)topic_len);
4498  
4499 /* partitions: ARRAY(1) */
4500 5 kf_buf_append_i32(&body, 1);
4501 5 kf_buf_append_i32(&body, (int32_t)partition);
4502  
4503 /* current_leader_epoch (v4+) */
4504 5 ▶ if (ver >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 4) 0 5 5
4505 0 kf_buf_append_i32(&body, -1);
4506  
4507 5 kf_buf_append_i64(&body, timestamp);
4508  
4509 5 conn_send_request(aTHX_ self, API_LIST_OFFSETS, ver, &body, cb, 0, 0);
4510 5 kf_buf_free(&body);
4511 }
4512  
4513 void
4514 produce(EV::Kafka::Conn self, const char *topic, int partition, SV *key_sv, SV *value_sv, SV *opts_sv = NULL, SV *cb = NULL)
4515 CODE:
4516 {
4517 4 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 4 4
4518 4 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 1 3 4
4519 1 croak("not connected");
4520  
4521 /* Handle optional opts hash: produce($topic, $part, $key, $val, \%opts, $cb)
4522 * or produce($topic, $part, $key, $val, $cb)
4523 */
4524 3 HV *headers = NULL;
4525 3 int16_t acks = 1;
4526 3 int compression = COMPRESS_NONE;
4527  
4528 4 ▶ if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 3 0 3
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 3 0 3
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 1 2 3
4529 1 HV *opts = (HV*)SvRV(opts_sv);
4530 SV **tmp;
4531 1 ▶ if ((tmp = hv_fetch(opts, "headers", 7, 0)) && SvROK(*tmp) && SvTYPE(SvRV(*tmp)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "headers", 7, 0)) && SvROK(*tmp) && SvTYPE(SvRV(*tmp)) == SVt_PVHV) 0 1 1
if ((tmp = hv_fetch(opts, "headers", 7, 0)) && SvROK(*tmp) && SvTYPE(SvRV(*tmp)) == SVt_PVHV) 0 0 0
if ((tmp = hv_fetch(opts, "headers", 7, 0)) && SvROK(*tmp) && SvTYPE(SvRV(*tmp)) == SVt_PVHV) 0 0 0
4532 0 headers = (HV*)SvRV(*tmp);
4533 1 ▶ if ((tmp = hv_fetch(opts, "acks", 4, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "acks", 4, 0))) 1 0 1
4534 1 acks = (int16_t)SvIV(*tmp);
4535 1 ▶ if ((tmp = hv_fetch(opts, "compression", 11, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "compression", 11, 0))) { 0 1 1
4536 STRLEN clen;
4537 0 const char *cstr = SvPV(*tmp, clen);
4538 0 ▶ if (clen == 4 && memcmp(cstr, "none", 4) == 0) compression = COMPRESS_NONE;
Branch: 2 branches
BranchTrueFalseTotal
if (clen == 4 && memcmp(cstr, "none", 4) == 0) compression = COMPRESS_NONE; 0 0 0
if (clen == 4 && memcmp(cstr, "none", 4) == 0) compression = COMPRESS_NONE; 0 0 0
4539 #ifdef HAVE_LZ4
4540 else if (clen == 3 && memcmp(cstr, "lz4", 3) == 0) compression = COMPRESS_LZ4;
4541 #endif
4542 #ifdef HAVE_ZLIB
4543 0 ▶ else if (clen == 4 && memcmp(cstr, "gzip", 4) == 0) compression = COMPRESS_GZIP;
Branch: 2 branches
BranchTrueFalseTotal
else if (clen == 4 && memcmp(cstr, "gzip", 4) == 0) compression = COMPRESS_GZIP; 0 0 0
else if (clen == 4 && memcmp(cstr, "gzip", 4) == 0) compression = COMPRESS_GZIP; 0 0 0
4544 #endif
4545 #ifdef HAVE_ZSTD
4546 else if (clen == 4 && memcmp(cstr, "zstd", 4) == 0) compression = COMPRESS_ZSTD;
4547 #endif
4548 #ifdef HAVE_SNAPPY
4549 else if (clen == 6 && memcmp(cstr, "snappy", 6) == 0) compression = COMPRESS_SNAPPY;
4550 #endif
4551 0 else croak("unsupported compression: %.*s", (int)clen, cstr);
4552 }
4553 2 ▶ } else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) { 2 0 2
} else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) { 2 0 2
} else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) { 2 0 2
4554 /* opts_sv is actually the callback */
4555 2 cb = opts_sv;
4556 2 opts_sv = NULL;
4557 }
4558  
4559 3 const char *key = NULL;
4560 3 STRLEN key_len = 0;
4561 3 ▶ if (SvOK(key_sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(key_sv)) 3 0 3
4562 3 key = SvPV(key_sv, key_len);
4563  
4564 3 const char *value = NULL;
4565 3 STRLEN value_len = 0;
4566 3 ▶ if (SvOK(value_sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(value_sv)) 3 0 3
4567 3 value = SvPV(value_sv, value_len);
4568  
4569 /* Build RecordBatch */
4570 int64_t timestamp;
4571 {
4572 3 SV **ts_tmp = NULL;
4573 3 ▶ if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) 1 2 3
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) 1 0 1
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) 1 0 1
4574 1 ts_tmp = hv_fetch((HV*)SvRV(opts_sv), "timestamp", 9, 0);
4575 3 ▶ if (ts_tmp && SvOK(*ts_tmp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ts_tmp && SvOK(*ts_tmp)) { 0 3 3
if (ts_tmp && SvOK(*ts_tmp)) { 0 0 0
4576 0 timestamp = (int64_t)SvIV(*ts_tmp);
4577 } else {
4578 struct timeval tv;
4579 3 gettimeofday(&tv, NULL);
4580 3 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
4581 }
4582 }
4583  
4584 /* Build a single-element AV and dispatch to the multi encoder so
4585 * the compression / CRC framing logic lives in one place. */
4586 kf_buf_t batch;
4587 {
4588 3 HV *rec = newHV();
4589 3 ▶ if (key) hv_stores(rec, "key", newSVpvn(key, key_len));
Branch: 1 branch
BranchTrueFalseTotal
if (key) hv_stores(rec, "key", newSVpvn(key, key_len)); 3 0 3
4590 3 ▶ if (value) hv_stores(rec, "value", newSVpvn(value, value_len));
Branch: 1 branch
BranchTrueFalseTotal
if (value) hv_stores(rec, "value", newSVpvn(value, value_len)); 3 0 3
4591 3 ▶ if (headers) hv_stores(rec, "headers", newRV_inc((SV*)headers));
Branch: 1 branch
BranchTrueFalseTotal
if (headers) hv_stores(rec, "headers", newRV_inc((SV*)headers)); 0 3 3
4592 3 AV *records_av = newAV();
4593 3 av_push(records_av, newRV_noinc((SV*)rec));
4594 3 kf_encode_record_batch_multi(aTHX_ &batch, records_av, timestamp,
4595 compression, -1, -1, -1, 0);
4596 3 SvREFCNT_dec((SV*)records_av);
4597 }
4598  
4599 /* Use Produce version — cap at v7 */
4600 3 int16_t ver = self->api_versions[API_PRODUCE];
4601 3 ▶ if (ver < 0) ver = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 3; 0 3 3
4602 3 ▶ if (ver > 7) ver = 7;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 7) ver = 7; 0 3 3
4603  
4604 3 STRLEN topic_len = strlen(topic);
4605  
4606 kf_buf_t body;
4607 3 kf_buf_init(&body);
4608  
4609 /* transactional_id (v3+): nullable string = null */
4610 3 ▶ if (ver >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 3) 3 0 3
4611 3 kf_buf_append_nullable_string(&body, NULL, 0);
4612  
4613 3 kf_buf_append_i16(&body, acks); /* acks */
4614 3 kf_buf_append_i32(&body, 30000); /* timeout_ms = 30s */
4615  
4616 /* topic_data: ARRAY(1) */
4617 3 kf_buf_append_i32(&body, 1); /* 1 topic */
4618 3 kf_buf_append_string(&body, topic, (int16_t)topic_len);
4619  
4620 /* partition_data: ARRAY(1) */
4621 3 kf_buf_append_i32(&body, 1); /* 1 partition */
4622 3 kf_buf_append_i32(&body, partition);
4623  
4624 /* record_set: BYTES (i32 length + record_batch) */
4625 3 kf_buf_append_i32(&body, (int32_t)batch.len);
4626 3 kf_buf_append(&body, batch.data, batch.len);
4627  
4628 6 ▶ conn_send_request(aTHX_ self, API_PRODUCE, ver, &body,
Branch: 1 branch
BranchTrueFalseTotal
conn_send_request(aTHX_ self, API_PRODUCE, ver, &body, 3 0 3
4629 3 ▶ (cb && SvOK(cb)) ? cb : NULL, 0, (acks == 0) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
(cb && SvOK(cb)) ? cb : NULL, 0, (acks == 0) ? 1 : 0); 3 0 3
4630  
4631 3 kf_buf_free(&body);
4632 3 kf_buf_free(&batch);
4633 }
4634  
4635 void
4636 find_coordinator(EV::Kafka::Conn self, const char *group_id, SV *cb, int key_type = 0)
4637 CODE:
4638 {
4639 9 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 9 9
4640 9 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 9 9
4641 0 croak("not connected");
4642  
4643 9 int16_t ver = self->api_versions[API_FIND_COORDINATOR];
4644 9 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 9 9
4645 9 ▶ if (ver > 2) ver = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 2) ver = 2; 0 9 9
4646  
4647 kf_buf_t body;
4648 9 kf_buf_init(&body);
4649  
4650 9 STRLEN glen = strlen(group_id);
4651 9 kf_buf_append_string(&body, group_id, (int16_t)glen);
4652  
4653 /* key_type (v1+): 0=group, 1=transaction */
4654 9 ▶ if (ver >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 1) 9 0 9
4655 9 kf_buf_append_i8(&body, (int8_t)key_type);
4656  
4657 9 conn_send_request(aTHX_ self, API_FIND_COORDINATOR, ver, &body, cb, 0, 0);
4658 9 kf_buf_free(&body);
4659 }
4660  
4661 void
4662 join_group(EV::Kafka::Conn self, const char *group_id, const char *member_id, SV *topics_sv, SV *cb, int session_timeout_ms = 30000, int rebalance_timeout_ms = 60000, SV *group_instance_id_sv = NULL)
4663 CODE:
4664 {
4665 9 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 9 9
4666 9 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 9 9
4667 0 croak("not connected");
4668  
4669 9 ▶ if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 8 1 9
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 0 8 8
4670 1 croak("join_group: expected arrayref");
4671  
4672 8 int16_t ver = self->api_versions[API_JOIN_GROUP];
4673 8 ▶ if (ver < 0) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 1; 0 8 8
4674 8 ▶ if (ver > 5) ver = 5;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 5) ver = 5; 0 8 8
4675  
4676 kf_buf_t body;
4677 8 kf_buf_init(&body);
4678  
4679 8 STRLEN glen = strlen(group_id);
4680 8 kf_buf_append_string(&body, group_id, (int16_t)glen);
4681 8 kf_buf_append_i32(&body, session_timeout_ms);
4682  
4683 /* rebalance_timeout_ms (v1+) */
4684 8 ▶ if (ver >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 1) 8 0 8
4685 8 kf_buf_append_i32(&body, rebalance_timeout_ms);
4686  
4687 8 STRLEN mlen = strlen(member_id);
4688 8 kf_buf_append_string(&body, member_id, (int16_t)mlen);
4689  
4690 /* group_instance_id (v5+) */
4691 8 ▶ if (ver >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 5) { 0 8 8
4692 0 ▶ if (group_instance_id_sv && SvOK(group_instance_id_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (group_instance_id_sv && SvOK(group_instance_id_sv)) { 0 0 0
if (group_instance_id_sv && SvOK(group_instance_id_sv)) { 0 0 0
4693 STRLEN gilen;
4694 0 const char *gi = SvPV(group_instance_id_sv, gilen);
4695 0 kf_buf_append_nullable_string(&body, gi, (int16_t)gilen);
4696 } else {
4697 0 kf_buf_append_nullable_string(&body, NULL, 0);
4698 }
4699 }
4700  
4701 /* protocol_type = "consumer" */
4702 8 kf_buf_append_string(&body, "consumer", 8);
4703  
4704 /* protocols: ARRAY(1) */
4705 8 kf_buf_append_i32(&body, 1);
4706  
4707 /* protocol name = "sticky" */
4708 8 kf_buf_append_string(&body, "sticky", 6);
4709  
4710 /* protocol metadata (ConsumerProtocol subscription) */
4711 /* Version:0, Topics:array, UserData:null */
4712 kf_buf_t meta;
4713 8 kf_buf_init(&meta);
4714 8 kf_buf_append_i16(&meta, 0); /* version */
4715  
4716 8 AV *topics = (AV*)SvRV(topics_sv);
4717 8 SSize_t i, tc = av_len(topics) + 1;
4718 8 kf_buf_append_i32(&meta, (int32_t)tc);
4719 16 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 8 8 16
4720 8 SV **elem = av_fetch(topics, i, 0);
4721 STRLEN tlen;
4722 8 const char *tname = SvPV(*elem, tlen);
4723 8 kf_buf_append_string(&meta, tname, (int16_t)tlen);
4724 }
4725 8 kf_buf_append_nullable_bytes(&meta, NULL, 0); /* user_data = null */
4726  
4727 8 kf_buf_append_bytes(&body, meta.data, (int32_t)meta.len);
4728 8 kf_buf_free(&meta);
4729  
4730 8 conn_send_request(aTHX_ self, API_JOIN_GROUP, ver, &body, cb, 0, 0);
4731 8 kf_buf_free(&body);
4732 }
4733  
4734 void
4735 sync_group(EV::Kafka::Conn self, const char *group_id, int generation_id, const char *member_id, SV *assignments_sv, SV *cb, SV *group_instance_id_sv = NULL)
4736 CODE:
4737 {
4738 4 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 4 4
4739 4 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 4 4
4740 0 croak("not connected");
4741  
4742 4 int16_t ver = self->api_versions[API_SYNC_GROUP];
4743 4 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 4 4
4744 4 ▶ if (ver > 3) ver = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 3) ver = 3; 0 4 4
4745  
4746 kf_buf_t body;
4747 4 kf_buf_init(&body);
4748  
4749 4 STRLEN glen = strlen(group_id);
4750 4 kf_buf_append_string(&body, group_id, (int16_t)glen);
4751 4 kf_buf_append_i32(&body, generation_id);
4752  
4753 4 STRLEN mlen = strlen(member_id);
4754 4 kf_buf_append_string(&body, member_id, (int16_t)mlen);
4755  
4756 /* group_instance_id (v3+) */
4757 4 ▶ if (ver >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 3) { 0 4 4
4758 0 ▶ if (group_instance_id_sv && SvOK(group_instance_id_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (group_instance_id_sv && SvOK(group_instance_id_sv)) { 0 0 0
if (group_instance_id_sv && SvOK(group_instance_id_sv)) { 0 0 0
4759 STRLEN gilen;
4760 0 const char *gi = SvPV(group_instance_id_sv, gilen);
4761 0 kf_buf_append_nullable_string(&body, gi, (int16_t)gilen);
4762 } else {
4763 0 kf_buf_append_nullable_string(&body, NULL, 0);
4764 }
4765 }
4766  
4767 /* assignments: ARRAY of {member_id, assignment_bytes} */
4768 4 ▶ if (SvOK(assignments_sv) && SvROK(assignments_sv)
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(assignments_sv) && SvROK(assignments_sv) 4 0 4
if (SvOK(assignments_sv) && SvROK(assignments_sv) 4 0 4
4769 8 ▶ && SvTYPE(SvRV(assignments_sv)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(assignments_sv)) == SVt_PVAV) { 4 0 4
4770 4 AV *assigns = (AV*)SvRV(assignments_sv);
4771 4 SSize_t i, ac = av_len(assigns) + 1;
4772 4 kf_buf_append_i32(&body, (int32_t)ac);
4773  
4774 8 ▶ for (i = 0; i < ac; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ac; i++) { 4 4 8
4775 4 SV **elem = av_fetch(assigns, i, 0);
4776 4 ▶ if (!elem || !SvROK(*elem)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!elem || !SvROK(*elem)) continue; 4 0 4
if (!elem || !SvROK(*elem)) continue; 0 4 4
4777 4 HV *ah = (HV*)SvRV(*elem);
4778  
4779 4 SV **mid_sv = hv_fetch(ah, "member_id", 9, 0);
4780 4 ▶ if (!mid_sv) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!mid_sv) continue; 0 4 4
4781 STRLEN mid_len;
4782 4 const char *mid = SvPV(*mid_sv, mid_len);
4783 4 kf_buf_append_string(&body, mid, (int16_t)mid_len);
4784  
4785 4 SV **data_sv = hv_fetch(ah, "assignment", 10, 0);
4786 4 ▶ if (!data_sv) { kf_buf_append_bytes(&body, NULL, 0); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!data_sv) { kf_buf_append_bytes(&body, NULL, 0); continue; } 0 4 4
4787 STRLEN dlen;
4788 4 const char *ddata = SvPV(*data_sv, dlen);
4789 4 kf_buf_append_bytes(&body, ddata, (int32_t)dlen);
4790 }
4791 } else {
4792 0 kf_buf_append_i32(&body, 0); /* empty array */
4793 }
4794  
4795 4 conn_send_request(aTHX_ self, API_SYNC_GROUP, ver, &body, cb, 0, 0);
4796 4 kf_buf_free(&body);
4797 }
4798  
4799 void
4800 heartbeat(EV::Kafka::Conn self, const char *group_id, int generation_id, const char *member_id, SV *cb, SV *group_instance_id_sv = NULL)
4801 CODE:
4802 {
4803 1 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 1 1
4804 1 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 1 1
4805 0 croak("not connected");
4806  
4807 1 int16_t ver = self->api_versions[API_HEARTBEAT];
4808 1 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 1 1
4809 1 ▶ if (ver > 4) ver = 4;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 4) ver = 4; 0 1 1
4810  
4811 kf_buf_t body;
4812 1 kf_buf_init(&body);
4813  
4814 1 STRLEN glen = strlen(group_id);
4815 1 kf_buf_append_string(&body, group_id, (int16_t)glen);
4816 1 kf_buf_append_i32(&body, generation_id);
4817  
4818 1 STRLEN mlen = strlen(member_id);
4819 1 kf_buf_append_string(&body, member_id, (int16_t)mlen);
4820  
4821 /* group_instance_id (v3+) */
4822 1 ▶ if (ver >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 3) { 0 1 1
4823 0 ▶ if (group_instance_id_sv && SvOK(group_instance_id_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (group_instance_id_sv && SvOK(group_instance_id_sv)) { 0 0 0
if (group_instance_id_sv && SvOK(group_instance_id_sv)) { 0 0 0
4824 STRLEN gilen;
4825 0 const char *gi = SvPV(group_instance_id_sv, gilen);
4826 0 kf_buf_append_nullable_string(&body, gi, (int16_t)gilen);
4827 } else {
4828 0 kf_buf_append_nullable_string(&body, NULL, 0);
4829 }
4830 }
4831  
4832 1 conn_send_request(aTHX_ self, API_HEARTBEAT, ver, &body, cb, 0, 0);
4833 1 kf_buf_free(&body);
4834 }
4835  
4836 void
4837 offset_commit(EV::Kafka::Conn self, const char *group_id, int generation_id, const char *member_id, SV *offsets_sv, SV *cb)
4838 CODE:
4839 {
4840 3 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 3 3
4841 3 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 3 3
4842 0 croak("not connected");
4843  
4844 3 ▶ if (!SvROK(offsets_sv) || SvTYPE(SvRV(offsets_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(offsets_sv) || SvTYPE(SvRV(offsets_sv)) != SVt_PVAV) 2 1 3
if (!SvROK(offsets_sv) || SvTYPE(SvRV(offsets_sv)) != SVt_PVAV) 0 2 2
4845 1 croak("offset_commit: expected arrayref");
4846  
4847 2 int16_t ver = self->api_versions[API_OFFSET_COMMIT];
4848 2 ▶ if (ver < 0) ver = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 2; 0 2 2
4849 2 ▶ if (ver > 7) ver = 7;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 7) ver = 7; 0 2 2
4850  
4851 kf_buf_t body;
4852 2 kf_buf_init(&body);
4853  
4854 2 STRLEN glen = strlen(group_id);
4855 2 kf_buf_append_string(&body, group_id, (int16_t)glen);
4856  
4857 /* generation_id (v1+) */
4858 2 ▶ if (ver >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 1) 2 0 2
4859 2 kf_buf_append_i32(&body, generation_id);
4860  
4861 /* member_id (v1+) */
4862 2 ▶ if (ver >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 1) { 2 0 2
4863 2 STRLEN mlen = strlen(member_id);
4864 2 kf_buf_append_string(&body, member_id, (int16_t)mlen);
4865 }
4866  
4867 /* group_instance_id (v7+): null */
4868 2 ▶ if (ver >= 7)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 7) 0 2 2
4869 0 kf_buf_append_nullable_string(&body, NULL, 0);
4870  
4871 /* topics: ARRAY of {topic, partitions: [{partition, committed_offset, metadata}]} */
4872 2 AV *topics = (AV*)SvRV(offsets_sv);
4873 2 SSize_t i, tc = av_len(topics) + 1;
4874 2 kf_buf_append_i32(&body, (int32_t)tc);
4875  
4876 4 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 2 2 4
4877 2 SV **elem = av_fetch(topics, i, 0);
4878 2 ▶ if (!elem || !SvROK(*elem)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!elem || !SvROK(*elem)) continue; 2 0 2
if (!elem || !SvROK(*elem)) continue; 0 2 2
4879 2 HV *th = (HV*)SvRV(*elem);
4880 2 SV **tname_sv = hv_fetch(th, "topic", 5, 0);
4881 2 ▶ if (!tname_sv) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!tname_sv) continue; 0 2 2
4882 STRLEN tnlen;
4883 2 const char *tname = SvPV(*tname_sv, tnlen);
4884 2 kf_buf_append_string(&body, tname, (int16_t)tnlen);
4885  
4886 2 SV **parts_sv = hv_fetch(th, "partitions", 10, 0);
4887 2 ▶ if (!parts_sv || !SvROK(*parts_sv)) { kf_buf_append_i32(&body, 0); continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (!parts_sv || !SvROK(*parts_sv)) { kf_buf_append_i32(&body, 0); continue; } 2 0 2
if (!parts_sv || !SvROK(*parts_sv)) { kf_buf_append_i32(&body, 0); continue; } 0 2 2
4888 2 AV *parts = (AV*)SvRV(*parts_sv);
4889 2 SSize_t j, pc = av_len(parts) + 1;
4890 2 kf_buf_append_i32(&body, (int32_t)pc);
4891  
4892 4 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 2 2 4
4893 2 SV **pelem = av_fetch(parts, j, 0);
4894 2 ▶ if (!pelem || !SvROK(*pelem)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!pelem || !SvROK(*pelem)) continue; 2 0 2
if (!pelem || !SvROK(*pelem)) continue; 0 2 2
4895 2 HV *ph = (HV*)SvRV(*pelem);
4896  
4897 2 SV **pid_sv = hv_fetch(ph, "partition", 9, 0);
4898 2 ▶ kf_buf_append_i32(&body, pid_sv ? (int32_t)SvIV(*pid_sv) : 0);
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_i32(&body, pid_sv ? (int32_t)SvIV(*pid_sv) : 0); 2 0 2
4899  
4900 2 SV **off_sv = hv_fetch(ph, "offset", 6, 0);
4901 2 ▶ kf_buf_append_i64(&body, off_sv ? (int64_t)SvIV(*off_sv) : 0);
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_i64(&body, off_sv ? (int64_t)SvIV(*off_sv) : 0); 2 0 2
4902  
4903 /* leader_epoch (v6+) */
4904 2 ▶ if (ver >= 6)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 6) 0 2 2
4905 0 kf_buf_append_i32(&body, -1);
4906  
4907 /* metadata: nullable string = empty */
4908 2 kf_buf_append_nullable_string(&body, "", 0);
4909 }
4910 }
4911  
4912 2 conn_send_request(aTHX_ self, API_OFFSET_COMMIT, ver, &body, cb, 0, 0);
4913 2 kf_buf_free(&body);
4914 }
4915  
4916 void
4917 offset_fetch(EV::Kafka::Conn self, const char *group_id, SV *topics_sv, SV *cb)
4918 CODE:
4919 {
4920 5 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 5 5
4921 5 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 5 5
4922 0 croak("not connected");
4923  
4924 5 ▶ if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 4 1 5
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 0 4 4
4925 1 croak("offset_fetch: expected arrayref");
4926  
4927 4 int16_t ver = self->api_versions[API_OFFSET_FETCH];
4928 4 ▶ if (ver < 0) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 1; 0 4 4
4929 4 ▶ if (ver > 5) ver = 5;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 5) ver = 5; 0 4 4
4930  
4931 kf_buf_t body;
4932 4 kf_buf_init(&body);
4933  
4934 4 STRLEN glen = strlen(group_id);
4935 4 kf_buf_append_string(&body, group_id, (int16_t)glen);
4936  
4937 /* topics: ARRAY */
4938 4 AV *topics = (AV*)SvRV(topics_sv);
4939 4 SSize_t i, tc = av_len(topics) + 1;
4940 4 kf_buf_append_i32(&body, (int32_t)tc);
4941  
4942 8 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 4 4 8
4943 4 SV **elem = av_fetch(topics, i, 0);
4944 4 ▶ if (!elem || !SvROK(*elem)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!elem || !SvROK(*elem)) continue; 4 0 4
if (!elem || !SvROK(*elem)) continue; 0 4 4
4945 4 HV *th = (HV*)SvRV(*elem);
4946 4 SV **tname_sv = hv_fetch(th, "topic", 5, 0);
4947 4 ▶ if (!tname_sv) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!tname_sv) continue; 0 4 4
4948 STRLEN tnlen;
4949 4 const char *tname = SvPV(*tname_sv, tnlen);
4950 4 kf_buf_append_string(&body, tname, (int16_t)tnlen);
4951  
4952 4 SV **parts_sv = hv_fetch(th, "partitions", 10, 0);
4953 4 ▶ if (!parts_sv || !SvROK(*parts_sv)) { kf_buf_append_i32(&body, 0); continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (!parts_sv || !SvROK(*parts_sv)) { kf_buf_append_i32(&body, 0); continue; } 4 0 4
if (!parts_sv || !SvROK(*parts_sv)) { kf_buf_append_i32(&body, 0); continue; } 0 4 4
4954 4 AV *parts = (AV*)SvRV(*parts_sv);
4955 4 SSize_t j, pc = av_len(parts) + 1;
4956 4 kf_buf_append_i32(&body, (int32_t)pc);
4957  
4958 8 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 4 4 8
4959 4 SV **pelem = av_fetch(parts, j, 0);
4960 4 ▶ kf_buf_append_i32(&body, pelem ? (int32_t)SvIV(*pelem) : 0);
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_i32(&body, pelem ? (int32_t)SvIV(*pelem) : 0); 4 0 4
4961 }
4962 }
4963  
4964 4 conn_send_request(aTHX_ self, API_OFFSET_FETCH, ver, &body, cb, 0, 0);
4965 4 kf_buf_free(&body);
4966 }
4967  
4968 void
4969 leave_group(EV::Kafka::Conn self, const char *group_id, const char *member_id, SV *cb)
4970 CODE:
4971 {
4972 1 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 1 1
4973 1 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 1 1
4974 0 croak("not connected");
4975  
4976 1 int16_t ver = self->api_versions[API_LEAVE_GROUP];
4977 1 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 1 1
4978 1 ▶ if (ver > 3) ver = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 3) ver = 3; 0 1 1
4979  
4980 kf_buf_t body;
4981 1 kf_buf_init(&body);
4982  
4983 1 STRLEN glen = strlen(group_id);
4984 1 kf_buf_append_string(&body, group_id, (int16_t)glen);
4985  
4986 1 STRLEN mlen = strlen(member_id);
4987 1 kf_buf_append_string(&body, member_id, (int16_t)mlen);
4988  
4989 1 conn_send_request(aTHX_ self, API_LEAVE_GROUP, ver, &body, cb, 0, 0);
4990 1 kf_buf_free(&body);
4991 }
4992  
4993 void
4994 create_topics(EV::Kafka::Conn self, SV *topics_sv, int timeout_ms, SV *cb)
4995 CODE:
4996 {
4997 1 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 1 1
4998 1 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 1 1
4999 0 croak("not connected");
5000  
5001 1 ▶ if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 0 1 1
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 0 0 0
5002 1 croak("create_topics: expected arrayref");
5003  
5004 0 int16_t ver = self->api_versions[API_CREATE_TOPICS];
5005 0 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 0 0
5006 0 ▶ if (ver > 4) ver = 4;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 4) ver = 4; 0 0 0
5007  
5008 kf_buf_t body;
5009 0 kf_buf_init(&body);
5010  
5011 0 AV *topics = (AV*)SvRV(topics_sv);
5012 0 SSize_t i, tc = av_len(topics) + 1;
5013 0 kf_buf_append_i32(&body, (int32_t)tc);
5014  
5015 0 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 0 0 0
5016 0 SV **elem = av_fetch(topics, i, 0);
5017 0 ▶ if (!elem || !SvROK(*elem)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!elem || !SvROK(*elem)) continue; 0 0 0
if (!elem || !SvROK(*elem)) continue; 0 0 0
5018 0 HV *th = (HV*)SvRV(*elem);
5019  
5020 0 SV **name_sv = hv_fetch(th, "name", 4, 0);
5021 0 ▶ if (!name_sv) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!name_sv) continue; 0 0 0
5022 STRLEN nlen;
5023 0 const char *name = SvPV(*name_sv, nlen);
5024 0 kf_buf_append_string(&body, name, (int16_t)nlen);
5025  
5026 0 SV **np_sv = hv_fetch(th, "num_partitions", 14, 0);
5027 0 ▶ int32_t num_partitions = np_sv ? (int32_t)SvIV(*np_sv) : 1;
Branch: 1 branch
BranchTrueFalseTotal
int32_t num_partitions = np_sv ? (int32_t)SvIV(*np_sv) : 1; 0 0 0
5028 0 kf_buf_append_i32(&body, num_partitions);
5029  
5030 0 SV **rf_sv = hv_fetch(th, "replication_factor", 18, 0);
5031 0 ▶ int16_t replication_factor = rf_sv ? (int16_t)SvIV(*rf_sv) : 1;
Branch: 1 branch
BranchTrueFalseTotal
int16_t replication_factor = rf_sv ? (int16_t)SvIV(*rf_sv) : 1; 0 0 0
5032 0 kf_buf_append_i16(&body, replication_factor);
5033  
5034 /* assignments: empty array */
5035 0 kf_buf_append_i32(&body, 0);
5036  
5037 /* configs: empty array */
5038 0 kf_buf_append_i32(&body, 0);
5039 }
5040  
5041 0 kf_buf_append_i32(&body, timeout_ms);
5042  
5043 /* validate_only (v1+) */
5044 0 ▶ if (ver >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 1) 0 0 0
5045 0 kf_buf_append_i8(&body, 0);
5046  
5047 0 conn_send_request(aTHX_ self, API_CREATE_TOPICS, ver, &body, cb, 0, 0);
5048 0 kf_buf_free(&body);
5049 }
5050  
5051 void
5052 delete_topics(EV::Kafka::Conn self, SV *topics_sv, int timeout_ms, SV *cb)
5053 CODE:
5054 {
5055 1 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 1 1
5056 1 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 1 1
5057 0 croak("not connected");
5058  
5059 1 ▶ if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 0 1 1
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 0 0 0
5060 1 croak("delete_topics: expected arrayref");
5061  
5062 0 int16_t ver = self->api_versions[API_DELETE_TOPICS];
5063 0 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 0 0
5064 0 ▶ if (ver > 3) ver = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 3) ver = 3; 0 0 0
5065  
5066 kf_buf_t body;
5067 0 kf_buf_init(&body);
5068  
5069 0 AV *topics = (AV*)SvRV(topics_sv);
5070 0 SSize_t i, tc = av_len(topics) + 1;
5071 0 kf_buf_append_i32(&body, (int32_t)tc);
5072  
5073 0 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 0 0 0
5074 0 SV **elem = av_fetch(topics, i, 0);
5075 0 ▶ if (!elem) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!elem) continue; 0 0 0
5076 STRLEN tlen;
5077 0 const char *tname = SvPV(*elem, tlen);
5078 0 kf_buf_append_string(&body, tname, (int16_t)tlen);
5079 }
5080  
5081 0 kf_buf_append_i32(&body, timeout_ms);
5082  
5083 0 conn_send_request(aTHX_ self, API_DELETE_TOPICS, ver, &body, cb, 0, 0);
5084 0 kf_buf_free(&body);
5085 }
5086  
5087 void
5088 init_producer_id(EV::Kafka::Conn self, SV *transactional_id_sv, int txn_timeout_ms, SV *cb)
5089 CODE:
5090 {
5091 10 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 10 10
5092 10 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 10 10
5093 0 croak("not connected");
5094  
5095 10 int16_t ver = self->api_versions[API_INIT_PRODUCER_ID];
5096 10 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 10 10
5097 10 ▶ if (ver > 1) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 1) ver = 1; 0 10 10
5098  
5099 kf_buf_t body;
5100 10 kf_buf_init(&body);
5101  
5102 10 ▶ if (SvOK(transactional_id_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(transactional_id_sv)) { 3 7 10
5103 STRLEN tlen;
5104 3 const char *tid = SvPV(transactional_id_sv, tlen);
5105 3 kf_buf_append_string(&body, tid, (int16_t)tlen);
5106 } else {
5107 7 kf_buf_append_nullable_string(&body, NULL, 0);
5108 }
5109  
5110 10 kf_buf_append_i32(&body, txn_timeout_ms);
5111  
5112 /* v2+: producer_id(i64=-1), producer_epoch(i16=-1) */
5113 10 ▶ if (ver >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 2) { 0 10 10
5114 0 kf_buf_append_i64(&body, -1);
5115 0 kf_buf_append_i16(&body, -1);
5116 }
5117  
5118 10 conn_send_request(aTHX_ self, API_INIT_PRODUCER_ID, ver, &body, cb, 0, 0);
5119 10 kf_buf_free(&body);
5120 }
5121  
5122 void
5123 add_partitions_to_txn(EV::Kafka::Conn self, const char *transactional_id, SV *producer_id_sv, int producer_epoch, SV *topics_sv, SV *cb)
5124 CODE:
5125 {
5126 20205 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 20205 20205
5127 20205 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 20205 20205
5128 0 croak("not connected");
5129  
5130 20205 int16_t ver = self->api_versions[API_ADD_PARTITIONS_TXN];
5131 20205 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 20202 3 20205
5132 20205 ▶ if (ver > 1) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 1) ver = 1; 0 20205 20205
5133  
5134 20205 int64_t pid = SvIV(producer_id_sv);
5135  
5136 20205 kf_validate_txn_topics("add_partitions_to_txn", topics_sv, 0);
5137  
5138 kf_buf_t body;
5139 4 kf_buf_init(&body);
5140  
5141 4 STRLEN tid_len = strlen(transactional_id);
5142 4 kf_buf_append_string(&body, transactional_id, (int16_t)tid_len);
5143 4 kf_buf_append_i64(&body, pid);
5144 4 kf_buf_append_i16(&body, (int16_t)producer_epoch);
5145  
5146 /* topics: ARRAY of {topic, partitions: ARRAY(i32)} */
5147 4 AV *topics = (AV*)SvRV(topics_sv); /* validated above */
5148 4 SSize_t i, tc = av_len(topics) + 1;
5149 4 kf_buf_append_i32(&body, (int32_t)tc);
5150  
5151 8 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 4 4 8
5152 4 SV **elem = av_fetch(topics, i, 0);
5153 4 HV *th = (HV*)SvRV(*elem);
5154 4 SV **tname_sv = hv_fetch(th, "topic", 5, 0);
5155 STRLEN tnlen;
5156 4 const char *tname = SvPV(*tname_sv, tnlen);
5157 4 kf_buf_append_string(&body, tname, (int16_t)tnlen);
5158  
5159 4 SV **parts_sv = hv_fetch(th, "partitions", 10, 0);
5160 4 AV *parts = (AV*)SvRV(*parts_sv); /* validated above */
5161 4 SSize_t j, pc = av_len(parts) + 1;
5162 4 kf_buf_append_i32(&body, (int32_t)pc);
5163 8 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 4 4 8
5164 4 SV **pelem = av_fetch(parts, j, 0);
5165 4 ▶ kf_buf_append_i32(&body, pelem ? (int32_t)SvIV(*pelem) : 0);
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_i32(&body, pelem ? (int32_t)SvIV(*pelem) : 0); 4 0 4
5166 }
5167 }
5168  
5169 4 conn_send_request(aTHX_ self, API_ADD_PARTITIONS_TXN, ver, &body, cb, 0, 0);
5170 4 kf_buf_free(&body);
5171 }
5172  
5173 void
5174 end_txn(EV::Kafka::Conn self, const char *transactional_id, SV *producer_id_sv, int producer_epoch, int committed, SV *cb)
5175 CODE:
5176 {
5177 3 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 3 3
5178 3 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 3 3
5179 0 croak("not connected");
5180  
5181 3 int16_t ver = self->api_versions[API_END_TXN];
5182 3 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 3 3
5183 3 ▶ if (ver > 1) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 1) ver = 1; 0 3 3
5184  
5185 3 int64_t pid = SvIV(producer_id_sv);
5186  
5187 kf_buf_t body;
5188 3 kf_buf_init(&body);
5189  
5190 3 STRLEN tid_len = strlen(transactional_id);
5191 3 kf_buf_append_string(&body, transactional_id, (int16_t)tid_len);
5192 3 kf_buf_append_i64(&body, pid);
5193 3 kf_buf_append_i16(&body, (int16_t)producer_epoch);
5194 3 kf_buf_append_i8(&body, committed ? 1 : 0);
5195  
5196 3 conn_send_request(aTHX_ self, API_END_TXN, ver, &body, cb, 0, 0);
5197 3 kf_buf_free(&body);
5198 }
5199  
5200 void
5201 txn_offset_commit(EV::Kafka::Conn self, const char *transactional_id, const char *group_id, SV *producer_id_sv, int producer_epoch, int generation_id, const char *member_id, SV *offsets_sv, SV *cb)
5202 CODE:
5203 {
5204 20203 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 20203 20203
5205 20203 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 20203 20203
5206 0 croak("not connected");
5207  
5208 20203 int16_t ver = self->api_versions[API_TXN_OFFSET_COMMIT];
5209 20203 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 20202 1 20203
5210 20203 ▶ if (ver > 3) ver = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 3) ver = 3; 0 20203 20203
5211  
5212 20203 int64_t pid = SvIV(producer_id_sv);
5213  
5214 20203 kf_validate_txn_topics("txn_offset_commit", offsets_sv, 1);
5215  
5216 kf_buf_t body;
5217 2 kf_buf_init(&body);
5218  
5219 2 STRLEN tid_len = strlen(transactional_id);
5220 2 kf_buf_append_string(&body, transactional_id, (int16_t)tid_len);
5221  
5222 2 STRLEN gid_len = strlen(group_id);
5223 2 kf_buf_append_string(&body, group_id, (int16_t)gid_len);
5224  
5225 2 kf_buf_append_i64(&body, pid);
5226 2 kf_buf_append_i16(&body, (int16_t)producer_epoch);
5227  
5228 /* v3+: generation_id, member_id, group_instance_id */
5229 2 ▶ if (ver >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 3) { 0 2 2
5230 0 kf_buf_append_i32(&body, generation_id);
5231 0 STRLEN mid_len = strlen(member_id);
5232 0 kf_buf_append_string(&body, member_id, (int16_t)mid_len);
5233 0 kf_buf_append_nullable_string(&body, NULL, 0); /* group_instance_id */
5234 }
5235  
5236 /* offsets: ARRAY of {topic, partitions: [{partition, offset}]} */
5237 2 AV *topics = (AV*)SvRV(offsets_sv); /* validated above */
5238 2 SSize_t i, tc = av_len(topics) + 1;
5239 2 kf_buf_append_i32(&body, (int32_t)tc);
5240  
5241 4 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 2 2 4
5242 2 SV **elem = av_fetch(topics, i, 0);
5243 2 HV *th = (HV*)SvRV(*elem);
5244  
5245 2 SV **tname_sv = hv_fetch(th, "topic", 5, 0);
5246 STRLEN tnlen;
5247 2 const char *tname = SvPV(*tname_sv, tnlen);
5248 2 kf_buf_append_string(&body, tname, (int16_t)tnlen);
5249  
5250 2 SV **parts_sv = hv_fetch(th, "partitions", 10, 0);
5251 2 AV *parts = (AV*)SvRV(*parts_sv); /* validated above */
5252 2 SSize_t j, pc = av_len(parts) + 1;
5253 2 kf_buf_append_i32(&body, (int32_t)pc);
5254  
5255 4 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 2 2 4
5256 2 SV **pelem = av_fetch(parts, j, 0);
5257 2 HV *ph = (HV*)SvRV(*pelem); /* validated above */
5258  
5259 2 SV **ppid_sv = hv_fetch(ph, "partition", 9, 0);
5260 2 ▶ kf_buf_append_i32(&body, ppid_sv ? (int32_t)SvIV(*ppid_sv) : 0);
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_i32(&body, ppid_sv ? (int32_t)SvIV(*ppid_sv) : 0); 2 0 2
5261  
5262 2 SV **off_sv = hv_fetch(ph, "offset", 6, 0);
5263 2 ▶ kf_buf_append_i64(&body, off_sv ? (int64_t)SvIV(*off_sv) : 0);
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_i64(&body, off_sv ? (int64_t)SvIV(*off_sv) : 0); 2 0 2
5264  
5265 /* leader_epoch (v2+) */
5266 2 ▶ if (ver >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 2) 0 2 2
5267 0 kf_buf_append_i32(&body, -1);
5268  
5269 2 kf_buf_append_nullable_string(&body, "", 0); /* metadata */
5270 }
5271 }
5272  
5273 2 conn_send_request(aTHX_ self, API_TXN_OFFSET_COMMIT, ver, &body, cb, 0, 0);
5274 2 kf_buf_free(&body);
5275 }
5276  
5277 MODULE = EV::Kafka PACKAGE = EV::Kafka
5278  
5279 int
5280 _murmur2(SV *data_sv)
5281 CODE:
5282 {
5283 STRLEN len;
5284 32 const unsigned char *data = (const unsigned char *)SvPV(data_sv, len);
5285 32 uint32_t h = 0x9747b28c ^ (uint32_t)len;
5286 32 const uint32_t m = 0x5bd1e995;
5287 32 size_t i = 0;
5288 32 size_t remaining = len;
5289  
5290 291 ▶ while (remaining >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
while (remaining >= 4) { 259 32 291
5291 uint32_t k;
5292 259 memcpy(&k, data + i, 4); /* little-endian on x86, matches Java */
5293 259 k *= m;
5294 259 k ^= k >> 24;
5295 259 k *= m;
5296 259 h *= m;
5297 259 h ^= k;
5298 259 i += 4;
5299 259 remaining -= 4;
5300 }
5301  
5302 32 switch (remaining) {
5303 5 case 3: h ^= (uint32_t)data[i + 2] << 16; /* fallthrough */
5304 7 case 2: h ^= (uint32_t)data[i + 1] << 8; /* fallthrough */
5305 26 case 1: h ^= (uint32_t)data[i]; h *= m;
5306 }
5307  
5308 32 h ^= h >> 13;
5309 32 h *= m;
5310 32 h ^= h >> 15;
5311  
5312 32 ▶ RETVAL = (int)(h & 0x7FFFFFFF);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int)(h & 0x7FFFFFFF); 19 13 32
5313 }
5314 OUTPUT:
5315 RETVAL
5316  
5317 unsigned int
5318 _crc32c(SV *data_sv)
5319 CODE:
5320 {
5321 STRLEN len;
5322 7 const char *data = SvPV(data_sv, len);
5323 7 RETVAL = crc32c(data, len);
5324 }
5325 OUTPUT:
5326 RETVAL
5327  
5328 void
5329 _error_name(int code)
5330 PPCODE:
5331 {
5332 14 const char *name = NULL;
5333 14 switch (code) {
5334 1 case 0: name = "NONE"; break;
5335 2 case 1: name = "OFFSET_OUT_OF_RANGE"; break;
5336 0 case 2: name = "CORRUPT_MESSAGE"; break;
5337 2 case 3: name = "UNKNOWN_TOPIC_OR_PARTITION"; break;
5338 0 case 5: name = "LEADER_NOT_AVAILABLE"; break;
5339 1 case 6: name = "NOT_LEADER_OR_FOLLOWER"; break;
5340 0 case 7: name = "REQUEST_TIMED_OUT"; break;
5341 0 case 10: name = "MESSAGE_TOO_LARGE"; break;
5342 1 case 15: name = "COORDINATOR_NOT_AVAILABLE"; break;
5343 1 case 16: name = "NOT_COORDINATOR"; break;
5344 0 case 17: name = "INVALID_TOPIC_EXCEPTION"; break;
5345 0 case 19: name = "NOT_ENOUGH_REPLICAS"; break;
5346 0 case 20: name = "NOT_ENOUGH_REPLICAS_AFTER_APPEND"; break;
5347 0 case 22: name = "ILLEGAL_GENERATION"; break;
5348 0 case 25: name = "UNKNOWN_MEMBER_ID"; break;
5349 0 case 26: name = "INVALID_SESSION_TIMEOUT"; break;
5350 1 case 27: name = "REBALANCE_IN_PROGRESS"; break;
5351 0 case 35: name = "UNSUPPORTED_VERSION"; break;
5352 1 case 36: name = "TOPIC_ALREADY_EXISTS"; break;
5353 0 case 39: name = "REASSIGNMENT_IN_PROGRESS"; break;
5354 0 case 41: name = "NOT_CONTROLLER"; break;
5355 1 case 45: name = "OUT_OF_ORDER_SEQUENCE_NUMBER"; break;
5356 1 case 46: name = "DUPLICATE_SEQUENCE_NUMBER"; break;
5357 0 case 47: name = "INVALID_REPLICATION_FACTOR"; break;
5358 0 case 58: name = "SASL_AUTHENTICATION_FAILED"; break;
5359 0 case 72: name = "LISTENER_NOT_FOUND"; break;
5360 1 case 79: name = "MEMBER_ID_REQUIRED"; break;
5361 1 default: name = "UNKNOWN"; break;
5362 }
5363 14 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 14 14
5364 14 mPUSHp(name, strlen(name));
5365 14 XSRETURN(1);
5366 }
5367  
5368 # ---- internal test helpers (not part of the public API) ----
5369  
5370 SV*
5371 _test_zigzag_i32(int v)
5372 CODE:
5373 {
5374 5 int32_t v32 = (int32_t)v;
5375 5 uint32_t z = (uint32_t)((v32 << 1) ^ (v32 >> 31));
5376 5 RETVAL = newSVuv(z);
5377 }
5378 OUTPUT:
5379 RETVAL
5380  
5381 SV*
5382 _test_zigzag_i64(SV *v_sv)
5383 CODE:
5384 {
5385 5 int64_t v = (int64_t)SvIV(v_sv);
5386 5 uint64_t z = (uint64_t)((v << 1) ^ (v >> 63));
5387 5 RETVAL = newSVuv(z);
5388 }
5389 OUTPUT:
5390 RETVAL
5391  
5392 # Encode then decode a varint; returns the round-tripped i64 or undef on
5393 # malformed input.
5394 SV*
5395 _test_varint_roundtrip(SV *v_sv)
5396 CODE:
5397 {
5398 14 int64_t v = (int64_t)SvIV(v_sv);
5399 kf_buf_t b;
5400 14 kf_buf_init(&b);
5401 14 kf_buf_append_varint(&b, v);
5402 int64_t out;
5403 14 int n = kf_read_varint(b.data, b.data + b.len, &out);
5404 14 ▶ if (n < 0) RETVAL = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) RETVAL = &PL_sv_undef; 0 14 14
5405 14 else RETVAL = newSViv((IV)out);
5406 14 kf_buf_free(&b);
5407 }
5408 OUTPUT:
5409 RETVAL
5410  
5411 # Encode a single-record RecordBatch v2 with given options.
5412 # opts: { compression => 0|1|3, producer_id, producer_epoch,
5413 # base_sequence, is_transactional, timestamp }.
5414 SV*
5415 _test_encode_batch(SV *records_sv, SV *opts_sv = NULL)
5416 CODE:
5417 {
5418 21 ▶ if (!SvROK(records_sv) || SvTYPE(SvRV(records_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(records_sv) || SvTYPE(SvRV(records_sv)) != SVt_PVAV) 21 0 21
if (!SvROK(records_sv) || SvTYPE(SvRV(records_sv)) != SVt_PVAV) 0 21 21
5419 0 croak("_test_encode_batch: arrayref required");
5420 21 AV *records = (AV*)SvRV(records_sv);
5421  
5422 21 int compression = 0;
5423 21 int64_t producer_id = -1;
5424 21 int16_t producer_epoch = -1;
5425 21 int32_t base_sequence = -1;
5426 21 int is_txn = 0;
5427 21 int64_t ts = 0;
5428 21 ▶ if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 11 10 21
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 11 0 11
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 11 0 11
5429 11 HV *opts = (HV*)SvRV(opts_sv);
5430 SV **v;
5431 11 ▶ if ((v = hv_fetchs(opts, "compression", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "compression", 0)) && SvOK(*v)) 10 1 11
if ((v = hv_fetchs(opts, "compression", 0)) && SvOK(*v)) 10 0 10
5432 10 compression = (int)SvIV(*v);
5433 11 ▶ if ((v = hv_fetchs(opts, "producer_id", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "producer_id", 0)) && SvOK(*v)) 1 10 11
if ((v = hv_fetchs(opts, "producer_id", 0)) && SvOK(*v)) 1 0 1
5434 1 producer_id = (int64_t)SvIV(*v);
5435 11 ▶ if ((v = hv_fetchs(opts, "producer_epoch", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "producer_epoch", 0)) && SvOK(*v)) 1 10 11
if ((v = hv_fetchs(opts, "producer_epoch", 0)) && SvOK(*v)) 1 0 1
5436 1 producer_epoch = (int16_t)SvIV(*v);
5437 11 ▶ if ((v = hv_fetchs(opts, "base_sequence", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "base_sequence", 0)) && SvOK(*v)) 1 10 11
if ((v = hv_fetchs(opts, "base_sequence", 0)) && SvOK(*v)) 1 0 1
5438 1 base_sequence = (int32_t)SvIV(*v);
5439 11 ▶ if ((v = hv_fetchs(opts, "is_transactional", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "is_transactional", 0)) && SvOK(*v)) 0 11 11
if ((v = hv_fetchs(opts, "is_transactional", 0)) && SvOK(*v)) 0 0 0
5440 0 is_txn = (int)SvIV(*v);
5441 11 ▶ if ((v = hv_fetchs(opts, "timestamp", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "timestamp", 0)) && SvOK(*v)) 0 11 11
if ((v = hv_fetchs(opts, "timestamp", 0)) && SvOK(*v)) 0 0 0
5442 0 ts = (int64_t)SvIV(*v);
5443 }
5444  
5445 kf_buf_t out;
5446 21 kf_encode_record_batch_multi(aTHX_ &out, records, ts, compression,
5447 producer_id, producer_epoch, base_sequence, is_txn);
5448 21 RETVAL = newSVpvn(out.data, out.len);
5449 21 kf_buf_free(&out);
5450 }
5451 OUTPUT:
5452 RETVAL
5453  
5454 # Parse a captured response body of a known API. Returns the parsed hash
5455 # (same shape user callbacks see) or undef if the parser rejected the bytes.
5456 # api: 'metadata' | 'produce' | 'fetch' | 'list_offsets' | 'find_coordinator'.
5457 SV*
5458 _test_parse_response(const char *api, int version, SV *bytes_sv)
5459 CODE:
5460 {
5461 STRLEN len;
5462 52018 const char *data = SvPV(bytes_sv, len);
5463 ev_kafka_conn_t stub;
5464 52018 memset(&stub, 0, sizeof(stub));
5465 52018 stub.magic = KF_MAGIC_ALIVE;
5466 52018 stub.fd = -1;
5467 52018 SV *result = NULL;
5468 52018 ▶ if (strcmp(api, "metadata") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(api, "metadata") == 0) { 52004 14 52018
5469 52004 result = conn_parse_metadata_response(aTHX_ &stub, (int16_t)version, data, len);
5470 14 ▶ } else if (strcmp(api, "produce") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "produce") == 0) { 1 13 14
5471 1 result = conn_parse_produce_response(aTHX_ &stub, (int16_t)version, data, len);
5472 13 ▶ } else if (strcmp(api, "fetch") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "fetch") == 0) { 1 12 13
5473 1 result = conn_parse_fetch_response(aTHX_ &stub, (int16_t)version, data, len);
5474 12 ▶ } else if (strcmp(api, "list_offsets") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "list_offsets") == 0) { 0 12 12
5475 0 result = conn_parse_list_offsets_response(aTHX_ &stub, (int16_t)version, data, len);
5476 12 ▶ } else if (strcmp(api, "find_coordinator") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "find_coordinator") == 0) { 1 11 12
5477 1 result = conn_parse_find_coordinator_response(aTHX_ &stub, (int16_t)version, data, len);
5478 11 ▶ } else if (strcmp(api, "join_group") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "join_group") == 0) { 1 10 11
5479 1 result = conn_parse_join_group_response(aTHX_ &stub, (int16_t)version, data, len);
5480 10 ▶ } else if (strcmp(api, "sync_group") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "sync_group") == 0) { 1 9 10
5481 1 result = conn_parse_sync_group_response(aTHX_ &stub, (int16_t)version, data, len);
5482 9 ▶ } else if (strcmp(api, "heartbeat") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "heartbeat") == 0) { 1 8 9
5483 1 result = conn_parse_heartbeat_response(aTHX_ &stub, (int16_t)version, data, len);
5484 8 ▶ } else if (strcmp(api, "offset_commit") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "offset_commit") == 0) { 0 8 8
5485 0 result = conn_parse_offset_commit_response(aTHX_ &stub, (int16_t)version, data, len);
5486 8 ▶ } else if (strcmp(api, "offset_fetch") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "offset_fetch") == 0) { 1 7 8
5487 1 result = conn_parse_offset_fetch_response(aTHX_ &stub, (int16_t)version, data, len);
5488 7 ▶ } else if (strcmp(api, "leave_group") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "leave_group") == 0) { 1 6 7
5489 1 result = conn_parse_leave_group_response(aTHX_ &stub, (int16_t)version, data, len);
5490 6 ▶ } else if (strcmp(api, "create_topics") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "create_topics") == 0) { 1 5 6
5491 1 result = conn_parse_create_topics_response(aTHX_ &stub, (int16_t)version, data, len);
5492 5 ▶ } else if (strcmp(api, "delete_topics") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "delete_topics") == 0) { 1 4 5
5493 1 result = conn_parse_delete_topics_response(aTHX_ &stub, (int16_t)version, data, len);
5494 4 ▶ } else if (strcmp(api, "init_producer_id") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "init_producer_id") == 0) { 1 3 4
5495 1 result = conn_parse_init_producer_id_response(aTHX_ &stub, (int16_t)version, data, len);
5496 3 ▶ } else if (strcmp(api, "add_partitions_to_txn") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "add_partitions_to_txn") == 0) { 1 2 3
5497 1 result = conn_parse_add_partitions_to_txn_response(aTHX_ &stub, (int16_t)version, data, len);
5498 2 ▶ } else if (strcmp(api, "end_txn") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "end_txn") == 0) { 1 1 2
5499 1 result = conn_parse_end_txn_response(aTHX_ &stub, (int16_t)version, data, len);
5500 1 ▶ } else if (strcmp(api, "txn_offset_commit") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "txn_offset_commit") == 0) { 1 0 1
5501 1 result = conn_parse_txn_offset_commit_response(aTHX_ &stub, (int16_t)version, data, len);
5502 } else {
5503 0 croak("_test_parse_response: unknown api '%s'", api);
5504 }
5505 52018 ▶ RETVAL = result ? SvREFCNT_inc(result) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = result ? SvREFCNT_inc(result) : &PL_sv_undef; 52018 0 52018
5506 }
5507 OUTPUT:
5508 RETVAL
5509  
5510 # Decode a RecordBatch v2 blob. Returns arrayref of {offset, key, value,
5511 # headers} on success, or undef on malformed input (CRC mismatch, etc.).
5512 SV*
5513 _test_decode_batch(SV *bytes_sv)
5514 CODE:
5515 {
5516 STRLEN len;
5517 16 const char *data = SvPV(bytes_sv, len);
5518 16 AV *records_av = newAV();
5519 int64_t base_offset;
5520 16 int n = kf_decode_record_batch(aTHX_ data, len, records_av, &base_offset);
5521 16 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 3 13 16
5522 3 SvREFCNT_dec((SV*)records_av);
5523 3 RETVAL = &PL_sv_undef;
5524 } else {
5525 13 RETVAL = newRV_noinc((SV*)records_av);
5526 }
5527 }
5528 OUTPUT:
5529 RETVAL
5530  
5531 BOOT:
5532 {
5533 34 ▶ I_EV_API("EV::Kafka");
Branch: 3 branches
BranchTrueFalseTotal
I_EV_API("EV::Kafka"); 0 34 34
I_EV_API("EV::Kafka"); 34 0 34
I_EV_API("EV::Kafka"); 0 34 34
5534 34 crc32c_init_table();
5535 }
« blib/lib/EV/Kafka.pm ↑ Summary