代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
IP地址转换
作者:
Dezai.CN
/ 发布于
2011/2/10
/
656
<div>/* rangeToCidr.c - Convert Ip ranges to CIDR */</div> <div>/* modification history -------------------- 01a,17sep08,karn written */</div> <div>/* includes */</div> #include<stdio.h> #include<unistd.h> #include<string.h> #include<math.h> #include<errno.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h></div> <div>/* defines */ //#define DBG #ifdef DBG #define DEBUG(x) fprintf(stderr,x) #else #define DEBUG #endif /* DBG */</div> <div>define IP_BINARY_LENGTH 32+1 /* 32 bits ipv4 address +1 for null */ #define IP_HEX_LENGTH 10 #define MAX_CIDR_MASK 32 #define MAX_CIDR_LEN 18+1 /*255.255.255.255/32*/</div> <div>/* Forward declaratopms */ void rangeToCidr(uint32_t from ,uint32_t to, void (callback)(char *cidrNotation)); int ipToBin(uint32_t ip , char * pOut);</div> <div>void printNotation(char *cidrNotation);</div> <div>/* Globals */</div> <div> /******************************************************************************* * * ipToBin - convert an ipv4 address to binary representation * and pads zeros to the beginning of the string if * the length is not 32 * (Important for ranges like 10.10.0.1 - 20.20.20.20 ) * * ip - ipv4 address on host order * pOut - Buffer to store binary. * * RETURNS: OK or ERROR */</div> <div>int ipToBin(uint32_t ip , char * pOut) { char hex[IP_HEX_LENGTH]; int i; int result=0; int len; char pTmp[2]; int tmp; /* * XXX: Could use bit operations instead but was easier to debug */ char binMap[16][5] = { "0000","0001","0010","0011", "0100", "0101","0110","0111","1000", "1001", "1010","1011","1100", "1101","1110","1111", }; pTmp[1]=0x0; memset(hex,0x0,sizeof(hex)); len=sprintf(hex,"%x",ip);</div> <div> for(i=0;i<len;i++) {</div> <div> /* Ugly but to use strtol , we need the last byte as null */ pTmp[0]=hex[i];</div> <div> errno = 0; tmp = strtol(pTmp, 0x0, 16);</div> <div> /* Should not happen */ if (errno != 0) { memset(pOut,'0',IP_BINARY_LENGTH -1); DEBUG ("strtol failed for hex 0x%s\n",pTmp); return -1; } <div> result+=sprintf(pOut+result,"%s",binMap[tmp]); } <div> DEBUG("bits %u printed for ip address for hex len %u\n",result,len); /* if length is not 32 , pad the start with zeros*/</div> <div> if(result < IP_BINARY_LENGTH-1) { char pSwap[IP_BINARY_LENGTH]; strncpy(pSwap,pOut,IP_BINARY_LENGTH); memset(pOut,'0',IP_BINARY_LENGTH); strncpy(pOut+IP_BINARY_LENGTH-1-result,pSwap,result); DEBUG("corrected length to 32\n"); } <div> else if (result > IP_BINARY_LENGTH-1) return -1;</div> <div> /* Success */ return 0; } <div>/******************************************************************************* * main : * * arg1 : Start Ip Address * arg2 : End Ip address */</div> <div>int main (int argc,char **argv) { long fromIp, toIp; struct in_addr addr; if(argc !=3 ) { printf("Usage: %s <from> <to>\n",argv[0]); return(0); } <div> /* All operation on host order */ if (inet_aton(argv[1],&addr) == 0) goto error; fromIp = ntohl(addr.s_addr);</div> <div> if (inet_aton(argv[2],&addr) ==0) goto error; toIp = ntohl(addr.s_addr);</div> <div> rangeToCidr(fromIp,toIp,printNotation);</div> <div> return 0; error: printf("Invalid Argument\n"); return -EINVAL; } <div> /******************************************************************************* * * rangeToCidr - convert an ip Range to CIDR, and call 'callback' to handle * the value. * * from - IP Range start address * to - IP Range end address * callback - Callback function to handle cidr. * RETURNS: OK or ERROR */</div> <div>void rangeToCidr(uint32_t from ,uint32_t to, void (callback)(char *cidrNotation)) { int cidrStart = 0; int cidrEnd = MAX_CIDR_MASK - 1; long newfrom; long mask; char fromIp[IP_BINARY_LENGTH]; char toIp[IP_BINARY_LENGTH]; struct in_addr addr; char cidrNotation[MAX_CIDR_LEN];</div> <div> memset (fromIp,0x0,sizeof(fromIp)); memset (toIp,0x0,sizeof(toIp));</div> <div> if ( ipToBin(from,fromIp) != 0 ) return; if ( ipToBin(to,toIp) != 0 ) return;</div> <div> DEBUG ("from %lu to %lu\n", from,to); DEBUG("from %s\n",fromIp); DEBUG("to %s\n",toIp);</div> <div> if(from < to ) {</div> <div> /* Compare the from and to address ranges to get the first * point of difference */</div> <div> while(fromIp[cidrStart]==toIp[cidrStart]) cidrStart ++; cidrStart = 32 - cidrStart -1 ; DEBUG("cidrStart is %u\n",cidrStart);</div> <div> /* Starting from the found point of difference make all bits on the * right side zero */</div> <div> newfrom = from >> cidrStart +1 << cidrStart +1 ;</div> <div> /* Starting from the end iterate reverse direction to find * cidrEnd */ while( fromIp[cidrEnd] == '0' && toIp[cidrEnd] == '1') cidrEnd --;</div> <div> cidrEnd = MAX_CIDR_MASK - 1 - cidrEnd; DEBUG("cidrEnd is %u\n",cidrEnd);</div> <div> if(cidrEnd <= cidrStart) { /* * Make all the bit-shifted bits equal to 1, for * iteration # 1. */ mask = pow (2, cidrStart ) - 1; DEBUG("it1 is %lu \n",newfrom | mask ); rangeToCidr (from , newfrom | mask, callback); DEBUG("it2 is %lu \n",newfrom | 1 << cidrStart); rangeToCidr (newfrom | 1 << cidrStart ,to ,callback); } else { addr.s_addr = htonl(newfrom); sprintf(cidrNotation,"%s/%d", inet_ntoa(addr), MAX_CIDR_MASK-cidrEnd); if (callback != NULL) callback(cidrNotation); } } <div> else { addr.s_addr = htonl(from); sprintf(cidrNotation,"%s/%d",inet_ntoa(addr),MAX_CIDR_MASK); if(callback != NULL) callback(cidrNotation); } } <div>/******************************************************************************* * * printNotation - This is an example callback function to handle cidr notation. * * RETURNS: */</div> <div>void printNotation(char *cidrNotation) { printf("%s\n",cidrNotation); } </div>
试试其它关键字
IP地址
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3