<commit_before>#include <stdio.h>
#include <string.h>
#include <vtimer.h>
#include <mutex.h>
#include "msg.h"
#include "sixlowip.h"
#include "sixlowmac.h"
#include "sixlownd.h"
#include "sixlowpan.h"

msg msg_queue[IP_PKT_RECV_BUF_SIZE];
struct ipv6_hdr_t* ipv6_buf;
struct icmpv6_hdr_t* icmp_buf;
uint8_t ipv6_ext_hdr_len;
uint8_t *nextheader;
iface_t iface;
uint8_t iface_addr_list_count = 0;

//mutex_t buf_mutex;

struct ipv6_hdr_t* get_ipv6_buf(void){
    return ((struct ipv6_hdr_t*)&(buffer[LL_HDR_LEN]));
}

struct icmpv6_hdr_t* get_icmpv6_buf(uint8_t ext_len){
    return ((struct icmpv6_hdr_t*)&(buffer[LLHDR_IPV6HDR_LEN + ext_len]));
}

uint8_t * get_payload_buf(uint8_t ext_len){
    return &(buffer[LLHDR_IPV6HDR_LEN + ext_len]);
}

void sixlowpan_bootstrapping(void){
    
    init_rtr_sol(OPT_SLLAO);
}

void sixlowpan_send(ipv6_addr_t *addr, uint8_t *payload, uint16_t p_len){
    ipv6_buf = get_ipv6_buf();
    icmp_buf = get_icmpv6_buf(ipv6_ext_hdr_len);
    packet_length = 0;

    icmp_buf->type = ICMP_RTR_SOL;
    icmp_buf->code = 0;
    ipv6_buf->version_trafficclass = IPV6_VER;
    ipv6_buf->trafficclass_flowlabel = 0;
    ipv6_buf->flowlabel = 0;
    ipv6_buf->nextheader = PROTO_NUM_NONE;
    ipv6_buf->hoplimit = MULTIHOP_HOPLIMIT;
    ipv6_buf->length = p_len;    

    memcpy(&(ipv6_buf->destaddr), addr, 16);
    ipv6_get_saddr(&(ipv6_buf->srcaddr), &(ipv6_buf->destaddr));

    uint8_t *p_ptr = get_payload_buf(ipv6_ext_hdr_len); 
    //*p_ptr = payload;

    memcpy(p_ptr,payload,p_len);

    packet_length = IPV6_HDR_LEN + p_len;

    lowpan_init((ieee_802154_long_t*)&(ipv6_buf->destaddr.uint16[4]),(uint8_t*)ipv6_buf); 
}

void ipv6_process(void){
    msg m;
    msg_init_queue(msg_queue, IP_PKT_RECV_BUF_SIZE);
    
    while(1){
        msg_receive(&m);

        ipv6_buf = get_ipv6_buf();
        ipv6_buf = (struct ipv6_hdr_t*) m.content.ptr;

        /* identifiy packet */
        nextheader = &ipv6_buf->nextheader;
        
        switch(*nextheader) {
            case(PROTO_NUM_ICMPV6):{
                /* checksum test*/
                if(icmpv6_csum(PROTO_NUM_ICMPV6) != 0xffff){
                    printf("ERROR: wrong checksum\n");
                }
                icmp_buf = get_icmpv6_buf(ipv6_ext_hdr_len);
                switch(icmp_buf->type) {
                    case(ICMP_RTR_SOL):{
                        printf("INFO: packet type: icmp router solicitation\n");
                        /* processing router solicitation */
                        recv_rtr_sol();    
                        /* init solicited router advertisment*/
                        break;
                    }
                    case(ICMP_RTR_ADV):{
                        printf("INFO: packet type: icmp router advertisment\n");
                        /* processing router advertisment */
                        recv_rtr_adv();
                        /* init neighbor solicitation */
                        break;
                    }
                    case(ICMP_NBR_SOL):{
                        printf("INFO: packet type: icmp neighbor solicitation\n");
                        recv_nbr_sol();
                        break;
                    }
                    case(ICMP_NBR_ADV):{
                        printf("INFO: packet type: icmp neighbor advertisment\n");
                    }
                    default:
                        break;
                }            
                break;
            }
            case(PROTO_NUM_NONE):{
                //printf("Packet with no Header following the IPv6 Header received\n");
                //uint8_t *ptr = get_payload_buf(ipv6_ext_hdr_len);
                printf("hello\n");
            }
            default:
                break;
        }
    }   
}

