From e02c12f582286335701d4fa07a25026b4de06cc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=98=95?= Date: Thu, 18 May 2023 09:13:20 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20IPTV=20=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=98=AF=E5=90=A6=E4=B8=8A=E6=8A=A5=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/service/dhcpd/db_interface.c | 133 +++++++++++++++--------------- 1 file changed, 67 insertions(+), 66 deletions(-) diff --git a/srcs/service/dhcpd/db_interface.c b/srcs/service/dhcpd/db_interface.c index fd976c4..1a5ade6 100644 --- a/srcs/service/dhcpd/db_interface.c +++ b/srcs/service/dhcpd/db_interface.c @@ -56,11 +56,12 @@ " ( id INTEGER PRIMARY KEY AUTOINCREMENT," \ " uid INTEGER NOT NULL," \ " mac CHAR(20) NOT NULL," \ + " report INTEGER NOT NULL," \ " createTm TIMESTAMP DEFAULT (datetime('now', 'localtime')) NOT NULL" \ "); CREATE INDEX IF NOT EXISTS iptv_index ON iptv(uid);" #define INSERT_IPTV_DEVICE \ - "INSERT INTO iptv (uid, mac) SELECT %d, '%s' WHERE NOT EXISTS (SELECT id FROM iptv WHERE uid = %d AND mac = " \ + "INSERT INTO iptv (uid, mac, report) SELECT %d, '%s', 0 WHERE NOT EXISTS (SELECT id FROM iptv WHERE uid = %d AND mac = " \ "'%s');" #define INSERT_PRE_ASSIGN_ROW_FMT \ @@ -125,7 +126,7 @@ #define LEASE_RELEASE_FMT "DELETE FROM lease WHERE uid = %d AND mac = '%s' AND ip = '%s'" int db_iptv_add_device(U32 uid, const char *pMac) { - int rc; + int rc; char buf[2048] = {0}; //INSERT INTO iptv (uid, mac) SELECT 0, '58:B4:2D:DA:D4:25' WHERE NOT EXISTS (SELECT id FROM iptv WHERE uid = 0 AND mac = '58:B4:2D:DA:D4:26') @@ -140,18 +141,18 @@ int db_iptv_add_device(U32 uid, const char *pMac) { return ERR_SUCCESS; } -static int lease_add(PDHCP_REQ pReq, +static int lease_add(PDHCP_REQ pReq, const char *ip, const char *netmask, const char *gw, const char *dns1, const char *dns2, - U32 leaseTime) { - int rc; - char buf[2048] = {0}; - char macStr[20] = {0}; + U32 leaseTime) { + int rc; + char buf[2048] = {0}; + char macStr[20] = {0}; char **dbResult; - int nRow = 0, nColumn = 0; + int nRow = 0, nColumn = 0; MAC_TO_STR(pReq->cliMac, macStr); snprintf(buf, 2048, LEASE_FIND_ITEM_FMT, macStr, pReq->hostName, pReq->uid, ip); @@ -189,24 +190,24 @@ static int lease_add(PDHCP_REQ pReq, int db_add_lease(PDHCP_REQ pReq, PPOOL_CTX pCtx) { const char *pNetmask = u32_to_str_ip_safe(htonl(pCtx->netMask)); const char *pGateway = u32_to_str_ip_safe(htonl(pCtx->gwAddr)); - const char *pDns1 = u32_to_str_ip_safe(htonl(pCtx->primeDNS)); - const char *pDns2 = u32_to_str_ip_safe(htonl(pCtx->salveDNS)); - const char *pIp = u32_to_str_ip_safe(htonl(pReq->cliAddr)); + const char *pDns1 = u32_to_str_ip_safe(htonl(pCtx->primeDNS)); + const char *pDns2 = u32_to_str_ip_safe(htonl(pCtx->salveDNS)); + const char *pIp = u32_to_str_ip_safe(htonl(pReq->cliAddr)); // 添加租约信息到lease数据库 lease_add(pReq, pIp, pNetmask, pGateway, pDns1, pDns2, pCtx->leaseTime); - free((void *)pNetmask); - free((void *)pGateway); - free((void *)pDns1); - free((void *)pDns2); - free((void *)pIp); + free((void *) pNetmask); + free((void *) pGateway); + free((void *) pDns1); + free((void *) pDns2); + free((void *) pIp); return ERR_SUCCESS; } int db_release_lease(PDHCP_REQ pReq) { - int rc; - char buf[1024] = {0}; + int rc; + char buf[1024] = {0}; char macStr[20] = {0}; MAC_TO_STR(pReq->cliMac, macStr); snprintf(buf, 1024, LEASE_RELEASE_FMT, pReq->uid, macStr, u32_to_str_ip(htonl(pReq->reqIpAddr))); @@ -220,10 +221,10 @@ int db_release_lease(PDHCP_REQ pReq) { } int db_lock_pre_assign_ip() { - int rc; - char buf[1024] = {0}; + int rc; + char buf[1024] = {0}; char **dbResult; - int nRow = 0, nColumn = 0; + int nRow = 0, nColumn = 0; snprintf(buf, 1024, GET_PRE_ASSIGN_ROW_FMT, DCHP_STEP_CLEANUP_TIMEOUT); @@ -231,8 +232,8 @@ int db_lock_pre_assign_ip() { if (rc == ERR_SUCCESS && nRow > 0 && nColumn > 0) { int i; for (i = 1; i <= nRow; i++) { - U32 uid = strtoul(dbResult[i * nColumn + 1], NULL, 10); - U32 ip = ntohl(inet_addr(dbResult[i * nColumn])); + U32 uid = strtoul(dbResult[i * nColumn + 1], NULL, 10); + U32 ip = ntohl(inet_addr(dbResult[i * nColumn])); PDHCP_USER pUser = dhcp_user_create(uid); if (pUser) { @@ -250,8 +251,8 @@ int db_lock_pre_assign_ip() { if (rc == ERR_SUCCESS && nRow > 0 && nColumn > 0) { int i; for (i = 1; i <= nRow; i++) { - U32 uid = strtoul(dbResult[i * nColumn + 1], NULL, 10); - U32 ip = ntohl(inet_addr(dbResult[i * nColumn])); + U32 uid = strtoul(dbResult[i * nColumn + 1], NULL, 10); + U32 ip = ntohl(inet_addr(dbResult[i * nColumn])); PDHCP_USER pUser = dhcp_user_create(uid); if (pUser) { usr_lease_lock_ip(pUser, ip); @@ -265,7 +266,7 @@ int db_lock_pre_assign_ip() { } int db_clearup_timeout_lease() { - int rc; + int rc; char buf[1024] = {0}; snprintf(buf, 1024, CLS_TIMEOUT_PRE_ASSIGN_ROW_FMT, DCHP_STEP_CLEANUP_TIMEOUT); @@ -289,10 +290,10 @@ int db_clearup_timeout_lease() { } int db_ip_is_pre_assign(U32 uid, U32 ip) { - int rc; - char buf[1024] = {0}; + int rc; + char buf[1024] = {0}; char **dbResult; - int nRow = 0, nColumn = 0; + int nRow = 0, nColumn = 0; // 判断IP是否存曾经被预分配 snprintf(buf, 1024, IP_IS_PRE_ASSIGN, u32_to_str_ip(htonl(ip)), uid); @@ -338,19 +339,19 @@ int db_ip_is_pre_assign(U32 uid, U32 ip) { } static int db_get_table_items(PPOOL_CTX pAssign, const char *pRunSql, U64 *pDbId) { - int rc; + int rc; char **dbResult; - int nRow = 0, nColumn = 0; + int nRow = 0, nColumn = 0; rc = db_sqlite3_get_rows(pRunSql, &dbResult, &nRow, &nColumn, NULL); if (rc == ERR_SUCCESS && nRow > 0 && nColumn > 0) { char *pNetmask = dbResult[nColumn + 3]; char *pGateway = dbResult[nColumn + 4]; - char *pDns1 = dbResult[nColumn + 5]; - char *pDns2 = dbResult[nColumn + 6]; + char *pDns1 = dbResult[nColumn + 5]; + char *pDns2 = dbResult[nColumn + 6]; // minAddr 记录可分配的IP - pAssign->minAddr = ntohl(inet_addr(dbResult[nColumn + 1])); + pAssign->minAddr = ntohl(inet_addr(dbResult[nColumn + 1])); pAssign->leaseTime = strtoul(dbResult[nColumn + 2], NULL, 10); if (strlen(pNetmask) > 0) { @@ -382,9 +383,9 @@ static int db_get_table_items(PPOOL_CTX pAssign, const char *pRunSql, U64 *pDbId } int db_get_lease(PDHCP_REQ pReq, PPOOL_CTX pAssign) { - int rc; - U64 id; - char buf[2048] = {0}; + int rc; + U64 id; + char buf[2048] = {0}; char macStr[20] = {0}; MAC_TO_STR(pReq->cliMac, macStr); @@ -406,10 +407,10 @@ int db_get_lease(PDHCP_REQ pReq, PPOOL_CTX pAssign) { } int db_get_pre_lease(PDHCP_REQ pReq, PPOOL_CTX pAssign) { - int rc; - char buf[2048] = {0}; + int rc; + char buf[2048] = {0}; char macStr[20] = {0}; - U64 id; + U64 id; MAC_TO_STR(pReq->cliMac, macStr); snprintf(buf, 2048, GET_ASSIGN_IP_INFO_FMT, macStr, pReq->hostName, pReq->uid, u32_to_str_ip(pReq->serverAddr), @@ -420,9 +421,9 @@ int db_get_pre_lease(PDHCP_REQ pReq, PPOOL_CTX pAssign) { if (rc == ERR_SUCCESS) { const char *pNetmask = u32_to_str_ip_safe(htonl(pAssign->netMask)); const char *pGateway = u32_to_str_ip_safe(htonl(pAssign->gwAddr)); - const char *pDns1 = u32_to_str_ip_safe(htonl(pAssign->primeDNS)); - const char *pDns2 = u32_to_str_ip_safe(htonl(pAssign->salveDNS)); - const char *pIp = u32_to_str_ip_safe(htonl(pAssign->minAddr)); + const char *pDns1 = u32_to_str_ip_safe(htonl(pAssign->primeDNS)); + const char *pDns2 = u32_to_str_ip_safe(htonl(pAssign->salveDNS)); + const char *pIp = u32_to_str_ip_safe(htonl(pAssign->minAddr)); // 删除预分配IP信息 #if 0 @@ -433,21 +434,21 @@ int db_get_pre_lease(PDHCP_REQ pReq, PPOOL_CTX pAssign) { // 添加租约信息到lease数据库 lease_add(pReq, pIp, pNetmask, pGateway, pDns1, pDns2, pAssign->leaseTime); - free((void *)pNetmask); - free((void *)pGateway); - free((void *)pDns1); - free((void *)pDns2); - free((void *)pIp); + free((void *) pNetmask); + free((void *) pGateway); + free((void *) pDns1); + free((void *) pDns2); + free((void *) pIp); return ERR_SUCCESS; } return -ERR_ITEM_UNEXISTS; } int db_get_pre_assign(U32 uid, const char *mac, const char *hostname, U32 *preAssign) { - int rc; - char buf[1024] = {0}; + int rc; + char buf[1024] = {0}; char **dbResult; - int nRow = 0, nColumn = 0; + int nRow = 0, nColumn = 0; snprintf(buf, 1024, GET_PRE_ASSIGN_EXISTS_ROW_FMT, mac, hostname, uid); @@ -487,26 +488,26 @@ int db_get_pre_assign(U32 uid, const char *mac, const char *hostname, U32 *preAs } int db_add_pre_assign(PDHCP_REQ pReq, U32 ip, PPOOL_CTX pPool) { - int rc; - char buf[1024] = {0}; - char macStr[20] = {0}; - const char *pIp = u32_to_str_ip_safe(htonl(ip)); - const char *pMask = u32_to_str_ip_safe(htonl(pPool->netMask)); - const char *pGw = u32_to_str_ip_safe(htonl(pPool->gwAddr)); - const char *pDns1 = u32_to_str_ip_safe(htonl(pPool->primeDNS)); - const char *pDns2 = u32_to_str_ip_safe(htonl(pPool->salveDNS)); - const char *pServer = u32_to_str_ip_safe(pReq->serverAddr); + int rc; + char buf[1024] = {0}; + char macStr[20] = {0}; + const char *pIp = u32_to_str_ip_safe(htonl(ip)); + const char *pMask = u32_to_str_ip_safe(htonl(pPool->netMask)); + const char *pGw = u32_to_str_ip_safe(htonl(pPool->gwAddr)); + const char *pDns1 = u32_to_str_ip_safe(htonl(pPool->primeDNS)); + const char *pDns2 = u32_to_str_ip_safe(htonl(pPool->salveDNS)); + const char *pServer = u32_to_str_ip_safe(pReq->serverAddr); MAC_TO_STR(pReq->cliMac, macStr); snprintf(buf, 1024, INSERT_PRE_ASSIGN_ROW_FMT, pReq->uid, pReq->xid, pReq->hostName, macStr, pIp, pPool->leaseTime, pMask, pGw, pDns1, pDns2, pServer); - free((void *)pIp); - free((void *)pMask); - free((void *)pGw); - free((void *)pDns1); - free((void *)pDns2); - free((void *)pServer); + free((void *) pIp); + free((void *) pMask); + free((void *) pGw); + free((void *) pDns1); + free((void *) pDns2); + free((void *) pServer); rc = db_sqlite3_sql_exec(buf, NULL, NULL, NULL);