123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- /*
- domain.c
- 域名解析功能
- */
- #include "includes.h"
- typedef struct{
- T_UINT8 status:4;
- T_UINT8 result:2;
- T_UINT16 timeout;
- T_UINT16 failTimeout;
- T_INT8 address[IP_DOMAIN_SIZE+1];
- }DOMAIN_INFO_DEF;
- #define DOMAIN_NUM_MAX 5 //support analyse max domain numbers
- DOMAIN_INFO_DEF domainInfo[DOMAIN_NUM_MAX];
- T_BOOL userDomainBusy=FALSE;
- T_UINT8 userDomainIndex;
- #if defined PLATFORM_MS5700
- void domainCallBack(void *param){
- LSAPI_OSI_Event_t *ev = (LSAPI_OSI_Event_t *)param;
- DOMAIN_INFO_DEF *domainInfo=(DOMAIN_INFO_DEF *)ev->param3;
- T_BOOL needtobeclose=FALSE;
- if (ev->id== LSAPI_SOCK_DNS_RESOLV_SUC_IND) {
- LSAPI_SOCK_IP_ADDR_t* nIpAddr = (LSAPI_SOCK_IP_ADDR_t *)ev->param1;
- snprintf(domainInfo->address, sizeof(domainInfo->address),"%s",LSAPI_SOCK_IPAddr_ntoa(nIpAddr));
- wlog_info("LSAPI_SOCK_Gethostbyname callback: %s",domainInfo->address);
- domainInfo->status=4;
- needtobeclose=TRUE;
- }else if (ev->id== LSAPI_SOCK_RESOLV_ERR_IND) {
- wlog_warn("LSAPI_SOCK_Gethostbyname EV_CFW_DNS_RESOLV_ERR_IND FAIL");
- domainInfo->status=3;
- needtobeclose=TRUE;
- }
- LSAPI_OSI_Free(ev);
- if(TRUE==needtobeclose) threadPostEvent(LSAPI_OSI_ThreadCurrent(),USER_EVENT_EXIT,NULL);
- }
- void dnsEntry(void *param){
- DOMAIN_INFO_DEF *domainInfo=(DOMAIN_INFO_DEF *)param;
- LSAPI_DNS_RESULT_t dnsRet;
- LSAPI_SOCK_IP_ADDR_t pAddr;
- wlog_info("[NewThread:dnsEntry]%x:%s",LSAPI_OSI_ThreadCurrent(),domainInfo->address);
- dnsRet=LSAPI_SOCK_Gethostbyname(domainInfo->address, &pAddr, 1,0,domainCallBack,(void *)param);
- if(dnsRet==LSAPI_RESOLV_COMPLETE) {//传IP地址会直接返回这个,我们是判断了之后才进这里,即不会传IP进来
- wlog_info("domain LSAPI_RESOLV_COMPLETE");
- snprintf(domainInfo->address, sizeof(domainInfo->address),"%s",LSAPI_SOCK_IPAddr_ntoa(&pAddr));
- domainInfo->status=4;
- userExitThread("dnsEntry");
- }else if(dnsRet==LSAPI_RESOLV_QUERY_INVALID){
- wlog_error("domain LSAPI_RESOLV_QUERY_INVALID");
- domainInfo->status=3;
- userExitThread("dnsEntry");
- }else if(dnsRet==LSAPI_RESOLV_QUERY_QUEUED){
- wlog_info("domain LSAPI_RESOLV_QUERY_QUEUED");
- }
- LSAPI_OSI_Event_t event = {};
- for(;;){
- LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);
- if(USER_EVENT_EXIT==event.id){
- userExitThread("dnsEntry");
- }
- }
- }
- #endif
- static void userDomainCheck(void);
- /*
- tryAnalyseDomain
- 域名解析API操作
- */
- static DOMAIN_STEP tryAnalyseDomain(DOMAIN_INFO_DEF *domainInfo){
- #if defined ACTIVE_LINUX
- struct hostent *hptr;
- hptr=(struct hostent *)gethostbyname(domainInfo->address);
- if(NULL==hptr) return DOMAIN_ERR;
- if(NULL==hptr->h_addr_list[0]) return DOMAIN_ERR;
- inet_ntop(hptr->h_addrtype, hptr->h_addr_list[0],domainInfo->address, IP_DOMAIN_SIZE);
- return DOMAIN_OK;
- #elif defined PLATFORM_MS5700
- //要开线程去获取,通过回调来处理,没有回调试方式是block
- if(NULL==LSAPI_OSI_ThreadCreate("dnsEntry", dnsEntry,(void *)domainInfo,LSAPI_OSI_PRIORITY_NORMAL,DOMAIN_THREAD_STACK,4))
- wlog_info("dns entry trhead null");
- return DOMAIN_WAIT;//wait
- #else
- wlog_info("tryAnalyseDomain is not finished");
- return FALSE;
- #endif
- }
- //检测任务是否空闲
- void isDomainIdle(void){
- T_UINT8 i;
- for(i=0;i<DOMAIN_NUM_MAX;i++){
- if(0!=domainInfo[i].status){
- ticketDeVote(TICKET_PT_DOMAIN);
- return;
- }
- }
- ticketVote(TICKET_PT_DOMAIN);
- }
- /*
- tryAnalyseDomain
- 域名解析任务
- */
- PT_THREAD (ptDomainTask(pt_timer_t *ptPool, struct pt *pt)){
- static pt_timer_t ptTimer;
- static T_UINT8 iniStatus=1;
- IPD_ENUM ipType;
- T_UINT8 i;
- T_UINT8 result;
- if(iniStatus){
- memset((T_UINT8 *)&domainInfo, 0, sizeof(DOMAIN_INFO_DEF));
- iniStatus=0;
- }
- PT_BEGIN(pt);
- while(1){
- userDomainCheck();
- if(app.netWork.netReayd==0) break;
- for(i=0;i<DOMAIN_NUM_MAX;i++){
- if(1==domainInfo[i].status){//have task to do, check ip or domain first
- wlog_info("Domain addr check[%d]:%s", i,domainInfo[i].address);
- if(FALSE==checkIpDomain(domainInfo[i].address, strlen(domainInfo[i].address), NULL, &ipType)){
- wlog_warn("Domain invalid addr");
- domainInfo[i].status=3;//failed
- }else{
- if(IPD_IP==ipType){//it is already ip
- domainInfo[i].status=4;//success
- }else domainInfo[i].status=2;//domain , need anaylise
- }
- }else if(2==domainInfo[i].status){//try analyse
- result=tryAnalyseDomain(&domainInfo[i]);
- if(DOMAIN_ERR==result){
- wlog_error("analyse failed");
- domainInfo[i].status=3;
- }else if(DOMAIN_OK==result){
- wlog_info("analyse ok:%s",domainInfo[i].address);
- domainInfo[i].status=4;
- }else if(DOMAIN_WAIT==result){
- domainInfo[i].status=5;//wait
- domainInfo[i].timeout=0;
- }
- }else if(5==domainInfo[i].status){
- if(++domainInfo[i].timeout>2000){//10秒没结果为超时
- wlog_warn("domain timeout");
- domainInfo[i].status=3;
- }
- }else if(3==domainInfo[i].status){//获取失败时,如果应用层未取值,超时扔掉值,否则系统无法休眠
- if(++domainInfo[i].failTimeout>=1000){
- domainInfo[i].status=0;
- wlog_warn("domain faild value timeout");
- }
- }
- }
- isDomainIdle();
- PTTimerStart(ptPool, &ptTimer,1);//can be be fast
- PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
- }
- PT_END(pt);
- }
- /*
- setDomainForIp
- 域名解析请求入队列
- */
- //addr, the address that need to anaylise
- //index, the index of domain queue feed back if return TRUE
- T_BOOL setDomainForIp(T_INT8 *addr, T_UINT8 *index){
- T_UINT8 i;
- for(i=0;i<DOMAIN_NUM_MAX;i++){
- if(domainInfo[i].status==0){//idle
- strncpy(domainInfo[i].address, addr, IP_DOMAIN_SIZE);
- domainInfo[i].status=1;
- domainInfo[i].failTimeout=0;
- *index=i;
- return TRUE;
- }
- }
- return FALSE;
- }
- /*
- getDomainForIp
- 域名解析结果查询
- */
- //index, this index of domain queue, which get form setDomainForIp
- //outIp, fill with ip data if success
- //outLen, the max size of outIp buffer
- //return, 0 busy, 1 failed, 2 ok
- T_UINT8 getDomainForIp(T_UINT8 index, T_INT8 *outIp, T_UINT8 outLen){
- T_UINT8 i,ret=DOMAIN_WAIT;
- i=index;
- if(i >= DOMAIN_NUM_MAX) return 1;//err
- switch(domainInfo[i].status){
- case 0:
- ret=DOMAIN_ERR;
- break;
- case 1:
- case 2:
- ret=DOMAIN_WAIT;//busy
- break;
- case 3:
- ret=DOMAIN_ERR;//failed
- domainInfo[i].status=0;
- break;
- case 4:
- ret=DOMAIN_OK;//ok
- strncpy(outIp, domainInfo[i].address, outLen);
- domainInfo[i].status=0;
- break;
- }
- return ret;
- }
- void tryDomain(T_UINT8 *data, T_UINT16 len){
- T_UINT16 i;
- for(i=0;i<len;i++){
- if(data[i]==' ' || data[i]=='\r'){
- data[i]=0;
- break;
- }
- }
- if(i<=3) return;
- wlog_info("user domain req:%s",data);
- if(userDomainBusy==TRUE){
- msgToOutter("+DOMAIN:SYNC\r\n");
- return;
- }
- if(FALSE==setDomainForIp((T_INT8 *)data, &userDomainIndex)) msgToOutter("+DOMAIN:BUSY\r\n");
- userDomainBusy=TRUE;
- }
- /*
- userDomainCheck
- 用户外部(串口)请求域名解析的检测操作
- */
- static void userDomainCheck(void){
- T_UINT8 ret;
- T_INT8 ipAddr[20];
- T_INT8 info[40];
- if(FALSE==userDomainBusy) return;
- ret=getDomainForIp(userDomainIndex, ipAddr, sizeof(ipAddr)-1);
- if(ret==1){
- msgToOutter("+DOMAIN:ERROR\r\n");
- goto UD_END;
- }else if(ret==0){}//wait
- else{
- snprintf(info, sizeof(info), "+DOMAIN:%s\r\n",ipAddr);
- msgToOutter(info);
- goto UD_END;
- }
- return;
- UD_END:
- userDomainBusy=FALSE;
- }
|