1 | /* $Id: hash.c 45321 2013-04-04 04:47:30Z vboxsync $ */
|
---|
2 | /*
|
---|
3 | * Copyright (c) 2003,2004 Armin Wolfermann
|
---|
4 | *
|
---|
5 | * Permission is hereby granted, free of charge, to any person obtaining a
|
---|
6 | * copy of this software and associated documentation files (the "Software"),
|
---|
7 | * to deal in the Software without restriction, including without limitation
|
---|
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
---|
9 | * and/or sell copies of the Software, and to permit persons to whom the
|
---|
10 | * Software is furnished to do so, subject to the following conditions:
|
---|
11 | *
|
---|
12 | * The above copyright notice and this permission notice shall be included in
|
---|
13 | * all copies or substantial portions of the Software.
|
---|
14 | *
|
---|
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
---|
18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
---|
21 | * DEALINGS IN THE SOFTWARE.
|
---|
22 | */
|
---|
23 |
|
---|
24 | #ifndef VBOX
|
---|
25 | #include <config.h>
|
---|
26 | #include "dnsproxy.h"
|
---|
27 |
|
---|
28 | #define HASHSIZE 10
|
---|
29 | #define HASH(id) (id & ((1 << HASHSIZE) - 1))
|
---|
30 |
|
---|
31 | static struct request *request_hash[1 << HASHSIZE];
|
---|
32 | #else /* VBOX */
|
---|
33 | # include "slirp.h"
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | void
|
---|
37 | hash_add_request(PNATState pData, struct request *req)
|
---|
38 | {
|
---|
39 | struct request **p = &request_hash[HASH(req->id)];
|
---|
40 | Log2(("NAT: hash req id %d has been added \n", req->id));
|
---|
41 |
|
---|
42 | if ((req->next = *p) != NULL) {
|
---|
43 | (*p)->prev = &req->next;
|
---|
44 | ++hash_collisions;
|
---|
45 | }
|
---|
46 | *p = req;
|
---|
47 | req->prev = p;
|
---|
48 |
|
---|
49 | ++active_queries;
|
---|
50 | }
|
---|
51 |
|
---|
52 | void
|
---|
53 | hash_remove_request(PNATState pData, struct request *req)
|
---|
54 | {
|
---|
55 | if (!req->prev) return;
|
---|
56 | if (req->next)
|
---|
57 | req->next->prev = req->prev;
|
---|
58 | *req->prev = req->next;
|
---|
59 | req->prev = NULL;
|
---|
60 |
|
---|
61 | --active_queries;
|
---|
62 | }
|
---|
63 |
|
---|
64 | struct request *
|
---|
65 | hash_find_request(PNATState pData, unsigned short id)
|
---|
66 | {
|
---|
67 | struct request *req = request_hash[HASH(id)];
|
---|
68 | Log2(("NAT: hash try to find req by id %d \n", id));
|
---|
69 |
|
---|
70 | for (;;) {
|
---|
71 | if (!req) break;
|
---|
72 | if (req->id == id) break;
|
---|
73 | req = req->next;
|
---|
74 | }
|
---|
75 |
|
---|
76 | return req;
|
---|
77 | }
|
---|