Skip to content
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

ports: Enhance TCP Client Disconnect Notifications in TLS Handling #627

Merged
merged 4 commits into from
Jun 3, 2024

Conversation

jkralik
Copy link
Member

@jkralik jkralik commented May 21, 2024

This pull request introduces a significant update to the oc_tls.c file within the security module, focusing on the management of TCP client disconnect notifications. A new boolean parameter, notify_session_end, has been added to several function signatures including oc_tls_free_peer, oc_tls_remove_peer, and oc_tls_close_peer. This parameter allows for controlled notification of session ends, thereby enhancing the flexibility and robustness of session handling across the TLS code.

Fixes #634

Key Changes:

  • The oc_tls_free_peer function now includes an additional boolean parameter notify_session_end to decide if the session end should be notified.
  • Similar updates have been made to oc_tls_remove_peer and oc_tls_close_peer, incorporating the notify_session_end parameter to manage session notifications effectively.
  • These changes ensure that the session end notification can be managed more precisely, improving the overall stability and functionality of the TLS handling.

This update aims to provide better control over how session ends are notified, which is crucial for maintaining stable and secure communications in networked applications.

Copy link

coderabbitai bot commented May 21, 2024

Walkthrough

The recent updates primarily focus on enhancing the session management within the TLS and TCP layers, introducing a new boolean parameter notify_session_end across various functions to handle session notifications. Additionally, new functions and modifications aim to streamline session handling, improve endpoint comparisons, and refine TCP session management, including unique session IDs and atomic operations. These changes enhance security, connectivity, and overall robustness of the system.

Changes

File Path Change Summary
security/oc_tls.c Added notify_session_end parameter to multiple functions for session notifications.
api/cloud/oc_cloud.c Simplified endpoint closure logic by using oc_close_session.
api/oc_client_api.c Included additional headers, modified oc_close_session logic, and added session closure handling.
api/oc_endpoint.c Added function to compare TCP session IDs and updated endpoint comparison logic.
api/oc_session_events.c Added assertions, modified oc_tls_remove_peer call, and adjusted session event handling.
api/oc_session_events_internal.h Added new functions for session handling, moved existing functions within conditional blocks.
api/oc_tcp.c Introduced oc_tcp_get_new_session_id for generating unique session IDs.
api/oc_tcp_internal.h Added new function and constants for TCP connection management.
port/android/tcpadapter.c Modified session functions to include notify_session_end and session ID parameters.
port/esp32/adapter/src/tcpadapter.c Added notify_session_end and session ID parameters to session functions, included new headers.
port/linux/tcpsession.c Enhanced TCP session management with notify_session_end and session ID handling, added logging.
port/linux/tcpsession.h Corrected header guard typo, modified tcp_end_session function signature.
port/windows/tcpadapter.c Updated session functions to handle notify_session_end and session ID parameters.
port/unittest/connectivitytest.cpp Added namespace usage, modified session handling, and added assertions for session IDs.
port/oc_connectivity_internal.h Added new functions and constants for TCP connection and session management.
security/oc_tls_internal.h Updated oc_tls_remove_peer to include notify_session_end parameter.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant Client
    participant Server
    participant TLS
    participant TCP

    Client->>Server: Initiate Connection
    Server->>TLS: Establish TLS Session
    TLS->>Server: Session Established
    Server->>Client: Connection Established

    Client->>Server: Send Data
    Server->>TLS: Secure Data
    TLS->>TCP: Send Data
    TCP->>Server: Data Received

    Server->>Client: Acknowledge Data

    Client->>Server: End Connection
    Server->>TLS: Close TLS Session (notify_session_end)
    TLS->>TCP: End TCP Session (notify_session_end)
    TCP->>Server: Session Ended
    Server->>Client: Connection Closed
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ocf-conformance-test-tool
Copy link

🎉 Thank you for your code contribution! To guarantee the change/addition is conformant to the OCF Specification, we would like to ask you to execute OCF Conformance Testing of your change ☝️ when your work is ready to be reviewed.


ℹ️ To verify your latest change (ec7715e), label this PR with OCF Conformance Testing.

⚠️ Label is removed with every code change.

@jkralik jkralik force-pushed the jkralik/fix/closing-tcp-session branch from ec7715e to abdd667 Compare May 21, 2024 08:00
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

Outside diff range and nitpick comments (2)
port/linux/tcpsession.c (1)

303-304: Consider the impact of session end notification on system performance.

The conditional check for session end notifications (session->notify_session_end) could potentially impact system performance if frequently triggered. Consider profiling this part under high load scenarios to ensure it does not become a bottleneck.

port/windows/ipadapter.c (1)

1687-1701: The implementation of oc_connectivity_end_session_v1 with the notify_session_end parameter aligns well with the PR's objectives to manage TCP client disconnect notifications more effectively.

Consider adding documentation for the notify_session_end parameter to clarify its usage and impact.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 4dd98e5 and ec7715e.
Files selected for processing (19)
  • api/cloud/oc_cloud.c (2 hunks)
  • api/oc_client_api.c (4 hunks)
  • messaging/coap/engine.c (2 hunks)
  • messaging/coap/signal.c (2 hunks)
  • port/android/ipadapter.c (1 hunks)
  • port/android/tcpadapter.c (7 hunks)
  • port/android/tcpadapter.h (1 hunks)
  • port/esp32/adapter/src/ipadapter.c (1 hunks)
  • port/esp32/adapter/src/tcpadapter.c (7 hunks)
  • port/esp32/adapter/src/tcpadapter.h (1 hunks)
  • port/linux/ipadapter.c (1 hunks)
  • port/linux/tcpsession.c (8 hunks)
  • port/linux/tcpsession.h (1 hunks)
  • port/oc_connectivity.h (1 hunks)
  • port/windows/ipadapter.c (1 hunks)
  • port/windows/tcpadapter.c (4 hunks)
  • port/windows/tcpadapter.h (1 hunks)
  • security/oc_tls.c (1 hunks)
  • swig/swig_interfaces/oc_connectivity.i (1 hunks)
Files skipped from review due to trivial changes (1)
  • swig/swig_interfaces/oc_connectivity.i
Additional comments not posted (25)
port/windows/tcpadapter.h (1)

36-36: Change approved: The function signature for oc_tcp_end_session now correctly includes the notify_session_end parameter and returns a boolean to indicate success.

port/android/tcpadapter.h (1)

42-43: Change approved: The function signature for oc_tcp_end_session now correctly includes the notify_session_end parameter and returns a boolean to indicate success.

Verification successful

The function oc_tcp_end_session in tcpadapter.c has been correctly updated to include the notify_session_end parameter and returns a boolean. The usage in ipadapter.c also correctly includes the new parameter.

  • port/android/tcpadapter.c: The function signature and implementation match the new definition.
  • port/android/ipadapter.c: The function is called with the correct parameters.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_tcp_end_session` is correctly used across the Android platform.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type c "oc_tcp_end_session" port/android/

Length of output: 334



Script:

#!/bin/bash
# Verify the function signature and implementation in tcpadapter.c
rg --type c "oc_tcp_end_session" port/android/tcpadapter.c

# Verify the usage of the function in ipadapter.c
rg --type c "oc_tcp_end_session" port/android/ipadapter.c

Length of output: 253



Script:

#!/bin/bash
# Extract the full function signature and implementation in tcpadapter.c
rg --type c -A 5 "oc_tcp_end_session" port/android/tcpadapter.c

Length of output: 297

port/esp32/adapter/src/tcpadapter.h (1)

42-43: Change approved: The function signature for oc_tcp_end_session now correctly includes the notify_session_end parameter and returns a boolean to indicate success.

Verification successful

The function signature for oc_tcp_end_session in tcpadapter.c correctly includes the new parameter notify_session_end and matches the updated header file. Additionally, the function call in ipadapter.c uses the new parameter correctly.

  • tcpadapter.c: The function signature is correctly updated.
  • ipadapter.c: The function call includes the new parameter notify_session_end.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_tcp_end_session` is correctly used across the ESP32 platform.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type c "oc_tcp_end_session" port/esp32/

Length of output: 362



Script:

#!/bin/bash
# Description: Verify the function signature and usage of `oc_tcp_end_session` in `tcpadapter.c` and `ipadapter.c`.

# Check the function signature in `tcpadapter.c`
rg --type c "oc_tcp_end_session" port/esp32/adapter/src/tcpadapter.c

# Check the function call in `ipadapter.c`
rg --type c "oc_tcp_end_session" port/esp32/adapter/src/ipadapter.c

Length of output: 273



Script:

#!/bin/bash
# Description: Extract the full function signature of `oc_tcp_end_session` from `tcpadapter.c`.

# Extract the full function signature
rg --type c -A 3 "oc_tcp_end_session" port/esp32/adapter/src/tcpadapter.c

Length of output: 227

port/linux/tcpsession.h (1)

108-108: Change approved: The function signature for tcp_end_session now correctly includes the notify_session_end parameter and returns a boolean to indicate success.

port/oc_connectivity.h (1)

161-178: Changes approved: The function oc_connectivity_end_session is correctly marked as deprecated, and oc_connectivity_end_session_v1 is introduced with the notify_session_end parameter and a boolean return type.

Verification successful

The function oc_connectivity_end_session_v1 is correctly used across the platform with the appropriate parameters and return type.

  • port/windows/ipadapter.c
  • port/linux/ipadapter.c
  • port/esp32/adapter/src/ipadapter.c
  • port/android/ipadapter.c
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_connectivity_end_session_v1` is correctly used across the platform.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type c "oc_connectivity_end_session_v1" port/

Length of output: 898



Script:

#!/bin/bash
# Description: Verify the correct usage of `oc_connectivity_end_session_v1` in the identified files.

# Check the usage in port/windows/ipadapter.c
echo "Checking port/windows/ipadapter.c"
rg --context 5 "oc_connectivity_end_session_v1" port/windows/ipadapter.c

# Check the usage in port/linux/ipadapter.c
echo "Checking port/linux/ipadapter.c"
rg --context 5 "oc_connectivity_end_session_v1" port/linux/ipadapter.c

# Check the usage in port/esp32/adapter/src/ipadapter.c
echo "Checking port/esp32/adapter/src/ipadapter.c"
rg --context 5 "oc_connectivity_end_session_v1" port/esp32/adapter/src/ipadapter.c

# Check the usage in port/android/ipadapter.c
echo "Checking port/android/ipadapter.c"
rg --context 5 "oc_connectivity_end_session_v1" port/android/ipadapter.c

Length of output: 2252

messaging/coap/signal.c (1)

227-227: Change approved: The use of oc_close_session in handling the RELEASE_7_04 signal is appropriate and aligns with the PR objectives.

Verification successful

Let's verify the context and correctness of oc_close_session usage in messaging/coap/engine.c.


The usage of oc_close_session in both signal.c and engine.c is appropriate and aligns with the intended functionality of handling TCP client disconnect notifications.

  • messaging/coap/signal.c: Handles the RELEASE_7_04 signal.
  • messaging/coap/engine.c: Handles invalid inbound messages for TCP connections.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_close_session` is correctly used in COAP signal handling.

# Test: Search for the function usage. Expect: Only occurrences of the new usage.
rg --type c "oc_close_session" messaging/coap/

Length of output: 164



Script:

#!/bin/bash
# Description: Verify the context of `oc_close_session` usage in `messaging/coap/engine.c`.

# Extract 10 lines before and after the usage of `oc_close_session` in `engine.c` to understand its context.
rg --type c -A 10 -B 10 "oc_close_session" messaging/coap/engine.c

Length of output: 709

api/cloud/oc_cloud.c (1)

136-136: Simplified session closure logic aligns with PR objectives.

port/esp32/adapter/src/tcpadapter.c (2)

162-166: Refactored free_tcp_session to support conditional notification on session end.

Also applies to: 377-377, 383-383, 404-404, 414-414, 424-424


438-447: Updated oc_tcp_end_session to support conditional notification, enhancing control over session management.

port/android/tcpadapter.c (2)

192-196: Ensure proper session end notification logic.

The updated logic in free_tcp_session to conditionally trigger oc_session_end_event based on notify_session_end and ongoing disconnect checks is a good approach to handle session closures more gracefully.


465-474: Validate session closure in oc_tcp_end_session.

The implementation of oc_tcp_end_session correctly handles the session search and conditional closure based on the notify_session_end flag. This aligns well with the PR's objective to improve TCP client disconnect notifications.

api/oc_client_api.c (2)

21-30: Validate inclusion of new headers.

The inclusion of oc_endpoint.h, port/oc_connectivity.h, and security/oc_tls_internal.h under their respective conditional compilation flags is appropriate and aligns with the changes described in the PR summary.


Line range hint 957-970: Ensure correct session closure handling in oc_close_session.

The modifications in oc_close_session to use oc_connectivity_end_session_v1 for TCP endpoints and handle the session state appropriately are well-implemented. This change supports the PR's goal of fixing TCP client disconnect notifications effectively.

port/windows/tcpadapter.c (5)

57-57: Added notify_session_end to tcp_session struct to control session end notifications.

This change aligns with the PR's objective to enhance session management by allowing control over session end notifications.


188-189: Ensure session end events are only triggered when not already disconnecting.

This logic prevents redundant session end notifications, which is crucial for maintaining clean session states and avoiding potential race conditions or errors during session cleanup.


197-201: Introduced asynchronous session freeing with notification control.

This modification supports the asynchronous handling of session termination, allowing for more efficient resource management and improving the responsiveness of the system under load.


240-240: Default notify_session_end to true when adding a new session.

Setting this flag to true by default ensures that session end events are triggered unless explicitly suppressed, which maintains backward compatibility and ensures that existing functionalities relying on these notifications continue to work as expected.


320-329: Refactored oc_tcp_end_session to support notification control.

This change is crucial for the flexibility of session management, allowing the caller to specify whether to notify about the session end, which can be useful in scenarios where multiple sessions are ended in a batch, or where notifications are managed at a higher layer.

port/linux/tcpsession.c (1)

242-242: Verify the default value of notify_session_end during session creation.

Verification successful

The tcp_session_t structure is defined in multiple files, including port/linux/tcpsession.c, and it contains the notify_session_end member. However, the search results do not show the initialization of this member during session creation.

To verify the default value of notify_session_end during session creation, we need to inspect the initialization code for tcp_session_t instances, specifically looking for the assignment of notify_session_end.

Let's search for the initialization of notify_session_end within the context of tcp_session_t creation.


The search results indicate that the notify_session_end member of the tcp_session_t structure is explicitly initialized to true during session creation in multiple instances. This confirms that the default value of notify_session_end is set to true when a new session is created.

Here are the relevant code snippets confirming this:

  • In port/linux/tcpsession.c:

    session->notify_session_end = true;
  • In port/windows/tcpadapter.c:

    session->notify_session_end = true;
  • In port/esp32/adapter/src/tcpadapter.c:

    session->notify_session_end = true;
  • In port/android/tcpadapter.c:

    session->notify_session_end = true;

These initializations ensure that notify_session_end is set to true by default during the creation of a TCP session.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the default value of `notify_session_end` is set to true in all session creation scenarios.
# Test: Search for all occurrences of session creation and check the initialization of `notify_session_end`.
rg --type c "oc_memb_alloc" --context 5

Length of output: 67307



Script:

#!/bin/bash
# Description: Locate the definition of `tcp_session_t` and inspect the initialization of its members.
# Step 1: Find the definition of `tcp_session_t`.
rg --type c --word "typedef struct tcp_session_t" --context 10

# Step 2: Search for the initialization of `tcp_session_t` members.
rg --type c "tcp_session_t" --context 10

Length of output: 64700



Script:

#!/bin/bash
# Description: Search for the initialization of `notify_session_end` within the context of `tcp_session_t` creation.
# Step 1: Find the initialization of `notify_session_end` in `tcp_session_t` instances.
rg --type c "notify_session_end" --context 10

Length of output: 30451

messaging/coap/engine.c (2)

1104-1104: Verify the impact of oc_close_session on TCP session management.

Verification successful

