aboutsummaryrefslogtreecommitdiff
path: root/tests/nt.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/nt.c')
-rw-r--r--tests/nt.c62
1 files changed, 36 insertions, 26 deletions
diff --git a/tests/nt.c b/tests/nt.c
index 7501586..d30e447 100644
--- a/tests/nt.c
+++ b/tests/nt.c
@@ -107,29 +107,49 @@ stdin_reader (same as net_reader)
107net_writer (peer -> stdin_reader) same as stdout_writer 107net_writer (peer -> stdin_reader) same as stdout_writer
108*/ 108*/
109 109
110static inline int
111peer_is_state (netcat_server_t *srv, int state)
112{
113 return srv->peer && (srv->peer->state & state);
114}
115
116static void
117netcat_stream_disconnect (netcat_server_t *srv, int mask)
118{
119 srv->disconnect (srv);
120 srv->state &= ~mask;
121 srv->pollfd->events &= ~mask;
122 if (srv->pollfd->events == 0)
123 srv->pollfd->fd = -1;
124}
125
110ssize_t 126ssize_t
111netcat_stream_read (netcat_server_t *srv) 127netcat_stream_read (netcat_server_t *srv)
112{ 128{
113 ssize_t n; 129 ssize_t n;
114 130
115 if (iobuf_avail_size (&srv->buf[IN])) 131 if (iobuf_avail_size (&srv->buf[IN]))
116 { 132 {
117 n = iobuf_fill (&srv->buf[IN], srv->pollfd->fd); 133 n = iobuf_fill (&srv->buf[IN], srv->pollfd->fd);
118 if (n == -1) 134 if (n == -1)
119 { 135 {
136 if (errno == EINTR)
137 return 0;
120 grecs_error (NULL, errno, "%s: read", srv->id); 138 grecs_error (NULL, errno, "%s: read", srv->id);
121 srv->pollfd->events &= ~POLLIN; 139 srv->pollfd->events &= ~POLLIN;
122 return -1; 140 return -1;
123 } 141 }
124 if (n == 0) 142 if (n == 0 || !peer_is_state (srv, POLLOUT))
125 { 143 {
126 /* No more input is expected */ 144 /* No more input is expected || needed */
127 srv->disconnect (srv); 145 netcat_stream_disconnect (srv, POLLIN);
128 srv->state &= ~POLLIN; 146
129 srv->pollfd->events &= ~POLLIN; 147 if (srv->peer)
130 if (srv->pollfd->events == 0) 148 netcat_stream_disconnect (srv->peer, POLLOUT);
131 srv->pollfd->fd = -1; 149 return 0;
132 } 150 }
151 else
152 srv->peer->pollfd->events |= POLLOUT;
133 } 153 }
134 else 154 else
135 { 155 {
@@ -139,12 +159,6 @@ netcat_stream_read (netcat_server_t *srv)
139 return n; 159 return n;
140} 160}
141 161
142static inline int
143peer_is_state (netcat_server_t *srv, int state)
144{
145 return srv->peer && (srv->peer->state & state);
146}
147
148ssize_t 162ssize_t
149netcat_stream_write (netcat_server_t *srv) 163netcat_stream_write (netcat_server_t *srv)
150{ 164{
@@ -155,22 +169,21 @@ netcat_stream_write (netcat_server_t *srv)
155 if (!peer_is_state (srv, POLLIN)) 169 if (!peer_is_state (srv, POLLIN))
156 { 170 {
157 // shutdown write end 171 // shutdown write end
158 srv->disconnect (srv); 172 netcat_stream_disconnect (srv, POLLOUT);
159 srv->state &= ~POLLOUT;
160 srv->pollfd->events &= ~POLLOUT;
161 if (srv->pollfd->events == 0)
162 srv->pollfd->fd = -1;
163 return -1; 173 return -1;
164 } 174 }
165 if (iobuf_copy (&srv->buf[OUT], &srv->peer->buf[IN]) == 0) 175 if (iobuf_copy (&srv->buf[OUT], &srv->peer->buf[IN]) == 0)
166 { 176 {
167 srv->peer->pollfd->events |= POLLIN; 177 srv->peer->pollfd->events |= POLLIN;
178 srv->pollfd->events &= ~POLLOUT;
168 return 0; 179 return 0;
169 } 180 }
170 } 181 }
171 n = iobuf_flush (&srv->buf[OUT], srv->pollfd->fd); 182 n = iobuf_flush (&srv->buf[OUT], srv->pollfd->fd);
172 if (n == -1) 183 if (n == -1)
173 { 184 {
185 if (errno == EINTR)
186 return 0;
174 grecs_error (NULL, errno, "%s: write", srv->id); 187 grecs_error (NULL, errno, "%s: write", srv->id);
175 return -1; 188 return -1;
176 } 189 }
@@ -253,19 +266,16 @@ netcat (char const *urlstr)
253 { 266 {
254 netcat_server_t *next = srv->next; 267 netcat_server_t *next = srv->next;
255 int events; 268 int events;
256 269
257 events = (srv->pollfd->events|POLLHUP) 270 events = (srv->pollfd->events|POLLHUP)
258 & (srv->pollfd->revents & (srv->state|POLLHUP)); 271 & (srv->pollfd->revents
272 & (srv->state | ((srv->state & POLLOUT) ? POLLHUP : 0)));
259 if (events) 273 if (events)
260 { 274 {
261 if (events & POLLHUP) 275 if (events & POLLHUP)
262 { 276 {
263 //grecs_error (NULL, 0, "HUP on %s", srv->id); 277 //grecs_error (NULL, 0, "HUP on %s", srv->id);
264 srv->disconnect (srv); 278 netcat_stream_disconnect (srv, srv->state);
265 srv->pollfd->events &= ~srv->state;
266 if (srv->pollfd->events == 0)
267 srv->pollfd->fd = -1;
268 srv->state = 0;
269 } 279 }
270 else if (events & POLLIN) 280 else if (events & POLLIN)
271 netcat_stream_read (srv); 281 netcat_stream_read (srv);

Return to:

Send suggestions and report system problems to the System administrator.