]>
git.ipfire.org Git - people/amarx/ipfire-3.x.git/blob - pkgs/initscripts/src/ipcalc.c
8decccadcb1a93870676867f0f79ccd913fe1db1
2 * Copyright (c) 1997-2009 Red Hat, Inc. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 * Erik Troan <ewt@redhat.com>
19 * Preston Brown <pbrown@redhat.com>
20 * David Cantrell <dcantrell@redhat.com>
28 #include <sys/socket.h>
29 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
36 \brief provides utilities for manipulating IP addresses.
38 ipcalc provides utilities and a front-end command line interface for
39 manipulating IP addresses, and calculating various aspects of an ip
40 address/netmask/network address/prefix/etc.
42 Functionality can be accessed from other languages from the library
43 interface, documented here. To use ipcalc from the shell, read the
44 ipcalc(1) manual page.
46 When passing parameters to the various functions, take note of whether they
47 take host byte order or network byte order. Most take host byte order, and
48 return host byte order, but there are some exceptions.
52 \fn struct in_addr prefix2mask(int bits)
53 \brief creates a netmask from a specified number of bits
55 This function converts a prefix length to a netmask. As CIDR (classless
56 internet domain internet domain routing) has taken off, more an more IP
57 addresses are being specified in the format address/prefix
58 (i.e. 192.168.2.3/24, with a corresponding netmask 255.255.255.0). If you
59 need to see what netmask corresponds to the prefix part of the address, this
60 is the function. See also \ref mask2prefix.
62 \param prefix is the number of bits to create a mask for.
63 \return a network mask, in network byte order.
65 struct in_addr
prefix2mask(int prefix
) {
67 memset(&mask
, 0, sizeof(mask
));
69 mask
.s_addr
= htonl(~((1 << (32 - prefix
)) - 1));
71 mask
.s_addr
= htonl(0);
77 \fn int mask2prefix(struct in_addr mask)
78 \brief calculates the number of bits masked off by a netmask.
80 This function calculates the significant bits in an IP address as specified by
81 a netmask. See also \ref prefix2mask.
83 \param mask is the netmask, specified as an struct in_addr in network byte order.
84 \return the number of significant bits. */
85 int mask2prefix(struct in_addr mask
)
88 uint32_t saddr
= ntohl(mask
.s_addr
);
90 for (count
=0; saddr
> 0; count
++) {
98 \fn struct in_addr default_netmask(struct in_addr addr)
100 \brief returns the default (canonical) netmask associated with specified IP
103 When the Internet was originally set up, various ranges of IP addresses were
104 segmented into three network classes: A, B, and C. This function will return
105 a netmask that is associated with the IP address specified defining where it
106 falls in the predefined classes.
108 \param addr an IP address in network byte order.
109 \return a netmask in network byte order. */
110 struct in_addr
default_netmask(struct in_addr addr
)
112 uint32_t saddr
= addr
.s_addr
;
115 memset(&mask
, 0, sizeof(mask
));
117 if (((ntohl(saddr
) & 0xFF000000) >> 24) <= 127)
118 mask
.s_addr
= htonl(0xFF000000);
119 else if (((ntohl(saddr
) & 0xFF000000) >> 24) <= 191)
120 mask
.s_addr
= htonl(0xFFFF0000);
122 mask
.s_addr
= htonl(0xFFFFFF00);
128 \fn struct in_addr calc_broadcast(struct in_addr addr, int prefix)
130 \brief calculate broadcast address given an IP address and a prefix length.
132 \param addr an IP address in network byte order.
133 \param prefix a prefix length.
135 \return the calculated broadcast address for the network, in network byte
138 struct in_addr
calc_broadcast(struct in_addr addr
, int prefix
)
140 struct in_addr mask
= prefix2mask(prefix
);
141 struct in_addr broadcast
;
143 memset(&broadcast
, 0, sizeof(broadcast
));
144 broadcast
.s_addr
= (addr
.s_addr
& mask
.s_addr
) | ~mask
.s_addr
;
149 \fn struct in_addr calc_network(struct in_addr addr, int prefix)
150 \brief calculates the network address for a specified address and prefix.
152 \param addr an IP address, in network byte order
153 \param prefix the network prefix
154 \return the base address of the network that addr is associated with, in
157 struct in_addr
calc_network(struct in_addr addr
, int prefix
)
159 struct in_addr mask
= prefix2mask(prefix
);
160 struct in_addr network
;
162 memset(&network
, 0, sizeof(network
));
163 network
.s_addr
= addr
.s_addr
& mask
.s_addr
;
168 \fn const char *get_hostname(int family, void *addr)
169 \brief returns the hostname associated with the specified IP address
171 \param family the address family, either AF_INET or AF_INET6.
172 \param addr an IP address to find a hostname for, in network byte order,
173 should either be a pointer to a struct in_addr or a struct in6_addr.
175 \return a hostname, or NULL if one cannot be determined. Hostname is stored
176 in a static buffer that may disappear at any time, the caller should copy the
177 data if it needs permanent storage.
179 char *get_hostname(int family
, void *addr
)
181 struct hostent
* hostinfo
= NULL
;
183 struct in_addr addr4
;
184 struct in6_addr addr6
;
186 if (family
== AF_INET
) {
187 memset(&addr4
, 0, sizeof(addr4
));
188 memcpy(&addr4
, addr
, sizeof(addr4
));
189 hostinfo
= gethostbyaddr((const void *) &addr4
,
190 sizeof(addr4
), family
);
191 } else if (family
== AF_INET6
) {
192 memset(&addr6
, 0, sizeof(addr6
));
193 memcpy(&addr6
, addr
, sizeof(addr6
));
194 hostinfo
= gethostbyaddr((const void *) &addr6
,
195 sizeof(addr6
), family
);
201 for (x
=0; hostinfo
->h_name
[x
]; x
++) {
202 hostinfo
->h_name
[x
] = tolower(hostinfo
->h_name
[x
]);
204 return hostinfo
->h_name
;
208 \fn main(int argc, const char **argv)
209 \brief wrapper program for ipcalc functions.
211 This is a wrapper program for the functions that the ipcalc library provides.
212 It can be used from shell scripts or directly from the command line.
214 For more information, please see the ipcalc(1) man page.
216 int main(int argc
, const char **argv
) {
217 int showBroadcast
= 0, showPrefix
= 0, showNetwork
= 0;
218 int showHostname
= 0, showNetmask
= 0;
220 int doCheck
= 0, familyIPv4
= 0, familyIPv6
= 0;
223 char *ipStr
, *prefixStr
, *netmaskStr
, *chptr
;
224 char *hostName
= NULL
;
225 char namebuf
[INET6_ADDRSTRLEN
+1];
226 struct in_addr ip
, netmask
, network
, broadcast
;
230 struct poptOption optionsTable
[] = {
231 { "check", 'c', 0, &doCheck
, 0,
232 "Validate IP address for specified address family", },
233 { "ipv4", '4', 0, &familyIPv4
, 0,
234 "IPv4 address family (default)", },
235 { "ipv6", '6', 0, &familyIPv6
, 0,
236 "IPv6 address family", },
237 { "broadcast", 'b', 0, &showBroadcast
, 0,
238 "Display calculated broadcast address", },
239 { "hostname", 'h', 0, &showHostname
, 0,
240 "Show hostname determined via DNS" },
241 { "netmask", 'm', 0, &showNetmask
, 0,
242 "Display default netmask for IP (class A, B, or C)" },
243 { "network", 'n', 0, &showNetwork
, 0,
244 "Display network address", },
245 { "prefix", 'p', 0, &showPrefix
, 0,
246 "Display network prefix", },
247 { "silent", 's', 0, &beSilent
, 0,
248 "Don't ever display error messages" },
250 { NULL
, '\0', 0, 0, 0, NULL
, NULL
}
253 optCon
= poptGetContext("ipcalc", argc
, argv
, optionsTable
, 0);
254 poptReadDefaultConfig(optCon
, 1);
256 if ((rc
= poptGetNextOpt(optCon
)) < -1) {
258 fprintf(stderr
, "ipcalc: bad argument %s: %s\n",
259 poptBadOption(optCon
, POPT_BADOPTION_NOALIAS
),
261 poptPrintHelp(optCon
, stderr
, 0);
266 if (!(ipStr
= (char *) poptGetArg(optCon
))) {
268 fprintf(stderr
, "ipcalc: ip address expected\n");
269 poptPrintHelp(optCon
, stderr
, 0);
274 /* if there is a : in the address, it is an IPv6 address */
275 if (strchr(ipStr
,':') != NULL
) {
279 if (strchr(ipStr
,'/') != NULL
) {
280 prefixStr
= strchr(ipStr
, '/') + 1;
282 *prefixStr
= '\0'; /* fix up ipStr */
288 if (prefixStr
!= NULL
) {
289 prefix
= atoi(prefixStr
);
290 if (prefix
< 0 || ((familyIPv6
&& prefix
> 128) || (!familyIPv6
&& prefix
> 32))) {
292 fprintf(stderr
, "ipcalc: bad prefix: %s\n", prefixStr
);
297 if (showBroadcast
|| showNetwork
|| showPrefix
) {
298 if (!(netmaskStr
= (char *) poptGetArg(optCon
)) && (prefix
< 0)) {
300 fprintf(stderr
, "ipcalc: netmask or prefix expected\n");
301 poptPrintHelp(optCon
, stderr
, 0);
304 } else if (netmaskStr
&& prefix
>= 0) {
306 fprintf(stderr
, "ipcalc: both netmask and prefix specified\n");
307 poptPrintHelp(optCon
, stderr
, 0);
310 } else if (netmaskStr
) {
311 if (inet_pton(AF_INET
, netmaskStr
, &netmask
) <= 0) {
313 fprintf(stderr
, "ipcalc: bad netmask: %s\n", netmaskStr
);
316 prefix
= mask2prefix(netmask
);
320 if ((chptr
= (char *) poptGetArg(optCon
))) {
322 fprintf(stderr
, "ipcalc: unexpected argument: %s\n", chptr
);
323 poptPrintHelp(optCon
, stderr
, 0);
328 if (!familyIPv4
&& !familyIPv6
)
331 if (familyIPv4
&& familyIPv6
) {
333 fprintf(stderr
, "ipcalc: cannot specify both address families\n");
338 /* Handle CIDR entries such as 172/8 */
339 if (prefix
>= 0 && familyIPv4
) {
343 for (i
=3; i
> 0; i
--) {
344 tmp
= strchr(tmp
,'.');
353 if (asprintf(&tmp
, "%s.0", ipStr
) == -1) {
354 fprintf(stderr
, "Memory allocation failure line %d\n", __LINE__
);
362 if (inet_pton(AF_INET
, ipStr
, &ip
) <= 0) {
364 fprintf(stderr
, "ipcalc: bad IPv4 address: %s\n", ipStr
);
366 } else if (prefix
> 32) {
368 fprintf(stderr
, "ipcalc: bad IPv4 prefix %d\n", prefix
);
377 if (inet_pton(AF_INET6
, ipStr
, &ip6
) <= 0) {
379 fprintf(stderr
, "ipcalc: bad IPv6 address: %s\n", ipStr
);
381 } else if (prefix
> 128) {
383 fprintf(stderr
, "ipcalc: bad IPv6 prefix %d\n", prefix
);
392 (showBroadcast
|| showNetmask
|| showNetwork
|| showPrefix
)) {
394 fprintf(stderr
, "ipcalc: unable to show setting for IPv6\n");
400 !(showNetmask
|showPrefix
|showBroadcast
|showNetwork
|showHostname
)) {
401 poptPrintHelp(optCon
, stderr
, 0);
405 poptFreeContext(optCon
);
407 /* we know what we want to display now, so display it. */
411 netmask
= prefix2mask(prefix
);
413 netmask
= default_netmask(ip
);
414 prefix
= mask2prefix(netmask
);
417 memset(&namebuf
, '\0', sizeof(namebuf
));
419 if (inet_ntop(AF_INET
, &netmask
, namebuf
, INET_ADDRSTRLEN
) == NULL
) {
420 fprintf(stderr
, "Memory allocation failure line %d\n", __LINE__
);
424 printf("NETMASK=%s\n", namebuf
);
429 prefix
= mask2prefix(ip
);
430 printf("PREFIX=%d\n", prefix
);
434 broadcast
= calc_broadcast(ip
, prefix
);
435 memset(&namebuf
, '\0', sizeof(namebuf
));
437 if (inet_ntop(AF_INET
, &broadcast
, namebuf
, INET_ADDRSTRLEN
) == NULL
) {
438 fprintf(stderr
, "Memory allocation failure line %d\n", __LINE__
);
442 printf("BROADCAST=%s\n", namebuf
);
446 network
= calc_network(ip
, prefix
);
447 memset(&namebuf
, '\0', sizeof(namebuf
));
449 if (inet_ntop(AF_INET
, &network
, namebuf
, INET_ADDRSTRLEN
) == NULL
) {
450 fprintf(stderr
, "Memory allocation failure line %d\n", __LINE__
);
454 printf("NETWORK=%s\n", namebuf
);
459 hostName
= get_hostname(AF_INET
, &ip
);
460 } else if (familyIPv6
) {
461 hostName
= get_hostname(AF_INET6
, &ip6
);
464 if (hostName
== NULL
) {
466 sprintf(errBuf
, "ipcalc: cannot find hostname for %s", ipStr
);
472 printf("HOSTNAME=%s\n", hostName
);