Permalink
Browse files

use getnameinfo instead of casting to sockaddr_in

It seems that while you can cast from sockaddr_in to sockaddr, a cast
in the opposite direction may change the size of the type and hence
alignment on 64 bit platforms.  This is fine, I now use getnameinfo to
return a char * of the IP address.

Also bump the version to 0.12.2, and re-add -Wcast-align to the warning
flags.
  • Loading branch information...
1 parent a72b51b commit e86946b55edc1b1679bb19ddc920666051eb150f @jaqx0r jaqx0r committed Jun 10, 2004
Showing with 27 additions and 17 deletions.
  1. +2 −2 configure.in
  2. +25 −15 resolver.c
View
@@ -1,6 +1,6 @@
AC_PREREQ(2.50)
-AC_INIT(filtergen, 0.12.1, jaq@spacepants.org)
+AC_INIT(filtergen, 0.12.2, jaq@spacepants.org)
AM_INIT_AUTOMAKE([foreign dist-bzip2 dist-zip])
AC_CONFIG_AUX_DIR(.)
@@ -48,7 +48,7 @@ dnl -----------------
if test "x$ac_cv_c_compiler_gnu" = xyes ; then
CFLAGS="$CFLAGS -W -Wall -Werror -Waggregate-return"
- CFLAGS="$CFLAGS -Wcast-qual -Wnested-externs"
+ CFLAGS="$CFLAGS -Wcast-align -Wcast-qual -Wnested-externs"
CFLAGS="$CFLAGS -Wshadow -Wbad-function-cast -Wwrite-strings"
fi
View
@@ -26,6 +26,7 @@
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
+#include <errno.h>
#include "resolver.h"
#include "ast.h"
#include "icmpent.h"
@@ -184,21 +185,30 @@ void resolve_host_argument_list(struct host_argument_list_s * n) {
case 0:
/* replace the hostname with the IP */
free(n->arg->host);
- n->arg->host = strdup(inet_ntoa(((struct sockaddr_in *) a->ai_addr)->sin_addr));
- /* if there's more, create some more hosts */
- for (i = a->ai_next; i; i = i->ai_next) {
- list = malloc(sizeof(struct host_argument_list_s));
- host = malloc(sizeof(struct host_argument_s));
- host->host = strdup(inet_ntoa(((struct sockaddr_in *) i->ai_addr)->sin_addr));
- if (n->arg->mask) {
- host->mask = strdup(n->arg->mask);
- }
-
- /* insert the new node */
- list->arg = host;
- list->list = n->list;
- n->list = list;
- }
+ /* getnameinfo does no allocation. */
+ n->arg->host = malloc(NI_MAXHOST + 1);
+ if (getnameinfo(a->ai_addr, a->ai_addrlen, n->arg->host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0) {
+ /* if there's more, create some more hosts */
+ for (i = a->ai_next; i; i = i->ai_next) {
+ list = malloc(sizeof(struct host_argument_list_s));
+ host = malloc(sizeof(struct host_argument_s));
+ host->host = malloc(NI_MAXHOST + 1);
+ if (getnameinfo(i->ai_addr, i->ai_addrlen, host->host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0) {
+ if (n->arg->mask) {
+ host->mask = strdup(n->arg->mask);
+ }
+
+ /* insert the new node */
+ list->arg = host;
+ list->list = n->list;
+ n->list = list;
+ } else {
+ fprintf(stderr, "warning: %s\n", strerror(errno));
+ }
+ }
+ } else {
+ fprintf(stderr, "warning: %s\n", strerror(errno));
+ }
freeaddrinfo(a);
break;
default:

0 comments on commit e86946b

Please sign in to comment.