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,
29 #include "relay_agt.h"
34 extern Channels
*chan
;
36 static const char cvsid
[] = "$Id: relay_agt.c,v 1.28 2008/08/28 03:24:59 necrotaur Exp $";
39 relay_agent(char *callback
, int port
, char *target
, int port2
)
41 int comm_chan
, retval
, tmp_sock
, message
= -1;
43 struct sockaddr_in sa_comm
, tmp_sa
;
44 struct timeval timeout
;
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) {
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 */
68 FD_SET(comm_chan
, &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) {
78 if (retval
== 0) { /* timeout. */
79 /* If the heart beat fail, the socket should throw us an
81 send_comm_message(comm_chan
, SV_HEART_BEAT
);
85 if ((timer
+ conf
.keepalive
) < time(NULL
)) {
86 send_comm_message(comm_chan
, SV_HEART_BEAT
);
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
97 //EOF, i.e. socket closed
98 printf("Comm channel closed, quitting\n");
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) {
111 register_sock(tmp_sock
);
113 if (conf
.verbosity
!= VB_QUIET
)
114 printf("New relay agent connection established to %s:%i\n", callback
, port
);
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) {
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
);