Various changes, see Changelog
[revinetd.git] / proxy.c
bloba32d7cca88503b12e1a708f5a1236e9572b400eb
1 /*
2 * proxy.c
4 * This file is a part of the revinetd project
6 * Revinetd is copyright (c) 2003-2008 by Steven M. Gill
7 * and distributed under the GPL.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 * USA.
24 * */
26 #include "includes.h"
27 #include "proxy.h"
28 #include "misc.h"
29 #include "statistics.h"
31 static const char cvsid[] = "$Id: proxy.c,v 1.8 2008/08/28 03:24:59 necrotaur Exp $";
33 extern Statistics statistics;
35 int
36 proxy(fd_set *active, fd_set *perm)
38 Channels *temp;
40 temp = chan;
41 while (temp != NULL) {
42 if (FD_ISSET(temp->source, active)) {
43 if (copy_between_ports(temp->source, temp->target) < 0) {
44 /* We need better handling here. */
45 FD_CLR(temp->source, perm);
46 FD_CLR(temp->target, perm);
47 if (temp->next == NULL) {
48 chan_remove(temp);
49 return 0;
50 } else {
51 temp = temp->next;
52 chan_remove(temp->prev);
56 if (FD_ISSET(temp->target, active)) {
57 if (copy_between_ports(temp->target, temp->source) < 0) {
58 /* We need better handling here. */
59 FD_CLR(temp->source, perm);
60 FD_CLR(temp->target, perm);
61 if (temp->next == NULL) {
62 chan_remove(temp);
63 return 0;
64 } else {
65 temp = temp->next;
66 chan_remove(temp->prev);
70 temp = temp->next;
73 return 0;
76 void
77 chan_remove(Channels *temp)
79 unregister_sock(temp->source);
80 unregister_sock(temp->target);
82 close(temp->source);
83 close(temp->target);
85 if (temp->prev == NULL) {
86 if (temp->next == NULL) {
87 chan = NULL;
88 free(temp);
89 } else {
90 chan = temp->next;
91 chan->prev = NULL;
92 free(temp);
94 } else {
95 if (temp->next == NULL) {
96 temp->prev->next = NULL;
97 free(temp);
98 } else {
99 temp->prev->next = temp->next;
100 temp->next->prev = temp->prev;
101 free(temp);
104 statistics.nmb_relay_worker_conns_cur--;
107 Channels *
108 chan_add(void)
110 Channels *temp;
112 temp = (Channels *)malloc(sizeof(Channels));
113 if (temp == NULL) {
114 perror("malloc");
115 exit(1);
118 memset(temp, 0, sizeof(Channels));
120 return temp;