|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 09:00:02 2025 UTC |
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; }