Commit graph

554 commits

Author SHA1 Message Date
Paul Sokolovsky eedb8a7bd8 net: sockets: Make poll() call threadsafe by avoiding global array
Initial implementation of poll() was more of a proof of concept, so
was coded with a single-thread application in mind. As we move to
sockets as the main networking API, make it possible to use poll()
from different threads.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-07-17 13:02:16 +03:00
Robert Lubos 47f908872d net: tls: Implement poll with support for mbedTLS pending data
Implement socket poll function for TLS socket. In addition to regular
poll checks, we have to check if there is some decrypted data pending on
mbedTLS.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-13 15:03:45 -04:00
Robert Lubos 07f1a1fe2c net: tls: Handle TLS socket send and recv
Implement socket recv/recvfrom and send/sendto functions.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-13 15:03:45 -04:00
Robert Lubos d08fd07f60 net: tls: Handle TLS handshake
Implement TLS handshake handling in socket connect/accept functions.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-13 15:03:45 -04:00
Robert Lubos 2d4815dd15 net: tls: Add mbedTLS logging
Add mbedTLS logging function to enable logs from mbedTLS.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-13 15:03:45 -04:00
Robert Lubos ccdc6a6bdf net: tls: Add mbedTLS entropy source
Add entropy source for mbedTLS. If no entropy driver is available, use
non-secure, software entropy source.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-13 15:03:45 -04:00
Robert Lubos a7c698d936 net: tls: Add TLS context allocation/deallocation
Add tls_context structure that stored data required by TLS socket
implementation. This structure is allocated from global pool during
socket creation and freed during socket closure.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-13 15:03:45 -04:00
Robert Lubos 00a69bf9bb net: socket: Add switch to enable TLS socket option support
Add switch to a socket layer that will enable switching socket API to
TLS secure sockets. At this point there is no secure sockets
implementation, so secure socket calls redirect to regular socket calls.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-13 15:03:45 -04:00
Ulf Magnusson 1073882998 subsys: kconfig: Remove 'default n' properties and clean up a bit
Bool symbols implicitly default to 'n'.

A 'default n' can make sense e.g. in a Kconfig.defconfig file, if you
want to override a 'default y' on the base definition of the symbol. It
isn't used like that on any of these symbols though.

Remove some 'default ""' properties on string symbols too.

Also make definitions more consistent by converting some

  config FOO
  	<type>
  	prompt "foo"

definitions to a shorter form:

  config FOO
  	<type> "foo"

This shorthand works for int/hex/string symbols too, not just for bool
symbols.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2018-07-12 23:13:22 -04:00
Stig Bjørlykke 97b83ffc2c net: sockets: Check for errors from dns_get_addr_info
When dns_get_addr_info() returns an error it does not call the
resolve callback, and thus the semaphore will not be given.

This fix will avoid a deadlock situation for various errors.

Added some small tests for getaddrinfo().

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2018-04-03 14:15:43 +03:00
Stig Bjørlykke e652b74860 net: sockets: Do not lookup DNS AAAA entries if IPv6 is disabled
Do not lookup DNS AAAA entries if IPv6 is disabled. The result can not
be used anyway because "struct sockaddr" does not have enough space for
IPv6 address in that case.

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2018-04-03 10:23:06 +03:00
Stig Bjørlykke 5f42a26488 net: sockets: Allow empty service in getaddrinfo()
Make the service parameter optional in getaddrinfo().

The application should be able to use NULL as service when only
interested in the host lookup.

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2018-03-21 10:00:17 +02:00
Stig Bjørlykke 57e7ea8793 net: sockets: Support MSG_DONTWAIT flag in zsock_sendto
Add support for MSG_DONTWAIT flag in send() and sendto(). This
aligns with the same flag used in recv() and recvfrom().

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2018-03-21 09:59:49 +02:00
Stig Bjørlykke 7c9c825138 net: sockets: Support MSG_PEEK flag in zsock_recvfrom
Add support for MSG_PEEK flag in recv and recvfrom.

This flag is needed when using non-zephyr embedded applications with
Zephyr's socket API.

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2018-03-20 15:55:04 +02:00
Stig Bjørlykke 158adc684f net: sockets: Support MSG_DONTWAIT flag in zsock_recvfrom
Add support for MSG_DONTWAIT flag in recv and recvfrom.

This flag is needed when using non-zephyr embedded applications with
Zephyr's socket API.

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2018-03-18 17:06:38 -04:00
Stig Bjørlykke b0c8cd8048 net: sockets: Refactoring zsock_recvfrom
Refactor recvfrom code for handling SOCK_DGRAM into zsock_recv_dgram
to align with SOCK_STREAM. Add flags parameter, will be used later.

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2018-03-15 15:10:04 +02:00
Paul Sokolovsky 5925112ee6 net: sockets: Preserve user_data for any net_context send() or recv()
We use ctx->user_data to hold socket flags. As each call to
net_context_recv() and net_context_send() overwrites its previous
value, we explicitly must pass the current ctx value there.

Without this, non-blocking socket was turned into blocking after
e.g. switching from receiving to sending.

Fixes: #6309

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-03-05 12:54:42 +01:00
Paul Sokolovsky 934195cc43 net: sockets: zsock_accepted_cb: Init child socket on success only
If accept callback is called with error, don't treat the context
passed to the callback as a new accepted context.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-03-05 12:54:42 +01:00
Paul Sokolovsky 9e02f7b644 net: sockets: Fix mask application in sock_set_flag()
We pass normal 1-based mask, and mask invert it before logically
AND'ing with the value. This apparently a mix-up between how the
mask was intended to be passed initially and how it was in the end.
This issue actually didn't have an effect, because currently defined
flags have mutually exclusive lifetime (when "eof" flag is set,
"non-blocking" flag value is no longer import). Anyway, that's a
bug and needs fixing.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-03-05 12:54:42 +01:00
Leandro Pereira 816c709ef2 net: sockets: Do not reference NULL pointer with empty frags on start
Assertions should only be used to check invariants.  Things that may
change value in runtime are better left to proper checks.

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2018-03-02 07:11:46 +01:00
Paul Sokolovsky ede25c12a3 net: sockets: getaddrinfo() buffer overflow, etc. fixes
The existing implementation assumed DNS resolv callback will be
called just once, but that's not always the case (apparently,
for multi-homes hosts or something). So, apply array bounds
checking (and do pointer arithmetic only after it, as the C
standard otherwise warns of "undefined behavior"). In such a case,
the port number wasn't set in each entry too, so rework how it's
done.

The issues discovered while resolving archive.ubuntu.com.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-02-12 20:17:26 -05:00
Anas Nashif 429c2a4d9d kconfig: fix help syntax and add spaces
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2017-12-13 17:43:28 -06:00
Paul Sokolovsky 21f31e90ec net: sockets: Handle EINTR return from k_poll()
In 90b471fe4, there was a change to make k_poll() return EINTR error
if it was cancelled with k_fifo_cancel_wait(). Handle this change, or
otherwise sockets EOF handling was broken.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-11-21 07:01:49 -05:00
Aska Wu 7e58c5609a net: sockets: Fix net pkt leak
If net_context_recv() returns a error, net pkt will not be released. For
example, net_context_recv() returns -EBADF because the TCP connection is
closed by the peer.

Handle the return value instead of using SET_ERRNO().

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-11-17 12:58:32 +02:00
Paul Sokolovsky 91e810f87a net: sockets: sendto: Remove adhoc handling of max pkt payload size
Now the check happens on the level of the core IP stack, in
net_pkt_append().

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-11-10 16:30:42 +02:00
Sebastian Bøe 0829ddfe9a kbuild: Removed KBuild
Signed-off-by: Sebastian Boe <sebastian.boe@nordicsemi.no>
2017-11-08 20:00:22 -05:00
Sebastian Bøe 12f8f76165 Introduce cmake-based rewrite of KBuild
Introducing CMake is an important step in a larger effort to make
Zephyr easy to use for application developers working on different
platforms with different development environment needs.

