Back to index

php5  5.3.10
php_network.h
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Stig Venaas <venaas@uninett.no>                              |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: php_network.h 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #ifndef _PHP_NETWORK_H
00022 #define _PHP_NETWORK_H
00023 
00024 #ifdef PHP_WIN32
00025 # include "win32/inet.h"
00026 #else
00027 # undef closesocket
00028 # define closesocket close
00029 #endif
00030 
00031 #ifndef HAVE_SHUTDOWN
00032 #undef shutdown
00033 #define shutdown(s,n)       /* nothing */
00034 #endif
00035 
00036 #ifdef PHP_WIN32
00037 # ifdef EWOULDBLOCK
00038 #  undef EWOULDBLOCK
00039 # endif
00040 # ifdef EINPROGRESS
00041 #  undef EINPROGRESS
00042 # endif
00043 # define EWOULDBLOCK WSAEWOULDBLOCK
00044 # define EINPROGRESS WSAEWOULDBLOCK
00045 # define fsync _commit
00046 # define ftruncate(a, b) chsize(a, b)
00047 #endif /* defined(PHP_WIN32) */
00048 
00049 #ifndef EWOULDBLOCK
00050 # define EWOULDBLOCK EAGAIN
00051 #endif
00052 
00053 #ifdef PHP_WIN32
00054 #define php_socket_errno() WSAGetLastError()
00055 #else
00056 #define php_socket_errno() errno
00057 #endif
00058 
00059 /* like strerror, but caller must efree the returned string,
00060  * unless buf is not NULL.
00061  * Also works sensibly for win32 */
00062 BEGIN_EXTERN_C()
00063 PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize);
00064 END_EXTERN_C()
00065 
00066 #ifdef HAVE_NETINET_IN_H
00067 # include <netinet/in.h>
00068 #endif
00069 
00070 #ifdef HAVE_SYS_SOCKET_H
00071 #include <sys/socket.h>
00072 #endif
00073 
00074 /* These are here, rather than with the win32 counterparts above,
00075  * since <sys/socket.h> defines them. */
00076 #ifndef SHUT_RD
00077 # define SHUT_RD 0
00078 # define SHUT_WR 1
00079 # define SHUT_RDWR 2
00080 #endif
00081 
00082 #ifdef HAVE_SYS_TIME_H
00083 #include <sys/time.h>
00084 #endif
00085 
00086 #ifdef HAVE_STDDEF_H
00087 #include <stddef.h>
00088 #endif
00089 
00090 #ifdef PHP_WIN32
00091 typedef SOCKET php_socket_t;
00092 #else
00093 typedef int php_socket_t;
00094 #endif
00095 
00096 #ifdef PHP_WIN32
00097 # define SOCK_ERR INVALID_SOCKET
00098 # define SOCK_CONN_ERR SOCKET_ERROR
00099 # define SOCK_RECV_ERR SOCKET_ERROR
00100 #else
00101 # define SOCK_ERR -1
00102 # define SOCK_CONN_ERR -1
00103 # define SOCK_RECV_ERR -1
00104 #endif
00105 
00106 /* uncomment this to debug poll(2) emulation on systems that have poll(2) */
00107 /* #define PHP_USE_POLL_2_EMULATION 1 */
00108 
00109 #if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
00110 # include <sys/poll.h>
00111 typedef struct pollfd php_pollfd;
00112 #else
00113 typedef struct _php_pollfd {
00114        php_socket_t fd;
00115        short events;
00116        short revents;
00117 } php_pollfd;
00118 
00119 PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout);
00120 
00121 #ifndef POLLIN
00122 # define POLLIN      0x0001    /* There is data to read */
00123 # define POLLPRI     0x0002    /* There is urgent data to read */
00124 # define POLLOUT     0x0004    /* Writing now will not block */
00125 # define POLLERR     0x0008    /* Error condition */
00126 # define POLLHUP     0x0010    /* Hung up */
00127 # define POLLNVAL    0x0020    /* Invalid request: fd not open */
00128 #endif
00129 
00130 # ifndef PHP_USE_POLL_2_EMULATION
00131 #  define PHP_USE_POLL_2_EMULATION 1
00132 # endif
00133 #endif
00134 
00135 #define PHP_POLLREADABLE    (POLLIN|POLLERR|POLLHUP)
00136 
00137 #ifndef PHP_USE_POLL_2_EMULATION
00138 # define php_poll2(ufds, nfds, timeout)          poll(ufds, nfds, timeout)
00139 #endif
00140 
00141 /* timeval-to-timeout (for poll(2)) */
00142 static inline int php_tvtoto(struct timeval *timeouttv)
00143 {
00144        if (timeouttv) {
00145               return (timeouttv->tv_sec * 1000) + (timeouttv->tv_usec / 1000);
00146        }
00147        return -1;
00148 }
00149 
00150 /* hybrid select(2)/poll(2) for a single descriptor.
00151  * timeouttv follows same rules as select(2), but is reduced to millisecond accuracy.
00152  * Returns 0 on timeout, -1 on error, or the event mask (ala poll(2)).
00153  */
00154 static inline int php_pollfd_for(php_socket_t fd, int events, struct timeval *timeouttv)
00155 {
00156        php_pollfd p;
00157        int n;
00158 
00159        p.fd = fd;
00160        p.events = events;
00161        p.revents = 0;
00162 
00163        n = php_poll2(&p, 1, php_tvtoto(timeouttv));
00164 
00165        if (n > 0) {
00166               return p.revents;
00167        }
00168 
00169        return n;
00170 }
00171 
00172 static inline int php_pollfd_for_ms(php_socket_t fd, int events, int timeout)
00173 {
00174        php_pollfd p;
00175        int n;
00176 
00177        p.fd = fd;
00178        p.events = events;
00179        p.revents = 0;
00180 
00181        n = php_poll2(&p, 1, timeout);
00182 
00183        if (n > 0) {
00184               return p.revents;
00185        }
00186 
00187        return n;
00188 }
00189 
00190 /* emit warning and suggestion for unsafe select(2) usage */
00191 PHPAPI void _php_emit_fd_setsize_warning(int max_fd);
00192 
00193 #ifdef PHP_WIN32
00194 /* it is safe to FD_SET too many fd's under win32; the macro will simply ignore
00195  * descriptors that go beyond the default FD_SETSIZE */
00196 # define PHP_SAFE_FD_SET(fd, set)  FD_SET(fd, set)
00197 # define PHP_SAFE_FD_ISSET(fd, set)       FD_ISSET(fd, set)
00198 # define PHP_SAFE_MAX_FD(m, n)            do { if (n + 1 >= FD_SETSIZE) { _php_emit_fd_setsize_warning(n); }} while(0)
00199 #else
00200 # define PHP_SAFE_FD_SET(fd, set)  do { if (fd < FD_SETSIZE) FD_SET(fd, set); } while(0)
00201 # define PHP_SAFE_FD_ISSET(fd, set)       ((fd < FD_SETSIZE) && FD_ISSET(fd, set))
00202 # define PHP_SAFE_MAX_FD(m, n)            do { if (m >= FD_SETSIZE) { _php_emit_fd_setsize_warning(m); m = FD_SETSIZE - 1; }} while(0)
00203 #endif
00204 
00205 
00206 #define PHP_SOCK_CHUNK_SIZE 8192
00207 
00208 #ifdef HAVE_SOCKADDR_STORAGE
00209 typedef struct sockaddr_storage php_sockaddr_storage;
00210 #else
00211 typedef struct {
00212 #ifdef HAVE_SOCKADDR_SA_LEN
00213               unsigned char ss_len;
00214               unsigned char ss_family;
00215 #else
00216         unsigned short ss_family;
00217 #endif
00218         char info[126];
00219 } php_sockaddr_storage;
00220 #endif
00221 
00222 BEGIN_EXTERN_C()
00223 PHPAPI php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
00224               int socktype, int asynchronous, struct timeval *timeout, char **error_string,
00225               int *error_code, char *bindto, unsigned short bindport 
00226               TSRMLS_DC);
00227 
00228 PHPAPI int php_network_connect_socket(php_socket_t sockfd,
00229               const struct sockaddr *addr,
00230               socklen_t addrlen,
00231               int asynchronous,
00232               struct timeval *timeout,
00233               char **error_string,
00234               int *error_code);
00235 
00236 #define php_connect_nonb(sock, addr, addrlen, timeout) \
00237        php_network_connect_socket((sock), (addr), (addrlen), 0, (timeout), NULL, NULL)
00238 
00239 PHPAPI php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port,
00240               int socktype, char **error_string, int *error_code
00241               TSRMLS_DC);
00242 
00243 PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock,
00244               char **textaddr, long *textaddrlen,
00245               struct sockaddr **addr,
00246               socklen_t *addrlen,
00247               struct timeval *timeout,
00248               char **error_string,
00249               int *error_code
00250               TSRMLS_DC);
00251 
00252 PHPAPI int php_network_get_sock_name(php_socket_t sock, 
00253               char **textaddr, long *textaddrlen,
00254               struct sockaddr **addr,
00255               socklen_t *addrlen
00256               TSRMLS_DC);
00257        
00258 PHPAPI int php_network_get_peer_name(php_socket_t sock, 
00259               char **textaddr, long *textaddrlen,
00260               struct sockaddr **addr,
00261               socklen_t *addrlen
00262               TSRMLS_DC);
00263 
00264 PHPAPI void php_any_addr(int family, php_sockaddr_storage *addr, unsigned short port);
00265 PHPAPI int php_sockaddr_size(php_sockaddr_storage *addr);
00266 END_EXTERN_C()
00267 
00268 struct _php_netstream_data_t       {
00269        php_socket_t socket;
00270        char is_blocked;
00271        struct timeval timeout;
00272        char timeout_event;
00273        size_t ownsize;
00274 };
00275 typedef struct _php_netstream_data_t php_netstream_data_t;
00276 PHPAPI extern php_stream_ops php_stream_socket_ops;
00277 extern php_stream_ops php_stream_generic_socket_ops;
00278 #define PHP_STREAM_IS_SOCKET       (&php_stream_socket_ops)
00279 
00280 BEGIN_EXTERN_C()
00281 PHPAPI php_stream *_php_stream_sock_open_from_socket(php_socket_t socket, const char *persistent_id STREAMS_DC TSRMLS_DC );
00282 /* open a connection to a host using php_hostconnect and return a stream */
00283 PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port,
00284               int socktype, struct timeval *timeout, const char *persistent_id STREAMS_DC TSRMLS_DC);
00285 PHPAPI void php_network_populate_name_from_sockaddr(
00286               /* input address */
00287               struct sockaddr *sa, socklen_t sl,
00288               /* output readable address */
00289               char **textaddr, long *textaddrlen,
00290               /* output address */
00291               struct sockaddr **addr,
00292               socklen_t *addrlen
00293               TSRMLS_DC);
00294 
00295 PHPAPI int php_network_parse_network_address_with_port(const char *addr,
00296               long addrlen, struct sockaddr *sa, socklen_t *sl TSRMLS_DC);
00297 END_EXTERN_C()
00298 
00299 #define php_stream_sock_open_from_socket(socket, persistent)   _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_CC TSRMLS_CC)
00300 #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)
00301 
00302 /* {{{ memory debug */
00303 #define php_stream_sock_open_from_socket_rel(socket, persistent)      _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_REL_CC TSRMLS_CC)
00304 #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)
00305 #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)
00306 
00307 /* }}} */
00308 
00309 #endif /* _PHP_NETWORK_H */
00310 
00311 /*
00312  * Local variables:
00313  * tab-width: 8
00314  * c-basic-offset: 8
00315  * End:
00316  */