ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C语言检查IP、MAC合法函数 实用 码住喽!

2021-09-22 16:31:53  阅读:339  来源: 互联网

标签:return IP printf MAC 码住 mac token ip FALSE


一、简单说说

  • 最近在项目开发中用到的几个很实用的小函数推荐给大家,提高开发时间效率!话不多说,直接上代码哈 ~

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

看完后感觉得到帮助的小伙伴,要点点赞哦~
给笔者一些动力嘛!谢谢啦~

标签:return,IP,printf,MAC,码住,mac,token,ip,FALSE
来源: https://blog.csdn.net/weixin_45636395/article/details/120333553

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有