]> git.proxmox.com Git - mirror_frr.git/blame_incremental - ldpd/accept.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / ldpd / accept.c
... / ...
CommitLineData
1// SPDX-License-Identifier: ISC
2/* $OpenBSD$ */
3
4/*
5 * Copyright (c) 2012 Claudio Jeker <claudio@openbsd.org>
6 */
7
8#include <zebra.h>
9
10#include "ldpd.h"
11#include "ldpe.h"
12#include "log.h"
13
14struct accept_ev {
15 LIST_ENTRY(accept_ev) entry;
16 struct thread *ev;
17 void (*accept_cb)(struct thread *);
18 void *arg;
19 int fd;
20};
21
22struct {
23 LIST_HEAD(, accept_ev) queue;
24 struct thread *evt;
25} accept_queue;
26
27static void accept_arm(void);
28static void accept_unarm(void);
29static void accept_cb(struct thread *);
30static void accept_timeout(struct thread *);
31
32void
33accept_init(void)
34{
35 LIST_INIT(&accept_queue.queue);
36}
37
38int accept_add(int fd, void (*cb)(struct thread *), void *arg)
39{
40 struct accept_ev *av;
41
42 if ((av = calloc(1, sizeof(*av))) == NULL)
43 return (-1);
44 av->fd = fd;
45 av->accept_cb = cb;
46 av->arg = arg;
47 LIST_INSERT_HEAD(&accept_queue.queue, av, entry);
48
49 thread_add_read(master, accept_cb, av, av->fd, &av->ev);
50
51 log_debug("%s: accepting on fd %d", __func__, fd);
52
53 return (0);
54}
55
56void
57accept_del(int fd)
58{
59 struct accept_ev *av;
60
61 LIST_FOREACH(av, &accept_queue.queue, entry)
62 if (av->fd == fd) {
63 log_debug("%s: %d removed from queue", __func__, fd);
64 THREAD_OFF(av->ev);
65 LIST_REMOVE(av, entry);
66 free(av);
67 return;
68 }
69}
70
71void
72accept_pause(void)
73{
74 log_debug(__func__);
75 accept_unarm();
76 thread_add_timer(master, accept_timeout, NULL, 1, &accept_queue.evt);
77}
78
79void
80accept_unpause(void)
81{
82 if (accept_queue.evt != NULL) {
83 log_debug(__func__);
84 THREAD_OFF(accept_queue.evt);
85 accept_arm();
86 }
87}
88
89static void
90accept_arm(void)
91{
92 struct accept_ev *av;
93 LIST_FOREACH(av, &accept_queue.queue, entry) {
94 thread_add_read(master, accept_cb, av, av->fd, &av->ev);
95 }
96}
97
98static void
99accept_unarm(void)
100{
101 struct accept_ev *av;
102 LIST_FOREACH(av, &accept_queue.queue, entry)
103 THREAD_OFF(av->ev);
104}
105
106static void accept_cb(struct thread *thread)
107{
108 struct accept_ev *av = THREAD_ARG(thread);
109 thread_add_read(master, accept_cb, av, av->fd, &av->ev);
110 av->accept_cb(thread);
111}
112
113static void accept_timeout(struct thread *thread)
114{
115 accept_queue.evt = NULL;
116
117 log_debug(__func__);
118 accept_arm();
119}