* Mini ipcalc implementation for busybox
* By Jordan Crouse <jordan@cosmicpenguin.net>
* Stephan Linz <linz@li-pro.net>
* This is a complete reimplementation of the ipcalc program
* from Red Hat. I didn't look at their source code, but there
* is no denying that this is a loving reimplementation
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
#define CLASS_A_NETMASK ntohl(0xFF000000)
#define CLASS_B_NETMASK ntohl(0xFFFF0000)
#define CLASS_C_NETMASK ntohl(0xFFFFFF00)
static unsigned long get_netmask(unsigned long ipaddr)
if ((ipaddr & 0xC0000000) == 0xC0000000)
else if ((ipaddr & 0x80000000) == 0x80000000)
else if ((ipaddr & 0x80000000) == 0)
#ifdef CONFIG_FEATURE_IPCALC_FANCY
static int get_prefix(unsigned long netmask)
unsigned long msk = 0x80000000;
netmask = htonl(netmask);
int get_prefix(unsigned long netmask);
#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
static const struct option long_options[] = {
{ "netmask", no_argument, NULL, 'm' },
{ "broadcast", no_argument, NULL, 'b' },
{ "network", no_argument, NULL, 'n' },
# if ENABLE_FEATURE_IPCALC_FANCY
{ "prefix", no_argument, NULL, 'p' },
{ "hostname", no_argument, NULL, 'h' },
{ "silent", no_argument, NULL, 's' },
int ipcalc_main(int argc, char **argv)
in_addr_t netmask, broadcast, network, ipaddr;
#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
applet_long_options = long_options;
opt = getopt32(argc, argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs"));
if (opt & (BROADCAST | NETWORK | NETPREFIX)) {
if (argc > 2 || argc <= 0)
logmode = LOGMODE_NONE; /* Suppress error_msg() output */
if (ENABLE_FEATURE_IPCALC_FANCY) {
unsigned long netprefix = 0;
netprefix = xatoul_range(prefixstr, 0, 32);
netmask = htonl(netmask);
/* Even if it was 0, we will signify that we have a netmask. This allows */
/* for specification of default routes, etc which have a 0 netmask/prefix */
ipaddr = inet_aton(ipstr, &a);
bb_error_msg_and_die("bad IP address: %s", argv[0]);
if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
bb_error_msg_and_die("use prefix or netmask, not both");
netmask = inet_aton(argv[1], &a);
bb_error_msg_and_die("bad netmask: %s", argv[1]);
/* JHC - If the netmask wasn't provided then calculate it */
if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
netmask = get_netmask(ipaddr);
printf("NETMASK=%sn", inet_ntoa((*(struct in_addr *) &netmask)));
broadcast = (ipaddr & netmask) | ~netmask;
printf("BROADCAST=%sn", inet_ntoa((*(struct in_addr *) &broadcast)));
network = ipaddr & netmask;
printf("NETWORK=%sn", inet_ntoa((*(struct in_addr *) &network)));
if (ENABLE_FEATURE_IPCALC_FANCY) {
printf("PREFIX=%in", get_prefix(netmask));
struct hostent *hostinfo;
hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
bb_herror_msg_and_die("cannot find hostname for %s", argv[0]);
for (x = 0; hostinfo->h_name[x]; x++) {
hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
printf("HOSTNAME=%sn", hostinfo->h_name);
阅读源码后发现,选项PREFIX的配置值在此处并未生效,此工具完全根据输入的ip地址和掩码进行分析,应该不会出现ifcfg-eth0中的掩码配置不生效的问题。
测试环境暂时搭建完成,NETMASK和PREFIX配置冲突问题还未找到合理的解释!!!