diff mbox

libxtables: xtables.c: Use getnameinfo()

Message ID 1481284200-6104-1-git-send-email-mayhs11saini@gmail.com
State Changes Requested
Delegated to: Pablo Neira
Headers show

Commit Message

Shyam Saini Dec. 9, 2016, 11:50 a.m. UTC
Use getnameinfo() instead of deprecated gethostbyaddr()

Signed-off-by: Shyam Saini <mayhs11saini@gmail.com>
---
 libxtables/xtables.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

Comments

Pablo Neira Ayuso Dec. 11, 2016, 8:02 p.m. UTC | #1
On Fri, Dec 09, 2016 at 05:20:00PM +0530, Shyam Saini wrote:
> Use getnameinfo() instead of deprecated gethostbyaddr()
> 
> Signed-off-by: Shyam Saini <mayhs11saini@gmail.com>
> ---
>  libxtables/xtables.c | 25 ++++++++++++++++++++-----
>  1 file changed, 20 insertions(+), 5 deletions(-)
> 
> diff --git a/libxtables/xtables.c b/libxtables/xtables.c
> index 921dfe9..338e325 100644
> --- a/libxtables/xtables.c
> +++ b/libxtables/xtables.c
> @@ -1210,13 +1210,28 @@ const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
>  
>  static const char *ipaddr_to_host(const struct in_addr *addr)
>  {
> -	struct hostent *host;
> +	static char hostname[NI_MAXHOST];
> +	struct sockaddr_in saddr;
> +	int err;
>  
> -	host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
> -	if (host == NULL)
> -		return NULL;
> +	memset(&saddr, 0, sizeof(struct sockaddr_in));
> +	memcpy(&saddr.sin_addr, addr, sizeof(*addr));

You can skip this by perfoming C99 initialization, eg.

        struct sockaddr_in sin = { .sin_family = AF_INET, };
        sin.sin_addr = *addr;

> +	saddr.sin_family = AF_INET;
> +
> +	err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in),
> +			              hostname, sizeof(hostname) - 1, NULL, 0, 0);
> +
> +	 if (err != 0) {
> +#ifdef DEBUG
> +		 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
> +#endif
> +		 return NULL;
> +	}
>  
> -	return host->h_name;
> +#ifdef DEBUG
> +		fprintf (stderr, "\naddr2host: %s\n", hostname);
> +#endif
> +		return hostname;
>  }
>  
>  static const char *ipaddr_to_network(const struct in_addr *addr)
> -- 
> 2.7.4
> 
> --
> To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso Dec. 11, 2016, 8:05 p.m. UTC | #2
On Sun, Dec 11, 2016 at 09:02:26PM +0100, Pablo Neira Ayuso wrote:
> On Fri, Dec 09, 2016 at 05:20:00PM +0530, Shyam Saini wrote:
> > Use getnameinfo() instead of deprecated gethostbyaddr()
> > 
> > Signed-off-by: Shyam Saini <mayhs11saini@gmail.com>
> > ---
> >  libxtables/xtables.c | 25 ++++++++++++++++++++-----
> >  1 file changed, 20 insertions(+), 5 deletions(-)
> > 
> > diff --git a/libxtables/xtables.c b/libxtables/xtables.c
> > index 921dfe9..338e325 100644
> > --- a/libxtables/xtables.c
> > +++ b/libxtables/xtables.c
> > @@ -1210,13 +1210,28 @@ const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
> >  
> >  static const char *ipaddr_to_host(const struct in_addr *addr)
> >  {
> > -	struct hostent *host;
> > +	static char hostname[NI_MAXHOST];
> > +	struct sockaddr_in saddr;
> > +	int err;
> >  
> > -	host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
> > -	if (host == NULL)
> > -		return NULL;
> > +	memset(&saddr, 0, sizeof(struct sockaddr_in));
> > +	memcpy(&saddr.sin_addr, addr, sizeof(*addr));
> 
> You can skip this by perfoming C99 initialization, eg.
> 
>         struct sockaddr_in sin = { .sin_family = AF_INET, };
>         sin.sin_addr = *addr;

One more comment below.

> > +	saddr.sin_family = AF_INET;
> > +
> > +	err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in),
> > +			              hostname, sizeof(hostname) - 1, NULL, 0, 0);
> > +
> > +	 if (err != 0) {
> > +#ifdef DEBUG
> > +		 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
> > +#endif

I don't remember to have used this debugging this ever, probably it is
good to remove it.

> > +		 return NULL;
> > +	}
> >  
> > -	return host->h_name;
> > +#ifdef DEBUG
> > +		fprintf (stderr, "\naddr2host: %s\n", hostname);
> > +#endif
> > +		return hostname;
     ^^^^^^^^^^^

