iliaa           Mon Jun 13 22:39:43 2005 EDT

  Modified files:              
    /php-src/main       network.c php_network.h 
    /php-src/main/streams       xp_socket.c 
    /php-src/ext/ftp    ftp.c 
    /php-src    NEWS 
  Log:
  Added bindto socket context option.
  
  
http://cvs.php.net/diff.php/php-src/main/network.c?r1=1.114&r2=1.115&ty=u
Index: php-src/main/network.c
diff -u php-src/main/network.c:1.114 php-src/main/network.c:1.115
--- php-src/main/network.c:1.114        Wed Oct  6 09:22:21 2004
+++ php-src/main/network.c      Mon Jun 13 22:39:42 2005
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: network.c,v 1.114 2004/10/06 13:22:21 hyanantha Exp $ */
+/* $Id: network.c,v 1.115 2005/06/14 02:39:42 iliaa Exp $ */
 
 /*#define DEBUG_MAIN_NETWORK 1*/
 
@@ -722,7 +722,7 @@
 /* {{{ php_network_connect_socket_to_host */
 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
+               int *error_code, char *bindto, unsigned short bindport 
                TSRMLS_DC)
 {
        int num_addrs, n, fatal = 0;
@@ -785,7 +785,39 @@
 
                if (sa) {
                        /* make a connection attempt */
+
+                       if (bindto) {
+                               struct sockaddr local_address;
                        
+                               if (sa->sa_family == AF_INET) {
+                                       struct sockaddr_in *in4 = (struct 
sockaddr_in*)&local_address;
+                               
+                                       in4->sin_family = sa->sa_family;
+                                       in4->sin_port = htons(bindport);
+                                       if (!inet_aton(bindto, &in4->sin_addr)) 
{
+                                               goto bad_ip;
+                                       }
+                                       bzero(&(in4->sin_zero), 8);
+                               }
+#if HAVE_IPV6 && HAVE_INET_PTON
+                                else { /* IPV6 */
+                                       struct sockaddr_in6 *in6 = (struct 
sockaddr_in6*)&local_address;
+                               
+                                       in6->sin6_family = sa->sa_family;
+                                       in6->sin6_port = htons(bindport);
+                                       if (inet_pton(AF_INET6, bindto, 
&in6->sin6_addr) < 1) {
+                                               goto bad_ip;
+                                       }
+                               }
+#endif
+                               if (bind(sock, &local_address, sizeof(struct 
sockaddr))) {
+                                       php_error_docref(NULL TSRMLS_CC, 
E_WARNING, "failed to bind to '%s:%d', system said: %s", bindto, bindport, 
strerror(errno));
+                               }
+                               goto bind_done;
+bad_ip:
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING, 
"Invalid IP Address: %s", bindto);
+                       }
+bind_done:                     
                        n = php_network_connect_socket(sock, sa, socklen, 
asynchronous,
                                        timeout ? &working_timeout : NULL,
                                        error_string, error_code);
http://cvs.php.net/diff.php/php-src/main/php_network.h?r1=1.53&r2=1.54&ty=u
Index: php-src/main/php_network.h
diff -u php-src/main/php_network.h:1.53 php-src/main/php_network.h:1.54
--- php-src/main/php_network.h:1.53     Fri Sep 17 08:44:56 2004
+++ php-src/main/php_network.h  Mon Jun 13 22:39:42 2005
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_network.h,v 1.53 2004/09/17 12:44:56 wez Exp $ */
+/* $Id: php_network.h,v 1.54 2005/06/14 02:39:42 iliaa Exp $ */
 
 #ifndef _PHP_NETWORK_H
 #define _PHP_NETWORK_H
@@ -223,7 +223,7 @@
 BEGIN_EXTERN_C()
 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
+               int *error_code, char *bindto, unsigned short bindport 
                TSRMLS_DC);
 
 PHPAPI int php_network_connect_socket(php_socket_t sockfd,
http://cvs.php.net/diff.php/php-src/main/streams/xp_socket.c?r1=1.30&r2=1.31&ty=u
Index: php-src/main/streams/xp_socket.c
diff -u php-src/main/streams/xp_socket.c:1.30 
php-src/main/streams/xp_socket.c:1.31
--- php-src/main/streams/xp_socket.c:1.30       Fri Sep 17 10:36:55 2004
+++ php-src/main/streams/xp_socket.c    Mon Jun 13 22:39:42 2005
@@ -16,7 +16,7 @@
   +----------------------------------------------------------------------+
 */
 
-/* $Id: xp_socket.c,v 1.30 2004/09/17 14:36:55 wez Exp $ */
+/* $Id: xp_socket.c,v 1.31 2005/06/14 02:39:42 iliaa Exp $ */
 
 #include "php.h"
 #include "ext/standard/file.h"
@@ -487,7 +487,7 @@
 }
 #endif
 
