blob: 0c3461e71b9f20948cd68fabcf7e2162c967e4be [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
ajs634f9ea2005-04-11 15:51:40 +00003 * Copyright (C) 2005 Andrew J. Schorr
paul718e3742002-12-13 20:15:29 +00004 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * MA 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "prefix.h"
26#include "stream.h"
ajs634f9ea2005-04-11 15:51:40 +000027#include "buffer.h"
paul718e3742002-12-13 20:15:29 +000028#include "network.h"
29#include "if.h"
30#include "log.h"
31#include "thread.h"
32#include "zclient.h"
33#include "memory.h"
34#include "table.h"
paul718e3742002-12-13 20:15:29 +000035
36/* Zebra client events. */
37enum event {ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT};
38
39/* Prototype for event manager. */
40static void zclient_event (enum event, struct zclient *);
41
ajs634f9ea2005-04-11 15:51:40 +000042extern struct thread_master *master;
43
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +040044char *zclient_serv_path = NULL;
45
paul718e3742002-12-13 20:15:29 +000046/* This file local debug flag. */
47int zclient_debug = 0;
48
49/* Allocate zclient structure. */
50struct zclient *
51zclient_new ()
52{
53 struct zclient *zclient;
Stephen Hemminger393deb92008-08-18 14:13:29 -070054 zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
paul718e3742002-12-13 20:15:29 +000055
56 zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
57 zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs634f9ea2005-04-11 15:51:40 +000058 zclient->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +000059
60 return zclient;
61}
62
Chris Caputo228da422009-07-18 05:44:03 +000063/* This function is only called when exiting, because
ajs634f9ea2005-04-11 15:51:40 +000064 many parts of the code do not check for I/O errors, so they could
65 reference an invalid pointer if the structure was ever freed.
ajs634f9ea2005-04-11 15:51:40 +000066
Chris Caputo228da422009-07-18 05:44:03 +000067 Free zclient structure. */
paul718e3742002-12-13 20:15:29 +000068void
69zclient_free (struct zclient *zclient)
70{
ajs634f9ea2005-04-11 15:51:40 +000071 if (zclient->ibuf)
72 stream_free(zclient->ibuf);
73 if (zclient->obuf)
74 stream_free(zclient->obuf);
75 if (zclient->wb)
76 buffer_free(zclient->wb);
77
paul718e3742002-12-13 20:15:29 +000078 XFREE (MTYPE_ZCLIENT, zclient);
79}
80
81/* Initialize zebra client. Argument redist_default is unwanted
82 redistribute route type. */
83void
84zclient_init (struct zclient *zclient, int redist_default)
85{
86 int i;
87
88 /* Enable zebra client connection by default. */
89 zclient->enable = 1;
90
91 /* Set -1 to the default socket value. */
92 zclient->sock = -1;
93
94 /* Clear redistribution flags. */
95 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
96 zclient->redist[i] = 0;
97
98 /* Set unwanted redistribute route. bgpd does not need BGP route
99 redistribution. */
100 zclient->redist_default = redist_default;
101 zclient->redist[redist_default] = 1;
102
103 /* Set default-information redistribute to zero. */
104 zclient->default_information = 0;
105
106 /* Schedule first zclient connection. */
107 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000108 zlog_debug ("zclient start scheduled");
paul718e3742002-12-13 20:15:29 +0000109
110 zclient_event (ZCLIENT_SCHEDULE, zclient);
111}
112
113/* Stop zebra client services. */
114void
115zclient_stop (struct zclient *zclient)
116{
117 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000118 zlog_debug ("zclient stopped");
paul718e3742002-12-13 20:15:29 +0000119
120 /* Stop threads. */
ajs634f9ea2005-04-11 15:51:40 +0000121 THREAD_OFF(zclient->t_read);
122 THREAD_OFF(zclient->t_connect);
123 THREAD_OFF(zclient->t_write);
124
125 /* Reset streams. */
126 stream_reset(zclient->ibuf);
127 stream_reset(zclient->obuf);
128
129 /* Empty the write buffer. */
130 buffer_reset(zclient->wb);
paul718e3742002-12-13 20:15:29 +0000131
132 /* Close socket. */
133 if (zclient->sock >= 0)
134 {
135 close (zclient->sock);
136 zclient->sock = -1;
137 }
138 zclient->fail = 0;
139}
140
141void
142zclient_reset (struct zclient *zclient)
143{
144 zclient_stop (zclient);
145 zclient_init (zclient, zclient->redist_default);
146}
147
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400148#ifdef HAVE_TCP_ZEBRA
149
paul718e3742002-12-13 20:15:29 +0000150/* Make socket to zebra daemon. Return zebra socket. */
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400151static int
ajs634f9ea2005-04-11 15:51:40 +0000152zclient_socket(void)
paul718e3742002-12-13 20:15:29 +0000153{
154 int sock;
155 int ret;
156 struct sockaddr_in serv;
157
158 /* We should think about IPv6 connection. */
159 sock = socket (AF_INET, SOCK_STREAM, 0);
160 if (sock < 0)
161 return -1;
162
163 /* Make server socket. */
164 memset (&serv, 0, sizeof (struct sockaddr_in));
165 serv.sin_family = AF_INET;
166 serv.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000167#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000168 serv.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000169#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000170 serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
171
172 /* Connect to zebra. */
173 ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
174 if (ret < 0)
175 {
176 close (sock);
177 return -1;
178 }
179 return sock;
180}
181
Vyacheslav Trushkin3414d032011-11-30 21:03:44 +0400182#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400183
paul718e3742002-12-13 20:15:29 +0000184/* For sockaddr_un. */
185#include <sys/un.h>
186
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400187static int
hasso8c328f12004-10-05 21:01:23 +0000188zclient_socket_un (const char *path)
paul718e3742002-12-13 20:15:29 +0000189{
190 int ret;
191 int sock, len;
192 struct sockaddr_un addr;
193
194 sock = socket (AF_UNIX, SOCK_STREAM, 0);
195 if (sock < 0)
196 return -1;
197
198 /* Make server socket. */
199 memset (&addr, 0, sizeof (struct sockaddr_un));
200 addr.sun_family = AF_UNIX;
201 strncpy (addr.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000202#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +0000203 len = addr.sun_len = SUN_LEN(&addr);
204#else
205 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000206#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +0000207
208 ret = connect (sock, (struct sockaddr *) &addr, len);
209 if (ret < 0)
210 {
211 close (sock);
212 return -1;
213 }
214 return sock;
215}
216
Vyacheslav Trushkin3414d032011-11-30 21:03:44 +0400217#endif /* HAVE_TCP_ZEBRA */
218
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400219/**
220 * Connect to zebra daemon.
221 * @param zclient a pointer to zclient structure
222 * @return socket fd just to make sure that connection established
223 * @see zclient_init
224 * @see zclient_new
225 */
226int
227zclient_socket_connect (struct zclient *zclient)
228{
229#ifdef HAVE_TCP_ZEBRA
230 zclient->sock = zclient_socket ();
231#else
232 zclient->sock = zclient_socket_un (zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH);
233#endif
234 return zclient->sock;
235}
236
ajs634f9ea2005-04-11 15:51:40 +0000237static int
238zclient_failed(struct zclient *zclient)
239{
240 zclient->fail++;
241 zclient_stop(zclient);
242 zclient_event(ZCLIENT_CONNECT, zclient);
243 return -1;
244}
245
246static int
247zclient_flush_data(struct thread *thread)
248{
249 struct zclient *zclient = THREAD_ARG(thread);
250
251 zclient->t_write = NULL;
252 if (zclient->sock < 0)
253 return -1;
254 switch (buffer_flush_available(zclient->wb, zclient->sock))
255 {
256 case BUFFER_ERROR:
257 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
258 __func__, zclient->sock);
259 return zclient_failed(zclient);
260 break;
261 case BUFFER_PENDING:
262 zclient->t_write = thread_add_write(master, zclient_flush_data,
263 zclient, zclient->sock);
264 break;
265 case BUFFER_EMPTY:
266 break;
267 }
268 return 0;
269}
270
paul718e3742002-12-13 20:15:29 +0000271int
ajs634f9ea2005-04-11 15:51:40 +0000272zclient_send_message(struct zclient *zclient)
273{
274 if (zclient->sock < 0)
275 return -1;
276 switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf),
277 stream_get_endp(zclient->obuf)))
278 {
279 case BUFFER_ERROR:
280 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
281 __func__, zclient->sock);
282 return zclient_failed(zclient);
283 break;
284 case BUFFER_EMPTY:
285 THREAD_OFF(zclient->t_write);
286 break;
287 case BUFFER_PENDING:
288 THREAD_WRITE_ON(master, zclient->t_write,
289 zclient_flush_data, zclient, zclient->sock);
290 break;
291 }
292 return 0;
293}
294
pauld2110862006-01-17 17:43:18 +0000295void
paulc1b98002006-01-16 01:54:02 +0000296zclient_create_header (struct stream *s, uint16_t command)
297{
298 /* length placeholder, caller can update */
299 stream_putw (s, ZEBRA_HEADER_SIZE);
300 stream_putc (s, ZEBRA_HEADER_MARKER);
301 stream_putc (s, ZSERV_VERSION);
302 stream_putw (s, command);
303}
304
ajs634f9ea2005-04-11 15:51:40 +0000305/* Send simple Zebra message. */
306static int
paul718e3742002-12-13 20:15:29 +0000307zebra_message_send (struct zclient *zclient, int command)
308{
309 struct stream *s;
310
311 /* Get zclient output buffer. */
312 s = zclient->obuf;
313 stream_reset (s);
314
315 /* Send very simple command only Zebra message. */
paulc1b98002006-01-16 01:54:02 +0000316 zclient_create_header (s, command);
317
ajs634f9ea2005-04-11 15:51:40 +0000318 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000319}
320
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400321static int
322zebra_hello_send (struct zclient *zclient)
323{
324 struct stream *s;
325
326 if (zclient->redist_default)
327 {
328 s = zclient->obuf;
329 stream_reset (s);
330
331 zclient_create_header (s, ZEBRA_HELLO);
332 stream_putc (s, zclient->redist_default);
333 stream_putw_at (s, 0, stream_get_endp (s));
334 return zclient_send_message(zclient);
335 }
336
337 return 0;
338}
339
paul718e3742002-12-13 20:15:29 +0000340/* Make connection to zebra daemon. */
341int
342zclient_start (struct zclient *zclient)
343{
344 int i;
345
346 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000347 zlog_debug ("zclient_start is called");
paul718e3742002-12-13 20:15:29 +0000348
349 /* zclient is disabled. */
350 if (! zclient->enable)
351 return 0;
352
353 /* If already connected to the zebra. */
354 if (zclient->sock >= 0)
355 return 0;
356
357 /* Check connect thread. */
358 if (zclient->t_connect)
359 return 0;
360
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400361 if (zclient_socket_connect(zclient) < 0)
paul718e3742002-12-13 20:15:29 +0000362 {
363 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000364 zlog_debug ("zclient connection fail");
paul718e3742002-12-13 20:15:29 +0000365 zclient->fail++;
366 zclient_event (ZCLIENT_CONNECT, zclient);
367 return -1;
368 }
369
ajs634f9ea2005-04-11 15:51:40 +0000370 if (set_nonblocking(zclient->sock) < 0)
371 zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
372
paul718e3742002-12-13 20:15:29 +0000373 /* Clear fail count. */
374 zclient->fail = 0;
375 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000376 zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
paul718e3742002-12-13 20:15:29 +0000377
378 /* Create read thread. */
379 zclient_event (ZCLIENT_READ, zclient);
380
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400381 zebra_hello_send (zclient);
382
hasso18a6dce2004-10-03 18:18:34 +0000383 /* We need router-id information. */
384 zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD);
385
Dmitry Tejblum08a7a912010-10-18 19:05:39 +0400386 /* We need interface information. */
387 zebra_message_send (zclient, ZEBRA_INTERFACE_ADD);
388
paul718e3742002-12-13 20:15:29 +0000389 /* Flush all redistribute request. */
390 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
391 if (i != zclient->redist_default && zclient->redist[i])
ajs634f9ea2005-04-11 15:51:40 +0000392 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, i);
paul718e3742002-12-13 20:15:29 +0000393
394 /* If default information is needed. */
395 if (zclient->default_information)
396 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD);
397
398 return 0;
399}
400
401/* This function is a wrapper function for calling zclient_start from
402 timer or event thread. */
ajs634f9ea2005-04-11 15:51:40 +0000403static int
paul718e3742002-12-13 20:15:29 +0000404zclient_connect (struct thread *t)
405{
406 struct zclient *zclient;
407
408 zclient = THREAD_ARG (t);
409 zclient->t_connect = NULL;
410
411 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000412 zlog_debug ("zclient_connect is called");
paul718e3742002-12-13 20:15:29 +0000413
414 return zclient_start (zclient);
415}
416
paul0a589352004-05-08 11:48:26 +0000417 /*
418 * "xdr_encode"-like interface that allows daemon (client) to send
419 * a message to zebra server for a route that needs to be
420 * added/deleted to the kernel. Info about the route is specified
421 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
422 * the info down the zclient socket using the stream_* functions.
423 *
424 * The corresponding read ("xdr_decode") function on the server
425 * side is zread_ipv4_add()/zread_ipv4_delete().
426 *
427 * 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
428 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
429 * | Length (2) | Command | Route Type |
430 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
431 * | ZEBRA Flags | Message Flags | Prefix length |
432 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
433 * | Destination IPv4 Prefix for route |
434 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
435 * | Nexthop count |
436 * +-+-+-+-+-+-+-+-+
437 *
438 *
439 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
440 * described, as per the Nexthop count. Each nexthop described as:
441 *
442 * +-+-+-+-+-+-+-+-+
443 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
444 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
445 * | IPv4 Nexthop address or Interface Index number |
446 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
447 *
448 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
449 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
450 * nexthop information is provided, and the message describes a prefix
451 * to blackhole or reject route.
452 *
453 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
454 * byte value.
455 *
456 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
457 * byte value.
458 *
459 * XXX: No attention paid to alignment.
460 */
paul718e3742002-12-13 20:15:29 +0000461int
paul0a589352004-05-08 11:48:26 +0000462zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
463 struct zapi_ipv4 *api)
paul718e3742002-12-13 20:15:29 +0000464{
465 int i;
466 int psize;
467 struct stream *s;
468
469 /* Reset stream. */
470 s = zclient->obuf;
471 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000472
473 zclient_create_header (s, cmd);
474
475 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000476 stream_putc (s, api->type);
477 stream_putc (s, api->flags);
478 stream_putc (s, api->message);
G.Balaji5a616c02011-11-26 21:58:42 +0400479 stream_putw (s, api->safi);
paul0a589352004-05-08 11:48:26 +0000480
paul718e3742002-12-13 20:15:29 +0000481 /* Put prefix information. */
482 psize = PSIZE (p->prefixlen);
483 stream_putc (s, p->prefixlen);
paul0a589352004-05-08 11:48:26 +0000484 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000485
486 /* Nexthop, ifindex, distance and metric information. */
487 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
488 {
paul595db7f2003-05-25 21:35:06 +0000489 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
490 {
491 stream_putc (s, 1);
492 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
paul0a589352004-05-08 11:48:26 +0000493 /* XXX assert(api->nexthop_num == 0); */
494 /* XXX assert(api->ifindex_num == 0); */
paul595db7f2003-05-25 21:35:06 +0000495 }
496 else
497 stream_putc (s, api->nexthop_num + api->ifindex_num);
paul718e3742002-12-13 20:15:29 +0000498
499 for (i = 0; i < api->nexthop_num; i++)
paul595db7f2003-05-25 21:35:06 +0000500 {
501 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
502 stream_put_in_addr (s, api->nexthop[i]);
503 }
paul718e3742002-12-13 20:15:29 +0000504 for (i = 0; i < api->ifindex_num; i++)
paul595db7f2003-05-25 21:35:06 +0000505 {
506 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
507 stream_putl (s, api->ifindex[i]);
508 }
paul718e3742002-12-13 20:15:29 +0000509 }
510
511 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
512 stream_putc (s, api->distance);
513 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
514 stream_putl (s, api->metric);
515
516 /* Put length at the first point of the stream. */
517 stream_putw_at (s, 0, stream_get_endp (s));
518
ajs634f9ea2005-04-11 15:51:40 +0000519 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000520}
521
522#ifdef HAVE_IPV6
523int
paul0a589352004-05-08 11:48:26 +0000524zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
paul718e3742002-12-13 20:15:29 +0000525 struct zapi_ipv6 *api)
526{
527 int i;
528 int psize;
529 struct stream *s;
530
531 /* Reset stream. */
532 s = zclient->obuf;
533 stream_reset (s);
534
paulc1b98002006-01-16 01:54:02 +0000535 zclient_create_header (s, cmd);
paul718e3742002-12-13 20:15:29 +0000536
paulc1b98002006-01-16 01:54:02 +0000537 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000538 stream_putc (s, api->type);
539 stream_putc (s, api->flags);
540 stream_putc (s, api->message);
541
542 /* Put prefix information. */
543 psize = PSIZE (p->prefixlen);
544 stream_putc (s, p->prefixlen);
545 stream_write (s, (u_char *)&p->prefix, psize);
546
547 /* Nexthop, ifindex, distance and metric information. */
548 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
549 {
550 stream_putc (s, api->nexthop_num + api->ifindex_num);
551
552 for (i = 0; i < api->nexthop_num; i++)
553 {
554 stream_putc (s, ZEBRA_NEXTHOP_IPV6);
555 stream_write (s, (u_char *)api->nexthop[i], 16);
556 }
557 for (i = 0; i < api->ifindex_num; i++)
558 {
559 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
560 stream_putl (s, api->ifindex[i]);
561 }
562 }
563
564 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
565 stream_putc (s, api->distance);
566 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
567 stream_putl (s, api->metric);
568
569 /* Put length at the first point of the stream. */
570 stream_putw_at (s, 0, stream_get_endp (s));
571
ajs634f9ea2005-04-11 15:51:40 +0000572 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000573}
paul718e3742002-12-13 20:15:29 +0000574#endif /* HAVE_IPV6 */
575
paul0a589352004-05-08 11:48:26 +0000576/*
577 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
578 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
579 * then set/unset redist[type] in the client handle (a struct zserv) for the
580 * sending client
581 */
paul718e3742002-12-13 20:15:29 +0000582int
ajs634f9ea2005-04-11 15:51:40 +0000583zebra_redistribute_send (int command, struct zclient *zclient, int type)
paul718e3742002-12-13 20:15:29 +0000584{
paul718e3742002-12-13 20:15:29 +0000585 struct stream *s;
586
ajs634f9ea2005-04-11 15:51:40 +0000587 s = zclient->obuf;
588 stream_reset(s);
paul718e3742002-12-13 20:15:29 +0000589
paulc1b98002006-01-16 01:54:02 +0000590 zclient_create_header (s, command);
paul718e3742002-12-13 20:15:29 +0000591 stream_putc (s, type);
paulc1b98002006-01-16 01:54:02 +0000592
593 stream_putw_at (s, 0, stream_get_endp (s));
594
ajs634f9ea2005-04-11 15:51:40 +0000595 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000596}
597
hasso18a6dce2004-10-03 18:18:34 +0000598/* Router-id update from zebra daemon. */
599void
600zebra_router_id_update_read (struct stream *s, struct prefix *rid)
601{
602 int plen;
603
604 /* Fetch interface address. */
605 rid->family = stream_getc (s);
606
607 plen = prefix_blen (rid);
608 stream_get (&rid->u.prefix, s, plen);
609 rid->prefixlen = stream_getc (s);
610}
611
paul718e3742002-12-13 20:15:29 +0000612/* Interface addition from zebra daemon. */
paul0a589352004-05-08 11:48:26 +0000613/*
614 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
615 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
616 * 0 1 2 3
617 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
618 * +-+-+-+-+-+-+-+-+
619 * | type |
620 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
621 * | ifname |
622 * | |
623 * | |
624 * | |
625 * | |
626 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
627 * | ifindex |
628 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
629 * | if_flags |
paulc77d4542006-01-11 01:59:04 +0000630 * | |
paul0a589352004-05-08 11:48:26 +0000631 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
632 * | metric |
633 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
634 * | ifmtu |
635 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
636 * | ifmtu6 |
637 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
638 * | bandwidth |
639 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
640 * | sockaddr_dl |
641 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
642 */
643
paul718e3742002-12-13 20:15:29 +0000644struct interface *
645zebra_interface_add_read (struct stream *s)
646{
647 struct interface *ifp;
paul02ff83c2004-06-11 11:27:03 +0000648 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000649
650 /* Read interface name. */
651 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
652
ajsa3491982005-04-02 22:50:38 +0000653 /* Lookup/create interface by name. */
654 ifp = if_get_by_name_len (ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ));
paul718e3742002-12-13 20:15:29 +0000655
656 /* Read interface's index. */
657 ifp->ifindex = stream_getl (s);
658
659 /* Read interface's value. */
paul2e3b2e42002-12-13 21:03:13 +0000660 ifp->status = stream_getc (s);
paulc77d4542006-01-11 01:59:04 +0000661 ifp->flags = stream_getq (s);
paul718e3742002-12-13 20:15:29 +0000662 ifp->metric = stream_getl (s);
663 ifp->mtu = stream_getl (s);
paul0a589352004-05-08 11:48:26 +0000664 ifp->mtu6 = stream_getl (s);
paul718e3742002-12-13 20:15:29 +0000665 ifp->bandwidth = stream_getl (s);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000666#ifdef HAVE_STRUCT_SOCKADDR_DL
paul718e3742002-12-13 20:15:29 +0000667 stream_get (&ifp->sdl, s, sizeof (ifp->sdl));
668#else
669 ifp->hw_addr_len = stream_getl (s);
670 if (ifp->hw_addr_len)
671 stream_get (ifp->hw_addr, s, ifp->hw_addr_len);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000672#endif /* HAVE_STRUCT_SOCKADDR_DL */
paul718e3742002-12-13 20:15:29 +0000673
674 return ifp;
675}
676
paul0a589352004-05-08 11:48:26 +0000677/*
678 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
679 * from zebra server. The format of this message is the same as
680 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
681 * comments for zebra_interface_add_read), except that no sockaddr_dl
682 * is sent at the tail of the message.
683 */
paul718e3742002-12-13 20:15:29 +0000684struct interface *
685zebra_interface_state_read (struct stream *s)
686{
687 struct interface *ifp;
paul02ff83c2004-06-11 11:27:03 +0000688 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000689
690 /* Read interface name. */
691 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
692
693 /* Lookup this by interface index. */
ajsa3491982005-04-02 22:50:38 +0000694 ifp = if_lookup_by_name_len (ifname_tmp,
695 strnlen(ifname_tmp, INTERFACE_NAMSIZ));
paul718e3742002-12-13 20:15:29 +0000696
697 /* If such interface does not exist, indicate an error */
698 if (! ifp)
699 return NULL;
700
701 /* Read interface's index. */
702 ifp->ifindex = stream_getl (s);
703
704 /* Read interface's value. */
paul2e3b2e42002-12-13 21:03:13 +0000705 ifp->status = stream_getc (s);
paulc77d4542006-01-11 01:59:04 +0000706 ifp->flags = stream_getq (s);
paul718e3742002-12-13 20:15:29 +0000707 ifp->metric = stream_getl (s);
708 ifp->mtu = stream_getl (s);
paul0a589352004-05-08 11:48:26 +0000709 ifp->mtu6 = stream_getl (s);
paul718e3742002-12-13 20:15:29 +0000710 ifp->bandwidth = stream_getl (s);
711
712 return ifp;
713}
714
paul0a589352004-05-08 11:48:26 +0000715/*
716 * format of message for address additon is:
717 * 0
718 * 0 1 2 3 4 5 6 7
719 * +-+-+-+-+-+-+-+-+
720 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
721 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
722 * | |
723 * + +
724 * | ifindex |
725 * + +
726 * | |
727 * + +
728 * | |
729 * +-+-+-+-+-+-+-+-+
730 * | ifc_flags | flags for connected address
731 * +-+-+-+-+-+-+-+-+
732 * | addr_family |
733 * +-+-+-+-+-+-+-+-+
734 * | addr... |
735 * : :
736 * | |
737 * +-+-+-+-+-+-+-+-+
738 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
739 * +-+-+-+-+-+-+-+-+
740 * | daddr.. |
741 * : :
742 * | |
743 * +-+-+-+-+-+-+-+-+
744 *
745 */
746
hasso18a6dce2004-10-03 18:18:34 +0000747void
748zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
749{
750 /* Read interface's index. */
751 ifp->ifindex = stream_getl (s);
hasso508ec912004-10-23 14:26:49 +0000752 ifp->status = stream_getc (s);
hasso18a6dce2004-10-03 18:18:34 +0000753
754 /* Read interface's value. */
paulc77d4542006-01-11 01:59:04 +0000755 ifp->flags = stream_getq (s);
hasso18a6dce2004-10-03 18:18:34 +0000756 ifp->metric = stream_getl (s);
757 ifp->mtu = stream_getl (s);
hasso508ec912004-10-23 14:26:49 +0000758 ifp->mtu6 = stream_getl (s);
hasso18a6dce2004-10-03 18:18:34 +0000759 ifp->bandwidth = stream_getl (s);
760}
761
hasso3fb9cd62004-10-19 19:44:43 +0000762static int
763memconstant(const void *s, int c, size_t n)
764{
765 const u_char *p = s;
766
767 while (n-- > 0)
768 if (*p++ != c)
769 return 0;
770 return 1;
771}
772
paul718e3742002-12-13 20:15:29 +0000773struct connected *
paul0a589352004-05-08 11:48:26 +0000774zebra_interface_address_read (int type, struct stream *s)
paul718e3742002-12-13 20:15:29 +0000775{
776 unsigned int ifindex;
777 struct interface *ifp;
778 struct connected *ifc;
paul0a589352004-05-08 11:48:26 +0000779 struct prefix p, d;
paul718e3742002-12-13 20:15:29 +0000780 int family;
781 int plen;
paul0a589352004-05-08 11:48:26 +0000782 u_char ifc_flags;
783
784 memset (&p, 0, sizeof(p));
785 memset (&d, 0, sizeof(d));
paul718e3742002-12-13 20:15:29 +0000786
787 /* Get interface index. */
788 ifindex = stream_getl (s);
789
790 /* Lookup index. */
791 ifp = if_lookup_by_index (ifindex);
792 if (ifp == NULL)
793 {
paul0a589352004-05-08 11:48:26 +0000794 zlog_warn ("zebra_interface_address_read(%s): "
795 "Can't find interface by ifindex: %d ",
796 (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"),
797 ifindex);
paul718e3742002-12-13 20:15:29 +0000798 return NULL;
799 }
800
801 /* Fetch flag. */
paul0a589352004-05-08 11:48:26 +0000802 ifc_flags = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000803
804 /* Fetch interface address. */
805 family = p.family = stream_getc (s);
806
paul0a589352004-05-08 11:48:26 +0000807 plen = prefix_blen (&p);
808 stream_get (&p.u.prefix, s, plen);
paul718e3742002-12-13 20:15:29 +0000809 p.prefixlen = stream_getc (s);
810
811 /* Fetch destination address. */
paul0a589352004-05-08 11:48:26 +0000812 stream_get (&d.u.prefix, s, plen);
paul718e3742002-12-13 20:15:29 +0000813 d.family = family;
814
paul0a589352004-05-08 11:48:26 +0000815 if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
816 {
hasso3fb9cd62004-10-19 19:44:43 +0000817 /* N.B. NULL destination pointers are encoded as all zeroes */
818 ifc = connected_add_by_prefix(ifp, &p,(memconstant(&d.u.prefix,0,plen) ?
819 NULL : &d));
paul0a589352004-05-08 11:48:26 +0000820 if (ifc != NULL)
Andrew J. Schorre4529632006-12-12 19:18:21 +0000821 {
822 ifc->flags = ifc_flags;
823 if (ifc->destination)
824 ifc->destination->prefixlen = ifc->address->prefixlen;
825 }
paul0a589352004-05-08 11:48:26 +0000826 }
827 else
828 {
829 assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
830 ifc = connected_delete_by_prefix(ifp, &p);
831 }
paul718e3742002-12-13 20:15:29 +0000832
833 return ifc;
834}
paul0a589352004-05-08 11:48:26 +0000835
paul718e3742002-12-13 20:15:29 +0000836
837/* Zebra client message read function. */
ajs634f9ea2005-04-11 15:51:40 +0000838static int
paul718e3742002-12-13 20:15:29 +0000839zclient_read (struct thread *thread)
840{
ajs634f9ea2005-04-11 15:51:40 +0000841 size_t already;
paulc1b98002006-01-16 01:54:02 +0000842 uint16_t length, command;
843 uint8_t marker, version;
paul718e3742002-12-13 20:15:29 +0000844 struct zclient *zclient;
845
846 /* Get socket to zebra. */
paul718e3742002-12-13 20:15:29 +0000847 zclient = THREAD_ARG (thread);
848 zclient->t_read = NULL;
849
ajs634f9ea2005-04-11 15:51:40 +0000850 /* Read zebra header (if we don't have it already). */
851 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +0000852 {
ajs634f9ea2005-04-11 15:51:40 +0000853 ssize_t nbyte;
854 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
855 ZEBRA_HEADER_SIZE-already)) == 0) ||
856 (nbyte == -1))
857 {
858 if (zclient_debug)
859 zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
860 return zclient_failed(zclient);
861 }
862 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
863 {
864 /* Try again later. */
865 zclient_event (ZCLIENT_READ, zclient);
866 return 0;
867 }
868 already = ZEBRA_HEADER_SIZE;
paul718e3742002-12-13 20:15:29 +0000869 }
870
ajs634f9ea2005-04-11 15:51:40 +0000871 /* Reset to read from the beginning of the incoming packet. */
872 stream_set_getp(zclient->ibuf, 0);
paul718e3742002-12-13 20:15:29 +0000873
paulc1b98002006-01-16 01:54:02 +0000874 /* Fetch header values. */
paul718e3742002-12-13 20:15:29 +0000875 length = stream_getw (zclient->ibuf);
paulc1b98002006-01-16 01:54:02 +0000876 marker = stream_getc (zclient->ibuf);
877 version = stream_getc (zclient->ibuf);
878 command = stream_getw (zclient->ibuf);
879
880 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
881 {
882 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
883 __func__, zclient->sock, marker, version);
884 return zclient_failed(zclient);
885 }
886
ajs634f9ea2005-04-11 15:51:40 +0000887 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +0000888 {
ajs634f9ea2005-04-11 15:51:40 +0000889 zlog_err("%s: socket %d message length %u is less than %d ",
890 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
891 return zclient_failed(zclient);
paul718e3742002-12-13 20:15:29 +0000892 }
ajs634f9ea2005-04-11 15:51:40 +0000893
894 /* Length check. */
895 if (length > STREAM_SIZE(zclient->ibuf))
896 {
897 struct stream *ns;
898 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
899 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
900 ns = stream_new(length);
901 stream_copy(ns, zclient->ibuf);
902 stream_free (zclient->ibuf);
903 zclient->ibuf = ns;
904 }
paul718e3742002-12-13 20:15:29 +0000905
906 /* Read rest of zebra packet. */
ajs634f9ea2005-04-11 15:51:40 +0000907 if (already < length)
908 {
909 ssize_t nbyte;
910 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
911 length-already)) == 0) ||
912 (nbyte == -1))
913 {
914 if (zclient_debug)
915 zlog_debug("zclient connection closed socket [%d].", zclient->sock);
916 return zclient_failed(zclient);
917 }
918 if (nbyte != (ssize_t)(length-already))
919 {
920 /* Try again later. */
921 zclient_event (ZCLIENT_READ, zclient);
922 return 0;
923 }
924 }
925
926 length -= ZEBRA_HEADER_SIZE;
paul718e3742002-12-13 20:15:29 +0000927
paul0a589352004-05-08 11:48:26 +0000928 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000929 zlog_debug("zclient 0x%p command 0x%x \n", zclient, command);
paul0a589352004-05-08 11:48:26 +0000930
paul718e3742002-12-13 20:15:29 +0000931 switch (command)
932 {
hasso18a6dce2004-10-03 18:18:34 +0000933 case ZEBRA_ROUTER_ID_UPDATE:
934 if (zclient->router_id_update)
Stephen Hemminger9206f9e2011-12-18 19:43:40 +0400935 (*zclient->router_id_update) (command, zclient, length);
hasso18a6dce2004-10-03 18:18:34 +0000936 break;
paul718e3742002-12-13 20:15:29 +0000937 case ZEBRA_INTERFACE_ADD:
938 if (zclient->interface_add)
Stephen Hemminger9206f9e2011-12-18 19:43:40 +0400939 (*zclient->interface_add) (command, zclient, length);
paul718e3742002-12-13 20:15:29 +0000940 break;
941 case ZEBRA_INTERFACE_DELETE:
942 if (zclient->interface_delete)
Stephen Hemminger9206f9e2011-12-18 19:43:40 +0400943 (*zclient->interface_delete) (command, zclient, length);
paul718e3742002-12-13 20:15:29 +0000944 break;
945 case ZEBRA_INTERFACE_ADDRESS_ADD:
946 if (zclient->interface_address_add)
Stephen Hemminger9206f9e2011-12-18 19:43:40 +0400947 (*zclient->interface_address_add) (command, zclient, length);
paul718e3742002-12-13 20:15:29 +0000948 break;
949 case ZEBRA_INTERFACE_ADDRESS_DELETE:
950 if (zclient->interface_address_delete)
Stephen Hemminger9206f9e2011-12-18 19:43:40 +0400951 (*zclient->interface_address_delete) (command, zclient, length);
paul718e3742002-12-13 20:15:29 +0000952 break;
953 case ZEBRA_INTERFACE_UP:
954 if (zclient->interface_up)
Stephen Hemminger9206f9e2011-12-18 19:43:40 +0400955 (*zclient->interface_up) (command, zclient, length);
paul718e3742002-12-13 20:15:29 +0000956 break;
957 case ZEBRA_INTERFACE_DOWN:
958 if (zclient->interface_down)
Stephen Hemminger9206f9e2011-12-18 19:43:40 +0400959 (*zclient->interface_down) (command, zclient, length);
paul718e3742002-12-13 20:15:29 +0000960 break;
961 case ZEBRA_IPV4_ROUTE_ADD:
962 if (zclient->ipv4_route_add)
Stephen Hemminger9206f9e2011-12-18 19:43:40 +0400963 (*zclient->ipv4_route_add) (command, zclient, length);
paul718e3742002-12-13 20:15:29 +0000964 break;
965 case ZEBRA_IPV4_ROUTE_DELETE:
966 if (zclient->ipv4_route_delete)
Stephen Hemminger9206f9e2011-12-18 19:43:40 +0400967 (*zclient->ipv4_route_delete) (command, zclient, length);
paul718e3742002-12-13 20:15:29 +0000968 break;
969 case ZEBRA_IPV6_ROUTE_ADD:
970 if (zclient->ipv6_route_add)
Stephen Hemminger9206f9e2011-12-18 19:43:40 +0400971 (*zclient->ipv6_route_add) (command, zclient, length);
paul718e3742002-12-13 20:15:29 +0000972 break;
973 case ZEBRA_IPV6_ROUTE_DELETE:
974 if (zclient->ipv6_route_delete)
Stephen Hemminger9206f9e2011-12-18 19:43:40 +0400975 (*zclient->ipv6_route_delete) (command, zclient, length);
paul718e3742002-12-13 20:15:29 +0000976 break;
977 default:
978 break;
979 }
980
ajs634f9ea2005-04-11 15:51:40 +0000981 if (zclient->sock < 0)
982 /* Connection was closed during packet processing. */
983 return -1;
984
paul718e3742002-12-13 20:15:29 +0000985 /* Register read thread. */
ajs634f9ea2005-04-11 15:51:40 +0000986 stream_reset(zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000987 zclient_event (ZCLIENT_READ, zclient);
988
989 return 0;
990}
991
992void
paul0a589352004-05-08 11:48:26 +0000993zclient_redistribute (int command, struct zclient *zclient, int type)
paul718e3742002-12-13 20:15:29 +0000994{
paul718e3742002-12-13 20:15:29 +0000995
paul0a589352004-05-08 11:48:26 +0000996 if (command == ZEBRA_REDISTRIBUTE_ADD)
997 {
998 if (zclient->redist[type])
999 return;
1000 zclient->redist[type] = 1;
1001 }
1002 else
1003 {
1004 if (!zclient->redist[type])
1005 return;
1006 zclient->redist[type] = 0;
1007 }
paul718e3742002-12-13 20:15:29 +00001008
1009 if (zclient->sock > 0)
ajs634f9ea2005-04-11 15:51:40 +00001010 zebra_redistribute_send (command, zclient, type);
paul718e3742002-12-13 20:15:29 +00001011}
1012
paul0a589352004-05-08 11:48:26 +00001013
paul718e3742002-12-13 20:15:29 +00001014void
paul0a589352004-05-08 11:48:26 +00001015zclient_redistribute_default (int command, struct zclient *zclient)
paul718e3742002-12-13 20:15:29 +00001016{
paul718e3742002-12-13 20:15:29 +00001017
paul0a589352004-05-08 11:48:26 +00001018 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
1019 {
1020 if (zclient->default_information)
1021 return;
1022 zclient->default_information = 1;
1023 }
1024 else
1025 {
1026 if (!zclient->default_information)
1027 return;
1028 zclient->default_information = 0;
1029 }
paul718e3742002-12-13 20:15:29 +00001030
1031 if (zclient->sock > 0)
paul0a589352004-05-08 11:48:26 +00001032 zebra_message_send (zclient, command);
paul718e3742002-12-13 20:15:29 +00001033}
1034
paul718e3742002-12-13 20:15:29 +00001035static void
1036zclient_event (enum event event, struct zclient *zclient)
1037{
1038 switch (event)
1039 {
1040 case ZCLIENT_SCHEDULE:
1041 if (! zclient->t_connect)
1042 zclient->t_connect =
1043 thread_add_event (master, zclient_connect, zclient, 0);
1044 break;
1045 case ZCLIENT_CONNECT:
1046 if (zclient->fail >= 10)
1047 return;
1048 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +00001049 zlog_debug ("zclient connect schedule interval is %d",
paul718e3742002-12-13 20:15:29 +00001050 zclient->fail < 3 ? 10 : 60);
1051 if (! zclient->t_connect)
1052 zclient->t_connect =
1053 thread_add_timer (master, zclient_connect, zclient,
1054 zclient->fail < 3 ? 10 : 60);
1055 break;
1056 case ZCLIENT_READ:
1057 zclient->t_read =
1058 thread_add_read (master, zclient_read, zclient, zclient->sock);
1059 break;
1060 }
1061}
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001062
1063void
1064zclient_serv_path_set (char *path)
1065{
1066 struct stat sb;
1067
1068 /* reset */
1069 zclient_serv_path = NULL;
1070
1071 /* test if `path' is socket. don't set it otherwise. */
1072 if (stat(path, &sb) == -1)
1073 {
1074 zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
1075 return;
1076 }
1077
1078 if ((sb.st_mode & S_IFMT) != S_IFSOCK)
1079 {
1080 zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
1081 return;
1082 }
1083
1084 /* it seems that path is unix socket */
1085 zclient_serv_path = path;
1086}
1087