Blame
Date:
Sat Nov 8 20:28:31 2014 UTC
Message:
0.9.0 is the last version of Wildcarddnsd, the succeeding project is called Delphinusdnsd and will have its first release around November 15th, 2015. The new project page is at http://delphinusdns.centroid.eu.
001
2014-05-05
pjp
/*
002
2014-05-05
pjp
* Copyright (c) 2014 Peter J. Philipp
003
2014-05-05
pjp
* All rights reserved.
004
2014-05-05
pjp
*
005
2014-05-05
pjp
* Redistribution and use in source and binary forms, with or without
006
2014-05-05
pjp
* modification, are permitted provided that the following conditions
007
2014-05-05
pjp
* are met:
008
2014-05-05
pjp
* 1. Redistributions of source code must retain the above copyright
009
2014-05-05
pjp
* notice, this list of conditions and the following disclaimer.
010
2014-05-05
pjp
* 2. Redistributions in binary form must reproduce the above copyright
011
2014-05-05
pjp
* notice, this list of conditions and the following disclaimer in the
012
2014-05-05
pjp
* documentation and/or other materials provided with the distribution.
013
2014-05-05
pjp
* 3. The name of the author may not be used to endorse or promote products
014
2014-05-05
pjp
* derived from this software without specific prior written permission
015
2014-05-05
pjp
*
016
2014-05-05
pjp
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017
2014-05-05
pjp
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
018
2014-05-05
pjp
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
019
2014-05-05
pjp
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
020
2014-05-05
pjp
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
021
2014-05-05
pjp
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
022
2014-05-05
pjp
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
023
2014-05-05
pjp
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024
2014-05-05
pjp
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
025
2014-05-05
pjp
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026
2014-05-05
pjp
*
027
2014-05-05
pjp
*/
028
2014-05-05
pjp
#include "include.h"
029
2014-05-05
pjp
#include "dns.h"
030
2014-05-05
pjp
#include "db.h"
031
2014-05-05
pjp
032
2014-05-18
pjp
void add_rrlimit(int, u_int16_t *, int, char *);
033
2014-05-18
pjp
int check_rrlimit(int, u_int16_t *, int, char *);
034
2014-05-18
pjp
extern void dolog(int, char *, ...);
035
2014-05-18
pjp
static u_int16_t hash_rrlimit(u_int16_t *, int);
036
2014-05-18
pjp
char *rrlimit_setup(int);
037
2014-05-05
pjp
038
2014-05-05
pjp
struct rrlimit {
039
2014-05-05
pjp
u_int8_t pointer;
040
2014-05-05
pjp
time_t times[256];
041
2014-05-05
pjp
};
042
2014-05-05
pjp
043
2014-05-05
pjp
int ratelimit = 0;
044
2014-05-05
pjp
int ratelimit_packets_per_second = 6;
045
2014-05-05
pjp
046
2014-05-05
pjp
char *
047
2014-05-05
pjp
rrlimit_setup(int size)
048
2014-05-05
pjp
{
049
2014-05-05
pjp
char *ptr;
050
2014-05-05
pjp
051
2014-05-05
pjp
if (size > 255)
052
2014-05-05
pjp
return NULL;
053
2014-05-05
pjp
054
2014-05-05
pjp
size = 65536 * ((size * sizeof(time_t)) + sizeof(u_int8_t));
055
2014-05-05
pjp
056
2014-05-05
pjp
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED |\
057
2014-05-05
pjp
MAP_ANON, -1, 0);
058
2014-05-05
pjp
059
2014-05-05
pjp
if (ptr == MAP_FAILED) {
060
2014-05-05
pjp
dolog(LOG_ERR, "failed to setup rlimit mmap segment, exit\n");
061
2014-05-05
pjp
exit(1);
062
2014-05-05
pjp
}
063
2014-05-05
pjp
064
2014-05-05
pjp
memset(ptr, 0, size);
065
2014-05-05
pjp
066
2014-05-05
pjp
return (ptr);
067
2014-05-05
pjp
}
068
2014-05-05
pjp
069
2014-05-05
pjp
int
070
2014-05-05
pjp
check_rrlimit(int size, u_int16_t *ip, int sizeip, char *rrlimit_ptr)
071
2014-05-05
pjp
{
072
2014-05-05
pjp
struct rrlimit *rl;
073
2014-05-05
pjp
u_int16_t hash;
074
2014-05-05
pjp
int count = 0, i;
075
2014-05-05
pjp
u_int8_t offset;
076
2014-05-05
pjp
time_t now;
077
2014-05-05
pjp
char *tmp;
078
2014-05-05
pjp
079
2014-05-05
pjp
hash = hash_rrlimit(ip, sizeip);
080
2014-05-05
pjp
081
2014-05-05
pjp
tmp = rrlimit_ptr + (hash * ((size * sizeof(time_t)) + sizeof(u_int8_t)));
082
2014-05-05
pjp
rl = (struct rrlimit *)tmp;
083
2014-05-05
pjp
084
2014-05-05
pjp
offset = rl->pointer;
085
2014-05-05
pjp
086
2014-05-05
pjp
now = time(NULL);
087
2014-05-05
pjp
088
2014-05-05
pjp
for (i = 0; i < size; i++) {
089
2014-05-05
pjp
if (difftime(now, rl->times[(offset + i) % size]) <= 1)
090
2014-05-05
pjp
count++;
091
2014-05-05
pjp
else
092
2014-05-05
pjp
break;
093
2014-05-05
pjp
}
094
2014-05-05
pjp
095
2014-05-05
pjp
if (count > ratelimit_packets_per_second)
096
2014-05-05
pjp
return 1;
097
2014-05-05
pjp
098
2014-05-05
pjp
return 0;
099
2014-05-05
pjp
}
100
2014-05-05
pjp
101
2014-05-05
pjp
102
2014-05-05
pjp
void
103
2014-05-05
pjp
add_rrlimit(int size, u_int16_t *ip, int sizeip, char *rrlimit_ptr)
104
2014-05-05
pjp
{
105
2014-05-05
pjp
struct rrlimit *rl;
106
2014-05-05
pjp
u_int16_t hash;
107
2014-05-05
pjp
int offset;
108
2014-05-05
pjp
time_t now;
109
2014-05-05
pjp
char *tmp;
110
2014-05-05
pjp
111
2014-05-05
pjp
hash = hash_rrlimit(ip, sizeip);
112
2014-05-05
pjp
113
2014-05-05
pjp
tmp = rrlimit_ptr + (hash * ((size * sizeof(time_t)) + sizeof(u_int8_t)));
114
2014-05-05
pjp
rl = (struct rrlimit *)tmp;
115
2014-05-05
pjp
116
2014-05-05
pjp
offset = rl->pointer;
117
2014-05-05
pjp
118
2014-05-05
pjp
offset--;
119
2014-05-05
pjp
if (offset < 0)
120
2014-05-05
pjp
offset = size - 1;
121
2014-05-05
pjp
122
2014-05-05
pjp
now = time(NULL);
123
2014-05-05
pjp
124
2014-05-05
pjp
rl->times[offset] = now;
125
2014-05-05
pjp
rl->pointer = offset; /* XXX race */
126
2014-05-05
pjp
127
2014-05-05
pjp
}
128
2014-05-05
pjp
129
2014-05-05
pjp
static u_int16_t
130
2014-05-05
pjp
hash_rrlimit(u_int16_t *ip, int size)
131
2014-05-05
pjp
{
132
2014-05-05
pjp
u_int64_t total = 0;
133
2014-05-07
pjp
int i, j;
134
2014-05-05
pjp
135
2014-05-07
pjp
for (i = 0, j = 0; i < size; i += 2) {
136
2014-05-07
pjp
total += (u_int64_t)ip[j++];
137
2014-05-05
pjp
}
138
2014-05-05
pjp
139
2014-05-05
pjp
total %= 0xffff;
140
2014-05-05
pjp
141
2014-05-05
pjp
return ((u_int16_t)total);
142
2014-05-05
pjp
}
repomaster@centroid.eu