mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-09-22 11:30:01 +08:00
[net][sal] Add SAL (socket abstraction layer) framework
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* File : dfs_net.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2015-2016, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2015-02-17 Bernard First version
|
||||
* 2016-05-05 Bernard rename dfs_lwip to dfs_net.
|
||||
*/
|
||||
|
||||
#ifndef DFS_NET_H__
|
||||
#define DFS_NET_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <dfs_file.h>
|
||||
|
||||
const struct dfs_file_ops* dfs_net_get_fops(void);
|
||||
int dfs_net_getsocket(int fd);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* File : select.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2015-05-02 Bernard First version
|
||||
*/
|
||||
|
||||
#ifndef SELECT_H__
|
||||
#define SELECT_H__
|
||||
|
||||
#include <dfs_select.h>
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* File : sal.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-05-17 ChenYong First version
|
||||
*/
|
||||
|
||||
#ifndef SAL_H__
|
||||
#define SAL_H__
|
||||
|
||||
#include <dfs_file.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
|
||||
typedef uint32_t socklen_t;
|
||||
#endif
|
||||
|
||||
/* sal socket magic word */
|
||||
#define SAL_SOCKET_MAGIC 0x5A10
|
||||
|
||||
/* The maximum number of sockets structure */
|
||||
#define SAL_SOCKETS_NUM DFS_FD_MAX
|
||||
|
||||
/* The maximum number of protocol families */
|
||||
#ifndef SAL_PROTO_FAMILIES_NUM
|
||||
#define SAL_PROTO_FAMILIES_NUM 4
|
||||
#endif
|
||||
|
||||
/* sal socket offset */
|
||||
#ifndef SAL_SOCKET_OFFSET
|
||||
#define SAL_SOCKET_OFFSET 0
|
||||
#endif
|
||||
|
||||
struct proto_ops
|
||||
{
|
||||
int (*socket) (int domain, int type, int protocol);
|
||||
int (*closesocket)(int s);
|
||||
int (*bind) (int s, const struct sockaddr *name, socklen_t namelen);
|
||||
int (*listen) (int s, int backlog);
|
||||
int (*connect) (int s, const struct sockaddr *name, socklen_t namelen);
|
||||
int (*accept) (int s, struct sockaddr *addr, socklen_t *addrlen);
|
||||
int (*sendto) (int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
|
||||
int (*recvfrom) (int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
|
||||
int (*getsockopt) (int s, int level, int optname, void *optval, socklen_t *optlen);
|
||||
int (*setsockopt) (int s, int level, int optname, const void *optval, socklen_t optlen);
|
||||
int (*shutdown) (int s, int how);
|
||||
int (*getpeername)(int s, struct sockaddr *name, socklen_t *namelen);
|
||||
int (*getsockname)(int s, struct sockaddr *name, socklen_t *namelen);
|
||||
int (*ioctlsocket)(int s, long cmd, void *arg);
|
||||
int (*poll) (struct dfs_fd *file, struct rt_pollreq *req);
|
||||
};
|
||||
|
||||
struct sal_socket
|
||||
{
|
||||
uint32_t magic; /* sal socket magic word */
|
||||
|
||||
int socket; /* sal socket descriptor */
|
||||
int domain;
|
||||
int type;
|
||||
int protocol;
|
||||
|
||||
const struct proto_ops *ops; /* socket options */
|
||||
void *user_data; /* specific sal socket data */
|
||||
};
|
||||
|
||||
struct proto_family
|
||||
{
|
||||
int family; /* primary protocol families type*/
|
||||
int sec_family; /* secondary protocol families type*/
|
||||
int (*create)(struct sal_socket *sal_socket, int type, int protocol); /* register socket options */
|
||||
|
||||
struct hostent* (*gethostbyname) (const char *name);
|
||||
int (*gethostbyname_r)(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop);
|
||||
void (*freeaddrinfo) (struct addrinfo *ai);
|
||||
int (*getaddrinfo) (const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
|
||||
};
|
||||
|
||||
/* SAL socket initialization */
|
||||
int sal_init(void);
|
||||
|
||||
int sal_proto_family_register(const struct proto_family *pf);
|
||||
struct sal_socket *sal_get_socket(int sock);
|
||||
|
||||
#endif /* SAL_H__ */
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* File : sal_ipaddr.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-05-18 ChenYong First version
|
||||
*/
|
||||
#ifndef SAL_IPADDR_H__
|
||||
#define SAL_IPADDR_H__
|
||||
|
||||
#include "sal_type.h"
|
||||
|
||||
/** IPv4 only: set the IP address given as an u32_t */
|
||||
#define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
|
||||
/** IPv4 only: get the IP address as an u32_t */
|
||||
#define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
|
||||
|
||||
#define IP4ADDR_STRLEN_MAX 16
|
||||
|
||||
/* These macros should be calculated by the preprocessor and are used
|
||||
with compile-time constants only (so that there is no little-endian
|
||||
overhead at runtime). */
|
||||
#define PP_HTONS(x) ((((x) & 0x00ffUL) << 8) | (((x) & 0xff00UL) >> 8))
|
||||
#define PP_NTOHS(x) PP_HTONS(x)
|
||||
#define PP_HTONL(x) ((((x) & 0x000000ffUL) << 24) | \
|
||||
(((x) & 0x0000ff00UL) << 8) | \
|
||||
(((x) & 0x00ff0000UL) >> 8) | \
|
||||
(((x) & 0xff000000UL) >> 24))
|
||||
#define PP_NTOHL(x) PP_HTONL(x)
|
||||
|
||||
#define htons(x) (u16_t)PP_HTONS(x)
|
||||
#define ntohs(x) (u16_t)PP_NTOHS(x)
|
||||
#define htonl(x) (u32_t)PP_HTONL(x)
|
||||
#define ntohl(x) (u32_t)PP_NTOHL(x)
|
||||
|
||||
/* 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 u32_t in_addr_t;
|
||||
#endif
|
||||
|
||||
struct in_addr
|
||||
{
|
||||
in_addr_t s_addr;
|
||||
};
|
||||
|
||||
struct in6_addr
|
||||
{
|
||||
union
|
||||
{
|
||||
u32_t u32_addr[4];
|
||||
u8_t u8_addr[16];
|
||||
} un;
|
||||
#define s6_addr un.u8_addr
|
||||
};
|
||||
|
||||
enum sal_ip_addr_type
|
||||
{
|
||||
/** IPv4 */
|
||||
IPADDR_TYPE_V4 = 0U,
|
||||
/** IPv6 */
|
||||
IPADDR_TYPE_V6 = 6U,
|
||||
/** IPv4+IPv6 ("dual-stack") */
|
||||
IPADDR_TYPE_ANY = 46U
|
||||
};
|
||||
|
||||
typedef struct ip4_addr
|
||||
{
|
||||
u32_t addr;
|
||||
} ip4_addr_t;
|
||||
|
||||
typedef struct ip6_addr
|
||||
{
|
||||
u32_t addr[4];
|
||||
} ip6_addr_t;
|
||||
|
||||
typedef struct _ip_addr
|
||||
{
|
||||
union
|
||||
{
|
||||
ip6_addr_t ip6;
|
||||
ip4_addr_t ip4;
|
||||
} u_addr;
|
||||
/** @ref sal_ip_addr_type */
|
||||
u8_t type;
|
||||
} ip_addr_t;
|
||||
|
||||
/** 255.255.255.255 */
|
||||
#define IPADDR_NONE ((u32_t)0xffffffffUL)
|
||||
/** 127.0.0.1 */
|
||||
#define IPADDR_LOOPBACK ((u32_t)0x7f000001UL)
|
||||
/** 0.0.0.0 */
|
||||
#define IPADDR_ANY ((u32_t)0x00000000UL)
|
||||
/** 255.255.255.255 */
|
||||
#define IPADDR_BROADCAST ((u32_t)0xffffffffUL)
|
||||
|
||||
/** 255.255.255.255 */
|
||||
#define INADDR_NONE IPADDR_NONE
|
||||
/** 127.0.0.1 */
|
||||
#define INADDR_LOOPBACK IPADDR_LOOPBACK
|
||||
/** 0.0.0.0 */
|
||||
#define INADDR_ANY IPADDR_ANY
|
||||
/** 255.255.255.255 */
|
||||
#define INADDR_BROADCAST IPADDR_BROADCAST
|
||||
|
||||
#define IPADDR_BROADCAST_STRING "255.255.255.255"
|
||||
|
||||
in_addr_t sal_ipaddr_addr(const char *cp);
|
||||
int sal_ip4addr_aton(const char *cp, ip4_addr_t *addr);
|
||||
char *sal_ip4addr_ntoa(const ip4_addr_t *addr);
|
||||
char *sal_ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen);
|
||||
|
||||
#define inet_addr(cp) sal_ipaddr_addr(cp)
|
||||
#define inet_aton(cp,addr) sal_ip4addr_aton(cp,(ip4_addr_t*)addr)
|
||||
#define inet_ntoa(addr) sal_ip4addr_ntoa((const ip4_addr_t*)&(addr))
|
||||
#define inet_ntoa_r(addr, buf, buflen) sal_ip4addr_ntoa_r((const ip4_addr_t*)&(addr), buf, buflen)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SAL_IPADDR_H__ */
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* File : sal_netdb.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-05-24 ChenYong First version
|
||||
*/
|
||||
#ifndef SAL_NETDB_H__
|
||||
#define SAL_NETDB_H__
|
||||
|
||||
#include <sal_socket.h>
|
||||
|
||||
#define EAI_NONAME 200
|
||||
#define EAI_SERVICE 201
|
||||
#define EAI_FAIL 202
|
||||
#define EAI_MEMORY 203
|
||||
#define EAI_FAMILY 204
|
||||
|
||||
#define HOST_NOT_FOUND 210
|
||||
#define NO_DATA 211
|
||||
#define NO_RECOVERY 212
|
||||
#define TRY_AGAIN 213
|
||||
|
||||
#define AI_PASSIVE 0x01
|
||||
#define AI_CANONNAME 0x02
|
||||
#define AI_NUMERICHOST 0x04
|
||||
#define AI_NUMERICSERV 0x08
|
||||
#define AI_V4MAPPED 0x10
|
||||
#define AI_ALL 0x20
|
||||
#define AI_ADDRCONFIG 0x40
|
||||
|
||||
/* input flags for structure addrinfo */
|
||||
#define AI_PASSIVE 0x01
|
||||
#define AI_CANONNAME 0x02
|
||||
#define AI_NUMERICHOST 0x04
|
||||
#define AI_NUMERICSERV 0x08
|
||||
#define AI_V4MAPPED 0x10
|
||||
#define AI_ALL 0x20
|
||||
#define AI_ADDRCONFIG 0x40
|
||||
|
||||
#define DNS_MAX_NAME_LENGTH 256
|
||||
|
||||
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 addrinfo {
|
||||
int ai_flags; /* Input flags. */
|
||||
int ai_family; /* Address family of socket. */
|
||||
int ai_socktype; /* Socket type. */
|
||||
int ai_protocol; /* Protocol of socket. */
|
||||
socklen_t ai_addrlen; /* Length of socket address. */
|
||||
struct sockaddr *ai_addr; /* Socket address of socket. */
|
||||
char *ai_canonname; /* Canonical name of service location. */
|
||||
struct addrinfo *ai_next; /* Pointer to next in list. */
|
||||
};
|
||||
|
||||
struct hostent *sal_gethostbyname(const char *name);
|
||||
|
||||
int sal_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
|
||||
size_t buflen, struct hostent **result, int *h_errnop);
|
||||
void sal_freeaddrinfo(struct addrinfo *ai);
|
||||
int sal_getaddrinfo(const char *nodename,
|
||||
const char *servname,
|
||||
const struct addrinfo *hints,
|
||||
struct addrinfo **res);
|
||||
|
||||
#endif /* SAL_NETDB_H__ */
|
||||
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* File : sal_socket.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-05-24 ChenYong First version
|
||||
*/
|
||||
|
||||
#ifndef SAL_SOCKET_H__
|
||||
#define SAL_SOCKET_H__
|
||||
|
||||
#include "sal_ipaddr.h"
|
||||
|
||||
#if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
|
||||
typedef uint32_t socklen_t;
|
||||
#endif
|
||||
|
||||
#if !defined(sa_family_t) && !defined(SA_FAMILY_T_DEFINED)
|
||||
typedef uint8_t sa_family_t;
|
||||
#endif
|
||||
|
||||
/* 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
|
||||
|
||||
/* Socket protocol types (TCP/UDP/RAW) */
|
||||
#define SOCK_STREAM 1
|
||||
#define SOCK_DGRAM 2
|
||||
#define SOCK_RAW 3
|
||||
|
||||
#define SOCK_MAX (SOCK_RAW + 1)
|
||||
|
||||
/*
|
||||
* Option flags per-socket. These must match the SOF_ flags in ip.h (checked in init.c)
|
||||
*/
|
||||
#define SO_REUSEADDR 0x0004 /* Allow local address reuse */
|
||||
#define SO_KEEPALIVE 0x0008 /* keep connections alive */
|
||||
#define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
|
||||
|
||||
/*
|
||||
* Additional options, not kept in so_options.
|
||||
*/
|
||||
#define SO_DEBUG 0x0001 /* Unimplemented: turn on debugging info recording */
|
||||
#define SO_ACCEPTCONN 0x0002 /* socket has had listen() */
|
||||
#define SO_DONTROUTE 0x0010 /* Unimplemented: just use interface addresses */
|
||||
#define SO_USELOOPBACK 0x0040 /* Unimplemented: bypass hardware when possible */
|
||||
#define SO_LINGER 0x0080 /* linger on close if data present */
|
||||
#define SO_DONTLINGER ((int)(~SO_LINGER))
|
||||
#define SO_OOBINLINE 0x0100 /* Unimplemented: leave received OOB data in line */
|
||||
#define SO_REUSEPORT 0x0200 /* Unimplemented: allow local address & port reuse */
|
||||
#define SO_SNDBUF 0x1001 /* Unimplemented: send buffer size */
|
||||
#define SO_RCVBUF 0x1002 /* receive buffer size */
|
||||
#define SO_SNDLOWAT 0x1003 /* Unimplemented: send low-water mark */
|
||||
#define SO_RCVLOWAT 0x1004 /* Unimplemented: receive low-water mark */
|
||||
#define SO_SNDTIMEO 0x1005 /* send timeout */
|
||||
#define SO_RCVTIMEO 0x1006 /* receive timeout */
|
||||
#define SO_ERROR 0x1007 /* get error status and clear */
|
||||
#define SO_TYPE 0x1008 /* get socket type */
|
||||
#define SO_CONTIMEO 0x1009 /* Unimplemented: connect timeout */
|
||||
#define SO_NO_CHECK 0x100a /* don't create UDP checksum */
|
||||
|
||||
/*
|
||||
* Level number for (get/set)sockopt() to apply to socket itself.
|
||||
*/
|
||||
#define SOL_SOCKET 0xfff /* options for socket level */
|
||||
|
||||
#define AF_UNSPEC 0
|
||||
#define AF_INET 2
|
||||
#define AF_INET6 10
|
||||
#define AF_CAN 29 /* Controller Area Network */
|
||||
#define AF_AT 45 /* AT socket */
|
||||
#define PF_INET AF_INET
|
||||
#define PF_INET6 AF_INET6
|
||||
#define PF_UNSPEC AF_UNSPEC
|
||||
#define PF_CAN AF_CAN
|
||||
#define PF_AT AF_AT
|
||||
|
||||
#define AF_MAX (AF_AT + 1) /* For now.. */
|
||||
|
||||
#define IPPROTO_IP 0
|
||||
#define IPPROTO_ICMP 1
|
||||
#define IPPROTO_TCP 6
|
||||
#define IPPROTO_UDP 17
|
||||
#define IPPROTO_IPV6 41
|
||||
#define IPPROTO_ICMPV6 58
|
||||
#define IPPROTO_UDPLITE 136
|
||||
#define IPPROTO_RAW 255
|
||||
|
||||
/* Flags we can use with send and recv. */
|
||||
#define MSG_PEEK 0x01 /* Peeks at an incoming message */
|
||||
#define MSG_WAITALL 0x02 /* Unimplemented: Requests that the function block until the full amount of data requested can be returned */
|
||||
#define MSG_OOB 0x04 /* Unimplemented: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific */
|
||||
#define MSG_DONTWAIT 0x08 /* Nonblocking i/o for this operation only */
|
||||
#define MSG_MORE 0x10 /* Sender will send more */
|
||||
|
||||
/*
|
||||
* Options for level IPPROTO_TCP
|
||||
*/
|
||||
#define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
|
||||
#define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */
|
||||
#define TCP_KEEPIDLE 0x03 /* set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */
|
||||
#define TCP_KEEPINTVL 0x04 /* set pcb->keep_intvl - Use seconds for get/setsockopt */
|
||||
#define TCP_KEEPCNT 0x05 /* set pcb->keep_cnt - Use number of probes sent for get/setsockopt */
|
||||
|
||||
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_storage {
|
||||
uint8_t s2_len;
|
||||
sa_family_t ss_family;
|
||||
char s2_data1[2];
|
||||
uint32_t s2_data2[3];
|
||||
#if SAL_IPV6
|
||||
u32_t s2_data3[3];
|
||||
#endif /* SAL_IPV6 */
|
||||
};
|
||||
|
||||
int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen);
|
||||
int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen);
|
||||
int sal_shutdown(int socket, int how);
|
||||
int sal_getpeername (int socket, struct sockaddr *name, socklen_t *namelen);
|
||||
int sal_getsockname (int socket, struct sockaddr *name, socklen_t *namelen);
|
||||
int sal_getsockopt (int socket, int level, int optname, void *optval, socklen_t *optlen);
|
||||
int sal_setsockopt (int socket, int level, int optname, const void *optval, socklen_t optlen);
|
||||
int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen);
|
||||
int sal_listen(int socket, int backlog);
|
||||
int sal_recvfrom(int socket, void *mem, size_t len, int flags,
|
||||
struct sockaddr *from, socklen_t *fromlen);
|
||||
int sal_sendto(int socket, const void *dataptr, size_t size, int flags,
|
||||
const struct sockaddr *to, socklen_t tolen);
|
||||
int sal_socket(int domain, int type, int protocol);
|
||||
int sal_closesocket(int socket);
|
||||
int sal_ioctlsocket(int socket, long cmd, void *arg);
|
||||
|
||||
#endif /* SAL_SOCKET_H__ */
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* File : sal_type.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-05-17 ChenYong First version
|
||||
*/
|
||||
|
||||
#ifndef SAL_TYPE_H__
|
||||
#define SAL_TYPE_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef int8_t err_t;
|
||||
typedef uint8_t u8_t;
|
||||
typedef int8_t s8_t;
|
||||
typedef uint16_t u16_t;
|
||||
typedef int16_t s16_t;
|
||||
typedef uint32_t u32_t;
|
||||
typedef int32_t s32_t;
|
||||
typedef uintptr_t mem_ptr_t;
|
||||
|
||||
#endif /* SAL_TYPE_H__ */
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* File : inet.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2015-02-17 Bernard First version
|
||||
*/
|
||||
|
||||
#ifndef INET_H__
|
||||
#define INET_H__
|
||||
|
||||
#include <sal_ipaddr.h>
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* File : netdb.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2015-02-17 Bernard First version
|
||||
* 2108-05-24 ChenYong Add socket abstraction layer
|
||||
*/
|
||||
|
||||
#ifndef NETDB_H__
|
||||
#define NETDB_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <sal_netdb.h>
|
||||
|
||||
struct hostent *gethostbyname(const char *name);
|
||||
|
||||
int gethostbyname_r(const char *name, struct hostent *ret, char *buf,
|
||||
size_t buflen, struct hostent **result, int *h_errnop);
|
||||
void freeaddrinfo(struct addrinfo *ai);
|
||||
int getaddrinfo(const char *nodename,
|
||||
const char *servname,
|
||||
const struct addrinfo *hints,
|
||||
struct addrinfo **res);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* File : in.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2015-02-17 Bernard First version
|
||||
*/
|
||||
|
||||
#ifndef IN_H__
|
||||
#define IN_H__
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
#ifndef IN6_IS_ADDR_MULTICAST
|
||||
#define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* File : tcp.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2015-02-17 Bernard First version
|
||||
*/
|
||||
|
||||
#ifndef TCP_H__
|
||||
#define TCP_H__
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* File : udp.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2015-02-17 Bernard First version
|
||||
*/
|
||||
|
||||
#ifndef UDP_H__
|
||||
#define UDP_H__
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* File : socket.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2015-02-17 Bernard First version
|
||||
* 2018-05-17 ChenYong Add socket abstraction layer
|
||||
*/
|
||||
|
||||
#ifndef SYS_SOCKET_H_
|
||||
#define SYS_SOCKET_H_
|
||||
|
||||
#include <sal_socket.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
|
||||
int bind(int s, const struct sockaddr *name, socklen_t namelen);
|
||||
int shutdown(int s, int how);
|
||||
int getpeername (int s, struct sockaddr *name, socklen_t *namelen);
|
||||
int getsockname (int s, struct sockaddr *name, socklen_t *namelen);
|
||||
int getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen);
|
||||
int setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen);
|
||||
int connect(int s, const struct sockaddr *name, socklen_t namelen);
|
||||
int listen(int s, int backlog);
|
||||
int recv(int s, void *mem, size_t len, int flags);
|
||||
int recvfrom(int s, void *mem, size_t len, int flags,
|
||||
struct sockaddr *from, socklen_t *fromlen);
|
||||
int send(int s, const void *dataptr, size_t size, int flags);
|
||||
int sendto(int s, const void *dataptr, size_t size, int flags,
|
||||
const struct sockaddr *to, socklen_t tolen);
|
||||
int socket(int domain, int type, int protocol);
|
||||
int closesocket(int s);
|
||||
int ioctlsocket(int s, long cmd, void *arg);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _SYS_SOCKET_H_ */
|
||||
Reference in New Issue
Block a user