Skip to content

sock/async: add function to retrieve session object of current DTLS event#15755

Merged
miri64 merged 4 commits intoRIOT-OS:masterfrom
janosbrodbeck:pr/dtls/get_event_session
Jan 22, 2021
Merged

sock/async: add function to retrieve session object of current DTLS event#15755
miri64 merged 4 commits intoRIOT-OS:masterfrom
janosbrodbeck:pr/dtls/get_event_session

Conversation

@janosbrodbeck
Copy link
Copy Markdown
Member

@janosbrodbeck janosbrodbeck commented Jan 12, 2021

Contribution description

This PR adds a new function to sock async: sock_dtls_get_event_session() to retrieve the session object within a DTLS event. Currently it is not possible to get this session for every event type (e.g. SOCK_ASYNC_CONN_FIN, SOCK_ASYNC_CONN_RDY). It should work for every possible event type. The issue was described in #15517.

I didn't get it to work properly with just a pointer in the socket structure, since the session needed to be stored somewhere and storing it was not possible within the _event() function of tinydtls. So I placed it right away in the socket.

Not sure whether I placed the new function correctly.

Testing procedure

Implementation in tinyDTLS can be tested with pkg_tinydtls_sock_async

  • Flash and run on two devices (e.g. native)
  • Execute dtlss on one of the devices
  • Execute dtlsc {ip of other node} {some data} on the other device

Issues/PRs references

Fixes #15517

@janosbrodbeck
Copy link
Copy Markdown
Member Author

I have also added a very small doc fix commit. The documentation of sock_dtls_set_cb wrote something about getting something but it just sets stuff. So I've changed Get to Set. But can drop this commit if unwanted or wrong.

@janosbrodbeck janosbrodbeck added Area: network Area: Networking Area: pkg Area: External package ports Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care. labels Jan 12, 2021
@janosbrodbeck janosbrodbeck changed the title Pr/dtls/get event session sock/async: add function to retrieve session object of current DTLS event Jan 12, 2021
@janosbrodbeck janosbrodbeck force-pushed the pr/dtls/get_event_session branch 2 times, most recently from e593e51 to 4db4112 Compare January 12, 2021 18:54
@pokgak
Copy link
Copy Markdown
Contributor

pokgak commented Jan 14, 2021

(Is there something like a spoiler or collapse tag?)

You can use the HTML <details></details> tag:

DetailsHIDE ME

@janosbrodbeck janosbrodbeck added the State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet label Jan 14, 2021
@janosbrodbeck
Copy link
Copy Markdown
Member Author

Pushed the discussed changes. Now, the event session is only set for SOCK_ASYNC_CONN_READY and SOCK_ASYNC_CONN_FIN. Added this info into the note section of the function documentation. Thought about putting this info into a warning section, but that may be a bit overkill.

Did not adjust the provided test procedure. You can see the expected behaviour in the test. For the SOCK_ASYNC_CONN_RECV event the retrieved session is still the previously set session or empty if it's the first.

@janosbrodbeck janosbrodbeck removed the State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet label Jan 18, 2021
@janosbrodbeck
Copy link
Copy Markdown
Member Author

janosbrodbeck commented Jan 18, 2021

Also adjusted the line length of the function head to comply the 80 character recommendation.

@pokgak
Copy link
Copy Markdown
Contributor

pokgak commented Jan 19, 2021

LGTM. Can you also include the patch (only CONN_RECV and CONN_FIN part) as commit to show usage of sock_dtls_get_event_session in the DTLS handler so that it will at least get tested the next time someone run the test?

Comment on lines +40 to +42
* @note Only available with @ref SOCK_HAS_ASYNC defined.
* Should only be called within a DTLS event and only for the event
* types @ref SOCK_ASYNC_CONN_RDY and @ref SOCK_ASYNC_CONN_FIN.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Mhhh maybe add a return value, that signals to the caller when the the session is available or not? I know this makes the implementation more complicated than it is now, but sock is an end-user API that is supposed to be easy to use. Could be boolean or an errno-based status code

Copy link
Copy Markdown
Member Author

@janosbrodbeck janosbrodbeck Jan 19, 2021

Choose a reason for hiding this comment

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

Totally forgot, sorry. Squashed the undo directly with the related commit.

Regarding the return value. It would be nice to have it...but how? The only idea I have for this currently is to also add (e.g.) a boolean to the socket object and set this boolean when an event occurs. So setting it for the CONN_RDY, CONN_FIN to available and for every other event to not available. Would that be sufficient or do you imagine it differently?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In the specific case of tinydtls you could use the size member of the session_t struct as a signifier if the session is set.

