目前有一个文件,是用的keil带的rt_net来写的,我这边需要使用lwip的net socket来写, 由于确实对lwip的socket编程不熟悉,所以看一下各位有没有会将下面文件修改的,谢谢各位了[mw_shl_code=c,true]/*
* TCP/IP or UDP/IP networking functions for MDK-Pro Network Dual Stack
*
* Copyright (C) 2006-2018, Arm Limited, All Rights Reserved
* 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.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
/*
* Prepare for using the sockets interface
*/
static int net_prepare (void) {
/* Verify that the Network Component is not already running */
if (netSYS_GetHostName () == NULL) {
if (netInitialize () != netOK) {
return (MBEDTLS_ERR_NET_SOCKET_FAILED);
}
/* Small delay for Network to setup */
osDelay (500);
}
return (0);
}
/* Do name resolution with both IPv4 and IPv6 */
stat = netDNSc_GetHostByNameX (host, NET_ADDR_IP4, &addr);
#if defined(RTE_Network_IPv6)
if (stat == netDnsResolverError) {
/* Failed for IPv4, retry for IPv6 */
stat = netDNSc_GetHostByNameX (host, NET_ADDR_IP6, &addr);
}
#endif
if (stat != netOK) {
return (MBEDTLS_ERR_NET_UNKNOWN_HOST);
}
/* Get port number */
addr.port = 0;
sscanf (port, "%hu", &addr.port);
if (addr.port == 0) {
return (MBEDTLS_ERR_NET_UNKNOWN_HOST);
}
/* Listen only makes sense for TCP */
if (proto == MBEDTLS_NET_PROTO_TCP) {
if (listen (ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG) < 0) {
closesocket (ctx->fd);
return (MBEDTLS_ERR_NET_LISTEN_FAILED);
}
}
return (0);
}
/*
* Accept a connection from a remote client
*/
int mbedtls_net_accept (mbedtls_net_context *bind_ctx,
mbedtls_net_context *client_ctx,
void *client_ip, size_t buf_size, size_t *ip_len) {
#if defined(RTE_Network_IPv6)
SOCKADDR_IN6 client_addr;
#else
SOCKADDR_IN client_addr;
#endif
int32_t type, type_len = sizeof (type);
int32_t ret, n = sizeof(client_addr);
/* Is this a TCP or UDP socket? */
if (getsockopt (bind_ctx->fd, SOL_SOCKET, SO_TYPE, (char *)&type, &type_len) < 0) {
return (MBEDTLS_ERR_NET_ACCEPT_FAILED);
}
if (type == SOCK_STREAM) {
/* TCP: actual accept() */
ret = accept (bind_ctx->fd, SOCK_ADDR(&client_addr), &n);
client_ctx->fd = (ret < 0) ? -1 : ret;
}
else {
/* UDP: wait for a message, but keep it in the queue */
char buf[1] = { 0 };
ret = recvfrom (bind_ctx->fd, buf, sizeof (buf), MSG_PEEK,
SOCK_ADDR(&client_addr), &n );
}
if (ret < 0) {
if (ret == BSD_EWOULDBLOCK) {
return (MBEDTLS_ERR_SSL_WANT_READ);
}
return (MBEDTLS_ERR_NET_ACCEPT_FAILED);
}
/* UDP: currently only one connection is accepted */
/* (multiplexing is not supported) */
if (type != SOCK_STREAM) {
/* Enable address filtering */
if (connect (bind_ctx->fd, SOCK_ADDR(&client_addr), n) < 0) {
return (MBEDTLS_ERR_NET_ACCEPT_FAILED);
}
client_ctx->fd = bind_ctx->fd;
bind_ctx->fd = -1;
}