Linux C 检查IP和MAC的合法性

1,检测IP算法,接口说明也如下图。

 
  1. /*

  2. *brief

  3. *param: the ip address

  4. *return value: if susscess,0 is returned,or 1 is returned on error.

  5. */

  6. int checkValidIP(const char *ipaddr)

  7. {

  8. char str[31], temp[31];

  9. int a, b, c, d;

  10.  
  11. 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)

  12. {

  13. sprintf(temp, "%d.%d.%d.%d", a, b, c, d); //把格式化的数据写入字符串temp

  14. if (strcmp(temp, ipaddr) == 0)

  15. {

  16. return 0; //success

  17. }

  18. else

  19. {

  20. return 1;

  21. }

  22. }

  23. else

  24. {

  25. return 1;

  26. }

  27.  
  28. return 0;

  29. }

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进进字符制组成结尾

 
  1. /*

  2. *brief

  3. *param: pattern,正则表达式字符串;value,the mac address.

  4. *return value: if susscess,0 is returned,or 1 is returned on error.

  5. */

  6. int ereg(char *pattern, char *value)

  7. {

  8. int r, cflags = 0;

  9. regmatch_t pm[10];

  10. const size_t nmatch = 10;

  11. regex_t reg;

  12.  
  13. r = regcomp(&reg, pattern, cflags);

  14. if (r == 0)

  15. {

  16. r = regexec(&reg, value, nmatch, pm, cflags);

  17. }

  18. regfree(&reg);

  19.  
  20. return r;

  21. }

  22. /*

  23. *brief check whether the mac address is format-right

  24. *param: the mac address

  25. *return value: if susscess,0 is returned,or 1 is returned on error.

  26. */

  27. int isValidMac(char *macaddr)

  28. {

  29. int r;

  30. char reg[1024] = {"^[a-f0-9A-F]\\([a-f0-9A-F]\\:[a-f0-9A-F]\\)\\{5\\}[a-f0-9A-F]$"};

  31. r = ereg(reg, macaddr);

  32. return r;

  33. }

 
  1. #include <stdio.h>

  2. #include <sys/types.h>

  3. #include <regex.h>

  4.  
  5.  
  6. int is_valid_mac_addr(char* mac) {

  7.  
  8. int status;

  9. const char * pattern = "^([A-Fa-f0-9]{2}[-,:]){5}[A-Fa-f0-9]{2}$";

  10. const int cflags = REG_EXTENDED | REG_NEWLINE;

  11.  
  12. char ebuf[128];

  13. regmatch_t pmatch[1];

  14. int nmatch = 10;

  15. regex_t reg;

  16.  
  17.  
  18. status = regcomp(&reg, pattern, cflags);//编译正则模式

  19. if(status != 0) {

  20. regerror(status, &reg, ebuf, sizeof(ebuf));

  21. fprintf(stderr, "regcomp fail: %s , pattern '%s' \n",ebuf, pattern);

  22. goto failed;

  23. }

  24.  
  25. status = regexec(&reg, mac, nmatch, pmatch,0);//执行正则表达式和缓存的比较,

  26. if(status != 0) {

  27. regerror(status, &reg, ebuf, sizeof(ebuf));

  28. fprintf(stderr, "regexec fail: %s , mac:\"%s\" \n", ebuf, mac);

  29. goto failed;

  30. }

  31.  
  32. printf("[%s] match success.\n", __FUNCTION__);

  33. regfree(&reg);

  34. return 0;

  35.  
  36. failed:

  37. regfree(&reg);

  38. return -1;

  39. }

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,正则表达式介绍:

http://www.runoob.com/regexp/regexp-syntax.html

猜你喜欢

转载自blog.csdn.net/qq_19004627/article/details/84784827
今日推荐