Holger Hans Peter Freyther | 180027c | 2015-05-25 01:21:50 +0800 | [diff] [blame] | 1 | /* 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 | |
| 27 | struct cares_event_fd { |
| 28 | struct llist_head head; |
| 29 | struct osmo_fd fd; |
| 30 | }; |
| 31 | |
| 32 | struct cares_cb_data { |
| 33 | ares_host_callback cb; |
| 34 | void *data; |
| 35 | }; |
| 36 | |
| 37 | static void osmo_ares_reschedule(struct sgsn_instance *sgsn); |
| 38 | static 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 | |
| 47 | static 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 | |
| 58 | static 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 | |
| 67 | static 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) { |
| 74 | sgsn->ares_timer.cb = ares_timeout_cb; |
| 75 | sgsn->ares_timer.data = sgsn; |
| 76 | |
| 77 | LOGP(DGPRS, LOGL_DEBUG, "C-ares scheduling timeout %llu.%llu\n", |
| 78 | (unsigned long long) tv.tv_sec, |
| 79 | (unsigned long long) tv.tv_usec); |
| 80 | osmo_timer_schedule(&sgsn->ares_timer, tv.tv_sec, tv.tv_usec); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static 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 Freyther | 61bee91 | 2015-06-02 09:31:04 +0200 | [diff] [blame] | 117 | if (osmo_fd_register(&ufd->fd) != 0) |
| 118 | LOGP(DGPRS, LOGL_ERROR, "Failed to register C-ares fd (%d)\n", fd); |
Holger Hans Peter Freyther | 180027c | 2015-05-25 01:21:50 +0800 | [diff] [blame] | 119 | llist_add(&ufd->head, &sgsn->ares_fds); |
| 120 | |
| 121 | update_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 | |
| 135 | int 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 | |
| 148 | int 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 Freyther | 13c6785 | 2015-05-29 19:51:21 +0200 | [diff] [blame] | 159 | optmask = ARES_OPT_FLAGS | ARES_OPT_SOCK_STATE_CB | ARES_OPT_DOMAINS; |
Holger Hans Peter Freyther | 180027c | 2015-05-25 01:21:50 +0800 | [diff] [blame] | 160 | |
Holger Hans Peter Freyther | 8adb9c4 | 2015-05-25 15:20:27 +0800 | [diff] [blame] | 161 | if (sgsn->ares_servers) |
| 162 | optmask |= ARES_OPT_SERVERS; |
Holger Hans Peter Freyther | 180027c | 2015-05-25 01:21:50 +0800 | [diff] [blame] | 163 | |
| 164 | ares_library_init(ARES_LIB_INIT_ALL); |
| 165 | rc = ares_init_options(&sgsn->ares_channel, &options, optmask); |
Holger Hans Peter Freyther | 8adb9c4 | 2015-05-25 15:20:27 +0800 | [diff] [blame] | 166 | 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 Freyther | 180027c | 2015-05-25 01:21:50 +0800 | [diff] [blame] | 171 | |
| 172 | return rc; |
| 173 | } |
| 174 | |
| 175 | osmo_static_assert(ARES_SUCCESS == 0, ares_success_zero); |