aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Cantrell <dcantrell@redhat.com>2008-09-26 15:04:45 -1000
committerBill Nottingham <notting@redhat.com>2009-03-19 15:49:45 -0400
commit9471d0fb6c32d610cb5f14bc0122f222236025d0 (patch)
tree0865be5291b47d5187f72f98f77fe01c536402f8
parent73889d4492fc397fa7f31515d2ea0a3763d235c5 (diff)
downloadinitscripts-9471d0fb6c32d610cb5f14bc0122f222236025d0.tar
initscripts-9471d0fb6c32d610cb5f14bc0122f222236025d0.tar.gz
initscripts-9471d0fb6c32d610cb5f14bc0122f222236025d0.tar.bz2
initscripts-9471d0fb6c32d610cb5f14bc0122f222236025d0.tar.xz
initscripts-9471d0fb6c32d610cb5f14bc0122f222236025d0.zip
Whitespace cleanups for src/ipcalc.c
Consistently use 4 spaces for indentation throughout ipcalc.c
-rw-r--r--src/ipcalc.c265
1 files changed, 130 insertions, 135 deletions
diff --git a/src/ipcalc.c b/src/ipcalc.c
index 9497b8ef..f97f8bc3 100644
--- a/src/ipcalc.c
+++ b/src/ipcalc.c
@@ -18,7 +18,7 @@
* Erik Troan <ewt@redhat.com>
* Preston Brown <pbrown@redhat.com>
*/
-
+
#include <ctype.h>
#include <popt.h>
@@ -58,20 +58,19 @@
When passing parameters to the various functions, take note of whether they
take host byte order or network byte order. Most take host byte order, and
return host byte order, but there are some exceptions.
-
*/
/*!
\fn u_int32_t prefix2mask(int bits)
\brief creates a netmask from a specified number of bits
-
+
This function converts a prefix length to a netmask. As CIDR (classless
internet domain internet domain routing) has taken off, more an more IP
addresses are being specified in the format address/prefix
(i.e. 192.168.2.3/24, with a corresponding netmask 255.255.255.0). If you
need to see what netmask corresponds to the prefix part of the address, this
is the function. See also \ref mask2prefix.
-
+
\param prefix is the number of bits to create a mask for.
\return a network mask, in network byte order.
*/
@@ -92,10 +91,10 @@ int mask2prefix(u_int32_t mask)
{
int i;
int count = IPBITS;
-
+
for (i = 0; i < IPBITS; i++) {
- if (!(ntohl(mask) & ((2 << i) - 1)))
- count--;
+ if (!(ntohl(mask) & ((2 << i) - 1)))
+ count--;
}
return count;
@@ -117,11 +116,11 @@ int mask2prefix(u_int32_t mask)
u_int32_t default_netmask(u_int32_t addr)
{
if (((ntohl(addr) & 0xFF000000) >> 24) <= 127)
- return htonl(0xFF000000);
+ return htonl(0xFF000000);
else if (((ntohl(addr) & 0xFF000000) >> 24) <= 191)
- return htonl(0xFFFF0000);
+ return htonl(0xFFFF0000);
else
- return htonl(0xFFFFFF00);
+ return htonl(0xFFFFFF00);
}
/*!
@@ -131,13 +130,12 @@ u_int32_t default_netmask(u_int32_t addr)
\param addr an IP address in network byte order.
\param prefix a prefix length.
-
+
\return the calculated broadcast address for the network, in network byte
order.
*/
-u_int32_t calc_broadcast(u_int32_t addr,
- int prefix)
-{
+u_int32_t calc_broadcast(u_int32_t addr, int prefix)
+{
return (addr & prefix2mask(prefix)) | ~prefix2mask(prefix);
}
@@ -169,13 +167,13 @@ const char *get_hostname(u_int32_t addr)
{
struct hostent * hostinfo;
int x;
-
+
hostinfo = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
if (!hostinfo)
- return NULL;
+ return NULL;
for (x=0; hostinfo->h_name[x]; x++) {
- hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
+ hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
}
return hostinfo->h_name;
}
@@ -183,10 +181,10 @@ const char *get_hostname(u_int32_t addr)
/*!
\fn main(int argc, const char **argv)
\brief wrapper program for ipcalc functions.
-
+
This is a wrapper program for the functions that the ipcalc library provides.
It can be used from shell scripts or directly from the command line.
-
+
For more information, please see the ipcalc(1) man page.
*/
int main(int argc, const char **argv) {
@@ -200,124 +198,121 @@ int main(int argc, const char **argv) {
int prefix = 0;
char errBuf[250];
struct poptOption optionsTable[] = {
- { "broadcast", 'b', 0, &showBroadcast, 0,
- "Display calculated broadcast address", },
- { "hostname", 'h', 0, &showHostname, 0,
- "Show hostname determined via DNS" },
- { "netmask", 'm', 0, &showNetmask, 0,
- "Display default netmask for IP (class A, B, or C)" },
- { "network", 'n', 0, &showNetwork, 0,
- "Display network address", },
- { "prefix", 'p', 0, &showPrefix, 0,
- "Display network prefix", },
- { "silent", 's', 0, &beSilent, 0,
- "Don't ever display error messages " },
- POPT_AUTOHELP
- { NULL, '\0', 0, 0, 0, NULL, NULL }
+ { "broadcast", 'b', 0, &showBroadcast, 0,
+ "Display calculated broadcast address", },
+ { "hostname", 'h', 0, &showHostname, 0,
+ "Show hostname determined via DNS" },
+ { "netmask", 'm', 0, &showNetmask, 0,
+ "Display default netmask for IP (class A, B, or C)" },
+ { "network", 'n', 0, &showNetwork, 0,
+ "Display network address", },
+ { "prefix", 'p', 0, &showPrefix, 0,
+ "Display network prefix", },
+ { "silent", 's', 0, &beSilent, 0,
+ "Don't ever display error messages" },
+ POPT_AUTOHELP
+ { NULL, '\0', 0, 0, 0, NULL, NULL }
};
optCon = poptGetContext("ipcalc", argc, argv, optionsTable, 0);
poptReadDefaultConfig(optCon, 1);
if ((rc = poptGetNextOpt(optCon)) < -1) {
- if (!beSilent) {
- fprintf(stderr, "ipcalc: bad argument %s: %s\n",
- poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
- poptStrerror(rc));
- poptPrintHelp(optCon, stderr, 0);
- }
- return 1;
+ if (!beSilent) {
+ fprintf(stderr, "ipcalc: bad argument %s: %s\n",
+ poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
+ poptStrerror(rc));
+ poptPrintHelp(optCon, stderr, 0);
+ }
+ return 1;
}
if (!(ipStr = (char *) poptGetArg(optCon))) {
- if (!beSilent) {
- fprintf(stderr, "ipcalc: ip address expected\n");
- poptPrintHelp(optCon, stderr, 0);
- }
- return 1;
+ if (!beSilent) {
+ fprintf(stderr, "ipcalc: ip address expected\n");
+ poptPrintHelp(optCon, stderr, 0);
+ }
+ return 1;
}
if (strchr(ipStr,'/') != NULL) {
- prefixStr = strchr(ipStr, '/') + 1;
- prefixStr--;
- *prefixStr = '\0'; /* fix up ipStr */
- prefixStr++;
+ prefixStr = strchr(ipStr, '/') + 1;
+ prefixStr--;
+ *prefixStr = '\0'; /* fix up ipStr */
+ prefixStr++;
} else
- prefixStr = NULL;
-
+ prefixStr = NULL;
+
if (prefixStr != NULL) {
- prefix = atoi(prefixStr);
- if (prefix == 0) {
- if (!beSilent)
- fprintf(stderr, "ipcalc: bad prefix: %s\n",
- prefixStr);
- return 1;
- }
+ prefix = atoi(prefixStr);
+ if (prefix == 0) {
+ if (!beSilent)
+ fprintf(stderr, "ipcalc: bad prefix: %s\n", prefixStr);
+ return 1;
+ }
}
-
+
if (showBroadcast || showNetwork || showPrefix) {
- if (!(netmaskStr = (char *) poptGetArg(optCon)) &&
- (prefix == 0)) {
- if (!beSilent) {
- fprintf(stderr, "ipcalc: netmask or prefix expected\n");
- poptPrintHelp(optCon, stderr, 0);
- }
- return 1;
- } else if (netmaskStr && prefix != 0) {
- if (!beSilent) {
- fprintf(stderr, "ipcalc: both netmask and prefix specified\n");
- poptPrintHelp(optCon, stderr, 0);
- }
- return 1;
- } else if (netmaskStr) {
- if (!inet_aton(netmaskStr, &netmask)) {
- if (!beSilent)
- fprintf(stderr, "ipcalc: bad netmask: %s\n",
- netmaskStr);
- return 1;
- }
- prefix = mask2prefix(netmask.s_addr);
- }
+ if (!(netmaskStr = (char *) poptGetArg(optCon)) && (prefix == 0)) {
+ if (!beSilent) {
+ fprintf(stderr, "ipcalc: netmask or prefix expected\n");
+ poptPrintHelp(optCon, stderr, 0);
+ }
+ return 1;
+ } else if (netmaskStr && prefix != 0) {
+ if (!beSilent) {
+ fprintf(stderr, "ipcalc: both netmask and prefix specified\n");
+ poptPrintHelp(optCon, stderr, 0);
+ }
+ return 1;
+ } else if (netmaskStr) {
+ if (!inet_aton(netmaskStr, &netmask)) {
+ if (!beSilent)
+ fprintf(stderr, "ipcalc: bad netmask: %s\n", netmaskStr);
+ return 1;
+ }
+ prefix = mask2prefix(netmask.s_addr);
+ }
}
if ((chptr = (char *) poptGetArg(optCon))) {
- if (!beSilent) {
- fprintf(stderr, "ipcalc: unexpected argument: %s\n", chptr);
- poptPrintHelp(optCon, stderr, 0);
- }
- return 1;
+ if (!beSilent) {
+ fprintf(stderr, "ipcalc: unexpected argument: %s\n", chptr);
+ poptPrintHelp(optCon, stderr, 0);
+ }
+ return 1;
}
/* Handle CIDR entries such as 172/8 */
if (prefix) {
- char *tmp = ipStr;
- int i;
-
- for(i=3; i> 0; i--) {
- tmp = strchr(tmp,'.');
- if (!tmp)
- break;
- else
- tmp++;
- }
- tmp = NULL;
- for (; i>0; i--) {
- tmp = malloc(strlen(ipStr) + 3);
- sprintf(tmp,"%s.0",ipStr);
- ipStr = tmp;
- }
+ char *tmp = ipStr;
+ int i;
+
+ for (i=3; i> 0; i--) {
+ tmp = strchr(tmp,'.');
+ if (!tmp)
+ break;
+ else
+ tmp++;
+ }
+ tmp = NULL;
+ for (; i>0; i--) {
+ tmp = malloc(strlen(ipStr) + 3);
+ sprintf(tmp,"%s.0",ipStr);
+ ipStr = tmp;
+ }
}
if (!inet_aton(ipStr, (struct in_addr *) &ip)) {
- if (!beSilent)
- fprintf(stderr, "ipcalc: bad ip address: %s\n", ipStr);
- return 1;
+ if (!beSilent)
+ fprintf(stderr, "ipcalc: bad ip address: %s\n", ipStr);
+ return 1;
}
-
+
if (!(showNetmask|showPrefix|showBroadcast|showNetwork|showHostname)) {
- poptPrintHelp(optCon, stderr, 0);
- return 1;
+ poptPrintHelp(optCon, stderr, 0);
+ return 1;
}
poptFreeContext(optCon);
@@ -325,43 +320,43 @@ int main(int argc, const char **argv) {
/* we know what we want to display now, so display it. */
if (showNetmask) {
- if (prefix) {
- netmask.s_addr = prefix2mask(prefix);
- } else {
- netmask.s_addr = default_netmask(ip.s_addr);
- prefix = mask2prefix(netmask.s_addr);
- }
-
- printf("NETMASK=%s\n", inet_ntoa(netmask));
+ if (prefix) {
+ netmask.s_addr = prefix2mask(prefix);
+ } else {
+ netmask.s_addr = default_netmask(ip.s_addr);
+ prefix = mask2prefix(netmask.s_addr);
+ }
+
+ printf("NETMASK=%s\n", inet_ntoa(netmask));
}
if (showPrefix) {
- if (!prefix)
- prefix = mask2prefix(ip.s_addr);
- printf("PREFIX=%d\n", prefix);
+ if (!prefix)
+ prefix = mask2prefix(ip.s_addr);
+ printf("PREFIX=%d\n", prefix);
}
-
+
if (showBroadcast) {
- broadcast.s_addr = calc_broadcast(ip.s_addr, prefix);
- printf("BROADCAST=%s\n", inet_ntoa(broadcast));
+ broadcast.s_addr = calc_broadcast(ip.s_addr, prefix);
+ printf("BROADCAST=%s\n", inet_ntoa(broadcast));
}
if (showNetwork) {
- network.s_addr = calc_network(ip.s_addr, prefix);
- printf("NETWORK=%s\n", inet_ntoa(network));
+ network.s_addr = calc_network(ip.s_addr, prefix);
+ printf("NETWORK=%s\n", inet_ntoa(network));
}
-
+
if (showHostname) {
- if ((hostName = (char *) get_hostname(ip.s_addr)) == NULL) {
- if (!beSilent) {
- sprintf(errBuf, "ipcalc: cannot find hostname for %s", ipStr);
- herror(errBuf);
- }
- return 1;
- }
-
- printf("HOSTNAME=%s\n", hostName);
+ if ((hostName = (char *) get_hostname(ip.s_addr)) == NULL) {
+ if (!beSilent) {
+ sprintf(errBuf, "ipcalc: cannot find hostname for %s", ipStr);
+ herror(errBuf);
+ }
+ return 1;
+ }
+
+ printf("HOSTNAME=%s\n", hostName);
}
-
+
return 0;
}