Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions include/net/net_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ struct net_conn_handle;
* anyway. This saves 12 bytes / context in IPv6.
*/
struct net_context {
/** User data.
*
* First member of the structure to let users either have user data
* associated with a context, or put contexts into a FIFO.
*/
void *user_data;

/** Reference count
*/
atomic_t refcount;
Expand Down Expand Up @@ -206,10 +213,6 @@ struct net_context {
*/
net_context_connect_cb_t connect_cb;

/** User data.
*/
void *user_data;

#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
/** Get TX net_buf pool for this context.
*/
Expand Down Expand Up @@ -237,6 +240,14 @@ struct net_context {
/** TCP connection information */
struct net_tcp *tcp;
#endif /* CONFIG_NET_TCP */

#if defined(CONFIG_NET_SOCKETS)
/** Per-socket packet or connection queues */
union {
struct k_fifo recv_q;
struct k_fifo accept_q;
};
#endif /* CONFIG_NET_SOCKETS */
};

static inline bool net_context_is_used(struct net_context *context)
Expand Down
20 changes: 17 additions & 3 deletions include/net/net_pkt.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ struct net_pkt {
sys_snode_t sent_list;
#endif

u8_t sent : 1; /* Is this sent or not
u8_t sent_or_eof: 1; /* For outgoing packet: is this sent or not
* For incoming packet of a socket: last
* packet before EOF
* Used only if defined(CONFIG_NET_TCP)
*/
u8_t forwarding : 1; /* Are we forwarding this pkt
Expand Down Expand Up @@ -188,13 +190,25 @@ static inline void net_pkt_set_next_hdr(struct net_pkt *pkt, u8_t *hdr)
#if defined(CONFIG_NET_TCP)
static inline u8_t net_pkt_sent(struct net_pkt *pkt)
{
return pkt->sent;
return pkt->sent_or_eof;
}

static inline void net_pkt_set_sent(struct net_pkt *pkt, bool sent)
{
pkt->sent = sent;
pkt->sent_or_eof = sent;
}

#if defined(CONFIG_NET_SOCKETS)
static inline u8_t net_pkt_eof(struct net_pkt *pkt)
{
return pkt->sent_or_eof;
}

static inline void net_pkt_set_eof(struct net_pkt *pkt, bool eof)
{
pkt->sent_or_eof = eof;
}
#endif
#endif

#if defined(CONFIG_NET_ROUTE)
Expand Down
45 changes: 45 additions & 0 deletions include/net/socket.h
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 */
18 changes: 18 additions & 0 deletions samples/net/socket_echo/Makefile
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
26 changes: 26 additions & 0 deletions samples/net/socket_echo/prj_qemu_x86.conf
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
3 changes: 3 additions & 0 deletions samples/net/socket_echo/src/Makefile
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
76 changes: 76 additions & 0 deletions samples/net/socket_echo/src/socket_echo.c
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);
}
}
1 change: 1 addition & 0 deletions subsys/net/lib/Kbuild
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
obj-$(CONFIG_NET_SOCKETS) += sockets/
obj-$(CONFIG_ZOAP) += zoap/
obj-$(CONFIG_DNS_RESOLVER) += dns/
obj-$(CONFIG_MQTT_LIB) += mqtt/
Expand Down
2 changes: 2 additions & 0 deletions subsys/net/lib/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

menu "Network Protocols"

source "subsys/net/lib/sockets/Kconfig"
Copy link
Member

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.


source "subsys/net/lib/zoap/Kconfig"

source "subsys/net/lib/dns/Kconfig"
Expand Down
4 changes: 4 additions & 0 deletions subsys/net/lib/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
ifdef CONFIG_NET_SOCKETS
include $(srctree)/subsys/net/lib/sockets/Makefile
endif

ifdef CONFIG_ZOAP
include $(srctree)/subsys/net/lib/zoap/Makefile
endif
Expand Down
35 changes: 35 additions & 0 deletions subsys/net/lib/sockets/Kconfig
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
3 changes: 3 additions & 0 deletions subsys/net/lib/sockets/Makefile
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
Loading