1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-12 15:21:19 +03:00

resloved: transaction - unify IPv4 and IPv6 sockets

A transaction can only have one socket at a time, so no need to distinguish these.
This commit is contained in:
Tom Gundersen 2015-07-15 18:48:17 +02:00
parent 2d5c8a2756
commit 0eb99d0a6a
3 changed files with 25 additions and 59 deletions

View File

@ -162,12 +162,7 @@ int dns_scope_emit(DnsScope *s, DnsTransaction *t, DnsPacket *p, DnsServer **ser
if (p->size + UDP_PACKET_HEADER_SIZE > mtu) if (p->size + UDP_PACKET_HEADER_SIZE > mtu)
return -EMSGSIZE; return -EMSGSIZE;
if (family == AF_INET) fd = transaction_dns_fd(t);
fd = transaction_dns_ipv4_fd(t);
else if (family == AF_INET6)
fd = transaction_dns_ipv6_fd(t);
else
return -EAFNOSUPPORT;
if (fd < 0) if (fd < 0)
return fd; return fd;

View File

@ -39,10 +39,8 @@ DnsTransaction* dns_transaction_free(DnsTransaction *t) {
dns_packet_unref(t->received); dns_packet_unref(t->received);
dns_answer_unref(t->cached); dns_answer_unref(t->cached);
sd_event_source_unref(t->dns_ipv4_event_source); sd_event_source_unref(t->dns_event_source);
sd_event_source_unref(t->dns_ipv6_event_source); safe_close(t->dns_fd);
safe_close(t->dns_ipv4_fd);
safe_close(t->dns_ipv6_fd);
dns_server_unref(t->server); dns_server_unref(t->server);
dns_stream_free(t->stream); dns_stream_free(t->stream);
@ -94,7 +92,7 @@ int dns_transaction_new(DnsTransaction **ret, DnsScope *s, DnsQuestion *q) {
if (!t) if (!t)
return -ENOMEM; return -ENOMEM;
t->dns_ipv4_fd = t->dns_ipv6_fd = -1; t->dns_fd = -1;
t->question = dns_question_ref(q); t->question = dns_question_ref(q);
@ -655,7 +653,7 @@ static int on_dns_packet(sd_event_source *s, int fd, uint32_t revents, void *use
return 0; return 0;
} }
int transaction_dns_ipv4_fd(DnsTransaction *t) { int transaction_dns_fd(DnsTransaction *t) {
const int one = 1; const int one = 1;
int r; int r;
@ -663,59 +661,36 @@ int transaction_dns_ipv4_fd(DnsTransaction *t) {
assert(t->scope); assert(t->scope);
assert(t->scope->manager); assert(t->scope->manager);
if (t->dns_ipv4_fd >= 0) if (t->dns_fd >= 0)
return t->dns_ipv4_fd; return t->dns_fd;
t->dns_ipv4_fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); t->dns_fd = socket(t->scope->family, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (t->dns_ipv4_fd < 0) if (t->dns_fd < 0)
return -errno; return -errno;
r = setsockopt(t->dns_ipv4_fd, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one)); switch (t->scope->family) {
case AF_INET:
r = setsockopt(t->dns_fd, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one));
break;
case AF_INET6:
r = setsockopt(t->dns_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
break;
default:
return -EAFNOSUPPORT;
}
if (r < 0) { if (r < 0) {
r = -errno; r = -errno;
goto fail; goto fail;
} }
r = sd_event_add_io(t->scope->manager->event, &t->dns_ipv4_event_source, t->dns_ipv4_fd, EPOLLIN, on_dns_packet, t); r = sd_event_add_io(t->scope->manager->event, &t->dns_event_source, t->dns_fd, EPOLLIN, on_dns_packet, t);
if (r < 0) if (r < 0)
goto fail; goto fail;
return t->dns_ipv4_fd; return t->dns_fd;
fail: fail:
t->dns_ipv4_fd = safe_close(t->dns_ipv4_fd); t->dns_fd = safe_close(t->dns_fd);
return r;
}
int transaction_dns_ipv6_fd(DnsTransaction *t) {
const int one = 1;
int r;
assert(t);
assert(t->scope);
assert(t->scope->manager);
if (t->dns_ipv6_fd >= 0)
return t->dns_ipv6_fd;
t->dns_ipv6_fd = socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (t->dns_ipv6_fd < 0)
return -errno;
r = setsockopt(t->dns_ipv6_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(t->scope->manager->event, &t->dns_ipv6_event_source, t->dns_ipv6_fd, EPOLLIN, on_dns_packet, t);
if (r < 0)
goto fail;
return t->dns_ipv6_fd;
fail:
t->dns_ipv6_fd = safe_close(t->dns_ipv6_fd);
return r; return r;
} }

View File

@ -61,11 +61,8 @@ struct DnsTransaction {
sd_event_source *timeout_event_source; sd_event_source *timeout_event_source;
unsigned n_attempts; unsigned n_attempts;
int dns_ipv4_fd; int dns_fd;
int dns_ipv6_fd; sd_event_source *dns_event_source;
sd_event_source *dns_ipv4_event_source;
sd_event_source *dns_ipv6_event_source;
/* the active server */ /* the active server */
DnsServer *server; DnsServer *server;
@ -95,8 +92,7 @@ int dns_transaction_go(DnsTransaction *t);
void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p); void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p);
void dns_transaction_complete(DnsTransaction *t, DnsTransactionState state); void dns_transaction_complete(DnsTransaction *t, DnsTransactionState state);
int transaction_dns_ipv4_fd(DnsTransaction *t); int transaction_dns_fd(DnsTransaction *t);
int transaction_dns_ipv6_fd(DnsTransaction *t);
const char* dns_transaction_state_to_string(DnsTransactionState p) _const_; const char* dns_transaction_state_to_string(DnsTransactionState p) _const_;
DnsTransactionState dns_transaction_state_from_string(const char *s) _pure_; DnsTransactionState dns_transaction_state_from_string(const char *s) _pure_;