The implementation of oc_close_session in api/oc_client_api.c confirms that it handles TCP session termination correctly. Specifically, it checks if the endpoint is a TCP connection and then calls oc_connectivity_end_session_v1 followed by oc_handle_session to manage the session state.

  • api/oc_client_api.c:966-972: The function ensures proper TCP session termination by invoking necessary connectivity and session handling functions.

This aligns with the intended improvement for TCP client disconnect notifications.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the impact of `oc_close_session` on TCP session management.

# Test: Search for the function `oc_close_session` usage in TCP context. Expect: Proper session termination logic.
ast-grep --lang c --pattern $'oc_close_session($_, $_) {
  $$$
}'

Length of output: 523



Script:

#!/bin/bash
# Description: Verify the impact of `oc_close_session` on TCP session management.

# Search for the definition of the `oc_close_session` function.
ast-grep --lang c --pattern 'void oc_close_session($_) {
  $$$
}'

# Search for all calls to the `oc_close_session` function.
rg 'oc_close_session\('

Length of output: 503



Script:

#!/bin/bash
# Description: Extract the implementation of the `oc_close_session` function to verify its impact on TCP session management.

# Extract the implementation of `oc_close_session` from `api/oc_client_api.c`.
ast-grep --lang c --pattern 'void oc_close_session(const oc_endpoint_t *endpoint) {
  $$$
}' api/oc_client_api.c

Length of output: 850


50-50: Ensure the new include api/oc_client_api_internal.h is necessary.

port/android/ipadapter.c (1)

1636-1646: Review the return logic in oc_connectivity_end_session_v1.

This function now includes a boolean parameter notify_session_end which affects how session ends are notified. It's crucial to ensure that the oc_tcp_end_session function, which is called here, properly handles this parameter to maintain the intended behavior across different scenarios.

port/esp32/adapter/src/ipadapter.c (2)

1620-1623: Ensure backward compatibility with the original oc_connectivity_end_session function.

This change maintains backward compatibility by wrapping the new oc_connectivity_end_session_v1 function, ensuring that existing code that uses the old function signature still operates correctly.


1624-1634: Review the implementation of oc_connectivity_end_session_v1.

This function now includes a boolean parameter notify_session_end which is passed to oc_tcp_end_session. It's crucial to ensure that this parameter is being handled correctly within oc_tcp_end_session to manage TCP session notifications effectively.

security/oc_tls.c (1)

540-546: Ensure proper handling of the disconnected state in oc_tls_free_peer.

@jkralik jkralik force-pushed the jkralik/fix/closing-tcp-session branch 5 times, most recently from 26a7ebb to 90d39f2 Compare May 21, 2024 08:55
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 4dd98e5 and 90d39f2.
Files selected for processing (25)
  • api/cloud/oc_cloud.c (1 hunks)
  • api/oc_client_api.c (4 hunks)
  • api/oc_session_events.c (1 hunks)
  • api/plgd/plgd_time.c (1 hunks)
  • messaging/coap/engine.c (1 hunks)
  • messaging/coap/signal.c (2 hunks)
  • port/android/ipadapter.c (1 hunks)
  • port/android/tcpadapter.c (7 hunks)
  • port/android/tcpadapter.h (1 hunks)
  • port/esp32/adapter/src/ipadapter.c (1 hunks)
  • port/esp32/adapter/src/tcpadapter.c (7 hunks)
  • port/esp32/adapter/src/tcpadapter.h (1 hunks)
  • port/linux/ipadapter.c (1 hunks)
  • port/linux/tcpsession.c (8 hunks)
  • port/linux/tcpsession.h (1 hunks)
  • port/oc_connectivity.h (1 hunks)
  • port/windows/ipadapter.c (1 hunks)
  • port/windows/tcpadapter.c (4 hunks)
  • port/windows/tcpadapter.h (1 hunks)
  • security/oc_tls.c (18 hunks)
  • security/oc_tls_internal.h (1 hunks)
  • security/unittest/acltest.cpp (7 hunks)
  • security/unittest/rolestest.cpp (1 hunks)
  • security/unittest/tlstest.cpp (2 hunks)
  • swig/swig_interfaces/oc_connectivity.i (1 hunks)
Files skipped from review due to trivial changes (1)
  • swig/swig_interfaces/oc_connectivity.i
Additional comments not posted (44)
port/android/tcpadapter.h (1)

42-43: The updated function signature with notify_session_end parameter aligns with the PR objectives to enhance session management.

Verification successful

The function oc_tcp_end_session appears to be correctly integrated with the new signature in the Android-specific files. However, to ensure thorough verification, let's check the function definition and usage in port/android/tcpadapter.c and port/android/ipadapter.c.


The function oc_tcp_end_session is correctly defined and used with the new signature in the Android-specific files. The integration appears consistent and complete.

  • port/android/tcpadapter.c: The function is defined with the new signature.
  • port/android/ipadapter.c: The function is used with the new signature.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_tcp_end_session` is correctly integrated and used throughout the codebase on Android.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type c $'oc_tcp_end_session'

Length of output: 934



Script:

#!/bin/bash
# Description: Verify the function definition and usage of `oc_tcp_end_session` in Android-specific files.

# Check the function definition in `port/android/tcpadapter.c`
rg --type c --context 5 $'oc_tcp_end_session' port/android/tcpadapter.c

# Check the function usage in `port/android/ipadapter.c`
rg --type c --context 5 $'oc_tcp_end_session' port/android/ipadapter.c

Length of output: 706

port/esp32/adapter/src/tcpadapter.h (1)

42-43: The updated function signature with notify_session_end parameter aligns with the PR objectives to enhance session management.

Verification successful

The updated function signature with notify_session_end parameter is consistently applied across the ESP32, Windows, and Android platforms. The integration appears correct based on the search results.

  • port/esp32/adapter/src/tcpadapter.h
  • port/esp32/adapter/src/ipadapter.c
  • port/esp32/adapter/src/tcpadapter.c
  • port/windows/tcpadapter.h
  • port/windows/tcpadapter.c
  • port/windows/ipadapter.c
  • port/android/tcpadapter.h
  • port/android/ipadapter.c
  • port/android/tcpadapter.c
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_tcp_end_session` is correctly integrated and used throughout the codebase on ESP32.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type c $'oc_tcp_end_session'

Length of output: 934

port/linux/tcpsession.h (1)

108-108: The updated function signature with notify_session_end parameter aligns with the PR objectives to enhance session management.

Verification successful

The updated function signature with notify_session_end parameter is correctly integrated in the Linux-specific files.

  • port/linux/ipadapter.c
  • port/linux/tcpsession.c
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `tcp_end_session` is correctly integrated and used throughout the codebase on Linux.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type c $'tcp_end_session'

Length of output: 1210

security/unittest/tlstest.cpp (1)

179-179: The updated function call with notify_session_end parameter in the unit tests aligns with the PR objectives to enhance session management.

Also applies to: 211-211

Verification successful

The function oc_tls_remove_peer is correctly integrated and used throughout the codebase with the new notify_session_end parameter.

  • security/oc_tls_internal.h: Function definition includes the new parameter.
  • security/unittest/tlstest.cpp: Updated function calls.
  • security/unittest/rolestest.cpp: Updated function call.
  • security/unittest/acltest.cpp: Multiple updated function calls.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_tls_remove_peer` is correctly integrated and used throughout the codebase.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type cpp $'oc_tls_remove_peer'

Length of output: 727

api/oc_session_events.c (1)

335-335: The updated function call with notify_session_end parameter in handle_session_disconnected aligns with the PR objectives to enhance session management.

messaging/coap/signal.c (1)

227-227: Proper handling of RELEASE_7_04 signal with session closure.

Verification successful

The handling of the RELEASE_7_04 signal in the codebase appears to be correctly implemented, with the session being closed appropriately when this signal is received.

  • In messaging/coap/signal.c, the oc_close_session(endpoint) function is called when the RELEASE_7_04 signal is detected, ensuring the session is closed.
  • The references in messaging/coap/coap.c are related to setting and getting options for the RELEASE_7_04 signal, which are consistent with the expected behavior.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correct handling of `RELEASE_7_04` signal across the codebase.

# Test: Search for handling of `RELEASE_7_04`. Expect: Proper session closure.
rg --type c 'RELEASE_7_04'

Length of output: 720



Script:

#!/bin/bash
# Description: Extract relevant code segments for `RELEASE_7_04` handling.

# Extract lines around `RELEASE_7_04` references in `messaging/coap/signal.c`
rg --type c --context 5 'RELEASE_7_04' messaging/coap/signal.c

# Extract lines around `RELEASE_7_04` references in `messaging/coap/coap.c`
rg --type c --context 5 'RELEASE_7_04' messaging/coap/coap.c

Length of output: 3025

security/oc_tls_internal.h (1)

163-165: Addition of notify_session_end parameter to oc_tls_remove_peer enhances session management.

Verification successful

The notify_session_end parameter in oc_tls_remove_peer is used correctly in the following contexts:

  • In api/oc_session_events.c, it is set to false for secured TCP endpoints, indicating no session end notification.
  • In api/plgd/plgd_time.c, it is set to true when an insecure peer is added, indicating a session end notification.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correct usage of the new `notify_session_end` parameter in `oc_tls_remove_peer` across the codebase.

# Test: Search for the function usage. Expect: Correct usage of the new parameter.
rg --type c 'oc_tls_remove_peer\('

Length of output: 371



Script:

#!/bin/bash
# Extract surrounding code for the function call in `api/oc_session_events.c`
rg --context 5 'oc_tls_remove_peer\(' api/oc_session_events.c

# Extract surrounding code for the function call in `api/plgd/plgd_time.c`
rg --context 5 'oc_tls_remove_peer\(' api/plgd/plgd_time.c

Length of output: 930

api/cloud/oc_cloud.c (1)

135-135: Proper handling of cloud endpoint closure with session management.

Verification successful

The function oc_cloud_close_endpoint is consistently used with proper session management across the codebase. The context around each call shows that oc_close_session is invoked appropriately.

  • api/cloud/oc_cloud.c: The function oc_cloud_close_endpoint calls oc_close_session directly.
  • api/cloud/oc_cloud_context.c: The function oc_cloud_close_endpoint is used in a context where session management is handled.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correct handling of cloud endpoint closures across the codebase.

# Test: Search for the function usage. Expect: Proper session management.
rg --type c 'oc_cloud_close_endpoint\('

Length of output: 408



Script:

#!/bin/bash
# Description: Verify the context around each call to `oc_cloud_close_endpoint` to ensure proper session management.

# Search for the function usage and display surrounding lines for context.
rg --type c --context 5 'oc_cloud_close_endpoint\('

Length of output: 2862

port/esp32/adapter/src/tcpadapter.c (1)

438-447: The new oc_tcp_end_session function correctly handles the session termination by utilizing the free_tcp_session function with the notify_session_end parameter. This is a crucial part of managing TCP sessions properly. Good job on implementing this with thread safety using mutex locks.

port/android/tcpadapter.c (1)

465-474: The new oc_tcp_end_session function correctly handles the session termination by utilizing the free_tcp_session function with the notify_session_end parameter. This is a crucial part of managing TCP sessions properly. Good job on implementing this with thread safety using mutex locks.

security/unittest/rolestest.cpp (1)

85-85: The addition of the true parameter to oc_tls_remove_peer aligns with the PR's objectives to handle session end notifications.

Verification successful

All instances of oc_tls_remove_peer include the new boolean parameter, confirming that the change has been consistently applied across the codebase.

  • security/unittest/tlstest.cpp
  • security/unittest/rolestest.cpp
  • security/unittest/acltest.cpp
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that all calls to `oc_tls_remove_peer` include the new boolean parameter.

# Test: Search for the function usage without the new parameter. Expect: No occurrences.
rg --type cpp --glob '*.*' '!oc_tls_remove_peer\([^,]*\)'

Length of output: 59



Script:

#!/bin/bash
# Description: Verify that all calls to `oc_tls_remove_peer` include the new boolean parameter.

# Test: Search for all instances of `oc_tls_remove_peer` and check if they include the boolean parameter.
rg --type cpp --pcre2 'oc_tls_remove_peer\([^,]+,[^)]*\)'

Length of output: 752

api/plgd/plgd_time.c (1)

925-925: The addition of the true parameter to oc_tls_remove_peer correctly implements the intended session end notification logic as per the PR objectives.

api/oc_client_api.c (2)

21-27: Reordered and added include statements for better modularity.


970-972: Ensure proper session closure for TCP connections.

Verification successful

The implementation of oc_handle_session in api/oc_session_events.c confirms that it properly handles the OC_SESSION_DISCONNECTED state by calling handle_session_disconnected(endpoint). This ensures proper session closure for TCP connections.

  • api/oc_session_events.c, lines 348-350:
    if (state == OC_SESSION_DISCONNECTED) {
      handle_session_disconnected(endpoint);
    }
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_handle_session` properly handles the session state change.

# Test: Search for the function usage. Expect: Proper handling of OC_SESSION_DISCONNECTED.
rg --type python $'oc_handle_session'

Length of output: 67



Script:

#!/bin/bash
# Description: Verify that `oc_handle_session` properly handles the session state change.

# Test: Search for the function usage. Expect: Proper handling of OC_SESSION_DISCONNECTED.
rg --type c 'oc_handle_session'

Length of output: 650



Script:

#!/bin/bash
# Description: Extract the implementation of `oc_handle_session` to verify proper handling of session states.

# Extract the function implementation from the relevant file.
ast-grep --lang c --pattern $'void oc_handle_session(const oc_endpoint_t *endpoint, oc_session_state_t state) {
  $$$
}' api/oc_session_events.c

Length of output: 782

port/windows/tcpadapter.c (5)

57-57: The addition of notify_session_end to the tcp_session struct is appropriate for managing session end notifications.


188-189: The conditional logic to trigger oc_session_end_event based on notify_session_end is correctly implemented.


197-201: Correct handling of notify_session_end in asynchronous session termination ensures proper session lifecycle management.


240-240: Setting notify_session_end to true by default in new sessions is a safe and sensible default.


320-329: Proper propagation of notify_session_end in oc_tcp_end_session ensures that session end notifications are handled correctly.

security/unittest/acltest.cpp (6)

326-326: Ensure the new notify_session_end parameter is correctly handled in oc_tls_remove_peer.


845-845: Ensure that the notify_session_end parameter in oc_tls_remove_peer is being used appropriately to manage session terminations.

Verification successful

The context surrounding the oc_tls_remove_peer calls shows that the notify_session_end parameter is consistently set to true across various test cases. This indicates that the intention is to notify the session end in all these scenarios. Given the consistent usage pattern, it appears that the parameter is being used appropriately to manage session terminations.

  • security/unittest/acltest.cpp: Multiple instances where oc_tls_remove_peer(&ep, true) is called after various assertions and checks.
  • security/unittest/rolestest.cpp: oc_tls_remove_peer(&peer->endpoint, true) is called within the TearDown method to clean up peers.
  • security/unittest/tlstest.cpp: oc_tls_remove_peer(&ep, true) is called after sending messages and checking event counts.

These usages suggest that the notify_session_end parameter is correctly used to ensure proper session termination notifications.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Ensure that the `notify_session_end` parameter in `oc_tls_remove_peer` is being used appropriately.

# Test: Search for the function usage. Expect: Only occurances of the new signature.
rg --type cpp $'oc_tls_remove_peer'

Length of output: 727



Script:

#!/bin/bash
# Extract surrounding lines of each `oc_tls_remove_peer` call to understand the context of `notify_session_end` parameter usage.

# Extract 5 lines before and after each occurrence of `oc_tls_remove_peer` in the relevant files.
rg --type cpp --context 5 'oc_tls_remove_peer' security/unittest/tlstest.cpp security/unittest/rolestest.cpp security/unittest/acltest.cpp

Length of output: 5860


403-403: Verify that the notify_session_end parameter is correctly propagated and handled in oc_tls_remove_peer.


737-737: Check the implementation of oc_tls_remove_peer to ensure the notify_session_end parameter is being used to control session end notifications correctly.

Verification successful

The implementation of oc_tls_remove_peer in security/oc_tls.c confirms that the notify_session_end parameter is used to control session end notifications. Specifically, it is passed to the oc_tls_free_peer function, which likely handles the session end notifications based on this parameter.

  • security/oc_tls.c: oc_tls_remove_peer(const oc_endpoint_t *endpoint, bool notify_session_end)
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Check the implementation of `oc_tls_remove_peer`.

# Test: Search for the function usage. Expect: Only occurances of the new signature.
rg --type cpp $'oc_tls_remove_peer'

Length of output: 727



Script:

#!/bin/bash
# Description: Locate and display the implementation of `oc_tls_remove_peer`.

# Search for the definition of the function in the codebase.
ast-grep --lang cpp --pattern 'void oc_tls_remove_peer(const oc_endpoint_t *endpoint, bool notify_session_end) { $$$ }'

Length of output: 121



Script:

#!/bin/bash
# Description: Search for the definition of `oc_tls_remove_peer` in the codebase.

# Search for the function name in the codebase.
rg --type cpp 'void oc_tls_remove_peer'

Length of output: 149



Script:

#!/bin/bash
# Description: Search for the implementation of `oc_tls_remove_peer` in the codebase.

# Search for the function name in the codebase.
rg --type cpp 'oc_tls_remove_peer'

Length of output: 727



Script:

#!/bin/bash
# Description: Search for the implementation of `oc_tls_remove_peer` in all files.

# Search for the function name in all files.
rg 'oc_tls_remove_peer'

Length of output: 943



Script:

#!/bin/bash
# Description: Extract and display the implementation of `oc_tls_remove_peer` from `security/oc_tls.c`.

# Extract the implementation of the function.
rg -A 20 'oc_tls_remove_peer(const oc_endpoint_t \*endpoint, bool notify_session_end)' security/oc_tls.c

Length of output: 106



Script:

#!/bin/bash
# Description: Extract and display the implementation of `oc_tls_remove_peer` from `security/oc_tls.c`.

# Extract the implementation of the function with a larger surrounding context.
rg -A 50 'oc_tls_remove_peer' security/oc_tls.c

Length of output: 1290


649-649: Confirm that the notify_session_end parameter is correctly utilized in oc_tls_remove_peer to ensure session end notifications are managed as expected.

Verification successful

The implementation of the oc_tls_remove_peer function in security/oc_tls.c confirms that the notify_session_end parameter is correctly utilized. Specifically, the parameter is passed to the oc_tls_free_peer function, which handles the session end notifications as expected.

  • security/oc_tls.c: oc_tls_remove_peer(const oc_endpoint_t *endpoint, bool notify_session_end)
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Confirm that the `notify_session_end` parameter is correctly utilized in `oc_tls_remove_peer`.

# Test: Search for the function usage. Expect: Only occurances of the new signature.
rg --type cpp $'oc_tls_remove_peer'

Length of output: 727



Script:

#!/bin/bash
# Description: Locate and display the implementation of the `oc_tls_remove_peer` function to verify the usage of the `notify_session_end` parameter.

# Search for the implementation of the `oc_tls_remove_peer` function in the codebase.
ast-grep --lang cpp --pattern $'void oc_tls_remove_peer(const oc_endpoint_t *endpoint, bool notify_session_end) {
  $$$
}'

Length of output: 123



Script:

#!/bin/bash
# Description: Search for the implementation of the `oc_tls_remove_peer` function to verify the usage of the `notify_session_end` parameter.

# Search for the function name directly in the codebase to locate its implementation.
rg --type cpp 'void oc_tls_remove_peer'

Length of output: 149



Script:

#!/bin/bash
# Description: Search for the implementation of the `oc_tls_remove_peer` function to verify the usage of the `notify_session_end` parameter.

# Search for the function name directly in the codebase without the return type to locate its implementation.
rg --type cpp 'oc_tls_remove_peer'

Length of output: 727



Script:

#!/bin/bash
# Description: Search for the implementation of the `oc_tls_remove_peer` function to verify the usage of the `notify_session_end` parameter.

# Search for the function name in header files to locate its implementation or macro definitions.
rg --type c,h 'oc_tls_remove_peer'

Length of output: 62



Script:

#!/bin/bash
# Description: Search for the implementation of the `oc_tls_remove_peer` function to verify the usage of the `notify_session_end` parameter.

# Search for the function name in `.c` files to locate its implementation.
rg --type c 'oc_tls_remove_peer'

# Search for the function name in `.h` files to locate its implementation or macro definitions.
rg --type h 'oc_tls_remove_peer'

Length of output: 508



Script:

#!/bin/bash
# Description: Display the implementation of the `oc_tls_remove_peer` function to verify the usage of the `notify_session_end` parameter.

# Extract the implementation of `oc_tls_remove_peer` from `security/oc_tls.c`.
rg --type c --context 20 'oc_tls_remove_peer(const oc_endpoint_t *endpoint, bool notify_session_end)' security/oc_tls.c

Length of output: 121



Script:

#!/bin/bash
# Description: Display the implementation of the `oc_tls_remove_peer` function to verify the usage of the `notify_session_end` parameter.

# Extract the implementation of `oc_tls_remove_peer` from `security/oc_tls.c` with an increased context range.
rg --type c --context 40 'oc_tls_remove_peer' security/oc_tls.c

Length of output: 2031


787-787: Review the usage of notify_session_end in oc_tls_remove_peer to ensure it aligns with the intended session management enhancements.

port/linux/tcpsession.c (8)

63-63: Added notify_session_end field to tcp_session_t struct.

This change aligns with the PR's objective to enhance session management by allowing the system to determine whether to notify on session end.


99-99: Added notify_session_end field to tcp_waiting_session_t struct.

This addition is consistent with the changes in tcp_session_t and supports the same functionality for sessions that are waiting to open a connection.


242-242: Initialized notify_session_end to true in add_new_session_locked.

This default setting ensures that session end notifications are enabled by default, which is a safe approach to handle session closures unless specified otherwise.


303-304: Conditionally trigger oc_session_end_event based on notify_session_end.

This modification ensures that session end events are only triggered if notify_session_end is true, providing controlled behavior based on session settings.


589-589: Set notify_session_end to true in add_new_waiting_session_locked.

Ensures that new waiting sessions are also configured to notify on session end by default, maintaining consistency across session types.


687-691: Introduced free_session_async_locked function to asynchronously free sessions with control over notification.

This function enhancement allows for asynchronous session cleanup while respecting the notify_session_end flag, which is crucial for proper session termination handling.


701-706: Introduced free_waiting_session_async_locked for asynchronous cleanup of waiting sessions.

Similar to free_session_async_locked, this function provides controlled cleanup of waiting sessions, which is essential for maintaining system stability and correct notification behavior.


716-731: Updated tcp_end_session to handle both active and waiting sessions with notification control.

This update enhances the session ending logic by incorporating the new notify_session_end parameter, allowing for flexible session termination based on the context.

messaging/coap/engine.c (1)

1103-1103: The addition of oc_close_session for handling invalid TCP messages aligns with the PR's objective to enhance TCP session management. Good inclusion.

port/linux/ipadapter.c (2)

1591-1594: The wrapper function oc_connectivity_end_session correctly simplifies the interface by always passing true for notify_session_end to oc_connectivity_end_session_v1.


1580-1589: The implementation of oc_connectivity_end_session_v1 correctly handles the TCP flag and passes the notify_session_end parameter to tcp_end_session. Ensure that tcp_end_session is updated to handle this parameter.

port/esp32/adapter/src/ipadapter.c (2)

1620-1623: Ensure backward compatibility with the deprecated oc_connectivity_end_session.

This change maintains backward compatibility by wrapping the new oc_connectivity_end_session_v1 function, ensuring that existing code that uses the old function signature still works correctly.


1624-1634: Review the implementation of oc_connectivity_end_session_v1.

The function now takes an additional boolean parameter notify_session_end which is used to determine whether to notify about the session end. This is a crucial part of the PR's objective to improve TCP client disconnect notifications. The implementation correctly handles the TCP flag and delegates to oc_tcp_end_session if applicable.

port/windows/ipadapter.c (2)

1687-1689: The update to always notify on session end when calling oc_connectivity_end_session aligns with the PR's objectives to enhance session management.


1691-1701: The refactoring of oc_connectivity_end_session_v1 to include session end notification control is well-implemented and necessary for the enhanced TCP session management.

security/oc_tls.c (4)

Line range hint 465-546: The modifications to oc_tls_free_peer to include session end notifications are correctly implemented and align with the PR's objectives to enhance session management.


573-577: The update to oc_tls_remove_peer to support session end notifications via the notify_session_end parameter is correctly implemented.


591-591: The update to oc_tls_close_peer to support session end notifications via the notify_session_end parameter is correctly implemented.


2226-2226: The inclusion of the notify_session_end parameter in the oc_tls_free_peer call within oc_tls_shutdown ensures that session end notifications are handled during shutdown, aligning with the PR's objectives.

port/android/ipadapter.c Outdated Show resolved Hide resolved
port/android/ipadapter.c Outdated Show resolved Hide resolved
port/android/tcpadapter.c Show resolved Hide resolved
port/oc_connectivity.h Outdated Show resolved Hide resolved
port/windows/tcpadapter.h Outdated Show resolved Hide resolved
@jkralik jkralik force-pushed the jkralik/fix/closing-tcp-session branch 2 times, most recently from c0ea5d3 to 5c520c4 Compare May 21, 2024 11:02
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 90d39f2 and c0ea5d3.
Files selected for processing (26)
  • api/cloud/oc_cloud.c (1 hunks)
  • api/oc_client_api.c (4 hunks)
  • api/oc_session_events.c (1 hunks)
  • api/plgd/plgd_time.c (1 hunks)
  • apps/client_multithread_linux.c (1 hunks)
  • messaging/coap/engine.c (1 hunks)
  • messaging/coap/signal.c (2 hunks)
  • port/android/ipadapter.c (1 hunks)
  • port/android/tcpadapter.c (7 hunks)
  • port/android/tcpadapter.h (1 hunks)
  • port/esp32/adapter/src/ipadapter.c (1 hunks)
  • port/esp32/adapter/src/tcpadapter.c (7 hunks)
  • port/esp32/adapter/src/tcpadapter.h (1 hunks)
  • port/linux/ipadapter.c (1 hunks)
  • port/linux/tcpsession.c (8 hunks)
  • port/linux/tcpsession.h (1 hunks)
  • port/oc_connectivity.h (1 hunks)
  • port/windows/ipadapter.c (1 hunks)
  • port/windows/tcpadapter.c (4 hunks)
  • port/windows/tcpadapter.h (1 hunks)
  • security/oc_tls.c (18 hunks)
  • security/oc_tls_internal.h (1 hunks)
  • security/unittest/acltest.cpp (6 hunks)
  • security/unittest/rolestest.cpp (1 hunks)
  • security/unittest/tlstest.cpp (2 hunks)
  • swig/swig_interfaces/oc_connectivity.i (1 hunks)
Files skipped from review as they are similar to previous changes (25)
  • api/cloud/oc_cloud.c
  • api/oc_client_api.c
  • api/oc_session_events.c
  • api/plgd/plgd_time.c
  • messaging/coap/engine.c
  • messaging/coap/signal.c
  • port/android/ipadapter.c
  • port/android/tcpadapter.c
  • port/android/tcpadapter.h
  • port/esp32/adapter/src/ipadapter.c
  • port/esp32/adapter/src/tcpadapter.c
  • port/esp32/adapter/src/tcpadapter.h
  • port/linux/ipadapter.c
  • port/linux/tcpsession.c
  • port/linux/tcpsession.h
  • port/oc_connectivity.h
  • port/windows/ipadapter.c
  • port/windows/tcpadapter.c
  • port/windows/tcpadapter.h
  • security/oc_tls.c
  • security/oc_tls_internal.h
  • security/unittest/acltest.cpp
  • security/unittest/rolestest.cpp
  • security/unittest/tlstest.cpp
  • swig/swig_interfaces/oc_connectivity.i
Additional comments not posted (1)
apps/client_multithread_linux.c (1)

110-110: Change from oc_connectivity_end_session to oc_close_session aligns with the PR's objectives to improve session management.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between c0ea5d3 and 5c520c4.
Files selected for processing (27)
  • api/cloud/oc_cloud.c (1 hunks)
  • api/oc_client_api.c (4 hunks)
  • api/oc_session_events.c (1 hunks)
  • api/plgd/plgd_time.c (1 hunks)
  • apps/client_multithread_linux.c (1 hunks)
  • messaging/coap/engine.c (1 hunks)
  • messaging/coap/signal.c (2 hunks)
  • port/android/ipadapter.c (1 hunks)
  • port/android/tcpadapter.c (7 hunks)
  • port/android/tcpadapter.h (1 hunks)
  • port/esp32/adapter/src/ipadapter.c (1 hunks)
  • port/esp32/adapter/src/tcpadapter.c (7 hunks)
  • port/esp32/adapter/src/tcpadapter.h (1 hunks)
  • port/linux/ipadapter.c (1 hunks)
  • port/linux/tcpsession.c (8 hunks)
  • port/linux/tcpsession.h (1 hunks)
  • port/oc_connectivity.h (1 hunks)
  • port/oc_connectivity_internal.h (1 hunks)
  • port/windows/ipadapter.c (1 hunks)
  • port/windows/tcpadapter.c (4 hunks)
  • port/windows/tcpadapter.h (1 hunks)
  • security/oc_tls.c (18 hunks)
  • security/oc_tls_internal.h (1 hunks)
  • security/unittest/acltest.cpp (6 hunks)
  • security/unittest/rolestest.cpp (1 hunks)
  • security/unittest/tlstest.cpp (2 hunks)
  • swig/swig_interfaces/oc_connectivity.i (1 hunks)
Files skipped from review as they are similar to previous changes (26)
  • api/cloud/oc_cloud.c
  • api/oc_client_api.c
  • api/oc_session_events.c
  • api/plgd/plgd_time.c
  • apps/client_multithread_linux.c
  • messaging/coap/engine.c
  • messaging/coap/signal.c
  • port/android/ipadapter.c
  • port/android/tcpadapter.c
  • port/android/tcpadapter.h
  • port/esp32/adapter/src/ipadapter.c
  • port/esp32/adapter/src/tcpadapter.c
  • port/esp32/adapter/src/tcpadapter.h
  • port/linux/ipadapter.c
  • port/linux/tcpsession.c
  • port/linux/tcpsession.h
  • port/oc_connectivity.h
  • port/windows/ipadapter.c
  • port/windows/tcpadapter.c
  • port/windows/tcpadapter.h
  • security/oc_tls.c
  • security/oc_tls_internal.h
  • security/unittest/acltest.cpp
  • security/unittest/rolestest.cpp
  • security/unittest/tlstest.cpp
  • swig/swig_interfaces/oc_connectivity.i
Additional comments not posted (1)
port/oc_connectivity_internal.h (1)

101-110: The implementation of oc_connectivity_end_session_v1 aligns well with the PR's objectives to enhance session management. The addition of the notify_session_end parameter is a thoughtful inclusion for better control over session notifications.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
api/oc_tcp.c (1)

38-42: The implementation of oc_tcp_get_new_session_id correctly uses atomic operations to ensure thread safety. Consider adding a comment explaining the handling of the wrap-around case to zero, which ensures a non-zero session ID is always returned.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 5c520c4 and 446fdaf.
Files selected for processing (9)
  • api/oc_endpoint.c (2 hunks)
  • api/oc_tcp.c (1 hunks)
  • api/oc_tcp_internal.h (2 hunks)
  • include/oc_endpoint.h (1 hunks)
  • port/android/tcpadapter.c (8 hunks)
  • port/esp32/adapter/src/tcpadapter.c (8 hunks)
  • port/linux/tcpsession.c (10 hunks)
  • port/windows/tcpadapter.c (4 hunks)
  • security/oc_tls.c (19 hunks)
