add socket api

pull/32/head
jiangxiangbing 2018-12-14 18:09:45 +08:00
parent 94557f4bb9
commit f33c17442a
17 changed files with 1105 additions and 71 deletions

View File

@ -23,7 +23,7 @@ set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE C)
target_link_libraries(${PROJECT_NAME}
-Wl,--start-group
m freertos atomic bsp c stdc++ drivers
m freertos atomic bsp c stdc++ drivers posix
-Wl,--end-group
)

View File

@ -416,8 +416,12 @@ public:
virtual void connect(const socket_address_t &address) = 0;
virtual void listen(uint32_t backlog) = 0;
virtual void shutdown(socket_shutdown_t how) = 0;
virtual size_t send(gsl::span<const uint8_t> buffer) = 0;
virtual size_t receive(gsl::span<uint8_t> buffer) = 0;
virtual size_t send(gsl::span<const uint8_t> buffer, uint8_t flags) = 0;
virtual size_t receive(gsl::span<uint8_t> buffer, uint8_t flags) = 0;
virtual size_t send_to(gsl::span<const uint8_t> buffer, uint8_t flags, const socket_address_t *to) = 0;
virtual size_t receive_from(gsl::span<uint8_t> buffer, uint8_t flags, socket_address_t *from) = 0;
virtual size_t read(gsl::span<uint8_t> buffer) = 0;
virtual size_t write(gsl::span<const uint8_t> buffer) = 0;
};
extern driver_registry_t g_hal_drivers[];

View File

@ -30,20 +30,40 @@ int network_interface_set_enable(handle_t netif_handle, bool enable);
int network_interface_set_as_default(handle_t netif_handle);
int network_set_addr(handle_t netif_handle, const ip_address_t *ip_address, const ip_address_t *net_mask, const ip_address_t *gateway);
int network_get_addr(handle_t netif_handle, ip_address_t *ip_address, ip_address_t *net_mask, ip_address_t *gateway);
dhcp_state_t network_interface_dhcp_pooling(handle_t netif_handle);
handle_t network_socket_open(address_family_t address_family, socket_type_t type, protocol_type_t protocol);
handle_t network_socket_close(handle_t socket_handle);
int network_socket_bind(handle_t socket_handle, const socket_address_t* remote_address);
int network_socket_bind(handle_t socket_handle, const socket_address_t *remote_address);
int network_socket_connect(handle_t socket_handle, const socket_address_t* remote_address);
int network_socket_connect(handle_t socket_handle, const socket_address_t *remote_address);
int network_socket_listen(handle_t socket_handle, uint32_t backlog);
handle_t network_socket_accept(handle_t socket_handle, socket_address_t* remote_address);
handle_t network_socket_accept(handle_t socket_handle, socket_address_t *remote_address);
int network_socket_shutdown(handle_t socket_handle, socket_shutdown_t how);
int network_socket_send(handle_t socket_handle, const uint8_t *data, size_t len, uint8_t flags);
int network_socket_receive(handle_t socket_handle, uint8_t *data, size_t len, uint8_t flags);
int network_socket_send_to(handle_t socket_handle, const uint8_t *data, size_t len, uint8_t flags, const socket_address_t *to);
int network_socket_receive_from(handle_t socket_handle, uint8_t *data, size_t len, uint8_t flags, socket_address_t *from);
hostent_t *network_socket_gethostbyname(const char *name);
void network_socket_addr_parse(const char *ip_addr, int port, uint8_t *socket_addr);
void network_socket_addr_to_string(uint8_t *socket_addr, char *ip_addr, int *port);
#ifdef __cplusplus
}
#endif

View File

@ -260,7 +260,7 @@ typedef enum _socket_type
typedef enum _protocol_type
{
PROTCL_TCP
PROTCL_IP
} protocol_type_t;
typedef struct _socket_address
@ -288,6 +288,32 @@ typedef struct _mac_address
uint8_t data[6];
} mac_address_t;
typedef struct _hostent
{
/* Official name of the host. */
const char *h_name;
/* A pointer to an array of pointers to alternative host names, terminated by a null pointer. */
uint8_t **h_aliases;
/* Address type. */
uint32_t h_addrtype;
/* The length, in bytes, of the address. */
uint32_t h_length;
/* A pointer to an array of pointers to network addresses (in
network byte order) for the host, terminated by a null pointer. */
uint8_t **h_addr_list;
/* for backward compatibility */
#define h_addr h_addr_list[0]
} hostent_t;
typedef enum _dhcp_state
{
DHCP_START = 0,
DHCP_WAIT_ADDRESS,
DHCP_ADDRESS_ASSIGNED,
DHCP_TIMEOUT,
DHCP_FAIL
} dhcp_state_t;
#ifdef __cplusplus
}
#endif

View File

