static void dns_gethostbyname(void) { dns_ok = 0; evdns_resolve_ipv4("www.monkey.org", 0, dns_gethostbyname_cb, NULL); event_dispatch(); tt_int_op(dns_ok, ==, DNS_IPv4_A); test_ok = dns_ok; end: ; }
int NeubotPoller_resolve(struct NeubotPoller *poller, const char *family, const char *address, neubot_hook_vos callback, void *opaque) { struct ResolveContext *rc; int result; (void) poller; rc = calloc(1, sizeof (*rc)); if (rc == NULL) { neubot_warn("resolve: calloc() failed"); goto failure; } rc->callback = callback; rc->opaque = opaque; rc->names = evbuffer_new(); if (rc->names == NULL) { neubot_warn("resolve: evbuffer_new() failed"); goto failure; } if (strcmp(family, "PF_INET6") == 0) result = evdns_resolve_ipv6(address, DNS_QUERY_NO_SEARCH, NeubotPoller_resolve_callback_internal, rc); else if (strcmp(family, "PF_INET") == 0) result = evdns_resolve_ipv4(address, DNS_QUERY_NO_SEARCH, NeubotPoller_resolve_callback_internal, rc); else { neubot_warn("resolve: invalid family"); goto failure; } if (result != 0) { neubot_warn("resolve: evdns_resolve_ipvX() failed"); goto failure; } return (0); failure: if (rc != NULL && rc->names != NULL) evbuffer_free(rc->names); if (rc != NULL) free(rc); return (-1); }
static void dns_gethostbyname(void) { fprintf(stdout, "Simple DNS resolve: "); dns_ok = 0; evdns_resolve_ipv4("www.monkey.org", 0, dns_gethostbyname_cb, NULL); event_dispatch(); if (dns_ok == DNS_IPv4_A) { fprintf(stdout, "OK\n"); } else { fprintf(stdout, "FAILED\n"); exit(1); } }
static void doDNS( void * vtask ) { tr_address addr; int port = -1; char * host = NULL; struct hostent *he; struct sockaddr_in adr; struct tr_web_task * task = vtask; tr_dns_result lookup_result = TR_DNS_UNTESTED; assert( task->resolved_host == NULL ); if( !tr_urlParse( task->url, -1, NULL, &host, &port, NULL ) ) { task->port = port; task->host = host; /* If 'host' is an IPv4 or IPv6 address in text form, use it as-is. * Otherwise, see if its resolved name is in our DNS cache */ if( tr_pton( task->host, &addr ) != NULL ) { task->resolved_host = task->host; lookup_result = TR_DNS_OK; } else { if( (he = gethostbyname(host)) ) { memcpy(&adr.sin_addr, he->h_addr_list[0], he->h_length); task->resolved_host = strdup(inet_ntoa(adr.sin_addr)); lookup_result = TR_DNS_OK; } } } if( lookup_result != TR_DNS_UNTESTED ) { addTask( task ); } else if( !host || evdns_resolve_ipv4( host, 0, dns_ipv4_done_cb, task ) ) { dns_ipv4_done_cb( DNS_ERR_UNKNOWN, DNS_IPv4_A, 0, 0, NULL, task ); } }
void * urlparser(void *none) { Surl *url = NULL; Url *ourl = NULL; map<string, string>::const_iterator itr; //event_base * base = event_base_new(); //evdns_base * dnsbase = evdns_base_new(base, 1); //event_base_loop(base,EVLOOP_NONBLOCK); while(1) { pthread_mutex_lock(&sq_lock); while (surl_queue.empty()) { pthread_cond_wait(&sq_cond, &sq_lock); } url = surl_queue.front(); surl_queue.pop(); pthread_mutex_unlock(&sq_lock); ourl = surl2ourl(url); itr = host_ip_map.find(ourl->domain); if (itr == host_ip_map.end()) { /* not found */ /* dns resolve */ event_base * base = event_init(); evdns_init(); evdns_resolve_ipv4(ourl->domain, 0, dns_callback, ourl); event_dispatch(); event_base_free(base); //evdns_base_resolve_ipv4(dnsbase, ourl->domain, 0, dns_callback, ourl); //event_base_loop(base, EVLOOP_ONCE | EVLOOP_NONBLOCK); } else { ourl->ip = strdup(itr->second.c_str()); push_ourlqueue(ourl); } } //evdns_base_free(dnsbase, 0); //event_base_free(base); return NULL; }
int hub_connect(const char *hubname, const char *nick, const char *email, const char *description, const char *speed, bool passive, const char *password, const char *encoding) { return_val_if_fail(nick, -1); return_val_if_fail(hubname, -1); char *host = 0; int port = 0; if(split_host_port(hubname, &host, &port) != 0) { return -1; } else if(port < 0) { ui_send_status_message(NULL, hubname, "Invalid port in hub address: %s", hubname); free(host); return -1; } else if(port == 0) { port = 411; /* default port */ } ui_send_status_message(NULL, hubname, "Connecting to %s...", hubname); hub_connect_data_t *hcd = calloc(1, sizeof(hub_connect_data_t)); hcd->nick = strdup(nick); hcd->email = xstrdup(email); hcd->description = xstrdup(description); hcd->speed = xstrdup(speed); hcd->address = strdup(hubname); hcd->passive = passive; hcd->password = xstrdup(password); hcd->encoding = xstrdup(encoding); hcd->port = port; struct in_addr xaddr; if(inet_aton(host, &xaddr)) { /* host already given as an IP address */ hub_connect_async(hcd, &xaddr); free(host); } else { int rc = evdns_resolve_ipv4(host, 0, hub_lookup_event, hcd); free(host); if(rc != DNS_ERR_NONE) { WARNING("Failed to lookup '%s': %s", hubname, evdns_err_to_string(rc)); ui_send_status_message(NULL, hubname, "Failed to lookup '%s': %s", hubname, evdns_err_to_string(rc)); hcd_free(hcd); return -1; } } return 0; }
static void dns_server(void) { int sock; struct sockaddr_in my_addr; struct evdns_server_port *port; struct in_addr resolve_addr; dns_ok = 1; fprintf(stdout, "DNS server support: "); /* Add ourself as the only nameserver, and make sure we really are * the only nameserver. */ evdns_nameserver_ip_add("127.0.0.1:35353"); if (evdns_count_nameservers() != 1) { fprintf(stdout, "Couldn't set up.\n"); exit(1); } /* Now configure a nameserver port. */ sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock == -1) { perror("socket"); exit(1); } #ifdef WIN32 { u_long nonblocking = 1; ioctlsocket(sock, FIONBIO, &nonblocking); } #else fcntl(sock, F_SETFL, O_NONBLOCK); #endif memset(&my_addr, 0, sizeof(my_addr)); my_addr.sin_family = AF_INET; my_addr.sin_port = htons(35353); my_addr.sin_addr.s_addr = htonl(0x7f000001UL); if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) { perror("bind"); exit (1); } port = evdns_add_server_port(sock, 0, dns_server_request_cb, NULL); /* Send two queries. */ evdns_resolve_ipv4("zz.example.com", DNS_QUERY_NO_SEARCH, dns_server_gethostbyname_cb, NULL); evdns_resolve_ipv6("zz.example.com", DNS_QUERY_NO_SEARCH, dns_server_gethostbyname_cb, NULL); resolve_addr.s_addr = htonl(0xc0a80b0bUL); /* 192.168.11.11 */ evdns_resolve_reverse(&resolve_addr, 0, dns_server_gethostbyname_cb, NULL); event_dispatch(); if (dns_ok) { fprintf(stdout, "OK\n"); } else { fprintf(stdout, "FAILED\n"); exit(1); } evdns_close_server_port(port); evdns_shutdown(0); /* remove ourself as nameserver. */ #ifdef WIN32 closesocket(sock); #else close(sock); #endif }