Files skipped from review as they are similar to previous changes (4)
  • port/android/tcpadapter.c
  • port/esp32/adapter/src/tcpadapter.c
  • port/windows/tcpadapter.c
  • security/oc_tls.c
Additional comments not posted (12)
api/oc_tcp_internal.h (1)

39-39: The declaration of oc_tcp_get_new_session_id is correctly added to support unique session ID generation for TCP sessions.

include/oc_endpoint.h (1)

110-111: The addition of the session_id field to the oc_endpoint_t structure is correctly implemented to support pairing TLS peers with TCP sessions. The comment clearly explains the meaning of a session ID of 0.

api/oc_endpoint.c (1)

679-687: The implementation of oc_endpoint_compare_session_ids correctly handles session ID comparison, including the case where a session ID of 0 acts as a wildcard. This supports flexible session management.

port/linux/tcpsession.c (9)

63-63: Added notify_session_end field to tcp_session_t struct.

This change aligns with the PR's objective to enhance session management by allowing control over session end notifications.


99-99: Added notify_session_end field to tcp_waiting_session_t struct.

This addition is consistent with the changes in tcp_session_t and supports the same functionality for waiting sessions.


257-260: Initialization of notify_session_end and session_id in add_new_session_locked.

Proper initialization of new fields ensures that sessions handle end notifications correctly and have unique session IDs.


321-322: Conditionally triggering session end events based on notify_session_end.

This logic ensures that session end notifications are only sent when appropriate, which is crucial for correct session management.


333-334: Logging session details when freeing a session.

Enhanced logging provides better visibility into session management, which is useful for debugging and monitoring.


608-608: Initialization of notify_session_end in add_new_waiting_session_locked.

Consistent with other session-related changes, ensuring that waiting sessions also respect the notification settings.


710-710: Setting notify_session_end during asynchronous session freeing.

This change allows dynamic control over notification behavior during session cleanup, which can be important for resource management.


725-725: Setting notify_session_end for waiting sessions during asynchronous cleanup.

Ensures that the notification settings are respected even during the cleanup of waiting sessions.


735-755: Updated tcp_end_session to handle session end notifications.

The function now correctly handles both active and waiting sessions, ensuring that notifications are managed according to the new notify_session_end parameter.

@jkralik jkralik force-pushed the jkralik/fix/closing-tcp-session branch from 446fdaf to 9789aed Compare May 21, 2024 15:16
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 446fdaf and 9789aed.
Files selected for processing (19)
  • api/oc_client_api.c (4 hunks)
  • api/oc_endpoint.c (2 hunks)
  • api/oc_tcp.c (1 hunks)
  • api/oc_tcp_internal.h (2 hunks)
  • include/oc_endpoint.h (1 hunks)
  • port/android/ipadapter.c (1 hunks)
  • port/android/tcpadapter.c (8 hunks)
  • port/android/tcpadapter.h (1 hunks)
  • port/esp32/adapter/src/ipadapter.c (1 hunks)
  • port/esp32/adapter/src/tcpadapter.c (8 hunks)
  • port/esp32/adapter/src/tcpadapter.h (1 hunks)
  • port/linux/ipadapter.c (1 hunks)
  • port/linux/tcpsession.c (10 hunks)
  • port/linux/tcpsession.h (1 hunks)
  • port/oc_connectivity_internal.h (1 hunks)
  • port/windows/ipadapter.c (1 hunks)
  • port/windows/tcpadapter.c (4 hunks)
  • port/windows/tcpadapter.h (1 hunks)
  • security/oc_tls.c (20 hunks)
Files skipped from review due to trivial changes (1)
  • port/esp32/adapter/src/ipadapter.c
Files skipped from review as they are similar to previous changes (16)
  • api/oc_client_api.c
  • api/oc_endpoint.c
  • api/oc_tcp.c
  • api/oc_tcp_internal.h
  • include/oc_endpoint.h
  • port/android/ipadapter.c
  • port/android/tcpadapter.c
  • port/esp32/adapter/src/tcpadapter.c
  • port/esp32/adapter/src/tcpadapter.h
  • port/linux/ipadapter.c
  • port/linux/tcpsession.h
  • port/oc_connectivity_internal.h
  • port/windows/ipadapter.c
  • port/windows/tcpadapter.c
  • port/windows/tcpadapter.h
  • security/oc_tls.c
Additional comments not posted (2)
port/android/tcpadapter.h (1)

42-44: The addition of the notify_session_end parameter to oc_tcp_end_session enhances control over session notifications. Ensure all calls to this function are updated to pass this new parameter.

Verification successful

The verification confirms that the function declarations and calls to oc_tcp_end_session across various files include the new notify_session_end parameter. This ensures consistency and proper integration of the new parameter.

  • port/windows/tcpadapter.h
  • port/windows/ipadapter.c
  • port/esp32/adapter/src/tcpadapter.h
  • port/esp32/adapter/src/tcpadapter.c
  • port/esp32/adapter/src/ipadapter.c
  • port/windows/tcpadapter.c
  • port/android/tcpadapter.h
  • port/android/tcpadapter.c
  • port/android/ipadapter.c
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `oc_tcp_end_session` include the new parameter.

# Test: Search for the function usage. Expect: Only occurances with the new parameter.
rg --type c $'oc_tcp_end_session'

Length of output: 949



Script:

#!/bin/bash
# Description: Verify function declarations of `oc_tcp_end_session` include the new parameter.

# Test: Search for the function declarations. Expect: Declarations with the new parameter.
rg --type c --glob '*tcpadapter.h' --glob '*tcpadapter.c' --glob '*ipadapter.c' $'oc_tcp_end_session'

Length of output: 1017

port/linux/tcpsession.c (1)

63-63: The integration of the notify_session_end parameter across various functions and structures in tcpsession.c is consistent and aligns with the PR's objectives to manage TCP client disconnect notifications more effectively. Ensure that the logic surrounding this parameter's usage correctly handles all edge cases, especially in asynchronous session management.

Also applies to: 99-99, 257-260, 321-322, 706-710, 720-725, 735-762

Verification successful

The integration of the notify_session_end parameter across various functions and structures in tcpsession.c and other related files is consistent and aligns with the PR's objectives to manage TCP client disconnect notifications more effectively. The parameter is properly checked in conditionals before usage, ensuring correct handling in session management.

  • port/linux/tcpsession.c: Proper checks before usage of notify_session_end.
  • port/windows/tcpadapter.c: Proper checks before usage of notify_session_end.
  • port/esp32/adapter/src/tcpadapter.c: Proper checks before usage of notify_session_end.
  • port/android/tcpadapter.c: Proper checks before usage of notify_session_end.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correct handling of `notify_session_end` across session management functions.

# Test: Search for usage of `notify_session_end` in conditionals. Expect: Proper checks before usage.
rg --type c $'notify_session_end' | grep 'if'

Length of output: 4006

@jkralik jkralik force-pushed the jkralik/fix/closing-tcp-session branch from 9789aed to 71b84a7 Compare May 21, 2024 15:38
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 9789aed and 71b84a7.
Files selected for processing (19)
  • api/oc_client_api.c (4 hunks)
  • api/oc_endpoint.c (2 hunks)
  • api/oc_tcp.c (1 hunks)
  • api/oc_tcp_internal.h (2 hunks)
  • include/oc_endpoint.h (1 hunks)
  • port/android/ipadapter.c (1 hunks)
  • port/android/tcpadapter.c (8 hunks)
  • port/android/tcpadapter.h (1 hunks)
  • port/esp32/adapter/src/ipadapter.c (1 hunks)
  • port/esp32/adapter/src/tcpadapter.c (8 hunks)
  • port/esp32/adapter/src/tcpadapter.h (1 hunks)
  • port/linux/ipadapter.c (1 hunks)
  • port/linux/tcpsession.c (10 hunks)
  • port/linux/tcpsession.h (1 hunks)
  • port/oc_connectivity_internal.h (1 hunks)
  • port/windows/ipadapter.c (1 hunks)
  • port/windows/tcpadapter.c (4 hunks)
  • port/windows/tcpadapter.h (1 hunks)
  • security/oc_tls.c (20 hunks)
Files skipped from review due to trivial changes (1)
  • port/esp32/adapter/src/ipadapter.c
Files skipped from review as they are similar to previous changes (17)
  • api/oc_client_api.c
  • api/oc_endpoint.c
  • api/oc_tcp.c
  • api/oc_tcp_internal.h
  • include/oc_endpoint.h
  • port/android/ipadapter.c
  • port/android/tcpadapter.c
  • port/android/tcpadapter.h
  • port/esp32/adapter/src/tcpadapter.c
  • port/esp32/adapter/src/tcpadapter.h
  • port/linux/ipadapter.c
  • port/linux/tcpsession.h
  • port/oc_connectivity_internal.h
  • port/windows/ipadapter.c
  • port/windows/tcpadapter.c
  • port/windows/tcpadapter.h
  • security/oc_tls.c
Additional comments not posted (1)
port/linux/tcpsession.c (1)

257-260: Validate the session ID generation logic.

This script checks the usage of oc_tcp_get_new_session_id to ensure it's being used correctly to generate unique session IDs.

port/linux/tcpsession.c Show resolved Hide resolved
port/linux/tcpsession.c Show resolved Hide resolved
port/linux/tcpsession.c Show resolved Hide resolved
port/linux/tcpsession.c Show resolved Hide resolved
@jkralik jkralik force-pushed the jkralik/fix/closing-tcp-session branch from 71b84a7 to 7f0b328 Compare May 21, 2024 19:30
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 71b84a7 and 7f0b328.
Files selected for processing (19)
  • api/oc_client_api.c (4 hunks)
  • api/oc_endpoint.c (2 hunks)
  • api/oc_tcp.c (1 hunks)
  • api/oc_tcp_internal.h (2 hunks)
  • include/oc_endpoint.h (1 hunks)
  • port/android/ipadapter.c (1 hunks)
  • port/android/tcpadapter.c (8 hunks)
  • port/android/tcpadapter.h (1 hunks)
  • port/esp32/adapter/src/ipadapter.c (1 hunks)
  • port/esp32/adapter/src/tcpadapter.c (8 hunks)
  • port/esp32/adapter/src/tcpadapter.h (1 hunks)
  • port/linux/ipadapter.c (1 hunks)
  • port/linux/tcpsession.c (10 hunks)
  • port/linux/tcpsession.h (1 hunks)
  • port/oc_connectivity_internal.h (1 hunks)
  • port/windows/ipadapter.c (1 hunks)
  • port/windows/tcpadapter.c (4 hunks)
  • port/windows/tcpadapter.h (1 hunks)
  • security/oc_tls.c (21 hunks)
Files skipped from review as they are similar to previous changes (18)
  • api/oc_client_api.c
  • api/oc_endpoint.c
  • api/oc_tcp.c
  • api/oc_tcp_internal.h
  • include/oc_endpoint.h
  • port/android/ipadapter.c
  • port/android/tcpadapter.c
  • port/android/tcpadapter.h
  • port/esp32/adapter/src/ipadapter.c
  • port/esp32/adapter/src/tcpadapter.c
  • port/esp32/adapter/src/tcpadapter.h
  • port/linux/ipadapter.c
  • port/linux/tcpsession.h
  • port/oc_connectivity_internal.h
  • port/windows/ipadapter.c
  • port/windows/tcpadapter.c
  • port/windows/tcpadapter.h
  • security/oc_tls.c
Additional comments not posted (6)
port/linux/tcpsession.c (6)

257-260: Initialization of notify_session_end to true in add_new_session_locked function is crucial for ensuring that session ends are notified by default. This aligns with the PR's objective to manage session end notifications effectively.


321-322: The condition for session end notification is well implemented. It checks if the disconnect is not ongoing and if the session end should be notified, which is a robust way to handle conditional notifications.


608-608: The default setting of notify_session_end to true in add_new_waiting_session_locked ensures that new waiting sessions are also prepared to handle end notifications. This consistency across session types is good for maintainability.


706-710: The asynchronous session freeing logic in free_session_async_locked correctly modifies the notify_session_end field. This allows dynamic control over notification behavior during session cleanup.


720-725: Similar to free_session_async_locked, the free_waiting_session_async_locked function handles the notify_session_end flag appropriately, ensuring that waiting sessions are also cleaned up according to the notification settings.


735-762: The tcp_end_session function effectively uses the notify_session_end parameter to decide whether to notify the end of a session. This function is a key part of the PR's objective to manage disconnect notifications correctly.

port/linux/tcpsession.c Show resolved Hide resolved
port/linux/tcpsession.c Show resolved Hide resolved
@jkralik jkralik added the OCF Conformance Testing OCF Conformance Testing required label May 22, 2024
@jkralik
Copy link
Member Author

jkralik commented May 22, 2024

@coderabbitai update description for this pull request based on changes

Copy link

coderabbitai bot commented May 22, 2024

Tip

For best results, initiate chat on the files or code changes.

@jkralik, based on the changes made in the PR, here's an updated description that you can use:


Title: Enhance TCP Client Disconnect Notifications in TLS Handling

Description:
This pull request introduces a significant update to the oc_tls.c file within the security module, focusing on the management of TCP client disconnect notifications. A new boolean parameter, notify_session_end, has been added to several function signatures including oc_tls_free_peer, oc_tls_remove_peer, and oc_tls_close_peer. This parameter allows for controlled notification of session ends, thereby enhancing the flexibility and robustness of session handling across the TLS code.

Key Changes:

  • The oc_tls_free_peer function now includes an additional boolean parameter notify_session_end to decide if the session end should be notified.
  • Similar updates have been made to oc_tls_remove_peer and oc_tls_close_peer, incorporating the notify_session_end parameter to manage session notifications effectively.
  • These changes ensure that the session end notification can be managed more precisely, improving the overall stability and functionality of the TLS handling.

This update aims to provide better control over how session ends are notified, which is crucial for maintaining stable and secure communications in networked applications.


Feel free to adjust the wording as you see fit!

@jkralik jkralik changed the title ports: fix TCP client disconnect notifications ports: Enhance TCP Client Disconnect Notifications in TLS Handling May 22, 2024
@Danielius1922 Danielius1922 force-pushed the jkralik/fix/closing-tcp-session branch from 7f0b328 to ff02ba0 Compare May 31, 2024 19:40
@ocf-conformance-test-tool ocf-conformance-test-tool bot removed the OCF Conformance Testing OCF Conformance Testing required label May 31, 2024
@Danielius1922 Danielius1922 marked this pull request as ready for review May 31, 2024 19:40
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 9

Outside diff range and nitpick comments (1)
port/android/tcpadapter.c (1)

Line range hint 217-242: Refactor to improve session ID assignment logic.

The session ID assignment can be simplified by using a ternary operator, reducing the number of lines and improving readability.

-  if (session_id == 0) {
-    session->endpoint.session_id = oc_tcp_get_new_session_id();
-  } else {
-    session->endpoint.session_id = session_id;
-  }
+  session->endpoint.session_id = (session_id == 0) ? oc_tcp_get_new_session_id() : session_id;
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7f0b328 and ff02ba0.

Files selected for processing (32)
  • api/cloud/oc_cloud.c (1 hunks)
  • api/oc_client_api.c (3 hunks)
  • api/oc_endpoint.c (2 hunks)
  • api/oc_session_events.c (3 hunks)
  • api/oc_session_events_internal.h (4 hunks)
  • api/oc_tcp.c (1 hunks)
  • api/oc_tcp_internal.h (2 hunks)
  • api/plgd/plgd_time.c (1 hunks)
  • apps/client_multithread_linux.c (1 hunks)
  • include/oc_endpoint.h (1 hunks)
  • messaging/coap/engine.c (1 hunks)
  • messaging/coap/signal.c (2 hunks)
  • port/android/ipadapter.c (1 hunks)
  • port/android/tcpadapter.c (13 hunks)
  • port/android/tcpadapter.h (1 hunks)
  • port/esp32/adapter/src/ipadapter.c (1 hunks)
  • port/esp32/adapter/src/tcpadapter.c (13 hunks)
  • port/esp32/adapter/src/tcpadapter.h (1 hunks)
  • port/linux/ipadapter.c (1 hunks)
  • port/linux/tcpsession.c (16 hunks)
  • port/linux/tcpsession.h (4 hunks)
  • port/oc_connectivity.h (1 hunks)
  • port/oc_connectivity_internal.h (3 hunks)
  • port/windows/ipadapter.c (1 hunks)
  • port/windows/tcpadapter.c (9 hunks)
  • port/windows/tcpadapter.h (1 hunks)
  • security/oc_tls.c (20 hunks)
  • security/oc_tls_internal.h (1 hunks)
  • security/unittest/acltest.cpp (6 hunks)
  • security/unittest/rolestest.cpp (1 hunks)
  • security/unittest/tlstest.cpp (2 hunks)
  • swig/swig_interfaces/oc_connectivity.i (1 hunks)
