Removed
Link Here
|
1 |
--- rinetd.c.orig Mon Apr 14 22:19:23 2003 |
2 |
+++ rinetd.c Tue Oct 4 07:25:42 2005 |
3 |
@@ -12,6 +12,7 @@ |
4 |
#include <netinet/in.h> |
5 |
#include <getopt.h> |
6 |
#include <errno.h> |
7 |
+#include <poll.h> |
8 |
#define INVALID_SOCKET (-1) |
9 |
#include <sys/time.h> |
10 |
#endif /* WIN32 */ |
11 |
@@ -94,6 +95,7 @@ |
12 |
#include "match.h" |
13 |
|
14 |
SOCKET *seFds = 0; |
15 |
+static int first_set = 0; |
16 |
/* In network order, for network purposes */ |
17 |
struct in_addr *seLocalAddrs = 0; |
18 |
unsigned short *seLocalPorts = 0; |
19 |
@@ -750,15 +752,82 @@ |
20 |
void openLocalFd(int se, int i); |
21 |
int getAddress(char *host, struct in_addr *iaddr); |
22 |
|
23 |
+inline void poll_init_fds(struct pollfd *pfds, int size) { |
24 |
+ int i; |
25 |
+ |
26 |
+ memset(pfds, 0, sizeof(struct pollfd) * size); |
27 |
+ first_set = 1; |
28 |
+ for(i = 0; i < size; i++) |
29 |
+ pfds[i].fd = -1; |
30 |
+} |
31 |
+ |
32 |
+inline int poll_set_fd(struct pollfd *pfds, int size, int count, |
33 |
+ int fd, short int ev) { |
34 |
+#ifdef _NEW_POLL_SET_FD |
35 |
+ if(first_set) { |
36 |
+ pfds[count].fd = fd; |
37 |
+ pfds[count].events |= ev; |
38 |
+ first_set = 0; |
39 |
+ return 0; |
40 |
+ } |
41 |
+ if(pfds[count].fd != fd && !first_set) { |
42 |
+ count++; |
43 |
+ } |
44 |
+ |
45 |
+ pfds[count].fd = fd; |
46 |
+ pfds[count].events |= ev; |
47 |
+ |
48 |
+ return count; |
49 |
+#else |
50 |
+ int i; |
51 |
+ |
52 |
+ for(i = 0; i < size; i++) { |
53 |
+ if(pfds[i].fd == -1) { |
54 |
+ pfds[i].fd = fd; |
55 |
+ pfds[i].events |= ev; |
56 |
+ count++; |
57 |
+ break; |
58 |
+ } |
59 |
+ if(pfds[i].fd == fd) { |
60 |
+ pfds[i].events |= ev; |
61 |
+ break; |
62 |
+ } |
63 |
+ } |
64 |
+ |
65 |
+ return count; |
66 |
+#endif |
67 |
+} |
68 |
+ |
69 |
+int poll_fd_isset(struct pollfd *pfds, int nfds, int fd, short event) { |
70 |
+ int i; |
71 |
+ |
72 |
+ for(i = 0; i < nfds; i++) { |
73 |
+ if(pfds[i].fd == fd) |
74 |
+ return pfds[i].revents & event; |
75 |
+ } |
76 |
+ |
77 |
+ return 0; |
78 |
+} |
79 |
+ |
80 |
void selectPass(void) { |
81 |
int i; |
82 |
- fd_set readfds, writefds; |
83 |
- FD_ZERO(&readfds); |
84 |
- FD_ZERO(&writefds); |
85 |
+ int nfds = 0; |
86 |
+ int total = 0; |
87 |
+ static struct pollfd *pfds = NULL; |
88 |
+ |
89 |
/* Server sockets */ |
90 |
+ total = seTotal + (coTotal * 2); |
91 |
+ |
92 |
+ if(!pfds) { |
93 |
+ pfds = malloc(sizeof(struct pollfd) * total); |
94 |
+ } |
95 |
+ |
96 |
+ poll_init_fds(pfds, total); |
97 |
+ |
98 |
for (i = 0; (i < seTotal); i++) { |
99 |
if (seFds[i] != INVALID_SOCKET) { |
100 |
- FD_SET(seFds[i], &readfds); |
101 |
+ //FD_SET(seFds[i], &readfds) |
102 |
+ nfds = poll_set_fd(pfds, total, nfds, seFds[i], POLLIN); |
103 |
} |
104 |
} |
105 |
/* Connection sockets */ |
106 |
@@ -768,35 +837,47 @@ |
107 |
} |
108 |
if (coClosing[i]) { |
109 |
if (!reClosed[i]) { |
110 |
- FD_SET(reFds[i], &writefds); |
111 |
- } |
112 |
- if (!loClosed[i]) { |
113 |
- FD_SET(loFds[i], &writefds); |
114 |
+ //FD_SET(reFds[i], &writefds); |
115 |
+ nfds = poll_set_fd(pfds, total, nfds, |
116 |
+ reFds[i], POLLOUT); |
117 |
} |
118 |
} |
119 |
/* Get more input if we have room for it */ |
120 |
if ((!reClosed[i]) && (coInputRPos[i] < bufferSpace)) { |
121 |
- FD_SET(reFds[i], &readfds); |
122 |
+ //FD_SET(reFds[i], &readfds); |
123 |
+ nfds = poll_set_fd(pfds, total, nfds, reFds[i], POLLIN); |
124 |
} |
125 |
/* Send more output if we have any */ |
126 |
if ((!reClosed[i]) && (coOutputWPos[i] < coOutputRPos[i])) { |
127 |
- FD_SET(reFds[i], &writefds); |
128 |
+ //FD_SET(reFds[i], &writefds); |
129 |
+ nfds = poll_set_fd(pfds, total, nfds, reFds[i], POLLOUT); |
130 |
} |
131 |
+ if (coClosing[i]) { |
132 |
+ if (!loClosed[i]) { |
133 |
+ //FD_SET(loFds[i], &writefds); |
134 |
+ nfds = poll_set_fd(pfds, total, nfds, |
135 |
+ loFds[i], POLLOUT); |
136 |
+ } |
137 |
+ } |
138 |
/* Accept more output from the local |
139 |
server if there's room */ |
140 |
if ((!loClosed[i]) && (coOutputRPos[i] < bufferSpace)) { |
141 |
- FD_SET(loFds[i], &readfds); |
142 |
+ //FD_SET(loFds[i], &readfds); |
143 |
+ nfds = poll_set_fd(pfds, total, nfds, loFds[i], POLLIN); |
144 |
} |
145 |
/* Send more input to the local server |
146 |
if we have any */ |
147 |
if ((!loClosed[i]) && (coInputWPos[i] < coInputRPos[i])) { |
148 |
- FD_SET(loFds[i], &writefds); |
149 |
+ //FD_SET(loFds[i], &writefds); |
150 |
+ nfds = poll_set_fd(pfds, total, nfds, loFds[i], POLLOUT); |
151 |
} |
152 |
} |
153 |
- select(maxfd + 1, &readfds, &writefds, 0, 0); |
154 |
+ //select(maxfd + 1, &readfds, &writefds, 0, 0); |
155 |
+ poll(pfds, nfds + 1, -1); |
156 |
for (i = 0; (i < seTotal); i++) { |
157 |
if (seFds[i] != -1) { |
158 |
- if (FD_ISSET(seFds[i], &readfds)) { |
159 |
+ //if (FD_ISSET(seFds[i], &readfds)) { |
160 |
+ if (poll_fd_isset(pfds, nfds, seFds[i], POLLIN)) { |
161 |
handleAccept(i); |
162 |
} |
163 |
} |
164 |
@@ -806,22 +887,26 @@ |
165 |
continue; |
166 |
} |
167 |
if (!reClosed[i]) { |
168 |
- if (FD_ISSET(reFds[i], &readfds)) { |
169 |
+ //if (FD_ISSET(reFds[i], &readfds)) { |
170 |
+ if (poll_fd_isset(pfds, nfds, reFds[i], POLLIN)) { |
171 |
handleRemoteRead(i); |
172 |
} |
173 |
} |
174 |
if (!reClosed[i]) { |
175 |
- if (FD_ISSET(reFds[i], &writefds)) { |
176 |
+ //if (FD_ISSET(reFds[i], &writefds)) { |
177 |
+ if (poll_fd_isset(pfds, nfds, reFds[i], POLLOUT)) { |
178 |
handleRemoteWrite(i); |
179 |
} |
180 |
} |
181 |
if (!loClosed[i]) { |
182 |
- if (FD_ISSET(loFds[i], &readfds)) { |
183 |
+ //if (FD_ISSET(loFds[i], &readfds)) { |
184 |
+ if (poll_fd_isset(pfds, nfds, loFds[i], POLLIN)) { |
185 |
handleLocalRead(i); |
186 |
} |
187 |
} |
188 |
if (!loClosed[i]) { |
189 |
- if (FD_ISSET(loFds[i], &writefds)) { |
190 |
+ //if (FD_ISSET(loFds[i], &writefds)) { |
191 |
+ if (poll_fd_isset(pfds, nfds, loFds[i], POLLOUT)) { |
192 |
handleLocalWrite(i); |
193 |
} |
194 |
} |