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,正則表達式介紹: