- fix typo in helptext
[oweals/busybox.git] / networking / ifconfig.c
index 9cdd41c3a82545b1001902a751a76a278d45159e..3dcb118ee0f127d1d85be59d1a06e83ebc321464 100644 (file)
@@ -37,7 +37,7 @@
 #include <netinet/if_ether.h>
 #endif
 #include "inet_common.h"
-#include "busybox.h"
+#include "libbb.h"
 
 #if ENABLE_FEATURE_IFCONFIG_SLIP
 # include <net/if_slip.h>
@@ -261,13 +261,11 @@ static int in_ether(const char *bufp, struct sockaddr *sap);
  * Our main function.
  */
 
+int ifconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int ifconfig_main(int argc, char **argv)
 {
        struct ifreq ifr;
        struct sockaddr_in sai;
-#if ENABLE_FEATURE_IPV6
-       struct sockaddr_in6 sai6;
-#endif
 #if ENABLE_FEATURE_IFCONFIG_HW
        struct sockaddr sa;
 #endif
@@ -339,7 +337,7 @@ int ifconfig_main(int argc, char **argv)
                }
 
                /* We fell through, so treat as possible hostname. */
-               a1op = Arg1Opt + (sizeof(Arg1Opt) / sizeof(Arg1Opt[0])) - 1;
+               a1op = Arg1Opt + ARRAY_SIZE(Arg1Opt) - 1;
                mask = op->arg_flags;
                goto HOSTNAME;
 
@@ -388,31 +386,34 @@ int ifconfig_main(int argc, char **argv)
                                                        sai.sin_addr.s_addr = (~sai_netmask) | (sai_hostname & sai_netmask);
                                                }
 #endif
+                                               else {
+                                                       len_and_sockaddr *lsa;
+                                                       if (strcmp(host, "inet") == 0)
+                                                               continue; /* compat stuff */
+                                                       lsa = xhost2sockaddr(host, 0);
 #if ENABLE_FEATURE_IPV6
-                                               else if (inet_pton(AF_INET6, host, &sai6.sin6_addr) > 0) {
-                                                       int sockfd6;
-                                                       struct in6_ifreq ifr6;
-
-                                                       memcpy((char *) &ifr6.ifr6_addr,
-                                                                       (char *) &sai6.sin6_addr,
-                                                                       sizeof(struct in6_addr));
-
-                                                       /* Create a channel to the NET kernel. */
-                                                       sockfd6 = xsocket(AF_INET6, SOCK_DGRAM, 0);
-                                                       if (ioctl(sockfd6, SIOGIFINDEX, &ifr) < 0)
-                                                               bb_perror_msg_and_die("SIOGIFINDEX");
-                                                       ifr6.ifr6_ifindex = ifr.ifr_ifindex;
-                                                       ifr6.ifr6_prefixlen = prefix_len;
-                                                       if (ioctl(sockfd6, a1op->selector, &ifr6) < 0)
-                                                               bb_perror_msg_and_die(a1op->name);
-                                                       continue;
-                                               }
-#endif
-                                               else if (inet_aton(host, &sai.sin_addr) == 0) {
-                                                       /* It's not a dotted quad. */
-                                                       struct hostent *hp = xgethostbyname(host);
-                                                       memcpy((char *) &sai.sin_addr, (char *) hp->h_addr_list[0],
-                                                                       sizeof(struct in_addr));
+                                                       if (lsa->sa.sa_family == AF_INET6) {
+                                                               int sockfd6;
+                                                               struct in6_ifreq ifr6;
+
+                                                               memcpy((char *) &ifr6.ifr6_addr,
+                                                                               (char *) &(lsa->sin6.sin6_addr),
+                                                                               sizeof(struct in6_addr));
+
+                                                               /* Create a channel to the NET kernel. */
+                                                               sockfd6 = xsocket(AF_INET6, SOCK_DGRAM, 0);
+                                                               xioctl(sockfd6, SIOGIFINDEX, &ifr);
+                                                               ifr6.ifr6_ifindex = ifr.ifr_ifindex;
+                                                               ifr6.ifr6_prefixlen = prefix_len;
+                                                               ioctl_or_perror_and_die(sockfd6, a1op->selector, &ifr6, "%s", a1op->name);
+                                                               if (ENABLE_FEATURE_CLEAN_UP)
+                                                                       free(lsa);
+                                                               continue;
+                                                       }
+#endif
+                                                       sai.sin_addr = lsa->sin.sin_addr;
+                                                       if (ENABLE_FEATURE_CLEAN_UP)
+                                                               free(lsa);
                                                }
 #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
                                                if (mask & A_HOSTNAME)
@@ -441,8 +442,7 @@ int ifconfig_main(int argc, char **argv)
                                        p = ((char *)&ifr) + a1op->ifr_offset;
 #if ENABLE_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
                                        if (mask & A_MAP_TYPE) {
-                                               if (ioctl(sockfd, SIOCGIFMAP, &ifr) < 0)
-                                                       bb_perror_msg_and_die("SIOCGIFMAP");
+                                               xioctl(sockfd, SIOCGIFMAP, &ifr);
                                                if ((mask & A_MAP_UCHAR) == A_MAP_UCHAR)
                                                        *((unsigned char *) p) = i;
                                                else if (mask & A_MAP_USHORT)
@@ -457,8 +457,7 @@ int ifconfig_main(int argc, char **argv)
                                                *((int *) p) = i;
                                }
 
-                               if (ioctl(sockfd, a1op->selector, &ifr) < 0)
-                                       bb_perror_msg_and_die(a1op->name);
+                               ioctl_or_perror_and_die(sockfd, a1op->selector, &ifr, "%s", a1op->name);
 #ifdef QUESTIONABLE_ALIAS_CASE
                                if (mask & A_COLON_CHK) {
                                        /*
@@ -483,15 +482,13 @@ int ifconfig_main(int argc, char **argv)
                        mask = N_SET;
                }
 
-               if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0)
-                       bb_perror_msg_and_die("SIOCGIFFLAGS");
+               xioctl(sockfd, SIOCGIFFLAGS, &ifr);
                selector = op->selector;
                if (mask & SET_MASK)
                        ifr.ifr_flags |= selector;
                else
                        ifr.ifr_flags &= ~selector;
-               if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0)
-                       bb_perror_msg_and_die("SIOCSIFFLAGS");
+               xioctl(sockfd, SIOCSIFFLAGS, &ifr);
        } /* while () */
 
        if (ENABLE_FEATURE_CLEAN_UP)