rfc:socket_getaddrinfo

PHP RFC: Implement 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 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.

Proposal

The implementation[1] I'm proposing implements 4 new functions. socket_addrinfo_lookup() will return an array of resources, with each resource corresponding to each returned addrinfo. 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 last, ``socket_addrinfo_explain`` is to convert the resource to an array for examination.

socket_addrinfo_lookup(string node[, mixed service, array hints]) : array
socket_addrinfo_connect(resource $addrinfo) : resource
socket_addrinfo_bind(resource $addrinfo) : resource
socket_addrinfo_explain(resource $addrinfo) : array

IPv4 Example

<?php
$addrinfo = socket_addrinfo_lookup('localhost', 2000, array('ai_family' => AF_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
 
var_dump(socket_addrinfo_explain($sockaddr));
/* Outputs:
array(5) {
  ["ai_flags"]=>
  int(0)
  ["ai_family"]=>
  int(2)
  ["ai_socktype"]=>
  int(1)
  ["ai_protocol"]=>
  int(6)
  ["ai_addr"]=>
  array(2) {
    ["sin_port"]=>
    int(2000)
    ["sin_addr"]=>
    string(9) "127.0.0.1"
  }
}
*/

IPv6 Example

<?php
$addrinfo = socket_addrinfo_lookup('localhost', 2000, array('ai_family' => AF_INET6, '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 [::1]:2000 using TCP/IP ready for reading
 
var_dump(socket_addrinfo_explain($sockaddr));
/* Outputs:
array(5) {
  ["ai_flags"]=>
  int(0)
  ["ai_family"]=>
  int(10)
  ["ai_socktype"]=>
  int(1)
  ["ai_protocol"]=>
  int(6)
  ["ai_addr"]=>
  array(2) {
    ["sin6_port"]=>
    int(2000)
    ["sin6_addr"]=>
    string(3) "::1"
  }
}
*/

Backward Incompatible Changes

New functionality; no BC issues.

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.

Proposed Voting Choices

Vote to implement the new functionality, would require a 2/3 majority.

Implement socket_getaddrinfo family
Real name Yes No
bishop (bishop)  
colinodell (colinodell)  
daverandom (daverandom)  
derick (derick)  
hywan (hywan)  
kguest (kguest)  
lstrojny (lstrojny)  
mariano (mariano)  
trowski (trowski)  
Final result: 9 0
This poll has been closed.

Vote Start: 2016-08-24 15:23 UTC

Vote End: 2016-08-31 23:59 UTC

Implementation

  1. merged into master (i.e. pre 7.2)
  2. a link to the PHP manual entry for the feature

References

rfc/socket_getaddrinfo.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1