534e21f4bfa1acdfc466effdb75209f1882c289c
[l2tpns.git] / cluster_slave.c
1 // L2TPNS Cluster Master
2 // $Id: cluster_slave.c,v 1.3 2004-03-05 00:22:06 fred_nerk Exp $
3
4 #include <stdio.h>
5 #include <netinet/in.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <time.h>
10 #include <errno.h>
11 #include <malloc.h>
12 #include <sys/socket.h>
13 #include <sys/file.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include <arpa/inet.h>
17 #include "l2tpns.h"
18 #include "cluster.h"
19 #include "ll.h"
20 #include "util.h"
21
22 // vim: sw=4 ts=8
23
24 extern int cluster_sockfd;
25 extern char hostname[1000];
26 extern ippoolt *ip_address_pool;
27 extern uint32_t vip_address;
28 extern struct configt *config;
29
30 int handle_tunnel(char *buf, int l);
31 int handle_session(char *buf, int l);
32 int handle_hello_response(char *buf, int l);
33
34 int processcluster(char *buf, int l)
35 {
36 char mtype;
37 uint32_t addr;
38
39 log_hex(4, "Cluster receive", buf, l);
40 if (!buf || l <= sizeof(uint32_t)) return 0;
41
42 addr = ntohl(*(uint32_t*)buf);
43 buf += sizeof(uint32_t);
44 l -= sizeof(uint32_t);
45
46 if (addr != vip_address)
47 {
48 log(0, 0, 0, 0, "Received cluster message for VIP %s, which isn't ours\n", inet_toa(addr));
49 }
50
51 mtype = *buf; buf++; l--;
52
53 switch (mtype)
54 {
55 case C_HELLO:
56 break;
57 case C_HELLO_RESPONSE:
58 handle_hello_response(buf, l);
59 break;
60 case C_PING:
61 break;
62 case C_TUNNEL:
63 handle_tunnel(buf, l);
64 break;
65 case C_SESSION:
66 handle_session(buf, l);
67 break;
68 }
69 return mtype;
70
71 return 0;
72 }
73
74 int handle_tunnel(char *buf, int l)
75 {
76 int t;
77
78 // Ignore tunnel message if NOSTATEFILE exists
79 if (config->ignore_cluster_updates)
80 {
81 log(1, 0, 0, 0, "Discarding tunnel message from cluster master.\n", l, sizeof(tunnelt));
82 return 0;
83 }
84
85 t = *(int *)buf;
86 log(1, 0, 0, t, "Receiving tunnel %d from cluster master (%d bytes)\n", t, l);
87 buf += sizeof(int); l -= sizeof(int);
88
89 if (t > MAXTUNNEL)
90 {
91 log(0, 0, 0, t, "Cluster master tried to send tunnel %d, which is bigger than MAXTUNNEL (%d)\n", t, MAXTUNNEL);
92 return 0;
93 }
94
95 if (l != sizeof(tunnelt))
96 {
97 log(1, 0, 0, t, "Discarding bogus tunnel message (%d bytes instead of %d).\n", l, sizeof(tunnelt));
98 return 0;
99 }
100
101 memcpy(&tunnel[t], buf, l);
102 log(3, 0, 0, t, "Cluster master sent tunnel for %s\n", tunnel[t].hostname);
103
104 tunnel[t].controlc = 0;
105 tunnel[t].controls = NULL;
106 tunnel[t].controle = NULL;
107 return 0;
108 }
109
110 int handle_session(char *buf, int l)
111 {
112 int s;
113
114 // Ignore tunnel message if NOSTATEFILE exists
115 if (config->ignore_cluster_updates)
116 {
117 log(1, 0, 0, 0, "Discarding session message from cluster master.\n", l, sizeof(tunnelt));
118 return 0;
119 }
120
121 s = *(int *)buf;
122 log(1, 0, s, 0, "Receiving session %d from cluster master (%d bytes)\n", s, l);
123 buf += sizeof(int); l -= sizeof(int);
124
125 if (s > MAXSESSION)
126 {
127 log(0, 0, s, 0, "Cluster master tried to send session %d, which is bigger than MAXSESSION (%d)\n", s, MAXSESSION);
128 return 0;
129 }
130
131 if (l != sizeof(sessiont))
132 {
133 log(1, 0, s, 0, "Discarding short session message (%d bytes instead of %d).\n", l, sizeof(sessiont));
134 return 0;
135 }
136
137 if (s > 1)
138 {
139 session[s-1].next = session[s].next;
140 }
141
142 if (sessionfree == s)
143 {
144 sessionfree = session[s].next;
145 }
146
147 memcpy(&session[s], buf, l);
148 session[s].tbf = 0;
149 session[s].throttle = 0;
150 if (session[s].opened)
151 {
152 log(2, 0, s, session[s].tunnel, "Cluster master sent active session for user %s\n", session[s].user);
153 sessionsetup(session[s].tunnel, s, 0);
154 if (session[s].ip && session[s].ip != 0xFFFFFFFE)
155 {
156 int x;
157 for (x = 0; x < MAXIPPOOL && ip_address_pool[x].address; x++)
158 {
159 if (ip_address_pool[x].address == session[s].ip)
160 {
161 ip_address_pool[x].assigned = 1;
162 break;
163 }
164 }
165 }
166 }
167 return 0;
168 }
169
170 int handle_hello_response(char *buf, int l)
171 {
172 int numtunnels, numsessions;
173
174 /* The cluster master has downed the address, so send another garp */
175 send_garp(vip_address);
176
177 if (!l) return 0;
178
179 if (l < (4 * IL))
180 {
181 log(1, 0, 0, 0, "Cluster master sent invalid hello response: %d bytes instead of %d\n", l, (4 * IL));
182 return 0;
183 }
184 numtunnels = *(int *)(buf + IL * 0);
185 numsessions = *(int *)(buf + IL * 1);
186 if (numtunnels == 0 && numsessions == 0)
187 {
188 log(2, 0, 0, 0, "Cluster master has no state information for us.\n");
189 return 0;
190 }
191 log(2, 0, 0, 0, "The cluster master will send %d tunnels and %d sessions.\n", numtunnels, numsessions);
192 return 0;
193 }
194
195 int cluster_send_session(int s)
196 {
197 char *packet;
198 int len = 0;
199
200 if (!cluster_sockfd) return 1;
201
202 packet = malloc(4096);
203
204 // Hostname
205 len = strlen(hostname);
206 *(char *)packet = len;
207 memcpy((char *)(packet + 1), hostname, len);
208 len++;
209
210 // Session ID
211 *(int *)(packet + len) = s;
212 len += sizeof(int);
213
214 // Session data
215 memcpy((char *)(packet + len), &session[s], sizeof(sessiont));
216 len += sizeof(sessiont);
217
218 cluster_send_message(config->cluster_address, vip_address, C_SESSION, packet, len);
219 free(packet);
220
221 return 1;
222 }
223
224 int cluster_send_tunnel(int t)
225 {
226 char *packet;
227 int len = 0;
228
229 packet = malloc(4096);
230
231 // Hostname
232 len = strlen(hostname);
233 *(char *)packet = len;
234 memcpy((char *)(packet + 1), hostname, len);
235 len++;
236
237 // Tunnel ID
238 *(int *)(packet + len) = t;
239 len += sizeof(int);
240
241 // Tunnel data
242 memcpy((char *)(packet + len), &tunnel[t], sizeof(tunnelt));
243 len += sizeof(tunnelt);
244
245 cluster_send_message(config->cluster_address, vip_address, C_TUNNEL, packet, len);
246 free(packet);
247
248 return 1;
249 }
250
251 int cluster_send_goodbye()
252 {
253 char *packet;
254 int len = 0;
255
256 packet = malloc(4096);
257
258 log(2, 0, 0, 0, "Sending goodbye to cluster master\n");
259 // Hostname
260 len = strlen(hostname);
261 *(char *)packet = len;
262 memcpy((char *)(packet + 1), hostname, len);
263 len++;
264
265 cluster_send_message(config->cluster_address, vip_address, C_GOODBYE, packet, len);
266 free(packet);
267
268 return 1;
269 }
270