Simon Kelley | 5954608 | 2012-01-06 20:02:04 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2012 Simon Kelley |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 12 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 19 | #ifdef HAVE_LINUX_NETWORK |
| 20 | |
| 21 | int indextoname(int fd, int index, char *name) |
| 22 | { |
| 23 | struct ifreq ifr; |
| 24 | |
| 25 | if (index == 0) |
| 26 | return 0; |
| 27 | |
| 28 | ifr.ifr_ifindex = index; |
| 29 | if (ioctl(fd, SIOCGIFNAME, &ifr) == -1) |
| 30 | return 0; |
| 31 | |
| 32 | strncpy(name, ifr.ifr_name, IF_NAMESIZE); |
| 33 | |
| 34 | return 1; |
| 35 | } |
| 36 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 37 | |
| 38 | #elif defined(HAVE_SOLARIS_NETWORK) |
| 39 | |
| 40 | #include <zone.h> |
| 41 | #include <alloca.h> |
| 42 | #ifndef LIFC_UNDER_IPMP |
| 43 | # define LIFC_UNDER_IPMP 0 |
| 44 | #endif |
| 45 | |
| 46 | int indextoname(int fd, int index, char *name) |
| 47 | { |
| 48 | int64_t lifc_flags; |
| 49 | struct lifnum lifn; |
| 50 | int numifs, bufsize, i; |
| 51 | struct lifconf lifc; |
| 52 | struct lifreq *lifrp; |
| 53 | |
| 54 | if (index == 0) |
| 55 | return 0; |
| 56 | |
| 57 | if (getzoneid() == GLOBAL_ZONEID) |
| 58 | { |
| 59 | if (!if_indextoname(index, name)) |
| 60 | return 0; |
| 61 | return 1; |
| 62 | } |
| 63 | |
| 64 | lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES | LIFC_UNDER_IPMP; |
| 65 | lifn.lifn_family = AF_UNSPEC; |
| 66 | lifn.lifn_flags = lifc_flags; |
| 67 | if (ioctl(fd, SIOCGLIFNUM, &lifn) < 0) |
| 68 | return 0; |
| 69 | |
| 70 | numifs = lifn.lifn_count; |
| 71 | bufsize = numifs * sizeof(struct lifreq); |
| 72 | |
| 73 | lifc.lifc_family = AF_UNSPEC; |
| 74 | lifc.lifc_flags = lifc_flags; |
| 75 | lifc.lifc_len = bufsize; |
| 76 | lifc.lifc_buf = alloca(bufsize); |
| 77 | |
| 78 | if (ioctl(fd, SIOCGLIFCONF, &lifc) < 0) |
| 79 | return 0; |
| 80 | |
| 81 | lifrp = lifc.lifc_req; |
| 82 | for (i = lifc.lifc_len / sizeof(struct lifreq); i; i--, lifrp++) |
| 83 | { |
| 84 | struct lifreq lifr; |
| 85 | strncpy(lifr.lifr_name, lifrp->lifr_name, IF_NAMESIZE); |
| 86 | if (ioctl(fd, SIOCGLIFINDEX, &lifr) < 0) |
| 87 | return 0; |
| 88 | |
| 89 | if (lifr.lifr_index == index) { |
| 90 | strncpy(name, lifr.lifr_name, IF_NAMESIZE); |
| 91 | return 1; |
| 92 | } |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 98 | #else |
| 99 | |
| 100 | int indextoname(int fd, int index, char *name) |
| 101 | { |
| 102 | if (index == 0 || !if_indextoname(index, name)) |
| 103 | return 0; |
| 104 | |
| 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | #endif |
| 109 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 110 | int iface_check(int family, struct all_addr *addr, char *name) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 111 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 112 | struct iname *tmp; |
Simon Kelley | 309331f | 2006-04-22 15:05:01 +0100 | [diff] [blame] | 113 | int ret = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 114 | |
Simon Kelley | 309331f | 2006-04-22 15:05:01 +0100 | [diff] [blame] | 115 | /* Note: have to check all and not bail out early, so that we set the |
| 116 | "used" flags. */ |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 117 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 118 | if (daemon->if_names || daemon->if_addrs) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 119 | { |
| 120 | #ifdef HAVE_DHCP |
| 121 | struct dhcp_context *range; |
| 122 | #endif |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 123 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 124 | ret = 0; |
| 125 | |
| 126 | #ifdef HAVE_DHCP |
| 127 | for (range = daemon->dhcp; range; range = range->next) |
| 128 | if (range->interface && strcmp(range->interface, name) == 0) |
| 129 | ret = 1; |
| 130 | #endif |
| 131 | |
| 132 | for (tmp = daemon->if_names; tmp; tmp = tmp->next) |
| 133 | if (tmp->name && (strcmp(tmp->name, name) == 0)) |
| 134 | ret = tmp->used = 1; |
| 135 | |
| 136 | for (tmp = daemon->if_addrs; tmp; tmp = tmp->next) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 137 | if (tmp->addr.sa.sa_family == family) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 138 | { |
| 139 | if (family == AF_INET && |
| 140 | tmp->addr.in.sin_addr.s_addr == addr->addr.addr4.s_addr) |
| 141 | ret = tmp->used = 1; |
| 142 | #ifdef HAVE_IPV6 |
| 143 | else if (family == AF_INET6 && |
| 144 | IN6_ARE_ADDR_EQUAL(&tmp->addr.in6.sin6_addr, |
| 145 | &addr->addr.addr6)) |
| 146 | ret = tmp->used = 1; |
| 147 | #endif |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | for (tmp = daemon->if_except; tmp; tmp = tmp->next) |
| 152 | if (tmp->name && (strcmp(tmp->name, name) == 0)) |
| 153 | ret = 0; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 154 | |
Simon Kelley | 309331f | 2006-04-22 15:05:01 +0100 | [diff] [blame] | 155 | return ret; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 158 | static int iface_allowed(struct irec **irecp, int if_index, |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 159 | union mysockaddr *addr, struct in_addr netmask, int dad) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 160 | { |
| 161 | struct irec *iface; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 162 | int fd, mtu = 0, loopback; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 163 | struct ifreq ifr; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 164 | int tftp_ok = daemon->tftp_unlimited; |
Simon Kelley | 9380ba7 | 2012-04-16 14:41:56 +0100 | [diff] [blame] | 165 | int dhcp_ok = 1; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 166 | #ifdef HAVE_DHCP |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 167 | struct iname *tmp; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 168 | #endif |
| 169 | struct interface_list *ir = NULL; |
| 170 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 171 | /* check whether the interface IP has been added already |
| 172 | we call this routine multiple times. */ |
| 173 | for (iface = *irecp; iface; iface = iface->next) |
| 174 | if (sockaddr_isequal(&iface->addr, addr)) |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 175 | { |
| 176 | iface->dad = dad; |
| 177 | return 1; |
| 178 | } |
| 179 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 180 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1 || |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 181 | !indextoname(fd, if_index, ifr.ifr_name) || |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 182 | ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) |
| 183 | { |
| 184 | if (fd != -1) |
| 185 | { |
| 186 | int errsave = errno; |
| 187 | close(fd); |
| 188 | errno = errsave; |
| 189 | } |
| 190 | return 0; |
| 191 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 192 | |
| 193 | loopback = ifr.ifr_flags & IFF_LOOPBACK; |
Simon Kelley | 9380ba7 | 2012-04-16 14:41:56 +0100 | [diff] [blame] | 194 | |
| 195 | if (loopback) |
| 196 | dhcp_ok = 0; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 197 | |
| 198 | if (ioctl(fd, SIOCGIFMTU, &ifr) != -1) |
| 199 | mtu = ifr.ifr_mtu; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 200 | |
| 201 | close(fd); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 202 | |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 203 | /* If we are restricting the set of interfaces to use, make |
| 204 | sure that loopback interfaces are in that set. */ |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 205 | if (daemon->if_names && loopback) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 206 | { |
| 207 | struct iname *lo; |
| 208 | for (lo = daemon->if_names; lo; lo = lo->next) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 209 | if (lo->name && strcmp(lo->name, ifr.ifr_name) == 0) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 210 | { |
| 211 | lo->isloop = 1; |
| 212 | break; |
| 213 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 214 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 215 | if (!lo && |
| 216 | (lo = whine_malloc(sizeof(struct iname))) && |
| 217 | (lo->name = whine_malloc(strlen(ifr.ifr_name)+1))) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 218 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 219 | strcpy(lo->name, ifr.ifr_name); |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 220 | lo->isloop = lo->used = 1; |
| 221 | lo->next = daemon->if_names; |
| 222 | daemon->if_names = lo; |
| 223 | } |
| 224 | } |
| 225 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 226 | #ifdef HAVE_TFTP |
| 227 | /* implement wierd TFTP service rules */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 228 | for (ir = daemon->tftp_interfaces; ir; ir = ir->next) |
| 229 | if (strcmp(ir->interface, ifr.ifr_name) == 0) |
| 230 | { |
| 231 | tftp_ok = 1; |
| 232 | break; |
| 233 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 234 | #endif |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 235 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 236 | if (!ir) |
| 237 | { |
| 238 | if (addr->sa.sa_family == AF_INET && |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 239 | !iface_check(AF_INET, (struct all_addr *)&addr->in.sin_addr, ifr.ifr_name)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 240 | return 1; |
| 241 | |
| 242 | #ifdef HAVE_DHCP |
| 243 | for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
| 244 | if (tmp->name && (strcmp(tmp->name, ifr.ifr_name) == 0)) |
Simon Kelley | 9380ba7 | 2012-04-16 14:41:56 +0100 | [diff] [blame] | 245 | { |
| 246 | tftp_ok = 0; |
| 247 | dhcp_ok = 0; |
| 248 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 249 | #endif |
| 250 | |
| 251 | #ifdef HAVE_IPV6 |
| 252 | if (addr->sa.sa_family == AF_INET6 && |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 253 | !iface_check(AF_INET6, (struct all_addr *)&addr->in6.sin6_addr, ifr.ifr_name)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 254 | return 1; |
| 255 | #endif |
| 256 | } |
| 257 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 258 | /* add to list */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 259 | if ((iface = whine_malloc(sizeof(struct irec)))) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 260 | { |
| 261 | iface->addr = *addr; |
| 262 | iface->netmask = netmask; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 263 | iface->tftp_ok = tftp_ok; |
Simon Kelley | 9380ba7 | 2012-04-16 14:41:56 +0100 | [diff] [blame] | 264 | iface->dhcp_ok = dhcp_ok; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 265 | iface->mtu = mtu; |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 266 | iface->dad = dad; |
| 267 | iface->done = 0; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 268 | if ((iface->name = whine_malloc(strlen(ifr.ifr_name)+1))) |
Simon Kelley | 6f13e53 | 2012-04-17 14:25:06 +0100 | [diff] [blame] | 269 | { |
| 270 | strcpy(iface->name, ifr.ifr_name); |
| 271 | iface->next = *irecp; |
| 272 | *irecp = iface; |
| 273 | return 1; |
| 274 | } |
| 275 | free(iface); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 278 | errno = ENOMEM; |
| 279 | return 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 282 | #ifdef HAVE_IPV6 |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 283 | static int iface_allowed_v6(struct in6_addr *local, int prefix, |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 284 | int scope, int if_index, int dad, void *vparam) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 285 | { |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 286 | union mysockaddr addr; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 287 | struct in_addr netmask; /* dummy */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 288 | netmask.s_addr = 0; |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 289 | |
| 290 | (void)prefix; /* warning */ |
| 291 | (void)scope; /* warning */ |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 292 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 293 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 294 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 295 | addr.in6.sin6_len = sizeof(addr.in6); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 296 | #endif |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 297 | addr.in6.sin6_family = AF_INET6; |
| 298 | addr.in6.sin6_addr = *local; |
| 299 | addr.in6.sin6_port = htons(daemon->port); |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 300 | addr.in6.sin6_scope_id = if_index; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 301 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 302 | return iface_allowed((struct irec **)vparam, if_index, &addr, netmask, dad); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 303 | } |
| 304 | #endif |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 305 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 306 | static int iface_allowed_v4(struct in_addr local, int if_index, |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 307 | struct in_addr netmask, struct in_addr broadcast, void *vparam) |
| 308 | { |
| 309 | union mysockaddr addr; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 310 | |
| 311 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 312 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 313 | addr.in.sin_len = sizeof(addr.in); |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 314 | #endif |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 315 | addr.in.sin_family = AF_INET; |
| 316 | addr.in.sin_addr = broadcast; /* warning */ |
| 317 | addr.in.sin_addr = local; |
| 318 | addr.in.sin_port = htons(daemon->port); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 319 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 320 | return iface_allowed((struct irec **)vparam, if_index, &addr, netmask, 0); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 321 | } |
| 322 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 323 | int enumerate_interfaces(void) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 324 | { |
| 325 | #ifdef HAVE_IPV6 |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 326 | if (!iface_enumerate(AF_INET6, &daemon->interfaces, iface_allowed_v6)) |
| 327 | return 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 328 | #endif |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 329 | |
| 330 | return iface_enumerate(AF_INET, &daemon->interfaces, iface_allowed_v4); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 333 | /* set NONBLOCK bit on fd: See Stevens 16.6 */ |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 334 | int fix_fd(int fd) |
| 335 | { |
| 336 | int flags; |
| 337 | |
| 338 | if ((flags = fcntl(fd, F_GETFL)) == -1 || |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 339 | fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 340 | return 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 341 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 342 | return 1; |
| 343 | } |
| 344 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 345 | static int make_sock(union mysockaddr *addr, int type, int dienow) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 346 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 347 | int family = addr->sa.sa_family; |
| 348 | int fd, rc, opt = 1; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 349 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 350 | if ((fd = socket(family, type, 0)) == -1) |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 351 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 352 | int port; |
| 353 | |
| 354 | /* No error if the kernel just doesn't support this IP flavour */ |
| 355 | if (errno == EPROTONOSUPPORT || |
| 356 | errno == EAFNOSUPPORT || |
| 357 | errno == EINVAL) |
| 358 | return -1; |
| 359 | |
| 360 | err: |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 361 | if (dienow) |
| 362 | { |
| 363 | port = prettyprint_addr(addr, daemon->namebuff); |
| 364 | if (!option_bool(OPT_NOWILD)) |
| 365 | sprintf(daemon->namebuff, "port %d", port); |
| 366 | die(_("failed to create listening socket for %s: %s"), |
| 367 | daemon->namebuff, EC_BADNET); |
| 368 | |
| 369 | } |
| 370 | return -1; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 || !fix_fd(fd)) |
| 374 | goto err; |
| 375 | |
| 376 | #ifdef HAVE_IPV6 |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 377 | if (family == AF_INET6 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)) == -1) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 378 | goto err; |
| 379 | #endif |
| 380 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 381 | if ((rc = bind(fd, (struct sockaddr *)addr, sa_len(addr))) == -1) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 382 | goto err; |
| 383 | |
| 384 | if (type == SOCK_STREAM) |
| 385 | { |
| 386 | if (listen(fd, 5) == -1) |
| 387 | goto err; |
| 388 | } |
| 389 | else if (!option_bool(OPT_NOWILD)) |
| 390 | { |
| 391 | if (family == AF_INET) |
| 392 | { |
| 393 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 394 | if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt)) == -1) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 395 | goto err; |
| 396 | #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF) |
| 397 | if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt)) == -1 || |
| 398 | setsockopt(fd, IPPROTO_IP, IP_RECVIF, &opt, sizeof(opt)) == -1) |
| 399 | goto err; |
| 400 | #endif |
| 401 | } |
| 402 | #ifdef HAVE_IPV6 |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 403 | else if (!set_ipv6pktinfo(fd)) |
| 404 | goto err; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 405 | #endif |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 406 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 407 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 408 | return fd; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 409 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 410 | |
| 411 | #ifdef HAVE_IPV6 |
| 412 | int set_ipv6pktinfo(int fd) |
| 413 | { |
| 414 | int opt = 1; |
| 415 | |
| 416 | /* The API changed around Linux 2.6.14 but the old ABI is still supported: |
| 417 | handle all combinations of headers and kernel. |
| 418 | OpenWrt note that this fixes the problem addressed by your very broken patch. */ |
| 419 | daemon->v6pktinfo = IPV6_PKTINFO; |
| 420 | |
| 421 | #ifdef IPV6_RECVPKTINFO |
| 422 | if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &opt, sizeof(opt)) != -1) |
| 423 | return 1; |
| 424 | # ifdef IPV6_2292PKTINFO |
| 425 | else if (errno == ENOPROTOOPT && setsockopt(fd, IPPROTO_IPV6, IPV6_2292PKTINFO, &opt, sizeof(opt)) != -1) |
| 426 | { |
| 427 | daemon->v6pktinfo = IPV6_2292PKTINFO; |
| 428 | return 1; |
| 429 | } |
| 430 | # endif |
| 431 | #else |
| 432 | if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &opt, sizeof(opt)) != -1) |
| 433 | return 1; |
| 434 | #endif |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | #endif |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 439 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 440 | static struct listener *create_listeners(union mysockaddr *addr, int do_tftp, int dienow) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 441 | { |
| 442 | struct listener *l = NULL; |
| 443 | int fd = -1, tcpfd = -1, tftpfd = -1; |
| 444 | |
| 445 | if (daemon->port != 0) |
| 446 | { |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 447 | fd = make_sock(addr, SOCK_DGRAM, dienow); |
| 448 | tcpfd = make_sock(addr, SOCK_STREAM, dienow); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | #ifdef HAVE_TFTP |
| 452 | if (do_tftp) |
| 453 | { |
| 454 | if (addr->sa.sa_family == AF_INET) |
| 455 | { |
| 456 | /* port must be restored to DNS port for TCP code */ |
| 457 | short save = addr->in.sin_port; |
| 458 | addr->in.sin_port = htons(TFTP_PORT); |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 459 | tftpfd = make_sock(addr, SOCK_DGRAM, dienow); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 460 | addr->in.sin_port = save; |
| 461 | } |
| 462 | # ifdef HAVE_IPV6 |
| 463 | else |
| 464 | { |
| 465 | short save = addr->in6.sin6_port; |
| 466 | addr->in6.sin6_port = htons(TFTP_PORT); |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 467 | tftpfd = make_sock(addr, SOCK_DGRAM, dienow); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 468 | addr->in6.sin6_port = save; |
| 469 | } |
| 470 | # endif |
| 471 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 472 | #endif |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 473 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 474 | if (fd != -1 || tcpfd != -1 || tftpfd != -1) |
| 475 | { |
| 476 | l = safe_malloc(sizeof(struct listener)); |
| 477 | l->next = NULL; |
| 478 | l->family = addr->sa.sa_family; |
| 479 | l->fd = fd; |
| 480 | l->tcpfd = tcpfd; |
| 481 | l->tftpfd = tftpfd; |
| 482 | } |
| 483 | |
| 484 | return l; |
| 485 | } |
| 486 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 487 | void create_wildcard_listeners(void) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 488 | { |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 489 | union mysockaddr addr; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 490 | struct listener *l; |
| 491 | int tftp_enabled = daemon->tftp_unlimited || daemon->tftp_interfaces; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 492 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 493 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 494 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 495 | addr.in.sin_len = sizeof(addr.in); |
| 496 | #endif |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 497 | addr.in.sin_family = AF_INET; |
| 498 | addr.in.sin_addr.s_addr = INADDR_ANY; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 499 | addr.in.sin_port = htons(daemon->port); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 500 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 501 | l = create_listeners(&addr, tftp_enabled, 1); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 502 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 503 | #ifdef HAVE_IPV6 |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 504 | memset(&addr, 0, sizeof(addr)); |
| 505 | # ifdef HAVE_SOCKADDR_SA_LEN |
| 506 | addr.in6.sin6_len = sizeof(addr.in6); |
| 507 | # endif |
| 508 | addr.in6.sin6_family = AF_INET6; |
| 509 | addr.in6.sin6_addr = in6addr_any; |
| 510 | addr.in6.sin6_port = htons(daemon->port); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 511 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 512 | if (l) |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 513 | l->next = create_listeners(&addr, tftp_enabled, 1); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 514 | else |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 515 | l = create_listeners(&addr, tftp_enabled, 1); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 516 | #endif |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 517 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 518 | daemon->listeners = l; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 521 | void create_bound_listeners(int dienow) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 522 | { |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 523 | struct listener *new; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 524 | struct irec *iface; |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 525 | struct iname *if_tmp; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 526 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 527 | for (iface = daemon->interfaces; iface; iface = iface->next) |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 528 | if (!iface->done && !iface->dad && |
| 529 | (new = create_listeners(&iface->addr, iface->tftp_ok, dienow))) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 530 | { |
| 531 | new->iface = iface; |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 532 | new->next = daemon->listeners; |
| 533 | daemon->listeners = new; |
| 534 | iface->done = 1; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 535 | } |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 536 | |
| 537 | /* Check for --listen-address options that haven't been used because there's |
| 538 | no interface with a matching address. These may be valid: eg it's possible |
| 539 | to listen on 127.0.1.1 even if the loopback interface is 127.0.0.1 |
| 540 | |
| 541 | If the address isn't valid the bind() will fail and we'll die(). |
| 542 | |
| 543 | The resulting listeners have the ->iface field NULL, and this has to be |
| 544 | handled by the DNS and TFTP code. It disables --localise-queries processing |
| 545 | (no netmask) and some MTU login the tftp code. */ |
| 546 | |
| 547 | for (if_tmp = daemon->if_addrs; if_tmp; if_tmp = if_tmp->next) |
| 548 | if (!if_tmp->used && |
| 549 | (new = create_listeners(&if_tmp->addr, 1, dienow))) |
| 550 | { |
| 551 | new->iface = NULL; |
| 552 | new->next = daemon->listeners; |
| 553 | daemon->listeners = new; |
| 554 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 557 | int is_dad_listeners(void) |
| 558 | { |
| 559 | struct irec *iface; |
| 560 | |
| 561 | if (option_bool(OPT_NOWILD)) |
| 562 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 563 | if (iface->dad && !iface->done) |
| 564 | return 1; |
| 565 | |
| 566 | return 0; |
| 567 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 568 | /* return a UDP socket bound to a random port, have to cope with straying into |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 569 | occupied port nos and reserved ones. */ |
| 570 | int random_sock(int family) |
| 571 | { |
| 572 | int fd; |
| 573 | |
| 574 | if ((fd = socket(family, SOCK_DGRAM, 0)) != -1) |
| 575 | { |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 576 | union mysockaddr addr; |
Simon Kelley | 1ad24ae | 2008-07-20 20:22:50 +0100 | [diff] [blame] | 577 | unsigned int ports_avail = 65536u - (unsigned short)daemon->min_port; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 578 | int tries = ports_avail < 30 ? 3 * ports_avail : 100; |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 579 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 580 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 581 | addr.sa.sa_family = family; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 582 | |
Simon Kelley | 1ad24ae | 2008-07-20 20:22:50 +0100 | [diff] [blame] | 583 | /* don't loop forever if all ports in use. */ |
| 584 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 585 | if (fix_fd(fd)) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 586 | while(tries--) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 587 | { |
| 588 | unsigned short port = rand16(); |
| 589 | |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 590 | if (daemon->min_port != 0) |
Simon Kelley | 1ad24ae | 2008-07-20 20:22:50 +0100 | [diff] [blame] | 591 | port = htons(daemon->min_port + (port % ((unsigned short)ports_avail))); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 592 | |
| 593 | if (family == AF_INET) |
| 594 | { |
| 595 | addr.in.sin_addr.s_addr = INADDR_ANY; |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 596 | addr.in.sin_port = port; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 597 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 598 | addr.in.sin_len = sizeof(struct sockaddr_in); |
| 599 | #endif |
| 600 | } |
| 601 | #ifdef HAVE_IPV6 |
| 602 | else |
| 603 | { |
| 604 | addr.in6.sin6_addr = in6addr_any; |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 605 | addr.in6.sin6_port = port; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 606 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 607 | addr.in6.sin6_len = sizeof(struct sockaddr_in6); |
| 608 | #endif |
| 609 | } |
| 610 | #endif |
| 611 | |
| 612 | if (bind(fd, (struct sockaddr *)&addr, sa_len(&addr)) == 0) |
| 613 | return fd; |
| 614 | |
| 615 | if (errno != EADDRINUSE && errno != EACCES) |
| 616 | break; |
| 617 | } |
| 618 | |
| 619 | close(fd); |
| 620 | } |
| 621 | |
| 622 | return -1; |
| 623 | } |
| 624 | |
| 625 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 626 | int local_bind(int fd, union mysockaddr *addr, char *intname, int is_tcp) |
| 627 | { |
| 628 | union mysockaddr addr_copy = *addr; |
| 629 | |
| 630 | /* cannot set source _port_ for TCP connections. */ |
| 631 | if (is_tcp) |
| 632 | { |
| 633 | if (addr_copy.sa.sa_family == AF_INET) |
| 634 | addr_copy.in.sin_port = 0; |
| 635 | #ifdef HAVE_IPV6 |
| 636 | else |
| 637 | addr_copy.in6.sin6_port = 0; |
| 638 | #endif |
| 639 | } |
| 640 | |
| 641 | if (bind(fd, (struct sockaddr *)&addr_copy, sa_len(&addr_copy)) == -1) |
| 642 | return 0; |
| 643 | |
| 644 | #if defined(SO_BINDTODEVICE) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 645 | if (intname[0] != 0 && |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 646 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, intname, IF_NAMESIZE) == -1) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 647 | return 0; |
| 648 | #endif |
| 649 | |
| 650 | return 1; |
| 651 | } |
| 652 | |
| 653 | static struct serverfd *allocate_sfd(union mysockaddr *addr, char *intname) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 654 | { |
| 655 | struct serverfd *sfd; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 656 | int errsave; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 657 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 658 | /* when using random ports, servers which would otherwise use |
| 659 | the INADDR_ANY/port0 socket have sfd set to NULL */ |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 660 | if (!daemon->osport && intname[0] == 0) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 661 | { |
| 662 | errno = 0; |
| 663 | |
| 664 | if (addr->sa.sa_family == AF_INET && |
| 665 | addr->in.sin_addr.s_addr == INADDR_ANY && |
| 666 | addr->in.sin_port == htons(0)) |
| 667 | return NULL; |
| 668 | |
| 669 | #ifdef HAVE_IPV6 |
| 670 | if (addr->sa.sa_family == AF_INET6 && |
| 671 | memcmp(&addr->in6.sin6_addr, &in6addr_any, sizeof(in6addr_any)) == 0 && |
| 672 | addr->in6.sin6_port == htons(0)) |
| 673 | return NULL; |
| 674 | #endif |
| 675 | } |
| 676 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 677 | /* may have a suitable one already */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 678 | for (sfd = daemon->sfds; sfd; sfd = sfd->next ) |
| 679 | if (sockaddr_isequal(&sfd->source_addr, addr) && |
| 680 | strcmp(intname, sfd->interface) == 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 681 | return sfd; |
| 682 | |
| 683 | /* need to make a new one. */ |
| 684 | errno = ENOMEM; /* in case malloc fails. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 685 | if (!(sfd = whine_malloc(sizeof(struct serverfd)))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 686 | return NULL; |
| 687 | |
| 688 | if ((sfd->fd = socket(addr->sa.sa_family, SOCK_DGRAM, 0)) == -1) |
| 689 | { |
| 690 | free(sfd); |
| 691 | return NULL; |
| 692 | } |
| 693 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 694 | if (!local_bind(sfd->fd, addr, intname, 0) || !fix_fd(sfd->fd)) |
| 695 | { |
| 696 | errsave = errno; /* save error from bind. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 697 | close(sfd->fd); |
| 698 | free(sfd); |
| 699 | errno = errsave; |
| 700 | return NULL; |
| 701 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 702 | |
| 703 | strcpy(sfd->interface, intname); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 704 | sfd->source_addr = *addr; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 705 | sfd->next = daemon->sfds; |
| 706 | daemon->sfds = sfd; |
| 707 | return sfd; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 710 | /* create upstream sockets during startup, before root is dropped which may be needed |
| 711 | this allows query_port to be a low port and interface binding */ |
| 712 | void pre_allocate_sfds(void) |
| 713 | { |
| 714 | struct server *srv; |
| 715 | |
| 716 | if (daemon->query_port != 0) |
| 717 | { |
| 718 | union mysockaddr addr; |
| 719 | memset(&addr, 0, sizeof(addr)); |
| 720 | addr.in.sin_family = AF_INET; |
| 721 | addr.in.sin_addr.s_addr = INADDR_ANY; |
| 722 | addr.in.sin_port = htons(daemon->query_port); |
| 723 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 724 | addr.in.sin_len = sizeof(struct sockaddr_in); |
| 725 | #endif |
| 726 | allocate_sfd(&addr, ""); |
| 727 | #ifdef HAVE_IPV6 |
| 728 | memset(&addr, 0, sizeof(addr)); |
| 729 | addr.in6.sin6_family = AF_INET6; |
| 730 | addr.in6.sin6_addr = in6addr_any; |
| 731 | addr.in6.sin6_port = htons(daemon->query_port); |
| 732 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 733 | addr.in6.sin6_len = sizeof(struct sockaddr_in6); |
| 734 | #endif |
| 735 | allocate_sfd(&addr, ""); |
| 736 | #endif |
| 737 | } |
| 738 | |
| 739 | for (srv = daemon->servers; srv; srv = srv->next) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 740 | if (!(srv->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR | SERV_USE_RESOLV | SERV_NO_REBIND)) && |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 741 | !allocate_sfd(&srv->source_addr, srv->interface) && |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 742 | errno != 0 && |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 743 | option_bool(OPT_NOWILD)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 744 | { |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 745 | prettyprint_addr(&srv->source_addr, daemon->namebuff); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 746 | if (srv->interface[0] != 0) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 747 | { |
| 748 | strcat(daemon->namebuff, " "); |
| 749 | strcat(daemon->namebuff, srv->interface); |
| 750 | } |
| 751 | die(_("failed to bind server socket for %s: %s"), |
| 752 | daemon->namebuff, EC_BADNET); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 757 | void check_servers(void) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 758 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 759 | struct irec *iface; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 760 | struct server *new, *tmp, *ret = NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 761 | int port = 0; |
| 762 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 763 | /* interface may be new since startup */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 764 | if (!option_bool(OPT_NOWILD)) |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 765 | enumerate_interfaces(); |
| 766 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 767 | for (new = daemon->servers; new; new = tmp) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 768 | { |
| 769 | tmp = new->next; |
| 770 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 771 | if (!(new->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR | SERV_USE_RESOLV | SERV_NO_REBIND))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 772 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 773 | port = prettyprint_addr(&new->addr, daemon->namebuff); |
| 774 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 775 | /* 0.0.0.0 is nothing, the stack treats it like 127.0.0.1 */ |
| 776 | if (new->addr.sa.sa_family == AF_INET && |
| 777 | new->addr.in.sin_addr.s_addr == 0) |
| 778 | { |
| 779 | free(new); |
| 780 | continue; |
| 781 | } |
| 782 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 783 | for (iface = daemon->interfaces; iface; iface = iface->next) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 784 | if (sockaddr_isequal(&new->addr, &iface->addr)) |
| 785 | break; |
| 786 | if (iface) |
| 787 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 788 | my_syslog(LOG_WARNING, _("ignoring nameserver %s - local interface"), daemon->namebuff); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 789 | free(new); |
| 790 | continue; |
| 791 | } |
| 792 | |
| 793 | /* Do we need a socket set? */ |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 794 | if (!new->sfd && |
| 795 | !(new->sfd = allocate_sfd(&new->source_addr, new->interface)) && |
| 796 | errno != 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 797 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 798 | my_syslog(LOG_WARNING, |
| 799 | _("ignoring nameserver %s - cannot make/bind socket: %s"), |
| 800 | daemon->namebuff, strerror(errno)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 801 | free(new); |
| 802 | continue; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | /* reverse order - gets it right. */ |
| 807 | new->next = ret; |
| 808 | ret = new; |
| 809 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 810 | if (!(new->flags & SERV_NO_REBIND)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 811 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 812 | if (new->flags & (SERV_HAS_DOMAIN | SERV_FOR_NODOTS | SERV_USE_RESOLV)) |
| 813 | { |
| 814 | char *s1, *s2; |
| 815 | if (!(new->flags & SERV_HAS_DOMAIN)) |
| 816 | s1 = _("unqualified"), s2 = _("names"); |
| 817 | else if (strlen(new->domain) == 0) |
| 818 | s1 = _("default"), s2 = ""; |
| 819 | else |
| 820 | s1 = _("domain"), s2 = new->domain; |
| 821 | |
| 822 | if (new->flags & SERV_NO_ADDR) |
| 823 | my_syslog(LOG_INFO, _("using local addresses only for %s %s"), s1, s2); |
| 824 | else if (new->flags & SERV_USE_RESOLV) |
| 825 | my_syslog(LOG_INFO, _("using standard nameservers for %s %s"), s1, s2); |
| 826 | else if (!(new->flags & SERV_LITERAL_ADDRESS)) |
| 827 | my_syslog(LOG_INFO, _("using nameserver %s#%d for %s %s"), daemon->namebuff, port, s1, s2); |
| 828 | } |
| 829 | else if (new->interface[0] != 0) |
| 830 | my_syslog(LOG_INFO, _("using nameserver %s#%d(via %s)"), daemon->namebuff, port, new->interface); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 831 | else |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 832 | my_syslog(LOG_INFO, _("using nameserver %s#%d"), daemon->namebuff, port); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 833 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 836 | daemon->servers = ret; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 837 | } |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 838 | |
| 839 | /* Return zero if no servers found, in that case we keep polling. |
| 840 | This is a protection against an update-time/write race on resolv.conf */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 841 | int reload_servers(char *fname) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 842 | { |
| 843 | FILE *f; |
| 844 | char *line; |
| 845 | struct server *old_servers = NULL; |
| 846 | struct server *new_servers = NULL; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 847 | struct server *serv; |
| 848 | int gotone = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 849 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 850 | /* buff happens to be MAXDNAME long... */ |
| 851 | if (!(f = fopen(fname, "r"))) |
| 852 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 853 | my_syslog(LOG_ERR, _("failed to read %s: %s"), fname, strerror(errno)); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 854 | return 0; |
| 855 | } |
| 856 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 857 | /* move old servers to free list - we can reuse the memory |
| 858 | and not risk malloc if there are the same or fewer new servers. |
| 859 | Servers which were specced on the command line go to the new list. */ |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 860 | for (serv = daemon->servers; serv;) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 861 | { |
| 862 | struct server *tmp = serv->next; |
| 863 | if (serv->flags & SERV_FROM_RESOLV) |
| 864 | { |
| 865 | serv->next = old_servers; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 866 | old_servers = serv; |
| 867 | /* forward table rules reference servers, so have to blow them away */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 868 | server_gone(serv); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 869 | } |
| 870 | else |
| 871 | { |
| 872 | serv->next = new_servers; |
| 873 | new_servers = serv; |
| 874 | } |
| 875 | serv = tmp; |
| 876 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 877 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 878 | while ((line = fgets(daemon->namebuff, MAXDNAME, f))) |
| 879 | { |
| 880 | union mysockaddr addr, source_addr; |
| 881 | char *token = strtok(line, " \t\n\r"); |
| 882 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 883 | if (!token) |
| 884 | continue; |
| 885 | if (strcmp(token, "nameserver") != 0 && strcmp(token, "server") != 0) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 886 | continue; |
| 887 | if (!(token = strtok(NULL, " \t\n\r"))) |
| 888 | continue; |
| 889 | |
| 890 | memset(&addr, 0, sizeof(addr)); |
| 891 | memset(&source_addr, 0, sizeof(source_addr)); |
| 892 | |
| 893 | if ((addr.in.sin_addr.s_addr = inet_addr(token)) != (in_addr_t) -1) |
| 894 | { |
| 895 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 896 | source_addr.in.sin_len = addr.in.sin_len = sizeof(source_addr.in); |
| 897 | #endif |
| 898 | source_addr.in.sin_family = addr.in.sin_family = AF_INET; |
| 899 | addr.in.sin_port = htons(NAMESERVER_PORT); |
| 900 | source_addr.in.sin_addr.s_addr = INADDR_ANY; |
| 901 | source_addr.in.sin_port = htons(daemon->query_port); |
| 902 | } |
| 903 | #ifdef HAVE_IPV6 |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 904 | else |
| 905 | { |
| 906 | int scope_index = 0; |
| 907 | char *scope_id = strchr(token, '%'); |
| 908 | |
| 909 | if (scope_id) |
| 910 | { |
| 911 | *(scope_id++) = 0; |
| 912 | scope_index = if_nametoindex(scope_id); |
| 913 | } |
| 914 | |
| 915 | if (inet_pton(AF_INET6, token, &addr.in6.sin6_addr) > 0) |
| 916 | { |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 917 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 918 | source_addr.in6.sin6_len = addr.in6.sin6_len = sizeof(source_addr.in6); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 919 | #endif |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 920 | source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6; |
| 921 | source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0; |
| 922 | addr.in6.sin6_port = htons(NAMESERVER_PORT); |
| 923 | addr.in6.sin6_scope_id = scope_index; |
| 924 | source_addr.in6.sin6_addr = in6addr_any; |
| 925 | source_addr.in6.sin6_port = htons(daemon->query_port); |
| 926 | source_addr.in6.sin6_scope_id = 0; |
| 927 | } |
| 928 | else |
| 929 | continue; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 930 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 931 | #else /* IPV6 */ |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 932 | else |
| 933 | continue; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 934 | #endif |
| 935 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 936 | if (old_servers) |
| 937 | { |
| 938 | serv = old_servers; |
| 939 | old_servers = old_servers->next; |
| 940 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 941 | else if (!(serv = whine_malloc(sizeof (struct server)))) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 942 | continue; |
| 943 | |
| 944 | /* this list is reverse ordered: |
| 945 | it gets reversed again in check_servers */ |
| 946 | serv->next = new_servers; |
| 947 | new_servers = serv; |
| 948 | serv->addr = addr; |
| 949 | serv->source_addr = source_addr; |
| 950 | serv->domain = NULL; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 951 | serv->interface[0] = 0; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 952 | serv->sfd = NULL; |
| 953 | serv->flags = SERV_FROM_RESOLV; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 954 | serv->queries = serv->failed_queries = 0; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 955 | gotone = 1; |
| 956 | } |
| 957 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 958 | /* Free any memory not used. */ |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 959 | while (old_servers) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 960 | { |
| 961 | struct server *tmp = old_servers->next; |
| 962 | free(old_servers); |
| 963 | old_servers = tmp; |
| 964 | } |
| 965 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 966 | daemon->servers = new_servers; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 967 | fclose(f); |
| 968 | |
| 969 | return gotone; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 973 | /* Use an IPv4 listener socket for ioctling */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 974 | struct in_addr get_ifaddr(char *intr) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 975 | { |
| 976 | struct listener *l; |
| 977 | struct ifreq ifr; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 978 | struct sockaddr_in ret; |
| 979 | |
| 980 | ret.sin_addr.s_addr = -1; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 981 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 982 | for (l = daemon->listeners; |
| 983 | l && (l->family != AF_INET || l->fd == -1); |
| 984 | l = l->next); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 985 | |
| 986 | strncpy(ifr.ifr_name, intr, IF_NAMESIZE); |
| 987 | ifr.ifr_addr.sa_family = AF_INET; |
| 988 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 989 | if (l && ioctl(l->fd, SIOCGIFADDR, &ifr) != -1) |
| 990 | memcpy(&ret, &ifr.ifr_addr, sizeof(ret)); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 991 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 992 | return ret.sin_addr; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 993 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 994 | |
| 995 | |
| 996 | |