-
-
Save tbaumann/0f466c984c858767c966458d53483697 to your computer and use it in GitHub Desktop.
Broken SetLinkDNS() in c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <systemd/sd-bus.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <netinet/ip.h> | |
#include <arpa/inet.h> | |
int SetLinkDNSv4(sd_bus *bus, int if_index, struct in_addr ns1, struct in_addr ns2) { | |
sd_bus_error error = SD_BUS_ERROR_NULL; | |
sd_bus_message *m = NULL; | |
int r; | |
struct dns_address { | |
int32_t sin_family; | |
struct in_addr ip_addr; | |
}; | |
struct dns_address addresses[2]; | |
addresses[0].sin_family = AF_INET; | |
addresses[0].ip_addr = ns1; | |
addresses[1].sin_family = AF_INET; | |
addresses[1].ip_addr = ns2; | |
r = sd_bus_call_method(bus, | |
"org.freedesktop.resolve1", /* service to contact */ | |
"/org/freedesktop/resolve1", /* object path */ | |
"org.freedesktop.resolve1.Manager", /* interface name */ | |
"SetLinkDNS", /* method name */ | |
&error, /* object to return error in */ | |
&m, /* return message on success */ | |
"ia(iay)", /* input signature */ | |
if_index, | |
2, | |
addresses); | |
if (r < 0) { | |
fprintf(stderr, "Failed to issue method call: %s\n", error.message); | |
goto finish; | |
} | |
/* Parse the response message */ | |
/* | |
r = sd_bus_message_read(m, "o", &path); | |
if (r < 0) { | |
fprintf(stderr, "Failed to parse response message: %s\n", strerror(-r)); | |
goto finish; | |
} | |
*/ | |
finish: | |
sd_bus_error_free(&error); | |
sd_bus_message_unref(m); | |
} | |
int main(int argc, char *argv[]) { | |
sd_bus *bus = NULL; | |
int r; | |
/* Connect to the system bus */ | |
r = sd_bus_open_system(&bus); | |
if (r < 0) { | |
fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r)); | |
goto finish; | |
} | |
struct in_addr ns1; | |
struct in_addr ns2; | |
inet_aton("10.0.0.1", &ns1); | |
inet_aton("10.0.0.2", &ns2); | |
r = SetLinkDNSv4(bus, 16, ns1, ns2) ; | |
finish: | |
sd_bus_unref(bus); | |
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment