From ba637fbcd0ba03d5329718fff5129c0922ec8fdd Mon Sep 17 00:00:00 2001 From: flykhan Date: Fri, 22 Sep 2023 15:16:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E6=95=B4=E5=90=88=E5=85=A5db.c=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- router/db.c | 90 +++++++++++++++++++++++++++++++++++++++- router/db.h | 40 ++++++++++++++++-- router/net_task_thread.c | 8 ++-- router/router.c | 64 +++------------------------- router/router_firewall.c | 2 + router/router_firewall.h | 4 ++ 6 files changed, 143 insertions(+), 65 deletions(-) create mode 100644 router/router_firewall.c create mode 100644 router/router_firewall.h diff --git a/router/db.c b/router/db.c index 76bd79c..3b89bd6 100644 --- a/router/db.c +++ b/router/db.c @@ -87,7 +87,7 @@ int insert(const char *sql, MYSQL_BIND *params) 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_prepare(stmt, sql, strlen(sql)); @@ -140,4 +140,92 @@ int result_rows(const char *sql) } 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"); + } } \ No newline at end of file diff --git a/router/db.h b/router/db.h index f7dc639..8c82571 100644 --- a/router/db.h +++ b/router/db.h @@ -1,3 +1,6 @@ +#ifndef __DB_H__ +#define __DB_H__ + #include #include @@ -9,7 +12,7 @@ extern int connect_mysql(const char *host, int port, const char *user, const cha // 关闭 extern int close_mysql(); -// 查询 +// 查询 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 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 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 \ No newline at end of file diff --git a/router/net_task_thread.c b/router/net_task_thread.c index c2b7e9b..8da980b 100644 --- a/router/net_task_thread.c +++ b/router/net_task_thread.c @@ -5,8 +5,8 @@ void *net_task(void *arg) { printf("net_task\n"); - char *dev1 = "ens33"; // 第一个网卡 - char *dev2 = "ens34"; // 第二个网卡 + char *dev1 = "ens38"; // 第一个网卡 + char *dev2 = "ens33"; // 第二个网卡 // 打开网卡设备 // 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; } + printf("testssssxxxxs\n"); + libnet_ptag_t t = libnet_build_ethernet( eth_hdr->ether_dhost, 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); } } -} \ No newline at end of file +} diff --git a/router/router.c b/router/router.c index cfb5d14..1993cfa 100644 --- a/router/router.c +++ b/router/router.c @@ -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() { printf("**************************************************\n"); @@ -172,10 +157,7 @@ void menu_choice() } // 查询数据库黑名单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); + int ret = search_ip_fw(add_blacked_ip); if (ret > 0) { @@ -184,30 +166,14 @@ void menu_choice() } // 将IP地址加入黑名单 - MYSQL_BIND parmas[1]; - 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"); - } + insert_ip_fw(add_blacked_ip); } // 后端查询黑名单 ip else if (2 == choice) { - printf("黑名单列表如下:\n"); - const char *select_ip_fw_sql = "select * from ip_fw"; - query(select_ip_fw_sql, printResult); + // 打印黑名单 + print_ip_fw(); } // 后端删除黑名单 ip @@ -226,32 +192,14 @@ void menu_choice() } // 查询数据库黑名单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); + int ret = search_ip_fw(delete_blacked_ip); if (ret > 0) { 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"); - } + delete_ip_fw(delete_blacked_ip); } else { diff --git a/router/router_firewall.c b/router/router_firewall.c new file mode 100644 index 0000000..136ea34 --- /dev/null +++ b/router/router_firewall.c @@ -0,0 +1,2 @@ +#include "router_firewall.h" + diff --git a/router/router_firewall.h b/router/router_firewall.h new file mode 100644 index 0000000..c6ee3f0 --- /dev/null +++ b/router/router_firewall.h @@ -0,0 +1,4 @@ +#ifndef __ROUTER_FIREWALL_H__ +#define __ROUTER_FIREWALL_H__ + +#endif \ No newline at end of file