nss-mymachines.c revision 3a6fb33c54bc64398e0af1c9d7c74a6b614a849d
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering This file is part of systemd.
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering Copyright 2014 Lennart Poettering
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering systemd is free software; you can redistribute it and/or modify it
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering under the terms of the GNU Lesser General Public License as published by
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering (at your option) any later version.
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering systemd is distributed in the hope that it will be useful, but
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering Lesser General Public License for more details.
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering You should have received a copy of the GNU Lesser General Public License
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poetteringstatic int count_addresses(sd_bus_message *m, int af, unsigned *ret) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering unsigned c = 0;
3a6fb33c54bc64398e0af1c9d7c74a6b614a849dLennart Poettering while ((r = sd_bus_message_enter_container(m, 'r', "iay")) > 0) {
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_read(m, "i", &family);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poetteringenum nss_status _nss_mymachines_gethostbyname4_r(
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering struct gaih_addrtuple *r_tuple, *r_tuple_first = NULL;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering _cleanup_bus_message_unref_ sd_bus_message* reply = NULL;
249968612f16a71df909d6e73785c18a9ff36a65Lennart Poettering _cleanup_bus_close_unref_ sd_bus *bus = NULL;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering unsigned i = 0, c = 0;
634af5665fda8776d22624d947c8de830e30a874Lennart Poettering n_ifindices = sd_machine_get_ifindices(name, &ifindices);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "org.freedesktop.machine1",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "org.freedesktop.machine1.Manager",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "GetMachineAddresses",
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_enter_container(reply, 'a', "(iay)");
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = count_addresses(reply, AF_UNSPEC, &c);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * c;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* First, append name */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* Second, append addresses */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r_tuple_first = (struct gaih_addrtuple*) (buffer + idx);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering while ((r = sd_bus_message_enter_container(reply, 'r', "iay")) > 0) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering const void *a;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_read(reply, "i", &family);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_read_array(reply, 'y', &a, &sz);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_exit_container(reply);
555bd6e95bf2b469306d9cd86e126c0122d0895bLennart Poettering if (!IN_SET(family, AF_INET, AF_INET6)) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r_tuple = (struct gaih_addrtuple*) (buffer + idx);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r_tuple->next = i == c-1 ? NULL : (struct gaih_addrtuple*) ((char*) r_tuple + ALIGN(sizeof(struct gaih_addrtuple)));
634af5665fda8776d22624d947c8de830e30a874Lennart Poettering r_tuple->scopeid = n_ifindices == 1 ? ifindices[0] : 0;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering idx += ALIGN(sizeof(struct gaih_addrtuple));
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_exit_container(reply);
e70df46b9721a3d025e7a0b4ffb5893cbde5e55dLennart Poettering /* Explicitly reset all error variables */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poetteringenum nss_status _nss_mymachines_gethostbyname3_r(
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering _cleanup_bus_message_unref_ sd_bus_message* reply = NULL;
249968612f16a71df909d6e73785c18a9ff36a65Lennart Poettering _cleanup_bus_close_unref_ sd_bus *bus = NULL;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering unsigned c = 0, i = 0;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering char *r_name, *r_aliases, *r_addr, *r_addr_list;
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "org.freedesktop.machine1",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "org.freedesktop.machine1.Manager",
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering "GetMachineAddresses",
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_enter_container(reply, 'a', "(iay)");
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering sizeof(char*) +
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* First, append name */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* Second, create aliases array */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering idx += sizeof(char*);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* Third, append addresses */
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering while ((r = sd_bus_message_enter_container(reply, 'r', "iay")) > 0) {
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering const void *a;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering r = sd_bus_message_read(reply, "i", &family);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_read_array(reply, 'y', &a, &sz);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_exit_container(reply);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering r = sd_bus_message_exit_container(reply);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering /* Third, append address pointer array */
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering for (i = 0; i < c; i++)
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering ((char**) r_addr_list)[i] = r_addr + i*ALIGN(alen);
cabb0bc6b1a4ec57e108dc99364687d7c4f9670fLennart Poettering result->h_addr_list = (char**) r_addr_list;
e70df46b9721a3d025e7a0b4ffb5893cbde5e55dLennart Poettering /* Explicitly reset all error variables */