Simplified, this change retains Kconfig as-is, and replaces all
Makefiles with CMakeLists.txt. The DSL-like Make language that KBuild
offers is replaced by a set of CMake extentions. These extentions have
either provided simple one-to-one translations of KBuild features or
introduced new concepts that replace KBuild concepts.

This is a breaking change for existing test infrastructure and build
scripts that are maintained out-of-tree. But for FW itself, no porting
should be necessary.

For users that just want to continue their work with minimal
disruption the following should suffice:

Install CMake 3.8.2+

Port any out-of-tree Makefiles to CMake.

Learn the absolute minimum about the new command line interface:

$ cd samples/hello_world
$ mkdir build && cd build
$ cmake -DBOARD=nrf52_pca10040 ..

$ cd build
$ make

PR: zephyrproject-rtos#4692
docs: http://docs.zephyrproject.org/getting_started/getting_started.html

Signed-off-by: Sebastian Boe <sebastian.boe@nordicsemi.no>
2017-11-08 20:00:22 -05:00
Aska Wu 62e5f8d7a1 net: socket: Handle the value-result argument
The addrlen of accept() and recvfrom() is a value-result argument. It
should be updated to the actual size of the source address after
calling accept() and recvfrom().

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-11-06 20:59:05 +02:00
Paul Sokolovsky 6f76a19f77 net: sockets: Fix Coverity false positive
Due to parameters used, net_context_recv() call cannot fail (it just
installs a callback, no I/O performed).

Coverity-CID: 178247
Fixes: #4581

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-11-01 09:47:40 +02:00
Aska Wu 632904117c net: sockets: Fix the error zsock_send() failed on the datagram socket
net_context_sendto() returns an error if dest address is NULL.

If dest address is available, net_conext_sendto() should be used.
Otherwise, net_context_send() should be used.

Fixes #4347

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-10-17 08:10:14 -04:00
Paul Sokolovsky 82a03936a4 net: sockets: Simplify if logic in sendto(), fix warning
send()/sendto() aren't "front facing" functions, so when user calls
them, context type hopefully should be already validated by other
functions. They are also on critical path of app/network performance,
so getting rid of extra check helps a little bit too. This also
fixes a warning of "err" possibly being used non-initialized.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-10-03 16:18:11 -04:00
Aska Wu 9ec60774f8 net: sockets: Implement sendto() and recvfrom()
sendto() and recvfrom() are often used with datagram socket.

sendto() is based on net_context_sendto() and recvfrom() is based on
zsock_recv() with parsing source address from the packet header.

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-10-03 14:40:05 +03:00
Paul Sokolovsky d3c7152528 net: sockets: Implement getaddrinfo()
POSIX-compatible getaddrinfo() call, implemented on top of native
Zephyr DNS resolver.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-09-19 17:39:44 +03:00
Paul Sokolovsky c7da45f9e2 net: Enable logging in more modules if CONFIG_NET_LOG_GLOBAL defined
This is useful to enable error/warning logging across the net
codebase (less useful for debug level logging, but that's true
for CONFIG_NET_LOG_GLOBAL already).

Implementation-wise, instead of keeping adding to long list of
"select"'s in CONFIG_NET_LOG_GLOBAL and thus introduce component
inter-dependencies, add "default y if NET_LOG_GLOBAL" to
individual components' logging options.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-09-18 08:19:27 -04:00
Paul Sokolovsky 958ff51da8 net: sock: zsock_close: Explicitly cast few calls to (void).
To make Coverity happy.

Coverity-CID: 173646
Jira: ZEP-2465

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-09-01 09:31:19 -04:00
Paul Sokolovsky eb5d6fcef5 net: sockets: Be sure to initialize socket's queue on accept
When new socket context is created on accepting connection to a
listening socket, its recv_q FIFO should be initialized. Without
initialization, this worked by a chance when FIFO structure was
simple, but recent change to add dlist to it (which now needs
proper initialization) exposed this issue.

Jira: ZEP-2576

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-08-31 15:07:33 -04:00
Paul Sokolovsky d9af77bfd6 net: sockets: Consistently use "BSD Sockets compatible API" moniker
This is how it's called in the main docs, so use this same phrase in
Kconfig and samples too.

