/*
 * ABOUTME: Parses, validates, and renders EUI48 (TYPE 108) resource records.
 * ABOUTME: Presentation form is six hyphen-separated hex octets (RFC 7043).
 *
 * Part of DNS zone file validator `validns`.
 *
 * Copyright 2025-2026 OARC, Inc.
 * Copyright 2011-2025 Anton Berezin <tobez@tobez.org>
 * Modified BSD license.
 * (See LICENSE file in the distribution.)
 *
 */
#include <sys/types.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "common.h"
#include "textparse.h"
#include "mempool.h"
#include "carp.h"
#include "rr.h"

static struct rr* eui48_parse(char* name, long ttl, int type, char* s)
{
    struct rr_eui48* rr = getmem(sizeof(*rr));

    if (extract_eui(&s, "EUI48 rdata", rr->address, 6) <= 0)
        return NULL;
    if (*s)
        return bitch("garbage after valid EUI48 data");

    return store_record(type, name, ttl, rr);
}

static char* eui48_human(struct rr* rrv)
{
    RRCAST(eui48);
    char s[32];

    snprintf(s, sizeof(s), "%02x-%02x-%02x-%02x-%02x-%02x",
        rr->address[0], rr->address[1], rr->address[2],
        rr->address[3], rr->address[4], rr->address[5]);
    return quickstrdup_temp(s);
}

static struct binary_data eui48_wirerdata(struct rr* rrv)
{
    RRCAST(eui48);
    struct binary_data r;

    r.length = sizeof(rr->address);
    r.data   = (void*)rr->address;
    return r;
}

struct rr_methods eui48_methods = { eui48_parse, eui48_human, eui48_wirerdata, NULL, NULL };
