OCT 1. 备份代码
This commit is contained in:
parent
e0f812fc43
commit
54865a60c6
|
@ -8,6 +8,7 @@
|
||||||
#include "user_errno.h"
|
#include "user_errno.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
static sqlite3 *g_pSqlHandle = NULL;
|
static sqlite3 *g_pSqlHandle = NULL;
|
||||||
|
|
||||||
|
@ -57,6 +58,7 @@ int db_sqlite3_sql_exec(const char *pSqlCmd, void *pCallback, void *pData, char
|
||||||
|
|
||||||
void db_sqlite3_uninit() {
|
void db_sqlite3_uninit() {
|
||||||
if (g_pSqlHandle) {
|
if (g_pSqlHandle) {
|
||||||
|
DEBUG_CODE_LINE();
|
||||||
sqlite3_close(g_pSqlHandle);
|
sqlite3_close(g_pSqlHandle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,8 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK
|
||||||
signal(SIGQUIT, catch_system_interupt);
|
signal(SIGQUIT, catch_system_interupt);
|
||||||
signal(SIGTSTP, catch_system_interupt);
|
signal(SIGTSTP, catch_system_interupt);
|
||||||
signal(SIGHUP, catch_system_interupt);
|
signal(SIGHUP, catch_system_interupt);
|
||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, catch_system_interupt);
|
||||||
|
signal(SIGKILL, catch_system_interupt);
|
||||||
|
|
||||||
// 初始化 libuv loop
|
// 初始化 libuv loop
|
||||||
get_task_manager();
|
get_task_manager();
|
||||||
|
|
|
@ -333,6 +333,7 @@ static void on_sock_recv(uv_work_t *req) {
|
||||||
memset(&reqDhcp, 0, sizeof(DHCP_REQ));
|
memset(&reqDhcp, 0, sizeof(DHCP_REQ));
|
||||||
reqDhcp.unicast = pkg->dhcp.flags != 0 ? TRUE : FALSE;
|
reqDhcp.unicast = pkg->dhcp.flags != 0 ? TRUE : FALSE;
|
||||||
reqDhcp.xid = DHCP_XID(pkg->dhcp.xid);
|
reqDhcp.xid = DHCP_XID(pkg->dhcp.xid);
|
||||||
|
reqDhcp.uid = VLAN_VNI_ID(pkg->vlan_hdr.vlan.id);
|
||||||
memcpy(reqDhcp.cliMac, pkg->dhcp.chaddr, ETH_ALEN);
|
memcpy(reqDhcp.cliMac, pkg->dhcp.chaddr, ETH_ALEN);
|
||||||
|
|
||||||
switch (*optMsg.pValue) {
|
switch (*optMsg.pValue) {
|
||||||
|
@ -363,7 +364,7 @@ static void on_sock_recv(uv_work_t *req) {
|
||||||
}
|
}
|
||||||
|
|
||||||
MAC_TO_STR(reqDhcp.cliMac, macStr);
|
MAC_TO_STR(reqDhcp.cliMac, macStr);
|
||||||
ret = pre_alloc_dhcp_res(VLAN_VNI_ID(pkg->vlan_hdr.vlan.id), macStr, &ip, &pIpInfo);
|
ret = pre_alloc_dhcp_res(&reqDhcp, &ip, &pIpInfo);
|
||||||
|
|
||||||
if (ret == ERR_SUCCESS) {
|
if (ret == ERR_SUCCESS) {
|
||||||
LOG_MOD(trace,
|
LOG_MOD(trace,
|
||||||
|
@ -506,7 +507,7 @@ int create_udp_raw_socket(const char *pNicName) {
|
||||||
// 1. create socket
|
// 1. create socket
|
||||||
int sock_fd = socket(PF_PACKET, SOCK_RAW, 0);
|
int sock_fd = socket(PF_PACKET, SOCK_RAW, 0);
|
||||||
if (sock_fd < 0) {
|
if (sock_fd < 0) {
|
||||||
LOG_MOD(error, ZLOG_MOD_DHCPD, "Socket created failure\n");
|
LOG_MOD(error, ZLOG_MOD_DHCPD, "Socket created failure: %s --> %s\n", pNicName, strerror(errno));
|
||||||
return -ERR_SOCK_CREATE;
|
return -ERR_SOCK_CREATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,7 @@ typedef struct {
|
||||||
UT_hash_handle hh;
|
UT_hash_handle hh;
|
||||||
} IPPOOL_INFO, *PIPPOOL_INFO;
|
} IPPOOL_INFO, *PIPPOOL_INFO;
|
||||||
|
|
||||||
|
|
||||||
void init_default_pool();
|
void init_default_pool();
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#include <linux/if_ether.h>
|
#include <linux/if_ether.h>
|
||||||
#include <common.h>
|
#include <common.h>
|
||||||
#include "ip_pool.h"
|
#include "ip_pool.h"
|
||||||
|
#include "rfc2131.h"
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
@ -26,7 +27,8 @@ typedef struct {
|
||||||
} LEASE_CACHE, *PLEASE_CACHE;
|
} LEASE_CACHE, *PLEASE_CACHE;
|
||||||
|
|
||||||
int dhcp_lease_init();
|
int dhcp_lease_init();
|
||||||
int pre_alloc_dhcp_res(U32 uid, const char *pMac, U32 *pOutIp, PIPPOOL_INFO *pOutPool);
|
//int pre_alloc_dhcp_res(U32 uid, const char *pMac, U32 *pOutIp, PIPPOOL_INFO *pOutPool);
|
||||||
|
int pre_alloc_dhcp_res(PDHCP_REQ pReq, U32 *pOutIp, PIPPOOL_INFO *pOutPool);
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -21,6 +21,7 @@ extern "C" {
|
||||||
typedef struct {
|
typedef struct {
|
||||||
U8 unicast;
|
U8 unicast;
|
||||||
U8 cliMac[ETH_ALEN];
|
U8 cliMac[ETH_ALEN];
|
||||||
|
U32 uid;
|
||||||
U32 xid;
|
U32 xid;
|
||||||
U32 reqIpAddr;
|
U32 reqIpAddr;
|
||||||
U32 leaseTime;
|
U32 leaseTime;
|
||||||
|
|
|
@ -8,13 +8,15 @@
|
||||||
#include "zlog_module.h"
|
#include "zlog_module.h"
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
#include "user_mgr.h"
|
#include "user_mgr.h"
|
||||||
|
#include "rfc2131.h"
|
||||||
|
#include "dhcp_network.h"
|
||||||
|
|
||||||
#define LEASE_DB_NAME ("lease")
|
#define LEASE_DB_NAME "lease"
|
||||||
#define PREALLOC_IP_TIMEOUT (60)
|
#define PREALLOC_IP_TIMEOUT (60)
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
U32 ipAddr;
|
U32 ipAddr;
|
||||||
char macAddr[20];
|
U8 macAddr[ETH_ALEN];
|
||||||
U32 bitset;
|
U32 bitset;
|
||||||
PIPPOOL_INFO pCtx;
|
PIPPOOL_INFO pCtx;
|
||||||
U32 timeStamp;
|
U32 timeStamp;
|
||||||
|
@ -25,8 +27,8 @@ typedef struct {
|
||||||
//static PMAC_FILTER g_blackListTbl = NULL;
|
//static PMAC_FILTER g_blackListTbl = NULL;
|
||||||
static PPRE_ALLOC_IP g_pPreAllocIp = NULL;
|
static PPRE_ALLOC_IP g_pPreAllocIp = NULL;
|
||||||
|
|
||||||
#define CREATE_LEASE_TABLE(name) \
|
#define CREATE_LEASE_TABLE() \
|
||||||
"CREATE TABLE IF NOT EXISTS " #name \
|
"CREATE TABLE IF NOT EXISTS lease " \
|
||||||
" ( 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," \
|
||||||
|
@ -41,19 +43,71 @@ static PPRE_ALLOC_IP g_pPreAllocIp = NULL;
|
||||||
" hostname CHAR(64) DEFAULT '' NOT NULL," \
|
" hostname CHAR(64) DEFAULT '' NOT NULL," \
|
||||||
" keyType INTEGER NOT NULL" \
|
" keyType INTEGER NOT NULL" \
|
||||||
");" \
|
");" \
|
||||||
"CREATE INDEX IF NOT EXISTS " #name "_index ON " #name " (uid, mac);"
|
"CREATE INDEX IF NOT EXISTS " \
|
||||||
|
"lease_index ON lease (uid, mac);"
|
||||||
|
|
||||||
int pre_alloc_dhcp_res(U32 uid, const char *pMac, U32 *pOutIp, PIPPOOL_INFO *pOutPool) {
|
#define CREATE_PRE_ASSIGN_TABLE() \
|
||||||
|
"CREATE TABLE IF NOT EXISTS pre_assign" \
|
||||||
|
" ( id INTEGER PRIMARY KEY AUTOINCREMENT," \
|
||||||
|
" uid INTEGER NOT NULL," \
|
||||||
|
" xid INTEGER NOT NULL," \
|
||||||
|
" hostname CHAR(64) DEFAULT '' NOT NULL," \
|
||||||
|
" mac CHAR(20) NOT NULL," \
|
||||||
|
" ip INTEGER NOT NULL," \
|
||||||
|
" lease INTEGER NOT NULL," \
|
||||||
|
" netmask INTEGER," \
|
||||||
|
" gateway INTEGER," \
|
||||||
|
" dns1 INTEGER," \
|
||||||
|
" dns2 INTEGER," \
|
||||||
|
" createTm TIMESTAMP DEFAULT (datetime('now', 'localtime')) NOT NULL" \
|
||||||
|
"); CREATE INDEX IF NOT EXISTS pre_assign_index ON pre_assign(ip, uid);"
|
||||||
|
|
||||||
|
#define INSERT_PRE_ASSIGN_ROW_FMT \
|
||||||
|
"INSERT INTO pre_assign (uid, xid, hostname, mac, ip, lease, netmask, gateway, dns1, dns2) " \
|
||||||
|
"VALUES (%d, %d, \'%s\', \'%s\', %d, %d, %d, %d, %d, %d);"
|
||||||
|
|
||||||
|
static int lease_db_add_pre_assign(PDHCP_REQ pReq, U32 ip, PIPPOOL_INFO pPool) {
|
||||||
|
int rc;
|
||||||
|
char buf[1024] = {0};
|
||||||
|
char macStr[20] = {0};
|
||||||
|
|
||||||
|
MAC_TO_STR(pReq->cliMac, macStr);
|
||||||
|
snprintf(buf,
|
||||||
|
1024,
|
||||||
|
INSERT_PRE_ASSIGN_ROW_FMT,
|
||||||
|
pReq->uid,
|
||||||
|
pReq->xid,
|
||||||
|
pReq->hostName,
|
||||||
|
macStr,
|
||||||
|
ip,
|
||||||
|
pPool->leaseTime,
|
||||||
|
pPool->netMask,
|
||||||
|
pPool->gwAddr,
|
||||||
|
pPool->primeDNS,
|
||||||
|
pPool->salveDNS);
|
||||||
|
|
||||||
|
rc = db_sqlite3_sql_exec(CREATE_PRE_ASSIGN_TABLE(), NULL, NULL, NULL);
|
||||||
|
|
||||||
|
if (rc != ERR_SUCCESS) {
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG_CODE_LINE();
|
||||||
|
return ERR_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
//int pre_alloc_dhcp_res(U32 uid, const char *pMac, U32 *pOutIp, PIPPOOL_INFO *pOutPool) {
|
||||||
|
int pre_alloc_dhcp_res(PDHCP_REQ pReq, U32 *pOutIp, PIPPOOL_INFO *pOutPool) {
|
||||||
PIPPOOL_INFO pPool, pTemp;
|
PIPPOOL_INFO pPool, pTemp;
|
||||||
PPRE_ALLOC_IP pNewIp, pTmp, pCache = NULL;
|
PPRE_ALLOC_IP pNewIp, pTmp, pCache = NULL;
|
||||||
PIPPOOL_INFO pUserPool;
|
PIPPOOL_INFO pUserPool;
|
||||||
|
|
||||||
if (pOutIp == NULL || pOutPool == NULL) {
|
if (pReq == NULL || pOutIp == NULL || pOutPool == NULL) {
|
||||||
LOG_MOD(error, ZLOG_MOD_DHCPD, "Input params error: %p, %p\n", pOutIp, pOutPool);
|
LOG_MOD(error, ZLOG_MOD_DHCPD, "Input params error: %p, %p\n", pOutIp, pOutPool);
|
||||||
return -ERR_INPUT_PARAMS;
|
return -ERR_INPUT_PARAMS;
|
||||||
}
|
}
|
||||||
|
|
||||||
pUserPool = user_get_pool(uid);
|
pUserPool = user_get_pool(pReq->uid);
|
||||||
if (pUserPool == NULL) {
|
if (pUserPool == NULL) {
|
||||||
LOG_MOD(error, ZLOG_MOD_DHCPD, "Can't found avaliable address pool\n");
|
LOG_MOD(error, ZLOG_MOD_DHCPD, "Can't found avaliable address pool\n");
|
||||||
return -ERR_DHCP_NO_POOL;
|
return -ERR_DHCP_NO_POOL;
|
||||||
|
@ -64,6 +118,7 @@ int pre_alloc_dhcp_res(U32 uid, const char *pMac, U32 *pOutIp, PIPPOOL_INFO *pOu
|
||||||
U32 addr;
|
U32 addr;
|
||||||
|
|
||||||
// 查看是否预分配过该设备
|
// 查看是否预分配过该设备
|
||||||
|
#if 0
|
||||||
if (pMac && strlen(pMac) > 0) {
|
if (pMac && strlen(pMac) > 0) {
|
||||||
HASH_ITER(hh, g_pPreAllocIp, pNewIp, pTmp) {
|
HASH_ITER(hh, g_pPreAllocIp, pNewIp, pTmp) {
|
||||||
if (strcmp(pNewIp->macAddr, pMac) == 0) {
|
if (strcmp(pNewIp->macAddr, pMac) == 0) {
|
||||||
|
@ -74,7 +129,7 @@ int pre_alloc_dhcp_res(U32 uid, const char *pMac, U32 *pOutIp, PIPPOOL_INFO *pOu
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
while ((addr = bitset_minimum(pPool->assignPool)) > 0) {
|
while ((addr = bitset_minimum(pPool->assignPool)) > 0) {
|
||||||
U32 ipAddr = (pPool->minAddr & pPool->netMask) + addr;
|
U32 ipAddr = (pPool->minAddr & pPool->netMask) + addr;
|
||||||
|
|
||||||
|
@ -102,11 +157,11 @@ int pre_alloc_dhcp_res(U32 uid, const char *pMac, U32 *pOutIp, PIPPOOL_INFO *pOu
|
||||||
pNewIp->pCtx = pPool;
|
pNewIp->pCtx = pPool;
|
||||||
pNewIp->ipAddr = ipAddr;
|
pNewIp->ipAddr = ipAddr;
|
||||||
pNewIp->bitset = addr;
|
pNewIp->bitset = addr;
|
||||||
if (pMac) {
|
memcpy(pNewIp->macAddr, pReq->cliMac, ETH_ALEN);
|
||||||
strcpy(pNewIp->macAddr, pMac);
|
|
||||||
}
|
|
||||||
|
|
||||||
HASH_ADD_INT(g_pPreAllocIp, ipAddr, pNewIp);
|
//HASH_ADD_INT(g_pPreAllocIp, ipAddr, pNewIp);
|
||||||
|
DEBUG_CODE_LINE();
|
||||||
|
lease_db_add_pre_assign(pReq, ipAddr, pPool);
|
||||||
|
|
||||||
*pOutIp = ipAddr;
|
*pOutIp = ipAddr;
|
||||||
*pOutPool = pPool;
|
*pOutPool = pPool;
|
||||||
|
@ -116,6 +171,7 @@ int pre_alloc_dhcp_res(U32 uid, const char *pMac, U32 *pOutIp, PIPPOOL_INFO *pOu
|
||||||
LOG_MOD(trace, ZLOG_MOD_DHCPD, "Select ipaddr %08X at %d of %p\n", ipAddr, addr, pPool->assignPool);
|
LOG_MOD(trace, ZLOG_MOD_DHCPD, "Select ipaddr %08X at %d of %p\n", ipAddr, addr, pPool->assignPool);
|
||||||
return ERR_SUCCESS;
|
return ERR_SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
|
DEBUG_CODE_LINE();
|
||||||
if (time(NULL) - pNewIp->timeStamp < PREALLOC_IP_TIMEOUT) {
|
if (time(NULL) - pNewIp->timeStamp < PREALLOC_IP_TIMEOUT) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -155,14 +211,24 @@ int pre_alloc_dhcp_res(U32 uid, const char *pMac, U32 *pOutIp, PIPPOOL_INFO *pOu
|
||||||
}
|
}
|
||||||
|
|
||||||
// 没有可预分配的IP,报错
|
// 没有可预分配的IP,报错
|
||||||
LOG_MOD(error, ZLOG_MOD_DHCPD, "No free ipaddress in poll: uid = %u, pool = 0x%08X\n", uid, pUserPool->poolKey);
|
LOG_MOD(error,
|
||||||
|
ZLOG_MOD_DHCPD,
|
||||||
|
"No free ipaddress in poll: uid = %u, pool = 0x%08X\n",
|
||||||
|
pReq->uid,
|
||||||
|
pUserPool->poolKey);
|
||||||
return -ERR_DHCP_NO_ADDR;
|
return -ERR_DHCP_NO_ADDR;
|
||||||
}
|
}
|
||||||
|
|
||||||
int dhcp_lease_init() {
|
int dhcp_lease_init() {
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
rc = db_sqlite3_sql_exec(CREATE_LEASE_TABLE(LEASE_DB_NAME), NULL, NULL, NULL);
|
rc = db_sqlite3_sql_exec(CREATE_LEASE_TABLE(), NULL, NULL, NULL);
|
||||||
|
|
||||||
|
if (rc != ERR_SUCCESS) {
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = db_sqlite3_sql_exec(CREATE_PRE_ASSIGN_TABLE(), NULL, NULL, NULL);
|
||||||
|
|
||||||
if (rc != ERR_SUCCESS) {
|
if (rc != ERR_SUCCESS) {
|
||||||
return rc;
|
return rc;
|
||||||
|
|
Loading…
Reference in New Issue