chiark / gitweb /
resolved: rework logic so that we can share transactions between queries of different...
[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
64         sd_event_source *timeout_event_source;
65         unsigned n_attempts;
66
67         /* TCP connection logic */
68         int tcp_fd;
69         sd_event_source *tcp_event_source;
70         size_t tcp_written, tcp_read;
71         be16_t tcp_read_size;
72
73         /* Queries this transaction is referenced by and that shall by
74          * notified about this specific transaction completing. */
75         Set *queries;
76
77         unsigned block_gc;
78
79         LIST_FIELDS(DnsQueryTransaction, transactions_by_scope);
80 };
81
82 struct DnsQuery {
83         Manager *manager;
84         DnsQuestion *question;
85
86         DnsQueryState state;
87         unsigned n_cname_redirects;
88
89         sd_event_source *timeout_event_source;
90
91         /* Discovered data */
92         DnsPacket *received;
93         DnsAnswer *answer;
94         int answer_ifindex;
95         int answer_rcode;
96
97         /* Bus client information */
98         sd_bus_message *request;
99         int request_family;
100         const char *request_hostname;
101         union in_addr_union request_address;
102
103         /* Completion callback */
104         void (*complete)(DnsQuery* q);
105         unsigned block_ready;
106
107         Set *transactions;
108
109         LIST_FIELDS(DnsQuery, queries);
110 };
111
112 DnsQueryTransaction* dns_query_transaction_free(DnsQueryTransaction *t);
113 void dns_query_transaction_complete(DnsQueryTransaction *t, DnsQueryState state);
114
115 void dns_query_transaction_process_reply(DnsQueryTransaction *t, DnsPacket *p);
116
117 int dns_query_new(Manager *m, DnsQuery **q, DnsQuestion *question);
118 DnsQuery *dns_query_free(DnsQuery *q);
119
120 int dns_query_go(DnsQuery *q);
121 void dns_query_ready(DnsQuery *q);
122
123 int dns_query_cname_redirect(DnsQuery *q, const char *name);
124
125 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsQuery*, dns_query_free);