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

Replace usage addrinfo with Ip::Address in icmp4 #1789

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jtstrs
Copy link
Contributor

@jtstrs jtstrs commented Apr 18, 2024

Replace usage of addrinfo in icmp4 with more
appropriate way

@squid-prbot
Copy link
Collaborator

Can one of the admins verify this patch?

@jtstrs
Copy link
Contributor Author

jtstrs commented Apr 18, 2024

As we discussed, aim of this PR is try to remove usage of addrinfo.
Since Ip::Address, have only one field sockaddr_in6 and
dont have any virtual methods, inheritance stuff and another
things, which could force compiler to add any overhead, we
could assume, that Ip::Address is byte compatible with
addrinfo and could be used directly instead of it.
At least, I think thats true for this context with Icmp4


This pr related to #1671

@jtstrs
Copy link
Contributor Author

jtstrs commented Apr 18, 2024

Feel free to add some comments and suggestions, since, honestly, I've lack of knowledge and experience with such type things.

from->ai_addr,
&from->ai_addrlen);
reinterpret_cast<sockaddr*>(&from),
0);
Copy link
Contributor

Choose a reason for hiding this comment

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

The horrible casting aside, this change violates recvfrom() call intent (and possibly API) by supplying a nil pointer to address size (disguised as integer 0): "the caller should initialize [addrlen memory] before the call to the size of the buffer associated with src_addr"

To move this PR forward, I suggest:

  • Test changes before posting a PR (or PR updates).
  • Remove reinterpret_cast. If you need a sockaddr object, use a local sockaddr variable .
  • Read recvfrom() documentation and understand why that specific system call is used here.
  • Supply a non-nil addrlen pointer to recvfrom(). Use another local variable of the right type for this addrlen. The value of that variable will depend on how your solution provides the memory for the address, but it will not be zero.
  • Add a check for recvfrom() truncating the address, report an error if it did, and do not use the truncated address.
  • Do not use std::move() unless the code does not compile without it (its use is always a red flag in Squid code, but there are legitimate use cases, of course): It is difficult to use std::move() correctly, it is usually a bad idea to use it, and it is probably not going to be needed in this PR.

@rousskov rousskov added the S-waiting-for-author author action is expected (and usually required) label Apr 18, 2024
@rousskov rousskov marked this pull request as draft April 18, 2024 20:12
Copy link
Contributor

@yadij yadij left a comment

Choose a reason for hiding this comment

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

could assume, that Ip::Address is byte compatible with
addrinfo and could be used directly instead of it.

This assumption is false. IPv4 and IPv6 different in byte sizes. While the field layout of sockaddr group of structures matches, the address fields differ in size and format.

@@ -156,7 +156,7 @@ void
Icmp4::Recv(void)
{
int n;
struct addrinfo *from = nullptr;
Ip::Address from;
Copy link
Contributor

Choose a reason for hiding this comment

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

from should be of type sockaddr_storage and empty.

Suggested change
Ip::Address from;
sockaddr_storage from;
memset(&from, 0, sizeof(from));

Also, these lines should be down just before first-use, where the recvfrom() is called.

return;
}

preply.from = *from;
preply.from = std::move(from);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
preply.from = std::move(from);
preply.from = from;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-for-author author action is expected (and usually required)
Projects
None yet
4 participants