Exemplo n.º 1
0
int dns_mx(stralloc *out,const stralloc *fqdn)
{
  if (!dns_domain_fromdot(&q,fqdn->s,fqdn->len)) return -1;
  if (dns_resolve(q,DNS_T_MX) == -1) return -1;
  if (dns_mx_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen) == -1) return -1;
  dns_transmit_free(&dns_resolve_tx);
  dns_domain_free(&q);
  return 0;
}
Exemplo n.º 2
0
/** Request the name (PTR) record for an IPv4 address. */
int dns_name4_r(struct dns_transmit* tx, struct dns_result* out, const ipv4addr *ip)
{
  char name[DNS_NAME4_DOMAIN];

  dns_name4_domain(name,ip);
  if (dns_resolve(tx,name,DNS_T_PTR) == -1) return -1;
  if (dns_name_packet(out,tx->packet,tx->packetlen) == -1) return -1;
  dns_transmit_free(tx);
  return 0;
}
Exemplo n.º 3
0
static int doit(stralloc *out,stralloc *fqdn)
{
  unsigned int i;
  char code;
  char ch;
  char ip[16];

  if (!stralloc_copys(out,"")) return -1;
  if (!stralloc_readyplus(fqdn,1)) return -1;
  fqdn->s[fqdn->len]=0;
  if ((i=ip6_scan(fqdn->s,ip))) {
    if (!fqdn->s[i]) {
      stralloc_copyb(out,ip,16);
      return 0;
    }
  }
  if ((i=ip4_scan(fqdn->s,ip+12))) {
    if (!fqdn->s[i]) {
      byte_zero(ip,10);
      ip[10]=ip[11]=0xff;
      stralloc_copyb(out,ip,16);
      return 0;
    }
  }
  code = 0;
  for (i = 0;i <= fqdn->len;++i) {
    if (i < fqdn->len)
      ch = fqdn->s[i];
    else
      ch = '.';

    if ((ch == '[') || (ch == ']')) continue;
    if (ch == '.') {
      if (!stralloc_append(out,&code)) return -1;
      code = 0;
      continue;
    }
    if ((ch >= '0') && (ch <= '9')) {
      code *= 10;
      code += ch - '0';
      continue;
    }

    if (!dns_domain_fromdot(&q,fqdn->s,fqdn->len)) return -1;
    if (dns_resolve(q,DNS_T_AAAA) == -1) return -1;
    if (dns_ip6_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen) == -1) return -1;
    dns_transmit_free(&dns_resolve_tx);
    dns_domain_free(&q);
    return 0;
  }

  out->len &= ~3;
  return 0;
}
Exemplo n.º 4
0
/*
 * @brief 发送线程
 */