-static inline char *parse_ip_address(php_stream_xport_param *xparam, int 
*portno TSRMLS_DC)
+static inline char *parse_ip_address_ex(const char *str, int str_len, int 
*portno, int get_err, char **err TSRMLS_DC)
 {
        char *colon;
        char *host = NULL;
@@ -495,27 +495,27 @@
 #ifdef HAVE_IPV6
        char *p;
 
-       if (*(xparam->inputs.name) == '[') {
+       if (*(str) == '[') {
                /* IPV6 notation to specify raw address with port (i.e. 
[fe80::1]:80) */
-               p = memchr(xparam->inputs.name + 1, ']', xparam->inputs.namelen 
- 2);
+               p = memchr(str + 1, ']', str_len - 2);
                if (!p || *(p + 1) != ':') {
-                       if (xparam->want_errortext) {
-                               spprintf(&xparam->outputs.error_text, 0, 
"Failed to parse IPv6 address \"%s\"", xparam->inputs.name);
+                       if (get_err) {
+                               spprintf(err, 0, "Failed to parse IPv6 address 
\"%s\"", str);
                        }
                        return NULL;
                }
                *portno = atoi(p + 2);
-               return estrndup(xparam->inputs.name + 1, p - 
xparam->inputs.name - 1);
+               return estrndup(str + 1, p - str - 1);
        }
 #endif
 
-       colon = memchr(xparam->inputs.name, ':', xparam->inputs.namelen - 1);
+       colon = memchr(str, ':', str_len - 1);
        if (colon) {
                *portno = atoi(colon + 1);
-               host = estrndup(xparam->inputs.name, colon - 
xparam->inputs.name);
+               host = estrndup(str, colon - str);
        } else {
-               if (xparam->want_errortext) {
-                       spprintf(&xparam->outputs.error_text, 0, "Failed to 
parse address \"%s\"", xparam->inputs.name);
+               if (get_err) {
+                       spprintf(err, 0, "Failed to parse address \"%s\"", str);
                }
                return NULL;
        }
@@ -523,6 +523,11 @@
        return host;
 }
 
+static inline char *parse_ip_address(php_stream_xport_param *xparam, int 
*portno TSRMLS_DC)
+{
+       return parse_ip_address_ex(xparam->inputs.name, xparam->inputs.namelen, 
portno, xparam->want_errortext, &xparam->outputs.error_text);
+}
+
 static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t 
*sock,
                php_stream_xport_param *xparam TSRMLS_DC)
 {
@@ -572,10 +577,11 @@
 static inline int php_tcp_sockop_connect(php_stream *stream, 
php_netstream_data_t *sock,
                php_stream_xport_param *xparam TSRMLS_DC)
 {
-       char *host = NULL;
-       int portno;
+       char *host = NULL, *bindto = NULL;
+       int portno, bindport;
        int err;
        int ret;
+       zval **tmpzval = NULL;
 
 #ifdef AF_UNIX
        if (stream->ops == &php_stream_unix_socket_ops || stream->ops == 
&php_stream_unixdg_socket_ops) {
@@ -610,6 +616,16 @@
                return -1;
        }
 
+       if (stream->context && php_stream_context_get_option(stream->context, 
"socket", "bindto", &tmpzval) == SUCCESS) {
+               if (Z_TYPE_PP(tmpzval) != IS_STRING) {
+                       if (xparam->want_errortext) {
+                               spprintf(&xparam->outputs.error_text, 0, 
"local_addr context option is not a string.");
+                       }
+                       return -1;
+               }
+               bindto = parse_ip_address_ex(Z_STRVAL_PP(tmpzval), 
Z_STRLEN_PP(tmpzval), &bindport, xparam->want_errortext, 
&xparam->outputs.error_text TSRMLS_CC);
+       }
+
        /* Note: the test here for php_stream_udp_socket_ops is important, 
because we
         * want the default to be TCP sockets so that the openssl extension can
         * re-use this code. */
@@ -619,7 +635,9 @@
                        xparam->op == STREAM_XPORT_OP_CONNECT_ASYNC,
                        xparam->inputs.timeout,
                        xparam->want_errortext ? &xparam->outputs.error_text : 
NULL,
-                       &err
+                       &err,
+                       bindto,
+                       bindport
                        TSRMLS_CC);
        
        ret = sock->socket == -1 ? -1 : 0;
@@ -628,6 +646,9 @@
        if (host) {
                efree(host);
        }
+       if (bindto) {
+               efree(bindto);
+       }
 
 #ifdef AF_UNIX
 out:
http://cvs.php.net/diff.php/php-src/ext/ftp/ftp.c?r1=1.110&r2=1.111&ty=u
Index: php-src/ext/ftp/ftp.c
diff -u php-src/ext/ftp/ftp.c:1.110 php-src/ext/ftp/ftp.c:1.111
--- php-src/ext/ftp/ftp.c:1.110 Thu Mar 17 12:16:43 2005
+++ php-src/ext/ftp/ftp.c       Mon Jun 13 22:39:42 2005
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: ftp.c,v 1.110 2005/03/17 17:16:43 iliaa Exp $ */
+/* $Id: ftp.c,v 1.111 2005/06/14 02:39:42 iliaa Exp $ */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -136,7 +136,7 @@
 
        ftp->fd = php_network_connect_socket_to_host(host,
                        (unsigned short) (port ? port : 21), SOCK_STREAM,
-                       0, &tv, NULL, NULL TSRMLS_CC);
+                       0, &tv, NULL, NULL, NULL, 0 TSRMLS_CC);
        if (ftp->fd == -1) {
                goto bail;
        }
http://cvs.php.net/diff.php/php-src/NEWS?r1=1.1917&r2=1.1918&ty=u
Index: php-src/NEWS
diff -u php-src/NEWS:1.1917 php-src/NEWS:1.1918
--- php-src/NEWS:1.1917 Mon Jun 13 18:20:05 2005
+++ php-src/NEWS        Mon Jun 13 22:39:42 2005
@@ -1,6 +1,7 @@
 PHP                                                                        NEWS
 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 ?? Jun 2005, PHP 5.1 Beta 2
+- Added bindto socket context option. (Ilia)
 - Fixed PDO shutdown problem (possible inifite loop running rollback on
   shutdown). (Wez)
 - Fixed PECL bug #3714 (beginTransaction doesn't work if you're in

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to