Back to index

php5  5.3.10
Classes | Defines | Typedefs | Functions | Variables
php_network.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  _php_pollfd
struct  php_sockaddr_storage
struct  _php_netstream_data_t

Defines

#define closesocket   close
#define shutdown(s, n)   /* nothing */
#define EWOULDBLOCK   EAGAIN
#define php_socket_errno()   errno
#define SHUT_RD   0
#define SHUT_WR   1
#define SHUT_RDWR   2
#define SOCK_ERR   -1
#define SOCK_CONN_ERR   -1
#define SOCK_RECV_ERR   -1
#define POLLIN   0x0001 /* There is data to read */
#define POLLPRI   0x0002 /* There is urgent data to read */
#define POLLOUT   0x0004 /* Writing now will not block */
#define POLLERR   0x0008 /* Error condition */
#define POLLHUP   0x0010 /* Hung up */
#define POLLNVAL   0x0020 /* Invalid request: fd not open */
#define PHP_USE_POLL_2_EMULATION   1
#define PHP_POLLREADABLE   (POLLIN|POLLERR|POLLHUP)
#define PHP_SAFE_FD_SET(fd, set)   do { if (fd < FD_SETSIZE) FD_SET(fd, set); } while(0)
#define PHP_SAFE_FD_ISSET(fd, set)   ((fd < FD_SETSIZE) && FD_ISSET(fd, set))
#define PHP_SAFE_MAX_FD(m, n)   do { if (m >= FD_SETSIZE) { _php_emit_fd_setsize_warning(m); m = FD_SETSIZE - 1; }} while(0)
#define PHP_SOCK_CHUNK_SIZE   8192
#define php_connect_nonb(sock, addr, addrlen, timeout)   php_network_connect_socket((sock), (addr), (addrlen), 0, (timeout), NULL, NULL)
#define PHP_STREAM_IS_SOCKET   (&php_stream_socket_ops)
#define php_stream_sock_open_from_socket(socket, persistent)   _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_CC TSRMLS_CC)
#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)
#define php_stream_sock_open_from_socket_rel(socket, persistent)   _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_REL_CC TSRMLS_CC)
#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)
#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)

Typedefs

typedef int php_socket_t
typedef struct _php_pollfd php_pollfd
typedef struct _php_netstream_data_t

Functions

PHPAPI char * php_socket_strerror (long err, char *buf, size_t bufsize)
PHPAPI int php_poll2 (php_pollfd *ufds, unsigned int nfds, int timeout)
static int php_tvtoto (struct timeval *timeouttv)
static int php_pollfd_for (php_socket_t fd, int events, struct timeval *timeouttv)
static int php_pollfd_for_ms (php_socket_t fd, int events, int timeout)
PHPAPI void _php_emit_fd_setsize_warning (int max_fd)
PHPAPI php_socket_t php_network_connect_socket_to_host (const char *host, unsigned short port, int socktype, int asynchronous, struct timeval *timeout, char **error_string, int *error_code, char *bindto, unsigned short bindport TSRMLS_DC)
PHPAPI int php_network_connect_socket (php_socket_t sockfd, const struct sockaddr *addr, socklen_t addrlen, int asynchronous, struct timeval *timeout, char **error_string, int *error_code)
PHPAPI php_socket_t php_network_bind_socket_to_local_addr (const char *host, unsigned port, int socktype, char **error_string, int *error_code TSRMLS_DC)
PHPAPI php_socket_t php_network_accept_incoming (php_socket_t srvsock, char **textaddr, long *textaddrlen, struct sockaddr **addr, socklen_t *addrlen, struct timeval *timeout, char **error_string, int *error_code TSRMLS_DC)
PHPAPI int php_network_get_sock_name (php_socket_t sock, char **textaddr, long *textaddrlen, struct sockaddr **addr, socklen_t *addrlen TSRMLS_DC)
PHPAPI int php_network_get_peer_name (php_socket_t sock, char **textaddr, long *textaddrlen, struct sockaddr **addr, socklen_t *addrlen TSRMLS_DC)
PHPAPI void php_any_addr (int family, php_sockaddr_storage *addr, unsigned short port)
PHPAPI int php_sockaddr_size (php_sockaddr_storage *addr)
PHPAPI php_stream * _php_stream_sock_open_from_socket (php_socket_t socket, const char *persistent_id STREAMS_DC TSRMLS_DC)
PHPAPI php_stream * _php_stream_sock_open_host (const char *host, unsigned short port, int socktype, struct timeval *timeout, const char *persistent_id STREAMS_DC TSRMLS_DC)
PHPAPI void php_network_populate_name_from_sockaddr (struct sockaddr *sa, socklen_t sl, char **textaddr, long *textaddrlen, struct sockaddr **addr, socklen_t *addrlen TSRMLS_DC)
PHPAPI int php_network_parse_network_address_with_port (const char *addr, long addrlen, struct sockaddr *sa, socklen_t *sl TSRMLS_DC)

