]> git.ipfire.org Git - thirdparty/dhcp.git/blob - omapip/array.c
[master] Add patch to limit the value of an fd we accept for a connection.
[thirdparty/dhcp.git] / omapip / array.c
1 /* listener.c
2
3 Subroutines that support the omapi extensible array type. */
4
5 /*
6 * Copyright (c) 2004-2007,2009,2014 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 2001-2003 by Internet Software Consortium
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 *
21 * Internet Systems Consortium, Inc.
22 * 950 Charter Street
23 * Redwood City, CA 94063
24 * <info@isc.org>
25 * https://www.isc.org/
26 *
27 */
28
29 #include "dhcpd.h"
30
31 #include <omapip/omapip_p.h>
32
33 /* Allocate a new extensible array. */
34
35 isc_result_t omapi_array_allocate (omapi_array_t **array,
36 omapi_array_ref_t ref,
37 omapi_array_deref_t deref,
38 const char *file, int line)
39 {
40 omapi_array_t *aptr;
41
42 if (!array || *array)
43 return DHCP_R_INVALIDARG;
44 aptr = dmalloc (sizeof (omapi_array_t),file, line);
45 if (!aptr)
46 return ISC_R_NOMEMORY;
47 *array = aptr;
48 aptr -> ref = ref;
49 aptr -> deref = deref;
50 return ISC_R_SUCCESS;
51 }
52
53 isc_result_t omapi_array_free (omapi_array_t **array,
54 const char *file, int line)
55 {
56 omapi_array_t *aptr;
57 int i;
58
59 if (!array || !*array)
60 return DHCP_R_INVALIDARG;
61 aptr = *array;
62 for (i = 0; i < aptr -> count; i++)
63 if (aptr -> data [i] && aptr -> deref)
64 (*aptr -> deref) (&aptr -> data [i], file, line);
65 dfree (aptr -> data, MDL);
66 dfree (aptr, MDL);
67 *array = (omapi_array_t *)0;
68 return ISC_R_SUCCESS;
69 }
70
71 /* Extend the size of the array by one entry (we may allocate more than that)
72 and store the specified value in the new array element. */
73
74 isc_result_t omapi_array_extend (omapi_array_t *array, char *ptr,
75 int *index, const char *file, int line)
76 {
77 isc_result_t status;
78 int new = array -> count;
79 status = omapi_array_set (array, ptr, new, file, line);
80 if (index && status == ISC_R_SUCCESS)
81 *index = new;
82 return status;
83 }
84
85 /* Set a value in the specified array, extending it if necessary. */
86
87 isc_result_t omapi_array_set (omapi_array_t *array, void *ptr, int index,
88 const char *file, int line)
89 {
90 char **newbuf;
91 int delta;
92 isc_result_t status;
93
94 if (!array)
95 return DHCP_R_INVALIDARG;
96 if (!ptr)
97 return DHCP_R_INVALIDARG;
98 if (index < 0)
99 return DHCP_R_INVALIDARG;
100
101 /* If the proposed index is larger than the current available
102 space in the array, make more space in the array. */
103 if (array -> max <= index) {
104 delta = index - array -> max + 10;
105 newbuf = dmalloc ((array -> max + delta) * sizeof (char *),
106 file, line);
107 if (!newbuf)
108 return ISC_R_NOMEMORY;
109 /* Zero the new elements. */
110 memset (&newbuf [array -> max], 0, (sizeof (char *)) * delta);
111 array -> max += delta;
112 /* Copy the old array data into the new buffer. */
113 if (array -> data) {
114 memcpy (newbuf,
115 array -> data, array -> count * sizeof (char *));
116 dfree (array -> data, file, line);
117 }
118 array -> data = newbuf;
119 } else {
120 /* If there's already data there, and this is an array
121 of references, dereference what's there. */
122 if (array -> data [index]) {
123 status = ((*array -> deref) (&array -> data [index],
124 file, line));
125
126 if (status != ISC_R_SUCCESS)
127 return status;
128 }
129 }
130
131 /* Store the pointer using the referencer function. We have
132 either just memset this to zero or dereferenced what was
133 there previously, so there is no need to do anything if the
134 pointer we have been asked to store is null. */
135 if (ptr) {
136 status = (*array -> ref) (&array -> data [index], ptr,
137 file, line);
138 if (status != ISC_R_SUCCESS)
139 return status;
140 }
141 if (index >= array -> count)
142 array -> count = index + 1;
143 return ISC_R_SUCCESS;
144 }
145
146 isc_result_t omapi_array_lookup (char **ptr, omapi_array_t *array, int index,
147 const char *file, int line)
148 {
149 if (!array || !ptr || *ptr || index < 0 || index >= array -> count)
150 return DHCP_R_INVALIDARG;
151 if (array -> data [index])
152 return (*array -> ref) (ptr,
153 array -> data [index], file, line);
154 return ISC_R_NOTFOUND;
155 }
156