From 925c1f68e66fdaaa244c9482d778993137f01440 Mon Sep 17 00:00:00 2001 From: flykhan Date: Sat, 23 Sep 2023 16:37:01 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=AF=E7=94=B1=E6=A8=A1=E5=9D=97:=20?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=87=8D=E6=9E=84=E5=90=8E=E7=9A=84=E5=A4=9A?= =?UTF-8?q?=E4=BD=99=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- router/router.c | 99 +++++++++++++++++-------------------------------- router/router.h | 5 --- 2 files changed, 35 insertions(+), 69 deletions(-) diff --git a/router/router.c b/router/router.c index 6c2f474..9bae1e6 100644 --- a/router/router.c +++ b/router/router.c @@ -24,35 +24,6 @@ void net_init() printf("数据库连接错误\n"); } -// MySQL 测试 -int is_blocked_ip(unsigned char *ip) -{ - char sql[1024] = ""; - sprintf(sql, "select * from ip_fw where ip = '%s'", ip); - int rows = result_rows(sql); - - printf("打印 %s\n", sql); - printf("rows: %d\n", rows); - - // execlp("iptables", "iptables", "-A", "FORWARD", "-s", ip, "-j", "DROP", NULL); // 将源IP地址加入防火墙 - execlp("iptables", "iptables", "-D", "FORWARD", "-s", ip, "-j", "DROP", NULL); // 将源IP地址加入防火墙 - - // 获取行数 - if (rows > 0) - { - printf("IP地址 %s 在黑名单中\n", ip); - // execlp("iptables", "iptables", "-A", "FORWARD", "-s", ip, "-j", "DROP", NULL); // 将源IP地址加入防火墙 - // 从防火墙删除 - execlp("iptables", "iptables", "-D", "FORWARD", "-s", ip, "-j", "DROP", NULL); // 将源IP地址加入防火墙 - return 0; - } - else - { - printf("IP地址 %s 不在黑名单中\n", ip); - return -1; - } -} - void show() { printf("**************************************************\n"); @@ -73,41 +44,6 @@ void show() printf("**************************************************\n"); } -// 判断IP地址格式是否正确 -int is_ip_valid(const char *ip) -{ - regex_t regex; // 正则表达式 - int ret; - - // 编译正则表达式 - ret = regcomp(®ex, "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", REG_EXTENDED); // REG_EXTENDED 扩展正则表达式 - if (ret != 0) - { - printf("regcomp error\n"); - return -1; - } - - // 执行正则表达式 - ret = regexec(®ex, ip, 0, NULL, 0); - regfree(®ex); // 释放正则表达式 - - if (ret == 0) - { - printf("IP地址格式正确\n"); - return 0; - } - else if (ret == REG_NOMATCH) - { - printf("IP地址格式非法\n"); - return -1; - } - else - { - printf("正则表达式匹配错误\n"); - return -1; - } -} - // 终端控制菜单 void menu_choice() { @@ -235,4 +171,39 @@ void menu_choice() printf("输入错误,请重新输入\n"); } } +} + +// 判断IP地址格式是否正确 +int is_ip_valid(const char *ip) +{ + regex_t regex; // 正则表达式 + int ret; + + // 编译正则表达式 + ret = regcomp(®ex, "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", REG_EXTENDED); // REG_EXTENDED 扩展正则表达式 + if (ret != 0) + { + printf("regcomp error\n"); + return -1; + } + + // 执行正则表达式 + ret = regexec(®ex, ip, 0, NULL, 0); + regfree(®ex); // 释放正则表达式 + + if (ret == 0) + { + printf("IP地址格式正确\n"); + return 0; + } + else if (ret == REG_NOMATCH) + { + printf("IP地址格式非法\n"); + return -1; + } + else + { + printf("正则表达式匹配错误\n"); + return -1; + } } \ No newline at end of file diff --git a/router/router.h b/router/router.h index fd8996a..e023708 100644 --- a/router/router.h +++ b/router/router.h @@ -24,15 +24,10 @@ // int sockfd; // 原始套接字 int ret; // 调用数据库的返回值 char *errmsg; // 错误信息 -char **dbResult; -int nrow; -int ncolumn; void menu_choice(); void show(void); // 后端控制菜单 void net_init(); // 网络初始化 -void *net_task(void *arg); // 网络任务 -int is_blocked_ip(unsigned char *ip); // 黑名单探测 void printResult(MYSQL_ROW row, char (*columns)[30], int cols); // 显示查询结果的回调函数 int is_ip_valid(const char *ip); // 检查ip地址是否合法