]>
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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <lib/version.h>
38 /* work around gcc bug 69981, disable MTYPEs in libospf */
39 #define _QUAGGA_OSPF_MEMORY_H
41 #include "ospfd/ospfd.h"
42 #include "ospfd/ospf_interface.h"
43 #include "ospfd/ospf_asbr.h"
44 #include "ospfd/ospf_lsa.h"
45 #include "ospfd/ospf_opaque.h"
46 #include "ospfd/ospf_lsdb.h"
47 #include "ospfd/ospf_neighbor.h"
48 #include "ospfd/ospf_dump.h"
49 #include "ospfd/ospf_route.h"
50 #include "ospfd/ospf_zebra.h"
51 #include "ospfd/ospf_api.h"
53 #include "ospf_apiclient.h"
55 /* *sigh* ... can't find a better way to hammer this into automake */
56 #include "ospfd/ospf_dump_api.c"
57 #include "ospfd/ospf_api.c"
59 DEFINE_MGROUP(OSPFCLIENT
, "libospfapiclient")
60 DEFINE_MTYPE_STATIC(OSPFCLIENT
, OSPF_APICLIENT
, "OSPF-API client")
62 /* Backlog for listen */
65 /* -----------------------------------------------------------
66 * Forward declarations
67 * -----------------------------------------------------------
70 void ospf_apiclient_handle_reply(struct ospf_apiclient
*oclient
,
72 void ospf_apiclient_handle_update_notify(struct ospf_apiclient
*oclient
,
74 void ospf_apiclient_handle_delete_notify(struct ospf_apiclient
*oclient
,
77 /* -----------------------------------------------------------
79 * -----------------------------------------------------------
82 static unsigned short ospf_apiclient_getport(void)
84 struct servent
*sp
= getservbyname("ospfapi", "tcp");
86 return sp
? ntohs(sp
->s_port
) : OSPF_API_SYNC_PORT
;
89 /* -----------------------------------------------------------
90 * Followings are functions for connection management
91 * -----------------------------------------------------------
94 struct ospf_apiclient
*ospf_apiclient_connect(char *host
, int syncport
)
96 struct sockaddr_in myaddr_sync
;
97 struct sockaddr_in myaddr_async
;
98 struct sockaddr_in peeraddr
;
100 struct ospf_apiclient
*new;
102 unsigned int peeraddrlen
;
103 int async_server_sock
;
108 /* There are two connections between the client and the server.
109 First the client opens a connection for synchronous requests/replies
110 to the server. The server will accept this connection and
111 as a reaction open a reverse connection channel for
112 asynchronous messages. */
114 async_server_sock
= socket(AF_INET
, SOCK_STREAM
, 0);
115 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
, SO_REUSEADDR
,
134 (void *)&on
, sizeof(on
));
137 "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 "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 "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
);
165 fprintf(stderr
, "ospf_apiclient_connect: listen: %s\n",
166 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
);
175 fprintf(stderr
, "ospf_apiclient_connect: no such host %s\n",
177 close(async_server_sock
);
181 fd1
= socket(AF_INET
, SOCK_STREAM
, 0);
183 close(async_server_sock
);
185 "ospf_apiclient_connect: creating sync socket failed\n");
190 /* Reuse addr and port */
191 ret
= setsockopt(fd1
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&on
,
195 "ospf_apiclient_connect: SO_REUSEADDR failed\n");
197 close(async_server_sock
);
202 ret
= setsockopt(fd1
, SOL_SOCKET
, SO_REUSEPORT
, (void *)&on
,
206 "ospf_apiclient_connect: SO_REUSEPORT failed\n");
208 close(async_server_sock
);
211 #endif /* SO_REUSEPORT */
214 /* Bind sync socket to address structure. This is needed since we
215 want the sync port number on a fixed port number. The reverse
216 async channel will be at this port+1 */
218 memset(&myaddr_sync
, 0, sizeof(struct sockaddr_in
));
219 myaddr_sync
.sin_family
= AF_INET
;
220 myaddr_sync
.sin_port
= htons(syncport
);
221 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
222 myaddr_sync
.sin_len
= sizeof(struct sockaddr_in
);
223 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
225 ret
= bind(fd1
, (struct sockaddr
*)&myaddr_sync
, size
);
228 "ospf_apiclient_connect: bind sync socket failed\n");
230 close(async_server_sock
);
234 /* Prepare address structure for connect */
235 memcpy(&myaddr_sync
.sin_addr
, hp
->h_addr
, hp
->h_length
);
236 myaddr_sync
.sin_family
= AF_INET
;
237 myaddr_sync
.sin_port
= htons(ospf_apiclient_getport());
238 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
239 myaddr_sync
.sin_len
= sizeof(struct sockaddr_in
);
240 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
242 /* Now establish synchronous channel with OSPF daemon */
243 ret
= connect(fd1
, (struct sockaddr
*)&myaddr_sync
,
244 sizeof(struct sockaddr_in
));
247 "ospf_apiclient_connect: sync connect failed\n");
248 close(async_server_sock
);
253 /* Accept reverse connection */
254 peeraddrlen
= sizeof(struct sockaddr_in
);
255 memset(&peeraddr
, 0, peeraddrlen
);
257 fd2
= accept(async_server_sock
, (struct sockaddr
*)&peeraddr
,
261 "ospf_apiclient_connect: accept async failed\n");
262 close(async_server_sock
);
268 /* Server socket is not needed anymore since we are not accepting more
270 close(async_server_sock
);
272 /* Create new client-side instance */
273 new = XCALLOC(MTYPE_OSPF_APICLIENT
, sizeof(struct ospf_apiclient
));
275 /* Initialize socket descriptors for sync and async channels */
282 int ospf_apiclient_close(struct ospf_apiclient
*oclient
)
285 if (oclient
->fd_sync
>= 0) {
286 close(oclient
->fd_sync
);
289 if (oclient
->fd_async
>= 0) {
290 close(oclient
->fd_async
);
293 /* Free client structure */
294 XFREE(MTYPE_OSPF_APICLIENT
, oclient
);
298 /* -----------------------------------------------------------
299 * Followings are functions to send a request to OSPFd
300 * -----------------------------------------------------------
303 /* Send synchronous request, wait for reply */
304 static int ospf_apiclient_send_request(struct ospf_apiclient
*oclient
,
308 struct msg_reply
*msgreply
;
311 /* NB: Given "msg" is freed inside this function. */
313 /* Remember the sequence number of the request */
314 reqseq
= ntohl(msg
->hdr
.msgseq
);
316 /* Write message to OSPFd */
317 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 * -----------------------------------------------------------
345 static uint32_t ospf_apiclient_get_seqnr(void)
347 static uint32_t seqnr
= MIN_SEQ
;
351 /* Increment sequence number */
352 if (seqnr
< MAX_SEQ
) {
360 /* -----------------------------------------------------------
361 * API to access OSPF daemon by client applications.
362 * -----------------------------------------------------------
366 * Synchronous request to register opaque type.
368 int ospf_apiclient_register_opaque_type(struct ospf_apiclient
*cl
,
369 uint8_t ltype
, uint8_t otype
)
374 /* just put 1 as a sequence number. */
375 msg
= new_msg_register_opaque_type(ospf_apiclient_get_seqnr(), ltype
,
378 fprintf(stderr
, "new_msg_register_opaque_type failed\n");
382 rc
= ospf_apiclient_send_request(cl
, msg
);
387 * Synchronous request to synchronize with OSPF's LSDB.
388 * Two steps required: register_event in order to get
389 * dynamic updates and LSDB_Sync.
391 int ospf_apiclient_sync_lsdb(struct ospf_apiclient
*oclient
)
395 struct lsa_filter_type filter
;
397 filter
.typemask
= 0xFFFF; /* all LSAs */
398 filter
.origin
= ANY_ORIGIN
;
399 filter
.num_areas
= 0; /* all Areas. */
401 msg
= new_msg_register_event(ospf_apiclient_get_seqnr(), &filter
);
403 fprintf(stderr
, "new_msg_register_event failed\n");
406 rc
= ospf_apiclient_send_request(oclient
, msg
);
411 msg
= new_msg_sync_lsdb(ospf_apiclient_get_seqnr(), &filter
);
413 fprintf(stderr
, "new_msg_sync_lsdb failed\n");
416 rc
= ospf_apiclient_send_request(oclient
, msg
);
423 * Synchronous request to originate or update an LSA.
426 int ospf_apiclient_lsa_originate(struct ospf_apiclient
*oclient
,
427 struct in_addr ifaddr
, struct in_addr area_id
,
428 uint8_t lsa_type
, uint8_t opaque_type
,
429 uint32_t opaque_id
, void *opaquedata
,
434 uint8_t buf
[OSPF_MAX_LSA_SIZE
];
435 struct lsa_header
*lsah
;
439 /* We can only originate opaque LSAs */
440 if (!IS_OPAQUE_LSA(lsa_type
)) {
441 fprintf(stderr
, "Cannot originate non-opaque LSA type %d\n",
443 return OSPF_API_ILLEGALLSATYPE
;
446 /* Make a new LSA from parameters */
447 lsah
= (struct lsa_header
*)buf
;
450 lsah
->type
= lsa_type
;
452 tmp
= SET_OPAQUE_LSID(opaque_type
, opaque_id
);
453 lsah
->id
.s_addr
= htonl(tmp
);
454 lsah
->adv_router
.s_addr
= 0;
457 lsah
->length
= htons(sizeof(struct lsa_header
) + opaquelen
);
459 memcpy(((uint8_t *)lsah
) + sizeof(struct lsa_header
), opaquedata
,
462 msg
= new_msg_originate_request(ospf_apiclient_get_seqnr(), ifaddr
,
465 fprintf(stderr
, "new_msg_originate_request failed\n");
466 return OSPF_API_NOMEMORY
;
469 rc
= ospf_apiclient_send_request(oclient
, msg
);
473 int ospf_apiclient_lsa_delete(struct ospf_apiclient
*oclient
,
474 struct in_addr area_id
, uint8_t lsa_type
,
475 uint8_t opaque_type
, uint32_t opaque_id
)
480 /* Only opaque LSA can be deleted */
481 if (!IS_OPAQUE_LSA(lsa_type
)) {
482 fprintf(stderr
, "Cannot delete non-opaque LSA type %d\n",
484 return OSPF_API_ILLEGALLSATYPE
;
487 /* opaque_id is in host byte order and will be converted
488 * to network byte order by new_msg_delete_request */
489 msg
= new_msg_delete_request(ospf_apiclient_get_seqnr(), area_id
,
490 lsa_type
, opaque_type
, opaque_id
);
492 rc
= ospf_apiclient_send_request(oclient
, msg
);
496 /* -----------------------------------------------------------
497 * Followings are handlers for messages from OSPF daemon
498 * -----------------------------------------------------------
501 static void ospf_apiclient_handle_ready(struct ospf_apiclient
*oclient
,
504 struct msg_ready_notify
*r
;
505 r
= (struct msg_ready_notify
*)STREAM_DATA(msg
->s
);
507 /* Invoke registered callback function. */
508 if (oclient
->ready_notify
) {
509 (oclient
->ready_notify
)(r
->lsa_type
, r
->opaque_type
, r
->addr
);
513 static void ospf_apiclient_handle_new_if(struct ospf_apiclient
*oclient
,
516 struct msg_new_if
*n
;
517 n
= (struct msg_new_if
*)STREAM_DATA(msg
->s
);
519 /* Invoke registered callback function. */
520 if (oclient
->new_if
) {
521 (oclient
->new_if
)(n
->ifaddr
, n
->area_id
);
525 static void ospf_apiclient_handle_del_if(struct ospf_apiclient
*oclient
,
528 struct msg_del_if
*d
;
529 d
= (struct msg_del_if
*)STREAM_DATA(msg
->s
);
531 /* Invoke registered callback function. */
532 if (oclient
->del_if
) {
533 (oclient
->del_if
)(d
->ifaddr
);
537 static void ospf_apiclient_handle_ism_change(struct ospf_apiclient
*oclient
,
540 struct msg_ism_change
*m
;
541 m
= (struct msg_ism_change
*)STREAM_DATA(msg
->s
);
543 /* Invoke registered callback function. */
544 if (oclient
->ism_change
) {
545 (oclient
->ism_change
)(m
->ifaddr
, m
->area_id
, m
->status
);
549 static void ospf_apiclient_handle_nsm_change(struct ospf_apiclient
*oclient
,
552 struct msg_nsm_change
*m
;
553 m
= (struct msg_nsm_change
*)STREAM_DATA(msg
->s
);
555 /* Invoke registered callback function. */
556 if (oclient
->nsm_change
) {
557 (oclient
->nsm_change
)(m
->ifaddr
, m
->nbraddr
, m
->router_id
,
562 static void ospf_apiclient_handle_lsa_update(struct ospf_apiclient
*oclient
,
565 struct msg_lsa_change_notify
*cn
;
566 struct lsa_header
*lsa
;
569 cn
= (struct msg_lsa_change_notify
*)STREAM_DATA(msg
->s
);
571 /* Extract LSA from message */
572 lsalen
= ntohs(cn
->data
.length
);
573 lsa
= XMALLOC(MTYPE_OSPF_APICLIENT
, lsalen
);
575 memcpy(lsa
, &(cn
->data
), lsalen
);
577 /* Invoke registered update callback function */
578 if (oclient
->update_notify
) {
579 (oclient
->update_notify
)(cn
->ifaddr
, cn
->area_id
,
580 cn
->is_self_originated
, lsa
);
583 /* free memory allocated by ospf apiclient library */
584 XFREE(MTYPE_OSPF_APICLIENT
, lsa
);
587 static void ospf_apiclient_handle_lsa_delete(struct ospf_apiclient
*oclient
,
590 struct msg_lsa_change_notify
*cn
;
591 struct lsa_header
*lsa
;
594 cn
= (struct msg_lsa_change_notify
*)STREAM_DATA(msg
->s
);
596 /* Extract LSA from message */
597 lsalen
= ntohs(cn
->data
.length
);
598 lsa
= XMALLOC(MTYPE_OSPF_APICLIENT
, lsalen
);
600 memcpy(lsa
, &(cn
->data
), lsalen
);
602 /* Invoke registered update callback function */
603 if (oclient
->delete_notify
) {
604 (oclient
->delete_notify
)(cn
->ifaddr
, cn
->area_id
,
605 cn
->is_self_originated
, lsa
);
608 /* free memory allocated by ospf apiclient library */
609 XFREE(MTYPE_OSPF_APICLIENT
, lsa
);
612 static void ospf_apiclient_msghandle(struct ospf_apiclient
*oclient
,
615 /* Call message handler function. */
616 switch (msg
->hdr
.msgtype
) {
617 case MSG_READY_NOTIFY
:
618 ospf_apiclient_handle_ready(oclient
, msg
);
621 ospf_apiclient_handle_new_if(oclient
, msg
);
624 ospf_apiclient_handle_del_if(oclient
, msg
);
627 ospf_apiclient_handle_ism_change(oclient
, msg
);
630 ospf_apiclient_handle_nsm_change(oclient
, msg
);
632 case MSG_LSA_UPDATE_NOTIFY
:
633 ospf_apiclient_handle_lsa_update(oclient
, msg
);
635 case MSG_LSA_DELETE_NOTIFY
:
636 ospf_apiclient_handle_lsa_delete(oclient
, msg
);
640 "ospf_apiclient_read: Unknown message type: %d\n",
646 /* -----------------------------------------------------------
647 * Callback handler registration
648 * -----------------------------------------------------------
651 void ospf_apiclient_register_callback(
652 struct ospf_apiclient
*oclient
,
653 void (*ready_notify
)(uint8_t lsa_type
, uint8_t opaque_type
,
654 struct in_addr addr
),
655 void (*new_if
)(struct in_addr ifaddr
, struct in_addr area_id
),
656 void (*del_if
)(struct in_addr ifaddr
),
657 void (*ism_change
)(struct in_addr ifaddr
, struct in_addr area_id
,
659 void (*nsm_change
)(struct in_addr ifaddr
, struct in_addr nbraddr
,
660 struct in_addr router_id
, uint8_t status
),
661 void (*update_notify
)(struct in_addr ifaddr
, struct in_addr area_id
,
662 uint8_t self_origin
, struct lsa_header
*lsa
),
663 void (*delete_notify
)(struct in_addr ifaddr
, struct in_addr area_id
,
664 uint8_t self_origin
, struct lsa_header
*lsa
))
667 assert(update_notify
);
669 /* Register callback function */
670 oclient
->ready_notify
= ready_notify
;
671 oclient
->new_if
= new_if
;
672 oclient
->del_if
= del_if
;
673 oclient
->ism_change
= ism_change
;
674 oclient
->nsm_change
= nsm_change
;
675 oclient
->update_notify
= update_notify
;
676 oclient
->delete_notify
= delete_notify
;
679 /* -----------------------------------------------------------
680 * Asynchronous message handling
681 * -----------------------------------------------------------
684 int ospf_apiclient_handle_async(struct ospf_apiclient
*oclient
)
689 msg
= msg_read(oclient
->fd_async
);
692 /* Connection broke down */
697 ospf_apiclient_msghandle(oclient
, msg
);
699 /* Don't forget to free this message */