Author: cazfi
Date: Mon Mar  2 04:16:11 2015
New Revision: 28351

URL: http://svn.gna.org/viewcvs/freeciv?rev=28351&view=rev
Log:
Introduced fc_inet_aton()

See patch #5866

Modified:
    trunk/client/servers.c
    trunk/server/sernet.c
    trunk/utility/netintf.c
    trunk/utility/netintf.h

Modified: trunk/client/servers.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/servers.c?rev=28351&r1=28350&r2=28351&view=diff
==============================================================================
--- trunk/client/servers.c      (original)
+++ trunk/client/servers.c      Mon Mar  2 04:16:11 2015
@@ -389,21 +389,15 @@
   group = get_multicast_group(announce == ANNOUNCE_IPV6);
   memset(&addr, 0, sizeof(addr));
 
-#ifndef IPV6_SUPPORT
-  if (family == AF_INET) {
-#ifdef HAVE_INET_ATON
-    inet_aton(group, &addr.saddr_in4.sin_addr);
-#else  /* HAVE_INET_ATON */
-    addr.saddr_in4.sin_addr.s_addr = inet_addr(group);
-#endif /* HAVE_INET_ATON */
-#else  /* IPv6 support */
+#ifdef IPV6_SUPPORT
   if (family == AF_INET6) {
     addr.saddr.sa_family = AF_INET6;
     inet_pton(AF_INET6, group, &addr.saddr_in6.sin6_addr);
     addr.saddr_in6.sin6_port = htons(SERVER_LAN_PORT);
-  } else if (family == AF_INET) {
-    inet_pton(AF_INET, group, &addr.saddr_in4.sin_addr);
-#endif /* IPv6 support */
+  } else
+#endif /* IPv6 Support */
+  if (family == AF_INET) {
+    fc_inet_aton(group, &addr.saddr_in4.sin_addr, FALSE);
     addr.saddr.sa_family = AF_INET;
     addr.saddr_in4.sin_port = htons(SERVER_LAN_PORT);
   } else {
@@ -490,14 +484,7 @@
     return FALSE;
   }
 
-#ifndef IPV6_SUPPORT
-  {
-#ifdef HAVE_INET_ATON
-    inet_aton(group, &mreq4.imr_multiaddr);
-#else  /* HAVE_INET_ATON */
-    mreq4.imr_multiaddr.s_addr = inet_addr(group);
-#endif /* HAVE_INET_ATON */
-#else  /* IPv6 support */
+#ifdef IPV6_SUPPORT
   if (family == AF_INET6) {
     inet_pton(AF_INET6, group, &mreq6.ipv6mr_multiaddr.s6_addr);
     mreq6.ipv6mr_interface = 0; /* TODO: Interface selection */
@@ -506,9 +493,10 @@
                    (const char*)&mreq6, sizeof(mreq6)) < 0) {
       scan->error_func(scan, fc_strerror(fc_get_errno()));
     }
-  } else {
-    inet_pton(AF_INET, group, &mreq4.imr_multiaddr.s_addr);
+  } else
 #endif /* IPv6 support */
+  {
+    fc_inet_aton(group, &mreq4.imr_multiaddr, FALSE);
     mreq4.imr_interface.s_addr = htonl(INADDR_ANY);
 
     if (setsockopt(scan->sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,

Modified: trunk/server/sernet.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/sernet.c?rev=28351&r1=28350&r2=28351&view=diff
==============================================================================
--- trunk/server/sernet.c       (original)
+++ trunk/server/sernet.c       Mon Mar  2 04:16:11 2015
@@ -1246,14 +1246,7 @@
     log_error("Lan bind failed: %s", fc_strerror(fc_get_errno()));
   }
 
-#ifndef IPV6_SUPPORT
-  if (addr.saddr.sa_family == AF_INET) {
-#ifdef HAVE_INET_ATON
-    inet_aton(group, &mreq4.imr_multiaddr);
-#else  /* HAVE_INET_ATON */
-    mreq4.imr_multiaddr.s_addr = inet_addr(group);
-#endif /* HAVE_INET_ATON */
-#else  /* IPv6 support */
+#ifdef IPV6_SUPPORT
   if (addr.saddr.sa_family == AF_INET6) {
     inet_pton(AF_INET6, group, &mreq6.ipv6mr_multiaddr.s6_addr);
     mreq6.ipv6mr_interface = 0; /* TODO: Interface selection */
@@ -1262,9 +1255,10 @@
       log_error("FC_IPV6_ADD_MEMBERSHIP (%s) failed: %s",
                 group, fc_strerror(fc_get_errno()));
     }
-  } else if (addr.saddr.sa_family == AF_INET) {
-    inet_pton(AF_INET, group, &mreq4.imr_multiaddr.s_addr);
-#endif /* IPv6 support */
+  } else
+#endif /* IPV6 Support */
+  if (addr.saddr.sa_family == AF_INET) {
+    fc_inet_aton(group, &mreq4.imr_multiaddr, FALSE);
     mreq4.imr_interface.s_addr = htonl(INADDR_ANY);
 
     if (setsockopt(socklan, IPPROTO_IP, IP_ADD_MEMBERSHIP,

Modified: trunk/utility/netintf.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/utility/netintf.c?rev=28351&r1=28350&r2=28351&view=diff
==============================================================================
--- trunk/utility/netintf.c     (original)
+++ trunk/utility/netintf.c     Mon Mar  2 04:16:11 2015
@@ -427,19 +427,12 @@
     return addrs;
   }
 
-#if defined(HAVE_INET_ATON)
-  if (inet_aton(name, &sock4->sin_addr) != 0) {
+  if (fc_inet_aton(name, &sock4->sin_addr, FALSE)) {
     fc_sockaddr_list_append(addrs, result);
 
     return addrs;
   }
-#else  /* HAVE_INET_ATON */
-  if ((sock4->sin_addr.s_addr = inet_addr(name)) != INADDR_NONE) {
-    fc_sockaddr_list_append(addrs, result);
-
-    return addrs;
-  }
-#endif /* HAVE_INET_ATON */
+
   hp = gethostbyname(name);
   if (!hp || hp->h_addrtype != AF_INET) {
     FC_FREE(result);
@@ -454,6 +447,34 @@
 
 #endif /* !HAVE_GETADDRINFO */
 
+}
+
+/*************************************************************************
+  Convert internet IPv4 host address to binary form and store it to inp.
+  Return FALSE on failure if possible, i.e., FALSE is guarantee that it
+  failed but TRUE is not guarantee that it succeeded.
+*************************************************************************/
+bool fc_inet_aton(const char *cp, struct in_addr *inp, bool addr_none_ok)
+{
+#ifdef IPV6_SUPPORT
+  /* Use inet_pton() */
+  if (!inet_pton(AF_INET, cp, &inp->s_addr)) {
+    return FALSE;
+  }
+#else  /* IPv6 Support */
+#ifdef HAVE_INET_ATON
+  if (!inet_aton(cp, inp)) {
+    return FALSE;
+  }
+#else  /* HAVE_INET_ATON */
+  inp->s_addr = inet_addr(cp);
+  if (!addr_none_ok && inp->s_addr == INADDR_NONE) {
+    return FALSE;
+  }
+#endif /* HAVE_INET_ATON */
+#endif /* IPv6 Support */
+
+  return TRUE;
 }
 
 /*************************************************************************
@@ -603,12 +624,7 @@
     sock4->sin_family = AF_INET;
     sock4->sin_port = htons(port);
     if (net_interface != NULL) {
-#if defined(HAVE_INET_ATON)
-      if (inet_aton(net_interface, &sock4->sin_addr) == 0) {
-#else /* HAVE_INET_ATON */
-      sock4->sin_addr.s_addr = inet_addr(net_interface);
-      if (sock4->sin_addr.s_addr == INADDR_NONE) {
-#endif /* HAVE_INET_ATON */
+      if (!fc_inet_aton(net_interface, &sock4->sin_addr, FALSE)) {
         struct hostent *hp;
 
         hp = gethostbyname(net_interface);

Modified: trunk/utility/netintf.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/utility/netintf.h?rev=28351&r1=28350&r2=28351&view=diff
==============================================================================
--- trunk/utility/netintf.h     (original)
+++ trunk/utility/netintf.h     Mon Mar  2 04:16:11 2015
@@ -125,6 +125,7 @@
 void fc_nonblock(int sockfd);
 struct fc_sockaddr_list *net_lookup_service(const char *name, int port,
                                            enum fc_addr_family family);
+bool fc_inet_aton(const char *cp, struct in_addr *inp, bool addr_none_ok);
 fz_FILE *fc_querysocket(int sock, void *buf, size_t size);
 int find_next_free_port(int starting_port, enum fc_addr_family family,
                         char *net_interface, bool not_avail_ok);


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to