数据库操作全部整合入db.c中

This commit is contained in:
flykhan 2023-09-22 15:16:23 +08:00
parent f972b37e39
commit ba637fbcd0
6 changed files with 143 additions and 65 deletions

View File

@ -87,7 +87,7 @@ int insert(const char *sql, MYSQL_BIND *params)
return ret; // 返回是影响的行数 return ret; // 返回是影响的行数
} }
int delete(const char *sql, MYSQL_BIND *params) int delete (const char *sql, MYSQL_BIND *params)
{ {
MYSQL_STMT *stmt = mysql_stmt_init(conn_db); MYSQL_STMT *stmt = mysql_stmt_init(conn_db);
mysql_stmt_prepare(stmt, sql, strlen(sql)); mysql_stmt_prepare(stmt, sql, strlen(sql));
@ -140,4 +140,92 @@ int result_rows(const char *sql)
} }
return rows; return rows;
}
void insert_routing_list(const char *ip, const char *mask, const char *nexthop)
{
char sql[100];
sprintf(sql, "insert into routing_list(ip, mask, nexthop) values('%s', '%s', '%s')", ip, mask, nexthop);
if (mysql_real_query(conn_db, sql, strlen(sql)) != 0)
{
printf("添加路由表失败\n");
}
}
void delete_routing_list(const char *ip)
{
char sql[100];
sprintf(sql, "delete from routing_list where ip = '%s'", ip);
if (mysql_real_query(conn_db, sql, strlen(sql)) != 0)
{
printf("删除路由表失败\n");
}
}
void print_routing_list()
{
char sql[100];
sprintf(sql, "select * from routing_list");
query(sql, printResult); // 打印查询结果
}
void insert_arp_list(const char *ip, const char *mac)
{
char sql[100];
sprintf(sql, "insert into ip_mac(ip, mac) values('%s', '%s')", ip, mac);
if (mysql_real_query(conn_db, sql, strlen(sql)) != 0)
{
printf("添加ARP表失败\n");
}
}
void print_arp_list()
{
char sql[100];
sprintf(sql, "select * from ip_mac");
query(sql, printResult); // 打印查询结果
}
void printResult(MYSQL_ROW row, char (*columns)[30], int cols)
{
for (int i = 0; i < cols; i++)
{
printf("%s: %s \t", columns[i], row[i]);
}
printf("\n");
}
void insert_ip_fw(const char *ip)
{
char sql[100];
sprintf(sql, "insert into ip_fw(ip) values('%s')", ip);
if (mysql_real_query(conn_db, sql, strlen(sql)) != 0)
{
printf("添加黑名单失败\n");
}
}
int search_ip_fw(const char *ip)
{
char sql[100];
sprintf(sql, "select * from ip_fw where ip = '%s'", ip);
int ret = result_rows(sql);
return ret;
}
void print_ip_fw()
{
char sql[100];
sprintf(sql, "select * from ip_fw");
query(sql, printResult); // 打印查询结果
}
void delete_ip_fw(const char *ip)
{
char sql[100];
sprintf(sql, "delete from ip_fw where ip = '%s'", ip);
if (mysql_real_query(conn_db, sql, strlen(sql)) != 0)
{
printf("删除黑名单失败\n");
}
} }

View File

