- 论坛徽章:
- 0
|
计算网络掩码程序,稍作修改,用处多多:)
/* (c) Steve Parker 22 July 2004
* Written for NetOps staff to help with cluster planning, networking, etc
*/
#include
#include
#include
char vversion[] = __DATE__ " " __TIME__;
struct ip4_addr
{
unsigned char a, b, c, d;
};
struct ip4_addr IP;
struct ip4_addr MASK;
unsigned char NETMASK;
struct ip4_addr
dotted_quad_to_ip (char *dottedquad)
{
struct ip4_addr ip;
int a, b, c, d;
sscanf (dottedquad, "%d.%d.%d.%d", &a, &b, &c, &d);
ip.a = a;
ip.b = b;
ip.c = c;
ip.d = d;
return ip;
}
struct ip4_addr
hex_to_ip (char *hex)
{
struct ip4_addr ip;
int a, b, c, d;
sscanf (hex, "%02x%02x%02x%02x", &a, &b, &c, &d);
ip.a = a;
ip.b = b;
ip.c = c;
ip.d = d;
return ip;
}
void
usage ()
{
printf ("Steve's Netmask Calculator (Build : %s)
", vversion);
printf ("Usage: netmask [ ip_address ] netmask
");
printf (" IP and Netmask can be seperated by space, '/', or both.
");
printf (" If only a netmask is provided, the leading '/' is required.
");
printf (" Netmask can be provided in any of three formats:
");
printf (" /24 or /255.255.255.0 or /ffffff00
");
printf ("e.g.: netmask 192.168.1.1/24
");
printf (" netmask 192.168.1.1 255.255.255.0
");
printf (" netmask 192.168.1.1 ffffff00
");
printf (" netmask /24
");
printf (" netmask /255.255.255.0
");
printf (" netmask /ffffff00
");
printf ("
");
exit (1);
}
int
ip_to_netmask (struct ip4_addr n)
{
unsigned char i, m = 0;
for (i = 8; i 32)
{
printf ("ERROR: /nn format must be 1-32
");
exit (1);
}
n = NETMASK;
netmask.a = netmask.b = netmask.c = netmask.d = 0;
p = &netmask.a;
if (n > 23)
{
netmask.a = netmask.b = netmask.c = 255;
n -= 24;
p = &netmask.d;
}
if (n > 15)
{
netmask.a = netmask.b = 255;
n -= 16;
p = &netmask.c;
}
if (n > 7)
{
netmask.a = 255;
n -= 8;
p = &netmask.b;
}
for (m = 7; m 0)
{
*p += (1 slash)
dot = NULL; // "dot" implies an IP address, not an x.x.x.x netmask
if (slash != NULL)
{
mask = (char *) slash + sizeof (char);
MASK = parse_mask (mask);
*slash = ' ';
}
if (dot != NULL)
{
IP = dotted_quad_to_ip (argv[1]);
}
IP = dotted_quad_to_ip (argv[1]);
if (dot != NULL)
printf ("IP = %03d.%03d.%03d.%03d
", IP.a, IP.b, IP.c, IP.d);
if (slash != NULL)
printf ("Mask = %03d.%03d.%03d.%03d (%02x%02x%02x%02x) - aka /%d
",
MASK.a, MASK.b, MASK.c, MASK.d, MASK.a, MASK.b, MASK.c, MASK.d,
NETMASK);
if (dot != NULL)
{
get_network ();
get_broadcast ();
}
return 0;
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/2389/showart_25097.html |
|