<commit_before>/*
Copyright (c) 2007, 2008 by <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

#include <arpa/inet.h>

#include "babel.h"
#include "util.h"
#include "net.h"
#include "network.h"
#include "source.h"
#include "neighbour.h"
#include "route.h"
#include "xroute.h"
#include "request.h"
#include "message.h"
#include "filter.h"

struct timeval update_flush_timeout = {0, 0};

const unsigned char packet_header[8] = {42, 1};

int parasitic = 0;
int silent_time = 30;
int split_horizon = 1;

unsigned short myseqno = 0;
struct timeval seqno_time = {0, 0};
int seqno_interval = -1;

struct buffered_update {
    unsigned char prefix[16];
    unsigned char plen;
};
struct buffered_update buffered_updates[MAX_BUFFERED_UPDATES];
struct network *update_net = NULL;
int updates = 0;

unsigned short
hash_id(const unsigned char *id)
{
    int i;
    unsigned short hash = 0;
    for(i = 0; i < 8; i++)
        hash ^= (id[2 * i] << 8) | id[2 * i + 1];
    return hash;
}

void
parse_packet(const unsigned char *from, struct network *net,
             const unsigned char *packet, int len)
{
    int i, j;
    const unsigned char *message;
    unsigned char type, plen, hop_count;
    unsigned short seqno, metric;
    const unsigned char *address;
    struct neighbour *neigh;
    int have_current_source = 0;
    unsigned char current_source[16];

    if(from[0] != 0xFE || (from[1] & 0xC0) != 0x80) {
        fprintf(stderr, "Received packet from non-local address %s.\n",
                format_address(from));
        return;
    }

    if(packet[0] != 42) {
        fprintf(stderr, "Received malformed packet on %s from %s.\n",
                net->ifname, format_address(from));
        return;
    }

    if(packet[1] != 1) {
        fprintf(stderr,
                "Received packet with unknown version %d on %s from %s.\n",
                packet[1], net->ifname, format_address(from));
        return;
    }

    if(len % 24 != 8) {
        fprintf(stderr, "Received malformed packet on %s from %s.\n",
                net->ifname, format_address(from));
        return;
    }

    j = 0;
    for(i = 0; i < (len - 8) / 24; i++) {
        message = packet + 8 + 24 * i;
        type = message[0];
        plen = message[1];
        hop_count = message[3];
        seqno = ntohs(*(uint16_t*)(message + 4));
        metric = ntohs(*(uint16_t*)(message + 6));
        address = message + 8;
        if(type == 0) {
            int changed;
            if(memcmp(address, myid, 16) == 0)
                continue;
            debugf("Received hello (%d) on %s from %s (%s).\n",
                   metric, net->ifname,
                   format_address(address),
                   format_address(from));
            net->activity_time = now.tv_sec;
            neigh = add_neighbour(address, from, net);
            if(neigh == NULL)
                continue;
            changed = update_neighbour(neigh, seqno, metric);
            if(changed)
                update_neighbour_metric(neigh);
        } else {
            neigh = find_neighbour(from, net);
            if(neigh == NULL)
                continue;
            net->activity_time = now.tv_sec;
            if(type == 1) {
                debugf("Received ihu %d for %s from %s (%s) %d.\n",
                       metric,
                       format_address(address),
                       format_address(neigh->id),
                       format_address(from), seqno);
                if(memcmp(myid, address, 16) == 0) {
                    neigh->txcost = metric;
                    neigh->ihu_time = now;
                    neigh->ihu_interval = seqno;
                    update_neighbour_metric(neigh);
                }
            } else if(type == 2) {
                debugf("Received request on %s from %s (%s) for %s "
                       "(%d hops).\n",
                       net->ifname,
                       format_address(neigh->id),
                       format_address(from),
                       plen == 0xFF ?
                       "any" :<commit_msg>Check for idle networks when receiving a hello.<commit_after>/*
Copyright (c) 2007, 2008 by <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

#include <arpa/inet.h>

#include "babel.h"
#include "util.h"
#include "net.h"
#include "network.h"
#include "source.h"
#include "neighbour.h"
#include "route.h"
#include "xroute.h"
#include "request.h"
#include "message.h"
#include "filter.h"

struct timeval update_flush_timeout = {0, 0};

const unsigned char packet_header[8] = {42, 1};

int parasitic = 0;
int silent_time = 30;
int split_horizon = 1;

unsigned short myseqno = 0;
struct timeval seqno_time = {0, 0};
int seqno_interval = -1;

struct buffered_update {
    unsigned char prefix[16];
    unsigned char plen;
};
struct buffered_update buffered_updates[MAX_BUFFERED_UPDATES];
struct network *update_net = NULL;
int updates = 0;

unsigned short
hash_id(const unsigned char *id)
{
    int i;
    unsigned short hash = 0;
    for(i = 0; i < 8; i++)
        hash ^= (id[2 * i] << 8) | id[2 * i + 1];
    return hash;
}

void
parse_packet(const unsigned char *from, struct network *net,
             const unsigned char *packet, int len)
{
    int i, j;
    const unsigned char *message;
    unsigned char type, plen, hop_count;
    unsigned short seqno, metric;
    const unsigned char *address;
    struct neighbour *neigh;
    int have_current_source = 0;
    unsigned char current_source[16];

    if(from[0] != 0xFE || (from[1] & 0xC0) != 0x80) {
        fprintf(stderr, "Received packet from non-local address %s.\n",
                format_address(from));
        return;
    }

    if(packet[0] != 42) {
        fprintf(stderr, "Received malformed packet on %s from %s.\n",
                net->ifname, format_address(from));
        return;
    }

    if(packet[1] != 1) {
        fprintf(stderr,
                "Received packet with unknown version %d on %s from %s.\n",
                packet[1], net->ifname, format_address(from));
        return;
    }

    if(len % 24 != 8) {
        fprintf(stderr, "Received malformed packet on %s from %s.\n",
                net->ifname, format_address(from));
        return;
    }

    j = 0;
    for(i = 0; i < (len - 8) / 24; i++) {
        message = packet + 8 + 24 * i;
        type = message[0];
        plen = message[1];
        hop_count = message[3];
        seqno = ntohs(*(uint16_t*)(message + 4));
        metric = ntohs(*(uint16_t*)(message + 6));
        address = message + 8;
        if(type == 0) {
            int changed;
            if(memcmp(address, myid, 16) == 0)
                continue;
            debugf("Received hello (%d) on %s from %s (%s).\n",
                   metric, net->ifname,
                   format_address(address),
                   format_address(from));
            net->activity_time = now.tv_sec;
            update_hello_interval(net);
            neigh = add_neighbour(address, from, net);
            if(neigh == NULL)
                continue;
            changed = update_neighbour(neigh, seqno, metric);
            if(changed)
                update_neighbour_metric(neigh);
        } else {
            neigh = find_neighbour(from, net);
            if(neigh == NULL)
                continue;
            net->activity_time = now.tv_sec;
            if(type == 1) {
                debugf("Received ihu %d for %s from %s (%s) %d.\n",
                       metric,
                       format_address(address),
                       format_address(neigh->id),
                       format_address(from), seqno);
                if(memcmp(myid, address, 16) == 0) {
                    neigh->txcost = metric;
                    neigh->ihu_time = now;
                    neigh->ihu_interval = seqno;
                    update_neighbour_metric(neigh);
                }
            } else if(type == 2) {
                debugf("Received request on %s from %s (%s) for %s "
                       "(%d hops).\n",
                       net->ifname,
                       format_address(neigh->id),
                       format_address(from),
                       plen == 0xFF ?
                       "any" :
{'fraction_non_alphanumeric': 0.10865775711795468, 'fraction_numerical': 0.015494867325198529, 'mean_word_length': 2.119939577039275, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 39, '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': '3450887', 'n_tokens_mistral': 3213, 'n_tokens_neox': 2991, 'n_words': 1006}