netutils/usrsock_rpmsg: broadcast the client DNS server to server

Signed-off-by: songlinzhang <songlinzhang@xiaomi.com>
This commit is contained in:
songlinzhang 2022-04-14 10:30:30 +08:00 committed by Xiang Xiao
parent 7663f993f7
commit 460f631fa3
3 changed files with 70 additions and 5 deletions

View File

@ -33,12 +33,22 @@
#define USRSOCK_RPMSG_EPT_NAME "rpmsg-usrsock"
#define USRSOCK_RPMSG_DNS_REQUEST USRSOCK_REQUEST__MAX
#define USRSOCK_RPMSG_DNS_EVENT 127
/****************************************************************************
* Public Types
****************************************************************************/
/* DNS request message */
begin_packed_struct struct usrsock_rpmsg_dns_request_s
{
struct usrsock_request_common_s head;
uint16_t addrlen;
} end_packed_struct;
/* DNS event message */
begin_packed_struct struct usrsock_rpmsg_dns_event_s

View File

@ -66,6 +66,14 @@ static void usrsock_rpmsg_device_destroy(struct rpmsg_device *rdev,
static int usrsock_rpmsg_ept_cb(struct rpmsg_endpoint *ept, void *data,
size_t len, uint32_t src, void *priv);
static void usrsock_rpmsg_ns_unbind(struct rpmsg_endpoint *ept);
#ifdef CONFIG_NETDB_DNSCLIENT
static int usrsock_rpmsg_send_dns_request(void *arg,
struct sockaddr *addr,
socklen_t addrlen);
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
@ -82,9 +90,12 @@ static void usrsock_rpmsg_device_created(struct rpmsg_device *rdev,
ret = rpmsg_create_ept(&priv->ept, rdev, USRSOCK_RPMSG_EPT_NAME,
RPMSG_ADDR_ANY, RPMSG_ADDR_ANY,
usrsock_rpmsg_ept_cb, NULL);
usrsock_rpmsg_ept_cb, usrsock_rpmsg_ns_unbind);
if (ret == 0)
{
#ifdef CONFIG_NETDB_DNSCLIENT
dns_register_notify(usrsock_rpmsg_send_dns_request, priv);
#endif
sem_post(&priv->sem);
}
}
@ -105,14 +116,13 @@ static void usrsock_rpmsg_device_destroy(struct rpmsg_device *rdev,
static int usrsock_rpmsg_dns_handler(struct rpmsg_endpoint *ept, void *data,
size_t len, uint32_t src, void *priv)
{
int ret = OK;
#ifdef CONFIG_NETDB_DNSCLIENT
struct usrsock_rpmsg_dns_event_s *dns = data;
ret = dns_add_nameserver((struct sockaddr *)(dns + 1), dns->addrlen);
dns_add_nameserver((struct sockaddr *)(dns + 1), dns->addrlen);
#endif
return ret;
return 0;
}
static int usrsock_rpmsg_default_handler(struct rpmsg_endpoint *ept,
@ -155,6 +165,35 @@ static int usrsock_rpmsg_ept_cb(struct rpmsg_endpoint *ept, void *data,
return ret;
}
#ifdef CONFIG_NETDB_DNSCLIENT
static int usrsock_rpmsg_send_dns_request(void *arg,
struct sockaddr *addr,
socklen_t addrlen)
{
struct usrsock_rpmsg_s *priv = arg;
struct rpmsg_endpoint *ept = &priv->ept;
struct usrsock_rpmsg_dns_request_s *dns;
uint32_t len;
dns = rpmsg_get_tx_payload_buffer(ept, &len, true);
dns->head.reqid = USRSOCK_RPMSG_DNS_REQUEST;
dns->head.xid = 0;
dns->head.reserved = 0;
dns->addrlen = addrlen;
memcpy(dns + 1, addr, addrlen);
return rpmsg_send_nocopy(ept, dns, sizeof(*dns) + addrlen);
}
#endif
static void usrsock_rpmsg_ns_unbind(struct rpmsg_endpoint *ept)
{
#ifdef CONFIG_NETDB_DNSCLIENT
dns_unregister_notify(usrsock_rpmsg_send_dns_request, ept->priv);
#endif
}
/****************************************************************************
* Public Functions
****************************************************************************/

View File

@ -104,6 +104,9 @@ static int usrsock_rpmsg_accept_handler(struct rpmsg_endpoint *ept,
static int usrsock_rpmsg_ioctl_handler(struct rpmsg_endpoint *ept,
void *data, size_t len,
uint32_t src, void *priv_);
static int usrsock_rpmsg_dns_handler(struct rpmsg_endpoint *ept,
void *data, size_t len,
uint32_t src, void *priv_);
static void usrsock_rpmsg_ns_bind(struct rpmsg_device *rdev, void *priv_,
const char *name, uint32_t dest);
@ -136,6 +139,7 @@ static const rpmsg_ept_cb g_usrsock_rpmsg_handler[] =
[USRSOCK_REQUEST_LISTEN] = usrsock_rpmsg_listen_handler,
[USRSOCK_REQUEST_ACCEPT] = usrsock_rpmsg_accept_handler,
[USRSOCK_REQUEST_IOCTL] = usrsock_rpmsg_ioctl_handler,
[USRSOCK_RPMSG_DNS_REQUEST] = usrsock_rpmsg_dns_handler,
};
/****************************************************************************
@ -755,6 +759,18 @@ static int usrsock_rpmsg_ioctl_handler(struct rpmsg_endpoint *ept,
ack, req->head.xid, ret, req->arglen, req->arglen);
}
static int usrsock_rpmsg_dns_handler(struct rpmsg_endpoint *ept, void *data,
size_t len, uint32_t src, void *priv_)
{
#ifdef CONFIG_NETDB_DNSCLIENT
struct usrsock_rpmsg_dns_request_s *dns = data;
dns_add_nameserver((struct sockaddr *)(dns + 1), dns->addrlen);
#endif
return 0;
}
#ifdef CONFIG_NETDB_DNSCLIENT
static int usrsock_rpmsg_send_dns_event(void *arg,
struct sockaddr *addr,
@ -860,7 +876,7 @@ static int usrsock_rpmsg_ept_cb(struct rpmsg_endpoint *ept, void *data,
{
return usrsock_rpmsg_sendto_handler(ept, data, len, src, priv);
}
else if (common->reqid >= 0 && common->reqid < USRSOCK_REQUEST__MAX)
else if (common->reqid >= 0 && common->reqid <= USRSOCK_REQUEST__MAX)
{
return g_usrsock_rpmsg_handler[common->reqid](ept, data, len,
src, priv);