File: KeyList.cpp

package info (click to toggle)
dibbler 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,352 kB
  • sloc: cpp: 60,323; ansic: 12,235; sh: 11,951; yacc: 3,418; lex: 969; makefile: 940; perl: 319; xml: 116; python: 74
file content (86 lines) | stat: -rw-r--r-- 2,135 bytes parent folder | download | duplicates (3)
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
/*
 * Dibbler - a portable DHCPv6
 *
 * author : Michal Kowalczuk <michal@kowalczuk.eu>
 * changes: Tomasz Mrugalski <thomson(at)klub.com.pl>
 *
 * released under GNU GPL v2 only licence
 *
 */

#include "KeyList.h"
#include "Logger.h"
#include <stdio.h>

KeyList::~KeyList() {
    KeyListElement * tmp1 = beginning, * tmp2;
    while(tmp1) {
        tmp2 = tmp1;
        tmp1 = tmp1->next;
        delete tmp2->AuthInfoKey;
        delete tmp2;
    }
}

void KeyList::Add(uint32_t SPI, uint32_t AAASPI, char * AuthInfoKey) {
    char buf[100];
    sprintf(buf, "Auth: Key (SPI: 0x%8.8x, AAASPI: 0x%8.8x, pointer: %p) added to keylist.", SPI, AAASPI, AuthInfoKey);
    Log(Debug) << buf << LogEnd;

    if (AuthInfoKey == NULL) {
        Log(Error) << "Auth: AuthInfoKey is NULL. Probably internal error." << LogEnd;
        return;
    }

    KeyListElement * act = beginning;
    KeyListElement * act2 = NULL;
    while (act) {
        if (act->SPI == SPI) {
            Log(Debug) << "Auth: Strange, SPI already exists in KeyList" << LogEnd;
            return;
        }
        act2 = act;
        act = act->next;
    }

    KeyListElement * new_el = new KeyListElement;
    new_el->SPI = SPI;
    new_el->AAASPI = AAASPI;
    new_el->AuthInfoKey = new char[AUTHKEYLEN];
    memcpy(new_el->AuthInfoKey, AuthInfoKey, AUTHKEYLEN);
    new_el->next = NULL;
    if (act2)
        act2->next = new_el;
    else
        beginning = new_el;
}

char * KeyList::Get(uint32_t SPI) {
    KeyListElement * tmp = beginning;
    while (tmp) {
        if (tmp->SPI == SPI)
            return tmp->AuthInfoKey;
        tmp = tmp->next;
    }

    Log(Warning) << "Auth: Required key (SPI=" << SPI << ") not found." << LogEnd;
    return NULL;
}

void KeyList::Del(uint32_t SPI) {
    KeyListElement * tmp = beginning, * prev = NULL;
    while (tmp) {
        if (tmp->SPI == SPI) {
            if (prev)
                prev->next = tmp->next;
            else
                beginning = tmp->next;

            delete tmp->AuthInfoKey;
            delete tmp;
            return;
        }
        prev = tmp;
        tmp = tmp->next;
    }
}