dns-lookup.c revision 1b75b342eca820e52ca27e6bc33e0062d63eece3
5a580c3a38ced62d4bcc95b8ac7c4f2935b5d294Timo Sirainen/* Copyright (c) 2010-2013 Dovecot authors, see the included COPYING file */
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen unsigned int ip_idx;
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainenstatic void dns_lookup_free(struct dns_lookup **_lookup);
1b75b342eca820e52ca27e6bc33e0062d63eece3Timo Sirainenstatic void dns_lookup_close(struct dns_lookup *lookup)
1b75b342eca820e52ca27e6bc33e0062d63eece3Timo Sirainen i_error("close(%s) failed: %m", lookup->path);
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainenstatic int dns_lookup_input_line(struct dns_lookup *lookup, const char *line)
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen struct dns_lookup_result *result = &lookup->result;
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen /* <ret> [<name>] */
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen result->error = net_gethosterror(result->ret);
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen /* first line: <ret> <ip count> */
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen result->error = net_gethosterror(result->ret);
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen if (net_addr2ip(line, &lookup->ips[lookup->ip_idx]) < 0)
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainenstatic void dns_lookup_save_msecs(struct dns_lookup *lookup)
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen diff = timeval_diff_msecs(&now, &lookup->start_time);
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainenstatic void dns_lookup_input(struct dns_lookup *lookup)
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen struct dns_lookup_result *result = &lookup->result;
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen while ((line = i_stream_read_next_line(lookup->input)) != NULL) {
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen /* already got the error */
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen } else if (lookup->input->stream_errno != 0) {
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen result->error = t_strdup_printf("read(%s) failed: %m",
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen result->error = t_strdup_printf("Unexpected EOF from %s",
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainenstatic void dns_lookup_timeout(struct dns_lookup *lookup)
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen lookup->result.error = "DNS lookup timed out";
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen lookup->callback(&lookup->result, lookup->context);
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainendns_lookup_common(const char *cmd, bool ptr_lookup,
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen dns_lookup_callback_t *callback, void *context,
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen fd = net_connect_unix(set->dns_client_socket_path);
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen result.error = t_strdup_printf("connect(%s) failed: %m",
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen result.error = t_strdup_printf("write(%s) failed: %m",
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen lookup->path = i_strdup(set->dns_client_socket_path);
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen lookup->input = i_stream_create_fd(fd, MAX_INBUF_SIZE, FALSE);
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen lookup->io = io_add(fd, IO_READ, dns_lookup_input, lookup);
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen if (gettimeofday(&lookup->start_time, NULL) < 0)
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainenint dns_lookup(const char *host, const struct dns_lookup_settings *set,
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen dns_lookup_callback_t *callback, void *context,
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen return dns_lookup_common(t_strconcat("IP\t", host, "\n", NULL), FALSE,
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen dns_lookup_callback_t *callback, void *context,
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen const char *cmd = t_strconcat("NAME\t", net_ip2addr(ip), "\n", NULL);
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen return dns_lookup_common(cmd, TRUE, set, callback, context, lookup_r);
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainenstatic void dns_lookup_free(struct dns_lookup **_lookup)
7d102c66eb1755e1894cf56e3594cd744e855238Timo Sirainenvoid dns_lookup_abort(struct dns_lookup **lookup)
96e3a90451b495d8bedbe5dd731539269cb8c08dStephan Boschvoid dns_lookup_switch_ioloop(struct dns_lookup *lookup)