[net][sal] Add SAL components TLS features support

Signed-off-by: chenyong <1521761801@qq.com>
This commit is contained in:
chenyong committed 2018-11-15 14:48:20 +08:00
1 parent 1353a41183
commit beaff7c09d
9 files changed
+608 -92

No files matched your search

+5
View File
@@ -21,6 +21,11 @@ config RT_USING_SAL
bool "Support AT Commands stack"
default y
depends on AT_USING_SOCKET
config SAL_USING_TLS
bool "Support MbedTLS protocol"
default y
depends on PKG_USING_MBEDTLS
endmenu
endif
+4 -1
View File
@@ -15,10 +15,13 @@ if GetDepend('SAL_USING_LWIP'):
if GetDepend('SAL_USING_AT'):
src += Glob('impl/af_inet_at.c')
if GetDepend('SAL_USING_LWIP') or GetDepend('SAL_USING_AT'):
CPPPATH += [cwd + '/impl']
if GetDepend('SAL_USING_TLS'):
src += Glob('impl/proto_mbedtls.c')
if GetDepend('SAL_USING_POSIX'):
CPPPATH += [cwd + '/include/dfs_net']
src += Glob('socket/net_sockets.c')
+14 -9
View File
@@ -62,7 +62,7 @@ static int at_poll(struct dfs_fd *file, struct rt_pollreq *req)
}
#endif
static const struct proto_ops at_inet_stream_ops =
static const struct sal_socket_ops at_socket_ops =
{
at_socket,
at_closesocket,
@@ -90,25 +90,30 @@ static int at_create(struct sal_socket *socket, int type, int protocol)
//TODO Check type & protocol
socket->ops = &at_inet_stream_ops;
socket->ops = &at_socket_ops;
return 0;
}
static const struct proto_family at_inet_family_ops = {
"at",
static struct sal_proto_ops at_proto_ops =
{
at_gethostbyname,
NULL,
at_getaddrinfo,
at_freeaddrinfo,
};
static const struct sal_proto_family at_inet_family =
{
AF_AT,
AF_INET,
at_create,
at_gethostbyname,
NULL,
at_freeaddrinfo,
at_getaddrinfo,
&at_proto_ops,
};
int at_inet_init(void)
{
sal_proto_family_register(&at_inet_family_ops);
sal_proto_family_register(&at_inet_family);
return 0;
}
+15 -9
View File
@@ -259,7 +259,8 @@ static int inet_poll(struct dfs_fd *file, struct rt_pollreq *req)
}
#endif
static const struct proto_ops lwip_inet_stream_ops = {
static const struct sal_socket_ops lwip_socket_ops =
{
inet_socket,
lwip_close,
lwip_bind,
@@ -286,25 +287,30 @@ static int inet_create(struct sal_socket *socket, int type, int protocol)
//TODO Check type & protocol
socket->ops = &lwip_inet_stream_ops;
socket->ops = &lwip_socket_ops;
return 0;
}
static const struct proto_family lwip_inet_family_ops = {
"lwip",
static struct sal_proto_ops lwip_proto_ops =
{
lwip_gethostbyname,
lwip_gethostbyname_r,
lwip_getaddrinfo,
lwip_freeaddrinfo,
};
static const struct sal_proto_family lwip_inet_family =
{
AF_INET,
AF_INET,
inet_create,
lwip_gethostbyname,
lwip_gethostbyname_r,
lwip_freeaddrinfo,
lwip_getaddrinfo,
&lwip_proto_ops,
};
int lwip_inet_init(void)
{
sal_proto_family_register(&lwip_inet_family_ops);
sal_proto_family_register(&lwip_inet_family);
return 0;
}
File diff suppressed because it is too large. Load diff
+27 -18
View File
@@ -25,7 +25,7 @@ extern "C" {
typedef uint32_t socklen_t;
#endif
/* sal socket magic word */
/* SAL socket magic word */
#define SAL_SOCKET_MAGIC 0x5A10
/* The maximum number of sockets structure */
@@ -38,12 +38,12 @@ typedef uint32_t socklen_t;
#define SAL_PROTO_FAMILIES_NUM 4
#endif
/* sal socket offset */
/* SAL socket offset */
#ifndef SAL_SOCKET_OFFSET
#define SAL_SOCKET_OFFSET 0
#endif
struct proto_ops
struct sal_socket_ops
{
int (*socket) (int domain, int type, int protocol);
int (*closesocket)(int s);
@@ -64,30 +64,38 @@ struct proto_ops
#endif
};
struct sal_proto_ops
{
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);
int (*getaddrinfo) (const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
void (*freeaddrinfo) (struct addrinfo *ai);
};
struct sal_socket
{
uint32_t magic; /* sal socket magic word */
uint32_t magic; /* SAL socket magic word */
int socket; /* sal socket descriptor */
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 */
const struct sal_socket_ops *ops; /* socket options */
void *user_data; /* user-specific data */
#ifdef SAL_USING_TLS
void *user_data_tls; /* user-specific TLS data */
#endif
};
struct proto_family
struct sal_proto_family
{
char name[RT_NAME_MAX];
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 */
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);
struct sal_proto_ops *ops; /* protocol family options */
};
/* SAL(Socket Abstraction Layer) initialize */
@@ -95,10 +103,11 @@ int sal_init(void);
struct sal_socket *sal_get_socket(int sock);
/* protocol family register and unregister operate */
int sal_proto_family_register(const struct proto_family *pf);
int sal_proto_family_unregister(const struct proto_family *pf);
struct proto_family *sal_proto_family_find(const char *name);
/* SAL protocol family register and unregister operate */
int sal_proto_family_register(const struct sal_proto_family *pf);
int sal_proto_family_unregister(int family);
rt_bool_t sal_proto_family_is_registered(int family);
struct sal_proto_family *sal_proto_family_find(int family);
#ifdef __cplusplus
}
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-11-10 ChenYong First version
*/
#ifndef __SAL_TLS_H__
#define __SAL_TLS_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <rtthread.h>
/* Protocol level for TLS.
* Here, the same socket protocol level for TLS as in Linux was used.
*/
#define SOL_TLS 282
/* Socket options for TLS */
/* Socket option to select TLS credentials to use. */
#define TLS_CRET_LIST 1
/* Socket option to set select ciphersuites to use. */
#define TLS_CIPHERSUITE_LIST 2
/* Socket option to set peer verification level for TLS connection. */
#define TLS_PEER_VERIFY 3
/* Socket option to set role for DTLS connection. */
#define TLS_DTLS_ROLE 4
/* Protocol numbers for TLS protocols */
#define PROTOCOL_TLS 256
#define PROTOCOL_DTLS 257
struct sal_proto_tls_ops
{
int (*init)(void);
void* (*socket)(int socket);
int (*connect)(void *sock);
int (*send)(void *sock, const void *data, size_t size);
int (*recv)(void *sock, void *mem, size_t len);
int (*closesocket)(void *sock);
int (*set_cret_list)(void *sock, const void *cert, size_t size); /* Set TLS credentials */
int (*set_ciphersurite)(void *sock, const void* ciphersurite, size_t size); /* Set select ciphersuites */
int (*set_peer_verify)(void *sock, const void* peer_verify, size_t size); /* Set peer verification */
int (*set_dtls_role)(void *sock, const void *dtls_role, size_t size); /* Set role for DTLS */
};
struct sal_proto_tls
{
char name[RT_NAME_MAX]; /* TLS protocol name */
const struct sal_proto_tls_ops *ops; /* SAL TLS protocol options */
};
/* SAL TLS protocol register */
int sal_proto_tls_register(const struct sal_proto_tls *pt);
#ifdef __cplusplus
}
#endif
#endif /* __SAL_TLS_H__ */
@@ -14,6 +14,9 @@
#include <rtthread.h>
#include <sal_socket.h>
#ifdef SAL_USING_TLS
#include <sal_tls.h>
#endif
#ifdef __cplusplus
extern "C" {
File diff suppressed because it is too large. Load diff