PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - main - php_network.h (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 19 20 95.0 %
Date: 2014-09-27 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Author: Stig Venaas <venaas@uninett.no>                              |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #ifndef _PHP_NETWORK_H
      22             : #define _PHP_NETWORK_H
      23             : 
      24             : #include <php.h>
      25             : 
      26             : #ifdef PHP_WIN32
      27             : # include "win32/inet.h"
      28             : #else
      29             : # undef closesocket
      30             : # define closesocket close
      31             : #endif
      32             : 
      33             : #ifndef HAVE_SHUTDOWN
      34             : #undef shutdown
      35             : #define shutdown(s,n)   /* nothing */
      36             : #endif
      37             : 
      38             : #ifdef PHP_WIN32
      39             : # ifdef EWOULDBLOCK
      40             : #  undef EWOULDBLOCK
      41             : # endif
      42             : # ifdef EINPROGRESS
      43             : #  undef EINPROGRESS
      44             : # endif
      45             : # define EWOULDBLOCK WSAEWOULDBLOCK
      46             : # define EINPROGRESS    WSAEWOULDBLOCK
      47             : # define fsync _commit
      48             : # define ftruncate(a, b) chsize(a, b)
      49             : #endif /* defined(PHP_WIN32) */
      50             : 
      51             : #ifndef EWOULDBLOCK
      52             : # define EWOULDBLOCK EAGAIN
      53             : #endif
      54             : 
      55             : #ifdef PHP_WIN32
      56             : #define php_socket_errno() WSAGetLastError()
      57             : #else
      58             : #define php_socket_errno() errno
      59             : #endif
      60             : 
      61             : /* like strerror, but caller must efree the returned string,
      62             :  * unless buf is not NULL.
      63             :  * Also works sensibly for win32 */
      64             : BEGIN_EXTERN_C()
      65             : PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize);
      66             : PHPAPI zend_string *php_socket_error_str(long err);
      67             : END_EXTERN_C()
      68             : 
      69             : #ifdef HAVE_NETINET_IN_H
      70             : # include <netinet/in.h>
      71             : #endif
      72             : 
      73             : #ifdef HAVE_SYS_SOCKET_H
      74             : #include <sys/socket.h>
      75             : #endif
      76             : 
      77             : /* These are here, rather than with the win32 counterparts above,
      78             :  * since <sys/socket.h> defines them. */
      79             : #ifndef SHUT_RD
      80             : # define SHUT_RD 0
      81             : # define SHUT_WR 1
      82             : # define SHUT_RDWR 2
      83             : #endif
      84             : 
      85             : #ifdef HAVE_SYS_TIME_H
      86             : #include <sys/time.h>
      87             : #endif
      88             : 
      89             : #ifdef HAVE_STDDEF_H
      90             : #include <stddef.h>
      91             : #endif
      92             : 
      93             : #ifdef PHP_WIN32
      94             : typedef SOCKET php_socket_t;
      95             : #else
      96             : typedef int php_socket_t;
      97             : #endif
      98             : 
      99             : #ifdef PHP_WIN32
     100             : # define SOCK_ERR INVALID_SOCKET
     101             : # define SOCK_CONN_ERR SOCKET_ERROR
     102             : # define SOCK_RECV_ERR SOCKET_ERROR
     103             : #else
     104             : # define SOCK_ERR -1
     105             : # define SOCK_CONN_ERR -1
     106             : # define SOCK_RECV_ERR -1
     107             : #endif
     108             : 
     109             : #define STREAM_SOCKOP_NONE         1 << 0
     110             : #define STREAM_SOCKOP_SO_REUSEPORT 1 << 1
     111             : #define STREAM_SOCKOP_SO_BROADCAST 1 << 2
     112             : 
     113             : 
     114             : /* uncomment this to debug poll(2) emulation on systems that have poll(2) */
     115             : /* #define PHP_USE_POLL_2_EMULATION 1 */
     116             : 
     117             : #if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
     118             : # include <sys/poll.h>
     119             : typedef struct pollfd php_pollfd;
     120             : #else
     121             : typedef struct _php_pollfd {
     122             :         php_socket_t fd;
     123             :         short events;
     124             :         short revents;
     125             : } php_pollfd;
     126             : 
     127             : PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout);
     128             : 
     129             : #ifndef POLLIN
     130             : # define POLLIN      0x0001    /* There is data to read */
     131             : # define POLLPRI     0x0002    /* There is urgent data to read */
     132             : # define POLLOUT     0x0004    /* Writing now will not block */
     133             : # define POLLERR     0x0008    /* Error condition */
     134             : # define POLLHUP     0x0010    /* Hung up */
     135             : # define POLLNVAL    0x0020    /* Invalid request: fd not open */
     136             : #endif
     137             : 
     138             : # ifndef PHP_USE_POLL_2_EMULATION
     139             : #  define PHP_USE_POLL_2_EMULATION 1
     140             : # endif
     141             : #endif
     142             : 
     143             : #define PHP_POLLREADABLE        (POLLIN|POLLERR|POLLHUP)
     144             : 
     145             : #ifndef PHP_USE_POLL_2_EMULATION
     146             : # define php_poll2(ufds, nfds, timeout)         poll(ufds, nfds, timeout)
     147             : #endif
     148             : 
     149             : /* timeval-to-timeout (for poll(2)) */
     150      124077 : static inline int php_tvtoto(struct timeval *timeouttv)
     151             : {
     152      124077 :         if (timeouttv) {
     153      124077 :                 return (timeouttv->tv_sec * 1000) + (timeouttv->tv_usec / 1000);
     154             :         }
     155           0 :         return -1;
     156             : }
     157             : 
     158             : /* hybrid select(2)/poll(2) for a single descriptor.
     159             :  * timeouttv follows same rules as select(2), but is reduced to millisecond accuracy.
     160             :  * Returns 0 on timeout, -1 on error, or the event mask (ala poll(2)).
     161             :  */
     162      124077 : static inline int php_pollfd_for(php_socket_t fd, int events, struct timeval *timeouttv)
     163             : {
     164             :         php_pollfd p;
     165             :         int n;
     166             : 
     167      124077 :         p.fd = fd;
     168      124077 :         p.events = events;
     169      124077 :         p.revents = 0;
     170             : 
     171      124077 :         n = php_poll2(&p, 1, php_tvtoto(timeouttv));
     172             : 
     173      124077 :         if (n > 0) {
     174      123965 :                 return p.revents;
     175             :         }
     176             : 
     177         112 :         return n;
     178             : }
     179             : 
     180         479 : static inline int php_pollfd_for_ms(php_socket_t fd, int events, int timeout)
     181             : {
     182             :         php_pollfd p;
     183             :         int n;
     184             : 
     185         479 :         p.fd = fd;
     186         479 :         p.events = events;
     187         479 :         p.revents = 0;
     188             : 
     189         479 :         n = php_poll2(&p, 1, timeout);
     190             : 
     191         479 :         if (n > 0) {
     192         478 :                 return p.revents;
     193             :         }
     194             : 
     195           1 :         return n;
     196             : }
     197             : 
     198             : /* emit warning and suggestion for unsafe select(2) usage */
     199             : PHPAPI void _php_emit_fd_setsize_warning(int max_fd);
     200             : 
     201             : #ifdef PHP_WIN32
     202             : /* it is safe to FD_SET too many fd's under win32; the macro will simply ignore
     203             :  * descriptors that go beyond the default FD_SETSIZE */
     204             : # define PHP_SAFE_FD_SET(fd, set)       FD_SET(fd, set)
     205             : # define PHP_SAFE_FD_CLR(fd, set)       FD_CLR(fd, set)
     206             : # define PHP_SAFE_FD_ISSET(fd, set)     FD_ISSET(fd, set)
     207             : # define PHP_SAFE_MAX_FD(m, n)          do { if (n + 1 >= FD_SETSIZE) { _php_emit_fd_setsize_warning(n); }} while(0)
     208             : #else
     209             : # define PHP_SAFE_FD_SET(fd, set)       do { if (fd < FD_SETSIZE) FD_SET(fd, set); } while(0)
     210             : # define PHP_SAFE_FD_CLR(fd, set)       do { if (fd < FD_SETSIZE) FD_CLR(fd, set); } while(0)
     211             : # define PHP_SAFE_FD_ISSET(fd, set)     ((fd < FD_SETSIZE) && FD_ISSET(fd, set))
     212             : # define PHP_SAFE_MAX_FD(m, n)          do { if (m >= FD_SETSIZE) { _php_emit_fd_setsize_warning(m); m = FD_SETSIZE - 1; }} while(0)
     213             : #endif
     214             : 
     215             : 
     216             : #define PHP_SOCK_CHUNK_SIZE     8192
     217             : 
     218             : #ifdef HAVE_SOCKADDR_STORAGE
     219             : typedef struct sockaddr_storage php_sockaddr_storage;
     220             : #else
     221             : typedef struct {
     222             : #ifdef HAVE_SOCKADDR_SA_LEN
     223             :                 unsigned char ss_len;
     224             :                 unsigned char ss_family;
     225             : #else
     226             :         unsigned short ss_family;
     227             : #endif
     228             :         char info[126];
     229             : } php_sockaddr_storage;
     230             : #endif
     231             : 
     232             : BEGIN_EXTERN_C()
     233             : PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, zend_string **error_string TSRMLS_DC);
     234             : PHPAPI void php_network_freeaddresses(struct sockaddr **sal);
     235             : 
     236             : PHPAPI php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
     237             :                 int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string,
     238             :                 int *error_code, char *bindto, unsigned short bindport, long sockopts
     239             :                 TSRMLS_DC);
     240             : 
     241             : PHPAPI int php_network_connect_socket(php_socket_t sockfd,
     242             :                 const struct sockaddr *addr,
     243             :                 socklen_t addrlen,
     244             :                 int asynchronous,
     245             :                 struct timeval *timeout,
     246             :                 zend_string **error_string,
     247             :                 int *error_code);
     248             : 
     249             : #define php_connect_nonb(sock, addr, addrlen, timeout) \
     250             :         php_network_connect_socket((sock), (addr), (addrlen), 0, (timeout), NULL, NULL)
     251             : 
     252             : PHPAPI php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port,
     253             :                 int socktype, long sockopts, zend_string **error_string, int *error_code
     254             :                 TSRMLS_DC);
     255             : 
     256             : PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock,
     257             :                 zend_string **textaddr,
     258             :                 struct sockaddr **addr,
     259             :                 socklen_t *addrlen,
     260             :                 struct timeval *timeout,
     261             :                 zend_string **error_string,
     262             :                 int *error_code
     263             :                 TSRMLS_DC);
     264             : 
     265             : PHPAPI int php_network_get_sock_name(php_socket_t sock, 
     266             :                 zend_string **textaddr,
     267             :                 struct sockaddr **addr,
     268             :                 socklen_t *addrlen
     269             :                 TSRMLS_DC);
     270             :         
     271             : PHPAPI int php_network_get_peer_name(php_socket_t sock, 
     272             :                 zend_string **textaddr,
     273             :                 struct sockaddr **addr,
     274             :                 socklen_t *addrlen
     275             :                 TSRMLS_DC);
     276             : 
     277             : PHPAPI void php_any_addr(int family, php_sockaddr_storage *addr, unsigned short port);
     278             : PHPAPI int php_sockaddr_size(php_sockaddr_storage *addr);
     279             : END_EXTERN_C()
     280             : 
     281             : struct _php_netstream_data_t    {
     282             :         php_socket_t socket;
     283             :         char is_blocked;
     284             :         struct timeval timeout;
     285             :         char timeout_event;
     286             :         size_t ownsize;
     287             : };
     288             : typedef struct _php_netstream_data_t php_netstream_data_t;
     289             : PHPAPI extern php_stream_ops php_stream_socket_ops;
     290             : extern php_stream_ops php_stream_generic_socket_ops;
     291             : #define PHP_STREAM_IS_SOCKET    (&php_stream_socket_ops)
     292             : 
     293             : BEGIN_EXTERN_C()
     294             : PHPAPI php_stream *_php_stream_sock_open_from_socket(php_socket_t socket, const char *persistent_id STREAMS_DC TSRMLS_DC );
     295             : /* open a connection to a host using php_hostconnect and return a stream */
     296             : PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port,
     297             :                 int socktype, struct timeval *timeout, const char *persistent_id STREAMS_DC TSRMLS_DC);
     298             : PHPAPI void php_network_populate_name_from_sockaddr(
     299             :                 /* input address */
     300             :                 struct sockaddr *sa, socklen_t sl,
     301             :                 /* output readable address */
     302             :                 zend_string **textaddr,
     303             :                 /* output address */
     304             :                 struct sockaddr **addr,
     305             :                 socklen_t *addrlen
     306             :                 TSRMLS_DC);
     307             : 
     308             : PHPAPI int php_network_parse_network_address_with_port(const char *addr,
     309             :                 zend_long addrlen, struct sockaddr *sa, socklen_t *sl TSRMLS_DC);
     310             : END_EXTERN_C()
     311             : 
     312             : #define php_stream_sock_open_from_socket(socket, persistent)    _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_CC TSRMLS_CC)
     313             : #define php_stream_sock_open_host(host, port, socktype, timeout, persistent)    _php_stream_sock_open_host((host), (port), (socktype), (timeout), (persistent) STREAMS_CC TSRMLS_CC)
     314             : 
     315             : /* {{{ memory debug */
     316             : #define php_stream_sock_open_from_socket_rel(socket, persistent)        _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_REL_CC TSRMLS_CC)
     317             : #define php_stream_sock_open_host_rel(host, port, socktype, timeout, persistent)        _php_stream_sock_open_host((host), (port), (socktype), (timeout), (persistent) STREAMS_REL_CC TSRMLS_CC)
     318             : #define php_stream_sock_open_unix_rel(path, pathlen, persistent, timeval)       _php_stream_sock_open_unix((path), (pathlen), (persistent), (timeval) STREAMS_REL_CC TSRMLS_CC)
     319             : 
     320             : /* }}} */
     321             : 
     322             : #endif /* _PHP_NETWORK_H */
     323             : 
     324             : /*
     325             :  * Local variables:
     326             :  * tab-width: 8
     327             :  * c-basic-offset: 8
     328             :  * End:
     329             :  */

Generated by: LCOV version 1.10

Generated at Sat, 27 Sep 2014 16:43:24 +0000 (4 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.