diff --git a/srcs/include/pppoe_info.h b/srcs/include/pppoe_info.h index 19963b6..340b378 100644 --- a/srcs/include/pppoe_info.h +++ b/srcs/include/pppoe_info.h @@ -10,7 +10,9 @@ extern "C" { #include #include "netif/ppp/ppp.h" -#define PPPOE_MAX_TIMEOUT (30) +#define PPPOE_MAX_TIMEOUT (30) +#define MAX_IP_V4_STR (16) +#define MAX_MAC_ADDR_STR (18) typedef enum { STATUS_TASK_INIT, @@ -30,36 +32,64 @@ typedef enum { } USER_STATUS; typedef struct { - unsigned int timeout; - unsigned int count; + unsigned int timeout; + unsigned int count; } RETRY_WORK; +typedef struct { + unsigned short sessionId; + char clientIp[MAX_IP_V4_STR]; + char clientGw[MAX_IP_V4_STR]; + char clientMask[MAX_IP_V4_STR]; + char clientMac[MAX_MAC_ADDR_STR]; + char localMac[MAX_MAC_ADDR_STR]; +} PPPOE_SESSION_DATA, *PPPPOE_SESSION_DATA; + typedef struct { unsigned int userid; struct netif *nicif; struct netif *pppif; ppp_pcb *ppp; - RETRY_WORK retry; + RETRY_WORK retry; - PPPOE_TASK status; + PPPOE_SESSION_DATA data; + + PPPOE_TASK status; } PPPOE_SESSION, *PPPPOE_SESSION; typedef struct { - unsigned short qinq_tag1; - unsigned short qinq_tag2; + unsigned int userid; + unsigned int vni; + unsigned short q1; + unsigned short q2; unsigned char mac_addr[6]; const char *pppoe_user; const char *pppoe_passwd; - USER_STATUS user_status; +} USER_PARAMS, *PUSER_PARAMS; + +typedef struct { + unsigned int vni; + unsigned short q1; + unsigned short q2; +} VXLAN_TAG, *PVXLAN_TAG; + +typedef struct { + unsigned char mac_addr[6]; + const char *pppoe_user; + const char *pppoe_passwd; } USER_INFO, *PUSER_INFO; typedef struct { unsigned int userid; + VXLAN_TAG vxlan; USER_INFO user_info; PPPOE_SESSION session; - UT_hash_handle hh; + USER_STATUS user_status; + + UT_hash_handle hh_id; + UT_hash_handle hh_vxlan; } USER_INFO_CONTEXT, *PUSER_INFO_CONTEXT; int pppoe_session_create(PUSER_INFO_CONTEXT pUser); diff --git a/srcs/include/user_info.h b/srcs/include/user_info.h index e16b51c..bdd35d5 100644 --- a/srcs/include/user_info.h +++ b/srcs/include/user_info.h @@ -11,11 +11,11 @@ extern "C" { #include "pppoe_info.h" void user_info_init(); -int user_info_add(unsigned int userid, PUSER_INFO pInfo); +int user_info_add(unsigned int userid, PUSER_PARAMS pInfo); void user_info_remove(unsigned int userid); void user_info_delete(unsigned int userid); PUSER_INFO user_info_get_by_userid(unsigned int userid); -void user_info_change_status(PUSER_INFO pInfo, USER_STATUS status); +void user_info_change_status(PUSER_INFO_CONTEXT pInfo, USER_STATUS status); PUSER_INFO_CONTEXT get_all_user(); uv_rwlock_t* get_user_lock(); #ifdef __cplusplus diff --git a/srcs/libs/config/agent.cfg b/srcs/libs/config/agent.cfg index 9bdc289..397eb38 100644 --- a/srcs/libs/config/agent.cfg +++ b/srcs/libs/config/agent.cfg @@ -45,6 +45,7 @@ application: zero_mq: { svr_port = 6278; # ZeroMQ 服务器端口 + agent_port = 6279; # Agetn 通信端口 }; # 网络相关 diff --git a/srcs/libs/configure/config.c b/srcs/libs/configure/config.c index 2dfaf40..20d2d94 100644 --- a/srcs/libs/configure/config.c +++ b/srcs/libs/configure/config.c @@ -456,31 +456,31 @@ do { ADD_CFG_ITEM(CFG_BANNER_SHOW, "application.system.show_banner", VALUE_TYPE_BOOL, "1", "Enable/Disable show banner"); \ ADD_CFG_ITEM(CFG_HARDWARE_WATCH, "application.system.system_info_watch", VALUE_TYPE_BOOL, "1", "Monitor cpu, memory, disk, fan, temperature ..."); \ ADD_CFG_ITEM(CFG_HARDWARE_REFRESH, "application.system.system_info_refresh", VALUE_TYPE_INTEGRAL, "10", "Monitor hardware information upgrade frequency"); \ - /* 系统监控设备相配置 */ \ + /* 系统监控设备相配置 */ \ ADD_CFG_ITEM(CFG_WATCH_CPU, "application.watch_params.cpu", VALUE_TYPE_BOOL, "1", "Monitor cpu information"); \ ADD_CFG_ITEM(CFG_WATCH_MEMORY, "application.watch_params.memory", VALUE_TYPE_BOOL, "1", "Monitor memory information"); \ ADD_CFG_ITEM(CFG_WATCH_DISK, "application.watch_params.disk", VALUE_TYPE_BOOL, "1", "Monitor disk partition information"); \ ADD_CFG_ITEM(CFG_WATCH_SENSOR, "application.watch_params.sensor", VALUE_TYPE_BOOL, "1", "Sensor information refresh frequency"); \ - /* 系统监控设备刷频率 */ \ + /* 系统监控设备刷频率 */ \ ADD_CFG_ITEM(CFG_CPU_REFRESH, "application.watch_params.cpu_refresh", VALUE_TYPE_INTEGRAL, "10", "CPU information refresh frequency"); \ ADD_CFG_ITEM(CFG_MEM_REFRESH, "application.watch_params.mem_refresh", VALUE_TYPE_INTEGRAL, "10", "Memory information refresh frequency"); \ ADD_CFG_ITEM(CFG_DISK_REFRESH, "application.watch_params.disk_refresh", VALUE_TYPE_INTEGRAL, "10", "Disk information refresh frequency"); \ ADD_CFG_ITEM(CFG_SENSOR_REFRESH, "application.watch_params.sensor_refresh", VALUE_TYPE_INTEGRAL, "10", "Sensor information refresh frequency"); \ - /* 数据库相配置 */ \ - /* Redis配置 */ \ + /* 数据库相配置 */ \ + /* Redis配置 */ \ ADD_CFG_ITEM(CFG_DB_REDIS_SERVER, "application.database.redis_server", VALUE_TYPE_STRING, "127.0.0.1", "Redis database server ip address"); \ ADD_CFG_ITEM(CFG_DB_REDIS_PORT, "application.database.redis_port", VALUE_TYPE_INTEGRAL, "6379", "Redis database server port"); \ ADD_CFG_ITEM(CFG_DB_REDIS_PASSWD, "application.database.redis_passwd", VALUE_TYPE_STRING, "", "Redis database server password"); \ - /* MySQL配置 */ \ + /* MySQL配置 */ \ ADD_CFG_ITEM(CFG_DB_MYSQL_SERVER, "application.database.mysql_server", VALUE_TYPE_STRING, "127.0.0.1", "MySQL database server ip address"); \ ADD_CFG_ITEM(CFG_DB_MYSQL_PORT, "application.database.mysql_port", VALUE_TYPE_INTEGRAL, "3306", "MySQL database server port"); \ ADD_CFG_ITEM(CFG_DB_MYSQL_USER, "application.database.mysql_user", VALUE_TYPE_STRING, "", "MySQL database user name"); \ ADD_CFG_ITEM(CFG_DB_MYSQL_PASSWD, "application.database.mysql_passwd", VALUE_TYPE_STRING, "", "MySQL database server password"); \ ADD_CFG_ITEM(CFG_DB_MYSQL_DB_NAME, "application.database.mysql_database", VALUE_TYPE_STRING, ".main", "MySQL database used"); \ - /* 消息队列相配置 */ \ - /* ZeroMq配置 */ \ - ADD_CFG_ITEM(CFG_MQ_SVR_PORT, "application.zero_mq.svr_port", VALUE_TYPE_INTEGRAL, "6378", "ZeroMQ server port"); \ - /* 网络相关配置 */ \ + /* 消息队列相配置 */ \ + /* ZeroMq配置 */ \ + ADD_CFG_ITEM(CFG_MQ_SVR_PORT, "application.zero_mq.svr_port", VALUE_TYPE_INTEGRAL, "6278", "ZeroMQ server port"); \ + ADD_CFG_ITEM(CFG_MQ_DATA_CH, "application.zero_mq.agent_port", VALUE_TYPE_INTEGRAL, "6279", "ZeroMQ Agent server port"); \ ADD_CFG_ITEM(CFG_NIC_CARD_NAME, "application.network.svr_port", VALUE_TYPE_STRING, "ens160", "Network card name to send data"); \ ADD_CFG_ITEM(CFG_VXLAN_SUPPORT, "application.network.vxlan_support", VALUE_TYPE_BOOL, "1", "Is support vxLan tune"); \ } while (0)// clang-format on diff --git a/srcs/libs/configure/config_help.c b/srcs/libs/configure/config_help.c index fa54ec5..62f1fb4 100644 --- a/srcs/libs/configure/config_help.c +++ b/srcs/libs/configure/config_help.c @@ -97,4 +97,8 @@ const char *cfg_get_mysql_database() { int cfg_get_zero_mq_port() { return (unsigned short)cfg_get_integral_value(CFG_MQ_SVR_PORT); +} + +int cfg_get_zero_mq_data_channel() { + return (unsigned short)cfg_get_integral_value(CFG_MQ_DATA_CH); } \ No newline at end of file diff --git a/srcs/libs/include/config.h b/srcs/libs/include/config.h index e2eacdb..d7b8720 100644 --- a/srcs/libs/include/config.h +++ b/srcs/libs/include/config.h @@ -41,8 +41,9 @@ typedef enum { CFG_DB_MYSQL_PASSWD = 19, CFG_DB_MYSQL_DB_NAME = 20, CFG_MQ_SVR_PORT = 21, - CFG_NIC_CARD_NAME = 22, - CFG_VXLAN_SUPPORT = 23, + CFG_MQ_DATA_CH = 22, + CFG_NIC_CARD_NAME = 23, + CFG_VXLAN_SUPPORT = 24, CONFIG_ITEM_ID_MAX } CONFIG_ITEM_ID; @@ -71,6 +72,7 @@ const char *cfg_get_mysql_user(); const char *cfg_get_mysql_passwd(); const char *cfg_get_mysql_database(); int cfg_get_zero_mq_port(); +int cfg_get_zero_mq_data_channel(); const char *cfg_get_string_value(CONFIG_ITEM_ID id); long double cfg_get_float_value(CONFIG_ITEM_ID id); diff --git a/srcs/libs/include/msg_queue.h b/srcs/libs/include/msg_queue.h index 6b04cca..52a868d 100644 --- a/srcs/libs/include/msg_queue.h +++ b/srcs/libs/include/msg_queue.h @@ -10,6 +10,8 @@ extern "C" { #include "misc.h" +typedef const char *(*DATACHNNELCB)(const char *pMsg, void *pArgs); + typedef struct { char cmd[MAX_PATH]; char key[MAX_PATH]; @@ -20,6 +22,9 @@ int mq_init(void); void mq_uninit(void); const char *on_msg_cmd(const char *pCmd); +int mq_data_init(DATACHNNELCB dataCb); +int mq_data_send_msg(const char *pMsg); +void *get_mq_context(void); #ifdef __cplusplus } diff --git a/srcs/libs/mq/mq_data.c b/srcs/libs/mq/mq_data.c new file mode 100644 index 0000000..136f326 --- /dev/null +++ b/srcs/libs/mq/mq_data.c @@ -0,0 +1,105 @@ +// +// Created by xajhuang on 2022/6/6. +// +#include +#include +#include +#include +#include + +#include "msg_queue.h" +#include "config.h" +#include "user_errno.h" +#include "misc.h" + +static void *g_pDataCh = NULL; +static DATACHNNELCB g_pDataChCb = NULL; + +static void process_data_msg(void *pDataCh, zmq_msg_t *pMsg) { + const char *pResp; + zmq_msg_t msg; + const char *pRecMsg = strdup((const char *)zmq_msg_data(pMsg)); + + dzlog_info("receive(%zu): %s\n", zmq_msg_size(pMsg), pRecMsg); + zmq_msg_close(pMsg); + + if (g_pDataChCb) { + pResp = g_pDataChCb(pRecMsg, pDataCh); + + if (pResp != NULL && strlen(pResp) > 0) { + zmq_msg_init_size(&msg, strlen(pResp) + 1); + memcpy(zmq_msg_data(&msg), pResp, strlen(pResp)); + zmq_msg_send(&msg, pDataCh, 0); + free((void *)pResp); + } + + zmq_msg_close(&msg); + } else { + dzlog_warn("Unhandled message: %s\n", pRecMsg); + } + + free((void *)pRecMsg); +} + +int mq_data_send_msg(const char *pMsg) { + zmq_msg_t msg; + + if (pMsg) { + dzlog_debug("Send PPPoE Session:\n%s\n", pMsg); + zmq_msg_init_size(&msg, strlen(pMsg) + 1); + memcpy(zmq_msg_data(&msg), pMsg, strlen(pMsg)); + zmq_msg_send(&msg, g_pDataCh, 0); + zmq_msg_close(&msg); + } + + return ERR_SUCCESS; +} + +_Noreturn static void mqDataChannelCb(void *pDataCh) { + while (TRUE) { + zmq_msg_t msg; + zmq_msg_init(&msg); + + if (zmq_msg_recv(&msg, pDataCh, 0) != -1) { + process_data_msg(pDataCh, &msg); + zmq_msg_close(&msg); + } + + uv_sleep(10); + } +} + +int mq_data_init(DATACHNNELCB dataCb) { + static uv_thread_t uvThread; + void *pContext = get_mq_context(); + + char buf[1024]; + + if (pContext == NULL) { + return -ERR_MQ_CREATE_MQ; + } + + g_pDataChCb = dataCb; + + g_pDataCh = zmq_socket(pContext, ZMQ_PAIR); + + if (g_pDataCh == NULL) { + zmq_ctx_destroy(g_pDataCh); + return -ERR_MQ_CREATE_REP; + } + + memset(buf, 0, 1024); + + sprintf(buf, "tcp://*:%d", cfg_get_zero_mq_data_channel()); + dzlog_info("Start message queue server: tcp://*:%d\n", cfg_get_zero_mq_data_channel()); + + if (zmq_bind(g_pDataCh, buf) != 0) { + zmq_close(g_pDataCh); + zmq_ctx_destroy(g_pDataCh); + return -ERR_MQ_BIND_SOCKET; + } + + uv_thread_create(&uvThread, mqDataChannelCb, g_pDataCh); + + return ERR_SUCCESS; +} \ No newline at end of file diff --git a/srcs/libs/mq/msg_queue.c b/srcs/libs/mq/msg_queue.c index f0adae4..3bbe607 100644 --- a/srcs/libs/mq/msg_queue.c +++ b/srcs/libs/mq/msg_queue.c @@ -105,6 +105,10 @@ int mq_init(void) { return ERR_SUCCESS; } +void* get_mq_context() { + return g_pContext; +} + void mq_uninit(void) { g_mqExit = TRUE; } diff --git a/srcs/lwip/src/arch_linux/netif/rawif.c b/srcs/lwip/src/arch_linux/netif/rawif.c index 0d67a29..1a218c4 100644 --- a/srcs/lwip/src/arch_linux/netif/rawif.c +++ b/srcs/lwip/src/arch_linux/netif/rawif.c @@ -52,8 +52,10 @@ #include "lwip/sys.h" #include "netif/etharp.h" - #include "netif/rawif.h" + +#include "lwip/prot/udp.h" + #include "config.h" #if defined(LWIP_UNIX_LINUX) @@ -70,6 +72,8 @@ #define DEFAULT_GW_IPADDR (0xC0A80001) +#define VXLAN_PORT_NET_ORDER (0x12B5) + struct sockaddr_ll { unsigned short int sll_family; unsigned short int sll_protocol; @@ -262,8 +266,7 @@ static struct pbuf *low_level_input(struct netif *netif) { } len = (u16_t)readlen; - if ((buf[0] == 0x00 && buf[1] == 0x0C && buf[2] == 0x01 && buf[3] == 0x02) - || memcmp(buf, bmac, 6) == 0) { + if ((buf[0] == 0x00 && buf[1] == 0x0C && buf[2] == 0x01 && buf[3] == 0x02) || memcmp(buf, bmac, 6) == 0) { MIB2_STATS_NETIF_ADD(netif, ifinoctets, len); /* We allocate a pbuf chain of pbufs from the pool. */ @@ -326,8 +329,6 @@ static void rawif_input(struct netif *netif) { err_t rawif_init(struct netif *netif) { struct rawif *rawif = (struct rawif *)netif->state; - dzlog_debug("++++++++++++rawif<%p>\n", (void *)rawif); - if (rawif == NULL) { LWIP_DEBUGF(NETIF_DEBUG, ("rawif_init: out of memory for rawif\n")); return ERR_MEM; @@ -406,9 +407,30 @@ static void link_callback(struct netif *state_netif) { #endif /* LWIP_NETIF_LINK_CALLBACK */ static err_t netif_input_data(struct pbuf *p, struct netif *inp) { - struct netif *netif; + struct netif *netif; + struct rawif *rawif = (struct rawif *)inp->state; + unsigned char buf[1500]; + unsigned char *payload = (unsigned char *)p->payload; LWIP_ASSERT_CORE_LOCKED(); + + // 网卡支持vxlan + if (rawif->vxlan_support) { + const struct eth_hdr *ethhdr = (const struct eth_hdr *)payload; + const struct ip_hdr *iphdr = (const struct ip_hdr *)(payload + SIZEOF_ETH_HDR); + const struct udp_hdr *udphdr = (const struct udp_hdr *)(payload + SIZEOF_ETH_HDR + IPH_HL_BYTES(iphdr)); + + // UDP xvLan 数据包 + if (udphdr->dest == VXLAN_PORT_NET_ORDER) { + // 首包 + if (rawif->vxlan_buf.input_head == NULL) { + + + } + } + + } + NETIF_FOREACH(netif) { err_t err; unsigned char *pMac = (unsigned char *)p->payload; @@ -487,7 +509,6 @@ struct netif *bind_rawsocket_if(const char *eth_name) { return NULL; } - dzlog_debug("++++++++++++rawif<%p>\n", (void *)rawif); netif_add(netif, &ipaddr, &netmask, &gw, rawif, rawif_init, netif_input_data); #if LWIP_NETIF_STATUS_CALLBACK diff --git a/srcs/pppoe/agent_proto.c b/srcs/pppoe/agent_proto.c new file mode 100644 index 0000000..648bac6 --- /dev/null +++ b/srcs/pppoe/agent_proto.c @@ -0,0 +1,3 @@ +// +// Created by xajhuang on 2022/6/6. +// diff --git a/srcs/pppoe/vcpe_pppoe.c b/srcs/pppoe/vcpe_pppoe.c index 346ae29..ab510c7 100644 --- a/srcs/pppoe/vcpe_pppoe.c +++ b/srcs/pppoe/vcpe_pppoe.c @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "pppoe_session.h" #include "netif/rawif.h" #include "netif/ppp/ppp.h" @@ -13,6 +15,13 @@ #include "user_errno.h" #include "netif/ppp/pppapi.h" #include "config.h" +#include "msg_queue.h" + +typedef struct PPPOE_CACHE { + PPPPOE_SESSION_DATA pSessionData; + + struct PPPOE_CACHE *next, *prev; +} PPPOE_CACHE, *PPPPOE_CACHE; struct PPPOE_ERR_INFO_ { int errid; @@ -34,6 +43,8 @@ struct PPPOE_ERR_INFO_ { }; static struct netif *g_rawSocketIf = NULL; +static PPPPOE_CACHE g_pPPPCache = NULL; +static uv_rwlock_t g_cacheLock; static void pppLinkStatusCallback(ppp_pcb *pcb, int errCode, void *ctx) { struct netif *pppif = ppp_netif(pcb); @@ -42,15 +53,30 @@ static void pppLinkStatusCallback(ppp_pcb *pcb, int errCode, void *ctx) { switch (errCode) { case PPPERR_NONE: { /* No error. */ + PPPPOE_CACHE pCache = (PPPPOE_CACHE)malloc(sizeof(PPPOE_CACHE)); dzlog_info("<%p> PPPoE user(%05d:%s) connect server succeeded[%08X], Session: %04X\n", pcb, - pUser->userid, pUser->user_info.pppoe_user, + pUser->userid, + pUser->user_info.pppoe_user, pcb->lcp_gotoptions.magicnumber, sc->sc_session); #if LWIP_IPV4 - dzlog_info(" our_ipaddr = %s\n", ip4addr_ntoa(netif_ip4_addr(pppif))); - dzlog_info(" his_ipaddr = %s\n", ip4addr_ntoa(netif_ip4_gw(pppif))); - dzlog_info(" netmask = %s\n", ip4addr_ntoa(netif_ip4_netmask(pppif))); + memset(&pUser->session.data, 0, sizeof(PPPOE_SESSION_DATA)); + pUser->session.data.sessionId = sc->sc_session; + strncpy(pUser->session.data.clientIp, ip4addr_ntoa(netif_ip4_addr(pppif)), MAX_IP_V4_STR); + strncpy(pUser->session.data.clientGw, ip4addr_ntoa(netif_ip4_gw(pppif)), MAX_IP_V4_STR); + strncpy(pUser->session.data.clientMask, ip4addr_ntoa(netif_ip4_netmask(pppif)), MAX_IP_V4_STR); + sprintf(pUser->session.data.clientMac, + "%02X:%02X:%02X:%02X:%02X:%02X", + pUser->user_info.mac_addr[0], + pUser->user_info.mac_addr[1], + pUser->user_info.mac_addr[2], + pUser->user_info.mac_addr[3], + pUser->user_info.mac_addr[4], + pUser->user_info.mac_addr[5]); + dzlog_info(" our_ipaddr = %s\n", pUser->session.data.clientIp); + dzlog_info(" his_ipaddr = %s\n", pUser->session.data.clientGw); + dzlog_info(" netmask = %s\n", pUser->session.data.clientMask); #endif /* LWIP_IPV4 */ #if LWIP_DNS dzlog_info(" dns1 = %s\n", ipaddr_ntoa(dns_getserver(0))); @@ -59,6 +85,13 @@ static void pppLinkStatusCallback(ppp_pcb *pcb, int errCode, void *ctx) { #if PPP_IPV6_SUPPORT dzlog_info(" our6_ipaddr = %s\n", ip6addr_ntoa(netif_ip6_addr(pppif, 0))); #endif /* PPP_IPV6_SUPPORT */ + + if (pCache) { + pCache->pSessionData = &pUser->session.data; + uv_rwlock_wrlock(&g_cacheLock); + LL_APPEND(g_pPPPCache, pCache); + uv_rwlock_wrunlock(&g_cacheLock); + } pUser->session.status = STATUS_TASK_CONNECTED; break; } @@ -106,7 +139,7 @@ _Noreturn void sessionCalcCb(void *UNUSED(pArg)) { PUSER_INFO_CONTEXT pUser, pTmp; uv_rwlock_rdlock(get_user_lock()); - HASH_ITER(hh, pUserList, pUser, pTmp) { + HASH_ITER(hh_id, pUserList, pUser, pTmp) { PPPPOE_SESSION pSession = &pUser->session; switch (pSession->status) { @@ -168,6 +201,7 @@ _Noreturn void sessionCalcCb(void *UNUSED(pArg)) { pppapi_close(pUser->session.ppp, 0); pUser->session.retry.timeout = 0; } + break; #endif default: @@ -179,8 +213,46 @@ _Noreturn void sessionCalcCb(void *UNUSED(pArg)) { } while (TRUE); } +_Noreturn void cacheCalcCb(void *UNUSED(pArg)) { + do { + PPPPOE_CACHE pCache, pTmp; + int count; + + uv_rwlock_rdlock(&g_cacheLock); + LL_COUNT(g_pPPPCache, pCache, count); + uv_rwlock_rdunlock(&g_cacheLock); + + if (count > 0) { + cJSON *pRoot = cJSON_CreateObject(); + cJSON *pSession = cJSON_CreateArray(); + cJSON_AddStringToObject(pRoot, "message", "add-ywg-pppoe"); + + uv_rwlock_wrlock(&g_cacheLock); + LL_FOREACH_SAFE(g_pPPPCache, pCache, pTmp) { + cJSON *pItem = cJSON_CreateObject(); + cJSON_AddNumberToObject(pItem, "sessionId", pCache->pSessionData->sessionId); + cJSON_AddStringToObject(pItem, "clientIp", pCache->pSessionData->clientIp); + cJSON_AddStringToObject(pItem, "clientGw", pCache->pSessionData->clientGw); + cJSON_AddStringToObject(pItem, "clientMask", pCache->pSessionData->clientMask); + cJSON_AddStringToObject(pItem, "clientMac", pCache->pSessionData->clientMac); + cJSON_AddStringToObject(pItem, "localMac", pCache->pSessionData->localMac); + cJSON_AddItemToArray(pSession, pItem); + LL_DELETE(g_pPPPCache, pCache); + free(pCache); + } + uv_rwlock_wrunlock(&g_cacheLock); + cJSON_AddItemToObject(pRoot, "params", pSession); + mq_data_send_msg(cJSON_Print(pRoot)); + cJSON_Delete(pRoot); + } + + uv_sleep(1000); + } while (TRUE); +} + int pppoe_session_init() { - static uv_thread_t uvThread; + static uv_thread_t uvThread, cacheThread; + uv_rwlock_init(&g_cacheLock); g_rawSocketIf = bind_rawsocket_if(config_get_nic_name()); @@ -190,6 +262,7 @@ int pppoe_session_init() { // 启动Session状态机线程 uv_thread_create(&uvThread, sessionCalcCb, NULL); + uv_thread_create(&cacheThread, cacheCalcCb, NULL); return ERR_SUCCESS; } diff --git a/srcs/user/user_info.c b/srcs/user/user_info.c index 5abc052..38f907d 100644 --- a/srcs/user/user_info.c +++ b/srcs/user/user_info.c @@ -5,13 +5,14 @@ #include "user_info.h" #include "user_errno.h" -static PUSER_INFO_CONTEXT g_pUserList = NULL; -static uv_rwlock_t g_userLock; +static PUSER_INFO_CONTEXT g_pUserByIdList = NULL; +static PUSER_INFO_CONTEXT g_pUserByTagsList = NULL; +static uv_rwlock_t g_userLock; -static USER_INFO g_userInfo[] = { - {0, 1, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x02}, "xajhuang3", "aaaHuang1", STATUS_USER_NEW}, - {1, 2, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x03}, "xajhuang1", "aaaHuang1", STATUS_USER_NEW}, - {3, 4, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x04}, "xajhuang2", "aaaHuang1", STATUS_USER_NEW}, +static USER_PARAMS g_userInfo[] = { + {0, 100, 16, 12, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x02}, "xajhuang3", "aaaHuang1"}, + {1, 200, 24, 371, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x03}, "xajhuang1", "aaaHuang1"}, + {2, 300, 14, 15, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x04}, "xajhuang2", "aaaHuang1"}, }; void user_info_init() { @@ -22,18 +23,18 @@ void user_info_init() { user_info_add(2, &g_userInfo[2]); } -void user_info_change_status(PUSER_INFO pInfo, USER_STATUS status) { +void user_info_change_status(PUSER_INFO_CONTEXT pInfo, USER_STATUS status) { pInfo->user_status = status; } -int user_info_add(unsigned int userid, PUSER_INFO pInfo) { +int user_info_add(unsigned int userid, PUSER_PARAMS pInfo) { PUSER_INFO_CONTEXT pUser; uv_rwlock_rdlock(&g_userLock); - HASH_FIND_INT(g_pUserList, &userid, pUser); + HASH_FIND(hh_id, g_pUserByIdList, &userid, sizeof(unsigned int), pUser); uv_rwlock_rdunlock(&g_userLock); - if(pUser != NULL) { + if (pUser != NULL) { return -ERR_ITEM_EXISTS; } @@ -46,18 +47,20 @@ int user_info_add(unsigned int userid, PUSER_INFO pInfo) { memset(pList, 0, sizeof(USER_INFO_CONTEXT)); + pList->user_status = STATUS_USER_NEW; pList->userid = userid; - pList->user_info.qinq_tag1 = pInfo->qinq_tag1; - pList->user_info.qinq_tag2 = pInfo->qinq_tag2; + pList->vxlan.vni = pInfo->vni; + pList->vxlan.q1 = pInfo->q1; + pList->vxlan.q2 = pInfo->q2; pList->user_info.pppoe_user = strdup(pInfo->pppoe_user); pList->user_info.pppoe_passwd = strdup(pInfo->pppoe_passwd); - pList->user_info.user_status = STATUS_USER_NEW; memcpy(pList->user_info.mac_addr, pInfo->mac_addr, 6); pList->session.status = STATUS_TASK_INIT; uv_rwlock_wrlock(&g_userLock); - HASH_ADD_INT(g_pUserList, userid, pList); + HASH_ADD(hh_id, g_pUserByIdList, userid, sizeof(unsigned int), pList); + HASH_ADD(hh_vxlan, g_pUserByTagsList, vxlan, sizeof(VXLAN_TAG), pList); uv_rwlock_wrunlock(&g_userLock); } @@ -68,11 +71,11 @@ void user_info_remove(unsigned int userid) { PUSER_INFO_CONTEXT pInfo; uv_rwlock_rdlock(&g_userLock); - HASH_FIND_INT(g_pUserList, &userid, pInfo); + HASH_FIND(hh_id, g_pUserByIdList, &userid, sizeof(unsigned int), pInfo); uv_rwlock_rdunlock(&g_userLock); if (pInfo) { - pInfo->user_info.user_status = STATUS_USER_DELETE; + pInfo->user_status = STATUS_USER_DELETE; } } @@ -80,11 +83,12 @@ void user_info_delete(unsigned int userid) { PUSER_INFO_CONTEXT pInfo; uv_rwlock_rdlock(&g_userLock); - HASH_FIND_INT(g_pUserList, &userid, pInfo); + HASH_FIND(hh_id, g_pUserByIdList, &userid, sizeof(unsigned int), pInfo); uv_rwlock_rdunlock(&g_userLock); if (pInfo) { - HASH_DEL(g_pUserList, pInfo); + HASH_DELETE(hh_id, g_pUserByIdList, pInfo); + HASH_DELETE(hh_vxlan, g_pUserByTagsList, pInfo); } } @@ -92,7 +96,7 @@ int user_info_modify(unsigned int userid, PUSER_INFO pUser) { PUSER_INFO_CONTEXT pInfo; uv_rwlock_rdlock(&g_userLock); - HASH_FIND_INT(g_pUserList, &userid, pInfo); + HASH_FIND(hh_id, g_pUserByIdList, &userid, sizeof(unsigned int), pInfo); uv_rwlock_rdunlock(&g_userLock); if (pInfo) { @@ -104,19 +108,19 @@ int user_info_modify(unsigned int userid, PUSER_INFO pUser) { } if (strcmp(pInfo->user_info.pppoe_user, pUser->pppoe_user) != 0) { - free((void*)pInfo->user_info.pppoe_user); + free((void *)pInfo->user_info.pppoe_user); pInfo->user_info.pppoe_user = strdup(pUser->pppoe_user); isUpgrade = TRUE; } if (strcmp(pInfo->user_info.pppoe_passwd, pUser->pppoe_passwd) != 0) { - free((void*)pInfo->user_info.pppoe_passwd); + free((void *)pInfo->user_info.pppoe_passwd); pInfo->user_info.pppoe_passwd = strdup(pUser->pppoe_passwd); isUpgrade = TRUE; } if (isUpgrade) { - pInfo->user_info.user_status = STATUS_USER_MODIFY; + pInfo->user_status = STATUS_USER_MODIFY; } return ERR_SUCCESS; @@ -128,7 +132,7 @@ int user_info_modify(unsigned int userid, PUSER_INFO pUser) { PUSER_INFO user_info_get_by_userid(unsigned int userid) { PUSER_INFO_CONTEXT pInfo; - HASH_FIND_INT(g_pUserList, &userid, pInfo); + HASH_FIND(hh_id, g_pUserByIdList, &userid, sizeof(unsigned int), pInfo); if (pInfo) { return &pInfo->user_info; @@ -138,9 +142,9 @@ PUSER_INFO user_info_get_by_userid(unsigned int userid) { } PUSER_INFO_CONTEXT get_all_user() { - return g_pUserList; + return g_pUserByIdList; } -uv_rwlock_t* get_user_lock() { +uv_rwlock_t *get_user_lock() { return &g_userLock; } \ No newline at end of file