1,检测IP算法,接口说明也如下图。
-
/*
-
*brief
-
*param: the ip address
-
*return value: if susscess,0 is returned,or 1 is returned on error.
-
*/
-
int checkValidIP(const char *ipaddr)
-
{
-
char str[31], temp[31];
-
int a, b, c, d;
-
if (sscanf(ipaddr, "%d.%d.%d.%d ", &a, &b, &c, &d) == 4 && a >= 0 && a <= 255 && b >= 0 && b <= 255 && c >= 0 && c <= 255 && d >= 0 && d <= 255)
-
{
-
sprintf(temp, "%d.%d.%d.%d", a, b, c, d); //把格式化的数据写入字符串temp
-
if (strcmp(temp, ipaddr) == 0)
-
{
-
return 0; //success
-
}
-
else
-
{
-
return 1;
-
}
-
}
-
else
-
{
-
return 1;
-
}
-
return 0;
-
}
2,检测Mac算法,接口说明也如下图。
MAC(Media Access Control或者Medium Access Control)地址,意译为媒体访问控制,或称为物理地址、硬件地址,用来定义网络设备的位置。在OSI模型中,第三层网络层负责 IP地址,第二层数据链路层则负责 MAC地址。MAC地址是网卡决定的,是固定的。因此一个主机会有一个MAC地址,而每个网络位置会有一个专属于它的IP地址。为确保MAC地址的唯一性,以太网卡制造商将MAC地址固化到网卡中。地址的前半部分(24位)标识网卡的制造商,由IEEE分配,称为OUI(组织唯一标识符);地址的后半部分由网卡制造商为其网卡分配一个唯一的编号。MAC地址为电脑网卡的物理地址,每个网卡拥有全球唯一的MAC,以示区别。格式通常为6个字节的二进制代码(以6组16进制数表示,共占6个字节),格式为XX-XX-XX-XX-XX-XX 或者 XX:XX:XX:XX:XX:XX 比如: ED-DD-4D-45-5A-9F
在一些使用MAC地址的场合中,需要对MAC地址进行合法性检测,以下提示C和JAVA两种检测方法,原理基本一致,都是使用正则表达式进行匹配:
匹配模式:"^([A-Fa-f0-9]{2}[-,:]){5}[A-Fa-f0-9]{2}$"
^ 代表开头
[A-Fa-f0-9]{2} 表示由两个16进进字符制组成
[-,:] 分隔符可能 为 " : "或 " - "
{5} 前面由5个类似格式的字段组成,即 5个 XX- 或 XX:
[A-Fa-f0-9]{2} 表示由两个16进进字符制组成结尾
-
/*
-
*brief
-
*param: pattern,正则表达式字符串;value,the mac address.
-
*return value: if susscess,0 is returned,or 1 is returned on error.
-
*/
-
int ereg(char *pattern, char *value)
-
{
-
int r, cflags = 0;
-
regmatch_t pm[10];
-
const size_t nmatch = 10;
-
regex_t reg;
-
r = regcomp(®, pattern, cflags);
-
if (r == 0)
-
{
-
r = regexec(®, value, nmatch, pm, cflags);
-
}
-
regfree(®);
-
return r;
-
}
-
/*
-
*brief check whether the mac address is format-right
-
*param: the mac address
-
*return value: if susscess,0 is returned,or 1 is returned on error.
-
*/
-
int isValidMac(char *macaddr)
-
{
-
int r;
-
char reg[1024] = {"^[a-f0-9A-F]\\([a-f0-9A-F]\\:[a-f0-9A-F]\\)\\{5\\}[a-f0-9A-F]$"};
-
r = ereg(reg, macaddr);
-
return r;
-
}
-
#include <stdio.h>
-
#include <sys/types.h>
-
#include <regex.h>
-
int is_valid_mac_addr(char* mac) {
-
int status;
-
const char * pattern = "^([A-Fa-f0-9]{2}[-,:]){5}[A-Fa-f0-9]{2}$";
-
const int cflags = REG_EXTENDED | REG_NEWLINE;
-
char ebuf[128];
-
regmatch_t pmatch[1];
-
int nmatch = 10;
-
regex_t reg;
-
status = regcomp(®, pattern, cflags);//编译正则模式
-
if(status != 0) {
-
regerror(status, ®, ebuf, sizeof(ebuf));
-
fprintf(stderr, "regcomp fail: %s , pattern '%s' \n",ebuf, pattern);
-
goto failed;
-
}
-
status = regexec(®, mac, nmatch, pmatch,0);//执行正则表达式和缓存的比较,
-
if(status != 0) {
-
regerror(status, ®, ebuf, sizeof(ebuf));
-
fprintf(stderr, "regexec fail: %s , mac:\"%s\" \n", ebuf, mac);
-
goto failed;
-
}
-
printf("[%s] match success.\n", __FUNCTION__);
-
regfree(®);
-
return 0;
-
failed:
-
regfree(®);
-
return -1;
-
}
3,相关库函数—— regcomp regexec讲解 参考链接
https://blog.csdn.net/tanghaiyu777/article/details/65444332
https://www.cnblogs.com/274914765qq/p/4574367.html
https://www.cnblogs.com/lifan3a/articles/7536749.html
https://www.cnblogs.com/catgatp/p/8052556.html
4,正则表达式介绍: