Exemplo n.º 1
0
Arquivo: ip.c Projeto: A-L-E-X/RIOT
int is_our_address(ipv6_addr_t *addr)
{
    ipv6_net_if_ext_t *net_if_ext;
    ipv6_net_if_addr_t *myaddr;
    uint8_t prefix, suffix;
    int if_id = -1;

    DEBUGF("Is this my addres: %s?\n", ipv6_addr_to_str(addr_str, IPV6_MAX_ADDR_STR_LEN, addr));
    while ((if_id = net_if_iter_interfaces(if_id)) >= 0) {
        net_if_ext = ipv6_net_if_get_ext(if_id);
        myaddr = NULL;
        prefix = net_if_ext->prefix / 8;
        suffix = IPV6_ADDR_LEN - prefix;

        while ((myaddr = (ipv6_net_if_addr_t *)net_if_iter_addresses(if_id,
                         (net_if_addr_t **) &myaddr)) != NULL) {
            DEBUGF("\tCompare with: %s?\n", ipv6_addr_to_str(addr_str, IPV6_MAX_ADDR_STR_LEN, (ipv6_addr_t*) myaddr->addr_data));
            if ((ipv6_get_addr_match(myaddr->addr_data, addr) >= net_if_ext->prefix) &&
                (memcmp(&addr->uint8[prefix], &myaddr->addr_data->uint8[prefix], suffix) == 0)) {
                return 1;
            }
        }
    }

    return 0;
}
Exemplo n.º 2
0
Arquivo: ip.c Projeto: A-L-E-X/RIOT
ipv6_net_if_hit_t *ipv6_net_if_addr_match(ipv6_net_if_hit_t *hit,
        const ipv6_addr_t *addr)
{
    int if_id = -1;
    ipv6_net_if_addr_t *addr_entry = NULL;

    while ((if_id = net_if_iter_interfaces(if_id)) >= 0) {
        while (net_if_iter_addresses(if_id, (net_if_addr_t **) &addr_entry) != NULL) {
            if (addr_entry->addr_protocol & NET_IF_L3P_IPV6) {
                uint8_t byte_al = addr_entry->addr_len / 8;
                uint8_t mask[] = {0x00, 0x80, 0xc0, 0xe0,
                                  0xf0, 0xf8, 0xfc, 0xfe
                                 };

                if (memcmp(addr_entry->addr_data, addr, byte_al) == 0 &&
                    (addr_entry->addr_len % 8 == 0 ||
                     ((addr_entry->addr_data->uint8[byte_al] - addr->uint8[byte_al]) & mask[addr_entry->addr_len - (byte_al * 8)]))) {
                    hit->if_id = if_id;
                    hit->addr = addr_entry;
                    return hit;
                }
            }
        }
    }

    return NULL;
}
Exemplo n.º 3
0
Arquivo: ip.c Projeto: Troels51/RIOT
/**
 * @brief Check if the given IPv6 address is assigned to any configured
 *        interface
 *
 * @param[in] addr  The IPv6 address to check
 *
 * @return 1    If *addr* is assigned to at least one interface
 * @return 0    If *addr* is not assigned to any interface
 * @return -1   If no IPv6 address is configured to any interface
 */
static int is_our_address(ipv6_addr_t *addr)
{
    int if_id = -1;
    int if_counter = -1;

    DEBUGF("Is this my addres: %s?\n", ipv6_addr_to_str(addr_str, IPV6_MAX_ADDR_STR_LEN, addr));
    while ((if_id = net_if_iter_interfaces(if_id)) >= 0) {
        ipv6_net_if_ext_t *net_if_ext = ipv6_net_if_get_ext(if_id);
        ipv6_net_if_addr_t *myaddr = NULL;
        uint8_t prefix = net_if_ext->prefix / 8;
        uint8_t suffix = IPV6_ADDR_LEN - prefix;

        while ((myaddr = (ipv6_net_if_addr_t *)net_if_iter_addresses(if_id,
                         (net_if_addr_t **) &myaddr)) != NULL) {
            if_counter++;
            DEBUGF("\tCompare with: %s?\n", ipv6_addr_to_str(addr_str, IPV6_MAX_ADDR_STR_LEN, (ipv6_addr_t*) myaddr->addr_data));
            if ((ipv6_get_addr_match(myaddr->addr_data, addr) >= net_if_ext->prefix) &&
                (memcmp(&addr->uint8[prefix], &myaddr->addr_data->uint8[prefix], suffix) == 0)) {
                return 1;
            }
        }
    }

    /* return negative value if no address is configured so far */
    if (if_counter >= 0) {
        return 0;
    }
    return -1;
}
Exemplo n.º 4
0
Arquivo: ip.c Projeto: A-L-E-X/RIOT
void ipv6_net_if_get_best_src_addr(ipv6_addr_t *src, const ipv6_addr_t *dest)
{
    /* try to find best match if dest is not mcast or link local */
    int if_id = 0; // TODO: get this somehow
    uint8_t tmp = 0;
    uint8_t bmatch = 0;
    ipv6_net_if_addr_t *addr = NULL;
    ipv6_net_if_addr_t *tmp_addr = NULL;

    if (!(ipv6_addr_is_link_local(dest)) && !(ipv6_addr_is_multicast(dest))) {
        while ((addr = (ipv6_net_if_addr_t *)net_if_iter_addresses(if_id,
                       (net_if_addr_t **)&addr))) {
            if (addr->ndp_state == NDP_ADDR_STATE_PREFERRED) {
                if (!ipv6_addr_is_link_local(addr->addr_data) &&
                    !ipv6_addr_is_multicast(addr->addr_data) &&
                    !ipv6_addr_is_unique_local_unicast(addr->addr_data)) {
                    tmp = ipv6_get_addr_match(dest, addr->addr_data);

                    if (tmp >= bmatch) {
                        bmatch = tmp;
                        tmp_addr = addr;
                    }
                }
            }
        }
    }
    else {
        while ((addr = (ipv6_net_if_addr_t *)net_if_iter_addresses(if_id,
                       (net_if_addr_t **)&addr))) {
            if (addr->ndp_state == NDP_ADDR_STATE_PREFERRED &&
                ipv6_addr_is_link_local(addr->addr_data) &&
                !ipv6_addr_is_multicast(addr->addr_data)) {
                tmp_addr = addr;
            }
        }
    }

    if (tmp_addr == NULL) {
        memset(src, 0, 16);
    }
    else {
        memcpy(src, tmp_addr->addr_data, 16);
    }
}
Exemplo n.º 5
0
int sixlowpan_lowpan_border_init(int if_id)
{
    ipv6_net_if_addr_t *addr = NULL;
    uint8_t abr_addr_initialized = 0;

    serial_reader_pid = thread_create(
                            serial_reader_stack, READER_STACK_SIZE,
                            PRIORITY_MAIN - 1, CREATE_STACKTEST,
                            serial_reader_f, "serial_reader");
    ip_process_pid = thread_create(ip_process_buf, IP_PROCESS_STACKSIZE,
                                   PRIORITY_MAIN - 1, CREATE_STACKTEST,
                                   border_process_lowpan,
                                   "border_process_lowpan");

    if (ip_process_pid < 0) {
        return 0;
    }

    if (!sixlowpan_lowpan_init_interface(if_id)) {
        return 0;
    }

    while (net_if_iter_addresses(if_id, (net_if_addr_t **) &addr)) {
        if (!ipv6_addr_is_multicast(addr->addr_data) &&
            !ipv6_addr_is_link_local(addr->addr_data) &&
            !ipv6_addr_is_loopback(addr->addr_data) &&
            !ipv6_addr_is_unique_local_unicast(addr->addr_data)) {
            abr_addr_initialized = 1;
            abr_addr = addr->addr_data;
            break;
        }
    }

    if (!abr_addr_initialized) {
        DEBUG("sixlowpan_lowpan_border_init(): A prefix must be initialized to"
              "interface %d first", if_id);
        return 0;
    }

    ipv6_init_as_router();

    return 1;
}
Exemplo n.º 6
0
Arquivo: ip.c Projeto: A-L-E-X/RIOT
ipv6_net_if_hit_t *ipv6_net_if_addr_prefix_eq(ipv6_net_if_hit_t *hit,
        ipv6_addr_t *addr)
{
    int if_id = -1;
    ipv6_net_if_addr_t *addr_entry = NULL;

    while ((if_id = net_if_iter_interfaces(if_id)) >= 0) {
        while (net_if_iter_addresses(if_id, (net_if_addr_t **) &addr_entry) != NULL) {
            if (addr_entry->addr_protocol & NET_IF_L3P_IPV6) {
                if (memcmp(addr_entry->addr_data, &addr, 8) == 0) {
                    hit->if_id = if_id;
                    hit->addr = addr_entry;
                    return hit;
                }
            }
        }
    }

    return NULL;
}
Exemplo n.º 7
0
int net_if_del_l3p_types(int if_id, net_if_l3p_t protocols)
{
    net_if_addr_t *addr_ptr = NULL;

    if (if_id < 0 || if_id >= NET_IF_MAX || !interfaces[if_id].initialized) {
        DEBUG("Remove L3 protocols: No interface initialized with ID %d.\n", if_id);
        return 0;
    }

    while (net_if_iter_addresses(if_id, &addr_ptr)) {
        if (addr_ptr->addr_protocol & protocols) {
            net_if_del_address(if_id, addr_ptr);
            addr_ptr = NULL;
        }
    }

    interfaces[if_id].protocols &= ~protocols;

    return 1;
}
Exemplo n.º 8
0
Arquivo: main.c Projeto: Rossano/RIOT
int test_net_if_del_address(int iface, net_if_addr_t *addr1,
                            net_if_addr_t *addr2)
{
    int count = 0;
    net_if_addr_t *addr_ptr = NULL;

    if (net_if_del_address(iface + 1, addr1)) {
        printf("FAILED: expected net_if_del_address(%d, %p) to fail.\n",
               iface + 1, (void *)addr1);
        return 0;
    }

    if (net_if_del_address(iface, NULL)) {
        printf("FAILED: expected net_if_del_address(%d, NULL) to fail.\n",
               iface);
        return 0;
    }

    if (!net_if_del_address(iface, addr1)) {
        printf("FAILED: Address deletion failed\n");
        return 0;
    }

    while (net_if_iter_addresses(iface, &addr_ptr)) {
        if (addr_ptr == addr1 || addr_ptr == addr2) {
            count++;
        }
    }

    if (count != 1) {
        printf("FAILED: expected 1 address in iface's address list\n");
        printf("        missing '%d'\n", 1 - count);
        return 0;
    }

    return 1;
}
Exemplo n.º 9
0
Arquivo: ip.c Projeto: Rossano/RIOT
int is_our_address(ipv6_addr_t *addr)
{
    ipv6_net_if_ext_t *net_if_ext;
    ipv6_net_if_addr_t *myaddr;
    uint8_t prefix, suffix;
    int if_id = -1;

    while ((if_id = net_if_iter_interfaces(if_id)) >= 0) {
        net_if_ext = ipv6_net_if_get_ext(if_id);
        myaddr = NULL;
        prefix = net_if_ext->prefix / 8;
        suffix = IPV6_ADDR_LEN - prefix;

        while ((myaddr = (ipv6_net_if_addr_t *)net_if_iter_addresses(if_id,
                         (net_if_addr_t **) &myaddr)) != NULL) {
            if ((ipv6_get_addr_match(myaddr->addr_data, addr) >= net_if_ext->prefix) &&
                (memcmp(&addr->uint8[prefix], &myaddr->addr_data->uint8[prefix], suffix) == 0)) {
                return 1;
            }
        }
    }

    return 0;
}
Exemplo n.º 10
0
Arquivo: main.c Projeto: Rossano/RIOT
int main(void)
{
    int iface;
    char *addr1_data = "abcdefgh", *addr2_data = "12345678";
    net_if_addr_t addr1 = {
        .addr_next = NULL,
        .addr_prev = NULL,
        .addr_protocol = NET_IF_L3P_IPV6_MULTICAST,
        .addr_data = (void *)addr1_data,
        .addr_len = (strlen(addr1_data) + 1) * 8
    };
    net_if_addr_t addr2 = {
        .addr_next = NULL,
        .addr_prev = NULL,
        .addr_protocol = NET_IF_L3P_IPV6_PREFIX,
        .addr_data = (void *)addr2_data,
        .addr_len = (strlen(addr2_data) + 1) * 8
    };
    uint16_t own = 1, target = 2;
    net_if_eui64_t eui64;

    iface = initialize_tests();

    if (!test_net_if_initialization(iface)) {
        printf("FAILED: test_net_if_initialization()\n");
        return -1;
    }

    if (!test_net_if_get_add_l3p_types(iface)) {
        printf("FAILED: test_net_if_get_add_l3p_types()\n");
        return -1;
    }

    if (!test_net_if_add_address(iface, &addr1, &addr2)) {
        printf("FAILED: test_net_if_add_address()\n");
        return -1;
    }

    if (!test_net_if_del_address(iface, &addr1, &addr2)) {
        printf("FAILED: test_net_if_del_address()\n");
        return -1;
    }

    if (!test_net_if_get_set_hardware_address(iface, own)) {
        printf("FAILED: test_net_if_get_set_hardware_address()\n");
        return -1;
    }

    if (!test_net_if_get_set_pan_id(iface)) {
        printf("FAILED: test_net_if_get_set_pan_id()\n");
        return -1;
    }

    if (!test_net_if_get_set_eui64(iface, &eui64, own)) {
        printf("FAILED: test_net_if_get_set_eui64()\n");
        return -1;
    }

    int count = net_if_send_packet(iface, target, "Test", 4);

    printf("Count was %i after net_if_send_packet()\n", count);

    printf("All test ran successfully.\n");

    return 0;
}

