Hm.
[tinc] / src / pokey / protocol_misc.c
1 /*
2     protocol_misc.c -- handle the meta-protocol, miscellaneous functions
3     Copyright (C) 1999-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
4                   2000-2002 Guus Sliepen <guus@sliepen.warande.net>
5
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.
10
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.
15
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.
19
20     $Id: protocol_misc.c,v 1.1 2002/04/28 12:46:26 zarq Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <errno.h>
30
31 #include <utils.h>
32
33 #include "conf.h"
34 #include "interface.h"
35 #include "net.h"
36 #include "netutl.h"
37 #include "protocol.h"
38 #include "meta.h"
39 #include "connection.h"
40 #include "logging.h"
41
42 #include "system.h"
43
44 /* Status and error notification routines */
45
46 int send_status(connection_t *c, int statusno, char *statusstring)
47 {
48 cp
49   if(!statusstring)
50     statusstring = status_text[statusno];
51 cp
52   return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
53 }
54
55 int status_h(connection_t *c)
56 {
57   int statusno;
58   char statusstring[MAX_STRING_SIZE];
59 cp
60   if(sscanf(c->buffer, "%*d %d "MAX_STRING, &statusno, statusstring) != 2)
61     {
62       syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS",
63               c->name, c->hostname);
64        return -1;
65     }
66
67   if(debug_lvl >= DEBUG_STATUS)
68     {
69       syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"),
70              c->name, c->hostname, status_text[statusno], statusstring);
71     }
72
73 cp
74   return 0;
75 }
76
77 int send_error(connection_t *c, int err, char *errstring)
78 {
79 cp
80   if(!errstring)
81     errstring = strerror(err);
82   return send_request(c, "%d %d %s", ERROR, err, errstring);
83 }
84
85 int error_h(connection_t *c)
86 {
87   int err;
88   char errorstring[MAX_STRING_SIZE];
89 cp
90   if(sscanf(c->buffer, "%*d %d "MAX_STRING, &err, errorstring) != 2)
91     {
92        syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR",
93               c->name, c->hostname);
94        return -1;
95     }
96
97   if(debug_lvl >= DEBUG_ERROR)
98     {
99       syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"),
100              c->name, c->hostname, strerror(err), errorstring);
101     }
102
103   terminate_connection(c, c->status.active);
104 cp
105   return 0;
106 }
107
108 int send_termreq(connection_t *c)
109 {
110 cp
111   return send_request(c, "%d", TERMREQ);
112 }
113
114 int termreq_h(connection_t *c)
115 {
116 cp
117   terminate_connection(c, c->status.active);
118 cp
119   return 0;
120 }
121
122 int send_ping(connection_t *c)
123 {
124 cp
125   c->status.pinged = 1;
126   c->last_ping_time = now;
127 cp
128   return send_request(c, "%d", PING);
129 }
130
131 int ping_h(connection_t *c)
132 {
133 cp
134   return send_pong(c);
135 }
136
137 int send_pong(connection_t *c)
138 {
139 cp
140   return send_request(c, "%d", PONG);
141 }
142
143 int pong_h(connection_t *c)
144 {
145 cp
146   c->status.pinged = 0;
147
148   /* Succesful connection, reset timeout if this is an outgoing connection. */
149   
150   if(c->outgoing)
151     c->outgoing->timeout = 0;
152 cp
153   return 0;
154 }
155
156 /* Sending and receiving packets via TCP */
157
158 int send_tcppacket(connection_t *c, vpn_packet_t *packet)
159 {
160   int x;
161 cp  
162   /* Evil hack. */
163
164   x = send_request(c, "%d %hd", PACKET, packet->len);
165
166   if(x)
167     return x;
168 cp
169   return send_meta(c, packet->data, packet->len);
170 }
171
172 /* Status strings */
173
174 char (*status_text[]) = {
175   "Warning",
176 };
177
178 /* Error strings */
179
180 char (*error_text[]) = {
181   "Error",
182 };