typedef struct {
  socklen_t size;		/**< size of addr */
  union {
    struct sockaddr     sa;
    struct sockaddr_storage st;
    struct sockaddr_in  sin;
    struct sockaddr_in6 sin6;
  } addr;
  uint8_t ifindex;
} session_t;

The object needs to zero'd of course. However, I would do this on read and socket init. This way, if you e.g. receive a CONN_RDY and a MSG_RECV in the same callback you don't accidentally remove the session before it can be read.

Copy link
Copy Markdown
Member Author

@janosbrodbeck janosbrodbeck Jan 21, 2021

Choose a reason for hiding this comment

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

Done :) Thank you! I've adjusted the test procedure to test that the session is not available for other event types.

@janosbrodbeck janosbrodbeck force-pushed the pr/dtls/get_event_session branch 2 times, most recently from 76167fc to 69b265a Compare January 21, 2021 01:39
@janosbrodbeck
Copy link
Copy Markdown
Member Author

janosbrodbeck commented Jan 21, 2021

LGTM. Can you also include the patch (only CONN_RECV and CONN_FIN part) as commit to show usage of sock_dtls_get_event_session in the DTLS handler so that it will at least get tested the next time someone run the test?

I've added the usage of the new function to the test. Are you okay with it the way it is now?

@janosbrodbeck janosbrodbeck force-pushed the pr/dtls/get_event_session branch 2 times, most recently from 2dc3423 to 6cb35f1 Compare January 21, 2021 02:22
sock_dtls_session_t session = { 0 };

if (type & SOCK_ASYNC_CONN_RECV) {
puts("Session handshake received");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here and in the other event types that are not SOCK_ASYNC_CONN_FIN or SOCK_ASYNC_CONN_RDY (as well in the client), you could also use expect(!sock_dtls_get_event_session(sock, &session)) to make this proper tests ;-).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done! In the event types SOCK_ASYNC_CONN_FIN and SOCK_ASYNC_CONN_RDY we could also use expect(sock_dtls....) to test it since currently it is only printed and not tested. Would that be too much to also add it there? Would like to keep the session printing, because it is currently the only example usage of this new function.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would like to keep the session printing, because it is currently the only example usage of this new function.

Yes, as you print output, that provides enough result (more than expect which does not print anything when succeeding)

sock_dtls_session_get_udp_ep(&session, &ep);

printf("Session became ready: ");
print_ipv6_addr_with_port(ep.addr.ipv6, sizeof(ep.addr), ep.port);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Any reason why you not use sock_udp_ep_fmt() from sys/include/net/sock/util.h here? Seems like re-inventing the wheel.

Copy link
Copy Markdown
Member Author

@janosbrodbeck janosbrodbeck Jan 21, 2021

Choose a reason for hiding this comment

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

I just knew the format function when the address is a ipv6_addr_t object but didn't see one for an endpoint. Was not my intention to reinvent, just did not found a proper function. I'll change it. Way nicer to have a proper function for it.

@miri64
Copy link
Copy Markdown
Member

miri64 commented Jan 21, 2021

One last comment. Other than that: I tested everything and am fine with most changes (apart from the one mentioned above). You may squash directly.

@janosbrodbeck janosbrodbeck force-pushed the pr/dtls/get_event_session branch from 85e1bea to 7d08c05 Compare January 21, 2021 18:17
@janosbrodbeck janosbrodbeck added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jan 21, 2021
@janosbrodbeck
Copy link
Copy Markdown
Member Author

Addressed last comment and squashed. Test uses sock_udp_ep_fmt() now for printing.

@janosbrodbeck janosbrodbeck force-pushed the pr/dtls/get_event_session branch from 7d08c05 to 328f0c9 Compare January 21, 2021 18:41
@janosbrodbeck janosbrodbeck force-pushed the pr/dtls/get_event_session branch from 328f0c9 to c65b4d3 Compare January 21, 2021 18:43
Copy link
Copy Markdown
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

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

ACK. Re-tested and everything works as expected. :-)

@janosbrodbeck
Copy link
Copy Markdown
Member Author

Thanks for the help and review @miri64 and @pokgak !

@miri64 miri64 merged commit 879fbc1 into RIOT-OS:master Jan 22, 2021
@janosbrodbeck janosbrodbeck deleted the pr/dtls/get_event_session branch February 8, 2021 16:24
@kaspar030 kaspar030 added this to the Release 2021.04 milestone Apr 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: network Area: Networking Area: pkg Area: External package ports CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pkg/tinydtls: async event SOCK_ASYNC_CONN_FIN does not indicate which session has been finished

4 participants