chiark / gitweb /
resolved: implement negative caching
[elogind.git] / src / resolve / resolved-dns-query.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2014 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <inttypes.h>
25
26 #include "sd-bus.h"
27 #include "util.h"
28 #include "set.h"
29
30 typedef struct DnsQuery DnsQuery;
31 typedef struct DnsQueryTransaction DnsQueryTransaction;
32
33 #include "resolved.h"
34 #include "resolved-dns-scope.h"
35 #include "resolved-dns-rr.h"
36 #include "resolved-dns-packet.h"
37 #include "resolved-dns-question.h"
38 #include "resolved-dns-answer.h"
39
40 typedef enum DnsQueryState {
41         DNS_QUERY_NULL,
42         DNS_QUERY_PENDING,
43         DNS_QUERY_FAILURE,
44         DNS_QUERY_SUCCESS,
45         DNS_QUERY_NO_SERVERS,
46         DNS_QUERY_TIMEOUT,
47         DNS_QUERY_ATTEMPTS_MAX,
48         DNS_QUERY_INVALID_REPLY,
49         DNS_QUERY_RESOURCES,
50         DNS_QUERY_ABORTED,
51 } DnsQueryState;
52
53 struct DnsQueryTransaction {
54         DnsScope *scope;
55
56         DnsQuestion *question;
57
58         DnsQueryState state;
59         uint16_t id;
60
61         DnsPacket *sent, *received;
62         DnsAnswer *cached;
63         int cached_rcode;
64
65         sd_event_source *timeout_event_source;
66         unsigned n_attempts;
67
68         /* TCP connection logic */
69         int tcp_fd;
70         sd_event_source *tcp_event_source;
71         size_t tcp_written, tcp_read;
72         be16_t tcp_read_size;
73
74         /* Queries this transaction is referenced by and that shall by
75          * notified about this specific transaction completing. */
76         Set *queries;
77
78         unsigned block_gc;
79
80         LIST_FIELDS(DnsQueryTransaction, transactions_by_scope);
81 };
82
83 struct DnsQuery {
84         Manager *manager;
85         DnsQuestion *question;
86
87         DnsQueryState state;
88         unsigned n_cname_redirects;
89
90         sd_event_source *timeout_event_source;
91
92         /* Discovered data */
93         DnsPacket *received;
94         DnsAnswer *answer;
95         int answer_ifindex;
96         int answer_rcode;
97
98         /* Bus client information */
99         sd_bus_message *request;
100         int request_family;
101         const char *request_hostname;
102         union in_addr_union request_address;
103
104         /* Completion callback */
105         void (*complete)(DnsQuery* q);
106         unsigned block_ready;
107
108         Set *transactions;
109
110         LIST_FIELDS(DnsQuery, queries);
111 };
112
113 DnsQueryTransaction* dns_query_transaction_free(DnsQueryTransaction *t);
114 void dns_query_transaction_complete(DnsQueryTransaction *t, DnsQueryState state);
115
116 void dns_query_transaction_process_reply(DnsQueryTransaction *t, DnsPacket *p);
117
118 int dns_query_new(Manager *m, DnsQuery **q, DnsQuestion *question);
119 DnsQuery *dns_query_free(DnsQuery *q);
120
121 int dns_query_go(DnsQuery *q);
122 void dns_query_ready(DnsQuery *q);
123
124 int dns_query_cname_redirect(DnsQuery *q, const char *name);
125
126 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsQuery*, dns_query_free);