mirror of https://github.com/ARMmbed/mbed-os.git
				
				
				
			Refactor DTLSSocket to use Socket::getpeername()
Also, let DTLSSocket to be a friend of InternetSocket so it can do the name resolution from its _stack. + Some whitespace fixespull/8659/head
							parent
							
								
									695db63df8
								
							
						
					
					
						commit
						46c46019bc
					
				| 
						 | 
				
			
			@ -25,19 +25,27 @@
 | 
			
		|||
 | 
			
		||||
nsapi_error_t DTLSSocket::connect(const char *host, uint16_t port)
 | 
			
		||||
{
 | 
			
		||||
    if (!_remote_address) {
 | 
			
		||||
        nsapi_error_t err = _stack->gethostbyname(host, &_remote_address);
 | 
			
		||||
    SocketAddress addr;
 | 
			
		||||
    nsapi_error_t ret;
 | 
			
		||||
 | 
			
		||||
    ret = _udp_socket.getpeername(&addr);
 | 
			
		||||
    if (ret != NSAPI_ERROR_NO_CONNECTION) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!addr || ret == NSAPI_ERROR_NO_CONNECTION) {
 | 
			
		||||
        nsapi_error_t err = _udp_socket._stack->gethostbyname(host, &addr);
 | 
			
		||||
        if (err) {
 | 
			
		||||
            return NSAPI_ERROR_DNS_FAILURE;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        _remote_address.set_port(port);
 | 
			
		||||
        addr.set_port(port);
 | 
			
		||||
 | 
			
		||||
        set_hostname(host);
 | 
			
		||||
        _udp_socket.connect(_remote_address); // UDPSocket::connect() cannot fail
 | 
			
		||||
        _udp_socket.connect(addr); // UDPSocket::connect() cannot fail
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return connect(_remote_address);
 | 
			
		||||
    return connect(addr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
DTLSSocket::~DTLSSocket()
 | 
			
		||||
| 
						 | 
				
			
			@ -48,4 +56,4 @@ DTLSSocket::~DTLSSocket()
 | 
			
		|||
    close();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif // MBEDTLS_SSL_CLI_C
 | 
			
		||||
#endif // MBEDTLS_SSL_CLI_C
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,20 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright (c) 2018 ARM Limited
 | 
			
		||||
 * SPDX-License-Identifier: Apache-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * 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 DTLSSOCKET_H
 | 
			
		||||
#define DTLSSOCKET_H
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -46,7 +63,6 @@ public:
 | 
			
		|||
     *  @return         0 on success, negative error code on failure
 | 
			
		||||
     */
 | 
			
		||||
    virtual nsapi_error_t open(NetworkStack *stack) {
 | 
			
		||||
        _stack = stack;
 | 
			
		||||
        return _udp_socket.open(stack);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -70,9 +86,7 @@ public:
 | 
			
		|||
 | 
			
		||||
private:
 | 
			
		||||
    UDPSocket _udp_socket;
 | 
			
		||||
    NetworkStack *_stack;
 | 
			
		||||
    SocketAddress _remote_address;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -52,4 +52,4 @@ void DTLSSocketWrapper::timer_event(void)
 | 
			
		|||
    event();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif /* MBEDTLS_SSL_CLI_C */
 | 
			
		||||
#endif /* MBEDTLS_SSL_CLI_C */
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,4 +18,4 @@ private:
 | 
			
		|||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -175,6 +175,8 @@ protected:
 | 
			
		|||
    static const int WRITE_FLAG    = 0x2u;
 | 
			
		||||
    static const int FINISHED_FLAG = 0x3u;
 | 
			
		||||
 | 
			
		||||
    friend class DTLSSocket;  // Allow DTLSSocket::connect() to do name resolution on the _stack
 | 
			
		||||
 | 
			
		||||
#endif //!defined(DOXYGEN_ONLY)
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -47,4 +47,4 @@ TLSSocket::~TLSSocket()
 | 
			
		|||
    close();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif // MBEDTLS_SSL_CLI_C
 | 
			
		||||
#endif // MBEDTLS_SSL_CLI_C
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue