2023-09-22 15:15:17 +08:00
|
|
|
/**
|
|
|
|
* 该文件用于匹配fw.html
|
|
|
|
* 使用该.c文件可实现从名为router的数据库中
|
|
|
|
* 查询存有黑名单数据的ip_fw表
|
|
|
|
*/
|
|
|
|
|
2023-09-21 11:23:55 +08:00
|
|
|
#include <cjson/cJSON.h>
|
|
|
|
#include <mysql/mysql.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
MYSQL *conn;
|
|
|
|
MYSQL_RES *res;
|
|
|
|
MYSQL_ROW row;
|
|
|
|
|
|
|
|
conn = mysql_init(NULL);
|
|
|
|
if (conn == NULL) {
|
|
|
|
fprintf(stderr, "mysql_init() failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-09-23 16:27:09 +08:00
|
|
|
if (mysql_real_connect(conn, "localhost", "flykhan", "1202", "router", 3306,
|
2023-09-21 11:23:55 +08:00
|
|
|
NULL, 0) == NULL) {
|
|
|
|
fprintf(stderr, "mysql_real_connect() failed\n");
|
|
|
|
mysql_close(conn);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mysql_query(conn, "SELECT * FROM ip_fw")) {
|
|
|
|
fprintf(stderr, "SELECT query failed. Error: %s\n", mysql_error(conn));
|
|
|
|
mysql_close(conn);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = mysql_store_result(conn);
|
|
|
|
if (res == NULL) {
|
|
|
|
fprintf(stderr, "mysql_store_result() failed\n");
|
|
|
|
mysql_close(conn);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON *result_array = cJSON_CreateArray();
|
|
|
|
|
|
|
|
while ((row = mysql_fetch_row(res))) {
|
|
|
|
cJSON *item = cJSON_CreateObject();
|
|
|
|
cJSON_AddStringToObject(item, "id", row[0]);
|
|
|
|
cJSON_AddStringToObject(item, "ip", row[1]);
|
|
|
|
cJSON_AddItemToArray(result_array, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *result_json_string = cJSON_Print(result_array);
|
|
|
|
|
|
|
|
printf("Content-Type: application/json;charset=utf-8\r\n");
|
|
|
|
printf("\r\n");
|
|
|
|
printf("%s", result_json_string);
|
|
|
|
|
|
|
|
free(result_json_string);
|
|
|
|
cJSON_Delete(result_array);
|
|
|
|
mysql_free_result(res);
|
|
|
|
mysql_close(conn);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|