return dns_stream_complete(s, ETIMEDOUT);
}
+static DnsPacket *dns_stream_take_read_packet(DnsStream *s) {
+ assert(s);
+
+ if (!s->read_packet)
+ return NULL;
+
+ if (s->n_read < sizeof(s->read_size))
+ return NULL;
+
+ if (s->n_read < sizeof(s->read_size) + be16toh(s->read_size))
+ return NULL;
+
+ s->n_read = 0;
+ return TAKE_PTR(s->read_packet);
+}
+
static int on_stream_io_impl(DnsStream *s, uint32_t revents) {
bool progressed = false;
int r;
/* Are we done? If so, call the packet handler and re-enable EPOLLIN for the
* event source if necessary. */
- if (s->n_read >= sizeof(s->read_size) + be16toh(s->read_size)) {
+ _cleanup_(dns_packet_unrefp) DnsPacket *p = dns_stream_take_read_packet(s);
+ if (p) {
assert(s->on_packet);
- r = s->on_packet(s);
+ r = s->on_packet(s, p);
if (r < 0)
return r;
DnsProtocol protocol,
int fd,
const union sockaddr_union *tfo_address,
- int (on_packet)(DnsStream*),
+ int (on_packet)(DnsStream*, DnsPacket*),
int (complete)(DnsStream*, int), /* optional */
usec_t connect_timeout_usec) {
return dns_stream_update_io(s);
}
-DnsPacket *dns_stream_take_read_packet(DnsStream *s) {
- assert(s);
-
- if (!s->read_packet)
- return NULL;
-
- if (s->n_read < sizeof(s->read_size))
- return NULL;
-
- if (s->n_read < sizeof(s->read_size) + be16toh(s->read_size))
- return NULL;
-
- s->n_read = 0;
- return TAKE_PTR(s->read_packet);
-}
-
void dns_stream_detach(DnsStream *s) {
assert(s);
size_t n_written, n_read;
OrderedSet *write_queue;
- int (*on_packet)(DnsStream *s);
+ int (*on_packet)(DnsStream *s, DnsPacket *p);
int (*complete)(DnsStream *s, int error);
LIST_HEAD(DnsTransaction, transactions); /* when used by the transaction logic */
DnsProtocol protocol,
int fd,
const union sockaddr_union *tfo_address,
- int (on_packet)(DnsStream*),
+ int (on_packet)(DnsStream*, DnsPacket*),
int (complete)(DnsStream*, int), /* optional */
usec_t connect_timeout_usec);
#if ENABLE_DNS_OVER_TLS
return !!s->write_packet;
}
-DnsPacket *dns_stream_take_read_packet(DnsStream *s);
-
void dns_stream_detach(DnsStream *s);
return on_dns_stub_packet_internal(s, fd, revents, l->manager, l);
}
-static int on_dns_stub_stream_packet(DnsStream *s) {
- _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
-
+static int on_dns_stub_stream_packet(DnsStream *s, DnsPacket *p) {
assert(s);
-
- p = dns_stream_take_read_packet(s);
+ assert(s->manager);
assert(p);
if (dns_packet_validate_query(p) > 0) {
return 0;
}
-static int on_stream_packet(DnsStream *s) {
- _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
+static int on_stream_packet(DnsStream *s, DnsPacket *p) {
DnsTransaction *t;
assert(s);
-
- /* Take ownership of packet to be able to receive new packets */
- assert_se(p = dns_stream_take_read_packet(s));
+ assert(s->manager);
+ assert(p);
t = hashmap_get(s->manager->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p)));
if (t && t->stream == s) /* Validate that the stream we got this on actually is the stream the
return m->llmnr_ipv6_udp_fd = TAKE_FD(s);
}
-static int on_llmnr_stream_packet(DnsStream *s) {
- _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
+static int on_llmnr_stream_packet(DnsStream *s, DnsPacket *p) {
DnsScope *scope;
assert(s);
-
- p = dns_stream_take_read_packet(s);
+ assert(s->manager);
assert(p);
scope = manager_find_scope(s->manager, p);
static DnsPacket *received_packets[2] = {};
static size_t n_received_packets = 0;
-static int on_stream_packet(DnsStream *stream) {
+static int on_stream_packet(DnsStream *stream, DnsPacket *p) {
assert_se(n_received_packets < MAX_RECEIVED_PACKETS);
- assert_se(received_packets[n_received_packets++] = dns_stream_take_read_packet(stream));
+ assert_se(received_packets[n_received_packets++] = dns_packet_ref(p));
return 0;
}