Blame view

addr_hash.c 2.48 KB
pdw committed
1 2 3 4
/* hash table */

#include <stdio.h>
#include <stdlib.h>
5
#include <string.h>
pdw committed
6 7
#include "addr_hash.h"
#include "hash.h"
chris committed
8
#include "iftop.h"
pdw committed
9 10 11 12 13 14

#define hash_table_size 256

int compare(void* a, void* b) {
    addr_pair* aa = (addr_pair*)a;
    addr_pair* bb = (addr_pair*)b;
15 16 17 18 19 20 21 22 23 24 25 26 27

    if (aa->af != bb->af)
        return 0;

    if (aa->af == AF_INET6) {
       return (IN6_ARE_ADDR_EQUAL(&aa->src6, &bb->src6)
               && aa->src_port == bb->src_port
               && IN6_ARE_ADDR_EQUAL(&aa->dst6, &bb->dst6)
               && aa->dst_port == bb->dst_port
               && aa->protocol == bb->protocol);
    }

    /* AF_INET or unknown. */
pdw committed
28
    return (aa->src.s_addr == bb->src.s_addr 
29 30
            && aa->src_port == bb->src_port
            && aa->dst.s_addr == bb->dst.s_addr
31 32
            && aa->dst_port == bb->dst_port
            && aa->protocol == bb->protocol);
pdw committed
33 34
}

35 36 37 38 39 40 41
static int __inline__ hash_uint32(uint32_t n) {
    return ((n & 0x000000FF)
            + ((n & 0x0000FF00) >> 8)
            + ((n & 0x00FF0000) >> 16)
            + ((n & 0xFF000000) >> 24));
}

pdw committed
42 43 44
int hash(void* key) {
    int hash;
    addr_pair* ap = (addr_pair*)key;
45 46

    if (ap->af == AF_INET6) {
47
        uint32_t* addr6 = (uint32_t*)ap->src6.s6_addr;
48 49 50 51 52 53 54

        hash = ( hash_uint32(addr6[0])
                + hash_uint32(addr6[1])
                + hash_uint32(addr6[2])
                + hash_uint32(addr6[3])
                + ap->src_port) % 0xFF;

55
        addr6 = (uint32_t*)ap->dst6.s6_addr;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        hash = ( hash + hash_uint32(addr6[0])
                + hash_uint32(addr6[1])
                + hash_uint32(addr6[2])
                + hash_uint32(addr6[3])
                + ap->dst_port) % 0xFF;
    } else {
        in_addr_t addr = ap->src.s_addr;

        hash = ( hash_uint32(addr)
                + ap->src_port) % 0xFF;

        addr = ap->dst.s_addr;
        hash = ( hash + hash_uint32(addr)
                + ap->dst_port) % 0xFF;
    }
pdw committed
71 72 73 74 75

    return hash;
}

void* copy_key(void* orig) {
chris committed
76 77
    addr_pair* copy;
    copy = xmalloc(sizeof *copy);
pdw committed
78 79 80 81 82 83 84 85 86 87 88 89 90
    *copy = *(addr_pair*)orig;
    return copy;
}

void delete_key(void* key) {
    free(key);
}

/*
 * Allocate and return a hash
 */
hash_type* addr_hash_create() {
    hash_type* hash_table;
chris committed
91
    hash_table = xcalloc(hash_table_size, sizeof *hash_table);
pdw committed
92 93 94 95 96 97 98 99 100
    hash_table->size = hash_table_size;
    hash_table->compare = &compare;
    hash_table->hash = &hash;
    hash_table->delete_key = &delete_key;
    hash_table->copy_key = &copy_key;
    hash_initialise(hash_table);
    return hash_table;
}