summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.lib/mipagent/snmp_maAdvConfigEntry.c
blob: 8952083e18d814e6966d0efb47aa80e15ae74e1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1999-2001 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * file: mipagentsnmp_maAdvConfigEntry.c
 *
 * This file contains the SNMP routines used to retrieve
 * the Mobility Agent's Interface Information.
 */

#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <impl.h>
#include <snmp.h>

#include "snmp_stub.h"
#include "agent.h"

extern HashTable maAdvConfigHash;
extern char *ntoa(uint32_t, char *);

/*
 * Function: get_maAdvConfigEntry
 *
 * Arguments:	search_type - The type of search (first, next, exact)
 *		maAdvConfigEntry_data - Pointer to a pointer which
 *			will contain the interface entry upon
 *			successful completion.
 *		index - Pointer to the current index
 *
 * Description: This function will allocate the memory required for
 *		the interface entry, and will find the appropriate
 *		interface entry based on the index provided. If the
 *		search type is set to FIRST_ENTRY, we will return
 *		the first interface entry in the Hash Table, otherwise
 *		the index is used.
 *
 *		The interface entry is then setup with the values found
 *		in the entry from the hash table and returned to the
 *		caller.
 *
 *		Note, the caller is responsible for either freeing the
 *		memory, or calling free_maAdvConfigEntry()
 *
 * Returns: int, 0 if successful
 */
extern int
get_maAdvConfigEntry(int search_type,
    MaAdvConfigEntry_t **maAdvConfigEntry_data,
    IndexType *index)
{
	Integer *integer;
	String  *string;
	MaAdvConfigEntry *maAdvConfigEntry = NULL;
	HashEntry *hashEntry;
	int i;
	int j;
	int len;
	int return_code = SNMP_ERR_NOERROR;
	char buffer[258];
	boolean_t found = _B_FALSE;

	*maAdvConfigEntry_data =
	    (MaAdvConfigEntry_t *)calloc(1, sizeof (MaAdvConfigEntry_t));

	if (maAdvConfigEntry_data == NULL) {
		return (SNMP_ERR_GENERR);
	}

	/*
	 * In the case, the search_type is FIRST_ENTRY or NEXT_ENTRY
	 * this function should modify the index argument to the
	 * appropriate value
	 */
	switch (search_type) {
	case FIRST_ENTRY:
		/*
		 * We are looking for the first entry in the list.
		 */
		index->value[0] = 1;
		index->len = 1;
		break;
	case NEXT_ENTRY:
		/*
		 * Increment the index value.
		 */
		index->value[0]++;
		break;
	case EXACT_ENTRY:
		/*
		 * We don't need to play around with the
		 * index for this search type.
		 */
		break;
	default:
		return_code = SNMP_ERR_GENERR;
		goto the_end;
	}

	/*
	 * Now search for the entry...
	 */
	for (i = 0, j = 1; i < HASH_TBL_SIZE && found == _B_FALSE; i++) {
		if (maAdvConfigHash.buckets[i]) {
			/*
			 * Lock the bucket
			 */
			(void) rw_rdlock(&maAdvConfigHash.bucketLock[i]);

			hashEntry = maAdvConfigHash.buckets[i];
			while (hashEntry != NULL) {
				if (j == index->value[0]) {
					maAdvConfigEntry = hashEntry->data;
					found = _B_TRUE;

					/*
					 * Lock the node
					 */
					(void) rw_rdlock(&maAdvConfigEntry->
					    maIfaceNodeLock);

					break;
				}
				hashEntry = hashEntry->next;
				j++;
			}

			/*
			 * Unlock the bucket
			 */
			(void) rw_unlock(&maAdvConfigHash.bucketLock[i]);
		}
	}

	if (maAdvConfigEntry == NULL) {
		return_code = END_OF_TABLE;
		goto the_end;
	}

	/*
	 * And now we return the Maximum Registration Lifetime.
	 */
	integer = &((*maAdvConfigEntry_data)->maAdvMaxRegLifetime);
	*integer = (int)maAdvConfigEntry->maAdvMaxRegLifetime;

	/*
	 * And now we return the Prefix Length Inclusion.
	 */
	integer = &((*maAdvConfigEntry_data)->maAdvPrefixLengthInclusion);
	if (maAdvConfigEntry->maAdvPrefixLenInclusion) {
		*integer = 1;
	} else {
		/* I need to validate this one. Should this be 2 or 0 ? */
		*integer = 2;
	}

	/*
	 * Get the Advertised Address.
	 */
	(void) ntoa(maAdvConfigEntry->maAdvAddr, buffer);
	len = strlen(buffer);
	string = &((*maAdvConfigEntry_data)->maAdvAddress);
	string->chars = (unsigned char *)calloc(1, len);
	if (string == NULL) {
		return_code = SNMP_ERR_GENERR;
		goto the_end;
	}
	(void) memcpy(string->chars, buffer, len);
	string->len = len;

	/*
	 * And now we return the Maximum Advertisement Interval.
	 */
	integer = &((*maAdvConfigEntry_data)->maAdvMaxInterval);
	*integer = (int)maAdvConfigEntry->maAdvMaxInterval;

	/*
	 * And now we return the Minimum Advertisement Interval.
	 */
	integer = &((*maAdvConfigEntry_data)->maAdvMinInterval);
	*integer = (int)maAdvConfigEntry->maAdvMinInterval;

#if 0
	/*
	 * And now we return whether we only advertise if we receive
	 * a soliciation.
	 */
	integer = &((*maAdvConfigEntry_data)->maAdvResponseSolicitationOnly);

	if (maAdvConfigEntry->maAdvResponseSolicitationOnly) {
		*integer = 1;
	} else {
		/* I need to validate this one. Should this be 2 or 0 ? */
		*integer = 2;
	}
#endif

	/*
	 * And now we return whether the entry is active. In our case,
	 * all entries are active, so we can hardcode the value.
	 */
	integer = &((*maAdvConfigEntry_data)->maAdvStatus);
	*integer = 1;

the_end:
	if (maAdvConfigEntry != NULL) {
		/*
		 * Unlock the node
		 */
		(void) rw_unlock(&maAdvConfigEntry->maIfaceNodeLock);
	}

	if (return_code != SNMP_ERR_NOERROR) {
		free_maAdvConfigEntry(*maAdvConfigEntry_data);
		*maAdvConfigEntry_data = NULL;
	}

	(void) fprintf(stderr, "get_maAdvConfigEntry: returning %d (%p)\n",
	    return_code, (void *)*maAdvConfigEntry_data);

	return (return_code);
}


/*
 * Function: free_maAdvConfigEntry
 *
 * Arguments:	maAdvConfigEntry - Pointer to a previously
 *			allocated SNMP interface entry
 *
 * Description: This function is called to free a previously
 *		allocated SNMP interface entry.
 *
 * Returns:
 */
void
free_maAdvConfigEntry(MaAdvConfigEntry_t *maAdvConfigEntry)
{
	String  *string;

	if (maAdvConfigEntry) {
		/*
		 * The template generates code that checks both
		 * the pointer, and the length. I am not sure
		 * if this is excessive, so I will leave it as is.
		 */
		string = &(maAdvConfigEntry->maAdvAddress);
		if (string->chars != NULL && string->len != 0) {
			free(string->chars);
			string->len = 0;
		}

		free(maAdvConfigEntry);
		maAdvConfigEntry = NULL;
	}
}