Skip to content

Implement Socket::getpeername() API #8651

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

Merged
merged 1 commit into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ extern std::list<uint32_t> eventFlagsStubNextRetval;
// InternetSocket is an abstract class, so we have to test it via its child.
class stubInternetSocket : public InternetSocket {
protected:
nsapi_error_t return_value = 0;
nsapi_error_t return_value;
public:
stubInternetSocket() {
return_value = 0;
}
virtual nsapi_error_t connect(const SocketAddress &address)
{
_remote_peer = address;
return return_value;
}
virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size)
Expand Down Expand Up @@ -227,3 +231,25 @@ TEST_F(TestInternetSocket, sigio)
socket->close(); // Trigger event;
EXPECT_EQ(callback_is_called, true);
}

TEST_F(TestInternetSocket, getpeername)
{
SocketAddress peer;
SocketAddress zero;

stack.return_value = NSAPI_ERROR_OK;

EXPECT_EQ(socket->getpeername(&peer), NSAPI_ERROR_NO_SOCKET);

socket->open((NetworkStack *)&stack);
socket->connect(zero);

EXPECT_EQ(socket->getpeername(&peer), NSAPI_ERROR_NO_CONNECTION);

const nsapi_addr_t saddr = {NSAPI_IPv4, {192, 168, 0, 1} };
const SocketAddress remote(saddr, 1024);
socket->connect(remote);

EXPECT_EQ(socket->getpeername(&peer), NSAPI_ERROR_OK);
EXPECT_EQ(remote, peer);
}
7 changes: 6 additions & 1 deletion UNITTESTS/stubs/NetworkStack_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
class NetworkStackstub : public NetworkStack {
public:
std::list<nsapi_error_t> return_values;
nsapi_error_t return_value = 0;
nsapi_error_t return_value;

NetworkStackstub() {
return_value = 0;
}

virtual const char *get_ip_address()
{
return "127.0.0.1";
Expand Down
12 changes: 12 additions & 0 deletions features/netsocket/InternetSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,15 @@ void InternetSocket::attach(Callback<void()> callback)
{
sigio(callback);
}

nsapi_error_t InternetSocket::getpeername(SocketAddress *address)
{
if (!_socket) {
return NSAPI_ERROR_NO_SOCKET;
}
if (!_remote_peer) {
return NSAPI_ERROR_NO_CONNECTION;
}
*address = _remote_peer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this function need a return value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thank you.

return NSAPI_ERROR_OK;
}
4 changes: 4 additions & 0 deletions features/netsocket/InternetSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ class InternetSocket : public Socket {
*/
virtual void sigio(mbed::Callback<void()> func);

/** @copydoc Socket::getpeername
*/
virtual nsapi_error_t getpeername(SocketAddress *address);

/** Register a callback on state change of the socket.
*
* @see Socket::sigio
Expand Down
11 changes: 11 additions & 0 deletions features/netsocket/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,17 @@ class Socket {
* @return NSAPI_ERROR_OK on success, negative error code on failure
*/
virtual nsapi_error_t listen(int backlog = 1) = 0;

/** Get the remote-end peer associated with this socket.
*
* Copy the remote peer address to a SocketAddress structure pointed by
* address parameter. Socket must be connected to have a peer address
* associated.
*
* @param address Pointer to SocketAddress structure.
* @return NSAPI_ERROR_OK on success, negative error code on failure.
*/
virtual nsapi_error_t getpeername(SocketAddress *address) = 0;
};


Expand Down
8 changes: 8 additions & 0 deletions features/netsocket/TLSSocketWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,4 +577,12 @@ nsapi_error_t TLSSocketWrapper::listen(int)
return NSAPI_ERROR_UNSUPPORTED;
}

nsapi_error_t TLSSocketWrapper::getpeername(SocketAddress *address)
{
if (!_handshake_completed) {
return NSAPI_ERROR_NO_CONNECTION;
}
return _transport->getpeername(address);
}

#endif /* MBEDTLS_SSL_CLI_C */
1 change: 1 addition & 0 deletions features/netsocket/TLSSocketWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class TLSSocketWrapper : public Socket {
virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen);
virtual Socket *accept(nsapi_error_t *error = NULL);
virtual nsapi_error_t listen(int backlog = 1);
virtual nsapi_error_t getpeername(SocketAddress *address);

#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(DOXYGEN)
/** Get own certificate directly from Mbed TLS
Expand Down