@ -1,3 +1,6 @@
#ifndef __DB_H__
#define __DB_H__
#include <mysql/mysql.h> #include <mysql/mysql.h>
#include <string.h> #include <string.h>
@ -9,7 +12,7 @@ extern int connect_mysql(const char *host, int port, const char *user, const cha
// 关闭 // 关闭
extern int close_mysql(); extern int close_mysql();
// 查询 // 查询
extern int query(const char *sql, void (*callback)(MYSQL_ROW row, char (*columns)[30], int cols)); extern int query(const char *sql, void (*callback)(MYSQL_ROW row, char (*columns)[30], int cols));
// 返回查询结果的行数 // 返回查询结果的行数
@ -19,8 +22,39 @@ extern int result_rows(const char *sql);
extern int insert(const char *sql, MYSQL_BIND *params); extern int insert(const char *sql, MYSQL_BIND *params);
// 删除 // 删除
extern int delete(const char *sql, MYSQL_BIND *params); extern int delete (const char *sql, MYSQL_BIND *params);
// 更新 // 更新
extern int update(const char *sql, MYSQL_BIND *params); extern int update(const char *sql, MYSQL_BIND *params);
// 添加路由表
extern void insert_routing_list(const char *ip, const char *mask, const char *nexthop);
// 删除路由表中的一条记录
extern void delete_routing_list(const char *ip);
// 打印路由表
extern void print_routing_list();
// 打印查询结果的回调函数
extern void printResult(MYSQL_ROW row, char (*columns)[30], int cols);
// 添加ARP表
extern void insert_arp_list(const char *ip, const char *mac);
// 打印ARP表
extern void print_arp_list();
// 添加黑名单
extern void insert_ip_fw(const char *ip);
// 查询黑名单中是否存在此IP
extern int search_ip_fw(const char *ip);
// 打印黑名单
extern void print_ip_fw();
// 删除黑名单中的一条记录
extern void delete_ip_fw(const char *ip);
#endif

View File

@ -5,8 +5,8 @@ void *net_task(void *arg)
{ {
printf("net_task\n"); printf("net_task\n");
char *dev1 = "ens33"; // 第一个网卡 char *dev1 = "ens38"; // 第一个网卡
char *dev2 = "ens34"; // 第二个网卡 char *dev2 = "ens33"; // 第二个网卡
// 打开网卡设备 // 打开网卡设备
// libnet_context1 使用 open_device 函数打开网卡设备1 后返回的 libnet 上下文 // libnet_context1 使用 open_device 函数打开网卡设备1 后返回的 libnet 上下文
@ -118,6 +118,8 @@ void process_packet(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char
libnet = libnet_context1; libnet = libnet_context1;
} }
printf("testssssxxxxs\n");
libnet_ptag_t t = libnet_build_ethernet( libnet_ptag_t t = libnet_build_ethernet(
eth_hdr->ether_dhost, eth_hdr->ether_dhost,
eth_hdr->ether_shost, eth_hdr->ether_shost,
@ -204,4 +206,4 @@ void process_arp_packet(const u_char *packet)
printf("ARP: %s -> %s\n", ip_address, mac_address); printf("ARP: %s -> %s\n", ip_address, mac_address);
} }
} }
} }

View File

@ -70,21 +70,6 @@ int is_blocked_ip(unsigned char *ip)
} }
} }
void printResult(MYSQL_ROW row, char (*columns)[30], int cols)
{
// printf("printResult\n");
// printf("cols: %d\n", cols);
// printf("row: %s\n", row[0]);
// printf("%s\n", columns[0]);
for (int i = 0; i < cols; i++)
{
printf("%s: %s \t", columns[i], row[i]);
}
printf("\n");
}
void show() void show()
{ {
printf("**************************************************\n"); printf("**************************************************\n");
@ -172,10 +157,7 @@ void menu_choice()
} }
// 查询数据库黑名单IP表中是否已存在此IP // 查询数据库黑名单IP表中是否已存在此IP
char search_ip_fw_sql[1024]; int ret = search_ip_fw(add_blacked_ip);
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) if (ret > 0)
{ {
@ -184,30 +166,14 @@ void menu_choice()
} }
// 将IP地址加入黑名单 // 将IP地址加入黑名单
MYSQL_BIND parmas[1]; insert_ip_fw(add_blacked_ip);
parmas[0].buffer_type = MYSQL_TYPE_STRING;
parmas[0].buffer = add_blacked_ip;
parmas[0].buffer_length = strlen(add_blacked_ip);
parmas[0].is_null = 0;
parmas[0].length = &parmas[0].buffer_length;
const char *insert_ip_fw_sql = "insert into ip_fw(ip) values(?)";
int result = insert(insert_ip_fw_sql, parmas);
if (result > 0)
{
printf("黑名单添加成功\n");
}
else
{
printf("黑名单添加失败\n");
}
} }
// 后端查询黑名单 ip // 后端查询黑名单 ip
else if (2 == choice) else if (2 == choice)
{ {
printf("黑名单列表如下:\n"); // 打印黑名单
const char *select_ip_fw_sql = "select * from ip_fw"; print_ip_fw();
query(select_ip_fw_sql, printResult);
} }
// 后端删除黑名单 ip // 后端删除黑名单 ip
@ -226,32 +192,14 @@ void menu_choice()
} }
// 查询数据库黑名单IP表中是否已存在此IP // 查询数据库黑名单IP表中是否已存在此IP
char search_ip_fw_sql[1024]; int ret = search_ip_fw(delete_blacked_ip);
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) if (ret > 0)
{ {
printf("delete_blacked_ip: %s\n", delete_blacked_ip); printf("delete_blacked_ip: %s\n", delete_blacked_ip);
// 将IP地址从黑名单中删除 // 将IP地址从黑名单中删除
MYSQL_BIND parmas[1]; delete_ip_fw(delete_blacked_ip);
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 else
{ {

2
router/router_firewall.c Normal file
View File

@ -0,0 +1,2 @@
#include "router_firewall.h"

4
router/router_firewall.h Normal file
View File

@ -0,0 +1,4 @@
#ifndef __ROUTER_FIREWALL_H__
#define __ROUTER_FIREWALL_H__
#endif