Fixing a compilation issue, wherein two global vars, conf and chan, were defined...
[revinetd.git] / proxy.c
blob644c2eb8ed5356a2ecdeae1de555fca9370995e6
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 // Global var:
36 Channels *chan;
38 int
39 proxy(fd_set *active, fd_set *perm)
41 Channels *temp;
43 temp = chan;
44 while (temp != NULL) {
45 if (FD_ISSET(temp->source, active)) {
46 if (copy_between_ports(temp->source, temp->target, temp) < 0) {
47 /* We need better handling here. */
48 FD_CLR(temp->source, perm);
49 FD_CLR(temp->target, perm);
50 if (temp->next == NULL) {
51 chan_remove(temp);
52 return 0;
53 } else {
54 temp = temp->next;
55 chan_remove(temp->prev);
59 if (FD_ISSET(temp->target, active)) {
60 if (copy_between_ports(temp->target, temp->source, temp) < 0) {
61 /* We need better handling here. */
62 FD_CLR(temp->source, perm);
63 FD_CLR(temp->target, perm);
64 if (temp->next == NULL) {
65 chan_remove(temp);
66 return 0;
67 } else {
68 temp = temp->next;
69 chan_remove(temp->prev);
73 temp = temp->next;
76 return 0;
79 void
80 chan_remove(Channels *temp)
82 unregister_sock(temp->source);
83 unregister_sock(temp->target);
85 close(temp->source);
86 close(temp->target);
87 if (temp->foreign_address_str != NULL)
88 free(temp->foreign_address_str);
90 if (temp->prev == NULL) {
91 if (temp->next == NULL) {
92 chan = NULL;
93 free(temp);
94 } else {
95 chan = temp->next;
96 chan->prev = NULL;
97 free(temp);
99 } else {
100 if (temp->next == NULL) {
101 temp->prev->next = NULL;
102 free(temp);
103 } else {
104 temp->prev->next = temp->next;
105 temp->next->prev = temp->prev;
106 free(temp);
109 statistics.nmb_relay_worker_conns_cur--;
112 Channels *
113 chan_add(void)
115 Channels *temp;
117 temp = (Channels *)malloc(sizeof(Channels));
118 if (temp == NULL) {
119 perror("malloc");
120 exit(1);
123 memset(temp, 0, sizeof(Channels));
125 return temp;