Merge branch 'dev' of gitee.com:iot-xa2301-fly-noob/c-router-emulator into dev

This commit is contained in:
flykhan 2023-09-21 11:27:04 +08:00
commit 7caa1be979
2 changed files with 108 additions and 25 deletions

View File

@ -6,39 +6,63 @@
<meta name="author" content="zifan"> <meta name="author" content="zifan">
<meta name="keywords" content="物联网"> <meta name="keywords" content="物联网">
<title>ARP列表</title> <title>ARP列表</title>
<style>
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
body {
display: flex;
flex-direction: column;
justify-content: space-between;
}
#arpTable {
background-color: rgba(41, 117, 117, 0.667);
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.5);
}
</style>
</head> </head>
<body> <body>
<table id="arpTable" width="700" align="center" border="1"> <div></div>
<thead> <div class="tablecenter">
<th>ID</th> <table id="arpTable" width="700" align="center" border="1" cellspacing="0">
<th>ip地址</th> <thead>
<th>mac地址</th> <th>ID</th>
</thead> <th>ip地址</th>
<tbody id="arpTableBody" align="center"> <th>mac地址</th>
</thead>
<tbody id="arpTableBody" align="center">
</tbody> </tbody>
</table> </table>
</div>
<div></div>
<div></div>
<script> <script>
window.onload = function () { window.onload = function () {
fetch("/cgi-bin/ip.cgi").then(resp => resp.json()).then(data => { fetch("/cgi-bin/arp.cgi").then(resp => resp.json()).then(data => {
if (data.code == 0) { arpTableBody.innerText = "";
arpTableBody.innerText = ""; data.forEach(rowData => {
data.data.forEach(rowData => { let tr = document.createElement("tr");
let tr = document.createElement("tr"); let td1 = document.createElement("td");
let td1 = document.createElement("td"); let td2 = document.createElement("td");
let td2 = document.createElement("td"); let td3 = document.createElement("td");
let td3 = document.createElement("td");
td1.innerText = rowData.id; td1.innerText = rowData.id;
td2.innerText = rowData.ip; td2.innerText = rowData.ip;
td3.innerText = rowData.mac; td3.innerText = rowData.mac;
tr.append(td1, td2, td3); tr.append(td1, td2, td3);
arpTableBody.append(tr); arpTableBody.append(tr);
}); });
} }
}) )
} }
</script> </script>
</body> </body>

View File

@ -0,0 +1,59 @@
#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);
}
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);
}
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;
}