OpenDNSSEC-signer 2.1.10
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 <stdlib.h>
34#include "log.h"
35#include "wire/listener.h"
36
37static const char* listener_str = "listener";
38
39
46{
47 listener_type* listener = NULL;
48 CHECKALLOC(listener = (listener_type*) malloc(sizeof(listener_type)));
49 listener->count = 0;
50 listener->interfaces = NULL;
51 return listener;
52}
53
54
60listener_push(listener_type* listener, char* address, int family, const char* port)
61{
62 interface_type* ifs_old = NULL;
63 ods_log_assert(listener);
64 ods_log_assert(address);
65 ifs_old = listener->interfaces;
66 CHECKALLOC(listener->interfaces = (interface_type*) malloc((listener->count + 1) * sizeof(interface_type)));
67 if (ifs_old) {
68 memcpy(listener->interfaces, ifs_old,
69 (listener->count) * sizeof(interface_type));
70 }
71 free(ifs_old);
72 listener->count++;
73 listener->interfaces[listener->count -1].address = strdup(address);
74 listener->interfaces[listener->count -1].family = family;
75
76 if (port) {
77 listener->interfaces[listener->count -1].port = strdup(port);
78 } else{
79 listener->interfaces[listener->count -1].port = NULL;
80 }
81 memset(&listener->interfaces[listener->count -1].addr, 0,
82 sizeof(union acl_addr_storage));
83 if (listener->interfaces[listener->count -1].family == AF_INET6 &&
84 strlen(listener->interfaces[listener->count -1].address) > 0) {
85 if (inet_pton(listener->interfaces[listener->count -1].family,
86 listener->interfaces[listener->count -1].address,
87 &listener->interfaces[listener->count -1].addr.addr6) != 1) {
88 ods_log_error("[%s] bad ip address '%s'", listener_str,
89 listener->interfaces[listener->count -1].address);
90 return NULL;
91 }
92 } else if (listener->interfaces[listener->count -1].family == AF_INET &&
93 strlen(listener->interfaces[listener->count -1].address) > 0) {
94 if (inet_pton(listener->interfaces[listener->count -1].family,
95 listener->interfaces[listener->count -1].address,
96 &listener->interfaces[listener->count -1].addr.addr) != 1) {
97 ods_log_error("[%s] bad ip address '%s'", listener_str,
98 listener->interfaces[listener->count -1].address);
99 return NULL;
100 }
101 }
102 return &listener->interfaces[listener->count -1];
103}
104
105
110void
112{
113 if (!i) {
114 return;
115 }
116 free((void*)i->port);
117 free((void*)i->address);
118}
119
120
125void
127{
128 uint16_t i = 0;
129 if (!listener) {
130 return;
131 }
132 for (i=0; i < listener->count; i++) {
133 interface_cleanup(&listener->interfaces[i]);
134 }
135 free(listener->interfaces);
136 free(listener);
137}
void listener_cleanup(listener_type *listener)
Definition: listener.c:126
void interface_cleanup(interface_type *i)
Definition: listener.c:111
interface_type * listener_push(listener_type *listener, char *address, int family, const char *port)
Definition: listener.c:60
listener_type * listener_create()
Definition: listener.c:45
union acl_addr_storage addr
Definition: listener.h:73
char * address
Definition: listener.h:71
size_t count
Definition: listener.h:83
interface_type * interfaces
Definition: listener.h:82
struct in_addr addr
Definition: listener.h:60
struct in6_addr addr6
Definition: listener.h:61