Variables

PHPAPI php_stream_ops php_stream_socket_ops
php_stream_ops php_stream_generic_socket_ops

Class Documentation

struct _php_pollfd

Definition at line 113 of file php_network.h.

Class Members
short events
php_socket_t fd
short revents
struct php_sockaddr_storage

Definition at line 211 of file php_network.h.

Class Members
char info
unsigned short ss_family
struct _php_netstream_data_t

Definition at line 268 of file php_network.h.

Class Members
char is_blocked
size_t ownsize
php_socket_t socket
char timeout_event

Define Documentation

#define closesocket   close

Definition at line 28 of file php_network.h.

#define EWOULDBLOCK   EAGAIN

Definition at line 50 of file php_network.h.

#define php_connect_nonb (   sock,
  addr,
  addrlen,
  timeout 
)    php_network_connect_socket((sock), (addr), (addrlen), 0, (timeout), NULL, NULL)

Definition at line 236 of file php_network.h.

Definition at line 135 of file php_network.h.

#define PHP_SAFE_FD_ISSET (   fd,
  set 
)    ((fd < FD_SETSIZE) && FD_ISSET(fd, set))

Definition at line 201 of file php_network.h.

#define PHP_SAFE_FD_SET (   fd,
  set 
)    do { if (fd < FD_SETSIZE) FD_SET(fd, set); } while(0)

Definition at line 200 of file php_network.h.

#define PHP_SAFE_MAX_FD (   m,
  n 
)    do { if (m >= FD_SETSIZE) { _php_emit_fd_setsize_warning(m); m = FD_SETSIZE - 1; }} while(0)

Definition at line 202 of file php_network.h.

#define PHP_SOCK_CHUNK_SIZE   8192

Definition at line 206 of file php_network.h.

#define php_socket_errno ( )    errno

Definition at line 56 of file php_network.h.

Definition at line 278 of file php_network.h.

#define php_stream_sock_open_from_socket (   socket,
  persistent 
)    _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_CC TSRMLS_CC)

Definition at line 299 of file php_network.h.

#define php_stream_sock_open_from_socket_rel (   socket,
  persistent 
)    _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_REL_CC TSRMLS_CC)

Definition at line 303 of file php_network.h.

#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)

Definition at line 300 of file php_network.h.

#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)

Definition at line 304 of file php_network.h.

#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)

Definition at line 305 of file php_network.h.

#define PHP_USE_POLL_2_EMULATION   1

Definition at line 131 of file php_network.h.

#define POLLERR   0x0008 /* Error condition */

Definition at line 125 of file php_network.h.

#define POLLHUP   0x0010 /* Hung up */

Definition at line 126 of file php_network.h.

#define POLLIN   0x0001 /* There is data to read */

Definition at line 122 of file php_network.h.

#define POLLNVAL   0x0020 /* Invalid request: fd not open */

Definition at line 127 of file php_network.h.

#define POLLOUT   0x0004 /* Writing now will not block */

Definition at line 124 of file php_network.h.

#define POLLPRI   0x0002 /* There is urgent data to read */

Definition at line 123 of file php_network.h.

#define SHUT_RD   0

Definition at line 77 of file php_network.h.

#define SHUT_RDWR   2

Definition at line 79 of file php_network.h.

#define SHUT_WR   1

Definition at line 78 of file php_network.h.

#define shutdown (   s,
  n 
)    /* nothing */

Definition at line 33 of file php_network.h.

#define SOCK_CONN_ERR   -1

Definition at line 102 of file php_network.h.

#define SOCK_ERR   -1

Definition at line 101 of file php_network.h.

#define SOCK_RECV_ERR   -1

Definition at line 103 of file php_network.h.


Typedef Documentation

typedef struct _php_netstream_data_t

Definition at line 275 of file php_network.h.

typedef struct _php_pollfd php_pollfd
typedef int php_socket_t

Definition at line 93 of file php_network.h.


