static int dns_module_init(noit_module_t *self) { const struct dns_nameval *nv; struct dns_ctx *pctx; int i; pthread_mutex_init(&dns_ctx_store_lock, NULL); pthread_mutex_init(&active_events_lock, NULL); /* HASH the rr types */ for(i=0, nv = dns_type_index(i); nv->name; nv = dns_type_index(++i)) noit_hash_store(&dns_rtypes, nv->name, strlen(nv->name), (void *)nv); /* HASH the class types */ for(i=0, nv = dns_class_index(i); nv->name; nv = dns_class_index(++i)) noit_hash_store(&dns_ctypes, nv->name, strlen(nv->name), (void *)nv); noit_check_interpolate_register_oper_fn("inaddrarpa", dns_interpolate_inaddr_arpa); noit_check_interpolate_register_oper_fn("reverseip", dns_interpolate_reverse_ip); if (dns_init(NULL, 0) < 0 || (pctx = dns_new(NULL)) == NULL) { noitL(nlerr, "Unable to initialize dns subsystem\n"); return -1; } dns_free(pctx); if(dns_ctx_alloc(NULL, 0) == NULL) { noitL(nlerr, "Error setting up default dns resolver context.\n"); return -1; } return 0; }
/****************************************************************************** * DnsQuery_W [DNSAPI.@] * */ DNS_STATUS WINAPI DnsQuery_W( PCWSTR name, WORD type, DWORD options, PVOID servers, PDNS_RECORDW *result, PVOID *reserved ) { char *nameU; DNS_RECORDA *resultA; DNS_STATUS status; TRACE( "(%s,%s,0x%08x,%p,%p,%p)\n", debugstr_w(name), dns_type_to_str( type ), options, servers, result, reserved ); if (!name || !result) return ERROR_INVALID_PARAMETER; nameU = dns_strdup_wu( name ); if (!nameU) return ERROR_NOT_ENOUGH_MEMORY; status = DnsQuery_UTF8( nameU, type, options, servers, &resultA, reserved ); if (status == ERROR_SUCCESS) { *result = (DNS_RECORDW *)DnsRecordSetCopyEx( (DNS_RECORD *)resultA, DnsCharSetUtf8, DnsCharSetUnicode ); if (!*result) status = ERROR_NOT_ENOUGH_MEMORY; DnsRecordListFree( (DNS_RECORD *)resultA, DnsFreeRecordList ); } dns_free( nameU ); return status; }
static void dns_ctx_handle_free(void *vh) { dns_ctx_handle_t *h = vh; assert(h->timeout == NULL); free(h->ns); dns_close(h->ctx); dns_free(h->ctx); }
void noit_lua_init_dns() { int i; const struct dns_nameval *nv; struct dns_ctx *pctx; /* HASH the rr types */ for(i=0, nv = dns_type_index(i); nv->name; nv = dns_type_index(++i)) noit_hash_store(&dns_rtypes, nv->name, strlen(nv->name), (void *)nv); /* HASH the class types */ for(i=0, nv = dns_class_index(i); nv->name; nv = dns_class_index(++i)) noit_hash_store(&dns_ctypes, nv->name, strlen(nv->name), (void *)nv); eventer_name_callback("lua/dns_eventer", noit_lua_dns_eventer); eventer_name_callback("lua/dns_timeouts", noit_lua_dns_timeouts); if (dns_init(NULL, 0) < 0 || (pctx = dns_new(NULL)) == NULL) { noitL(noit_error, "Unable to initialize dns subsystem\n"); } else dns_free(pctx); }
/****************************************************************************** * DnsQuery_A [DNSAPI.@] * */ DNS_STATUS WINAPI DnsQuery_A( PCSTR name, WORD type, DWORD options, PVOID servers, PDNS_RECORDA *result, PVOID *reserved ) { WCHAR *nameW; DNS_RECORDW *resultW; DNS_STATUS status; TRACE( "(%s,%s,0x%08x,%p,%p,%p)\n", debugstr_a(name), dns_type_to_str( type ), options, servers, result, reserved ); if (!name || !result) return ERROR_INVALID_PARAMETER; nameW = dns_strdup_aw( name ); if (!nameW) return ERROR_NOT_ENOUGH_MEMORY; status = DnsQuery_W( nameW, type, options, servers, &resultW, reserved ); if (status == ERROR_SUCCESS) { *result = (DNS_RECORDA *)DnsRecordSetCopyEx( (DNS_RECORD *)resultW, DnsCharSetUnicode, DnsCharSetAnsi ); if (!*result) status = ERROR_NOT_ENOUGH_MEMORY; DnsRecordListFree( (DNS_RECORD *)resultW, DnsFreeRecordList ); } dns_free( nameW ); return status; }
/****************************************************************************** * DnsNameCompare_A [DNSAPI.@] * */ BOOL WINAPI DnsNameCompare_A( LPSTR name1, LPSTR name2 ) { BOOL ret; LPWSTR name1W, name2W; TRACE( "(%s,%s)\n", debugstr_a(name1), debugstr_a(name2) ); name1W = dns_strdup_aw( name1 ); name2W = dns_strdup_aw( name2 ); ret = DnsNameCompare_W( name1W, name2W ); dns_free( name1W ); dns_free( name2W ); return ret; }
static void Resolver_dealloc(Resolver *self) { if (NULL != self->ctx) { dns_free(self->ctx); self->ctx = NULL; } self->ob_type->tp_free((PyObject*)self); }
unsigned char *dns_constructpacket (struct dnspkt *pkt, int *l) { static unsigned char *buf, *ptr; int len, *offsets, qdcount, i; struct rr *list; qdcount = _get_listlen(pkt->query); len = dns_getpktsize(pkt); ptr = buf = malloc(len); memset(buf, 0, len); if (len > 512) syslog(LOG_WARNING, "WARNING: Constructed non-conform DNS-packet (size: %d)\n", len); offsets = alloca(qdcount * 4); /* Header */ buf[0] = pkt->id / 256; buf[1] = pkt->id % 256; if (pkt->type == DNS_RESPONSE) { buf[2] = 0x84; /* Flags: Response, Authoritative Answer */ buf[3] = 0x80; /* Flag: Recursion available */ } else buf[2] = 0x01; /* Flags: Recursion desired */ buf[5] = qdcount; buf[7] = _get_listlen(pkt->answer); ptr += 12; /* Query section */ for (list = pkt->query, i=0; list; list = list->next, i++) { offsets[i] = ptr-buf; memcpy(ptr, list->data, list->len); ptr += list->len; ptr[1] = 16; ptr[3] = 1; ptr += 4; } /* Answer section */ for (list = pkt->answer; list; list = list->next) { ptr[0] = 0xc0 | (offsets[list->link]/256); ptr[1] = offsets[list->link]%256; ptr[3] = 16; ptr[5] = 1; ptr[10] = list->len / 256; ptr[11] = list->len % 256; ptr += 12; memcpy(ptr, list->data, list->len); ptr += list->len; } *l = len; dns_free (pkt); return buf; }
/****************************************************************************** * DnsValidateName_UTF8 [DNSAPI.@] * */ DNS_STATUS WINAPI DnsValidateName_UTF8( LPCSTR name, DNS_NAME_FORMAT format ) { LPWSTR nameW; DNS_STATUS ret; TRACE( "(%s, %d)\n", debugstr_a(name), format ); nameW = dns_strdup_uw( name ); ret = DnsValidateName_W( nameW, format ); dns_free( nameW ); return ret; }
static DNS_STATUS dns_copy_record( ns_msg msg, ns_sect section, unsigned short num, DNS_RECORDA **recp ) { DNS_STATUS ret; DNS_RECORDA *record; WORD dlen; ns_rr rr; if (dns_ns_parserr( &msg, section, num, &rr ) < 0) return DNS_ERROR_BAD_PACKET; if (!(record = dns_zero_alloc( dns_get_record_size( &rr ) ))) return ERROR_NOT_ENOUGH_MEMORY; record->pName = dns_strdup_u( rr.name ); if (!record->pName) { dns_free( record ); return ERROR_NOT_ENOUGH_MEMORY; } record->wType = rr.type; record->Flags.S.Section = section; record->Flags.S.CharSet = DnsCharSetUtf8; record->dwTtl = rr.ttl; if ((ret = dns_copy_rdata( msg, &rr, record, &dlen ))) { dns_free( record->pName ); dns_free( record ); return ret; } record->wDataLength = dlen; *recp = record; TRACE( "found %s record in %s section\n", dns_type_to_str( rr.type ), dns_section_to_str( section ) ); return ERROR_SUCCESS; }
static int dns_module_init(noit_module_t *self) { const struct dns_nameval *nv; struct dns_ctx *pctx; int i; const char *config_val; dns_mod_config_t *conf; conf = noit_module_get_userdata(self); pthread_mutex_init(&dns_ctx_store_lock, NULL); pthread_mutex_init(&active_events_lock, NULL); conf->contexts = DEFAULT_MAX_CONTEXTS; if(noit_hash_retr_str(conf->options, "contexts", strlen("contexts"), (const char**)&config_val)) { conf->contexts = atoi(config_val); if (conf->contexts <= 0) conf->contexts = DEFAULT_MAX_CONTEXTS; } /* HASH the rr types */ for(i=0, nv = dns_type_index(i); nv->name; nv = dns_type_index(++i)) noit_hash_store(&dns_rtypes, nv->name, strlen(nv->name), (void *)nv); /* HASH the class types */ for(i=0, nv = dns_class_index(i); nv->name; nv = dns_class_index(++i)) noit_hash_store(&dns_ctypes, nv->name, strlen(nv->name), (void *)nv); noit_check_interpolate_register_oper_fn("inaddrarpa", dns_interpolate_inaddr_arpa); noit_check_interpolate_register_oper_fn("reverseip", dns_interpolate_reverse_ip); if (dns_init(NULL, 0) < 0 || (pctx = dns_new(NULL)) == NULL) { noitL(nlerr, "Unable to initialize dns subsystem\n"); return -1; } dns_free(pctx); if(dns_module_dns_ctx_alloc(self, NULL, 0) == NULL) { noitL(nlerr, "Error setting up default dns resolver context.\n"); return -1; } register_console_dns_commands(); return 0; }
static void dns_ctx_handle_free(void *vh) { dns_ctx_handle_t *h = vh; free(h->ns); eventer_remove_fd(h->e->fd); eventer_free(h->e); h->e = NULL; if(h->timeout) { eventer_remove(h->timeout); eventer_free(h->timeout); h->timeout = NULL; } dns_close(h->ctx); dns_free(h->ctx); assert(h->timeout == NULL); free(h); }
gint slave_free(Slave* slave) { MAGIC_ASSERT(slave); gint returnCode = (slave->numPluginErrors > 0) ? -1 : 0; /* this launches delete on all the plugins and should be called before * the engine is marked "killed" and workers are destroyed. */ g_hash_table_destroy(slave->hosts); /* we will never execute inside the plugin again */ slave->forceShadowContext = TRUE; if(slave->topology) { topology_free(slave->topology); } if(slave->dns) { dns_free(slave->dns); } g_hash_table_destroy(slave->programs); g_mutex_clear(&(slave->lock)); g_mutex_clear(&(slave->pluginInitLock)); /* join and free spawned worker threads */ //TODO if(slave->cwdPath) { g_free(slave->cwdPath); } if(slave->dataPath) { g_free(slave->dataPath); } if(slave->hostsPath) { g_free(slave->hostsPath); } /* free main worker */ worker_free(slave->mainThreadWorker); MAGIC_CLEAR(slave); g_free(slave); return returnCode; }
void master_free(Master* master) { MAGIC_ASSERT(master); if(master->topology) { topology_free(master->topology); } if(master->dns) { dns_free(master->dns); } if(master->config) { configuration_free(master->config); } if(master->random) { random_free(master->random); } MAGIC_CLEAR(master); g_free(master); message("simulation master destroyed"); }
//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()
struct dnspkt * dns_extractpkt(const unsigned char *buf, int len) { u_int16_t qdcount, ancount, remain, *offsets, i, j, off; const unsigned char *ptr; struct dnspkt *pkt; struct rr *rrp; struct ns_answer_header *nsh; if (len < 17) return NULL; pkt = dns_alloc(); struct ns_transaction_header *nsth= (struct ns_transaction_header*)buf; pkt->id = ntohs(nsth->tid); qdcount = ntohs(nsth->questions); ancount = ntohs(nsth->answers); offsets = malloc(qdcount * 4); ptr = buf + sizeof(struct ns_transaction_header); remain = len - sizeof(struct ns_transaction_header); i = 0; while (qdcount--) { offsets[i++] = ptr - buf; rrp = _new_listitem(&pkt->query); rrp->data = decompress_label((char*)buf, len, (char*)ptr); if (!rrp->data) { syslog(LOG_ERR, "dns_extractpkt: decompress_label choked in qd\n"); free(offsets); dns_free(pkt); return NULL; } rrp->len = strlen(rrp->data)+1; ptr = _skip_lbl(ptr, &remain); if (!ptr) { syslog(LOG_ERR, "dns_extractpkt: _skip_lbl choked in qd\n"); free(offsets); dns_free(pkt); return NULL; } ptr += 4; remain -= 4; } while (ancount--) { rrp = _new_listitem(&pkt->answer); rrp->link = -1; if ((ptr[0] & 0xc0) == 0xc0) { off = (ptr[0] & ~(0xc0)) * 256 + ptr[1]; for (j = 0; j < i; j++) if (offsets[j] == off) break; if (j < i) rrp->link = j; } ptr=_skip_lbl(ptr,&remain); nsh=(struct ns_answer_header *)ptr; ptr+=sizeof(struct ns_answer_header); remain-=sizeof(struct ns_answer_header); //printf("REMAIN=%u, datalen=%u, type= 0x%02x, sizeof ns_answer_header=%u\n",remain,ntohs(nsh->datalen), ntohs(nsh->type),sizeof(struct ns_answer_header)); if (ntohs(nsh->type) != NSTYPE_TXT){ ptr+=ntohs(nsh->datalen); remain-=ntohs(nsh->datalen); continue; } rrp->len = ntohs(nsh->datalen); rrp->data = malloc(rrp->len); memcpy(rrp->data, ptr,rrp->len); remain -= rrp->len; ptr += rrp->len; } return(pkt); }
//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)
static dns_ctx_handle_t *dns_module_dns_ctx_alloc(noit_module_t *self, const char *ns, int port) { void *vh; char *hk = NULL; dns_mod_config_t *conf = noit_module_get_userdata(self); int randkey = random() % conf->contexts; dns_ctx_handle_t *h = NULL; if(ns && *ns == '\0') ns = NULL; pthread_mutex_lock(&dns_ctx_store_lock); if(ns == NULL && default_ctx_handle != NULL) { /* special case -- default context */ h = default_ctx_handle; dns_module_dns_ctx_acquire(h); goto bail; } if (ns != NULL) { int len = snprintf(NULL, 0, "%s:%d:%d", ns, port, randkey); hk = (char *)malloc(len+1); snprintf(hk, len+1, "%s:%d:%d", ns, port, randkey); } if(ns && noit_hash_retrieve(&dns_ctx_store, hk, strlen(hk), &vh)) { h = (dns_ctx_handle_t *)vh; dns_module_dns_ctx_acquire(h); free(hk); } else { int failed = 0; h = calloc(1, sizeof(*h)); h->ns = ns ? strdup(ns) : NULL; h->ctx = dns_new(NULL); if(dns_init(h->ctx, 0) != 0) { noitL(nlerr, "dns_init failed\n"); failed++; } dns_set_dbgfn(h->ctx, dns_debug_wrap); if(ns) { if(dns_add_serv(h->ctx, NULL) < 0) { noitL(nlerr, "dns_add_serv(NULL) failed\n"); failed++; } if(dns_add_serv(h->ctx, ns) < 0) { noitL(nlerr, "dns_add_serv(%s) failed\n", ns); failed++; } } if(port && port != DNS_PORT) { dns_set_opt(h->ctx, DNS_OPT_PORT, port); } if(dns_open(h->ctx) < 0) { noitL(nlerr, "dns_open failed\n"); failed++; } if(failed) { free(h->ns); dns_free(h->ctx); free(h); free(hk); h = NULL; goto bail; } h->hkey = hk; dns_set_tmcbck(h->ctx, dns_module_eventer_dns_utm_fn, h); h->e = eventer_alloc(); h->e->mask = EVENTER_READ | EVENTER_EXCEPTION; h->e->closure = h; h->e->callback = dns_module_eventer_callback; h->e->fd = dns_sock(h->ctx); eventer_add(h->e); h->refcnt = 1; if(!ns) default_ctx_handle = h; else noit_hash_store(&dns_ctx_store, h->hkey, strlen(h->hkey), h); } bail: pthread_mutex_unlock(&dns_ctx_store_lock); return h; }
static DNS_STATUS dns_copy_rdata( ns_msg msg, const ns_rr *rr, DNS_RECORDA *r, WORD *dlen ) { DNS_STATUS ret = ERROR_SUCCESS; const unsigned char *pos = rr->rdata; unsigned int i, size; switch (rr->type) { case ns_t_a: { r->Data.A.IpAddress = *(const DWORD *)pos; *dlen = sizeof(DNS_A_DATA); break; } case ns_t_aaaa: { for (i = 0; i < sizeof(IP6_ADDRESS)/sizeof(DWORD); i++) { r->Data.AAAA.Ip6Address.IP6Dword[i] = *(const DWORD *)pos; pos += sizeof(DWORD); } *dlen = sizeof(DNS_AAAA_DATA); break; } case ns_t_key: { /* FIXME: byte order? */ r->Data.KEY.wFlags = *(const WORD *)pos; pos += sizeof(WORD); r->Data.KEY.chProtocol = *(const BYTE *)pos++; r->Data.KEY.chAlgorithm = *(const BYTE *)pos++; size = rr->rdata + rr->rdlength - pos; for (i = 0; i < size; i++) r->Data.KEY.Key[i] = *(const BYTE *)pos++; *dlen = sizeof(DNS_KEY_DATA) + (size - 1) * sizeof(BYTE); break; } case ns_t_rp: case ns_t_minfo: { r->Data.MINFO.pNameMailbox = dns_dname_from_msg( msg, pos ); if (!r->Data.MINFO.pNameMailbox) return ERROR_NOT_ENOUGH_MEMORY; if (dns_ns_name_skip( &pos, ns_msg_end( msg ) ) < 0) return DNS_ERROR_BAD_PACKET; r->Data.MINFO.pNameErrorsMailbox = dns_dname_from_msg( msg, pos ); if (!r->Data.MINFO.pNameErrorsMailbox) { dns_free( r->Data.MINFO.pNameMailbox ); return ERROR_NOT_ENOUGH_MEMORY; } *dlen = sizeof(DNS_MINFO_DATAA); break; } case ns_t_afsdb: case ns_t_rt: case ns_t_mx: { r->Data.MX.wPreference = ntohs( *(const WORD *)pos ); r->Data.MX.pNameExchange = dns_dname_from_msg( msg, pos + sizeof(WORD) ); if (!r->Data.MX.pNameExchange) return ERROR_NOT_ENOUGH_MEMORY; *dlen = sizeof(DNS_MX_DATAA); break; } case ns_t_null: { r->Data.Null.dwByteCount = rr->rdlength; memcpy( r->Data.Null.Data, rr->rdata, rr->rdlength ); *dlen = sizeof(DNS_NULL_DATA) + rr->rdlength - 1; break; } case ns_t_cname: case ns_t_ns: case ns_t_mb: case ns_t_md: case ns_t_mf: case ns_t_mg: case ns_t_mr: case ns_t_ptr: { r->Data.PTR.pNameHost = dns_dname_from_msg( msg, pos ); if (!r->Data.PTR.pNameHost) return ERROR_NOT_ENOUGH_MEMORY; *dlen = sizeof(DNS_PTR_DATAA); break; } case ns_t_sig: { r->Data.SIG.pNameSigner = dns_dname_from_msg( msg, pos ); if (!r->Data.SIG.pNameSigner) return ERROR_NOT_ENOUGH_MEMORY; if (dns_ns_name_skip( &pos, ns_msg_end( msg ) ) < 0) return DNS_ERROR_BAD_PACKET; /* FIXME: byte order? */ r->Data.SIG.wTypeCovered = *(const WORD *)pos; pos += sizeof(WORD); r->Data.SIG.chAlgorithm = *(const BYTE *)pos++; r->Data.SIG.chLabelCount = *(const BYTE *)pos++; r->Data.SIG.dwOriginalTtl = *(const DWORD *)pos; pos += sizeof(DWORD); r->Data.SIG.dwExpiration = *(const DWORD *)pos; pos += sizeof(DWORD); r->Data.SIG.dwTimeSigned = *(const DWORD *)pos; pos += sizeof(DWORD); r->Data.SIG.wKeyTag = *(const WORD *)pos; size = rr->rdata + rr->rdlength - pos; for (i = 0; i < size; i++) r->Data.SIG.Signature[i] = *(const BYTE *)pos++; *dlen = sizeof(DNS_SIG_DATAA) + (size - 1) * sizeof(BYTE); break; } case ns_t_soa: { r->Data.SOA.pNamePrimaryServer = dns_dname_from_msg( msg, pos ); if (!r->Data.SOA.pNamePrimaryServer) return ERROR_NOT_ENOUGH_MEMORY; if (dns_ns_name_skip( &pos, ns_msg_end( msg ) ) < 0) return DNS_ERROR_BAD_PACKET; r->Data.SOA.pNameAdministrator = dns_dname_from_msg( msg, pos ); if (!r->Data.SOA.pNameAdministrator) { dns_free( r->Data.SOA.pNamePrimaryServer ); return ERROR_NOT_ENOUGH_MEMORY; } if (dns_ns_name_skip( &pos, ns_msg_end( msg ) ) < 0) return DNS_ERROR_BAD_PACKET; r->Data.SOA.dwSerialNo = ntohl( *(const DWORD *)pos ); pos += sizeof(DWORD); r->Data.SOA.dwRefresh = ntohl( *(const DWORD *)pos ); pos += sizeof(DWORD); r->Data.SOA.dwRetry = ntohl( *(const DWORD *)pos ); pos += sizeof(DWORD); r->Data.SOA.dwExpire = ntohl( *(const DWORD *)pos ); pos += sizeof(DWORD); r->Data.SOA.dwDefaultTtl = ntohl( *(const DWORD *)pos ); pos += sizeof(DWORD); *dlen = sizeof(DNS_SOA_DATAA); break; } case ns_t_srv: { r->Data.SRV.wPriority = ntohs( *(const WORD *)pos ); pos += sizeof(WORD); r->Data.SRV.wWeight = ntohs( *(const WORD *)pos ); pos += sizeof(WORD); r->Data.SRV.wPort = ntohs( *(const WORD *)pos ); pos += sizeof(WORD); r->Data.SRV.pNameTarget = dns_dname_from_msg( msg, pos ); if (!r->Data.SRV.pNameTarget) return ERROR_NOT_ENOUGH_MEMORY; *dlen = sizeof(DNS_SRV_DATAA); break; } case ns_t_hinfo: case ns_t_isdn: case ns_t_x25: case ns_t_txt: { i = 0; while (pos[0] && pos < rr->rdata + rr->rdlength) { r->Data.TXT.pStringArray[i] = dns_str_from_rdata( pos ); if (!r->Data.TXT.pStringArray[i]) { while (i > 0) dns_free( r->Data.TXT.pStringArray[--i] ); return ERROR_NOT_ENOUGH_MEMORY; } i++; pos += pos[0] + 1; } r->Data.TXT.dwStringCount = i; *dlen = sizeof(DNS_TXT_DATAA) + (i - 1) * sizeof(PCHAR); break; } case ns_t_atma: case ns_t_loc: case ns_t_nxt: case ns_t_tsig: case ns_t_wks: case 0x00f9: /* TKEY */ case 0xff01: /* WINS */ case 0xff02: /* WINSR */ default: FIXME( "unhandled type: %s\n", dns_type_to_str( rr->type ) ); return DNS_ERROR_RCODE_NOT_IMPLEMENTED; } return ret; }