tagging vde-2 version 2.3.2
[vde.git] / 2.3.2 / src / vde_over_ns / queue.c
blob0cda62fb44fc2739720b190b349739f228b29dcb
1 /* ----------------------------------------------------------------------------
3 VDE_OVER_NS
4 (C) 2007 Daniele Lacamera
6 Derived from:
7 NSTX -- tunneling network-packets over DNS
9 (C) 2000 by Florian Heinz and Julien Oster
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License version 2, as
13 published by the Free Software Foundation.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 -------------------------------------------------------------------------- */
25 #include <sys/types.h>
26 #include <netinet/in.h>
27 #include <sys/socket.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <time.h>
32 #include <config.h>
33 #include <vde.h>
34 #include <vdecommon.h>
36 #include "fun.h"
38 static struct nstxqueue *qhead = NULL;
39 static int qlen = 0;
40 static int qtimeout = QUEUETIMEOUT;
42 struct nstxqueue *finditem (unsigned short id)
44 struct nstxqueue *ptr;
46 for (ptr = qhead; ptr; ptr = ptr->next)
47 if (ptr->id == id)
48 break;
50 return ptr;
53 void
54 queueitem(unsigned short id, const char *name, const struct sockaddr_in *peer)
56 struct nstxqueue *ptr, *tmp;
58 if (finditem(id))
59 return;
61 qlen++;
62 ptr = malloc(sizeof(struct nstxqueue));
63 memset(ptr, 0, sizeof(struct nstxqueue));
64 if (!qhead)
65 qhead = ptr;
66 else {
67 for (tmp = qhead; tmp->next; tmp = tmp->next)
69 tmp->next = ptr;
71 ptr->id = id;
72 if (name)
73 strcpy(ptr->name, name);
74 if (peer)
75 memcpy(&ptr->peer, peer, sizeof(struct sockaddr_in));
76 ptr->timeout = time(NULL) + qtimeout;
79 void queueid (unsigned short id)
81 queueitem(id, NULL, NULL);
84 struct nstxqueue *dequeueitem (int id)
86 static struct nstxqueue *tmp = NULL, *ptr;
88 if (!qhead)
89 return NULL;
90 if (tmp)
91 free(tmp);
93 if ((id < 0) || (qhead->id == id))
95 tmp = qhead;
96 qhead = qhead->next;
97 qlen--;
99 else
101 ptr = qhead;
102 for (tmp = qhead->next; tmp; tmp = tmp->next)
104 if (tmp->id == id)
106 ptr->next = tmp->next;
107 qlen--;
108 break;
110 ptr = tmp;
114 return tmp;
117 void timeoutqueue (void (*timeoutfn)(struct nstxqueue *))
119 struct nstxqueue *ptr;
120 time_t now;
122 now = time(NULL);
124 while (qhead && (qhead->timeout <= now))
126 if (timeoutfn)
127 timeoutfn(qhead);
128 ptr = qhead;
129 qhead = qhead->next;
130 qlen--;
131 free(ptr);
135 int queuelen (void)
137 return qlen;
140 void qsettimeout (int timeout)
142 qtimeout = timeout;