Skip to content

Commit 5c517b7

Browse files
committed
Handle MDNS deinited better.
1 parent ca80f30 commit 5c517b7

File tree

5 files changed

+62
-30
lines changed

5 files changed

+62
-30
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ STATIC bool inited = false;
3737

3838
void mdns_server_construct(mdns_server_obj_t *self, bool workflow) {
3939
if (inited) {
40+
self->inited = false;
4041
return;
4142
}
4243
mdns_init();
@@ -46,6 +47,8 @@ void mdns_server_construct(mdns_server_obj_t *self, bool workflow) {
4647
snprintf(self->default_hostname, sizeof(self->default_hostname), "cpy-%02x%02x%02x", mac[3], mac[4], mac[5]);
4748
common_hal_mdns_server_set_hostname(self, self->default_hostname);
4849

50+
self->inited = true;
51+
4952
if (workflow) {
5053
// Set a delegated entry to ourselves. This allows us to respond to "circuitpython.local"
5154
// queries as well.
@@ -67,21 +70,23 @@ void common_hal_mdns_server_construct(mdns_server_obj_t *self, mp_obj_t network_
6770
mp_raise_ValueError(translate("mDNS only works with built-in WiFi"));
6871
return;
6972
}
70-
if (inited) {
73+
mdns_server_construct(self, false);
74+
if (common_hal_mdns_server_deinited(self)) {
7175
mp_raise_RuntimeError(translate("mDNS already initialized"));
7276
}
73-
mdns_server_construct(self, false);
7477
}
7578

7679
void common_hal_mdns_server_deinit(mdns_server_obj_t *self) {
80+
if (common_hal_mdns_server_deinited(self)) {
81+
return;
82+
}
83+
self->inited = false;
7784
inited = false;
7885
mdns_free();
7986
}
8087

8188
bool common_hal_mdns_server_deinited(mdns_server_obj_t *self) {
82-
// This returns INVALID_STATE when not initialized and INVALID_PARAM when it
83-
// is.
84-
return mdns_instance_name_set(NULL) == ESP_ERR_INVALID_STATE;
89+
return !self->inited;
8590
}
8691

8792
const char *common_hal_mdns_server_get_hostname(mdns_server_obj_t *self) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ typedef struct {
3434
const char *instance_name;
3535
// "cpy-" "XXXXXX" "\0"
3636
char default_hostname[4 + 6 + 1];
37+
bool inited;
3738
} mdns_server_obj_t;

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,22 @@
3636
#include "lwip/apps/mdns.h"
3737
#include "lwip/prot/dns.h"
3838

39+
// Track if we are globally inited. This essentially forces one inited MDNS
40+
// object at a time. (But ignores MDNS objects that are deinited.)
3941
STATIC bool inited = false;
4042

4143
#define NETIF_STA (&cyw43_state.netif[CYW43_ITF_STA])
4244
#define NETIF_AP (&cyw43_state.netif[CYW43_ITF_AP])
4345

4446
void mdns_server_construct(mdns_server_obj_t *self, bool workflow) {
4547
if (inited) {
48+
self->inited = false;
4649
return;
4750
}
4851

4952
mdns_resp_init();
5053
inited = true;
54+
self->inited = true;
5155

5256
uint8_t mac[6];
5357
wifi_radio_get_mac_address(&common_hal_wifi_radio_obj, mac);
@@ -75,12 +79,16 @@ void common_hal_mdns_server_construct(mdns_server_obj_t *self, mp_obj_t network_
7579
}
7680

7781
void common_hal_mdns_server_deinit(mdns_server_obj_t *self) {
82+
if (common_hal_mdns_server_deinited(self)) {
83+
return;
84+
}
85+
self->inited = false;
7886
inited = false;
7987
mdns_resp_remove_netif(NETIF_STA);
8088
}
8189

8290
bool common_hal_mdns_server_deinited(mdns_server_obj_t *self) {
83-
return !mdns_resp_netif_active(NETIF_STA);
91+
return !self->inited;
8492
}
8593

8694
const char *common_hal_mdns_server_get_hostname(mdns_server_obj_t *self) {
@@ -215,7 +223,6 @@ STATIC void alloc_search_result_cb(struct mdns_answer *answer, const char *varpa
215223
if ((flags & MDNS_SEARCH_RESULT_FIRST) != 0) {
216224
// first
217225
mdns_remoteservice_obj_t *service = gc_alloc(sizeof(mdns_remoteservice_obj_t), 0, false);
218-
mp_printf(&mp_plat_print, "found service %p\n", service);
219226
if (service == NULL) {
220227
// alloc fails
221228
mdns_search_stop(state->request_id);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ typedef struct {
3737
// "cpy-" "XXXXXX" "\0"
3838
char default_hostname[4 + 6 + 1];
3939
const char *service_type[MDNS_MAX_SERVICES];
40+
bool inited;
4041
} mdns_server_obj_t;

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ STATIC void _update_encoded_ip(void) {
202202

203203
mdns_server_obj_t *supervisor_web_workflow_mdns(mp_obj_t network_interface) {
204204
#if CIRCUITPY_MDNS
205-
if (network_interface == &common_hal_wifi_radio_obj) {
205+
if (network_interface == &common_hal_wifi_radio_obj &&
206+
mdns.base.type == &mdns_server_type) {
206207
return &mdns;
207208
}
208209
#endif
@@ -309,11 +310,6 @@ void supervisor_start_web_workflow(void) {
309310

310311
if (first_start) {
311312
port_changed = false;
312-
#if CIRCUITPY_MDNS
313-
mdns_server_construct(&mdns, true);
314-
mdns.base.type = &mdns_server_type;
315-
common_hal_mdns_server_set_instance_name(&mdns, MICROPY_HW_BOARD_NAME);
316-
#endif
317313
pool.base.type = &socketpool_socketpool_type;
318314
common_hal_socketpool_socketpool_construct(&pool, &common_hal_wifi_radio_obj);
319315

@@ -322,13 +318,26 @@ void supervisor_start_web_workflow(void) {
322318

323319
websocket_init();
324320
}
321+
#if CIRCUITPY_MDNS
322+
// Try to start MDNS if the user deinited it.
323+
if (mdns.base.type != &mdns_server_type ||
324+
common_hal_mdns_server_deinited(&mdns)) {
325+
mdns_server_construct(&mdns, true);
326+
mdns.base.type = &mdns_server_type;
327+
if (!common_hal_mdns_server_deinited(&mdns)) {
328+
common_hal_mdns_server_set_instance_name(&mdns, MICROPY_HW_BOARD_NAME);
329+
}
330+
}
331+
#endif
325332
if (port_changed) {
326333
common_hal_socketpool_socket_close(&listening);
327334
}
328335
if (first_start || port_changed) {
329336
web_api_port = new_port;
330337
#if CIRCUITPY_MDNS
331-
common_hal_mdns_server_advertise_service(&mdns, "_circuitpython", "_tcp", web_api_port);
338+
if (!common_hal_mdns_server_deinited(&mdns)) {
339+
common_hal_mdns_server_advertise_service(&mdns, "_circuitpython", "_tcp", web_api_port);
340+
}
332341
#endif
333342
socketpool_socket(&pool, SOCKETPOOL_AF_INET, SOCKETPOOL_SOCK_STREAM, &listening);
334343
common_hal_socketpool_socket_settimeout(&listening, 0);
@@ -453,17 +462,18 @@ static bool _origin_ok(const char *origin) {
453462
}
454463
// These are prefix checks up to : so that any port works.
455464
// TODO: Support DHCP hostname in addition to MDNS.
465+
const char *end;
456466
#if CIRCUITPY_MDNS
457-
const char *local = ".local";
458-
const char *hostname = common_hal_mdns_server_get_hostname(&mdns);
459-
const char *end = origin + strlen(http) + strlen(hostname) + strlen(local);
460-
if (strncmp(origin + strlen(http), hostname, strlen(hostname)) == 0 &&
461-
strncmp(origin + strlen(http) + strlen(hostname), local, strlen(local)) == 0 &&
462-
(end[0] == '\0' || end[0] == ':')) {
463-
return true;
467+
if (!common_hal_mdns_server_deinited(&mdns)) {
468+
const char *local = ".local";
469+
const char *hostname = common_hal_mdns_server_get_hostname(&mdns);
470+
end = origin + strlen(http) + strlen(hostname) + strlen(local);
471+
if (strncmp(origin + strlen(http), hostname, strlen(hostname)) == 0 &&
472+
strncmp(origin + strlen(http) + strlen(hostname), local, strlen(local)) == 0 &&
473+
(end[0] == '\0' || end[0] == ':')) {
474+
return true;
475+
}
464476
}
465-
#else
466-
const char *end;
467477
#endif
468478

469479
_update_encoded_ip();
@@ -742,12 +752,13 @@ static void _reply_with_file(socketpool_socket_obj_t *socket, _request *request,
742752
}
743753

744754
static void _reply_with_devices_json(socketpool_socket_obj_t *socket, _request *request) {
755+
size_t total_results = 0;
745756
#if CIRCUITPY_MDNS
746757
mdns_remoteservice_obj_t found_devices[32];
747-
size_t total_results = mdns_server_find(&mdns, "_circuitpython", "_tcp", 1, found_devices, MP_ARRAY_SIZE(found_devices));
758+
if (!common_hal_mdns_server_deinited(&mdns)) {
759+
total_results = mdns_server_find(&mdns, "_circuitpython", "_tcp", 1, found_devices, MP_ARRAY_SIZE(found_devices));
760+
}
748761
size_t count = MIN(total_results, MP_ARRAY_SIZE(found_devices));
749-
#else
750-
size_t total_results = 0;
751762
#endif
752763
socketpool_socket_send(socket, (const uint8_t *)OK_JSON, strlen(OK_JSON));
753764
_cors_header(socket, request);
@@ -784,10 +795,11 @@ static void _reply_with_version_json(socketpool_socket_obj_t *socket, _request *
784795
_send_str(socket, "\r\n");
785796
mp_print_t _socket_print = {socket, _print_chunk};
786797

787-
#if CIRCUITPY_MDNS
788-
const char *hostname = common_hal_mdns_server_get_hostname(&mdns);
789-
#else
790798
const char *hostname = "";
799+
#if CIRCUITPY_MDNS
800+
if (!common_hal_mdns_server_deinited(&mdns)) {
801+
hostname = common_hal_mdns_server_get_hostname(&mdns);
802+
}
791803
#endif
792804
_update_encoded_ip();
793805
// Note: this leverages the fact that C concats consecutive string literals together.
@@ -1032,7 +1044,13 @@ static void _decode_percents(char *str) {
10321044
static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
10331045
if (request->redirect) {
10341046
#if CIRCUITPY_MDNS
1035-
_reply_redirect(socket, request, request->path);
1047+
if (!common_hal_mdns_server_deinited(&mdns)) {
1048+
_reply_redirect(socket, request, request->path);
1049+
} else {
1050+
_reply_missing(socket, request);
1051+
}
1052+
#else
1053+
_reply_missing(socket, request);
10361054
#endif
10371055
} else if (strlen(request->origin) > 0 && !_origin_ok(request->origin)) {
10381056
_reply_forbidden(socket, request);

0 commit comments

Comments
 (0)