/*
Copyright (c) 2015, Jens Georg
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>

#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

static int connect_nl()
{
    struct sockaddr_nl sa = { 0 };
    int fd = -1;
    int result = -1;

    fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);

    if (fd < 0)
    {
        fprintf(stderr, "Failed to create netlink socket: %m");

        goto out;
    }

    memset(&sa, 0, sizeof(sa));
    sa.nl_family = AF_NETLINK;
    sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;

    result = bind(fd, (const struct sockaddr *) &sa, sizeof(sa));
    if (result < 0)
    {
        fprintf(stderr, "Failed to bind to netlink socket: %m");

        goto out;
    }

    return fd;

out:
    if (fd >= 0)
    {
        close(fd);
    }

    return -1;
}

static void dump(const uint8_t * const buffer, size_t length)
{
    size_t offset = 0;
    char ascii[17] = { 0 };
    char padding[49] = { 0 };
    uint8_t modulus = 0;

    for (offset = 0; offset < length; offset++)
    {
        modulus = (offset % 16);
        if (modulus == 0)
        {
            printf("%04zx: ", offset);
        }
        printf("%02x ", buffer[offset]);
        ascii[modulus] = isprint(buffer[offset]) ?
                            (char) buffer[offset] : '.';

        /* end of line, dump ASCII */
        if (modulus == 15)
        {
            printf("  %s\n", ascii);
            memset(ascii, 0, sizeof(ascii));
        }
    }

    if (modulus != 15)
    {
        memset(padding, ' ', 3 * (16 - modulus));
        printf("%s  %s\n", padding, ascii);
    }
}

int main(void)
{
    uint8_t buffer[65536];
    ssize_t bytes_read = 0;
    uint64_t packet_counter = 0;

    int fd = connect_nl();
    if (fd < 0)
    {
        return 2;
    }

    while (true)
    {
        bytes_read = read(fd, buffer, sizeof(buffer));
        if (bytes_read < 0)
        {
            if (errno == EAGAIN)
            {
                continue;
            }
            fprintf(stderr, "Error reading from socket: %m");

            return 2;
        }

        printf("=========> %08" PRIx64" <==========\n", packet_counter);
        packet_counter++;
        dump(buffer, bytes_read);
    }

    return 0;
}
