]> git.proxmox.com Git - mirror_frr.git/blob - ldpd/accept.c
ldpd: adapt the code for Quagga
[mirror_frr.git] / ldpd / accept.c
1 /* $OpenBSD$ */
2
3 /*
4 * Copyright (c) 2012 Claudio Jeker <claudio@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <zebra.h>
20
21 #include "ldpd.h"
22 #include "ldpe.h"
23 #include "log.h"
24
25 struct accept_ev {
26 LIST_ENTRY(accept_ev) entry;
27 struct thread *ev;
28 int (*accept_cb)(struct thread *);
29 void *arg;
30 int fd;
31 };
32
33 struct {
34 LIST_HEAD(, accept_ev) queue;
35 struct thread *evt;
36 } accept_queue;
37
38 static void accept_arm(void);
39 static void accept_unarm(void);
40 static int accept_cb(struct thread *);
41 static int accept_timeout(struct thread *);
42
43 void
44 accept_init(void)
45 {
46 LIST_INIT(&accept_queue.queue);
47 }
48
49 int
50 accept_add(int fd, int (*cb)(struct thread *), void *arg)
51 {
52 struct accept_ev *av;
53
54 if ((av = calloc(1, sizeof(*av))) == NULL)
55 return (-1);
56 av->fd = fd;
57 av->accept_cb = cb;
58 av->arg = arg;
59 LIST_INSERT_HEAD(&accept_queue.queue, av, entry);
60
61 av->ev = thread_add_read(master, accept_cb, av, av->fd);
62
63 log_debug("%s: accepting on fd %d", __func__, fd);
64
65 return (0);
66 }
67
68 void
69 accept_del(int fd)
70 {
71 struct accept_ev *av;
72
73 LIST_FOREACH(av, &accept_queue.queue, entry)
74 if (av->fd == fd) {
75 log_debug("%s: %d removed from queue", __func__, fd);
76 THREAD_READ_OFF(av->ev);
77 LIST_REMOVE(av, entry);
78 free(av);
79 return;
80 }
81 }
82
83 void
84 accept_pause(void)
85 {
86 log_debug(__func__);
87 accept_unarm();
88 accept_queue.evt = thread_add_timer(master, accept_timeout, NULL, 1);
89 }
90
91 void
92 accept_unpause(void)
93 {
94 if (accept_queue.evt != NULL) {
95 log_debug(__func__);
96 THREAD_TIMER_OFF(accept_queue.evt);
97 accept_arm();
98 }
99 }
100
101 static void
102 accept_arm(void)
103 {
104 struct accept_ev *av;
105 LIST_FOREACH(av, &accept_queue.queue, entry)
106 av->ev = thread_add_read(master, accept_cb, av, av->fd);
107 }
108
109 static void
110 accept_unarm(void)
111 {
112 struct accept_ev *av;
113 LIST_FOREACH(av, &accept_queue.queue, entry)
114 THREAD_READ_OFF(av->ev);
115 }
116
117 static int
118 accept_cb(struct thread *thread)
119 {
120 struct accept_ev *av = THREAD_ARG(thread);
121 av->ev = thread_add_read(master, accept_cb, av, av->fd);
122 av->accept_cb(thread);
123
124 return (0);
125 }
126
127 static int
128 accept_timeout(struct thread *thread)
129 {
130 accept_queue.evt = NULL;
131
132 log_debug(__func__);
133 accept_arm();
134
135 return (0);
136 }