aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/core/connection_table.c
blob: 0b7df03848d2a833b28991a52b599488da85db27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright (c) 2020 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * \file connection_table.c
 * \brief Implementation of hICN connection table
 */

#include <hicn/util/log.h>

#include "connection.h"
#include "connection_table.h"

/* This is only used as a hint for first allocation, as the table is resizeable */
#define DEFAULT_CONNECTION_TABLE_SIZE 64

connection_table_t *
_connection_table_create(size_t init_size, size_t max_size)
{
    if (init_size == 0)
        init_size = DEFAULT_CONNECTION_TABLE_SIZE;

    connection_table_t * table = malloc(sizeof(connection_table_t));
    if (!table)
        return NULL;

    table->max_size = max_size;

    /* Initialize indices */
    table->id_by_pair = kh_init_ct_pair();
    table->id_by_name = kh_init_ct_name();

    /*
     * We start by allocating a reasonably-sized pool, as this will eventually
     * be resized if needed.
     */
    pool_init(table->connections, init_size);

    return table;
}

void
connection_table_free(connection_table_t * table)
{
    kh_destroy_ct_pair(table->id_by_pair);
    kh_destroy_ct_name(table->id_by_name);
    pool_free(table->connections);
    free(table);
}

connection_t *
connection_table_get_by_pair(const connection_table_t * table,
        const address_pair_t * pair)
{
  khiter_t k = kh_get_ct_pair(table->id_by_pair, pair);
  if (k == kh_end(table->id_by_pair))
      return NULL;
  return table->connections + kh_val(table->id_by_pair, k);
}

off_t
connection_table_get_id_by_name(const connection_table_t * table,
        const char * name)
{
    khiter_t k = kh_get_ct_name(table->id_by_name, name);
    if (k == kh_end(table->id_by_name))
        return CONNECTION_ID_UNDEFINED;
    return kh_val(table->id_by_name, k);
}

connection_t *
connection_table_get_by_name(const connection_table_t * table,
        const char * name)
{
    unsigned conn_id = connection_table_get_id_by_name(table, name);
    if (!connection_id_is_valid(conn_id))
        return NULL;
    return table->connections + conn_id;
}

void
connection_table_remove_by_id(connection_table_t * table, off_t id)
{
    /*
     * Get the connection addresspair & name so as to be able to remove them
     * from the hash table index
     */
    connection_t * connection = connection_table_at(table, id);
    connection_table_deallocate(table, connection);
}