Skip to content

Commit 18cec24

Browse files
Andrew Boieandrewboie
authored andcommitted
net: introduce system calls for zsock socket APIs
Add system calls for the zsock implementations of socket, close, bind, connect, listen, accept, sendto, recvfrom, fcntl, poll, inet_pton, and getaddrinfo. Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
1 parent 09c22cc commit 18cec24

File tree

6 files changed

+399
-48
lines changed

6 files changed

+399
-48
lines changed

include/net/socket.h

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,57 @@ struct zsock_addrinfo {
106106
char _ai_canonname[DNS_MAX_NAME_SIZE + 1];
107107
};
108108

109-
int zsock_socket(int family, int type, int proto);
110-
int zsock_close(int sock);
111-
int zsock_bind(int sock, const struct sockaddr *addr, socklen_t addrlen);
112-
int zsock_connect(int sock, const struct sockaddr *addr, socklen_t addrlen);
113-
int zsock_listen(int sock, int backlog);
114-
int zsock_accept(int sock, struct sockaddr *addr, socklen_t *addrlen);
115-
ssize_t zsock_send(int sock, const void *buf, size_t len, int flags);
116-
ssize_t zsock_recv(int sock, void *buf, size_t max_len, int flags);
117-
ssize_t zsock_sendto(int sock, const void *buf, size_t len, int flags,
118-
const struct sockaddr *dest_addr, socklen_t addrlen);
119-
ssize_t zsock_recvfrom(int sock, void *buf, size_t max_len, int flags,
120-
struct sockaddr *src_addr, socklen_t *addrlen);
121-
int zsock_fcntl(int sock, int cmd, int flags);
122-
int zsock_poll(struct zsock_pollfd *fds, int nfds, int timeout);
109+
__syscall int zsock_socket(int family, int type, int proto);
110+
111+
__syscall int zsock_close(int sock);
112+
113+
__syscall int zsock_bind(int sock, const struct sockaddr *addr,
114+
socklen_t addrlen);
115+
116+
__syscall int zsock_connect(int sock, const struct sockaddr *addr,
117+
socklen_t addrlen);
118+
119+
__syscall int zsock_listen(int sock, int backlog);
120+
121+
__syscall int zsock_accept(int sock, struct sockaddr *addr, socklen_t *addrlen);
122+
123+
__syscall ssize_t zsock_sendto(int sock, const void *buf, size_t len,
124+
int flags, const struct sockaddr *dest_addr,
125+
socklen_t addrlen);
126+
127+
static inline ssize_t zsock_send(int sock, const void *buf, size_t len,
128+
int flags)
129+
{
130+
return zsock_sendto(sock, buf, len, flags, NULL, 0);
131+
}
132+
133+
__syscall ssize_t zsock_recvfrom(int sock, void *buf, size_t max_len,
134+
int flags, struct sockaddr *src_addr,
135+
socklen_t *addrlen);
136+
137+
static inline ssize_t zsock_recv(int sock, void *buf, size_t max_len,
138+
int flags)
139+
{
140+
return zsock_recvfrom(sock, buf, max_len, flags, NULL, NULL);
141+
}
142+
143+
__syscall int zsock_fcntl(int sock, int cmd, int flags);
144+
145+
__syscall int zsock_poll(struct zsock_pollfd *fds, int nfds, int timeout);
146+
123147
int zsock_getsockopt(int sock, int level, int optname,
124148
void *optval, socklen_t *optlen);
149+
125150
int zsock_setsockopt(int sock, int level, int optname,
126151
const void *optval, socklen_t optlen);
127-
int zsock_inet_pton(sa_family_t family, const char *src, void *dst);
152+
153+
__syscall int zsock_inet_pton(sa_family_t family, const char *src, void *dst);
154+
155+
__syscall int z_zsock_getaddrinfo_internal(const char *host,
156+
const char *service,
157+
const struct zsock_addrinfo *hints,
158+
struct zsock_addrinfo *res);
159+
128160
int zsock_getaddrinfo(const char *host, const char *service,
129161
const struct zsock_addrinfo *hints,
130162
struct zsock_addrinfo **res);
@@ -325,6 +357,8 @@ static inline void freeaddrinfo(struct zsock_addrinfo *ai)
325357
}
326358
#endif
327359

360+
#include <syscalls/socket.h>
361+
328362
/**
329363
* @}
330364
*/

subsys/net/lib/sockets/getaddrinfo.c

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
/* Zephyr headers */
1616
#include <kernel.h>
1717
#include <net/socket.h>
18+
#include <syscall_handler.h>
1819

1920
#define AI_ARR_MAX 2
2021

@@ -81,9 +82,9 @@ static void dns_resolve_cb(enum dns_resolve_status status,
8182
}
8283

8384

84-
static int zsock_getaddrinfo_internal(const char *host, const char *service,
85-
const struct zsock_addrinfo *hints,
86-
struct zsock_addrinfo *res)
85+
int _impl_z_zsock_getaddrinfo_internal(const char *host, const char *service,
86+
const struct zsock_addrinfo *hints,
87+
struct zsock_addrinfo *res)
8788
{
8889
int family = AF_UNSPEC;
8990
long int port = 0;
@@ -164,6 +165,46 @@ static int zsock_getaddrinfo_internal(const char *host, const char *service,
164165
return 0;
165166
}
166167

168+
#ifdef CONFIG_USERSPACE
169+
Z_SYSCALL_HANDLER(z_zsock_getaddrinfo_internal, host, service, hints, res)
170+
{
171+
struct zsock_addrinfo hints_copy;
172+
char *host_copy = NULL, *service_copy = NULL;
173+
u32_t ret;
174+
175+
if (hints) {
176+
Z_OOPS(z_user_from_copy(&hints_copy, (void *)hints,
177+
sizeof(hints_copy)));
178+
}
179+
Z_OOPS(Z_SYSCALL_MEMORY_ARRAY_WRITE(res, AI_ARR_MAX,
180+
sizeof(struct zsock_addrinfo)));
181+
182+
if (service) {
183+
service_copy = z_user_string_alloc_copy((char *)service, 64);
184+
if (!service_copy) {
185+
ret = DNS_EAI_MEMORY;
186+
goto out;
187+
}
188+
}
189+
190+
if (host) {
191+
host_copy = z_user_string_alloc_copy((char *)host, 64);
192+
if (!host_copy) {
193+
ret = DNS_EAI_MEMORY;
194+
goto out;
195+
}
196+
}
197+
198+
ret = _impl_z_zsock_getaddrinfo_internal(host_copy, service_copy,
199+
hints ? &hints_copy : NULL,
200+
(struct zsock_addrinfo *)res);
201+
out:
202+
k_free(service_copy);
203+
k_free(host_copy);
204+
205+
return ret;
206+
}
207+
#endif /* CONFIG_USERSPACE */
167208

168209
int zsock_getaddrinfo(const char *host, const char *service,
169210
const struct zsock_addrinfo *hints,
@@ -175,7 +216,7 @@ int zsock_getaddrinfo(const char *host, const char *service,
175216
if (!(*res)) {
176217
return DNS_EAI_MEMORY;
177218
}
178-
ret = zsock_getaddrinfo_internal(host, service, hints, *res);
219+
ret = z_zsock_getaddrinfo_internal(host, service, hints, *res);
179220
if (ret) {
180221
free(*res);
181222
*res = NULL;

0 commit comments

Comments
 (0)