This is an old revision of the document!
PHP RFC: Implement socket_getaddrinfo()
- Version: 0.1
- Date: 2016-08-08
- Author: David Walker (dave@mudsite.com)
- Status: Draft
- First Published at: http://wiki.php.net/rfc/socket_getaddrinfo
Introduction
This RFC targets a reported feature request in #72733. The request is that PHP expose the C level function getaddrinfo(). This is a missing nice piece to the current socket library. When dealing with IP variable networks it would be beneficial to allow libc to tell us what methods of connecting/listening would be most appropriate given a set of hints.
Proposals
I'm starting this RFC with 2 proposed solutions, which are very similar.
Proposal 1
The implementation[1] I'm proposing implements 4 new socket_* functions. socket_getaddrinfo()
will return an array of resources, with each resource corresponding to each returned addrinfo sockaddr. The next two implemented methods socket_addrinfo_bind()
, and socket_addrinfo_connect()
, would be used to bind, or connect, to a given resource rather than make the user go through the process of creating the socket and connect/binding themselves. The fourth function is a free'r if you'd want to free the resources before end of script.
array socket_getaddrinfo(string node[, mixed service, array hints]) resource socket_addrinfo_connect(sockaddrresource sockaddr) resource socket_addrinfo_bind(sockaddrresource sockaddr) null socket_addrinfo_close(sockaddrresource sockaddr)
<?php $addrinfo = socket_getaddrinfo('127.0.0.1', 2000, array('ai_family' => AI_INET, 'ai_socktype' => SOCK_STREAM)); $sockaddr = reset($addrinfo); if (!$sockaddr) die ("No Valid Socket Types"); $sock = socket_addrinfo_bind($sockaddr); // ^^ $sock is a socket resource that is bound to 127.0.0.1:2000 using TCP/IP ready for reading foreach ($addrinfo as $sockaddr) { socket_addrinfo_close($sockaddr); }
Proposal 2
This attempt [2] is very similar to the first, excep, I change socket_create()
and socket_bind()
to accept one argument from two, IF, that argument is our addrinfo resource. If it is our addrinfo resourece the functions, rather than a boolean return will return the resource to the bound/connected socket resource.
array socket_getaddrinfo(string node[, mixed service, array hints]) mixed socket_connect(resource sockaddr[, string address, int port = 0]) mixed socket_bind(resource sockaddr[, string address, int port = 0]) null socket_addrinfo_close(sockaddrresource sockaddr)
<?php $addrinfo = socket_getaddrinfo('127.0.0.1', 2000, array('ai_family' => AI_INET, 'ai_socktype' => SOCK_STREAM)); $sockaddr = reset($addrinfo); if (!$sockaddr) die ("No Valid Socket Types"); $sock = socket_bind($sockaddr); // ^^ $sock is a socket resource that is bound to 127.0.0.1:2000 using TCP/IP ready for reading foreach ($addrinfo as $sockaddr) { socket_addrinfo_close($sockaddr); }
Backward Incompatible Changes
New functionality; no BC problems here.
Proposed PHP Version(s)
Next PHP 7.x (currently 7.2)
RFC Impact
To SAPIs
None
To Existing Extensions
Socket acquired new functionality
To Opcache
None
New Constants
Exposing the AI_* family of constants.
Open Issues
The bug reporter provided desired code wherein getaddrinfo function would return an array-of-arrays that contain the ai_* info. User would then be liable for issuing the scket_create & socket_(bind|connect). I don't like adding more functions than necessary
Proposed Voting Choices
Vote to implement the new functions, would require a 2/3 majority.