2 protocol_misc.c -- handle the meta-protocol, miscellaneous functions
3 Copyright (C) 1999-2005 Ivo Timmermans <ivo@tinc-vpn.org>,
4 2000-2005 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "connection.h"
34 int maxoutbufsize = 0;
36 /* Status and error notification routines */
38 bool send_status(connection_t *c, int statusno, const char *statusstring)
43 statusstring = "Status";
45 return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
48 bool status_h(connection_t *c)
51 char statusstring[MAX_STRING_SIZE];
55 if(sscanf(c->buffer, "%*d %d " MAX_STRING, &statusno, statusstring) != 2) {
56 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS",
57 c->name, c->hostname);
61 ifdebug(STATUS) logger(LOG_NOTICE, _("Status message from %s (%s): %d: %s"),
62 c->name, c->hostname, statusno, statusstring);
67 bool send_error(connection_t *c, int err, const char *errstring)
74 return send_request(c, "%d %d %s", ERROR, err, errstring);
77 bool error_h(connection_t *c)
80 char errorstring[MAX_STRING_SIZE];
84 if(sscanf(c->buffer, "%*d %d " MAX_STRING, &err, errorstring) != 2) {
85 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR",
86 c->name, c->hostname);
90 ifdebug(ERROR) logger(LOG_NOTICE, _("Error message from %s (%s): %d: %s"),
91 c->name, c->hostname, err, errorstring);
93 terminate_connection(c, c->status.active);
98 bool send_termreq(connection_t *c)
102 return send_request(c, "%d", TERMREQ);
105 bool termreq_h(connection_t *c)
109 terminate_connection(c, c->status.active);
114 bool send_ping(connection_t *c)
118 c->status.pinged = true;
119 c->last_ping_time = now;
121 return send_request(c, "%d", PING);
124 bool ping_h(connection_t *c)
131 bool send_pong(connection_t *c)
135 return send_request(c, "%d", PONG);
138 bool pong_h(connection_t *c)
142 c->status.pinged = false;
144 /* Succesful connection, reset timeout if this is an outgoing connection. */
147 c->outgoing->timeout = 0;
152 /* Sending and receiving packets via TCP */
154 bool send_tcppacket(connection_t *c, vpn_packet_t *packet)
158 /* If there already is a lot of data in the outbuf buffer, discard this packet. */
160 if(c->outbuflen > maxoutbufsize)
163 if(!send_request(c, "%d %hd", PACKET, packet->len))
166 return send_meta(c, packet->data, packet->len);
169 bool tcppacket_h(connection_t *c)
175 if(sscanf(c->buffer, "%*d %hd", &len) != 1) {
176 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "PACKET", c->name,
181 /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */