黑名单数据库增加删除前, 增加判断规则, 看黑名单数据库中是否可以查到已有数据

This commit is contained in:
flykhan 2023-09-21 16:15:24 +08:00
parent 84cc8c6e23
commit afc1b2e97e
2 changed files with 41 additions and 14 deletions

Binary file not shown.

View File

@ -113,7 +113,7 @@ int is_ip_valid(const char *ip)
// 编译正则表达式
// /^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(?::(?:[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$/
ret = regcomp(&regex, "^((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 扩展正则表达式
ret = regcomp(&regex, "^((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");
@ -171,6 +171,18 @@ void menu_choice()
continue;
}
// 查询数据库黑名单IP表中是否已存在此IP
char search_ip_fw_sql[1024];
sprintf(search_ip_fw_sql, "select * from ip_fw where ip = '%s'", add_blacked_ip);
int ret = result_rows(search_ip_fw_sql);
printf("查询结果为%d\n", ret);
if (ret > 0)
{
printf("黑名单中已存在此IP\n");
continue;
}
// 将IP地址加入黑名单
MYSQL_BIND parmas[1];
parmas[0].buffer_type = MYSQL_TYPE_STRING;
@ -205,7 +217,7 @@ void menu_choice()
printf("请输入要删除的黑名单IP格式如 192.168.6.5\n");
fgets(delete_blacked_ip, sizeof(delete_blacked_ip), stdin);
delete_blacked_ip[strlen(delete_blacked_ip) - 1] = '\0'; // 将最后的换行符替换为字符串结束符
printf("delete_blacked_ip: %s\n", delete_blacked_ip);
// IP 格式检查
if (is_ip_valid(delete_blacked_ip) != 0)
{
@ -213,22 +225,37 @@ void menu_choice()
continue;
}
// 将IP地址从黑名单中删除
MYSQL_BIND parmas[1];
parmas[0].buffer_type = MYSQL_TYPE_STRING;
parmas[0].buffer = delete_blacked_ip;
parmas[0].buffer_length = strlen(delete_blacked_ip);
parmas[0].is_null = 0;
parmas[0].length = &parmas[0].buffer_length;
const char *delete_ip_fw_sql = "delete from ip_fw where ip = ?";
int result = delete (delete_ip_fw_sql, parmas);
if (result > 0)
// 查询数据库黑名单IP表中是否已存在此IP
char search_ip_fw_sql[1024];
sprintf(search_ip_fw_sql, "select * from ip_fw where ip = '%s'", delete_blacked_ip);
int ret = result_rows(search_ip_fw_sql);
printf("查询结果为%d\n", ret);
if (ret > 0)
{
printf("黑名单删除成功\n");
printf("delete_blacked_ip: %s\n", delete_blacked_ip);
// 将IP地址从黑名单中删除
MYSQL_BIND parmas[1];
parmas[0].buffer_type = MYSQL_TYPE_STRING;
parmas[0].buffer = delete_blacked_ip;
parmas[0].buffer_length = strlen(delete_blacked_ip);
parmas[0].is_null = 0;
parmas[0].length = &parmas[0].buffer_length;
const char *delete_ip_fw_sql = "delete from ip_fw where ip = ?";
int result = delete (delete_ip_fw_sql, parmas);
if (result > 0)
{
printf("黑名单删除成功\n");
}
else
{
printf("黑名单删除失败\n");
}
}
else
{
printf("黑名单删除失败\n");
printf("黑名单中不存在此IP\n");
}
}