summaryrefslogtreecommitdiffstats
path: root/ospfclient
diff options
context:
space:
mode:
authorwhitespace / reindent <invalid@invalid.invalid>2017-07-17 14:03:14 +0200
committerwhitespace / reindent <invalid@invalid.invalid>2017-07-17 14:04:07 +0200
commitd62a17aedeb0eebdba98238874bb13d62c48dbf9 (patch)
tree3b319b1d61c8b85b4d1f06adf8b844bb8a9b5107 /ospfclient
parent*: add indent control files (diff)
downloadfrr-d62a17aedeb0eebdba98238874bb13d62c48dbf9.tar.xz
frr-d62a17aedeb0eebdba98238874bb13d62c48dbf9.zip
indent.py `git ls-files | pcregrep '\.[ch]$' | pcregrep -v '^(ldpd|babeld|nhrpd)/'` Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Diffstat (limited to 'ospfclient')
-rw-r--r--ospfclient/ospf_apiclient.c1036
-rw-r--r--ospfclient/ospf_apiclient.h127
-rw-r--r--ospfclient/ospfclient.c454
3 files changed, 755 insertions, 862 deletions
diff --git a/ospfclient/ospf_apiclient.c b/ospfclient/ospf_apiclient.c
index 3f3b5511a..bdac0e49a 100644
--- a/ospfclient/ospf_apiclient.c
+++ b/ospfclient/ospf_apiclient.c
@@ -66,24 +66,23 @@ DEFINE_MTYPE_STATIC(OSPFCLIENT, OSPF_APICLIENT, "OSPF-API client")
* -----------------------------------------------------------
*/
-void ospf_apiclient_handle_reply (struct ospf_apiclient *oclient,
- struct msg *msg);
-void ospf_apiclient_handle_update_notify (struct ospf_apiclient *oclient,
- struct msg *msg);
-void ospf_apiclient_handle_delete_notify (struct ospf_apiclient *oclient,
- struct msg *msg);
+void ospf_apiclient_handle_reply(struct ospf_apiclient *oclient,
+ struct msg *msg);
+void ospf_apiclient_handle_update_notify(struct ospf_apiclient *oclient,
+ struct msg *msg);
+void ospf_apiclient_handle_delete_notify(struct ospf_apiclient *oclient,
+ struct msg *msg);
/* -----------------------------------------------------------
* Initialization
* -----------------------------------------------------------
*/
-static unsigned short
-ospf_apiclient_getport (void)
+static unsigned short ospf_apiclient_getport(void)
{
- struct servent *sp = getservbyname ("ospfapi", "tcp");
+ struct servent *sp = getservbyname("ospfapi", "tcp");
- return sp ? ntohs (sp->s_port) : OSPF_API_SYNC_PORT;
+ return sp ? ntohs(sp->s_port) : OSPF_API_SYNC_PORT;
}
/* -----------------------------------------------------------
@@ -91,214 +90,208 @@ ospf_apiclient_getport (void)
* -----------------------------------------------------------
*/
-struct ospf_apiclient *
-ospf_apiclient_connect (char *host, int syncport)
+struct ospf_apiclient *ospf_apiclient_connect(char *host, int syncport)
{
- struct sockaddr_in myaddr_sync;
- struct sockaddr_in myaddr_async;
- struct sockaddr_in peeraddr;
- struct hostent *hp;
- struct ospf_apiclient *new;
- int size = 0;
- unsigned int peeraddrlen;
- int async_server_sock;
- int fd1, fd2;
- int ret;
- int on = 1;
-
- /* There are two connections between the client and the server.
- First the client opens a connection for synchronous requests/replies
- to the server. The server will accept this connection and
- as a reaction open a reverse connection channel for
- asynchronous messages. */
-
- async_server_sock = socket (AF_INET, SOCK_STREAM, 0);
- if (async_server_sock < 0)
- {
- fprintf (stderr,
- "ospf_apiclient_connect: creating async socket failed\n");
- return NULL;
- }
-
- /* Prepare socket for asynchronous messages */
- /* Initialize async address structure */
- memset (&myaddr_async, 0, sizeof (struct sockaddr_in));
- myaddr_async.sin_family = AF_INET;
- myaddr_async.sin_addr.s_addr = htonl (INADDR_ANY);
- myaddr_async.sin_port = htons (syncport+1);
- size = sizeof (struct sockaddr_in);
+ struct sockaddr_in myaddr_sync;
+ struct sockaddr_in myaddr_async;
+ struct sockaddr_in peeraddr;
+ struct hostent *hp;
+ struct ospf_apiclient *new;
+ int size = 0;
+ unsigned int peeraddrlen;
+ int async_server_sock;
+ int fd1, fd2;
+ int ret;
+ int on = 1;
+
+ /* There are two connections between the client and the server.
+ First the client opens a connection for synchronous requests/replies
+ to the server. The server will accept this connection and
+ as a reaction open a reverse connection channel for
+ asynchronous messages. */
+
+ async_server_sock = socket(AF_INET, SOCK_STREAM, 0);
+ if (async_server_sock < 0) {
+ fprintf(stderr,
+ "ospf_apiclient_connect: creating async socket failed\n");
+ return NULL;
+ }
+
+ /* Prepare socket for asynchronous messages */
+ /* Initialize async address structure */
+ memset(&myaddr_async, 0, sizeof(struct sockaddr_in));
+ myaddr_async.sin_family = AF_INET;
+ myaddr_async.sin_addr.s_addr = htonl(INADDR_ANY);
+ myaddr_async.sin_port = htons(syncport + 1);
+ size = sizeof(struct sockaddr_in);
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
- myaddr_async.sin_len = size;
+ myaddr_async.sin_len = size;
#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
- /* This is a server socket, reuse addr and port */
- ret = setsockopt (async_server_sock, SOL_SOCKET,
- SO_REUSEADDR, (void *) &on, sizeof (on));
- if (ret < 0)
- {
- fprintf (stderr, "ospf_apiclient_connect: SO_REUSEADDR failed\n");
- close (async_server_sock);
- return NULL;
- }
+ /* This is a server socket, reuse addr and port */
+ ret = setsockopt(async_server_sock, SOL_SOCKET, SO_REUSEADDR,
+ (void *)&on, sizeof(on));
+ if (ret < 0) {
+ fprintf(stderr,
+ "ospf_apiclient_connect: SO_REUSEADDR failed\n");
+ close(async_server_sock);
+ return NULL;
+ }
#ifdef SO_REUSEPORT
- ret = setsockopt (async_server_sock, SOL_SOCKET, SO_REUSEPORT,
- (void *) &on, sizeof (on));
- if (ret < 0)
- {
- fprintf (stderr, "ospf_apiclient_connect: SO_REUSEPORT failed\n");
- close (async_server_sock);
- return NULL;
- }
+ ret = setsockopt(async_server_sock, SOL_SOCKET, SO_REUSEPORT,
+ (void *)&on, sizeof(on));
+ if (ret < 0) {
+ fprintf(stderr,
+ "ospf_apiclient_connect: SO_REUSEPORT failed\n");
+ close(async_server_sock);
+ return NULL;
+ }
#endif /* SO_REUSEPORT */
- /* Bind socket to address structure */
- ret = bind (async_server_sock, (struct sockaddr *) &myaddr_async, size);
- if (ret < 0)
- {
- fprintf (stderr, "ospf_apiclient_connect: bind async socket failed\n");
- close (async_server_sock);
- return NULL;
- }
-
- /* Wait for reverse channel connection establishment from server */
- ret = listen (async_server_sock, BACKLOG);
- if (ret < 0)
- {
- fprintf (stderr, "ospf_apiclient_connect: listen: %s\n", safe_strerror (errno));
- close (async_server_sock);
- return NULL;
- }
-
- /* Make connection for synchronous requests and connect to server */
- /* Resolve address of server */
- hp = gethostbyname (host);
- if (!hp)
- {
- fprintf (stderr, "ospf_apiclient_connect: no such host %s\n", host);
- close (async_server_sock);
- return NULL;
- }
-
- fd1 = socket (AF_INET, SOCK_STREAM, 0);
- if (fd1 < 0)
- {
- close (async_server_sock);
- fprintf (stderr,
- "ospf_apiclient_connect: creating sync socket failed\n");
- return NULL;
- }
-
-
- /* Reuse addr and port */
- ret = setsockopt (fd1, SOL_SOCKET,
- SO_REUSEADDR, (void *) &on, sizeof (on));
- if (ret < 0)
- {
- fprintf (stderr, "ospf_apiclient_connect: SO_REUSEADDR failed\n");
- close (fd1);
- close (async_server_sock);
- return NULL;
- }
+ /* Bind socket to address structure */
+ ret = bind(async_server_sock, (struct sockaddr *)&myaddr_async, size);
+ if (ret < 0) {
+ fprintf(stderr,
+ "ospf_apiclient_connect: bind async socket failed\n");
+ close(async_server_sock);
+ return NULL;
+ }
+
+ /* Wait for reverse channel connection establishment from server */
+ ret = listen(async_server_sock, BACKLOG);
+ if (ret < 0) {
+ fprintf(stderr, "ospf_apiclient_connect: listen: %s\n",
+ safe_strerror(errno));
+ close(async_server_sock);
+ return NULL;
+ }
+
+ /* Make connection for synchronous requests and connect to server */
+ /* Resolve address of server */
+ hp = gethostbyname(host);
+ if (!hp) {
+ fprintf(stderr, "ospf_apiclient_connect: no such host %s\n",
+ host);
+ close(async_server_sock);
+ return NULL;
+ }
+
+ fd1 = socket(AF_INET, SOCK_STREAM, 0);
+ if (fd1 < 0) {
+ close(async_server_sock);
+ fprintf(stderr,
+ "ospf_apiclient_connect: creating sync socket failed\n");
+ return NULL;
+ }
+
+
+ /* Reuse addr and port */
+ ret = setsockopt(fd1, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
+ sizeof(on));
+ if (ret < 0) {
+ fprintf(stderr,
+ "ospf_apiclient_connect: SO_REUSEADDR failed\n");
+ close(fd1);
+ close(async_server_sock);
+ return NULL;
+ }
#ifdef SO_REUSEPORT
- ret = setsockopt (fd1, SOL_SOCKET, SO_REUSEPORT,
- (void *) &on, sizeof (on));
- if (ret < 0)
- {
- fprintf (stderr, "ospf_apiclient_connect: SO_REUSEPORT failed\n");
- close (fd1);
- close (async_server_sock);
- return NULL;
- }
+ ret = setsockopt(fd1, SOL_SOCKET, SO_REUSEPORT, (void *)&on,
+ sizeof(on));
+ if (ret < 0) {
+ fprintf(stderr,
+ "ospf_apiclient_connect: SO_REUSEPORT failed\n");
+ close(fd1);
+ close(async_server_sock);
+ return NULL;
+ }
#endif /* SO_REUSEPORT */
- /* Bind sync socket to address structure. This is needed since we
- want the sync port number on a fixed port number. The reverse
- async channel will be at this port+1 */
+ /* Bind sync socket to address structure. This is needed since we
+ want the sync port number on a fixed port number. The reverse
+ async channel will be at this port+1 */
- memset (&myaddr_sync, 0, sizeof (struct sockaddr_in));
- myaddr_sync.sin_family = AF_INET;
- myaddr_sync.sin_port = htons (syncport);
+ memset(&myaddr_sync, 0, sizeof(struct sockaddr_in));
+ myaddr_sync.sin_family = AF_INET;
+ myaddr_sync.sin_port = htons(syncport);
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
- myaddr_sync.sin_len = sizeof (struct sockaddr_in);
+ myaddr_sync.sin_len = sizeof(struct sockaddr_in);
#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
- ret = bind (fd1, (struct sockaddr *) &myaddr_sync, size);
- if (ret < 0)
- {
- fprintf (stderr, "ospf_apiclient_connect: bind sync socket failed\n");
- close (fd1);
- close (async_server_sock);
- return NULL;
- }
-
- /* Prepare address structure for connect */
- memcpy (&myaddr_sync.sin_addr, hp->h_addr, hp->h_length);
- myaddr_sync.sin_family = AF_INET;
- myaddr_sync.sin_port = htons(ospf_apiclient_getport ());
+ ret = bind(fd1, (struct sockaddr *)&myaddr_sync, size);
+ if (ret < 0) {
+ fprintf(stderr,
+ "ospf_apiclient_connect: bind sync socket failed\n");
+ close(fd1);
+ close(async_server_sock);
+ return NULL;
+ }
+
+ /* Prepare address structure for connect */
+ memcpy(&myaddr_sync.sin_addr, hp->h_addr, hp->h_length);
+ myaddr_sync.sin_family = AF_INET;
+ myaddr_sync.sin_port = htons(ospf_apiclient_getport());
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
- myaddr_sync.sin_len = sizeof (struct sockaddr_in);
+ myaddr_sync.sin_len = sizeof(struct sockaddr_in);
#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
- /* Now establish synchronous channel with OSPF daemon */
- ret = connect (fd1, (struct sockaddr *) &myaddr_sync,
- sizeof (struct sockaddr_in));
- if (ret < 0)
- {
- fprintf (stderr, "ospf_apiclient_connect: sync connect failed\n");
- close (async_server_sock);
- close (fd1);
- return NULL;
- }
-
- /* Accept reverse connection */
- peeraddrlen = sizeof (struct sockaddr_in);
- memset (&peeraddr, 0, peeraddrlen);
-
- fd2 =
- accept (async_server_sock, (struct sockaddr *) &peeraddr, &peeraddrlen);
- if (fd2 < 0)
- {
- fprintf (stderr, "ospf_apiclient_connect: accept async failed\n");
- close (async_server_sock);
- close (fd1);
- close (fd2);
- return NULL;
- }
-
- /* Server socket is not needed anymore since we are not accepting more
- connections */
- close (async_server_sock);
-
- /* Create new client-side instance */
- new = XCALLOC (MTYPE_OSPF_APICLIENT, sizeof (struct ospf_apiclient));
-
- /* Initialize socket descriptors for sync and async channels */
- new->fd_sync = fd1;
- new->fd_async = fd2;
-
- return new;
+ /* Now establish synchronous channel with OSPF daemon */
+ ret = connect(fd1, (struct sockaddr *)&myaddr_sync,
+ sizeof(struct sockaddr_in));
+ if (ret < 0) {
+ fprintf(stderr,
+ "ospf_apiclient_connect: sync connect failed\n");
+ close(async_server_sock);
+ close(fd1);
+ return NULL;
+ }
+
+ /* Accept reverse connection */
+ peeraddrlen = sizeof(struct sockaddr_in);
+ memset(&peeraddr, 0, peeraddrlen);
+
+ fd2 = accept(async_server_sock, (struct sockaddr *)&peeraddr,
+ &peeraddrlen);
+ if (fd2 < 0) {
+ fprintf(stderr,
+ "ospf_apiclient_connect: accept async failed\n");
+ close(async_server_sock);
+ close(fd1);
+ close(fd2);
+ return NULL;
+ }
+
+ /* Server socket is not needed anymore since we are not accepting more
+ connections */
+ close(async_server_sock);
+
+ /* Create new client-side instance */
+ new = XCALLOC(MTYPE_OSPF_APICLIENT, sizeof(struct ospf_apiclient));
+
+ /* Initialize socket descriptors for sync and async channels */
+ new->fd_sync = fd1;
+ new->fd_async = fd2;
+
+ return new;
}
-int
-ospf_apiclient_close (struct ospf_apiclient *oclient)
+int ospf_apiclient_close(struct ospf_apiclient *oclient)
{
- if (oclient->fd_sync >= 0)
- {
- close (oclient->fd_sync);
- }
+ if (oclient->fd_sync >= 0) {
+ close(oclient->fd_sync);
+ }
- if (oclient->fd_async >= 0)
- {
- close (oclient->fd_async);
- }
+ if (oclient->fd_async >= 0) {
+ close(oclient->fd_async);
+ }
- /* Free client structure */
- XFREE (MTYPE_OSPF_APICLIENT, oclient);
- return 0;
+ /* Free client structure */
+ XFREE(MTYPE_OSPF_APICLIENT, oclient);
+ return 0;
}
/* -----------------------------------------------------------
@@ -307,40 +300,39 @@ ospf_apiclient_close (struct ospf_apiclient *oclient)
*/
/* Send synchronous request, wait for reply */
-static int
-ospf_apiclient_send_request (struct ospf_apiclient *oclient, struct msg *msg)
+static int ospf_apiclient_send_request(struct ospf_apiclient *oclient,
+ struct msg *msg)
{
- u_int32_t reqseq;
- struct msg_reply *msgreply;
- int rc;
+ u_int32_t reqseq;
+ struct msg_reply *msgreply;
+ int rc;
- /* NB: Given "msg" is freed inside this function. */
+ /* NB: Given "msg" is freed inside this function. */
- /* Remember the sequence number of the request */
- reqseq = ntohl (msg->hdr.msgseq);
+ /* Remember the sequence number of the request */
+ reqseq = ntohl(msg->hdr.msgseq);
- /* Write message to OSPFd */
- rc = msg_write (oclient->fd_sync, msg);
- msg_free (msg);
+ /* Write message to OSPFd */
+ rc = msg_write(oclient->fd_sync, msg);
+ msg_free(msg);
- if (rc < 0)
- {
- return -1;
- }
+ if (rc < 0) {
+ return -1;
+ }
- /* Wait for reply *//* NB: New "msg" is allocated by "msg_read()". */
- msg = msg_read (oclient->fd_sync);
- if (!msg)
- return -1;
+ /* Wait for reply */ /* NB: New "msg" is allocated by "msg_read()". */
+ msg = msg_read(oclient->fd_sync);
+ if (!msg)
+ return -1;
- assert (msg->hdr.msgtype == MSG_REPLY);
- assert (ntohl (msg->hdr.msgseq) == reqseq);
+ assert(msg->hdr.msgtype == MSG_REPLY);
+ assert(ntohl(msg->hdr.msgseq) == reqseq);
- msgreply = (struct msg_reply *) STREAM_DATA (msg->s);
- rc = msgreply->errcode;
- msg_free (msg);
+ msgreply = (struct msg_reply *)STREAM_DATA(msg->s);
+ rc = msgreply->errcode;
+ msg_free(msg);
- return rc;
+ return rc;
}
@@ -349,23 +341,19 @@ ospf_apiclient_send_request (struct ospf_apiclient *oclient, struct msg *msg)
* -----------------------------------------------------------
*/
-static u_int32_t
-ospf_apiclient_get_seqnr (void)
+static u_int32_t ospf_apiclient_get_seqnr(void)
{
- static u_int32_t seqnr = MIN_SEQ;
- u_int32_t tmp;
-
- tmp = seqnr;
- /* Increment sequence number */
- if (seqnr < MAX_SEQ)
- {
- seqnr++;
- }
- else
- {
- seqnr = MIN_SEQ;
- }
- return tmp;
+ static u_int32_t seqnr = MIN_SEQ;
+ u_int32_t tmp;
+
+ tmp = seqnr;
+ /* Increment sequence number */
+ if (seqnr < MAX_SEQ) {
+ seqnr++;
+ } else {
+ seqnr = MIN_SEQ;
+ }
+ return tmp;
}
/* -----------------------------------------------------------
@@ -376,141 +364,132 @@ ospf_apiclient_get_seqnr (void)
/*
* Synchronous request to register opaque type.
*/
-int
-ospf_apiclient_register_opaque_type (struct ospf_apiclient *cl,
- u_char ltype, u_char otype)
+int ospf_apiclient_register_opaque_type(struct ospf_apiclient *cl, u_char ltype,
+ u_char otype)
{
- struct msg *msg;
- int rc;
-
- /* just put 1 as a sequence number. */
- msg = new_msg_register_opaque_type (ospf_apiclient_get_seqnr (),
- ltype, otype);
- if (!msg)
- {
- fprintf (stderr, "new_msg_register_opaque_type failed\n");
- return -1;
- }
-
- rc = ospf_apiclient_send_request (cl, msg);
- return rc;
+ struct msg *msg;
+ int rc;
+
+ /* just put 1 as a sequence number. */
+ msg = new_msg_register_opaque_type(ospf_apiclient_get_seqnr(), ltype,
+ otype);
+ if (!msg) {
+ fprintf(stderr, "new_msg_register_opaque_type failed\n");
+ return -1;
+ }
+
+ rc = ospf_apiclient_send_request(cl, msg);
+ return rc;
}
-/*
+/*
* Synchronous request to synchronize with OSPF's LSDB.
* Two steps required: register_event in order to get
* dynamic updates and LSDB_Sync.
*/
-int
-ospf_apiclient_sync_lsdb (struct ospf_apiclient *oclient)
+int ospf_apiclient_sync_lsdb(struct ospf_apiclient *oclient)
{
- struct msg *msg;
- int rc;
- struct lsa_filter_type filter;
-
- filter.typemask = 0xFFFF; /* all LSAs */
- filter.origin = ANY_ORIGIN;
- filter.num_areas = 0; /* all Areas. */
-
- msg = new_msg_register_event (ospf_apiclient_get_seqnr (), &filter);
- if (!msg)
- {
- fprintf (stderr, "new_msg_register_event failed\n");
- return -1;
- }
- rc = ospf_apiclient_send_request (oclient, msg);
-
- if (rc != 0)
- goto out;
-
- msg = new_msg_sync_lsdb (ospf_apiclient_get_seqnr (), &filter);
- if (!msg)
- {
- fprintf (stderr, "new_msg_sync_lsdb failed\n");
- return -1;
- }
- rc = ospf_apiclient_send_request (oclient, msg);
+ struct msg *msg;
+ int rc;
+ struct lsa_filter_type filter;
+
+ filter.typemask = 0xFFFF; /* all LSAs */
+ filter.origin = ANY_ORIGIN;
+ filter.num_areas = 0; /* all Areas. */
+
+ msg = new_msg_register_event(ospf_apiclient_get_seqnr(), &filter);
+ if (!msg) {
+ fprintf(stderr, "new_msg_register_event failed\n");
+ return -1;
+ }
+ rc = ospf_apiclient_send_request(oclient, msg);
+
+ if (rc != 0)
+ goto out;
+
+ msg = new_msg_sync_lsdb(ospf_apiclient_get_seqnr(), &filter);
+ if (!msg) {
+ fprintf(stderr, "new_msg_sync_lsdb failed\n");
+ return -1;
+ }
+ rc = ospf_apiclient_send_request(oclient, msg);
out:
- return rc;
+ return rc;
}
-/*
+/*
* Synchronous request to originate or update an LSA.
*/
-int
-ospf_apiclient_lsa_originate (struct ospf_apiclient *oclient,
- struct in_addr ifaddr,
- struct in_addr area_id,
- u_char lsa_type,
- u_char opaque_type, u_int32_t opaque_id,
- void *opaquedata, int opaquelen)
+int ospf_apiclient_lsa_originate(struct ospf_apiclient *oclient,
+ struct in_addr ifaddr, struct in_addr area_id,
+ u_char lsa_type, u_char opaque_type,
+ u_int32_t opaque_id, void *opaquedata,
+ int opaquelen)
{
- struct msg *msg;
- int rc;
- u_char buf[OSPF_MAX_LSA_SIZE];
- struct lsa_header *lsah;
- u_int32_t tmp;
-
-
- /* We can only originate opaque LSAs */
- if (!IS_OPAQUE_LSA (lsa_type))
- {
- fprintf (stderr, "Cannot originate non-opaque LSA type %d\n", lsa_type);
- return OSPF_API_ILLEGALLSATYPE;
- }
-
- /* Make a new LSA from parameters */
- lsah = (struct lsa_header *) buf;
- lsah->ls_age = 0;
- lsah->options = 0;
- lsah->type = lsa_type;
-
- tmp = SET_OPAQUE_LSID (opaque_type, opaque_id);
- lsah->id.s_addr = htonl (tmp);
- lsah->adv_router.s_addr = 0;
- lsah->ls_seqnum = 0;
- lsah->checksum = 0;
- lsah->length = htons (sizeof (struct lsa_header) + opaquelen);
-
- memcpy (((u_char *) lsah) + sizeof (struct lsa_header), opaquedata,
- opaquelen);
-
- msg = new_msg_originate_request (ospf_apiclient_get_seqnr (),
- ifaddr, area_id, lsah);
- if (!msg)
- {
- fprintf (stderr, "new_msg_originate_request failed\n");
- return OSPF_API_NOMEMORY;
- }
-
- rc = ospf_apiclient_send_request (oclient, msg);
- return rc;
+ struct msg *msg;
+ int rc;
+ u_char buf[OSPF_MAX_LSA_SIZE];
+ struct lsa_header *lsah;
+ u_int32_t tmp;
+
+
+ /* We can only originate opaque LSAs */
+ if (!IS_OPAQUE_LSA(lsa_type)) {
+ fprintf(stderr, "Cannot originate non-opaque LSA type %d\n",
+ lsa_type);
+ return OSPF_API_ILLEGALLSATYPE;
+ }
+
+ /* Make a new LSA from parameters */
+ lsah = (struct lsa_header *)buf;
+ lsah->ls_age = 0;
+ lsah->options = 0;
+ lsah->type = lsa_type;
+
+ tmp = SET_OPAQUE_LSID(opaque_type, opaque_id);
+ lsah->id.s_addr = htonl(tmp);
+ lsah->adv_router.s_addr = 0;
+ lsah->ls_seqnum = 0;
+ lsah->checksum = 0;
+ lsah->length = htons(sizeof(struct lsa_header) + opaquelen);
+
+ memcpy(((u_char *)lsah) + sizeof(struct lsa_header), opaquedata,
+ opaquelen);
+
+ msg = new_msg_originate_request(ospf_apiclient_get_seqnr(), ifaddr,
+ area_id, lsah);
+ if (!msg) {
+ fprintf(stderr, "new_msg_originate_request failed\n");
+ return OSPF_API_NOMEMORY;
+ }
+
+ rc = ospf_apiclient_send_request(oclient, msg);
+ return rc;
}
-int
-ospf_apiclient_lsa_delete (struct ospf_apiclient *oclient,
- struct in_addr area_id, u_char lsa_type,
- u_char opaque_type, u_int32_t opaque_id)
+int ospf_apiclient_lsa_delete(struct ospf_apiclient *oclient,
+ struct in_addr area_id, u_char lsa_type,
+ u_char opaque_type, u_int32_t opaque_id)
{
- struct msg *msg;
- int rc;
-
- /* Only opaque LSA can be deleted */
- if (!IS_OPAQUE_LSA (lsa_type))
- {
- fprintf (stderr, "Cannot delete non-opaque LSA type %d\n", lsa_type);
- return OSPF_API_ILLEGALLSATYPE;
- }
-
- /* opaque_id is in host byte order and will be converted
- * to network byte order by new_msg_delete_request */
- msg = new_msg_delete_request (ospf_apiclient_get_seqnr (),
- area_id, lsa_type, opaque_type, opaque_id);
-
- rc = ospf_apiclient_send_request (oclient, msg);
- return rc;
+ struct msg *msg;
+ int rc;
+
+ /* Only opaque LSA can be deleted */
+ if (!IS_OPAQUE_LSA(lsa_type)) {
+ fprintf(stderr, "Cannot delete non-opaque LSA type %d\n",
+ lsa_type);
+ return OSPF_API_ILLEGALLSATYPE;
+ }
+
+ /* opaque_id is in host byte order and will be converted
+ * to network byte order by new_msg_delete_request */
+ msg = new_msg_delete_request(ospf_apiclient_get_seqnr(), area_id,
+ lsa_type, opaque_type, opaque_id);
+
+ rc = ospf_apiclient_send_request(oclient, msg);
+ return rc;
}
/* -----------------------------------------------------------
@@ -518,168 +497,155 @@ ospf_apiclient_lsa_delete (struct ospf_apiclient *oclient,
* -----------------------------------------------------------
*/
-static void
-ospf_apiclient_handle_ready (struct ospf_apiclient *oclient, struct msg *msg)
+static void ospf_apiclient_handle_ready(struct ospf_apiclient *oclient,
+ struct msg *msg)
{
- struct msg_ready_notify *r;
- r = (struct msg_ready_notify *) STREAM_DATA (msg->s);
-
- /* Invoke registered callback function. */
- if (oclient->ready_notify)
- {
- (oclient->ready_notify) (r->lsa_type, r->opaque_type, r->addr);
- }
+ struct msg_ready_notify *r;
+ r = (struct msg_ready_notify *)STREAM_DATA(msg->s);
+
+ /* Invoke registered callback function. */
+ if (oclient->ready_notify) {
+ (oclient->ready_notify)(r->lsa_type, r->opaque_type, r->addr);
+ }
}
-static void
-ospf_apiclient_handle_new_if (struct ospf_apiclient *oclient, struct msg *msg)
+static void ospf_apiclient_handle_new_if(struct ospf_apiclient *oclient,
+ struct msg *msg)
{
- struct msg_new_if *n;
- n = (struct msg_new_if *) STREAM_DATA (msg->s);
-
- /* Invoke registered callback function. */
- if (oclient->new_if)
- {
- (oclient->new_if) (n->ifaddr, n->area_id);
- }
+ struct msg_new_if *n;
+ n = (struct msg_new_if *)STREAM_DATA(msg->s);
+
+ /* Invoke registered callback function. */
+ if (oclient->new_if) {
+ (oclient->new_if)(n->ifaddr, n->area_id);
+ }
}
-static void
-ospf_apiclient_handle_del_if (struct ospf_apiclient *oclient, struct msg *msg)
+static void ospf_apiclient_handle_del_if(struct ospf_apiclient *oclient,
+ struct msg *msg)
{
- struct msg_del_if *d;
- d = (struct msg_del_if *) STREAM_DATA (msg->s);
-
- /* Invoke registered callback function. */
- if (oclient->del_if)
- {
- (oclient->del_if) (d->ifaddr);
- }
+ struct msg_del_if *d;
+ d = (struct msg_del_if *)STREAM_DATA(msg->s);
+
+ /* Invoke registered callback function. */
+ if (oclient->del_if) {
+ (oclient->del_if)(d->ifaddr);
+ }
}
-static void
-ospf_apiclient_handle_ism_change (struct ospf_apiclient *oclient,
- struct msg *msg)
+static void ospf_apiclient_handle_ism_change(struct ospf_apiclient *oclient,
+ struct msg *msg)
{
- struct msg_ism_change *m;
- m = (struct msg_ism_change *) STREAM_DATA (msg->s);
-
- /* Invoke registered callback function. */
- if (oclient->ism_change)
- {
- (oclient->ism_change) (m->ifaddr, m->area_id, m->status);
- }
+ struct msg_ism_change *m;
+ m = (struct msg_ism_change *)STREAM_DATA(msg->s);
+ /* Invoke registered callback function. */
+ if (oclient->ism_change) {
+ (oclient->ism_change)(m->ifaddr, m->area_id, m->status);
+ }
}
-static void
-ospf_apiclient_handle_nsm_change (struct ospf_apiclient *oclient,
- struct msg *msg)
+static void ospf_apiclient_handle_nsm_change(struct ospf_apiclient *oclient,
+ struct msg *msg)
{
- struct msg_nsm_change *m;
- m = (struct msg_nsm_change *) STREAM_DATA (msg->s);
-
- /* Invoke registered callback function. */
- if (oclient->nsm_change)
- {
- (oclient->nsm_change) (m->ifaddr, m->nbraddr, m->router_id, m->status);
- }
+ struct msg_nsm_change *m;
+ m = (struct msg_nsm_change *)STREAM_DATA(msg->s);
+
+ /* Invoke registered callback function. */
+ if (oclient->nsm_change) {
+ (oclient->nsm_change)(m->ifaddr, m->nbraddr, m->router_id,
+ m->status);
+ }
}
-static void
-ospf_apiclient_handle_lsa_update (struct ospf_apiclient *oclient,
- struct msg *msg)
+static void ospf_apiclient_handle_lsa_update(struct ospf_apiclient *oclient,
+ struct msg *msg)
{
- struct msg_lsa_change_notify *cn;
- struct lsa_header *lsa;
- int lsalen;
-
- cn = (struct msg_lsa_change_notify *) STREAM_DATA (msg->s);
-
- /* Extract LSA from message */
- lsalen = ntohs (cn->data.length);
- lsa = XMALLOC (MTYPE_OSPF_APICLIENT, lsalen);
- if (!lsa)
- {
- fprintf (stderr, "LSA update: Cannot allocate memory for LSA\n");
- return;
- }
- memcpy (lsa, &(cn->data), lsalen);
-
- /* Invoke registered update callback function */
- if (oclient->update_notify)
- {
- (oclient->update_notify) (cn->ifaddr, cn->area_id,
- cn->is_self_originated, lsa);
- }
-
- /* free memory allocated by ospf apiclient library */
- XFREE (MTYPE_OSPF_APICLIENT, lsa);
+ struct msg_lsa_change_notify *cn;
+ struct lsa_header *lsa;
+ int lsalen;
+
+ cn = (struct msg_lsa_change_notify *)STREAM_DATA(msg->s);
+
+ /* Extract LSA from message */
+ lsalen = ntohs(cn->data.length);
+ lsa = XMALLOC(MTYPE_OSPF_APICLIENT, lsalen);
+ if (!lsa) {
+ fprintf(stderr, "LSA update: Cannot allocate memory for LSA\n");
+ return;
+ }
+ memcpy(lsa, &(cn->data), lsalen);
+
+ /* Invoke registered update callback function */
+ if (oclient->update_notify) {
+ (oclient->update_notify)(cn->ifaddr, cn->area_id,
+ cn->is_self_originated, lsa);
+ }
+
+ /* free memory allocated by ospf apiclient library */
+ XFREE(MTYPE_OSPF_APICLIENT, lsa);
}
-static void
-ospf_apiclient_handle_lsa_delete (struct ospf_apiclient *oclient,
- struct msg *msg)
+static void ospf_apiclient_handle_lsa_delete(struct ospf_apiclient *oclient,
+ struct msg *msg)
{
- struct msg_lsa_change_notify *cn;
- struct lsa_header *lsa;
- int lsalen;
-
- cn = (struct msg_lsa_change_notify *) STREAM_DATA (msg->s);
-
- /* Extract LSA from message */
- lsalen = ntohs (cn->data.length);
- lsa = XMALLOC (MTYPE_OSPF_APICLIENT, lsalen);
- if (!lsa)
- {
- fprintf (stderr, "LSA delete: Cannot allocate memory for LSA\n");
- return;
- }
- memcpy (lsa, &(cn->data), lsalen);
-
- /* Invoke registered update callback function */
- if (oclient->delete_notify)
- {
- (oclient->delete_notify) (cn->ifaddr, cn->area_id,
- cn->is_self_originated, lsa);
- }
-
- /* free memory allocated by ospf apiclient library */
- XFREE (MTYPE_OSPF_APICLIENT, lsa);
+ struct msg_lsa_change_notify *cn;
+ struct lsa_header *lsa;
+ int lsalen;
+
+ cn = (struct msg_lsa_change_notify *)STREAM_DATA(msg->s);
+
+ /* Extract LSA from message */
+ lsalen = ntohs(cn->data.length);
+ lsa = XMALLOC(MTYPE_OSPF_APICLIENT, lsalen);
+ if (!lsa) {
+ fprintf(stderr, "LSA delete: Cannot allocate memory for LSA\n");
+ return;
+ }
+ memcpy(lsa, &(cn->data), lsalen);
+
+ /* Invoke registered update callback function */
+ if (oclient->delete_notify) {
+ (oclient->delete_notify)(cn->ifaddr, cn->area_id,
+ cn->is_self_originated, lsa);
+ }
+
+ /* free memory allocated by ospf apiclient library */
+ XFREE(MTYPE_OSPF_APICLIENT, lsa);
}
-static void
-ospf_apiclient_msghandle (struct ospf_apiclient *oclient, struct msg *msg)
+static void ospf_apiclient_msghandle(struct ospf_apiclient *oclient,
+ struct msg *msg)
{
- /* Call message handler function. */
- switch (msg->hdr.msgtype)
- {
- case MSG_READY_NOTIFY:
- ospf_apiclient_handle_ready (oclient, msg);
- break;
- case MSG_NEW_IF:
- ospf_apiclient_handle_new_if (oclient, msg);
- break;
- case MSG_DEL_IF:
- ospf_apiclient_handle_del_if (oclient, msg);
- break;
- case MSG_ISM_CHANGE:
- ospf_apiclient_handle_ism_change (oclient, msg);
- break;
- case MSG_NSM_CHANGE:
- ospf_apiclient_handle_nsm_change (oclient, msg);
- break;
- case MSG_LSA_UPDATE_NOTIFY:
- ospf_apiclient_handle_lsa_update (oclient, msg);
- break;
- case MSG_LSA_DELETE_NOTIFY:
- ospf_apiclient_handle_lsa_delete (oclient, msg);
- break;
- default:
- fprintf (stderr, "ospf_apiclient_read: Unknown message type: %d\n",
- msg->hdr.msgtype);
- break;
- }
+ /* Call message handler function. */
+ switch (msg->hdr.msgtype) {
+ case MSG_READY_NOTIFY:
+ ospf_apiclient_handle_ready(oclient, msg);
+ break;
+ case MSG_NEW_IF:
+ ospf_apiclient_handle_new_if(oclient, msg);
+ break;
+ case MSG_DEL_IF:
+ ospf_apiclient_handle_del_if(oclient, msg);
+ break;
+ case MSG_ISM_CHANGE:
+ ospf_apiclient_handle_ism_change(oclient, msg);
+ break;
+ case MSG_NSM_CHANGE:
+ ospf_apiclient_handle_nsm_change(oclient, msg);
+ break;
+ case MSG_LSA_UPDATE_NOTIFY:
+ ospf_apiclient_handle_lsa_update(oclient, msg);
+ break;
+ case MSG_LSA_DELETE_NOTIFY:
+ ospf_apiclient_handle_lsa_delete(oclient, msg);
+ break;
+ default:
+ fprintf(stderr,
+ "ospf_apiclient_read: Unknown message type: %d\n",
+ msg->hdr.msgtype);
+ break;
+ }
}
/* -----------------------------------------------------------
@@ -687,48 +653,32 @@ ospf_apiclient_msghandle (struct ospf_apiclient *oclient, struct msg *msg)
* -----------------------------------------------------------
*/
-void
-ospf_apiclient_register_callback (struct ospf_apiclient *oclient,
- void (*ready_notify) (u_char lsa_type,
- u_char opaque_type,
- struct in_addr addr),
- void (*new_if) (struct in_addr ifaddr,
- struct in_addr area_id),
- void (*del_if) (struct in_addr ifaddr),
- void (*ism_change) (struct in_addr ifaddr,
- struct in_addr area_id,
- u_char status),
- void (*nsm_change) (struct in_addr ifaddr,
- struct in_addr nbraddr,
- struct in_addr
- router_id,
- u_char status),
- void (*update_notify) (struct in_addr
- ifaddr,
- struct in_addr
- area_id,
- u_char self_origin,
- struct lsa_header *
- lsa),
- void (*delete_notify) (struct in_addr
- ifaddr,
- struct in_addr
- area_id,
- u_char self_origin,
- struct lsa_header *
- lsa))
+void ospf_apiclient_register_callback(
+ struct ospf_apiclient *oclient,
+ void (*ready_notify)(u_char lsa_type, u_char opaque_type,
+ struct in_addr addr),
+ void (*new_if)(struct in_addr ifaddr, struct in_addr area_id),
+ void (*del_if)(struct in_addr ifaddr),
+ void (*ism_change)(struct in_addr ifaddr, struct in_addr area_id,
+ u_char status),
+ void (*nsm_change)(struct in_addr ifaddr, struct in_addr nbraddr,
+ struct in_addr router_id, u_char status),
+ void (*update_notify)(struct in_addr ifaddr, struct in_addr area_id,
+ u_char self_origin, struct lsa_header *lsa),
+ void (*delete_notify)(struct in_addr ifaddr, struct in_addr area_id,
+ u_char self_origin, struct lsa_header *lsa))
{
- assert (oclient);
- assert (update_notify);
-
- /* Register callback function */
- oclient->ready_notify = ready_notify;
- oclient->new_if = new_if;
- oclient->del_if = del_if;
- oclient->ism_change = ism_change;
- oclient->nsm_change = nsm_change;
- oclient->update_notify = update_notify;
- oclient->delete_notify = delete_notify;
+ assert(oclient);
+ assert(update_notify);
+
+ /* Register callback function */
+ oclient->ready_notify = ready_notify;
+ oclient->new_if = new_if;
+ oclient->del_if = del_if;
+ oclient->ism_change = ism_change;
+ oclient->nsm_change = nsm_change;
+ oclient->update_notify = update_notify;
+ oclient->delete_notify = delete_notify;
}
/* -----------------------------------------------------------
@@ -736,25 +686,23 @@ ospf_apiclient_register_callback (struct ospf_apiclient *oclient,
* -----------------------------------------------------------
*/
-int
-ospf_apiclient_handle_async (struct ospf_apiclient *oclient)
+int ospf_apiclient_handle_async(struct ospf_apiclient *oclient)
{
- struct msg *msg;
+ struct msg *msg;
- /* Get a message */
- msg = msg_read (oclient->fd_async);
+ /* Get a message */
+ msg = msg_read(oclient->fd_async);
- if (!msg)
- {
- /* Connection broke down */
- return -1;
- }
+ if (!msg) {
+ /* Connection broke down */
+ return -1;
+ }
- /* Handle message */
- ospf_apiclient_msghandle (oclient, msg);
+ /* Handle message */
+ ospf_apiclient_msghandle(oclient, msg);
- /* Don't forget to free this message */
- msg_free (msg);
+ /* Don't forget to free this message */
+ msg_free(msg);
- return 0;
+ return 0;
}
diff --git a/ospfclient/ospf_apiclient.h b/ospfclient/ospf_apiclient.h
index 5882ac349..916a2d8ed 100644
--- a/ospfclient/ospf_apiclient.h
+++ b/ospfclient/ospf_apiclient.h
@@ -23,28 +23,25 @@
#define _OSPF_APICLIENT_H
/* Structure for the OSPF API client */
-struct ospf_apiclient
-{
-
- /* Sockets for sync requests and async notifications */
- int fd_sync;
- int fd_async;
-
- /* Pointer to callback functions */
- void (*ready_notify) (u_char lsa_type, u_char opaque_type,
- struct in_addr addr);
- void (*new_if) (struct in_addr ifaddr, struct in_addr area_id);
- void (*del_if) (struct in_addr ifaddr);
- void (*ism_change) (struct in_addr ifaddr, struct in_addr area_id,
- u_char status);
- void (*nsm_change) (struct in_addr ifaddr, struct in_addr nbraddr,
- struct in_addr router_id, u_char status);
- void (*update_notify) (struct in_addr ifaddr, struct in_addr area_id,
- u_char self_origin,
- struct lsa_header * lsa);
- void (*delete_notify) (struct in_addr ifaddr, struct in_addr area_id,
- u_char self_origin,
- struct lsa_header * lsa);
+struct ospf_apiclient {
+
+ /* Sockets for sync requests and async notifications */
+ int fd_sync;
+ int fd_async;
+
+ /* Pointer to callback functions */
+ void (*ready_notify)(u_char lsa_type, u_char opaque_type,
+ struct in_addr addr);
+ void (*new_if)(struct in_addr ifaddr, struct in_addr area_id);
+ void (*del_if)(struct in_addr ifaddr);
+ void (*ism_change)(struct in_addr ifaddr, struct in_addr area_id,
+ u_char status);
+ void (*nsm_change)(struct in_addr ifaddr, struct in_addr nbraddr,
+ struct in_addr router_id, u_char status);
+ void (*update_notify)(struct in_addr ifaddr, struct in_addr area_id,
+ u_char self_origin, struct lsa_header *lsa);
+ void (*delete_notify)(struct in_addr ifaddr, struct in_addr area_id,
+ u_char self_origin, struct lsa_header *lsa);
};
@@ -54,79 +51,53 @@ struct ospf_apiclient
/* Open connection to OSPF daemon. Two ports will be allocated on
client, sync channel at syncport and reverse channel at syncport+1 */
-struct ospf_apiclient *ospf_apiclient_connect (char *host, int syncport);
+struct ospf_apiclient *ospf_apiclient_connect(char *host, int syncport);
/* Shutdown connection to OSPF daemon. */
-int ospf_apiclient_close (struct ospf_apiclient *oclient);
+int ospf_apiclient_close(struct ospf_apiclient *oclient);
/* Synchronous request to register opaque type. */
-int ospf_apiclient_register_opaque_type (struct ospf_apiclient *oclient,
- u_char ltype, u_char otype);
+int ospf_apiclient_register_opaque_type(struct ospf_apiclient *oclient,
+ u_char ltype, u_char otype);
/* Synchronous request to register event mask. */
-int ospf_apiclient_register_events (struct ospf_apiclient *oclient,
- u_int32_t mask);
+int ospf_apiclient_register_events(struct ospf_apiclient *oclient,
+ u_int32_t mask);
/* Register callback functions.*/
-void ospf_apiclient_register_callback (struct ospf_apiclient *oclient,
- void (*ready_notify) (u_char lsa_type,
- u_char
- opaque_type,
- struct in_addr
- addr),
- void (*new_if) (struct in_addr ifaddr,
- struct in_addr
- area_id),
- void (*del_if) (struct in_addr ifaddr),
- void (*ism_change) (struct in_addr
- ifaddr,
- struct in_addr
- area_id,
- u_char status),
- void (*nsm_change) (struct in_addr
- ifaddr,
- struct in_addr
- nbraddr,
- struct in_addr
- router_id,
- u_char status),
- void (*update_notify) (struct in_addr
- ifaddr,
- struct in_addr
- area_id,
- u_char selforig,
- struct
- lsa_header *
- lsa),
- void (*delete_notify) (struct in_addr
- ifaddr,
- struct in_addr
- area_id,
- u_char selforig,
- struct
- lsa_header *
- lsa));
+void ospf_apiclient_register_callback(
+ struct ospf_apiclient *oclient,
+ void (*ready_notify)(u_char lsa_type, u_char opaque_type,
+ struct in_addr addr),
+ void (*new_if)(struct in_addr ifaddr, struct in_addr area_id),
+ void (*del_if)(struct in_addr ifaddr),
+ void (*ism_change)(struct in_addr ifaddr, struct in_addr area_id,
+ u_char status),
+ void (*nsm_change)(struct in_addr ifaddr, struct in_addr nbraddr,
+ struct in_addr router_id, u_char status),
+ void (*update_notify)(struct in_addr ifaddr, struct in_addr area_id,
+ u_char selforig, struct lsa_header *lsa),
+ void (*delete_notify)(struct in_addr ifaddr, struct in_addr area_id,
+ u_char selforig, struct lsa_header *lsa));
/* Synchronous request to synchronize LSDB. */
-int ospf_apiclient_sync_lsdb (struct ospf_apiclient *oclient);
+int ospf_apiclient_sync_lsdb(struct ospf_apiclient *oclient);
/* Synchronous request to originate or update opaque LSA. */
-int
-ospf_apiclient_lsa_originate(struct ospf_apiclient *oclient,
- struct in_addr ifaddr,
- struct in_addr area_id,
- u_char lsa_type,
- u_char opaque_type, u_int32_t opaque_id,
- void *opaquedata, int opaquelen);
+int ospf_apiclient_lsa_originate(struct ospf_apiclient *oclient,
+ struct in_addr ifaddr, struct in_addr area_id,
+ u_char lsa_type, u_char opaque_type,
+ u_int32_t opaque_id, void *opaquedata,
+ int opaquelen);
/* Synchronous request to delete opaque LSA. Parameter opaque_id is in
host byte order */
-int ospf_apiclient_lsa_delete (struct ospf_apiclient *oclient,
- struct in_addr area_id, u_char lsa_type,
- u_char opaque_type, u_int32_t opaque_id);
+int ospf_apiclient_lsa_delete(struct ospf_apiclient *oclient,
+ struct in_addr area_id, u_char lsa_type,
+ u_char opaque_type, u_int32_t opaque_id);
/* Fetch async message and handle it */
-int ospf_apiclient_handle_async (struct ospf_apiclient *oclient);
+int ospf_apiclient_handle_async(struct ospf_apiclient *oclient);
#endif /* _OSPF_APICLIENT_H */
diff --git a/ospfclient/ospfclient.c b/ospfclient/ospfclient.c
index 63fcafb73..5713105f4 100644
--- a/ospfclient/ospfclient.c
+++ b/ospfclient/ospfclient.c
@@ -15,7 +15,7 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-/*
+/*
* Simple program to demonstrate how OSPF API can be used. This
* application retrieves the LSDB from the OSPF daemon and then
* originates, updates and finally deletes an application-specific
@@ -41,17 +41,14 @@
#include "ospfd/ospf_api.h"
#include "ospf_apiclient.h"
-/* privileges struct.
+/* privileges struct.
* set cap_num_* and uid/gid to nothing to use NULL privs
* as ospfapiclient links in libospf.a which uses privs.
*/
-struct zebra_privs_t ospfd_privs =
-{
- .user = NULL,
- .group = NULL,
- .cap_num_p = 0,
- .cap_num_i = 0
-};
+struct zebra_privs_t ospfd_privs = {.user = NULL,
+ .group = NULL,
+ .cap_num_p = 0,
+ .cap_num_i = 0};
/* The following includes are specific to this application. For
example it uses threads from libfrr, however your application is
@@ -73,298 +70,275 @@ struct ospf_apiclient *oclient;
char **args;
/* Our opaque LSAs have the following format. */
-struct my_opaque_lsa
-{
- struct lsa_header hdr; /* include common LSA header */
- u_char data[4]; /* our own data format then follows here */
+struct my_opaque_lsa {
+ struct lsa_header hdr; /* include common LSA header */
+ u_char data[4]; /* our own data format then follows here */
};
/* ---------------------------------------------------------
- * Threads for asynchronous messages and LSA update/delete
+ * Threads for asynchronous messages and LSA update/delete
* ---------------------------------------------------------
*/
-static int
-lsa_delete (struct thread *t)
+static int lsa_delete(struct thread *t)
{
- struct ospf_apiclient *oclient;
- struct in_addr area_id;
- int rc;
-
- oclient = THREAD_ARG (t);
-
- rc = inet_aton (args[6], &area_id);
- if (rc <= 0)
- {
- printf("Address Specified: %s is invalid\n", args[6]);
- return rc;
- }
-
- printf ("Deleting LSA... ");
- rc = ospf_apiclient_lsa_delete (oclient,
- area_id,
- atoi (args[2]), /* lsa type */
- atoi (args[3]), /* opaque type */
- atoi (args[4])); /* opaque ID */
- printf ("done, return code is = %d\n", rc);
- return rc;
+ struct ospf_apiclient *oclient;
+ struct in_addr area_id;
+ int rc;
+
+ oclient = THREAD_ARG(t);
+
+ rc = inet_aton(args[6], &area_id);
+ if (rc <= 0) {
+ printf("Address Specified: %s is invalid\n", args[6]);
+ return rc;
+ }
+
+ printf("Deleting LSA... ");
+ rc = ospf_apiclient_lsa_delete(oclient, area_id,
+ atoi(args[2]), /* lsa type */
+ atoi(args[3]), /* opaque type */
+ atoi(args[4])); /* opaque ID */
+ printf("done, return code is = %d\n", rc);
+ return rc;
}
-static int
-lsa_inject (struct thread *t)
+static int lsa_inject(struct thread *t)
{
- struct ospf_apiclient *cl;
- struct in_addr ifaddr;
- struct in_addr area_id;
- u_char lsa_type;
- u_char opaque_type;
- u_int32_t opaque_id;
- void *opaquedata;
- int opaquelen;
-
- static u_int32_t counter = 1; /* Incremented each time invoked */
- int rc;
-
- cl = THREAD_ARG (t);
-
- rc = inet_aton (args[5], &ifaddr);
- if (rc <= 0)
- {
- printf ("Ifaddr specified %s is invalid\n", args[5]);
- return rc;
- }
-
- rc = inet_aton (args[6], &area_id);
- if (rc <= 0)
- {
- printf( "Area ID specified %s is invalid\n", args[6]);
- return rc;
- }
- lsa_type = atoi (args[2]);
- opaque_type = atoi (args[3]);
- opaque_id = atoi (args[4]);
- opaquedata = &counter;
- opaquelen = sizeof (u_int32_t);
-
- printf ("Originating/updating LSA with counter=%d... ", counter);
- rc = ospf_apiclient_lsa_originate(cl, ifaddr, area_id,
- lsa_type,
- opaque_type, opaque_id,
- opaquedata, opaquelen);
-
- printf ("done, return code is %d\n", rc);
-
- counter++;
-
- return 0;
+ struct ospf_apiclient *cl;
+ struct in_addr ifaddr;
+ struct in_addr area_id;
+ u_char lsa_type;
+ u_char opaque_type;
+ u_int32_t opaque_id;
+ void *opaquedata;
+ int opaquelen;
+
+ static u_int32_t counter = 1; /* Incremented each time invoked */
+ int rc;
+
+ cl = THREAD_ARG(t);
+
+ rc = inet_aton(args[5], &ifaddr);
+ if (rc <= 0) {
+ printf("Ifaddr specified %s is invalid\n", args[5]);
+ return rc;
+ }
+
+ rc = inet_aton(args[6], &area_id);
+ if (rc <= 0) {
+ printf("Area ID specified %s is invalid\n", args[6]);
+ return rc;
+ }
+ lsa_type = atoi(args[2]);
+ opaque_type = atoi(args[3]);
+ opaque_id = atoi(args[4]);
+ opaquedata = &counter;
+ opaquelen = sizeof(u_int32_t);
+
+ printf("Originating/updating LSA with counter=%d... ", counter);
+ rc = ospf_apiclient_lsa_originate(cl, ifaddr, area_id, lsa_type,
+ opaque_type, opaque_id, opaquedata,
+ opaquelen);
+
+ printf("done, return code is %d\n", rc);
+
+ counter++;
+
+ return 0;
}
/* This thread handles asynchronous messages coming in from the OSPF
API server */
-static int
-lsa_read (struct thread *thread)
+static int lsa_read(struct thread *thread)
{
- struct ospf_apiclient *oclient;
- int fd;
- int ret;
+ struct ospf_apiclient *oclient;
+ int fd;
+ int ret;
- printf ("lsa_read called\n");
+ printf("lsa_read called\n");
- oclient = THREAD_ARG (thread);
- fd = THREAD_FD (thread);
+ oclient = THREAD_ARG(thread);
+ fd = THREAD_FD(thread);
- /* Handle asynchronous message */
- ret = ospf_apiclient_handle_async (oclient);
- if (ret < 0) {
- printf ("Connection closed, exiting...");
- exit(0);
- }
+ /* Handle asynchronous message */
+ ret = ospf_apiclient_handle_async(oclient);
+ if (ret < 0) {
+ printf("Connection closed, exiting...");
+ exit(0);
+ }
- /* Reschedule read thread */
- thread_add_read(master, lsa_read, oclient, fd, NULL);
+ /* Reschedule read thread */
+ thread_add_read(master, lsa_read, oclient, fd, NULL);
- return 0;
+ return 0;
}
/* ---------------------------------------------------------
- * Callback functions for asynchronous events
+ * Callback functions for asynchronous events
* ---------------------------------------------------------
*/
-static void
-lsa_update_callback (struct in_addr ifaddr, struct in_addr area_id,
- u_char is_self_originated,
- struct lsa_header *lsa)
+static void lsa_update_callback(struct in_addr ifaddr, struct in_addr area_id,
+ u_char is_self_originated,
+ struct lsa_header *lsa)
{
- printf ("lsa_update_callback: ");
- printf ("ifaddr: %s ", inet_ntoa (ifaddr));
- printf ("area: %s\n", inet_ntoa (area_id));
- printf ("is_self_origin: %u\n", is_self_originated);
-
- /* It is important to note that lsa_header does indeed include the
- header and the LSA payload. To access the payload, first check
- the LSA type and then typecast lsa into the corresponding type,
- e.g.:
-
- if (lsa->type == OSPF_ROUTER_LSA) {
- struct router_lsa *rl = (struct router_lsa) lsa;
- ...
- u_int16_t links = rl->links;
- ...
- }
- */
-
- ospf_lsa_header_dump (lsa);
+ printf("lsa_update_callback: ");
+ printf("ifaddr: %s ", inet_ntoa(ifaddr));
+ printf("area: %s\n", inet_ntoa(area_id));
+ printf("is_self_origin: %u\n", is_self_originated);
+
+ /* It is important to note that lsa_header does indeed include the
+ header and the LSA payload. To access the payload, first check
+ the LSA type and then typecast lsa into the corresponding type,
+ e.g.:
+
+ if (lsa->type == OSPF_ROUTER_LSA) {
+ struct router_lsa *rl = (struct router_lsa) lsa;
+ ...
+ u_int16_t links = rl->links;
+ ...
+ }
+ */
+
+ ospf_lsa_header_dump(lsa);
}
-static void
-lsa_delete_callback (struct in_addr ifaddr, struct in_addr area_id,
- u_char is_self_originated,
- struct lsa_header *lsa)
+static void lsa_delete_callback(struct in_addr ifaddr, struct in_addr area_id,
+ u_char is_self_originated,
+ struct lsa_header *lsa)
{
- printf ("lsa_delete_callback: ");
- printf ("ifaddr: %s ", inet_ntoa (ifaddr));
- printf ("area: %s\n", inet_ntoa (area_id));
- printf ("is_self_origin: %u\n", is_self_originated);
+ printf("lsa_delete_callback: ");
+ printf("ifaddr: %s ", inet_ntoa(ifaddr));
+ printf("area: %s\n", inet_ntoa(area_id));
+ printf("is_self_origin: %u\n", is_self_originated);
- ospf_lsa_header_dump (lsa);
+ ospf_lsa_header_dump(lsa);
}
-static void
-ready_callback (u_char lsa_type, u_char opaque_type, struct in_addr addr)
+static void ready_callback(u_char lsa_type, u_char opaque_type,
+ struct in_addr addr)
{
- printf ("ready_callback: lsa_type: %d opaque_type: %d addr=%s\n",
- lsa_type, opaque_type, inet_ntoa (addr));
+ printf("ready_callback: lsa_type: %d opaque_type: %d addr=%s\n",
+ lsa_type, opaque_type, inet_ntoa(addr));
- /* Schedule opaque LSA originate in 5 secs */
- thread_add_timer(master, lsa_inject, oclient, 5, NULL);
+ /* Schedule opaque LSA originate in 5 secs */
+ thread_add_timer(master, lsa_inject, oclient, 5, NULL);
- /* Schedule opaque LSA update with new value */
- thread_add_timer(master, lsa_inject, oclient, 10, NULL);
+ /* Schedule opaque LSA update with new value */
+ thread_add_timer(master, lsa_inject, oclient, 10, NULL);
- /* Schedule delete */
- thread_add_timer(master, lsa_delete, oclient, 30, NULL);
+ /* Schedule delete */
+ thread_add_timer(master, lsa_delete, oclient, 30, NULL);
}
-static void
-new_if_callback (struct in_addr ifaddr, struct in_addr area_id)
+static void new_if_callback(struct in_addr ifaddr, struct in_addr area_id)
{
- printf ("new_if_callback: ifaddr: %s ", inet_ntoa (ifaddr));
- printf ("area_id: %s\n", inet_ntoa (area_id));
+ printf("new_if_callback: ifaddr: %s ", inet_ntoa(ifaddr));
+ printf("area_id: %s\n", inet_ntoa(area_id));
}
-static void
-del_if_callback (struct in_addr ifaddr)
+static void del_if_callback(struct in_addr ifaddr)
{
- printf ("new_if_callback: ifaddr: %s\n ", inet_ntoa (ifaddr));
+ printf("new_if_callback: ifaddr: %s\n ", inet_ntoa(ifaddr));
}
-static void
-ism_change_callback (struct in_addr ifaddr, struct in_addr area_id,
- u_char state)
+static void ism_change_callback(struct in_addr ifaddr, struct in_addr area_id,
+ u_char state)
{
- printf ("ism_change: ifaddr: %s ", inet_ntoa (ifaddr));
- printf ("area_id: %s\n", inet_ntoa (area_id));
- printf ("state: %d [%s]\n", state, lookup_msg(ospf_ism_state_msg, state, NULL));
+ printf("ism_change: ifaddr: %s ", inet_ntoa(ifaddr));
+ printf("area_id: %s\n", inet_ntoa(area_id));
+ printf("state: %d [%s]\n", state,
+ lookup_msg(ospf_ism_state_msg, state, NULL));
}
-static void
-nsm_change_callback (struct in_addr ifaddr, struct in_addr nbraddr,
- struct in_addr router_id, u_char state)
+static void nsm_change_callback(struct in_addr ifaddr, struct in_addr nbraddr,
+ struct in_addr router_id, u_char state)
{
- printf ("nsm_change: ifaddr: %s ", inet_ntoa (ifaddr));
- printf ("nbraddr: %s\n", inet_ntoa (nbraddr));
- printf ("router_id: %s\n", inet_ntoa (router_id));
- printf ("state: %d [%s]\n", state, lookup_msg(ospf_nsm_state_msg, state, NULL));
+ printf("nsm_change: ifaddr: %s ", inet_ntoa(ifaddr));
+ printf("nbraddr: %s\n", inet_ntoa(nbraddr));
+ printf("router_id: %s\n", inet_ntoa(router_id));
+ printf("state: %d [%s]\n", state,
+ lookup_msg(ospf_nsm_state_msg, state, NULL));
}
/* ---------------------------------------------------------
- * Main program
+ * Main program
* ---------------------------------------------------------
*/
static int usage(void)
{
- printf("Usage: ospfclient <ospfd> <lsatype> <opaquetype> <opaqueid> <ifaddr> <areaid>\n");
- printf("where ospfd : router where API-enabled OSPF daemon is running\n");
- printf(" lsatype : either 9, 10, or 11 depending on flooding scope\n");
- printf(" opaquetype: 0-255 (e.g., experimental applications use > 128)\n");
- printf(" opaqueid : arbitrary application instance (24 bits)\n");
- printf(" ifaddr : interface IP address (for type 9) otherwise ignored\n");
- printf(" areaid : area in IP address format (for type 10) otherwise ignored\n");
-
- exit(1);
+ printf("Usage: ospfclient <ospfd> <lsatype> <opaquetype> <opaqueid> <ifaddr> <areaid>\n");
+ printf("where ospfd : router where API-enabled OSPF daemon is running\n");
+ printf(" lsatype : either 9, 10, or 11 depending on flooding scope\n");
+ printf(" opaquetype: 0-255 (e.g., experimental applications use > 128)\n");
+ printf(" opaqueid : arbitrary application instance (24 bits)\n");
+ printf(" ifaddr : interface IP address (for type 9) otherwise ignored\n");
+ printf(" areaid : area in IP address format (for type 10) otherwise ignored\n");
+
+ exit(1);
}
-int
-main (int argc, char *argv[])
+int main(int argc, char *argv[])
{
- struct thread thread;
-
- args = argv;
-
- /* ospfclient should be started with the following arguments:
- *
- * (1) host (2) lsa_type (3) opaque_type (4) opaque_id (5) if_addr
- * (6) area_id
- *
- * host: name or IP of host where ospfd is running
- * lsa_type: 9, 10, or 11
- * opaque_type: 0-255 (e.g., experimental applications use > 128)
- * opaque_id: arbitrary application instance (24 bits)
- * if_addr: interface IP address (for type 9) otherwise ignored
- * area_id: area in IP address format (for type 10) otherwise ignored
- */
-
- if (argc != 7)
- {
- usage();
- }
-
- /* Initialization */
- zprivs_init (&ospfd_privs);
- master = thread_master_create(NULL);
-
- /* Open connection to OSPF daemon */
- oclient = ospf_apiclient_connect (args[1], ASYNCPORT);
- if (!oclient)
- {
- printf ("Connecting to OSPF daemon on %s failed!\n",
- args[1]);
- exit (1);
- }
-
- /* Register callback functions. */
- ospf_apiclient_register_callback (oclient,
- ready_callback,
- new_if_callback,
- del_if_callback,
- ism_change_callback,
- nsm_change_callback,
- lsa_update_callback,
- lsa_delete_callback);
-
- /* Register LSA type and opaque type. */
- ospf_apiclient_register_opaque_type (oclient, atoi (args[2]),
- atoi (args[3]));
-
- /* Synchronize database with OSPF daemon. */
- ospf_apiclient_sync_lsdb (oclient);
-
- /* Schedule thread that handles asynchronous messages */
- thread_add_read(master, lsa_read, oclient, oclient->fd_async, NULL);
-
- /* Now connection is established, run loop */
- while (1)
- {
- thread_fetch (master, &thread);
- thread_call (&thread);
- }
-
- /* Never reached */
- return 0;
+ struct thread thread;
+
+ args = argv;
+
+ /* ospfclient should be started with the following arguments:
+ *
+ * (1) host (2) lsa_type (3) opaque_type (4) opaque_id (5) if_addr
+ * (6) area_id
+ *
+ * host: name or IP of host where ospfd is running
+ * lsa_type: 9, 10, or 11
+ * opaque_type: 0-255 (e.g., experimental applications use > 128)
+ * opaque_id: arbitrary application instance (24 bits)
+ * if_addr: interface IP address (for type 9) otherwise ignored
+ * area_id: area in IP address format (for type 10) otherwise ignored
+ */
+
+ if (argc != 7) {
+ usage();
+ }
+
+ /* Initialization */
+ zprivs_init(&ospfd_privs);
+ master = thread_master_create(NULL);
+
+ /* Open connection to OSPF daemon */
+ oclient = ospf_apiclient_connect(args[1], ASYNCPORT);
+ if (!oclient) {
+ printf("Connecting to OSPF daemon on %s failed!\n", args[1]);
+ exit(1);
+ }
+
+ /* Register callback functions. */
+ ospf_apiclient_register_callback(
+ oclient, ready_callback, new_if_callback, del_if_callback,
+ ism_change_callback, nsm_change_callback, lsa_update_callback,
+ lsa_delete_callback);
+
+ /* Register LSA type and opaque type. */
+ ospf_apiclient_register_opaque_type(oclient, atoi(args[2]),
+ atoi(args[3]));
+
+ /* Synchronize database with OSPF daemon. */
+ ospf_apiclient_sync_lsdb(oclient);
+
+ /* Schedule thread that handles asynchronous messages */
+ thread_add_read(master, lsa_read, oclient, oclient->fd_async, NULL);
+
+ /* Now connection is established, run loop */
+ while (1) {
+ thread_fetch(master, &thread);
+ thread_call(&thread);
+ }
+
+ /* Never reached */
+ return 0;
}
-