Skip to content

Commit f9d3ec5

Browse files
committed
Support for publishing TXT records via mDNS
- Update lwIP+Raspberry Pi implementation to use lwIP API correctly - Add translations
1 parent 5f95232 commit f9d3ec5

File tree

7 files changed

+92
-8
lines changed

7 files changed

+92
-8
lines changed

locale/circuitpython.pot

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,15 @@ msgstr ""
10181018
msgid "Failed to acquire mutex, err 0x%04x"
10191019
msgstr ""
10201020

1021+
#: ports/raspberrypi/common-hal/mdns/Server.c
1022+
msgid "Failed to add service TXT record"
1023+
msgstr ""
1024+
1025+
#: shared-bindings/mdns/Server.c
1026+
msgid ""
1027+
"Failed to add service TXT record; non-string or bytes found in txt_records"
1028+
msgstr ""
1029+
10211030
#: shared-module/rgbmatrix/RGBMatrix.c
10221031
msgid "Failed to allocate %q buffer"
10231032
msgstr ""

ports/espressif/common-hal/mdns/Server.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,25 @@ mp_obj_t common_hal_mdns_server_find(mdns_server_obj_t *self, const char *servic
208208
return MP_OBJ_FROM_PTR(tuple);
209209
}
210210

211-
void common_hal_mdns_server_advertise_service(mdns_server_obj_t *self, const char *service_type, const char *protocol, mp_int_t port) {
211+
void common_hal_mdns_server_advertise_service(mdns_server_obj_t *self, const char *service_type, const char *protocol, mp_int_t port, const char *txt_records[], size_t num_txt_records) {
212212
if (mdns_service_exists(service_type, protocol, NULL)) {
213213
mdns_service_port_set(service_type, protocol, port);
214214
} else {
215+
// TODO: Add support for TXT record
216+
/* NOTE: The `mdns_txt_item_t *txt` argument of mdns_service_add uses a struct
217+
* that splits out the TXT record into keys and values, though it seems little
218+
* is done with those fields aside from concatenating them with an optional
219+
* equals sign and calculating the total length of the concatenated string.
220+
*
221+
* There should be little issue with the underlying implementation to populate
222+
* the mdns_txt_item_t struct with only a key containing exactly the desired TXT
223+
* record. As long as the underlying implementation calculates the length of the
224+
* key + NULL value correctly, it should work.
225+
*
226+
* Ref: RFC 6763, section 6.1:
227+
* > The format of each constituent string within the DNS TXT record is a single
228+
* > length byte, followed by 0-255 bytes of text data.
229+
*/
215230
mdns_service_add(NULL, service_type, protocol, port, NULL, 0);
216231
}
217232
}

ports/raspberrypi/common-hal/mdns/Server.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "py/runtime.h"
3131
#include "shared/runtime/interrupt_char.h"
3232
#include "shared-bindings/mdns/RemoteService.h"
33+
#include "shared-bindings/mdns/Server.h"
3334
#include "shared-bindings/wifi/__init__.h"
3435
#include "supervisor/shared/tick.h"
3536

@@ -295,7 +296,27 @@ mp_obj_t common_hal_mdns_server_find(mdns_server_obj_t *self, const char *servic
295296
return MP_OBJ_FROM_PTR(tuple);
296297
}
297298

298-
void common_hal_mdns_server_advertise_service(mdns_server_obj_t *self, const char *service_type, const char *protocol, mp_int_t port) {
299+
STATIC void srv_txt_cb(struct mdns_service *service, void *ptr) {
300+
mdns_server_obj_t *self = ptr;
301+
err_t res;
302+
for (size_t i = 0; i < self->num_txt_records; i++) {
303+
res = mdns_resp_add_service_txtitem(service, self->txt_records[i], strlen(self->txt_records[i]));
304+
if (res != ERR_OK) {
305+
mp_raise_RuntimeError(translate("Failed to add service TXT record"));
306+
return;
307+
}
308+
}
309+
}
310+
311+
STATIC void assign_txt_records(mdns_server_obj_t *self, const char *txt_records[], size_t num_txt_records) {
312+
size_t allowed_num_txt_records = MDNS_MAX_TXT_RECORDS < num_txt_records ? MDNS_MAX_TXT_RECORDS : num_txt_records;
313+
self->num_txt_records = allowed_num_txt_records;
314+
for (size_t i = 0; i < allowed_num_txt_records; i++) {
315+
self->txt_records[i] = txt_records[i];
316+
}
317+
}
318+
319+
void common_hal_mdns_server_advertise_service(mdns_server_obj_t *self, const char *service_type, const char *protocol, mp_int_t port, const char *txt_records[], size_t num_txt_records) {
299320
enum mdns_sd_proto proto = DNSSD_PROTO_UDP;
300321
if (strcmp(protocol, "_tcp") == 0) {
301322
proto = DNSSD_PROTO_TCP;
@@ -313,7 +334,9 @@ void common_hal_mdns_server_advertise_service(mdns_server_obj_t *self, const cha
313334
if (existing_slot < MDNS_MAX_SERVICES) {
314335
mdns_resp_del_service(NETIF_STA, existing_slot);
315336
}
316-
int8_t slot = mdns_resp_add_service(NETIF_STA, self->instance_name, service_type, proto, port, NULL, NULL);
337+
338+
assign_txt_records(self, txt_records, num_txt_records);
339+
int8_t slot = mdns_resp_add_service(NETIF_STA, self->instance_name, service_type, proto, port, srv_txt_cb, self);
317340
if (slot < 0) {
318341
mp_raise_RuntimeError(MP_ERROR_TEXT("Out of MDNS service slots"));
319342
return;

ports/raspberrypi/common-hal/mdns/Server.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@
3030

3131
#include "lwip/apps/mdns_opts.h"
3232

33+
#define MDNS_MAX_TXT_RECORDS 32
34+
3335
typedef struct {
3436
mp_obj_base_t base;
3537
const char *hostname;
3638
const char *instance_name;
3739
char default_hostname[sizeof("cpy-XXXXXX")];
3840
const char *service_type[MDNS_MAX_SERVICES];
41+
size_t num_txt_records;
42+
const char *txt_records[MDNS_MAX_TXT_RECORDS];
3943
// Track if this object owns access to the underlying MDNS service.
4044
bool inited;
4145
} mdns_server_obj_t;

shared-bindings/mdns/Server.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727

2828
#include <string.h>
2929

30+
#include "py/obj.h"
3031
#include "py/objproperty.h"
32+
#include "py/objstr.h"
3133
#include "py/runtime.h"
3234
#include "shared-bindings/mdns/__init__.h"
3335
#include "shared-bindings/mdns/Server.h"
@@ -173,20 +175,26 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mdns_server_find_obj, 1, _mdns_server_find);
173175
//|
174176
//| If web workflow is active, the port it uses can't also be used to advertise a service.
175177
//|
178+
//| **Limitations**: Publishing up to 32 TXT records is only supported on the RP2040 Pico W board at
179+
//| this time.
180+
//|
176181
//| :param str service_type: The service type such as "_http"
177182
//| :param str protocol: The service protocol such as "_tcp"
178-
//| :param int port: The port used by the service"""
183+
//| :param int port: The port used by the service
184+
//| :param Sequence[str] txt_records: An optional sequence of strings to serve as TXT records along with the service
185+
//| """
179186
//| ...
180187
//|
181188
STATIC mp_obj_t mdns_server_advertise_service(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
182189
mdns_server_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
183190
check_for_deinit(self);
184191

185-
enum { ARG_service_type, ARG_protocol, ARG_port };
192+
enum { ARG_service_type, ARG_protocol, ARG_port, ARG_txt_records };
186193
static const mp_arg_t allowed_args[] = {
187194
{ MP_QSTR_service_type, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
188195
{ MP_QSTR_protocol, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
189196
{ MP_QSTR_port, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
197+
{ MP_QSTR_txt_records, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
190198
};
191199

192200
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -195,7 +203,21 @@ STATIC mp_obj_t mdns_server_advertise_service(mp_uint_t n_args, const mp_obj_t *
195203
const char *service_type = mp_obj_str_get_str(args[ARG_service_type].u_obj);
196204
const char *protocol = mp_obj_str_get_str(args[ARG_protocol].u_obj);
197205

198-
common_hal_mdns_server_advertise_service(self, service_type, protocol, args[ARG_port].u_int);
206+
const mp_obj_t txt_records = args[ARG_txt_records].u_obj;
207+
const size_t num_txt_records = txt_records == mp_const_none
208+
? 0
209+
: (size_t)MP_OBJ_SMALL_INT_VALUE(mp_obj_len(txt_records));
210+
211+
const char *txt_records_array[num_txt_records];
212+
for (size_t i = 0; i < num_txt_records; i++) {
213+
mp_obj_t txt_record = mp_obj_subscr(txt_records, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL);
214+
if (!mp_obj_is_str_or_bytes(txt_record)) {
215+
mp_raise_ValueError(translate("Failed to add service TXT record; non-string or bytes found in txt_records"));
216+
}
217+
txt_records_array[i] = mp_obj_str_get_str(txt_record);
218+
}
219+
220+
common_hal_mdns_server_advertise_service(self, service_type, protocol, args[ARG_port].u_int, txt_records_array, num_txt_records);
199221
return mp_const_none;
200222
}
201223
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mdns_server_advertise_service_obj, 1, mdns_server_advertise_service);

shared-bindings/mdns/Server.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,18 @@ void common_hal_mdns_server_set_hostname(mdns_server_obj_t *self, const char *ho
4242
const char *common_hal_mdns_server_get_instance_name(mdns_server_obj_t *self);
4343
void common_hal_mdns_server_set_instance_name(mdns_server_obj_t *self, const char *instance_name);
4444
mp_obj_t common_hal_mdns_server_find(mdns_server_obj_t *self, const char *service_type, const char *protocol, mp_float_t timeout);
45-
void common_hal_mdns_server_advertise_service(mdns_server_obj_t *self, const char *service_type, const char *protocol, mp_int_t port);
45+
46+
/**
47+
* @brief Advertises service
48+
*
49+
* @param self
50+
* @param service_type A string indicating the DNS-SD type of service being advertised (e.g., _http)
51+
* @param protocol A string indicating the DNS-SD protocol of the service (e.g., _tcp or _udp)
52+
* @param port The TCP or UDP port number of the service
53+
* @param txt_records An array of strings representing TXT records to publish along with the service
54+
* @param num_txt_records Number of records expected in txt_records
55+
*/
56+
void common_hal_mdns_server_advertise_service(mdns_server_obj_t *self, const char *service_type, const char *protocol, mp_int_t port, const char *txt_records[], size_t num_txt_records);
4657

4758
// For internal use.
4859
void mdns_server_construct(mdns_server_obj_t *self, bool workflow);

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ bool supervisor_start_web_workflow(bool reload) {
355355
}
356356
}
357357
if (!common_hal_mdns_server_deinited(&mdns)) {
358-
common_hal_mdns_server_advertise_service(&mdns, "_circuitpython", "_tcp", web_api_port);
358+
common_hal_mdns_server_advertise_service(&mdns, "_circuitpython", "_tcp", web_api_port, NULL, 0);
359359
}
360360
#endif
361361

0 commit comments

Comments
 (0)