Also, added some articles to docs.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-08-17 12:02:55 -05:00
David B. Kinder 81f7c82625 doc: fix misspellings and missing EOF newlines
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-16 17:38:41 -05:00
Paul Sokolovsky c1a5573c85 net: sockets: Check return value of k_poll()
Mostly to make Coverity happy.

Coverity-CID: 173641

Jira: ZEP-2465

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-08-14 23:46:44 +03:00
Aska Wu eb8fd4aacf net: sockets: inet_pton() should return 1 on success
Add zsock_inet_pton() to convert the return value of net_addr_pton()
which returns 0 on success.

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-08-10 17:41:47 +03:00
Paul Sokolovsky f80b2ac865 net: sockets: Fix race condition with accept() call
For an accepted socket, we should set our receive callback and start
to queue packets ASAP (in the accept callback itself). Otherwise,
(if done in accept() call like before) we may miss to queue some
packets.

This issue wasn't exposed with slow SLIP and with emulated QEMU, but
easily exposed with Ethernet on a real hardware.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-08-08 12:44:28 +03:00
Jukka Rissanen 238c51f6d0 net: sockets: Comment Coverity warning about unchecked values
Coverity complains about non-checked return values here. This is false
positive as the return values do not need checking in this special
case because we are closing the socket.

Coverity-CID: 173646

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-07 21:32:03 -04:00
Paul Sokolovsky b7e3739043 net: sockets: Manage TCP receive window
As we buffer incoming packets in receive callbacks, we must decrease
receive window to avoid situation that incoming stream for one socket
uses up all buffers in the system and causes deadlock. Once user app
consumes queued data using recv() call, we increase window again.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-08-05 11:40:50 +03:00
Paul Sokolovsky 8088e14c74 net: sockets: poll: Use prefixed POLL* constants.
Default examples build with native POSIX aliases enabled, so
unprefixed refs got thru.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-08-04 17:24:22 +03:00
David B. Kinder 62a0cd246d doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.

This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).

This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".

We may not change this default role behavior, but it becomes an option
after the fixes in this patch.  In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).

Jira: ZEP-2414

Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-03 11:08:23 -04:00
Paul Sokolovsky 2c2e8b26b4 net: sockets: Implement poll() operation.
poll() allows to (efficiently) wait for available data on sockets,
and is essential operation for working with non-blocking sockets.

This is initial, very basic implementation, effectively supporting
just POLLIN operation. (POLLOUT implementation is dummy - it's
assumed that socket is always writable, as there's currently no
reasonable way to test that.)

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-07-18 21:46:06 +03:00
Paul Sokolovsky 1abb89f640 net: sockets: Implement non-blocking mode.
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-07-11 14:00:27 +03:00
Paul Sokolovsky 49eaa2b722 net: sockets: Explicitly flush conn/pkt queue on close()
If a socket is closed without reading all data from peer or accepting
all pending connection, they will be leaked. So, flush queues
explicitly.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-28 16:07:55 +03:00
Paul Sokolovsky 6055b6cd6b net: sockets: Implement recv() for DGRAM sockets
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-28 16:07:55 +03:00
Paul Sokolovsky 2b4a240859 net: sockets: Implement recv() for STREAM sockets
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-28 16:07:55 +03:00
Paul Sokolovsky 41e88ea40c net: sockets: Implement send()
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-28 16:07:55 +03:00
Paul Sokolovsky 7567962f3a net: sockets: Implement bind(), connect(), listen(), accept()
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-28 16:07:55 +03:00
Paul Sokolovsky 386c5bacd1 net: sockets: Add configurable option to provide raw POSIX API names
With CONFIG_NET_SOCKETS_POSIX_NAMES=y, "raw" POSIX names like
socket(), recv(), close() will be exposed (using macro defines).
The close() is the biggest culprit here, because in POSIX it
applies to any file descriptor, but in this implementation -
only to sockets.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-28 16:07:55 +03:00
Paul Sokolovsky 3432ff4fca net: sockets: Bootstrap Sockets API implementation
This adds Kconfig and build infrastructure and implements
zsock_socket() and zsock_close() functions.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-28 16:07:55 +03:00