C实现判断两个IP是否在同一个子网

在进行网络程序的时候避免不了对给定IP是否跨子网段进行判断。相关原理倒是简单, 贴出相关代码:

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#define ALL_BIT 32 /* ip address have 32 bits */

/*
** description: juge the ip_a and ip_b is in the same subnet or not
** this functio is used IPv4 only, and did not check the input parameter's
** correctness, so make sure the value of parameter before call it
** para: sub_mask is a decimal subnet mask , not dotted decimal
** ip_a/ip_b: the ip address string, in dotted decimal
** return: 0 for the same, -1 for the not
*/
int same_subnet (int sub_mask, char *ip_a, char *ip_b)
{
  int mask = 0xFFFFFFFF;
  mask = mask << (ALL_BIT - sub_mask);

  double cnt_a = 0.0;
  double cnt_b = 0.0;
  double tmp = 0.0;

  char *token = NULL;

 int i = 3;

  for (token = strtok (ip_a, "."); token != NULL; token = strtok (NULL, ".")) {
    tmp = atoi (token);
    tmp = tmp * pow (256, i--);  /* what dose this mean? do you understand */
    cnt_a += tmp;
  }

  i = 3; /* reset i */
  for (token = strtok (ip_b, "."); token != NULL; token = strtok (NULL, ".")) {
    tmp = atoi (token);
    tmp = tmp * pow (256, i--);
    cnt_b += tmp;
  }

  unsigned long mask_a = (unsigned long)cnt_a;
  unsigned long mask_b = (unsigned long)cnt_b;

  //printf ("mask_a %u\tmask_b %u\n", mask_a, mask_b);

  mask_a &= mask;   /* get the ip's netmask for compare */
  mask_b &= mask;
  //printf ("mask_a %u\tmask_b %u\n", mask_a, mask_b);

  if (mask_a == mask_b)
  return 0;
  else
    return -1;
}

/*
** test code
*/
int main (int argc, char **argv)
{
  int i = same_subnet (atoi (argv[1]), argv[2], argv[3]);
  printf ("%s subnet\n", i == 0? "same":"not same");
  return 0;
}

代码在gcc version 4.7.2 20121109 (Red Hat 4.7.2-8) (GCC)编译通过。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值