fix:Modify the hitls macro definition to be consistent with the system macro value

Signed-off-by: Dongjianwei001 <dongjianwei1@huawei.com>
This commit is contained in:
balabala-123
2026-01-31 10:27:30 +08:00
committed by Dongjianwei001
parent d4c85ccc88
commit af8b0129c3
7 changed files with 97 additions and 42 deletions
+4 -2
View File
@@ -25,6 +25,8 @@
#include "sal_file.h"
#include "bsl_sal.h"
#define FILE_SEEK_HITLS_2_POSIX(hitlsType) (hitlsType)
int32_t SAL_FILE_FOpen(bsl_sal_file_handle *stream, const char *path, const char *mode)
{
bsl_sal_file_handle temp = NULL;
@@ -129,8 +131,8 @@ int32_t SAL_FILE_FSeek(bsl_sal_file_handle stream, long offset, int32_t origin)
if (stream == NULL) {
return BSL_NULL_INPUT;
}
if (fseek(stream, offset, origin) == 0) {
int32_t posixOrigin = FILE_SEEK_HITLS_2_POSIX(origin);
if (fseek(stream, offset, posixOrigin) == 0) {
return BSL_SUCCESS;
}
+51 -20
View File
@@ -30,12 +30,18 @@
#include "bsl_errno.h"
#include "sal_net.h"
typedef union {
struct sockaddr addr;
struct sockaddr_in6 addrIn6;
struct sockaddr_in addrIn;
struct sockaddr_un addrUn;
} LinuxSockAddr;
typedef struct sockaddr_storage BSL_SOCKADDR_STORAGE;
#define ADDRESS_FAMILY_HITLS_2_POSIX(hitlsType) (hitlsType)
#define ADDRESS_SOCKET_TYPE_HITLS_2_POSIX(hitlsType) (hitlsType)
#define ADDRESS_PROTOCOL_HITLS_2_POSIX(hitlsType) (hitlsType)
#ifdef __APPLE__
#define ADDRESS_LEVEL_HITLS_2_POSIX(hitlsType) ((hitlsType) == SAL_NET_SOL_SOCKET ? 65535 : (hitlsType))
#else
#define ADDRESS_LEVEL_HITLS_2_POSIX(hitlsType) (hitlsType)
#endif
#define ADDRESS_OPTION_HITLS_2_POSIX(hitlsType) (hitlsType)
#define ADDRESS_FAMILY_POSIX_2_HITLS(hitlsType) (hitlsType)
int32_t SAL_NET_Write(int32_t fd, const void *buf, uint32_t len, int32_t *err)
{
@@ -89,7 +95,7 @@ int32_t SAL_NET_Recvfrom(int32_t sock, void *buf, size_t len, int32_t flags, voi
int32_t SAL_NET_SockAddrNew(BSL_SAL_SockAddr *sockAddr)
{
LinuxSockAddr *addr = (LinuxSockAddr *)BSL_SAL_Calloc(1, sizeof(LinuxSockAddr));
BSL_SOCKADDR_STORAGE *addr = (BSL_SOCKADDR_STORAGE *)BSL_SAL_Calloc(1, sizeof(BSL_SOCKADDR_STORAGE));
if (addr == NULL) {
return BSL_MALLOC_FAIL;
}
@@ -104,40 +110,61 @@ void SAL_NET_SockAddrFree(BSL_SAL_SockAddr sockAddr)
int32_t SAL_NET_SockAddrGetFamily(const BSL_SAL_SockAddr sockAddr)
{
const LinuxSockAddr *addr = (const LinuxSockAddr *)sockAddr;
const BSL_SOCKADDR_STORAGE *addr = (const BSL_SOCKADDR_STORAGE *)sockAddr;
if (addr == NULL) {
return AF_UNSPEC;
return ADDRESS_FAMILY_POSIX_2_HITLS(AF_UNSPEC);
}
return addr->addr.sa_family;
return ADDRESS_FAMILY_POSIX_2_HITLS(addr->ss_family);
}
uint32_t SAL_NET_SockAddrSize(const BSL_SAL_SockAddr sockAddr)
{
const LinuxSockAddr *addr = (const LinuxSockAddr *)sockAddr;
const BSL_SOCKADDR_STORAGE *addr = (const BSL_SOCKADDR_STORAGE *)sockAddr;
if (addr == NULL) {
return 0;
}
switch (addr->addr.sa_family) {
switch (addr->ss_family) {
case AF_INET:
return sizeof(addr->addrIn);
return sizeof(struct sockaddr_in);
case AF_INET6:
return sizeof(addr->addrIn6);
return sizeof(struct sockaddr_in6);
case AF_UNIX:
return sizeof(addr->addrUn);
return sizeof(struct sockaddr_un);
default:
break;
}
return sizeof(LinuxSockAddr);
return sizeof(BSL_SOCKADDR_STORAGE);
}
void SAL_NET_SockAddrCopy(BSL_SAL_SockAddr dst, BSL_SAL_SockAddr src)
{
memcpy_s(dst, sizeof(LinuxSockAddr), src, sizeof(LinuxSockAddr));
BSL_SOCKADDR_STORAGE *dstAddr = (BSL_SOCKADDR_STORAGE *)dst;
BSL_SOCKADDR_STORAGE *srcAddr = (BSL_SOCKADDR_STORAGE *)src;
uint32_t srcAddrLen = 0;
switch (srcAddr->ss_family) {
case AF_INET:
srcAddrLen = sizeof(struct sockaddr_in);
break;
case AF_INET6:
srcAddrLen = sizeof(struct sockaddr_in6);
break;
#ifdef AF_UNIX
case AF_UNIX:
srcAddrLen = sizeof(struct sockaddr_un);
break;
#endif
default:
break;
}
memcpy_s(dstAddr, sizeof(BSL_SOCKADDR_STORAGE), srcAddr, srcAddrLen);
}
int32_t SAL_NET_Socket(int32_t af, int32_t type, int32_t protocol)
{
return (int32_t)socket(af, type, protocol);
int32_t posixFamily = ADDRESS_FAMILY_HITLS_2_POSIX(af);
int32_t posixType = ADDRESS_SOCKET_TYPE_HITLS_2_POSIX(type);
int32_t posixProtocol = ADDRESS_PROTOCOL_HITLS_2_POSIX(protocol);
return (int32_t)socket(posixFamily, posixType, posixProtocol);
}
int32_t SAL_NET_SockClose(int32_t sockId)
@@ -150,7 +177,9 @@ int32_t SAL_NET_SockClose(int32_t sockId)
int32_t SAL_NET_SetSockopt(int32_t sockId, int32_t level, int32_t name, const void *val, int32_t len)
{
if (setsockopt((int32_t)sockId, level, name, (char *)(uintptr_t)val, (socklen_t)len) != 0) {
int32_t posixLevel = ADDRESS_LEVEL_HITLS_2_POSIX(level);
int32_t posixName = ADDRESS_OPTION_HITLS_2_POSIX(name);
if (setsockopt((int32_t)sockId, posixLevel, posixName, (char *)(uintptr_t)val, (socklen_t)len) != 0) {
return BSL_SAL_ERR_NET_SETSOCKOPT;
}
return BSL_SUCCESS;
@@ -158,7 +187,9 @@ int32_t SAL_NET_SetSockopt(int32_t sockId, int32_t level, int32_t name, const vo
int32_t SAL_NET_GetSockopt(int32_t sockId, int32_t level, int32_t name, void *val, int32_t *len)
{
if (getsockopt((int32_t)sockId, level, name, val, (socklen_t *)len) != 0) {
int32_t posixLevel = ADDRESS_LEVEL_HITLS_2_POSIX(level);
int32_t posixName = ADDRESS_OPTION_HITLS_2_POSIX(name);
if (getsockopt((int32_t)sockId, posixLevel, posixName, val, (socklen_t *)len) != 0) {
return BSL_SAL_ERR_NET_GETSOCKOPT;
}
return BSL_SUCCESS;
+4 -4
View File
@@ -134,20 +134,20 @@ static int32_t FilePending(BSL_UIO *uio, int32_t larg, uint64_t *ret)
BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION);
return BSL_INTERNAL_EXCEPTION;
}
if (SAL_FileSeek(f, 0, SEEK_END) != BSL_SUCCESS) { // move to the end
if (SAL_FileSeek(f, 0, SAL_NET_SEEK_END) != BSL_SUCCESS) { // move to the end
BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION);
return BSL_INTERNAL_EXCEPTION;
}
long max; // get the length
if (SAL_FileTell(f, &max) != BSL_SUCCESS || max < current) { // error, including < 0, should restore the current
(void)SAL_FileSeek(f, current, SEEK_SET);
(void)SAL_FileSeek(f, current, SAL_NET_SEEK_SET);
BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION);
return BSL_INTERNAL_EXCEPTION;
}
*ret = (uint64_t)(max - current); // save the remaining length
(void)SAL_FileSeek(f, current, SEEK_SET); // recover it
(void)SAL_FileSeek(f, current, SAL_NET_SEEK_SET); // recover it
return BSL_SUCCESS;
}
@@ -205,7 +205,7 @@ static int32_t FileReset(BSL_UIO *uio, int32_t larg, void *offset)
}
bsl_sal_file_handle *f = BSL_UIO_GetCtx(uio);
if (SAL_FileSeek(f, *(long *)offset, SEEK_SET) != 0) {
if (SAL_FileSeek(f, *(long *)offset, SAL_NET_SEEK_SET) != 0) {
BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION);
return BSL_INTERNAL_EXCEPTION;
}
+31 -2
View File
@@ -751,7 +751,7 @@ uint64_t BSL_SAL_TIME_GetNSec(void);
/**
* @ingroup bsl_sal_net
* @brief socket address.
*
*
* It should be defined like following union in linux, to cover various socket addresses.
* union SockAddr {
* struct sockaddr addr;
@@ -780,6 +780,35 @@ typedef int32_t (*BslSalSockAddrNew)(BSL_SAL_SockAddr *sockAddr);
*/
typedef void (*BslSalSockAddrFree)(BSL_SAL_SockAddr sockAddr);
#define SAL_NET_SEEK_SET 0
#define SAL_NET_SEEK_CUR 1
#define SAL_NET_SEEK_END 2
#define SAL_NET_IPV4 2 /* IPv4 Internet protocols */
#define SAL_NET_IPV6 10 /* IPv6 Internet protocols */
#define SAL_NET_IPUNIX 1 /* UNIX domain socket */
#define SAL_NET_IPANY 0 /* Any protocol */
#define SAL_NET_SOCK_STREAM 1
#define SAL_NET_SOCK_DGRAM 2
#define SAL_NET_SOCK_ALL 0 /* Indicates both SAL_NET_SOCK_STREAM and SAL_NET_SOCK_DGRAM. */
#define SAL_NET_IPPROTO_IP 0 /* IPV4 level, It is used in conjunction with the SAL_NET_SOL_* option. */
#define SAL_NET_IPPROTO_IPV6 41 /* IPV6 level, It is used in conjunction with the SAL_NET_IPV6_* option. */
#define SAL_NET_IPPROTO_TCP 6 /* It is used in conjunction with the SAL_NET_TCP_* option. */
#define SAL_NET_IPPROTO_UDP 17
#define SAL_NET_IPPROTO_SCTP 132
#define SAL_NET_IPPROTO_UNSPEC 0
#define SAL_NET_SOL_SOCKET 1 /* It is used in conjunction with the SAL_NET_SO_* option. */
#define SAL_NET_SO_REUSEADDR 2
#define SAL_NET_SO_KEEPALIVE 9
#define SAL_NET_SO_ERROR 4
#define SAL_NET_TCP_NODELAY 1
#define SAL_NET_IPV6_V6ONLY 26
#define SAL_NET_IP_MTU 14 /* Retrive the current known path MTU of the current socket. */
#define SAL_NET_IPV6_MTU 24 /* Retrive the current known path MTU of the current socket for IPV6. */
#define SAL_IPV4 2 /* IPv4 Internet protocols */
#define SAL_IPV6 10 /* IPv6 Internet protocols */
@@ -1081,7 +1110,7 @@ typedef int32_t (*BslSalNetRead)(int32_t fd, void *buf, uint32_t len, int32_t *e
* @par Description: Offsets the file read/write position to a certain position.
* @param fd [IN] File descriptor.
* @param offset [IN] The offset from the start position.
* @param origin [IN] The start position. One of SEEK_SET, SEEK_CUR and SEEK_END.
* @param origin [IN] The start position. One of SAL_NET_SEEK_SET, SAL_NET_SEEK_CUR and SAL_NET_SEEK_END.
* @return If succeed, return the new read/write position. Otherwise, return -1.
* @attention
* Thread safe : Thread-safe function.
-7
View File
@@ -50,13 +50,6 @@ typedef int32_t (*BslUioCbCtrlCb)(BSL_UIO *uio, int cmd, BSL_UIO_InfoCb *callbac
typedef struct BSL_UIO_MethodStruct BSL_UIO_Method;
/**
* @ingroup bsl_uio
*
* @brief UIO_Addr structure
*/
typedef union UIO_Address BSL_UIO_Addr;
/**
* @ingroup bsl_uio
* @brief Transmission protocol enumeration
+1 -1
View File
@@ -177,7 +177,7 @@ int32_t HITLS_Accept(HITLS_Ctx *ctx);
* @attention Only the DTLS server over udp calls this API.
* HITLS_Listen only supports ClientHellos that fit inside a single datagram.
* @param ctx [IN/OUT] TLS connection handle.
* @param clientAddr [IN/OUT] BSL_UIO_Addr pointed to by peer that sent the ClientHello.
* @param clientAddr [IN/OUT] BSL_SOCKADDR_STORAGE pointed to by peer that sent the ClientHello.
* @retval HITLS_SUCCESS, the handshake is successful.
* @retval For other error codes, see hitls_error.h.
*/
@@ -58,15 +58,15 @@
/* BEGIN_CASE */
void SDV_BSL_SAL_SOCKET_FUNC_TC001(void)
{
int32_t tcp = BSL_SAL_Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int32_t tcp = BSL_SAL_Socket(SAL_NET_IPV4, SAL_NET_SOCK_STREAM, SAL_NET_IPPROTO_TCP);
ASSERT_TRUE(tcp != -1);
struct timeval timeOut = { 0 };
timeOut.tv_sec = READ_TIME_OUT_SEC;
ASSERT_TRUE(BSL_SAL_SetSockopt(tcp, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeOut, sizeof(timeOut)) == 0);
ASSERT_TRUE(BSL_SAL_SetSockopt(tcp, SAL_NET_SOL_SOCKET, SO_RCVTIMEO, (char *)&timeOut, sizeof(timeOut)) == 0);
ASSERT_TRUE(BSL_SAL_SockClose(tcp) == 0);
int32_t udp = BSL_SAL_Socket(AF_INET, SOCK_DGRAM, 0);
int32_t udp = BSL_SAL_Socket(SAL_NET_IPV4, SAL_NET_SOCK_DGRAM, 0);
ASSERT_TRUE(udp != -1);
ASSERT_TRUE(BSL_SAL_SetSockopt(udp, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeOut, sizeof(timeOut)) == 0);
ASSERT_TRUE(BSL_SAL_SetSockopt(udp, SAL_NET_SOL_SOCKET, SO_RCVTIMEO, (char *)&timeOut, sizeof(timeOut)) == 0);
ASSERT_TRUE(BSL_SAL_SockClose(udp) == 0);
ASSERT_TRUE(TestIsErrStackEmpty());
EXIT:
@@ -93,7 +93,7 @@ static void *TestTcpClient(void *args)
remoteAddr.sin_family = AF_INET;
remoteAddr.sin_port = htons(GetPort());
remoteAddr.sin_addr.s_addr = htonl(INADDR_ANY);
int32_t socketRemote = BSL_SAL_Socket(AF_INET, SOCK_STREAM, 0);
int32_t socketRemote = BSL_SAL_Socket(SAL_NET_IPV4, SAL_NET_SOCK_STREAM, 0);
ASSERT_TRUE(socketRemote != -1);
while(true) {
int32_t ret = BSL_SAL_SockConnect(socketRemote, (BSL_SAL_SockAddr)&remoteAddr, sizeof(remoteAddr));
@@ -119,7 +119,7 @@ static void *TestTcpServer(void *args)
localAddr.sin_family = AF_INET;
localAddr.sin_port = htons(GetPort());
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
int32_t socketLocal = BSL_SAL_Socket(AF_INET, SOCK_STREAM, 0);
int32_t socketLocal = BSL_SAL_Socket(SAL_NET_IPV4, SAL_NET_SOCK_STREAM, 0);
ASSERT_TRUE(socketLocal != -1);
ASSERT_TRUE(BSL_SAL_SockBind(socketLocal, (BSL_SAL_SockAddr)&localAddr, sizeof(localAddr)) == 0);
ASSERT_TRUE(BSL_SAL_SockListen(socketLocal, 5) == 0);