Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/networking/ipcalc.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/networking/ipcalc.c
r821 r1770 1 /* vi: set sw=4 ts=4 ai: */1 /* vi: set sw=4 ts=4: */ 2 2 /* 3 3 * Mini ipcalc implementation for busybox … … 13 13 */ 14 14 15 #include "busybox.h"16 #include <ctype.h>17 15 #include <getopt.h> 18 16 #include <sys/socket.h> 19 17 #include <arpa/inet.h> 20 18 21 # define IPCALC_MSG(CMD,ALTCMD) if (mode & SILENT) {ALTCMD;} else {CMD;}19 #include "libbb.h" 22 20 23 21 #define CLASS_A_NETMASK ntohl(0xFF000000) … … 39 37 } 40 38 41 #if def CONFIG_FEATURE_IPCALC_FANCY39 #if ENABLE_FEATURE_IPCALC_FANCY 42 40 static int get_prefix(unsigned long netmask) 43 41 { … … 46 44 47 45 netmask = htonl(netmask); 48 while (msk) {46 while (msk) { 49 47 if (netmask & msk) 50 48 ret++; … … 57 55 #endif 58 56 57 59 58 #define NETMASK 0x01 60 59 #define BROADCAST 0x02 … … 65 64 66 65 #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS 67 static const struct option long_options[] = { 68 {"netmask", no_argument, NULL, 'm'}, 69 {"broadcast", no_argument, NULL, 'b'}, 70 {"network", no_argument, NULL, 'n'}, 71 #ifdef CONFIG_FEATURE_IPCALC_FANCY 72 {"prefix", no_argument, NULL, 'p'}, 73 {"hostname", no_argument, NULL, 'h'}, 74 {"silent", no_argument, NULL, 's'}, 75 #endif 76 {NULL, 0, NULL, 0} 77 }; 78 #else 79 #define long_options 0 66 static const char ipcalc_longopts[] ALIGN1 = 67 "netmask\0" No_argument "m" 68 "broadcast\0" No_argument "b" 69 "network\0" No_argument "n" 70 # if ENABLE_FEATURE_IPCALC_FANCY 71 "prefix\0" No_argument "p" 72 "hostname\0" No_argument "h" 73 "silent\0" No_argument "s" 74 # endif 75 ; 80 76 #endif 81 77 82 83 78 int ipcalc_main(int argc, char **argv); 84 79 int ipcalc_main(int argc, char **argv) 85 80 { 86 unsigned long mode;81 unsigned opt; 87 82 int have_netmask = 0; 88 83 in_addr_t netmask, broadcast, network, ipaddr; … … 90 85 char *ipstr; 91 86 92 if (ENABLE_FEATURE_IPCALC_LONG_OPTIONS) 93 bb_applet_long_options = long_options; 94 95 mode = bb_getopt_ulflags(argc, argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs")); 96 87 #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS 88 applet_long_options = ipcalc_longopts; 89 #endif 90 opt = getopt32(argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs")); 97 91 argc -= optind; 98 92 argv += optind; 99 if ( mode& (BROADCAST | NETWORK | NETPREFIX)) {93 if (opt & (BROADCAST | NETWORK | NETPREFIX)) { 100 94 if (argc > 2 || argc <= 0) 101 95 bb_show_usage(); … … 104 98 bb_show_usage(); 105 99 } 100 if (opt & SILENT) 101 logmode = LOGMODE_NONE; /* Suppress error_msg() output */ 106 102 107 103 ipstr = argv[0]; … … 112 108 prefixstr = ipstr; 113 109 114 while (*prefixstr) {110 while (*prefixstr) { 115 111 if (*prefixstr == '/') { 116 112 *prefixstr = (char)0; 117 113 prefixstr++; 118 114 if (*prefixstr) { 119 unsigned int msk; 120 121 if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) { 122 IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr), 123 exit(EXIT_FAILURE)); 124 } 115 unsigned msk; 116 netprefix = xatoul_range(prefixstr, 0, 32); 125 117 netmask = 0; 126 118 msk = 0x80000000; … … 143 135 144 136 if (ipaddr == 0) { 145 IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[0]), 146 exit(EXIT_FAILURE)); 137 bb_error_msg_and_die("bad IP address: %s", argv[0]); 147 138 } 148 139 ipaddr = a.s_addr; … … 150 141 if (argc == 2) { 151 142 if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) { 152 IPCALC_MSG(bb_error_msg_and_die("Use prefix or netmask, not both.\n"), 153 exit(EXIT_FAILURE)); 143 bb_error_msg_and_die("use prefix or netmask, not both"); 154 144 } 155 145 156 146 netmask = inet_aton(argv[1], &a); 157 147 if (netmask == 0) { 158 IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[1]), 159 exit(EXIT_FAILURE)); 148 bb_error_msg_and_die("bad netmask: %s", argv[1]); 160 149 } 161 150 netmask = a.s_addr; … … 167 156 } 168 157 169 if ( mode& NETMASK) {158 if (opt & NETMASK) { 170 159 printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask))); 171 160 } 172 161 173 if ( mode& BROADCAST) {162 if (opt & BROADCAST) { 174 163 broadcast = (ipaddr & netmask) | ~netmask; 175 164 printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast))); 176 165 } 177 166 178 if ( mode& NETWORK) {167 if (opt & NETWORK) { 179 168 network = ipaddr & netmask; 180 169 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network))); … … 182 171 183 172 if (ENABLE_FEATURE_IPCALC_FANCY) { 184 if ( mode& NETPREFIX) {173 if (opt & NETPREFIX) { 185 174 printf("PREFIX=%i\n", get_prefix(netmask)); 186 175 } 187 176 188 if ( mode& HOSTNAME) {177 if (opt & HOSTNAME) { 189 178 struct hostent *hostinfo; 190 179 int x; … … 192 181 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET); 193 182 if (!hostinfo) { 194 IPCALC_MSG(bb_herror_msg_and_die( 195 "cannot find hostname for %s", argv[0]),); 196 exit(EXIT_FAILURE); 183 bb_herror_msg_and_die("cannot find hostname for %s", argv[0]); 197 184 } 198 185 for (x = 0; hostinfo->h_name[x]; x++) {
Note:
See TracChangeset
for help on using the changeset viewer.