From e853edb32f7af0ed154e1a21789ec04343eac394 Mon Sep 17 00:00:00 2001 From: hrbrmstr Date: Sun, 20 Sep 2020 07:40:40 -0400 Subject: [PATCH] switch to jsonlite --- DESCRIPTION | 2 +- NAMESPACE | 2 +- R/bonjour-package.R | 2 +- R/discover.R | 7 +- R/query.R | 5 +- README.md | 12 +-- src/b64.h | 124 +++++++++++++++++++++++ src/bonjour-main.cpp | 275 ++++++++++++++++++++++++++------------------------- 8 files changed, 283 insertions(+), 146 deletions(-) create mode 100644 src/b64.h diff --git a/DESCRIPTION b/DESCRIPTION index e9d5633..105aaf3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,7 +30,7 @@ Depends: R (>= 3.6.0) Imports: Rcpp, - ndjson + jsonlite Roxygen: list(markdown = TRUE) RoxygenNote: 7.1.1 LinkingTo: diff --git a/NAMESPACE b/NAMESPACE index 57bdbcd..0a35a32 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,5 +7,5 @@ export(bnjr_query) export(mdns_discover) export(mdns_query) importFrom(Rcpp,sourceCpp) -importFrom(ndjson,flatten) +importFrom(jsonlite,stream_in) useDynLib(bonjour, .registration = TRUE) diff --git a/R/bonjour-package.R b/R/bonjour-package.R index 4fc4284..d9de1aa 100644 --- a/R/bonjour-package.R +++ b/R/bonjour-package.R @@ -14,7 +14,7 @@ #' @name bonjour #' @keywords internal #' @author Bob Rudis (bob@@rud.is) -#' @importFrom ndjson flatten +#' @importFrom jsonlite stream_in ## usethis namespace: start #' @importFrom Rcpp sourceCpp #' @useDynLib bonjour, .registration = TRUE diff --git a/R/discover.R b/R/discover.R index c77146a..1666339 100644 --- a/R/discover.R +++ b/R/discover.R @@ -7,8 +7,11 @@ bnjr_discover <- function(scan_time = 10L) { res <- int_bnjr_discover(scan_time) - res <- unlist(strsplit(res, "\n")) - ndjson::flatten(res, "tbl") + # res <- unlist(strsplit(res, "\n", useBytes = TRUE)) + # ndjson::flatten(res, "tbl") + res <- jsonlite::stream_in(textConnection(res), verbose = FALSE) + class(res) <- c("tbl_df", "tbl", "data.frame") + res } diff --git a/R/query.R b/R/query.R index fb38bfb..29675c8 100644 --- a/R/query.R +++ b/R/query.R @@ -8,8 +8,9 @@ bnjr_query <- function(query, scan_time = 10L) { res <- int_bnjr_query(query, scan_time) - res <- unlist(strsplit(res, "\n")) - ndjson::flatten(res, "tbl") + res <- jsonlite::stream_in(textConnection(res), verbose = FALSE) + class(res) <- c("tbl_df", "tbl", "data.frame") + res } diff --git a/README.md b/README.md index 75f1993..4acc047 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ library(tidyverse) # current version packageVersion("bonjour") -## [1] '0.2.0' +## [1] '0.3.0' ``` ``` r @@ -106,11 +106,11 @@ bnjr_query("_ssh._tcp.local.") %>% | Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) | | :----------- | -------: | ---: | ---: | ---: | ----------: | ---: | -------: | ---: | -| C/C++ Header | 1 | 0.06 | 980 | 0.33 | 159 | 0.26 | 129 | 0.26 | -| C++ | 2 | 0.11 | 462 | 0.16 | 113 | 0.19 | 40 | 0.08 | -| R | 5 | 0.28 | 24 | 0.01 | 13 | 0.02 | 44 | 0.09 | -| Rmd | 1 | 0.06 | 13 | 0.00 | 17 | 0.03 | 31 | 0.06 | -| SUM | 9 | 0.50 | 1479 | 0.50 | 302 | 0.50 | 244 | 0.50 | +| C/C++ Header | 2 | 0.10 | 1062 | 0.34 | 178 | 0.28 | 152 | 0.28 | +| C++ | 2 | 0.10 | 468 | 0.15 | 114 | 0.18 | 42 | 0.08 | +| R | 5 | 0.25 | 26 | 0.01 | 13 | 0.02 | 46 | 0.08 | +| Rmd | 1 | 0.05 | 13 | 0.00 | 17 | 0.03 | 31 | 0.06 | +| SUM | 10 | 0.50 | 1569 | 0.50 | 322 | 0.50 | 271 | 0.50 | clock Package Metrics for bonjour diff --git a/src/b64.h b/src/b64.h new file mode 100644 index 0000000..6cd0763 --- /dev/null +++ b/src/b64.h @@ -0,0 +1,124 @@ +#ifndef _MACARON_BASE64_H_ +#define _MACARON_BASE64_H_ + +/** + * The MIT License (MIT) + * Copyright (c) 2016 tomykaira + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include + +namespace macaron { + +class Base64 { +public: + + static std::string Encode(const std::string data) { + static constexpr char sEncodingTable[] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '+', '/' + }; + + size_t in_len = data.size(); + size_t out_len = 4 * ((in_len + 2) / 3); + std::string ret(out_len, '\0'); + size_t i; + char *p = const_cast(ret.c_str()); + + for (i = 0; i < in_len - 2; i += 3) { + *p++ = sEncodingTable[(data[i] >> 2) & 0x3F]; + *p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)]; + *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | ((int) (data[i + 2] & 0xC0) >> 6)]; + *p++ = sEncodingTable[data[i + 2] & 0x3F]; + } + if (i < in_len) { + *p++ = sEncodingTable[(data[i] >> 2) & 0x3F]; + if (i == (in_len - 1)) { + *p++ = sEncodingTable[((data[i] & 0x3) << 4)]; + *p++ = '='; + } + else { + *p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)]; + *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)]; + } + *p++ = '='; + } + + return ret; + } + + static std::string Decode(const std::string& input, std::string& out) { + static constexpr unsigned char kDecodingTable[] = { + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, + 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, + 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 + }; + + size_t in_len = input.size(); + if (in_len % 4 != 0) return "Input data size is not a multiple of 4"; + + size_t out_len = in_len / 4 * 3; + if (input[in_len - 1] == '=') out_len--; + if (input[in_len - 2] == '=') out_len--; + + out.resize(out_len); + + for (size_t i = 0, j = 0; i < in_len;) { + uint32_t a = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast(input[i++])]; + uint32_t b = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast(input[i++])]; + uint32_t c = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast(input[i++])]; + uint32_t d = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast(input[i++])]; + + uint32_t triple = (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6); + + if (j < out_len) out[j++] = (triple >> 2 * 8) & 0xFF; + if (j < out_len) out[j++] = (triple >> 1 * 8) & 0xFF; + if (j < out_len) out[j++] = (triple >> 0 * 8) & 0xFF; + } + + return ""; + } + +}; + +} + +#endif /* _MACARON_BASE64_H_ */ \ No newline at end of file diff --git a/src/bonjour-main.cpp b/src/bonjour-main.cpp index 47e3e41..eb6b41a 100644 --- a/src/bonjour-main.cpp +++ b/src/bonjour-main.cpp @@ -6,6 +6,7 @@ using namespace Rcpp; #include "mdns.h" +#include "b64.h" #include #include @@ -38,8 +39,6 @@ typedef struct { int port; } service_record_t; - - static mdns_string_t ipv4_address_to_string(char* buffer, size_t capacity, const struct sockaddr_in* addr, size_t addrlen) { @@ -93,141 +92,60 @@ static mdns_string_t -static int - open_client_sockets(int* sockets, int max_sockets, int port) { - // When sending, each socket can only send to one network interface - // Thus we need to open one socket for each interface and address family - int num_sockets = 0; +static int open_client_sockets(int* sockets, int max_sockets, int port) { + // When sending, each socket can only send to one network interface + // Thus we need to open one socket for each interface and address family + int num_sockets = 0; #ifdef _WIN32 - IP_ADAPTER_ADDRESSES* adapter_address = 0; - ULONG address_size = 8000; - unsigned int ret; - unsigned int num_retries = 4; - do { - adapter_address = malloc(address_size); - ret = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_ANYCAST, 0, - adapter_address, &address_size); - if (ret == ERROR_BUFFER_OVERFLOW) { - free(adapter_address); - adapter_address = 0; - } else { - break; - } - } while (num_retries-- > 0); - - if (!adapter_address || (ret != NO_ERROR)) { + IP_ADAPTER_ADDRESSES* adapter_address = 0; + ULONG address_size = 8000; + unsigned int ret; + unsigned int num_retries = 4; + do { + adapter_address = malloc(address_size); + ret = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_ANYCAST, 0, + adapter_address, &address_size); + if (ret == ERROR_BUFFER_OVERFLOW) { free(adapter_address); - printf("Failed to get network adapter addresses\n"); - return num_sockets; - } - - int first_ipv4 = 1; - int first_ipv6 = 1; - for (PIP_ADAPTER_ADDRESSES adapter = adapter_address; adapter; adapter = adapter->Next) { - if (adapter->TunnelType == TUNNEL_TYPE_TEREDO) - continue; - if (adapter->OperStatus != IfOperStatusUp) - continue; - - for (IP_ADAPTER_UNICAST_ADDRESS* unicast = adapter->FirstUnicastAddress; unicast; - unicast = unicast->Next) { - if (unicast->Address.lpSockaddr->sa_family == AF_INET) { - struct sockaddr_in* saddr = (struct sockaddr_in*)unicast->Address.lpSockaddr; - if ((saddr->sin_addr.S_un.S_un_b.s_b1 != 127) || - (saddr->sin_addr.S_un.S_un_b.s_b2 != 0) || - (saddr->sin_addr.S_un.S_un_b.s_b3 != 0) || - (saddr->sin_addr.S_un.S_un_b.s_b4 != 1)) { - int log_addr = 0; - if (first_ipv4) { - service_address_ipv4 = saddr->sin_addr.S_un.S_addr; - first_ipv4 = 0; - log_addr = 1; - } - has_ipv4 = 1; - if (num_sockets < max_sockets) { - saddr->sin_port = htons((unsigned short)port); - int sock = mdns_socket_open_ipv4(saddr); - if (sock >= 0) { - sockets[num_sockets++] = sock; - log_addr = 1; - } else { - log_addr = 0; - } - } - if (log_addr) { - char buffer[128]; - mdns_string_t addr = ipv4_address_to_string(buffer, sizeof(buffer), saddr, - sizeof(struct sockaddr_in)); - // printf("Local IPv4 address: %.*s\n", MDNS_STRING_FORMAT(addr)); - } - } - } else if (unicast->Address.lpSockaddr->sa_family == AF_INET6) { - struct sockaddr_in6* saddr = (struct sockaddr_in6*)unicast->Address.lpSockaddr; - static const unsigned char localhost[] = {0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 1}; - static const unsigned char localhost_mapped[] = {0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0xff, 0xff, 0x7f, 0, 0, 1}; - if ((unicast->DadState == NldsPreferred) && - memcmp(saddr->sin6_addr.s6_addr, localhost, 16) && - memcmp(saddr->sin6_addr.s6_addr, localhost_mapped, 16)) { - int log_addr = 0; - if (first_ipv6) { - memcpy(service_address_ipv6, &saddr->sin6_addr, 16); - first_ipv6 = 0; - log_addr = 1; - } - has_ipv6 = 1; - if (num_sockets < max_sockets) { - saddr->sin6_port = htons((unsigned short)port); - int sock = mdns_socket_open_ipv6(saddr); - if (sock >= 0) { - sockets[num_sockets++] = sock; - log_addr = 1; - } else { - log_addr = 0; - } - } - if (log_addr) { - char buffer[128]; - mdns_string_t addr = ipv6_address_to_string(buffer, sizeof(buffer), saddr, - sizeof(struct sockaddr_in6)); - // printf("Local IPv6 address: %.*s\n", MDNS_STRING_FORMAT(addr)); - } - } - } - } + adapter_address = 0; + } else { + break; } + } while (num_retries-- > 0); + if (!adapter_address || (ret != NO_ERROR)) { free(adapter_address); + printf("Failed to get network adapter addresses\n"); + return num_sockets; + } -#else - - struct ifaddrs* ifaddr = 0; - struct ifaddrs* ifa = 0; - - if (getifaddrs(&ifaddr) < 0) - printf("Unable to get interface addresses\n"); - - int first_ipv4 = 1; - int first_ipv6 = 1; - for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) { - if (!ifa->ifa_addr) - continue; - - if (ifa->ifa_addr->sa_family == AF_INET) { - struct sockaddr_in* saddr = (struct sockaddr_in*)ifa->ifa_addr; - if (saddr->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) { + int first_ipv4 = 1; + int first_ipv6 = 1; + for (PIP_ADAPTER_ADDRESSES adapter = adapter_address; adapter; adapter = adapter->Next) { + if (adapter->TunnelType == TUNNEL_TYPE_TEREDO) + continue; + if (adapter->OperStatus != IfOperStatusUp) + continue; + + for (IP_ADAPTER_UNICAST_ADDRESS* unicast = adapter->FirstUnicastAddress; unicast; + unicast = unicast->Next) { + if (unicast->Address.lpSockaddr->sa_family == AF_INET) { + struct sockaddr_in* saddr = (struct sockaddr_in*)unicast->Address.lpSockaddr; + if ((saddr->sin_addr.S_un.S_un_b.s_b1 != 127) || + (saddr->sin_addr.S_un.S_un_b.s_b2 != 0) || + (saddr->sin_addr.S_un.S_un_b.s_b3 != 0) || + (saddr->sin_addr.S_un.S_un_b.s_b4 != 1)) { int log_addr = 0; if (first_ipv4) { - service_address_ipv4 = saddr->sin_addr.s_addr; + service_address_ipv4 = saddr->sin_addr.S_un.S_addr; first_ipv4 = 0; log_addr = 1; } has_ipv4 = 1; if (num_sockets < max_sockets) { - saddr->sin_port = htons(port); + saddr->sin_port = htons((unsigned short)port); int sock = mdns_socket_open_ipv4(saddr); if (sock >= 0) { sockets[num_sockets++] = sock; @@ -243,13 +161,14 @@ static int // printf("Local IPv4 address: %.*s\n", MDNS_STRING_FORMAT(addr)); } } - } else if (ifa->ifa_addr->sa_family == AF_INET6) { - struct sockaddr_in6* saddr = (struct sockaddr_in6*)ifa->ifa_addr; + } else if (unicast->Address.lpSockaddr->sa_family == AF_INET6) { + struct sockaddr_in6* saddr = (struct sockaddr_in6*)unicast->Address.lpSockaddr; static const unsigned char localhost[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; static const unsigned char localhost_mapped[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x7f, 0, 0, 1}; - if (memcmp(saddr->sin6_addr.s6_addr, localhost, 16) && + if ((unicast->DadState == NldsPreferred) && + memcmp(saddr->sin6_addr.s6_addr, localhost, 16) && memcmp(saddr->sin6_addr.s6_addr, localhost_mapped, 16)) { int log_addr = 0; if (first_ipv6) { @@ -259,7 +178,7 @@ static int } has_ipv6 = 1; if (num_sockets < max_sockets) { - saddr->sin6_port = htons(port); + saddr->sin6_port = htons((unsigned short)port); int sock = mdns_socket_open_ipv6(saddr); if (sock >= 0) { sockets[num_sockets++] = sock; @@ -277,14 +196,93 @@ static int } } } + } - freeifaddrs(ifaddr); + free(adapter_address); -#endif +#else - return num_sockets; + struct ifaddrs* ifaddr = 0; + struct ifaddrs* ifa = 0; + + if (getifaddrs(&ifaddr) < 0) + printf("Unable to get interface addresses\n"); + + int first_ipv4 = 1; + int first_ipv6 = 1; + for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) { + if (!ifa->ifa_addr) + continue; + + if (ifa->ifa_addr->sa_family == AF_INET) { + struct sockaddr_in* saddr = (struct sockaddr_in*)ifa->ifa_addr; + if (saddr->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) { + int log_addr = 0; + if (first_ipv4) { + service_address_ipv4 = saddr->sin_addr.s_addr; + first_ipv4 = 0; + log_addr = 1; + } + has_ipv4 = 1; + if (num_sockets < max_sockets) { + saddr->sin_port = htons(port); + int sock = mdns_socket_open_ipv4(saddr); + if (sock >= 0) { + sockets[num_sockets++] = sock; + log_addr = 1; + } else { + log_addr = 0; + } + } + if (log_addr) { + char buffer[128]; + mdns_string_t addr = ipv4_address_to_string(buffer, sizeof(buffer), saddr, + sizeof(struct sockaddr_in)); + // printf("Local IPv4 address: %.*s\n", MDNS_STRING_FORMAT(addr)); + } + } + } else if (ifa->ifa_addr->sa_family == AF_INET6) { + struct sockaddr_in6* saddr = (struct sockaddr_in6*)ifa->ifa_addr; + static const unsigned char localhost[] = {0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1}; + static const unsigned char localhost_mapped[] = {0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0xff, 0xff, 0x7f, 0, 0, 1}; + if (memcmp(saddr->sin6_addr.s6_addr, localhost, 16) && + memcmp(saddr->sin6_addr.s6_addr, localhost_mapped, 16)) { + int log_addr = 0; + if (first_ipv6) { + memcpy(service_address_ipv6, &saddr->sin6_addr, 16); + first_ipv6 = 0; + log_addr = 1; + } + has_ipv6 = 1; + if (num_sockets < max_sockets) { + saddr->sin6_port = htons(port); + int sock = mdns_socket_open_ipv6(saddr); + if (sock >= 0) { + sockets[num_sockets++] = sock; + log_addr = 1; + } else { + log_addr = 0; + } + } + if (log_addr) { + char buffer[128]; + mdns_string_t addr = ipv6_address_to_string(buffer, sizeof(buffer), saddr, + sizeof(struct sockaddr_in6)); + // printf("Local IPv6 address: %.*s\n", MDNS_STRING_FORMAT(addr)); + } + } + } } + freeifaddrs(ifaddr); + +#endif + + return num_sockets; +} + static int query_callback(int sock, const struct sockaddr* from, size_t addrlen, @@ -380,14 +378,23 @@ static int query_callback(int sock, size_t parsed = mdns_record_parse_txt(data, size, offset, length, txtbuffer, sizeof(txtbuffer) / sizeof(mdns_record_txt_t)); + rec = rec + ", \"type\": \"TXT\", \"info\": [ "; + for (size_t itxt = 0; itxt < parsed; ++itxt) { - rec = rec + ", \"type\": \"TXT\""; + if (itxt > 0) rec = rec + ", "; if (txtbuffer[itxt].value.length) { - rec = rec + ", \"key\": \"" + std::string(txtbuffer[itxt].key.str, txtbuffer[itxt].key.length) + "\""; - rec = rec + ", \"key\": \"" + std::string(txtbuffer[itxt].value.str, txtbuffer[itxt].value.length) + "\""; + rec = rec + "{ "; + rec = rec + "\"key\":"; + rec = rec + "\"" + std::string(txtbuffer[itxt].key.str, txtbuffer[itxt].key.length) + "\""; + rec = rec + ", \"value\":" ; + rec = rec + "\"" + macaron::Base64::Encode(std::string(txtbuffer[itxt].value.str, txtbuffer[itxt].value.length)) + "\""; + rec = rec + " }"; + + // rec = rec + "\"key\": \"" + std::string(txtbuffer[itxt].key.str, txtbuffer[itxt].key.length) + "\""; + // rec = rec + "\"key\": \"" + std::string(txtbuffer[itxt].value.str, txtbuffer[itxt].value.length) + "\""; // printf("%.*s : %s TXT %.*s = %.*s\n", // MDNS_STRING_FORMAT(fromaddrstr), entrytype, @@ -396,7 +403,7 @@ static int query_callback(int sock, } else { - rec = rec + ", \"key\": \"" + std::string(txtbuffer[itxt].key.str, txtbuffer[itxt].key.length) + "\""; + rec = rec + "{ \"value\": \"" + macaron::Base64::Encode(std::string(txtbuffer[itxt].key.str, txtbuffer[itxt].key.length)) + "\" }"; // printf("%.*s : %s TXT %.*s\n", // MDNS_STRING_FORMAT(fromaddrstr), entrytype, @@ -405,6 +412,8 @@ static int query_callback(int sock, } + rec = rec + " ]"; + } else { rec = rec + ", \"type\": \"" + std::to_string((long)entrytype) + "";