📄 cdnsclient.c
字号:
{ if (THIS->iStatus) { GB.ReturnString(NULL); return; } GB.ReturnString(THIS->sHostIP); return; } if (THIS->iStatus) { GB.Error("HostIP can not be changed while working"); return; } GB.FreeString(&THIS->sHostIP); GB.StoreString(PROP(GB_STRING), &THIS->sHostIP);END_PROPERTY/************************************************* This property gets/sets asynchronous state: FALSE : Synchronous TRUE : Asynchronous *************************************************/BEGIN_PROPERTY ( CDNSCLIENT_Async ) if (READ_PROPERTY) { GB.ReturnBoolean(THIS->iAsync); return; } if (THIS->iStatus) { GB.Error("Async mode can not be changed while working"); return; } if ( !dns_set_async_mode (VPROP(GB_BOOLEAN),THIS)) return; GB.Error("No resources available start asynchronous mode");END_PROPERTY/********************************************************** This property gets status : 0 --> Inactive, 1 --> Working **********************************************************/BEGIN_PROPERTY ( CDNSCLIENT_Status ) GB.ReturnInteger(THIS->iStatus);END_PROPERTY/************************************************* Gambas object "Constructor" *************************************************/BEGIN_METHOD_VOID(CDNSCLIENT_new) THIS->CliParent=NULL; THIS->finished_callback=NULL; THIS->sHostIP=NULL; THIS->sHostName=NULL; THIS->iStatus=0; THIS->iAsync=0; THIS->i_id=0; sem_init(&THIS->sem_id,0,1); dns_count++; if (dns_object==NULL) GB.Alloc((void**)&dns_object,sizeof(void*)); else GB.Realloc((void**)&dns_object,dns_count*sizeof(void*)); dns_object[dns_count-1]=(void*)THIS;END_METHOD/************************************************* Gambas object "Destructor" *************************************************/BEGIN_METHOD_VOID(CDNSCLIENT_free) int myloop; int Position=-1; dns_close_all(THIS); GB.FreeString(&THIS->sHostIP); GB.FreeString(&THIS->sHostName); for (myloop=0;myloop<dns_count;myloop++) { if ( dns_object[myloop]==((void*)THIS) ) { Position=myloop; break; } } if (Position>=0) { for (myloop=Position;myloop< (dns_count-1);myloop++) { dns_object[myloop]=dns_object[myloop+1]; } dns_count--; if (!dns_count) { GB.Free((void**)&dns_object); if (dns_r_pipe != -1) { GB.Watch(dns_r_pipe , GB_WATCH_NONE , (void *)dns_callback,0); close(dns_r_pipe); close(dns_w_pipe); dns_r_pipe=-1; dns_w_pipe=-1; } } }END_METHOD/************************************************* To cancel an asynchronous operation *************************************************/BEGIN_METHOD_VOID(CDNSCLIENT_Stop) dns_close_all(THIS);END_METHOD/***************************************************************************** This method takes the Host IP, which was stored using HostName property, and trasnlates it to IP address *****************************************************************************/BEGIN_METHOD_VOID(CDNSCLIENT_GetHostName) struct hostent *stHost=NULL; struct in_addr addr; if (THIS->iStatus) { GB.Error("Object is already working"); return; } if (THIS->sHostIP) { if ( THIS->iAsync) { /* Asynchronous mode */ sem_wait(&THIS->sem_id); THIS->i_id++; sem_post(&THIS->sem_id); THIS->iStatus=1; if (dns_thread_getname(THIS)) { GB.Error("No resources available to create a thread"); return; } } else { /* Synchronous mode */ inet_aton(THIS->sHostIP,&addr);#ifdef __sun__ stHost=gethostbyaddr((const char *)&addr,sizeof(addr),AF_INET);#else stHost=gethostbyaddr(&addr,sizeof(addr),AF_INET);#endif if (stHost==NULL) { GB.FreeString(&THIS->sHostName); } else { GB.FreeString(&THIS->sHostName); GB.NewString(&THIS->sHostName,stHost->h_name,0); } GB.Raise((void*)THIS,Finished,0); } } else { GB.FreeString(&THIS->sHostName); }END_METHOD/***************************************************************************** This method takes the Host IP, which was stored using HostIP property, and trasnlates it to host name *****************************************************************************/BEGIN_METHOD_VOID(CDNSCLIENT_GetHostIP) struct hostent *stHost=NULL; if (THIS->iStatus) { GB.Error("Object is already working"); return; } if (THIS->sHostName) { if ( THIS->iAsync ) { /* Asynchronous mode */ sem_wait(&THIS->sem_id); THIS->i_id++; sem_post(&THIS->sem_id); THIS->iStatus=1; if (dns_thread_getip(THIS)) { GB.Error("No resources available to create a thread"); return; } } else { /* Synchronous mode */ stHost=gethostbyname(THIS->sHostName); if (stHost==NULL) { GB.FreeString(&THIS->sHostIP); } else { GB.FreeString(&THIS->sHostIP); GB.NewString(&THIS->sHostIP, inet_ntoa(*( (struct in_addr*)stHost->h_addr ) ) ,0); } GB.Raise((void*)THIS,Finished,0); } } else { GB.FreeString(&THIS->sHostIP); }END_METHOD/*************************************************************** Here we declare the public interface of DnsClient class ***************************************************************/GB_DESC CDnsClientDesc[] ={ GB_DECLARE("DnsClient", sizeof(CDNSCLIENT)), GB_EVENT("Finished", NULL, NULL, &Finished), GB_METHOD("_new", NULL, CDNSCLIENT_new, NULL), GB_METHOD("_free", NULL, CDNSCLIENT_free, NULL), GB_METHOD("Stop", NULL, CDNSCLIENT_Stop, NULL), GB_METHOD("GetHostName", NULL, CDNSCLIENT_GetHostName, NULL), GB_METHOD("GetHostIP", NULL, CDNSCLIENT_GetHostIP, NULL), GB_PROPERTY("HostName", "s", HostName), GB_PROPERTY("HostIP", "s", HostIP), GB_PROPERTY("Async", "b", CDNSCLIENT_Async), GB_PROPERTY_READ("Status", "i", CDNSCLIENT_Status), GB_CONSTANT("_Properties", "s", "HostName,HostIP,Async=TRUE"), GB_CONSTANT("_DefaultEvent", "s", "Finished"), GB_END_DECLARE};/******************************************************************************I do not know if Solaris accepts getaddrinfo and getnameinfo, so here is the old implementation for that O.S.*******************************************************************************//*void* dns_get_ip(void* v_obj){ char Buf[1]; char *BufData; int myid; int herr; struct hostent hostbuf, *stHost; size_t hstbuflen; char tmphstbuf[1024]; CDNSCLIENT *mythis; pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); hstbuflen=1024; Buf[0]='1'; mythis=(CDNSCLIENT*)v_obj; sem_wait(&mythis->sem_id); myid=mythis->i_id; sem_post(&mythis->sem_id); stHost=gethostbyname_r (mythis->sHostName, &hostbuf, tmphstbuf, hstbuflen, &herr); sem_wait(&dns_th_pipe); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL); write (dns_w_pipe,&v_obj,sizeof(void*)); write (dns_w_pipe,&myid,sizeof(int)); write (dns_w_pipe,Buf,sizeof(char)); if (stHost!=NULL) { BufData=inet_ntoa(*( (struct in_addr*)stHost->h_addr )); write (dns_w_pipe,BufData,strlen(BufData)*sizeof(char)); } write (dns_w_pipe,"\x10",sizeof(char)); sem_post(&dns_th_pipe); return NULL;}void* dns_get_name(void* v_obj){ char Buf[1]; int myid; int herr; size_t hstbuflen; char tmphstbuf[2048]; struct hostent hostbuf,*stHost=NULL; CDNSCLIENT *mythis; struct in_addr addr; pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); hstbuflen=2048; Buf[0]='0'; mythis=(CDNSCLIENT*)v_obj; sem_wait(&mythis->sem_id); myid=mythis->i_id; sem_post(&mythis->sem_id); inet_aton(mythis->sHostIP,&addr); stHost=gethostbyaddr_r((const char *)&addr, sizeof (addr), AF_INET, &hostbuf,tmphstbuf,hstbuflen,&herr); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL); sem_wait(&dns_th_pipe); write (dns_w_pipe,&v_obj,sizeof(void*)); write (dns_w_pipe,&myid,sizeof(int)); write (dns_w_pipe,Buf,sizeof(char)); if (stHost!=NULL) write (dns_w_pipe,stHost->h_name,strlen(stHost->h_name)*sizeof(char)); write (dns_w_pipe,"\x10",sizeof(char)); sem_post(&dns_th_pipe); return NULL;}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -