php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63979 Get blocking state, domain, protocol of a socket
Submitted: 2013-01-12 19:48 UTC Modified: -
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: arrakaij at digitalinsanity dot de Assigned:
Status: Open Package: Sockets related
PHP Version: Irrelevant OS: Linux / Ubuntu 12.04
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: arrakaij at digitalinsanity dot de
New email:
PHP Version: OS:

 

 [2013-01-12 19:48 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-09-20 12:09 UTC] 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;

}
 [2014-09-20 12:26 UTC] metamarkers at gmail dot com
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 27 16:01:27 2024 UTC