@@ -599,6 +599,9 @@ datagram_read(comm_t *connection, size_t length, dispatch_data_t content, nw_err
599599 return true;
600600 });
601601 if (ret == true) {
602+ // Set the local address
603+ message -> local = connection -> local ;
604+
602605 // Process the message.
603606 if (connection -> listener_state != NULL ) {
604607 connection -> listener_state -> datagram_callback (connection , message , connection -> listener_state -> context );
@@ -821,6 +824,30 @@ connection_state_changed(comm_t *connection, nw_connection_state_t state, nw_err
821824 }
822825}
823826
827+ static void
828+ ioloop_connection_get_address_from_endpoint (addr_t * addr , nw_endpoint_t endpoint )
829+ {
830+ nw_endpoint_type_t endpoint_type = nw_endpoint_get_type (endpoint );
831+ if (endpoint_type == nw_endpoint_type_address ) {
832+ char * address_string = nw_endpoint_copy_address_string (endpoint );
833+ if (address_string == NULL ) {
834+ ERROR ("unable to get description of new connection." );
835+ } else {
836+ getipaddr (addr , address_string );
837+ if (addr -> sa .sa_family == AF_INET6 ) {
838+ SEGMENTED_IPv6_ADDR_GEN_SRP (& addr -> sin6 .sin6_addr , rdata_buf );
839+ INFO ("parsed connection local IPv6 address is: " PRI_SEGMENTED_IPv6_ADDR_SRP ,
840+ SEGMENTED_IPv6_ADDR_PARAM_SRP (& addr -> sin6 .sin6_addr , rdata_buf ));
841+ } else {
842+ IPv4_ADDR_GEN_SRP (& addr -> sin .sin_addr , rdata_buf );
843+ INFO ("parsed connection local IPv4 address is: " PRI_IPv4_ADDR_SRP ,
844+ IPv4_ADDR_PARAM_SRP (& addr -> sin .sin_addr , rdata_buf ));
845+ }
846+ }
847+ free (address_string );
848+ }
849+ }
850+
824851static void
825852ioloop_connection_set_name_from_endpoint (comm_t * listener , comm_t * connection , nw_endpoint_t endpoint )
826853{
@@ -835,11 +862,11 @@ ioloop_connection_set_name_from_endpoint(comm_t *listener, comm_t *connection, n
835862 getipaddr (& connection -> address , address_string );
836863 if (connection -> address .sa .sa_family == AF_INET6 ) {
837864 SEGMENTED_IPv6_ADDR_GEN_SRP (& connection -> address .sin6 .sin6_addr , rdata_buf );
838- INFO ("parsed connection IPv6 address is: " PRI_SEGMENTED_IPv6_ADDR_SRP ,
865+ INFO ("parsed connection remote IPv6 address is: " PRI_SEGMENTED_IPv6_ADDR_SRP ,
839866 SEGMENTED_IPv6_ADDR_PARAM_SRP (& connection -> address .sin6 .sin6_addr , rdata_buf ));
840867 } else {
841868 IPv4_ADDR_GEN_SRP (& connection -> address .sin .sin_addr , rdata_buf );
842- INFO ("parsed connection IPv4 address is: " PRI_IPv4_ADDR_SRP ,
869+ INFO ("parsed connection remote IPv4 address is: " PRI_IPv4_ADDR_SRP ,
843870 IPv4_ADDR_PARAM_SRP (& connection -> address .sin .sin_addr , rdata_buf ));
844871 }
845872 }
@@ -905,6 +932,13 @@ connection_callback(comm_t *listener, nw_connection_t new_connection)
905932 connection -> name = strdup ("unidentified" );
906933 }
907934
935+ // Best effort
936+ nw_endpoint_t local_endpoint = nw_connection_copy_connected_local_endpoint (connection -> connection );
937+ if (local_endpoint != NULL ) {
938+ ioloop_connection_get_address_from_endpoint (& connection -> local , endpoint );
939+ nw_release (local_endpoint );
940+ }
941+
908942 connection -> datagram_callback = listener -> datagram_callback ;
909943 connection -> tcp_stream = listener -> tcp_stream ;
910944 connection -> server = true;
@@ -975,10 +1009,27 @@ ioloop_listener_cancel(comm_t *connection)
9751009 // connection->listener will be released in ioloop_listener_state_changed_handler: nw_listener_state_cancelled.
9761010 }
9771011#if UDP_LISTENER_USES_CONNECTION_GROUPS
978- if (!connection -> stream && connection -> io .fd != -1 ) {
979- ioloop_close (& connection -> io );
980- io -> refcnt = 100 ; // Prevent read_cancel from finalizing the io object
981- ioloop_read_cancel (& connection -> io );
1012+ if (connection -> connection_group != NULL ) {
1013+ INFO ("%p %p" , connection , connection -> connection_group );
1014+ nw_connection_group_cancel (connection -> connection_group );
1015+ }
1016+ #else
1017+ if (!connection -> tcp_stream && connection -> connection == NULL ) {
1018+ int fd = connection -> io .fd ;
1019+ if (fd != -1 ) {
1020+ ioloop_close (& connection -> io );
1021+ close (fd );
1022+ ioloop_read_cancel (& connection -> io );
1023+ if (connection -> cancel != NULL ) {
1024+ RETAIN_HERE (connection , listener );
1025+ dispatch_async (ioloop_main_queue , ^{
1026+ if (connection -> cancel != NULL ) {
1027+ connection -> cancel (connection -> context );
1028+ }
1029+ RELEASE_HERE (connection , listener );
1030+ });
1031+ }
1032+ }
9821033 }
9831034#endif
9841035}
@@ -1200,12 +1251,12 @@ ioloop_udp_listener_setup(comm_t *listener, const addr_t *ip_address, uint16_t p
12001251 if (family == AF_INET ) {
12011252 IPv4_ADDR_GEN_SRP (& listener -> address .sin .sin_addr .s_addr , ipv4_addr_buf );
12021253 ERROR ("Can't bind to " PRI_IPv4_ADDR_SRP "#%d: %s" ,
1203- IPv4_ADDR_PARAM_SRP (& listener -> address .sin .sin_addr .s_addr , ipv4_addr_buf ), ntohs ( port ) ,
1254+ IPv4_ADDR_PARAM_SRP (& listener -> address .sin .sin_addr .s_addr , ipv4_addr_buf ), port ,
12041255 strerror (errno ));
12051256 } else {
12061257 SEGMENTED_IPv6_ADDR_GEN_SRP (& listener -> address .sin6 .sin6_addr .s6_addr , ipv6_addr_buf );
12071258 ERROR ("Can't bind to " PRI_SEGMENTED_IPv6_ADDR_SRP "#%d: %s" ,
1208- SEGMENTED_IPv6_ADDR_PARAM_SRP (& listener -> address .sin6 .sin6_addr .s6_addr , ipv6_addr_buf ), ntohs ( port ) ,
1259+ SEGMENTED_IPv6_ADDR_PARAM_SRP (& listener -> address .sin6 .sin6_addr .s6_addr , ipv6_addr_buf ), port ,
12091260 strerror (errno ));
12101261 }
12111262 out :
0 commit comments