
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <systemd/sd-bus.h>

static char *malloc_copy_string(const char *string)
{
    char *copy = NULL;
    if (string) {
        ssize_t length = strlen(string);
        copy = (char *)malloc(length + 1);
        memcpy(copy, string, length+1);
    }
    return copy;
}


typedef struct NetworkLinkList NetworkLinkList;
struct NetworkLinkList
{
    int index;
    char *name; // malloc'ed
    char *path; // malloc'ed
    NetworkLinkList *next;
};

static void free_network_link_list(NetworkLinkList *list)
{
    while (list) {
        NetworkLinkList *next = list->next;
        free(list->name);
        free(list->path);
        free(list);
        list = next;
    }
}


static NetworkLinkList *get_network_link_list(sd_bus *bus)
{
    NetworkLinkList *list = 0;

    int res = 0;
    uint64_t timeout_usec;
    sd_bus_message *message = NULL;
    sd_bus_message *reply = NULL;
    sd_bus_error error = SD_BUS_ERROR_NULL;

    const char *service_name = "org.freedesktop.network1";
    const char *object_path = "/org/freedesktop/network1";
    const char *interface_name = "org.freedesktop.network1.Manager";
    const char *method_name = "ListLinks";

    res = sd_bus_message_new_method_call(bus, &message, service_name, object_path, interface_name, method_name);
    if (res < 0) {
        printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
        goto end;
    }

    timeout_usec = 1*1000000;
    res = sd_bus_call(bus, message, timeout_usec, &error, &reply);
    if (sd_bus_error_is_set(&error)) {
        printf("%s:%i: sd_bus_call failed (%s): %s\n", __FILE__, __LINE__, error.name, error.message);
        sd_bus_error_free(&error);
        goto end;
    } else if (res < 0) {
        printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
        goto end;
    }

    res = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(iso)"); // Docs don't say it, but we can pass (0, NULL) here, and sd-bus figures out the types for us.
    if (res < 0) {
        printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
        goto end;
    }

    while (1) {
        char next_type;
        res = sd_bus_message_peek_type(reply, &next_type, NULL);
        if (res == 0) {
            // End of array.
            break;
        } else if (res < 0) {
            printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
            goto end;
        }

        res = sd_bus_message_enter_container(reply, SD_BUS_TYPE_STRUCT, "iso");
        if (res < 0) {
            printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
            goto end;
        }

        int32_t index = 0;
        const char *name = NULL;
        const char *path = NULL;

        res = sd_bus_message_read_basic(reply, 'i', &index);
        if (res < 0) {
            printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
            goto end;
        }

        res = sd_bus_message_read_basic(reply, 's', &name);
        if (res < 0) {
            printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
            goto end;
        }

        res = sd_bus_message_read_basic(reply, 'o', &path);
        if (res < 0) {
            printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
            goto end;
        }

        NetworkLinkList *network_link = calloc(sizeof(NetworkLinkList), 1);
        network_link->index = index;
        network_link->name = malloc_copy_string(name);
        network_link->path = malloc_copy_string(path);

        network_link->next = list;
        list = network_link;

        res = sd_bus_message_exit_container(reply);
        if (res < 0) {
            printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
            goto end;
        }
    }

    res = sd_bus_message_exit_container(reply);
    if (res < 0) {
        printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
        goto end;
    }

  end:;
    sd_bus_message_unref(message);
    sd_bus_message_unref(reply);
    if (res < 0) {
        free_network_link_list(list);
        list = 0;
    }
    return list;
}

static char *get_administrative_state(sd_bus *bus, const char *network_path)
{
    char *result = NULL;

    int res = 0;
    uint64_t timeout_usec;
    sd_bus_message *message = NULL;
    sd_bus_message *reply = NULL;
    sd_bus_error error = SD_BUS_ERROR_NULL;

    const char *service_name = "org.freedesktop.network1";
    const char *interface_name = "org.freedesktop.DBus.Properties";
    const char *method_name = "Get";

    res = sd_bus_message_new_method_call(bus, &message, service_name, network_path, interface_name, method_name);
    if (res < 0) {
        printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
        goto end;
    }

    res = sd_bus_message_append_basic(message, 's', "org.freedesktop.network1.Link");
    if (res < 0) {
        printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
        goto end;
    }

    res = sd_bus_message_append_basic(message, 's', "AdministrativeState");
    if (res < 0) {
        printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
        goto end;
    }

    timeout_usec = 1*1000000;
    res = sd_bus_call(bus, message, timeout_usec, &error, &reply);
    if (sd_bus_error_is_set(&error)) {
        printf("%s:%i: sd_bus_call failed (%s): %s\n", __FILE__, __LINE__, error.name, error.message);
        sd_bus_error_free(&error);
        goto end;
    } else if (res < 0) {
        printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
        goto end;
    }

    res = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, "s");
    if (res < 0) {
        printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
        goto end;
    }

    res = sd_bus_message_read_basic(reply, 's', &result);
    if (res < 0) {
        printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
        goto end;
    }
    result = malloc_copy_string(result);

    res = sd_bus_message_exit_container(reply);
    if (res < 0) {
        printf("%s:%i: sd_bus error: %s\n", __FILE__, __LINE__, strerrorname_np(-res));
        goto end;
    }

  end:;
    sd_bus_message_unref(message);
    sd_bus_message_unref(reply);
    if (res < 0) {
        free(result);
        result = 0;
    }
    return result;
}

int main()
{
    sd_bus *bus;
    int res = sd_bus_default_system(&bus);
    if (res < 0) {
        printf("sd_bus_default_system failed: %s\n", strerrorname_np(-res));
    } else {
        NetworkLinkList *network_link_list = get_network_link_list(bus);
        for (NetworkLinkList *network_link = network_link_list; network_link; network_link = network_link->next) {
            char *administrative_state = get_administrative_state(bus, network_link->path);
            printf("%s is %s\n", network_link->name, administrative_state);
            free(administrative_state);
        }

        free_network_link_list(network_link_list);

        sd_bus_unrefp(&bus);
    }
    return 0;
}
