forked from arduino/ArduinoCore-mbed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0236-dhcp-add-methods-for-setting-hostname.patch
148 lines (138 loc) · 5.11 KB
/
0236-dhcp-add-methods-for-setting-hostname.patch
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
From f7740c5ec7986dab71901704936a923899041720 Mon Sep 17 00:00:00 2001
From: Channel59 <[email protected]>
Date: Tue, 9 Jul 2024 23:54:43 +0200
Subject: [PATCH 2/2] Add methods for setting hostname -- from: https://github.com/ARMmbed/mbed-os/pull/15506/commits/979a9425499565e434db708317a4fc0dc3792af8
---
.../include/netsocket/EMACInterface.h | 8 ++++++
.../include/netsocket/NetworkInterface.h | 16 ++++++++++++
.../netsocket/include/netsocket/nsapi_types.h | 10 +++++++
.../netsocket/source/EMACInterface.cpp | 26 +++++++++++++++++++
.../netsocket/source/NetworkInterface.cpp | 10 +++++++
5 files changed, 70 insertions(+)
diff --git a/connectivity/netsocket/include/netsocket/EMACInterface.h b/connectivity/netsocket/include/netsocket/EMACInterface.h
index 8cf47cb703..c06aeb850e 100644
--- a/connectivity/netsocket/include/netsocket/EMACInterface.h
+++ b/connectivity/netsocket/include/netsocket/EMACInterface.h
@@ -83,6 +83,12 @@ public:
/** @copydoc NetworkInterface::disconnect */
nsapi_error_t disconnect() override;
+ /** @copydoc NetworkInterface::get_hostname */
+ const char *get_hostname() override;
+
+ /** @copydoc NetworkInterface::set_hostname */
+ nsapi_error_t set_hostname(const char *hostname) override;
+
/** @copydoc NetworkInterface::get_mac_address */
const char *get_mac_address() override;
@@ -146,6 +152,8 @@ protected:
OnboardNetworkStack::Interface *_interface = nullptr;
bool _dhcp = true;
bool _blocking = true;
+ bool _hostname_set = false;
+ char _hostname[NSAPI_HOSTNAME_SIZE];
bool _hw_mac_addr_set = false;
char _mac_address[NSAPI_MAC_SIZE];
char _ip_address[NSAPI_IPv6_SIZE] {};
diff --git a/connectivity/netsocket/include/netsocket/NetworkInterface.h b/connectivity/netsocket/include/netsocket/NetworkInterface.h
index 9071a1e40b..0e2aa64c9d 100644
--- a/connectivity/netsocket/include/netsocket/NetworkInterface.h
+++ b/connectivity/netsocket/include/netsocket/NetworkInterface.h
@@ -90,6 +90,22 @@ public:
*/
virtual void set_as_default();
+ /** Get hostname.
+ *
+ * @return Hostname if configured, null otherwise
+ */
+ virtual const char *get_hostname();
+
+ /** Set hostname.
+ *
+ * @param hostname Hostname string
+ * @retval NSAPI_ERROR_OK on success
+ * @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
+ * @retval NSAPI_ERROR_PARAMETER if hostname is not valid
+ * @retval NSAPI_ERROR_BUSY if hostname can't be set
+ */
+ virtual nsapi_error_t set_hostname(const char *hostname);
+
/** Get the local MAC address.
*
* Provided MAC address is intended for info or debug purposes and
diff --git a/connectivity/netsocket/include/netsocket/nsapi_types.h b/connectivity/netsocket/include/netsocket/nsapi_types.h
index 3b496d5087..28dbcc9a38 100644
--- a/connectivity/netsocket/include/netsocket/nsapi_types.h
+++ b/connectivity/netsocket/include/netsocket/nsapi_types.h
@@ -196,6 +196,16 @@ typedef enum nsapi_security {
*/
#define NSAPI_IP_BYTES NSAPI_IPv6_BYTES
+/** Maximum size of hostname
+ *
+ * According to RFC 1034 [1], Section 3.1 "Name space specifications and
+ * terminology", 63 is the maximum size of a hostname. +1 for the string
+ * terminator.
+ *
+ * [1] https://www.rfc-editor.org/rfc/rfc1034
+ */
+#define NSAPI_HOSTNAME_SIZE 64
+
/** Maximum size of MAC address representation
*/
#define NSAPI_MAC_SIZE 18
diff --git a/connectivity/netsocket/source/EMACInterface.cpp b/connectivity/netsocket/source/EMACInterface.cpp
index f48bc0a185..26b7e29856 100644
--- a/connectivity/netsocket/source/EMACInterface.cpp
+++ b/connectivity/netsocket/source/EMACInterface.cpp
@@ -88,6 +88,32 @@ nsapi_error_t EMACInterface::disconnect()
return NSAPI_ERROR_NO_CONNECTION;
}
+const char *EMACInterface::get_hostname()
+{
+ if (_hostname_set) {
+ return _hostname;
+ }
+ return nullptr;
+}
+
+nsapi_error_t EMACInterface::set_hostname(const char *hostname)
+{
+ if (!hostname || strlen(hostname) > NSAPI_HOSTNAME_SIZE-1) {
+ return NSAPI_ERROR_PARAMETER;
+ }
+
+ if (_interface) {
+ // can't set hostname once initialized
+ return NSAPI_ERROR_BUSY;
+ }
+
+ memset(_hostname, 0, NSAPI_HOSTNAME_SIZE);
+ strncpy(_hostname, hostname, NSAPI_HOSTNAME_SIZE-1);
+ _hostname_set = true;
+
+ return NSAPI_ERROR_OK;
+}
+
const char *EMACInterface::get_mac_address()
{
if (_interface && _interface->get_mac_address(_mac_address, sizeof(_mac_address))) {
diff --git a/connectivity/netsocket/source/NetworkInterface.cpp b/connectivity/netsocket/source/NetworkInterface.cpp
index 0f237f0e19..649df0f9b3 100644
--- a/connectivity/netsocket/source/NetworkInterface.cpp
+++ b/connectivity/netsocket/source/NetworkInterface.cpp
@@ -29,6 +29,16 @@ void NetworkInterface::set_as_default()
}
+const char *NetworkInterface::get_hostname()
+{
+ return 0;
+}
+
+nsapi_error_t NetworkInterface::set_hostname(const char *hostname)
+{
+ return NSAPI_ERROR_UNSUPPORTED;
+}
+
const char *NetworkInterface::get_mac_address()
{
return 0;
--
2.34.1