-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LwIP: add ICMPv4 Socket support #10978
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0b5dea5
Changes to the PR 10978 (LWIP: Add RAWIPSocket support)
balajicyp d92a946
fix astyle convention
balajicyp 1a7288d
fix astyle convention
balajicyp b91836a
fix astyle convention
balajicyp 90e188b
fix a style convention
balajicyp ab88335
Incorporate review comments from @kjbracey-arm
balajicyp 5168bbd
Add InternetDatagram Base Class
balajicyp 6905b1b
Incorporate review comments from @kjbracey-arm
balajicyp d9045b4
index on lwip-rawsocket: 6905b1b547 Incorporate review comments from …
balajicyp 948e989
fix mbed_lib.json add ,
balajicyp 5b92916
rebase mbed_lib.json to upstream/master
balajicyp 2d410e3
Rebase the code Unittests aware of InternetDatagramSocket
balajicyp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,6 +156,10 @@ | |
"num-netbuf": { | ||
"help": "Number of netbufs, each netbuf requires 64 bytes of RAM, see LWIP's opt.h for more information. Current default is 8.", | ||
"value": 8 | ||
}, | ||
"raw-socket-enabled": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kjbracey-arm Fixed now? |
||
"help": "Enable lwip raw sockets, required for Mbed OS ICMPSocket", | ||
"value": false | ||
} | ||
}, | ||
"target_overrides": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* Socket | ||
* Copyright (c) 2015 ARM Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "ICMPSocket.h" | ||
#include "Timer.h" | ||
#include "mbed_assert.h" | ||
|
||
ICMPSocket::ICMPSocket() | ||
{ | ||
_socket_stats.stats_update_proto(this, NSAPI_ICMP); | ||
} | ||
|
||
nsapi_protocol_t ICMPSocket::get_proto() | ||
{ | ||
return NSAPI_ICMP; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** \addtogroup netsocket */ | ||
/** @{*/ | ||
/* ICMPSocket | ||
* Copyright (c) 2015 ARM Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef ICMPSOCKET_H | ||
#define ICMPSOCKET_H | ||
|
||
#include "netsocket/InternetSocket.h" | ||
#include "netsocket/InternetDatagramSocket.h" | ||
#include "netsocket/NetworkStack.h" | ||
#include "netsocket/NetworkInterface.h" | ||
#include "rtos/EventFlags.h" | ||
|
||
|
||
/** ICMP socket implementation. | ||
*/ | ||
class ICMPSocket : public InternetDatagramSocket { | ||
public: | ||
/** Create an uninitialized socket. | ||
* | ||
* @note Must call open to initialize the socket on a network stack. | ||
*/ | ||
ICMPSocket(); | ||
|
||
#if !defined(DOXYGEN_ONLY) | ||
|
||
protected: | ||
virtual nsapi_protocol_t get_proto(); | ||
|
||
#endif //!defined(DOXYGEN_ONLY) | ||
}; | ||
|
||
|
||
#endif | ||
|
||
/** @}*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* Socket | ||
* Copyright (c) 2015 ARM Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "InternetDatagramSocket.h" | ||
#include "Timer.h" | ||
#include "mbed_assert.h" | ||
|
||
nsapi_error_t InternetDatagramSocket::connect(const SocketAddress &address) | ||
{ | ||
_remote_peer = address; | ||
_socket_stats.stats_update_peer(this, _remote_peer); | ||
_socket_stats.stats_update_socket_state(this, SOCK_CONNECTED); | ||
return NSAPI_ERROR_OK; | ||
} | ||
|
||
nsapi_size_or_error_t InternetDatagramSocket::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size) | ||
{ | ||
SocketAddress address; | ||
nsapi_size_or_error_t err; | ||
|
||
if (!strcmp(_interface_name, "")) { | ||
err = _stack->gethostbyname(host, &address); | ||
} else { | ||
err = _stack->gethostbyname(host, &address, NSAPI_UNSPEC, _interface_name); | ||
} | ||
|
||
if (err) { | ||
return NSAPI_ERROR_DNS_FAILURE; | ||
} | ||
|
||
address.set_port(port); | ||
|
||
// sendto is thread safe | ||
return sendto(address, data, size); | ||
} | ||
|
||
nsapi_size_or_error_t InternetDatagramSocket::sendto(const SocketAddress &address, const void *data, nsapi_size_t size) | ||
{ | ||
_lock.lock(); | ||
nsapi_size_or_error_t ret; | ||
|
||
_writers++; | ||
if (_socket) { | ||
_socket_stats.stats_update_socket_state(this, SOCK_OPEN); | ||
_socket_stats.stats_update_peer(this, address); | ||
} | ||
while (true) { | ||
if (!_socket) { | ||
ret = NSAPI_ERROR_NO_SOCKET; | ||
break; | ||
} | ||
|
||
core_util_atomic_flag_clear(&_pending); | ||
nsapi_size_or_error_t sent = _stack->socket_sendto(_socket, address, data, size); | ||
if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) { | ||
_socket_stats.stats_update_sent_bytes(this, sent); | ||
ret = sent; | ||
break; | ||
} else { | ||
uint32_t flag; | ||
|
||
// Release lock before blocking so other threads | ||
// accessing this object aren't blocked | ||
_lock.unlock(); | ||
flag = _event_flag.wait_any(WRITE_FLAG, _timeout); | ||
_lock.lock(); | ||
|
||
if (flag & osFlagsError) { | ||
// Timeout break | ||
ret = NSAPI_ERROR_WOULD_BLOCK; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
_writers--; | ||
if (!_socket || !_writers) { | ||
_event_flag.set(FINISHED_FLAG); | ||
} | ||
_lock.unlock(); | ||
return ret; | ||
} | ||
|
||
nsapi_size_or_error_t InternetDatagramSocket::send(const void *data, nsapi_size_t size) | ||
{ | ||
if (!_remote_peer) { | ||
return NSAPI_ERROR_NO_ADDRESS; | ||
} | ||
return sendto(_remote_peer, data, size); | ||
} | ||
|
||
nsapi_size_or_error_t InternetDatagramSocket::recvfrom(SocketAddress *address, void *buffer, nsapi_size_t size) | ||
{ | ||
_lock.lock(); | ||
nsapi_size_or_error_t ret; | ||
SocketAddress ignored; | ||
|
||
if (!address) { | ||
address = &ignored; | ||
} | ||
|
||
_readers++; | ||
|
||
if (_socket) { | ||
_socket_stats.stats_update_socket_state(this, SOCK_OPEN); | ||
} | ||
while (true) { | ||
if (!_socket) { | ||
ret = NSAPI_ERROR_NO_SOCKET; | ||
break; | ||
} | ||
|
||
core_util_atomic_flag_clear(&_pending); | ||
nsapi_size_or_error_t recv = _stack->socket_recvfrom(_socket, address, buffer, size); | ||
|
||
// Filter incomming packets using connected peer address | ||
if (recv >= 0 && _remote_peer && _remote_peer != *address) { | ||
continue; | ||
} | ||
|
||
_socket_stats.stats_update_peer(this, _remote_peer); | ||
// Non-blocking sockets always return. Blocking only returns when success or errors other than WOULD_BLOCK | ||
if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) { | ||
ret = recv; | ||
_socket_stats.stats_update_recv_bytes(this, recv); | ||
break; | ||
} else { | ||
uint32_t flag; | ||
|
||
// Release lock before blocking so other threads | ||
// accessing this object aren't blocked | ||
_lock.unlock(); | ||
flag = _event_flag.wait_any(READ_FLAG, _timeout); | ||
_lock.lock(); | ||
|
||
if (flag & osFlagsError) { | ||
// Timeout break | ||
ret = NSAPI_ERROR_WOULD_BLOCK; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
_readers--; | ||
if (!_socket || !_readers) { | ||
_event_flag.set(FINISHED_FLAG); | ||
} | ||
|
||
_lock.unlock(); | ||
return ret; | ||
} | ||
|
||
nsapi_size_or_error_t InternetDatagramSocket::recv(void *buffer, nsapi_size_t size) | ||
{ | ||
return recvfrom(NULL, buffer, size); | ||
} | ||
|
||
Socket *InternetDatagramSocket::accept(nsapi_error_t *error) | ||
{ | ||
if (error) { | ||
*error = NSAPI_ERROR_UNSUPPORTED; | ||
} | ||
return NULL; | ||
} | ||
|
||
nsapi_error_t InternetDatagramSocket::listen(int) | ||
{ | ||
return NSAPI_ERROR_UNSUPPORTED; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For future-proofing, can you make this error for unknown
proto
? I know it didn't before, but it should have,There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you mean the known proto be NASPI_ERROR_PROTO_UNKNOWN and return from function as below.
...
} else if (proto == NSAPI_UDP) {
netconntype = NETCONN_UDP;
} else if { proto == NSAPI_ICMP) {
netconntype = NETCONN_RAW;
} else {
return NSAPI_ERROR_PROTO_UNKNOWN;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed NSAPI_ERROR_PROTO_UNKNOWN