OCT 1. IPTV 数据库增加是否上报服务器字段

This commit is contained in:
黄昕 2023-05-18 09:13:20 +08:00
parent b618761330
commit e02c12f582
1 changed files with 67 additions and 66 deletions

View File

@ -56,11 +56,12 @@
" ( id INTEGER PRIMARY KEY AUTOINCREMENT," \ " ( id INTEGER PRIMARY KEY AUTOINCREMENT," \
" uid INTEGER NOT NULL," \ " uid INTEGER NOT NULL," \
" mac CHAR(20) NOT NULL," \ " mac CHAR(20) NOT NULL," \
" report INTEGER NOT NULL," \
" createTm TIMESTAMP DEFAULT (datetime('now', 'localtime')) NOT NULL" \ " createTm TIMESTAMP DEFAULT (datetime('now', 'localtime')) NOT NULL" \
"); CREATE INDEX IF NOT EXISTS iptv_index ON iptv(uid);" "); CREATE INDEX IF NOT EXISTS iptv_index ON iptv(uid);"
#define INSERT_IPTV_DEVICE \ #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');" "'%s');"
#define INSERT_PRE_ASSIGN_ROW_FMT \ #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'" #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 db_iptv_add_device(U32 uid, const char *pMac) {
int rc; int rc;
char buf[2048] = {0}; 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') //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; return ERR_SUCCESS;
} }
static int lease_add(PDHCP_REQ pReq, static int lease_add(PDHCP_REQ pReq,
const char *ip, const char *ip,
const char *netmask, const char *netmask,
const char *gw, const char *gw,
const char *dns1, const char *dns1,
const char *dns2, const char *dns2,
U32 leaseTime) { U32 leaseTime) {
int rc; int rc;
char buf[2048] = {0}; char buf[2048] = {0};
char macStr[20] = {0}; char macStr[20] = {0};
char **dbResult; char **dbResult;
int nRow = 0, nColumn = 0; int nRow = 0, nColumn = 0;
MAC_TO_STR(pReq->cliMac, macStr); MAC_TO_STR(pReq->cliMac, macStr);
snprintf(buf, 2048, LEASE_FIND_ITEM_FMT, macStr, pReq->hostName, pReq->uid, ip); 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) { int db_add_lease(PDHCP_REQ pReq, PPOOL_CTX pCtx) {
const char *pNetmask = u32_to_str_ip_safe(htonl(pCtx->netMask)); const char *pNetmask = u32_to_str_ip_safe(htonl(pCtx->netMask));
const char *pGateway = u32_to_str_ip_safe(htonl(pCtx->gwAddr)); const char *pGateway = u32_to_str_ip_safe(htonl(pCtx->gwAddr));
const char *pDns1 = u32_to_str_ip_safe(htonl(pCtx->primeDNS)); const char *pDns1 = u32_to_str_ip_safe(htonl(pCtx->primeDNS));
const char *pDns2 = u32_to_str_ip_safe(htonl(pCtx->salveDNS)); const char *pDns2 = u32_to_str_ip_safe(htonl(pCtx->salveDNS));
const char *pIp = u32_to_str_ip_safe(htonl(pReq->cliAddr)); const char *pIp = u32_to_str_ip_safe(htonl(pReq->cliAddr));
// 添加租约信息到lease数据库 // 添加租约信息到lease数据库
lease_add(pReq, pIp, pNetmask, pGateway, pDns1, pDns2, pCtx->leaseTime); lease_add(pReq, pIp, pNetmask, pGateway, pDns1, pDns2, pCtx->leaseTime);
free((void *)pNetmask); free((void *) pNetmask);
free((void *)pGateway); free((void *) pGateway);
free((void *)pDns1); free((void *) pDns1);
free((void *)pDns2); free((void *) pDns2);
free((void *)pIp); free((void *) pIp);
return ERR_SUCCESS; return ERR_SUCCESS;
} }
int db_release_lease(PDHCP_REQ pReq) { int db_release_lease(PDHCP_REQ pReq) {
int rc; int rc;
char buf[1024] = {0}; char buf[1024] = {0};
char macStr[20] = {0}; char macStr[20] = {0};
MAC_TO_STR(pReq->cliMac, macStr); MAC_TO_STR(pReq->cliMac, macStr);
snprintf(buf, 1024, LEASE_RELEASE_FMT, pReq->uid, macStr, u32_to_str_ip(htonl(pReq->reqIpAddr))); 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 db_lock_pre_assign_ip() {
int rc; int rc;
char buf[1024] = {0}; char buf[1024] = {0};
char **dbResult; char **dbResult;
int nRow = 0, nColumn = 0; int nRow = 0, nColumn = 0;
snprintf(buf, 1024, GET_PRE_ASSIGN_ROW_FMT, DCHP_STEP_CLEANUP_TIMEOUT); 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) { if (rc == ERR_SUCCESS && nRow > 0 && nColumn > 0) {
int i; int i;
for (i = 1; i <= nRow; i++) { for (i = 1; i <= nRow; i++) {
U32 uid = strtoul(dbResult[i * nColumn + 1], NULL, 10); U32 uid = strtoul(dbResult[i * nColumn + 1], NULL, 10);
U32 ip = ntohl(inet_addr(dbResult[i * nColumn])); U32 ip = ntohl(inet_addr(dbResult[i * nColumn]));
PDHCP_USER pUser = dhcp_user_create(uid); PDHCP_USER pUser = dhcp_user_create(uid);
if (pUser) { if (pUser) {
@ -250,8 +251,8 @@ int db_lock_pre_assign_ip() {
if (rc == ERR_SUCCESS && nRow > 0 && nColumn > 0) { if (rc == ERR_SUCCESS && nRow > 0 && nColumn > 0) {
int i; int i;
for (i = 1; i <= nRow; i++) { for (i = 1; i <= nRow; i++) {
U32 uid = strtoul(dbResult[i * nColumn + 1], NULL, 10); U32 uid = strtoul(dbResult[i * nColumn + 1], NULL, 10);
U32 ip = ntohl(inet_addr(dbResult[i * nColumn])); U32 ip = ntohl(inet_addr(dbResult[i * nColumn]));
PDHCP_USER pUser = dhcp_user_create(uid); PDHCP_USER pUser = dhcp_user_create(uid);
if (pUser) { if (pUser) {
usr_lease_lock_ip(pUser, ip); usr_lease_lock_ip(pUser, ip);
@ -265,7 +266,7 @@ int db_lock_pre_assign_ip() {
} }
int db_clearup_timeout_lease() { int db_clearup_timeout_lease() {
int rc; int rc;
char buf[1024] = {0}; char buf[1024] = {0};
snprintf(buf, 1024, CLS_TIMEOUT_PRE_ASSIGN_ROW_FMT, DCHP_STEP_CLEANUP_TIMEOUT); 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 db_ip_is_pre_assign(U32 uid, U32 ip) {
int rc; int rc;
char buf[1024] = {0}; char buf[1024] = {0};
char **dbResult; char **dbResult;
int nRow = 0, nColumn = 0; int nRow = 0, nColumn = 0;
// 判断IP是否存曾经被预分配 // 判断IP是否存曾经被预分配
snprintf(buf, 1024, IP_IS_PRE_ASSIGN, u32_to_str_ip(htonl(ip)), uid); 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) { static int db_get_table_items(PPOOL_CTX pAssign, const char *pRunSql, U64 *pDbId) {
int rc; int rc;
char **dbResult; char **dbResult;
int nRow = 0, nColumn = 0; int nRow = 0, nColumn = 0;
rc = db_sqlite3_get_rows(pRunSql, &dbResult, &nRow, &nColumn, NULL); rc = db_sqlite3_get_rows(pRunSql, &dbResult, &nRow, &nColumn, NULL);
if (rc == ERR_SUCCESS && nRow > 0 && nColumn > 0) { if (rc == ERR_SUCCESS && nRow > 0 && nColumn > 0) {
char *pNetmask = dbResult[nColumn + 3]; char *pNetmask = dbResult[nColumn + 3];
char *pGateway = dbResult[nColumn + 4]; char *pGateway = dbResult[nColumn + 4];
char *pDns1 = dbResult[nColumn + 5]; char *pDns1 = dbResult[nColumn + 5];
char *pDns2 = dbResult[nColumn + 6]; char *pDns2 = dbResult[nColumn + 6];
// minAddr 记录可分配的IP // 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); pAssign->leaseTime = strtoul(dbResult[nColumn + 2], NULL, 10);
if (strlen(pNetmask) > 0) { 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 db_get_lease(PDHCP_REQ pReq, PPOOL_CTX pAssign) {
int rc; int rc;
U64 id; U64 id;
char buf[2048] = {0}; char buf[2048] = {0};
char macStr[20] = {0}; char macStr[20] = {0};
MAC_TO_STR(pReq->cliMac, macStr); 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 db_get_pre_lease(PDHCP_REQ pReq, PPOOL_CTX pAssign) {
int rc; int rc;
char buf[2048] = {0}; char buf[2048] = {0};
char macStr[20] = {0}; char macStr[20] = {0};
U64 id; U64 id;
MAC_TO_STR(pReq->cliMac, macStr); 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), 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) { if (rc == ERR_SUCCESS) {
const char *pNetmask = u32_to_str_ip_safe(htonl(pAssign->netMask)); const char *pNetmask = u32_to_str_ip_safe(htonl(pAssign->netMask));
const char *pGateway = u32_to_str_ip_safe(htonl(pAssign->gwAddr)); const char *pGateway = u32_to_str_ip_safe(htonl(pAssign->gwAddr));
const char *pDns1 = u32_to_str_ip_safe(htonl(pAssign->primeDNS)); const char *pDns1 = u32_to_str_ip_safe(htonl(pAssign->primeDNS));
const char *pDns2 = u32_to_str_ip_safe(htonl(pAssign->salveDNS)); const char *pDns2 = u32_to_str_ip_safe(htonl(pAssign->salveDNS));
const char *pIp = u32_to_str_ip_safe(htonl(pAssign->minAddr)); const char *pIp = u32_to_str_ip_safe(htonl(pAssign->minAddr));
// 删除预分配IP信息 // 删除预分配IP信息
#if 0 #if 0
@ -433,21 +434,21 @@ int db_get_pre_lease(PDHCP_REQ pReq, PPOOL_CTX pAssign) {
// 添加租约信息到lease数据库 // 添加租约信息到lease数据库
lease_add(pReq, pIp, pNetmask, pGateway, pDns1, pDns2, pAssign->leaseTime); lease_add(pReq, pIp, pNetmask, pGateway, pDns1, pDns2, pAssign->leaseTime);
free((void *)pNetmask); free((void *) pNetmask);
free((void *)pGateway); free((void *) pGateway);
free((void *)pDns1); free((void *) pDns1);
free((void *)pDns2); free((void *) pDns2);
free((void *)pIp); free((void *) pIp);
return ERR_SUCCESS; return ERR_SUCCESS;
} }
return -ERR_ITEM_UNEXISTS; return -ERR_ITEM_UNEXISTS;
} }
int db_get_pre_assign(U32 uid, const char *mac, const char *hostname, U32 *preAssign) { int db_get_pre_assign(U32 uid, const char *mac, const char *hostname, U32 *preAssign) {
int rc; int rc;
char buf[1024] = {0}; char buf[1024] = {0};
char **dbResult; char **dbResult;
int nRow = 0, nColumn = 0; int nRow = 0, nColumn = 0;
snprintf(buf, 1024, GET_PRE_ASSIGN_EXISTS_ROW_FMT, mac, hostname, uid); 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 db_add_pre_assign(PDHCP_REQ pReq, U32 ip, PPOOL_CTX pPool) {
int rc; int rc;
char buf[1024] = {0}; char buf[1024] = {0};
char macStr[20] = {0}; char macStr[20] = {0};
const char *pIp = u32_to_str_ip_safe(htonl(ip)); const char *pIp = u32_to_str_ip_safe(htonl(ip));
const char *pMask = u32_to_str_ip_safe(htonl(pPool->netMask)); const char *pMask = u32_to_str_ip_safe(htonl(pPool->netMask));
const char *pGw = u32_to_str_ip_safe(htonl(pPool->gwAddr)); const char *pGw = u32_to_str_ip_safe(htonl(pPool->gwAddr));
const char *pDns1 = u32_to_str_ip_safe(htonl(pPool->primeDNS)); const char *pDns1 = u32_to_str_ip_safe(htonl(pPool->primeDNS));
const char *pDns2 = u32_to_str_ip_safe(htonl(pPool->salveDNS)); const char *pDns2 = u32_to_str_ip_safe(htonl(pPool->salveDNS));
const char *pServer = u32_to_str_ip_safe(pReq->serverAddr); const char *pServer = u32_to_str_ip_safe(pReq->serverAddr);
MAC_TO_STR(pReq->cliMac, macStr); MAC_TO_STR(pReq->cliMac, macStr);
snprintf(buf, 1024, INSERT_PRE_ASSIGN_ROW_FMT, pReq->uid, pReq->xid, pReq->hostName, macStr, pIp, pPool->leaseTime, snprintf(buf, 1024, INSERT_PRE_ASSIGN_ROW_FMT, pReq->uid, pReq->xid, pReq->hostName, macStr, pIp, pPool->leaseTime,
pMask, pGw, pDns1, pDns2, pServer); pMask, pGw, pDns1, pDns2, pServer);
free((void *)pIp); free((void *) pIp);
free((void *)pMask); free((void *) pMask);
free((void *)pGw); free((void *) pGw);
free((void *)pDns1); free((void *) pDns1);
free((void *)pDns2); free((void *) pDns2);
free((void *)pServer); free((void *) pServer);
rc = db_sqlite3_sql_exec(buf, NULL, NULL, NULL); rc = db_sqlite3_sql_exec(buf, NULL, NULL, NULL);