- else if (CPTR_KILLED == client_dopacket(cptr, dolen))
- return CPTR_KILLED;
- }
- }
- return 1;
-}
-
-/*
- * Check all connections for new connections and input data that is to be
- * processed. Also check for connections with data queued and whether we can
- * write it out.
- *
- * Don't ever use ZERO for `delay', unless you mean to poll and then
- * you have to have sleep/wait somewhere else in the code.--msa
- */
-int read_message(time_t delay)
-{
- Reg1 aClient *cptr;
- Reg2 int nfds;
- struct timeval wait;
-#ifdef pyr
- struct timeval nowt;
- unsigned long us;
-#endif
- time_t delay2 = delay;
- unsigned long usec = 0;
- int res, length, fd, i;
- int auth = 0, ping = 0;
-#ifndef USE_POLL
- fd_set read_set, write_set;
-#else /* USE_POLL */
- unsigned int currfd_index = 0;
- unsigned int udpfdindex = 0;
- unsigned int resfdindex = 0;
- unsigned long timeout;
- int added;
-#endif /* USE_POLL */
-
-#ifdef pyr
- gettimeofday(&nowt, NULL);
- now = nowt.tv_sec;
-#endif
-
- for (res = 0;;)
- {
-#ifndef USE_POLL
- FD_ZERO(&read_set);
- FD_ZERO(&write_set);
-#endif /* not USE_POLL */
- for (i = highest_fd; i >= 0; i--)
- {
-#ifdef USE_POLL
- added = FALSE;
-#endif /* USE_POLL */
- if (!(cptr = loc_clients[i]))
- continue;
- if (IsLog(cptr))
- continue;
- if (DoingAuth(cptr))
- {
- auth++;
- Debug((DEBUG_NOTICE, "auth on %p %d", cptr, i));
- RFD_SET(cptr->authfd, &read_set, currfd_index, cptr);
- if (cptr->flags & FLAGS_WRAUTH)
- RWFD_SET(cptr->authfd, &write_set, currfd_index);
- }
- if (IsPing(cptr))
- {
- ping++;
- Debug((DEBUG_NOTICE, "open ping on %p %d", cptr, i));
- if (!cptr->firsttime || now <= cptr->firsttime)
- {
- RFD_SET(i, &read_set, currfd_index, cptr);
- delay2 = 1;
- if (DoPing(cptr) && now > cptr->lasttime)
- RWFD_SET(i, &write_set, currfd_index);
- }
- else
- {
- del_queries((char *)cptr);
- end_ping(cptr);
- }
-#ifdef USE_POLL
- if (added)
- currfd_index++;
-#endif /* USE_POLL */
- continue;
- }
- if (DoingDNS(cptr) || DoingAuth(cptr))
- {
-#ifdef USE_POLL
- if (added)
- currfd_index++;
-#endif /* USE_POLL */
- continue;
- }
- if (IsMe(cptr) && IsListening(cptr))
- RFD_SET(i, &read_set, currfd_index, cptr);
- else if (!IsMe(cptr))
- {
- if (DBufLength(&cptr->recvQ) && delay2 > 2)
- delay2 = 1;
- if (DBufLength(&cptr->recvQ) < 4088)
- RFD_SET(i, &read_set, currfd_index, cptr);
- if (DBufLength(&cptr->sendQ) || IsConnecting(cptr) ||
- (cptr->listing && DBufLength(&cptr->sendQ) < 2048))
-#ifndef pyr
- WFD_SET(i, &write_set, currfd_index, cptr);
-#else /* pyr */
- {
- if (!(cptr->flags & FLAGS_BLOCKED))
- WFD_SET(i, &write_set, currfd_index, cptr);
- else
- delay2 = 0, usec = 500000;
- }
- if (now - cptr->lw.tv_sec && nowt.tv_usec - cptr->lw.tv_usec < 0)
- us = 1000000;
- else
- us = 0;
- us += nowt.tv_usec;
- if (us - cptr->lw.tv_usec > 500000)
- cptr->flags &= ~FLAGS_BLOCKED;
-#endif /* pyr */
- }
-#ifdef USE_POLL
- if (added)
- currfd_index++;
-#endif /* USE_POLL */
- }
-
- if (udpfd >= 0)
- {
- RFD_SET(udpfd, &read_set, currfd_index, NULL);
-#ifdef USE_POLL
- udpfdindex = currfd_index;
- currfd_index++;
-#endif /* USE_POLL */
- }
- if (resfd >= 0)
- {
- RFD_SET(resfd, &read_set, currfd_index, NULL);
-#ifdef USE_POLL
- resfdindex = currfd_index;
- currfd_index++;
-#endif /* USE_POLL */
- }
-
- wait.tv_sec = MIN(delay2, delay);
- wait.tv_usec = usec;
-#ifndef USE_POLL
-#ifdef HPUX
- nfds = select(FD_SETSIZE, (int *)&read_set, (int *)&write_set, 0, &wait);
-#else
- nfds = select(FD_SETSIZE, &read_set, &write_set, 0, &wait);
-#endif
-#else /* USE_POLL */
- timeout = (wait.tv_sec * 1000) + (wait.tv_usec / 1000);
- nfds = poll(poll_fds, currfd_index, timeout);
-#endif /* USE_POLL */
- now = time(NULL);
- if (nfds == -1 && errno == EINTR)
- return -1;
- else if (nfds >= 0)
- break;
- report_error("select %s: %s", &me);
- res++;
- if (res > 5)
- restart("too many select errors");
- sleep(10);
- now += 10;
- }
-
- if (udpfd >= 0 && RFD_ISSET(udpfd, &read_set, udpfdindex))
- {
- polludp();
- nfds--;
- RFD_CLR_OUT(udpfd, &read_set, udpfdindex);
- }
- /*
- * Check fd sets for the ping fd's (if set and valid!) first
- * because these can not be processed using the normal loops below.
- * And we want them to be as fast as possible.
- * -Run
- */
- for (i = HIGHEST_INDEX; (ping > 0) && (i >= 0); i--)
- {
- if (!(cptr = LOC_CLIENTS(i)))
- continue;
- if (!IsPing(cptr))
- continue;
- ping--;
- if ((nfds > 0) && RFD_ISSET(cptr->fd, &read_set, i))
- {
- nfds--;
- RFD_CLR_OUT(cptr->fd, &read_set, i);
- read_ping(cptr); /* This can RunFree(cptr) ! */
- }
- else if ((nfds > 0) && WFD_ISSET(cptr->fd, &write_set, i))
- {
- nfds--;
- cptr->lasttime = now;
- WFD_CLR_OUT(cptr->fd, &write_set, i);
- send_ping(cptr); /* This can RunFree(cptr) ! */
- }
- }
- if (resfd >= 0 && RFD_ISSET(resfd, &read_set, resfdindex))
- {
- do_dns_async();
- nfds--;
- RFD_CLR_OUT(resfd, &read_set, resfdindex);
- }
- /*
- * Check fd sets for the auth fd's (if set and valid!) first
- * because these can not be processed using the normal loops below.
- * -avalon
- */
- for (i = HIGHEST_INDEX; (auth > 0) && (i >= 0); i--)
- {
- if (!(cptr = LOC_CLIENTS(i)))
- continue;
- if (cptr->authfd < 0)
- continue;
- auth--;
- if ((nfds > 0) && WFD_ISSET(cptr->authfd, &write_set, i))
- {
- nfds--;
- send_authports(cptr);
- }
- else if ((nfds > 0) && RFD_ISSET(cptr->authfd, &read_set, i))
- {
- nfds--;
- read_authports(cptr);
- }
- }
- for (i = HIGHEST_INDEX; i >= 0; i--)
- if ((cptr = LOC_CLIENTS(i)) && RFD_ISSET(i, &read_set, i) &&
- IsListening(cptr))
- {
- RFD_CLR_OUT(i, &read_set, i);
- nfds--;
- cptr->lasttime = now;