blob: d94d184a3686a59564314fd7aa1b48197e15c85d [file] [log] [blame]
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +08001/* C-ARES DNS resolver integration */
2
3/*
4 * (C) 2015 by Holger Hans Peter Freyther
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <openbsc/sgsn.h>
23#include <openbsc/debug.h>
24
25#include <netdb.h>
26
27struct cares_event_fd {
28 struct llist_head head;
29 struct osmo_fd fd;
30};
31
32struct cares_cb_data {
33 ares_host_callback cb;
34 void *data;
35};
36
37static void osmo_ares_reschedule(struct sgsn_instance *sgsn);
38static void ares_cb(void *_arg, int status, int timeouts, struct hostent *hostent)
39{
40 struct cares_cb_data *arg = _arg;
41
42 arg->cb(arg->data, status, timeouts, hostent);
43 osmo_ares_reschedule(sgsn);
44 talloc_free(arg);
45}
46
47static int ares_osmo_fd_cb(struct osmo_fd *fd, unsigned int what)
48{
49 LOGP(DGPRS, LOGL_DEBUG, "C-ares fd(%d) ready(%d)\n", fd->fd, what);
50
51 ares_process_fd(sgsn->ares_channel,
52 (what & BSC_FD_READ) ? fd->fd : ARES_SOCKET_BAD,
53 (what & BSC_FD_WRITE) ? fd->fd : ARES_SOCKET_BAD);
54 osmo_ares_reschedule(sgsn);
55 return 0;
56}
57
58static void ares_timeout_cb(void *data)
59{
60 struct sgsn_instance *sgsn = data;
61
62 LOGP(DGPRS, LOGL_DEBUG, "C-ares triggering timeout\n");
63 ares_process_fd(sgsn->ares_channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
64 osmo_ares_reschedule(sgsn);
65}
66
67static void osmo_ares_reschedule(struct sgsn_instance *sgsn)
68{
69 struct timeval *timeout, tv;
70
71 osmo_timer_del(&sgsn->ares_timer);
72 timeout = ares_timeout(sgsn->ares_channel, NULL, &tv);
73 if (timeout) {
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +080074 LOGP(DGPRS, LOGL_DEBUG, "C-ares scheduling timeout %llu.%llu\n",
75 (unsigned long long) tv.tv_sec,
76 (unsigned long long) tv.tv_usec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +020077 osmo_timer_setup(&sgsn->ares_timer, ares_timeout_cb, sgsn);
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +080078 osmo_timer_schedule(&sgsn->ares_timer, tv.tv_sec, tv.tv_usec);
79 }
80}
81
82static void setup_ares_osmo_fd(void *data, int fd, int read, int write)
83{
84 struct cares_event_fd *ufd, *tmp;
85
86 /* delete the entry */
87 if (read == 0 && write == 0) {
88 llist_for_each_entry_safe(ufd, tmp, &sgsn->ares_fds, head) {
89 if (ufd->fd.fd != fd)
90 continue;
91
92 LOGP(DGPRS, LOGL_DEBUG,
93 "Removing C-ares watched fd (%d)\n", fd);
94 osmo_fd_unregister(&ufd->fd);
95 llist_del(&ufd->head);
96 talloc_free(ufd);
97 return;
98 }
99 }
100
101 /* Search for the fd or create a new one */
102 llist_for_each_entry(ufd, &sgsn->ares_fds, head) {
103 if (ufd->fd.fd != fd)
104 continue;
105
106 LOGP(DGPRS, LOGL_DEBUG, "Updating C-ares fd (%d)\n", fd);
107 goto update_fd;
108 }
109
110 LOGP(DGPRS, LOGL_DEBUG, "Registering C-ares fd (%d)\n", fd);
111 ufd = talloc_zero(tall_bsc_ctx, struct cares_event_fd);
112 ufd->fd.fd = fd;
113 ufd->fd.cb = ares_osmo_fd_cb;
114 ufd->fd.data = data;
Holger Hans Peter Freyther65b0efe2015-06-02 09:31:04 +0200115 if (osmo_fd_register(&ufd->fd) != 0)
116 LOGP(DGPRS, LOGL_ERROR, "Failed to register C-ares fd (%d)\n", fd);
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +0800117 llist_add(&ufd->head, &sgsn->ares_fds);
118
119update_fd:
120 if (read)
121 ufd->fd.when |= BSC_FD_READ;
122 else
123 ufd->fd.when &= ~BSC_FD_READ;
124
125 if (write)
126 ufd->fd.when |= BSC_FD_WRITE;
127 else
128 ufd->fd.when &= ~BSC_FD_WRITE;
129
130 osmo_ares_reschedule(sgsn);
131}
132
133int sgsn_ares_query(struct sgsn_instance *sgsn, const char *name,
134 ares_host_callback cb, void *data)
135{
136 struct cares_cb_data *cb_data;
137
138 cb_data = talloc_zero(tall_bsc_ctx, struct cares_cb_data);
139 cb_data->cb = cb;
140 cb_data->data = data;
141 ares_gethostbyname(sgsn->ares_channel, name, AF_INET, ares_cb, cb_data);
142 osmo_ares_reschedule(sgsn);
143 return 0;
144}
145
146int sgsn_ares_init(struct sgsn_instance *sgsn)
147{
148 struct ares_options options;
149 int optmask;
150 int rc;
151
152 INIT_LLIST_HEAD(&sgsn->ares_fds);
153 memset(&options, 0, sizeof(options));
154 options.sock_state_cb = setup_ares_osmo_fd;
155 options.sock_state_cb_data = sgsn;
156
Holger Hans Peter Freyther9cb249b2015-05-29 19:51:21 +0200157 optmask = ARES_OPT_FLAGS | ARES_OPT_SOCK_STATE_CB | ARES_OPT_DOMAINS;
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +0800158
Holger Hans Peter Freythera5a6da42015-05-25 15:20:27 +0800159 if (sgsn->ares_servers)
160 optmask |= ARES_OPT_SERVERS;
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +0800161
162 ares_library_init(ARES_LIB_INIT_ALL);
163 rc = ares_init_options(&sgsn->ares_channel, &options, optmask);
Holger Hans Peter Freythera5a6da42015-05-25 15:20:27 +0800164 if (rc != ARES_SUCCESS)
165 return rc;
166
167 if (sgsn->ares_servers)
168 rc = ares_set_servers(sgsn->ares_channel, sgsn->ares_servers);
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +0800169
170 return rc;
171}
172
173osmo_static_assert(ARES_SUCCESS == 0, ares_success_zero);