Commit graph

285 commits

Author SHA1 Message Date
Felix Walter
40d685ad3e CLA: RX: Prevent infinite loop in buffer_read
Sometimes the parser does not read any data and goes into the error
state. In this case we get an infinite loop as `buffer_read` resets the
parsers but does not hand back control to the caller.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-16 11:34:14 +02:00
Felix Walter
e16ec4c579 Enable building a BPv7 decoder binary
This adds a target `data-decoder` which builds a binary to decode a
specified binary file using uD3TN's parsing logic (based on the CLA RX
subsystem) and print details about it. In this first version only the
decoding of BPv7 bundles is implemented.

This binary can be used, e.g., for fuzz testing.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-16 11:34:14 +02:00
Felix Walter
f81c96834e platform/hal_semaphore: Use defined constant instead of magic number
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:39:29 +02:00
Felix Walter
dc6f5c5887 contact_manager: Make assumption about maximum delay constant explicit
If the implementation of `hal_queue` changes at some point, we want the
conversion from `uint64_t` to `int64_t` still to be safe here.
If the condition is always true, compiler optimizations will remove it
automatically.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:39:29 +02:00
Felix Walter
1a0eca1587 platform/hal_semaphore: Reduce inline #ifdefs
This provides dedicated `hal_semaphore` functions for Apple and
non-Apple platforms.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:39:29 +02:00
Felix Walter
94bd34ea65 contact_manager: Add assertion for contact time calculation
It is important that the "next contact time" always exceeds the current
timestamp. Otherwise, an infinite loop and DoS can occur.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:39:29 +02:00
Felix Walter
6880b22b90 hal_semaphore/hal_queue: Accept 64-bit timestamps as delay
Previously the maximum waiting time was just over 24 days. In a DTN
setup we may want to wait longer, e.g., for contacts to occur. This
allows for waiting about 292 years. If the provided delay exceeds this
threshold, an infinite delay is assumed.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:39:29 +02:00
Felix Walter
7f322764e9 hal_semaphore: Fix determination of timestamp for sem_trywait
The nanoseconds field in struct timespec must not exceed 1000 million,
i.e., one second. As we add to an absolute time we might exceed this in
some cases. Thus, we need to check for it and reflect the additional
second in the seconds field of struct timespec.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:39:29 +02:00
Felix Walter
15f2748207 simple_queue: Deallocate pop and push semaphore on queue deletion
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:39:29 +02:00
Felix Walter
adb80c8938 simple_queue: Remove queueItemsWaiting
We cannot easily support this on MacOS, but we do not use it except in
tests. Thus, remove it and make the tests only run on Linux and use
`sem_getvalue` directly.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:01:34 +02:00
Felix Walter
1e8d86548c hal: Add a hal_semaphore implementation that works on MacOS
Unnamed semaphores are unsupported there, which is why we were seeing
segfaults.

See also: https://stackoverflow.com/a/27847103

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:01:34 +02:00
Felix Walter
f07262b909 Rename fallthrough macro to fallthrough_ok to prevent conflict
There is the compiler attribute `fallthrough` already. This
re-definition makes checking for it using __has_attribute impossible.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:01:34 +02:00
Felix Walter
03aaf723c4 application_agent: Conditionally enable _DARWIN_C_SOURCE on Apple
We use `MSG_DONTWAIT` in the app. agent, see #126.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:01:34 +02:00
Felix Walter
da5242cc3b CLA: TCP: Conditionally define constants not present on Mac
Some constants we use are not available when building on Mac, see #126.

- ENONET: Should not occur on these systems
- EHOSTDOWN: Should not occur on these systems
- NI_MAXHOST: Just a sensible default present on Linux, so we set our
              sensible default.
- NI_MAXSERV: Same as the above.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:01:34 +02:00
Felix Walter
6112e9669c
Merge branch 'hotfix/parser-issues'
See !108
2023-05-15 10:43:20 +02:00
Felix Walter
6b88cbc673
Merge branch 'feature/add-bundle-unit-tests'
See !102
2023-05-15 10:43:02 +02:00
Oleksandr Nazymko
691d10ede2 Fix bug in bundle.c
This fixes a small bug in `bundle.c` in `bundle_dup` function which caused an endless loop.

Signed-off-by: Oleksandr Nazymko <oleksandr.nazymko@d3tn.com>
2023-05-15 08:52:29 +02:00
Felix Walter
62cadd3719 Move the node_prepare_and_verify to router_task
This removes the `hal_time` dependency of the `routing_table`, as it now
assumes that the provided `struct node` is ordered, free of conflicts,
and has valid contact timestamps. This is ensured by invoking
`node_prepare_and_verify` beforehand, in `router_process_command`.
The tests are updated accordingly.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-12 11:59:43 +02:00
Marius Feldmann
11b0d49796 Validate contact intervals in RoutingTable
So far the contact interval check was done in the
config parser. As this is a semantical and not
syntactical check it is moved to the RoutingTable.

Modified @ 2022-12-21 by Felix Walter to adapt to new state of codebase,
adding the check to `node_prepare_and_verify` in `node.c` instead.

Closes: #47

Signed-off-by: Marius Feldmann <marius.feldmann@d3tn.com>
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-12 10:43:43 +02:00
Felix Walter
8a523d47fd
Merge branch 'feature/remove-dangerous-functions'
See !106
2023-05-12 10:20:43 +02:00
Felix Walter
b4e3e5aae9 bundle7/parser: Add note why status is set to DONE even if CRC is invalid
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-12 09:14:37 +02:00
Felix Walter
112fc969a7 bundle7/parser: Remove unnecessary string duplication
This is a remnant from our "EID manager" with which we were trying to
reduce copies of strings in memory. This module has long been removed as
it lead to only small gains while complicating everything. However, it
seems that during the removal process we introduced an unnecessary
strdup() here, which is removed by this change.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-12 09:14:37 +02:00
Felix Walter
7ea821b67a bundle7/parser: Set parsed EID to NULL on error
If there is an error the parser will try to reset the "in-progress
bundle". If an EID field is set to *some* pointer (except NULL), it will
be free()'d, leading to a double free if the `parse_eid` operation fails.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-12 09:14:37 +02:00
Felix Walter
00d5d2d050 external, hal: Remove unused crypto (HMAC, SHA2) and JSON functions
This removes all external functions currently unused in uD3TN.
We may re-introduce esp. JSON at a later point in time, but should
include updated libraries anyway.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-12 09:12:30 +02:00
Felix Walter
2eeea30285 Remove str(n)cpy from codebase
This function is dangerous: if the resulting string is longer than the
buffer, no null-termination is added to it. Also, it always fills the
remaining buffer length with zeroes.
snprintf is fast and can always be used as replacement (and sometimes
there are better substitutes).

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-12 09:11:11 +02:00
Felix Walter
fcd0fbe49c
application_agent: Fix usage of wrong variable name
Commit b07a653389 was not properly
rebased, so it still used the old variable name `time`, not `time_ms`
that was introduced in 3cd89eb6a5.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-12 08:44:22 +02:00
Felix Walter
22b4d9d7db
Merge branch 'hotfix/clang-tidy-false-positives'
See !98
2023-05-12 08:30:25 +02:00
Felix Walter
ce7900c93f
Merge branch 'feature/60-return-creation-timestamp'
See !90
2023-05-12 08:28:14 +02:00
Felix Walter
d9bd711c9a bundle6: Allow for receiving CBHE-encoded bundles
This also adds a test for it, sending bundles from within ION to uD3TN
in the "minimal forwarding test".

Fixes: #7

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-12 08:07:42 +02:00
Felix Walter
4914d9bd96 bundle_processor: Check the right EID before generating a report
Fixes: #85

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-12 08:02:27 +02:00
Felix Walter
b427c8b3c8 CLA: TX: Allow building with CLA TX rate-limiting
This allows us to specify a maximum number of bundles sent per second by
the CLA TX task.

We have to make the bundle transmission less "bursty" in case there is
no additional buffering on the lower layers. For example, if SPP data
received via the TCP socket are directly written to the onboard bus like
on the OPS-SAT.

This smoothes out waiting times by using a constant, pre-calculated
sleep in the TX task if compiled with CLA_TX_RATE_LIMIT defined.

Compile as follows:

    make posix CFLAGS="-DCLA_TX_RATE_LIMIT=<max_bundles_per_second>"

whereas `<max_bundles_per_second>` is an integer number (range of
uint32_t, but not zero).

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-12 08:00:51 +02:00
Felix Walter
424ea0e186 Remove comments to silence clang-tidy false positives
We disable the warning altogether via the proper commandline option.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-11 17:08:39 +02:00
Felix Walter
b07a653389 AAP: Return bundle creation timestamp and sequence number in bundle ID
See the added description in the AAP documentation - this adds an
internal format to the 64 bit integer value returned as "bundle ID" in
the `SENDCONFIRM` AAP message to uniquely identify the created bundle in
conformance to RFC 9171.

