免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1486 | 回复: 0
打印 上一主题 下一主题

NETMASK计算程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-05-09 20:50 |只看该作者 |倒序浏览
计算网络掩码程序,稍作修改,用处多多:)
/* (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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP