Commit graph

97 commits

Author SHA1 Message Date
Felix Walter
c4b43163fb cla_sqlite, sqlite_agent: Fix memory leak of prepared statement on error
We must finalize the prepared statements and not just reset them.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2024-09-30 15:54:28 +02:00
Felix Walter
553cb23bdd Fix constParameter and constVariable warnings by cppcheck
This improves const correctness, at least for pointers (making the
pointed-to data const where possible).

Some suppressions are added for false positive warnings, such as in
`simple_queue.c` where an arithmethic operation is used on the pointer
to calculate a non-const pointer that is returned (thus, the passed
pointer should also not point to const).

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2024-09-11 10:13:21 +02:00
Felix Walter
d12f6b7214 Fix badBitmaskCheck warnings by cppcheck
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2024-09-11 10:13:10 +02:00
Felix Walter
742d0d3c5f Fix variableScope warnings by cppcheck
This reduces the scope of several variables.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2024-09-11 10:12:44 +02:00
Felix Walter
7e810fa4c4 AAPv1: Set the BDM auth. flag if remote config is allowed
This keeps AAPv1 compatible for configuring our BDMs if we set the
additional flag.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2024-06-26 13:32:23 +02:00
Felix Walter
2dcf3b35d9 aap2: Secure the control of the storage and BDMs through AAP 2.0
With the addition of the storage agent and the compat. BDM we have the
issue that two new agents accept configuration through bundles, which
cannot check that those bundles come from trustworthy sources. In the
past we restricted contact configuration messages to local clients and
performed an "EID spoofing detection" so that we could check the source
EID - if it is the same as the local node ID, we allowed the
configuration bundle to be processed. With AAPv2 and potentially more
security-relevant components (such as BDMs) appearing in the future, we
need a new mechanism.

The idea behind the implemented mechanism is to reuse the existing AAP
2.0 shared-secret authentication that is applied for BDMs themselves
also for sending configuration messages: We add the possibility to
register an AAP 2.0 RPC agent (one that sends commands *toward* uD3TN)
with the "dispatch" authorization flag. This client can then request a
special flag to be added when sending bundles. The new flag is only
added internally by uD3TN to its in-memory data structure and is
delivered to all internal agents as well as AAP 2.0 clients receiving
the marked bundles. Those agents and clients (such as the sqlite/storage
agent) can then easily check for the flag to be present and thus
determine whether the bundle comes from an authenticated and authorized
source.

Note: The `adu_flags` field for the BundleADU AAP 2.0 message is now a
`repeated` field to represent the option of multiple flags being present
(Protobuf does not support bit fields for this purpose).

Fixes: #187

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2024-06-26 10:23:17 +02:00
Felix Walter
b07407112a sqlite: Remove destination EID from compund bundle ID
It is not part of the unique bundle identifier as per RFC 9171.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2024-06-21 10:36:30 +02:00
Felix Walter
6d2f204af5 Replace integrated next-hop forwarding with AAP 2.0
This adds functionality to dispatch bundles and control the FIB to AAP
2.0 and the bundle processor, replacing the old "router" completely.
AAP 2.0 is extended slightly to enable fragmentation.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2024-06-17 13:33:32 +02:00
Felix Walter
271ff4aa4f agent_manager: Extend by FIB and BDM mgmt., simplify, make testable
- Implement management of FIB and BDM agents:
  * add administrative secret which is required for registering FIB and
    BDM agents
  * allow registering reverse-direction agents for TX/RX of ADUs when
    passing the admin secret
  * add cmdline option to pass the admin secret via an env variable
  * make administrative secret optional in debug builds
- Register agents by number, as FIB and BDM agents may not have a sink
  ID. Numbers are allocated by the agent_register function.
- Remove global state, add function to de-allocate everything.
- Add comprehensive unit tests.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2024-06-17 13:33:31 +02:00
Felix Walter
1a65b0509a agents: Fix potential memory leak if agent registration fails
This is more or less a hypothetical issue for the uD3TN daemon, as both
agents are launched along with the program. However, when using the
library functions directly, one may run into this issue if one of the
agents is already registered.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2024-06-17 13:33:31 +02:00
Felix Walter
332323bed3 Adapt license of new files to AGPL-3.0
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2024-06-03 13:45:19 +02:00
Maximilian Nitsch
f22162952f SQLiteAgent: Clean-up on termination
Frees the allocated memory and closes the database connection on exit.

Signed-off-by: Maximilian Nitsch <maximilian.nitsch@d3tn.com>
2024-06-03 13:41:58 +02:00
Maximilian Nitsch
55f762b804 SQLiteCLA: Use WAL journal mode and a busy timeout instead of retries
https://www.sqlite.org/wal.html
https://www.sqlite.org/src/doc/204dbc15a682125c/doc/wal-lock.md

Signed-off-by: Maximilian Nitsch <maximilian.nitsch@d3tn.com>
2024-06-03 13:41:58 +02:00
Maximilian Nitsch
6a0a453b2f SQLiteCLA+Agent: Retry SQL statements if BUSY or LOCKED
Signed-off-by: Maximilian Nitsch <maximilian.nitsch@d3tn.com>
2024-06-03 13:41:58 +02:00
Maximilian Nitsch
aa753c2f9a SQLiteAgent: DELETE bundles by CompoundBundleId
Depending on the selected bundle_filter, bundles can now be deleted
using either a metadata filter or a unique CompoundBundleId.

Signed-off-by: Maximilian Nitsch <maximilian.nitsch@d3tn.com>
2024-06-03 13:41:58 +02:00
Maximilian Nitsch
3225b4fbd4 SQLiteAgent: Prevent infinite blocking when pushing into the agent_queue
Add a timeout to the push operation to prevent infinite blocking when
pushing into the agent_queue.  If the push operation fails, an error is
returned.

Signed-off-by: Maximilian Nitsch <maximilian.nitsch@d3tn.com>
2024-06-03 13:41:58 +02:00
Maximilian Nitsch
b7ce5ef89b SQLiteCLA+Agent: Extend StorageCall Protobuf definition
- Introduces a CompoundBundleId to filter exactly one bundle
- Filter bundles either by their ID or selected metadata
- Transfer the entire StorageCall object to the CLA via the agent queue

Signed-off-by: Maximilian Nitsch <maximilian.nitsch@d3tn.com>
2024-06-03 13:41:58 +02:00
Maximilian Nitsch
4fa34c69b3 SQLiteCLA+Agent: Execute select statement in CLA
To eliminate potential race conditions where rows are deleted before the
CLA processes them, the select statement is moved from the agent to the
CLA and everything is wrapped in a transaction to read all or nothing.

Signed-off-by: Maximilian Nitsch <maximilian.nitsch@d3tn.com>
2024-06-03 13:41:58 +02:00
Maximilian Nitsch
1321c59c22 sqlite_agent: Add an agent to interact with the SQLiteCLA
The SQLiteAgent enables SQLiteCLA to read packages from the database and
return them to μD3TN. For this purpose, the SQLiteAgent writes commands
to the queue that SQLiteCLA is waiting for.

The SQLiteAgent itself can be controlled by protobuf messages that
describe which action is to be applied to which set of bundles.

Currently the operations "Push" and "Delete" are implemented and a
filter based on a destination EID pattern.

Signed-off-by: Maximilian Nitsch <maximilian.nitsch@d3tn.com>
2024-06-03 13:41:58 +02:00
Maximilian Nitsch
3935a15441 hal_task: Make detached threads optional
Adds a parameter to the `hal_task_create()` function to allow
non-detached threads and a second parameter to pass the thread
identifier.

