]>
Commit | Line | Data |
---|---|---|
2fb975da TT |
1 | /* NHRP event manager |
2 | * Copyright (c) 2014-2015 Timo Teräs | |
3 | * | |
4 | * This file is free software: you may copy, redistribute and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | */ | |
9 | ||
10 | #include <string.h> | |
11 | #include <sys/socket.h> | |
12 | #include <sys/un.h> | |
13 | ||
14 | #include "thread.h" | |
15 | #include "zbuf.h" | |
16 | #include "log.h" | |
17 | #include "nhrpd.h" | |
18 | ||
19 | const char *nhrp_event_socket_path; | |
20 | struct nhrp_reqid_pool nhrp_event_reqid; | |
21 | ||
22 | struct event_manager { | |
23 | struct thread *t_reconnect, *t_read, *t_write; | |
24 | struct zbuf ibuf; | |
25 | struct zbuf_queue obuf; | |
26 | int fd; | |
27 | uint8_t ibuf_data[4*1024]; | |
28 | }; | |
29 | ||
30 | static int evmgr_reconnect(struct thread *t); | |
31 | ||
32 | static void evmgr_connection_error(struct event_manager *evmgr) | |
33 | { | |
34 | THREAD_OFF(evmgr->t_read); | |
35 | THREAD_OFF(evmgr->t_write); | |
36 | zbuf_reset(&evmgr->ibuf); | |
37 | zbufq_reset(&evmgr->obuf); | |
38 | ||
39 | if (evmgr->fd >= 0) | |
40 | close(evmgr->fd); | |
41 | evmgr->fd = -1; | |
42 | if (nhrp_event_socket_path) | |
ffa2c898 QY |
43 | thread_add_timer_msec(master, evmgr_reconnect, evmgr, 10, |
44 | &evmgr->t_reconnect); | |
2fb975da TT |
45 | } |
46 | ||
47 | static void evmgr_recv_message(struct event_manager *evmgr, struct zbuf *zb) | |
48 | { | |
49 | struct zbuf zl; | |
50 | uint32_t eventid = 0; | |
51 | size_t len; | |
52 | char buf[256], result[64] = ""; | |
53 | ||
54 | while (zbuf_may_pull_until(zb, "\n", &zl)) { | |
55 | len = zbuf_used(&zl) - 1; | |
56 | if (len >= sizeof(buf)-1) | |
57 | continue; | |
58 | memcpy(buf, zbuf_pulln(&zl, len), len); | |
59 | buf[len] = 0; | |
60 | ||
61 | debugf(NHRP_DEBUG_EVENT, "evmgr: msg: %s", buf); | |
6c8ca260 JB |
62 | if (sscanf(buf, "eventid=%d", &eventid) != 1) |
63 | continue; | |
64 | if (sscanf(buf, "result=%63s", result) != 1) | |
65 | continue; | |
2fb975da TT |
66 | } |
67 | debugf(NHRP_DEBUG_EVENT, "evmgr: received: eventid=%d result=%s", eventid, result); | |
68 | if (eventid && result[0]) { | |
69 | struct nhrp_reqid *r = nhrp_reqid_lookup(&nhrp_event_reqid, eventid); | |
70 | if (r) r->cb(r, result); | |
71 | } | |
72 | } | |
73 | ||
74 | static int evmgr_read(struct thread *t) | |
75 | { | |
76 | struct event_manager *evmgr = THREAD_ARG(t); | |
77 | struct zbuf *ibuf = &evmgr->ibuf; | |
78 | struct zbuf msg; | |
79 | ||
80 | evmgr->t_read = NULL; | |
81 | if (zbuf_read(ibuf, evmgr->fd, (size_t) -1) < 0) { | |
82 | evmgr_connection_error(evmgr); | |
83 | return 0; | |
84 | } | |
85 | ||
86 | /* Process all messages in buffer */ | |
87 | while (zbuf_may_pull_until(ibuf, "\n\n", &msg)) | |
88 | evmgr_recv_message(evmgr, &msg); | |
89 | ||
ffa2c898 | 90 | thread_add_read(master, evmgr_read, evmgr, evmgr->fd, &evmgr->t_read); |
2fb975da TT |
91 | return 0; |
92 | } | |
93 | ||
94 | static int evmgr_write(struct thread *t) | |
95 | { | |
96 | struct event_manager *evmgr = THREAD_ARG(t); | |
97 | int r; | |
98 | ||
99 | evmgr->t_write = NULL; | |
100 | r = zbufq_write(&evmgr->obuf, evmgr->fd); | |
101 | if (r > 0) { | |
ffa2c898 QY |
102 | thread_add_write(master, evmgr_write, evmgr, evmgr->fd, |
103 | &evmgr->t_write); | |
2fb975da TT |
104 | } else if (r < 0) { |
105 | evmgr_connection_error(evmgr); | |
106 | } | |
107 | ||
108 | return 0; | |
109 | } | |
110 | ||
111 | static void evmgr_hexdump(struct zbuf *zb, const uint8_t *val, size_t vallen) | |
112 | { | |
113 | static const char xd[] = "0123456789abcdef"; | |
114 | size_t i; | |
115 | char *ptr; | |
116 | ||
117 | ptr = zbuf_pushn(zb, 2*vallen); | |
118 | if (!ptr) return; | |
119 | ||
120 | for (i = 0; i < vallen; i++) { | |
121 | uint8_t b = val[i]; | |
122 | *(ptr++) = xd[b >> 4]; | |
123 | *(ptr++) = xd[b & 0xf]; | |
124 | } | |
125 | } | |
126 | ||
127 | static void evmgr_put(struct zbuf *zb, const char *fmt, ...) | |
128 | { | |
129 | const char *pos, *nxt, *str; | |
130 | const uint8_t *bin; | |
131 | const union sockunion *su; | |
132 | int len; | |
133 | va_list va; | |
134 | ||
135 | va_start(va, fmt); | |
136 | for (pos = fmt; (nxt = strchr(pos, '%')) != NULL; pos = nxt + 2) { | |
137 | zbuf_put(zb, pos, nxt-pos); | |
138 | switch (nxt[1]) { | |
139 | case '%': | |
140 | zbuf_put8(zb, '%'); | |
141 | break; | |
142 | case 'u': | |
143 | zb->tail += snprintf((char *) zb->tail, zbuf_tailroom(zb), "%u", va_arg(va, uint32_t)); | |
144 | break; | |
145 | case 's': | |
146 | str = va_arg(va, const char *); | |
147 | zbuf_put(zb, str, strlen(str)); | |
148 | break; | |
149 | case 'U': | |
150 | su = va_arg(va, const union sockunion *); | |
151 | if (sockunion2str(su, (char *) zb->tail, zbuf_tailroom(zb))) | |
152 | zb->tail += strlen((char *) zb->tail); | |
153 | else | |
154 | zbuf_set_werror(zb); | |
155 | break; | |
156 | case 'H': | |
157 | bin = va_arg(va, const uint8_t *); | |
158 | len = va_arg(va, int); | |
159 | evmgr_hexdump(zb, bin, len); | |
160 | break; | |
161 | } | |
162 | } | |
163 | va_end(va); | |
164 | zbuf_put(zb, pos, strlen(pos)); | |
165 | } | |
166 | ||
167 | static void evmgr_submit(struct event_manager *evmgr, struct zbuf *obuf) | |
168 | { | |
169 | if (obuf->error) { | |
170 | zbuf_free(obuf); | |
171 | return; | |
172 | } | |
173 | zbuf_put(obuf, "\n", 1); | |
174 | zbufq_queue(&evmgr->obuf, obuf); | |
175 | if (evmgr->fd >= 0) | |
ffa2c898 QY |
176 | thread_add_write(master, evmgr_write, evmgr, evmgr->fd, |
177 | &evmgr->t_write); | |
2fb975da TT |
178 | } |
179 | ||
180 | static int evmgr_reconnect(struct thread *t) | |
181 | { | |
182 | struct event_manager *evmgr = THREAD_ARG(t); | |
183 | int fd; | |
184 | ||
185 | evmgr->t_reconnect = NULL; | |
186 | if (evmgr->fd >= 0 || !nhrp_event_socket_path) return 0; | |
187 | ||
188 | fd = sock_open_unix(nhrp_event_socket_path); | |
189 | if (fd < 0) { | |
190 | zlog_warn("%s: failure connecting nhrp-event socket: %s", | |
191 | __PRETTY_FUNCTION__, strerror(errno)); | |
192 | zbufq_reset(&evmgr->obuf); | |
ffa2c898 QY |
193 | thread_add_timer(master, evmgr_reconnect, evmgr, 10, |
194 | &evmgr->t_reconnect); | |
2fb975da TT |
195 | return 0; |
196 | } | |
197 | ||
198 | zlog_info("Connected to Event Manager"); | |
199 | evmgr->fd = fd; | |
ffa2c898 | 200 | thread_add_read(master, evmgr_read, evmgr, evmgr->fd, &evmgr->t_read); |
2fb975da TT |
201 | |
202 | return 0; | |
203 | } | |
204 | ||
205 | static struct event_manager evmgr_connection; | |
206 | ||
207 | void evmgr_init(void) | |
208 | { | |
209 | struct event_manager *evmgr = &evmgr_connection; | |
210 | ||
211 | evmgr->fd = -1; | |
212 | zbuf_init(&evmgr->ibuf, evmgr->ibuf_data, sizeof(evmgr->ibuf_data), 0); | |
213 | zbufq_init(&evmgr->obuf); | |
ffa2c898 QY |
214 | thread_add_timer_msec(master, evmgr_reconnect, evmgr, 10, |
215 | &evmgr->t_reconnect); | |
2fb975da TT |
216 | } |
217 | ||
218 | void evmgr_set_socket(const char *socket) | |
219 | { | |
d258c885 | 220 | if (nhrp_event_socket_path) { |
2fb975da | 221 | free((char *) nhrp_event_socket_path); |
d258c885 DS |
222 | nhrp_event_socket_path = NULL; |
223 | } | |
224 | if (socket) | |
225 | nhrp_event_socket_path = strdup(socket); | |
2fb975da TT |
226 | evmgr_connection_error(&evmgr_connection); |
227 | } | |
228 | ||
229 | void evmgr_terminate(void) | |
230 | { | |
231 | } | |
232 | ||
233 | void evmgr_notify(const char *name, struct nhrp_cache *c, void (*cb)(struct nhrp_reqid *, void *)) | |
234 | { | |
235 | struct event_manager *evmgr = &evmgr_connection; | |
236 | struct nhrp_vc *vc; | |
237 | struct nhrp_interface *nifp = c->ifp->info; | |
238 | struct zbuf *zb; | |
239 | afi_t afi = family2afi(sockunion_family(&c->remote_addr)); | |
240 | ||
241 | if (!nhrp_event_socket_path) { | |
242 | cb(&c->eventid, (void*) "accept"); | |
243 | return; | |
244 | } | |
245 | ||
246 | debugf(NHRP_DEBUG_EVENT, "evmgr: sending event %s", name); | |
247 | ||
248 | vc = c->new.peer ? c->new.peer->vc : NULL; | |
249 | zb = zbuf_alloc(1024 + (vc ? (vc->local.certlen + vc->remote.certlen) * 2 : 0)); | |
250 | ||
251 | if (cb) { | |
252 | nhrp_reqid_free(&nhrp_event_reqid, &c->eventid); | |
253 | evmgr_put(zb, | |
254 | "eventid=%u\n", | |
255 | nhrp_reqid_alloc(&nhrp_event_reqid, &c->eventid, cb)); | |
256 | } | |
257 | ||
258 | evmgr_put(zb, | |
259 | "event=%s\n" | |
260 | "type=%s\n" | |
261 | "old_type=%s\n" | |
262 | "num_nhs=%u\n" | |
263 | "interface=%s\n" | |
264 | "local_addr=%U\n", | |
265 | name, | |
266 | nhrp_cache_type_str[c->new.type], | |
267 | nhrp_cache_type_str[c->cur.type], | |
268 | (unsigned int) nhrp_cache_counts[NHRP_CACHE_NHS], | |
269 | c->ifp->name, | |
270 | &nifp->afi[afi].addr); | |
271 | ||
272 | if (vc) { | |
273 | evmgr_put(zb, | |
274 | "vc_initiated=%s\n" | |
275 | "local_nbma=%U\n" | |
276 | "local_cert=%H\n" | |
277 | "remote_addr=%U\n" | |
278 | "remote_nbma=%U\n" | |
279 | "remote_cert=%H\n", | |
280 | c->new.peer->requested ? "yes" : "no", | |
281 | &vc->local.nbma, | |
282 | vc->local.cert, vc->local.certlen, | |
283 | &c->remote_addr, &vc->remote.nbma, | |
284 | vc->remote.cert, vc->remote.certlen); | |
285 | } | |
286 | ||
287 | evmgr_submit(evmgr, zb); | |
288 | } | |
289 |