@ -199,6 +199,7 @@ int io_read(handle_t file, uint8_t *buffer, size_t len)
else DEFINE_READ_PROXY(i2c_device_driver)
else DEFINE_READ_PROXY(spi_device_driver)
else DEFINE_READ_PROXY(filesystem_file)
else DEFINE_READ_PROXY(network_socket)
else
{
return -1;
@ -279,6 +280,7 @@ int io_write(handle_t file, const uint8_t *buffer, size_t len)
else DEFINE_WRITE_PROXY(i2c_device_driver)
else DEFINE_WRITE_PROXY(spi_device_driver)
else DEFINE_WRITE_PROXY(filesystem_file)
else DEFINE_WRITE_PROXY(network_socket)
else
{
return -1;

View File

@ -21,10 +21,15 @@
#include <lwip/init.h>
#include <lwip/snmp.h>
#include <lwip/tcpip.h>
#include <lwip/dhcp.h>
#include <lwip/netif.h>
#include <lwip/netdb.h>
#include <netif/ethernet.h>
#include <string.h>
using namespace sys;
#define MAX_DHCP_TRIES 5
int network_init()
{
tcpip_init(NULL, NULL);
@ -69,6 +74,90 @@ public:
netif_set_default(&netif_);
}
dhcp_state_t dhcp_pooling()
{
printf("dhcp\n");
auto &netif = netif_;
uint32_t ip_address;
dhcp_state_t dhcp_state;
dhcp_state = DHCP_START;
for (;;)
{
switch (dhcp_state)
{
case DHCP_START:
{
dhcp_start(&netif);
ip_address = 0;
dhcp_state = DHCP_WAIT_ADDRESS;
}
break;
case DHCP_WAIT_ADDRESS:
{
ip_address = netif.ip_addr.addr;
if (ip_address != 0)
{
dhcp_state = DHCP_ADDRESS_ASSIGNED;
dhcp_stop(&netif);
dhcp_cleanup(&netif);
return dhcp_state;
}
else
{
struct dhcp *dhcp = netif_dhcp_data(&netif);
if (dhcp->tries > MAX_DHCP_TRIES)
{
dhcp_state = DHCP_TIMEOUT;
dhcp_stop(&netif);
dhcp_cleanup(&netif);
return dhcp_state;
}
}
}
break;
default:
return dhcp_state;
}
vTaskDelay(250);
}
return DHCP_FAIL;
}
void set_addr(const ip_address_t &ip_address, const ip_address_t &net_mask, const ip_address_t &gate_way)
{
ip4_addr_t ipaddr, netmask, gw;
IP4_ADDR(&ipaddr, ip_address.data[0], ip_address.data[1], ip_address.data[2], ip_address.data[3]);
IP4_ADDR(&netmask, net_mask.data[0], net_mask.data[1], net_mask.data[2], net_mask.data[3]);
IP4_ADDR(&gw, gate_way.data[0], gate_way.data[1], gate_way.data[2], gate_way.data[3]);
netif_set_addr(&netif_, &ipaddr, &netmask, &gw);
}
void get_addr(ip_address_t &ip_address, ip_address_t &net_mask, ip_address_t &gate_way)
{
ip_address.data[0] = ip4_addr1(&netif_.ip_addr);
ip_address.data[1] = ip4_addr2(&netif_.ip_addr);
ip_address.data[2] = ip4_addr3(&netif_.ip_addr);
ip_address.data[3] = ip4_addr4(&netif_.ip_addr);
net_mask.data[0] = ip4_addr1(&netif_.gw);
net_mask.data[1] = ip4_addr2(&netif_.gw);
net_mask.data[2] = ip4_addr3(&netif_.gw);
net_mask.data[3] = ip4_addr4(&netif_.gw);
gate_way.data[0] = ip4_addr1(&netif_.netmask);
gate_way.data[1] = ip4_addr2(&netif_.netmask);
gate_way.data[2] = ip4_addr3(&netif_.netmask);
gate_way.data[3] = ip4_addr4(&netif_.netmask);
}
private:
virtual void notify_input() override
{
@ -347,3 +436,53 @@ int network_interface_set_as_default(handle_t netif_handle)
}
CATCH_ALL;
}
int network_set_addr(handle_t netif_handle, const ip_address_t *ip_address, const ip_address_t *net_mask, const ip_address_t *gateway)
{
try
{
NETIF_ENTRY;
f->set_addr(*ip_address, *net_mask, *gateway);
return 0;
}
CATCH_ALL;
}
int network_get_addr(handle_t netif_handle, ip_address_t *ip_address, ip_address_t *net_mask, ip_address_t *gateway)
{
try
{
NETIF_ENTRY;
f->get_addr(*ip_address, *net_mask, *gateway);
return 0;
}
CATCH_ALL;
}
dhcp_state_t network_interface_dhcp_pooling(handle_t netif_handle)
{
try
{
NETIF_ENTRY;
return f->dhcp_pooling();
}
catch (...)
{
return DHCP_FAIL;
}
}
hostent_t *network_socket_gethostbyname(const char *name)
{
try
{
return reinterpret_cast<hostent_t *>(lwip_gethostbyname(name));
}
catch (...)
{
return NULL;
}
}

View File

@ -17,6 +17,7 @@
#include "kernel/driver_impl.hpp"
#include "network.h"
#include <lwip/sockets.h>
#include <string.h>
using namespace sys;
@ -34,17 +35,18 @@ static void to_lwip_sockaddr(sockaddr_in &addr, const socket_address_t &socket_a
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(*reinterpret_cast<const uint16_t *>(socket_addr.data + 4));
addr.sin_addr.s_addr = LWIP_MAKEU32(socket_addr.data[0], socket_addr.data[1], socket_addr.data[2], socket_addr.data[3]);
addr.sin_addr.s_addr = LWIP_MAKEU32(socket_addr.data[3], socket_addr.data[2], socket_addr.data[1], socket_addr.data[0]);
}
static void to_sys_sockaddr(socket_address_t &addr, const sockaddr_in &socket_addr)
{
if (socket_addr.sin_family != AF_INET)
throw std::runtime_error("Invalid socket address.");
addr.data[0] = (socket_addr.sin_addr.s_addr >> 24) & 0xFF;
addr.data[1] = (socket_addr.sin_addr.s_addr >> 16) & 0xFF;
addr.data[2] = (socket_addr.sin_addr.s_addr >> 8) & 0xFF;
addr.data[3] = socket_addr.sin_addr.s_addr & 0xFF;
addr.family = AF_INTERNETWORK;
addr.data[3] = (socket_addr.sin_addr.s_addr >> 24) & 0xFF;
addr.data[2] = (socket_addr.sin_addr.s_addr >> 16) & 0xFF;
addr.data[1] = (socket_addr.sin_addr.s_addr >> 8) & 0xFF;
addr.data[0] = socket_addr.sin_addr.s_addr & 0xFF;
*reinterpret_cast<uint16_t *>(addr.data + 4) = ntohs(socket_addr.sin_port);
}
@ -80,8 +82,8 @@ public:
int s_protocol;
switch (protocol)
{
case PROTCL_TCP:
s_protocol = IPPROTO_TCP;
case PROTCL_IP:
s_protocol = IPPROTO_IP;
break;
default:
throw std::invalid_argument("Invalid protocol type.");
@ -157,21 +159,60 @@ public:
check_lwip_error(lwip_shutdown(sock_, s_how));
}
virtual size_t send(gsl::span<const uint8_t> buffer) override
virtual size_t send(gsl::span<const uint8_t> buffer, uint8_t flags) override
{
auto ret = lwip_write(sock_, buffer.data(), buffer.size_bytes());
auto ret = lwip_send(sock_, buffer.data(), buffer.size_bytes(), flags);
check_lwip_error(ret);
configASSERT(ret == buffer.size_bytes());
return ret;
}
virtual size_t receive(gsl::span<uint8_t> buffer) override
virtual size_t receive(gsl::span<uint8_t> buffer, uint8_t flags) override
{
auto ret = lwip_recv(sock_, buffer.data(), buffer.size_bytes(), flags);
check_lwip_error(ret);
return ret;
}
virtual size_t send_to(gsl::span<const uint8_t> buffer, uint8_t flags, const socket_address_t *to) override
{
sockaddr_in remote;
socklen_t remote_len = sizeof(remote);
to_lwip_sockaddr(remote, *to);
auto ret = lwip_sendto(sock_, buffer.data(), buffer.size_bytes(), flags, reinterpret_cast<const sockaddr *>(&remote), remote_len);
check_lwip_error(ret);
configASSERT(ret == buffer.size_bytes());
return ret;
}
virtual size_t receive_from(gsl::span<uint8_t> buffer, uint8_t flags, socket_address_t *from) override
{
sockaddr_in remote;
socklen_t remote_len = sizeof(remote);
auto ret = lwip_recvfrom(sock_, buffer.data(), buffer.size_bytes(), flags, reinterpret_cast<sockaddr *>(&remote), &remote_len);
check_lwip_error(ret);
if (from)
to_sys_sockaddr(*from, remote);
return ret;
}
virtual size_t read(gsl::span<uint8_t> buffer) override
{
auto ret = lwip_read(sock_, buffer.data(), buffer.size_bytes());
check_lwip_error(ret);
return ret;
}
virtual size_t write(gsl::span<const uint8_t> buffer) override
{
auto ret = lwip_write(sock_, buffer.data(), buffer.size_bytes());
check_lwip_error(ret);
configASSERT(ret == buffer.size_bytes());
return ret;
}
private:
k_network_socket()
: sock_(0)
@ -182,16 +223,17 @@ private:
int sock_;
};
#define SOCKET_ENTRY \
auto &obj = system_handle_to_object(socket_handle); \
configASSERT(obj.is<k_network_socket>()); \
#define SOCKET_ENTRY \
auto &obj = system_handle_to_object(socket_handle); \
configASSERT(obj.is<k_network_socket>()); \
auto f = obj.as<k_network_socket>();
#define CATCH_ALL \
catch (...) { return -1; }
#define CHECK_ARG(x) \
if (!x) throw std::invalid_argument(#x " is invalid.");
if (!x) \
throw std::invalid_argument(#x " is invalid.");
handle_t network_socket_open(address_family_t address_family, socket_type_t type, protocol_type_t protocol)
{
@ -211,7 +253,7 @@ handle_t network_socket_close(handle_t socket_handle)
return io_close(socket_handle);
}
int network_socket_connect(handle_t socket_handle, const socket_address_t* remote_address)
int network_socket_connect(handle_t socket_handle, const socket_address_t *remote_address)
{
try
{
@ -236,7 +278,7 @@ int network_socket_listen(handle_t socket_handle, uint32_t backlog)
CATCH_ALL;
}
handle_t network_socket_accept(handle_t socket_handle, socket_address_t* remote_address)
handle_t network_socket_accept(handle_t socket_handle, socket_address_t *remote_address)
{
try
{
@ -263,7 +305,7 @@ int network_socket_shutdown(handle_t socket_handle, socket_shutdown_t how)
CATCH_ALL;
}
int network_socket_bind(handle_t socket_handle, const socket_address_t* remote_address)
int network_socket_bind(handle_t socket_handle, const socket_address_t *remote_address)
{
try
{
@ -275,3 +317,88 @@ int network_socket_bind(handle_t socket_handle, const socket_address_t* remote_a
}
CATCH_ALL;
}
int network_socket_send(handle_t socket_handle, const uint8_t *data, size_t len, uint8_t flags)
{
try
{
SOCKET_ENTRY;
f->send({ data, std::ptrdiff_t(len) }, flags);
return 0;
}
CATCH_ALL;
}
int network_socket_receive(handle_t socket_handle, uint8_t *data, size_t len, uint8_t flags)
{
try
{
SOCKET_ENTRY;
return f->receive({ data, std::ptrdiff_t(len) }, flags);
}
CATCH_ALL;
}
int network_socket_send_to(handle_t socket_handle, const uint8_t *data, size_t len, uint8_t flags, const socket_address_t *to)
{
try
{
SOCKET_ENTRY;
f->send_to({ data, std::ptrdiff_t(len) }, flags, to);
return 0;
}
CATCH_ALL;
}
int network_socket_receive_from(handle_t socket_handle, uint8_t *data, size_t len, uint8_t flags, socket_address_t *from)
{
try
{
SOCKET_ENTRY;
return f->receive_from({ data, std::ptrdiff_t(len) }, flags, from);
}
CATCH_ALL;
}
void network_socket_addr_parse(const char *ip_addr, int port, uint8_t *socket_addr)
{
const char *sep = ".";
char *p;
int data;
char ip_addr_p[16];
strcpy(ip_addr_p, ip_addr);
uint8_t *socket_addr_p = socket_addr;
p = strtok(ip_addr_p, sep);
while (p)
{
data = atoi(p);
if (data > 255)
throw std::invalid_argument(" ipaddr is invalid.");
*socket_addr_p++ = (uint8_t)data;
p = strtok(NULL, sep);
}
if (socket_addr_p - socket_addr != 4)
throw std::invalid_argument(" ipaddr size is invalid.");
*socket_addr_p++ = port & 0xff;
*socket_addr_p = (port >> 8) & 0xff;
}
void network_socket_addr_to_string(uint8_t *socket_addr, char *ip_addr, int *port)
{
char *p = ip_addr;
int i = 0;
do
{
char tmp[8] = { 0 };
itoa(socket_addr[i++], tmp, 10);
strcpy(p, tmp);
p += strlen(tmp);
} while ((i < 4) && (*p++ = '.'));
*port = (int)(socket_addr[4] | (socket_addr[5] << 8));
}

View File

@ -0,0 +1,158 @@
/* Copyright 2018 Canaan Inc.
*
* 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.
*/
/**
* @file
* IPv4 address API
*/
/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <adam@sics.se>
*
*/
#ifndef LWIP_HDR_IP4_ADDR_H
#define LWIP_HDR_IP4_ADDR_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/** This is the aligned version of ip4_addr_t,
used as local variable, on the stack, etc. */
struct ip4_addr {
uint32_t addr;
};
/** ip4_addr_t uses a struct for convenience only, so that the same defines can
* operate both on ip4_addr_t as well as on ip4_addr_p_t. */
typedef struct ip4_addr ip4_addr_t;
/* Forward declaration to not include netif.h */
struct netif;
/** 255.255.255.255 */
#define IPADDR_NONE ((uint32_t)0xffffffffUL)
/** 127.0.0.1 */
#define IPADDR_LOOPBACK ((uint32_t)0x7f000001UL)
/** 0.0.0.0 */
#define IPADDR_ANY ((uint32_t)0x00000000UL)
/** 255.255.255.255 */
#define IPADDR_BROADCAST ((uint32_t)0xffffffffUL)
/* Definitions of the bits in an Internet address integer.
On subnets, host and network parts are found according to
the subnet mask, not these masks. */
#define IP_CLASSA(a) ((((uint32_t)(a)) & 0x80000000UL) == 0)
#define IP_CLASSA_NET 0xff000000
#define IP_CLASSA_NSHIFT 24
#define IP_CLASSA_HOST (0xffffffff & ~IP_CLASSA_NET)
#define IP_CLASSA_MAX 128
#define IP_CLASSB(a) ((((uint32_t)(a)) & 0xc0000000UL) == 0x80000000UL)
#define IP_CLASSB_NET 0xffff0000
#define IP_CLASSB_NSHIFT 16
#define IP_CLASSB_HOST (0xffffffff & ~IP_CLASSB_NET)
#define IP_CLASSB_MAX 65536
#define IP_CLASSC(a) ((((uint32_t)(a)) & 0xe0000000UL) == 0xc0000000UL)
#define IP_CLASSC_NET 0xffffff00
#define IP_CLASSC_NSHIFT 8
#define IP_CLASSC_HOST (0xffffffff & ~IP_CLASSC_NET)
#define IP_CLASSD(a) (((uint32_t)(a) & 0xf0000000UL) == 0xe0000000UL)
#define IP_CLASSD_NET 0xf0000000 /* These ones aren't really */
#define IP_CLASSD_NSHIFT 28 /* net and host fields, but */
#define IP_CLASSD_HOST 0x0fffffff /* routing needn't know. */
#define IP_MULTICAST(a) IP_CLASSD(a)
#define IP_EXPERIMENTAL(a) (((uint32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
#define IP_BADCLASS(a) (((uint32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
#define IP_LOOPBACKNET 127 /* official! */
/** Set an IP address given by the four byte-parts */
#define IP4_ADDR(ipaddr, a,b,c,d) (ipaddr)->addr = PP_HTONL(LWIP_MAKEU32(a,b,c,d))
/** Copy IP address - faster than ip4_addr_set: no NULL check */
#define ip4_addr_copy(dest, src) ((dest).addr = (src).addr)
/** Safely copy one IP address to another (src may be NULL) */
#define ip4_addr_set(dest, src) ((dest)->addr = \
((src) == NULL ? 0 : \
(src)->addr))
/** Set complete address to zero */
#define ip4_addr_set_zero(ipaddr) ((ipaddr)->addr = 0)
/** Set address to IPADDR_ANY (no need for lwip_htonl()) */
#define ip4_addr_set_any(ipaddr) ((ipaddr)->addr = IPADDR_ANY)
/** Set address to loopback address */
#define ip4_addr_set_loopback(ipaddr) ((ipaddr)->addr = PP_HTONL(IPADDR_LOOPBACK))
/** Check if an address is in the loopback region */
#define ip4_addr_isloopback(ipaddr) (((ipaddr)->addr & PP_HTONL(IP_CLASSA_NET)) == PP_HTONL(((uint32_t)IP_LOOPBACKNET) << 24))
/** Safely copy one IP address to another and change byte order
* from host- to network-order. */
#define ip4_addr_set_hton(dest, src) ((dest)->addr = \
((src) == NULL ? 0:\
lwip_htonl((src)->addr)))
/** IPv4 only: set the IP address given as an uint32_t */
#define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
/** IPv4 only: get the IP address as an uint32_t */
#define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
/** Get the network address by combining host address with netmask */
#define ip4_addr_get_network(target, host, netmask) do { ((target)->addr = ((host)->addr) & ((netmask)->addr)); } while(0)
#define IP4ADDR_STRLEN_MAX 16
/** For backwards compatibility */
#define ip_ntoa(ipaddr) ipaddr_ntoa(ipaddr)
int sys_ip4addr_aton(const char *cp, ip4_addr_t *addr);
char *sys_ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen);
char *sys_ip4addr_ntoa(const ip4_addr_t *addr);
#ifdef __cplusplus
}
#endif
#endif /* LWIP_HDR_IP_ADDR_H */

View File

@ -0,0 +1,29 @@
/* Copyright 2018 Canaan Inc.
*
* 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 _POSIX_SYS_IP_ADDR_H
#define _POSIX_SYS_IP_ADDR_H
#include <stddef.h>
#include <stdint.h>
#include "sys/ip4_addr.h"
typedef ip4_addr_t ip_addr_t;
#define IPADDR4_INIT(u32val) \
{ \
u32val \
}
#endif

View File

@ -0,0 +1,43 @@
/* Copyright 2018 Canaan Inc.
*
* 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 _POSIX_SYS_NETDB_H
#define _POSIX_SYS_NETDB_H
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct hostent {
char *h_name; /* Official name of the host. */
char **h_aliases; /* A pointer to an array of pointers to alternative host names,
terminated by a null pointer. */
int h_addrtype; /* Address type. */
int h_length; /* The length, in bytes, of the address. */
char **h_addr_list; /* A pointer to an array of pointers to network addresses (in
network byte order) for the host, terminated by a null pointer. */
#define h_addr h_addr_list[0] /* for backward compatibility */
};
struct hostent *gethostbyname(const char *name);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -16,28 +16,60 @@
#ifndef _POSIX_SYS_SOCKET_H
#define _POSIX_SYS_SOCKET_H
#include <stddef.h>
#include <stdint.h>
#include "sys/ip_addr.h"
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
/** Create u32_t value from bytes */
#define LWIP_MAKEU32(a, b, c, d) (((uint32_t)((a)&0xff) << 24) | \
((uint32_t)((b)&0xff) << 16) | \
((uint32_t)((c)&0xff) << 8) | \
(uint32_t)((d)&0xff))
/* If your port already typedef's sa_family_t, define SA_FAMILY_T_DEFINED
to prevent this code from redefining it. */
#if !defined(sa_family_t) && !defined(SA_FAMILY_T_DEFINED)
typedef uint8_t sa_family_t;
#endif
/* If your port already typedef's in_addr_t, define IN_ADDR_T_DEFINED
to prevent this code from redefining it. */
#if !defined(in_addr_t) && !defined(IN_ADDR_T_DEFINED)
typedef uint32_t in_addr_t;
#endif
struct in_addr
{
in_addr_t s_addr;
};
/* If your port already typedef's in_port_t, define IN_PORT_T_DEFINED
to prevent this code from redefining it. */
#if !defined(in_port_t) && !defined(IN_PORT_T_DEFINED)
typedef uint16_t in_port_t;
#endif
struct sockaddr {
uint8_t sa_len;
sa_family_t sa_family;
char sa_data[14];
/* members are in network byte order */
struct sockaddr_in
{
uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
#define SIN_ZERO_LEN 8
char sin_zero[SIN_ZERO_LEN];
};
struct sockaddr
{
uint8_t sa_len;
sa_family_t sa_family;
char sa_data[14];
};
/* If your port already typedef's socklen_t, define SOCKLEN_T_DEFINED
@ -46,14 +78,15 @@ struct sockaddr {
typedef uint32_t socklen_t;
#endif
struct msghdr {
void *msg_name;
socklen_t msg_namelen;
struct iovec *msg_iov;
int msg_iovlen;
void *msg_control;
socklen_t msg_controllen;
int msg_flags;
struct msghdr
{
void *msg_name;
socklen_t msg_namelen;
struct iovec *msg_iov;
int msg_iovlen;
void *msg_control;
socklen_t msg_controllen;
int msg_flags;
};
/* struct msghdr->msg_flags bit field values */
@ -61,11 +94,13 @@ struct msghdr {
#define MSG_CTRUNC 0x08
/* RFC 3542, Section 20: Ancillary Data */
struct cmsghdr {
socklen_t cmsg_len; /* number of bytes, including header */
int cmsg_level; /* originating protocol */
int cmsg_type; /* protocol-specific type */
struct cmsghdr
{
socklen_t cmsg_len; /* number of bytes, including header */
int cmsg_level; /* originating protocol */
int cmsg_type; /* protocol-specific type */
};
/* Data section follows header and possible padding, typically referred to as
unsigned char cmsg_data[]; */
@ -91,14 +126,11 @@ will need to increase long long */
(struct cmsghdr *)((void*)((u8_t *)(cmsg) + \
ALIGN_H((cmsg)->cmsg_len)))))
#define CMSG_DATA(cmsg) ((void*)((u8_t *)(cmsg) + \
ALIGN_D(sizeof(struct cmsghdr))))
#define CMSG_DATA(cmsg) ((void *)((u8_t *)(cmsg) + ALIGN_D(sizeof(struct cmsghdr))))
#define CMSG_SPACE(length) (ALIGN_D(sizeof(struct cmsghdr)) + \
ALIGN_H(length))
#define CMSG_SPACE(length) (ALIGN_D(sizeof(struct cmsghdr)) + ALIGN_H(length))
#define CMSG_LEN(length) (ALIGN_D(sizeof(struct cmsghdr)) + \
length)
#define CMSG_LEN(length) (ALIGN_D(sizeof(struct cmsghdr)) + length)
/* Socket protocol types (TCP/UDP/RAW) */
#define SOCK_STREAM 1
@ -139,9 +171,10 @@ will need to increase long long */
/*
* Structure used for manipulating linger option.
*/
struct linger {
int l_onoff; /* option on/off */
int l_linger; /* linger time in seconds */
struct linger
{
int l_onoff; /* option on/off */
int l_linger; /* linger time in seconds */
};
/*
@ -149,7 +182,6 @@ struct linger {
*/
#define SOL_SOCKET 0xfff /* options for socket level */
#define AF_UNSPEC 0
#define AF_INET 2
#define AF_INET6 AF_UNSPEC
@ -233,16 +265,36 @@ struct linger {
#define IPTOS_PREC_ROUTINE 0x00
#ifndef SHUT_RD
#define SHUT_RD 0
#define SHUT_WR 1
#define SHUT_RDWR 2
#define SHUT_RD 0
#define SHUT_WR 1
#define SHUT_RDWR 2
#endif
#define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
#define htons(x) ((uint16_t)((((x) & (uint16_t)0x00ffU) << 8) | (((x) & (uint16_t)0xff00U) >> 8)))
#define ntohs(x) htons(x)
int socket(int domain, int type, int protocol);
int bind(int socket, const struct sockaddr *address, socklen_t address_len);
int accept(int socket, struct sockaddr *address, socklen_t *address_len);
int shutdown(int s, int how);
int connect(int socket, const struct sockaddr *address, socklen_t address_len);
int listen(int socket, int backlog);
int recv(int socket, void *mem, size_t len, int flags);
int send(int socket, const void *data, size_t size, int flags);
int recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
int sendto(int socket, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
#define inet_ntoa(addr) sys_ip4addr_ntoa((const ip4_addr_t *)&(addr))
#define inet_ntop(af, src, dst, size) \
(((af) == AF_INET) ? sys_ip4addr_ntoa_r((const ip4_addr_t *)(src), (dst), (size)) : NULL)
#define inet_pton(af, src, dst) \
(((af) == AF_INET) ? sys_ip4addr_aton((src), (ip4_addr_t *)(dst)) : 0)
#define htonl(x) ((((x)&0x000000ffUL) << 24) | \
(((x)&0x0000ff00UL) << 8) | \
(((x)&0x00ff0000UL) >> 8) | \
(((x)&0xff000000UL) >> 24))
#ifdef __cplusplus
}

272
lib/posix/ip4_addr.cpp Normal file
View File

@ -0,0 +1,272 @@
/* Copyright 2018 Canaan Inc.
*
* 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.
*/
/**
* @file
* This is the IPv4 address tools implementation.
*
*/
/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <adam@sics.se>
*
*/
#include <cstdio>
#include "sys/ip_addr.h"
#include <ctype.h>
#define lwip_isdigit(c) isdigit((unsigned char)(c))
#define lwip_isxdigit(c) isxdigit((unsigned char)(c))
#define lwip_islower(c) islower((unsigned char)(c))
#define lwip_isspace(c) isspace((unsigned char)(c))
#define lwip_isupper(c) isupper((unsigned char)(c))
#define lwip_tolower(c) tolower((unsigned char)(c))
#define lwip_toupper(c) toupper((unsigned char)(c))
#define lwip_htonl(x) ((((x)&0x000000ffUL) << 24) | (((x)&0x0000ff00UL) << 8) | (((x)&0x00ff0000UL) >> 8) | (((x)&0xff000000UL) >> 24))
/* used by IP4_ADDR_ANY and IP_ADDR_BROADCAST in ip_addr.h */
const ip_addr_t ip_addr_any = IPADDR4_INIT(IPADDR_ANY);
const ip_addr_t ip_addr_broadcast = IPADDR4_INIT(IPADDR_BROADCAST);
/**
* Ascii internet address interpretation routine.
* The value returned is in network order.
*
* @param cp IP address in ascii representation (e.g. "127.0.0.1")
* @return ip address in network order
*/
uint32_t
sys_ipaddr_addr(const char *cp)
{
ip4_addr_t val;
if (sys_ip4addr_aton(cp, &val)) {
return ip4_addr_get_u32(&val);
}
return (IPADDR_NONE);
}
/**
* Check whether "cp" is a valid ascii representation
* of an Internet address and convert to a binary address.
* Returns 1 if the address is valid, 0 if not.
* This replaces inet_addr, the return value from which
* cannot distinguish between failure and a local broadcast address.
*
* @param cp IP address in ascii representation (e.g. "127.0.0.1")
* @param addr pointer to which to save the ip address in network order
* @return 1 if cp could be converted to addr, 0 on failure
*/
int sys_ip4addr_aton(const char *cp, ip4_addr_t *addr)
{
uint32_t val;
uint8_t base;
char c;
uint32_t parts[4];
uint32_t *pp = parts;
c = *cp;
for (;;) {
/*
* Collect number up to ``.''.
* Values are specified as for C:
* 0x=hex, 0=octal, 1-9=decimal.
*/
if (!lwip_isdigit(c)) {
return 0;
}
val = 0;
base = 10;
if (c == '0') {
c = *++cp;
if (c == 'x' || c == 'X') {
base = 16;
c = *++cp;
} else {
base = 8;
}
}
for (;;) {
if (lwip_isdigit(c)) {
val = (val * base) + (uint32_t)(c - '0');
c = *++cp;
} else if (base == 16 && lwip_isxdigit(c)) {
val = (val << 4) | (uint32_t)(c + 10 - (lwip_islower(c) ? 'a' : 'A'));
c = *++cp;
} else {
break;
}
}
if (c == '.') {
/*
* Internet format:
* a.b.c.d
* a.b.c (with c treated as 16 bits)
* a.b (with b treated as 24 bits)
*/
if (pp >= parts + 3) {
return 0;
}
*pp++ = val;
c = *++cp;
} else {
break;
}
}
/*
* Check for trailing characters.
*/
if (c != '\0' && !lwip_isspace(c)) {
return 0;
}
/*
* Concoct the address according to
* the number of parts specified.
*/
switch (pp - parts + 1) {
case 0:
return 0; /* initial nondigit */
case 1: /* a -- 32 bits */
break;
case 2: /* a.b -- 8.24 bits */
if (val > 0xffffffUL) {
return 0;
}
if (parts[0] > 0xff) {
return 0;
}
val |= parts[0] << 24;
break;
case 3: /* a.b.c -- 8.8.16 bits */
if (val > 0xffff) {
return 0;
}
if ((parts[0] > 0xff) || (parts[1] > 0xff)) {
return 0;
}
val |= (parts[0] << 24) | (parts[1] << 16);
break;
case 4: /* a.b.c.d -- 8.8.8.8 bits */
if (val > 0xff) {
return 0;
}
if ((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff)) {
return 0;
}
val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
break;
default:
printf("unhandled");
break;
}
if (addr) {
ip4_addr_set_u32(addr, lwip_htonl(val));
}
return 1;
}
/**
* Convert numeric IP address into decimal dotted ASCII representation.
* returns ptr to static buffer; not reentrant!
*
* @param addr ip address in network order to convert
* @return pointer to a global static (!) buffer that holds the ASCII
* representation of addr
*/
char *
sys_ip4addr_ntoa(const ip4_addr_t *addr)
{
static char str[IP4ADDR_STRLEN_MAX];
return sys_ip4addr_ntoa_r(addr, str, IP4ADDR_STRLEN_MAX);
}
/**
* Same as ip4addr_ntoa, but reentrant since a user-supplied buffer is used.
*
* @param addr ip address in network order to convert
* @param buf target buffer where the string is stored
* @param buflen length of buf
* @return either pointer to buf which now holds the ASCII
* representation of addr or NULL if buf was too small
*/
char *
sys_ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen)
{
uint32_t s_addr;
char inv[3];
char *rp;
uint8_t *ap;
uint8_t rem;
uint8_t n;
uint8_t i;
int len = 0;
s_addr = ip4_addr_get_u32(addr);
rp = buf;
ap = (uint8_t *)&s_addr;
for (n = 0; n < 4; n++) {
i = 0;
do {
rem = *ap % (uint8_t)10;
*ap /= (uint8_t)10;
inv[i++] = (char)('0' + rem);
} while (*ap);
while (i--) {
if (len++ >= buflen) {
return NULL;
}
*rp++ = inv[i];
}
if (len++ >= buflen) {
return NULL;
}
*rp++ = '.';
ap++;
}
*--rp = 0;
return buf;
}

