nss-resolve.c revision 9d485985338a46b8cb1acdf1af6c1eb2e88acfee
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering This file is part of systemd.
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering Copyright 2014 Lennart Poettering
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering systemd is free software; you can redistribute it and/or modify it
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering under the terms of the GNU Lesser General Public License as published by
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering (at your option) any later version.
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering systemd is distributed in the hope that it will be useful, but
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering Lesser General Public License for more details.
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering You should have received a copy of the GNU Lesser General Public License
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering#define DNS_CALL_TIMEOUT_USEC (45*USEC_PER_SEC)
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poetteringstatic int count_addresses(sd_bus_message *m, unsigned af, const char **canonical) {
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering r = sd_bus_message_enter_container(m, 'a', "(yayi)");
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering while ((r = sd_bus_message_enter_container(m, 'r', "yayi")) > 0) {
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering unsigned char family;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_read(m, "y", &family);
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering r = sd_bus_message_read(m, "s", canonical);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poetteringenum nss_status _nss_resolve_gethostbyname4_r(
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *req = NULL, *reply = NULL;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering struct gaih_addrtuple *r_tuple, *r_tuple_first = NULL;
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering int c, r, i = 0;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering "org.freedesktop.resolve1",
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering "org.freedesktop.resolve1.Manager",
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering "ResolveHostname");
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_set_auto_start(req, false);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_append(req, "sy", name, AF_UNSPEC);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_call(bus, req, DNS_CALL_TIMEOUT_USEC, &error, &reply);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering if (sd_bus_error_has_name(&error, _BUS_ERROR_DNS "NXDOMAIN")) {
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering c = count_addresses(reply, AF_UNSPEC, &canonical);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * c;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering /* First, append name */
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering /* Second, append addresses */
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r_tuple_first = (struct gaih_addrtuple*) (buffer + idx);
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering r = sd_bus_message_enter_container(reply, 'a', "(yayi)");
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering while ((r = sd_bus_message_enter_container(reply, 'r', "yayi")) > 0) {
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering unsigned char family;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering const void *a;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_read(reply, "y", &family);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_read_array(reply, 'y', &a, &sz);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_read(reply, "i", &ifindex);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_exit_container(reply);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r_tuple = (struct gaih_addrtuple*) (buffer + idx);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r_tuple->next = i == c-1 ? NULL : (struct gaih_addrtuple*) ((char*) r_tuple + ALIGN(sizeof(struct gaih_addrtuple)));
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering idx += ALIGN(sizeof(struct gaih_addrtuple));
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poetteringenum nss_status _nss_resolve_gethostbyname3_r(
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *req = NULL, *reply = NULL;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering char *r_name, *r_aliases, *r_addr, *r_addr_list;
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering int c, r, i = 0;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering "org.freedesktop.resolve1",
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering "org.freedesktop.resolve1.Manager",
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering "ResolveHostname");
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_set_auto_start(req, false);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_append(req, "sy", name, af);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_call(bus, req, DNS_CALL_TIMEOUT_USEC, &error, &reply);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering if (sd_bus_error_has_name(&error, _BUS_ERROR_DNS "NXDOMAIN")) {
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering c = count_addresses(reply, af, &canonical);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering sizeof(char*) +
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering /* First, append name */
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering /* Second, create empty aliases array */
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering idx += sizeof(char*);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering /* Third, append addresses */
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering r = sd_bus_message_enter_container(reply, 'a', "(yayi)");
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering while ((r = sd_bus_message_enter_container(reply, 'r', "yayi")) > 0) {
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering unsigned char family;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering const void *a;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_read(reply, "y", &family);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_read_array(reply, 'y', &a, &sz);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_read(reply, "i", &ifindex);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_exit_container(reply);
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering /* Fourth, append address pointer array */
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering for (i = 0; i < c; i++)
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering ((char**) r_addr_list)[i] = r_addr + i*ALIGN(alen);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering result->h_addr_list = (char**) r_addr_list;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poetteringenum nss_status _nss_resolve_gethostbyaddr2_r(
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *req = NULL, *reply = NULL;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering char *r_name, *r_aliases, *r_addr, *r_addr_list;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering unsigned c = 0, i = 0;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering const char *n;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering "org.freedesktop.resolve1",
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering "org.freedesktop.resolve1.Manager",
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering "ResolveAddress");
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_set_auto_start(req, false);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_append_array(req, 'y', addr, len);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_call(bus, req, DNS_CALL_TIMEOUT_USEC, &error, &reply);
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering if (sd_bus_error_has_name(&error, _BUS_ERROR_DNS "NXDOMAIN")) {
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering r = sd_bus_message_enter_container(reply, 'a', "s");
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering while ((r = sd_bus_message_read(reply, "s", &n)) > 0) {
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering 2 * sizeof(char*) + /* pointers to the address, plus trailing NULL */
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering c * sizeof(char*); /* pointers to aliases, plus trailing NULL */
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering /* First, place address */
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering /* Second, place address list */
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering /* Third, reserve space for the aliases array */
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering idx += sizeof(char*) * c;
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering /* Fourth, place aliases */
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering while ((r = sd_bus_message_read(reply, "s", &n)) > 0) {