Signed-off-by: Maximilian Nitsch <maximilian.nitsch@d3tn.com>
2024-01-23 15:25:59 +01:00
Felix Walter
5f5269ec1a
Merge branch 'feature/162-clean-up-hal-task'
See !150
2023-12-20 16:25:50 +01:00
Felix Walter
7fcdeab31d Remove hal_config headers
They are unnecessary now. We should do platform-specific definitions
centrally via config.mk as well.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-12-20 10:32:48 +01:00
Felix Walter
47194d21ac hal_task: Remove unused parameters
This removes the parameters for task name, priority, and stack size,
which were mostly relevant for FreeRTOS on STM32.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-12-20 10:32:48 +01:00
Felix Walter
9917bab703 hal_io: Implement log levels for LOG_ERRNO
This enables us to use log levels for `LOG_ERRNO`. The default is the
"warning" level so that we do not need to adjust all calls. The levels
for connection establishment and receiving from sockets are adjusted to
"info" such that common cases like unreachable peers do not
unnecessarily clutter the logs.

Fixes: #172

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-12-20 10:04:17 +01:00
Felix Walter
6525a453ba Adjust all logging calls to include a log level
Rules that have been applied:

- ERROR: Anything that is or may be problematic for the program to
  continue running.
- WARN: Anything abnormal but definitely not critical for program
  continuation, especially issues encountered when interacting with
  clients (e.g. AAP).
- INFO: All state changes not corresponding to the "normal" processing of
  individual data or messages/bundles, as well as further informative and
  helpful messages. Includea errors that occur during normal data
  processing.
- DEBUG: Messages to trace what happens with individual bundles and
  other fine-grained information about internal processes.

At the moment, `LOG_ERRNO` always emits messages with the "ERROR" level.
We might want to change this in the future to better differentiate WARN
and ERROR in this regard also.

Closes: #140
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-11-24 14:50:25 +01:00
Felix Walter
485a06b381 AAP, AAP2: Move common socket handling parts to dedicated module
There is some common functionality that should not be copied.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-11-09 14:33:48 +01:00
Felix Walter
267a392a36 config_agent, echo_agent: Construct agent struct before call
This improves readability and prevents a cppcheck false positive.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-11-08 15:11:52 +01:00
Felix Walter
4b5a6e2da3 bundle_processor: Pass signal and agent via struct
This prevents the need for passing unset fields as NULL and removes the
need to change the function signature everytime the structs are
extended.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-11-08 15:11:52 +01:00
Felix Walter
a8590e5e06 AppAgent: Drop unsent bundles
It was discovered during AAP 2.0 implementation that the agent does not
properly drop unsent bundles if bundles are received just before the client
terminates the connection.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-11-08 15:11:52 +01:00
Felix Walter
196c4749ef Make compile-time define checked inline static variables
This way, no part of the affected function definitions depends on the
setting of preprocessor definitions anymore. The variables are set right
at the beginning of the files and the definitions are easy to review for
possible errors.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-10-25 15:56:57 +02:00
Felix Walter
26e91ad69e Remove config.h: Move all definitions to individual headers
This moves all definitions from config.h to individual header files and
makes them configurable (i.e., does not define when already defined).

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-09-29 14:43:21 +02:00
Felix Walter
7330c79b52 style: Spacing, indentation, long lines, continuations
This harmonizes several style issues found by `checkpatch.pl` when using
strict mode.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-07-25 13:15:05 +02:00
Felix Walter
110b620386 config_parser: Fix style (order of static const)
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-07-25 13:15:05 +02:00
Felix Walter
b8d3423040 application_agent: Init all fields of task config before starting it
This makes sure the pipe is always initialized while the task is
running.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-07-14 10:36:27 +02:00
Felix Walter
f7b67ea581 application_agent: Remove MSG_DONTWAIT flag in recv() call
As the function `receive_from_socket` is only called after `poll` has
been used already to detect that there is data to be received, we do not
need to explicitly tell the OS to not wait here.

`MSG_DONTWAIT` is a Linux-specific API and not present, e.g., on BSD.
This change makes us able to compile and run uD3TN on FreeBSD.

Fixes: #131

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-16 16:09:18 +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
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
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
ce7900c93f
Merge branch 'feature/60-return-creation-timestamp'
See !90
2023-05-12 08:28:14 +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
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
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
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
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