@@ -2516,36 +2516,38 @@ PHP_FUNCTION(socket_export_stream)
2516
2516
Gets array with contents of getaddrinfo about the given hostname. */
2517
2517
PHP_FUNCTION (socket_addrinfo_lookup )
2518
2518
{
2519
+ char * service = NULL ;
2520
+ size_t service_len ;
2519
2521
zend_string * hostname , * key ;
2520
- zval * hint , * service , * zhints = NULL ;
2522
+ zval * hint , * zhints = NULL ;
2521
2523
2522
2524
struct addrinfo hints , * result , * rp , * res ;
2523
2525
2524
2526
memset (& hints , 0 , sizeof (hints ));
2525
2527
2526
- if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "S|za " , & hostname , & service , & zhints ) == FAILURE ) {
2528
+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "S|sa " , & hostname , & service , & service_len , & zhints ) == FAILURE ) {
2527
2529
RETURN_NULL ();
2528
2530
}
2529
2531
2530
2532
if (zhints ) {
2531
2533
ZEND_HASH_FOREACH_STR_KEY_VAL (Z_ARRVAL_P (zhints ), key , hint ) {
2532
2534
if (key ) {
2533
- if (strcmp (ZSTR_VAL (key ), "ai_flags" ) == 0 ) {
2534
- hints .ai_flags = Z_LVAL_P (hint );
2535
- } else if (strcmp (ZSTR_VAL (key ), "ai_socktype" ) == 0 ) {
2536
- hints .ai_socktype = Z_LVAL_P (hint );
2537
- } else if (strcmp (ZSTR_VAL (key ), "ai_protocol" ) == 0 ) {
2538
- hints .ai_protocol = Z_LVAL_P (hint );
2539
- } else if (strcmp (ZSTR_VAL (key ), "ai_family" ) == 0 ) {
2540
- hints .ai_family = Z_LVAL_P (hint );
2535
+ if (zend_string_equals_literal (key , "ai_flags" )) {
2536
+ hints .ai_flags = zval_get_long (hint );
2537
+ } else if (zend_string_equals_literal (key , "ai_socktype" )) {
2538
+ hints .ai_socktype = zval_get_long (hint );
2539
+ } else if (zend_string_equals_literal (key , "ai_protocol" )) {
2540
+ hints .ai_protocol = zval_get_long (hint );
2541
+ } else if (zend_string_equals_literal (key , "ai_family" )) {
2542
+ hints .ai_family = zval_get_long (hint );
2543
+ } else {
2544
+ php_error_docref (NULL , E_NOTICE , "Unknown hint %s" , ZSTR_VAL (key ));
2541
2545
}
2542
2546
}
2543
2547
} ZEND_HASH_FOREACH_END ();
2544
2548
}
2545
2549
2546
- convert_to_string (service );
2547
-
2548
- if (getaddrinfo (ZSTR_VAL (hostname ), Z_STRVAL_P (service ), & hints , & result ) != 0 ) {
2550
+ if (getaddrinfo (ZSTR_VAL (hostname ), service , & hints , & result ) != 0 ) {
2549
2551
RETURN_FALSE ;
2550
2552
}
2551
2553
@@ -2578,18 +2580,17 @@ PHP_FUNCTION(socket_addrinfo_bind)
2578
2580
zval * arg1 ;
2579
2581
int retval ;
2580
2582
struct addrinfo * ai ;
2581
- php_socket * php_sock = php_create_socket () ;
2583
+ php_socket * php_sock ;
2582
2584
2583
2585
if (zend_parse_parameters (ZEND_NUM_ARGS (), "r" , & arg1 ) == FAILURE ) {
2584
- efree (php_sock );
2585
2586
return ;
2586
2587
}
2587
2588
2588
2589
if ((ai = (struct addrinfo * ) zend_fetch_resource (Z_RES_P (arg1 ), le_addrinfo_name , le_addrinfo )) == NULL ) {
2589
- efree (php_sock );
2590
2590
RETURN_FALSE ;
2591
2591
}
2592
2592
2593
+ php_sock = php_create_socket ();
2593
2594
php_sock -> bsd_socket = socket (ai -> ai_family , ai -> ai_socktype , ai -> ai_protocol );
2594
2595
php_sock -> type = ai -> ai_family ;
2595
2596
@@ -2607,7 +2608,9 @@ PHP_FUNCTION(socket_addrinfo_bind)
2607
2608
case AF_UNIX :
2608
2609
{
2609
2610
// AF_UNIX sockets via getaddrino are not implemented due to security problems
2610
- break ;
2611
+ close (php_sock -> bsd_socket );
2612
+ efree (php_sock );
2613
+ RETURN_FALSE ;
2611
2614
}
2612
2615
2613
2616
case AF_INET :
@@ -2620,12 +2623,14 @@ PHP_FUNCTION(socket_addrinfo_bind)
2620
2623
}
2621
2624
default :
2622
2625
php_error_docref (NULL , E_WARNING , "unsupported socket type '%d', must be AF_UNIX, AF_INET, or AF_INET6" , php_sock -> type );
2626
+ close (php_sock -> bsd_socket );
2623
2627
efree (php_sock );
2624
2628
RETURN_FALSE ;
2625
2629
}
2626
2630
2627
2631
if (retval != 0 ) {
2628
2632
PHP_SOCKET_ERROR (php_sock , "unable to bind address" , errno );
2633
+ close (php_sock -> bsd_socket );
2629
2634
efree (php_sock );
2630
2635
RETURN_FALSE ;
2631
2636
}
@@ -2641,18 +2646,17 @@ PHP_FUNCTION(socket_addrinfo_connect)
2641
2646
zval * arg1 ;
2642
2647
int retval ;
2643
2648
struct addrinfo * ai ;
2644
- php_socket * php_sock = php_create_socket () ;
2649
+ php_socket * php_sock ;
2645
2650
2646
2651
if (zend_parse_parameters (ZEND_NUM_ARGS (), "r" , & arg1 ) == FAILURE ) {
2647
- efree (php_sock );
2648
2652
return ;
2649
2653
}
2650
2654
2651
2655
if ((ai = (struct addrinfo * ) zend_fetch_resource (Z_RES_P (arg1 ), le_addrinfo_name , le_addrinfo )) == NULL ) {
2652
- efree (php_sock );
2653
2656
RETURN_FALSE ;
2654
2657
}
2655
2658
2659
+ php_sock = php_create_socket ();
2656
2660
php_sock -> bsd_socket = socket (ai -> ai_family , ai -> ai_socktype , ai -> ai_protocol );
2657
2661
php_sock -> type = ai -> ai_family ;
2658
2662
@@ -2670,7 +2674,9 @@ PHP_FUNCTION(socket_addrinfo_connect)
2670
2674
case AF_UNIX :
2671
2675
{
2672
2676
// AF_UNIX sockets via getaddrino are not implemented due to security problems
2673
- break ;
2677
+ close (php_sock -> bsd_socket );
2678
+ efree (php_sock );
2679
+ RETURN_FALSE ;
2674
2680
}
2675
2681
2676
2682
case AF_INET :
@@ -2683,12 +2689,14 @@ PHP_FUNCTION(socket_addrinfo_connect)
2683
2689
}
2684
2690
default :
2685
2691
php_error_docref (NULL , E_WARNING , "unsupported socket type '%d', must be AF_UNIX, AF_INET, or AF_INET6" , php_sock -> type );
2692
+ close (php_sock -> bsd_socket );
2686
2693
efree (php_sock );
2687
2694
RETURN_FALSE ;
2688
2695
}
2689
2696
2690
2697
if (retval != 0 ) {
2691
2698
PHP_SOCKET_ERROR (php_sock , "unable to connect address" , errno );
2699
+ close (php_sock -> bsd_socket );
2692
2700
efree (php_sock );
2693
2701
RETURN_FALSE ;
2694
2702
}
0 commit comments