addr_hash.c 2.46 KB
Newer Older
pdw's avatar
pdw committed
1 2 3 4 5 6
/* hash table */

#include <stdio.h>
#include <stdlib.h>
#include "addr_hash.h"
#include "hash.h"
chris's avatar
chris committed
7
#include "iftop.h"
pdw's avatar
pdw committed
8 9 10 11 12 13

#define hash_table_size 256

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

    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's avatar
pdw committed
27
    return (aa->src.s_addr == bb->src.s_addr 
28 29
            && aa->src_port == bb->src_port
            && aa->dst.s_addr == bb->dst.s_addr
30 31
            && aa->dst_port == bb->dst_port
            && aa->protocol == bb->protocol);
pdw's avatar
pdw committed
32 33
}

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

pdw's avatar
pdw committed
41 42 43
int hash(void* key) {
    int hash;
    addr_pair* ap = (addr_pair*)key;
44 45

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

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

54
        addr6 = (uint32_t*)ap->dst6.s6_addr;
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        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's avatar
pdw committed
70 71 72 73 74

    return hash;
}

void* copy_key(void* orig) {
chris's avatar
chris committed
75 76
    addr_pair* copy;
    copy = xmalloc(sizeof *copy);
pdw's avatar
pdw committed
77 78 79 80 81 82 83 84 85 86 87 88 89
    *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's avatar
chris committed
90
    hash_table = xcalloc(hash_table_size, sizeof *hash_table);
pdw's avatar
pdw committed
91 92 93 94 95 96 97 98 99
    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;
}