ISC DHCP  4.3.0
A reference DHCPv4 and DHCPv6 implementation
salloc.c
Go to the documentation of this file.
1 /* salloc.c
2 
3  Memory allocation for the DHCP server... */
4 
5 /*
6  * Copyright (c) 2009,2012,2014 by Internet Systems Consortium, Inc. ("ISC")
7  * Copyright (c) 2004-2007 by Internet Systems Consortium, Inc. ("ISC")
8  * Copyright (c) 1996-2003 by Internet Software Consortium
9  *
10  * Permission to use, copy, modify, and distribute this software for any
11  * purpose with or without fee is hereby granted, provided that the above
12  * copyright notice and this permission notice appear in all copies.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  *
22  * Internet Systems Consortium, Inc.
23  * 950 Charter Street
24  * Redwood City, CA 94063
25  * <info@isc.org>
26  * https://www.isc.org/
27  *
28  */
29 
30 #include "dhcpd.h"
31 #include <omapip/omapip_p.h>
32 
33 #if defined (COMPACT_LEASES)
34 struct lease *free_leases;
35 
36 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
37 struct lease *lease_hunks;
38 
40 {
41  struct lease *c, *n, **p;
42  int i;
43 
44  /* Account for all the leases on the free list. */
45  for (n = lease_hunks; n; n = n->next) {
46  for (i = 1; i < n->starts + 1; i++) {
47  p = &free_leases;
48  for (c = free_leases; c; c = c->next) {
49  if (c == &n[i]) {
50  *p = c->next;
51  n->ends++;
52  break;
53  }
54  p = &c->next;
55  }
56  if (!c) {
57  log_info("lease %s refcnt %d",
58  piaddr (n[i].ip_addr), n[i].refcnt);
59 #if defined (DEBUG_RC_HISTORY)
60  dump_rc_history(&n[i]);
61 #endif
62  }
63  }
64  }
65 
66  for (c = lease_hunks; c; c = n) {
67  n = c->next;
68  if (c->ends != c->starts) {
69  log_info("lease hunk %lx leases %ld free %ld",
70  (unsigned long)c, (unsigned long)(c->starts),
71  (unsigned long)(c->ends));
72  }
73  dfree(c, MDL);
74  }
75 
76  /* Free all the rogue leases. */
77  for (c = free_leases; c; c = n) {
78  n = c->next;
79  dfree(c, MDL);
80  }
81 }
82 #endif
83 
84 struct lease *new_leases (n, file, line)
85  unsigned n;
86  const char *file;
87  int line;
88 {
89  struct lease *rval;
90 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
91  rval = dmalloc ((n + 1) * sizeof (struct lease), file, line);
92  memset (rval, 0, sizeof (struct lease));
93  rval -> starts = n;
94  rval -> next = lease_hunks;
95  lease_hunks = rval;
96  rval++;
97 #else
98  rval = dmalloc (n * sizeof (struct lease), file, line);
99 #endif
100  return rval;
101 }
102 
103 /* If we are allocating leases in aggregations, there's really no way
104  to free one, although perhaps we can maintain a free list. */
105 
106 isc_result_t dhcp_lease_free (omapi_object_t *lo,
107  const char *file, int line)
108 {
109  struct lease *lease;
110  if (lo -> type != dhcp_type_lease)
111  return DHCP_R_INVALIDARG;
112  lease = (struct lease *)lo;
113  memset (lease, 0, sizeof (struct lease));
114  lease -> next = free_leases;
115  free_leases = lease;
116  return ISC_R_SUCCESS;
117 }
118 
119 isc_result_t dhcp_lease_get (omapi_object_t **lp,
120  const char *file, int line)
121 {
122  struct lease **lease = (struct lease **)lp;
123  struct lease *lt;
124 
125  if (free_leases) {
126  lt = free_leases;
127  free_leases = lt -> next;
128  *lease = lt;
129  return ISC_R_SUCCESS;
130  }
131  return ISC_R_NOMEMORY;
132 }
133 #endif /* COMPACT_LEASES */
134 
135 OMAPI_OBJECT_ALLOC (lease, struct lease, dhcp_type_lease)
136 OMAPI_OBJECT_ALLOC (class, struct class, dhcp_type_class)
137 OMAPI_OBJECT_ALLOC (subclass, struct class, dhcp_type_subclass)
139 
140 #if !defined (NO_HOST_FREES) /* Scary debugging mode - don't enable! */
142 #else
143 isc_result_t host_allocate (struct host_decl **p, const char *file, int line)
144 {
146  dhcp_type_host, 0, file, line);
147 }
148 
149 isc_result_t host_reference (struct host_decl **pptr, struct host_decl *ptr,
150  const char *file, int line)
151 {
152  return omapi_object_reference ((omapi_object_t **)pptr,
153  (omapi_object_t *)ptr, file, line);
154 }
155 
156 isc_result_t host_dereference (struct host_decl **ptr,
157  const char *file, int line)
158 {
159  if ((*ptr) -> refcnt == 1) {
160  log_error ("host dereferenced with refcnt == 1.");
161 #if defined (DEBUG_RC_HISTORY)
162  dump_rc_history ();
163 #endif
164  abort ();
165  }
166  return omapi_object_dereference ((omapi_object_t **)ptr, file, line);
167 }
168 #endif
169 
171 
172 struct lease_state *new_lease_state (file, line)
173  const char *file;
174  int line;
175 {
176  struct lease_state *rval;
177 
178  if (free_lease_states) {
179  rval = free_lease_states;
180  free_lease_states =
181  (struct lease_state *)(free_lease_states -> next);
182  dmalloc_reuse (rval, file, line, 0);
183  } else {
184  rval = dmalloc (sizeof (struct lease_state), file, line);
185  if (!rval)
186  return rval;
187  }
188  memset (rval, 0, sizeof *rval);
189  if (!option_state_allocate (&rval -> options, file, line)) {
190  free_lease_state (rval, file, line);
191  return (struct lease_state *)0;
192  }
193  return rval;
194 }
195 
196 void free_lease_state (ptr, file, line)
197  struct lease_state *ptr;
198  const char *file;
199  int line;
200 {
201  if (ptr -> options)
202  option_state_dereference (&ptr -> options, file, line);
203  if (ptr -> packet)
204  packet_dereference (&ptr -> packet, file, line);
205  if (ptr -> shared_network)
206  shared_network_dereference (&ptr -> shared_network,
207  file, line);
208 
209  data_string_forget (&ptr -> parameter_request_list, file, line);
210  data_string_forget (&ptr -> filename, file, line);
211  data_string_forget (&ptr -> server_name, file, line);
212  ptr -> next = free_lease_states;
213  free_lease_states = ptr;
214  dmalloc_reuse (free_lease_states, (char *)0, 0, 0);
215 }
216 
217 #if defined (DEBUG_MEMORY_LEAKAGE) || \
218  defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
219 void relinquish_free_lease_states ()
220 {
221  struct lease_state *cs, *ns;
222 
223  for (cs = free_lease_states; cs; cs = ns) {
224  ns = cs -> next;
225  dfree (cs, MDL);
226  }
227  free_lease_states = (struct lease_state *)0;
228 }
229 #endif
230 
231 struct permit *new_permit (file, line)
232  const char *file;
233  int line;
234 {
235  struct permit *permit = ((struct permit *)
236  dmalloc (sizeof (struct permit), file, line));
237  if (!permit)
238  return permit;
239  memset (permit, 0, sizeof *permit);
240  return permit;
241 }
242 
243 void free_permit (permit, file, line)
244  struct permit *permit;
245  const char *file;
246  int line;
247 {
248  if (permit -> type == permit_class)
249  class_dereference (&permit -> class, MDL);
250  dfree (permit, file, line);
251 }
struct lease_state * next
Definition: dhcpd.h:594
struct lease * new_leases(unsigned, const char *, int)
const char int line
Definition: dhcpd.h:3535
Definition: dhcpd.h:507
isc_result_t omapi_object_reference(omapi_object_t **, omapi_object_t *, const char *, int)
Definition: alloc.c:557
const char * piaddr(const struct iaddr addr)
Definition: inet.c:581
omapi_object_type_t * dhcp_type_pool
Definition: omapi.c:45
void * dmalloc(unsigned, const char *, int)
Definition: alloc.c:56
struct iaddr ip_addr(struct iaddr subnet, struct iaddr mask, u_int32_t host_address)
Definition: inet.c:65
#define MDL
Definition: omapip.h:568
struct data_string parameter_request_list
Definition: dhcpd.h:603
#define DHCP_R_INVALIDARG
Definition: result.h:48
omapi_object_type_t * dhcp_type_lease
Definition: omapi.c:44
void data_string_forget(struct data_string *data, const char *file, int line)
Definition: alloc.c:1276
int log_error(const char *,...) __attribute__((__format__(__printf__
void relinquish_lease_hunks(void)
void free_permit(struct permit *permit, const char *file, int line)
Definition: salloc.c:243
#define OMAPI_OBJECT_ALLOC(name, stype, type)
Definition: omapip.h:161
struct permit * new_permit(char *file, int line) const
Definition: salloc.c:231
struct option_state * options
Definition: dhcpd.h:602
int option_state_allocate(struct option_state **ptr, const char *file, int line)
Definition: alloc.c:847
Definition: dhcpd.h:904
struct lease_state * free_lease_states
Definition: salloc.c:170
Definition: dhcpd.h:369
isc_result_t omapi_object_dereference(omapi_object_t **, const char *, int)
Definition: alloc.c:579
omapi_object_type_t * dhcp_type_subclass
Definition: omapi.c:47
TIME starts
Definition: dhcpd.h:513
omapi_object_type_t * dhcp_type_host
Definition: mdb.c:71
void dfree(void *, const char *, int)
Definition: alloc.c:131
enum permit::@0 type
void free_lease_state(struct lease_state *ptr, const char *file, int line)
Definition: salloc.c:196
int int log_info(const char *,...) __attribute__((__format__(__printf__
omapi_object_type_t * dhcp_type_class
Definition: omapi.c:46
isc_result_t dhcp_lease_get(omapi_object_t **, const char *, int)
Definition: dhcpd.h:888
struct lease_state * new_lease_state(char *file, int line) const
Definition: salloc.c:172
int option_state_dereference(struct option_state **ptr, const char *file, int line)
Definition: alloc.c:912
int packet_dereference(struct packet **ptr, const char *file, int line)
Definition: alloc.c:1082
isc_result_t omapi_object_allocate(omapi_object_t **, omapi_object_type_t *, size_t, const char *, int)
Definition: alloc.c:501
struct host_decl * host
Definition: dhcpd.h:516
#define dmalloc_reuse(x, y, l, z)
Definition: omapip.h:566
struct lease * next
Definition: dhcpd.h:509
isc_result_t dhcp_lease_free(omapi_object_t *, const char *, int)
const char * file
Definition: dhcpd.h:3535
struct data_string filename server_name
Definition: dhcpd.h:606
TIME ends
Definition: dhcpd.h:513