|
27 | 27 | #include "shared-bindings/wifi/__init__.h"
|
28 | 28 | #include "shared-bindings/wifi/AuthMode.h"
|
29 | 29 |
|
30 |
| -#include <regex.h> |
31 | 30 | #include <string.h>
|
32 | 31 |
|
33 | 32 | #include "py/runtime.h"
|
34 | 33 | #include "py/objproperty.h"
|
35 | 34 |
|
36 | 35 | #define MAC_ADDRESS_LENGTH 6
|
37 | 36 |
|
| 37 | +STATIC bool hostname_valid(const char *ptr, size_t len) { |
| 38 | + #if 0 // validated by mp_arg_validate_length_range |
| 39 | + if (len == 0 || len > 253) { |
| 40 | + // at most 253 characters long |
| 41 | + return false; |
| 42 | + } |
| 43 | + #endif |
| 44 | + int partlen = 0; |
| 45 | + while (len) { |
| 46 | + char c = *ptr++; |
| 47 | + len--; |
| 48 | + if (c == '.') { |
| 49 | + if (partlen == 0 || partlen > 63) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + partlen = 0; |
| 53 | + continue; |
| 54 | + } |
| 55 | + partlen++; |
| 56 | + if (c == '-') { |
| 57 | + if (partlen == 1) { |
| 58 | + return false; // part cannot begin with a dash |
| 59 | + } |
| 60 | + continue; |
| 61 | + } else if ( |
| 62 | + (c >= 'a' && c <= 'z') || |
| 63 | + (c >= 'A' && c <= 'Z') || |
| 64 | + (c >= '0' && c <= '9')) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + return false; |
| 68 | + } |
| 69 | + // check length of last part |
| 70 | + return !(partlen > 63); |
| 71 | +} |
| 72 | + |
| 73 | + |
38 | 74 | //| class Radio:
|
39 | 75 | //| """Native wifi radio.
|
40 | 76 | //|
|
@@ -89,14 +125,9 @@ STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in)
|
89 | 125 |
|
90 | 126 | mp_arg_validate_length_range(hostname.len, 1, 253, MP_QSTR_hostname);
|
91 | 127 |
|
92 |
| - #if !(defined(CONFIG_IDF_TARGET_ESP32C3) || defined(RASPBERRYPI)) |
93 |
| - regex_t regex; // validate hostname according to RFC 1123 |
94 |
| - regcomp(®ex,"^(([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])\\.)*([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])$", REG_EXTENDED | REG_ICASE | REG_NOSUB); |
95 |
| - if (regexec(®ex, hostname.buf, 0, NULL, 0)) { |
| 128 | + if (!hostname_valid(hostname.buf, hostname.len)) { |
96 | 129 | mp_raise_ValueError(translate("invalid hostname"));
|
97 | 130 | }
|
98 |
| - regfree(®ex); |
99 |
| - #endif |
100 | 131 |
|
101 | 132 | wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
102 | 133 | common_hal_wifi_radio_set_hostname(self, hostname.buf);
|
|
0 commit comments