~samiam/MaraDNS

MaraDNS/server/MaraAnyChain.c -rw-r--r-- 6.5 KiB View raw
ccb94b01Sam Trenholme Dockerfile now installs MaraDNS in Docker image. 22 days ago
                                                                                
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* Copyright (c) 2005 Sam Trenholme
 *
 * TERMS
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * This software is provided 'as is' with no guarantees of correctness or
 * fitness for purpose.
 */

#include "../MaraDns.h"
#include "../libs/MaraHash.h"
#ifndef MINGW32
#include <netinet/in.h>
#else
#include <winsock.h>
#include <wininet.h>
#endif
#include "functions_server.h"

/* Add an element to the ANY chain in the big hash
 * Input: Pointer to the bighash, the query to point to, the data being
 * pointed to
 * Output: JS_SUCCESS on success, JS_ERROR on error
 */

int any_add_rr(mhash *hash, js_string *query, rr *data) {
        int rrtype;
        int a;
        js_string *copy;
        rr_list *point;
        rr_list *new;
        mhash_e get;

        rrtype = get_rtype(query);
        /*printf("%d ",rrtype);show_esc_stdout(query);printf("\n");*//*DEBUG*/
        if(rrtype == JS_ERROR) {
                return JS_ERROR;
        }
        if(rrtype < 0 || rrtype > 65535) {
                return JS_ERROR;
        }

        /* Make the "copy" string be the "ANY" form of the query in question
         */
        if((copy = js_create(256,1)) == 0) {
                return JS_ERROR;
        }
        if(js_copy(query,copy) == JS_ERROR) {
                js_destroy(copy);
                return JS_ERROR;
        }
        if(change_rtype(copy,RR_ANY) == JS_ERROR) {
                js_destroy(copy);
                return JS_ERROR;
        }

        /* Make the new rr_list be where we will put this data */
        new = js_alloc(1,sizeof(rr_list));
        if(new == 0) {
                js_destroy(copy);
                return JS_ERROR;
        }
        /* Initialize the new rr_list */
        new->rr_type = rrtype;
        new->data = data;
        new->next = 0;

        /* Look for the "ANY" query in the big hash */
        get = mhash_get(hash,copy);
        /*printf("DEBUG 72: %p %d\n",get.value,get.datatype);*/

        /* If nothing was found, create a new entry in the big hash */
        if(get.value == 0 && get.datatype == 0) {
                mhash_put(hash,copy,(void *)new,MARA_DNS_LIST);
                js_destroy(copy);
                return JS_SUCCESS;
        }

        js_destroy(copy);

        /* Otherwise, put the new data at the end of the in-place linked
         * list */
        if(get.datatype != MARA_DNS_LIST) {
                harderror("Data not MARA_DNS_LIST in ANY query in bighash");
        }

        point = (rr_list *)get.value;
        if(point == 0) {
                return JS_ERROR;
        }

        /* Find the end of the list */
        for(a = 0; a < 65000; a++) {
                /*printf("96: %p %p\n",point,point->next);*//*DEBUG*/
                if(point->next == 0) {
                        break;
                }
                point = point->next;
        }

        if(point->next != 0) {
                return JS_ERROR;
        }

        /* Add this data to the end of the list */
        point->next = new;
        new->next = 0;
        new->data = data;
        return JS_SUCCESS;
}

/* Add an ANY element to the big hash, as long as the rtype of the element
 * is *not* RR_CNAME */
int any_add_c_rr(mhash *hash, js_string *query, rr *data) {
        int rrtype;
        rrtype = get_rtype(query);
        if(rrtype == RR_CNAME) {
                return JS_ERROR;
        }
        return any_add_rr(hash,query,data);
}

/* Remove an element from the ANY chain in the big hash
 * Input: Pointer to the big hash
 *        Pointer to data which we are now removing, query with this
 *        data
 * Output: JS_ERROR on error, JS_SUCCESS on success */

int any_zap_rr(mhash *hash, js_string *query, rr *data) {
        int a;
        js_string *copy;
        mhash_e get;
        rr_list *point;
        rr_list *last;

        /* Make the "copy" string be the "ANY" form of the query in question
         */
        if((copy = js_create(256,1)) == 0) {
                return JS_ERROR;
        }
        if(js_copy(query,copy) == JS_ERROR) {
                js_destroy(copy);
                return JS_ERROR;
        }
        if(change_rtype(copy,RR_ANY) == JS_ERROR) {
                js_destroy(copy);
                return JS_ERROR;
        }

        /* Look for the query in the big hash */
        get = mhash_get(hash,copy);

        if(get.value == 0 || get.datatype == 0) {
                js_destroy(copy);
                return JS_SUCCESS;
        }

        point = (rr_list *)get.value;
        last = 0;
        /* If the first link is the link we're removing, and it's also
         * the last link... */
        if(point->data == data && point->next == 0) {
                last = (rr_list *)mhash_undef(hash,copy);
                if(last != point) {
                        harderror("Line 166; mail the mailing list");
                }
                js_dealloc(last);
                js_destroy(copy);
                return JS_SUCCESS;
        }
        /* If the first link is the link we're removing, and it isn't the
         * last link, then we repoint the data in the hash */
        if(point->data == data && point->next != 0) {
                last = (rr_list *)mhash_undef(hash,copy);
                if(last != point) {
                        harderror("Line 177; mail the mailing list");
                }
                point = point->next;
                js_dealloc(last);
                mhash_put(hash,copy,point,MARA_DNS_LIST);
                js_destroy(copy);
                return JS_SUCCESS;
        }

        js_destroy(copy);

        /* OK, look for the link in the list */
        for(a=0;a<65000;a++) {
                if(point->next == 0) {
                        return -2; /* Not found */
                }
                last = point;
                point = point->next;
                /* If element to remove is found */
                if(point->data == data) {
                        last->next = point->next;
                        js_dealloc(point);
                        return JS_SUCCESS;
                }
        }

        return JS_ERROR; /* Chain far too long */
}