1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
4 *
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"
27 #include "buffer.h"
28 #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"
35
36 /* Zebra client events. */
37 enum event {ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT};
38
39 /* Prototype for event manager. */
40 static void zclient_event (enum event, struct zclient *);
41
42 const char *zclient_serv_path = NULL;
43
44 /* This file local debug flag. */
45 int zclient_debug = 0;
46
47 /* Allocate zclient structure. */
48 struct zclient *
zclient_new(struct thread_master * master)49 zclient_new (struct thread_master *master)
50 {
51 struct zclient *zclient;
52 zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
53
54 zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
55 zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
56 zclient->wb = buffer_new(0);
57 zclient->master = master;
58
59 return zclient;
60 }
61
62 /* This function is only called when exiting, because
63 many parts of the code do not check for I/O errors, so they could
64 reference an invalid pointer if the structure was ever freed.
65
66 Free zclient structure. */
67 void
zclient_free(struct zclient * zclient)68 zclient_free (struct zclient *zclient)
69 {
70 if (zclient->ibuf)
71 stream_free(zclient->ibuf);
72 if (zclient->obuf)
73 stream_free(zclient->obuf);
74 if (zclient->wb)
75 buffer_free(zclient->wb);
76
77 XFREE (MTYPE_ZCLIENT, zclient);
78 }
79
80 /* Initialize zebra client. Argument redist_default is unwanted
81 redistribute route type. */
82 void
zclient_init(struct zclient * zclient,int redist_default)83 zclient_init (struct zclient *zclient, int redist_default)
84 {
85 int i;
86
87 /* Enable zebra client connection by default. */
88 zclient->enable = 1;
89
90 /* Set -1 to the default socket value. */
91 zclient->sock = -1;
92
93 /* Clear redistribution flags. */
94 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
95 zclient->redist[i] = vrf_bitmap_init ();
96
97 /* Set unwanted redistribute route. bgpd does not need BGP route
98 redistribution. */
99 zclient->redist_default = redist_default;
100
101 /* Set default-information redistribute to zero. */
102 zclient->default_information = vrf_bitmap_init ();
103
104 /* Schedule first zclient connection. */
105 if (zclient_debug)
106 zlog_debug ("zclient start scheduled");
107
108 zclient_event (ZCLIENT_SCHEDULE, zclient);
109 }
110
111 /* Stop zebra client services. */
112 void
zclient_stop(struct zclient * zclient)113 zclient_stop (struct zclient *zclient)
114 {
115 int i;
116
117 if (zclient_debug)
118 zlog_debug ("zclient stopped");
119
120 /* Stop threads. */
121 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);
131
132 /* Close socket. */
133 if (zclient->sock >= 0)
134 {
135 close (zclient->sock);
136 zclient->sock = -1;
137 }
138 zclient->fail = 0;
139
140 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
141 {
142 vrf_bitmap_free(zclient->redist[i]);
143 zclient->redist[i] = VRF_BITMAP_NULL;
144 }
145 vrf_bitmap_free(zclient->default_information);
146 zclient->default_information = VRF_BITMAP_NULL;
147 }
148
149 void
zclient_reset(struct zclient * zclient)150 zclient_reset (struct zclient *zclient)
151 {
152 zclient_stop (zclient);
153 zclient_init (zclient, zclient->redist_default);
154 }
155
156 #ifdef HAVE_TCP_ZEBRA
157
158 /* Make socket to zebra daemon. Return zebra socket. */
159 static int
zclient_socket(void)160 zclient_socket(void)
161 {
162 int sock;
163 int ret;
164 struct sockaddr_in serv;
165
166 /* We should think about IPv6 connection. */
167 sock = socket (AF_INET, SOCK_STREAM, 0);
168 if (sock < 0)
169 return -1;
170
171 /* Make server socket. */
172 memset (&serv, 0, sizeof (struct sockaddr_in));
173 serv.sin_family = AF_INET;
174 serv.sin_port = htons (ZEBRA_PORT);
175 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
176 serv.sin_len = sizeof (struct sockaddr_in);
177 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
178 serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
179
180 /* Connect to zebra. */
181 ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
182 if (ret < 0)
183 {
184 zlog_warn ("%s connect failure: %d", __PRETTY_FUNCTION__, errno);
185 close (sock);
186 return -1;
187 }
188 return sock;
189 }
190
191 #else
192
193 /* For sockaddr_un. */
194 #include <sys/un.h>
195
196 static int
zclient_socket_un(const char * path)197 zclient_socket_un (const char *path)
198 {
199 int ret;
200 int sock, len;
201 struct sockaddr_un addr;
202
203 sock = socket (AF_UNIX, SOCK_STREAM, 0);
204 if (sock < 0)
205 return -1;
206
207 /* Make server socket. */
208 memset (&addr, 0, sizeof (struct sockaddr_un));
209 addr.sun_family = AF_UNIX;
210 strncpy (addr.sun_path, path, strlen (path));
211 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
212 len = addr.sun_len = SUN_LEN(&addr);
213 #else
214 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
215 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
216
217 ret = connect (sock, (struct sockaddr *) &addr, len);
218 if (ret < 0)
219 {
220 zlog_warn ("%s connect failure: %d", __PRETTY_FUNCTION__, errno);
221 close (sock);
222 return -1;
223 }
224 return sock;
225 }
226
227 #endif /* HAVE_TCP_ZEBRA */
228
229 /**
230 * Connect to zebra daemon.
231 * @param zclient a pointer to zclient structure
232 * @return socket fd just to make sure that connection established
233 * @see zclient_init
234 * @see zclient_new
235 */
236 int
zclient_socket_connect(struct zclient * zclient)237 zclient_socket_connect (struct zclient *zclient)
238 {
239 #ifdef HAVE_TCP_ZEBRA
240 zclient->sock = zclient_socket ();
241 #else
242 zclient->sock = zclient_socket_un (zclient_serv_path_get());
243 #endif
244 return zclient->sock;
245 }
246
247 static int
zclient_failed(struct zclient * zclient)248 zclient_failed(struct zclient *zclient)
249 {
250 zclient->fail++;
251 zclient_stop(zclient);
252 zclient_event(ZCLIENT_CONNECT, zclient);
253 return -1;
254 }
255
256 static int
zclient_flush_data(struct thread * thread)257 zclient_flush_data(struct thread *thread)
258 {
259 struct zclient *zclient = THREAD_ARG(thread);
260
261 zclient->t_write = NULL;
262 if (zclient->sock < 0)
263 return -1;
264 switch (buffer_flush_available(zclient->wb, zclient->sock))
265 {
266 case BUFFER_ERROR:
267 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
268 __func__, zclient->sock);
269 return zclient_failed(zclient);
270 break;
271 case BUFFER_PENDING:
272 zclient->t_write = thread_add_write (zclient->master, zclient_flush_data,
273 zclient, zclient->sock);
274 break;
275 case BUFFER_EMPTY:
276 break;
277 }
278 return 0;
279 }
280
281 int
zclient_send_message(struct zclient * zclient)282 zclient_send_message(struct zclient *zclient)
283 {
284 if (zclient->sock < 0)
285 return -1;
286 switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf),
287 stream_get_endp(zclient->obuf)))
288 {
289 case BUFFER_ERROR:
290 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
291 __func__, zclient->sock);
292 return zclient_failed(zclient);
293 break;
294 case BUFFER_EMPTY:
295 THREAD_OFF(zclient->t_write);
296 break;
297 case BUFFER_PENDING:
298 THREAD_WRITE_ON (zclient->master, zclient->t_write,
299 zclient_flush_data, zclient, zclient->sock);
300 break;
301 }
302 return 0;
303 }
304
305 void
zclient_create_header(struct stream * s,uint16_t command,vrf_id_t vrf_id)306 zclient_create_header (struct stream *s, uint16_t command, vrf_id_t vrf_id)
307 {
308 /* length placeholder, caller can update */
309 stream_putw (s, ZEBRA_HEADER_SIZE);
310 stream_putc (s, ZEBRA_HEADER_MARKER);
311 stream_putc (s, ZSERV_VERSION);
312 stream_putw (s, vrf_id);
313 stream_putw (s, command);
314 }
315
316 int
zclient_read_header(struct stream * s,int sock,u_int16_t * size,u_char * marker,u_char * version,u_int16_t * vrf_id,u_int16_t * cmd)317 zclient_read_header (struct stream *s, int sock, u_int16_t *size, u_char *marker,
318 u_char *version, u_int16_t *vrf_id, u_int16_t *cmd)
319 {
320 if (stream_read (s, sock, ZEBRA_HEADER_SIZE) != ZEBRA_HEADER_SIZE)
321 return -1;
322
323 *size = stream_getw (s) - ZEBRA_HEADER_SIZE;
324 *marker = stream_getc (s);
325 *version = stream_getc (s);
326 *vrf_id = stream_getw (s);
327 *cmd = stream_getw (s);
328
329 if (*version != ZSERV_VERSION || *marker != ZEBRA_HEADER_MARKER)
330 {
331 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
332 __func__, sock, *marker, *version);
333 return -1;
334 }
335
336 if (*size && stream_read (s, sock, *size) != *size)
337 return -1;
338
339 return 0;
340 }
341
342 /* Send simple Zebra message. */
343 static int
zebra_message_send(struct zclient * zclient,int command,vrf_id_t vrf_id)344 zebra_message_send (struct zclient *zclient, int command, vrf_id_t vrf_id)
345 {
346 struct stream *s;
347
348 /* Get zclient output buffer. */
349 s = zclient->obuf;
350 stream_reset (s);
351
352 /* Send very simple command only Zebra message. */
353 zclient_create_header (s, command, vrf_id);
354
355 return zclient_send_message(zclient);
356 }
357
358 static int
zebra_hello_send(struct zclient * zclient)359 zebra_hello_send (struct zclient *zclient)
360 {
361 struct stream *s;
362
363 if (zclient->redist_default)
364 {
365 s = zclient->obuf;
366 stream_reset (s);
367
368 /* The VRF ID in the HELLO message is always 0. */
369 zclient_create_header (s, ZEBRA_HELLO, VRF_DEFAULT);
370 stream_putc (s, zclient->redist_default);
371 stream_putw_at (s, 0, stream_get_endp (s));
372 return zclient_send_message(zclient);
373 }
374
375 return 0;
376 }
377
378 /* Send requests to zebra daemon for the information in a VRF. */
379 void
zclient_send_requests(struct zclient * zclient,vrf_id_t vrf_id)380 zclient_send_requests (struct zclient *zclient, vrf_id_t vrf_id)
381 {
382 int i;
383
384 /* zclient is disabled. */
385 if (! zclient->enable)
386 return;
387
388 /* If not connected to the zebra yet. */
389 if (zclient->sock < 0)
390 return;
391
392 if (zclient_debug)
393 zlog_debug ("%s: send messages for VRF %u", __func__, vrf_id);
394
395 /* We need router-id information. */
396 zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD, vrf_id);
397
398 /* We need interface information. */
399 zebra_message_send (zclient, ZEBRA_INTERFACE_ADD, vrf_id);
400
401 /* Set unwanted redistribute route. */
402 vrf_bitmap_set (zclient->redist[zclient->redist_default], vrf_id);
403
404 /* Flush all redistribute request. */
405 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
406 if (i != zclient->redist_default &&
407 vrf_bitmap_check (zclient->redist[i], vrf_id))
408 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, i, vrf_id);
409
410 /* If default information is needed. */
411 if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT))
412 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD, vrf_id);
413 }
414
415 /* Make connection to zebra daemon. */
416 int
zclient_start(struct zclient * zclient)417 zclient_start (struct zclient *zclient)
418 {
419 if (zclient_debug)
420 zlog_debug ("zclient_start is called");
421
422 /* zclient is disabled. */
423 if (! zclient->enable)
424 return 0;
425
426 /* If already connected to the zebra. */
427 if (zclient->sock >= 0)
428 return 0;
429
430 /* Check connect thread. */
431 if (zclient->t_connect)
432 return 0;
433
434 /*
435 * If we fail to connect to the socket on initialization,
436 * Let's wait a second and see if we can reconnect.
437 * Cause if we don't connect, we never attempt to
438 * reconnect. On startup if zebra is slow we
439 * can get into this situation.
440 */
441 while (zclient_socket_connect(zclient) < 0 && zclient->fail < 5)
442 {
443 if (zclient_debug)
444 zlog_debug ("zclient connection fail");
445 zclient->fail++;
446 sleep (1);
447 }
448
449 if (zclient->sock < 0)
450 {
451 zclient_event (ZCLIENT_CONNECT, zclient);
452 return -1;
453 }
454
455 if (set_nonblocking(zclient->sock) < 0)
456 zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
457
458 /* Clear fail count. */
459 zclient->fail = 0;
460 if (zclient_debug)
461 zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
462
463 /* Create read thread. */
464 zclient_event (ZCLIENT_READ, zclient);
465
466 zebra_hello_send (zclient);
467
468 /* Inform the successful connection. */
469 if (zclient->zebra_connected)
470 (*zclient->zebra_connected) (zclient);
471
472 return 0;
473 }
474
475 /* This function is a wrapper function for calling zclient_start from
476 timer or event thread. */
477 static int
zclient_connect(struct thread * t)478 zclient_connect (struct thread *t)
479 {
480 struct zclient *zclient;
481
482 zclient = THREAD_ARG (t);
483 zclient->t_connect = NULL;
484
485 if (zclient_debug)
486 zlog_debug ("zclient_connect is called");
487
488 return zclient_start (zclient);
489 }
490
491 /*
492 * "xdr_encode"-like interface that allows daemon (client) to send
493 * a message to zebra server for a route that needs to be
494 * added/deleted to the kernel. Info about the route is specified
495 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
496 * the info down the zclient socket using the stream_* functions.
497 *
498 * The corresponding read ("xdr_decode") function on the server
499 * side is zread_ipv4_add()/zread_ipv4_delete().
500 *
501 * 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
502 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
503 * | Length (2) | Command | Route Type |
504 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
505 * | ZEBRA Flags | Message Flags | Prefix length |
506 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
507 * | Destination IPv4 Prefix for route |
508 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
509 * | Nexthop count |
510 * +-+-+-+-+-+-+-+-+
511 *
512 *
513 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
514 * described, as per the Nexthop count. Each nexthop described as:
515 *
516 * +-+-+-+-+-+-+-+-+
517 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
518 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
519 * | IPv4 Nexthop address or Interface Index number |
520 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
521 *
522 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
523 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
524 * nexthop information is provided, and the message describes a prefix
525 * to blackhole or reject route.
526 *
527 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
528 * byte value.
529 *
530 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
531 * byte value.
532 *
533 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
534 *
535 * XXX: No attention paid to alignment.
536 */
537 int
zapi_ipv4_route(u_char cmd,struct zclient * zclient,struct prefix_ipv4 * p,struct zapi_ipv4 * api)538 zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
539 struct zapi_ipv4 *api)
540 {
541 int i;
542 int psize;
543 struct stream *s;
544
545 /* Reset stream. */
546 s = zclient->obuf;
547 stream_reset (s);
548
549 zclient_create_header (s, cmd, api->vrf_id);
550
551 /* Put type and nexthop. */
552 stream_putc (s, api->type);
553 stream_putc (s, api->flags);
554 stream_putc (s, api->message);
555 stream_putw (s, api->safi);
556
557 /* Put prefix information. */
558 psize = PSIZE (p->prefixlen);
559 stream_putc (s, p->prefixlen);
560 stream_write (s, (u_char *) & p->prefix, psize);
561
562 /* Nexthop, ifindex, distance and metric information. */
563 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
564 {
565 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
566 {
567 stream_putc (s, 1);
568 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
569 /* XXX assert(api->nexthop_num == 0); */
570 /* XXX assert(api->ifindex_num == 0); */
571 }
572 else
573 stream_putc (s, api->nexthop_num + api->ifindex_num);
574
575 for (i = 0; i < api->nexthop_num; i++)
576 {
577 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
578 stream_put_in_addr (s, api->nexthop[i]);
579 }
580 for (i = 0; i < api->ifindex_num; i++)
581 {
582 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
583 stream_putl (s, api->ifindex[i]);
584 }
585 }
586
587 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
588 stream_putc (s, api->distance);
589 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
590 stream_putl (s, api->metric);
591 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
592 stream_putl (s, api->mtu);
593 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
594 stream_putl (s, api->tag);
595
596 /* Put length at the first point of the stream. */
597 stream_putw_at (s, 0, stream_get_endp (s));
598
599 return zclient_send_message(zclient);
600 }
601
602 #ifdef HAVE_IPV6
603 int
zapi_ipv6_route(u_char cmd,struct zclient * zclient,struct prefix_ipv6 * p,struct zapi_ipv6 * api)604 zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
605 struct zapi_ipv6 *api)
606 {
607 int i;
608 int psize;
609 struct stream *s;
610
611 /* Reset stream. */
612 s = zclient->obuf;
613 stream_reset (s);
614
615 zclient_create_header (s, cmd, api->vrf_id);
616
617 /* Put type and nexthop. */
618 stream_putc (s, api->type);
619 stream_putc (s, api->flags);
620 stream_putc (s, api->message);
621 stream_putw (s, api->safi);
622
623 /* Put prefix information. */
624 psize = PSIZE (p->prefixlen);
625 stream_putc (s, p->prefixlen);
626 stream_write (s, (u_char *)&p->prefix, psize);
627
628 /* Nexthop, ifindex, distance and metric information. */
629 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
630 {
631 stream_putc (s, api->nexthop_num + api->ifindex_num);
632
633 for (i = 0; i < api->nexthop_num; i++)
634 {
635 stream_putc (s, ZEBRA_NEXTHOP_IPV6);
636 stream_write (s, (u_char *)api->nexthop[i], 16);
637 }
638 for (i = 0; i < api->ifindex_num; i++)
639 {
640 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
641 stream_putl (s, api->ifindex[i]);
642 }
643 }
644
645 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
646 stream_putc (s, api->distance);
647 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
648 stream_putl (s, api->metric);
649 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
650 stream_putl (s, api->mtu);
651 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
652 stream_putl (s, api->tag);
653
654 /* Put length at the first point of the stream. */
655 stream_putw_at (s, 0, stream_get_endp (s));
656
657 return zclient_send_message(zclient);
658 }
659 #endif /* HAVE_IPV6 */
660
661 /*
662 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
663 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
664 * then set/unset redist[type] in the client handle (a struct zserv) for the
665 * sending client
666 */
667 int
zebra_redistribute_send(int command,struct zclient * zclient,int type,vrf_id_t vrf_id)668 zebra_redistribute_send (int command, struct zclient *zclient, int type,
669 vrf_id_t vrf_id)
670 {
671 struct stream *s;
672
673 s = zclient->obuf;
674 stream_reset(s);
675
676 zclient_create_header (s, command, vrf_id);
677 stream_putc (s, type);
678
679 stream_putw_at (s, 0, stream_get_endp (s));
680
681 return zclient_send_message(zclient);
682 }
683
684 /* Get prefix in ZServ format; family should be filled in on prefix */
685 static void
zclient_stream_get_prefix(struct stream * s,struct prefix * p)686 zclient_stream_get_prefix (struct stream *s, struct prefix *p)
687 {
688 size_t plen = prefix_blen (p);
689 u_char c;
690 p->prefixlen = 0;
691
692 if (plen == 0)
693 return;
694
695 stream_get (&p->u.prefix, s, plen);
696 c = stream_getc(s);
697 p->prefixlen = MIN(plen * 8, c);
698 }
699
700 /* Router-id update from zebra daemon. */
701 void
zebra_router_id_update_read(struct stream * s,struct prefix * rid)702 zebra_router_id_update_read (struct stream *s, struct prefix *rid)
703 {
704 /* Fetch interface address. */
705 rid->family = stream_getc (s);
706
707 zclient_stream_get_prefix (s, rid);
708 }
709
710 /* Interface addition from zebra daemon. */
711 /*
712 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
713 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
714 * 0 1 2 3
715 * 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
716 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
717 * | ifname |
718 * | |
719 * | |
720 * | |
721 * | |
722 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
723 * | ifindex |
724 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
725 * | status |
726 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
727 * | if_flags |
728 * | |
729 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
730 * | metric |
731 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
732 * | ifmtu |
733 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
734 * | ifmtu6 |
735 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
736 * | bandwidth |
737 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
738 * | Link Layer Type |
739 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
740 * | Harware Address Length |
741 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
742 * | Hardware Address if HW lenght different from 0 |
743 * | ... max INTERFACE_HWADDR_MAX |
744 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
745 * | Link_params? | Whether a link-params follows: 1 or 0.
746 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
747 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
748 * | .... (struct if_link_params). |
749 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
750 */
751
752 struct interface *
zebra_interface_add_read(struct stream * s,vrf_id_t vrf_id)753 zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id)
754 {
755 struct interface *ifp;
756 char ifname_tmp[INTERFACE_NAMSIZ];
757
758 /* Read interface name. */
759 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
760
761 /* Lookup/create interface by name. */
762 ifp = if_get_by_name_len_vrf (ifname_tmp,
763 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
764 vrf_id);
765
766 zebra_interface_if_set_value (s, ifp);
767
768 return ifp;
769 }
770
771 /*
772 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
773 * from zebra server. The format of this message is the same as
774 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
775 * comments for zebra_interface_add_read), except that no sockaddr_dl
776 * is sent at the tail of the message.
777 */
778 struct interface *
zebra_interface_state_read(struct stream * s,vrf_id_t vrf_id)779 zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id)
780 {
781 struct interface *ifp;
782 char ifname_tmp[INTERFACE_NAMSIZ];
783
784 /* Read interface name. */
785 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
786
787 /* Lookup this by interface index. */
788 ifp = if_lookup_by_name_len_vrf (ifname_tmp,
789 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
790 vrf_id);
791
792 /* If such interface does not exist, indicate an error */
793 if (! ifp)
794 return NULL;
795
796 zebra_interface_if_set_value (s, ifp);
797
798 return ifp;
799 }
800
801 static void
link_params_set_value(struct stream * s,struct if_link_params * iflp)802 link_params_set_value(struct stream *s, struct if_link_params *iflp)
803 {
804
805 if (iflp == NULL)
806 return;
807
808 iflp->lp_status = stream_getl (s);
809 iflp->te_metric = stream_getl (s);
810 iflp->max_bw = stream_getf (s);
811 iflp->max_rsv_bw = stream_getf (s);
812 uint32_t bwclassnum = stream_getl (s);
813 {
814 unsigned int i;
815 for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++)
816 iflp->unrsv_bw[i] = stream_getf (s);
817 if (i < bwclassnum)
818 zlog_err ("%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
819 " - outdated library?",
820 __func__, bwclassnum, MAX_CLASS_TYPE);
821 }
822 iflp->admin_grp = stream_getl (s);
823 iflp->rmt_as = stream_getl (s);
824 iflp->rmt_ip.s_addr = stream_get_ipv4 (s);
825
826 iflp->av_delay = stream_getl (s);
827 iflp->min_delay = stream_getl (s);
828 iflp->max_delay = stream_getl (s);
829 iflp->delay_var = stream_getl (s);
830
831 iflp->pkt_loss = stream_getf (s);
832 iflp->res_bw = stream_getf (s);
833 iflp->ava_bw = stream_getf (s);
834 iflp->use_bw = stream_getf (s);
835 }
836
837 struct interface *
zebra_interface_link_params_read(struct stream * s)838 zebra_interface_link_params_read (struct stream *s)
839 {
840 struct if_link_params *iflp;
841 uint32_t ifindex = stream_getl (s);
842
843 struct interface *ifp = if_lookup_by_index (ifindex);
844
845 if (ifp == NULL || s == NULL)
846 {
847 zlog_err ("%s: unknown ifindex %u, shouldn't happen",
848 __func__, ifindex);
849 return NULL;
850 }
851
852 if ((iflp = if_link_params_get (ifp)) == NULL)
853 return NULL;
854
855 link_params_set_value(s, iflp);
856
857 return ifp;
858 }
859
860 void
zebra_interface_if_set_value(struct stream * s,struct interface * ifp)861 zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
862 {
863 u_char link_params_status = 0;
864
865 /* Read interface's index. */
866 ifp->ifindex = stream_getl (s);
867 ifp->status = stream_getc (s);
868
869 /* Read interface's value. */
870 ifp->flags = stream_getq (s);
871 ifp->metric = stream_getl (s);
872 ifp->mtu = stream_getl (s);
873 ifp->mtu6 = stream_getl (s);
874 ifp->bandwidth = stream_getl (s);
875 ifp->ll_type = stream_getl (s);
876 ifp->hw_addr_len = stream_getl (s);
877 if (ifp->hw_addr_len)
878 stream_get (ifp->hw_addr, s, MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
879
880 /* Read Traffic Engineering status */
881 link_params_status = stream_getc (s);
882 /* Then, Traffic Engineering parameters if any */
883 if (link_params_status)
884 {
885 struct if_link_params *iflp = if_link_params_get (ifp);
886 link_params_set_value(s, iflp);
887 }
888 }
889
890 size_t
zebra_interface_link_params_write(struct stream * s,struct interface * ifp)891 zebra_interface_link_params_write (struct stream *s, struct interface *ifp)
892 {
893 size_t w;
894 struct if_link_params *iflp;
895 int i;
896
897 if (s == NULL || ifp == NULL || ifp->link_params == NULL)
898 return 0;
899
900 iflp = ifp->link_params;
901 w = 0;
902
903 w += stream_putl (s, iflp->lp_status);
904
905 w += stream_putl (s, iflp->te_metric);
906 w += stream_putf (s, iflp->max_bw);
907 w += stream_putf (s, iflp->max_rsv_bw);
908
909 w += stream_putl (s, MAX_CLASS_TYPE);
910 for (i = 0; i < MAX_CLASS_TYPE; i++)
911 w += stream_putf (s, iflp->unrsv_bw[i]);
912
913 w += stream_putl (s, iflp->admin_grp);
914 w += stream_putl (s, iflp->rmt_as);
915 w += stream_put_in_addr (s, &iflp->rmt_ip);
916
917 w += stream_putl (s, iflp->av_delay);
918 w += stream_putl (s, iflp->min_delay);
919 w += stream_putl (s, iflp->max_delay);
920 w += stream_putl (s, iflp->delay_var);
921
922 w += stream_putf (s, iflp->pkt_loss);
923 w += stream_putf (s, iflp->res_bw);
924 w += stream_putf (s, iflp->ava_bw);
925 w += stream_putf (s, iflp->use_bw);
926
927 return w;
928 }
929
930 /*
931 * format of message for address additon is:
932 * 0
933 * 0 1 2 3 4 5 6 7
934 * +-+-+-+-+-+-+-+-+
935 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
936 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
937 * | |
938 * + +
939 * | ifindex |
940 * + +
941 * | |
942 * + +
943 * | |
944 * +-+-+-+-+-+-+-+-+
945 * | ifc_flags | flags for connected address
946 * +-+-+-+-+-+-+-+-+
947 * | addr_family |
948 * +-+-+-+-+-+-+-+-+
949 * | addr... |
950 * : :
951 * | |
952 * +-+-+-+-+-+-+-+-+
953 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
954 * +-+-+-+-+-+-+-+-+
955 * | daddr.. |
956 * : :
957 * | |
958 * +-+-+-+-+-+-+-+-+
959 */
960
961 static int
memconstant(const void * s,int c,size_t n)962 memconstant(const void *s, int c, size_t n)
963 {
964 const u_char *p = s;
965
966 while (n-- > 0)
967 if (*p++ != c)
968 return 0;
969 return 1;
970 }
971
972 struct connected *
zebra_interface_address_read(int type,struct stream * s,vrf_id_t vrf_id)973 zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id)
974 {
975 ifindex_t ifindex;
976 struct interface *ifp;
977 struct connected *ifc;
978 struct prefix p, d, *dp;
979 int plen;
980 u_char ifc_flags;
981
982 memset (&p, 0, sizeof(p));
983 memset (&d, 0, sizeof(d));
984
985 /* Get interface index. */
986 ifindex = stream_getl (s);
987
988 /* Lookup index. */
989 ifp = if_lookup_by_index_vrf (ifindex, vrf_id);
990 if (ifp == NULL)
991 {
992 zlog_warn ("zebra_interface_address_read(%s): "
993 "Can't find interface by ifindex: %d ",
994 (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"),
995 ifindex);
996 return NULL;
997 }
998
999 /* Fetch flag. */
1000 ifc_flags = stream_getc (s);
1001
1002 /* Fetch interface address. */
1003 d.family = p.family = stream_getc (s);
1004 plen = prefix_blen (&d);
1005
1006 zclient_stream_get_prefix (s, &p);
1007
1008 /* Fetch destination address. */
1009 stream_get (&d.u.prefix, s, plen);
1010
1011 /* N.B. NULL destination pointers are encoded as all zeroes */
1012 dp = memconstant(&d.u.prefix,0,plen) ? NULL : &d;
1013
1014 if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
1015 {
1016 /* N.B. NULL destination pointers are encoded as all zeroes */
1017 ifc = connected_add_by_prefix(ifp, &p, dp);
1018 if (ifc != NULL)
1019 {
1020 ifc->flags = ifc_flags;
1021 if (ifc->destination)
1022 ifc->destination->prefixlen = ifc->address->prefixlen;
1023 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
1024 {
1025 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
1026 char buf[PREFIX_STRLEN];
1027 zlog_warn("warning: interface %s address %s "
1028 "with peer flag set, but no peer address!",
1029 ifp->name,
1030 prefix2str (ifc->address, buf, sizeof buf));
1031 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
1032 }
1033 }
1034 }
1035 else
1036 {
1037 assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
1038 ifc = connected_delete_by_prefix(ifp, &p);
1039 }
1040
1041 return ifc;
1042 }
1043
1044
1045 /* Zebra client message read function. */
1046 static int
zclient_read(struct thread * thread)1047 zclient_read (struct thread *thread)
1048 {
1049 size_t already;
1050 uint16_t length, command;
1051 uint8_t marker, version;
1052 vrf_id_t vrf_id;
1053 struct zclient *zclient;
1054
1055 /* Get socket to zebra. */
1056 zclient = THREAD_ARG (thread);
1057 zclient->t_read = NULL;
1058
1059 /* Read zebra header (if we don't have it already). */
1060 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
1061 {
1062 ssize_t nbyte;
1063 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
1064 ZEBRA_HEADER_SIZE-already)) == 0) ||
1065 (nbyte == -1))
1066 {
1067 if (zclient_debug)
1068 zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
1069 return zclient_failed(zclient);
1070 }
1071 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
1072 {
1073 /* Try again later. */
1074 zclient_event (ZCLIENT_READ, zclient);
1075 return 0;
1076 }
1077 already = ZEBRA_HEADER_SIZE;
1078 }
1079
1080 /* Reset to read from the beginning of the incoming packet. */
1081 stream_set_getp(zclient->ibuf, 0);
1082
1083 /* Fetch header values. */
1084 length = stream_getw (zclient->ibuf);
1085 marker = stream_getc (zclient->ibuf);
1086 version = stream_getc (zclient->ibuf);
1087 vrf_id = stream_getw (zclient->ibuf);
1088 command = stream_getw (zclient->ibuf);
1089
1090 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1091 {
1092 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1093 __func__, zclient->sock, marker, version);
1094 return zclient_failed(zclient);
1095 }
1096
1097 if (length < ZEBRA_HEADER_SIZE)
1098 {
1099 zlog_err("%s: socket %d message length %u is less than %d ",
1100 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
1101 return zclient_failed(zclient);
1102 }
1103
1104 /* Length check. */
1105 if (length > STREAM_SIZE(zclient->ibuf))
1106 {
1107 struct stream *ns;
1108 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
1109 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
1110 ns = stream_new(length);
1111 stream_copy(ns, zclient->ibuf);
1112 stream_free (zclient->ibuf);
1113 zclient->ibuf = ns;
1114 }
1115
1116 /* Read rest of zebra packet. */
1117 if (already < length)
1118 {
1119 ssize_t nbyte;
1120 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
1121 length-already)) == 0) ||
1122 (nbyte == -1))
1123 {
1124 if (zclient_debug)
1125 zlog_debug("zclient connection closed socket [%d].", zclient->sock);
1126 return zclient_failed(zclient);
1127 }
1128 if (nbyte != (ssize_t)(length-already))
1129 {
1130 /* Try again later. */
1131 zclient_event (ZCLIENT_READ, zclient);
1132 return 0;
1133 }
1134 }
1135
1136 length -= ZEBRA_HEADER_SIZE;
1137
1138 if (zclient_debug)
1139 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id);
1140
1141 switch (command)
1142 {
1143 case ZEBRA_ROUTER_ID_UPDATE:
1144 if (zclient->router_id_update)
1145 (*zclient->router_id_update) (command, zclient, length, vrf_id);
1146 break;
1147 case ZEBRA_INTERFACE_ADD:
1148 if (zclient->interface_add)
1149 (*zclient->interface_add) (command, zclient, length, vrf_id);
1150 break;
1151 case ZEBRA_INTERFACE_DELETE:
1152 if (zclient->interface_delete)
1153 (*zclient->interface_delete) (command, zclient, length, vrf_id);
1154 break;
1155 case ZEBRA_INTERFACE_ADDRESS_ADD:
1156 if (zclient->interface_address_add)
1157 (*zclient->interface_address_add) (command, zclient, length, vrf_id);
1158 break;
1159 case ZEBRA_INTERFACE_ADDRESS_DELETE:
1160 if (zclient->interface_address_delete)
1161 (*zclient->interface_address_delete) (command, zclient, length, vrf_id);
1162 break;
1163 case ZEBRA_INTERFACE_UP:
1164 if (zclient->interface_up)
1165 (*zclient->interface_up) (command, zclient, length, vrf_id);
1166 break;
1167 case ZEBRA_INTERFACE_DOWN:
1168 if (zclient->interface_down)
1169 (*zclient->interface_down) (command, zclient, length, vrf_id);
1170 break;
1171 case ZEBRA_IPV4_ROUTE_ADD:
1172 if (zclient->ipv4_route_add)
1173 (*zclient->ipv4_route_add) (command, zclient, length, vrf_id);
1174 break;
1175 case ZEBRA_IPV4_ROUTE_DELETE:
1176 if (zclient->ipv4_route_delete)
1177 (*zclient->ipv4_route_delete) (command, zclient, length, vrf_id);
1178 break;
1179 case ZEBRA_IPV6_ROUTE_ADD:
1180 if (zclient->ipv6_route_add)
1181 (*zclient->ipv6_route_add) (command, zclient, length, vrf_id);
1182 break;
1183 case ZEBRA_IPV6_ROUTE_DELETE:
1184 if (zclient->ipv6_route_delete)
1185 (*zclient->ipv6_route_delete) (command, zclient, length, vrf_id);
1186 break;
1187 case ZEBRA_INTERFACE_LINK_PARAMS:
1188 if (zclient->interface_link_params)
1189 (*zclient->interface_link_params) (command, zclient, length);
1190 case ZEBRA_NEXTHOP_UPDATE:
1191 if (zclient->nexthop_update)
1192 (*zclient->nexthop_update) (command, zclient, length, vrf_id);
1193 break;
1194 default:
1195 break;
1196 }
1197
1198 if (zclient->sock < 0)
1199 /* Connection was closed during packet processing. */
1200 return -1;
1201
1202 /* Register read thread. */
1203 stream_reset(zclient->ibuf);
1204 zclient_event (ZCLIENT_READ, zclient);
1205
1206 return 0;
1207 }
1208
1209 void
zclient_redistribute(int command,struct zclient * zclient,int type,vrf_id_t vrf_id)1210 zclient_redistribute (int command, struct zclient *zclient, int type,
1211 vrf_id_t vrf_id)
1212 {
1213
1214 if (command == ZEBRA_REDISTRIBUTE_ADD)
1215 {
1216 if (vrf_bitmap_check (zclient->redist[type], vrf_id))
1217 return;
1218 vrf_bitmap_set (zclient->redist[type], vrf_id);
1219 }
1220 else
1221 {
1222 if (!vrf_bitmap_check (zclient->redist[type], vrf_id))
1223 return;
1224 vrf_bitmap_unset (zclient->redist[type], vrf_id);
1225 }
1226
1227 if (zclient->sock > 0)
1228 zebra_redistribute_send (command, zclient, type, vrf_id);
1229 }
1230
1231
1232 void
zclient_redistribute_default(int command,struct zclient * zclient,vrf_id_t vrf_id)1233 zclient_redistribute_default (int command, struct zclient *zclient,
1234 vrf_id_t vrf_id)
1235 {
1236
1237 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
1238 {
1239 if (vrf_bitmap_check (zclient->default_information, vrf_id))
1240 return;
1241 vrf_bitmap_set (zclient->default_information, vrf_id);
1242 }
1243 else
1244 {
1245 if (!vrf_bitmap_check (zclient->default_information, vrf_id))
1246 return;
1247 vrf_bitmap_unset (zclient->default_information, vrf_id);
1248 }
1249
1250 if (zclient->sock > 0)
1251 zebra_message_send (zclient, command, vrf_id);
1252 }
1253
1254 static void
zclient_event(enum event event,struct zclient * zclient)1255 zclient_event (enum event event, struct zclient *zclient)
1256 {
1257 switch (event)
1258 {
1259 case ZCLIENT_SCHEDULE:
1260 if (! zclient->t_connect)
1261 zclient->t_connect =
1262 thread_add_event (zclient->master, zclient_connect, zclient, 0);
1263 break;
1264 case ZCLIENT_CONNECT:
1265 if (zclient->fail >= 10)
1266 return;
1267 if (zclient_debug)
1268 zlog_debug ("zclient connect schedule interval is %d",
1269 zclient->fail < 3 ? 10 : 60);
1270 if (! zclient->t_connect)
1271 zclient->t_connect =
1272 thread_add_timer (zclient->master, zclient_connect, zclient,
1273 zclient->fail < 3 ? 10 : 60);
1274 break;
1275 case ZCLIENT_READ:
1276 zclient->t_read =
1277 thread_add_read (zclient->master, zclient_read, zclient, zclient->sock);
1278 break;
1279 }
1280 }
1281
zclient_serv_path_get()1282 const char *zclient_serv_path_get()
1283 {
1284 return zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH;
1285 }
1286
1287 void
zclient_serv_path_set(char * path)1288 zclient_serv_path_set (char *path)
1289 {
1290 struct stat sb;
1291
1292 /* reset */
1293 zclient_serv_path = NULL;
1294
1295 /* test if `path' is socket. don't set it otherwise. */
1296 if (stat(path, &sb) == -1)
1297 {
1298 zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
1299 return;
1300 }
1301
1302 if ((sb.st_mode & S_IFMT) != S_IFSOCK)
1303 {
1304 zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
1305 return;
1306 }
1307
1308 /* it seems that path is unix socket */
1309 zclient_serv_path = path;
1310 }
1311
1312