Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/networking/ipcalc.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/networking/ipcalc.c
r1765 r2725 10 10 * is no denying that this is a loving reimplementation 11 11 * 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. 13 13 */ 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 */ 17 16 #include <arpa/inet.h> 18 17 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) 24 21 25 22 static unsigned long get_netmask(unsigned long ipaddr) … … 65 62 #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS 66 63 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] 70 67 # 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 74 71 # endif 75 72 ; 76 73 #endif 77 74 78 int ipcalc_main(int argc, char **argv) ;79 int ipcalc_main(int argc , char **argv)75 int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 76 int ipcalc_main(int argc UNUSED_PARAM, char **argv) 80 77 { 81 78 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) 85 88 char *ipstr; 86 89 … … 88 91 applet_long_options = ipcalc_longopts; 89 92 #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")); 92 95 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]) 98 103 bb_show_usage(); 99 104 } 100 if (opt & SILENT)101 logmode = LOGMODE_NONE; /* Suppress error_msg() output */102 105 103 106 ipstr = argv[0]; … … 110 113 while (*prefixstr) { 111 114 if (*prefixstr == '/') { 112 *prefixstr = (char)0; 113 prefixstr++; 115 *prefixstr++ = '\0'; 114 116 if (*prefixstr) { 115 117 unsigned msk; … … 132 134 } 133 135 } 134 ipaddr = inet_aton(ipstr, &a);135 136 136 if (i paddr== 0) {137 if (inet_aton(ipstr, &s_ipaddr) == 0) { 137 138 bb_error_msg_and_die("bad IP address: %s", argv[0]); 138 139 } 139 ipaddr = a.s_addr;140 140 141 if (arg c == 2) {141 if (argv[1]) { 142 142 if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) { 143 143 bb_error_msg_and_die("use prefix or netmask, not both"); 144 144 } 145 146 netmask = inet_aton(argv[1], &a); 147 if (netmask == 0) { 145 if (inet_aton(argv[1], &s_netmask) == 0) { 148 146 bb_error_msg_and_die("bad netmask: %s", argv[1]); 149 147 } 150 netmask = a.s_addr;151 148 } else { 152 153 149 /* JHC - If the netmask wasn't provided then calculate it */ 154 150 if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask) … … 157 153 158 154 if (opt & NETMASK) { 159 printf("NETMASK=%s\n", inet_ntoa( (*(struct in_addr *) &netmask)));155 printf("NETMASK=%s\n", inet_ntoa(s_netmask)); 160 156 } 161 157 162 158 if (opt & BROADCAST) { 163 159 broadcast = (ipaddr & netmask) | ~netmask; 164 printf("BROADCAST=%s\n", inet_ntoa( (*(struct in_addr *) &broadcast)));160 printf("BROADCAST=%s\n", inet_ntoa(s_broadcast)); 165 161 } 166 162 167 163 if (opt & NETWORK) { 168 164 network = ipaddr & netmask; 169 printf("NETWORK=%s\n", inet_ntoa( (*(struct in_addr *) &network)));165 printf("NETWORK=%s\n", inet_ntoa(s_network)); 170 166 } 171 167 … … 177 173 if (opt & HOSTNAME) { 178 174 struct hostent *hostinfo; 179 int x;180 175 181 176 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET); 182 177 if (!hostinfo) { 183 bb_herror_msg_and_die("can not find hostname for %s", argv[0]);178 bb_herror_msg_and_die("can't find hostname for %s", argv[0]); 184 179 } 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); 188 181 189 182 printf("HOSTNAME=%s\n", hostinfo->h_name);
Note:
See TracChangeset
for help on using the changeset viewer.