Re: inet_aton() and inet_ntoa()
| From: | Lars Torben Wilson | Date: | Tue, 25 Jan 2000 01:43:39 +0000 |
| Subject: | Re: inet_aton() and inet_ntoa() | ||
| References: | 1 2 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-14896@lists.php.net to get a copy of this message | ||
Egon Schmid writes:
> Lars Torben Wilson wrote:
> >
> > 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.
>
> Hi Torben,
> the protos don't look very conformant to the CODING_STANDARDS.
>
> -Egon
Thanks for the quick reply. Unfortunately, it's not very helpful.
Attached is a slightly cleaner version. If there are still problems,
an actual hint as to the nature of any problem would be
appreciated. As it is, I've gone over it and fixed up the one item
mentioned in CODING_STANDARDS that I could find: #4 (don't use
strlen()). I've also added more indicative error messages, and cleaned
up the return values a bit. Other than that, it appears to me that
every requirement is met.
Any further information would be most welcome.
Thanks,
Torben
--
+----------------------------------------------------------------+
|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/25 01:44:33
@@ -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/25 01:44:33
@@ -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)) {
+ php3_error(E_WARNING, "Given IP address could not be converted to Internet number");
+ RETURN_FALSE;
+ }
+
+ retstr = (char *) emalloc( MAX_LENGTH_OF_LONG + 1 );
+ if ( !retstr ) {
+ RETURN_FALSE;
+ }
+
+ _php3_sprintf( retstr, "%u", in.s_addr );
+ RETURN_STRING( retstr, 1 );
+}
+/* }}} */
+
+
+/* {{{ proto string inet_ntoa(int 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)) ) {
+ php3_error( E_WARNING, "Given Internet Number could not be converted to dotted quad
notation" );
+ 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/25 01:44:33
@@ -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);