blob: 17567c484a0e436db24c00628254701906b7f80c [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
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020022#include <osmocom/sgsn/sgsn.h>
23#include <osmocom/sgsn/debug.h>
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +080024
25#include <netdb.h>
26
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020027extern void *tall_bsc_ctx;
28
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +080029struct cares_event_fd {
30 struct llist_head head;
31 struct osmo_fd fd;
32};
33
34struct cares_cb_data {
35 ares_host_callback cb;
36 void *data;
37};
38
39static void osmo_ares_reschedule(struct sgsn_instance *sgsn);
40static void ares_cb(void *_arg, int status, int timeouts, struct hostent *hostent)
41{
42 struct cares_cb_data *arg = _arg;
43
44 arg->cb(arg->data, status, timeouts, hostent);
45 osmo_ares_reschedule(sgsn);
46 talloc_free(arg);
47}
48
49static int ares_osmo_fd_cb(struct osmo_fd *fd, unsigned int what)
50{
51 LOGP(DGPRS, LOGL_DEBUG, "C-ares fd(%d) ready(%d)\n", fd->fd, what);
52
53 ares_process_fd(sgsn->ares_channel,
54 (what & BSC_FD_READ) ? fd->fd : ARES_SOCKET_BAD,
55 (what & BSC_FD_WRITE) ? fd->fd : ARES_SOCKET_BAD);
56 osmo_ares_reschedule(sgsn);
57 return 0;
58}
59
60static void ares_timeout_cb(void *data)
61{
62 struct sgsn_instance *sgsn = data;
63
64 LOGP(DGPRS, LOGL_DEBUG, "C-ares triggering timeout\n");
65 ares_process_fd(sgsn->ares_channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
66 osmo_ares_reschedule(sgsn);
67}
68
69static void osmo_ares_reschedule(struct sgsn_instance *sgsn)
70{
71 struct timeval *timeout, tv;
72
73 osmo_timer_del(&sgsn->ares_timer);
74 timeout = ares_timeout(sgsn->ares_channel, NULL, &tv);
75 if (timeout) {
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +080076 LOGP(DGPRS, LOGL_DEBUG, "C-ares scheduling timeout %llu.%llu\n",
77 (unsigned long long) tv.tv_sec,
78 (unsigned long long) tv.tv_usec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +020079 osmo_timer_setup(&sgsn->ares_timer, ares_timeout_cb, sgsn);
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +080080 osmo_timer_schedule(&sgsn->ares_timer, tv.tv_sec, tv.tv_usec);
81 }
82}
83
84static void setup_ares_osmo_fd(void *data, int fd, int read, int write)
85{
86 struct cares_event_fd *ufd, *tmp;
87
88 /* delete the entry */
89 if (read == 0 && write == 0) {
90 llist_for_each_entry_safe(ufd, tmp, &sgsn->ares_fds, head) {
91 if (ufd->fd.fd != fd)
92 continue;
93
94 LOGP(DGPRS, LOGL_DEBUG,
95 "Removing C-ares watched fd (%d)\n", fd);
96 osmo_fd_unregister(&ufd->fd);
97 llist_del(&ufd->head);
98 talloc_free(ufd);
99 return;
100 }
101 }
102
103 /* Search for the fd or create a new one */
104 llist_for_each_entry(ufd, &sgsn->ares_fds, head) {
105 if (ufd->fd.fd != fd)
106 continue;
107
108 LOGP(DGPRS, LOGL_DEBUG, "Updating C-ares fd (%d)\n", fd);
109 goto update_fd;
110 }
111
112 LOGP(DGPRS, LOGL_DEBUG, "Registering C-ares fd (%d)\n", fd);
113 ufd = talloc_zero(tall_bsc_ctx, struct cares_event_fd);
114 ufd->fd.fd = fd;
115 ufd->fd.cb = ares_osmo_fd_cb;
116 ufd->fd.data = data;
Holger Hans Peter Freyther65b0efe2015-06-02 09:31:04 +0200117 if (osmo_fd_register(&ufd->fd) != 0)
118 LOGP(DGPRS, LOGL_ERROR, "Failed to register C-ares fd (%d)\n", fd);
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +0800119 llist_add(&ufd->head, &sgsn->ares_fds);
120
121update_fd:
122 if (read)
123 ufd->fd.when |= BSC_FD_READ;
124 else
125 ufd->fd.when &= ~BSC_FD_READ;
126
127 if (write)
128 ufd->fd.when |= BSC_FD_WRITE;
129 else
130 ufd->fd.when &= ~BSC_FD_WRITE;
131
132 osmo_ares_reschedule(sgsn);
133}
134
135int sgsn_ares_query(struct sgsn_instance *sgsn, const char *name,
136 ares_host_callback cb, void *data)
137{
138 struct cares_cb_data *cb_data;
139
140 cb_data = talloc_zero(tall_bsc_ctx, struct cares_cb_data);
141 cb_data->cb = cb;
142 cb_data->data = data;
143 ares_gethostbyname(sgsn->ares_channel, name, AF_INET, ares_cb, cb_data);
144 osmo_ares_reschedule(sgsn);
145 return 0;
146}
147
148int sgsn_ares_init(struct sgsn_instance *sgsn)
149{
150 struct ares_options options;
151 int optmask;
152 int rc;
153
154 INIT_LLIST_HEAD(&sgsn->ares_fds);
155 memset(&options, 0, sizeof(options));
156 options.sock_state_cb = setup_ares_osmo_fd;
157 options.sock_state_cb_data = sgsn;
158
Holger Hans Peter Freyther9cb249b2015-05-29 19:51:21 +0200159 optmask = ARES_OPT_FLAGS | ARES_OPT_SOCK_STATE_CB | ARES_OPT_DOMAINS;
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +0800160
Holger Hans Peter Freythera5a6da42015-05-25 15:20:27 +0800161 if (sgsn->ares_servers)
162 optmask |= ARES_OPT_SERVERS;
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +0800163
164 ares_library_init(ARES_LIB_INIT_ALL);
165 rc = ares_init_options(&sgsn->ares_channel, &options, optmask);
Holger Hans Peter Freythera5a6da42015-05-25 15:20:27 +0800166 if (rc != ARES_SUCCESS)
167 return rc;
168
169 if (sgsn->ares_servers)
170 rc = ares_set_servers(sgsn->ares_channel, sgsn->ares_servers);
Holger Hans Peter Freyther66e71062015-05-25 01:21:50 +0800171
172 return rc;
173}
174
175osmo_static_assert(ARES_SUCCESS == 0, ares_success_zero);