-
Notifications
You must be signed in to change notification settings - Fork 5
/
control.c
811 lines (672 loc) · 19.3 KB
/
control.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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/*********************************************************************
* Copyright 2017-2018 Network Device Education Foundation, Inc. ("NetDEF")
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* control.c: implements the BFD daemon control socket. It will be used
* to talk with clients daemon/scripts/consumers.
*
* Authors
* -------
* Rafael Zalamena <[email protected]>
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <errno.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "bfd.h"
/*
* Prototypes
*/
void control_accept(evutil_socket_t sd, short ev, void *arg);
struct bfd_control_queue *control_queue_new(struct bfd_control_socket *bcs);
void control_queue_free(struct bfd_control_socket *bcs,
struct bfd_control_queue *bcq);
int control_queue_dequeue(struct bfd_control_socket *bcs);
int control_queue_enqueue(struct bfd_control_socket *bcs,
struct bfd_control_msg *bcm);
struct bfd_notify_peer *control_notifypeer_new(struct bfd_control_socket *bcs,
bfd_session *bs);
void control_notifypeer_free(struct bfd_control_socket *bcs,
struct bfd_notify_peer *bnp);
struct bfd_notify_peer *control_notifypeer_find(struct bfd_control_socket *bcs,
bfd_session *bs);
struct bfd_control_socket *control_new(int sd);
void control_free(struct bfd_control_socket *bcs);
void control_reset_buf(struct bfd_control_buffer *bcb);
void control_read(evutil_socket_t sd, short ev, void *arg);
void control_write(evutil_socket_t sd, short ev, void *arg);
void control_handle_request_add(struct bfd_control_socket *bcs,
struct bfd_control_msg *bcm);
void control_handle_request_del(struct bfd_control_socket *bcs,
struct bfd_control_msg *bcm);
int notify_add_cb(struct bfd_peer_cfg *bpc, void *arg);
int notify_del_cb(struct bfd_peer_cfg *bpc, void *arg);
void control_handle_notify_add(struct bfd_control_socket *bcs,
struct bfd_control_msg *bcm);
void control_handle_notify_del(struct bfd_control_socket *bcs,
struct bfd_control_msg *bcm);
void control_handle_notify(struct bfd_control_socket *bcs,
struct bfd_control_msg *bcm);
void control_response(struct bfd_control_socket *bcs, uint16_t id,
const char *status, const char *error);
static void _control_notify_config(struct bfd_control_socket *bcs,
const char *op, bfd_session *bs);
static void _control_notify(struct bfd_control_socket *bcs, bfd_session *bs);
/*
* Functions
*/
int control_init(const char *path)
{
int sd;
mode_t umval;
struct sockaddr_un sun = {
.sun_family = AF_UNIX, .sun_path = BFD_CONTROL_SOCK_PATH,
};
if (path) {
strxcpy(sun.sun_path, path, sizeof(sun.sun_path));
}
/* Remove previously created sockets. */
unlink(sun.sun_path);
sd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
PF_UNSPEC);
if (sd == -1) {
log_error("%s: socket: %s\n", __FUNCTION__, strerror(errno));
return -1;
}
umval = umask(0);
if (bind(sd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
log_error("%s: bind: %s\n", __FUNCTION__, strerror(errno));
close(sd);
return -1;
}
umask(umval);
if (listen(sd, SOMAXCONN) == -1) {
log_error("%s: listen: %s\n", __FUNCTION__, strerror(errno));
close(sd);
return -1;
}
bglobal.bg_csock = sd;
event_assign(&bglobal.bg_csockev, bglobal.bg_eb, sd,
EV_READ | EV_PERSIST, control_accept, NULL);
event_add(&bglobal.bg_csockev, NULL);
return 0;
}
void control_accept(evutil_socket_t sd, short ev __attribute__((unused)),
void *arg __attribute__((unused)))
{
int csock;
csock = accept(sd, NULL, 0);
if (csock == -1) {
log_warning("%s: accept: %s\n", __FUNCTION__, strerror(errno));
return;
}
if (control_new(csock) == NULL)
close(csock);
}
/*
* Client handling
*/
struct bfd_control_socket *control_new(int sd)
{
struct bfd_control_socket *bcs;
bcs = calloc(1, sizeof(*bcs));
if (bcs == NULL)
return NULL;
/* Disable notifications by default. */
bcs->bcs_notify = 0;
bcs->bcs_sd = sd;
event_assign(&bcs->bcs_ev, bglobal.bg_eb, sd, EV_READ | EV_PERSIST,
control_read, bcs);
event_assign(&bcs->bcs_outev, bglobal.bg_eb, sd, EV_WRITE | EV_PERSIST,
control_write, bcs);
event_add(&bcs->bcs_ev, NULL);
TAILQ_INIT(&bcs->bcs_bcqueue);
TAILQ_INIT(&bcs->bcs_bnplist);
TAILQ_INSERT_TAIL(&bglobal.bg_bcslist, bcs, bcs_entry);
return bcs;
}
void control_free(struct bfd_control_socket *bcs)
{
struct bfd_control_queue *bcq;
struct bfd_notify_peer *bnp;
event_del(&bcs->bcs_outev);
event_del(&bcs->bcs_ev);
close(bcs->bcs_sd);
TAILQ_REMOVE(&bglobal.bg_bcslist, bcs, bcs_entry);
/* Empty output queue. */
while (!TAILQ_EMPTY(&bcs->bcs_bcqueue)) {
bcq = TAILQ_FIRST(&bcs->bcs_bcqueue);
control_queue_free(bcs, bcq);
}
/* Empty notification list. */
while (!TAILQ_EMPTY(&bcs->bcs_bnplist)) {
bnp = TAILQ_FIRST(&bcs->bcs_bnplist);
control_notifypeer_free(bcs, bnp);
}
control_reset_buf(&bcs->bcs_bin);
free(bcs);
}
struct bfd_notify_peer *control_notifypeer_new(struct bfd_control_socket *bcs,
bfd_session *bs)
{
struct bfd_notify_peer *bnp;
bnp = control_notifypeer_find(bcs, bs);
if (bnp)
return bnp;
bnp = calloc(1, sizeof(*bnp));
if (bnp == NULL) {
log_warning("%s: calloc: %s", __FUNCTION__, strerror(errno));
return NULL;
}
TAILQ_INSERT_TAIL(&bcs->bcs_bnplist, bnp, bnp_entry);
bnp->bnp_bs = bs;
bs->refcount++;
return bnp;
}
void control_notifypeer_free(struct bfd_control_socket *bcs,
struct bfd_notify_peer *bnp)
{
TAILQ_REMOVE(&bcs->bcs_bnplist, bnp, bnp_entry);
bnp->bnp_bs->refcount--;
free(bnp);
}
struct bfd_notify_peer *control_notifypeer_find(struct bfd_control_socket *bcs,
bfd_session *bs)
{
struct bfd_notify_peer *bnp;
TAILQ_FOREACH (bnp, &bcs->bcs_bnplist, bnp_entry) {
if (bnp->bnp_bs == bs)
return bnp;
}
return NULL;
}
struct bfd_control_queue *control_queue_new(struct bfd_control_socket *bcs)
{
struct bfd_control_queue *bcq;
bcq = calloc(1, sizeof(*bcq));
if (bcq == NULL) {
log_warning("%s: calloc: %s\n", __FUNCTION__, strerror(errno));
return NULL;
}
control_reset_buf(&bcq->bcq_bcb);
TAILQ_INSERT_TAIL(&bcs->bcs_bcqueue, bcq, bcq_entry);
return bcq;
}
void control_queue_free(struct bfd_control_socket *bcs,
struct bfd_control_queue *bcq)
{
control_reset_buf(&bcq->bcq_bcb);
TAILQ_REMOVE(&bcs->bcs_bcqueue, bcq, bcq_entry);
free(bcq);
}
int control_queue_dequeue(struct bfd_control_socket *bcs)
{
struct bfd_control_queue *bcq;
/* List is empty, nothing to do. */
if (TAILQ_EMPTY(&bcs->bcs_bcqueue)) {
event_del(&bcs->bcs_outev);
bcs->bcs_bout = NULL;
return 0;
}
bcq = TAILQ_FIRST(&bcs->bcs_bcqueue);
control_queue_free(bcs, bcq);
/* Get the next buffer to send. */
if (TAILQ_EMPTY(&bcs->bcs_bcqueue)) {
event_del(&bcs->bcs_outev);
bcs->bcs_bout = NULL;
return 0;
}
bcq = TAILQ_FIRST(&bcs->bcs_bcqueue);
bcs->bcs_bout = &bcq->bcq_bcb;
return 1;
}
int control_queue_enqueue(struct bfd_control_socket *bcs,
struct bfd_control_msg *bcm)
{
struct bfd_control_queue *bcq;
struct bfd_control_buffer *bcb;
bcq = control_queue_new(bcs);
if (bcq == NULL)
return -1;
bcb = &bcq->bcq_bcb;
bcb->bcb_left = sizeof(struct bfd_control_msg) + ntohl(bcm->bcm_length);
bcb->bcb_pos = 0;
bcb->bcb_bcm = bcm;
/* If this is the first item, then dequeue and start using it. */
if (bcs->bcs_bout == NULL) {
bcs->bcs_bout = bcb;
/* New messages, active write events. */
event_add(&bcs->bcs_outev, NULL);
}
return 0;
}
void control_reset_buf(struct bfd_control_buffer *bcb)
{
/* Get ride of old data. */
free(bcb->bcb_buf);
bcb->bcb_buf = NULL;
bcb->bcb_pos = 0;
bcb->bcb_left = 0;
}
void control_read(evutil_socket_t sd, short ev __attribute__((unused)),
void *arg)
{
struct bfd_control_socket *bcs = arg;
struct bfd_control_buffer *bcb = &bcs->bcs_bin;
struct bfd_control_msg bcm;
ssize_t bread;
size_t plen;
/*
* Check if we have already downloaded message content, if so then skip
* to
* download the rest of it and process.
*
* Otherwise download a new message header and allocate the necessary
* memory.
*/
if (bcb->bcb_buf != NULL)
goto skip_header;
bread = read(sd, &bcm, sizeof(bcm));
if (bread == 0) {
control_free(bcs);
return;
}
if (bread < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
return;
log_warning("%s: read: %s\n", __FUNCTION__, strerror(errno));
control_free(bcs);
return;
}
/* Validate header fields. */
plen = ntohl(bcm.bcm_length);
if (plen < 2) {
log_debug("%s: client closed due small message length: %d\n",
__FUNCTION__, bcm.bcm_length);
control_free(bcs);
return;
}
if (bcm.bcm_ver != BMV_VERSION_1) {
log_debug("%s: client closed due bad version: %d\n",
__FUNCTION__, bcm.bcm_ver);
control_free(bcs);
return;
}
/* Prepare the buffer to load the message. */
bcs->bcs_version = bcm.bcm_ver;
bcs->bcs_type = bcm.bcm_type;
bcb->bcb_pos = sizeof(bcm);
bcb->bcb_left = plen;
bcb->bcb_buf = malloc(sizeof(bcm) + bcb->bcb_left + 1);
if (bcb->bcb_buf == NULL) {
log_warning("%s: not enough memory for message size: %u\n",
__FUNCTION__, bcb->bcb_left);
control_free(bcs);
return;
}
memcpy(bcb->bcb_buf, &bcm, sizeof(bcm));
/* Terminate data string with NULL for later processing. */
bcb->bcb_buf[sizeof(bcm) + bcb->bcb_left] = 0;
skip_header:
/* Download the remaining data of the message and process it. */
bread = read(sd, &bcb->bcb_buf[bcb->bcb_pos], bcb->bcb_left);
if (bread == 0) {
control_free(bcs);
return;
}
if (bread < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
return;
log_warning("%s: read: %s\n", __FUNCTION__, strerror(errno));
control_free(bcs);
return;
}
bcb->bcb_pos += bread;
bcb->bcb_left -= bread;
/* We need more data, return to wait more. */
if (bcb->bcb_left > 0)
return;
switch (bcm.bcm_type) {
case BMT_REQUEST_ADD:
control_handle_request_add(bcs, bcb->bcb_bcm);
break;
case BMT_REQUEST_DEL:
control_handle_request_del(bcs, bcb->bcb_bcm);
break;
case BMT_NOTIFY:
control_handle_notify(bcs, bcb->bcb_bcm);
break;
case BMT_NOTIFY_ADD:
control_handle_notify_add(bcs, bcb->bcb_bcm);
break;
case BMT_NOTIFY_DEL:
control_handle_notify_del(bcs, bcb->bcb_bcm);
break;
default:
log_debug("%s: unhandled message type: %d\n", __FUNCTION__,
bcm.bcm_type);
control_response(bcs, bcm.bcm_id, BCM_RESPONSE_ERROR,
"invalid message type");
break;
}
bcs->bcs_version = 0;
bcs->bcs_type = 0;
control_reset_buf(bcb);
}
void control_write(evutil_socket_t sd, short ev __attribute__((unused)),
void *arg)
{
struct bfd_control_socket *bcs = arg;
struct bfd_control_buffer *bcb = bcs->bcs_bout;
ssize_t bwrite;
bwrite = write(sd, &bcb->bcb_buf[bcb->bcb_pos], bcb->bcb_left);
if (bwrite == 0) {
control_free(bcs);
return;
}
if (bwrite < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
return;
log_warning("%s: write: %s\n", __FUNCTION__, strerror(errno));
control_free(bcs);
return;
}
bcb->bcb_pos += bwrite;
bcb->bcb_left -= bwrite;
if (bcb->bcb_left > 0)
return;
control_queue_dequeue(bcs);
}
/*
* Message processing
*/
void control_handle_request_add(struct bfd_control_socket *bcs,
struct bfd_control_msg *bcm)
{
const char *json = (const char *)bcm->bcm_data;
if (config_request_add(json) == 0)
control_response(bcs, bcm->bcm_id, BCM_RESPONSE_OK, NULL);
else
control_response(bcs, bcm->bcm_id, BCM_RESPONSE_ERROR,
"request add failed");
}
void control_handle_request_del(struct bfd_control_socket *bcs,
struct bfd_control_msg *bcm)
{
const char *json = (const char *)bcm->bcm_data;
if (config_request_del(json) == 0)
control_response(bcs, bcm->bcm_id, BCM_RESPONSE_OK, NULL);
else
control_response(bcs, bcm->bcm_id, BCM_RESPONSE_ERROR,
"request del failed");
}
static bfd_session *_notify_find_peer(struct bfd_peer_cfg *bpc)
{
struct peer_label *pl;
bfd_session *bs;
bfd_shop_key shop;
bfd_mhop_key mhop;
if (bpc->bpc_has_label) {
pl = pl_find(bpc->bpc_label);
if (pl)
return pl->pl_bs;
}
memset(&shop, 0, sizeof(shop));
if (bpc->bpc_mhop) {
memset(&mhop, 0, sizeof(mhop));
mhop.peer = bpc->bpc_peer;
mhop.local = bpc->bpc_local;
if (bpc->bpc_has_vrfname)
strxcpy(mhop.vrf_name, bpc->bpc_vrfname,
sizeof(mhop.vrf_name));
bs = bfd_find_mhop(&mhop);
} else {
memset(&shop, 0, sizeof(shop));
shop.peer = bpc->bpc_peer;
if (!bpc->bpc_has_vxlan && bpc->bpc_has_localif)
strxcpy(shop.port_name, bpc->bpc_localif,
sizeof(shop.port_name));
bs = bfd_find_shop(&shop);
}
return bs;
}
void control_handle_notify(struct bfd_control_socket *bcs,
struct bfd_control_msg *bcm)
{
bcs->bcs_notify = *(uint64_t *)bcm->bcm_data;
control_response(bcs, bcm->bcm_id, BCM_RESPONSE_OK, NULL);
/*
* If peer asked for notification configuration, send everything that
* was configured until the moment to sync up.
*/
if (bcs->bcs_notify & BCM_NOTIFY_CONFIG) {
bfd_session *bs, *tmp;
extern bfd_session *session_hash;
HASH_ITER (sh, session_hash, bs, tmp) {
/* Notify peer configuration. */
_control_notify_config(bcs, BCM_NOTIFY_CONFIG_ADD, bs);
/* Notify peer status. */
_control_notify(bcs, bs);
}
}
/*
* If peer asked for notification configuration, send the current
* state to sync.
*/
if (bcs->bcs_notify & BCM_NOTIFY_PEER_STATE) {
bfd_session *bs, *tmp;
extern bfd_session *session_hash;
HASH_ITER (sh, session_hash, bs, tmp) {
/* Notify peer status. */
_control_notify(bcs, bs);
}
}
}
int notify_add_cb(struct bfd_peer_cfg *bpc, void *arg)
{
struct bfd_control_socket *bcs = arg;
bfd_session *bs = _notify_find_peer(bpc);
if (bs == NULL)
return -1;
if (control_notifypeer_new(bcs, bs) == NULL)
return -1;
/* Notify peer status. */
_control_notify(bcs, bs);
return 0;
}
int notify_del_cb(struct bfd_peer_cfg *bpc, void *arg)
{
struct bfd_control_socket *bcs = arg;
bfd_session *bs = _notify_find_peer(bpc);
struct bfd_notify_peer *bnp;
if (bs == NULL)
return -1;
bnp = control_notifypeer_find(bcs, bs);
if (bnp)
control_notifypeer_free(bcs, bnp);
return 0;
}
void control_handle_notify_add(struct bfd_control_socket *bcs,
struct bfd_control_msg *bcm)
{
const char *json = (const char *)bcm->bcm_data;
if (config_notify_request(bcs, json, notify_add_cb) == 0) {
control_response(bcs, bcm->bcm_id, BCM_RESPONSE_OK, NULL);
return;
}
control_response(bcs, bcm->bcm_id, BCM_RESPONSE_ERROR,
"failed to parse notify data");
}
void control_handle_notify_del(struct bfd_control_socket *bcs,
struct bfd_control_msg *bcm)
{
const char *json = (const char *)bcm->bcm_data;
if (config_notify_request(bcs, json, notify_del_cb) == 0) {
control_response(bcs, bcm->bcm_id, BCM_RESPONSE_OK, NULL);
return;
}
control_response(bcs, bcm->bcm_id, BCM_RESPONSE_ERROR,
"failed to parse notify data");
}
/*
* Internal functions used by the BFD daemon.
*/
void control_response(struct bfd_control_socket *bcs, uint16_t id,
const char *status, const char *error)
{
struct bfd_control_msg *bcm;
char *jsonstr;
size_t jsonstrlen;
/* Generate JSON response. */
jsonstr = config_response(status, error);
if (jsonstr == NULL) {
log_warning("%s: config_response: failed to get JSON str\n",
__FUNCTION__);
return;
}
/* Allocate data and answer. */
jsonstrlen = strlen(jsonstr);
bcm = malloc(sizeof(struct bfd_control_msg) + jsonstrlen);
if (bcm == NULL) {
log_warning("%s: malloc: %s\n", __FUNCTION__, strerror(errno));
free(jsonstr);
return;
}
bcm->bcm_length = htonl(jsonstrlen);
bcm->bcm_ver = BMV_VERSION_1;
bcm->bcm_type = BMT_RESPONSE;
bcm->bcm_id = id;
memcpy(bcm->bcm_data, jsonstr, jsonstrlen);
free(jsonstr);
control_queue_enqueue(bcs, bcm);
}
static void _control_notify(struct bfd_control_socket *bcs, bfd_session *bs)
{
struct bfd_control_msg *bcm;
char *jsonstr;
size_t jsonstrlen;
/* Generate JSON response. */
jsonstr = config_notify(bs);
if (jsonstr == NULL) {
log_warning("%s: config_notify: failed to get JSON str\n",
__FUNCTION__);
return;
}
/* Allocate data and answer. */
jsonstrlen = strlen(jsonstr);
bcm = malloc(sizeof(struct bfd_control_msg) + jsonstrlen);
if (bcm == NULL) {
log_warning("%s: malloc: %s\n", __FUNCTION__, strerror(errno));
free(jsonstr);
return;
}
bcm->bcm_length = htonl(jsonstrlen);
bcm->bcm_ver = BMV_VERSION_1;
bcm->bcm_type = BMT_NOTIFY;
bcm->bcm_id = htons(BCM_NOTIFY_ID);
memcpy(bcm->bcm_data, jsonstr, jsonstrlen);
free(jsonstr);
control_queue_enqueue(bcs, bcm);
}
int control_notify(bfd_session *bs)
{
struct bfd_control_socket *bcs;
struct bfd_notify_peer *bnp;
/*
* PERFORMANCE: reuse the bfd_control_msg allocated data for
* all control sockets to avoid wasting memory.
*/
TAILQ_FOREACH (bcs, &bglobal.bg_bcslist, bcs_entry) {
/*
* Test for all notifications first, then search for
* specific peers.
*/
if ((bcs->bcs_notify & BCM_NOTIFY_PEER_STATE) == 0) {
bnp = control_notifypeer_find(bcs, bs);
/*
* If the notification is not configured here,
* don't send it.
*/
if (bnp == NULL)
continue;
}
_control_notify(bcs, bs);
}
return 0;
}
static void _control_notify_config(struct bfd_control_socket *bcs,
const char *op, bfd_session *bs)
{
struct bfd_control_msg *bcm;
char *jsonstr;
size_t jsonstrlen;
/* Generate JSON response. */
jsonstr = config_notify_config(op, bs);
if (jsonstr == NULL) {
log_warning(
"%s: config_notify_config: failed to get JSON str\n",
__FUNCTION__);
return;
}
/* Allocate data and answer. */
jsonstrlen = strlen(jsonstr);
bcm = malloc(sizeof(struct bfd_control_msg) + jsonstrlen);
if (bcm == NULL) {
log_warning("%s: malloc: %s\n", __FUNCTION__, strerror(errno));
free(jsonstr);
return;
}
bcm->bcm_length = htonl(jsonstrlen);
bcm->bcm_ver = BMV_VERSION_1;
bcm->bcm_type = BMT_NOTIFY;
bcm->bcm_id = htons(BCM_NOTIFY_ID);
memcpy(bcm->bcm_data, jsonstr, jsonstrlen);
free(jsonstr);
control_queue_enqueue(bcs, bcm);
}
int control_notify_config(const char *op, bfd_session *bs)
{
struct bfd_control_socket *bcs;
struct bfd_notify_peer *bnp;
/* Remove the control sockets notification for this peer. */
if (strcmp(op, BCM_NOTIFY_CONFIG_DELETE) == 0 && bs->refcount > 0) {
TAILQ_FOREACH (bcs, &bglobal.bg_bcslist, bcs_entry) {
bnp = control_notifypeer_find(bcs, bs);
if (bnp)
control_notifypeer_free(bcs, bnp);
}
}
/*
* PERFORMANCE: reuse the bfd_control_msg allocated data for
* all control sockets to avoid wasting memory.
*/
TAILQ_FOREACH (bcs, &bglobal.bg_bcslist, bcs_entry) {
/*
* Test for all notifications first, then search for
* specific peers.
*/
if ((bcs->bcs_notify & BCM_NOTIFY_CONFIG) == 0) {
continue;
}
_control_notify_config(bcs, op, bs);
}
return 0;
}