IPv4和IPv6地址的存取
存入IP地址时,使用inet_pton函数将输入的十进制字符串转出二进制。取出IP时再使用inet_ptop函数将“二进制整数”转成“点分十进制整数”显示。这两个函数都在宏定义#include <arpa/inet.h>中
C文件,test_ip.c
- 1 #include <stdio.h>
- 2 #include <arpa/inet.h>
- 3 #include "test_ip.h"
- 4
- 5 int main(int argc,char **argv)
- 6 {
- 7 ipmsg_t ip_msg;
- 8 ipaddr_t ip_addr;
- 9 char IPdotdec[20]="192.168.1.1";
- 10 inet_pton(AF_INET, IPdotdec, (void *)&ip_addr.ipv4);
//字符串 ——> 网络字节流
- 11 printf("decimal : %s -> hexadecimal : 0x%x\n",IPdotdec,ip_addr.ipv4);
- 12
- 13 ip_addr.ipv4 = ntohl(ip_addr.ipv4);
- 14 printf("decimal : %s -> hexadecimal : 0x%x\n",IPdotdec,ip_addr.ipv4);
- 15
- 16 ip_addr.ipv4 = htonl(ip_addr.ipv4);
- 17 ip_msg.ip_addr.data = (uint8_t *)&(ip_addr.ipv4);
- 18 ip_msg.ip_addr.len = 4;
- 19 ip_msg.prefix_len = 32;
- 20
- 21 char ip_str[20]={0};
- 22 inet_ntop(AF_INET, ip_msg.ip_addr.data, ip_str, INET_ADDRSTRLEN);
//网络字节流 ——> 字符串
23 printf("%s\n",ip_str);- 24 return 0;
- 25 }
test_ip.h文件用于放定义的结构体
- 1 #ifndef __TEST_IP_H__
- 2 #define __TEST_IP_H__
- 3
- 4 #include <stdio.h>
- 5
- 6 #define uint8_t unsigned char
- 7 #define uint32_t unsigned int
- 8 typedef struct _ipdata_s {
- 9 uint8_t len;
- 10 uint8_t *data;
- 11 } ipdata_t;
- 12
- 13 typedef struct _ipmsg_s
- 14 {
- 15 ipdata_t ip_addr;
- 16 uint32_t prefix_len;
- 17 }ipmsg_t;
- 18
- 19 typedef struct _ip_addr_s {
- 20 uint32_t ipv4; //ipv4地址有32位,uint32_t就够了
- 21 uint32_t ipv6[4]; //ipv6地址有128位,用数组存uint32_t*4
- 22 } ipaddr_t;
- 23
- 24 #endif
运行结果
- $ gcc -o test_ip test_ip.c
- $ ./test_ip
- decimal : 192.168.1.1 -> hexadecimal : 0x101a8c0
- decimal : 192.168.1.1 -> hexadecimal : 0xc0a80101
- 192.168.1.1
转换关系
十进制 | 192 | 168 | 1 | 1 |
二进制(32位) | 11000000 | 10101000 | 00000001 | 00000001 |
十六进制 | c0 | a8 | 1 | 1 |
IPv6地址的存取,test_ipv6.c
- 1 #include <stdio.h>
- 2 #include <arpa/inet.h>
- 3 #include "test_ip.h"
- 4
- 5 int main(int argc,char **argv)
- 6 {
- 7 ipmsg_t ip_msg;
- 8 ipaddr_t ip_addr;
- 9 char IPdotdec[46]="2017::192:168:11:1";
- 10 inet_pton(AF_INET6, IPdotdec, (void *)&ip_addr.ipv6);
//字符串 ——> 网络字节流
- 11 printf("十六进制 : %s \n",IPdotdec);
- 12 printf("十进制 : %u %u %u %u\n", ip_addr.ipv6[0], ip_addr.ipv6[1],
- 13 ip_addr.ipv6[2], ip_addr.ipv6[3]);
- 14
- 15
- 16 ip_msg.ip_addr.data = (uint8_t *)&(ip_addr.ipv6[0]);
- 17 ip_msg.ip_addr.len = 16;
- 18 ip_msg.prefix_len = 128;
- 19 //ipv6的字符串:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
20 char ipv6_str[46]={0}; //最长是32(“x”)+7(“:”)=39个字节。定义46的数组就足够了
- 21 inet_ntop(AF_INET6, ip_msg.ip_addr.data, ipv6_str, INET6_ADDRSTRLEN);
//网络字节流—>字符串
- 22 printf("%s\n",ipv6_str);
- 23 return 0;
- 24 }
运行结果
- $ gcc -o test_ipv6 test_ipv6.c
- $ ./test_ipv6
- 十六进制 : 2017::192:168:11:1
- 十进制 : 5920 0 1744933377 16781568
- 2017::192:168:11:1
转换关系
十六进制 | 1720 | 0 | 0 | 0 | 6801 | 9201 | 0100 | 1100 |
二进制(128位) | 0001011100100000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 110100000000001 | 1001001000000001 | 0000000100000000 | 0001000100000000 |
十进制 | 5920 | 0 | 1744933377 | 16781568 |
参考:
IPV6 转换二进制:https://www.easycalculation.com/other/ipv6-to-binary.php
IPv4和IPv6地址的存取的更多相关文章
- (转)协议森林04 地址耗尽危机 (IPv4与IPv6地址)
协议森林04 地址耗尽危机 (IPv4与IPv6地址) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! IP地址是IP协议的重要组 ...
- IP地址(IPv4)/IPv6地址的正则表达式
原地址:http://pfeishao.blog.163.com/blog/static/18162337020112113130453/ Pv4地址正则表达式:^((25[0-5]|2[0-4]\d ...
- 校验IPv4和IPv6地址和URL地址
1.校验IPV4地址: function validateIp(obj) { var ip=$(obj).val(); var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;// ...
- PHP中有关IPV4 和IPV6地址转换以及其它一些常见问题
这里主要介绍一下 IPV4 / IPV6 在 PHP / MySQL 中如何转换.以及中间容易碰到的一些问题. 首先介绍两个函数: ip2long:将 IPV4 的字符串互联网协议转换成长整型数字 i ...
- IPv4 和 IPv6地址
目前Internet上使用的基本都是IPv4地址,也就是说地址总共是32个比特位,也就是32位二进制数. 所以IPv4地址总的容量是 2的32次方 = 4294967296 比如 11010010 ...
- IPv6地址介绍
IPv6地址介绍 2008 年 04 月 10 日 1. 认识IPv6地址 IPv4地址是类似 A.B.C.D 的格式,它是32位,用\".\"分成四段,用10进制表示:而IPv6 ...
- 6.ipv6地址配置
1. "nmcli connection modify 网卡名 ipv4.addresses "ipv6地址" ipv6.method manual ". 2. ...
- IPv6下网络编程socket, TCP和UDP例子,以及兼容IPV4和IPV6的类
一.TCP socket ipv6与ipv4的区别 服务器端源代码如下: #include <stdio.h> #include <stdlib.h> #include < ...
- 【Atheros】pktgen的ipv6地址处理函数参考及ipv6基本知识
pktgen有很多函数可以作为很好的网络相关的工具函数,这里列出ipv6中1:0:0:0:0:0:0:1和1::1这两种地址形式相互转化的工具函数. 第一个函数,用于把一个1:0:0:0:0:0:0: ...
- 【转帖】2011-2018年中国IPv6地址数量及国际出口带宽数走势情况[图]
2011-2018年中国IPv6地址数量及国际出口带宽数走势情况[图] http://www.chyxx.com/industry/201910/791801.html 三亿多ipv4的地址. 接近9 ...
随机推荐
- vue中的v-if查找数组中最后一个,给他加上新的样式
vue: var app=new Vue({ el:".xiaomi", data: { typeInfo: [{img:"image/type/phone_1.webp ...
- 谣言检测(DUCK)《DUCK: Rumour Detection on Social Media by Modelling User and Comment Propagation Networks》
论文信息 论文标题:DUCK: Rumour Detection on Social Media by Modelling User and Comment Propagation Networks论 ...
- AVX图像算法优化系列二: 使用AVX2指令集加速查表算法。
查表算法,无疑也是一种非常常用.有效而且快捷的算法,我们在很多算法的加速过程中都能看到他的影子,在图像处理中,尤其常用,比如我们常见的各种基于直方图的增强,可以说,在photoshop中的调整菜单里8 ...
- selenium4-定位单个页面元素
在操作各项页面元素之前,先介绍下如何通过Python代码来找到这些元素.WebDriver提供了18种元素定位方法,共分为两类(定位当个元素.定位组元素),本节先举例详细介绍下selenium4-定位 ...
- java集合框架复习----(2)List
文章目录 三.List集合 listIterator:迭代器 List实现类 1.泛型类 2.泛型接口 三.List集合 特点 有序,打印输出的顺序和添加时的顺序一致(不会帮你自动排序) 有下标,可以 ...
- NAS数据存储之NFS搭建和使用
NFS是主流异构平台的共享文件系统之一,能够支持在不同类型的系统之间通过网络进行文件共享,允许一个系统在网络上与他人共享目录和文件.NFS传输协议用于服务器和客户机之间的文件访问和共享通信,从而使客户 ...
- MySQL索引报错
今天在MySQL 5.7版本的数据库中导库InnoDB表字段长度时遇到了"ERROR 1071 (42000): Specified key was too long; max key le ...
- 五、Python操作redis
五.Python操作redis 一.python对redis基本操作 (1)连接redis # 方式1 import redis r = redis.Redis(host='127.0.0.1', p ...
- Golang占位符
有哪些占位符? 常见占位符 %T 类型占位符 %v 值占位符 %d 整数占位符 %f 浮点占位符 %c 字符占位符 %s 字符串的占位符 占位符类型 通用占位符 占位符 说明 举例 %v 获取数据的值 ...
- LoadRunner11脚本小技能之添加请求头+定义变量+响应内容乱码转换打印+事务拆分
一.添加请求头 存在一些接口,发送请求时需要进行权限验证.登录验证(不加请求头时运行脚本,接口可能会报401等等),所以需要在脚本中给对应请求添加请求头.注意:请求头需在请求前添加,包含url类.su ...