Files skipped from review due to trivial changes (1)
  • swig/swig_interfaces/oc_connectivity.i
Additional comments not posted (66)
port/esp32/adapter/src/tcpadapter.h (1)

42-44: The updated function signature for oc_tcp_end_session on the ESP32 platform includes a dev parameter for device-specific session management, enhancing the flexibility of TCP session handling.

Verification successful

The function oc_tcp_end_session has different signatures across various platforms, indicating platform-specific implementations. The ESP32 platform's signature includes an additional ip_context_t *dev parameter, which is not present in the Windows implementation.

To ensure the changes are correctly integrated and do not affect other platforms, we need to verify the usage and integration of this function specifically for the ESP32 platform.


The function oc_tcp_end_session with the new signature is consistently used across the ESP32-specific files. This confirms that the changes are correctly integrated for the ESP32 platform without affecting other platforms.

  • port/esp32/adapter/src/tcpadapter.h
  • port/esp32/adapter/src/tcpadapter.c
  • port/esp32/adapter/src/ipadapter.c

The integration appears to be correct and consistent within the ESP32-specific code.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_tcp_end_session` is correctly integrated and used throughout the codebase on the ESP32 platform.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type c $'oc_tcp_end_session'

Length of output: 949



Script:

#!/bin/bash
# Description: Verify the usage of `oc_tcp_end_session` specifically for the ESP32 platform.

# Test: Search for the function usage in ESP32-specific files. Expect: Only occurrences of the new signature.
rg --type c $'oc_tcp_end_session' port/esp32/adapter/src/

Length of output: 373

api/oc_tcp_internal.h (2)

29-29: Including <stdint.h> is necessary for using standard integer types like uint32_t, which is used in the new session ID function.


39-39: The new function oc_tcp_get_new_session_id is a crucial addition for generating unique session IDs, enhancing the management of TCP sessions.

Verification successful

The function oc_tcp_get_new_session_id is correctly integrated and used throughout the codebase. Here are the locations where it is used:

  • security/oc_tls.c
  • port/windows/tcpadapter.c
  • port/linux/tcpsession.c
  • port/esp32/adapter/src/tcpadapter.c
  • port/android/tcpadapter.c
  • api/oc_tcp_internal.h
  • api/oc_tcp.c

The function is being utilized to assign new session IDs, which aligns with its intended purpose.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_tcp_get_new_session_id` is correctly integrated and used throughout the codebase.

# Test: Search for the function usage. Expect: Only occurrences of the new function.
rg --type c $'oc_tcp_get_new_session_id'

Length of output: 678

api/oc_session_events_internal.h (2)

25-25: Including "util/oc_features.h" is necessary for accessing feature flags, which are likely used in the session events API.


37-37: The documentation for oc_handle_session has been clarified to better describe its functionality, which is crucial for managing session events.

Verification successful

The oc_handle_session function is used in several files, indicating its importance in managing session events. To ensure the documentation is clear and the function is correctly integrated, we need to review the context of each usage.


The context provided for the oc_handle_session function usage in various files shows that it is correctly integrated and used to manage session events. The documentation update appears to be clear and accurately describes its functionality.

  • security/oc_tls.c: The function is used to handle session disconnection and connection events.
  • api/oc_session_events.c: The function is used within loops to handle session start and end events.
  • api/oc_client_api.c: The function is used to handle session disconnection for TCP endpoints.

The documentation for oc_handle_session has been clarified to better describe its functionality, which is crucial for managing session events.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_handle_session` is correctly integrated and used throughout the codebase.

# Test: Search for the function usage. Expect: Only occurrences of the updated documentation.
rg --type c $'oc_handle_session'

Length of output: 659



Script:

#!/bin/bash
# Description: Retrieve the context of each usage of `oc_handle_session` to verify correct integration and documentation clarity.

# Get the context of usage in security/oc_tls.c
echo "Context in security/oc_tls.c:"
rg --context 5 'oc_handle_session' security/oc_tls.c

# Get the context of usage in api/oc_session_events.c
echo "Context in api/oc_session_events.c:"
rg --context 5 'oc_handle_session' api/oc_session_events.c

# Get the context of usage in api/oc_client_api.c
echo "Context in api/oc_client_api.c:"
rg --context 5 'oc_handle_session' api/oc_client_api.c

Length of output: 2408

port/oc_connectivity_internal.h (2)

28-28: Including "util/oc_compiler.h" is necessary for using compiler-specific directives, which are likely used in the connectivity API.


99-102: The updated function oc_connectivity_end_session_v1 enhances session management by including a session_endpoint parameter, aligning with the PR's focus on enhancing session management.

Verification successful

The function oc_connectivity_end_session_v1 is correctly integrated and used throughout the codebase with the new signature.

  • security/oc_tls.c
  • port/windows/ipadapter.c
  • port/esp32/adapter/src/ipadapter.c
  • port/linux/ipadapter.c
  • port/android/ipadapter.c
  • api/oc_client_api.c
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_connectivity_end_session_v1` is correctly integrated and used throughout the codebase.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type c $'oc_connectivity_end_session_v1'

Length of output: 1046

port/linux/tcpsession.h (1)

110-111: The updated tcp_end_session function now supports controlled session end notifications and specific session endpoint handling. Ensure that all calls to this function across the system are updated to pass the new parameters.

Verification successful

The search results indicate that the function tcp_end_session has been updated in the Linux platform files. However, there are similar functions (oc_tcp_end_session) in other platform-specific files (Windows, Android, ESP32) that need to be checked for consistency with the new function signature.

Let's verify the function signatures in these files to ensure they match the new signature.


The absence of output suggests that the function signatures in the platform-specific files have not been updated to match the new signature. Let's extract the function definitions from the relevant files for manual verification.


The function definitions for oc_tcp_end_session in the Windows, Android, and ESP32 platform files match the new signature. This confirms that the updates have been correctly applied across the platform-specific files.

  • port/windows/tcpadapter.h
  • port/android/tcpadapter.h
  • port/esp32/adapter/src/tcpadapter.h
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `tcp_end_session` match the new signature.

# Test: Search for the function usage. Expect: Only occurances of the new signature.
rg --type c $'tcp_end_session'

Length of output: 1242



Script:

#!/bin/bash
# Description: Verify the function signatures of `oc_tcp_end_session` in platform-specific files.

# Test: Search for the function definitions. Expect: All definitions should match the new signature.
rg --type c --files-with-matches $'bool oc_tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end, oc_endpoint_t *session_endpoint)'

Length of output: 149



Script:

#!/bin/bash
# Description: Extract the function definitions of `oc_tcp_end_session` in platform-specific files for manual verification.

# Extract function definitions from Windows platform files
rg --type c --context 5 $'bool oc_tcp_end_session' port/windows/tcpadapter.h port/windows/tcpadapter.c port/windows/ipadapter.c

# Extract function definitions from Android platform files
rg --type c --context 5 $'bool oc_tcp_end_session' port/android/tcpadapter.h port/android/tcpadapter.c port/android/ipadapter.c

# Extract function definitions from ESP32 platform files
rg --type c --context 5 $'bool oc_tcp_end_session' port/esp32/adapter/src/tcpadapter.h port/esp32/adapter/src/tcpadapter.c port/esp32/adapter/src/ipadapter.c

Length of output: 2603

api/oc_tcp.c (1)

37-42: The implementation of oc_tcp_get_new_session_id correctly uses atomic operations to ensure thread safety and uniqueness of session IDs. Good handling of the session ID wrap-around case.

include/oc_endpoint.h (1)

110-112: The addition of the session_id field in oc_endpoint_t is a crucial update for pairing TLS peers with TCP sessions. Ensure that all uses of oc_endpoint_t across the system properly initialize and use this new field.

Verification successful

The addition of the session_id field in oc_endpoint_t is a crucial update for pairing TLS peers with TCP sessions. We need to ensure that all uses of oc_endpoint_t across the system properly initialize and use this new field.


The session_id field in the oc_endpoint_t structure is being properly initialized and used across various files, including security/oc_tls.c, port/windows/tcpadapter.c, port/linux/tcpsession.c, port/esp32/adapter/src/tcpadapter.c, and port/android/tcpadapter.c. This confirms that the new field is being handled correctly in the codebase.

  • security/oc_tls.c: The session_id is checked and assigned values.
  • port/windows/tcpadapter.c, port/linux/tcpsession.c, port/esp32/adapter/src/tcpadapter.c, port/android/tcpadapter.c: The session_id is initialized and assigned new session IDs.

This thorough initialization and usage indicate that the addition of the session_id field is well-integrated into the system.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all uses of `oc_endpoint_t` properly handle the new `session_id` field.

# Test: Search for the structure usage. Expect: Proper initialization and usage of `session_id`.
rg --type c $'oc_endpoint_t'

Length of output: 67114



Script:

#!/bin/bash
# Description: Verify initialization of the `session_id` field in `oc_endpoint_t` structure.

# Test: Search for the initialization of `session_id` in the codebase.
rg --type c 'session_id\s*='

Length of output: 1823

api/oc_session_events.c (1)

Line range hint 136-160: The assertions in oc_session_start_event and oc_session_end_event ensure that only specific sessions (non-zero session IDs) are handled. Verify that all session-related functions across the system correctly handle session IDs.

port/oc_connectivity.h (1)

161-168: The function oc_connectivity_end_session is correctly marked as deprecated and replaced by oc_close_session. Plan to remove this deprecated function in future versions to clean up the codebase.

messaging/coap/signal.c (1)

227-227: Replace session end function to support enhanced notification control.

Verification successful

Verify the implementation of oc_close_session to ensure it supports the new notify_session_end parameter and behaves correctly.


The implementation of oc_close_session in api/oc_client_api.c shows that it handles both secured and TCP connections appropriately. The function checks the endpoint flags and calls the relevant functions to close the connection. This confirms that the function is used consistently and correctly across the codebase.

  • oc_close_session handles secured connections by calling oc_tls_close_connection.
  • For TCP connections, it uses oc_connectivity_end_session_v1 and oc_handle_session.

No issues were found with the usage of oc_close_session.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_close_session` is used consistently and correctly across all relevant files.

# Test: Search for the function usage. Expect: Only occurances with correct context and parameters.
rg --type c "oc_close_session"

Length of output: 628



Script:

#!/bin/bash
# Description: Retrieve the implementation of `oc_close_session` to verify its behavior.

# Test: Extract the function definition and body. Expect: Correct handling of `notify_session_end` parameter.
ast-grep --lang c --pattern 'void oc_close_session(const oc_endpoint_t *endpoint) { $$$ }'

Length of output: 1270

apps/client_multithread_linux.c (1)

110-110: Update session closure to enhance notification control.

Verification successful

Verify the context and parameters of oc_close_session usage.


The context and parameters of oc_close_session usage across the identified files appear consistent and correct. The function is used to close sessions in various scenarios, such as handling TCP connections, security checks, and cloud endpoint closures. The usage aligns with the function's purpose as described in the header files.

  • api/plgd/plgd_time.c: Used within a conditional block checking for TCP or security flags.
  • api/oc_client_api.c: Defined with a security check for secured endpoints.
  • apps/client_multithread_linux.c: Used to close the connection after retry attempts.
  • include/oc_api.h: Declared with a description of its parameters.
  • api/cloud/oc_cloud.c: Used to close cloud endpoints.
  • messaging/coap/engine.c: Used within a TCP conditional block.
  • messaging/coap/signal.c: Used in response to a specific CoAP signal.
  • port/oc_connectivity.h: Marked as deprecated but still referenced for backward compatibility.

Given this consistency, the change in apps/client_multithread_linux.c to use oc_close_session is appropriate and aligns with the function's intended use.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_close_session` is used consistently and correctly across all relevant files.

# Test: Search for the function usage. Expect: Only occurances with correct context and parameters.
rg --type c "oc_close_session"

Length of output: 628



Script:

#!/bin/bash
# Description: Verify the context and parameters of `oc_close_session` usage in identified files.

# Test: Extract lines around the usage of `oc_close_session` to check context and parameters.
rg --type c -A 3 -B 3 "oc_close_session" port/oc_connectivity.h messaging/coap/signal.c messaging/coap/engine.c include/oc_api.h api/cloud/oc_cloud.c api/oc_client_api.c apps/client_multithread_linux.c api/plgd/plgd_time.c

Length of output: 3068

security/oc_tls_internal.h (1)

163-165: Enhance TLS peer removal to support session end notifications.

Verification successful

Verify correct usage of oc_tls_remove_peer with the new parameter.


The usage of oc_tls_remove_peer with the new notify_session_end parameter appears consistent and correct across the relevant files:

  • In api/plgd/plgd_time.c, the function is called with true for notify_session_end.
  • In api/oc_session_events.c, the function is called with false for notify_session_end.
  • In security/oc_tls.c, the function definition includes the new parameter.

These usages align with the intended functionality of the new parameter.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_tls_remove_peer` is used consistently and correctly across all relevant files.

# Test: Search for the function usage. Expect: Only occurances with correct context and parameters.
rg --type c "oc_tls_remove_peer"

Length of output: 372



Script:

#!/bin/bash
# Description: Verify the context and correctness of `oc_tls_remove_peer` usage in relevant files.

# Extract the relevant lines around the function usage in each file.
rg --type c --context 5 "oc_tls_remove_peer" security/oc_tls.c api/oc_session_events.c api/plgd/plgd_time.c

Length of output: 1941

api/cloud/oc_cloud.c (1)

135-135: The modification to use oc_close_session directly aligns with the PR's objective to enhance session management. This should help in managing TCP client disconnects more effectively.

api/oc_endpoint.c (2)

678-688: The addition of oc_endpoint_compare_session_ids function is a good implementation to handle session ID comparisons in TCP connections. This aligns with the PR's objectives to manage session IDs effectively.


705-707: The integration of session ID comparison into oc_endpoint_compare is well-implemented. This ensures that TCP connections are compared not just by endpoint properties but also by their session IDs, enhancing the accuracy of endpoint comparisons.

Also applies to: 718-719

port/esp32/adapter/src/tcpadapter.c (6)

Line range hint 187-212: The session ID handling logic is well-implemented, ensuring that a new session ID is generated only when necessary.


256-257: Proper error handling is implemented when failing to record a new TCP session.


383-383: The repeated calls to free_tcp_session with notify_session_end set to true are consistent with the PR's objectives to enhance session end notifications.

Also applies to: 389-389, 410-410, 420-420, 430-430


444-456: The oc_tcp_end_session function correctly handles the session endpoint copying and session termination based on the notify_session_end flag.


740-740: The use of free_tcp_session in the shutdown function with notify_session_end set to true ensures that session end events are properly triggered during shutdown.


497-497: Ensure that the session ID is correctly passed and handled in the add_new_session call within the initiate_new_session function.

port/android/tcpadapter.c (2)

192-196: Ensure proper handling of session end notifications.

The addition of the notify_session_end parameter allows for controlled session end notifications, aligning with the PR's objectives. This change enhances the flexibility in managing TCP session closures, especially in scenarios where notifications are conditionally required.


471-483: Verify correct session endpoint handling.

Ensure that the session endpoint is correctly handled when ending a session. This is crucial for maintaining accurate session tracking and logging.

security/unittest/rolestest.cpp (1)

82-82: Update to match new function signature.

The update to oc_tls_remove_peer to include the notify_session_end parameter is correctly implemented here, ensuring that session end notifications are appropriately managed during the teardown of test cases.

api/oc_client_api.c (1)

21-27: The inclusion of oc_session_events_internal.h and oc_connectivity_internal.h under the OC_TCP flag is appropriate, ensuring that these dependencies are only compiled when TCP support is enabled.

port/windows/tcpadapter.c (7)

197-201: Ensure notify_session_end is correctly propagated in asynchronous session free operations.


240-245: Proper handling of session IDs and notify_session_end during session creation.


326-339: Review the implementation of oc_tcp_end_session for correct session endpoint handling and notification logic.


188-189: Check the logic for session end notification.

Verification successful

The context provided shows that the logic for session end notification is indeed checking if a disconnect event is ongoing before proceeding with the session end event. This is evident in both instances found in the free_tcp_session and free_tcp_session_async_locked functions.

  • In free_tcp_session, the code checks if (!oc_session_events_disconnect_is_ongoing() && session->notify_session_end) before calling oc_session_end_event(&endpoint).
  • In free_tcp_session_async_locked, the code checks if (!oc_session_events_disconnect_is_ongoing()) before calling oc_session_end_event(&endpoint).

Both instances ensure that the session end event is only triggered if there is no ongoing disconnect event, which aligns with the expected logic.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the session end notification logic correctly handles ongoing disconnect events.

# Test: Search for usage of `oc_session_events_disconnect_is_ongoing` and `notify_session_end`. Expect: Correct handling in all relevant functions.
rg --type c "oc_session_events_disconnect_is_ongoing" "port/windows/tcpadapter.c"

Length of output: 187



Script:

#!/bin/bash
# Extract 10 lines before and after the instances of `oc_session_events_disconnect_is_ongoing` for context
rg --type c -C 10 "oc_session_events_disconnect_is_ongoing" "port/windows/tcpadapter.c"

Length of output: 1207


381-381: Check the session addition logic in initiate_new_session_locked for error handling consistency.

Verification successful

The session addition logic in initiate_new_session_locked consistently handles errors by logging error messages and performing necessary cleanup actions.

  • Instances of add_new_session_locked are followed by error handling logic that logs errors and performs cleanup actions such as closing sockets.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the session addition logic in `initiate_new_session_locked` consistently handles errors.

# Test: Search for usage of `add_new_session_locked` within `initiate_new_session_locked`. Expect: Consistent error handling.
rg --type c "add_new_session_locked" "port/windows/tcpadapter.c"

Length of output: 295



Script:

#!/bin/bash
# Extract and review the surrounding code for each instance of `add_new_session_locked` in `port/windows/tcpadapter.c`.

# Extract 10 lines before and after each instance of `add_new_session_locked` to understand the context and error handling.
rg --type c -A 10 -B 10 "add_new_session_locked" "port/windows/tcpadapter.c"

Length of output: 2165


288-289: Validate error handling in add_new_session_locked during session acceptance.

Verification successful

The error handling in add_new_session_locked during session acceptance is robust. The function and its calling code include proper mechanisms for logging errors, unlocking mutexes, and closing sockets on failure.

  • add_new_session_locked logs errors and closes the socket if WSAEventSelect fails.
  • The calling code logs errors, unlocks mutexes, and closes sockets if add_new_session_locked returns an error.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Ensure that error handling in `add_new_session_locked` is robust during session acceptance.

# Test: Search for error handling patterns in `add_new_session_locked`. Expect: Proper rollback and cleanup on errors.
rg --type c "add_new_session_locked" "port/windows/tcpadapter.c"

Length of output: 295



Script:

#!/bin/bash
# Locate the definition of `add_new_session_locked` and examine its implementation for error handling mechanisms.
ast-grep --lang c --pattern 'int add_new_session_locked(SOCKET sock, ip_context_t *dev, oc_endpoint_t *endpoint, int session_id, int csm_state) { $$$ }'

# Check the context where `add_new_session_locked` is called to ensure proper error handling.
rg --type c "add_new_session_locked" --context 5 "port/windows/tcpadapter.c"

Length of output: 1255


57-57: Ensure proper initialization of notify_session_end in tcp_session struct.

Verification successful

The notify_session_end field in the tcp_session_t struct is properly initialized in the code. It is set to true during the session setup and is also assigned a value in the free_tcp_session_async_locked function based on the passed parameter.

  • In the session setup, notify_session_end is explicitly set to true.
  • In the free_tcp_session_async_locked function, it is assigned the value of the notify_session_end parameter.

These assignments ensure that notify_session_end is properly initialized and managed within the tcp_session_t struct.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `notify_session_end` is always initialized properly when `tcp_session` structs are created or modified.

# Test: Search for all occurrences of `tcp_session` struct being used. Expect: Proper initialization of `notify_session_end`.
rg --type c "tcp_session" "port/windows/tcpadapter.c"

Length of output: 1773



Script:

#!/bin/bash
# Description: Verify the initialization of `notify_session_end` in `tcp_session_t` struct.

# Step 1: Find the definition of `tcp_session_t` struct.
rg --type c "typedef struct tcp_session" "port/windows/tcpadapter.c"

# Step 2: Check the initialization of `tcp_session_t` instances.
rg --type c "oc_memb_alloc" "port/windows/tcpadapter.c"

Length of output: 207



Script:

#!/bin/bash
# Description: Extract the full definition of `tcp_session_t` and the surrounding code for `oc_memb_alloc`.

# Step 1: Extract the full definition of `tcp_session_t` struct.
rg --type c -A 10 "typedef struct tcp_session" "port/windows/tcpadapter.c"

# Step 2: Extract the surrounding code for `oc_memb_alloc`.
rg --type c -A 5 -B 5 "oc_memb_alloc" "port/windows/tcpadapter.c"

Length of output: 654



Script:

#!/bin/bash
# Description: Search for assignments to `notify_session_end` in the codebase to verify its initialization.

# Step 1: Search for assignments to `notify_session_end`.
rg --type c "notify_session_end =" "port/windows/tcpadapter.c"

Length of output: 152



Script:

#!/bin/bash
# Description: Extract the surrounding code for assignments to `notify_session_end` to verify proper initialization.

# Step 1: Extract the surrounding code for assignments to `notify_session_end`.
rg --type c -A 5 -B 5 "notify_session_end =" "port/windows/tcpadapter.c"

Length of output: 806

security/unittest/acltest.cpp (6)

323-323: Ensure proper cleanup of TLS peers.


842-842: Ensure proper cleanup of TLS peers.

Verification successful

The implementation of oc_tls_remove_peer was found in security/oc_tls_internal.h:

void oc_tls_remove_peer(const oc_endpoint_t *endpoint, bool notify_session_end);

Additionally, the test files referencing oc_tls_remove_peer were displayed, showing that the function is used in various unit tests, including tlstest.cpp, rolestest.cpp, and acltest.cpp.

The tests in these files, such as DropOutputMessages and DropOutputMessagesTCP, verify that oc_tls_remove_peer properly cleans up TLS peers by checking that no events remain after the peer is removed.

Given this context, it appears that oc_tls_remove_peer is indeed responsible for cleaning up TLS peers and is being tested for proper cleanup in the unit tests.

  • security/unittest/tlstest.cpp
  • security/unittest/rolestest.cpp
  • security/unittest/acltest.cpp

These tests ensure that oc_tls_remove_peer is functioning correctly and cleaning up resources as expected.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_tls_remove_peer` is properly cleaning up TLS peers.

# Test: Search for memory leaks related to TLS peers. Expect: No leaks reported.
valgrind --leak-check=full --track-origins=yes ./path_to_executable

Length of output: 145



Script:

#!/bin/bash
# Find the implementation of `oc_tls_remove_peer`
ast-grep --lang cpp --pattern 'void oc_tls_remove_peer($_, $_)' 

# Find unit tests related to `oc_tls_remove_peer`
rg 'oc_tls_remove_peer' --glob '*test*'

Length of output: 690



Script:

#!/bin/bash
# Search for the definition of `oc_tls_remove_peer`
rg 'void oc_tls_remove_peer' 

# Display the contents of the test files that reference `oc_tls_remove_peer`
rg 'oc_tls_remove_peer' --glob '*test*' --files-with-matches | xargs cat

Length of output: 63731


734-734: Ensure proper cleanup of TLS peers.


646-646: Ensure proper cleanup of TLS peers.


400-400: Ensure proper cleanup of TLS peers.


784-784: Ensure proper cleanup of TLS peers.

Verification successful

The implementations of oc_tls_free_peer and tls_drop_endpoint_events have been located and reviewed. Both functions appear to handle the cleanup of TLS peers effectively:

  • oc_tls_free_peer removes the peer from the list, frees associated resources, and handles session notifications.
  • tls_drop_endpoint_events ensures that all events related to the endpoint are dropped from various processes.

This indicates that the cleanup process for TLS peers is properly managed.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `oc_tls_remove_peer` is properly cleaning up TLS peers.

# Test: Search for memory leaks related to TLS peers. Expect: No leaks reported.
valgrind --leak-check=full --track-origins=yes ./path_to_executable

Length of output: 145



Script:

#!/bin/bash
# Description: Locate the implementation of `oc_tls_remove_peer` to verify its cleanup process.

# Search for the implementation of `oc_tls_remove_peer` in the codebase.
ast-grep --lang cpp --pattern 'void oc_tls_remove_peer($_, $_) { $$$ }'

Length of output: 73



Script:

#!/bin/bash
# Description: Locate the implementation of `oc_tls_remove_peer` to verify its cleanup process.

# Search for the definition of `oc_tls_remove_peer` in the codebase.
rg 'void oc_tls_remove_peer'

Length of output: 138



Script:

#!/bin/bash
# Description: Locate the implementation of `oc_tls_remove_peer` to verify its cleanup process.

# Search for the implementation of `oc_tls_remove_peer` in the codebase.
rg 'void oc_tls_remove_peer' -A 20

Length of output: 2596



Script:

#!/bin/bash
# Description: Extract the complete implementation of `oc_tls_remove_peer` to verify its cleanup process.

# Extract the complete implementation of `oc_tls_remove_peer` from `security/oc_tls.c`.
ast-grep --lang cpp --pattern 'void oc_tls_remove_peer(const oc_endpoint_t *orig_endpoint, bool notify_session_end) { $$$ }' security/oc_tls.c

Length of output: 144



Script:

#!/bin/bash
# Description: Extract the complete implementation of `oc_tls_remove_peer` to verify its cleanup process.

# Extract the complete implementation of `oc_tls_remove_peer` from `security/oc_tls.c` with a larger context window.
rg 'void oc_tls_remove_peer' -A 50 security/oc_tls.c

Length of output: 54



Script:

#!/bin/bash
# Description: Locate the implementation of `oc_tls_remove_peer` to verify its cleanup process.

# Search for the function name `oc_tls_remove_peer` in the codebase.
rg 'oc_tls_remove_peer' -A 50

Length of output: 39674



Script:

#!/bin/bash
# Description: Locate the implementations of `oc_tls_free_peer` and `tls_drop_endpoint_events` to verify their cleanup processes.

# Search for the implementation of `oc_tls_free_peer` in the codebase.
rg 'void oc_tls_free_peer' -A 50

# Search for the implementation of `tls_drop_endpoint_events` in the codebase.
rg 'void tls_drop_endpoint_events' -A 50

Length of output: 77



Script:

#!/bin/bash
# Description: Locate the implementations of `oc_tls_free_peer` and `tls_drop_endpoint_events` to verify their cleanup processes.

# Search for the function name `oc_tls_free_peer` in the codebase.
rg 'oc_tls_free_peer' -A 50

# Search for the function name `tls_drop_endpoint_events` in the codebase.
rg 'tls_drop_endpoint_events' -A 50

Length of output: 44758

port/linux/tcpsession.c (8)

63-63: Ensure the new notify_session_end field in tcp_session_t is well-documented.


99-99: Document the notify_session_end field in tcp_waiting_session_t for clarity.


214-216: Ensure logging includes the new session_id for better traceability.


257-262: The default setting of notify_session_end to true ensures that session end notifications are sent unless explicitly disabled. This change aligns with the PR's goal to enhance control over session notifications.


324-325: Review the condition for session end notification.


711-715: Clarify the asynchronous session freeing logic.


725-730: Ensure that waiting sessions are freed correctly.


740-767: Check the logic for ending TCP sessions.

messaging/coap/engine.c (1)

1103-1103: The addition of oc_close_session for TCP connections in the coap_process_invalid_inbound_message function aligns with the PR's objective to enhance TCP client disconnect notifications. This ensures that TCP sessions are properly closed upon receiving invalid messages, enhancing the robustness of session management.

port/linux/ipadapter.c (2)

1592-1596: The modification to oc_connectivity_end_session to repeatedly call oc_connectivity_end_session_v1 until all sessions are ended enhances the robustness of session management.


1580-1590: The changes to oc_connectivity_end_session_v1 align with the PR objectives to enhance session management. Ensure that tcp_end_session correctly handles the new notify_session_end and session_endpoint parameters.

Verification successful

The function tcp_end_session correctly handles the new parameters notify_session_end and session_endpoint. The mutex is locked, the session is found and freed asynchronously, and the session endpoint is copied if provided. The function returns true if a session is found and false otherwise.

  • tcp_end_session implementation:
    • Handles notify_session_end by passing it to free_session_async_locked.
    • Copies session_endpoint if it is not NULL.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the implementation of `tcp_end_session` to ensure it handles the new parameters correctly.

# Test: Search for the function implementation. Expect: Handling of `notify_session_end` and `session_endpoint`.
ast-grep --lang c --pattern $'bool tcp_end_session(const oc_endpoint_t *endpoint, bool notify_session_end, oc_endpoint_t *session_endpoint) {
  $$$
}'

Length of output: 1973

port/android/ipadapter.c (1)

1638-1650: Ensure proper handling of TCP session endings.

The function oc_connectivity_end_session_v1 is critical for handling TCP session endings. It's important to verify that the oc_tcp_end_session function, which is called within, handles all necessary cleanup and state management, especially in terms of the new notify_session_end parameter.

Verification successful

The oc_tcp_end_session function in port/android/tcpadapter.c does handle the notify_session_end parameter by passing it to the free_tcp_session function. This indicates that the parameter is being used in the session cleanup process.

  • port/android/tcpadapter.c: oc_tcp_end_session(ip_context_t *dev, const oc_endpoint_t *endpoint, bool notify_session_end, oc_endpoint_t *session_endpoint): The function locks the TCP mutex, finds the session by endpoint, and if found, it calls free_tcp_session with the notify_session_end parameter.

This confirms that the notify_session_end parameter is being properly handled within the oc_tcp_end_session function.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the implementation of `oc_tcp_end_session` to ensure it properly handles session endings.

# Test: Search for the function implementation and check for proper handling of the `notify_session_end` parameter.
ast-grep --lang c --pattern $'ip_context_t *dev = get_ip_context_for_device($_);
if (dev) {
  oc_tcp_end_session(dev, $_, $_, $_);
}'

Length of output: 659



Script:

#!/bin/bash
# Description: Verify the implementation of `oc_tcp_end_session` to ensure it properly handles session endings.

# Step 1: Search for the implementation of the `oc_tcp_end_session` function.
ast-grep --lang c --pattern 'void oc_tcp_end_session($_, $_, $_, $_) { $$$ }'

# Step 2: If the function is found, inspect its handling of the `notify_session_end` parameter.
# Note: This step will be executed manually based on the output of the first step.

Length of output: 79



Script:

#!/bin/bash
# Description: Verify the implementation of `oc_tcp_end_session` to ensure it properly handles session endings.

# Step 1: Search for the definition of the `oc_tcp_end_session` function.
rg 'void oc_tcp_end_session' -t c

# Step 2: If the function is found, inspect its handling of the `notify_session_end` parameter.
# Note: This step will be executed manually based on the output of the first step.

Length of output: 35



Script:

#!/bin/bash
# Description: Verify the implementation of `oc_tcp_end_session` to ensure it properly handles session endings.

# Step 1: Search for the definition of the `oc_tcp_end_session` function without specifying the return type.
rg 'oc_tcp_end_session' -t c

# Step 2: If the function is found, inspect its handling of the `notify_session_end` parameter.
# Note: This step will be executed manually based on the output of the first step.

Length of output: 945



Script:

#!/bin/bash
# Description: Verify the implementation of `oc_tcp_end_session` to ensure it properly handles session endings.

# Step 1: Extract the implementation of the `oc_tcp_end_session` function from the relevant file for the Android platform.
ast-grep --lang c --pattern 'oc_tcp_end_session($_, $_, $_, $_) { $$$ }' port/android/tcpadapter.c

Length of output: 565



Script:

#!/bin/bash
# Description: Verify the implementation of `oc_tcp_end_session` to ensure it properly handles session endings.

# Step 1: Extract the implementation of the `oc_tcp_end_session` function from the relevant file for the Android platform.
ast-grep --lang c --pattern 'oc_tcp_end_session($_, $_, $_) { $$$ }' port/android/tcpadapter.c

Length of output: 557



Script:

#!/bin/bash
# Description: Verify the implementation of `oc_tcp_end_session` to ensure it properly handles session endings.

# Step 1: Search for the definition of the `oc_tcp_end_session` function without specifying the return type.
rg 'oc_tcp_end_session' -t c

# Step 2: Extract the implementation of the `oc_tcp_end_session` function from the relevant file for the Android platform.
rg -A 20 'oc_tcp_end_session' port/android/tcpadapter.c

Length of output: 1669

port/esp32/adapter/src/ipadapter.c (2)

1626-1638: The updates to oc_connectivity_end_session_v1 correctly handle the new notify_session_end parameter.


1620-1624: Verify the necessity of the loop in oc_connectivity_end_session.

port/windows/ipadapter.c (2)

1693-1704: Changes correctly implement the intended functionality by passing the notify_session_end parameter to oc_tcp_end_session.


1687-1691: Ensure oc_tcp_end_session can return false to prevent potential infinite loops.

security/oc_tls.c (9)

606-606: The function oc_tls_close_peer now correctly handles the notify_session_end parameter by passing it to oc_tls_free_peer. This change is consistent with the overall enhancement of session management.
[APROVED]


2175-2175: The function oc_tls_free_peer is called with the notify_session_end parameter set to true in the error handling path of oc_tls_peer_ssl_init. This is consistent with the intended use of the new parameter to control session end notifications.


2569-2569: The function oc_tls_free_peer is called with the notify_session_end parameter set to true in the error handling path of oc_tls_send_message_internal. This usage is appropriate and aligns with the changes made to enhance session management.


2847-2847: The function oc_tls_free_peer is called with the notify_session_end parameter set to true in the error handling path of tls_read_application_data_tcp. This consistent usage across different parts of the codebase helps in maintaining a uniform approach to session end notifications.


2896-2896: The function oc_tls_free_peer is called with the notify_session_end parameter set to true in the error handling path of tls_handshake_step. This is another instance where the new parameter is used correctly according to the intended enhancements.


Line range hint 465-546: The addition of the notify_session_end parameter to oc_tls_free_peer allows for controlled notification of session ends. This is a positive change for enhancing session management flexibility. However, ensure that all calls to this function across the codebase are updated to include this new parameter to avoid compilation errors.

Verification successful

All calls to oc_tls_free_peer have been updated to include the new notify_session_end parameter.

  • security/oc_tls.c: All instances of oc_tls_free_peer have been updated with the new parameter.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `oc_tls_free_peer` match the new signature.

# Test: Search for the function usage. Expect: Only occurances of the new signature.
rg --type c $'oc_tls_free_peer'

Length of output: 1467


2302-2314: The function tls_close_connection has been updated to handle the notify_session_end parameter. This is part of the broader changes to enhance session end notifications. It's important to verify that this function is used correctly throughout the codebase.

Verification successful

The verification confirms that the function tls_close_connection is called with the new signature in the codebase.

  • security/oc_tls.c: tls_close_connection(endpoint, false);
  • security/oc_tls.c: tls_close_connection(&endpoint, true);

No instances of the old signature were found.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `tls_close_connection` match the new signature.

# Test: Search for the function usage. Expect: Only occurances of the new signature.
rg --type c $'tls_close_connection'

Length of output: 1407



Script:

#!/bin/bash
# Description: Verify the function signature of `tls_close_connection` and ensure all calls match the new signature.

# Check the function signature of `tls_close_connection`
ast-grep --lang c --pattern $'tls_close_connection(const oc_endpoint_t *orig_endpoint, bool from_reset)'

# Verify all calls to `tls_close_connection` match the new signature
rg --type c --pcre2 '\btls_close_connection\s*\(\s*[^,]+,\s*(true|false)\s*\)'

Length of output: 653


3091-3093: The function tls_close_connection is called with the from_reset parameter set to true in close_all_tls_sessions_for_device_reset. This is appropriate for handling device resets, but ensure that the notify_session_end parameter is handled correctly in all scenarios where this function is called.

Verification successful

The verification confirms that the tls_close_connection function is called with the correct parameters in all instances within security/oc_tls.c. The from_reset parameter is appropriately set to true or false based on the context of the call.

  • tls_close_connection(endpoint, false); in oc_tls_close_connection
  • tls_close_connection(&endpoint, true); in oc_tls_prf

This ensures that the notify_session_end parameter is handled correctly in all scenarios where this function is called.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `tls_close_connection` match the new signature.

# Test: Search for the function usage. Expect: Only occurances of the new signature.
rg --type c $'tls_close_connection'

Length of output: 1407



Script:

#!/bin/bash
# Description: Verify all function calls to `tls_close_connection` match the new signature.

# Extract all instances of `tls_close_connection` calls and display the surrounding lines for context.
rg --type c --context 5 'tls_close_connection' security/oc_tls.c

Length of output: 1177


573-595: The function oc_tls_remove_peer now includes the notify_session_end parameter, which is correctly passed to oc_tls_free_peer. This change aligns with the PR's goal to enhance session end notifications. Ensure that all invocations of oc_tls_remove_peer are updated accordingly.

Verification successful

The search results indicate that the function oc_tls_remove_peer is invoked in two files: api/oc_session_events.c and api/plgd/plgd_time.c. We need to verify that these invocations correctly pass the notify_session_end parameter.


The invocations of oc_tls_remove_peer in both api/oc_session_events.c and api/plgd/plgd_time.c correctly pass the notify_session_end parameter as required by the new function signature.

  • api/oc_session_events.c: oc_tls_remove_peer(endpoint, false);
  • api/plgd/plgd_time.c: oc_tls_remove_peer(fetch.endpoint, true);

This confirms that all invocations of oc_tls_remove_peer have been updated to match the new signature.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `oc_tls_remove_peer` match the new signature.

# Test: Search for the function usage. Expect: Only occurances of the new signature.
rg --type c $'oc_tls_remove_peer'

Length of output: 372



Script:

#!/bin/bash
# Verify the invocations of `oc_tls_remove_peer` in the identified files.

# Display the relevant lines in `api/oc_session_events.c`
rg --type c --context 5 'oc_tls_remove_peer' api/oc_session_events.c

# Display the relevant lines in `api/plgd/plgd_time.c`
rg --type c --context 5 'oc_tls_remove_peer' api/plgd/plgd_time.c

Length of output: 940

api/plgd/plgd_time.c Show resolved Hide resolved
port/android/ipadapter.c Show resolved Hide resolved
api/oc_client_api.c Show resolved Hide resolved
port/esp32/adapter/src/tcpadapter.c Show resolved Hide resolved
port/android/tcpadapter.c Show resolved Hide resolved
port/android/tcpadapter.c Show resolved Hide resolved
security/unittest/tlstest.cpp Show resolved Hide resolved
port/windows/tcpadapter.h Show resolved Hide resolved
port/android/tcpadapter.h Show resolved Hide resolved
@Danielius1922 Danielius1922 force-pushed the jkralik/fix/closing-tcp-session branch 2 times, most recently from ac609f3 to e63a90d Compare May 31, 2024 20:52
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ff02ba0 and e63a90d.

Files selected for processing (14)
  • api/oc_client_api.c (3 hunks)
  • api/oc_endpoint.c (2 hunks)
  • api/oc_session_events.c (3 hunks)
  • api/oc_session_events_internal.h (4 hunks)
  • api/oc_tcp.c (2 hunks)
  • api/oc_tcp_internal.h (2 hunks)
  • port/android/tcpadapter.c (13 hunks)
  • port/esp32/adapter/src/tcpadapter.c (14 hunks)
  • port/linux/tcpsession.c (16 hunks)
  • port/linux/tcpsession.h (4 hunks)
  • port/oc_connectivity_internal.h (3 hunks)
  • port/windows/tcpadapter.c (12 hunks)
  • security/oc_tls.c (20 hunks)
  • security/oc_tls_internal.h (1 hunks)
Files skipped from review as they are similar to previous changes (13)
  • api/oc_client_api.c
  • api/oc_endpoint.c
  • api/oc_session_events.c
  • api/oc_session_events_internal.h
  • api/oc_tcp.c
  • api/oc_tcp_internal.h
  • port/android/tcpadapter.c
  • port/esp32/adapter/src/tcpadapter.c
  • port/linux/tcpsession.h
  • port/oc_connectivity_internal.h
  • port/windows/tcpadapter.c
  • security/oc_tls.c
  • security/oc_tls_internal.h
Additional comments not posted (9)
port/linux/tcpsession.c (9)

63-63: Ensure the new notify_session_end field in tcp_session_t is well-documented.


99-99: Document the notify_session_end field in tcp_waiting_session_t for clarity.


214-216: Ensure logging includes the new session ID for better traceability.


257-262: The session ID assignment logic is clear and well-implemented.


324-325: Review the condition for session end notification.


336-337: Ensure that session logging on free operations includes all relevant session details.


711-715: Clarify the asynchronous session freeing logic.


725-730: Ensure that waiting sessions are freed correctly.


740-767: Check the logic for ending TCP sessions.

@Danielius1922 Danielius1922 force-pushed the jkralik/fix/closing-tcp-session branch from e63a90d to 629c3d9 Compare May 31, 2024 21:06
@Danielius1922 Danielius1922 added OCF Conformance Testing OCF Conformance Testing required and removed OCF Conformance Testing OCF Conformance Testing required labels May 31, 2024
@ocf-conformance-test-tool ocf-conformance-test-tool bot removed the OCF Conformance Testing OCF Conformance Testing required label Jun 1, 2024
@Danielius1922 Danielius1922 force-pushed the jkralik/fix/closing-tcp-session branch from 15d56ff to 21ae1ff Compare June 1, 2024 14:05
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Outside diff range and nitpick comments (1)
port/linux/tcpsession.c (1)

Line range hint 239-267: Review the session creation logic for potential resource leaks if oc_memb_alloc fails after get_interface_index.

Consider adding a cleanup or error handling block after the interface index is obtained but before the session object allocation, to ensure that resources are properly managed in case of allocation failure.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e63a90d and 15d56ff.

Files selected for processing (15)
  • api/oc_client_api.c (3 hunks)
  • api/oc_endpoint.c (2 hunks)
  • api/oc_session_events.c (3 hunks)
  • api/oc_session_events_internal.h (4 hunks)
  • api/oc_tcp.c (2 hunks)
  • api/oc_tcp_internal.h (2 hunks)
  • port/android/tcpadapter.c (15 hunks)
  • port/esp32/adapter/src/tcpadapter.c (16 hunks)
  • port/linux/tcpsession.c (18 hunks)
  • port/linux/tcpsession.h (4 hunks)
  • port/oc_connectivity_internal.h (3 hunks)
  • port/unittest/connectivitytest.cpp (4 hunks)
  • port/windows/tcpadapter.c (13 hunks)
  • security/oc_tls.c (20 hunks)
  • security/oc_tls_internal.h (1 hunks)
Files skipped from review as they are similar to previous changes (12)
  • api/oc_client_api.c
  • api/oc_endpoint.c
  • api/oc_session_events.c
  • api/oc_session_events_internal.h
  • api/oc_tcp.c
  • api/oc_tcp_internal.h
  • port/android/tcpadapter.c
  • port/linux/tcpsession.h
  • port/oc_connectivity_internal.h
  • port/windows/tcpadapter.c
  • security/oc_tls.c
  • security/oc_tls_internal.h
Additional comments not posted (17)
port/unittest/connectivitytest.cpp (1)

45-45: Using std::chrono_literals enhances readability and ease of use with time durations, aligning with modern C++ practices.

port/esp32/adapter/src/tcpadapter.c (8)

162-166: Ensure proper session end notification in free_tcp_session.

Also applies to: 399-399, 405-405, 426-426, 436-436, 446-446


Line range hint 187-217: Adding session ID handling in add_new_session aligns with the new session management enhancements.


256-257: Proper error handling in accept_new_session when unable to record a new TCP session.


289-303: The function find_session_by_id efficiently locates TCP sessions using session IDs, which is crucial for managing multiple sessions.


460-472: The oc_tcp_end_session function correctly handles session termination with notification control.


513-513: Proper error handling in initiate_new_session when unable to record a new TCP session.


756-756: Session cleanup in oc_tcp_connectivity_shutdown ensures all sessions are properly terminated.


775-782: The oc_tcp_session_state function correctly reports the connection state based on session ID.

port/linux/tcpsession.c (8)

63-63: Ensure the notify_session_end field in tcp_session_t is well-documented.


99-99: Document the notify_session_end field in tcp_waiting_session_t for clarity.


331-332: Review the condition for session end notification.


740-744: Clarify the asynchronous session freeing logic.


754-759: Ensure that waiting sessions are freed correctly.


769-796: Check the logic for ending TCP sessions.


312-319: Ensure that the session creation in accept_new_session_locked handles all error paths correctly.

Verification successful

The function accept_new_session_locked in port/linux/tcpsession.c appears to handle error paths correctly by logging errors and returning -1 when the accept call fails. Additionally, the function ensures proper cleanup by closing the socket if session creation fails.

  • The function logs an error message if accept fails.
  • It returns -1 immediately if accept fails, preventing further processing.
  • If session creation fails, it logs an error, closes the new socket, and returns -1.

These measures indicate that the function handles error paths appropriately.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all error paths in session creation are handled.

# Test: Search for error handling in session creation. Expect: Proper cleanup and error logging on all paths.
rg --type python $'accept_new_session_locked'

Length of output: 75



Script:

#!/bin/bash
# Description: Verify all error paths in session creation are handled.

# Test: Search for the function definition and usage of `accept_new_session_locked` in C files.
rg --type c 'accept_new_session_locked'

Length of output: 494



Script:

#!/bin/bash
# Description: Extract the function definition and surrounding lines for `accept_new_session_locked` to verify error handling.

# Extract the function definition and 20 lines before and after for context.
rg --type c -A 20 -B 20 'accept_new_session_locked' port/linux/tcpsession.c

Length of output: 4192


208-218: Ensure logging functions are covered by tests to verify correct formatting and data handling.

@Danielius1922 Danielius1922 force-pushed the jkralik/fix/closing-tcp-session branch from 21ae1ff to 98e95f2 Compare June 1, 2024 14:17
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e63a90d and 21ae1ff.

Files selected for processing (15)
  • api/oc_client_api.c (3 hunks)
  • api/oc_endpoint.c (2 hunks)
  • api/oc_session_events.c (3 hunks)
  • api/oc_session_events_internal.h (4 hunks)
  • api/oc_tcp.c (2 hunks)
  • api/oc_tcp_internal.h (2 hunks)
  • port/android/tcpadapter.c (16 hunks)
  • port/esp32/adapter/src/tcpadapter.c (16 hunks)
  • port/linux/tcpsession.c (18 hunks)
  • port/linux/tcpsession.h (4 hunks)
  • port/oc_connectivity_internal.h (3 hunks)
  • port/unittest/connectivitytest.cpp (4 hunks)
  • port/windows/tcpadapter.c (15 hunks)
  • security/oc_tls.c (20 hunks)
  • security/oc_tls_internal.h (1 hunks)
