-
Notifications
You must be signed in to change notification settings - Fork 0
/
leases.c
175 lines (159 loc) · 4.69 KB
/
leases.c
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* natpmp - an implementation of NAT-PMP
* Copyright (C) 2007 Adrian Friedli
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdlib.h>
#ifdef DEBUG_LEASES
#include <arpa/inet.h>
#include <stdio.h>
#endif
#include "die.h"
#include "leases.h"
/* linked list of leases */
struct lease *first = NULL;
struct lease *last = NULL;
/* time the next leases expire */
uint32_t next_lease_expires = UINT32_MAX;
/* indicates if the next_lease_expires has to be updated, e.g. on change or
* removal of a lease */
int update_expires = 0; /* TODO ??? */
/* function that adds a lease to the list of leases */
struct lease *add_lease(const struct lease *a)
{
struct lease *new = malloc(sizeof(struct lease));
if (new == NULL) p_die("malloc");
*new = *a;
if (last) last->next = new;
else first = new;
new->prev = last;
new->next = NULL;
last = new;
return new;
}
/* function that removes a lease from the list of leases */
void remove_lease(struct lease *a)
{
if (a->prev == NULL && a->next == NULL) {
first = NULL;
last = NULL;
}
else {
if (a->prev) a->prev->next = a->next;
else first = a->next;
if (a->next) a->next->prev = a->prev;
else last = a->prev;
}
free(a);
}
/* function that returns a lease pointer by public port number, NULL if public
* port number is still unmapped */
struct lease *get_lease_by_port(const uint16_t port)
{
if (first == NULL) return NULL;
struct lease *a = first;
do {
if (a->public_port == port) return a;
} while ((a = a->next));
return NULL;
}
/* function that returns a lease pointer by client ip address and private port
* numnber, NULL if no lease found */
struct lease *get_lease_by_client_port(const uint32_t client,
const uint16_t port)
{
if (first == NULL) return NULL;
struct lease *a = first;
do {
if (a->client == client && a->private_port == port) return a;
} while ((a = a->next));
return NULL;
}
/* function that returns a pointer to the next lease by client ip address, NULL
* if no leases found, prev is the pointer to the lease from where to search
* from, NULL to search from beginning */
struct lease *get_next_lease_by_client(const uint32_t client,
struct lease *prev)
{
if (first == NULL || (prev && prev->next == NULL)) return NULL;
struct lease *a = (prev) ? prev->next : first;
do {
if (a->client == client) return a;
} while ((a = a->next));
return NULL;
}
/* function that returns a pointer to the next expired lease, NULL if no leases
* found, provide the actual time with now, prev is the pointer to the lease
* from where to search from, NULL to search from beginning */
struct lease *get_next_expired_lease(const uint32_t now,
struct lease *prev)
{
if (first == NULL || (prev && prev->next == NULL)) return NULL;
struct lease *a = (prev) ? prev->next : first;
do {
if (a->expires[1] <= now) return a;
if (a->expires[2] <= now) return a;
} while ((a = a->next));
return NULL;
}
/* function that updates the next_lease_expires variables */
void do_update_expires()
{
if (!update_expires) return;
next_lease_expires = UINT32_MAX;
if (first) {
struct lease *a = first;
do {
if (a->expires[1] < next_lease_expires)
next_lease_expires = a->expires[1];
if (a->expires[2] < next_lease_expires)
next_lease_expires = a->expires[2];
} while ((a = a->next));
}
update_expires = 0;
#ifdef DEBUG_LEASES
if (debuglevel >= 2)
printf("next lease expires: %u\n", next_lease_expires);
#endif
}
#ifdef DEBUG_LEASES
/* function that prints a lease */
void print_lease(const struct lease *a)
{
struct in_addr client = { a->client };
printf("Client: %s, UDP-Expires: %u, TCP-Expires: %u, Private: %hu, "
"Public: %hu\n",
inet_ntoa(client),
a->expires[1],
a->expires[2],
ntohs(a->private_port),
ntohs(a->public_port));
}
/* function to print all leases */
void print_leases()
{
printf("----LEASES----\n");
if (first) {
struct lease *a = first;
do {
print_lease(a);
} while ((a = a->next));
}
printf("--------------\n");
if (update_expires == 0)
printf("next lease expires: %u\n", next_lease_expires);
}
#endif