summaryrefslogtreecommitdiff
path: root/opentracker.c
diff options
context:
space:
mode:
Diffstat (limited to 'opentracker.c')
-rw-r--r--opentracker.c40
1 files changed, 12 insertions, 28 deletions
diff --git a/opentracker.c b/opentracker.c
index 0af0d17..60bdceb 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -23,7 +23,6 @@
23 23
24/* Libowfat */ 24/* Libowfat */
25#include "io.h" 25#include "io.h"
26#include "iob.h"
27 26
28/* Opentracker */ 27/* Opentracker */
29#include "ot_accesslist.h" 28#include "ot_accesslist.h"
@@ -36,6 +35,7 @@
36#include "ot_random.h" 35#include "ot_random.h"
37#include "ot_scan.h" 36#include "ot_scan.h"
38#include "ot_socket.h" 37#include "ot_socket.h"
38#include "ot_timeout.h"
39#include "ot_stats.h" 39#include "ot_stats.h"
40#include "ot_udp.h" 40#include "ot_udp.h"
41#include "trackerlogic.h" 41#include "trackerlogic.h"
@@ -120,9 +120,8 @@ static ssize_t header_complete(char *request, ssize_t byte_count) {
120static void handle_dead(const int64_t sock) { 120static void handle_dead(const int64_t sock) {
121 struct http_data *cookie = io_getcookie(sock); 121 struct http_data *cookie = io_getcookie(sock);
122 if (cookie) { 122 if (cookie) {
123 size_t i; 123 if (cookie->batch)
124 for (i = 0; i < cookie->batches; ++i) 124 batch_reset(cookie->batch);
125 iob_reset(cookie->batch + i);
126 free(cookie->batch); 125 free(cookie->batch);
127 buf_reset(&cookie->request); 126 buf_reset(&cookie->request);
128 if (cookie->flag & (STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER)) 127 if (cookie->flag & (STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER))
@@ -178,33 +177,23 @@ static void handle_read(const int64_t sock, struct ot_workstruct *ws) {
178 177
179static void handle_write(const int64_t sock) { 178static void handle_write(const int64_t sock) {
180 struct http_data *cookie = io_getcookie(sock); 179 struct http_data *cookie = io_getcookie(sock);
181 size_t i;
182 int chunked = 0; 180 int chunked = 0;
183 181
184 /* Look for the first io_batch still containing bytes to write */
185 if (cookie) { 182 if (cookie) {
186 if (cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER) 183 if (cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER)
187 chunked = 1; 184 chunked = 1;
188 185
189 for (i = 0; i < cookie->batches; ++i) { 186 if (cookie->batch) {
190 if (cookie->batch[i].bytesleft) { 187 if (batch_send(sock, cookie->batch) == BATCH_FAILED) {
191 int64_t res = iob_send(sock, cookie->batch + i); 188 handle_dead(sock);
192 189 return;
193 if (res == -3) {
194 handle_dead(sock);
195 return;
196 }
197
198 if (!cookie->batch[i].bytesleft)
199 continue;
200
201 if (res == -1 || res > 0 || i < cookie->batches - 1)
202 return;
203 } 190 }
191 if (batch_pending(cookie->batch))
192 return;
204 } 193 }
205 } 194 }
206 195
207 /* In a chunked transfer after all batches accumulated have been sent, wait for the next one */ 196 /* In a chunked transfer, wait for the next piece once everything queued has gone out */
208 if (chunked) 197 if (chunked)
209 io_dontwantwrite(sock); 198 io_dontwantwrite(sock);
210 else 199 else
@@ -216,7 +205,6 @@ static void handle_accept(const int64_t serversocket) {
216 int64_t sock; 205 int64_t sock;
217 ot_ip6 ip; 206 ot_ip6 ip;
218 uint16_t port; 207 uint16_t port;
219 tai6464 t;
220 208
221 while ((sock = socket_accept(serversocket, ip, &port)) != -1) { 209 while ((sock = socket_accept(serversocket, ip, &port)) != -1) {
222 210
@@ -235,11 +223,7 @@ static void handle_accept(const int64_t serversocket) {
235 223
236 stats_issue_event(EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip); 224 stats_issue_event(EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip);
237 225
238 /* That breaks taia encapsulation. But there is no way to take system 226 timeout_in(sock, OT_CLIENT_TIMEOUT);
239 time this often in FreeBSD and libowfat does not allow to set unix time */
240 taia_uint(&t, 0); /* Clear t */
241 tai_unix(&(t.sec), (g_now_seconds + OT_CLIENT_TIMEOUT));
242 io_timeout(sock, t);
243 } 227 }
244 io_eagain(serversocket); 228 io_eagain(serversocket);
245} 229}
@@ -305,7 +289,7 @@ static void listen_bind_one(ot_listen *entry) {
305 289
306#ifdef _DEBUG 290#ifdef _DEBUG
307 { 291 {
308 char *protos[] = {"TCP", "UDP", "UDP mcast"}; 292 const char *protos[] = {"TCP", "UDP", "UDP mcast"};
309 char _debug[512]; 293 char _debug[512];
310 int off = snprintf(_debug, sizeof(_debug), "Binding socket type %s to address [", protos[entry->proto]); 294 int off = snprintf(_debug, sizeof(_debug), "Binding socket type %s to address [", protos[entry->proto]);
311 off += ip_format(_debug + off, entry->ip); 295 off += ip_format(_debug + off, entry->ip);