1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
5 * This file is part of GNU Zebra.
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.
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.
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,
36 /* Zebra client events. */
37 enum event
{ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
39 /* Prototype for event manager. */
40 static void zclient_event (enum event
, struct zclient
*);
42 extern struct thread_master
*master
;
44 /* This file local debug flag. */
45 int zclient_debug
= 0;
47 /* Allocate zclient structure. */
51 struct zclient
*zclient
;
52 zclient
= XMALLOC (MTYPE_ZCLIENT
, sizeof (struct zclient
));
53 memset (zclient
, 0, sizeof (struct zclient
));
55 zclient
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
56 zclient
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
57 zclient
->wb
= buffer_new(0);
63 /* This function is never used. And it must not be used, because
64 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.
68 /* Free zclient structure. */
70 zclient_free (struct zclient
*zclient
)
73 stream_free(zclient
->ibuf
);
75 stream_free(zclient
->obuf
);
77 buffer_free(zclient
->wb
);
79 XFREE (MTYPE_ZCLIENT
, zclient
);
83 /* Initialize zebra client. Argument redist_default is unwanted
84 redistribute route type. */
86 zclient_init (struct zclient
*zclient
, int redist_default
)
90 /* Enable zebra client connection by default. */
93 /* Set -1 to the default socket value. */
96 /* Clear redistribution flags. */
97 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
98 zclient
->redist
[i
] = 0;
100 /* Set unwanted redistribute route. bgpd does not need BGP route
102 zclient
->redist_default
= redist_default
;
103 zclient
->redist
[redist_default
] = 1;
105 /* Set default-information redistribute to zero. */
106 zclient
->default_information
= 0;
108 /* Schedule first zclient connection. */
110 zlog_debug ("zclient start scheduled");
112 zclient_event (ZCLIENT_SCHEDULE
, zclient
);
115 /* Stop zebra client services. */
117 zclient_stop (struct zclient
*zclient
)
120 zlog_debug ("zclient stopped");
123 THREAD_OFF(zclient
->t_read
);
124 THREAD_OFF(zclient
->t_connect
);
125 THREAD_OFF(zclient
->t_write
);
128 stream_reset(zclient
->ibuf
);
129 stream_reset(zclient
->obuf
);
131 /* Empty the write buffer. */
132 buffer_reset(zclient
->wb
);
135 if (zclient
->sock
>= 0)
137 close (zclient
->sock
);
144 zclient_reset (struct zclient
*zclient
)
146 zclient_stop (zclient
);
147 zclient_init (zclient
, zclient
->redist_default
);
150 /* Make socket to zebra daemon. Return zebra socket. */
156 struct sockaddr_in serv
;
158 /* We should think about IPv6 connection. */
159 sock
= socket (AF_INET
, SOCK_STREAM
, 0);
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
);
168 serv
.sin_len
= sizeof (struct sockaddr_in
);
169 #endif /* HAVE_SIN_LEN */
170 serv
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
172 /* Connect to zebra. */
173 ret
= connect (sock
, (struct sockaddr
*) &serv
, sizeof (serv
));
182 /* For sockaddr_un. */
186 zclient_socket_un (const char *path
)
190 struct sockaddr_un addr
;
192 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
196 /* Make server socket. */
197 memset (&addr
, 0, sizeof (struct sockaddr_un
));
198 addr
.sun_family
= AF_UNIX
;
199 strncpy (addr
.sun_path
, path
, strlen (path
));
201 len
= addr
.sun_len
= SUN_LEN(&addr
);
203 len
= sizeof (addr
.sun_family
) + strlen (addr
.sun_path
);
204 #endif /* HAVE_SUN_LEN */
206 ret
= connect (sock
, (struct sockaddr
*) &addr
, len
);
216 zclient_failed(struct zclient
*zclient
)
219 zclient_stop(zclient
);
220 zclient_event(ZCLIENT_CONNECT
, zclient
);
225 zclient_flush_data(struct thread
*thread
)
227 struct zclient
*zclient
= THREAD_ARG(thread
);
229 zclient
->t_write
= NULL
;
230 if (zclient
->sock
< 0)
232 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
))
235 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
236 __func__
, zclient
->sock
);
237 return zclient_failed(zclient
);
240 zclient
->t_write
= thread_add_write(master
, zclient_flush_data
,
241 zclient
, zclient
->sock
);
250 zclient_send_message(struct zclient
*zclient
)
252 if (zclient
->sock
< 0)
254 switch (buffer_write(zclient
->wb
, zclient
->sock
, STREAM_DATA(zclient
->obuf
),
255 stream_get_endp(zclient
->obuf
)))
258 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
259 __func__
, zclient
->sock
);
260 return zclient_failed(zclient
);
263 THREAD_OFF(zclient
->t_write
);
266 THREAD_WRITE_ON(master
, zclient
->t_write
,
267 zclient_flush_data
, zclient
, zclient
->sock
);
273 /* Send simple Zebra message. */
275 zebra_message_send (struct zclient
*zclient
, int command
)
279 /* Get zclient output buffer. */
283 /* Send very simple command only Zebra message. */
285 stream_putc (s
, command
);
287 return zclient_send_message(zclient
);
290 /* Make connection to zebra daemon. */
292 zclient_start (struct zclient
*zclient
)
297 zlog_debug ("zclient_start is called");
299 /* zclient is disabled. */
300 if (! zclient
->enable
)
303 /* If already connected to the zebra. */
304 if (zclient
->sock
>= 0)
307 /* Check connect thread. */
308 if (zclient
->t_connect
)
312 #ifdef HAVE_TCP_ZEBRA
313 zclient
->sock
= zclient_socket ();
315 zclient
->sock
= zclient_socket_un (ZEBRA_SERV_PATH
);
316 #endif /* HAVE_TCP_ZEBRA */
317 if (zclient
->sock
< 0)
320 zlog_debug ("zclient connection fail");
322 zclient_event (ZCLIENT_CONNECT
, zclient
);
326 if (set_nonblocking(zclient
->sock
) < 0)
327 zlog_warn("%s: set_nonblocking(%d) failed", __func__
, zclient
->sock
);
329 /* Clear fail count. */
332 zlog_debug ("zclient connect success with socket [%d]", zclient
->sock
);
334 /* Create read thread. */
335 zclient_event (ZCLIENT_READ
, zclient
);
337 /* We need interface information. */
338 zebra_message_send (zclient
, ZEBRA_INTERFACE_ADD
);
340 /* We need router-id information. */
341 zebra_message_send (zclient
, ZEBRA_ROUTER_ID_ADD
);
343 /* Flush all redistribute request. */
344 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
345 if (i
!= zclient
->redist_default
&& zclient
->redist
[i
])
346 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, i
);
348 /* If default information is needed. */
349 if (zclient
->default_information
)
350 zebra_message_send (zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
);
355 /* This function is a wrapper function for calling zclient_start from
356 timer or event thread. */
358 zclient_connect (struct thread
*t
)
360 struct zclient
*zclient
;
362 zclient
= THREAD_ARG (t
);
363 zclient
->t_connect
= NULL
;
366 zlog_debug ("zclient_connect is called");
368 return zclient_start (zclient
);
372 * "xdr_encode"-like interface that allows daemon (client) to send
373 * a message to zebra server for a route that needs to be
374 * added/deleted to the kernel. Info about the route is specified
375 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
376 * the info down the zclient socket using the stream_* functions.
378 * The corresponding read ("xdr_decode") function on the server
379 * side is zread_ipv4_add()/zread_ipv4_delete().
381 * 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
382 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
383 * | Length (2) | Command | Route Type |
384 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
385 * | ZEBRA Flags | Message Flags | Prefix length |
386 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
387 * | Destination IPv4 Prefix for route |
388 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
393 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
394 * described, as per the Nexthop count. Each nexthop described as:
397 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
398 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
399 * | IPv4 Nexthop address or Interface Index number |
400 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
402 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
403 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
404 * nexthop information is provided, and the message describes a prefix
405 * to blackhole or reject route.
407 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
410 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
413 * XXX: No attention paid to alignment.
416 zapi_ipv4_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
417 struct zapi_ipv4
*api
)
427 /* Length place holder. */
430 /* Put command, type and nexthop. */
431 stream_putc (s
, cmd
);
432 stream_putc (s
, api
->type
);
433 stream_putc (s
, api
->flags
);
434 stream_putc (s
, api
->message
);
436 /* Put prefix information. */
437 psize
= PSIZE (p
->prefixlen
);
438 stream_putc (s
, p
->prefixlen
);
439 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
441 /* Nexthop, ifindex, distance and metric information. */
442 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
444 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
447 stream_putc (s
, ZEBRA_NEXTHOP_BLACKHOLE
);
448 /* XXX assert(api->nexthop_num == 0); */
449 /* XXX assert(api->ifindex_num == 0); */
452 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
454 for (i
= 0; i
< api
->nexthop_num
; i
++)
456 stream_putc (s
, ZEBRA_NEXTHOP_IPV4
);
457 stream_put_in_addr (s
, api
->nexthop
[i
]);
459 for (i
= 0; i
< api
->ifindex_num
; i
++)
461 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
462 stream_putl (s
, api
->ifindex
[i
]);
466 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
467 stream_putc (s
, api
->distance
);
468 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
469 stream_putl (s
, api
->metric
);
471 /* Put length at the first point of the stream. */
472 stream_putw_at (s
, 0, stream_get_endp (s
));
474 return zclient_send_message(zclient
);
479 zapi_ipv6_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
480 struct zapi_ipv6
*api
)
490 /* Length place holder. */
493 /* Put command, type and nexthop. */
494 stream_putc (s
, cmd
);
495 stream_putc (s
, api
->type
);
496 stream_putc (s
, api
->flags
);
497 stream_putc (s
, api
->message
);
499 /* Put prefix information. */
500 psize
= PSIZE (p
->prefixlen
);
501 stream_putc (s
, p
->prefixlen
);
502 stream_write (s
, (u_char
*)&p
->prefix
, psize
);
504 /* Nexthop, ifindex, distance and metric information. */
505 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
507 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
509 for (i
= 0; i
< api
->nexthop_num
; i
++)
511 stream_putc (s
, ZEBRA_NEXTHOP_IPV6
);
512 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
514 for (i
= 0; i
< api
->ifindex_num
; i
++)
516 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
517 stream_putl (s
, api
->ifindex
[i
]);
521 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
522 stream_putc (s
, api
->distance
);
523 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
524 stream_putl (s
, api
->metric
);
526 /* Put length at the first point of the stream. */
527 stream_putw_at (s
, 0, stream_get_endp (s
));
529 return zclient_send_message(zclient
);
531 #endif /* HAVE_IPV6 */
534 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
535 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
536 * then set/unset redist[type] in the client handle (a struct zserv) for the
540 zebra_redistribute_send (int command
, struct zclient
*zclient
, int type
)
547 /* Total length of the message. */
550 stream_putc (s
, command
);
551 stream_putc (s
, type
);
553 return zclient_send_message(zclient
);
556 /* Router-id update from zebra daemon. */
558 zebra_router_id_update_read (struct stream
*s
, struct prefix
*rid
)
562 /* Fetch interface address. */
563 rid
->family
= stream_getc (s
);
565 plen
= prefix_blen (rid
);
566 stream_get (&rid
->u
.prefix
, s
, plen
);
567 rid
->prefixlen
= stream_getc (s
);
570 /* Interface addition from zebra daemon. */
572 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
573 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
575 * 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
578 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
584 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
586 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
590 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
592 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
594 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
596 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
598 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
602 zebra_interface_add_read (struct stream
*s
)
604 struct interface
*ifp
;
605 char ifname_tmp
[INTERFACE_NAMSIZ
];
607 /* Read interface name. */
608 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
610 /* Lookup/create interface by name. */
611 ifp
= if_get_by_name_len (ifname_tmp
, strnlen(ifname_tmp
, INTERFACE_NAMSIZ
));
613 /* Read interface's index. */
614 ifp
->ifindex
= stream_getl (s
);
616 /* Read interface's value. */
617 ifp
->status
= stream_getc (s
);
618 ifp
->flags
= stream_getl (s
);
619 ifp
->metric
= stream_getl (s
);
620 ifp
->mtu
= stream_getl (s
);
621 ifp
->mtu6
= stream_getl (s
);
622 ifp
->bandwidth
= stream_getl (s
);
623 #ifdef HAVE_SOCKADDR_DL
624 stream_get (&ifp
->sdl
, s
, sizeof (ifp
->sdl
));
626 ifp
->hw_addr_len
= stream_getl (s
);
627 if (ifp
->hw_addr_len
)
628 stream_get (ifp
->hw_addr
, s
, ifp
->hw_addr_len
);
629 #endif /* HAVE_SOCKADDR_DL */
635 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
636 * from zebra server. The format of this message is the same as
637 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
638 * comments for zebra_interface_add_read), except that no sockaddr_dl
639 * is sent at the tail of the message.
642 zebra_interface_state_read (struct stream
*s
)
644 struct interface
*ifp
;
645 char ifname_tmp
[INTERFACE_NAMSIZ
];
647 /* Read interface name. */
648 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
650 /* Lookup this by interface index. */
651 ifp
= if_lookup_by_name_len (ifname_tmp
,
652 strnlen(ifname_tmp
, INTERFACE_NAMSIZ
));
654 /* If such interface does not exist, indicate an error */
658 /* Read interface's index. */
659 ifp
->ifindex
= stream_getl (s
);
661 /* Read interface's value. */
662 ifp
->status
= stream_getc (s
);
663 ifp
->flags
= stream_getl (s
);
664 ifp
->metric
= stream_getl (s
);
665 ifp
->mtu
= stream_getl (s
);
666 ifp
->mtu6
= stream_getl (s
);
667 ifp
->bandwidth
= stream_getl (s
);
673 * format of message for address additon is:
677 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
678 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
687 * | ifc_flags | flags for connected address
695 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
705 zebra_interface_if_set_value (struct stream
*s
, struct interface
*ifp
)
707 /* Read interface's index. */
708 ifp
->ifindex
= stream_getl (s
);
709 ifp
->status
= stream_getc (s
);
711 /* Read interface's value. */
712 ifp
->flags
= stream_getl (s
);
713 ifp
->metric
= stream_getl (s
);
714 ifp
->mtu
= stream_getl (s
);
715 ifp
->mtu6
= stream_getl (s
);
716 ifp
->bandwidth
= stream_getl (s
);
720 memconstant(const void *s
, int c
, size_t n
)
731 zebra_interface_address_read (int type
, struct stream
*s
)
733 unsigned int ifindex
;
734 struct interface
*ifp
;
735 struct connected
*ifc
;
741 memset (&p
, 0, sizeof(p
));
742 memset (&d
, 0, sizeof(d
));
744 /* Get interface index. */
745 ifindex
= stream_getl (s
);
748 ifp
= if_lookup_by_index (ifindex
);
751 zlog_warn ("zebra_interface_address_read(%s): "
752 "Can't find interface by ifindex: %d ",
753 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
? "ADD" : "DELETE"),
759 ifc_flags
= stream_getc (s
);
761 /* Fetch interface address. */
762 family
= p
.family
= stream_getc (s
);
764 plen
= prefix_blen (&p
);
765 stream_get (&p
.u
.prefix
, s
, plen
);
766 p
.prefixlen
= stream_getc (s
);
768 /* Fetch destination address. */
769 stream_get (&d
.u
.prefix
, s
, plen
);
772 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
)
774 /* N.B. NULL destination pointers are encoded as all zeroes */
775 ifc
= connected_add_by_prefix(ifp
, &p
,(memconstant(&d
.u
.prefix
,0,plen
) ?
778 ifc
->flags
= ifc_flags
;
782 assert (type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
783 ifc
= connected_delete_by_prefix(ifp
, &p
);
790 /* Zebra client message read function. */
792 zclient_read (struct thread
*thread
)
797 zebra_command_t command
;
798 struct zclient
*zclient
;
800 /* Get socket to zebra. */
801 zclient
= THREAD_ARG (thread
);
802 zclient
->t_read
= NULL
;
804 /* Read zebra header (if we don't have it already). */
805 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
)
808 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
809 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
813 zlog_debug ("zclient connection closed socket [%d].", zclient
->sock
);
814 return zclient_failed(zclient
);
816 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
818 /* Try again later. */
819 zclient_event (ZCLIENT_READ
, zclient
);
822 already
= ZEBRA_HEADER_SIZE
;
825 /* Reset to read from the beginning of the incoming packet. */
826 stream_set_getp(zclient
->ibuf
, 0);
828 /* Fetch length and command. */
829 length
= stream_getw (zclient
->ibuf
);
830 command
= stream_getc (zclient
->ibuf
);
832 if (length
< ZEBRA_HEADER_SIZE
)
834 zlog_err("%s: socket %d message length %u is less than %d ",
835 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
836 return zclient_failed(zclient
);
840 if (length
> STREAM_SIZE(zclient
->ibuf
))
843 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
844 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
845 ns
= stream_new(length
);
846 stream_copy(ns
, zclient
->ibuf
);
847 stream_free (zclient
->ibuf
);
851 /* Read rest of zebra packet. */
852 if (already
< length
)
855 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
856 length
-already
)) == 0) ||
860 zlog_debug("zclient connection closed socket [%d].", zclient
->sock
);
861 return zclient_failed(zclient
);
863 if (nbyte
!= (ssize_t
)(length
-already
))
865 /* Try again later. */
866 zclient_event (ZCLIENT_READ
, zclient
);
871 length
-= ZEBRA_HEADER_SIZE
;
874 zlog_debug("zclient 0x%p command 0x%x \n", zclient
, command
);
878 case ZEBRA_ROUTER_ID_UPDATE
:
879 if (zclient
->router_id_update
)
880 ret
= (*zclient
->router_id_update
) (command
, zclient
, length
);
882 case ZEBRA_INTERFACE_ADD
:
883 if (zclient
->interface_add
)
884 ret
= (*zclient
->interface_add
) (command
, zclient
, length
);
886 case ZEBRA_INTERFACE_DELETE
:
887 if (zclient
->interface_delete
)
888 ret
= (*zclient
->interface_delete
) (command
, zclient
, length
);
890 case ZEBRA_INTERFACE_ADDRESS_ADD
:
891 if (zclient
->interface_address_add
)
892 ret
= (*zclient
->interface_address_add
) (command
, zclient
, length
);
894 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
895 if (zclient
->interface_address_delete
)
896 ret
= (*zclient
->interface_address_delete
) (command
, zclient
, length
);
898 case ZEBRA_INTERFACE_UP
:
899 if (zclient
->interface_up
)
900 ret
= (*zclient
->interface_up
) (command
, zclient
, length
);
902 case ZEBRA_INTERFACE_DOWN
:
903 if (zclient
->interface_down
)
904 ret
= (*zclient
->interface_down
) (command
, zclient
, length
);
906 case ZEBRA_IPV4_ROUTE_ADD
:
907 if (zclient
->ipv4_route_add
)
908 ret
= (*zclient
->ipv4_route_add
) (command
, zclient
, length
);
910 case ZEBRA_IPV4_ROUTE_DELETE
:
911 if (zclient
->ipv4_route_delete
)
912 ret
= (*zclient
->ipv4_route_delete
) (command
, zclient
, length
);
914 case ZEBRA_IPV6_ROUTE_ADD
:
915 if (zclient
->ipv6_route_add
)
916 ret
= (*zclient
->ipv6_route_add
) (command
, zclient
, length
);
918 case ZEBRA_IPV6_ROUTE_DELETE
:
919 if (zclient
->ipv6_route_delete
)
920 ret
= (*zclient
->ipv6_route_delete
) (command
, zclient
, length
);
926 if (zclient
->sock
< 0)
927 /* Connection was closed during packet processing. */
930 /* Register read thread. */
931 stream_reset(zclient
->ibuf
);
932 zclient_event (ZCLIENT_READ
, zclient
);
938 zclient_redistribute (int command
, struct zclient
*zclient
, int type
)
941 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
943 if (zclient
->redist
[type
])
945 zclient
->redist
[type
] = 1;
949 if (!zclient
->redist
[type
])
951 zclient
->redist
[type
] = 0;
954 if (zclient
->sock
> 0)
955 zebra_redistribute_send (command
, zclient
, type
);
960 zclient_redistribute_default (int command
, struct zclient
*zclient
)
963 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
)
965 if (zclient
->default_information
)
967 zclient
->default_information
= 1;
971 if (!zclient
->default_information
)
973 zclient
->default_information
= 0;
976 if (zclient
->sock
> 0)
977 zebra_message_send (zclient
, command
);
981 zclient_event (enum event event
, struct zclient
*zclient
)
985 case ZCLIENT_SCHEDULE
:
986 if (! zclient
->t_connect
)
988 thread_add_event (master
, zclient_connect
, zclient
, 0);
990 case ZCLIENT_CONNECT
:
991 if (zclient
->fail
>= 10)
994 zlog_debug ("zclient connect schedule interval is %d",
995 zclient
->fail
< 3 ? 10 : 60);
996 if (! zclient
->t_connect
)
998 thread_add_timer (master
, zclient_connect
, zclient
,
999 zclient
->fail
< 3 ? 10 : 60);
1003 thread_add_read (master
, zclient_read
, zclient
, zclient
->sock
);