Closes: #60

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-11 17:03:29 +02:00
Felix Walter
6764171348 Perform all expiration time checks with milliseconds timestamps
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-11 16:59:13 +02:00
Felix Walter
6695ff0ae4 hal_time: Use integer operations to calculate timestamps
Fixes: #98

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-11 16:59:13 +02:00
Felix Walter
379f09d7af hal_time, agents: Remove the function to set the system time
This should be a function offered by the operating system, not by a
userland process like uD3TN.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-11 16:59:13 +02:00
Felix Walter
97d520db34 node: Adapt contact interval DTN timestamp unit to milliseconds
This changes the "from" and "to" fields in the contact struct to
milliseconds-based timestamps and adapts all dependent code. The contact
data format is kept second-based for backward compatibility.

The commit also introduces a new function to work with millisecond
timestamps in the remaining capacity calculation and modifies the tests
related to the latter such that they do not need to change the timestamp.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-11 16:59:13 +02:00
Felix Walter
a46fd3b53b node: Remove unused bundle count field
This was set but never read (anymore).

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-11 16:59:13 +02:00
Felix Walter
3cd89eb6a5 bundle, agents: Switch time unit to milliseconds
...to align with the general decision to make DTN timestamps
millisecond-based.

See: #53

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-11 16:59:13 +02:00
Felix Walter
d9bd61fd63 bundle: Add function to get bundle expiration time in milliseconds
This introduces a function to work with milliseconds-based timestamps
also in case of the expiration time.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-11 16:59:13 +02:00
Felix Walter
7a3816ca42 node: Add unit suffix to contact interval fields
This clearly identifies that we are working with seconds here.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-11 16:59:13 +02:00
Felix Walter
67fe84a395 node: Identify unit for bitrate in field name
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-11 16:59:13 +02:00
Felix Walter
f1c59a0024 bundle7: Silence occasional clang-tidy false positive
Every couple of tens to hundreds of builds, clang-tidy will issue a
warning of type `clang-analyzer-valist.Uninitialized` here. A `grep` on
the whole source tree shows no `va_list` being used anywhere near - the
`*printf` functions of `tinycbor` are not called by us.

Fixes: #117

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 17:25:13 +02:00
Felix Walter
0d89029dd6 bundle_processor: Properly free ADU if we cannot process the AR
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 17:25:13 +02:00
Felix Walter
aa2d3631ed init: Fix race condition in agent manager
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 17:25:13 +02:00
Felix Walter
9e08c42ed3 Enable agents to send bundles via direct function invocation
This removes the need to send them via the queue which might result in a
deadlock as described in #115. The bundle processor context is passed as
an opaque pointer that can be used for calling BP functions from agent
callbacks (which run inside the BP thread).

Fixes: #115

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 17:23:55 +02:00
Felix Walter
85f20e8b25 config_agent: Do not send command via BP queue
This may cause a deadlock which we observed sometimes in the routing
integration test in CI as one slot in the BP queue is always needed to
finish processing the Bundle in the BP (that causes `agent_forward` in
this case).

This makes the config agent directly invoke the BP function.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 17:23:55 +02:00
Felix Walter
1237eaa2db Remove heap-allocated Task_t
`hal_task_create` returned a reference to the new task allocated on the
heap via `malloc`. We commonly used the pattern to `free` this reference
from the new thread which, however, could result in race conditions and
other synchronization issues. We added a fix using a semaphore in the
application agent, but this would have been needed in other places (CLA,
...) as well. As we do not make use of the task reference anywhere
besides in the CLA for error handling, we can remove the heap-allocated
value altogether and replace it with an `enum ud3tn_result` to simplify
things and circumvent the named synchronization issues.

In the case of the CLA function `cla_link_init`, logic is introduced to
request the RX task to exit immediately in case the TX task cannot be
launched.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 17:23:55 +02:00
Felix Walter
e8b20fa236 application_agent: Guard task field with semaphore
The `task` field in the communication task config was set by the main
task _after_ the creation of the comm. task. This may lead to
synchronization issues as the comm. task needs to access the field to
de-allocate it upon termination.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 17:23:55 +02:00
Felix Walter
69e4095dcb Remove hal_random
The test was returning indeterministic errors and we did not even use
the function anymore.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 17:23:55 +02:00