comparison packages/net/snmp/lib/current/src/callback.c @ 103:95f3e12a6327 ecos-sw-2000-06-23

Merge from eCos master repository on 2000-06-23-16:41:10-BST
author jlarmour
date Fri, 23 Jun 2000 17:06:31 +0000
parents
children e0c0827131d1
comparison
equal deleted inserted replaced
102:6409b6d94dd7 103:95f3e12a6327
1 //==========================================================================
2 //
3 // ./lib/current/src/callback.c
4 //
5 //
6 //==========================================================================
7 //####COPYRIGHTBEGIN####
8 //
9 // -------------------------------------------
10 // The contents of this file are subject to the Red Hat eCos Public License
11 // Version 1.1 (the "License"); you may not use this file except in
12 // compliance with the License. You may obtain a copy of the License at
13 // http://www.redhat.com/
14 //
15 // Software distributed under the License is distributed on an "AS IS"
16 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
17 // License for the specific language governing rights and limitations under
18 // the License.
19 //
20 // The Original Code is eCos - Embedded Configurable Operating System,
21 // released September 30, 1998.
22 //
23 // The Initial Developer of the Original Code is Red Hat.
24 // Portions created by Red Hat are
25 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
26 // All Rights Reserved.
27 // -------------------------------------------
28 //
29 //####COPYRIGHTEND####
30 //####UCDSNMPCOPYRIGHTBEGIN####
31 //
32 // -------------------------------------------
33 //
34 // Portions of this software may have been derived from the UCD-SNMP
35 // project, <http://ucd-snmp.ucdavis.edu/> from the University of
36 // California at Davis, which was originally based on the Carnegie Mellon
37 // University SNMP implementation. Portions of this software are therefore
38 // covered by the appropriate copyright disclaimers included herein.
39 //
40 // The release used was version 4.1.2 of May 2000. "ucd-snmp-4.1.2"
41 // -------------------------------------------
42 //
43 //####UCDSNMPCOPYRIGHTEND####
44 //==========================================================================
45 //#####DESCRIPTIONBEGIN####
46 //
47 // Author(s): hmt
48 // Contributors: hmt
49 // Date: 2000-05-30
50 // Purpose: Port of UCD-SNMP distribution to eCos.
51 // Description:
52 //
53 //
54 //####DESCRIPTIONEND####
55 //
56 //==========================================================================
57 /********************************************************************
58 Copyright 1989, 1991, 1992 by Carnegie Mellon University
59
60 Derivative Work -
61 Copyright 1996, 1998, 1999, 2000 The Regents of the University of California
62
63 All Rights Reserved
64
65 Permission to use, copy, modify and distribute this software and its
66 documentation for any purpose and without fee is hereby granted,
67 provided that the above copyright notice appears in all copies and
68 that both that copyright notice and this permission notice appear in
69 supporting documentation, and that the name of CMU and The Regents of
70 the University of California not be used in advertising or publicity
71 pertaining to distribution of the software without specific written
72 permission.
73
74 CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALL
75 WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
76 WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL CMU OR
77 THE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,
78 INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
79 FROM THE LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
80 CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
81 CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
82 *********************************************************************/
83 /* callback.c: A generic callback mechanism */
84
85 #include <config.h>
86 #include <sys/types.h>
87 #include <stdio.h>
88 #if HAVE_STDLIB_H
89 #include <stdlib.h>
90 #endif
91 #if HAVE_WINSOCK_H
92 #include <winsock.h>
93 #endif
94 #if HAVE_NETINET_IN_H
95 #include <netinet/in.h>
96 #endif
97 #if HAVE_STRING_H
98 #include <string.h>
99 #else
100 #include <strings.h>
101 #endif
102
103 #if HAVE_DMALLOC_H
104 #include <dmalloc.h>
105 #endif
106
107 #include "tools.h"
108 #include "callback.h"
109 #include "asn1.h"
110 #include "snmp_api.h"
111 #include "snmp_debug.h"
112
113 static struct snmp_gen_callback *thecallbacks[MAX_CALLBACK_IDS][MAX_CALLBACK_SUBIDS];
114
115 /* the chicken. or the egg. You pick. */
116 void
117 init_callbacks(void) {
118 /* probably not needed? Should be full of 0's anyway? */
119 /* (poses a problem if you put init_callbacks() inside of
120 init_snmp() and then want the app to register a callback before
121 init_snmp() is called in the first place. -- Wes */
122 /* memset(thecallbacks, 0, sizeof(thecallbacks)); */
123 }
124
125 int
126 snmp_register_callback(int major, int minor, SNMPCallback *new_callback,
127 void *arg) {
128
129 struct snmp_gen_callback *scp;
130
131 if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
132 return SNMPERR_GENERR;
133 }
134
135 if (thecallbacks[major][minor] != NULL) {
136 /* get to the end of the list */
137 for(scp = thecallbacks[major][minor]; scp->next != NULL; scp = scp->next);
138
139 /* mallocate a new entry */
140 scp->next = SNMP_MALLOC_STRUCT(snmp_gen_callback);
141 scp = scp->next;
142 } else {
143 /* mallocate a new entry */
144 scp = SNMP_MALLOC_STRUCT(snmp_gen_callback);
145
146 /* make the new node the head */
147 thecallbacks[major][minor] = scp;
148 }
149
150 if (scp == NULL)
151 return SNMPERR_GENERR;
152
153 scp->sc_client_arg = arg;
154 scp->sc_callback = new_callback;
155
156 DEBUGMSGTL(("callback","registered callback for maj=%d min=%d\n",
157 major, minor));
158
159 return SNMPERR_SUCCESS;
160 }
161
162 int
163 snmp_call_callbacks(int major, int minor, void *caller_arg) {
164 struct snmp_gen_callback *scp;
165
166 if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
167 return SNMPERR_GENERR;
168 }
169
170 DEBUGMSGTL(("callback","START calling callbacks for maj=%d min=%d\n",
171 major, minor));
172
173 /* for each registered callback of type major and minor */
174 for(scp = thecallbacks[major][minor]; scp != NULL; scp = scp->next) {
175
176 DEBUGMSGTL(("callback","calling a callback for maj=%d min=%d\n",
177 major, minor));
178
179 /* call them */
180 (*(scp->sc_callback))(major, minor, caller_arg, scp->sc_client_arg);
181 }
182
183 DEBUGMSGTL(("callback","END calling callbacks for maj=%d min=%d\n",
184 major, minor));
185
186 return SNMPERR_SUCCESS;
187 }