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

refactor!: switch to sockaddr_storage, IPv6 and resolver improvements #1140

Merged
merged 4 commits into from
Jul 3, 2023

Conversation

Tachi107
Copy link
Member

@Tachi107 Tachi107 commented Jun 28, 2023

Using a struct sockaddr_storage allows us to get rid of the spooky union of sockaddr_in and sockaddr_in6, and to do IPv4 and IPv6 as recommended by RFC 3493.

The code is not that cleaner, but it should be more robust and less "dangerous". As discussed recently on IRC, Kip and I were quite puzzled by the various constructors of class IP, as it wasn't immediatly clear why they were memcpying INET_ADDRSTRLEN bytes into a smaller s_addr array.

Needless to say, this is a breaking change. I also removed a few now useless functions, but they could be kept if desired.

Note: this is still experimental and incomplete.

@Tachi107 Tachi107 marked this pull request as draft June 28, 2023 09:35
@codecov-commenter
Copy link

codecov-commenter commented Jun 28, 2023

Codecov Report

Patch coverage: 75.80% and project coverage change: -0.39 ⚠️

Comparison is base (91952a8) 78.60% compared to head (090e6a9) 78.22%.

❗ Current head 090e6a9 differs from pull request most recent head 0eb0cda. Consider uploading reports for the commit 0eb0cda to get more accurate results

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1140      +/-   ##
==========================================
- Coverage   78.60%   78.22%   -0.39%     
==========================================
  Files          53       53              
  Lines        6872     6852      -20     
==========================================
- Hits         5402     5360      -42     
- Misses       1470     1492      +22     
Impacted Files Coverage Δ
include/pistache/client.h 96.77% <ø> (ø)
include/pistache/net.h 84.61% <ø> (ø)
src/client/client.cc 83.35% <0.00%> (-0.93%) ⬇️
src/server/listener.cc 70.21% <58.33%> (-0.74%) ⬇️
src/common/http_header.cc 92.64% <66.66%> (-0.75%) ⬇️
src/common/net.cc 83.05% <84.94%> (-4.59%) ⬇️
include/pistache/http_header.h 90.62% <100.00%> (ø)

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@Tachi107
Copy link
Member Author

Yeah there are a lot of sizeofs without parentheses, I'll change them later

@Tachi107
Copy link
Member Author

@kiplingw please keep this PR in draft status until I'm confident to let you merge it. I'm not done yet :)

@Tachi107 Tachi107 marked this pull request as draft June 28, 2023 19:28
@Tachi107 Tachi107 force-pushed the sockaddr-storage branch 2 times, most recently from dfd0848 to d519ba9 Compare June 30, 2023 19:14
@Tachi107
Copy link
Member Author

Hey @dennisjenkins75 and @kiplingw, could you please have a look at at the changes? You can find all the relevant descriptions in the various commits.

I'm not completely done yet, I'd like to reorder the commit history, for instance.

@kiplingw kiplingw marked this pull request as ready for review June 30, 2023 19:28
@kiplingw
Copy link
Member

Sure @Tachi107. Will review when back at my desk.

@dennisjenkins75
Copy link
Collaborator

:( I have only ever written unix sockets code the old way. I was unaware of the newer APIs. I have no problems with the code cleanup so long as it passes all unit tests.

@kiplingw
Copy link
Member

From what I understood of it, it looks fine to me. Good work @Tachi107. Let us know when you are ready and I will re-review before merging. Main thing is, as @dennisjenkins75 said, don't break CI.

@Tachi107 Tachi107 changed the title refactor!: switch to sockaddr_storage refactor!: switch to sockaddr_storage, IPv6 improvements Jul 3, 2023
@Tachi107 Tachi107 changed the title refactor!: switch to sockaddr_storage, IPv6 improvements refactor!: switch to sockaddr_storage, IPv6 and resolver improvements Jul 3, 2023
@Tachi107
Copy link
Member Author

Tachi107 commented Jul 3, 2023

The commit history is now neatly separated in three independent commits with a somewhat exhaustive explanation about the relevant changes, and I've also added a couple more small tests. Lastly, I bumped the version to 0.2.0, since this patch introduces new features.

The commitlint and abidiff failures are expected.

@kiplingw I'll patiently wait for your re-review and merge :)

Using a struct sockaddr_storage allows us to get rid of the spooky union
of sockaddr_in and sockaddr_in6 contained in the IP class, and to do
IPv4 and IPv6 as recommended by RFC 3493.

The code is not that cleaner, but it should be more robust and less
"dangerous". As discussed recently on IRC, Kip and I were quite puzzled
by the various constructors of class IP, as it wasn't immediatly clear
why they were memcpying INET_ADDRSTRLEN bytes into a smaller s_addr
array.

Needless to say, this is a breaking change. It is impossible to change
this without breaking the ABI, and while at it I touched the API as well
to clean it up a bit.
Before this commit, Pistache didn't properly support "true" IPv6
addresses, but only IPv6 "host-uri", i.e. a common IPv6 representation
used in URIs and protocols like HTTP.

With these changes, the AddressParser now understands bare IPv6
addresses in addition to "host-uri" ones, e.g. you can now pass "::1" in
addition to "[::1]" to Address' constructor when you only want to
specify an IPv6 address without port.

Consequently, all the functions which return the textual representation
of IPv6 addresses, below the application level (HTTP), now return bare
IPv6 addresses, without brackets. Functions which return textual IPv6 +
port pairs, return the recommended textual IPv6 form, i.e.
[address]:port, which is the same as the URI format.
In this commit I've massively refactored the init() function of the
Address class. It used to carry out most of the address parsing on its
own with a lot of custom code, making a lot of IPv4 vs IPv6 differences
and special casing. It now makes use of the library functions
standardized by RFC 3493, and most of the heavy lifting is done by
getaddrinfo().

This refactor has the following niceties:

1. Less complex code, less chances of bugs.
2. Hooking into getaddrinfo() makes Address capable of accepting
   symbolic textual service names in addition to numeric ports, so that
   users can now create an Address object like Address("*:http")
3. The loopback and wildcard address are now resolved by the system
   resolver as well, hardcoding less and yielding more desireable
   results when, for example, a system is configured to operate in IPv4
   or IPv6 only mode. Since addresses like "localhost" are now resolved
   by the system, it is possible that "::1" is now returned as well as
   "127.0.0.1", while Pistache would previously only return the latter.
   This is a change in behaviour.

The new functionalities described in points 2 and 3 are covered by new
minimal unit tests.
@kiplingw
Copy link
Member

kiplingw commented Jul 3, 2023

@Tachi107, BSD sockets aren't my strong point because it's been years since I had to directly use them. But from what I can understand it looks fine to me. One question I have for you though is did you try running through valgrind to see if there's any buffer overflows or other memory corruption issues before and after the changes?

@Tachi107
Copy link
Member Author

Tachi107 commented Jul 3, 2023 via email

@kiplingw kiplingw merged commit 8729275 into master Jul 3, 2023
38 of 40 checks passed
@kiplingw
Copy link
Member

kiplingw commented Jul 3, 2023 via email

@Tachi107 Tachi107 deleted the sockaddr-storage branch July 5, 2023 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants