⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ares_init.c

📁 这是国外的resip协议栈
💻 C
📖 第 1 页 / 共 3 页
字号:
{  SCDynamicStoreContext context = {0, NULL, NULL, NULL, NULL};  SCDynamicStoreRef store = SCDynamicStoreCreate(NULL, CFSTR("init_by_defaults_systemconfiguration"), NULL, &context);    channel->nservers = 0;  if (store)  {    // kSCDynamicStoreDomainState/kSCCompNetwork/kSCCompGlobal/kSCEntNetDNS    CFStringRef key = CFSTR("State:/Network/Global/DNS");    CFDictionaryRef dnsDict = SCDynamicStoreCopyValue(store, key);    if (dnsDict)    {      CFArrayRef addresses = (CFArrayRef) CFDictionaryGetValue(dnsDict, kSCPropNetDNSServerAddresses);      if (addresses)      {        //CFShow(addresses);        channel->nservers = CFArrayGetCount(addresses);        channel->servers = malloc(channel->nservers * sizeof(struct server_state));        memset(channel->servers, '\0', channel->nservers * sizeof(struct server_state));        int i;        for (i = 0; i < channel->nservers; i++)        {          CFStringRef address = CFArrayGetValueAtIndex(addresses, i);          //CFShow(address);          const int kBufferSize = 20;          char str[kBufferSize];          CFStringGetCString(address, str, kBufferSize, kCFStringEncodingUTF8);          inet_pton4(str, (u_char*)&channel->servers[i].addr);#ifdef USE_IPV6          channel->servers[i].family = AF_INET;#endif        }      }      CFRelease(dnsDict);    }    /* If no specified servers, try a local named. */    if (channel->nservers == 0)    {      channel->servers = malloc(sizeof(struct server_state));      memset(channel->servers, '\0', sizeof(struct server_state));#ifdef USE_IPV6			       channel->servers[0].family = AF_INET;#endif      channel->servers[0].addr.s_addr = htonl(INADDR_LOOPBACK);      channel->nservers = 1;    }    CFRelease(store);  }}#endifstatic int init_by_defaults(ares_channel channel){  char hostname[MAXHOSTNAMELEN + 1];  if (channel->flags == -1)    channel->flags = 0;  if (channel->timeout == -1)    channel->timeout = DEFAULT_TIMEOUT;  if (channel->tries == -1)    channel->tries = DEFAULT_TRIES;  if (channel->ndots == -1)    channel->ndots = 1;  if (channel->udp_port == -1)    channel->udp_port = htons(NAMESERVER_PORT);  if (channel->tcp_port == -1)    channel->tcp_port = htons(NAMESERVER_PORT);  if (channel->nservers == -1)    {#ifdef WIN32     /*      * Way of getting nameservers that should work on all Windows from 98 on.      */      FIXED_INFO *     FixedInfo;      ULONG            ulOutBufLen;      DWORD            dwRetVal;      IP_ADDR_STRING * pIPAddr;	  HANDLE           hLib;	  int num;	  DWORD (WINAPI *GetNetworkParams)(FIXED_INFO*, DWORD*); 	  hLib = LoadLibrary(TEXT("iphlpapi.dll"));	  if(!hLib)	  {		  return ARES_ENOTIMP;	  }	  (void*)GetNetworkParams = GetProcAddress(hLib, TEXT("GetNetworkParams"));	  if(!GetNetworkParams)	  {		  FreeLibrary(hLib);		  return ARES_ENOTIMP;	  }      //printf("ARES: figuring out DNS servers\n");      FixedInfo = (FIXED_INFO *) GlobalAlloc( GPTR, sizeof( FIXED_INFO ) );      ulOutBufLen = sizeof( FIXED_INFO );      if( ERROR_BUFFER_OVERFLOW == (*GetNetworkParams)( FixedInfo, &ulOutBufLen ) ) 	  {        GlobalFree( FixedInfo );        FixedInfo = (FIXED_INFO *)GlobalAlloc( GPTR, ulOutBufLen );      }      if ( dwRetVal = (*GetNetworkParams)( FixedInfo, &ulOutBufLen ) )      {        //printf("ARES: couldn't get network params\n");        GlobalFree( FixedInfo );  	    FreeLibrary(hLib);        return ARES_ENODATA;      }      else      {       /**        printf( "Host Name: %s\n", FixedInfo -> HostName );        printf( "Domain Name: %s\n", FixedInfo -> DomainName );        printf( "DNS Servers:\n" );        printf( "\t%s\n", FixedInfo -> DnsServerList.IpAddress.String );        **/        // Count how many nameserver entries we have and allocate memory for them.        num = 0;        pIPAddr = &FixedInfo->DnsServerList;             while ( pIPAddr && strlen(pIPAddr->IpAddress.String) > 0)        {          num++;          pIPAddr = pIPAddr ->Next;        }        if(num>0)        {           channel->servers = malloc( (num) * sizeof(struct server_state));		   if (!channel->servers)		   {	           GlobalFree( FixedInfo ); 		       FreeLibrary(hLib);			   return ARES_ENOMEM;		   }           memset(channel->servers, '\0', num * sizeof(struct server_state));		   channel->nservers = 0;           pIPAddr = &FixedInfo->DnsServerList;              while ( pIPAddr && strlen(pIPAddr->IpAddress.String) > 0)		   {             struct in_addr addr;             addr.s_addr = inet_addr(pIPAddr->IpAddress.String);             // append unique only             if (find_server(channel->servers, channel->nservers, addr) >= 0)                continue;             // printf( "ARES: %s\n", pIPAddr ->IpAddress.String );#ifdef USE_IPV6			 			 channel->servers[ channel->nservers ].family = AF_INET;#endif	         channel->servers[channel->nservers].addr = addr;             if ((channel->flags & ARES_FLAG_TRY_NEXT_SERVER_ON_RCODE3))             {               get_physical_address(channel->servers[channel->nservers].physical_addr,                                    MAX_ADAPTER_ADDRESS_LENGTH,                                    &channel->servers[channel->nservers].physical_addr_len,                                    addr);             }			 channel->nservers++;             pIPAddr = pIPAddr ->Next;           }           //printf("ARES: got all %d nameservers\n",num);        }        else        {  		   /* If no specified servers, try a local named. */		   channel->servers = malloc(sizeof(struct server_state));		   if (!channel->servers)              return ARES_ENOMEM;           memset(channel->servers, '\0', sizeof(struct server_state));#ifdef USE_IPV6			            channel->servers[0].family = AF_INET;#endif		   channel->servers[0].addr.s_addr = htonl(INADDR_LOOPBACK);		   channel->nservers = 1;        }        GlobalFree( FixedInfo );	    FreeLibrary(hLib);	  }#elif defined(__APPLE__) || defined(__MACH__)      init_by_defaults_systemconfiguration(channel);#else		/* If nobody specified servers, try a local named. */		channel->servers = malloc(sizeof(struct server_state));		if (!channel->servers)			return ARES_ENOMEM;        memset(channel->servers, '\0', sizeof(struct server_state));		// need a way to test here if v4 or v6 is running		// if v4 is running...		channel->servers[0].addr.s_addr = htonl(INADDR_LOOPBACK);		// if v6 is running...        //	channel->servers[0].addr6.s_addr = htonl6(IN6ADDR_LOOPBACK_INIT);		// hard to decide if there is one server or two here		channel->nservers = 1;#endif        }  if (channel->ndomains == -1)    {      /* Derive a default domain search list from the kernel hostname,       * or set it to empty if the hostname isn't helpful.       */      if (gethostname(hostname, sizeof(hostname)) == -1	  || !strchr(hostname, '.'))	{	  channel->domains = 0; // malloc(0);	  channel->ndomains = 0;	}      else	{	  channel->domains = malloc(sizeof(char *));	  if (!channel->domains)	    return ARES_ENOMEM;	  channel->ndomains = 0;	  channel->domains[0] = strdup(strchr(hostname, '.') + 1);	  if (!channel->domains[0])	    return ARES_ENOMEM;	  channel->ndomains = 1;	}    }  if (channel->nsort == -1)    {      channel->sortlist = NULL;      channel->nsort = 0;    }  if (!channel->lookups)    {      channel->lookups = strdup("bf");      if (!channel->lookups)	return ARES_ENOMEM;    }  return ARES_SUCCESS;}static int config_domain(ares_channel channel, char *str){  char *q;  /* Set a single search domain. */  q = str;  while (*q && !isspace((unsigned char)*q))    q++;  *q = 0;  return set_search(channel, str);}static int config_lookup(ares_channel channel, const char *str){  char lookups[3], *l;  const char *p;  /* Set the lookup order.  Only the first letter of each work   * is relevant, and it has to be "b" for DNS or "f" for the   * host file.  Ignore everything else.   */  l = lookups;  p = str;  while (*p)    {      if ((*p == 'b' || *p == 'f') && l < lookups + 2)	*l++ = *p;      while (*p && !isspace((unsigned char)*p))	p++;      while (isspace((unsigned char)*p))	p++;    }  *l = 0;  channel->lookups = strdup(lookups);  return (channel->lookups) ? ARES_SUCCESS : ARES_ENOMEM;}static int config_nameserver(struct server_state **servers, int *nservers,			     const char *str){  struct in_addr addr;  struct server_state *newserv;#ifdef USE_IPV6  u_int8_t family;  struct in6_addr addr6;  /* Add a nameserver entry, if this is a valid address. */  if (inet_pton4(str, (u_char *) & addr))   /* is it an IPv4 address? */	family = AF_INET;  else  { 	if (inet_pton6(str, (u_char *) & addr6))  /* how about an IPv6 address? */	  family = AF_INET6;	else		  return ARES_SUCCESS;	/* nope, it was garbage, return early */  }#else  /* Add a nameserver entry, if this is a valid address. */  if (!inet_pton4(str, (u_char *) & addr))   /* is it an IPv4 address? */	  return ARES_SUCCESS;	/* nope, it was garbage, return early */#endif  newserv = realloc(*servers, (*nservers + 1) * sizeof(struct server_state));  if (!newserv)    return ARES_ENOMEM;  memset(&newserv[*nservers], '\0', sizeof(struct server_state));   // clear *new* memory only#ifdef USE_IPV6  newserv[*nservers].family = family;  if (family == AF_INET6)    newserv[*nservers].addr6 = addr6;    else  #endif	newserv[*nservers].addr = addr;  *servers = newserv;  (*nservers)++;  return ARES_SUCCESS;}static int config_sortlist(struct apattern **sortlist, int *nsort,			   const char *str){  struct apattern pat, *newsort;  const char *q;  /* Add sortlist entries. */  while (*str && *str != ';')    {      q = str;      while (*q && *q != '/' && *q != ';' && !isspace((unsigned char)*q))	q++;      if (ip_addr(str, q - str, &pat.addr) == 0)	{	  /* We have a pattern address; now determine the mask. */	  if (*q == '/')	    {	      str = q + 1;	      while (*q && *q != ';' && !isspace((unsigned char)*q))		q++;	      if (ip_addr(str, q - str, &pat.mask) != 0)		natural_mask(&pat);	    }	  else	    natural_mask(&pat);	  /* Add this pattern to our list. */	  newsort = realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern));	  if (!newsort)	    return ARES_ENOMEM;	  newsort[*nsort] = pat;	  *sortlist = newsort;	  (*nsort)++;	}      else	{	  while (*q && *q != ';' && !isspace((unsigned char)*q))	    q++;	}      str = q;      while (isspace((unsigned char)*str))	str++;    }  return ARES_SUCCESS;}static int set_search(ares_channel channel, const char *str){  int n;  const char *p, *q;  /* Count the domains given. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -