diff options
Diffstat (limited to 'irc.c')
-rw-r--r-- | irc.c | 66 |
1 files changed, 18 insertions, 48 deletions
@@ -44,14 +44,8 @@ static char *passchange( set_t *set, char *value ) irc_t *irc_new( int fd ) { irc_t *irc; - struct hostent *peer; - unsigned int i; - char buf[128]; -#ifdef IPV6 - struct sockaddr_in6 sock6[1]; - unsigned int i6; -#endif - struct sockaddr_in sock[1]; + struct sockaddr_storage sock; + socklen_t socklen = sizeof(sock); irc = g_new0( irc_t, 1 ); @@ -70,54 +64,30 @@ irc_t *irc_new( int fd ) irc->mynick = g_strdup( ROOT_NICK ); irc->channel = g_strdup( ROOT_CHAN ); - i = sizeof( *sock ); -#ifdef IPV6 - i6 = sizeof( *sock6 ); -#endif - if( global.conf->hostname ) irc->myhost = g_strdup( global.conf->hostname ); -#ifdef IPV6 - else if( getsockname( irc->fd, (struct sockaddr*) sock6, &i6 ) == 0 && sock6->sin6_family == AF_INET6 ) - { - if( ( peer = gethostbyaddr( (char*) &sock6->sin6_addr, sizeof( sock6->sin6_addr ), AF_INET6 ) ) ) - irc->myhost = g_strdup( peer->h_name ); - else if( inet_ntop( AF_INET6, &sock6->sin6_addr, buf, sizeof( buf ) - 1 ) != NULL ) - irc->myhost = g_strdup( ipv6_unwrap( buf ) ); - } -#endif - else if( getsockname( irc->fd, (struct sockaddr*) sock, &i ) == 0 && sock->sin_family == AF_INET ) + else if( getsockname( irc->fd, (struct sockaddr*) &sock, &socklen ) == 0) { - if( ( peer = gethostbyaddr( (char*) &sock->sin_addr, sizeof( sock->sin_addr ), AF_INET ) ) ) - irc->myhost = g_strdup( peer->h_name ); - else if( inet_ntop( AF_INET, &sock->sin_addr, buf, sizeof( buf ) - 1 ) != NULL ) - irc->myhost = g_strdup( buf ); + irc->myhost = g_new0(char, NI_MAXHOST); + + if (getnameinfo((struct sockaddr *)&sock, socklen, irc->myhost, + NI_MAXHOST, NULL, -1, 0)) { + /* Rare, but possible. */ + strncpy(irc->myhost, "localhost.", NI_MAXHOST); + } } - i = sizeof( *sock ); -#ifdef IPV6 - i6 = sizeof( *sock6 ); - if( getpeername( irc->fd, (struct sockaddr*) sock6, &i6 ) == 0 && sock6->sin6_family == AF_INET6 ) + if( getpeername( irc->fd, (struct sockaddr*) &sock, &socklen ) == 0) { - if( ( peer = gethostbyaddr( (char*) &sock6->sin6_addr, sizeof( sock6->sin6_addr ), AF_INET6 ) ) ) - irc->host = g_strdup( peer->h_name ); - else if( inet_ntop( AF_INET6, &sock6->sin6_addr, buf, sizeof( buf ) - 1 ) != NULL ) - irc->host = g_strdup( ipv6_unwrap( buf ) ); - } - else -#endif - if( getpeername( irc->fd, (struct sockaddr*) sock, &i ) == 0 && sock->sin_family == AF_INET ) - { - if( ( peer = gethostbyaddr( (char*) &sock->sin_addr, sizeof( sock->sin_addr ), AF_INET ) ) ) - irc->host = g_strdup( peer->h_name ); - else if( inet_ntop( AF_INET, &sock->sin_addr, buf, sizeof( buf ) - 1 ) != NULL ) - irc->host = g_strdup( buf ); + irc->host = g_new0(char, NI_MAXHOST); + + if (getnameinfo((struct sockaddr *)&sock, socklen, irc->host, + NI_MAXHOST, NULL, -1, 0)) { + /* Rare, but possible. */ + strncpy(irc->myhost, "localhost.", NI_MAXHOST); + } } - /* Rare, but possible. */ - if( !irc->host ) irc->host = g_strdup( "localhost." ); - if( !irc->myhost ) irc->myhost = g_strdup( "localhost." ); - if( global.conf->ping_interval > 0 && global.conf->ping_timeout > 0 ) irc->ping_source_id = b_timeout_add( global.conf->ping_interval * 1000, irc_userping, irc ); |