Function Documentation

Definition at line 1105 of file network.c.

{
       TSRMLS_FETCH();

#ifdef PHP_WIN32
       php_error_docref(NULL TSRMLS_CC, E_WARNING,
              "PHP needs to be recompiled with a larger value of FD_SETSIZE.\n"
              "If this binary is from an official www.php.net package, file a bug report\n"
              "at http://bugs.php.net, including the following information:\n"
              "FD_SETSIZE=%d, but you are using %d.\n"
              " --enable-fd-setsize=%d is recommended, but you may want to set it\n"
              "to match to maximum number of sockets each script will work with at\n"
              "one time, in order to avoid seeing this error again at a later date.",
              FD_SETSIZE, max_fd, (max_fd + 128) & ~127);
#else
       php_error_docref(NULL TSRMLS_CC, E_WARNING,
              "You MUST recompile PHP with a larger value of FD_SETSIZE.\n"
              "It is set to %d, but you have descriptors numbered at least as high as %d.\n"
              " --enable-fd-setsize=%d is recommended, but you may want to set it\n"
              "to equal the maximum number of open files supported by your system,\n"
              "in order to avoid seeing this error again at a later date.",
              FD_SETSIZE, max_fd, (max_fd + 1024) & ~1023);
#endif
}
PHPAPI php_stream* _php_stream_sock_open_from_socket ( php_socket_t  socket,
const char *persistent_id STREAMS_DC  TSRMLS_DC 
)

Definition at line 1028 of file network.c.

{
       php_stream *stream;
       php_netstream_data_t *sock;

       sock = pemalloc(sizeof(php_netstream_data_t), persistent_id ? 1 : 0);
       memset(sock, 0, sizeof(php_netstream_data_t));

       sock->is_blocked = 1;
       sock->timeout.tv_sec = FG(default_socket_timeout);
       sock->timeout.tv_usec = 0;
       sock->socket = socket;

       stream = php_stream_alloc_rel(&php_stream_generic_socket_ops, sock, persistent_id, "r+");

       if (stream == NULL) {
              pefree(sock, persistent_id ? 1 : 0);
       } else {
              stream->flags |= PHP_STREAM_FLAG_AVOID_BLOCKING;
       }

       return stream;
}
PHPAPI php_stream* _php_stream_sock_open_host ( const char *  host,
unsigned short  port,
int  socktype,
struct timeval *  timeout,
const char *persistent_id STREAMS_DC  TSRMLS_DC 
)

Definition at line 1052 of file network.c.

{
       char *res;
       long reslen;
       php_stream *stream;

       reslen = spprintf(&res, 0, "tcp://%s:%d", host, port);

       stream = php_stream_xport_create(res, reslen, ENFORCE_SAFE_MODE | REPORT_ERRORS,
                     STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, persistent_id, timeout, NULL, NULL, NULL);

       efree(res);

       return stream;
}

Here is the call graph for this function:

PHPAPI void php_any_addr ( int  family,
php_sockaddr_storage addr,
unsigned short  port 
)

Definition at line 930 of file network.c.

{
       memset(addr, 0, sizeof(php_sockaddr_storage));
       switch (family) {
#if HAVE_IPV6
       case AF_INET6: {
              struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
              sin6->sin6_family = AF_INET6;
              sin6->sin6_port = htons(port);
              sin6->sin6_addr = in6addr_any;
              break;
       }
#endif
       case AF_INET: {
              struct sockaddr_in *sin = (struct sockaddr_in *) addr;
              sin->sin_family = AF_INET;
              sin->sin_port = htons(port);
              sin->sin_addr.s_addr = htonl(INADDR_ANY);
              break;
       }
       }
}
PHPAPI php_socket_t php_network_accept_incoming ( php_socket_t  srvsock,
char **  textaddr,
long *  textaddrlen,
struct sockaddr **  addr,
socklen_t addrlen,
struct timeval *  timeout,
char **  error_string,
int *error_code  TSRMLS_DC 
)

Definition at line 705 of file network.c.

