<commit_before>/*
 * Copyright (C) 2015 <NAME> <<EMAIL>>,
 *                    <NAME> <<EMAIL>>
 *                    <NAME> <<EMAIL>>
 *                    Ell-i open source co-operative
 *
 * This file is subject to the terms and conditions of the GNU Lesser General
 * Public License v2.1. See the file LICENSE in the top level directory for
 * more details.
 */

/*
 * @ingroup netdev2
 * @{
 * @brief   Low-level ethernet driver for tap interfaces
 * @author  <NAME> <<EMAIL>>
 * @}
 */
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

#ifdef __MACH__
#define _POSIX_C_SOURCE
#include <net/if.h>
#undef _POSIX_C_SOURCE
#include <sys/types.h>
#include <ifaddrs.h>
#include <net/if_dl.h>
#elif defined(__FreeBSD__)
#include <sys/socket.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <net/if_dl.h>
#else
#include <net/if.h>
#include <linux/if_tun.h>
#include <linux/if_ether.h>
#endif

#include "native_internal.h"

#include "net/eui64.h"
#include "net/netdev2.h"
#include "net/ethernet.h"
#include "net/ethernet/hdr.h"
#include "netdev2_tap.h"
#include "net/netopt.h"
#include "net/eui64.h"

#define ENABLE_DEBUG (0)
#include "debug.h"

/* support one tap interface for now */
netdev2_tap_t netdev2_tap;

#ifdef __MACH__
pid_t _sigio_child_pid;
static void _sigio_child(netdev2_tap_t *dev);
#endif

/* netdev2 interface */
static int _init(netdev2_t *netdev);
static int _send(netdev2_t *netdev, const struct iovec *vector, int n);
static int _recv(netdev2_t *netdev, char* buf, int n);

static inline void _get_mac_addr(netdev2_t *netdev, uint8_t *dst)
{
    netdev2_tap_t *dev = (netdev2_tap_t*)netdev;
    memcpy(dst, dev->addr, ETHERNET_ADDR_LEN);
}

static inline void _set_mac_addr(netdev2_t *netdev, uint8_t *src)
{
    netdev2_tap_t *dev = (netdev2_tap_t*)netdev;
    memcpy(dev->addr, src, ETHERNET_ADDR_LEN);
}

static inline int _get_promiscous(netdev2_t *netdev)
{
    netdev2_tap_t *dev = (netdev2_tap_t*)netdev;
    return dev->promiscous;
}

static inline int _set_promiscous(netdev2_t *netdev, int value)
{
    netdev2_tap_t *dev = (netdev2_tap_t*)netdev;
    dev->promiscous = value;
    return value;
}

static inline int _get_iid(netdev2_t *netdev, eui64_t *value, size_t max_len)
{
    if (max_len < sizeof(eui64_t)) {
        return -EOVERFLOW;
    }

    uint8_t addr[ETHERNET_ADDR_LEN];
    _get_mac_addr(netdev, addr);
    ethernet_get_iid(value, addr);

    return sizeof(eui64_t);
}
static inline void _isr(netdev2_t *netdev)
{
    if (netdev->event_callback) {
        netdev->event_callback(netdev, NETDEV2_EVENT_RX_COMPLETE, (void*)NETDEV2_TYPE_ETHERNET);
    }
#if DEVELHELP
    else {
        puts("netdev2_tap: _isr(): no event_callback set.");
    }
#endif
}

int _get(netdev2_t *dev, netopt_t opt, void *value, size_t max_len)
{
    if (dev != (netdev2_t *)&netdev2_tap) {
        return -ENODEV;
    }

    int res = 0;

    switch (opt) {
        case NETOPT_DEVICE_TYPE:
            {
               uint16_t *tgt = (uint16_t *)value;
                *tgt = NETDEV2_TYPE_ETHERNET;
                res = 2;
                break;
            }
        case NETOPT_ADDRESS:
            if (max_len < ETHERNET_ADDR_LEN) {
                res = -EINVAL;
            }
            else {
                _get_mac_addr(dev, (uint8_t*)value);
                res = ETHERNET_ADDR_LEN;
            }
            break;
        case NETOPT_ADDR_LEN:
        case NETOPT_SRC_LEN:
            assert(max_len == 2);
            uint16_t *tgt = (uint16_t*)value;
            *tgt=6;
            res = sizeof(uint16_t);
            break;
        case NETOPT_PROMISCUOUSMODE:
            *((bool*)value) = (bool)_get_promiscous(dev);
            res = sizeof(bool);
            break;
        case NETOPT_IPV6_IID:
            return _get_iid(dev, value, max_len);<commit_msg>native: netdev2_tap: fix NETOPT_IS_WIRED flag<commit_after>/*
 * Copyright (C) 2015 <NAME> <<EMAIL>.de>,
 *                    <NAME> <<EMAIL>>
 *                    <NAME> <<EMAIL>>
 *                    Ell-i open source co-operative
 *
 * This file is subject to the terms and conditions of the GNU Lesser General
 * Public License v2.1. See the file LICENSE in the top level directory for
 * more details.
 */

/*
 * @ingroup netdev2
 * @{
 * @brief   Low-level ethernet driver for tap interfaces
 * @author  <NAME> <<EMAIL>>
 * @}
 */
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

#ifdef __MACH__
#define _POSIX_C_SOURCE
#include <net/if.h>
#undef _POSIX_C_SOURCE
#include <sys/types.h>
#include <ifaddrs.h>
#include <net/if_dl.h>
#elif defined(__FreeBSD__)
#include <sys/socket.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <net/if_dl.h>
#else
#include <net/if.h>
#include <linux/if_tun.h>
#include <linux/if_ether.h>
#endif

#include "native_internal.h"

#include "net/eui64.h"
#include "net/netdev2.h"
#include "net/ethernet.h"
#include "net/ethernet/hdr.h"
#include "netdev2_tap.h"
#include "net/netopt.h"
#include "net/eui64.h"

#define ENABLE_DEBUG (0)
#include "debug.h"

/* support one tap interface for now */
netdev2_tap_t netdev2_tap;

#ifdef __MACH__
pid_t _sigio_child_pid;
static void _sigio_child(netdev2_tap_t *dev);
#endif

/* netdev2 interface */
static int _init(netdev2_t *netdev);
static int _send(netdev2_t *netdev, const struct iovec *vector, int n);
static int _recv(netdev2_t *netdev, char* buf, int n);

static inline void _get_mac_addr(netdev2_t *netdev, uint8_t *dst)
{
    netdev2_tap_t *dev = (netdev2_tap_t*)netdev;
    memcpy(dst, dev->addr, ETHERNET_ADDR_LEN);
}

static inline void _set_mac_addr(netdev2_t *netdev, uint8_t *src)
{
    netdev2_tap_t *dev = (netdev2_tap_t*)netdev;
    memcpy(dev->addr, src, ETHERNET_ADDR_LEN);
}

static inline int _get_promiscous(netdev2_t *netdev)
{
    netdev2_tap_t *dev = (netdev2_tap_t*)netdev;
    return dev->promiscous;
}

static inline int _set_promiscous(netdev2_t *netdev, int value)
{
    netdev2_tap_t *dev = (netdev2_tap_t*)netdev;
    dev->promiscous = value;
    return value;
}

static inline int _get_iid(netdev2_t *netdev, eui64_t *value, size_t max_len)
{
    if (max_len < sizeof(eui64_t)) {
        return -EOVERFLOW;
    }

    uint8_t addr[ETHERNET_ADDR_LEN];
    _get_mac_addr(netdev, addr);
    ethernet_get_iid(value, addr);

    return sizeof(eui64_t);
}
static inline void _isr(netdev2_t *netdev)
{
    if (netdev->event_callback) {
        netdev->event_callback(netdev, NETDEV2_EVENT_RX_COMPLETE, (void*)NETDEV2_TYPE_ETHERNET);
    }
#if DEVELHELP
    else {
        puts("netdev2_tap: _isr(): no event_callback set.");
    }
#endif
}

int _get(netdev2_t *dev, netopt_t opt, void *value, size_t max_len)
{
    if (dev != (netdev2_t *)&netdev2_tap) {
        return -ENODEV;
    }

    int res = 0;

    switch (opt) {
        case NETOPT_DEVICE_TYPE:
            {
               uint16_t *tgt = (uint16_t *)value;
                *tgt = NETDEV2_TYPE_ETHERNET;
                res = 2;
                break;
            }
        case NETOPT_ADDRESS:
            if (max_len < ETHERNET_ADDR_LEN) {
                res = -EINVAL;
            }
            else {
                _get_mac_addr(dev, (uint8_t*)value);
                res = ETHERNET_ADDR_LEN;
            }
            break;
        case NETOPT_ADDR_LEN:
        case NETOPT_SRC_LEN:
            assert(max_len == 2);
            uint16_t *tgt = (uint16_t*)value;
            *tgt=6;
            res = sizeof(uint16_t);
            break;
        case NETOPT_PROMISCUOUSMODE:
            *((bool*)value) = (bool)_get_promiscous(dev);
            res = sizeof(bool);
            break;
        case NETOPT_IPV6_IID:
            return _get_iid(dev, value, max_len);
        case NETOPT_IS_WIRED:
            res = 1;
            break;
{'fraction_non_alphanumeric': 0.12315515316879573, 'fraction_numerical': 0.016867171028153292, 'mean_word_length': 2.7966101694915255, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5882948', 'n_tokens_mistral': 3121, 'n_tokens_neox': 3080, 'n_words': 672}