-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Remove unnecessary sockaddr memory allocation #7219
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
Conversation
cf1a979
to
f923b27
Compare
The size of sockaddr_in and sockaddr_in6 is known on compile-time and it is not that big for the stack.
switch (sa->sa_family) { | ||
#if HAVE_GETADDRINFO && HAVE_IPV6 | ||
case AF_INET6: | ||
((struct sockaddr_in6 *)sa)->sin6_family = sa->sa_family; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't seem to make sense, it is equal to a = a
, so I removed all of such code, correct me if I am wrong.
((struct sockaddr_in *)sa)->sin_port = htons(port); | ||
socklen = sizeof(struct sockaddr_in); | ||
break; | ||
default: | ||
/* Unknown family */ | ||
socklen = 0; | ||
sa = NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if sa
is NULL, it will close the socket and create a new socket in the next loop. We can check it first, then we create the socket.
main/network.c
Outdated
} | ||
} | ||
#endif | ||
|
||
if (!local_address || bind(sock, local_address, local_address_len)) { | ||
if (local_address.len == 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this makes len
more meaningful... And it also killed duplicated code of error report.
#ifdef HAVE_INET_PTON | ||
if (!inet_pton(AF_INET, bindto, &in4->sin_addr)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check ways of the two inet_pton
calls are not consistent, I think it might be better to judge whether it returns 1
Another optimization point, the socket can re-call connect after connect failed, so we need not re-create the socket if the address family did not change. But the manual does not stipulate such behavior, I only know that the windows documentation makes this clear.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks reasonable
The size of sockaddr_in and sockaddr_in6 is known on compile-time and it is not that big for the stack.