{
       php_socket_t clisock = -1;
       int error = 0, n;
       php_sockaddr_storage sa;
       socklen_t sl;

       n = php_pollfd_for(srvsock, PHP_POLLREADABLE, timeout);

       if (n == 0) {
              error = PHP_TIMEOUT_ERROR_VALUE;
       } else if (n == -1) {
              error = php_socket_errno();
       } else {
              sl = sizeof(sa);

              clisock = accept(srvsock, (struct sockaddr*)&sa, &sl);

              if (clisock != SOCK_ERR) {
                     php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl,
                                   textaddr, textaddrlen,
                                   addr, addrlen
                                   TSRMLS_CC);
              } else {
                     error = php_socket_errno();
              }
       }

       if (error_code) {
              *error_code = error;
       }
       if (error_string) {
              *error_string = php_socket_strerror(error, NULL, 0);
       }

       return clisock;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHPAPI php_socket_t php_network_bind_socket_to_local_addr ( const char *  host,
unsigned  port,
int  socktype,
char **  error_string,
int *error_code  TSRMLS_DC 
)

Definition at line 415 of file network.c.

{
       int num_addrs, n, err = 0;
       php_socket_t sock;
       struct sockaddr **sal, **psal, *sa;
       socklen_t socklen;

       num_addrs = php_network_getaddresses(host, socktype, &psal, error_string TSRMLS_CC);

       if (num_addrs == 0) {
              /* could not resolve address(es) */
              return -1;
       }

       for (sal = psal; *sal != NULL; sal++) {
              sa = *sal;

              /* create a socket for this address */
              sock = socket(sa->sa_family, socktype, 0);

              if (sock == SOCK_ERR) {
                     continue;
              }

              switch (sa->sa_family) {
#if HAVE_GETADDRINFO && HAVE_IPV6
                     case AF_INET6:
                            ((struct sockaddr_in6 *)sa)->sin6_family = sa->sa_family;
                            ((struct sockaddr_in6 *)sa)->sin6_port = htons(port);
                            socklen = sizeof(struct sockaddr_in6);
                            break;
#endif
                     case AF_INET:
                            ((struct sockaddr_in *)sa)->sin_family = sa->sa_family;
                            ((struct sockaddr_in *)sa)->sin_port = htons(port);
                            socklen = sizeof(struct sockaddr_in);
                            break;
                     default:
                            /* Unknown family */
                            socklen = 0;
                            sa = NULL;
              }

              if (sa) {
                     /* attempt to bind */

#ifdef SO_REUSEADDR
                     {
                            int val = 1;
                            setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&val, sizeof(val));
                     }
#endif

                     n = bind(sock, sa, socklen);

                     if (n != SOCK_CONN_ERR) {
                            goto bound;
                     }

                     err = php_socket_errno();
              }

              closesocket(sock);
       }
       sock = -1;

       if (error_code) {
              *error_code = err;
       }
       if (error_string) {
              *error_string = php_socket_strerror(err, NULL, 0);
       }

bound:

       php_network_freeaddresses(psal);

       return sock;

}

Here is the call graph for this function:

Here is the caller graph for this function:

PHPAPI int php_network_connect_socket ( php_socket_t  sockfd,
const struct sockaddr *  addr,
socklen_t  addrlen,
int  asynchronous,
struct timeval *  timeout,
char **  error_string,
int error_code 
)

Definition at line 300 of file network.c.

{
#if HAVE_NON_BLOCKING_CONNECT
       php_non_blocking_flags_t orig_flags;
       int n;
       int error = 0;
       socklen_t len;
       int ret = 0;

       SET_SOCKET_BLOCKING_MODE(sockfd, orig_flags);

       if ((n = connect(sockfd, addr, addrlen)) != 0) {
              error = php_socket_errno();

              if (error_code) {
                     *error_code = error;
              }

              if (error != EINPROGRESS) {
                     if (error_string) {
                            *error_string = php_socket_strerror(error, NULL, 0);
                     }

                     return -1;
              }
              if (asynchronous && error == EINPROGRESS) {
                     /* this is fine by us */
                     return 0;
              }
       }

       if (n == 0) {
              goto ok;
       }
# ifdef PHP_WIN32
       /* The documentation for connect() says in case of non-blocking connections
        * the select function reports success in the writefds set and failure in
        * the exceptfds set. Indeed, using PHP_POLLREADABLE results in select
        * failing only due to the timeout and not immediately as would be
        * expected when a connection is actively refused. This way,
        * php_pollfd_for will return a mask with POLLOUT if the connection
        * is successful and with POLLPRI otherwise. */
       if ((n = php_pollfd_for(sockfd, POLLOUT|POLLPRI, timeout)) == 0) {
#else
       if ((n = php_pollfd_for(sockfd, PHP_POLLREADABLE|POLLOUT, timeout)) == 0) {
#endif
              error = PHP_TIMEOUT_ERROR_VALUE;
       }

       if (n > 0) {
              len = sizeof(error);
              /*
                 BSD-derived systems set errno correctly
                 Solaris returns -1 from getsockopt in case of error
                 */
              if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (char*)&error, &len) != 0) {
                     ret = -1;
              }
       } else {
              /* whoops: sockfd has disappeared */
              ret = -1;
       }

ok:
       if (!asynchronous) {
              /* back to blocking mode */
              RESTORE_SOCKET_BLOCKING_MODE(sockfd, orig_flags);
       }

       if (error_code) {
              *error_code = error;
       }

       if (error) {
              ret = -1;
              if (error_string) {
                     *error_string = php_socket_strerror(error, NULL, 0);
              }
       }
       return ret;
#else
       if (asynchronous) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Asynchronous connect() not supported on this platform");
       }
       return (connect(sockfd, addr, addrlen) == 0) ? 0 : -1;
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHPAPI php_socket_t php_network_connect_socket_to_host ( const char *  host,
unsigned short  port,
int  socktype,
int  asynchronous,
struct timeval *  timeout,
char **  error_string,
int error_code,
char *  bindto,
unsigned short bindport  TSRMLS_DC 
)

Definition at line 760 of file network.c.

{
       int num_addrs, n, fatal = 0;
       php_socket_t sock;
       struct sockaddr **sal, **psal, *sa;
       struct timeval working_timeout;
       socklen_t socklen;
#if HAVE_GETTIMEOFDAY
       struct timeval limit_time, time_now;
#endif

       num_addrs = php_network_getaddresses(host, socktype, &psal, error_string TSRMLS_CC);

       if (num_addrs == 0) {
              /* could not resolve address(es) */
              return -1;
       }

       if (timeout) {
              memcpy(&working_timeout, timeout, sizeof(working_timeout));
#if HAVE_GETTIMEOFDAY
              gettimeofday(&limit_time, NULL);
              limit_time.tv_sec += working_timeout.tv_sec;
              limit_time.tv_usec += working_timeout.tv_usec;
              if (limit_time.tv_usec >= 1000000) {
                     limit_time.tv_usec -= 1000000;
                     limit_time.tv_sec++;
              }
#endif
       }

       for (sal = psal; !fatal && *sal != NULL; sal++) {
              sa = *sal;

              /* create a socket for this address */
              sock = socket(sa->sa_family, socktype, 0);

              if (sock == SOCK_ERR) {
                     continue;
              }

              switch (sa->sa_family) {
#if HAVE_GETADDRINFO && HAVE_IPV6
                     case AF_INET6:
                            if (!bindto || strchr(bindto, ':')) {
                                   ((struct sockaddr_in6 *)sa)->sin6_family = sa->sa_family;
                                   ((struct sockaddr_in6 *)sa)->sin6_port = htons(port);
                                   socklen = sizeof(struct sockaddr_in6);
                            } else {
                                   socklen = 0;
                                   sa = NULL;
                            }
                            break;
#endif
                     case AF_INET:
                            ((struct sockaddr_in *)sa)->sin_family = sa->sa_family;
                            ((struct sockaddr_in *)sa)->sin_port = htons(port);
                            socklen = sizeof(struct sockaddr_in);
                            break;
                     default:
                            /* Unknown family */
                            socklen = 0;
                            sa = NULL;
              }

              if (sa) {
                     /* make a connection attempt */

                     if (bindto) {
                            struct sockaddr *local_address = NULL;
                            int local_address_len = 0;

                            if (sa->sa_family == AF_INET) {
                                   struct sockaddr_in *in4 = emalloc(sizeof(struct sockaddr_in));

                                   local_address = (struct sockaddr*)in4;
                                   local_address_len = sizeof(struct sockaddr_in);

                                   in4->sin_family = sa->sa_family;
                                   in4->sin_port = htons(bindport);
                                   if (!inet_aton(bindto, &in4->sin_addr)) {
                                          php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid IP Address: %s", bindto);
                                          goto skip_bind;
                                   }
                                   memset(&(in4->sin_zero), 0, sizeof(in4->sin_zero));
                            }
#if HAVE_IPV6 && HAVE_INET_PTON
                             else { /* IPV6 */
                                   struct sockaddr_in6 *in6 = emalloc(sizeof(struct sockaddr_in6));

                                   local_address = (struct sockaddr*)in6;
                                   local_address_len = sizeof(struct sockaddr_in6);

                                   in6->sin6_family = sa->sa_family;
                                   in6->sin6_port = htons(bindport);
                                   if (inet_pton(AF_INET6, bindto, &in6->sin6_addr) < 1) {
                                          php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid IP Address: %s", bindto);
                                          goto skip_bind;
                                   }
                            }
#endif
                            if (!local_address || bind(sock, local_address, local_address_len)) {
                                   php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to bind to '%s:%d', system said: %s", bindto, bindport, strerror(errno));
                            }
skip_bind:
                            if (local_address) {
                                   efree(local_address);
                            }
                     }
                     /* free error string recieved during previous iteration (if any) */
                     if (error_string && *error_string) {
                            efree(*error_string);
                            *error_string = NULL;
                     }

                     n = php_network_connect_socket(sock, sa, socklen, asynchronous,
                                   timeout ? &working_timeout : NULL,
                                   error_string, error_code);

                     if (n != -1) {
                            goto connected;
                     }

                     /* adjust timeout for next attempt */
#if HAVE_GETTIMEOFDAY
                     if (timeout) {
                            gettimeofday(&time_now, NULL);

                            if (timercmp(&time_now, &limit_time, >=)) {
                                   /* time limit expired; don't attempt any further connections */
                                   fatal = 1;
                            } else {
                                   /* work out remaining time */
                                   sub_times(limit_time, time_now, &working_timeout);
                            }
                     }
#else
                     if (error_code && *error_code == PHP_TIMEOUT_ERROR_VALUE) {
                            /* Don't even bother trying to connect to the next alternative;
                             * we have no way to determine how long we have already taken
                             * and it is quite likely that the next attempt will fail too. */
                            fatal = 1;
                     } else {
                            /* re-use the same initial timeout.
                             * Not the best thing, but in practice it should be good-enough */
                            if (timeout) {
                                   memcpy(&working_timeout, timeout, sizeof(working_timeout));
                            }
                     }
#endif
              }

              closesocket(sock);
       }
       sock = -1;

connected:

       php_network_freeaddresses(psal);

       return sock;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHPAPI int php_network_get_peer_name ( php_socket_t  sock,
char **  textaddr,
long *  textaddrlen,
struct sockaddr **  addr,
socklen_t *addrlen  TSRMLS_DC 
)

Definition at line 654 of file network.c.

{
       php_sockaddr_storage sa;
       socklen_t sl = sizeof(sa);
       memset(&sa, 0, sizeof(sa));

       if (getpeername(sock, (struct sockaddr*)&sa, &sl) == 0) {
              php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl,
                            textaddr, textaddrlen,
                            addr, addrlen
                            TSRMLS_CC);
              return 0;
       }
       return -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHPAPI int php_network_get_sock_name ( php_socket_t  sock,
char **  textaddr,
long *  textaddrlen,
struct sockaddr **  addr,
socklen_t *addrlen  TSRMLS_DC 
)

Definition at line 674 of file network.c.

{
       php_sockaddr_storage sa;
       socklen_t sl = sizeof(sa);
       memset(&sa, 0, sizeof(sa));

       if (getsockname(sock, (struct sockaddr*)&sa, &sl) == 0) {
              php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl,
                            textaddr, textaddrlen,
                            addr, addrlen
                            TSRMLS_CC);
              return 0;
       }
       return -1;

}

