From a6a46d9b19b10bbf103df589945d09e5be3f0bb7 Mon Sep 17 00:00:00 2001 From: wang_chengh <13383929+wang_chengh@user.noreply.gitee.com> Date: Fri, 22 Sep 2023 09:36:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=AE=9E=E7=8E=B0=E9=98=B2?= =?UTF-8?q?=E7=81=AB=E5=A2=99=E6=B7=BB=E5=8A=A0=E5=88=A0=E9=99=A4=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- www/cgi-bin/c_mysql_ip_fw_add_del.c | 84 +++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 www/cgi-bin/c_mysql_ip_fw_add_del.c diff --git a/www/cgi-bin/c_mysql_ip_fw_add_del.c b/www/cgi-bin/c_mysql_ip_fw_add_del.c new file mode 100644 index 0000000..f7ee6f1 --- /dev/null +++ b/www/cgi-bin/c_mysql_ip_fw_add_del.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include + +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); + } + + if (mysql_real_connect(conn, "localhost", "root", "root", "router", 3306, + NULL, 0) == NULL) { + fprintf(stderr, "mysql_real_connect() failed\n"); + mysql_close(conn); + exit(1); + } + + // 获取前端传递的JSON数据 + char request_data[128] = ""; + fgets(request_data, 128, stdin); + + // 解析JSON数据 + cJSON *p = cJSON_Parse(request_data); + cJSON *code = cJSON_GetObjectItemCaseSensitive(p, "code"); + cJSON *ip = cJSON_GetObjectItemCaseSensitive(p, "ip"); + + if (!cJSON_IsNumber(code) || !cJSON_IsString(ip)) { + fprintf(stderr, "Error getting code or IP address from JSON data\n"); + cJSON_Delete(p); + mysql_close(conn); + exit(1); + } + + int operation_code = code->valueint; + char *ip_address = ip->valuestring; + + if (operation_code == 0) { + // 插入IP地址到数据库 + char query[1000]; + sprintf(query, "INSERT INTO ip_fw (ip) VALUES ('%s')", ip_address); + + if (mysql_query(conn, query)) { + fprintf(stderr, "INSERT query failed. Error: %s\n", + mysql_error(conn)); + cJSON_Delete(p); + mysql_close(conn); + exit(1); + } + } else if (operation_code == 1) { + // 从数据库中删除指定IP地址 + char query[1000]; + sprintf(query, "DELETE FROM ip_fw WHERE ip='%s'", ip_address); + + if (mysql_query(conn, query)) { + fprintf(stderr, "DELETE query failed. Error: %s\n", + mysql_error(conn)); + cJSON_Delete(p); + mysql_close(conn); + exit(1); + } + } else { + fprintf(stderr, "Invalid operation code\n"); + cJSON_Delete(p); + mysql_close(conn); + exit(1); + } + + cJSON_Delete(p); + + printf("Content-Type: application/json;charset=utf-8\r\n"); + printf("\r\n"); + printf("{\"status\": \"success\"}"); + + mysql_close(conn); + + return 0; +}