Minor nitpick: indentation is not correct here.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Shivani Bhardwaj Dec. 11, 2016, 8:17 p.m. UTC | #3
On Mon, Dec 12, 2016 at 1:35 AM, Pablo Neira Ayuso <pablo@netfilter.org> wrote:
> On Sun, Dec 11, 2016 at 09:02:26PM +0100, Pablo Neira Ayuso wrote:
>> On Fri, Dec 09, 2016 at 05:20:00PM +0530, Shyam Saini wrote:
>> > Use getnameinfo() instead of deprecated gethostbyaddr()
>> >
>> > Signed-off-by: Shyam Saini <mayhs11saini@gmail.com>
>> > ---
>> >  libxtables/xtables.c | 25 ++++++++++++++++++++-----
>> >  1 file changed, 20 insertions(+), 5 deletions(-)
>> >
>> > diff --git a/libxtables/xtables.c b/libxtables/xtables.c
>> > index 921dfe9..338e325 100644
>> > --- a/libxtables/xtables.c
>> > +++ b/libxtables/xtables.c
>> > @@ -1210,13 +1210,28 @@ const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
>> >
>> >  static const char *ipaddr_to_host(const struct in_addr *addr)
>> >  {
>> > -   struct hostent *host;
>> > +   static char hostname[NI_MAXHOST];
>> > +   struct sockaddr_in saddr;
>> > +   int err;
>> >
>> > -   host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
>> > -   if (host == NULL)
>> > -           return NULL;
>> > +   memset(&saddr, 0, sizeof(struct sockaddr_in));
>> > +   memcpy(&saddr.sin_addr, addr, sizeof(*addr));
>>
>> You can skip this by perfoming C99 initialization, eg.
>>
>>         struct sockaddr_in sin = { .sin_family = AF_INET, };
>>         sin.sin_addr = *addr;
>
> One more comment below.
>
>> > +   saddr.sin_family = AF_INET;
>> > +
>> > +   err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in),
>> > +                                 hostname, sizeof(hostname) - 1, NULL, 0, 0);
>> > +
>> > +    if (err != 0) {
>> > +#ifdef DEBUG
>> > +            fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
>> > +#endif
>
> I don't remember to have used this debugging this ever, probably it is
> good to remove it.
>

Debugging code has been in this applied patch too:
http://git.netfilter.org/iptables/commit/?id=2d2b5e046aa56a518160716a9ddf9df53fc79c1f.
Maybe its good to remove it as well then?

>> > +            return NULL;
>> > +   }
>> >
>> > -   return host->h_name;
>> > +#ifdef DEBUG
>> > +           fprintf (stderr, "\naddr2host: %s\n", hostname);
>> > +#endif
>> > +           return hostname;
>      ^^^^^^^^^^^
>
> Minor nitpick: indentation is not correct here.
> --
> To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso Dec. 11, 2016, 8:25 p.m. UTC | #4
On Mon, Dec 12, 2016 at 01:47:00AM +0530, Shivani Bhardwaj wrote:
> On Mon, Dec 12, 2016 at 1:35 AM, Pablo Neira Ayuso <pablo@netfilter.org> wrote:
> > On Sun, Dec 11, 2016 at 09:02:26PM +0100, Pablo Neira Ayuso wrote:
> >> On Fri, Dec 09, 2016 at 05:20:00PM +0530, Shyam Saini wrote:
> >> > Use getnameinfo() instead of deprecated gethostbyaddr()
> >> >
> >> > Signed-off-by: Shyam Saini <mayhs11saini@gmail.com>
> >> > ---
> >> >  libxtables/xtables.c | 25 ++++++++++++++++++++-----
> >> >  1 file changed, 20 insertions(+), 5 deletions(-)
> >> >
> >> > diff --git a/libxtables/xtables.c b/libxtables/xtables.c
> >> > index 921dfe9..338e325 100644
> >> > --- a/libxtables/xtables.c
> >> > +++ b/libxtables/xtables.c
> >> > @@ -1210,13 +1210,28 @@ const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
> >> >
> >> >  static const char *ipaddr_to_host(const struct in_addr *addr)
> >> >  {
> >> > -   struct hostent *host;
> >> > +   static char hostname[NI_MAXHOST];
> >> > +   struct sockaddr_in saddr;
> >> > +   int err;
> >> >
> >> > -   host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
> >> > -   if (host == NULL)
> >> > -           return NULL;
> >> > +   memset(&saddr, 0, sizeof(struct sockaddr_in));
> >> > +   memcpy(&saddr.sin_addr, addr, sizeof(*addr));
> >>
> >> You can skip this by perfoming C99 initialization, eg.
> >>
> >>         struct sockaddr_in sin = { .sin_family = AF_INET, };
> >>         sin.sin_addr = *addr;
> >
> > One more comment below.
> >
> >> > +   saddr.sin_family = AF_INET;
> >> > +
> >> > +   err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in),
> >> > +                                 hostname, sizeof(hostname) - 1, NULL, 0, 0);
> >> > +
> >> > +    if (err != 0) {
> >> > +#ifdef DEBUG
> >> > +            fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
> >> > +#endif
> >
> > I don't remember to have used this debugging this ever, probably it is
> > good to remove it.
> >
> 
> Debugging code has been in this applied patch too:
> http://git.netfilter.org/iptables/commit/?id=2d2b5e046aa56a518160716a9ddf9df53fc79c1f.
> Maybe its good to remove it as well then?

Yes, please remove all debugging in libxtables/xtables.c, in an
initial patch.

Thanks.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/libxtables/xtables.c b/libxtables/xtables.c
index 921dfe9..338e325 100644
--- a/libxtables/xtables.c
+++ b/libxtables/xtables.c
@@ -1210,13 +1210,28 @@  const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
 
 static const char *ipaddr_to_host(const struct in_addr *addr)
 {
-	struct hostent *host;
+	static char hostname[NI_MAXHOST];
+	struct sockaddr_in saddr;
+	int err;
 
-	host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
-	if (host == NULL)
-		return NULL;
+	memset(&saddr, 0, sizeof(struct sockaddr_in));
+	memcpy(&saddr.sin_addr, addr, sizeof(*addr));
+	saddr.sin_family = AF_INET;
+
+	err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in),
+			              hostname, sizeof(hostname) - 1, NULL, 0, 0);
+
+	 if (err != 0) {
+#ifdef DEBUG
+		 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
+#endif
+		 return NULL;
+	}
 
-	return host->h_name;
+#ifdef DEBUG
+		fprintf (stderr, "\naddr2host: %s\n", hostname);
+#endif
+		return hostname;
 }
 
 static const char *ipaddr_to_network(const struct in_addr *addr)