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