-
Notifications
You must be signed in to change notification settings - Fork 26
/
esi.c
593 lines (500 loc) · 14 KB
/
esi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
* Handle ESI events
*
* Copyright (C) 2007 Olaf Kirch <[email protected]>
*/
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "config.h"
#include <libisns/isns.h>
#include <libisns/attrs.h>
#include "objects.h"
#include <libisns/message.h>
#include "security.h"
#include <libisns/util.h>
#include "db.h"
#define ESI_RETRANS_TIMEOUT 60
typedef struct isns_esi isns_esi_t;
typedef struct isns_esi_portal isns_esi_portal_t;
struct isns_esi {
isns_list_t esi_list;
isns_object_t * esi_object;
isns_list_t esi_portals;
unsigned int esi_update : 1;
};
struct isns_esi_portal {
isns_list_t esp_list;
isns_object_t * esp_object;
isns_portal_info_t esp_portal;
unsigned int esp_interval;
isns_portal_info_t esp_dest;
isns_socket_t * esp_socket;
unsigned int esp_retries;
unsigned int esp_timeout;
time_t esp_start;
time_t esp_next_xmit;
uint32_t esp_xid;
};
int isns_esi_enabled = 0;
static isns_server_t * isns_esi_server = NULL;
static ISNS_LIST_DECLARE(isns_esi_list);
static void isns_esi_transmit(void *);
static void isns_esi_sendto(isns_esi_t *, isns_esi_portal_t *);
static void isns_process_esi_response(uint32_t, int,
isns_simple_t *);
static void isns_esi_disconnect(isns_esi_portal_t *);
static void isns_esi_restart(isns_esi_portal_t *);
static void isns_esi_drop_portal(isns_esi_portal_t *, isns_db_t *, int);
static void isns_esi_drop_entity(isns_esi_t *, isns_db_t *, int);
static int isns_esi_update(isns_esi_t *);
static void isns_esi_schedule(int);
static void isns_esi_callback(const isns_db_event_t *, void *);
void
isns_esi_init(isns_server_t *srv)
{
if (isns_config.ic_esi_retries == 0) {
isns_debug_esi("ESI disabled by administrator\n");
} else {
unsigned int max_interval;
isns_register_callback(isns_esi_callback, NULL);
isns_esi_schedule(0);
max_interval = isns_config.ic_registration_period / 2;
if (isns_config.ic_esi_max_interval > max_interval) {
isns_warning("Max ESI interval adjusted to %u sec "
"to match registration period\n",
max_interval);
isns_config.ic_esi_max_interval = max_interval;
if (isns_config.ic_esi_min_interval > max_interval)
isns_config.ic_esi_min_interval = max_interval;
}
isns_esi_server = srv;
isns_esi_enabled = 1;
}
}
/*
* Timer callback to send out ESI messages.
*/
void
isns_esi_transmit(void *ptr)
{
isns_db_t *db = isns_esi_server->is_db;
isns_list_t *esi_pos, *esi_next;
time_t now;
isns_object_t *obj;
time_t next_timeout;
now = time(NULL);
next_timeout = now + 3600;
isns_list_foreach(&isns_esi_list, esi_pos, esi_next) {
isns_list_t *esp_pos, *esp_next;
isns_esi_t *esi = isns_list_item(isns_esi_t, esi_list, esi_pos);
if (esi->esi_update) {
esi->esi_update = 0;
if (!isns_esi_update(esi))
continue;
}
isns_list_foreach(&esi->esi_portals, esp_pos, esp_next) {
isns_esi_portal_t *esp = isns_list_item(isns_esi_portal_t,
esp_list, esp_pos);
/* Check whether the portal object still exist */
obj = esp->esp_object;
if (obj->ie_state != ISNS_OBJECT_STATE_MATURE) {
isns_esi_drop_portal(esp, db, 0);
continue;
}
if (esp->esp_next_xmit <= now) {
if (esp->esp_retries == 0) {
char *ps = isns_portal_string(&esp->esp_dest);
isns_debug_esi("No ESI response from %s - dropping\n",
ps);
free(ps);
isns_esi_drop_portal(esp, db, 1);
continue;
}
esp->esp_retries -= 1;
esp->esp_next_xmit = now + esp->esp_timeout;
isns_esi_sendto(esi, esp);
}
if (esp->esp_next_xmit < next_timeout)
next_timeout = esp->esp_next_xmit;
}
if (isns_list_empty(&esi->esi_portals))
isns_esi_drop_entity(esi, db, 1);
}
isns_debug_esi("Next ESI message in %d seconds\n", next_timeout - now);
isns_esi_schedule(next_timeout - now);
}
/*
* Send an ESI message
*/
void
isns_esi_sendto(isns_esi_t *esi, isns_esi_portal_t *esp)
{
isns_attr_list_t attrs = ISNS_ATTR_LIST_INIT;
isns_socket_t *sock;
isns_simple_t *msg;
char *ps = NULL;
/* For TCP portals, kill the TCP socket every time. */
if (esp->esp_dest.proto == IPPROTO_TCP)
isns_esi_disconnect(esp);
if (esp->esp_socket == NULL) {
sock = isns_connect_to_portal(&esp->esp_dest);
if (sock == NULL)
return;
isns_socket_set_security_ctx(sock,
isns_default_security_context(0));
/* sock->is_disconnect_fatal = 1; */
esp->esp_socket = sock;
}
isns_attr_list_append_uint64(&attrs,
ISNS_TAG_TIMESTAMP,
time(NULL));
/* The following will extract the ENTITY IDENTIFIER */
isns_object_extract_keys(esi->esi_object, &attrs);
isns_portal_to_attr_list(&esp->esp_portal,
ISNS_TAG_PORTAL_IP_ADDRESS,
ISNS_TAG_PORTAL_TCP_UDP_PORT,
&attrs);
msg = isns_simple_create(ISNS_ENTITY_STATUS_INQUIRY,
NULL, &attrs);
if (msg == NULL)
return;
ps = isns_portal_string(&esp->esp_dest);
isns_debug_esi("*** Sending ESI message to %s (xid=0x%x); %u retries left\n",
ps, msg->is_xid, esp->esp_retries);
free(ps);
isns_simple_transmit(esp->esp_socket, msg,
NULL, esp->esp_timeout - 1,
isns_process_esi_response);
esp->esp_xid = msg->is_xid;
isns_simple_free(msg);
}
/*
* A new entity was added. See if it uses ESI, and create
* portals and such.
*/
static void
isns_esi_add_entity(isns_object_t *obj)
{
isns_esi_t *esi;
isns_debug_esi("Enable ESI monitoring for entity %u\n", obj->ie_index);
esi = isns_calloc(1, sizeof(*esi));
esi->esi_object = isns_object_get(obj);
esi->esi_update = 1;
isns_list_init(&esi->esi_list);
isns_list_init(&esi->esi_portals);
isns_list_append(&isns_esi_list, &esi->esi_list);
}
/*
* Given an entity, see if we can find ESI state for it.
*/
static isns_esi_t *
isns_esi_find(isns_object_t *obj)
{
isns_list_t *pos, *next;
isns_list_foreach(&isns_esi_list, pos, next) {
isns_esi_t *esi = isns_list_item(isns_esi_t, esi_list, pos);
if (esi->esi_object == obj)
return esi;
}
return NULL;
}
/*
* Update the ESI state after an entity has changed
*/
static int
isns_esi_update(isns_esi_t *esi)
{
isns_object_t *entity = esi->esi_object;
ISNS_LIST_DECLARE(hold);
isns_esi_portal_t *esp;
unsigned int i;
char *ps = NULL;
isns_debug_esi("Updating ESI state for entity %u\n", entity->ie_index);
isns_list_move(&hold, &esi->esi_portals);
for (i = 0; i < entity->ie_children.iol_count; ++i) {
isns_object_t *child = entity->ie_children.iol_data[i];
isns_portal_info_t esi_portal, portal_info;
uint32_t esi_interval;
isns_list_t *pos, *next;
int changed = 0;
if (!ISNS_IS_PORTAL(child))
continue;
if (!isns_portal_from_object(&portal_info,
ISNS_TAG_PORTAL_IP_ADDRESS,
ISNS_TAG_PORTAL_TCP_UDP_PORT,
child)
|| !isns_portal_from_object(&esi_portal,
ISNS_TAG_PORTAL_IP_ADDRESS,
ISNS_TAG_ESI_PORT,
child)
|| !isns_object_get_uint32(child,
ISNS_TAG_ESI_INTERVAL,
&esi_interval))
continue;
isns_list_foreach(&hold, pos, next) {
esp = isns_list_item(isns_esi_portal_t, esp_list, pos);
if (esp->esp_object == child) {
ps = isns_portal_string(&portal_info);
isns_debug_esi("Updating ESI state for %s\n",
ps);
free(ps);
isns_list_del(&esp->esp_list);
goto update;
}
}
ps = isns_portal_string(&portal_info);
isns_debug_esi("Creating ESI state for %s\n",
ps);
free(ps);
esp = isns_calloc(1, sizeof(*esp));
esp->esp_object = isns_object_get(child);
isns_list_init(&esp->esp_list);
changed = 1;
update:
if (!isns_portal_equal(&esp->esp_portal, &portal_info)) {
esp->esp_portal = portal_info;
changed++;
}
if (!isns_portal_equal(&esp->esp_dest, &esi_portal)) {
isns_esi_disconnect(esp);
esp->esp_dest = esi_portal;
changed++;
}
if (esp->esp_interval != esi_interval) {
esp->esp_interval = esi_interval;
changed++;
}
isns_esi_restart(esp);
isns_list_append(&esi->esi_portals, &esp->esp_list);
}
/* Destroy any old ESI portals */
while (!isns_list_empty(&hold)) {
esp = isns_list_item(isns_esi_portal_t, esp_list, hold.next);
isns_esi_drop_portal(esp, NULL, 0);
}
/* If the client explicitly unregistered all ESI portals,
* stop monitoring it but *without* destroying the entity. */
if (isns_list_empty(&esi->esi_portals)) {
isns_esi_drop_entity(esi, NULL, 0);
return 0;
}
return 1;
}
void
isns_esi_restart(isns_esi_portal_t *esp)
{
unsigned int timeo;
isns_esi_disconnect(esp);
esp->esp_start = time(NULL);
esp->esp_retries = isns_config.ic_esi_retries;
esp->esp_next_xmit = esp->esp_start + esp->esp_interval;
esp->esp_xid = 0;
timeo = esp->esp_interval / esp->esp_retries;
if (timeo == 0)
timeo = 1;
else if (timeo > ESI_RETRANS_TIMEOUT)
timeo = ESI_RETRANS_TIMEOUT;
esp->esp_timeout = timeo;
}
void
isns_esi_disconnect(isns_esi_portal_t *esp)
{
if (esp->esp_socket)
isns_socket_free(esp->esp_socket);
esp->esp_socket = NULL;
}
/*
* Generic wrapper to dropping an object
*/
static inline void
__isns_esi_drop_object(isns_db_t *db, isns_object_t *obj, unsigned int dead)
{
if (db && obj && obj->ie_state == ISNS_OBJECT_STATE_MATURE && dead)
isns_db_remove(db, obj);
isns_object_release(obj);
}
/*
* Portal did not respond in time. Drop it
*/
void
isns_esi_drop_portal(isns_esi_portal_t *esp, isns_db_t *db, int dead)
{
char *ps = isns_portal_string(&esp->esp_portal);
isns_debug_esi("ESI: dropping portal %s\n", ps);
free(ps);
isns_list_del(&esp->esp_list);
isns_esi_disconnect(esp);
__isns_esi_drop_object(db, esp->esp_object, dead);
isns_free(esp);
}
/*
* We ran out of ESI portals for this entity.
*/
void
isns_esi_drop_entity(isns_esi_t *esi, isns_db_t *db, int dead)
{
isns_debug_esi("ESI: dropping entity %u\n",
esi->esi_object->ie_index);
isns_list_del(&esi->esi_list);
__isns_esi_drop_object(db, esi->esi_object, dead);
while (!isns_list_empty(&esi->esi_portals)) {
isns_esi_portal_t *esp;
esp = isns_list_item(isns_esi_portal_t, esp_list,
esi->esi_portals.next);
isns_esi_drop_portal(esp, db, dead);
}
isns_free(esi);
}
/*
* When receiving an ESI response, find the portal we sent the
* original message to.
*/
static isns_esi_portal_t *
isns_esi_get_msg_portal(uint32_t xid, isns_esi_t **esip)
{
isns_list_t *esi_pos, *esi_next;
isns_list_foreach(&isns_esi_list, esi_pos, esi_next) {
isns_esi_t *esi = isns_list_item(isns_esi_t, esi_list, esi_pos);
isns_list_t *esp_pos, *esp_next;
isns_list_foreach(&esi->esi_portals, esp_pos, esp_next) {
isns_esi_portal_t *esp = isns_list_item(isns_esi_portal_t,
esp_list, esp_pos);
if (esp->esp_xid == xid) {
*esip = esi;
return esp;
}
}
}
return NULL;
}
/*
* Handle incoming ESI request
*/
int
isns_process_esi(isns_server_t *srv, isns_simple_t *call, isns_simple_t **reply)
{
const isns_attr_list_t *attrs = &call->is_message_attrs;
isns_object_t *portal = NULL;
/* We just echo back the attributes sent to us by the server,
* without further checking. */
*reply = isns_simple_create(ISNS_ENTITY_STATUS_INQUIRY,
srv->is_source, attrs);
/* Look up the portal and update its mtime.
* This can help the application find out if a portal has
* seen ESIs recently, and react.
*/
if (srv->is_db && attrs->ial_count == 4) {
const isns_attr_t *addr_attr, *port_attr;
addr_attr = attrs->ial_data[2];
port_attr = attrs->ial_data[3];
if (addr_attr->ia_tag_id == ISNS_TAG_PORTAL_IP_ADDRESS
&& port_attr->ia_tag_id == ISNS_TAG_PORTAL_TCP_UDP_PORT) {
isns_attr_list_t key;
key.ial_count = 2;
key.ial_data = attrs->ial_data + 2;
portal = isns_db_lookup(srv->is_db,
&isns_portal_template,
&key);
}
if (portal)
portal->ie_mtime = time(NULL);
}
return ISNS_SUCCESS;
}
void
isns_process_esi_response(uint32_t xid, int status, isns_simple_t *msg)
{
isns_portal_info_t portal_info;
isns_esi_portal_t *esp;
isns_esi_t *esi;
char *ps;
if (msg == NULL) {
isns_debug_esi("ESI call 0x%x timed out\n", xid);
return;
}
/* FIXME: As a matter of security, we should probably
* verify that the ESI response originated from the
* portal we sent it to; or at least that it was authenticated
* by the client we think we're talking to. */
/* Get the portal */
if (!isns_portal_from_attr_list(&portal_info,
ISNS_TAG_PORTAL_IP_ADDRESS,
ISNS_TAG_PORTAL_TCP_UDP_PORT,
&msg->is_message_attrs)) {
isns_debug_esi("Ignoring unintelligible ESI response\n");
return;
}
if (!(esp = isns_esi_get_msg_portal(xid, &esi))) {
isns_debug_esi("Ignoring unmatched ESI reply\n");
return;
}
if (!isns_portal_equal(&esp->esp_portal, &portal_info)) {
ps = isns_portal_string(&portal_info);
isns_warning("Faked ESI response for portal %s\n",
ps);
free(ps);
return;
}
ps = isns_portal_string(&portal_info);
isns_debug_esi("Good ESI response from %s\n", ps);
free(ps);
isns_esi_restart(esp);
/* Refresh the entity's registration timestamp */
isns_object_set_uint64(esi->esi_object,
ISNS_TAG_TIMESTAMP,
time(NULL));
isns_db_sync(isns_esi_server->is_db);
}
/*
* Helper function to schedule the next timeout
*/
static void
isns_esi_schedule(int timeout)
{
isns_cancel_timer(isns_esi_transmit, NULL);
isns_add_oneshot_timer(timeout, isns_esi_transmit, NULL);
}
/*
* Register an entity for ESI monitoring.
* This is called when reloading the database.
*/
void
isns_esi_register(isns_object_t *obj)
{
if (!isns_esi_find(obj))
isns_esi_add_entity(obj);
/* We do not call esi_schedule(0) here; that happens in
* isns_esi_init already. */
}
/*
* This callback is invoked whenever an object is added/removed/modified.
* We use this to keep track of ESI portals and such.
*/
void
isns_esi_callback(const isns_db_event_t *ev, void *ptr)
{
isns_object_t *obj, *entity;
isns_esi_t *esi;
uint32_t event;
obj = ev->ie_object;
event = ev->ie_bits;
if (obj->ie_flags & ISNS_OBJECT_PRIVATE)
return;
isns_debug_esi("isns_esi_callback(%p, 0x%x)\n", obj, event);
if (ISNS_IS_ENTITY(obj)
&& (event & ISNS_SCN_OBJECT_ADDED_MASK)) {
if (!isns_esi_find(obj))
isns_esi_add_entity(obj);
/* Schedule an immediate ESI timer run */
isns_esi_schedule(0);
return;
}
if (!(entity = isns_object_get_entity(obj)))
return;
esi = isns_esi_find(entity);
if (esi != NULL)
esi->esi_update = 1;
/* Schedule an immediate ESI timer run */
isns_esi_schedule(0);
}