筆記-TCP/IP IPv4/IPv6位址比較
- IP位址轉換函數
-
- 轉換執行個體
- IP位址比較
-
- 原理
- 比較大小
IP位址轉換函數
在比較IP位址之前需要将其轉換為網絡位元組序的二進制整數,常用的IP位址轉換函數是 inet_pton(),其支援IPv4和IPv6.
windows下:
#include <WS2tcpip.h>
linux下:
#include <arpa/inet.h>
int inet_pton(int af, const char *src, void *dst);
轉換執行個體
#include <arpa/inet.h>
int main()
{
// IPv4
char *str = "192.168.1.1";
struct in_addr ipv4; // 儲存IPv4位址的結構體
inet_pton(AF_INET, str, &ipv4); // AF_INET表示IPv4位址協定簇
// IPv6
char *str6 = "2000::1";
struct in6_addr ipv6; // 儲存IPv6位址的結構體
inet_pton(AF_INET6, str6, &ipv6); // AF_INET6表示IPv6位址協定簇
return 0;
}
IP位址比較
原理
大端模式 - 資料的高位元組儲存在記憶體的低位址中
小端模式 - 資料的高位元組儲存在記憶體的高位址中
以IPv4位址"192.168.2.1" 為例:
下面的例子,第一位元組為最低位址,第四位元組為最高位址
模式 | 第一位元組 | 第二位元組 | 第三位元組 | 第四位元組 |
---|---|---|---|---|
大端 | 192 | 168 | 2 | 1 |
小端 | 1 | 2 | 168 | 192 |
是以将需要比較的位址轉換為網絡位元組序(大端模式)後,使用函數 memcmp() 就可以按位元組比較兩個位址大小。
int memcmp(const void *buf1, const void *buf2, unsigned int count);
比較大小
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <arpa/inet.h>
// 簡單判斷是否為IPv6
bool isipv6(const char* ipstr)
{
assert(ipstr);
return !!strchr(ipstr, ':');
}
// IP位址比較,要求兩個IP位址同一類型,格式符合标準
int ipcmp(const char* ipstr1, const char *ipstr2)
{
assert(ipstr1 && ipstr2);
assert(isipv6(ipstr1) == isipv6(ipstr2)); // 斷言 兩個IP位址同一類型
char buf1[sizeof(struct in6_addr)] = {0};
char buf2[sizeof(struct in6_addr)] = {0};
int domain = isipv6(ipstr1) ? AF_INET6 : AF_INET;
int length = domain==AF_INET ? sizeof(struct in_addr) : sizeof(struct in6_addr);
int s1 = inet_pton(domain, ipstr1, buf1);
int s2 = inet_pton(domain, ipstr2, buf2);
assert(s1>0 && s2>0); // 斷言 兩個IP格式符合标準
return memcmp(buf1, buf2, length);
}
int main(int argc, char **argv)
{
if(argc != 3){
fprintf(stderr, "usage: %s string string\n", argv[0]);
exit(EXIT_FAILURE);
}
int result = ipcmp(argv[1], argv[2]);
if (result > 0)
printf("%s is greater than %s\n", argv[1], argv[2]);
else if (result < 0)
printf("%s is less than %s\n", argv[1], argv[2]);
else
printf("%s is equal to %s\n", argv[1], argv[2]);
exit(EXIT_SUCCESS);
}