[PHP-DEV] Looking for understanding... From: Lars Torben Wilson (torben <email protected>)
Date: 01/28/00

If anyone has a minute, could you list some of the more obvious
problems with the attached code? It's a simple inet_aton()/inet_ntoa()
implementation which accepts and returns strings instead of ints.

I've been told that an earlier version was not acceptable, but I've
fixed a few things and would like to find out what other sorts of
things I should watch out for. Thanks for any help.

/* {{{ 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;

        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 );
        RETVAL_STRING( retstr, 1 );
    efree( retstr );
}
/* }}} */

/* {{{ 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 );
}
/* }}} */

-- 
+----------------------------------------------------------------+
|Torben Wilson <torben <email protected>>                     Netmill iTech|
|http://www.coastnet.com/~torben            http://www.netmill.fi|
|Ph: 1 250 383-9735                             torben <email protected>|
+----------------------------------------------------------------+

-- PHP Development Mailing List <http://www.php.net/> To unsubscribe, e-mail: php-dev-unsubscribe <email protected> For additional commands, e-mail: php-dev-help <email protected> To contact the list administrators, e-mail: php-list-admin <email protected>