Skip to content

Commit 1c01f5d

Browse files
Mika Leppänenkjbracey
authored andcommitted
Corrected defects
- Changed call_in/call methods of the stack to callback provided by the stack - Specified what are limitations for operations that are made in callback - Added pure virtual class DNS that defines DNS operations - Added cancel operation and unique ID to DNS request used in cancel - Added DNS configuration options to netsocket/mbed_lib.json for retries, response wait time and cache size - Changed host name to use dynamic memory in DNS query list and cache, set maximum length for the name to 255 bytes. - Added mutex to asynchronous DNS - Reworked retries: there is now total retry count and a server specific count - Ignores invalid incoming UDP socket messages (DNS header is not valid), and retries DNS query - Reworked DNS module asynchronous operation functions - Corrected other review issues (nothrow new, missing free, missing mutex unlock etc.)
1 parent b7e8400 commit 1c01f5d

File tree

17 files changed

+639
-301
lines changed

17 files changed

+639
-301
lines changed

features/FEATURE_LWIP/lwip-interface/LWIPStack.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,6 @@ void LWIP::tcpip_thread_callback(void *ptr)
207207
}
208208
}
209209

210-
nsapi_error_t LWIP::call(mbed::Callback<void()> func)
211-
{
212-
return call_in(0, func);
213-
}
214-
215210
nsapi_error_t LWIP::call_in(int delay, mbed::Callback<void()> func)
216211
{
217212
lwip_callback *cb = new lwip_callback;
@@ -229,6 +224,12 @@ nsapi_error_t LWIP::call_in(int delay, mbed::Callback<void()> func)
229224
return NSAPI_ERROR_OK;
230225
}
231226

