ip地址转化代码实例
/*@author: lgh@
*
* */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>//ntohl
#include <arpa/inet.h> //inet_addr
#include <sys/types.h>
#ifdef UNICODE
#define T(x) __TXT(x)
#define __TXT(s) L ## s
#else
#define T(s) s
#endif /* UNICODE */
/*
#if defined (DEBUG_DISABLE)
#undef DEBUG_DISABLE
#else
#endif
*/ /*********************************************************************/
#ifndef DEBUG_DISABLE
/*
#define lprint(tag, fmt...) \
do{if(tag)printf(fmt);}while(0)
*/
#define lprint(x, ...) do{printf(x, ##__VA_ARGS__);}while(0)
#else
#define lprint(tag, fmt...)
#endif /*********************************************************************/
#ifdef CONFIG_DUAL_IMAGE
#if defined (CONFIG_RT2880_FLASH_2M)
#define IMAGE1_SIZE 0x100000
#elif defined (CONFIG_RT2880_FLASH_4M)
#define IMAGE1_SIZE 0x200000
#elif defined (CONFIG_RT2880_FLASH_8M)
#define IMAGE1_SIZE 0x400000
#elif defined (CONFIG_RT2880_FLASH_16M)
#define IMAGE1_SIZE 0x800000
#elif defined (CONFIG_RT2880_FLASH_32M)
#define IMAGE1_SIZE 0x1000000
#else
#define IMAGE_SIZE TEST
#endif
#endif//end #ifdef,否则会出现25:1: error: unterminated #ifdef,这样的错 /*********************************************************************/
typedef void* (*square_t)(int* x); void* square (int* x)
{
*x *= *x;
return (void*)square;
} int lt1224(void)
{
int x = ;
((square_t)((square_t)((square_t)square (&x)) (&x)) (&x)) (&x);
printf ("%d\n", x);
return ;
}
/*********************************************************************/
#ifndef NIP4
#define NIP4_FMT "%u.%u.%u.%u"
#define NIP4(addr) \
((unsigned char *)&addr)[], \
((unsigned char *)&addr)[], \
((unsigned char *)&addr)[], \
((unsigned char *)&addr)[] #define HIP4_FMT "%u.%u.%u.%u"
#define HIP4(addr) \
((unsigned char *)&addr)[], \
((unsigned char *)&addr)[], \
((unsigned char *)&addr)[], \
((unsigned char *)&addr)[]
#endif #ifndef NIP6
#define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
#define NIP6(addr) \
(unsigned)((addr)[]), \
(unsigned)((addr)[]), \
(unsigned)((addr)[]), \
(unsigned)((addr)[]), \
(unsigned)((addr)[]), \
(unsigned)((addr)[]), \
(unsigned)((addr)[]), \
(unsigned)((addr)[]), \
(unsigned)((addr)[]), \
(unsigned)((addr)[]), \
(unsigned)((addr)[]), \
(unsigned)((addr)[]), \
(unsigned)((addr)[]), \
(unsigned)((addr)[]), \
(unsigned)((addr)[]), \
(unsigned)((addr)[])
#endif /*
#ifdef __CHECKER__
#define __bitwise__ __attribute__((bitwise))
#else
#define __bitwise__
#endif
#ifdef __CHECK_ENDIAN__
#define __bitwise __bitwise__
#else
#define __bitwise
#endif
typedef __u16 __bitwise __le16;
typedef __u16 __bitwise __be16;
typedef __u32 __bitwise __le32;
typedef __u32 __bitwise __be32;
typedef __u64 __bitwise __le64;
typedef __u64 __bitwise __be64;
*/ typedef __signed__ char __s8;
typedef unsigned char __u8; typedef __signed__ short __s16;
typedef unsigned short __u16; typedef __signed__ int __s32;
typedef unsigned int __u32; typedef __signed__ long __s64;
typedef unsigned long __u64; union nf_inet_addr {
__u32 all[];
__u32 ip;// __be32 ip;
// __be32 ip6[4];
struct in_addr in;
struct in6_addr in6;
};
/*********************************************************************/ union endian{
int i;
char c;
}; int main(int argc, char *argv[])
{
char *s="";
char *p;
p=strpbrk(s,"a1 839"); /*1会最先在s字符串中找到*/
lprint("%s\n",p);
p=strpbrk(s,"");/*3 会最先在s 字符串中找到*/
lprint("%s\n",p);
lprint("%s\n", T("deling")); lprint("************************************************************************\n");
lt1224(); lprint("************************************************************************\n");
if(argc != )
{
return ;
}
union endian le;
le.i = 0x3456;/*16bit宽的数*/
if(le.c == 0x56){
lprint("little endian\n");
}else lprint("big endian\n");
printf("%0x, %0x\n", le.i, le.c); char ipdot[] = {};/*点分十进制*/
long iphost = ;/*主机字节序,x86一般都是小端模式*/
long ipnet = ;/*网络字节序,大端模式*/ strncpy(ipdot, argv[], sizeof(ipdot));
lprint("Get ipdot:%s\n", ipdot); ipnet = inet_addr(ipdot);
lprint("Get ipnet:%ld:"NIP4_FMT"\n", ipnet, NIP4(ipnet));
iphost = ntohl(ipnet);
lprint("Get iphost:%ld:"NIP4_FMT"\n", iphost, NIP4(iphost));
lprint("Get iphost:%ld:"HIP4_FMT"\n", iphost, HIP4(iphost));
iphost++;
lprint("Get iphost++:%ld:"HIP4_FMT"\n", iphost, HIP4(iphost)); /*net --> host*/
unsigned array[];
array[] = (ntohl(ipnet) >> ) & 0xFF;
array[] = (ntohl(ipnet) >> ) & 0xFF;
array[] = (ntohl(ipnet) >> ) & 0xFF;
array[] = ntohl(ipnet) & 0xFF;
lprint(NIP4_FMT"\n", array[], array[], array[], array[]); lprint("%ld.%ld.%ld.%ld\n", ipnet & 0xff,
ipnet>> & 0xff,
ipnet>> & 0xff,
ipnet>> & 0xff);
lprint("************************************************************************\n");
return ;
}
ip地址转化代码实例的更多相关文章
- C#验证ip地址的代码
如下的代码是关于C#验证ip地址的代码. public Boolean CheckIPValid(String strIP) { char chrFullStop = '.'; string[] ar ...
- JSP 获取真实IP地址的代码
[转载]JSP 获取真实IP地址的代码 JSP 获取真实IP地址的代码 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的. ...
- 【转载】JSP 获取真实IP地址的代码
JSP 获取真实IP地址的代码 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的. 但是在通过了 Apache,Squid ...
- Linux下获取本机IP地址的代码
Linux下获取本机IP地址的代码,返回值即为互联网标准点分格式的字符串. #define ETH_NAME "eth0" //获得本机IP地址 char* GetLocalAdd ...
- js获取IP地址多种方法实例教程
js获取IP地址方法总结 js代码获取IP地址的方法,如何在js中取得客户端的IP地址.原文地址:js获取IP地址的三种方法 http://www.jbxue.com/article/11338. ...
- 将IP地址转化为整数
$ip = 'IP地址';echo $intip = sprintf('%u',ip2long($ip)); //转换为无符号整型echo long2ip($intip);//将整型转换为ip
- 获取Linux下的IP地址 java代码
/** * 获取Linux下的IP地址 * * @return IP地址 * @throws SocketException */ public static String getLinuxLocal ...
- C#获取本地IP地址[常用代码段]
获得当前机器的IP代码,假设本地主机为单网卡 string strHostName = Dns.GetHostName(); //得到本机的主机名 IPHostEntry ipEntry = Dns. ...
- openresty(nginx)中使用lua脚本获取请求IP地址的代码
人狠话不多,直接上代码:------------------------------------------------------------------------------------- lo ...
随机推荐
- C#使用结构来传递多个参数
当参数超过5个时,建议用结构来传递多个参数. 示例代码如下: public struct MyStruct { public string str; public int number; } clas ...
- MongoDB(3):小的细节问题
1.文档 {“greeting”:“hello,world”,“foo”: 3} 文档中的键/值对是有序的,下面的文档与上面的文档是完全不同的两个文档. {“foo”: 3 ,“greeting”:“ ...
- 推荐个好东西swoole,php如虎添翼
Swoole:PHP语言的异步.并行.高性能网络通信框架,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,数据库连接池,AsyncTask,消息队列 ...
- android Material Design:主题
<style name="MyTheme" parent="@android:style/android:Theme.Material"> < ...
- 删:[CentOS 7] 安装nginx
下载对应当前系统版本的nginx包(package) # wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-cent ...
- 使用WMIC永久设置你的环境变量
关于wmic,引用一下这位哥们儿的话http://technet.microsoft.com/en-us/library/bb742610.aspx: WMIC扩展WMI(Windows Manage ...
- 多分类问题multicalss classification
多分类问题:有N个类别C1,C2,...,Cn,多分类学习的基本思路是"拆解法",即将多分类任务拆分为若干个而分类任务求解,最经典的拆分策略是:"一对一",&q ...
- python中split与join
1.split个人最喜欢的就是它能使输入的一连串数字变为list. str=raw_input("some ") str2=str.split(" ") str ...
- 15.python的for循环与迭代器、生成器
在前面学习讲完while循环之后,现在终于要将for循环这个坑填上了.之所以拖到现在是因为for循环对前面讲过的序列.字典.集合都是有效的,讲完前面的内容再来讲for循环会更加容易上手. 首先,for ...
- 行转列求和:不加 in 条件,sum的数据会不会准确?
我的习惯写法,担心不加 in 条件 ,统计结果会包含其他的数据 SELECT ZWKMYE_KJND as 年度,ZWKMYE_KJQJ as 月份,ZWKMYE_DWBH as 单位, ' then ...