void ipv6_iface_add_addr(ipv6_addr_t *addr, uint8_t state, uint32_t val_ltime,
                         uint32_t pref_ltime, uint8_t type){
    {
        ipv6_addr_t unspec;
        memset(&unspec,0,sizeof (ipv6_addr_t));
        if(ipv6_get_addr_match(addr,&unspec) == 128){
            printf("ERROR: unspecified address (::) can't be assigned to interface.\n");
            return;
        }
    }
    if(iface_addr_list_count < IFACE_ADDR_LIST_LEN){
        memcpy(&(iface.addr_list[iface_addr_list_count].addr.uint8[0]), 
               &(addr->uint8[0]), 16);
        iface.addr_list[iface_addr_list_count].state = state;
        timex_t vtime;
        vtime.seconds = val_ltime;
        timex_t prlt;
        prlt.seconds = pref_ltime;
        vtimer_set_sec(&(iface.addr_list[iface_addr_list_count].val_ltime), vtime);
        vtimer_set_sec(&(iface.addr_list[iface_addr_list_count].pref_ltime), prlt);
        iface.addr_list[iface_addr_list_count].type = type;
        iface_addr_list_count++;
        // Register to Solicited-Node multicast address according to RFC 4291
        if (type == ADDR_TYPE_ANYCAST || type == ADDR_TYPE_LINK_LOCAL ||
            type == ADDR_TYPE_GLOBAL || type == ADDR_TYPE_UNICAST) {
            ipv6_addr_t sol_node_mcast_addr;
            ipv6_set_sol_node_mcast_addr(addr, &sol_node_mcast_addr);
            if (ipv6_iface_addr_match(&sol_node_mcast_addr) == NULL) {
                ipv6_iface_add_addr(&sol_node_mcast_addr, state, val_ltime, pref_ltime, ADDR_TYPE_SOL_NODE_MCAST);
            }
        }<commit_msg>[sys/net/sixlowpan/sixlowip.c] * Check for unspecified address by the appropriate function<commit_after>#include <stdio.h>
#include <string.h>
#include <vtimer.h>
#include <mutex.h>
#include "msg.h"
#include "sixlowip.h"
#include "sixlowmac.h"
#include "sixlownd.h"
#include "sixlowpan.h"

msg msg_queue[IP_PKT_RECV_BUF_SIZE];
struct ipv6_hdr_t* ipv6_buf;
struct icmpv6_hdr_t* icmp_buf;
uint8_t ipv6_ext_hdr_len;
uint8_t *nextheader;
iface_t iface;
uint8_t iface_addr_list_count = 0;

//mutex_t buf_mutex;

struct ipv6_hdr_t* get_ipv6_buf(void){
    return ((struct ipv6_hdr_t*)&(buffer[LL_HDR_LEN]));
}

struct icmpv6_hdr_t* get_icmpv6_buf(uint8_t ext_len){
    return ((struct icmpv6_hdr_t*)&(buffer[LLHDR_IPV6HDR_LEN + ext_len]));
}

uint8_t * get_payload_buf(uint8_t ext_len){
    return &(buffer[LLHDR_IPV6HDR_LEN + ext_len]);
}

void sixlowpan_bootstrapping(void){
    
    init_rtr_sol(OPT_SLLAO);
}

void sixlowpan_send(ipv6_addr_t *addr, uint8_t *payload, uint16_t p_len){
    ipv6_buf = get_ipv6_buf();
    icmp_buf = get_icmpv6_buf(ipv6_ext_hdr_len);
    packet_length = 0;

    icmp_buf->type = ICMP_RTR_SOL;
    icmp_buf->code = 0;
    ipv6_buf->version_trafficclass = IPV6_VER;
    ipv6_buf->trafficclass_flowlabel = 0;
    ipv6_buf->flowlabel = 0;
    ipv6_buf->nextheader = PROTO_NUM_NONE;
    ipv6_buf->hoplimit = MULTIHOP_HOPLIMIT;
    ipv6_buf->length = p_len;    

    memcpy(&(ipv6_buf->destaddr), addr, 16);
    ipv6_get_saddr(&(ipv6_buf->srcaddr), &(ipv6_buf->destaddr));

    uint8_t *p_ptr = get_payload_buf(ipv6_ext_hdr_len); 
    //*p_ptr = payload;

    memcpy(p_ptr,payload,p_len);

    packet_length = IPV6_HDR_LEN + p_len;

    lowpan_init((ieee_802154_long_t*)&(ipv6_buf->destaddr.uint16[4]),(uint8_t*)ipv6_buf); 
}

void ipv6_process(void){
    msg m;
    msg_init_queue(msg_queue, IP_PKT_RECV_BUF_SIZE);
    
    while(1){
        msg_receive(&m);

        ipv6_buf = get_ipv6_buf();
        ipv6_buf = (struct ipv6_hdr_t*) m.content.ptr;

        /* identifiy packet */
        nextheader = &ipv6_buf->nextheader;
        
        switch(*nextheader) {
            case(PROTO_NUM_ICMPV6):{
                /* checksum test*/
                if(icmpv6_csum(PROTO_NUM_ICMPV6) != 0xffff){
                    printf("ERROR: wrong checksum\n");
                }
                icmp_buf = get_icmpv6_buf(ipv6_ext_hdr_len);
                switch(icmp_buf->type) {
                    case(ICMP_RTR_SOL):{
                        printf("INFO: packet type: icmp router solicitation\n");
                        /* processing router solicitation */
                        recv_rtr_sol();    
                        /* init solicited router advertisment*/
                        break;
                    }
                    case(ICMP_RTR_ADV):{
                        printf("INFO: packet type: icmp router advertisment\n");
                        /* processing router advertisment */
                        recv_rtr_adv();
                        /* init neighbor solicitation */
                        break;
                    }
                    case(ICMP_NBR_SOL):{
                        printf("INFO: packet type: icmp neighbor solicitation\n");
                        recv_nbr_sol();
                        break;
                    }
                    case(ICMP_NBR_ADV):{
                        printf("INFO: packet type: icmp neighbor advertisment\n");
                    }
                    default:
                        break;
                }            
                break;
            }
            case(PROTO_NUM_NONE):{
                //printf("Packet with no Header following the IPv6 Header received\n");
                //uint8_t *ptr = get_payload_buf(ipv6_ext_hdr_len);
                printf("hello\n");
            }
            default:
                break;
        }
    }   
}

void ipv6_iface_add_addr(ipv6_addr_t *addr, uint8_t state, uint32_t val_ltime,
                         uint32_t pref_ltime, uint8_t type){
    if(ipv6_addr_unspec_match(addr) == 128){
        printf("ERROR: unspecified address (::) can't be assigned to interface.\n");
        return;
    }
    if(iface_addr_list_count < IFACE_ADDR_LIST_LEN){
        memcpy(&(iface.addr_list[iface_addr_list_count].addr.uint8[0]), 
               &(addr->uint8[0]), 16);
        iface.addr_list[iface_addr_list_count].state = state;
        timex_t vtime;
        vtime.seconds = val_ltime;
        timex_t prlt;
        prlt.seconds = pref_ltime;
        vtimer_set_sec(&(iface.addr_list[iface_addr_list_count].val_ltime), vtime);
        vtimer_set_sec(&(iface.addr_list[iface_addr_list_count].pref_ltime), prlt);
        iface.addr_list[iface_addr_list_count].type = type;
        iface_addr_list_count++;
        // Register to Solicited-Node multicast address according to RFC 4291
        if (type == ADDR_TYPE_ANYCAST || type == ADDR_TYPE_LINK_LOCAL ||
            type == ADDR_TYPE_GLOBAL || type == ADDR_TYPE_UNICAST) {
            ipv6_addr_t sol_node_mcast_addr;
            ipv6_set_sol_node_mcast_addr(addr, &sol_node_mcast_addr);
            if (ipv6_iface_addr_match(&sol_node_mcast_addr) == NULL) {
                ipv6_iface_add_addr(&sol_node_mcast_addr, state, val_ltime, pref_ltime, ADDR_TYPE_SOL_NODE_MCAST);
            }
        }
{'fraction_non_alphanumeric': 0.10499624342599549, 'fraction_numerical': 0.01925244177310293, 'mean_word_length': 2.10466472303207, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 41, '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': '6060572', 'n_tokens_mistral': 3851, 'n_tokens_neox': 3733, 'n_words': 560}