inet_aton() and inet_ntoa()
| From: | Lars Torben Wilson | Date: | Mon, 24 Jan 2000 21:37:20 +0000 |
| Subject: | inet_aton() and inet_ntoa() | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-14888@lists.php.net to get a copy of this message | ||
We had a guy at work who needed these; PHP doesn't support unsigned
ints and the userland solution required the bcmath library--which
seemed like overkill. Anyway, these function both return strings
containing the relevant values, so that they can at least be used if
nothing else.
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+
Index: basic_functions.c
===================================================================
RCS file: /repository/php3/functions/basic_functions.c,v
retrieving revision 1.283
diff -u -r1.283 basic_functions.c
--- basic_functions.c 2000/01/01 04:31:14 1.283
+++ basic_functions.c 2000/01/24 21:35:16
@@ -323,6 +323,9 @@
{"call_user_func", php3_call_user_func, NULL},
{"call_user_method", php3_call_user_method, NULL},
+ PHP_FE(inet_aton, NULL)
+ PHP_FE(inet_ntoa, NULL)
+
PHP_FE(var_dump, NULL)
PHP_FE(serialize, first_arg_allow_ref)
PHP_FE(unserialize, first_arg_allow_ref)
Index: dns.c
===================================================================
RCS file: /repository/php3/functions/dns.c,v
retrieving revision 1.59
diff -u -r1.59 dns.c
--- dns.c 2000/01/01 04:31:14 1.59
+++ dns.c 2000/01/24 21:35:17
@@ -61,6 +61,7 @@
#include "internal_functions.h"
#include "dns.h"
+#include "snprintf.h"
char *_php3_gethostbyaddr(char *ip);
char *_php3_gethostbyname(char *name);
@@ -333,6 +334,64 @@
RETURN_TRUE;
}
/* }}} */
+
+
+/* {{{ proto string inet_aton(string ip)
+ Converts an dotted-quad IP address to an Internet number
+ The return value is the string representation of the Internet
+ number to circumvent PHP's lack of a uint type. */
+PHP_FUNCTION(inet_aton)
+{
+ pval *arg;
+ struct in_addr in;
+ char *retstr;
+ int retlen;
+
+ if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_string(arg);
+
+ if (!inet_aton(arg->value.str.val, &in)) {
+#if DEBUG
+ php3_error(E_WARNING, "address not in a.b.c.d form");
+#endif
+ RETURN_FALSE;
+ }
+
+ retstr = (char *) emalloc( MAX_LENGTH_OF_LONG + 1 );
+ if ( !retstr ) {
+ RETURN_FALSE;
+ }
+
+ retlen = sprintf( retstr, "%u", in.s_addr );
+ RETURN_STRINGL( retstr, retlen, 1 );
+}
+/* }}} */
+
+
+/* {{{ proto string inet_ntoa(string in)
+ Convert an Internet number to dotted-quad notation. */
+PHP_FUNCTION(inet_ntoa)
+{
+ pval *arg;
+ struct in_addr in;
+ char *retstr;
+
+ if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_string(arg);
+
+ in.s_addr = strtod( arg->value.str.val, NULL );
+
+ if ( !(retstr = inet_ntoa(in)) ) {
+ RETURN_FALSE;
+ }
+
+ RETURN_STRING( retstr, 1 );
+}
+/* }}} */
+
#endif
/*
Index: dns.h
===================================================================
RCS file: /repository/php3/functions/dns.h,v
retrieving revision 1.13
diff -u -r1.13 dns.h
--- dns.h 2000/01/01 04:44:09 1.13
+++ dns.h 2000/01/24 21:35:17
@@ -37,6 +37,8 @@
extern void php3_gethostbyaddr(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_gethostbyname(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_gethostbynamel(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_inet_aton(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_inet_ntoa(INTERNAL_FUNCTION_PARAMETERS);
#if !(WIN32|WINNT)||(HAVE_BINDLIB)
extern void php3_checkdnsrr(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_getmxrr(INTERNAL_FUNCTION_PARAMETERS);