Files skipped from review as they are similar to previous changes (10)
  • api/oc_client_api.c
  • api/oc_endpoint.c
  • api/oc_session_events.c
  • api/oc_session_events_internal.h
  • api/oc_tcp_internal.h
  • port/android/tcpadapter.c
  • port/linux/tcpsession.h
  • port/windows/tcpadapter.c
  • security/oc_tls.c
  • security/oc_tls_internal.h
Additional comments not posted (18)
port/oc_connectivity_internal.h (1)

99-102: The addition of notify_session_end and session_endpoint parameters enhances session management capabilities.

api/oc_tcp.c (1)

92-97: The use of atomic operations for session ID increment ensures thread safety and robustness.

port/unittest/connectivitytest.cpp (1)

472-473: The modification to handle session states based on session IDs enhances testing capabilities for TCP session management.

port/esp32/adapter/src/tcpadapter.c (8)

163-163: The implementation of free_tcp_session correctly handles the new notify_session_end parameter. This ensures that session end events are only triggered based on the condition provided, which aligns with the PR's objectives to enhance TCP client disconnect notifications.


257-258: The call to add_new_session within accept_new_session correctly passes a default session_id of 0 when a new TCP session is accepted. This is a good use of default values to ensure backward compatibility while still supporting new functionality.


400-406: The repeated calls to free_tcp_session within various error handling and session closure scenarios are consistent with the new parameter usage. This ensures that session end notifications are correctly managed during error conditions and normal session terminations.

Also applies to: 427-427, 437-437, 447-447


461-473: The function oc_tcp_end_session has been updated to include the notify_session_end parameter, which is used correctly to control the notification of session ends based on the provided boolean. This is a critical update for managing session ends more granularly.


514-514: The call to add_new_session within initiate_new_session correctly passes the session_id received as a parameter, ensuring that sessions are tracked with unique identifiers. This is crucial for session management and aligns with the changes made across the codebase.


757-757: The call to free_tcp_session within oc_tcp_connectivity_shutdown correctly uses the notify_session_end parameter set to true. This ensures that all session ends are notified during the shutdown process, which is essential for clean resource management and client notification.


777-783: The function oc_tcp_session_state correctly utilizes the find_session_by_id function to determine the state of a session based on its ID. This is an essential part of managing sessions by ID, which has been a focus of this update.


Line range hint 188-217: The function add_new_session has been updated to handle a new session_id parameter. This change is crucial for managing sessions more effectively by assigning unique session IDs. However, ensure that all calls to this function across the codebase are updated to pass the correct session_id.

Verification successful

The search results indicate that all calls to add_new_session include the session_id parameter. Here are the relevant lines from the codebase:

  • port/windows/tcpadapter.c

    • Line: if (add_new_session_locked(new_socket, dev, endpoint, /*session_id*/ 0,
    • Line: if (add_new_session_locked(sock, dev, endpoint, session_id, CSM_SENT) < 0) {
  • port/linux/tcpsession.c

    • Line: tcp_session_t *session = add_new_session_locked(new_socket, dev, endpoint,
    • Line: add_new_session_locked(sock, dev, endpoint, session_id, CSM_SENT);
  • port/esp32/adapter/src/tcpadapter.c

    • Line: if (add_new_session(new_socket, dev, endpoint, /*session_id*/ 0, CSM_NONE) <
    • Line: if (add_new_session(sock, dev, endpoint, session_id, CSM_SENT) < 0) {
  • port/android/tcpadapter.c

    • Line: if (add_new_session(new_socket, dev, endpoint, /*session_id*/ 0, CSM_NONE) <
    • Line: if (add_new_session(sock, dev, endpoint, session_id, CSM_SENT) < 0) {

All instances show that the session_id parameter is included in the function calls.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all calls to `add_new_session` include the `session_id` parameter.

# Test: Search for the function usage without the new parameter. Expect: No occurrences.
rg --type c 'add_new_session\([^,]*,[^,]*,[^,]*\)$'

Length of output: 53



Script:

#!/bin/bash
# Description: Find all occurrences of `add_new_session` in the codebase to verify the parameters.

# Search for all calls to `add_new_session` in C files.
rg --type c 'add_new_session'

Length of output: 1240

port/linux/tcpsession.c (7)

63-63: Ensure the new notify_session_end field in tcp_session_t is well-documented.


99-99: Document the notify_session_end field in tcp_waiting_session_t for clarity.


208-218: Ensure logging functions are properly excluded from coverage.


331-332: Review the condition for session end notification.


740-744: Clarify the asynchronous session freeing logic.


754-759: Ensure that waiting sessions are freed correctly.


769-796: Check the logic for ending TCP sessions.

@Danielius1922 Danielius1922 force-pushed the jkralik/fix/closing-tcp-session branch from 98e95f2 to 330a916 Compare June 1, 2024 14:42
@Danielius1922 Danielius1922 force-pushed the jkralik/fix/closing-tcp-session branch from 330a916 to ff1c658 Compare June 3, 2024 09:08
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Outside diff range and nitpick comments (1)
port/esp32/adapter/src/tcpadapter.c (1)

Line range hint 188-215: Review the session initialization logic for potential improvements.

The session initialization logic could be encapsulated into a separate function to improve readability and maintainability. Consider refactoring this block to a new function named initialize_session.

+ static int initialize_session(tcp_session_t *session, int sock, ip_context_t *dev, oc_endpoint_t *endpoint, uint32_t session_id, tcp_csm_state_t state) {
+   int if_index = get_interface_index(sock);
+   if (if_index < 0) {
+     OC_ERR("could not get interface index");
+     return -1;
+   }
+   session->dev = dev;
+   endpoint->interface_index = (unsigned)if_index;
+   if (session_id == 0) {
+     session_id = oc_tcp_get_new_session_id();
+   }
+   endpoint->session_id = session_id;
+   memcpy(&session->endpoint, endpoint, sizeof(oc_endpoint_t));
+   session->endpoint.next = NULL;
+   session->sock = sock;
+   session->csm_state = state;
+   oc_list_add(session_list, session);
+   if ((session->endpoint.flags & SECURED) == 0) {
+     oc_session_start_event(&session->endpoint);
+   }
+   OC_DBG("recorded new TCP session");
+   return 0;
+ }
- int if_index = get_interface_index(sock);
- if (if_index < 0) {
-   OC_ERR("could not get interface index");
-   return -1;
- }
- tcp_session_t *session = oc_memb_alloc(&tcp_session_s);
- if (!session) {
-   OC_ERR("could not allocate new TCP session object");
-   return -1;
- }
- session->dev = dev;
- endpoint->interface_index = (unsigned)if_index;
- if (session_id == 0) {
-   session_id = oc_tcp_get_new_session_id();
- }
- endpoint->session_id = session_id;
- memcpy(&session->endpoint, endpoint, sizeof(oc_endpoint_t));
- session->endpoint.next = NULL;
- session->sock = sock;
- session->csm_state = state;
- oc_list_add(session_list, session);
- if ((session->endpoint.flags & SECURED) == 0) {
-   oc_session_start_event(&session->endpoint);
- }
- OC_DBG("recorded new TCP session");

This refactor not only cleans up the add_new_session function but also isolates session initialization into a dedicated function, making the code easier to understand and modify.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e63a90d and ff1c658.

Files selected for processing (15)
  • api/oc_client_api.c (3 hunks)
  • api/oc_endpoint.c (2 hunks)
  • api/oc_session_events.c (3 hunks)
  • api/oc_session_events_internal.h (4 hunks)
  • api/oc_tcp.c (2 hunks)
  • api/oc_tcp_internal.h (2 hunks)
  • port/android/tcpadapter.c (16 hunks)
  • port/esp32/adapter/src/tcpadapter.c (16 hunks)
  • port/linux/tcpsession.c (18 hunks)
  • port/linux/tcpsession.h (4 hunks)
  • port/oc_connectivity_internal.h (3 hunks)
  • port/unittest/connectivitytest.cpp (4 hunks)
  • port/windows/tcpadapter.c (15 hunks)
  • security/oc_tls.c (20 hunks)
  • security/oc_tls_internal.h (1 hunks)
Files skipped from review as they are similar to previous changes (11)
  • api/oc_client_api.c
  • api/oc_endpoint.c
  • api/oc_session_events.c
  • api/oc_session_events_internal.h
  • api/oc_tcp.c
  • api/oc_tcp_internal.h
  • port/android/tcpadapter.c
  • port/linux/tcpsession.h
  • port/windows/tcpadapter.c
  • security/oc_tls.c
  • security/oc_tls_internal.h
Additional comments not posted (12)
port/oc_connectivity_internal.h (3)

99-102: The addition of oc_connectivity_end_session_v1 function is well-documented and correctly uses OC_NONNULL(1) to ensure safety.


106-115: The oc_tcp_connect_retry_t structure and related constants are well-defined, providing clear configuration options for TCP connection retries.


142-142: The oc_tcp_session_state function is clearly documented and provides useful information about the TCP connection state based on the session ID.

port/unittest/connectivitytest.cpp (1)

45-45: The use of std::chrono_literals improves the readability of time-related operations in the tests.

port/esp32/adapter/src/tcpadapter.c (1)

163-167: Ensure proper handling of session end notifications.

The addition of the notify_session_end parameter allows for controlled session end notifications, which can enhance the robustness of the TCP session management. Good implementation of conditional notification based on the ongoing state of session disconnections.

port/linux/tcpsession.c (7)

63-63: Ensure the new notify_session_end field in tcp_session_t is well-documented.


99-99: Document the notify_session_end field in tcp_waiting_session_t for clarity.


328-329: Review the condition for session end notification.


736-740: Clarify the asynchronous session freeing logic.


750-755: Ensure that waiting sessions are freed correctly.


765-792: Check the logic for ending TCP sessions.


208-218: Ensure logging functions are covered by tests to verify correct string formatting and data handling.

Verification successful

Ensure logging functions are covered by tests to verify correct string formatting and data handling.

  • The search results indicate that there are multiple instances of OC_DBG in various test files, confirming that logging functions are indeed covered by tests.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify coverage for logging functions.

# Test: Search for tests covering the logging functions. Expect: At least one test case per function.
rg --type python $'log_new_session'

Length of output: 65



Script:

#!/bin/bash
# Description: Verify coverage for logging functions.

# Test: Search for tests covering the logging functions. Expect: At least one test case per function.
rg --type py 'log_new_session'

Length of output: 30



Script:

#!/bin/bash
# Description: Verify coverage for logging functions.

# Step 1: Identify logging functions in the codebase.
rg --type c 'OC_DBG'

# Step 2: Search for test cases covering these logging functions.
rg --type py 'OC_DBG'

Length of output: 75363



Script:

#!/bin/bash
# Description: Verify coverage for logging functions.

# Step 1: Identify test files in the codebase.
fd --type f --name '*test*'

# Step 2: Check the contents of identified test files for logging function coverage.
rg 'OC_DBG' $(fd --type f --name '*test*')

Length of output: 126725

port/esp32/adapter/src/tcpadapter.c Show resolved Hide resolved
port/esp32/adapter/src/tcpadapter.c Show resolved Hide resolved
port/esp32/adapter/src/tcpadapter.c Show resolved Hide resolved
Copy link

sonarcloud bot commented Jun 3, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
20.7% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

@Danielius1922 Danielius1922 added the OCF Conformance Testing OCF Conformance Testing required label Jun 3, 2024
@Danielius1922
Copy link
Member

@coderabbitai update description for this pull request based on changes

Copy link

coderabbitai bot commented Jun 3, 2024

Tip

For best results, initiate chat on the files or code changes.

@Danielius1922, here's an updated description for the pull request that incorporates the latest changes and their implications:


Title: Enhanced Control Over TCP Client Disconnect Notifications in TLS Handling

Description:
This pull request introduces significant enhancements to the oc_tls.c file within the security module, focusing on the management of TCP client disconnect notifications. The introduction of a new boolean parameter, notify_session_end, across several function signatures including oc_tls_free_peer, oc_tls_remove_peer, and oc_tls_close_peer, marks a pivotal update. This parameter enables controlled notifications of session ends, thereby augmenting the flexibility and robustness of session handling within the TLS framework.

Key Changes:

  • Introduction of notify_session_end Parameter: Functions such as oc_tls_free_peer, oc_tls_remove_peer, and oc_tls_close_peer now include an additional boolean parameter notify_session_end. This allows for selective notification of session terminations, enhancing control over session lifecycle management.
  • Improved Session Management: The changes facilitate more precise management of session end notifications, which is crucial for maintaining the stability and security of network communications.
  • Enhanced Flexibility and Robustness: By allowing selective notification of session ends, the system can handle abrupt client disconnects more gracefully, ensuring that the TLS handling remains robust and flexible under various network conditions.

These updates aim to provide better control and flexibility in managing session ends, which is essential for the stability and security of networked applications.


Feel free to adjust the wording as needed!

@Danielius1922
Copy link
Member

@coderabbitai Are you analyzing all commits in this PR? The new session_id member on oc_endpoint_t is an important feature, but I don't see it mentioned anywhere. Why is that?

Copy link

coderabbitai bot commented Jun 3, 2024

Tip

For best results, initiate chat on the files or code changes.

@Danielius1922, thank you for pointing out the missing information regarding the session_id member in oc_endpoint_t. I've reviewed the contents of oc_endpoint.c and oc_endpoint.h and confirmed that the session_id member has indeed been added to the oc_endpoint_t structure. This member is crucial for pairing TLS peers with TCP sessions, which is a significant feature for managing session continuity and security.

It appears that this addition was overlooked in the initial summaries and descriptions provided. I apologize for this oversight. Here's how you might include this information in the PR description to highlight its importance:


Enhanced Control Over TCP Client Disconnect Notifications in TLS Handling

This pull request introduces significant enhancements to the oc_tls.c file within the security module, focusing on the management of TCP client disconnect notifications. The introduction of a new boolean parameter, notify_session_end, across several function signatures including oc_tls_free_peer, oc_tls_remove_peer, and oc_tls_close_peer, marks a pivotal update. Additionally, a new session_id member has been added to the oc_endpoint_t structure to ensure robust session management by pairing TLS peers with TCP sessions effectively.

Key Changes:

  • Introduction of notify_session_end Parameter: Functions such as oc_tls_free_peer, oc_tls_remove_peer, and oc_tls_close_peer now include an additional boolean parameter notify_session_end. This allows for selective notification of session terminations, enhancing control over session lifecycle management.
  • New session_id Member in oc_endpoint_t: This new member facilitates the effective pairing of TLS peers with TCP sessions, crucial for maintaining session continuity and security across network communications.
  • Improved Session Management: The changes facilitate more precise management of session end notifications and enhance session continuity through the new session_id feature.
  • Enhanced Flexibility and Robustness: By allowing selective notification of session ends and robust session pairing, the system can handle abrupt client disconnects more gracefully, ensuring that the TLS handling remains robust and flexible under various network conditions.

These updates aim to provide better control and flexibility in managing session ends and enhancing session security, which is essential for the stability and security of networked applications.


Please feel free to adjust the wording as needed!

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for oc_endpoint.c and oc_endpoint.h files and display their contents to verify the presence of 'session_id'
fd oc_endpoint.c --exec cat {}
fd oc_endpoint.h --exec cat {}

Length of output: 28676

@Danielius1922 Danielius1922 merged commit 09b9743 into master Jun 3, 2024
124 of 125 checks passed
@Danielius1922 Danielius1922 deleted the jkralik/fix/closing-tcp-session branch June 3, 2024 16:30
@github-actions github-actions bot locked and limited conversation to collaborators Jun 3, 2024
@jkralik
Copy link
Member Author

jkralik commented Jun 15, 2024

@coderabbitai create issue description from the PR description as the bug

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
OCF Conformance Testing OCF Conformance Testing required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Asynchronous TCP Session Closure Causes New Session to Close
2 participants