1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright (c) 2001 by Sun Microsystems, Inc.
  24  * All rights reserved.
  25  */
  26 
  27 #pragma ident   "%Z%%M% %I%     %E% SMI"
  28 
  29 /*
  30  * This module contains the glue functions necessary for gluing in
  31  * the selected public module API into the private API.
  32  */
  33 
  34 #include <stdlib.h>
  35 #include <string.h>
  36 #include <dhcp_svc_public.h>
  37 
  38 /*
  39  * Allocate a dt_rec_t structure.  Argument values are copied and set
  40  * to the respective fields within the allocated dt_rec_t structure.
  41  * Caller is should free structure using free_dtrec().
  42  */
  43 dt_rec_t *
  44 alloc_dtrec(const char *key, char type, const char *value)
  45 {
  46         dt_rec_t *retval = malloc(sizeof (dt_rec_t));
  47 
  48         if (key == NULL || value == NULL || retval == NULL) {
  49                 free(retval);
  50                 return (NULL);
  51         }
  52 
  53         (void) strlcpy(retval->dt_key, key, sizeof (retval->dt_key));
  54         retval->dt_sig = 0;
  55         retval->dt_type = type;
  56         retval->dt_value = strdup(value);
  57         if (retval->dt_value == NULL) {
  58                 free(retval);
  59                 return (NULL);
  60         }
  61         return (retval);
  62 }
  63 
  64 /*
  65  * Allocate a dn_rec_t structure.  Argument values are copied and set
  66  * to the respective fields within the allocated dn_rec_t structure.
  67  */
  68 dn_rec_t *
  69 alloc_dnrec(const uchar_t *cid, uchar_t cid_len, uchar_t flags,
  70     struct in_addr cip, struct in_addr sip, lease_t lease, const char *macro,
  71     const char *comment)
  72 {
  73         dn_rec_t *retval = malloc(sizeof (dn_rec_t));
  74 
  75         if (cid == NULL || retval == NULL) {
  76                 free(retval);
  77                 return (NULL);
  78         }
  79 
  80         retval->dn_cid_len   = cid_len;
  81         retval->dn_flags     = flags;
  82         retval->dn_cip               = cip;
  83         retval->dn_sip               = sip;
  84         retval->dn_lease     = lease;
  85         retval->dn_macro[0]  = '\0';
  86         retval->dn_comment[0]        = '\0';
  87         retval->dn_sig               = 0;
  88         (void) memcpy(retval->dn_cid, cid, cid_len);
  89 
  90         if (macro != NULL)
  91                 (void) strlcpy(retval->dn_macro, macro,
  92                     sizeof (retval->dn_macro));
  93 
  94         if (comment != NULL)
  95                 (void) strlcpy(retval->dn_comment, comment,
  96                     sizeof (retval->dn_comment));
  97 
  98         return (retval);
  99 }
 100 
 101 /*
 102  * Prepend a dt_rec_t to a dt_rec_list_t; if `listp' is NULL, then
 103  * the list is created.
 104  */
 105 dt_rec_list_t *
 106 add_dtrec_to_list(dt_rec_t *entryp, dt_rec_list_t *listp)
 107 {
 108         dt_rec_list_t *retval = malloc(sizeof (dt_rec_list_t));
 109 
 110         if (entryp == NULL || retval == NULL) {
 111                 free(retval);
 112                 return (NULL);
 113         }
 114 
 115         retval->dtl_next = listp;
 116         retval->dtl_rec = entryp;
 117         return (retval);
 118 }
 119 
 120 /*
 121  * Prepend a dn_rec_t to a dn_rec_list_t; if `listp' is NULL, then
 122  * the list is created.
 123  */
 124 dn_rec_list_t *
 125 add_dnrec_to_list(dn_rec_t *entryp, dn_rec_list_t *listp)
 126 {
 127         dn_rec_list_t *retval = malloc(sizeof (dn_rec_list_t));
 128 
 129         if (entryp == NULL || retval == NULL) {
 130                 free(retval);
 131                 return (NULL);
 132         }
 133 
 134         retval->dnl_next = listp;
 135         retval->dnl_rec = entryp;
 136         return (retval);
 137 }
 138 
 139 /*
 140  * Free all elements of dtp, as well as the dt_rec_t structure itself.
 141  */
 142 void
 143 free_dtrec(dt_rec_t *dtp)
 144 {
 145         if (dtp != NULL) {
 146                 free(dtp->dt_value);
 147                 free(dtp);
 148         }
 149 }
 150 
 151 /*
 152  * Free a list of dt_rec_t's
 153  */
 154 void
 155 free_dtrec_list(dt_rec_list_t *dtlp)
 156 {
 157         dt_rec_list_t *next;
 158 
 159         for (; dtlp != NULL; dtlp = next) {
 160                 free_dtrec(dtlp->dtl_rec);
 161                 next = dtlp->dtl_next;
 162                 free(dtlp);
 163         }
 164 }
 165 
 166 /*
 167  * Free the dn_rec_t structure.
 168  */
 169 void
 170 free_dnrec(dn_rec_t *dnp)
 171 {
 172         free(dnp);
 173 }
 174 
 175 /*
 176  * Free a list of dn_rec_t's
 177  */
 178 void
 179 free_dnrec_list(dn_rec_list_t *dnlp)
 180 {
 181         dn_rec_list_t *next;
 182 
 183         for (; dnlp != NULL; dnlp = next) {
 184                 free_dnrec(dnlp->dnl_rec);
 185                 next = dnlp->dnl_next;
 186                 free(dnlp);
 187         }
 188 }