int initialize_tests(void)
{
    int iface;

#ifndef MODULE_AUTO_INIT
    transceiver_init(TRANSCEIVER);
    transceiver_start();
    net_if_init();
    iface = net_if_init_interface(0, TRANSCEIVER);
    return iface;
#else
    iface = -1;

    while ((iface = net_if_iter_interfaces(iface)) >= 0) {
        return iface;
    }

    return iface;
#endif
}

int test_net_if_initialization(int iface)
{
    net_if_addr_t *addr_ptr = NULL;

    if (net_if_get_l3p_types(iface)) {
        printf("FAILED: No L3 type expected on interface %d.\n", iface);
        return 0;
    }

    if (net_if_iter_addresses(iface + 1, &addr_ptr)) {
        printf("FAILED: Expected error on interface '%d'\n", iface + 1);
        return 0;
    }

    if (net_if_iter_addresses(iface, &addr_ptr)) {
        printf("FAILED: Expected error on interface '%d'\n", iface);
        return 0;
    }

    return 1;
}
Exemplo n.º 11
0
Arquivo: main.c Projeto: Rossano/RIOT
int test_net_if_add_address(int iface, net_if_addr_t *addr1,
                            net_if_addr_t *addr2)
{
    int count = 0;
    net_if_addr_t *addr_ptr = NULL;

    if (net_if_add_address(iface + 1, addr1)) {
        printf("FAILED: expected net_if_add_address(%d, %p) to fail.\n",
               iface + 1, (void *)addr1);
        return 0;
    }

    if (net_if_add_address(iface, NULL)) {
        printf("FAILED: expected net_if_add_address(%d, NULL) to fail.\n",
               iface);
        return 0;
    }

    if (!net_if_add_address(iface, addr1)) {
        printf("FAILED: Address addition failed\n");
        return 0;
    }

    if (!(net_if_get_l3p_types(iface) & NET_IF_L3P_IPV6_MULTICAST)) {
        printf("FAILED: L3 type IPv6 multicast expected on interface %d.\n", iface);
        return 0;
    }

    if (net_if_get_l3p_types(iface) & ~NET_IF_L3P_IPV6_MULTICAST) {
        printf("FAILED: L3 type other than IPv6 multicast not expected on interface %d.\n",
               iface);
        return 0;
    }

    if (!net_if_add_address(iface, addr2)) {
        printf("FAILED: Address addition failed\n");
        return 0;
    }

    if (!(net_if_get_l3p_types(iface) & NET_IF_L3P_IPV6_MULTICAST)) {
        printf("FAILED: L3 type IPv6 multcast expected on interface %d.\n", iface);
        return 0;
    }

    if (!(net_if_get_l3p_types(iface) & NET_IF_L3P_IPV6_PREFIX)) {
        printf("FAILED: L3 type IPv6 prefix expected on interface %d.\n", iface);
        return 0;
    }

    if (net_if_get_l3p_types(iface) & ~(NET_IF_L3P_IPV6_MULTICAST | NET_IF_L3P_IPV6_PREFIX)) {
        printf("FAILED: L3 type other than IPv6 multicast and IPv6 prefix not expected on interface %d.\n",
               iface);
        return 0;
    }

    while (net_if_iter_addresses(iface, &addr_ptr)) {
        if (addr_ptr == addr1  || addr_ptr == addr2) {
            count++;
        }
    }

    if (count != 2) {
        printf("FAILED: expected 2 addresses in iface's address list once respectively\n");
        printf("        missing '%d'\n", 2 - count);
        return 0;
    }

    return 1;
}