Edit report at https://bugs.php.net/bug.php?id=72733&edit=1
ID: 72733
User updated by: email at davekok dot nl
Reported by: email at davekok dot nl
Summary: It would be nice to have something like getaddrinfo.
Status: Open
Type: Feature/Change Request
Package: Sockets related
Operating System: Any
PHP Version: 7.0.9
Block user comment: N
Private report: N
New Comment:
I would indeed prefer an array to be returned rather then a resource with a additional functions.
Otherwise great work!!
Previous Comments:
------------------------------------------------------------------------
[2016-08-07 19:41:00] dave at mudsite dot com
I took a stab at
this(https://github.com/bp1222/php-src/commit/785284cdbc8a46d7c1b07567bb0d06d348112684).
implementing a
array socket_getaddrinfo(string $node, mixed $service[, array $hints])
This attempt returns an array of resources, each resource being a C addrinfo struct. To use, I just
made a socket_addrinfo_connect and socket_addrinfo_bind to accept the resource to connect/bind.
Although in your desired example, you'd want to just have an array of info from addrinfo
returned, and you call create&connect/bind with returned variables.
Not sure if one would be preferable over the other, I would assume implementing your use would be
better as it wouldn't require resources & would be a couple less functions declared.
------------------------------------------------------------------------
[2016-08-07 07:19:16] email at davekok dot nl
Oh perhaps it would have been a better example if the socket type hint would also by in the
constructor.
------------------------------------------------------------------------
[2016-08-07 07:02:27] email at davekok dot nl
That is indeed the point. Mostly I only have a hostname, not an IP address and knowing whether a
hostname supports or requires IPv6 is hard to find out. It requires you to do your own DNS queries.
There is no gethostbyname for IPv6 in PHP.
A simple socket wrapper could look something like this with getaddrinfo.
<?php
class Socket {
private $address;
private $socket;
public function __construct(string $host, string $service) {
$addresses = socket_addrinfo($host, $service, [
"ai_family"=>PF_UNSPEC,
"ai_socktype"=>SOCK_STREAM
]);
if ($addresses === false) throw new Exception("host not found");
$this->address = reset($addresses);
$this->socket = socket_create(
$address["ai_family"],
$address["ai_socktype"],
$address["ai_protocol"]
);
if (!$this->socket) throw new Exception("error creating socket");
}
public function bind() {
socket_bind($this->socket, $this->address["ai_addr"]);
}
public function listen(int $backlog = 0) {
socket_listen($this->socket, $backlog);
}
public function connect() {
socket_connect($this->socket, $this->address["ai_addr"]);
}
// other socket stuff
}
As an additional benefit you don't even need to know the port number you can just use the
service name.
------------------------------------------------------------------------
[2016-08-07 04:12:58] kalle@php.net
Could you expand your idea with some code examples of what you do now and how you propose a solution
to visualize this some more?
------------------------------------------------------------------------
[2016-08-03 23:06:35] dave at mudsite dot com
I'm not certain I'm following what is hard about supporting both. socket_create()'s
first argument allows you to choose to have this socket be an AF_INET or AF_INET6. Unless
you're attempting to pull information about a host wherein you can get all addresses about a
host for a specific purpose a-la: http://www.logix.cz/michal/devel/various/getaddrinfo.c.xp
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=72733
--
Edit this bug report at https://bugs.php.net/bug.php?id=72733&edit=1