]>
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 /* *sigh* ... can't find a better way to hammer this into automake */
53 #include "ospfd/ospf_dump_api.c"
54 #include "ospfd/ospf_api.c"
56 DEFINE_MGROUP(OSPFCLIENT
, "libospfapiclient")
57 DEFINE_MTYPE_STATIC(OSPFCLIENT
, OSPF_APICLIENT
, "OSPF-API client")
59 /* Backlog for listen */
62 /* -----------------------------------------------------------
63 * Forward declarations
64 * -----------------------------------------------------------
67 void ospf_apiclient_handle_reply (struct ospf_apiclient
*oclient
,
69 void ospf_apiclient_handle_update_notify (struct ospf_apiclient
*oclient
,
71 void ospf_apiclient_handle_delete_notify (struct ospf_apiclient
*oclient
,
74 /* -----------------------------------------------------------
76 * -----------------------------------------------------------
80 ospf_apiclient_getport (void)
82 struct servent
*sp
= getservbyname ("ospfapi", "tcp");
84 return sp
? ntohs (sp
->s_port
) : OSPF_API_SYNC_PORT
;
87 /* -----------------------------------------------------------
88 * Followings are functions for connection management
89 * -----------------------------------------------------------
92 struct ospf_apiclient
*
93 ospf_apiclient_connect (char *host
, int syncport
)
95 struct sockaddr_in myaddr_sync
;
96 struct sockaddr_in myaddr_async
;
97 struct sockaddr_in peeraddr
;
99 struct ospf_apiclient
*new;
101 unsigned int peeraddrlen
;
102 int async_server_sock
;
107 /* There are two connections between the client and the server.
108 First the client opens a connection for synchronous requests/replies
109 to the server. The server will accept this connection and
110 as a reaction open a reverse connection channel for
111 asynchronous messages. */
113 async_server_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
114 if (async_server_sock
< 0)
117 "ospf_apiclient_connect: creating async socket failed\n");
121 /* Prepare socket for asynchronous messages */
122 /* Initialize async address structure */
123 memset (&myaddr_async
, 0, sizeof (struct sockaddr_in
));
124 myaddr_async
.sin_family
= AF_INET
;
125 myaddr_async
.sin_addr
.s_addr
= htonl (INADDR_ANY
);
126 myaddr_async
.sin_port
= htons (syncport
+1);
127 size
= sizeof (struct sockaddr_in
);
128 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
129 myaddr_async
.sin_len
= size
;
130 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
132 /* This is a server socket, reuse addr and port */
133 ret
= setsockopt (async_server_sock
, SOL_SOCKET
,
134 SO_REUSEADDR
, (void *) &on
, sizeof (on
));
137 fprintf (stderr
, "ospf_apiclient_connect: SO_REUSEADDR failed\n");
138 close (async_server_sock
);
143 ret
= setsockopt (async_server_sock
, SOL_SOCKET
, SO_REUSEPORT
,
144 (void *) &on
, sizeof (on
));
147 fprintf (stderr
, "ospf_apiclient_connect: SO_REUSEPORT failed\n");
148 close (async_server_sock
);
151 #endif /* SO_REUSEPORT */
153 /* Bind socket to address structure */
154 ret
= bind (async_server_sock
, (struct sockaddr
*) &myaddr_async
, size
);
157 fprintf (stderr
, "ospf_apiclient_connect: bind async socket failed\n");
158 close (async_server_sock
);
162 /* Wait for reverse channel connection establishment from server */
163 ret
= listen (async_server_sock
, BACKLOG
);
166 fprintf (stderr
, "ospf_apiclient_connect: listen: %s\n", safe_strerror (errno
));
167 close (async_server_sock
);
171 /* Make connection for synchronous requests and connect to server */
172 /* Resolve address of server */
173 hp
= gethostbyname (host
);
176 fprintf (stderr
, "ospf_apiclient_connect: no such host %s\n", host
);
177 close (async_server_sock
);
181 fd1
= socket (AF_INET
, SOCK_STREAM
, 0);
185 "ospf_apiclient_connect: creating sync socket failed\n");
190 /* Reuse addr and port */
191 ret
= setsockopt (fd1
, SOL_SOCKET
,
192 SO_REUSEADDR
, (void *) &on
, sizeof (on
));
195 fprintf (stderr
, "ospf_apiclient_connect: SO_REUSEADDR failed\n");
201 ret
= setsockopt (fd1
, SOL_SOCKET
, SO_REUSEPORT
,
202 (void *) &on
, sizeof (on
));
205 fprintf (stderr
, "ospf_apiclient_connect: SO_REUSEPORT failed\n");
209 #endif /* SO_REUSEPORT */
212 /* Bind sync socket to address structure. This is needed since we
213 want the sync port number on a fixed port number. The reverse
214 async channel will be at this port+1 */
216 memset (&myaddr_sync
, 0, sizeof (struct sockaddr_in
));
217 myaddr_sync
.sin_family
= AF_INET
;
218 myaddr_sync
.sin_port
= htons (syncport
);
219 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
220 myaddr_sync
.sin_len
= sizeof (struct sockaddr_in
);
221 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
223 ret
= bind (fd1
, (struct sockaddr
*) &myaddr_sync
, size
);
226 fprintf (stderr
, "ospf_apiclient_connect: bind sync socket failed\n");
231 /* Prepare address structure for connect */
232 memcpy (&myaddr_sync
.sin_addr
, hp
->h_addr
, hp
->h_length
);
233 myaddr_sync
.sin_family
= AF_INET
;
234 myaddr_sync
.sin_port
= htons(ospf_apiclient_getport ());
235 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
236 myaddr_sync
.sin_len
= sizeof (struct sockaddr_in
);
237 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
239 /* Now establish synchronous channel with OSPF daemon */
240 ret
= connect (fd1
, (struct sockaddr
*) &myaddr_sync
,
241 sizeof (struct sockaddr_in
));
244 fprintf (stderr
, "ospf_apiclient_connect: sync connect failed\n");
245 close (async_server_sock
);
250 /* Accept reverse connection */
251 peeraddrlen
= sizeof (struct sockaddr_in
);
252 memset (&peeraddr
, 0, peeraddrlen
);
255 accept (async_server_sock
, (struct sockaddr
*) &peeraddr
, &peeraddrlen
);
258 fprintf (stderr
, "ospf_apiclient_connect: accept async failed\n");
259 close (async_server_sock
);
264 /* Server socket is not needed anymore since we are not accepting more
266 close (async_server_sock
);
268 /* Create new client-side instance */
269 new = XCALLOC (MTYPE_OSPF_APICLIENT
, sizeof (struct ospf_apiclient
));
271 /* Initialize socket descriptors for sync and async channels */
279 ospf_apiclient_close (struct ospf_apiclient
*oclient
)
282 if (oclient
->fd_sync
>= 0)
284 close (oclient
->fd_sync
);
287 if (oclient
->fd_async
>= 0)
289 close (oclient
->fd_async
);
292 /* Free client structure */
293 XFREE (MTYPE_OSPF_APICLIENT
, oclient
);
297 /* -----------------------------------------------------------
298 * Followings are functions to send a request to OSPFd
299 * -----------------------------------------------------------
302 /* Send synchronous request, wait for reply */
304 ospf_apiclient_send_request (struct ospf_apiclient
*oclient
, struct msg
*msg
)
307 struct msg_reply
*msgreply
;
310 /* NB: Given "msg" is freed inside this function. */
312 /* Remember the sequence number of the request */
313 reqseq
= ntohl (msg
->hdr
.msgseq
);
315 /* Write message to OSPFd */
316 rc
= msg_write (oclient
->fd_sync
, msg
);
324 /* Wait for reply *//* NB: New "msg" is allocated by "msg_read()". */
325 msg
= msg_read (oclient
->fd_sync
);
329 assert (msg
->hdr
.msgtype
== MSG_REPLY
);
330 assert (ntohl (msg
->hdr
.msgseq
) == reqseq
);
332 msgreply
= (struct msg_reply
*) STREAM_DATA (msg
->s
);
333 rc
= msgreply
->errcode
;
340 /* -----------------------------------------------------------
342 * -----------------------------------------------------------
346 ospf_apiclient_get_seqnr (void)
348 static u_int32_t seqnr
= MIN_SEQ
;
352 /* Increment sequence number */
364 /* -----------------------------------------------------------
365 * API to access OSPF daemon by client applications.
366 * -----------------------------------------------------------
370 * Synchronous request to register opaque type.
373 ospf_apiclient_register_opaque_type (struct ospf_apiclient
*cl
,
374 u_char ltype
, u_char otype
)
379 /* just put 1 as a sequence number. */
380 msg
= new_msg_register_opaque_type (ospf_apiclient_get_seqnr (),
384 fprintf (stderr
, "new_msg_register_opaque_type failed\n");
388 rc
= ospf_apiclient_send_request (cl
, msg
);
393 * Synchronous request to synchronize with OSPF's LSDB.
394 * Two steps required: register_event in order to get
395 * dynamic updates and LSDB_Sync.
398 ospf_apiclient_sync_lsdb (struct ospf_apiclient
*oclient
)
402 struct lsa_filter_type filter
;
404 filter
.typemask
= 0xFFFF; /* all LSAs */
405 filter
.origin
= ANY_ORIGIN
;
406 filter
.num_areas
= 0; /* all Areas. */
408 msg
= new_msg_register_event (ospf_apiclient_get_seqnr (), &filter
);
411 fprintf (stderr
, "new_msg_register_event failed\n");
414 rc
= ospf_apiclient_send_request (oclient
, msg
);
419 msg
= new_msg_sync_lsdb (ospf_apiclient_get_seqnr (), &filter
);
422 fprintf (stderr
, "new_msg_sync_lsdb failed\n");
425 rc
= ospf_apiclient_send_request (oclient
, msg
);
432 * Synchronous request to originate or update an LSA.
436 ospf_apiclient_lsa_originate (struct ospf_apiclient
*oclient
,
437 struct in_addr ifaddr
,
438 struct in_addr area_id
,
440 u_char opaque_type
, u_int32_t opaque_id
,
441 void *opaquedata
, int opaquelen
)
445 u_char buf
[OSPF_MAX_LSA_SIZE
];
446 struct lsa_header
*lsah
;
450 /* We can only originate opaque LSAs */
451 if (!IS_OPAQUE_LSA (lsa_type
))
453 fprintf (stderr
, "Cannot originate non-opaque LSA type %d\n", lsa_type
);
454 return OSPF_API_ILLEGALLSATYPE
;
457 /* Make a new LSA from parameters */
458 lsah
= (struct lsa_header
*) buf
;
461 lsah
->type
= lsa_type
;
463 tmp
= SET_OPAQUE_LSID (opaque_type
, opaque_id
);
464 lsah
->id
.s_addr
= htonl (tmp
);
465 lsah
->adv_router
.s_addr
= 0;
468 lsah
->length
= htons (sizeof (struct lsa_header
) + opaquelen
);
470 memcpy (((u_char
*) lsah
) + sizeof (struct lsa_header
), opaquedata
,
473 msg
= new_msg_originate_request (ospf_apiclient_get_seqnr (),
474 ifaddr
, area_id
, lsah
);
477 fprintf (stderr
, "new_msg_originate_request failed\n");
478 return OSPF_API_NOMEMORY
;
481 rc
= ospf_apiclient_send_request (oclient
, msg
);
486 ospf_apiclient_lsa_delete (struct ospf_apiclient
*oclient
,
487 struct in_addr area_id
, u_char lsa_type
,
488 u_char opaque_type
, u_int32_t opaque_id
)
493 /* Only opaque LSA can be deleted */
494 if (!IS_OPAQUE_LSA (lsa_type
))
496 fprintf (stderr
, "Cannot delete non-opaque LSA type %d\n", lsa_type
);
497 return OSPF_API_ILLEGALLSATYPE
;
500 /* opaque_id is in host byte order and will be converted
501 * to network byte order by new_msg_delete_request */
502 msg
= new_msg_delete_request (ospf_apiclient_get_seqnr (),
503 area_id
, lsa_type
, opaque_type
, opaque_id
);
505 rc
= ospf_apiclient_send_request (oclient
, msg
);
509 /* -----------------------------------------------------------
510 * Followings are handlers for messages from OSPF daemon
511 * -----------------------------------------------------------
515 ospf_apiclient_handle_ready (struct ospf_apiclient
*oclient
, struct msg
*msg
)
517 struct msg_ready_notify
*r
;
518 r
= (struct msg_ready_notify
*) STREAM_DATA (msg
->s
);
520 /* Invoke registered callback function. */
521 if (oclient
->ready_notify
)
523 (oclient
->ready_notify
) (r
->lsa_type
, r
->opaque_type
, r
->addr
);
528 ospf_apiclient_handle_new_if (struct ospf_apiclient
*oclient
, struct msg
*msg
)
530 struct msg_new_if
*n
;
531 n
= (struct msg_new_if
*) STREAM_DATA (msg
->s
);
533 /* Invoke registered callback function. */
536 (oclient
->new_if
) (n
->ifaddr
, n
->area_id
);
541 ospf_apiclient_handle_del_if (struct ospf_apiclient
*oclient
, struct msg
*msg
)
543 struct msg_del_if
*d
;
544 d
= (struct msg_del_if
*) STREAM_DATA (msg
->s
);
546 /* Invoke registered callback function. */
549 (oclient
->del_if
) (d
->ifaddr
);
554 ospf_apiclient_handle_ism_change (struct ospf_apiclient
*oclient
,
557 struct msg_ism_change
*m
;
558 m
= (struct msg_ism_change
*) STREAM_DATA (msg
->s
);
560 /* Invoke registered callback function. */
561 if (oclient
->ism_change
)
563 (oclient
->ism_change
) (m
->ifaddr
, m
->area_id
, m
->status
);
569 ospf_apiclient_handle_nsm_change (struct ospf_apiclient
*oclient
,
572 struct msg_nsm_change
*m
;
573 m
= (struct msg_nsm_change
*) STREAM_DATA (msg
->s
);
575 /* Invoke registered callback function. */
576 if (oclient
->nsm_change
)
578 (oclient
->nsm_change
) (m
->ifaddr
, m
->nbraddr
, m
->router_id
, m
->status
);
583 ospf_apiclient_handle_lsa_update (struct ospf_apiclient
*oclient
,
586 struct msg_lsa_change_notify
*cn
;
587 struct lsa_header
*lsa
;
590 cn
= (struct msg_lsa_change_notify
*) STREAM_DATA (msg
->s
);
592 /* Extract LSA from message */
593 lsalen
= ntohs (cn
->data
.length
);
594 lsa
= XMALLOC (MTYPE_OSPF_APICLIENT
, lsalen
);
597 fprintf (stderr
, "LSA update: Cannot allocate memory for LSA\n");
600 memcpy (lsa
, &(cn
->data
), lsalen
);
602 /* Invoke registered update callback function */
603 if (oclient
->update_notify
)
605 (oclient
->update_notify
) (cn
->ifaddr
, cn
->area_id
,
606 cn
->is_self_originated
, lsa
);
609 /* free memory allocated by ospf apiclient library */
610 XFREE (MTYPE_OSPF_APICLIENT
, lsa
);
614 ospf_apiclient_handle_lsa_delete (struct ospf_apiclient
*oclient
,
617 struct msg_lsa_change_notify
*cn
;
618 struct lsa_header
*lsa
;
621 cn
= (struct msg_lsa_change_notify
*) STREAM_DATA (msg
->s
);
623 /* Extract LSA from message */
624 lsalen
= ntohs (cn
->data
.length
);
625 lsa
= XMALLOC (MTYPE_OSPF_APICLIENT
, lsalen
);
628 fprintf (stderr
, "LSA delete: Cannot allocate memory for LSA\n");
631 memcpy (lsa
, &(cn
->data
), lsalen
);
633 /* Invoke registered update callback function */
634 if (oclient
->delete_notify
)
636 (oclient
->delete_notify
) (cn
->ifaddr
, cn
->area_id
,
637 cn
->is_self_originated
, lsa
);
640 /* free memory allocated by ospf apiclient library */
641 XFREE (MTYPE_OSPF_APICLIENT
, lsa
);
645 ospf_apiclient_msghandle (struct ospf_apiclient
*oclient
, struct msg
*msg
)
647 /* Call message handler function. */
648 switch (msg
->hdr
.msgtype
)
650 case MSG_READY_NOTIFY
:
651 ospf_apiclient_handle_ready (oclient
, msg
);
654 ospf_apiclient_handle_new_if (oclient
, msg
);
657 ospf_apiclient_handle_del_if (oclient
, msg
);
660 ospf_apiclient_handle_ism_change (oclient
, msg
);
663 ospf_apiclient_handle_nsm_change (oclient
, msg
);
665 case MSG_LSA_UPDATE_NOTIFY
:
666 ospf_apiclient_handle_lsa_update (oclient
, msg
);
668 case MSG_LSA_DELETE_NOTIFY
:
669 ospf_apiclient_handle_lsa_delete (oclient
, msg
);
672 fprintf (stderr
, "ospf_apiclient_read: Unknown message type: %d\n",
678 /* -----------------------------------------------------------
679 * Callback handler registration
680 * -----------------------------------------------------------
684 ospf_apiclient_register_callback (struct ospf_apiclient
*oclient
,
685 void (*ready_notify
) (u_char lsa_type
,
687 struct in_addr addr
),
688 void (*new_if
) (struct in_addr ifaddr
,
689 struct in_addr area_id
),
690 void (*del_if
) (struct in_addr ifaddr
),
691 void (*ism_change
) (struct in_addr ifaddr
,
692 struct in_addr area_id
,
694 void (*nsm_change
) (struct in_addr ifaddr
,
695 struct in_addr nbraddr
,
699 void (*update_notify
) (struct in_addr
706 void (*delete_notify
) (struct in_addr
715 assert (update_notify
);
717 /* Register callback function */
718 oclient
->ready_notify
= ready_notify
;
719 oclient
->new_if
= new_if
;
720 oclient
->del_if
= del_if
;
721 oclient
->ism_change
= ism_change
;
722 oclient
->nsm_change
= nsm_change
;
723 oclient
->update_notify
= update_notify
;
724 oclient
->delete_notify
= delete_notify
;
727 /* -----------------------------------------------------------
728 * Asynchronous message handling
729 * -----------------------------------------------------------
733 ospf_apiclient_handle_async (struct ospf_apiclient
*oclient
)
738 msg
= msg_read (oclient
->fd_async
);
742 /* Connection broke down */
747 ospf_apiclient_msghandle (oclient
, msg
);
749 /* Don't forget to free this message */