27
lib/posix/netdb.cpp Normal file
View File

@ -0,0 +1,27 @@
/* Copyright 2018 Canaan Inc.
*
* 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 "sys/netdb.h"
#include "utils.h"
#include <kernel/driver_impl.hpp>
#include <network.h>
#include <string.h>
using namespace sys;
struct hostent *gethostbyname(const char *name)
{
auto ret = network_socket_gethostbyname(name);
return (struct hostent *)ret;
}

View File

@ -12,15 +12,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "utils.h"
#include "sys/socket.h"
#include <network.h>
#include "utils.h"
#include <kernel/driver_impl.hpp>
#include <network.h>
#include <string.h>
using namespace sys;
#define SOCKET_ENTRY \
auto &obj = system_handle_to_object(socket); \
auto &obj = system_handle_to_object(socket); \
configASSERT(obj.is<network_socket>()); \
auto f = obj.as<network_socket>();
@ -28,17 +29,31 @@ using namespace sys;
catch (...) { return -1; }
#define CHECK_ARG(x) \
if (!x) throw std::invalid_argument(#x " is invalid.");
if (!x) \
throw std::invalid_argument(#x " is invalid.");
static void to_sys_sockaddr(socket_address_t &addr, const struct sockaddr &socket_addr)
static void to_posix_sockaddr(sockaddr_in &addr, const socket_address_t &socket_addr)
{
if (socket_addr.sa_family != AF_INET)
if (socket_addr.family != AF_INTERNETWORK)
throw std::runtime_error("Invalid socket address.");
addr.data[0] = socket_addr.sa_data[2];
addr.data[1] = socket_addr.sa_data[3];
addr.data[2] = socket_addr.sa_data[4];
addr.data[3] = socket_addr.sa_data[5];
*reinterpret_cast<uint16_t *>(addr.data + 4) = ntohs(*reinterpret_cast<const uint16_t *>(socket_addr.sa_data));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(*reinterpret_cast<const uint16_t *>(socket_addr.data + 4));
addr.sin_addr.s_addr = LWIP_MAKEU32(socket_addr.data[3], socket_addr.data[2], socket_addr.data[1], socket_addr.data[0]);
}
static void to_sys_sockaddr(socket_address_t &addr, const sockaddr_in &socket_addr)
{
if (socket_addr.sin_family != AF_INET)
throw std::runtime_error("Invalid socket address.");
addr.family = AF_INTERNETWORK;
addr.data[3] = (socket_addr.sin_addr.s_addr >> 24) & 0xFF;
addr.data[2] = (socket_addr.sin_addr.s_addr >> 16) & 0xFF;
addr.data[1] = (socket_addr.sin_addr.s_addr >> 8) & 0xFF;
addr.data[0] = socket_addr.sin_addr.s_addr & 0xFF;
*reinterpret_cast<uint16_t *>(addr.data + 4) = ntohs(socket_addr.sin_port);
}
int socket(int domain, int type, int protocol)
@ -71,8 +86,14 @@ int socket(int domain, int type, int protocol)
protocol_type_t s_protocol;
switch (protocol)
{
case IPPROTO_IP:
s_protocol = PROTCL_IP;
break;
case IPPROTO_TCP:
s_protocol = PROTCL_TCP;
s_protocol = PROTCL_IP;
break;
case IPPROTO_UDP:
s_protocol = PROTCL_IP;
break;
default:
throw std::invalid_argument("Invalid protocol.");
@ -94,9 +115,119 @@ int bind(int socket, const struct sockaddr *address, socklen_t address_len)
CHECK_ARG(address);
socket_address_t remote_addr;
to_sys_sockaddr(remote_addr, *address);
to_sys_sockaddr(remote_addr, *(reinterpret_cast<const sockaddr_in *>(address)));
f->bind(remote_addr);
return 0;
}
CATCH_ALL;
}
int accept(int socket, struct sockaddr *address, socklen_t *address_len)
{
try
{
SOCKET_ENTRY;
CHECK_ARG(address);
socket_address_t remote_addr;
auto ret = f->accept(&remote_addr);
sockaddr_in *addr = reinterpret_cast<sockaddr_in *>(address);
to_posix_sockaddr(*addr, remote_addr);
return system_alloc_handle(std::move(ret));
}
CATCH_ALL;
}
int shutdown(int socket, int how)
{
try
{
SOCKET_ENTRY;
f->shutdown((socket_shutdown_t)how);
return 0;
}
CATCH_ALL;
}
int connect(int socket, const struct sockaddr *address, socklen_t address_len)
{
try
{
SOCKET_ENTRY;
CHECK_ARG(address);
socket_address_t remote_addr;
to_sys_sockaddr(remote_addr, *reinterpret_cast<const sockaddr_in *>(address));
f->connect(remote_addr);
return 0;
}
CATCH_ALL;
}
int listen(int socket, int backlog)
{
try
{
SOCKET_ENTRY;
f->listen(backlog);
return 0;
}
CATCH_ALL;
}
int recv(int socket, void *mem, size_t len, int flags)
{
try
{
SOCKET_ENTRY;
return f->receive({ (uint8_t *)mem, std::ptrdiff_t(len) }, flags);
}
CATCH_ALL;
}
int send(int socket, const void *data, size_t size, int flags)
{
try
{
SOCKET_ENTRY;
f->send({ (const uint8_t *)data, std::ptrdiff_t(size) }, flags);
return 0;
}
CATCH_ALL;
}
int recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
{
try
{
SOCKET_ENTRY;
socket_address_t remote_addr;
auto ret = f->receive_from({ (uint8_t *)mem, std::ptrdiff_t(len) }, flags, &remote_addr);
sockaddr_in *addr = reinterpret_cast<sockaddr_in *>(from);
to_posix_sockaddr(*addr, remote_addr);
return ret;
}
CATCH_ALL;
}
int sendto(int socket, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen)
{
try
{
SOCKET_ENTRY;
socket_address_t remote_addr;
to_sys_sockaddr(remote_addr, *reinterpret_cast<const sockaddr_in *>(to));
f->send_to({ (const uint8_t *)data, std::ptrdiff_t(size) }, flags, &remote_addr);
return 0;
}
CATCH_ALL;
}

View File

@ -311,6 +311,10 @@ const ip_addr_t dns_mquery_v6group = DNS_MQUERY_IPV6_GROUP_INIT;
* Initialize the resolver: set up the UDP pcb and configure the default server
* (if DNS_SERVER_ADDRESS is set).
*/
#ifndef DNS_SERVER_ADDRESS
#define DNS_SERVER_ADDRESS(ipaddr) (ip4_addr_set_u32(ipaddr, ipaddr_addr("202.106.0.20")))
#endif
void
dns_init(void)
{

View File

@ -1368,7 +1368,7 @@ dhcp_release_and_stop(struct netif *netif)
}
/* remove IP address from interface (prevents routing from selecting this interface) */
netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
//netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
#if LWIP_DHCP_AUTOIP_COOP
if (dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {

View File

@ -915,7 +915,7 @@
* LWIP_DHCP==1: Enable DHCP module.
*/
#if !defined LWIP_DHCP || defined __DOXYGEN__
#define LWIP_DHCP 0
#define LWIP_DHCP 1
#endif
#if !LWIP_IPV4
/* disable DHCP when IPv4 is disabled */
@ -1092,7 +1092,7 @@
* transport.
*/
#if !defined LWIP_DNS || defined __DOXYGEN__
#define LWIP_DNS 0
#define LWIP_DNS 1
#endif
/** DNS maximum number of entries to maintain locally. */