mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2025-12-05 15:15:29 +00:00
format: format sal socket code
This commit is contained in:
@@ -174,9 +174,9 @@ typedef struct ip_mreq
|
||||
|
||||
/* Options for shatdown type */
|
||||
#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
|
||||
|
||||
struct sockaddr
|
||||
@@ -260,7 +260,7 @@ struct cmsghdr
|
||||
|
||||
#define CMSG_NXTHDR(mhdr, cmsg) cmsg_nxthdr((mhdr), (cmsg))
|
||||
|
||||
#define CMSG_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long)-1))
|
||||
#define CMSG_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long) - 1))
|
||||
|
||||
#define CMSG_DATA(cmsg) ((void *)(cmsg) + sizeof(struct cmsghdr))
|
||||
#define CMSG_SPACE(len) (sizeof(struct cmsghdr) + CMSG_ALIGN(len))
|
||||
@@ -333,10 +333,10 @@ struct sal_ifreq
|
||||
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_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_sendmsg(int socket, const struct msghdr *message, int flags);
|
||||
|
||||
@@ -74,7 +74,7 @@ struct ifconf
|
||||
int ifc_len; /* Size of buffer. */
|
||||
union
|
||||
{
|
||||
char* ifcu_buf;
|
||||
char *ifcu_buf;
|
||||
struct sal_ifreq *ifcu_req;
|
||||
} ifc_ifcu;
|
||||
};
|
||||
@@ -95,44 +95,52 @@ static struct sal_netdev_res_table sal_dev_res_tbl[SAL_SOCKETS_NUM];
|
||||
#define SAL_SOCKOPS_PROTO_TLS_VALID(sock, name) (proto_tls && (proto_tls->ops->name) && IS_SOCKET_PROTO_TLS(sock))
|
||||
|
||||
#define SAL_SOCKOPT_PROTO_TLS_EXEC(sock, name, optval, optlen) \
|
||||
do { \
|
||||
if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, name)){ \
|
||||
do \
|
||||
{ \
|
||||
if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, name)) \
|
||||
{ \
|
||||
return proto_tls->ops->name((sock)->user_data_tls, (optval), (optlen)); \
|
||||
} \
|
||||
}while(0) \
|
||||
} while (0)
|
||||
|
||||
#define SAL_SOCKET_OBJ_GET(sock, socket) \
|
||||
do { \
|
||||
do \
|
||||
{ \
|
||||
(sock) = sal_get_socket(socket); \
|
||||
if ((sock) == RT_NULL) { \
|
||||
if ((sock) == RT_NULL) \
|
||||
{ \
|
||||
return -1; \
|
||||
} \
|
||||
}while(0) \
|
||||
} while (0)
|
||||
|
||||
#define SAL_NETDEV_IS_UP(netdev) \
|
||||
do { \
|
||||
if (!netdev_is_up(netdev)) { \
|
||||
do \
|
||||
{ \
|
||||
if (!netdev_is_up(netdev)) \
|
||||
{ \
|
||||
return -1; \
|
||||
} \
|
||||
}while(0) \
|
||||
} while (0)
|
||||
|
||||
#define SAL_NETDEV_SOCKETOPS_VALID(netdev, pf, ops) \
|
||||
do { \
|
||||
(pf) = (struct sal_proto_family *) netdev->sal_user_data; \
|
||||
if ((pf)->skt_ops->ops == RT_NULL){ \
|
||||
do \
|
||||
{ \
|
||||
(pf) = (struct sal_proto_family *)netdev->sal_user_data; \
|
||||
if ((pf)->skt_ops->ops == RT_NULL) \
|
||||
{ \
|
||||
return -1; \
|
||||
} \
|
||||
}while(0) \
|
||||
} while (0)
|
||||
|
||||
#define SAL_NETDEV_NETDBOPS_VALID(netdev, pf, ops) \
|
||||
((netdev) && netdev_is_up(netdev) && \
|
||||
((pf) = (struct sal_proto_family *) (netdev)->sal_user_data) != RT_NULL && \
|
||||
(pf)->netdb_ops->ops) \
|
||||
((pf) = (struct sal_proto_family *)(netdev)->sal_user_data) != RT_NULL && \
|
||||
(pf)->netdb_ops->ops)
|
||||
|
||||
#define SAL_NETDBOPS_VALID(netdev, pf, ops) \
|
||||
((netdev) && \
|
||||
((pf) = (struct sal_proto_family *) (netdev)->sal_user_data) != RT_NULL && \
|
||||
(pf)->netdb_ops->ops) \
|
||||
((pf) = (struct sal_proto_family *)(netdev)->sal_user_data) != RT_NULL && \
|
||||
(pf)->netdb_ops->ops)
|
||||
|
||||
/**
|
||||
* SAL (Socket Abstraction Layer) initialize.
|
||||
@@ -196,11 +204,11 @@ static void check_netdev_internet_up_work(struct rt_work *work, void *work_data)
|
||||
socklen_t addr_len = sizeof(struct sockaddr_in);
|
||||
char send_data[SAL_INTERNET_BUFF_LEN], recv_data = 0;
|
||||
|
||||
const char month[][SAL_INTERNET_MONTH_LEN] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
||||
const char month[][SAL_INTERNET_MONTH_LEN] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
|
||||
char date[SAL_INTERNET_DATE_LEN];
|
||||
unsigned int moth_num = 0;
|
||||
|
||||
struct sal_proto_family *pf = (struct sal_proto_family *) netdev->sal_user_data;
|
||||
struct sal_proto_family *pf = (struct sal_proto_family *)netdev->sal_user_data;
|
||||
const struct sal_socket_ops *skt_ops;
|
||||
|
||||
if (work)
|
||||
@@ -215,7 +223,7 @@ static void check_netdev_internet_up_work(struct rt_work *work, void *work_data)
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
host = (struct hostent *) pf->netdb_ops->gethostbyname(SAL_INTERNET_HOST);
|
||||
host = (struct hostent *)pf->netdb_ops->gethostbyname(SAL_INTERNET_HOST);
|
||||
if (host == RT_NULL)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
@@ -238,8 +246,8 @@ static void check_netdev_internet_up_work(struct rt_work *work, void *work_data)
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
/* set receive and send timeout */
|
||||
skt_ops->setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (void *) &timeout, sizeof(timeout));
|
||||
skt_ops->setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (void *) &timeout, sizeof(timeout));
|
||||
skt_ops->setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout));
|
||||
skt_ops->setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof(timeout));
|
||||
|
||||
/* get build moth value*/
|
||||
rt_memset(date, 0x00, SAL_INTERNET_DATE_LEN);
|
||||
@@ -343,7 +351,7 @@ int sal_check_netdev_internet_up(struct netdev *netdev)
|
||||
int sal_proto_tls_register(const struct sal_proto_tls *pt)
|
||||
{
|
||||
RT_ASSERT(pt);
|
||||
proto_tls = (struct sal_proto_tls *) pt;
|
||||
proto_tls = (struct sal_proto_tls *)pt;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -362,7 +370,7 @@ struct sal_socket *sal_get_socket(int socket)
|
||||
|
||||
socket = socket - SAL_SOCKET_OFFSET;
|
||||
|
||||
if (socket < 0 || socket >= (int) st->max_socket)
|
||||
if (socket < 0 || socket >= (int)st->max_socket)
|
||||
{
|
||||
return RT_NULL;
|
||||
}
|
||||
@@ -426,8 +434,7 @@ int sal_netdev_cleanup(struct netdev *netdev)
|
||||
{
|
||||
rt_thread_mdelay(100);
|
||||
}
|
||||
}
|
||||
while (find_dev);
|
||||
} while (find_dev);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -492,7 +499,7 @@ static int socket_init(int family, int type, int protocol, struct sal_socket **r
|
||||
if (netdv_def && netdev_is_up(netdv_def))
|
||||
{
|
||||
/* check default network interface device protocol family */
|
||||
pf = (struct sal_proto_family *) netdv_def->sal_user_data;
|
||||
pf = (struct sal_proto_family *)netdv_def->sal_user_data;
|
||||
if (pf != RT_NULL && pf->skt_ops && (pf->family == family || pf->sec_family == family))
|
||||
{
|
||||
sock->netdev = netdv_def;
|
||||
@@ -523,7 +530,7 @@ static int socket_alloc(struct sal_socket_table *st, int f_socket)
|
||||
int idx;
|
||||
|
||||
/* find an empty socket entry */
|
||||
for (idx = f_socket; idx < (int) st->max_socket; idx++)
|
||||
for (idx = f_socket; idx < (int)st->max_socket; idx++)
|
||||
{
|
||||
if (st->sockets[idx] == RT_NULL)
|
||||
{
|
||||
@@ -532,7 +539,7 @@ static int socket_alloc(struct sal_socket_table *st, int f_socket)
|
||||
}
|
||||
|
||||
/* allocate a larger sockte container */
|
||||
if (idx == (int) st->max_socket && st->max_socket < SAL_SOCKETS_NUM)
|
||||
if (idx == (int)st->max_socket && st->max_socket < SAL_SOCKETS_NUM)
|
||||
{
|
||||
int cnt, index;
|
||||
struct sal_socket **sockets;
|
||||
@@ -556,7 +563,7 @@ static int socket_alloc(struct sal_socket_table *st, int f_socket)
|
||||
}
|
||||
|
||||
/* allocate 'struct sal_socket' */
|
||||
if (idx < (int) st->max_socket && st->sockets[idx] == RT_NULL)
|
||||
if (idx < (int)st->max_socket && st->sockets[idx] == RT_NULL)
|
||||
{
|
||||
st->sockets[idx] = rt_calloc(1, sizeof(struct sal_socket));
|
||||
if (st->sockets[idx] == RT_NULL)
|
||||
@@ -590,7 +597,7 @@ static int socket_new(void)
|
||||
idx = socket_alloc(st, 0);
|
||||
|
||||
/* can't find an empty sal socket entry */
|
||||
if (idx == (int) st->max_socket)
|
||||
if (idx == (int)st->max_socket)
|
||||
{
|
||||
idx = -(1 + SAL_SOCKET_OFFSET);
|
||||
goto __result;
|
||||
@@ -617,7 +624,7 @@ static void socket_delete(int socket)
|
||||
int idx;
|
||||
|
||||
idx = socket - SAL_SOCKET_OFFSET;
|
||||
if (idx < 0 || idx >= (int) st->max_socket)
|
||||
if (idx < 0 || idx >= (int)st->max_socket)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -685,7 +692,7 @@ int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
|
||||
|
||||
static void sal_sockaddr_to_ipaddr(const struct sockaddr *name, ip_addr_t *local_ipaddr)
|
||||
{
|
||||
const struct sockaddr_in *svr_addr = (const struct sockaddr_in *) name;
|
||||
const struct sockaddr_in *svr_addr = (const struct sockaddr_in *)name;
|
||||
|
||||
#if NETDEV_IPV4 && NETDEV_IPV6
|
||||
local_ipaddr->u_addr.ip4.addr = svr_addr->sin_addr.s_addr;
|
||||
@@ -870,7 +877,7 @@ int sal_setsockopt(int socket, int level, int optname, const void *optval, sockl
|
||||
}
|
||||
else
|
||||
{
|
||||
return pf->skt_ops->setsockopt((int) sock->user_data, level, optname, optval, optlen);
|
||||
return pf->skt_ops->setsockopt((int)sock->user_data, level, optname, optval, optlen);
|
||||
}
|
||||
#else
|
||||
return pf->skt_ops->setsockopt((int)(size_t)sock->user_data, level, optname, optval, optlen);
|
||||
@@ -1048,7 +1055,7 @@ int sal_sendto(int socket, const void *dataptr, size_t size, int flags,
|
||||
}
|
||||
else
|
||||
{
|
||||
return pf->skt_ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen);
|
||||
return pf->skt_ops->sendto((int)sock->user_data, dataptr, size, flags, to, tolen);
|
||||
}
|
||||
#else
|
||||
return pf->skt_ops->sendto((int)(size_t)sock->user_data, dataptr, size, flags, to, tolen);
|
||||
@@ -1206,13 +1213,13 @@ int sal_ioctlsocket(int socket, long cmd, void *arg)
|
||||
if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
|
||||
{
|
||||
addr_in = (struct sockaddr_in *)&(ifr->ifr_ifru.ifru_addr);
|
||||
#if NETDEV_IPV4 && NETDEV_IPV6
|
||||
#if NETDEV_IPV4 && NETDEV_IPV6
|
||||
addr_in->sin_addr.s_addr = sock->netdev->ip_addr.u_addr.ip4.addr;
|
||||
#elif NETDEV_IPV4
|
||||
#elif NETDEV_IPV4
|
||||
addr_in->sin_addr.s_addr = sock->netdev->ip_addr.addr;
|
||||
#elif NETDEV_IPV6
|
||||
#error "not only support IPV6"
|
||||
#endif /* NETDEV_IPV4 && NETDEV_IPV6*/
|
||||
#elif NETDEV_IPV6
|
||||
#error "not only support IPV6"
|
||||
#endif /* NETDEV_IPV4 && NETDEV_IPV6*/
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
@@ -1229,13 +1236,13 @@ int sal_ioctlsocket(int socket, long cmd, void *arg)
|
||||
if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
|
||||
{
|
||||
addr_in = (struct sockaddr_in *)&(ifr->ifr_ifru.ifru_addr);
|
||||
#if NETDEV_IPV4 && NETDEV_IPV6
|
||||
#if NETDEV_IPV4 && NETDEV_IPV6
|
||||
addr_in->sin_addr.s_addr = netdev->ip_addr.u_addr.ip4.addr;
|
||||
#elif NETDEV_IPV4
|
||||
#elif NETDEV_IPV4
|
||||
addr_in->sin_addr.s_addr = netdev->ip_addr.addr;
|
||||
#elif NETDEV_IPV6
|
||||
#error "Do not only support IPV6"
|
||||
#endif /* NETDEV_IPV4 && NETDEV_IPV6 */
|
||||
#elif NETDEV_IPV6
|
||||
#error "Do not only support IPV6"
|
||||
#endif /* NETDEV_IPV4 && NETDEV_IPV6 */
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1277,13 +1284,13 @@ int sal_ioctlsocket(int socket, long cmd, void *arg)
|
||||
if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
|
||||
{
|
||||
addr_in = (struct sockaddr_in *)&(ifr->ifr_ifru.ifru_netmask);
|
||||
#if NETDEV_IPV4 && NETDEV_IPV6
|
||||
#if NETDEV_IPV4 && NETDEV_IPV6
|
||||
addr_in->sin_addr.s_addr = sock->netdev->netmask.u_addr.ip4.addr;
|
||||
#elif NETDEV_IPV4
|
||||
#elif NETDEV_IPV4
|
||||
addr_in->sin_addr.s_addr = sock->netdev->netmask.addr;
|
||||
#elif NETDEV_IPV6
|
||||
#error "not only support IPV6"
|
||||
#endif /* NETDEV_IPV4 && NETDEV_IPV6*/
|
||||
#elif NETDEV_IPV6
|
||||
#error "not only support IPV6"
|
||||
#endif /* NETDEV_IPV4 && NETDEV_IPV6*/
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
@@ -1300,13 +1307,13 @@ int sal_ioctlsocket(int socket, long cmd, void *arg)
|
||||
if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
|
||||
{
|
||||
addr_in = (struct sockaddr_in *)&(ifr->ifr_ifru.ifru_netmask);
|
||||
#if NETDEV_IPV4 && NETDEV_IPV6
|
||||
#if NETDEV_IPV4 && NETDEV_IPV6
|
||||
addr_in->sin_addr.s_addr = netdev->netmask.u_addr.ip4.addr;
|
||||
#elif NETDEV_IPV4
|
||||
#elif NETDEV_IPV4
|
||||
addr_in->sin_addr.s_addr = netdev->netmask.addr;
|
||||
#elif NETDEV_IPV6
|
||||
#error "not only support IPV6"
|
||||
#endif /* NETDEV_IPV4 && NETDEV_IPV6*/
|
||||
#elif NETDEV_IPV6
|
||||
#error "not only support IPV6"
|
||||
#endif /* NETDEV_IPV4 && NETDEV_IPV6*/
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -1344,18 +1351,18 @@ int sal_ioctlsocket(int socket, long cmd, void *arg)
|
||||
}
|
||||
|
||||
case SIOCGIFHWADDR:
|
||||
if (!strcmp(ifr->ifr_ifrn.ifrn_name,sock->netdev->name))
|
||||
if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
|
||||
{
|
||||
addr = (struct sockaddr *)&(ifr->ifr_ifru.ifru_hwaddr);
|
||||
#ifdef RT_USING_LWP
|
||||
if (!strcmp("lo", sock->netdev->name))
|
||||
{
|
||||
struct musl_ifreq * musl_ifreq_tmp = (struct musl_ifreq *)arg;
|
||||
struct musl_ifreq *musl_ifreq_tmp = (struct musl_ifreq *)arg;
|
||||
musl_ifreq_tmp->ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_LOOPBACK;
|
||||
}
|
||||
else
|
||||
{
|
||||
struct musl_ifreq * musl_ifreq_tmp = (struct musl_ifreq *)arg;
|
||||
struct musl_ifreq *musl_ifreq_tmp = (struct musl_ifreq *)arg;
|
||||
musl_ifreq_tmp->ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_ETHER;
|
||||
}
|
||||
#endif
|
||||
@@ -1379,12 +1386,12 @@ int sal_ioctlsocket(int socket, long cmd, void *arg)
|
||||
#ifdef RT_USING_LWP
|
||||
if (!strcmp("lo", netdev->name))
|
||||
{
|
||||
struct musl_ifreq * musl_ifreq_tmp = (struct musl_ifreq *)arg;
|
||||
struct musl_ifreq *musl_ifreq_tmp = (struct musl_ifreq *)arg;
|
||||
musl_ifreq_tmp->ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_LOOPBACK;
|
||||
}
|
||||
else
|
||||
{
|
||||
struct musl_ifreq * musl_ifreq_tmp = (struct musl_ifreq *)arg;
|
||||
struct musl_ifreq *musl_ifreq_tmp = (struct musl_ifreq *)arg;
|
||||
musl_ifreq_tmp->ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_ETHER;
|
||||
}
|
||||
#endif
|
||||
@@ -1609,12 +1616,12 @@ int sal_getaddrinfo(const char *nodename,
|
||||
}
|
||||
}
|
||||
|
||||
if(ret == RT_EOK)
|
||||
if (ret == RT_EOK)
|
||||
{
|
||||
/*record the netdev and res*/
|
||||
for(i = 0; i < SAL_SOCKETS_NUM; i++)
|
||||
for (i = 0; i < SAL_SOCKETS_NUM; i++)
|
||||
{
|
||||
if(sal_dev_res_tbl[i].res == RT_NULL)
|
||||
if (sal_dev_res_tbl[i].res == RT_NULL)
|
||||
{
|
||||
sal_dev_res_tbl[i].res = *res;
|
||||
sal_dev_res_tbl[i].netdev = netdev;
|
||||
@@ -1623,7 +1630,6 @@ int sal_getaddrinfo(const char *nodename,
|
||||
}
|
||||
|
||||
RT_ASSERT((i < SAL_SOCKETS_NUM));
|
||||
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -1636,9 +1642,9 @@ void sal_freeaddrinfo(struct addrinfo *ai)
|
||||
rt_uint32_t i = 0;
|
||||
|
||||
/*when use the multi netdev, it must free the ai use the getaddrinfo netdev */
|
||||
for(i = 0; i < SAL_SOCKETS_NUM; i++)
|
||||
for (i = 0; i < SAL_SOCKETS_NUM; i++)
|
||||
{
|
||||
if(sal_dev_res_tbl[i].res == ai)
|
||||
if (sal_dev_res_tbl[i].res == ai)
|
||||
{
|
||||
netdev = sal_dev_res_tbl[i].netdev;
|
||||
sal_dev_res_tbl[i].res = RT_NULL;
|
||||
|
||||
Reference in New Issue
Block a user