void *data_send()
{
	//pthread_detach(pthread_self());
	while (1) {
		if (send_load_config() == -1)
			pthread_exit(NULL);
		if (if_ddns)
			dns_resolve(analyze_server_domainname);
		polling_files();
		free_server_set();
		sleep(30);
	}
}
Exemplo n.º 5
0
int logic_pinger_http_set_data(void)
{
  http_post_data((void*)&logic_pinger_setup, sizeof logic_pinger_setup);
#ifdef DNS_MODULE
  struct logic_pinger_setup_s *ep = eeprom_logic_pinger_setup;
  struct logic_pinger_setup_s *p =  logic_pinger_setup;
  for(int i=0; i<LOGIC_MAX_PINGER; ++i, ++ep, ++p)
    dns_resolve(ep->hostname, p->hostname);
#endif
  EEPROM_WRITE(eeprom_logic_pinger_setup, logic_pinger_setup, sizeof eeprom_logic_pinger_setup);
  logic_pinger_restart();
  http_redirect("/logic.html");
  return 0;
}
Exemplo n.º 6
0
/** Request the IPv4 address (A) records for a domain name. */
int dns_ip4_r(struct dns_transmit* tx, struct dns_result *out, const char* fqdn)
{
  char *q = 0;
  ipv4addr ip;
  const char* x;

  if ((x = ipv4_scan(fqdn, &ip)) != 0 && *x == 0) {
    if (dns_result_alloc(out, DNS_T_A, 1, 0) < 0)
      return -1;
    memcpy(out->rr.ip4, &ip, sizeof ip);
    return 0;
  }

  if (!dns_domain_fromdot(&q,fqdn,strlen(fqdn))) return -1;
  if (dns_resolve(tx,q,DNS_T_A) == -1) { free(q); return -1; }
  free(q);
  if (dns_ip4_packet(out,tx->packet,tx->packetlen) == -1)  return -1;
  dns_transmit_free(tx);
  return 0;
}
Exemplo n.º 7
0
int dns_ip4(stralloc *out,const stralloc *fqdn)
{
  unsigned int i;
  char code;
  char ch;

  if (!stralloc_copys(out,"")) return -1;
  code = 0;
  for (i = 0;i <= fqdn->len;++i) {
    if (i < fqdn->len)
      ch = fqdn->s[i];
    else
      ch = '.';

    if ((ch == '[') || (ch == ']')) continue;
    if (ch == '.') {
      if (!stralloc_append(out,&code)) return -1;
      code = 0;
      continue;
    }
    if ((ch >= '0') && (ch <= '9')) {
      code *= 10;
      code += ch - '0';
      continue;
    }

    if (!dns_domain_fromdot(&q,fqdn->s,fqdn->len)) return -1;
    if (dns_resolve(q,DNS_T_A) == -1) return -1;
    if (dns_ip4_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen) == -1) return -1;
    dns_transmit_free(&dns_resolve_tx);
    dns_domain_free(&q);
    return 0;
  }

  out->len &= ~3;
  return 0;
}
Exemplo n.º 8
0
void dns_resolve_wrapper(HURLDomain *domain,
                         HURLPath *path)
{
    int retval, i;
    char *final_qname = NULL;
    DNSRecord *record;
    DNSMessage *response = NULL;
    DNSResolverState state;
    struct timeval begin_resolve, end_resolve, exec_time;
    ElementStat *stat = (ElementStat *)path->tag;
    int nrof_addresses = 0;

    log_debug(__func__, "Resolving %s", domain->domain);
    log_debug(__func__,
              "Resolution triggered by %s%s",
              domain->domain,
              path->path);

    /* Check if domain string is actually and IP address */
    domain->addresses = calloc(1, sizeof(struct sockaddr *));
    domain->addresses[0] = calloc(1, sizeof(struct sockaddr));

    if (inet_pton(AF_INET,
                  domain->domain,
                  &((struct sockaddr_in *)domain->addresses[0])->sin_addr) == 1)
    {
        /* The string was actually an IPv4 address */
        nrof_addresses++;
        ((struct sockaddr_in *)domain->addresses[0])->sin_family = AF_INET;
    }
    else if (inet_pton(AF_INET6,
                       domain->domain,
                       &((struct sockaddr_in6 *)domain->addresses[0])->sin6_addr)
        == 1)
    {
        /* The string was actually an IPv6 address */
        nrof_addresses++;
        ((struct sockaddr_in6 *)domain->addresses[0])->sin6_family = AF_INET6;
    }
    else
    {
        /* The string was not an IPv4 or IPv6 address -- continue with DNS lookup */
        free(domain->addresses);
    }

    if (nrof_addresses > 0)
    {
        log_debug(__func__, "URL contained IP address. Skipping DNS lookup...");
        stat->no_hostname = 1;
        domain->nrof_addresses = nrof_addresses;
        domain->dns_state = DNS_STATE_RESOLVED;
        return;
    }

    /* Copy resolver state template. */
    memcpy(&state, test->dns_state_template, sizeof(DNSResolverState));

    gettimeofday(&begin_resolve, NULL);
    retval = dns_resolve(test->cache,
                         &state,
                         domain->domain,
                         test->dns_query_type,
                         &final_qname);
    gettimeofday(&end_resolve, NULL);
    log_debug(__func__, "DNS resolv retval: %d", retval);
    if (retval == DNS_OK)
    {
        domain->dns_state = DNS_STATE_RESOLVED;
        response = state.responses[state.nrof_responses - 1];
        domain->nrof_addresses = dns_count_rr(test->dns_query_type,
                                              ANSWERS,
                                              response);

        /* Allocate memory for address structures */
        if ((domain->addresses = calloc((size_t)domain->nrof_addresses,
                                        sizeof(struct sockaddr *))) == NULL)
        {
            domain->dns_state = DNS_STATE_ERROR;
            return;
        }
        for (i = 0; i < response->nrof_answers; i++)
        {
            record = response->answers[i];
            record_debug(__func__, record);
            if (record->type == A)
            {
                domain->addresses[nrof_addresses] =
                    calloc(1, sizeof(struct sockaddr));
                domain->addresses[nrof_addresses++]->sa_family = AF_INET;
                memcpy(&((struct sockaddr_in * ) domain->addresses[i])->sin_addr.s_addr,
                       record->data,
                       record->data_len);
            }
            else if (record->type == AAAA)
            {
                domain->addresses[nrof_addresses] =
                    calloc(1, sizeof(struct sockaddr));
                domain->addresses[nrof_addresses++]->sa_family = AF_INET6;
                memcpy(&((struct sockaddr_in6 * ) domain->addresses[i])->sin6_addr,
                       record->data,
                       record->data_len);
            }
        }

        /* Detect CDN provider */
        cdn_detect(stat, &state);

    }
    else
    {
        domain->dns_state = DNS_STATE_ERROR;
        gettimeofday(&stat->end_transfer, NULL);
    }

    if ((stat->dns = calloc(1, sizeof(DNSStat))) == NULL)
    {
        /* Out of memory */
        /* Free DNS resolver queries and responses. */
        dns_state_reset(&state);
        return;
    }

    /* Save return value */
    stat->dns->return_code = retval;

    /* Calculate DNS execution time. */
    timersub(&end_resolve, &begin_resolve, &exec_time);
    stat->dns->exec_time = timeval_to_msec(&exec_time);
    memcpy(&stat->dns->begin_resolve, &begin_resolve, sizeof(struct timeval));

    /* Get DNS network time. */
    stat->dns->network_time = state.stats.network_time;

    /* Save message and data counters. */
    stat->dns->msg_tx = state.stats.packet_tx;
    stat->dns->msg_rx = state.stats.packet_rx;
    stat->dns->data_tx = state.stats.data_tx;
    stat->dns->data_rx = state.stats.data_rx;

    /* Get query name and final query name */
    stat->dns->qname = strdup(domain->domain);
    stat->dns->qname_final = final_qname; /* Reusing pointer, so dont free() it! */

    /* Get number of domain names resolved in order to resolve the target domain name. */
    stat->dns->queries = state.nrof_queries;

    if (retval == 0)
    {
        /* Get number of A and AAAA records. */
        stat->dns->nrof_answers_a = dns_count_rr(A, ANSWERS, response);
        stat->dns->nrof_answers_aaaa = dns_count_rr(AAAA, ANSWERS, response);

        /* Get first A record and its TTL */
        if ((record = dns_message_find_rr(response, ANSWERS, final_qname, A))
            != NULL)
        {
            stat->dns->answer_a = dns_record_rdata_str(record);
            stat->dns->answer_a_ttl = (int)record->ttl;
        }
        else
        {
            stat->dns->answer_a = calloc(1, sizeof(char));
            stat->dns->answer_a_ttl = -1;
        }

        /* Get first AAAA record and its TTL */
        if ((record = dns_message_find_rr(response, ANSWERS, final_qname, AAAA))
            != NULL)
        {
            stat->dns->answer_aaaa = dns_record_rdata_str(record);
            stat->dns->answer_aaaa_ttl = (int)record->ttl;
        }
        else
        {
            stat->dns->answer_aaaa = calloc(1, sizeof(char));
            stat->dns->answer_aaaa_ttl = -1;
        }
    }
    else
    {
        stat->dns->answer_a = calloc(1, sizeof(char));
        stat->dns->answer_a_ttl = -1;
        stat->dns->answer_aaaa = calloc(1, sizeof(char));
        stat->dns->answer_aaaa_ttl = -1;
    }

    /* Dump DNS trace */
    if (test->stats.dns.trace)
    {
        stat->dns->trace = dns_trace_json(&state);
    }

    /* Free DNS resolver queries and responses. */
    dns_state_reset(&state);
}
Exemplo n.º 9
0
//Can be used to resolv a single PBX if uPBX!=0
void DNSUpdate(unsigned uPBX)
{
        MYSQL_RES *res;
        MYSQL_ROW field;
	unsigned uCount=0;
	unsigned uRRCount=0;

	if(!guSilent) printf("DNSUpdate(%u) start\n",uPBX);

	sprintf(gcQuery,"SELECT tContainer.cHostname,tContainer.uContainer"
			" FROM tContainer,tGroup,tGroupGlue"
			" WHERE tGroup.cLabel='Production PBXs'"
			" AND tGroup.uGroup=tGroupGlue.uGroup"
			" AND tContainer.uContainer=tGroupGlue.uContainer"
			" AND tContainer.uSource=0"
			" AND IF(%u,tContainer.uContainer=%u,1)"
				,uPBX,uPBX);
	mysql_query(&gMysql,gcQuery);
	if(mysql_errno(&gMysql))
	{
		printf(gcQuery);
		logfileLine("DNSUpdate",mysql_error(&gMysql));
		mysql_close(&gMysql);
		exit(2);
	}
        res=mysql_store_result(&gMysql);
	while((field=mysql_fetch_row(res)))
	{
		unsigned uA=0,uB=0,uC=0,uD=0;
		if(!guSilent)
			printf("%s\n",field[0]);

		//This has to be done first
		sscanf(field[1],"%u",&uPBX);

		//Handle special case where cDomain is an IPv4 number
		if(sscanf(field[0],"%u.%u.%u.%u",&uA,&uB,&uC,&uD)==4)
		{
			//Not sure if this is an exact test see RFC
			if(uA>0 && uA<256 && uB<256 && uC<256 && uD<256)
			{
				//5060,0,0 pattern it is not an A record
				//UpdatetAddressForPBX(uPBX,field[0],5060,0,0);
				UpdatetContainer(uPBX,field[0],5060,0,0);
				sprintf(gcQuery,"%s added as IP to %u\n",field[0],uPBX);
				if(!guSilent)
					printf(gcQuery);
				if(guLogLevel>0)
					logfileLine("DNSUpdate",gcQuery);
				continue;
			}
			//Matches pattern but out of range values
			logfileLine("DNSUpdate",field[0]);
		}
		uCount++;

		//DNS
		dns_host_t sDnsHost;
		dns_host_t sDnsHostSave;
		dns_host_t sDnsHost2;
		dns_host_t sDnsHostSave2;
		dns_srv_t sDnsSrv;
		char cHostname[128]={""};

		//SRV records
		unsigned uSRVCount=0;
		sprintf(cHostname,"_sip._udp.%.99s",field[0]);
		sDnsHost=dns_resolve(cHostname,3);
		if(guLogLevel>3)
			logfileLine("DNSUpdate dns_resolv",cHostname);
		sDnsHostSave=sDnsHost;
		if(sDnsHost!=NULL)
		{
			if(!guSilent)
				printf("SRV records\n");
			sDnsSrv=(dns_srv_t)sDnsHost->rr;

			//Nested get A record
			sprintf(cHostname,"%.99s",sDnsSrv->name);
			sDnsHost2=dns_resolve(cHostname,1);
			if(guLogLevel>3)
				logfileLine("DNSUpdate dns_resolv",cHostname);
			sDnsHostSave2=sDnsHost2;
			if(sDnsHost2!=NULL)
			{
				UpdatetContainer(uPBX,(char *)sDnsHost2->rr,sDnsSrv->port,sDnsSrv->priority,sDnsSrv->weight);
				uSRVCount++;
				if(guLogLevel>3)
					logfileLine("DNSUpdate A",(char *)sDnsHost2->rr);
				uRRCount++;
				while(sDnsHost2->next!=NULL)
				{
					sDnsHost2=sDnsHost2->next;
					UpdatetContainer(uPBX,(char *)sDnsHost2->rr,sDnsSrv->port,sDnsSrv->priority,sDnsSrv->weight);
					uSRVCount++;
					if(guLogLevel>3)
						logfileLine("DNSUpdate A",(char *)sDnsHost2->rr);
					uRRCount++;
				}
			}
			//
			if(guLogLevel>3)
				logfileLine("DNSUpdate SRV",sDnsSrv->name);
			uRRCount++;
			while(sDnsHost->next!=NULL)
			{
				sDnsHost=sDnsHost->next;
				sDnsSrv=(dns_srv_t)sDnsHost->rr;
				//Nested get A record
				sprintf(cHostname,"%.99s",sDnsSrv->name);
				sDnsHost2=dns_resolve(cHostname,1);
				sDnsHostSave2=sDnsHost2;
				if(sDnsHost2!=NULL)
				{
					UpdatetContainer(uPBX,(char *)sDnsHost2->rr,sDnsSrv->port,sDnsSrv->priority,sDnsSrv->weight);
					uSRVCount++;
					if(guLogLevel>3)
						logfileLine("DNSUpdate A",(char *)sDnsHost2->rr);
					uRRCount++;
					while(sDnsHost2->next!=NULL)
					{
						sDnsHost2=sDnsHost2->next;
						UpdatetContainer(uPBX,(char *)sDnsHost2->rr,sDnsSrv->port,sDnsSrv->priority,sDnsSrv->weight);
						uSRVCount++;
						if(guLogLevel>3)
							logfileLine("DNSUpdate A",(char *)sDnsHost2->rr);
						uRRCount++;
					}
			}
			//
				if(guLogLevel>3)
					logfileLine("DNSUpdate SRV",sDnsSrv->name);
				uRRCount++;
			}
		}
		else
		{
			if(guLogLevel>4)
				logfileLine("DNSUpdate No SRV",cHostname);
		}
		dns_free(sDnsHostSave);

		//Only if not two SRV records
		if(uSRVCount<2)
		{
			//A records
			sprintf(cHostname,"%.99s",field[0]);
			sDnsHost=dns_resolve(cHostname,1);
			sDnsHostSave=sDnsHost;
			if(sDnsHost!=NULL)
			{
				//UpdatetAddressForPBX(uPBX,(char *)sDnsHost->rr,5060,1000,100);
				UpdatetContainer(uPBX,(char *)sDnsHost2->rr,5060,0,0);
				if(guLogLevel>3)
					logfileLine("DNSUpdate A not SRV",(char *)sDnsHost->rr);
				uRRCount++;
				while(sDnsHost->next!=NULL)
				{
					sDnsHost=sDnsHost->next;
					//UpdatetAddressForPBX(uPBX,(char *)sDnsHost->rr,5060,1000,100);
					UpdatetContainer(uPBX,(char *)sDnsHost2->rr,5060,0,0);
					if(guLogLevel>3)
						logfileLine("DNSUpdate A not SRV",(char *)sDnsHost->rr);
					uRRCount++;
				}
			}
			else
			{
				if(guLogLevel>0)
					logfileLine("DNSUpdate No A not SRV",cHostname);
				if(!guSilent)
					printf("No A records not SRV\n");
			}
			dns_free(sDnsHostSave);
		}
	}
	mysql_free_result(res);

	if(guLogLevel>0)
	{
		sprintf(gcQuery,"Processed %u cHostnames updated %u tAddress records",uCount,uRRCount);
		logfileLine("DNSUpdate",gcQuery);
	}

	if(!guSilent) printf("DNSUpdate() end\n");
}//void DNSUpdate()
Exemplo n.º 10
0
void main(void)
{
  uint8_t eth_init = ETH_INIT_DEFAULT;
  uint32_t server;
  struct timespec time;

  strncpy(_tz.tzname, TIMEZONE_CODE,
          sizeof(_tz.tzname) - 1);
  _tz.timezone = TIMEZONE_SECS;

  if (doesclrscrafterexit())
  {
    atexit(confirm_exit);
  }

#ifdef __APPLE2__
  {
    int file;

    printf("\nSetting slot ");
    file = open("ethernet.slot", O_RDONLY);
    if (file != -1)
    {
      read(file, &eth_init, 1);
      close(file);
      eth_init &= ~'0';
    }
    printf("- %d\n", eth_init);
  }
#endif

  printf("\nInitializing ");
  if (ip65_init(eth_init))
  {
    error_exit();
  }

  printf("- Ok\n\nObtaining IP address ");
  if (dhcp_init())
  {
    error_exit();
  }

  printf("- Ok\n\nResolving %s ", NTP_SERVER);
  server = dns_resolve(NTP_SERVER);
  if (!server)
  {
    error_exit();
  }

  printf("- Ok\n\nGetting %s ", _tz.tzname);
  time.tv_sec = sntp_get_time(server);
  if (!time.tv_sec)
  {
    error_exit();
  }

  // Convert time from seconds since 1900 to
  // seconds since 1970 according to RFC 868
  time.tv_sec -= 2208988800UL;

  printf("- %s", ctime(&time.tv_sec));

  time.tv_nsec = 0;
  clock_settime(CLOCK_REALTIME, &time);
}
Exemplo n.º 11
0
Arquivo: dns.c Projeto: SpComb/nwprog
void dns (void *ctx)
{
    struct dns_task *task = ctx;
    const struct options *options = task->options;
    const char *arg = task->arg;

    struct dns_resolve *resolve;
    int err;

    log_info("%s", arg);

    // query
    enum dns_type types[] = { DNS_A, DNS_AAAA, DNS_MX, 0 };
    char name[DNS_NAME];

    if (str_copy(name, sizeof(name), arg)) {
        log_warning("name overflow: %s", name);
        return 1;
    }

    for (enum dns_type *type = types; *type; type++) {
        if ((err = dns_resolve(options->dns, &resolve, name, *type))) {
            log_fatal("dns_resolve: %s", arg);
            return 1;
        }

        // response
        enum dns_section section;
        struct dns_record rr;
        union dns_rdata rdata;

        while (!(err = dns_resolve_record(resolve, &section, &rr, &rdata))) {
            char buf[INET6_ADDRSTRLEN];

            if (rr.type == DNS_A) {
                inet_ntop(AF_INET, &rdata.A, buf, sizeof(buf));
            } else if (rr.type == DNS_AAAA) {
                inet_ntop(AF_INET6, &rdata.AAAA, buf, sizeof(buf));
            } else {
                buf[0] = '\0';
            }

            if (section == DNS_AN && rr.type == DNS_CNAME) {
                printf("%s is an alias for %s\n", rr.name, rdata.CNAME);

                // fix further queries
                if (str_copy(name, sizeof(name), rdata.CNAME)) {
                    log_warning("cname overflow: %s", rdata.CNAME);
                    return 1;
            }

            } else if (section == DNS_AN && rr.type == DNS_A) {
                printf("%s has address %s\n", rr.name, buf);
            } else if (section == DNS_AN && rr.type == DNS_AAAA) {
                printf("%s has IPv6 address %s\n", rr.name, buf);
            } else if (section == DNS_AN && rr.type == DNS_MX) {
                printf("%s mail is handled by %u %s\n", rr.name, rdata.MX.preference, rdata.MX.exchange);
            }
        }
    }

    return 0;
}
Exemplo n.º 12
0
//Can be used to resolv a single GW if uGateway!=0
void DNSUpdateGW(char const *cCluster,unsigned uGateway)
{
        MYSQL_RES *res;
        MYSQL_ROW field;
	unsigned uCount=0;
	unsigned uRRCount=0;

	if(!guSilent) printf("DNSUpdateGW() start\n");

	//outbound GW only non empty cHostname
	sprintf(gcQuery,"SELECT tGateway.cHostname,tGateway.uGateway"
			" FROM tGateway,tCluster"
			" WHERE tGateway.uCluster=tCluster.uCluster"
			" AND tCluster.cLabel='%s'"
			" AND tGateway.uGatewayType=2"
			" AND tGateway.cHostname!=''"
			" AND IF(%u,tGateway.uGateway=%u,1)"
				,cCluster,uGateway,uGateway);
	mysql_query(&gMysql,gcQuery);
	if(mysql_errno(&gMysql))
	{
		printf(gcQuery);
		logfileLine("DNSUpdateGW",mysql_error(&gMysql));
		mysql_close(&gMysql);
		exit(2);
	}
        res=mysql_store_result(&gMysql);
	while((field=mysql_fetch_row(res)))
	{
		unsigned uA=0,uB=0,uC=0,uD=0;
		if(!guSilent)
			printf("%s\n",field[0]);

		//This has to be done first
		sscanf(field[1],"%u",&uGateway);

		//Handle special case where cDomain is an IPv4 number
		if(sscanf(field[0],"%u.%u.%u.%u",&uA,&uB,&uC,&uD)==4)
		{
			//Not sure if this is an exact test see RFC
			if(uA>0 && uA<256 && uB<256 && uC<256 && uD<256)
			{
				//5060,0,0 pattern it is not an A record
				UpdatetAddressForGateway(uGateway,field[0],5060,0,0);
				sprintf(gcQuery,"%s added as IP to %u\n",field[0],uGateway);
				if(!guSilent)
					printf(gcQuery);
				if(guLogLevel>0)
					logfileLine("DNSUpdateGW",gcQuery);
				continue;
			}
			//Matches pattern but out of range values
			logfileLine("DNSUpdateGW",field[0]);
		}
		uCount++;

		//DNS
		dns_host_t sDnsHost;
		dns_host_t sDnsHostSave;
		dns_host_t sDnsHost2;
		dns_host_t sDnsHostSave2;
		dns_srv_t sDnsSrv;
		char cHostname[128]={""};

		//SRV records
		sprintf(cHostname,"_sip._udp.%.99s",field[0]);
		sDnsHost=dns_resolve(cHostname,3);
		sDnsHostSave=sDnsHost;
		if(sDnsHost!=NULL)
		{
			if(!guSilent)
				printf("SRV records\n");
			sDnsSrv=(dns_srv_t)sDnsHost->rr;

			//Nested get A record
			sprintf(cHostname,"%.99s",sDnsSrv->name);
			sDnsHost2=dns_resolve(cHostname,1);
			sDnsHostSave2=sDnsHost2;
			if(sDnsHost2!=NULL)
			{
				UpdatetAddressForGateway(uGateway,(char *)sDnsHost2->rr,sDnsSrv->port,sDnsSrv->priority,sDnsSrv->weight);
				if(guLogLevel>3)
					logfileLine("DNSUpdateGW A",(char *)sDnsHost2->rr);
				uRRCount++;
				while(sDnsHost2->next!=NULL)
				{
					sDnsHost2=sDnsHost2->next;
					UpdatetAddressForGateway(uGateway,(char *)sDnsHost2->rr,sDnsSrv->port,sDnsSrv->priority,sDnsSrv->weight);
					if(guLogLevel>3)
						logfileLine("DNSUpdateGW A",(char *)sDnsHost2->rr);
					uRRCount++;
				}
			}
			//
			if(guLogLevel>3)
				logfileLine("DNSUpdateGW SRV",sDnsSrv->name);
			uRRCount++;
			while(sDnsHost->next!=NULL)
			{
				sDnsHost=sDnsHost->next;
				sDnsSrv=(dns_srv_t)sDnsHost->rr;
				//Nested get A record
				sprintf(cHostname,"%.99s",sDnsSrv->name);
				sDnsHost2=dns_resolve(cHostname,1);
				sDnsHostSave2=sDnsHost2;
				if(sDnsHost2!=NULL)
				{
					UpdatetAddressForGateway(uGateway,(char *)sDnsHost2->rr,sDnsSrv->port,sDnsSrv->priority,sDnsSrv->weight);
					if(guLogLevel>3)
						logfileLine("DNSUpdateGW A",(char *)sDnsHost2->rr);
					uRRCount++;
					while(sDnsHost2->next!=NULL)
					{
						sDnsHost2=sDnsHost2->next;
						UpdatetAddressForGateway(uGateway,(char *)sDnsHost2->rr,sDnsSrv->port,sDnsSrv->priority,sDnsSrv->weight);
						if(guLogLevel>3)
							logfileLine("DNSUpdateGW A",(char *)sDnsHost2->rr);
						uRRCount++;
					}
			}
			//
				if(guLogLevel>3)
					logfileLine("DNSUpdateGW SRV",sDnsSrv->name);
				uRRCount++;
			}
		}
		else
		{
			if(guLogLevel>4)
				logfileLine("DNSUpdateGW No SRV",cHostname);
		}
		dns_free(sDnsHostSave);

		//A records
		sprintf(cHostname,"%.99s",field[0]);
		sDnsHost=dns_resolve(cHostname,1);
		sDnsHostSave=sDnsHost;
		if(sDnsHost!=NULL)
		{
			UpdatetAddressForGateway(uGateway,(char *)sDnsHost->rr,5060,1000,100);
			if(guLogLevel>3)
				logfileLine("DNSUpdateGW A",(char *)sDnsHost->rr);
			uRRCount++;
			while(sDnsHost->next!=NULL)
			{
				sDnsHost=sDnsHost->next;
				UpdatetAddressForGateway(uGateway,(char *)sDnsHost->rr,5060,1000,100);
				if(guLogLevel>3)
					logfileLine("DNSUpdateGW A",(char *)sDnsHost->rr);
				uRRCount++;
			}
		}
		else
		{
			if(guLogLevel>0)
				logfileLine("DNSUpdateGW No A",cHostname);
			if(!guSilent)
				printf("No A records\n");
		}
		dns_free(sDnsHostSave);

		//Remove non updated in this pass records 300s 5min ago
		sprintf(gcQuery,"DELETE FROM tAddress"
				" WHERE tAddress.uGateway=%u AND uOwner=0"
				" AND ((uModDate>0 AND (uModDate<(UNIX_TIMESTAMP(NOW())-300))) OR (uModDate=0 AND uCreatedDate<(UNIX_TIMESTAMP(NOW())-300)))"
					,uGateway);
		mysql_query(&gMysql,gcQuery);
		if(mysql_errno(&gMysql))
		{
			printf(gcQuery);
			logfileLine("DNSUpdateGW",mysql_error(&gMysql));
			mysql_close(&gMysql);
			exit(2);
		}
		if(guLogLevel>0)
		{
			sprintf(gcQuery,"%lu stale tAddress records deleted",(long unsigned)mysql_affected_rows(&gMysql));
			logfileLine("DNSUpdateGW",gcQuery);
		}
	}
	mysql_free_result(res);

	if(guLogLevel>0)
	{
		sprintf(gcQuery,"Processed %u cHostnames updated %u tAddress records",uCount,uRRCount);
		logfileLine("DNSUpdateGW",gcQuery);
	}

	if(!guSilent) printf("DNSUpdateGW() end\n");
}//void DNSUpdateGW(char const *cCluster)
Exemplo n.º 13
0
static void
NS(test_main)(void *arg)
{
  (void) arg;
  int retval;
  int prev_n_send_resolved_hostname_cell_replacement;
  int prev_n_send_resolved_cell_replacement;
  int prev_n_connection_free;
  cached_resolve_t *fake_resolved = tor_malloc(sizeof(cached_resolve_t));
  edge_connection_t *exitconn = tor_malloc(sizeof(edge_connection_t));
  edge_connection_t *nextconn = tor_malloc(sizeof(edge_connection_t));

  or_circuit_t *on_circuit = tor_malloc(sizeof(or_circuit_t));
  memset(on_circuit,0,sizeof(or_circuit_t));
  on_circuit->base_.magic = OR_CIRCUIT_MAGIC;

  memset(fake_resolved,0,sizeof(cached_resolve_t));
  memset(exitconn,0,sizeof(edge_connection_t));
  memset(nextconn,0,sizeof(edge_connection_t));

  NS_MOCK(dns_resolve_impl);
  NS_MOCK(send_resolved_cell);
  NS_MOCK(send_resolved_hostname_cell);

  /*
   * CASE 1: dns_resolve_impl returns 1 and sets a hostname. purpose is
   * EXIT_PURPOSE_RESOLVE.
   *
   * We want dns_resolve() to call send_resolved_hostname_cell() for a
   * given exit connection (represented by edge_connection_t object)
   * with a hostname it received from _impl.
   */

  prev_n_send_resolved_hostname_cell_replacement =
  n_send_resolved_hostname_cell_replacement;

  exitconn->base_.purpose = EXIT_PURPOSE_RESOLVE;
  exitconn->on_circuit = &(on_circuit->base_);

  resolve_retval = 1;
  resolved_name = tor_strdup("www.torproject.org");

  retval = dns_resolve(exitconn);

  tt_int_op(retval,OP_EQ,1);
  tt_str_op(resolved_name,OP_EQ,last_resolved_hostname);
  tt_assert(conn_for_resolved_cell == exitconn);
  tt_int_op(n_send_resolved_hostname_cell_replacement,OP_EQ,
            prev_n_send_resolved_hostname_cell_replacement + 1);
  tt_assert(exitconn->on_circuit == NULL);

  tor_free(last_resolved_hostname);
  // implies last_resolved_hostname = NULL;

  /* CASE 2: dns_resolve_impl returns 1, but does not set hostname.
   * Instead, it yields cached_resolve_t object.
   *
   * We want dns_resolve to call send_resolved_cell on exitconn with
   * RESOLVED_TYPE_AUTO and the cached_resolve_t object from _impl.
   */

  tor_free(resolved_name);
  resolved_name = NULL;

  exitconn->on_circuit = &(on_circuit->base_);

  cache_entry_mock = fake_resolved;

  prev_n_send_resolved_cell_replacement =
  n_send_resolved_cell_replacement;

  retval = dns_resolve(exitconn);

  tt_int_op(retval,OP_EQ,1);
  tt_assert(conn_for_resolved_cell == exitconn);
  tt_int_op(n_send_resolved_cell_replacement,OP_EQ,
            prev_n_send_resolved_cell_replacement + 1);
  tt_assert(last_resolved == fake_resolved);
  tt_int_op(last_answer_type,OP_EQ,0xff);
  tt_assert(exitconn->on_circuit == NULL);

  /* CASE 3: The purpose of exit connection is not EXIT_PURPOSE_RESOLVE
   * and _impl returns 1.
   *
   * We want dns_resolve to prepend exitconn to n_streams linked list.
   * We don't want it to send any cells about hostname being resolved.
   */

  exitconn->base_.purpose = EXIT_PURPOSE_CONNECT;
  exitconn->on_circuit = &(on_circuit->base_);

  on_circuit->n_streams = nextconn;

  prev_n_send_resolved_cell_replacement =
  n_send_resolved_cell_replacement;

  prev_n_send_resolved_hostname_cell_replacement =
  n_send_resolved_hostname_cell_replacement;

  retval = dns_resolve(exitconn);

  tt_int_op(retval,OP_EQ,1);
  tt_assert(on_circuit->n_streams == exitconn);
  tt_assert(exitconn->next_stream == nextconn);
  tt_int_op(prev_n_send_resolved_cell_replacement,OP_EQ,
            n_send_resolved_cell_replacement);
  tt_int_op(prev_n_send_resolved_hostname_cell_replacement,OP_EQ,
            n_send_resolved_hostname_cell_replacement);

  /* CASE 4: _impl returns 0.
   *
   * We want dns_resolve() to set exitconn state to
   * EXIT_CONN_STATE_RESOLVING and prepend exitconn to resolving_streams
   * linked list.
   */

  exitconn->on_circuit = &(on_circuit->base_);

  resolve_retval = 0;

  exitconn->next_stream = NULL;
  on_circuit->resolving_streams = nextconn;

  retval = dns_resolve(exitconn);

  tt_int_op(retval,OP_EQ,0);
  tt_int_op(exitconn->base_.state,OP_EQ,EXIT_CONN_STATE_RESOLVING);
  tt_assert(on_circuit->resolving_streams == exitconn);
  tt_assert(exitconn->next_stream == nextconn);

  /* CASE 5: _impl returns -1 when purpose of exitconn is
   * EXIT_PURPOSE_RESOLVE. We want dns_resolve to call send_resolved_cell
   * on exitconn with type being RESOLVED_TYPE_ERROR.
   */

  NS_MOCK(dns_cancel_pending_resolve);
  NS_MOCK(connection_free_);

  exitconn->on_circuit = &(on_circuit->base_);
  exitconn->base_.purpose = EXIT_PURPOSE_RESOLVE;

  resolve_retval = -1;

  prev_n_send_resolved_cell_replacement =
  n_send_resolved_cell_replacement;

  prev_n_connection_free = n_connection_free;

  retval = dns_resolve(exitconn);

  tt_int_op(retval,OP_EQ,-1);
  tt_int_op(n_send_resolved_cell_replacement,OP_EQ,
            prev_n_send_resolved_cell_replacement + 1);
  tt_int_op(last_answer_type,OP_EQ,RESOLVED_TYPE_ERROR);
  tt_int_op(n_dns_cancel_pending_resolve_replacement,OP_EQ,1);
  tt_int_op(n_connection_free,OP_EQ,prev_n_connection_free + 1);
  tt_assert(last_freed_conn == TO_CONN(exitconn));

  done:
  NS_UNMOCK(dns_resolve_impl);
  NS_UNMOCK(send_resolved_cell);
  NS_UNMOCK(send_resolved_hostname_cell);
  NS_UNMOCK(dns_cancel_pending_resolve);
  NS_UNMOCK(connection_free_);
  tor_free(on_circuit);
  tor_free(exitconn);
  tor_free(nextconn);
  tor_free(resolved_name);
  tor_free(fake_resolved);
  tor_free(last_resolved_hostname);
  return;
}