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

Properly format IPv6 addresses and return null for unknown addresses #14

Merged
merged 2 commits into from
Jan 23, 2017

Conversation

clue
Copy link
Member

@clue clue commented Jan 22, 2017

UDP6/IPv6 addresses should be returned as [::1]:12345 instead of the non-standard, ambiguous ::1:12345.

Refs reactphp/reactphp#199

@clue clue added the bug label Jan 22, 2017
if ($this->socket !== false) {
return stream_socket_get_name($this->socket, false);
}
return $this->sanitizeAddress(@stream_socket_get_name($this->socket, false));
Copy link
Member

Choose a reason for hiding this comment

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

Is there a specific reason for omitting the $this->socket !== false now?

Copy link
Member Author

Choose a reason for hiding this comment

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

There are several occasions where the stream_socket_get_name() call may emit a warning and unfortunately this is not reliable across different PHP versions and HHVM. Besides, the socket property can accessed from outside of this class so that the false check is simply not reliable either.

I've removed the check completely for consistency with the Socket component and simply discard any error messages from this call.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, my suggestion was not to remove @ from the stream_socket_get_name call again. I've been just curious if there are any consequences to potentially call stream_socket_get_name with false (or anything which isn't a resource).

Copy link
Member Author

Choose a reason for hiding this comment

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

This is all covered by the test suite and we now make sure to either return the correct address string or null if it's unknown. Here's how this works under the hood:

>>> $f = stream_socket_client('google.com:80');
=> stream resource #279
>>> stream_socket_get_name($f, true);
=> "216.58.205.238:80"
>>> fclose($f);
=> true
>>> @stream_socket_get_name($f, true);
PHP warning:  stream_socket_get_name(): supplied resource is not a valid stream resource on line 1
=> false
>>> @stream_socket_get_name(false, true);
PHP warning:  stream_socket_get_name() expects parameter 1 to be resource, boolean given on line 1
=> false

This uses a client socket for simplicity, the server socket emits similar errors but has far more error situations, e.g. the server socket does not have a remote address and returns false without emitting an error (unless you're on HHVM).

Copy link
Member

Choose a reason for hiding this comment

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

Yes, i'm aware that stream_socket_get_namewould return false for non-resources, but we're relying now on PHP triggering a warning.

I also noticed, that i looks like being more inconsistent as before.

Note, that i'm reviewing without too much knowledge about the internals, so my comments are not meant to block a merge.

Copy link
Member Author

Choose a reason for hiding this comment

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

No worries, I actually appreciate these kind of discussions :-)

This PR makes it so that we ignore any warnings (which are unreliable) and simply rely on the function returning false (which is documented behavior and works reliably across different PHP versions and HHVM). This is covered by both our test suite and the above gist.

Once this PR is in, the behavior is consistent throughout the Datagram component.

It is currently not consistent with regards to the Socket component. As you rightfully pointed out, the Socket component does currently not implement any checks and does not suppress any errors either. This means that this does in fact error out when one tries to access to remote address after the socket has been closed. See also reactphp/socket#19, for which I've prepared a PR that I'll file once reactphp/socket#61 is in 👍

Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member Author

Choose a reason for hiding this comment

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

For the reference, here's the promised follow-up PR: reactphp/socket#63 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants