1 /*
2  * DNS Reply Tool (drool)
3  *
4  * Copyright (c) 2017-2018, OARC, Inc.
5  * Copyright (c) 2017, Comcast Corporation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. Neither the name of the copyright holder nor the names of its
21  *    contributors may be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include "config.h"
39 
40 #include "conf_interface.h"
41 #include "conf.h"
42 #include "assert.h"
43 
44 #include <stdlib.h>
45 #include <string.h>
46 
conf_interface_new(void)47 drool_conf_interface_t* conf_interface_new(void)
48 {
49     drool_conf_interface_t* conf_interface = calloc(1, sizeof(drool_conf_interface_t));
50     return conf_interface;
51 }
52 
conf_interface_free(drool_conf_interface_t * conf_interface)53 void conf_interface_free(drool_conf_interface_t* conf_interface)
54 {
55     if (conf_interface) {
56         conf_interface_release(conf_interface);
57         free(conf_interface);
58     }
59 }
60 
conf_interface_release(drool_conf_interface_t * conf_interface)61 void conf_interface_release(drool_conf_interface_t* conf_interface)
62 {
63     if (conf_interface) {
64         if (conf_interface->name) {
65             free(conf_interface->name);
66             conf_interface->name = 0;
67         }
68     }
69 }
70 
conf_interface_next(const drool_conf_interface_t * conf_interface)71 inline const drool_conf_interface_t* conf_interface_next(const drool_conf_interface_t* conf_interface)
72 {
73     drool_assert(conf_interface);
74     return conf_interface->next;
75 }
76 
conf_interface_set_next(drool_conf_interface_t * conf_interface,drool_conf_interface_t * next)77 int conf_interface_set_next(drool_conf_interface_t* conf_interface, drool_conf_interface_t* next)
78 {
79     if (!conf_interface) {
80         return CONF_EINVAL;
81     }
82     if (!next) {
83         return CONF_EINVAL;
84     }
85 
86     conf_interface->next = next;
87 
88     return CONF_OK;
89 }
90 
conf_interface_name(const drool_conf_interface_t * conf_interface)91 inline const char* conf_interface_name(const drool_conf_interface_t* conf_interface)
92 {
93     drool_assert(conf_interface);
94     return conf_interface->name;
95 }
96 
conf_interface_set_name(drool_conf_interface_t * conf_interface,const char * name,size_t length)97 int conf_interface_set_name(drool_conf_interface_t* conf_interface, const char* name, size_t length)
98 {
99     if (!conf_interface) {
100         return CONF_EINVAL;
101     }
102     if (!name) {
103         return CONF_EINVAL;
104     }
105 
106     if (conf_interface->name) {
107         free(conf_interface->name);
108     }
109     if (length) {
110         if (!(conf_interface->name = strndup(name, length))) {
111             return CONF_ENOMEM;
112         }
113     } else {
114         if (!(conf_interface->name = strdup(name))) {
115             return CONF_ENOMEM;
116         }
117     }
118 
119     return CONF_OK;
120 }
121