[systemd-devel] sd-bus example code for SetLinkDNS()

Tilman Baumann tilman at baumann.name
Fri Jul 21 15:13:18 UTC 2017


On 21.07.2017 14:44, Tilman Baumann wrote:
> On 21.07.2017 14:35, Lennart Poettering wrote:
>> On Wed, 19.07.17 11:20, Tilman Baumann (tilman at baumann.name) wrote:
>>
>  >> My guess is that I can have it easier if I somehow use
>>> sd_bus_message_append() to assemble the message. But I don't see a clear
>>> path either.
>>
>> You have to do something like this:
>>
>> sd_bus_message_new_method(..., &m);
>> sd_bus_message_append(m, "i", ifindex);
>> sd_bus_message_open_container(m, 'a', '(iay)');
>> for (i = 0; i < n_addresses; i++) {
>>         sd_bus_message_open_container(m, '(', "iay"));
>>         sd_bus_message_append(m, "i", addresses[i].sin_family);
>>         sd_bus_message_append_array(m, 'y', &addresses[i].ip_addr, sizeof(addresses[i].ip_addr));
>>         sd_bus_message_close_container(m);
>> }
>> sd_bus_message_close_container(m);
>> sd_bus_message_send(..., m);
>> sd_bus_message_unref(m);

And here the working solution for the permanent record.
r = sd_bus_message_new_method_call(
  bus, &m,
  "org.freedesktop.resolve1",
  "/org/freedesktop/resolve1",
  "org.freedesktop.resolve1.Manager",
  "SetLinkDNS");
r = sd_bus_message_append(m, "i", if_index);
r = sd_bus_message_open_container(m, 'a', "(iay)");
for (int i = 0; i < addresses_size; i++) {
        fprintf(stderr, "server %d\n", i);
        r = sd_bus_message_open_container(m, 'r', "iay");
        r = sd_bus_message_append(m, "i", addresses[i].sin_family);
        r = sd_bus_message_append_array(m, 'y', &addresses[i].ip_addr,
sizeof(addresses[i].ip_addr));
        r = sd_bus_message_close_container(m);
}
r = sd_bus_message_close_container(m);
r = sd_bus_call(bus, m, 0, &error, &reply);


More information about the systemd-devel mailing list