]>
git.proxmox.com Git - mirror_frr.git/blob - ospfclient/ospf_apiclient.c
2 * Client side of OSPF API.
3 * Copyright (C) 2001, 2002, 2003 Ralph Keller
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,
20 * Boston, MA 02111-1307, USA.
25 #include <lib/version.h>
39 #include "ospfd/ospfd.h"
40 #include "ospfd/ospf_interface.h"
41 #include "ospfd/ospf_asbr.h"
42 #include "ospfd/ospf_lsa.h"
43 #include "ospfd/ospf_opaque.h"
44 #include "ospfd/ospf_lsdb.h"
45 #include "ospfd/ospf_neighbor.h"
46 #include "ospfd/ospf_dump.h"
47 #include "ospfd/ospf_zebra.h"
48 #include "ospfd/ospf_api.h"
50 #include "ospf_apiclient.h"
52 DEFINE_MTYPE_STATIC(OSPFD
, OSPF_APICLIENT
, "OSPF-API client")
54 /* Backlog for listen */
57 /* -----------------------------------------------------------
58 * Forward declarations
59 * -----------------------------------------------------------
62 void ospf_apiclient_handle_reply (struct ospf_apiclient
*oclient
,
64 void ospf_apiclient_handle_update_notify (struct ospf_apiclient
*oclient
,
66 void ospf_apiclient_handle_delete_notify (struct ospf_apiclient
*oclient
,
69 /* -----------------------------------------------------------
71 * -----------------------------------------------------------
75 ospf_apiclient_getport (void)
77 struct servent
*sp
= getservbyname ("ospfapi", "tcp");
79 return sp
? ntohs (sp
->s_port
) : OSPF_API_SYNC_PORT
;
82 /* -----------------------------------------------------------
83 * Followings are functions for connection management
84 * -----------------------------------------------------------
87 struct ospf_apiclient
*
88 ospf_apiclient_connect (char *host
, int syncport
)
90 struct sockaddr_in myaddr_sync
;
91 struct sockaddr_in myaddr_async
;
92 struct sockaddr_in peeraddr
;
94 struct ospf_apiclient
*new;
96 unsigned int peeraddrlen
;
97 int async_server_sock
;
102 /* There are two connections between the client and the server.
103 First the client opens a connection for synchronous requests/replies
104 to the server. The server will accept this connection and
105 as a reaction open a reverse connection channel for
106 asynchronous messages. */
108 async_server_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
109 if (async_server_sock
< 0)
112 "ospf_apiclient_connect: creating async socket failed\n");
116 /* Prepare socket for asynchronous messages */
117 /* Initialize async address structure */
118 memset (&myaddr_async
, 0, sizeof (struct sockaddr_in
));
119 myaddr_async
.sin_family
= AF_INET
;
120 myaddr_async
.sin_addr
.s_addr
= htonl (INADDR_ANY
);
121 myaddr_async
.sin_port
= htons (syncport
+1);
122 size
= sizeof (struct sockaddr_in
);
123 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
124 myaddr_async
.sin_len
= size
;
125 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
127 /* This is a server socket, reuse addr and port */
128 ret
= setsockopt (async_server_sock
, SOL_SOCKET
,
129 SO_REUSEADDR
, (void *) &on
, sizeof (on
));
132 fprintf (stderr
, "ospf_apiclient_connect: SO_REUSEADDR failed\n");
133 close (async_server_sock
);
138 ret
= setsockopt (async_server_sock
, SOL_SOCKET
, SO_REUSEPORT
,
139 (void *) &on
, sizeof (on
));
142 fprintf (stderr
, "ospf_apiclient_connect: SO_REUSEPORT failed\n");
143 close (async_server_sock
);
146 #endif /* SO_REUSEPORT */
148 /* Bind socket to address structure */
149 ret
= bind (async_server_sock
, (struct sockaddr
*) &myaddr_async
, size
);
152 fprintf (stderr
, "ospf_apiclient_connect: bind async socket failed\n");
153 close (async_server_sock
);
157 /* Wait for reverse channel connection establishment from server */
158 ret
= listen (async_server_sock
, BACKLOG
);
161 fprintf (stderr
, "ospf_apiclient_connect: listen: %s\n", safe_strerror (errno
));
162 close (async_server_sock
);
166 /* Make connection for synchronous requests and connect to server */
167 /* Resolve address of server */
168 hp
= gethostbyname (host
);
171 fprintf (stderr
, "ospf_apiclient_connect: no such host %s\n", host
);
172 close (async_server_sock
);
176 fd1
= socket (AF_INET
, SOCK_STREAM
, 0);
180 "ospf_apiclient_connect: creating sync socket failed\n");
185 /* Reuse addr and port */
186 ret
= setsockopt (fd1
, SOL_SOCKET
,
187 SO_REUSEADDR
, (void *) &on
, sizeof (on
));
190 fprintf (stderr
, "ospf_apiclient_connect: SO_REUSEADDR failed\n");
196 ret
= setsockopt (fd1
, SOL_SOCKET
, SO_REUSEPORT
,
197 (void *) &on
, sizeof (on
));
200 fprintf (stderr
, "ospf_apiclient_connect: SO_REUSEPORT failed\n");
204 #endif /* SO_REUSEPORT */
207 /* Bind sync socket to address structure. This is needed since we
208 want the sync port number on a fixed port number. The reverse
209 async channel will be at this port+1 */
211 memset (&myaddr_sync
, 0, sizeof (struct sockaddr_in
));
212 myaddr_sync
.sin_family
= AF_INET
;
213 myaddr_sync
.sin_port
= htons (syncport
);
214 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
215 myaddr_sync
.sin_len
= sizeof (struct sockaddr_in
);
216 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
218 ret
= bind (fd1
, (struct sockaddr
*) &myaddr_sync
, size
);
221 fprintf (stderr
, "ospf_apiclient_connect: bind sync socket failed\n");
226 /* Prepare address structure for connect */
227 memcpy (&myaddr_sync
.sin_addr
, hp
->h_addr
, hp
->h_length
);
228 myaddr_sync
.sin_family
= AF_INET
;
229 myaddr_sync
.sin_port
= htons(ospf_apiclient_getport ());
230 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
231 myaddr_sync
.sin_len
= sizeof (struct sockaddr_in
);
232 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
234 /* Now establish synchronous channel with OSPF daemon */
235 ret
= connect (fd1
, (struct sockaddr
*) &myaddr_sync
,
236 sizeof (struct sockaddr_in
));
239 fprintf (stderr
, "ospf_apiclient_connect: sync connect failed\n");
240 close (async_server_sock
);
245 /* Accept reverse connection */
246 peeraddrlen
= sizeof (struct sockaddr_in
);
247 memset (&peeraddr
, 0, peeraddrlen
);
250 accept (async_server_sock
, (struct sockaddr
*) &peeraddr
, &peeraddrlen
);
253 fprintf (stderr
, "ospf_apiclient_connect: accept async failed\n");
254 close (async_server_sock
);
259 /* Server socket is not needed anymore since we are not accepting more
261 close (async_server_sock
);
263 /* Create new client-side instance */
264 new = XCALLOC (MTYPE_OSPF_APICLIENT
, sizeof (struct ospf_apiclient
));
266 /* Initialize socket descriptors for sync and async channels */
274 ospf_apiclient_close (struct ospf_apiclient
*oclient
)
277 if (oclient
->fd_sync
>= 0)
279 close (oclient
->fd_sync
);
282 if (oclient
->fd_async
>= 0)
284 close (oclient
->fd_async
);
287 /* Free client structure */
288 XFREE (MTYPE_OSPF_APICLIENT
, oclient
);
292 /* -----------------------------------------------------------
293 * Followings are functions to send a request to OSPFd
294 * -----------------------------------------------------------
297 /* Send synchronous request, wait for reply */
299 ospf_apiclient_send_request (struct ospf_apiclient
*oclient
, struct msg
*msg
)
302 struct msg_reply
*msgreply
;
305 /* NB: Given "msg" is freed inside this function. */
307 /* Remember the sequence number of the request */
308 reqseq
= ntohl (msg
->hdr
.msgseq
);
310 /* Write message to OSPFd */
311 rc
= msg_write (oclient
->fd_sync
, msg
);
319 /* Wait for reply *//* NB: New "msg" is allocated by "msg_read()". */
320 msg
= msg_read (oclient
->fd_sync
);
324 assert (msg
->hdr
.msgtype
== MSG_REPLY
);
325 assert (ntohl (msg
->hdr
.msgseq
) == reqseq
);
327 msgreply
= (struct msg_reply
*) STREAM_DATA (msg
->s
);
328 rc
= msgreply
->errcode
;
335 /* -----------------------------------------------------------
337 * -----------------------------------------------------------
341 ospf_apiclient_get_seqnr (void)
343 static u_int32_t seqnr
= MIN_SEQ
;
347 /* Increment sequence number */
359 /* -----------------------------------------------------------
360 * API to access OSPF daemon by client applications.
361 * -----------------------------------------------------------
365 * Synchronous request to register opaque type.
368 ospf_apiclient_register_opaque_type (struct ospf_apiclient
*cl
,
369 u_char ltype
, u_char otype
)
374 /* just put 1 as a sequence number. */
375 msg
= new_msg_register_opaque_type (ospf_apiclient_get_seqnr (),
379 fprintf (stderr
, "new_msg_register_opaque_type failed\n");
383 rc
= ospf_apiclient_send_request (cl
, msg
);
388 * Synchronous request to synchronize with OSPF's LSDB.
389 * Two steps required: register_event in order to get
390 * dynamic updates and LSDB_Sync.
393 ospf_apiclient_sync_lsdb (struct ospf_apiclient
*oclient
)
397 struct lsa_filter_type filter
;
399 filter
.typemask
= 0xFFFF; /* all LSAs */
400 filter
.origin
= ANY_ORIGIN
;
401 filter
.num_areas
= 0; /* all Areas. */
403 msg
= new_msg_register_event (ospf_apiclient_get_seqnr (), &filter
);
406 fprintf (stderr
, "new_msg_register_event failed\n");
409 rc
= ospf_apiclient_send_request (oclient
, msg
);
414 msg
= new_msg_sync_lsdb (ospf_apiclient_get_seqnr (), &filter
);
417 fprintf (stderr
, "new_msg_sync_lsdb failed\n");
420 rc
= ospf_apiclient_send_request (oclient
, msg
);
427 * Synchronous request to originate or update an LSA.
431 ospf_apiclient_lsa_originate (struct ospf_apiclient
*oclient
,
432 struct in_addr ifaddr
,
433 struct in_addr area_id
,
435 u_char opaque_type
, u_int32_t opaque_id
,
436 void *opaquedata
, int opaquelen
)
440 u_char buf
[OSPF_MAX_LSA_SIZE
];
441 struct lsa_header
*lsah
;
445 /* We can only originate opaque LSAs */
446 if (!IS_OPAQUE_LSA (lsa_type
))
448 fprintf (stderr
, "Cannot originate non-opaque LSA type %d\n", lsa_type
);
449 return OSPF_API_ILLEGALLSATYPE
;
452 /* Make a new LSA from parameters */
453 lsah
= (struct lsa_header
*) buf
;
456 lsah
->type
= lsa_type
;
458 tmp
= SET_OPAQUE_LSID (opaque_type
, opaque_id
);
459 lsah
->id
.s_addr
= htonl (tmp
);
460 lsah
->adv_router
.s_addr
= 0;
463 lsah
->length
= htons (sizeof (struct lsa_header
) + opaquelen
);
465 memcpy (((u_char
*) lsah
) + sizeof (struct lsa_header
), opaquedata
,
468 msg
= new_msg_originate_request (ospf_apiclient_get_seqnr (),
469 ifaddr
, area_id
, lsah
);
472 fprintf (stderr
, "new_msg_originate_request failed\n");
473 return OSPF_API_NOMEMORY
;
476 rc
= ospf_apiclient_send_request (oclient
, msg
);
481 ospf_apiclient_lsa_delete (struct ospf_apiclient
*oclient
,
482 struct in_addr area_id
, u_char lsa_type
,
483 u_char opaque_type
, u_int32_t opaque_id
)
488 /* Only opaque LSA can be deleted */
489 if (!IS_OPAQUE_LSA (lsa_type
))
491 fprintf (stderr
, "Cannot delete non-opaque LSA type %d\n", lsa_type
);
492 return OSPF_API_ILLEGALLSATYPE
;
495 /* opaque_id is in host byte order and will be converted
496 * to network byte order by new_msg_delete_request */
497 msg
= new_msg_delete_request (ospf_apiclient_get_seqnr (),
498 area_id
, lsa_type
, opaque_type
, opaque_id
);
500 rc
= ospf_apiclient_send_request (oclient
, msg
);
504 /* -----------------------------------------------------------
505 * Followings are handlers for messages from OSPF daemon
506 * -----------------------------------------------------------
510 ospf_apiclient_handle_ready (struct ospf_apiclient
*oclient
, struct msg
*msg
)
512 struct msg_ready_notify
*r
;
513 r
= (struct msg_ready_notify
*) STREAM_DATA (msg
->s
);
515 /* Invoke registered callback function. */
516 if (oclient
->ready_notify
)
518 (oclient
->ready_notify
) (r
->lsa_type
, r
->opaque_type
, r
->addr
);
523 ospf_apiclient_handle_new_if (struct ospf_apiclient
*oclient
, struct msg
*msg
)
525 struct msg_new_if
*n
;
526 n
= (struct msg_new_if
*) STREAM_DATA (msg
->s
);
528 /* Invoke registered callback function. */
531 (oclient
->new_if
) (n
->ifaddr
, n
->area_id
);
536 ospf_apiclient_handle_del_if (struct ospf_apiclient
*oclient
, struct msg
*msg
)
538 struct msg_del_if
*d
;
539 d
= (struct msg_del_if
*) STREAM_DATA (msg
->s
);
541 /* Invoke registered callback function. */
544 (oclient
->del_if
) (d
->ifaddr
);
549 ospf_apiclient_handle_ism_change (struct ospf_apiclient
*oclient
,
552 struct msg_ism_change
*m
;
553 m
= (struct msg_ism_change
*) STREAM_DATA (msg
->s
);
555 /* Invoke registered callback function. */
556 if (oclient
->ism_change
)
558 (oclient
->ism_change
) (m
->ifaddr
, m
->area_id
, m
->status
);
564 ospf_apiclient_handle_nsm_change (struct ospf_apiclient
*oclient
,
567 struct msg_nsm_change
*m
;
568 m
= (struct msg_nsm_change
*) STREAM_DATA (msg
->s
);
570 /* Invoke registered callback function. */
571 if (oclient
->nsm_change
)
573 (oclient
->nsm_change
) (m
->ifaddr
, m
->nbraddr
, m
->router_id
, m
->status
);
578 ospf_apiclient_handle_lsa_update (struct ospf_apiclient
*oclient
,
581 struct msg_lsa_change_notify
*cn
;
582 struct lsa_header
*lsa
;
585 cn
= (struct msg_lsa_change_notify
*) STREAM_DATA (msg
->s
);
587 /* Extract LSA from message */
588 lsalen
= ntohs (cn
->data
.length
);
589 lsa
= XMALLOC (MTYPE_OSPF_APICLIENT
, lsalen
);
592 fprintf (stderr
, "LSA update: Cannot allocate memory for LSA\n");
595 memcpy (lsa
, &(cn
->data
), lsalen
);
597 /* Invoke registered update callback function */
598 if (oclient
->update_notify
)
600 (oclient
->update_notify
) (cn
->ifaddr
, cn
->area_id
,
601 cn
->is_self_originated
, lsa
);
604 /* free memory allocated by ospf apiclient library */
605 XFREE (MTYPE_OSPF_APICLIENT
, lsa
);
609 ospf_apiclient_handle_lsa_delete (struct ospf_apiclient
*oclient
,
612 struct msg_lsa_change_notify
*cn
;
613 struct lsa_header
*lsa
;
616 cn
= (struct msg_lsa_change_notify
*) STREAM_DATA (msg
->s
);
618 /* Extract LSA from message */
619 lsalen
= ntohs (cn
->data
.length
);
620 lsa
= XMALLOC (MTYPE_OSPF_APICLIENT
, lsalen
);
623 fprintf (stderr
, "LSA delete: Cannot allocate memory for LSA\n");
626 memcpy (lsa
, &(cn
->data
), lsalen
);
628 /* Invoke registered update callback function */
629 if (oclient
->delete_notify
)
631 (oclient
->delete_notify
) (cn
->ifaddr
, cn
->area_id
,
632 cn
->is_self_originated
, lsa
);
635 /* free memory allocated by ospf apiclient library */
636 XFREE (MTYPE_OSPF_APICLIENT
, lsa
);
640 ospf_apiclient_msghandle (struct ospf_apiclient
*oclient
, struct msg
*msg
)
642 /* Call message handler function. */
643 switch (msg
->hdr
.msgtype
)
645 case MSG_READY_NOTIFY
:
646 ospf_apiclient_handle_ready (oclient
, msg
);
649 ospf_apiclient_handle_new_if (oclient
, msg
);
652 ospf_apiclient_handle_del_if (oclient
, msg
);
655 ospf_apiclient_handle_ism_change (oclient
, msg
);
658 ospf_apiclient_handle_nsm_change (oclient
, msg
);
660 case MSG_LSA_UPDATE_NOTIFY
:
661 ospf_apiclient_handle_lsa_update (oclient
, msg
);
663 case MSG_LSA_DELETE_NOTIFY
:
664 ospf_apiclient_handle_lsa_delete (oclient
, msg
);
667 fprintf (stderr
, "ospf_apiclient_read: Unknown message type: %d\n",
673 /* -----------------------------------------------------------
674 * Callback handler registration
675 * -----------------------------------------------------------
679 ospf_apiclient_register_callback (struct ospf_apiclient
*oclient
,
680 void (*ready_notify
) (u_char lsa_type
,
682 struct in_addr addr
),
683 void (*new_if
) (struct in_addr ifaddr
,
684 struct in_addr area_id
),
685 void (*del_if
) (struct in_addr ifaddr
),
686 void (*ism_change
) (struct in_addr ifaddr
,
687 struct in_addr area_id
,
689 void (*nsm_change
) (struct in_addr ifaddr
,
690 struct in_addr nbraddr
,
694 void (*update_notify
) (struct in_addr
701 void (*delete_notify
) (struct in_addr
710 assert (update_notify
);
712 /* Register callback function */
713 oclient
->ready_notify
= ready_notify
;
714 oclient
->new_if
= new_if
;
715 oclient
->del_if
= del_if
;
716 oclient
->ism_change
= ism_change
;
717 oclient
->nsm_change
= nsm_change
;
718 oclient
->update_notify
= update_notify
;
719 oclient
->delete_notify
= delete_notify
;
722 /* -----------------------------------------------------------
723 * Asynchronous message handling
724 * -----------------------------------------------------------
728 ospf_apiclient_handle_async (struct ospf_apiclient
*oclient
)
733 msg
= msg_read (oclient
->fd_async
);
737 /* Connection broke down */
742 ospf_apiclient_msghandle (oclient
, msg
);
744 /* Don't forget to free this message */