| 1 | /* |
| 2 | * event.c |
| 3 | * - event loop core |
| 4 | * - TCP connection management |
| 5 | * - user-visible check/wait and event-loop-related functions |
| 6 | */ |
| 7 | /* |
| 8 | * This file is |
| 9 | * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
| 10 | * |
| 11 | * It is part of adns, which is |
| 12 | * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
| 13 | * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at> |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2, or (at your option) |
| 18 | * any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write to the Free Software Foundation, |
| 27 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | #include <errno.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/time.h> |
| 36 | #include <netdb.h> |
| 37 | #include <sys/socket.h> |
| 38 | #include <netinet/in.h> |
| 39 | #include <arpa/inet.h> |
| 40 | |
| 41 | #include "internal.h" |
| 42 | #include "tvarith.h" |
| 43 | |
| 44 | /* TCP connection management. */ |
| 45 | |
| 46 | static void tcp_close(adns_state ads) { |
| 47 | int serv; |
| 48 | |
| 49 | serv= ads->tcpserver; |
| 50 | close(ads->tcpsocket); |
| 51 | ads->tcpsocket= -1; |
| 52 | ads->tcprecv.used= ads->tcprecv_skip= ads->tcpsend.used= 0; |
| 53 | } |
| 54 | |
| 55 | void adns__tcp_broken(adns_state ads, const char *what, const char *why) { |
| 56 | int serv; |
| 57 | adns_query qu; |
| 58 | |
| 59 | assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok); |
| 60 | serv= ads->tcpserver; |
| 61 | if (what) adns__warn(ads,serv,0,"TCP connection failed: %s: %s",what,why); |
| 62 | |
| 63 | if (ads->tcpstate == server_connecting) { |
| 64 | /* Counts as a retry for all the queries waiting for TCP. */ |
| 65 | for (qu= ads->tcpw.head; qu; qu= qu->next) |
| 66 | qu->retries++; |
| 67 | } |
| 68 | |
| 69 | tcp_close(ads); |
| 70 | ads->tcpstate= server_broken; |
| 71 | ads->tcpserver= (serv+1)%ads->nservers; |
| 72 | } |
| 73 | |
| 74 | static void tcp_connected(adns_state ads, struct timeval now) { |
| 75 | adns_query qu, nqu; |
| 76 | |
| 77 | adns__debug(ads,ads->tcpserver,0,"TCP connected"); |
| 78 | ads->tcpstate= server_ok; |
| 79 | for (qu= ads->tcpw.head; qu && ads->tcpstate == server_ok; qu= nqu) { |
| 80 | nqu= qu->next; |
| 81 | assert(qu->state == query_tcpw); |
| 82 | adns__querysend_tcp(qu,now); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void adns__tcp_tryconnect(adns_state ads, struct timeval now) { |
| 87 | int r, fd, tries; |
| 88 | struct sockaddr_in addr; |
| 89 | struct protoent *proto; |
| 90 | |
| 91 | for (tries=0; tries<ads->nservers; tries++) { |
| 92 | switch (ads->tcpstate) { |
| 93 | case server_connecting: |
| 94 | case server_ok: |
| 95 | case server_broken: |
| 96 | return; |
| 97 | case server_disconnected: |
| 98 | break; |
| 99 | default: |
| 100 | abort(); |
| 101 | } |
| 102 | |
| 103 | assert(!ads->tcpsend.used); |
| 104 | assert(!ads->tcprecv.used); |
| 105 | assert(!ads->tcprecv_skip); |
| 106 | |
| 107 | proto= getprotobyname("tcp"); |
| 108 | if (!proto) { adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); return; } |
| 109 | fd= socket(AF_INET,SOCK_STREAM,proto->p_proto); |
| 110 | if (fd<0) { |
| 111 | adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno)); |
| 112 | return; |
| 113 | } |
| 114 | r= adns__setnonblock(ads,fd); |
| 115 | if (r) { |
| 116 | adns__diag(ads,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r)); |
| 117 | close(fd); |
| 118 | return; |
| 119 | } |
| 120 | memset(&addr,0,sizeof(addr)); |
| 121 | addr.sin_family= AF_INET; |
| 122 | addr.sin_port= htons(DNS_PORT); |
| 123 | addr.sin_addr= ads->servers[ads->tcpserver].addr; |
| 124 | r= connect(fd,(const struct sockaddr*)&addr,sizeof(addr)); |
| 125 | ads->tcpsocket= fd; |
| 126 | ads->tcpstate= server_connecting; |
| 127 | if (r==0) { tcp_connected(ads,now); return; } |
| 128 | if (errno == EWOULDBLOCK || errno == EINPROGRESS) { |
| 129 | ads->tcptimeout= now; |
| 130 | timevaladd(&ads->tcptimeout,TCPCONNMS); |
| 131 | return; |
| 132 | } |
| 133 | adns__tcp_broken(ads,"connect",strerror(errno)); |
| 134 | ads->tcpstate= server_disconnected; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* Timeout handling functions. */ |
| 139 | |
| 140 | void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io, |
| 141 | struct timeval *tv_buf) { |
| 142 | const struct timeval *now; |
| 143 | int r; |
| 144 | |
| 145 | now= *now_io; |
| 146 | if (now) return; |
| 147 | r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; } |
| 148 | adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno)); |
| 149 | adns_globalsystemfailure(ads); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | static void inter_immed(struct timeval **tv_io, struct timeval *tvbuf) { |
| 154 | struct timeval *rbuf; |
| 155 | |
| 156 | if (!tv_io) return; |
| 157 | |
| 158 | rbuf= *tv_io; |
| 159 | if (!rbuf) { *tv_io= rbuf= tvbuf; } |
| 160 | |
| 161 | timerclear(rbuf); |
| 162 | } |
| 163 | |
| 164 | static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf, |
| 165 | struct timeval maxto) { |
| 166 | struct timeval *rbuf; |
| 167 | |
| 168 | if (!tv_io) return; |
| 169 | rbuf= *tv_io; |
| 170 | if (!rbuf) { |
| 171 | *tvbuf= maxto; *tv_io= tvbuf; |
| 172 | } else { |
| 173 | if (timercmp(rbuf,&maxto,>)) *rbuf= maxto; |
| 174 | } |
| 175 | /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n", |
| 176 | maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/ |
| 177 | } |
| 178 | |
| 179 | static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf, |
| 180 | struct timeval now, struct timeval maxtime) { |
| 181 | /* tv_io may be 0 */ |
| 182 | ldiv_t dr; |
| 183 | |
| 184 | /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n", |
| 185 | now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/ |
| 186 | if (!tv_io) return; |
| 187 | maxtime.tv_sec -= (now.tv_sec+2); |
| 188 | maxtime.tv_usec -= (now.tv_usec-2000000); |
| 189 | dr= ldiv(maxtime.tv_usec,1000000); |
| 190 | maxtime.tv_sec += dr.quot; |
| 191 | maxtime.tv_usec -= dr.quot*1000000; |
| 192 | if (maxtime.tv_sec<0) timerclear(&maxtime); |
| 193 | inter_maxto(tv_io,tvbuf,maxtime); |
| 194 | } |
| 195 | |
| 196 | static void timeouts_queue(adns_state ads, int act, |
| 197 | struct timeval **tv_io, struct timeval *tvbuf, |
| 198 | struct timeval now, struct query_queue *queue) { |
| 199 | adns_query qu, nqu; |
| 200 | |
| 201 | for (qu= queue->head; qu; qu= nqu) { |
| 202 | nqu= qu->next; |
| 203 | if (!timercmp(&now,&qu->timeout,>)) { |
| 204 | inter_maxtoabs(tv_io,tvbuf,now,qu->timeout); |
| 205 | } else { |
| 206 | if (!act) { inter_immed(tv_io,tvbuf); return; } |
| 207 | LIST_UNLINK(*queue,qu); |
| 208 | if (qu->state != query_tosend) { |
| 209 | adns__query_fail(qu,adns_s_timeout); |
| 210 | } else { |
| 211 | adns__query_send(qu,now); |
| 212 | } |
| 213 | nqu= queue->head; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static void tcp_events(adns_state ads, int act, |
| 219 | struct timeval **tv_io, struct timeval *tvbuf, |
| 220 | struct timeval now) { |
| 221 | adns_query qu, nqu; |
| 222 | |
| 223 | for (;;) { |
| 224 | switch (ads->tcpstate) { |
| 225 | case server_broken: |
| 226 | if (!act) { inter_immed(tv_io,tvbuf); return; } |
| 227 | for (qu= ads->tcpw.head; qu; qu= nqu) { |
| 228 | nqu= qu->next; |
| 229 | assert(qu->state == query_tcpw); |
| 230 | if (qu->retries > ads->nservers) { |
| 231 | LIST_UNLINK(ads->tcpw,qu); |
| 232 | adns__query_fail(qu,adns_s_allservfail); |
| 233 | } |
| 234 | } |
| 235 | ads->tcpstate= server_disconnected; |
| 236 | case server_disconnected: /* fall through */ |
| 237 | if (!ads->tcpw.head) return; |
| 238 | if (!act) { inter_immed(tv_io,tvbuf); return; } |
| 239 | adns__tcp_tryconnect(ads,now); |
| 240 | break; |
| 241 | case server_ok: |
| 242 | if (ads->tcpw.head) return; |
| 243 | if (!ads->tcptimeout.tv_sec) { |
| 244 | assert(!ads->tcptimeout.tv_usec); |
| 245 | ads->tcptimeout= now; |
| 246 | timevaladd(&ads->tcptimeout,TCPIDLEMS); |
| 247 | } |
| 248 | case server_connecting: /* fall through */ |
| 249 | if (!act || !timercmp(&now,&ads->tcptimeout,>)) { |
| 250 | inter_maxtoabs(tv_io,tvbuf,now,ads->tcptimeout); |
| 251 | return; |
| 252 | } { |
| 253 | /* TCP timeout has happened */ |
| 254 | switch (ads->tcpstate) { |
| 255 | case server_connecting: /* failed to connect */ |
| 256 | adns__tcp_broken(ads,"unable to make connection","timed out"); |
| 257 | break; |
| 258 | case server_ok: /* idle timeout */ |
| 259 | tcp_close(ads); |
| 260 | ads->tcpstate= server_disconnected; |
| 261 | return; |
| 262 | default: |
| 263 | abort(); |
| 264 | } |
| 265 | } |
| 266 | break; |
| 267 | default: |
| 268 | abort(); |
| 269 | } |
| 270 | } |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | void adns__timeouts(adns_state ads, int act, |
| 275 | struct timeval **tv_io, struct timeval *tvbuf, |
| 276 | struct timeval now) { |
| 277 | timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->udpw); |
| 278 | timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->tcpw); |
| 279 | tcp_events(ads,act,tv_io,tvbuf,now); |
| 280 | } |
| 281 | |
| 282 | void adns_firsttimeout(adns_state ads, |
| 283 | struct timeval **tv_io, struct timeval *tvbuf, |
| 284 | struct timeval now) { |
| 285 | adns__consistency(ads,0,cc_entex); |
| 286 | adns__timeouts(ads, 0, tv_io,tvbuf, now); |
| 287 | adns__consistency(ads,0,cc_entex); |
| 288 | } |
| 289 | |
| 290 | void adns_processtimeouts(adns_state ads, const struct timeval *now) { |
| 291 | struct timeval tv_buf; |
| 292 | |
| 293 | adns__consistency(ads,0,cc_entex); |
| 294 | adns__must_gettimeofday(ads,&now,&tv_buf); |
| 295 | if (now) adns__timeouts(ads, 1, 0,0, *now); |
| 296 | adns__consistency(ads,0,cc_entex); |
| 297 | } |
| 298 | |
| 299 | /* fd handling functions. These are the top-level of the real work of |
| 300 | * reception and often transmission. |
| 301 | */ |
| 302 | |
| 303 | int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) { |
| 304 | /* Returns the number of entries filled in. Always zeroes revents. */ |
| 305 | |
| 306 | assert(MAX_POLLFDS==2); |
| 307 | |
| 308 | pollfds_buf[0].fd= ads->udpsocket; |
| 309 | pollfds_buf[0].events= POLLIN; |
| 310 | pollfds_buf[0].revents= 0; |
| 311 | |
| 312 | switch (ads->tcpstate) { |
| 313 | case server_disconnected: |
| 314 | case server_broken: |
| 315 | return 1; |
| 316 | case server_connecting: |
| 317 | pollfds_buf[1].events= POLLOUT; |
| 318 | break; |
| 319 | case server_ok: |
| 320 | pollfds_buf[1].events= ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI; |
| 321 | break; |
| 322 | default: |
| 323 | abort(); |
| 324 | } |
| 325 | pollfds_buf[1].fd= ads->tcpsocket; |
| 326 | return 2; |
| 327 | } |
| 328 | |
| 329 | int adns_processreadable(adns_state ads, int fd, const struct timeval *now) { |
| 330 | int want, dgramlen, r, udpaddrlen, serv, old_skip; |
| 331 | byte udpbuf[DNS_MAXUDP]; |
| 332 | struct sockaddr_in udpaddr; |
| 333 | |
| 334 | adns__consistency(ads,0,cc_entex); |
| 335 | |
| 336 | switch (ads->tcpstate) { |
| 337 | case server_disconnected: |
| 338 | case server_broken: |
| 339 | case server_connecting: |
| 340 | break; |
| 341 | case server_ok: |
| 342 | if (fd != ads->tcpsocket) break; |
| 343 | assert(!ads->tcprecv_skip); |
| 344 | do { |
| 345 | if (ads->tcprecv.used >= ads->tcprecv_skip+2) { |
| 346 | dgramlen= ((ads->tcprecv.buf[ads->tcprecv_skip]<<8) | |
| 347 | ads->tcprecv.buf[ads->tcprecv_skip+1]); |
| 348 | if (ads->tcprecv.used >= ads->tcprecv_skip+2+dgramlen) { |
| 349 | old_skip= ads->tcprecv_skip; |
| 350 | ads->tcprecv_skip += 2+dgramlen; |
| 351 | adns__procdgram(ads, ads->tcprecv.buf+old_skip+2, |
| 352 | dgramlen, ads->tcpserver, 1,*now); |
| 353 | continue; |
| 354 | } else { |
| 355 | want= 2+dgramlen; |
| 356 | } |
| 357 | } else { |
| 358 | want= 2; |
| 359 | } |
| 360 | ads->tcprecv.used -= ads->tcprecv_skip; |
| 361 | memmove(ads->tcprecv.buf,ads->tcprecv.buf+ads->tcprecv_skip,ads->tcprecv.used); |
| 362 | ads->tcprecv_skip= 0; |
| 363 | if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; } |
| 364 | assert(ads->tcprecv.used <= ads->tcprecv.avail); |
| 365 | if (ads->tcprecv.used == ads->tcprecv.avail) continue; |
| 366 | r= read(ads->tcpsocket, |
| 367 | ads->tcprecv.buf+ads->tcprecv.used, |
| 368 | ads->tcprecv.avail-ads->tcprecv.used); |
| 369 | if (r>0) { |
| 370 | ads->tcprecv.used+= r; |
| 371 | } else { |
| 372 | if (r) { |
| 373 | if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; } |
| 374 | if (errno==EINTR) continue; |
| 375 | if (errno_resources(errno)) { r= errno; goto xit; } |
| 376 | } |
| 377 | adns__tcp_broken(ads,"read",r?strerror(errno):"closed"); |
| 378 | } |
| 379 | } while (ads->tcpstate == server_ok); |
| 380 | r= 0; goto xit; |
| 381 | default: |
| 382 | abort(); |
| 383 | } |
| 384 | if (fd == ads->udpsocket) { |
| 385 | for (;;) { |
| 386 | udpaddrlen= sizeof(udpaddr); |
| 387 | r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0, |
| 388 | (struct sockaddr*)&udpaddr,&udpaddrlen); |
| 389 | if (r<0) { |
| 390 | if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; } |
| 391 | if (errno == EINTR) continue; |
| 392 | if (errno_resources(errno)) { r= errno; goto xit; } |
| 393 | adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno)); |
| 394 | r= 0; goto xit; |
| 395 | } |
| 396 | if (udpaddrlen != sizeof(udpaddr)) { |
| 397 | adns__diag(ads,-1,0,"datagram received with wrong address length %d" |
| 398 | " (expected %lu)", udpaddrlen, |
| 399 | (unsigned long)sizeof(udpaddr)); |
| 400 | continue; |
| 401 | } |
| 402 | if (udpaddr.sin_family != AF_INET) { |
| 403 | adns__diag(ads,-1,0,"datagram received with wrong protocol family" |
| 404 | " %u (expected %u)",udpaddr.sin_family,AF_INET); |
| 405 | continue; |
| 406 | } |
| 407 | if (ntohs(udpaddr.sin_port) != DNS_PORT) { |
| 408 | adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)", |
| 409 | ntohs(udpaddr.sin_port),DNS_PORT); |
| 410 | continue; |
| 411 | } |
| 412 | for (serv= 0; |
| 413 | serv < ads->nservers && |
| 414 | ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr; |
| 415 | serv++); |
| 416 | if (serv >= ads->nservers) { |
| 417 | adns__warn(ads,-1,0,"datagram received from unknown nameserver %s", |
| 418 | inet_ntoa(udpaddr.sin_addr)); |
| 419 | continue; |
| 420 | } |
| 421 | adns__procdgram(ads,udpbuf,r,serv,0,*now); |
| 422 | } |
| 423 | } |
| 424 | r= 0; |
| 425 | xit: |
| 426 | adns__consistency(ads,0,cc_entex); |
| 427 | return r; |
| 428 | } |
| 429 | |
| 430 | int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) { |
| 431 | int r; |
| 432 | |
| 433 | adns__consistency(ads,0,cc_entex); |
| 434 | |
| 435 | switch (ads->tcpstate) { |
| 436 | case server_disconnected: |
| 437 | case server_broken: |
| 438 | break; |
| 439 | case server_connecting: |
| 440 | if (fd != ads->tcpsocket) break; |
| 441 | assert(ads->tcprecv.used==0); |
| 442 | assert(ads->tcprecv_skip==0); |
| 443 | for (;;) { |
| 444 | if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; } |
| 445 | r= read(ads->tcpsocket,&ads->tcprecv.buf,1); |
| 446 | if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) { |
| 447 | tcp_connected(ads,*now); |
| 448 | r= 0; goto xit; |
| 449 | } |
| 450 | if (r>0) { |
| 451 | adns__tcp_broken(ads,"connect/read","sent data before first request"); |
| 452 | r= 0; goto xit; |
| 453 | } |
| 454 | if (errno==EINTR) continue; |
| 455 | if (errno_resources(errno)) { r= errno; goto xit; } |
| 456 | adns__tcp_broken(ads,"connect/read",strerror(errno)); |
| 457 | r= 0; goto xit; |
| 458 | } /* not reached */ |
| 459 | case server_ok: |
| 460 | if (fd != ads->tcpsocket) break; |
| 461 | while (ads->tcpsend.used) { |
| 462 | adns__sigpipe_protect(ads); |
| 463 | r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used); |
| 464 | adns__sigpipe_unprotect(ads); |
| 465 | if (r<0) { |
| 466 | if (errno==EINTR) continue; |
| 467 | if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; } |
| 468 | if (errno_resources(errno)) { r= errno; goto xit; } |
| 469 | adns__tcp_broken(ads,"write",strerror(errno)); |
| 470 | r= 0; goto xit; |
| 471 | } else if (r>0) { |
| 472 | ads->tcpsend.used -= r; |
| 473 | memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used); |
| 474 | } |
| 475 | } |
| 476 | r= 0; |
| 477 | goto xit; |
| 478 | default: |
| 479 | abort(); |
| 480 | } |
| 481 | r= 0; |
| 482 | xit: |
| 483 | adns__consistency(ads,0,cc_entex); |
| 484 | return r; |
| 485 | } |
| 486 | |
| 487 | int adns_processexceptional(adns_state ads, int fd, const struct timeval *now) { |
| 488 | adns__consistency(ads,0,cc_entex); |
| 489 | switch (ads->tcpstate) { |
| 490 | case server_disconnected: |
| 491 | case server_broken: |
| 492 | break; |
| 493 | case server_connecting: |
| 494 | case server_ok: |
| 495 | if (fd != ads->tcpsocket) break; |
| 496 | adns__tcp_broken(ads,"poll/select","exceptional condition detected"); |
| 497 | break; |
| 498 | default: |
| 499 | abort(); |
| 500 | } |
| 501 | adns__consistency(ads,0,cc_entex); |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static void fd_event(adns_state ads, int fd, |
| 506 | int revent, int pollflag, |
| 507 | int maxfd, const fd_set *fds, |
| 508 | int (*func)(adns_state, int fd, const struct timeval *now), |
| 509 | struct timeval now, int *r_r) { |
| 510 | int r; |
| 511 | |
| 512 | if (!(revent & pollflag)) return; |
| 513 | if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return; |
| 514 | r= func(ads,fd,&now); |
| 515 | if (r) { |
| 516 | if (r_r) { |
| 517 | *r_r= r; |
| 518 | } else { |
| 519 | adns__diag(ads,-1,0,"process fd failed after select: %s",strerror(errno)); |
| 520 | adns_globalsystemfailure(ads); |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | void adns__fdevents(adns_state ads, |
| 526 | const struct pollfd *pollfds, int npollfds, |
| 527 | int maxfd, const fd_set *readfds, |
| 528 | const fd_set *writefds, const fd_set *exceptfds, |
| 529 | struct timeval now, int *r_r) { |
| 530 | int i, fd, revents; |
| 531 | |
| 532 | for (i=0; i<npollfds; i++) { |
| 533 | fd= pollfds[i].fd; |
| 534 | if (fd >= maxfd) maxfd= fd+1; |
| 535 | revents= pollfds[i].revents; |
| 536 | fd_event(ads,fd, revents,POLLIN, maxfd,readfds, adns_processreadable,now,r_r); |
| 537 | fd_event(ads,fd, revents,POLLOUT, maxfd,writefds, adns_processwriteable,now,r_r); |
| 538 | fd_event(ads,fd, revents,POLLPRI, maxfd,exceptfds, adns_processexceptional,now,r_r); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /* Wrappers for select(2). */ |
| 543 | |
| 544 | void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io, |
| 545 | fd_set *writefds_io, fd_set *exceptfds_io, |
| 546 | struct timeval **tv_mod, struct timeval *tv_tobuf, |
| 547 | const struct timeval *now) { |
| 548 | struct timeval tv_nowbuf; |
| 549 | struct pollfd pollfds[MAX_POLLFDS]; |
| 550 | int i, fd, maxfd, npollfds; |
| 551 | |
| 552 | adns__consistency(ads,0,cc_entex); |
| 553 | |
| 554 | if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) { |
| 555 | /* The caller is planning to sleep. */ |
| 556 | adns__must_gettimeofday(ads,&now,&tv_nowbuf); |
| 557 | if (!now) { inter_immed(tv_mod,tv_tobuf); goto xit; } |
| 558 | adns__timeouts(ads, 0, tv_mod,tv_tobuf, *now); |
| 559 | } |
| 560 | |
| 561 | npollfds= adns__pollfds(ads,pollfds); |
| 562 | maxfd= *maxfd_io; |
| 563 | for (i=0; i<npollfds; i++) { |
| 564 | fd= pollfds[i].fd; |
| 565 | if (fd >= maxfd) maxfd= fd+1; |
| 566 | if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io); |
| 567 | if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io); |
| 568 | if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io); |
| 569 | } |
| 570 | *maxfd_io= maxfd; |
| 571 | |
| 572 | xit: |
| 573 | adns__consistency(ads,0,cc_entex); |
| 574 | } |
| 575 | |
| 576 | void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds, |
| 577 | const fd_set *writefds, const fd_set *exceptfds, |
| 578 | const struct timeval *now) { |
| 579 | struct timeval tv_buf; |
| 580 | struct pollfd pollfds[MAX_POLLFDS]; |
| 581 | int npollfds, i; |
| 582 | |
| 583 | adns__consistency(ads,0,cc_entex); |
| 584 | adns__must_gettimeofday(ads,&now,&tv_buf); |
| 585 | if (!now) goto xit; |
| 586 | adns_processtimeouts(ads,now); |
| 587 | |
| 588 | npollfds= adns__pollfds(ads,pollfds); |
| 589 | for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI; |
| 590 | adns__fdevents(ads, |
| 591 | pollfds,npollfds, |
| 592 | maxfd,readfds,writefds,exceptfds, |
| 593 | *now, 0); |
| 594 | xit: |
| 595 | adns__consistency(ads,0,cc_entex); |
| 596 | } |
| 597 | |
| 598 | /* General helpful functions. */ |
| 599 | |
| 600 | void adns_globalsystemfailure(adns_state ads) { |
| 601 | adns__consistency(ads,0,cc_entex); |
| 602 | |
| 603 | while (ads->udpw.head) adns__query_fail(ads->udpw.head, adns_s_systemfail); |
| 604 | while (ads->tcpw.head) adns__query_fail(ads->tcpw.head, adns_s_systemfail); |
| 605 | |
| 606 | switch (ads->tcpstate) { |
| 607 | case server_connecting: |
| 608 | case server_ok: |
| 609 | adns__tcp_broken(ads,0,0); |
| 610 | break; |
| 611 | case server_disconnected: |
| 612 | case server_broken: |
| 613 | break; |
| 614 | default: |
| 615 | abort(); |
| 616 | } |
| 617 | adns__consistency(ads,0,cc_entex); |
| 618 | } |
| 619 | |
| 620 | int adns_processany(adns_state ads) { |
| 621 | int r, i; |
| 622 | struct timeval now; |
| 623 | struct pollfd pollfds[MAX_POLLFDS]; |
| 624 | int npollfds; |
| 625 | |
| 626 | adns__consistency(ads,0,cc_entex); |
| 627 | |
| 628 | r= gettimeofday(&now,0); |
| 629 | if (!r) adns_processtimeouts(ads,&now); |
| 630 | |
| 631 | /* We just use adns__fdevents to loop over the fd's trying them. |
| 632 | * This seems more sensible than calling select, since we're most |
| 633 | * likely just to want to do a read on one or two fds anyway. |
| 634 | */ |
| 635 | npollfds= adns__pollfds(ads,pollfds); |
| 636 | for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events & ~POLLPRI; |
| 637 | adns__fdevents(ads, |
| 638 | pollfds,npollfds, |
| 639 | 0,0,0,0, |
| 640 | now,&r); |
| 641 | |
| 642 | adns__consistency(ads,0,cc_entex); |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | void adns__autosys(adns_state ads, struct timeval now) { |
| 647 | if (ads->iflags & adns_if_noautosys) return; |
| 648 | adns_processany(ads); |
| 649 | } |
| 650 | |
| 651 | int adns__internal_check(adns_state ads, |
| 652 | adns_query *query_io, |
| 653 | adns_answer **answer, |
| 654 | void **context_r) { |
| 655 | adns_query qu; |
| 656 | |
| 657 | qu= *query_io; |
| 658 | if (!qu) { |
| 659 | if (ads->output.head) { |
| 660 | qu= ads->output.head; |
| 661 | } else if (ads->udpw.head || ads->tcpw.head) { |
| 662 | return EAGAIN; |
| 663 | } else { |
| 664 | return ESRCH; |
| 665 | } |
| 666 | } else { |
| 667 | if (qu->id>=0) return EAGAIN; |
| 668 | } |
| 669 | LIST_UNLINK(ads->output,qu); |
| 670 | *answer= qu->answer; |
| 671 | if (context_r) *context_r= qu->ctx.ext; |
| 672 | *query_io= qu; |
| 673 | free(qu); |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | int adns_wait(adns_state ads, |
| 678 | adns_query *query_io, |
| 679 | adns_answer **answer_r, |
| 680 | void **context_r) { |
| 681 | int r, maxfd, rsel; |
| 682 | fd_set readfds, writefds, exceptfds; |
| 683 | struct timeval tvbuf, *tvp; |
| 684 | |
| 685 | adns__consistency(ads,*query_io,cc_entex); |
| 686 | for (;;) { |
| 687 | r= adns__internal_check(ads,query_io,answer_r,context_r); |
| 688 | if (r != EAGAIN) break; |
| 689 | maxfd= 0; tvp= 0; |
| 690 | FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); |
| 691 | adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0); |
| 692 | assert(tvp); |
| 693 | rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp); |
| 694 | if (rsel==-1) { |
| 695 | if (errno == EINTR) { |
| 696 | if (ads->iflags & adns_if_eintr) { r= EINTR; break; } |
| 697 | } else { |
| 698 | adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno)); |
| 699 | adns_globalsystemfailure(ads); |
| 700 | } |
| 701 | } else { |
| 702 | assert(rsel >= 0); |
| 703 | adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0); |
| 704 | } |
| 705 | } |
| 706 | adns__consistency(ads,0,cc_entex); |
| 707 | return r; |
| 708 | } |
| 709 | |
| 710 | int adns_check(adns_state ads, |
| 711 | adns_query *query_io, |
| 712 | adns_answer **answer_r, |
| 713 | void **context_r) { |
| 714 | struct timeval now; |
| 715 | int r; |
| 716 | |
| 717 | adns__consistency(ads,*query_io,cc_entex); |
| 718 | r= gettimeofday(&now,0); |
| 719 | if (!r) adns__autosys(ads,now); |
| 720 | |
| 721 | r= adns__internal_check(ads,query_io,answer_r,context_r); |
| 722 | adns__consistency(ads,0,cc_entex); |
| 723 | return r; |
| 724 | } |