ISC DHCP  4.4.1
A reference DHCPv4 and DHCPv6 implementation
alloc.c
Go to the documentation of this file.
1 /* alloc.c
2 
3  Memory allocation... */
4 
5 /*
6  * Copyright (c) 2004-2017 by Internet Systems Consortium, Inc. ("ISC")
7  * Copyright (c) 1996-2003 by Internet Software Consortium
8  *
9  * This Source Code Form is subject to the terms of the Mozilla Public
10  * License, v. 2.0. If a copy of the MPL was not distributed with this
11  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
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 #include <omapip/omapip_p.h>
31 
34 
36  struct option_chain_head **ptr;
37  const char *file;
38  int line;
39 {
40  struct option_chain_head *h;
41 
42  if (!ptr) {
43  log_error ("%s(%d): null pointer", file, line);
44 #if defined (POINTER_DEBUG)
45  abort ();
46 #else
47  return 0;
48 #endif
49  }
50  if (*ptr) {
51  log_error ("%s(%d): non-null pointer", file, line);
52 #if defined (POINTER_DEBUG)
53  abort ();
54 #else
55  *ptr = (struct option_chain_head *)0;
56 #endif
57  }
58 
59  h = dmalloc (sizeof *h, file, line);
60  if (h) {
61  memset (h, 0, sizeof *h);
62  return option_chain_head_reference (ptr, h, file, line);
63  }
64  return 0;
65 }
66 
68  struct option_chain_head **ptr;
69  struct option_chain_head *bp;
70  const char *file;
71  int line;
72 {
73  if (!ptr) {
74  log_error ("%s(%d): null pointer", file, line);
75 #if defined (POINTER_DEBUG)
76  abort ();
77 #else
78  return 0;
79 #endif
80  }
81  if (*ptr) {
82  log_error ("%s(%d): non-null pointer", file, line);
83 #if defined (POINTER_DEBUG)
84  abort ();
85 #else
86  *ptr = (struct option_chain_head *)0;
87 #endif
88  }
89  *ptr = bp;
90  bp -> refcnt++;
91  rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
92  return 1;
93 }
94 
96  struct option_chain_head **ptr;
97  const char *file;
98  int line;
99 {
101  pair car, cdr;
102 
103  if (!ptr || !*ptr) {
104  log_error ("%s(%d): null pointer", file, line);
105 #if defined (POINTER_DEBUG)
106  abort ();
107 #else
108  return 0;
109 #endif
110  }
111 
112  option_chain_head = *ptr;
113  *ptr = (struct option_chain_head *)0;
117  if (option_chain_head -> refcnt > 0)
118  return 1;
119 
120  if (option_chain_head -> refcnt < 0) {
121  log_error ("%s(%d): negative refcnt!", file, line);
122 #if defined (DEBUG_RC_HISTORY)
123  dump_rc_history (option_chain_head);
124 #endif
125 #if defined (POINTER_DEBUG)
126  abort ();
127 #else
128  return 0;
129 #endif
130  }
131 
132  /* If there are any options on this head, free them. */
133  for (car = option_chain_head -> first; car; car = cdr) {
134  cdr = car -> cdr;
135  if (car -> car)
137  (&car -> car), MDL);
138  dfree (car, MDL);
139  }
140 
142  return 1;
143 }
144 
146  struct group **ptr;
147  const char *file;
148  int line;
149 {
150  struct group *g;
151 
152  if (!ptr) {
153  log_error ("%s(%d): null pointer", file, line);
154 #if defined (POINTER_DEBUG)
155  abort ();
156 #else
157  return 0;
158 #endif
159  }
160  if (*ptr) {
161  log_error ("%s(%d): non-null pointer", file, line);
162 #if defined (POINTER_DEBUG)
163  abort ();
164 #else
165  *ptr = (struct group *)0;
166 #endif
167  }
168 
169  g = dmalloc (sizeof *g, file, line);
170  if (g) {
171  memset (g, 0, sizeof *g);
172  return group_reference (ptr, g, file, line);
173  }
174  return 0;
175 }
176 
177 int group_reference (ptr, bp, file, line)
178  struct group **ptr;
179  struct group *bp;
180  const char *file;
181  int line;
182 {
183  if (!ptr) {
184  log_error ("%s(%d): null pointer", file, line);
185 #if defined (POINTER_DEBUG)
186  abort ();
187 #else
188  return 0;
189 #endif
190  }
191  if (*ptr) {
192  log_error ("%s(%d): non-null pointer", file, line);
193 #if defined (POINTER_DEBUG)
194  abort ();
195 #else
196  *ptr = (struct group *)0;
197 #endif
198  }
199  *ptr = bp;
200  bp -> refcnt++;
201  rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
202  return 1;
203 }
204 
206  struct group **ptr;
207  const char *file;
208  int line;
209 {
210  struct group *group;
211 
212  if (!ptr || !*ptr) {
213  log_error ("%s(%d): null pointer", file, line);
214 #if defined (POINTER_DEBUG)
215  abort ();
216 #else
217  return 0;
218 #endif
219  }
220 
221  group = *ptr;
222  *ptr = (struct group *)0;
223  --group -> refcnt;
224  rc_register (file, line, ptr, group, group -> refcnt, 1, RC_MISC);
225  if (group -> refcnt > 0)
226  return 1;
227 
228  if (group -> refcnt < 0) {
229  log_error ("%s(%d): negative refcnt!", file, line);
230 #if defined (DEBUG_RC_HISTORY)
231  dump_rc_history (group);
232 #endif
233 #if defined (POINTER_DEBUG)
234  abort ();
235 #else
236  return 0;
237 #endif
238  }
239 
240  if (group -> object)
241  group_object_dereference (&group -> object, file, line);
242  if (group -> subnet)
243  subnet_dereference (&group -> subnet, file, line);
244  if (group -> shared_network)
245  shared_network_dereference (&group -> shared_network,
246  file, line);
247  if (group -> statements)
249  file, line);
250  if (group -> next)
252  dfree (group, file, line);
253  return 1;
254 }
255 
257  const char *file;
258  int line;
259 {
260  struct dhcp_packet *rval;
261  rval = (struct dhcp_packet *)dmalloc (sizeof (struct dhcp_packet),
262  file, line);
263  return rval;
264 }
265 
267  const char *file;
268  int line;
269 {
270  struct protocol *rval = dmalloc (sizeof (struct protocol), file, line);
271  return rval;
272 }
273 
275  const char *file;
276  int line;
277 {
278  struct domain_search_list *rval =
279  dmalloc (sizeof (struct domain_search_list), file, line);
280  return rval;
281 }
282 
284  const char *file;
285  int line;
286 {
287  struct name_server *rval =
288  dmalloc (sizeof (struct name_server), file, line);
289  return rval;
290 }
291 
293  struct name_server *ptr;
294  const char *file;
295  int line;
296 {
297  dfree ((void *)ptr, file, line);
298 }
299 
301  const char *name;
302  const char *file;
303  int line;
304 {
305  struct option *rval;
306  int len;
307 
308  len = strlen(name);
309 
310  rval = dmalloc(sizeof(struct option) + len + 1, file, line);
311 
312  if(rval) {
313  memcpy(rval + 1, name, len);
314  rval->name = (char *)(rval + 1);
315  }
316 
317  return rval;
318 }
319 
321  const char *file;
322  int line;
323 {
324  struct universe *rval =
325  dmalloc (sizeof (struct universe), file, line);
326  return rval;
327 }
328 
329 void free_universe (ptr, file, line)
330  struct universe *ptr;
331  const char *file;
332  int line;
333 {
334  dfree ((void *)ptr, file, line);
335 }
336 
338  struct domain_search_list *ptr;
339  const char *file;
340  int line;
341 {
342  dfree ((void *)ptr, file, line);
343 }
344 
345 void free_protocol (ptr, file, line)
346  struct protocol *ptr;
347  const char *file;
348  int line;
349 {
350  dfree ((void *)ptr, file, line);
351 }
352 
354  struct dhcp_packet *ptr;
355  const char *file;
356  int line;
357 {
358  dfree ((void *)ptr, file, line);
359 }
360 
362  const char *file;
363  int line;
364 {
365  return (struct client_lease *)dmalloc (sizeof (struct client_lease),
366  file, line);
367 }
368 
370  struct client_lease *lease;
371  const char *file;
372  int line;
373 {
374  dfree (lease, file, line);
375 }
376 
378 
380  const char *file;
381  int line;
382 {
383  pair foo;
384 
385  if (free_pairs) {
386  foo = free_pairs;
387  free_pairs = foo -> cdr;
388  memset (foo, 0, sizeof *foo);
389  dmalloc_reuse (foo, file, line, 0);
390  return foo;
391  }
392 
393  foo = dmalloc (sizeof *foo, file, line);
394  if (!foo)
395  return foo;
396  memset (foo, 0, sizeof *foo);
397  return foo;
398 }
399 
400 void free_pair (foo, file, line)
401  pair foo;
402  const char *file;
403  int line;
404 {
405  foo -> cdr = free_pairs;
406  free_pairs = foo;
407  dmalloc_reuse (free_pairs, __FILE__, __LINE__, 0);
408 }
409 
410 #if defined (DEBUG_MEMORY_LEAKAGE) || \
411  defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
412 void relinquish_free_pairs ()
413 {
414  pair pf, pc;
415 
416  for (pf = free_pairs; pf; pf = pc) {
417  pc = pf -> cdr;
418  dfree (pf, MDL);
419  }
420  free_pairs = (pair)0;
421 }
422 #endif
423 
425 
427  struct expression **cptr;
428  const char *file;
429  int line;
430 {
431  struct expression *rval;
432 
433  if (free_expressions) {
434  rval = free_expressions;
435  free_expressions = rval -> data.not;
436  dmalloc_reuse (rval, file, line, 1);
437  } else {
438  rval = dmalloc (sizeof (struct expression), file, line);
439  if (!rval)
440  return 0;
441  }
442  memset (rval, 0, sizeof *rval);
443  return expression_reference (cptr, rval, file, line);
444 }
445 
447  struct expression **ptr;
448  struct expression *src;
449  const char *file;
450  int line;
451 {
452  if (!ptr) {
453  log_error ("%s(%d): null pointer", file, line);
454 #if defined (POINTER_DEBUG)
455  abort ();
456 #else
457  return 0;
458 #endif
459  }
460  if (*ptr) {
461  log_error ("%s(%d): non-null pointer", file, line);
462 #if defined (POINTER_DEBUG)
463  abort ();
464 #else
465  *ptr = (struct expression *)0;
466 #endif
467  }
468  *ptr = src;
469  src -> refcnt++;
470  rc_register (file, line, ptr, src, src -> refcnt, 0, RC_MISC);
471  return 1;
472 }
473 
474 void free_expression (expr, file, line)
475  struct expression *expr;
476  const char *file;
477  int line;
478 {
479  expr -> data.not = free_expressions;
480  free_expressions = expr;
481  dmalloc_reuse (free_expressions, __FILE__, __LINE__, 0);
482 }
483 
484 #if defined (DEBUG_MEMORY_LEAKAGE) || \
485  defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
486 void relinquish_free_expressions ()
487 {
488  struct expression *e, *n;
489 
490  for (e = free_expressions; e; e = n) {
491  n = e -> data.not;
492  dfree (e, MDL);
493  }
494  free_expressions = (struct expression *)0;
495 }
496 #endif
497 
499 
501  struct binding_value **cptr;
502  const char *file;
503  int line;
504 {
505  struct binding_value *rval;
506 
507  if (free_binding_values) {
508  rval = free_binding_values;
509  free_binding_values = rval -> value.bv;
510  dmalloc_reuse (rval, file, line, 1);
511  } else {
512  rval = dmalloc (sizeof (struct binding_value), file, line);
513  if (!rval)
514  return 0;
515  }
516  memset (rval, 0, sizeof *rval);
517  return binding_value_reference (cptr, rval, file, line);
518 }
519 
521  struct binding_value **ptr;
522  struct binding_value *src;
523  const char *file;
524  int line;
525 {
526  if (!ptr) {
527  log_error ("%s(%d): null pointer", file, line);
528 #if defined (POINTER_DEBUG)
529  abort ();
530 #else
531  return 0;
532 #endif
533  }
534  if (*ptr) {
535  log_error ("%s(%d): non-null pointer", file, line);
536 #if defined (POINTER_DEBUG)
537  abort ();
538 #else
539  *ptr = (struct binding_value *)0;
540 #endif
541  }
542  *ptr = src;
543  src -> refcnt++;
544  rc_register (file, line, ptr, src, src -> refcnt, 0, RC_MISC);
545  return 1;
546 }
547 
549  struct binding_value *bv;
550  const char *file;
551  int line;
552 {
553  bv -> value.bv = free_binding_values;
554  free_binding_values = bv;
555  dmalloc_reuse (free_binding_values, (char *)0, 0, 0);
556 }
557 
558 #if defined (DEBUG_MEMORY_LEAKAGE) || \
559  defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
560 void relinquish_free_binding_values ()
561 {
562  struct binding_value *b, *n;
563 
564  for (b = free_binding_values; b; b = n) {
565  n = b -> value.bv;
566  dfree (b, MDL);
567  }
568  free_binding_values = (struct binding_value *)0;
569 }
570 #endif
571 
573  struct fundef **cptr;
574  const char *file;
575  int line;
576 {
577  struct fundef *rval;
578 
579  rval = dmalloc (sizeof (struct fundef), file, line);
580  if (!rval)
581  return 0;
582  memset (rval, 0, sizeof *rval);
583  return fundef_reference (cptr, rval, file, line);
584 }
585 
586 int fundef_reference (ptr, src, file, line)
587  struct fundef **ptr;
588  struct fundef *src;
589  const char *file;
590  int line;
591 {
592  if (!ptr) {
593  log_error ("%s(%d): null pointer", file, line);
594 #if defined (POINTER_DEBUG)
595  abort ();
596 #else
597  return 0;
598 #endif
599  }
600  if (*ptr) {
601  log_error ("%s(%d): non-null pointer", file, line);
602 #if defined (POINTER_DEBUG)
603  abort ();
604 #else
605  *ptr = (struct fundef *)0;
606 #endif
607  }
608  *ptr = src;
609  src -> refcnt++;
610  rc_register (file, line, ptr, src, src -> refcnt, 0, RC_MISC);
611  return 1;
612 }
613 
615 
616 #if defined (DEBUG_MEMORY_LEAKAGE) || \
617  defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
618 void relinquish_free_option_caches ()
619 {
620  struct option_cache *o, *n;
621 
622  for (o = free_option_caches; o; o = n) {
623  n = (struct option_cache *)(o -> expression);
624  dfree (o, MDL);
625  }
626  free_option_caches = (struct option_cache *)0;
627 }
628 #endif
629 
631  struct option_cache **cptr;
632  const char *file;
633  int line;
634 {
635  struct option_cache *rval;
636 
637  if (free_option_caches) {
638  rval = free_option_caches;
640  (struct option_cache *)(rval -> expression);
641  dmalloc_reuse (rval, file, line, 0);
642  } else {
643  rval = dmalloc (sizeof (struct option_cache), file, line);
644  if (!rval)
645  return 0;
646  }
647  memset (rval, 0, sizeof *rval);
648  return option_cache_reference (cptr, rval, file, line);
649 }
650 
652  struct option_cache **ptr;
653  struct option_cache *src;
654  const char *file;
655  int line;
656 {
657  if (!ptr) {
658  log_error ("%s(%d): null pointer", file, line);
659 #if defined (POINTER_DEBUG)
660  abort ();
661 #else
662  return 0;
663 #endif
664  }
665  if (*ptr) {
666  log_error ("%s(%d): non-null pointer", file, line);
667 #if defined (POINTER_DEBUG)
668  abort ();
669 #else
670  *ptr = (struct option_cache *)0;
671 #endif
672  }
673  *ptr = src;
674  src -> refcnt++;
675  rc_register (file, line, ptr, src, src -> refcnt, 0, RC_MISC);
676  return 1;
677 }
678 
679 int buffer_allocate (ptr, len, file, line)
680  struct buffer **ptr;
681  unsigned len;
682  const char *file;
683  int line;
684 {
685  struct buffer *bp;
686 
687  /* XXXSK: should check for bad ptr values, otherwise we
688  leak memory if they are wrong */
689  bp = dmalloc (len + sizeof *bp, file, line);
690  if (!bp)
691  return 0;
692  /* XXXSK: both of these initializations are unnecessary */
693  memset (bp, 0, sizeof *bp);
694  bp -> refcnt = 0;
695  return buffer_reference (ptr, bp, file, line);
696 }
697 
698 int buffer_reference (ptr, bp, file, line)
699  struct buffer **ptr;
700  struct buffer *bp;
701  const char *file;
702  int line;
703 {
704  if (!ptr) {
705  log_error ("%s(%d): null pointer", file, line);
706 #if defined (POINTER_DEBUG)
707  abort ();
708 #else
709  return 0;
710 #endif
711  }
712  if (*ptr) {
713  log_error ("%s(%d): non-null pointer", file, line);
714 #if defined (POINTER_DEBUG)
715  abort ();
716 #else
717  *ptr = (struct buffer *)0;
718 #endif
719  }
720  *ptr = bp;
721  bp -> refcnt++;
722  rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
723  return 1;
724 }
725 
727  struct buffer **ptr;
728  const char *file;
729  int line;
730 {
731  if (!ptr) {
732  log_error ("%s(%d): null pointer", file, line);
733 #if defined (POINTER_DEBUG)
734  abort ();
735 #else
736  return 0;
737 #endif
738  }
739 
740  if (!*ptr) {
741  log_error ("%s(%d): null pointer", file, line);
742 #if defined (POINTER_DEBUG)
743  abort ();
744 #else
745  return 0;
746 #endif
747  }
748 
749  (*ptr) -> refcnt--;
750  rc_register (file, line, ptr, *ptr, (*ptr) -> refcnt, 1, RC_MISC);
751  if (!(*ptr) -> refcnt) {
752  dfree ((*ptr), file, line);
753  } else if ((*ptr) -> refcnt < 0) {
754  log_error ("%s(%d): negative refcnt!", file, line);
755 #if defined (DEBUG_RC_HISTORY)
756  dump_rc_history (*ptr);
757 #endif
758 #if defined (POINTER_DEBUG)
759  abort ();
760 #else
761  return 0;
762 #endif
763  }
764  *ptr = (struct buffer *)0;
765  return 1;
766 }
767 
768 int dns_host_entry_allocate (ptr, hostname, file, line)
769  struct dns_host_entry **ptr;
770  const char *hostname;
771  const char *file;
772  int line;
773 {
774  struct dns_host_entry *bp;
775 
776  bp = dmalloc (strlen (hostname) + sizeof *bp, file, line);
777  if (!bp)
778  return 0;
779  memset (bp, 0, sizeof *bp);
780  bp -> refcnt = 0;
781  strcpy (bp -> hostname, hostname);
782  return dns_host_entry_reference (ptr, bp, file, line);
783 }
784 
786  struct dns_host_entry **ptr;
787  struct dns_host_entry *bp;
788  const char *file;
789  int line;
790 {
791  if (!ptr) {
792  log_error ("%s(%d): null pointer", file, line);
793 #if defined (POINTER_DEBUG)
794  abort ();
795 #else
796  return 0;
797 #endif
798  }
799  if (*ptr) {
800  log_error ("%s(%d): non-null pointer", file, line);
801 #if defined (POINTER_DEBUG)
802  abort ();
803 #else
804  *ptr = (struct dns_host_entry *)0;
805 #endif
806  }
807  *ptr = bp;
808  bp -> refcnt++;
809  rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
810  return 1;
811 }
812 
814  struct dns_host_entry **ptr;
815  const char *file;
816  int line;
817 {
818  if (!ptr || !*ptr) {
819  log_error ("%s(%d): null pointer", file, line);
820 #if defined (POINTER_DEBUG)
821  abort ();
822 #else
823  return 0;
824 #endif
825  }
826 
827  (*ptr)->refcnt--;
828  rc_register (file, line, ptr, *ptr, (*ptr)->refcnt, 1, RC_MISC);
829  if ((*ptr)->refcnt == 0) {
830  dfree ((*ptr), file, line);
831  } else if ((*ptr)->refcnt < 0) {
832  log_error ("%s(%d): negative refcnt!", file, line);
833 #if defined (DEBUG_RC_HISTORY)
834  dump_rc_history (*ptr);
835 #endif
836 #if defined (POINTER_DEBUG)
837  abort ();
838 #else
839  return 0;
840 #endif
841  }
842  *ptr = (struct dns_host_entry *)0;
843  return 1;
844 }
845 
847  struct option_state **ptr;
848  const char *file;
849  int line;
850 {
851  unsigned size;
852 
853  if (!ptr) {
854  log_error ("%s(%d): null pointer", file, line);
855 #if defined (POINTER_DEBUG)
856  abort ();
857 #else
858  return 0;
859 #endif
860  }
861  if (*ptr) {
862  log_error ("%s(%d): non-null pointer", file, line);
863 #if defined (POINTER_DEBUG)
864  abort ();
865 #else
866  *ptr = (struct option_state *)0;
867 #endif
868  }
869 
870  size = sizeof **ptr + (universe_count - 1) * sizeof (void *);
871  *ptr = dmalloc (size, file, line);
872  if (*ptr) {
873  memset (*ptr, 0, size);
874  (*ptr) -> universe_count = universe_count;
875  (*ptr) -> refcnt = 1;
877  ptr, *ptr, (*ptr) -> refcnt, 0, RC_MISC);
878  return 1;
879  }
880  return 0;
881 }
882 
884  struct option_state **ptr;
885  struct option_state *bp;
886  const char *file;
887  int line;
888 {
889  if (!ptr) {
890  log_error ("%s(%d): null pointer", file, line);
891 #if defined (POINTER_DEBUG)
892  abort ();
893 #else
894  return 0;
895 #endif
896  }
897  if (*ptr) {
898  log_error ("%s(%d): non-null pointer", file, line);
899 #if defined (POINTER_DEBUG)
900  abort ();
901 #else
902  *ptr = (struct option_state *)0;
903 #endif
904  }
905  *ptr = bp;
906  bp -> refcnt++;
907  rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
908  return 1;
909 }
910 
912  struct option_state **ptr;
913  const char *file;
914  int line;
915 {
916  int i;
917  struct option_state *options;
918 
919  if (!ptr || !*ptr) {
920  log_error ("%s(%d): null pointer", file, line);
921 #if defined (POINTER_DEBUG)
922  abort ();
923 #else
924  return 0;
925 #endif
926  }
927 
928  options = *ptr;
929  *ptr = (struct option_state *)0;
930  --options -> refcnt;
931  rc_register (file, line, ptr, options, options -> refcnt, 1, RC_MISC);
932  if (options -> refcnt > 0)
933  return 1;
934 
935  if (options -> refcnt < 0) {
936  log_error ("%s(%d): negative refcnt!", file, line);
937 #if defined (DEBUG_RC_HISTORY)
938  dump_rc_history (options);
939 #endif
940 #if defined (POINTER_DEBUG)
941  abort ();
942 #else
943  return 0;
944 #endif
945  }
946 
947  /* Loop through the per-universe state. */
948  for (i = 0; i < options -> universe_count; i++)
949  if (options -> universes [i] &&
952  (universes [i], options, file, line));
953 
954  dfree (options, file, line);
955  return 1;
956 }
957 
959  struct executable_statement **ptr;
960  const char *file;
961  int line;
962 {
963  struct executable_statement *bp;
964 
965  bp = dmalloc (sizeof *bp, file, line);
966  if (!bp)
967  return 0;
968  memset (bp, 0, sizeof *bp);
969  return executable_statement_reference (ptr, bp, file, line);
970 }
971 
973  struct executable_statement **ptr;
974  struct executable_statement *bp;
975  const char *file;
976  int line;
977 {
978  if (!ptr) {
979  log_error ("%s(%d): null pointer", file, line);
980 #if defined (POINTER_DEBUG)
981  abort ();
982 #else
983  return 0;
984 #endif
985  }
986  if (*ptr) {
987  log_error ("%s(%d): non-null pointer", file, line);
988 #if defined (POINTER_DEBUG)
989  abort ();
990 #else
991  *ptr = (struct executable_statement *)0;
992 #endif
993  }
994  *ptr = bp;
995  bp -> refcnt++;
996  rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
997  return 1;
998 }
999 
1000 static struct packet *free_packets;
1001 
1002 #if defined (DEBUG_MEMORY_LEAKAGE) || \
1003  defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
1004 void relinquish_free_packets ()
1005 {
1006  struct packet *p, *n;
1007  for (p = free_packets; p; p = n) {
1008  n = (struct packet *)(p -> raw);
1009  dfree (p, MDL);
1010  }
1011  free_packets = (struct packet *)0;
1012 }
1013 #endif
1014 
1016  struct packet **ptr;
1017  const char *file;
1018  int line;
1019 {
1020  struct packet *p;
1021 
1022  if (!ptr) {
1023  log_error ("%s(%d): null pointer", file, line);
1024 #if defined (POINTER_DEBUG)
1025  abort ();
1026 #else
1027  return 0;
1028 #endif
1029  }
1030  if (*ptr) {
1031  log_error ("%s(%d): non-null pointer", file, line);
1032 #if defined (POINTER_DEBUG)
1033  abort ();
1034 #else
1035  *ptr = (struct packet *)0;
1036 #endif
1037  }
1038 
1039  if (free_packets) {
1040  p = free_packets;
1041  free_packets = (struct packet *)(p -> raw);
1042  dmalloc_reuse (p, file, line, 1);
1043  } else {
1044  p = dmalloc (sizeof *p, file, line);
1045  }
1046  if (p) {
1047  memset (p, 0, sizeof *p);
1048  return packet_reference (ptr, p, file, line);
1049  }
1050  return 0;
1051 }
1052 
1053 int packet_reference (ptr, bp, file, line)
1054  struct packet **ptr;
1055  struct packet *bp;
1056  const char *file;
1057  int line;
1058 {
1059  if (!ptr) {
1060  log_error ("%s(%d): null pointer", file, line);
1061 #if defined (POINTER_DEBUG)
1062  abort ();
1063 #else
1064  return 0;
1065 #endif
1066  }
1067  if (*ptr) {
1068  log_error ("%s(%d): non-null pointer", file, line);
1069 #if defined (POINTER_DEBUG)
1070  abort ();
1071 #else
1072  *ptr = (struct packet *)0;
1073 #endif
1074  }
1075  *ptr = bp;
1076  bp -> refcnt++;
1077  rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
1078  return 1;
1079 }
1080 
1082  struct packet **ptr;
1083  const char *file;
1084  int line;
1085 {
1086  int i;
1087  struct packet *packet;
1088 
1089  if (!ptr || !*ptr) {
1090  log_error ("%s(%d): null pointer", file, line);
1091 #if defined (POINTER_DEBUG)
1092  abort ();
1093 #else
1094  return 0;
1095 #endif
1096  }
1097 
1098  packet = *ptr;
1099  *ptr = (struct packet *)0;
1100  --packet -> refcnt;
1101  rc_register (file, line, ptr, packet, packet -> refcnt, 1, RC_MISC);
1102  if (packet -> refcnt > 0)
1103  return 1;
1104 
1105  if (packet -> refcnt < 0) {
1106  log_error ("%s(%d): negative refcnt!", file, line);
1107 #if defined (DEBUG_RC_HISTORY)
1108  dump_rc_history (packet);
1109 #endif
1110 #if defined (POINTER_DEBUG)
1111  abort ();
1112 #else
1113  return 0;
1114 #endif
1115  }
1116 
1117  if (packet -> options)
1119  if (packet -> interface)
1120  interface_dereference (&packet -> interface, MDL);
1121  if (packet -> shared_network)
1122  shared_network_dereference (&packet -> shared_network, MDL);
1123  for (i = 0; i < packet -> class_count && i < PACKET_MAX_CLASSES; i++) {
1124  if (packet -> classes [i])
1126  &packet -> classes [i], MDL);
1127  }
1128  packet -> raw = (struct dhcp_packet *)free_packets;
1129  free_packets = packet;
1130  dmalloc_reuse (free_packets, __FILE__, __LINE__, 0);
1131  return 1;
1132 }
1133 
1135  struct dns_zone **ptr;
1136  const char *file;
1137  int line;
1138 {
1139  struct dns_zone *d;
1140 
1141  if (!ptr) {
1142  log_error ("%s(%d): null pointer", file, line);
1143 #if defined (POINTER_DEBUG)
1144  abort ();
1145 #else
1146  return 0;
1147 #endif
1148  }
1149  if (*ptr) {
1150  log_error ("%s(%d): non-null pointer", file, line);
1151 #if defined (POINTER_DEBUG)
1152  abort ();
1153 #else
1154  *ptr = (struct dns_zone *)0;
1155 #endif
1156  }
1157 
1158  d = dmalloc (sizeof *d, file, line);
1159  if (d) {
1160  memset (d, 0, sizeof *d);
1161  return dns_zone_reference (ptr, d, file, line);
1162  }
1163  return 0;
1164 }
1165 
1167  struct dns_zone **ptr;
1168  struct dns_zone *bp;
1169  const char *file;
1170  int line;
1171 {
1172  if (!ptr) {
1173  log_error ("%s(%d): null pointer", file, line);
1174 #if defined (POINTER_DEBUG)
1175  abort ();
1176 #else
1177  return 0;
1178 #endif
1179  }
1180  if (*ptr) {
1181  log_error ("%s(%d): non-null pointer", file, line);
1182 #if defined (POINTER_DEBUG)
1183  abort ();
1184 #else
1185  *ptr = (struct dns_zone *)0;
1186 #endif
1187  }
1188  *ptr = bp;
1189  bp -> refcnt++;
1190  rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
1191  return 1;
1192 }
1193 
1195  struct binding_scope **ptr;
1196  const char *file;
1197  int line;
1198 {
1199  struct binding_scope *bp;
1200 
1201  if (!ptr) {
1202  log_error ("%s(%d): null pointer", file, line);
1203 #if defined (POINTER_DEBUG)
1204  abort ();
1205 #else
1206  return 0;
1207 #endif
1208  }
1209 
1210  if (*ptr) {
1211  log_error ("%s(%d): non-null pointer", file, line);
1212 #if defined (POINTER_DEBUG)
1213  abort ();
1214 #else
1215  return 0;
1216 #endif
1217  }
1218 
1219  bp = dmalloc (sizeof *bp, file, line);
1220  if (!bp)
1221  return 0;
1222  memset (bp, 0, sizeof *bp);
1223  binding_scope_reference (ptr, bp, file, line);
1224  return 1;
1225 }
1226 
1228  struct binding_scope **ptr;
1229  struct binding_scope *bp;
1230  const char *file;
1231  int line;
1232 {
1233  if (!ptr) {
1234  log_error ("%s(%d): null pointer", file, line);
1235 #if defined (POINTER_DEBUG)
1236  abort ();
1237 #else
1238  return 0;
1239 #endif
1240  }
1241  if (*ptr) {
1242  log_error ("%s(%d): non-null pointer", file, line);
1243 #if defined (POINTER_DEBUG)
1244  abort ();
1245 #else
1246  *ptr = (struct binding_scope *)0;
1247 #endif
1248  }
1249  *ptr = bp;
1250  bp -> refcnt++;
1251  rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
1252  return 1;
1253 }
1254 
1271 int
1272 data_string_new(struct data_string *new_string,
1273  const char *src, unsigned int len,
1274  const char *file, int line)
1275 {
1276  unsigned int copy_len = 0;
1277 
1278  if (new_string == NULL) {
1279  log_error("data_string_new: new_string cannot be NULL %s(%d)",
1280  file, line);
1281  return (0);
1282  }
1283 
1284  if (src == NULL) {
1285  log_error("data_string_new: src cannot be NULL %s(%d)",
1286  file, line);
1287  return (0);
1288  }
1289 
1290  memset(new_string, 0, sizeof (struct data_string));
1291 
1292  /* If we already have a NULL back off length by one. This lets
1293  * us always just add a NULL at the end. */
1294  copy_len = (len > 0 && src[len - 1] == 0) ? len - 1 : len;
1295 
1296  /* Allocate the buffer, accounting for terminating null */
1297  if (!buffer_allocate(&(new_string->buffer), copy_len + 1, MDL)) {
1298  log_error("data_string_new: No memory %s(%d)", file, line);
1299  return (0);
1300  }
1301 
1302  /* Only copy if there's something to copy */
1303  if (copy_len > 0) {
1304  memcpy(new_string->buffer->data, src, copy_len);
1305  }
1306 
1307  /* Always tack on the null */
1308  new_string->buffer->data[copy_len] = 0;
1309 
1310  /* Update data_string accessor values. Note len does NOT include
1311  * the NULL. */
1312  new_string->data = new_string->buffer->data;
1313  new_string->len = copy_len;
1314  new_string->terminated = 1;
1315 
1316  return (1);
1317 }
1318 
1319 /* Make a copy of the data in data_string, upping the buffer reference
1320  count if there's a buffer. */
1321 
1322 void
1323 data_string_copy(struct data_string *dest, const struct data_string *src,
1324  const char *file, int line)
1325 {
1326  if (src -> buffer) {
1327  buffer_reference (&dest -> buffer, src -> buffer, file, line);
1328  } else {
1329  dest->buffer = NULL;
1330  }
1331  dest -> data = src -> data;
1332  dest -> terminated = src -> terminated;
1333  dest -> len = src -> len;
1334 }
1335 
1336 /* Release the reference count to a data string's buffer (if any) and
1337  zero out the other information, yielding the null data string. */
1338 
1340  struct data_string *data;
1341  const char *file;
1342  int line;
1343 {
1344  if (data -> buffer)
1346  memset (data, 0, sizeof *data);
1347 }
1348 
1349 /* If the data_string is larger than the specified length, reduce
1350  the data_string to the specified size. */
1351 
1353  struct data_string *dp;
1354  int len;
1355 {
1356  /* XXX: do we need to consider the "terminated" flag in the check? */
1357  if (len < dp -> len) {
1358  dp -> terminated = 0;
1359  dp -> len = len;
1360  }
1361 }
1362 
1363 /* \brief Converts a data_string to a null-terminated data string
1364  *
1365  * If the given string isn't null-terminated, replace it with a
1366  * null-terminated version and free the current string decrementing
1367  * the referecne count. If the string is null-terminated it is left
1368  * as is.
1369  *
1370  * Currently this routine doesn't check if the string is 0 length
1371  * that must be checked by the caller.
1372  *
1373  * \param [in/out] str the data_string to convert
1374  * \param file the file this routine was called from
1375  * \param line the line this routine was called from
1376  *
1377  * \return 1 if the string was converted successfully (or already terminated),
1378  * 0 if the conversion failed. Failure is only possible if memory for the new
1379  * string could not be allocated. If the conversion fails, the original
1380  * string's content is lost.
1381  */
1383  struct data_string* str;
1384  const char *file;
1385  int line;
1386 {
1387  int ret_val = 1;
1388 
1389  if (str->terminated == 0) {
1390  struct data_string temp;
1391  memset(&temp, 0, sizeof(temp));
1392 
1393  data_string_copy(&temp, str, file, line);
1394  data_string_forget(str, file, line);
1395  if (data_string_new(str, (const char*)temp.data, temp.len,
1396  file, line) == 0) {
1397  /* couldn't create a copy, probably a memory issue,
1398  * an error message has already been logged. */
1399  ret_val = 0;
1400  }
1401 
1402  /* get rid of temp string */
1403  data_string_forget(&temp, file, line);
1404  }
1405 
1406  return (ret_val);
1407 }
#define rc_register(file, line, reference, addr, refcnt, d, f)
Definition: alloc.h:88
int data_string_new(struct data_string *new_string, const char *src, unsigned int len, const char *file, int line)
Constructs a null-terminated data_string from a char* and length.
Definition: alloc.c:1272
int executable_statement_reference(struct executable_statement **ptr, struct executable_statement *bp, const char *file, int line)
Definition: alloc.c:972
int executable_statement_allocate(struct executable_statement **ptr, const char *file, int line)
Definition: alloc.c:958
Definition: tree.h:30
const char int line
Definition: dhcpd.h:3781
int fundef_reference(struct fundef **ptr, struct fundef *src, const char *file, int line)
Definition: alloc.c:586
struct option_cache * free_option_caches
Definition: alloc.c:614
Definition: dhcpd.h:559
unsigned len
Definition: tree.h:79
int executable_statement_dereference(struct executable_statement **ptr, const char *file, int line)
Definition: execute.c:623
Definition: tree.h:93
struct universe * new_universe(char *file, int line) const
Definition: alloc.c:320
isc_result_t omapi_object_dereference(omapi_object_t **h, const char *file, int line)
Definition: alloc.c:593
int option_cache_dereference(struct option_cache **ptr, const char *file, int line)
Definition: options.c:2915
void free_domain_search_list(struct domain_search_list *ptr, const char *file, int line)
Definition: alloc.c:337
int expression_allocate(struct expression **cptr, const char *file, int line)
Definition: alloc.c:426
Definition: dhcpd.h:1060
int refcnt
Definition: tree.h:124
int binding_value_reference(struct binding_value **ptr, struct binding_value *src, const char *file, int line)
Definition: alloc.c:520
#define MDL
Definition: omapip.h:567
int refcnt
Definition: tree.h:198
int refcnt
Definition: dhcpd.h:407
pair first
Definition: tree.h:37
pair new_pair(char *file, int line) const
Definition: alloc.c:379
int group_reference(struct group **ptr, struct group *bp, const char *file, int line)
Definition: alloc.c:177
void data_string_forget(struct data_string *data, const char *file, int line)
Definition: alloc.c:1339
void free_universe(struct universe *ptr, const char *file, int line)
Definition: alloc.c:329
int option_cache_reference(struct option_cache **ptr, struct option_cache *src, const char *file, int line)
Definition: alloc.c:651
int fundef_allocate(struct fundef **cptr, const char *file, int line)
Definition: alloc.c:572
int log_error(const char *,...) __attribute__((__format__(__printf__
int dns_zone_reference(struct dns_zone **ptr, struct dns_zone *bp, const char *file, int line)
Definition: alloc.c:1166
struct domain_search_list * new_domain_search_list(char *file, int line) const
Definition: alloc.c:274
struct _pair * pair
struct name_server * new_name_server(char *file, int line) const
Definition: alloc.c:283
struct expression * not
Definition: tree.h:209
int terminated
Definition: tree.h:80
struct option_state * options
Definition: dhcpd.h:449
Definition: tree.h:301
void data_string_truncate(struct data_string *dp, int len)
Definition: alloc.c:1352
void free_name_server(struct name_server *ptr, const char *file, int line)
Definition: alloc.c:292
char hostname[1]
Definition: tree.h:291
int buffer_reference(struct buffer **ptr, struct buffer *bp, const char *file, int line)
Definition: alloc.c:698
int binding_value_allocate(struct binding_value **cptr, const char *file, int line)
Definition: alloc.c:500
struct executable_statement * statements
Definition: dhcpd.h:955
int option_cache_allocate(struct option_cache **cptr, const char *file, int line)
Definition: alloc.c:630
int option_state_reference(struct option_state **ptr, struct option_state *bp, const char *file, int line)
Definition: alloc.c:883
struct dhcp_packet * raw
Definition: dhcpd.h:406
union expression::expr_union data
int option_state_allocate(struct option_state **ptr, const char *file, int line)
Definition: alloc.c:846
int refcnt
Definition: dhcpd.h:386
Definition: tree.h:345
int option_chain_head_dereference(struct option_chain_head **ptr, const char *file, int line)
Definition: alloc.c:95
int refcnt
Definition: tree.h:94
int packet_reference(struct packet **ptr, struct packet *bp, const char *file, int line)
Definition: alloc.c:1053
int buffer_allocate(struct buffer **ptr, unsigned len, const char *file, int line)
Definition: alloc.c:679
struct class * classes[PACKET_MAX_CLASSES]
Definition: dhcpd.h:455
struct interface_info * interface
Definition: dhcpd.h:433
int binding_scope_allocate(struct binding_scope **ptr, const char *file, int line)
Definition: alloc.c:1194
pair free_pairs
Definition: alloc.c:377
struct group * next
Definition: dhcpd.h:948
int group_dereference(struct group **ptr, const char *file, int line)
Definition: alloc.c:205
int group_allocate(struct group **ptr, const char *file, int line)
Definition: alloc.c:145
Definition: dhcpd.h:405
void dfree(void *ptr, const char *file, int line)
Definition: alloc.c:145
struct binding_value * free_binding_values
Definition: alloc.c:498
void * dmalloc(size_t size, const char *file, int line)
Definition: alloc.c:57
struct expression * free_expressions
Definition: alloc.c:424
struct option * new_option(char *name, const char *file, int line) const
Definition: alloc.c:300
const char * name
Definition: tree.h:346
int option_chain_head_reference(struct option_chain_head **ptr, struct option_chain_head *bp, const char *file, int line)
Definition: alloc.c:67
int refcnt
Definition: dhcpd.h:950
struct dhcp_packet * dhcp_free_list
Definition: alloc.c:32
Definition: tree.h:287
void free_client_lease(struct client_lease *lease, const char *file, int line)
Definition: alloc.c:369
int refcnt
Definition: dhcpd.h:1504
struct packet * packet_free_list
Definition: alloc.c:33
union binding_value::value value
struct universe ** universes
Definition: tables.c:967
int option_state_dereference(struct option_state **ptr, const char *file, int line)
Definition: alloc.c:911
Definition: dhcpd.h:947
int binding_scope_reference(struct binding_scope **ptr, struct binding_scope *bp, const char *file, int line)
Definition: alloc.c:1227
void free_protocol(struct protocol *ptr, const char *file, int line)
Definition: alloc.c:345
void free_pair(pair foo, const char *file, int line)
Definition: alloc.c:400
int packet_dereference(struct packet **ptr, const char *file, int line)
Definition: alloc.c:1081
int packet_allocate(struct packet **ptr, const char *file, int line)
Definition: alloc.c:1015
int option_chain_head_allocate(struct option_chain_head **ptr, const char *file, int line)
Definition: alloc.c:35
void free_expression(struct expression *expr, const char *file, int line)
Definition: alloc.c:474
struct client_lease * new_client_lease(char *file, int line) const
Definition: alloc.c:361
unsigned char data[1]
Definition: tree.h:62
#define dmalloc_reuse(x, y, l, z)
Definition: omapip.h:565
Definition: tree.h:60
struct protocol * new_protocol(char *file, int line) const
Definition: alloc.c:266
int expression_reference(struct expression **ptr, struct expression *src, const char *file, int line)
Definition: alloc.c:446
int refcnt
Definition: dhcpd.h:397
int refcnt
Definition: tree.h:100
int dns_host_entry_allocate(struct dns_host_entry **ptr, const char *hostname, const char *file, int line)
Definition: alloc.c:768
int class_count
Definition: dhcpd.h:454
int dns_host_entry_dereference(struct dns_host_entry **ptr, const char *file, int line)
Definition: alloc.c:813
struct binding_value * bv
Definition: tree.h:113
int dns_zone_allocate(struct dns_zone **ptr, const char *file, int line)
Definition: alloc.c:1134
const char * file
Definition: dhcpd.h:3781
const unsigned char * data
Definition: tree.h:78
void data_string_copy(struct data_string *dest, const struct data_string *src, const char *file, int line)
Definition: alloc.c:1323
void free_binding_value(struct binding_value *bv, const char *file, int line)
Definition: alloc.c:548
int dns_host_entry_reference(struct dns_host_entry **ptr, struct dns_host_entry *bp, const char *file, int line)
Definition: alloc.c:785
#define RC_MISC
Definition: alloc.h:56
struct dhcp_packet * new_dhcp_packet(char *file, int line) const
Definition: alloc.c:256
int refcnt
Definition: tree.h:288
int refcnt
Definition: tree.h:61
#define PACKET_MAX_CLASSES
Definition: dhcpd.h:452
void free_dhcp_packet(struct dhcp_packet *ptr, const char *file, int line)
Definition: alloc.c:353
int universe_count
Definition: tables.c:968
struct buffer * buffer
Definition: tree.h:77
int buffer_dereference(struct buffer **ptr, const char *file, int line)
Definition: alloc.c:726
int data_string_terminate(struct data_string *str, const char *file, int line)
Definition: alloc.c:1382