Skip to content

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

Merged
merged 2 commits into from
Jul 9, 2021

Conversation

twose
Copy link
Member

@twose twose commented Jul 7, 2021

The size of sockaddr_in and sockaddr_in6 is known on compile-time and it is not that big for the stack.

@twose twose force-pushed the sockaddr_malloc branch 2 times, most recently from cf1a979 to f923b27 Compare July 8, 2021 04:00
The size of sockaddr_in and sockaddr_in6 is known on compile-time and it is not that big for the stack.
@twose twose force-pushed the sockaddr_malloc branch from f923b27 to 36e1406 Compare July 8, 2021 05:38
switch (sa->sa_family) {
#if HAVE_GETADDRINFO && HAVE_IPV6
case AF_INET6:
((struct sockaddr_in6 *)sa)->sin6_family = sa->sa_family;
Copy link
Member Author

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;
Copy link
Member Author

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) {
Copy link
Member Author

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)) {
Copy link
Member Author

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

@twose
Copy link
Member Author

twose commented Jul 8, 2021

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.

If the error code returned indicates the connection attempt failed (that is, WSAECONNREFUSED, WSAENETUNREACH, WSAETIMEDOUT) the application can call connect again for the same socket. (https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect)

Copy link
Member

@nikic nikic left a comment

Choose a reason for hiding this comment

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

Looks reasonable

@twose twose force-pushed the sockaddr_malloc branch from ff3cc63 to 4aa74e6 Compare July 8, 2021 09:21
@twose twose merged commit 6086e17 into php:master Jul 9, 2021
@twose twose deleted the sockaddr_malloc branch July 9, 2021 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants