Skip to content

Commit b7c30a0

Browse files
committed
Handle more than __FD_SETSIZE file descriptors. Fixes #1.
1 parent edeb168 commit b7c30a0

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

rinetd.c

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ FILE *logFile = 0;
156156

157157
static int const bufferSpace = 1024;
158158

159-
void readConfiguration();
159+
static void selectPass(void);
160+
static void readConfiguration(void);
160161

161162
/* Signal handlers */
162163
RETSIGTYPE plumber(int s);
@@ -166,8 +167,6 @@ RETSIGTYPE term(int s);
166167
void allocConnections(int count);
167168
void RegisterPID(void);
168169

169-
void selectLoop(void);
170-
171170
void logEvent(ConnectionInfo const *cnx, int i, int result);
172171

173172
int getAddress(char *host, struct in_addr *iaddr);
@@ -268,7 +267,9 @@ int main(int argc, char *argv[])
268267
readConfiguration();
269268
RegisterPID();
270269
syslog(LOG_INFO, "Starting redirections...");
271-
selectLoop();
270+
while (1) {
271+
selectPass();
272+
}
272273
#ifndef WIN32
273274
#ifndef DEBUG
274275
} else {
@@ -283,7 +284,7 @@ int getConfLine(FILE *in, char *line, int space, int *lnum);
283284

284285
int patternBad(char *pattern);
285286

286-
void readConfiguration(void)
287+
static void readConfiguration(void)
287288
{
288289
FILE *in;
289290
char line[16384];
@@ -669,14 +670,6 @@ void allocConnections(int count)
669670
coTotal += count;
670671
}
671672

672-
void selectPass(void);
673-
674-
void selectLoop(void) {
675-
while (1) {
676-
selectPass();
677-
}
678-
}
679-
680673
void handleRemoteWrite(ConnectionInfo *cnx);
681674
void handleRemoteRead(ConnectionInfo *cnx);
682675
void handleLocalWrite(ConnectionInfo *cnx);
@@ -687,14 +680,20 @@ void handleAccept(int i);
687680
void openLocalFd(int se, ConnectionInfo *cnx);
688681
int getAddress(char *host, struct in_addr *iaddr);
689682

690-
void selectPass(void) {
691-
fd_set readfds, writefds;
692-
FD_ZERO(&readfds);
693-
FD_ZERO(&writefds);
683+
static void selectPass(void) {
684+
685+
int const fdSetCount = maxfd / __FD_SETSIZE + 1;
686+
# define FD_ZERO_EXT(ar) for (int i = 0; i < fdSetCount; ++i) { FD_ZERO(&(ar)[i]); }
687+
# define FD_SET_EXT(fd, ar) FD_SET((fd) % __FD_SETSIZE, &(ar)[(fd) / __FD_SETSIZE])
688+
# define FD_ISSET_EXT(fd, ar) FD_ISSET((fd) % __FD_SETSIZE, &(ar)[(fd) / __FD_SETSIZE])
689+
690+
fd_set readfds[fdSetCount], writefds[fdSetCount];
691+
FD_ZERO_EXT(readfds);
692+
FD_ZERO_EXT(writefds);
694693
/* Server sockets */
695694
for (int i = 0; i < seTotal; ++i) {
696695
if (seInfo[i].fd != INVALID_SOCKET) {
697-
FD_SET(seInfo[i].fd, &readfds);
696+
FD_SET_EXT(seInfo[i].fd, readfds);
698697
}
699698
}
700699
/* Connection sockets */
@@ -705,35 +704,35 @@ void selectPass(void) {
705704
}
706705
if (cnx->coClosing) {
707706
if (!cnx->reClosed) {
708-
FD_SET(cnx->reFd, &writefds);
707+
FD_SET_EXT(cnx->reFd, writefds);
709708
}
710709
if (!cnx->loClosed) {
711-
FD_SET(cnx->loFd, &writefds);
710+
FD_SET_EXT(cnx->loFd, writefds);
712711
}
713712
}
714713
/* Get more input if we have room for it */
715714
if ((!cnx->reClosed) && (cnx->inputRPos < bufferSpace)) {
716-
FD_SET(cnx->reFd, &readfds);
715+
FD_SET_EXT(cnx->reFd, readfds);
717716
}
718717
/* Send more output if we have any */
719718
if ((!cnx->reClosed) && (cnx->outputWPos < cnx->outputRPos)) {
720-
FD_SET(cnx->reFd, &writefds);
719+
FD_SET_EXT(cnx->reFd, writefds);
721720
}
722721
/* Accept more output from the local
723722
server if there's room */
724723
if ((!cnx->loClosed) && (cnx->outputRPos < bufferSpace)) {
725-
FD_SET(cnx->loFd, &readfds);
724+
FD_SET_EXT(cnx->loFd, readfds);
726725
}
727726
/* Send more input to the local server
728727
if we have any */
729728
if ((!cnx->loClosed) && (cnx->inputWPos < cnx->inputRPos)) {
730-
FD_SET(cnx->loFd, &writefds);
729+
FD_SET_EXT(cnx->loFd, writefds);
731730
}
732731
}
733-
select(maxfd + 1, &readfds, &writefds, 0, 0);
732+
select(maxfd + 1, readfds, writefds, 0, 0);
734733
for (int i = 0; i < seTotal; ++i) {
735734
if (seInfo[i].fd != -1) {
736-
if (FD_ISSET(seInfo[i].fd, &readfds)) {
735+
if (FD_ISSET_EXT(seInfo[i].fd, readfds)) {
737736
handleAccept(i);
738737
}
739738
}
@@ -744,22 +743,22 @@ void selectPass(void) {
744743
continue;
745744
}
746745
if (!cnx->reClosed) {
747-
if (FD_ISSET(cnx->reFd, &readfds)) {
746+
if (FD_ISSET_EXT(cnx->reFd, readfds)) {
748747
handleRemoteRead(cnx);
749748
}
750749
}
751750
if (!cnx->reClosed) {
752-
if (FD_ISSET(cnx->reFd, &writefds)) {
751+
if (FD_ISSET_EXT(cnx->reFd, writefds)) {
753752
handleRemoteWrite(cnx);
754753
}
755754
}
756755
if (!cnx->loClosed) {
757-
if (FD_ISSET(cnx->loFd, &readfds)) {
756+
if (FD_ISSET_EXT(cnx->loFd, readfds)) {
758757
handleLocalRead(cnx);
759758
}
760759
}
761760
if (!cnx->loClosed) {
762-
if (FD_ISSET(cnx->loFd, &writefds)) {
761+
if (FD_ISSET_EXT(cnx->loFd, writefds)) {
763762
handleLocalWrite(cnx);
764763
}
765764
}

0 commit comments

Comments
 (0)