Req #63979 [Com]: Get blocking state, domain, protocol of a socket
| From: | metamarkers at gmail dot com | Date: | Sat, 20 Sep 2014 12:26:53 +0000 |
| Subject: | Req #63979 [Com]: Get blocking state, domain, protocol of a socket | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-187623@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=63979&edit=1
ID: 63979
Comment by: metamarkers at gmail dot com
Reported by: arrakaij at digitalinsanity dot de
Summary: Get blocking state, domain, protocol of a socket
Status: Open
Type: Feature/Change Request
Package: Sockets related
Operating System: Linux / Ubuntu 12.04
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
error_get_last() actually returns an associative array. oops.
replace relevant lines in my example with:
$error = error_get_last();
$errstr = $error['message'];
you get the idea
Previous Comments:
------------------------------------------------------------------------
[2014-09-20 12:09:46] metamarkers at gmail dot com
This has been open for almost 2 years, still not fixed in 5.5
I'll see about submitting a pull request.
Here is a workaround.
/**
* @param resource $socket
* @return int|false
*/
function socket_getdomain($socket) {
$address = null;
// this is the only function that tells us anything
if (!@socket_getsockname($socket,$address)) {
// bad socket
if (socket_last_error($socket)) return false;
// retrieve domain from E_WARNING
list($errno,$errstr) = error_get_last();
@preg_match('/address family (\d+)$/',$errstr,$errmatch);
return intval($errmatch[1]);
}
elseif (filter_var(
$address,
FILTER_VALIDATE_IP,
FILTER_FLAG_IPV4
)) return AF_INET;
elseif (filter_var(
$address,
FILTER_VALIDATE_IP,
FILTER_FLAG_IPV6
)) return AF_INET6;
elseif (file_exists($address)) return AF_UNIX;
else return false;
}
------------------------------------------------------------------------
[2013-01-12 19:48:01] arrakaij at digitalinsanity dot de
Description:
------------
I am writing a oop socket wrapper for sockets in php (5.3+) and am missing some functions (names are
examples):
bool socket_get_blocking($socket);
/* Return whether a socket is blocking or not. streams do have this function, however, sockets do
not. this information is available in php_socket struct, but there is no way to get this
information. would be required for better error handling, allowing/disallowing things etc */
(following function could be written as one, returning an array)
int socket_get_domain($socket);
/* returning AF_INET, AF_INET6, AF_UNIX and so on, whichever i gave the socket_create() command */
int socket_get_protocol($socket);
/* returning SOL_TCP, SOL_UDP and so on, whichever i gave the socket_create() command */
int socket_get_type($socket);
/* returning SOCK_STREAM, SOCK_DGRAM and so on, whichever i gave the socket_create() command */
On C-Side there is e.g. the struct sockaddr that contains the domain. So it should be no problem to
adapt those information into PHP.
There is a workaround getting those last three informations, however, it is very hard to find and
not safe, as #defines can change. see below
Test script:
---------------
//Workaround for getting domain, type and family of an socket
$socketProtocol = socket_get_option($socket, 1, 38); //SOL_TCP, ...
$socketType = socket_get_option($socket, SOL_SOCKET, SO_TYPE); //SOCK_STREAM, ...
$socketDomain = socket_get_option($socket, 1, 39); //AF_INET,...
Expected result:
----------------
(easy) possibility to get information that are provided to sockets in php
Actual result:
--------------
for the informations mentioned, there are no functions available
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63979&edit=1