Fixing a compilation issue, wherein two global vars, conf and chan, were defined...
[revinetd.git] / relay_agt.c
blob8667d0f18caf78620199436036510950ab9afb1d
1 /*
2 * relay_agt.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 * */
27 #include "includes.h"
28 #include "revinetd.h"
29 #include "relay_agt.h"
30 #include "misc.h"
31 #include "proxy.h"
33 extern Conf conf;
34 extern Channels *chan;
36 static const char cvsid[] = "$Id: relay_agt.c,v 1.28 2008/08/28 03:24:59 necrotaur Exp $";
38 int
39 relay_agent(char *callback, int port, char *target, int port2)
41 int comm_chan, retval, tmp_sock, message = -1;
42 time_t timer;
43 struct sockaddr_in sa_comm, tmp_sa;
44 struct timeval timeout;
45 fd_set active, read;
46 Channels *tmp_chan;
48 /* init time. */
49 timer = time(NULL);
51 /* Establish the callback channel. */
53 comm_chan = init_sockaddr(&sa_comm, callback, port);
54 if (connect(comm_chan, (struct sockaddr *)&sa_comm,
55 sizeof(sa_comm)) == -1) {
56 perror("connect");
57 return 1;
59 register_sock(comm_chan);
61 if (conf.verbosity != VB_QUIET)
62 printf("Server comm channel connection established to %s:%i\n", callback, port);
64 /* Initialize the channels */
65 chan = NULL;
67 FD_ZERO(&active);
68 FD_SET(comm_chan, &active);
70 while (1) {
71 read = active;
72 timeout.tv_sec = conf.keepalive;
73 timeout.tv_usec = 0L; /* 0 micro seconds, also as a long */
74 if ((retval = select(FD_SETSIZE, &read, NULL, NULL, &timeout)) < 0) {
75 perror("select");
76 exit(EXIT_FAILURE);
78 if (retval == 0) { /* timeout. */
79 /* If the heart beat fail, the socket should throw us an
80 * error. */
81 send_comm_message(comm_chan, SV_HEART_BEAT);
82 timer = time(NULL);
83 continue;
85 if ((timer + conf.keepalive) < time(NULL)) {
86 send_comm_message(comm_chan, SV_HEART_BEAT);
87 timer = time(NULL);
89 if (FD_ISSET(comm_chan, &read)) {
90 if (conf.verbosity == VB_VERBOSE)
91 printf("Received message on comm channel: ");
93 message = get_comm_message(comm_chan);
94 /* We either get a heart beat reply or a request for new
95 * channel. */
96 if (message == -1){
97 //EOF, i.e. socket closed
98 printf("Comm channel closed, quitting\n");
99 exit(3);
101 if (message == RA_TARGET_UP) {
102 if (conf.verbosity != VB_QUIET)
103 printf("New relay agent proxy pair requested from %s:%i\n", callback, port);
105 tmp_sock = init_sockaddr(&tmp_sa, callback, port);
106 if (connect(tmp_sock, (struct sockaddr *)&tmp_sa,
107 sizeof(tmp_sa)) == -1) {
108 perror("connect");
109 return 1;
111 register_sock(tmp_sock);
113 if (conf.verbosity != VB_QUIET)
114 printf("New relay agent connection established to %s:%i\n", callback, port);
117 if (chan == NULL) {
118 chan = chan_add();
119 tmp_chan = chan;
120 } else {
121 tmp_chan = chan;
122 while (tmp_chan->next != NULL) {
123 tmp_chan = tmp_chan->next;
125 tmp_chan->next = chan_add();
126 tmp_chan->next->prev = tmp_chan;
127 tmp_chan = tmp_chan->next;
130 tmp_chan->source = tmp_sock;
132 if (conf.verbosity != VB_QUIET)
133 printf("Initializing new target connection to %s:%i\n", target, port2);
136 tmp_sock = init_sockaddr(&tmp_sa, target, port2);
137 if (connect(tmp_sock, (struct sockaddr *)&tmp_sa,
138 sizeof(tmp_sa)) == -1) {
139 perror("connect");
140 return 1;
142 register_sock(tmp_sock);
144 if (conf.verbosity != VB_QUIET)
145 printf("New target connection established to %s:%i\n", target, port2);
147 tmp_chan->target = tmp_sock;
149 FD_SET(tmp_chan->source, &active);
150 FD_SET(tmp_chan->target, &active);
153 proxy(&read, &active);
156 return 0;