1 /*
2 *
3 * Copyright (c) 2003 The Regents of the University of California. All
4 * rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * - Neither the name of the University nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31
32 /*
33 * liblink/link_neighbor.c: A helper library for talking to the
34 * neighbor discovery daemon.
35 *
36 * $Id: neighbor_client.c,v 1.13 2007-12-08 04:05:04 girod Exp $
37 */
38
39 char neighbor_c_cvsid[] = "$Id: neighbor_client.c,v 1.13 2007-12-08 04:05:04 girod Exp $";
40
41 #include <stdio.h>
42 #include <stdlib.h>
43
44 #include "libdev/status_client.h"
45 #include "link/neighbor.h"
46
47 #define DEFAULT_NEIGHBOR_REFRESH 10000
48
49 static void neighborlist_destroy(void *data, g_event_t *event)
50 {
51 /* free the copy of the opts that we made when the event was created */
52 free(data);
53 }
54
55
56 /* When the status client code tells us that we have new data */
57 static int neighborlist_dispatch(void *new_buffer, size_t size, void *data)
58 {
59 int retval = EVENT_RENEW;
60 neighbor_opts_t *opts = (neighbor_opts_t *) data;
61
62 if (size % sizeof(neighbor_t)) {
63 elog(LOG_CRIT, "got corrupt neighborlist size %d!!", (int) size);
64 goto done;
65 }
66
67 if (opts->new_list)
68 retval = (opts->new_list)((neighbor_t *) new_buffer, size/sizeof(neighbor_t),
69 opts->data);
70
71 done:
72 return retval;
73 }
74
75
76 /*
77 * create an event that fires a user callback every time we get a new
78 * neighborlist from the neighborlist service
79 */
80 int g_neighbors(neighbor_opts_t *neighbor_opts, status_client_context_t **ref)
81 {
82 int retval;
83 neighbor_opts_t *opts_copy = malloc(sizeof(neighbor_opts_t));
84 status_client_opts_t sc_opts = {
85 handler: neighborlist_dispatch,
86 reread_period: (neighbor_opts->refresh_rate
87 ? neighbor_opts->refresh_rate : DEFAULT_NEIGHBOR_REFRESH),
88 private_data: opts_copy,
89 destroy: neighborlist_destroy,
90 no_init_crashproof: neighbor_opts->no_init_crashproof
91 };
92
93 /* make sure malloc didn't fail */
94 if (opts_copy == NULL) {
95 errno = ENOMEM;
96 retval = -1;
97 goto fail;
98 }
99
100 /* make sure user provided options */
101 if (neighbor_opts == NULL) {
102 errno = EINVAL;
103 retval = -1;
104 goto fail;
105 }
106
107 /* compute the neighbors link name */
108 sc_opts.devname = link_name_s(neighbor_opts->link_name, LINK_NEIGHBORS_SUBDEV);
109
110 /* make a copy of the neighbor options */
111 *opts_copy = *neighbor_opts;
112
113 /* try to open the file */
114 return g_status_client_full(&sc_opts, ref);
115
116 fail:
117 if (retval < 0) {
118 if (opts_copy)
119 free(opts_copy);
120 }
121 return retval;
122 }
123
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.