Artifactspublic-v0

curl

master
ply4 git calls153 ms295 KiB2 cachedprotocol v2
923 lines24 KiBRaw
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "first.h"
26/* Function
27 *
28 * Accepts a TCP connection on a custom port (IPv4 or IPv6). Speaks MQTT.
29 *
30 * Read commands from FILE (set with --config). The commands control how to
31 * act and is reset to defaults each client TCP connect.
32 *
33 */
35/* based on sockfilt.c */
37#define MQTT_MSG_CONNECT 0x10
38#define MQTT_MSG_CONNACK 0x20
39#define MQTT_MSG_PUBLISH 0x30
40/* #define MQTT_MSG_PUBACK 0x40 */
41#define MQTT_MSG_SUBSCRIBE 0x82
42#define MQTT_MSG_SUBACK 0x90
43#define MQTT_MSG_PINGRESP 0xd0
44#define MQTT_MSG_DISCONNECT 0xe0
46struct mqttd_configurable {
47 int testnum;
48 unsigned char version; /* initial version byte in the request must match
49 this */
50 bool publish_before_suback;
51 bool short_publish;
52 bool excessive_remaining;
53 bool pingresp_as_connack; /* send PINGRESP with payload instead of CONNACK */
54 bool disconnect_malformed; /* DISCONNECT with nonzero remlen */
55 unsigned char error_connack;
56 unsigned char remlen_connack;
57};
59#define REQUEST_DUMP "server.input"
60#define CONFIG_VERSION 5
62static struct mqttd_configurable m_config;
64static void mqttd_resetdefaults(void)
66 logmsg("Reset to defaults");
67 m_config.version = CONFIG_VERSION;
68 m_config.publish_before_suback = FALSE;
69 m_config.short_publish = FALSE;
70 m_config.excessive_remaining = FALSE;
71 m_config.pingresp_as_connack = FALSE;
72 m_config.disconnect_malformed = FALSE;
73 m_config.error_connack = 0;
74 m_config.remlen_connack = 0;
75 m_config.testnum = 0;
78static void mqttd_getconfig(void)
80 FILE *fp = curlx_fopen(configfile, FOPEN_READTEXT);
81 mqttd_resetdefaults();
82 if(fp) {
83 char buffer[512];
84 logmsg("parse config file");
85 while(fgets(buffer, sizeof(buffer), fp)) {
86 char key[32];
87 char value[32];
88 const char *pval;
89 curl_off_t num;
90 if(sscanf(buffer, "%31s %31s", key, value) == 2) {
91 if(!strcmp(key, "version")) {
92 pval = value;
93 if(!curlx_str_number(&pval, &num, 0xff)) {
94 m_config.version = (unsigned char)num;
95 logmsg("version [%d] set", m_config.version);
96 }
97 }
98 else if(!strcmp(key, "PUBLISH-before-SUBACK")) {
99 logmsg("PUBLISH-before-SUBACK set");
100 m_config.publish_before_suback = TRUE;
101 }
102 else if(!strcmp(key, "short-PUBLISH")) {
103 logmsg("short-PUBLISH set");
104 m_config.short_publish = TRUE;
105 }
106 else if(!strcmp(key, "PINGRESP-as-CONNACK")) {
107 logmsg("PINGRESP-as-CONNACK set");
108 m_config.pingresp_as_connack = TRUE;
109 }
110 else if(!strcmp(key, "DISCONNECT-malformed")) {
111 logmsg("DISCONNECT-malformed set");
112 m_config.disconnect_malformed = TRUE;
113 }
114 else if(!strcmp(key, "error-CONNACK")) {
115 pval = value;
116 if(!curlx_str_number(&pval, &num, 0xff)) {
117 m_config.error_connack = (unsigned char)num;
118 logmsg("error-CONNACK = %d", m_config.error_connack);
119 }
120 }
121 else if(!strcmp(key, "remlen-CONNACK")) {
122 pval = value;
123 if(!curlx_str_number(&pval, &num, 0xff)) {
124 m_config.remlen_connack = (unsigned char)num;
125 logmsg("remlen-CONNACK = %d", m_config.remlen_connack);
126 }
127 }
128 else if(!strcmp(key, "Testnum")) {
129 pval = value;
130 if(!curlx_str_number(&pval, &num, INT_MAX)) {
131 m_config.testnum = (int)num;
132 logmsg("testnum = %d", m_config.testnum);
133 }
134 }
135 else if(!strcmp(key, "excessive-remaining")) {
136 logmsg("excessive-remaining set");
137 m_config.excessive_remaining = TRUE;
138 }
139 }
140 }
141 curlx_fclose(fp);
142 }
143 else {
144 logmsg("No config file '%s' to read", configfile);
145 }
148typedef enum {
149 FROM_CLIENT,
150 FROM_SERVER
151} mqttdir;
153static void logprotocol(mqttdir dir,
154 const char *prefix, size_t remlen,
155 FILE *output,
156 const unsigned char *buffer, ssize_t len)
158 char data[12000] = "";
159 ssize_t i;
160 const unsigned char *ptr = buffer;
161 char *optr = data;
162 int left = sizeof(data);
164 for(i = 0; i < len && (left >= 0); i++) {
165 snprintf(optr, left, "%02x", ptr[i]);
166 optr += 2;
167 left -= 2;
168 }
169 fprintf(output, "%s %s %lx %s\n",
170 dir == FROM_CLIENT ? "client" : "server",
171 prefix, (unsigned long)remlen, data);
174/* return 0 on success */
175static int connack(FILE *dump, curl_socket_t fd)
177 unsigned char packet[] = {
178 MQTT_MSG_CONNACK, 0x02,
179 0x00, 0x00
180 };
181 const char *label = "CONNACK";
182 ssize_t rc;
184 if(m_config.pingresp_as_connack) {
185 /* Send a PINGRESP (0xD0) with remaining_length=2 and payload
186 mimicking a successful CONNACK. MQTT 3.1.1 s. 3.13.1 requires
187 PINGRESP to have remaining_length=0, so this is malformed. */
188 packet[0] = MQTT_MSG_PINGRESP;
189 label = "PINGRESP-as-CONNACK";
190 logmsg("Sending malformed PINGRESP in place of CONNACK");
191 }
193 if(m_config.remlen_connack)
194 packet[1] = m_config.remlen_connack;
195 packet[3] = m_config.error_connack;
197 rc = swrite(fd, packet, sizeof(packet));
198 if(rc > 0) {
199 logmsg("WROTE %ld bytes [%s]", (long)rc, label);
200 loghex(packet, rc);
201 logprotocol(FROM_SERVER, label, packet[1], dump,
202 packet, rc);
203 }
204 if(rc == sizeof(packet)) {
205 return 0;
206 }
207 return 1;
210/* return 0 on success */
211static int suback(FILE *dump, curl_socket_t fd, unsigned short packetid)
213 unsigned char packet[] = {
214 MQTT_MSG_SUBACK, 0x03,
215 0, 0, /* filled in below */
216 0x00
217 };
218 ssize_t rc;
219 packet[2] = (unsigned char)(packetid >> 8);
220 packet[3] = (unsigned char)(packetid & 0xff);
222 rc = swrite(fd, packet, sizeof(packet));
223 if(rc == sizeof(packet)) {
224 logmsg("WROTE %ld bytes [SUBACK]", (long)rc);
225 loghex(packet, rc);
226 logprotocol(FROM_SERVER, "SUBACK", 3, dump, packet, rc);
227 return 0;
228 }
229 return 1;
232#ifdef QOS
233/* return 0 on success */
234static int puback(FILE *dump, curl_socket_t fd, unsigned short packetid)
236 unsigned char packet[] = {
237 MQTT_MSG_PUBACK, 0x00,
238 0, 0 /* filled in below */
239 };
240 ssize_t rc;
241 packet[2] = (unsigned char)(packetid >> 8);
242 packet[3] = (unsigned char)(packetid & 0xff);
244 rc = swrite(fd, packet, sizeof(packet));
245 if(rc == sizeof(packet)) {
246 logmsg("WROTE %ld bytes [PUBACK]", (long)rc);
247 loghex(packet, rc);
248 logprotocol(FROM_SERVER, "PUBACK", 0, dump, packet, rc);
249 return 0;
250 }
251 logmsg("Failed sending [PUBACK]");
252 return 1;
254#endif
256/* return 0 on success */
257static int disconnect(FILE *dump, curl_socket_t fd)
259 unsigned char packet[] = {
260 MQTT_MSG_DISCONNECT, 0x00,
261 0x00, 0x00 /* extra bytes for malformed variant */
262 };
263 const char *label = "DISCONNECT";
264 size_t pktlen = 2;
265 ssize_t rc;
267 if(m_config.disconnect_malformed) {
268 /* Send DISCONNECT with remaining_length=2 (must be 0 per spec) */
269 packet[1] = 0x02;
270 pktlen = 4;
271 label = "DISCONNECT-malformed";
272 logmsg("Sending malformed DISCONNECT with nonzero remaining_length");
273 }
275 rc = swrite(fd, packet, pktlen);
276 if(rc > 0) {
277 logmsg("WROTE %ld bytes [%s]", (long)rc, label);
278 loghex(packet, rc);
279 logprotocol(FROM_SERVER, label, packet[1], dump, packet, rc);
280 }
281 if(rc == (ssize_t)pktlen) {
282 return 0;
283 }
284 logmsg("Failed sending [%s]", label);
285 return 1;
288/*
289 do
290 encodedByte = X MOD 128
292 X = X DIV 128
294 // if there are more data to encode, set the top bit of this byte
296 if ( X > 0 )
297 encodedByte = encodedByte OR 128
298 endif
300 'output' encodedByte
302 while ( X > 0 )
303 */
305/* return number of bytes used */
306static size_t encode_length(size_t packetlen,
307 unsigned char *remlength) /* 4 bytes */
309 size_t bytes = 0;
310 unsigned char encode;
312 do {
313 encode = packetlen % 0x80;
314 packetlen /= 0x80;
315 if(packetlen)
316 encode |= 0x80;
318 remlength[bytes++] = encode;
320 if(bytes > 3) {
321 logmsg("too large packet!");
322 return 0;
323 }
324 } while(packetlen);
326 return bytes;
329static size_t decode_length(const unsigned char *buffer,
330 size_t buflen, size_t *lenbytes)
332 size_t len = 0;
333 size_t mult = 1;
334 size_t i;
335 unsigned char encoded = 0x80;
337 for(i = 0; (i < buflen) && (encoded & 0x80); i++) {
338 encoded = buffer[i];
339 len += (encoded & 0x7f) * mult;
340 mult *= 0x80;
341 }
343 if(lenbytes)
344 *lenbytes = i;
346 return len;
349/* return 0 on success */
350static int publish(FILE *dump,
351 curl_socket_t fd, unsigned short packetid,
352 char *topic, const char *payload, size_t payloadlen)
354 size_t topiclen = strlen(topic);
355 unsigned char *packet;
356 size_t payloadindex;
357 size_t remaininglength = topiclen + 2 + payloadlen;
358 size_t packetlen;
359 size_t sendamount;
360 ssize_t rc;
361 unsigned char rembuffer[4];
362 size_t encodedlen;
364 if(m_config.excessive_remaining) {
365 /* manually set illegal remaining length */
366 rembuffer[0] = 0xff;
367 rembuffer[1] = 0xff;
368 rembuffer[2] = 0xff;
369 rembuffer[3] = 0x80; /* maximum allowed here by spec is 0x7f */
370 encodedlen = 4;
371 }
372 else
373 encodedlen = encode_length(remaininglength, rembuffer);
375 /* one packet type byte (possibly two more for packetid) */
376 packetlen = remaininglength + encodedlen + 1;
377 packet = malloc(packetlen);
378 if(!packet)
379 return 1;
381 packet[0] = MQTT_MSG_PUBLISH;
382 memcpy(&packet[1], rembuffer, encodedlen);
384 (void)packetid;
385 /* packet_id if QoS is set */
387 packet[1 + encodedlen] = (unsigned char)(topiclen >> 8);
388 packet[2 + encodedlen] = (unsigned char)(topiclen & 0xff);
389 /* NOLINTNEXTLINE(bugprone-not-null-terminated-result) */
390 memcpy(&packet[3 + encodedlen], topic, topiclen);
392 payloadindex = 3 + topiclen + encodedlen;
393 memcpy(&packet[payloadindex], payload, payloadlen);
395 sendamount = packetlen;
396 if(m_config.short_publish)
397 sendamount -= 2;
399 rc = swrite(fd, packet, sendamount);
400 if(rc > 0) {
401 logmsg("WROTE %ld bytes [PUBLISH]", (long)rc);
402 loghex(packet, rc);
403 logprotocol(FROM_SERVER, "PUBLISH", remaininglength, dump, packet, rc);
404 }
405 free(packet);
406 if((size_t)rc == packetlen)
407 return 0;
408 return 1;
411#define MAX_TOPIC_LENGTH 65535
412#define MAX_CLIENT_ID_LENGTH 32
414static char topic[MAX_TOPIC_LENGTH + 1];
416static bool readall(curl_socket_t fd, unsigned char *buffer, size_t len)
418 size_t nread = 0;
420 while(nread < len) {
421 ssize_t rc = sread(fd, buffer + nread, len - nread);
422 if(rc <= 0) {
423 if(rc < 0 && SOCKERRNO == SOCKEINTR && !got_exit_signal)
424 continue;
425 logmsg("READ %ld bytes [SHORT!]", (long)rc);
426 return FALSE;
427 }
428 logmsg("READ %ld bytes", (long)rc);
429 loghex(buffer + nread, rc);
430 nread += (size_t)rc;
431 }
432 return TRUE;
435static bool fixedheader(curl_socket_t fd,
436 unsigned char *bytep,
437 size_t *remaining_lengthp,
438 size_t *remaining_length_bytesp)
440 /* get the fixed header */
441 unsigned char buffer[10];
443 size_t i;
445 /* get the first two bytes */
446 if(!readall(fd, buffer, 2))
447 return FALSE; /* fail */
448 *bytep = buffer[0];
450 /* if the length byte has the top bit set, get the next one too */
451 i = 1;
452 while(buffer[i] & 0x80) {
453 i++;
454 if(!readall(fd, &buffer[i], 1)) {
455 logmsg("Remaining Length broken");
456 return FALSE;
457 }
458 }
459 *remaining_lengthp = decode_length(&buffer[1], i, remaining_length_bytesp);
460 logmsg("Remaining Length: %lu [%lu bytes]",
461 (unsigned long)*remaining_lengthp,
462 (unsigned long)*remaining_length_bytesp);
463 return TRUE;
466static curl_socket_t mqttit(curl_socket_t fd)
468 size_t buff_size = 10 * 1024;
469 unsigned char *buffer = NULL;
470 unsigned char byte;
471 unsigned short packet_id;
472 size_t payload_len;
473 size_t client_id_length;
474 size_t topic_len;
475 size_t remaining_length = 0;
476 size_t bytes = 0; /* remaining length field size in bytes */
477 char client_id[MAX_CLIENT_ID_LENGTH];
478 long testno;
479 FILE *stream = NULL;
480 FILE *dump;
481 char dumpfile[256];
483 static const char protocol[7] = {
484 0x00, 0x04, /* protocol length */
485 'M','Q','T','T', /* protocol name */
486 0x04 /* protocol level */
487 };
488 snprintf(dumpfile, sizeof(dumpfile), "%s/%s", logdir, REQUEST_DUMP);
489 dump = curlx_fopen(dumpfile, "ab");
490 if(!dump)
491 goto end;
493 mqttd_getconfig();
495 testno = m_config.testnum;
497 if(testno)
498 logmsg("Found test number %ld", testno);
500 buffer = malloc(buff_size);
501 if(!buffer) {
502 logmsg("Out of memory, unable to allocate buffer");
503 goto end;
504 }
505 memset(buffer, 0, buff_size);
507 do {
508 unsigned char usr_flag = 0x80;
509 unsigned char passwd_flag = 0x40;
510 unsigned char conn_flags;
511 const size_t client_id_offset = 12;
512 size_t start_usr;
513 size_t start_passwd;
514 ssize_t rc = 0;
516 /* get the fixed header */
517 if(!fixedheader(fd, &byte, &remaining_length, &bytes))
518 break;
520 if(remaining_length >= buff_size) {
521 unsigned char *newbuffer;
522 buff_size = remaining_length;
523 newbuffer = realloc(buffer, buff_size);
524 if(!newbuffer) {
525 logmsg("Failed realloc of size %lu", (unsigned long)buff_size);
526 goto end;
527 }
528 buffer = newbuffer;
529 }
531 /* Read the complete variable header and payload. */
532 if(!readall(fd, buffer, remaining_length))
533 goto end;
534 rc = (ssize_t)remaining_length;
536 if(byte == MQTT_MSG_CONNECT) {
537 logprotocol(FROM_CLIENT, "CONNECT", remaining_length, dump, buffer, rc);
539 if(memcmp(protocol, buffer, sizeof(protocol))) {
540 logmsg("Protocol preamble mismatch");
541 goto end;
542 }
543 /* ignore the connect flag byte and two keepalive bytes */
544 payload_len = (size_t)(buffer[10] << 8) | buffer[11];
545 /* first part of the payload is the client ID */
546 client_id_length = payload_len;
548 /* checking if user and password flags were set */
549 conn_flags = buffer[7];
551 start_usr = client_id_offset + payload_len;
552 if(usr_flag == (conn_flags & usr_flag)) {
553 logmsg("User flag is present in CONN flag");
554 payload_len += (size_t)(buffer[start_usr] << 8) |
555 buffer[start_usr + 1];
556 payload_len += 2; /* MSB and LSB for user length */
557 }
559 start_passwd = client_id_offset + payload_len;
560 if(passwd_flag == (conn_flags & passwd_flag)) {
561 logmsg("Password flag is present in CONN flags");
562 payload_len += (size_t)(buffer[start_passwd] << 8) |
563 buffer[start_passwd + 1];
564 payload_len += 2; /* MSB and LSB for password length */
565 }
567 /* check the length of the payload */
568 if((ssize_t)payload_len != (rc - 12)) {
569 logmsg("Payload length mismatch, expected %ld got %ld",
570 (long)rc - 12, (long)payload_len);
571 goto end;
572 }
573 /* check the length of the client ID */
574 else if((client_id_length + 1) > MAX_CLIENT_ID_LENGTH) {
575 logmsg("Too large client id");
576 goto end;
577 }
578 memcpy(client_id, &buffer[12], client_id_length);
579 client_id[client_id_length] = 0;
581 logmsg("MQTT client connect accepted: %s", client_id);
583 /* The first packet sent from the Server to the Client MUST be a
584 CONNACK Packet */
586 if(connack(dump, fd)) {
587 logmsg("failed sending CONNACK");
588 goto end;
589 }
590 }
591 else if(byte == MQTT_MSG_SUBSCRIBE) {
592 int error;
593 char *data;
594 size_t datalen;
595 logprotocol(FROM_CLIENT, "SUBSCRIBE", remaining_length,
596 dump, buffer, rc);
597 logmsg("Incoming SUBSCRIBE");
599 if(rc < 6) {
600 logmsg("Too small SUBSCRIBE");
601 goto end;
602 }
604 /* two bytes packet id */
605 packet_id = (unsigned short)((buffer[0] << 8) | buffer[1]);
607 /* two bytes topic length */
608 topic_len = (size_t)(buffer[2] << 8) | buffer[3];
609 if(topic_len != (remaining_length - 5)) {
610 logmsg("Wrong topic length, got %lu expected %lu",
611 (unsigned long)topic_len, (unsigned long)remaining_length - 5);
612 goto end;
613 }
614 memcpy(topic, &buffer[4], topic_len);
615 topic[topic_len] = 0;
617 /* there is a QoS byte (two bits) after the topic */
619 logmsg("SUBSCRIBE to '%s' [%d]", topic, packet_id);
620 stream = test2fopen(testno, logdir);
621 if(!stream) {
622 char errbuf[STRERROR_LEN];
623 error = errno;
624 logmsg("fopen() failed with error (%d) %s",
625 error, curlx_strerror(error, errbuf, sizeof(errbuf)));
626 logmsg("Could not open test file %ld", testno);
627 goto end;
628 }
629 error = getpart(&data, &datalen, "reply", "data", stream);
630 if(!error) {
631 if(!m_config.publish_before_suback) {
632 if(suback(dump, fd, packet_id)) {
633 logmsg("failed sending SUBACK");
634 free(data);
635 goto end;
636 }
637 }
638 if(publish(dump, fd, packet_id, topic, data, datalen)) {
639 logmsg("PUBLISH failed");
640 free(data);
641 goto end;
642 }
643 free(data);
644 if(m_config.publish_before_suback) {
645 if(suback(dump, fd, packet_id)) {
646 logmsg("failed sending SUBACK");
647 goto end;
648 }
649 }
650 }
651 else {
652 static const char def[] = "this is random payload yes yes it is";
653 publish(dump, fd, packet_id, topic, def, CURL_CSTRLEN(def));
654 }
655 disconnect(dump, fd);
656 }
657 else if((byte & 0xf0) == (MQTT_MSG_PUBLISH & 0xf0)) {
658 size_t topiclen;
660 logmsg("Incoming PUBLISH");
661 logprotocol(FROM_CLIENT, "PUBLISH", remaining_length, dump, buffer, rc);
663 topiclen = (size_t)(buffer[1 + bytes] << 8) | buffer[2 + bytes];
664 logmsg("Got %lu bytes topic", (unsigned long)topiclen);
666#ifdef QOS
667 /* Handle packetid if there is one. Send puback if QoS > 0 */
668 puback(dump, fd, 0);
669#endif
670 /* expect a disconnect here */
671 /* get the request */
672 if(!readall(fd, buffer, 2))
673 goto end;
674 logprotocol(FROM_CLIENT, "DISCONNECT", 0, dump, buffer, 2);
675 goto end;
676 }
677 else {
678 /* not supported (yet) */
679 goto end;
680 }
681 } while(1);
683end:
684 if(buffer)
685 free(buffer);
686 if(dump)
687 curlx_fclose(dump);
688 if(stream)
689 curlx_fclose(stream);
690 return CURL_SOCKET_BAD;
693/* sockfdp is a pointer to an established stream or CURL_SOCKET_BAD
695 if sockfd is CURL_SOCKET_BAD, listendfd is a listening socket we must
696 accept() */
697static bool mqttd_incoming(curl_socket_t listenfd)
699 fd_set fds_read;
700 fd_set fds_write;
701 fd_set fds_err;
702 int clients = 0; /* connected clients */
704 if(got_exit_signal) {
705 logmsg("signalled to die, exiting...");
706 return FALSE;
707 }
709#ifdef HAVE_GETPPID
710 /* As a last resort, quit if mqttd process becomes orphan. */
711 if(getppid() <= 1) {
712 logmsg("process becomes orphan, exiting");
713 return FALSE;
714 }
715#endif
717 do {
718 ssize_t rc;
719 int sockerr = 0;
720 char errbuf[STRERROR_LEN];
721 curl_socket_t sockfd = listenfd;
722 int maxfd = (int)sockfd;
724 FD_ZERO(&fds_read);
725 FD_ZERO(&fds_write);
726 FD_ZERO(&fds_err);
728 /* there is always a socket to wait for */
729 FD_SET(sockfd, &fds_read);
731 do {
732 /* select() blocking behavior call on blocking descriptors please */
733 rc = select(maxfd + 1, &fds_read, &fds_write, &fds_err, NULL);
734 if(got_exit_signal) {
735 logmsg("signalled to die, exiting...");
736 return FALSE;
737 }
738 } while((rc == -1) && ((sockerr = SOCKERRNO) == SOCKEINTR));
740 if(rc < 0) {
741 logmsg("select() failed with error (%d) %s",
742 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
743 return FALSE;
744 }
746 if(FD_ISSET(sockfd, &fds_read)) {
747 curl_socket_t newfd = accept(sockfd, NULL, NULL);
748 if(newfd == CURL_SOCKET_BAD) {
749 sockerr = SOCKERRNO;
750 logmsg("accept() failed with error (%d) %s",
751 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
752 }
753 else {
754 logmsg("====> Client connect, fd %ld. "
755 "Read config from %s", (long)newfd, configfile);
756 set_advisor_read_lock(loglockfile);
757 (void)mqttit(newfd); /* until done */
758 clear_advisor_read_lock(loglockfile);
760 logmsg("====> Client disconnect");
761 sclose(newfd);
762 }
763 }
764 } while(clients);
766 return TRUE;
769static int test_mqttd(int argc, const char *argv[])
771 curl_socket_t sock = CURL_SOCKET_BAD;
772 curl_socket_t msgsock = CURL_SOCKET_BAD;
773 int wrotepidfile = 0;
774 int wroteportfile = 0;
775 bool juggle_again;
776 int sockerr;
777 char errbuf[STRERROR_LEN];
778 int arg = 1;
780 pidname = ".mqttd.pid";
781 portname = ".mqttd.port";
782 serverlogfile = "log/mqttd.log";
783 configfile = "mqttd.config";
784 server_port = 1883; /* MQTT default port */
786 while(argc > arg) {
787 const char *opt;
788 curl_off_t num;
789 if(!strcmp("--version", argv[arg])) {
790 printf("mqttd IPv4%s\n",
791#ifdef USE_IPV6
792 "/IPv6"
793#else
794 ""
795#endif
796 );
797 return 0;
798 }
799 else if(!strcmp("--pidfile", argv[arg])) {
800 arg++;
801 if(argc > arg)
802 pidname = argv[arg++];
803 }
804 else if(!strcmp("--portfile", argv[arg])) {
805 arg++;
806 if(argc > arg)
807 portname = argv[arg++];
808 }
809 else if(!strcmp("--config", argv[arg])) {
810 arg++;
811 if(argc > arg)
812 configfile = argv[arg++];
813 }
814 else if(!strcmp("--logfile", argv[arg])) {
815 arg++;
816 if(argc > arg)
817 serverlogfile = argv[arg++];
818 }
819 else if(!strcmp("--logdir", argv[arg])) {
820 arg++;
821 if(argc > arg)
822 logdir = argv[arg++];
823 }
824 else if(!strcmp("--ipv6", argv[arg])) {
825#ifdef USE_IPV6
826 socket_type = "IPv6";
827 socket_domain = AF_INET6;
828#endif
829 arg++;
830 }
831 else if(!strcmp("--ipv4", argv[arg])) {
832 socket_type = "IPv4";
833 socket_domain = AF_INET;
834 arg++;
835 }
836 else if(!strcmp("--port", argv[arg])) {
837 arg++;
838 if(argc > arg) {
839 opt = argv[arg];
840 if(curlx_str_number(&opt, &num, 0xffff)) {
841 fprintf(stderr, "mqttd: invalid --port argument (%s)\n", argv[arg]);
842 return 0;
843 }
844 server_port = (uint16_t)num;
845 arg++;
846 }
847 }
848 else {
849 puts("Usage: mqttd [option]\n"
850 " --config [file]\n"
851 " --version\n"
852 " --logfile [file]\n"
853 " --logdir [directory]\n"
854 " --pidfile [file]\n"
855 " --portfile [file]\n"
856 " --ipv4\n"
857 " --ipv6\n"
858 " --port [port]\n");
859 return 0;
860 }
861 }
863 snprintf(loglockfile, sizeof(loglockfile), "%s/%s/mqtt-%s.lock",
864 logdir, SERVERLOGS_LOCKDIR, socket_type);
866 CURL_BINMODE(stdin);
867 CURL_BINMODE(stdout);
868 CURL_BINMODE(stderr);
870 install_signal_handlers(FALSE);
872 sock = socket(socket_domain, SOCK_STREAM, 0);
874 if(sock == CURL_SOCKET_BAD) {
875 sockerr = SOCKERRNO;
876 logmsg("Error creating socket (%d) %s",
877 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
878 goto mqttd_cleanup;
879 }
881 {
882 /* passive daemon style */
883 sock = sockdaemon(sock, &server_port, NULL, FALSE);
884 if(sock == CURL_SOCKET_BAD) {
885 goto mqttd_cleanup;
886 }
887 msgsock = CURL_SOCKET_BAD; /* no stream socket yet */
888 }
890 logmsg("Running %s version", socket_type);
891 logmsg("Listening on port %hu", server_port);
893 wrotepidfile = write_pidfile(pidname);
894 if(!wrotepidfile) {
895 goto mqttd_cleanup;
896 }
898 wroteportfile = write_portfile(portname, server_port);
899 if(!wroteportfile) {
900 goto mqttd_cleanup;
901 }
903 do {
904 juggle_again = mqttd_incoming(sock);
905 } while(juggle_again);
907mqttd_cleanup:
909 if((msgsock != sock) && (msgsock != CURL_SOCKET_BAD))
910 sclose(msgsock);
912 if(sock != CURL_SOCKET_BAD)
913 sclose(sock);
915 if(wrotepidfile)
916 unlink(pidname);
917 if(wroteportfile)
918 unlink(portname);
920 restore_signal_handlers(FALSE);
922 return 0;
Diagnostics4 git calls · 153 ms
CallStatusSizeTime0153 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit19 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=19
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch2007.6 KiB132 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=132
git.response.bytes=7798
git.response.cache=MISS
git.response.duration_ms=132
git.fetch.wants=8bc990e36aa8de92ce20c7a6fabdc7ae29c45a81
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=7798
git.pkt.payload.max=7777
git.sideband.band1.bytes=7776
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e2fa010be490c0
Request
810e2534…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format