1 /*
2 * Copyright (C) 2014 Red Hat
3 *
4 * Author: Nikos Mavrogiannopoulos
5 *
6 * This file is part of ocserv.
7 *
8 * ocserv is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * ocserv is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <occtl/occtl.h>
27 #include <c-strcase.h>
28 #include <minmax.h>
29 #include <common.h>
30
31 typedef struct ip_entries_st {
32 char ip[MAX_IP_STR];
33 unsigned ip_size;
34 } ip_entries_st;
35
36 static ip_entries_st *ip_entries = NULL;
37 static unsigned ip_entries_size = 0;
38 static unsigned max_ip_entries_size = 0;
39
ip_entries_clear(void)40 void ip_entries_clear(void)
41 {
42 unsigned i;
43
44 for (i=0;i<ip_entries_size;i++) {
45 ip_entries[i].ip_size = 0;
46 }
47 ip_entries_size = 0;
48 }
49
ip_entries_add(void * pool,const char * ip,unsigned ip_size)50 void ip_entries_add(void *pool, const char* ip, unsigned ip_size)
51 {
52 if (ip_entries_size+1 > max_ip_entries_size) {
53 max_ip_entries_size += 128;
54 ip_entries = talloc_realloc_size(pool, ip_entries, sizeof(ip_entries_st)*max_ip_entries_size);
55 }
56
57 strlcpy(ip_entries[ip_entries_size].ip, ip, sizeof(ip_entries[ip_entries_size].ip));
58 ip_entries[ip_entries_size].ip_size = ip_size;
59 ip_entries_size++;
60
61 return;
62 }
63
search_for_ip(unsigned idx,const char * match,int match_size)64 char* search_for_ip(unsigned idx, const char* match, int match_size)
65 {
66 unsigned i;
67
68 if (idx >= ip_entries_size)
69 return NULL;
70
71 for (i=idx;i<ip_entries_size;i++) {
72 if (match_size <= ip_entries[i].ip_size) {
73 if (c_strncasecmp(match, ip_entries[i].ip, match_size) == 0)
74 return strdup(ip_entries[i].ip);
75 }
76 }
77
78 return NULL;
79 }
80