Here is the call graph for this function:

Here is the caller graph for this function:

PHPAPI int php_network_parse_network_address_with_port ( const char *  addr,
long  addrlen,
struct sockaddr *  sa,
socklen_t *sl  TSRMLS_DC 
)

Definition at line 499 of file network.c.

{
       char *colon;
       char *tmp;
       int ret = FAILURE;
       short port;
       struct sockaddr_in *in4 = (struct sockaddr_in*)sa;
       struct sockaddr **psal;
       int n;
       char *errstr = NULL;
#if HAVE_IPV6
       struct sockaddr_in6 *in6 = (struct sockaddr_in6*)sa;
#endif

       if (*addr == '[') {
              colon = memchr(addr + 1, ']', addrlen-1);
              if (!colon || colon[1] != ':') {
                     return FAILURE;
              }
              port = atoi(colon + 2);
              addr++;
       } else {
              colon = memchr(addr, ':', addrlen);
              if (!colon) {
                     return FAILURE;
              }
              port = atoi(colon + 1);
       }

       tmp = estrndup(addr, colon - addr);

       /* first, try interpreting the address as a numeric address */

#if HAVE_IPV6 && HAVE_INET_PTON
       if (inet_pton(AF_INET6, tmp, &in6->sin6_addr) > 0) {
              in6->sin6_port = htons(port);
              in6->sin6_family = AF_INET6;
              *sl = sizeof(struct sockaddr_in6);
              ret = SUCCESS;
              goto out;
       }
#endif
       if (inet_aton(tmp, &in4->sin_addr) > 0) {
              in4->sin_port = htons(port);
              in4->sin_family = AF_INET;
              *sl = sizeof(struct sockaddr_in);
              ret = SUCCESS;
              goto out;
       }

       /* looks like we'll need to resolve it */
       n = php_network_getaddresses(tmp, SOCK_DGRAM, &psal, &errstr TSRMLS_CC);

       if (n == 0) {
              if (errstr) {
                     php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to resolve `%s': %s", tmp, errstr);
                     STR_FREE(errstr);
              }
              goto out;
       }

       /* copy the details from the first item */
       switch ((*psal)->sa_family) {
#if HAVE_GETADDRINFO && HAVE_IPV6
              case AF_INET6:
                     *in6 = **(struct sockaddr_in6**)psal;
                     in6->sin6_port = htons(port);
                     *sl = sizeof(struct sockaddr_in6);
                     ret = SUCCESS;
                     break;
#endif
              case AF_INET:
                     *in4 = **(struct sockaddr_in**)psal;
                     in4->sin_port = htons(port);
                     *sl = sizeof(struct sockaddr_in);
                     ret = SUCCESS;
                     break;
       }

       php_network_freeaddresses(psal);

out:
       STR_FREE(tmp);
       return ret;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHPAPI void php_network_populate_name_from_sockaddr ( struct sockaddr *  sa,
socklen_t  sl,
char **  textaddr,
long *  textaddrlen,
struct sockaddr **  addr,
socklen_t *addrlen  TSRMLS_DC 
)

Definition at line 586 of file network.c.

{
       if (addr) {
              *addr = emalloc(sl);
              memcpy(*addr, sa, sl);
              *addrlen = sl;
       }

       if (textaddr) {
#if HAVE_IPV6 && HAVE_INET_NTOP
              char abuf[256];
#endif
              char *buf = NULL;

              switch (sa->sa_family) {
                     case AF_INET:
                            /* generally not thread safe, but it *is* thread safe under win32 */
                            buf = inet_ntoa(((struct sockaddr_in*)sa)->sin_addr);
                            if (buf) {
                                   *textaddrlen = spprintf(textaddr, 0, "%s:%d",
                                          buf, ntohs(((struct sockaddr_in*)sa)->sin_port));
                            }

                            break;

#if HAVE_IPV6 && HAVE_INET_NTOP
                     case AF_INET6:
                            buf = (char*)inet_ntop(sa->sa_family, &((struct sockaddr_in6*)sa)->sin6_addr, (char *)&abuf, sizeof(abuf));
                            if (buf) {
                                   *textaddrlen = spprintf(textaddr, 0, "%s:%d",
                                          buf, ntohs(((struct sockaddr_in6*)sa)->sin6_port));
                            }

                            break;
#endif
#ifdef AF_UNIX
                     case AF_UNIX:
                            {
                                   struct sockaddr_un *ua = (struct sockaddr_un*)sa;

                                   if (ua->sun_path[0] == '\0') {
                                          /* abstract name */
                                          int len = strlen(ua->sun_path + 1) + 1;
                                          *textaddrlen = len;
                                          *textaddr = emalloc(len + 1);
                                          memcpy(*textaddr, ua->sun_path, len);
                                          (*textaddr)[len] = '\0';
                                   } else {
                                          *textaddrlen = strlen(ua->sun_path);
                                          *textaddr = estrndup(ua->sun_path, *textaddrlen);
                                   }
                            }
                            break;
#endif

              }

       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHPAPI int php_poll2 ( php_pollfd ufds,
unsigned int  nfds,
int  timeout 
)

Definition at line 1134 of file network.c.

{
       fd_set rset, wset, eset;
       php_socket_t max_fd = SOCK_ERR;
       unsigned int i;
       int n;
       struct timeval tv;

       /* check the highest numbered descriptor */
       for (i = 0; i < nfds; i++) {
              if (ufds[i].fd > max_fd)
                     max_fd = ufds[i].fd;
       }

       PHP_SAFE_MAX_FD(max_fd, nfds + 1);

       FD_ZERO(&rset);
       FD_ZERO(&wset);
       FD_ZERO(&eset);

       for (i = 0; i < nfds; i++) {
              if (ufds[i].events & PHP_POLLREADABLE) {
                     PHP_SAFE_FD_SET(ufds[i].fd, &rset);
              }
              if (ufds[i].events & POLLOUT) {
                     PHP_SAFE_FD_SET(ufds[i].fd, &wset);
              }
              if (ufds[i].events & POLLPRI) {
                     PHP_SAFE_FD_SET(ufds[i].fd, &eset);
              }
       }

       if (timeout >= 0) {
              tv.tv_sec = timeout / 1000;
              tv.tv_usec = (timeout - (tv.tv_sec * 1000)) * 1000;
       }
/* Reseting/initializing */
#ifdef PHP_WIN32
       WSASetLastError(0);
#else
       errno = 0;
#endif
       n = select(max_fd + 1, &rset, &wset, &eset, timeout >= 0 ? &tv : NULL);

       if (n >= 0) {
              for (i = 0; i < nfds; i++) {
                     ufds[i].revents = 0;

                     if (PHP_SAFE_FD_ISSET(ufds[i].fd, &rset)) {
                            /* could be POLLERR or POLLHUP but can't tell without probing */
                            ufds[i].revents |= POLLIN;
                     }
                     if (PHP_SAFE_FD_ISSET(ufds[i].fd, &wset)) {
                            ufds[i].revents |= POLLOUT;
                     }
                     if (PHP_SAFE_FD_ISSET(ufds[i].fd, &eset)) {
                            ufds[i].revents |= POLLPRI;
                     }
              }
       }
       return n;
}

Here is the caller graph for this function:

static int php_pollfd_for ( php_socket_t  fd,
int  events,
struct timeval *  timeouttv 
) [inline, static]

Definition at line 154 of file php_network.h.

{
       php_pollfd p;
       int n;

       p.fd = fd;
       p.events = events;
       p.revents = 0;

       n = php_poll2(&p, 1, php_tvtoto(timeouttv));

       if (n > 0) {
              return p.revents;
       }

       return n;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int php_pollfd_for_ms ( php_socket_t  fd,
int  events,
int  timeout 
) [inline, static]

Definition at line 172 of file php_network.h.

{
       php_pollfd p;
       int n;

       p.fd = fd;
       p.events = events;
       p.revents = 0;

       n = php_poll2(&p, 1, timeout);

       if (n > 0) {
              return p.revents;
       }

       return n;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 957 of file network.c.

{
       switch (((struct sockaddr *)addr)->sa_family) {
       case AF_INET:
              return sizeof(struct sockaddr_in);
#if HAVE_IPV6
       case AF_INET6:
              return sizeof(struct sockaddr_in6);
#endif
#ifdef AF_UNIX
       case AF_UNIX:
              return sizeof(struct sockaddr_un);
#endif
       default:
              return 0;
       }
}
PHPAPI char* php_socket_strerror ( long  err,
char *  buf,
size_t  bufsize 
)

Definition at line 982 of file network.c.

{
#ifndef PHP_WIN32
       char *errstr;

       errstr = strerror(err);
       if (buf == NULL) {
              buf = estrdup(errstr);
       } else {
              strncpy(buf, errstr, bufsize);
       }
       return buf;
#else
       char *sysbuf;
       int free_it = 1;

       if (!FormatMessage(
                            FORMAT_MESSAGE_ALLOCATE_BUFFER |
                            FORMAT_MESSAGE_FROM_SYSTEM |
                            FORMAT_MESSAGE_IGNORE_INSERTS,
                            NULL,
                            err,
                            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                            (LPTSTR)&sysbuf,
                            0,
                            NULL)) {
              free_it = 0;
              sysbuf = "Unknown Error";
       }

       if (buf == NULL) {
              buf = estrdup(sysbuf);
       } else {
              strncpy(buf, sysbuf, bufsize);
       }

       if (free_it) {
              LocalFree(sysbuf);
       }

       return buf;
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int php_tvtoto ( struct timeval *  timeouttv) [inline, static]

Definition at line 142 of file php_network.h.

{
       if (timeouttv) {
              return (timeouttv->tv_sec * 1000) + (timeouttv->tv_usec / 1000);
       }
       return -1;
}

Here is the caller graph for this function:


Variable Documentation