OpenDNSSEC-signer  1.4.1
listener.c
Go to the documentation of this file.
1 /*
2  * $Id: listener.c 4958 2011-04-18 07:11:09Z matthijs $
3  *
4  * Copyright (c) 2011 NLNet Labs. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
34 #include "config.h"
35 #include "shared/log.h"
36 #include "wire/listener.h"
37 
38 static const char* listener_str = "listener";
39 
40 
47 {
48  listener_type* listener = NULL;
49  if (!allocator) {
50  return NULL;
51  }
52  listener = (listener_type*) allocator_alloc(allocator,
53  sizeof(listener_type));
54  if (!listener) {
55  ods_log_error("[%s] create listener failed: allocator_alloc() failed",
56  listener_str);
57  return NULL;
58  }
59  listener->allocator = allocator;
60  listener->count = 0;
61  listener->interfaces = NULL;
62  return listener;
63 }
64 
65 
71 listener_push(listener_type* listener, char* address, int family, char* port)
72 {
73  interface_type* ifs_old = NULL;
74  ods_log_assert(listener);
75  ods_log_assert(address);
76  ifs_old = listener->interfaces;
78  listener->allocator, (listener->count + 1) * sizeof(interface_type));
79  if (!listener->interfaces) {
80  ods_fatal_exit("[%s] fatal unable to add interface: allocator_alloc() failed",
81  listener_str);
82  }
83  if (ifs_old) {
84  memcpy(listener->interfaces, ifs_old,
85  (listener->count) * sizeof(interface_type));
86  }
87  allocator_deallocate(listener->allocator, (void*) ifs_old);
88  listener->count++;
89  listener->interfaces[listener->count -1].address =
90  allocator_strdup(listener->allocator, address);
91  listener->interfaces[listener->count -1].family = family;
92 
93  if (port) {
94  listener->interfaces[listener->count -1].port =
95  allocator_strdup(listener->allocator, port);
96  } else{
97  listener->interfaces[listener->count -1].port = NULL;
98  }
99  memset(&listener->interfaces[listener->count -1].addr, 0,
100  sizeof(union acl_addr_storage));
101  if (listener->interfaces[listener->count -1].family == AF_INET6 &&
102  strlen(listener->interfaces[listener->count -1].address) > 0) {
103  if (inet_pton(listener->interfaces[listener->count -1].family,
104  listener->interfaces[listener->count -1].address,
105  &listener->interfaces[listener->count -1].addr.addr6) != 1) {
106  ods_log_error("[%s] bad ip address '%s'",
107  listener->interfaces[listener->count -1].address);
108  return NULL;
109  }
110  } else if (listener->interfaces[listener->count -1].family == AF_INET &&
111  strlen(listener->interfaces[listener->count -1].address) > 0) {
112  if (inet_pton(listener->interfaces[listener->count -1].family,
113  listener->interfaces[listener->count -1].address,
114  &listener->interfaces[listener->count -1].addr.addr) != 1) {
115  ods_log_error("[%s] bad ip address '%s'",
116  listener->interfaces[listener->count -1].address);
117  return NULL;
118  }
119  }
120  return &listener->interfaces[listener->count -1];
121 }
122 
123 
128 static void
129 interface_print(FILE* fd, interface_type* i)
130 {
131  if (!fd || !i) {
132  return;
133  }
134  fprintf(fd, "<Interface>");
135  if (i->family == AF_INET && i->address) {
136  fprintf(fd, "<IPv4>%s</IPv4>", i->address);
137  } else if (i->family == AF_INET6 && i->address) {
138  fprintf(fd, "<IPv6>%s</IPv6>", i->address);
139  }
140  if (i->port) {
141  fprintf(fd, "<Port>%s</Port>", i->port);
142  }
143  fprintf(fd, "</Interface>\n");
144  return;
145 }
146 
147 
152 void
153 listener_print(FILE* fd, listener_type* listener)
154 {
155  uint16_t i = 0;
156  if (!fd || !listener || listener->count <= 0) {
157  return;
158  }
159  fprintf(fd, "<Listener>\n");
160  for (i=0; i < listener->count; i++) {
161  interface_print(fd, &listener->interfaces[i]);
162  }
163  fprintf(fd, "</Listener>\n");
164  return;
165 }
166 
167 
172 static void
173 interface_log(interface_type* i)
174 {
175  if (!i) {
176  return;
177  }
178  ods_log_debug("[%s] FAMILY[%s] ADDRESS[%s] PORT[%s]", listener_str,
179  i->family==AF_INET6?"IPv6":"IPv4",
180  i->address?i->address:"localhost",
181  i->port?i->port:DNS_PORT_STRING);
182  return;
183 }
184 
185 
190 void
192 {
193  uint16_t i = 0;
194  if (!listener || listener->count <= 0) {
195  return;
196  }
197  for (i=0; i < listener->count; i++) {
198  interface_log(&listener->interfaces[i]);
199  }
200  return;
201 }
202 
203 
208 void
210 {
211  if (!i) {
212  return;
213  }
214  free((void*)i->port);
215  free((void*)i->address);
216  return;
217 }
218 
219 
224 void
226 {
227  uint16_t i = 0;
228  allocator_type* allocator = NULL;
229  if (!listener) {
230  return;
231  }
232  for (i=0; i < listener->count; i++) {
233  interface_cleanup(&listener->interfaces[i]);
234  }
235  allocator = listener->allocator;
236  allocator_deallocate(allocator, (void*) listener->interfaces);
237  allocator_deallocate(allocator, (void*) listener);
238  return;
239 }
void listener_cleanup(listener_type *listener)
Definition: listener.c:225
#define DNS_PORT_STRING
Definition: listener.h:53
void ods_log_debug(const char *format,...)
Definition: log.c:272
void * allocator_alloc(allocator_type *allocator, size_t size)
Definition: allocator.c:68
void ods_fatal_exit(const char *format,...)
Definition: log.c:384
void ods_log_error(const char *format,...)
Definition: log.c:336
void listener_log(listener_type *listener)
Definition: listener.c:191
listener_type * listener_create(allocator_type *allocator)
Definition: listener.c:46
size_t count
Definition: listener.h:86
char * allocator_strdup(allocator_type *allocator, const char *string)
Definition: allocator.c:123
void interface_cleanup(interface_type *i)
Definition: listener.c:209
char * address
Definition: listener.h:73
void listener_print(FILE *fd, listener_type *listener)
Definition: listener.c:153
interface_type * interfaces
Definition: listener.h:85
void allocator_deallocate(allocator_type *allocator, void *data)
Definition: allocator.c:137
struct interface_struct interface_type
Definition: listener.h:70
union acl_addr_storage addr
Definition: listener.h:75
struct in_addr addr
Definition: listener.h:62
#define ods_log_assert(x)
Definition: log.h:156
allocator_type * allocator
Definition: listener.h:84
interface_type * listener_push(listener_type *listener, char *address, int family, char *port)
Definition: listener.c:71
struct in6_addr addr6
Definition: listener.h:63