数据库操作全部整合入db.c中
This commit is contained in:
parent
f972b37e39
commit
ba637fbcd0
90
router/db.c
90
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));
|
||||
|
@ -141,3 +141,91 @@ 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");
|
||||
}
|
||||
}
|
38
router/db.h
38
router/db.h
|
@ -1,3 +1,6 @@
|
|||
#ifndef __DB_H__
|
||||
#define __DB_H__
|
||||
|
||||
#include <mysql/mysql.h>
|
||||
#include <string.h>
|
||||
|
||||
|
@ -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
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
#include "router_firewall.h"
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
#ifndef __ROUTER_FIREWALL_H__
|
||||
#define __ROUTER_FIREWALL_H__
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue