]> granicus.if.org Git - php/commitdiff
New Functions inet_pton() and inet_ntop()
authorSara Golemon <pollita@php.net>
Sat, 7 Aug 2004 04:50:24 +0000 (04:50 +0000)
committerSara Golemon <pollita@php.net>
Sat, 7 Aug 2004 04:50:24 +0000 (04:50 +0000)
NEWS
ext/standard/basic_functions.c
ext/standard/basic_functions.h

diff --git a/NEWS b/NEWS
index 244fde1bee7e5b590af76ff5199f8f9d10e8aabe..9ab25ec2e92c07a0b809f4e018e2347f7e44458c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -17,6 +17,8 @@ PHP                                                                        NEWS
   . stream_socket_enable_crypto()  (Wez)
   . SimpleXMLElement->registerXPathNamespace() (Christian)
   . mysqli->client_info property (Georg)
+  . inet_pton() (Sara)
+  . inet_ntop() (Sara)
 - PHP will now respect extension dependencies when initializing.  (Wez)
 - Added Cursor support for MySQL 5.0.x in mysqli (Georg)
 - Added proxy support to ftp wrapper via http. (Sara)
index 9148a66d192f7722c8492a592c03777d01b85898..f364719189710b51d22c8120671f79bc2a55eb72 100644 (file)
@@ -419,6 +419,12 @@ function_entry basic_functions[] = {
        PHP_FE(base_convert,                                                                                                    NULL)
        PHP_FE(number_format,                                                                                                   NULL)
        PHP_FE(fmod,                                                                                                                    NULL)
+#ifdef HAVE_INET_NTOP
+       PHP_FE(inet_ntop,                                                                                                               NULL)
+#endif
+#ifdef HAVE_INET_PTON
+       PHP_FE(inet_pton,                                                                                                               NULL)
+#endif
        PHP_FE(ip2long,                                                                                                                 NULL)
        PHP_FE(long2ip,                                                                                                                 NULL)
 
@@ -1273,6 +1279,79 @@ PHP_FUNCTION(constant)
 }
 /* }}} */
 
+#ifdef HAVE_INET_NTOP
+/* {{{ proto string inet_ntop(string in_addr)
+   Converts a packed inet address to a human readable IP address string */
+PHP_FUNCTION(inet_ntop)
+{
+       char *address;
+       int address_len, af = AF_INET;
+       char buffer[40];
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &address, &address_len) == FAILURE) {
+               RETURN_FALSE;
+       }
+
+#ifdef HAVE_IPV6
+       if (address_len == 16) {
+               af = AF_INET6;
+       } else
+#endif
+       if (address_len != 4) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid in_addr value");
+               RETURN_FALSE;
+       }
+
+       if (!inet_ntop(af, address, buffer, sizeof(buffer))) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "An unknown error occured");
+               RETURN_FALSE;
+       }
+
+       RETURN_STRING(buffer, 1);
+}
+/* }}} */
+#endif /* HAVE_INET_NTOP */
+
+#ifdef HAVE_INET_PTON
+/* {{{ proto string inet_pton(string ip_address)
+   Converts a human readable IP address to a packed binary string */
+PHP_FUNCTION(inet_pton)
+{
+       int ret, af = AF_INET;
+       char *address;
+       int address_len;
+       char buffer[17];
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &address, &address_len) == FAILURE) {
+               RETURN_FALSE;
+       }
+
+       memset(buffer, 0, sizeof(buffer));
+
+#ifdef HAVE_IPV6
+       if (strchr(address, ':')) {
+               af = AF_INET6;
+       } else 
+#endif
+       if (!strchr(address, '.')) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unrecognized address %s", address);
+               RETURN_FALSE;
+       }
+
+       ret = inet_pton(af, address, buffer);
+
+       if (ret <= 0) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unrecognized address %s", address);
+               RETURN_FALSE;
+       }
+
+       RETURN_STRING(buffer, 1);
+}
+/* }}} */
+#endif /* HAVE_INET_PTON */
+
+
+
 /* {{{ proto int ip2long(string ip_address)
    Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address */
 PHP_FUNCTION(ip2long)
index 468d27c4449f9756406025f7ac54fcc7f1641a1d..ead825bfe20bf86014447c1bc409e3a3a54b3959 100644 (file)
@@ -52,6 +52,12 @@ PHP_FUNCTION(usleep);
 PHP_FUNCTION(time_nanosleep);
 #endif
 PHP_FUNCTION(flush);
+#ifdef HAVE_INET_NTOP
+PHP_FUNCTION(inet_ntop);
+#endif
+#ifdef HAVE_INET_PTON
+PHP_FUNCTION(inet_pton);
+#endif
 PHP_FUNCTION(ip2long);
 PHP_FUNCTION(long2ip);