227+
LWIP::call_in_callback_cb_t LWIP::get_call_in_callback()
228+
{
229+
call_in_callback_cb_t cb(this, &LWIP::call_in);
230+
return cb;
231+
}
232+
232233
const char *LWIP::get_ip_address()
233234
{
234235
if (!default_interface) {

features/FEATURE_LWIP/lwip-interface/LWIPStack.h

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -209,27 +209,6 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
209209
*/
210210
virtual nsapi_error_t get_dns_server(int index, SocketAddress *address);
211211

212-
/** Call a callback
213-
*
214-
* Call a callback from the network stack context. If returns error
215-
* callback will not be called.
216-
*
217-
* @param func Callback to be called
218-
* @return 0 on success, negative error code on failure
219-
*/
220-
virtual nsapi_error_t call(mbed::Callback<void()> func);
221-
222-
/** Call a callback after a delay
223-
*
224-
* Call a callback from the network stack context after a delay. If
225-
* returns error callback will not be called.
226-
*
227-
* @param delay Delay in milliseconds
228-
* @param func Callback to be called
229-
* @return 0 on success, negative error code on failure
230-
*/
231-
virtual nsapi_error_t call_in(int delay, mbed::Callback<void()> func);
232-
233212
/** Get the local IP address
234213
*
235214
* @return Null-terminated representation of the local IP address
@@ -438,6 +417,37 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
438417
int optname, void *optval, unsigned *optlen);
439418
private:
440419

420+
/** Call in callback
421+
*
422+
* Callback is used to call the call in method of the network stack.
423+
*/
424+
typedef mbed::Callback<nsapi_error_t (int delay_ms, mbed::Callback<void()> user_cb)> call_in_callback_cb_t;
425+
426+
/** Get a call in callback
427+
*
428+
* Get a call in callback from the network stack context.
429+
*
430+
* Callback should not take more than 10ms to execute, otherwise it might
431+
* prevent underlying thread processing. A portable user of the callback
432+
* should not make calls to network operations due to stack size limitations.
433+
* The callback should not perform expensive operations such as socket recv/send
434+
* calls or blocking operations.
435+
*
436+
* @return Call in callback
437+
*/
438+
virtual call_in_callback_cb_t get_call_in_callback();
439+
440+
/** Call a callback after a delay
441+
*
442+
* Call a callback from the network stack context after a delay. If function
443+
* returns error callback will not be called.
444+
*
445+
* @param delay Delay in milliseconds
446+
* @param func Callback to be called
447+
* @return 0 on success, negative error code on failure
448+
*/
449+
nsapi_error_t call_in(int delay, mbed::Callback<void()> func);
450+
441451
struct mbed_lwip_socket {
442452
bool in_use;
443453

features/FEATURE_LWIP/lwip-interface/lwip-sys/arch/lwip_sys_arch.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -482,20 +482,14 @@ void sys_msleep(u32_t ms) {
482482

483483
osThreadId_t lwip_tcpip_thread_id = 0;
484484

485-
bool sys_tcpip_thread_set(void)
485+
void sys_tcpip_thread_set(void)
486486
{
487487
lwip_tcpip_thread_id = osThreadGetId();
488488
}
489489

490490
bool sys_tcpip_thread_check(void)
491491
{
492-
osThreadId_t thread_id = osThreadGetId();
493-
494-
if (thread_id == lwip_tcpip_thread_id) {
495-
return true;
496-
} else {
497-
return false;
498-
}
492+
return osThreadGetId() == lwip_tcpip_thread_id;
499493
}
500494

501495
// Keep a pool of thread structures

features/FEATURE_LWIP/lwip-interface/lwip-sys/arch/sys_arch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ typedef sys_thread_data_t* sys_thread_t;
8585
// === PROTECTION ===
8686
typedef int sys_prot_t;
8787

88-
bool sys_tcpip_thread_set(void);
88+
void sys_tcpip_thread_set(void);
8989
bool sys_tcpip_thread_check(void);
9090

9191
#else

features/FEATURE_LWIP/lwip-interface/lwip/src/api/lwip_api_lib.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,8 @@ netconn_join_leave_group(struct netconn *conn,
910910
#endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
911911

912912
#if LWIP_DNS
913+
#if LWIP_FULL_DNS
914+
913915
/**
914916
* @ingroup netconn_common
915917
* Execute a DNS query, only one IP address is returned
@@ -989,6 +991,8 @@ netconn_gethostbyname(const char *name, ip_addr_t *addr)
989991
API_VAR_FREE(MEMP_DNS_API_MSG, msg);
990992
return err;
991993
}
994+
995+
#endif /* LWIP_FULL_DNS */
992996
#endif /* LWIP_DNS*/
993997

994998
#if LWIP_NETCONN_SEM_PER_THREAD

features/FEATURE_LWIP/lwip-interface/lwip/src/api/lwip_api_msg.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,7 @@ lwip_netconn_do_join_leave_group(void *m)
18921892
#endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
18931893

18941894
#if LWIP_DNS
1895+
#if LWIP_FULL_DNS
18951896
/**
18961897
* Callback function that is called when DNS name is resolved
18971898
* (or on timeout). A waiting application thread is waked up by
@@ -1943,5 +1944,6 @@ lwip_netconn_do_gethostbyname(void *arg)
19431944
}
19441945
}
19451946
#endif /* LWIP_DNS */
1947+
#endif /* LWIP_FULL_DNS */
19461948

19471949
#endif /* LWIP_NETCONN */

features/nanostack/nanostack-interface/Nanostack.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,6 @@ void Nanostack::call_event_tasklet_main(arm_event_s *event)
461461
}
462462
}
463463

464-
nsapi_error_t Nanostack::call(mbed::Callback<void()> func)
465-
{
466-
return call_in(0, func);
467-
}
468-
469464
nsapi_error_t Nanostack::call_in(int delay, mbed::Callback<void()> func)
470465
{
471466
if (call_event_tasklet < 0) {
@@ -494,17 +489,25 @@ nsapi_error_t Nanostack::call_in(int delay, mbed::Callback<void()> func)
494489
if (delay) {
495490
uint32_t ticks = eventOS_event_timer_ms_to_ticks(delay);
496491
if (!eventOS_event_send_in(&event, ticks)) {
492+
delete cb;
497493
return NSAPI_ERROR_NO_MEMORY;
498494
}
499495
} else {
500496
if (eventOS_event_send(&event) < 0) {
497+
delete cb;
501498
return NSAPI_ERROR_NO_MEMORY;
502499
}
503500
}
504501

505502
return NSAPI_ERROR_OK;
506503
}
507504

505+
Nanostack::call_in_callback_cb_t Nanostack::get_call_in_callback()
506+
{
507+
call_in_callback_cb_t cb(this, &Nanostack::call_in);
508+
return cb;
509+
}
510+
508511
const char * Nanostack::get_ip_address()
509512
{
510513
NanostackLockGuard lock;

features/nanostack/nanostack-interface/Nanostack.h

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,6 @@ class Nanostack : public OnboardNetworkStack, private mbed::NonCopyable<Nanostac
4444
/* Local variant with stronger typing and manual address specification */
4545
nsapi_error_t add_ethernet_interface(EMAC &emac, bool default_if, Nanostack::EthernetInterface **interface_out, const uint8_t *mac_addr = NULL);
4646

47-
/** Call a callback
48-
*
49-
* Call a callback from the network stack context. If returns error
50-
* callback will not be called.
51-
*
52-
* @param func Callback to be called
53-
* @return 0 on success, negative error code on failure
54-
*/
55-
virtual nsapi_error_t call(mbed::Callback<void()> func);
56-
57-
/** Call a callback after a delay
58-
*
59-
* Call a callback from the network stack context after a delay. If
60-
* returns error callback will not be called.
61-
*
62-
* @param delay Delay in milliseconds
63-
* @param func Callback to be called
64-
* @return 0 on success, negative error code on failure
65-
*/
66-
virtual nsapi_error_t call_in(int delay, mbed::Callback<void()> func);
67-
6847
protected:
6948

7049
Nanostack();
@@ -266,6 +245,38 @@ class Nanostack : public OnboardNetworkStack, private mbed::NonCopyable<Nanostac
266245
virtual nsapi_error_t getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen);
267246

268247
private:
248+
249+
/** Call in callback
250+
*
251+
* Callback is used to call the call in method of the network stack.
252+
*/
253+
typedef mbed::Callback<nsapi_error_t (int delay_ms, mbed::Callback<void()> user_cb)> call_in_callback_cb_t;
254+
255+
/** Get a call in callback
256+
*
257+
* Get a call in callback from the network stack context.
258+
*
259+
* Callback should not take more than 10ms to execute, otherwise it might
260+
* prevent underlying thread processing. A portable user of the callback
261+
* should not make calls to network operations due to stack size limitations.
262+
* The callback should not perform expensive operations such as socket recv/send
263+
* calls or blocking operations.
264+
*
265+
* @return Call in callback
266+
*/
267+
virtual call_in_callback_cb_t get_call_in_callback();
268+
269+
/** Call a callback after a delay
270+
*
271+
* Call a callback from the network stack context after a delay. If function
272+
* returns error callback will not be called.
273+
*
274+
* @param delay Delay in milliseconds
275+
* @param func Callback to be called
276+
* @return 0 on success, negative error code on failure
277+
*/
278+
nsapi_error_t call_in(int delay, mbed::Callback<void()> func);
279+
269280
struct nanostack_callback {
270281
mbed::Callback<void()> callback;
271282
};

features/netsocket/DNS.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef DNS_H
18+
#define DNS_H
19+
20+
class DNS {
21+
public:
22+
23+
/** Translates a hostname to an IP address with specific version
24+
*
25+
* The hostname may be either a domain name or an IP address. If the
26+
* hostname is an IP address, no network transactions will be performed.
27+
*
28+
* If no stack-specific DNS resolution is provided, the hostname
29+
* will be resolve using a UDP socket on the stack.
30+
*
31+
* @param host Hostname to resolve
32+
* @param address Destination for the host SocketAddress
33+
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
34+
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
35+
* @return 0 on success, negative error code on failure
36+
*/
37+
virtual nsapi_error_t gethostbyname(const char *host,
38+
SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC) = 0;
39+
40+
/** Hostname translation callback (asynchronous)
41+
*
42+
* Callback will be called after DNS resolution completes or a failure occurs.
43+
*
44+
* Callback should not take more than 10ms to execute, otherwise it might
45+
* prevent underlying thread processing. A portable user of the callback
46+
* should not make calls to network operations due to stack size limitations.
47+
* The callback should not perform expensive operations such as socket recv/send
48+
* calls or blocking operations.
49+
*
50+
* @param status 0 on success, negative error code on failure
51+
* @param address On success, destination for the host SocketAddress
52+
*/
53+
typedef mbed::Callback<void (nsapi_error_t result, SocketAddress *address)> hostbyname_cb_t;
54+
55+
/** Translates a hostname to an IP address (asynchronous)
56+
*
57+
* The hostname may be either a domain name or an IP address. If the
58+
* hostname is an IP address, no network transactions will be performed.
59+
*
60+
* If no stack-specific DNS resolution is provided, the hostname
61+
* will be resolve using a UDP socket on the stack.
62+
*
63+
* Call is non-blocking. Result of the DNS operation is returned by the callback.
64+
* If this function returns failure, callback will not be called. In case result
65+
* is success (IP address was found from DNS cache), callback will be called
66+
* before function returns.
67+
*
68+
* @param host Hostname to resolve
69+
* @param callback Callback that is called for result
70+
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
71+
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
72+
* @return 0 on success, negative error code on failure or an unique id that
73+
* represents the hostname translation operation and can be passed to
74+
* cancel
75+
*/
76+
virtual nsapi_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback,
77+
nsapi_version_t version = NSAPI_UNSPEC) = 0;
78+
79+
/** Cancels asynchronous hostname translation
80+
*
81+
* When translation is cancelled, callback will not be called.
82+
*
83+
* @param id Unique id of the hostname translation operation
84+
* @return 0 on success, negative error code on failure
85+
*/
86+
virtual nsapi_error_t gethostbyname_async_cancel(nsapi_error_t id) = 0;
87+
88+
/** Add a domain name server to list of servers to query
89+
*
90+
* @param address Destination for the host address
91+
* @return 0 on success, negative error code on failure
92+
*/
93+
virtual nsapi_error_t add_dns_server(const SocketAddress &address) = 0;
94+
};
95+
96+
#endif

features/netsocket/NetworkInterface.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,14 @@ nsapi_error_t NetworkInterface::gethostbyname(const char *name, SocketAddress *a
6060
return get_stack()->gethostbyname(name, address, version);
6161
}
6262

63-
nsapi_error_t NetworkInterface::gethostbyname_async(const char *host, hostbyname_cb_t callback, void *data, nsapi_version_t version)
63+
nsapi_error_t NetworkInterface::gethostbyname_async(const char *host, hostbyname_cb_t callback, nsapi_version_t version)
6464
{
65-
return get_stack()->gethostbyname_async(host, callback, data, version);
65+
return get_stack()->gethostbyname_async(host, callback, version);
66+
}
67+
68+
nsapi_error_t NetworkInterface::gethostbyname_async_cancel(nsapi_error_t handle)
69+
{
70+
return get_stack()->gethostbyname_async_cancel(handle);
6671
}
6772

6873
nsapi_error_t NetworkInterface::add_dns_server(const SocketAddress &address)

0 commit comments

Comments
 (0)