123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- /*
- 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;
- 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",nwy_get_current_thread(),domainInfo->address);
- char *ip_str=nwy_gethostbyname(domainInfo->address);
- if(ip_str==NULL || 0==strlen(ip_str)){
- domainInfo->status=3;
- }else{
- snprintf(domainInfo->address, sizeof(domainInfo->address),"%s",ip_str);
- wlog_info("dnsEntry: %s",domainInfo->address);
- domainInfo->status=4;
- }
- userExitThread("dnsEntry");
- }
- static void userDomainCheck(void);
- /*
- tryAnalyseDomain
- 域名解析API操作
- */
- static DOMAIN_STEP tryAnalyseDomain(DOMAIN_INFO_DEF *domainInfo){
- //要开线程去获取,通过回调来处理,没有回调试方式是block
- if(NULL==nwy_create_thread("dnsEntry", dnsEntry,(void *)domainInfo,NWY_OSI_PRIORITY_NORMAL,1024,4))
- wlog_info("dns entry trhead null");
- return DOMAIN_WAIT;//wait
- }
- //检测任务是否空闲
- 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(talking.netWork.pdp==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");
- }
- }else if(4==domainInfo[i].status){//获取成功时,如果应用层未取值,超时扔掉值,否则系统无法休眠
- if(++domainInfo[i].failTimeout>=1000){
- domainInfo[i].status=0;
- wlog_warn("domain ok 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
- domainInfo[i].failTimeout=0;
- domainInfo[i].status=1;
- strncpy(domainInfo[i].address, addr, IP_DOMAIN_SIZE);
- *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;
- }
|