-
Notifications
You must be signed in to change notification settings - Fork 8.2k
[WIP, 1.9] Bootstrapping BSD Sockets like API #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
pfalcon
wants to merge
12
commits into
zephyrproject-rtos:master
from
pfalcon:net-sockets-bootstrap1
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4d3c4bd
net: sockets: Bootstrap Sockets API implementation
pfalcon e0ec24f
net: context, pkt: Changes for Sockets API
pfalcon 23bf8f2
net: sockets: Add configurable option to provide raw POSIX API names
pfalcon 6db54c8
net: context: Allow to put context into FIFO at expense of user_data
pfalcon f284be2
WIP: kernel: fifo: Add peek_head/peek_tail accessors.
pfalcon 656b31b
net: sockets: Implement bind(), connect(), listen(), accept()
pfalcon 4a60185
net: sockets: Add POSIX compat defines for inet_ntop, inet_pton
pfalcon 1bf6933
net: sockets: Implement send()
pfalcon 2919b53
net: sockets: Implement recv() for STREAM sockets
pfalcon f73a3b8
samples: net: Add socket-based echo server example
pfalcon ac6ec7f
net: sockets: Add debug logging config
pfalcon 287230f
net: sockets: Explicitly flush conn/pkt queue on close()
pfalcon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Copyright (c) 2017 Linaro Limited | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef __NET_SOCKET_H | ||
| #define __NET_SOCKET_H | ||
|
|
||
| #include <sys/types.h> | ||
| #include <zephyr/types.h> | ||
| #include <net/net_ip.h> | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| int zsock_socket(int family, int type, int proto); | ||
| int zsock_close(int sock); | ||
| int zsock_bind(int sock, const struct sockaddr *addr, socklen_t addrlen); | ||
| int zsock_connect(int sock, const struct sockaddr *addr, socklen_t addrlen); | ||
| int zsock_listen(int sock, int backlog); | ||
| int zsock_accept(int sock, struct sockaddr *addr, socklen_t *addrlen); | ||
| ssize_t zsock_send(int sock, const void *buf, size_t len, int flags); | ||
| ssize_t zsock_recv(int sock, void *buf, size_t max_len, int flags); | ||
|
|
||
| #if defined(CONFIG_NET_SOCKETS_POSIX_NAMES) | ||
| #define socket zsock_socket | ||
| #define close zsock_close | ||
| #define bind zsock_bind | ||
| #define connect zsock_connect | ||
| #define listen zsock_listen | ||
| #define accept zsock_accept | ||
| #define send zsock_send | ||
| #define recv zsock_recv | ||
|
|
||
| #define inet_ntop net_addr_ntop | ||
| #define inet_pton net_addr_pton | ||
| #endif | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* __NET_SOCKET_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Makefile - simple socket-based echo server | ||
|
|
||
| # | ||
| # Copyright (c) 2017 Linaro Limited | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
|
|
||
| BOARD ?= qemu_x86 | ||
| CONF_FILE ?= prj_$(BOARD).conf | ||
|
|
||
| include $(ZEPHYR_BASE)/Makefile.inc | ||
|
|
||
| ifeq ($(CONFIG_NET_L2_BLUETOOTH), y) | ||
| QEMU_EXTRA_FLAGS = -serial unix:/tmp/bt-server-bredr | ||
| else | ||
| include $(ZEPHYR_BASE)/samples/net/common/Makefile.ipstack | ||
| endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # General config | ||
| CONFIG_NEWLIB_LIBC=y | ||
|
|
||
| # Networking config | ||
| CONFIG_NETWORKING=y | ||
| CONFIG_NET_IPV4=y | ||
| CONFIG_NET_IPV6=n | ||
| CONFIG_NET_TCP=y | ||
| CONFIG_NET_SOCKETS=y | ||
| CONFIG_NET_SOCKETS_POSIX_NAMES=y | ||
|
|
||
| # Network driver config | ||
| CONFIG_NET_SLIP_TAP=y | ||
| CONFIG_TEST_RANDOM_GENERATOR=y | ||
|
|
||
| # Without CONFIG_NET_BUF_LOG printf() doesn't work | ||
| CONFIG_NET_BUF_LOG=y | ||
|
|
||
| # Network address config | ||
| CONFIG_NET_APP_SETTINGS=y | ||
| CONFIG_NET_APP_MY_IPV4_ADDR="192.0.2.1" | ||
| CONFIG_NET_APP_PEER_IPV4_ADDR="192.0.2.2" | ||
|
|
||
| # Network debug config | ||
| #CONFIG_NET_DEBUG_SOCKETS=y | ||
| CONFIG_SYS_LOG_NET_LEVEL=2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| include $(ZEPHYR_BASE)/samples/net/common/Makefile.common | ||
|
|
||
| obj-y += socket_echo.o |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Copyright (c) 2017 Linaro Limited | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <stdio.h> | ||
|
|
||
| #ifndef __ZEPHYR__ | ||
|
|
||
| #include <netinet/in.h> | ||
| #include <sys/socket.h> | ||
| #include <arpa/inet.h> | ||
| #include <unistd.h> | ||
|
|
||
| #define init_net() | ||
|
|
||
| #else | ||
|
|
||
| #include <net/socket.h> | ||
| #include <kernel.h> | ||
| #include <net_sample_app.h> | ||
|
|
||
| void init_net(void) | ||
| { | ||
| int ret = net_sample_app_init("socket_echo", NET_SAMPLE_NEED_IPV4, | ||
| K_SECONDS(3)); | ||
|
|
||
| if (ret < 0) { | ||
| printf("Application init failed\n"); | ||
| k_panic(); | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
|
|
||
| int main(void) | ||
| { | ||
| int serv; | ||
| struct sockaddr_in bind_addr; | ||
|
|
||
| init_net(); | ||
|
|
||
| serv = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | ||
|
|
||
| bind_addr.sin_family = AF_INET; | ||
| bind_addr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
| bind_addr.sin_port = htons(4242); | ||
| bind(serv, (struct sockaddr *)&bind_addr, sizeof(bind_addr)); | ||
|
|
||
| listen(serv, 5); | ||
|
|
||
| while (1) { | ||
| struct sockaddr_in client_addr; | ||
| socklen_t client_addr_len = sizeof(client_addr); | ||
| char addr_str[32]; | ||
| int client = accept(serv, (struct sockaddr *)&client_addr, | ||
| &client_addr_len); | ||
| inet_ntop(client_addr.sin_family, &client_addr.sin_addr, | ||
| addr_str, sizeof(addr_str)); | ||
| printf("Connection from %s\n", addr_str); | ||
|
|
||
| while (1) { | ||
| char buf[128]; | ||
| int len = recv(client, buf, sizeof(buf), 0); | ||
|
|
||
| if (len == 0) { | ||
| break; | ||
| } | ||
| send(client, buf, len, 0); | ||
| } | ||
|
|
||
| close(client); | ||
| printf("Connection from %s closed\n", addr_str); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Kconfig - BSD Sockets like API | ||
|
|
||
| # | ||
| # Copyright (c) 2017 Linaro Limited. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
|
|
||
| menuconfig NET_SOCKETS | ||
| bool "BSD Sockets like API" | ||
| default n | ||
| help | ||
| Provide BSD Sockets like API on top of native Zephyr networking API. | ||
|
|
||
| if NET_SOCKETS | ||
|
|
||
| config NET_SOCKETS_POSIX_NAMES | ||
| bool "Standard POSIX names for Sockets API" | ||
| default n | ||
| help | ||
| By default, Sockets API function are prefixed with "zsock_" to avoid | ||
| namespacing issues. If this option is enabled, they will be provided | ||
| with standard POSIX names like socket(), recv(), and close(), to help | ||
| with porting existing code. Note that close() may require a special | ||
| attention, as in POSIX it closes any file descriptor, while with this | ||
| option enaled, it will still apply only to sockets. | ||
|
|
||
| config NET_DEBUG_SOCKETS | ||
| bool "Debug BSD Sockets like API calls" | ||
| default n | ||
| help | ||
| Enables logging for sockets code. (Logging level is defined by | ||
| SYS_LOG_NET_LEVEL setting). | ||
|
|
||
| endif # NET_SOCKETS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ccflags-y += -I$(srctree)/subsys/net/lib/sockets | ||
|
|
||
| obj-$(CONFIG_NET_SOCKETS) += sockets.o |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am fine with having a separate directory for this so we do not clutter subsys/net/ip too much.