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);
}