一、简单说说
- 最近在项目开发中用到的几个很实用的小函数推荐给大家,提高开发时间效率!话不多说,直接上代码哈 ~
IP合法检验函数
- 凡是有一点点错误的IP地址统统卡死,哎,都是面向测试部编程的经验 !!!。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define FALSE 0
#define TRUE 1
bool ip_check(const char *ip)
{
int dots = 0; //字符 . 的个数
int setions = 0; //ip每一部分总和(0-255)
char ip_temp[16+1] = {0}; //IP缓存
char *token = NULL; //分割的字串
strncpy(ip_temp, ip, sizeof(ip_temp));//IP备份
token = strtok(ip_temp, "."); //获取第一个子字符串
while (token != NULL) //继续获取其他的子字符串
{
printf("token:<%s>\n",token);
if (strlen(token) > 3) //每一个IP段长度不能大于3
{
return FALSE;
}
token = strtok(NULL, ".");
}
if (NULL == ip || *ip == '.')
{
return FALSE; //排除输入参数为NULL, 或者一个字符为'.'的字符串
}
while (*ip)
{
if (*ip == '.')
{
dots ++;
if (setions >= 0 && setions <= 255) //检查ip是否合法
{
setions = 0;
ip++;
continue;
}else
{
return FALSE;
}
}
else if (*ip >= '0' && *ip <= '9')
{ /*判断是不是数字*/
setions = setions * 10 + (*ip - '0'); /*求每一段总和*/
} else
return FALSE;
ip++;
}
if (setions >= 0 && setions <= 255) /*判断IP最后一段是否合法*/
{
if (dots == 3)
{
return TRUE;
}
}
return FALSE;
}
int main()
{
if (ip_check("1.01.255.0255") == TRUE)
{
printf("is ip is ok!\n");
}else
{
printf("is ip is err!!!\n");
}
return 0;
}
MAC合法检验函数
- 说明一下mac地址的分隔符可能有所不同,改一下分隔符即可。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define FALSE 0
#define TRUE 1
bool mac_check(const char *mac)
{
int dots = 0; //字0符 : 的个数
char mac_temp[17+1] = {0}; //mac缓存
char *token = NULL; //分割字串
if (NULL == mac || *mac == '.')
{
return FALSE; //排除输入参数为NULL, 或者一个字符为':'的字符串
}
if(strlen(mac) != 17) //长度判断
{
return FALSE;
}
strncpy(mac_temp, mac, sizeof(mac_temp)); //mac备份
printf("mac:<%s\n>",mac);
printf("mac_temp<%s\n>",mac_temp);
token = strtok(mac_temp, ":"); //获取第一个子字符串
while (token != NULL)
{
printf("mac:<%s>\n",token);
if (strlen(token) != 2) //字串个数为2
{
return FALSE;
}
while (*token)
{
printf("*token:<%c>\n",*token);
if ('0' <= *token && *token <= '9')
{
;
}
else if ('A' <= *token && *token <= 'F')
{
;
}
else if ('a' <= *token && *token <= 'f')
{
;
}
else
{
return FALSE;
}
token++;
}
token = strtok(NULL,":");
dots++;
}
if (dots != 6) // 字串的个数
{
printf("dots:<%d>\n",dots);
return FALSE;
}
else
{
return TRUE;
}
}
int main()
{
if (mac_check("BC:54:2F:BF:5E:,8") == TRUE)
{
printf("is mac is ok!\n");
}else
{
printf("is mac is err!!!\n");
}
return 0;
}
子网掩码合法检验函数 (位操作法)
- 先验证是否为合法IP。
- 再排除特殊子网掩码 “0.0.0.0” 是合法子网掩码,但不可设置,不可用。
- 然后将掩码转化成32无符号整型,取反为000…00111…1,然后再加1为00…01000…0,此时为2^n,如果满足就为合法掩码。
bool mask_check(const char *mask)
{
if(ip_check(mask) != FALSE)
{
unsigned int b = 0, i, n[4];
sscanf(mask, "%u.%u.%u.%u", &n[3], &n[2], &n[1], &n[0]);
if (strcmp(mask,"0.0.0.0") == 0) //"0.0.0.0" 是合法子网掩码,但不可设置,不可用。
return FALSE;
for(i = 0; i < 4; ++i) //将子网掩码存入32位无符号整型
{
b += n[i] << (i * 8);
}
b = ~b + 1;
if((b & (b - 1)) == 0) //判断是否为2^n
return TRUE;
}
return FALSE;
}
子网掩码合法检验函数 (字符串查询法)
- 此类方法虽简单明了,但是消耗CPU资源。
char rightmask[31][16] = {"255.255.255.255","128.0.0.0","192.0.0.0","224.0.0.0","240.0.0.0","248.0.0.0","252.0.0.0","254.0.0.0","255.0.0.0", "255.128.0.0","255.192.0.0","255.224.0.0","255.240.0","255.248.0.0","255.252.0.0","255.254.0.0","255.255.0.0", "255.255.128.0","255.255.192.0","255.255.224.0","255.255.240.0","255.255.248.0","255.255.252.0","255.255.254.0","255.255.255.0", "255.255.255.128","255.255.255.192","255.255.255.224","255.255.255.240","255.255.255.248","255.255.255.252" };
bool mask_check(const char *mask)
{
if (ip_check(mask) != true) //首先是合法的IPV4地址
{
return FALSE;
}
for(int i=0; i<31; i++ ) //查找是否为正确的子网掩码
{
if(strcmp(mask,rightmask[i])==0)
{
return TRUE;
}
}
return FALSE;
}
看完后感觉得到帮助的小伙伴,要点点赞哦~
给笔者一些动力嘛!谢谢啦~