Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/networking/ipcalc.c

    r1765 r2725  
    1010 * is no denying that this is a loving reimplementation
    1111 *
    12  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
     12 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    1313 */
    14 
    15 #include <getopt.h>
    16 #include <sys/socket.h>
     14#include "libbb.h"
     15/* After libbb.h, because on some systems it needs other includes */
    1716#include <arpa/inet.h>
    1817
    19 #include "libbb.h"
    20 
    21 #define CLASS_A_NETMASK ntohl(0xFF000000)
    22 #define CLASS_B_NETMASK ntohl(0xFFFF0000)
    23 #define CLASS_C_NETMASK ntohl(0xFFFFFF00)
     18#define CLASS_A_NETMASK ntohl(0xFF000000)
     19#define CLASS_B_NETMASK ntohl(0xFFFF0000)
     20#define CLASS_C_NETMASK ntohl(0xFFFFFF00)
    2421
    2522static unsigned long get_netmask(unsigned long ipaddr)
     
    6562#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
    6663    static const char ipcalc_longopts[] ALIGN1 =
    67         "netmask\0"   No_argument "m"
    68         "broadcast\0" No_argument "b"
    69         "network\0"   No_argument "n"
     64        "netmask\0"   No_argument "m" // netmask from IP (assuming complete class A, B, or C network)
     65        "broadcast\0" No_argument "b" // broadcast from IP [netmask]
     66        "network\0"   No_argument "n" // network from IP [netmask]
    7067# if ENABLE_FEATURE_IPCALC_FANCY
    71         "prefix\0"    No_argument "p"
    72         "hostname\0"  No_argument "h"
    73         "silent\0"    No_argument "s"
     68        "prefix\0"    No_argument "p" // prefix from IP[/prefix] [netmask]
     69        "hostname\0"  No_argument "h" // hostname from IP
     70        "silent\0"    No_argument "s" // don’t ever display error messages
    7471# endif
    7572        ;
    7673#endif
    7774
    78 int ipcalc_main(int argc, char **argv);
    79 int ipcalc_main(int argc, char **argv)
     75int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     76int ipcalc_main(int argc UNUSED_PARAM, char **argv)
    8077{
    8178    unsigned opt;
    82     int have_netmask = 0;
    83     in_addr_t netmask, broadcast, network, ipaddr;
    84     struct in_addr a;
     79    bool have_netmask = 0;
     80    struct in_addr s_netmask, s_broadcast, s_network, s_ipaddr;
     81    /* struct in_addr { in_addr_t s_addr; }  and  in_addr_t
     82     * (which in turn is just a typedef to uint32_t)
     83     * are essentially the same type. A few macros for less verbosity: */
     84#define netmask   (s_netmask.s_addr)
     85#define broadcast (s_broadcast.s_addr)
     86#define network   (s_network.s_addr)
     87#define ipaddr    (s_ipaddr.s_addr)
    8588    char *ipstr;
    8689
     
    8891    applet_long_options = ipcalc_longopts;
    8992#endif
    90     opt = getopt32(argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs"));
    91     argc -= optind;
     93    opt_complementary = "-1:?2"; /* minimum 1 arg, maximum 2 args */
     94    opt = getopt32(argv, "mbn" IF_FEATURE_IPCALC_FANCY("phs"));
    9295    argv += optind;
    93     if (opt & (BROADCAST | NETWORK | NETPREFIX)) {
    94         if (argc > 2 || argc <= 0)
    95             bb_show_usage();
    96     } else {
    97         if (argc != 1)
     96    if (opt & SILENT)
     97        logmode = LOGMODE_NONE; /* suppress error_msg() output */
     98    opt &= ~SILENT;
     99    if (!(opt & (BROADCAST | NETWORK | NETPREFIX))) {
     100        /* if no options at all or
     101         * (no broadcast,network,prefix) and (two args)... */
     102        if (!opt || argv[1])
    98103            bb_show_usage();
    99104    }
    100     if (opt & SILENT)
    101         logmode = LOGMODE_NONE; /* Suppress error_msg() output */
    102105
    103106    ipstr = argv[0];
     
    110113        while (*prefixstr) {
    111114            if (*prefixstr == '/') {
    112                 *prefixstr = (char)0;
    113                 prefixstr++;
     115                *prefixstr++ = '\0';
    114116                if (*prefixstr) {
    115117                    unsigned msk;
     
    132134        }
    133135    }
    134     ipaddr = inet_aton(ipstr, &a);
    135136
    136     if (ipaddr == 0) {
     137    if (inet_aton(ipstr, &s_ipaddr) == 0) {
    137138        bb_error_msg_and_die("bad IP address: %s", argv[0]);
    138139    }
    139     ipaddr = a.s_addr;
    140140
    141     if (argc == 2) {
     141    if (argv[1]) {
    142142        if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
    143143            bb_error_msg_and_die("use prefix or netmask, not both");
    144144        }
    145 
    146         netmask = inet_aton(argv[1], &a);
    147         if (netmask == 0) {
     145        if (inet_aton(argv[1], &s_netmask) == 0) {
    148146            bb_error_msg_and_die("bad netmask: %s", argv[1]);
    149147        }
    150         netmask = a.s_addr;
    151148    } else {
    152 
    153149        /* JHC - If the netmask wasn't provided then calculate it */
    154150        if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
     
    157153
    158154    if (opt & NETMASK) {
    159         printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
     155        printf("NETMASK=%s\n", inet_ntoa(s_netmask));
    160156    }
    161157
    162158    if (opt & BROADCAST) {
    163159        broadcast = (ipaddr & netmask) | ~netmask;
    164         printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
     160        printf("BROADCAST=%s\n", inet_ntoa(s_broadcast));
    165161    }
    166162
    167163    if (opt & NETWORK) {
    168164        network = ipaddr & netmask;
    169         printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
     165        printf("NETWORK=%s\n", inet_ntoa(s_network));
    170166    }
    171167
     
    177173        if (opt & HOSTNAME) {
    178174            struct hostent *hostinfo;
    179             int x;
    180175
    181176            hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
    182177            if (!hostinfo) {
    183                 bb_herror_msg_and_die("cannot find hostname for %s", argv[0]);
     178                bb_herror_msg_and_die("can't find hostname for %s", argv[0]);
    184179            }
    185             for (x = 0; hostinfo->h_name[x]; x++) {
    186                 hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
    187             }
     180            str_tolower(hostinfo->h_name);
    188181
    189182            printf("HOSTNAME=%s\n", hostinfo->h_name);
Note: See TracChangeset for help on using the changeset viewer.