File: conn.c

package info (click to toggle)
innduct 2.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 664 kB
  • sloc: sh: 4,270; ansic: 3,114; perl: 130; makefile: 33
file content (446 lines) | stat: -rw-r--r-- 12,363 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
 *  innduct
 *  tailing reliable realtime streaming feeder for inn
 *  conn.c - connection establishment and teardown
 *
 *  Copyright Ian Jackson <ijackson@chiark.greenend.org.uk>
 *  and contributors; see LICENCE.txt.
 *  SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "innduct.h"

/*========== management of connections ==========*/

static void reconnect_blocking_event(void) {
  until_connect= reconnect_delay_periods;
}

void conn_closefd(Conn *conn, const char *msgprefix) {
  int r= close_perhaps(&conn->fd);
  if (r) info("C%d %serror closing socket: %s",
	      conn->fd, msgprefix, strerror(errno));
}

int conn_busy(Conn *conn) {
  return
    conn->waiting.count ||
    conn->priority.count ||
    conn->sent.count ||
    conn->xmitu;
}

void conn_dispose(Conn *conn) {
  if (!conn) return;
  if (conn->rd) {
    oop_rd_cancel(conn->rd);
    oop_rd_delete_kill(conn->rd);
    conn->rd= 0;
  }
  if (conn->fd) {
    loop->cancel_fd(loop, conn->fd, OOP_WRITE);
    loop->cancel_fd(loop, conn->fd, OOP_EXCEPTION);
  }
  conn_closefd(conn,"");
  free(conn);
}

static void *conn_exception(oop_source *lp, int fd,
			    oop_event ev, void *conn_v) {
  Conn *conn= conn_v;
  unsigned char ch;
  assert(fd == conn->fd);
  assert(ev == OOP_EXCEPTION);
  int r= read(conn->fd, &ch, 1);
  if (r<0) connfail(conn,"read failed: %s",strerror(errno));
  else connfail(conn,"exceptional condition on socket (peer sent urgent"
		" data? read(,&ch,1)=%d,ch='\\x%02x')",r,ch);
  return OOP_CONTINUE;
}  

void vconnfail(Conn *conn, const char *fmt, va_list al) {
  int requeue[art_MaxState];
  memset(requeue,0,sizeof(requeue));

  Article *art;
  
  while ((art= LIST_REMHEAD(conn->priority)))
    LIST_ADDTAIL(art->ipf->queue, art);

  while ((art= LIST_REMHEAD(conn->waiting)))
    LIST_ADDTAIL(art->ipf->queue, art);

  while ((art= LIST_REMHEAD(conn->sent))) {
    requeue[art->state]++;
    if (art->state==art_Unsolicited) art->state= art_Unchecked;
    LIST_ADDTAIL(art->ipf->queue,art);
    check_reading_pause_resume(art->ipf);
  }

  int i;
  XmitDetails *d;
  for (i=0, d=conn->xmitd; i<conn->xmitu; i++, d++)
    xmit_free(d);

  LIST_REMOVE(conns,conn);

  char *m= mvasprintf(fmt,al);
  warn("C%d (now %d) connection failed "
       "(requeueing " RCI_TRIPLE_FMT_BASE "): %s",
       conn->fd, conns.count, RCI_TRIPLE_VALS_BASE(requeue, /*nothing*/), m);
  free(m);

  reconnect_blocking_event();
  conn_dispose(conn);
  check_assign_articles();
}

void connfail(Conn *conn, const char *fmt, ...) {
  va_list al;
  va_start(al,fmt);
  vconnfail(conn,fmt,al);
  va_end(al);
}

static void conn_idle_close(Conn *conn, const char *why) {
  static const char quitcmd[]= "QUIT\r\n";
  int todo= sizeof(quitcmd)-1;
  const char *p= quitcmd;
  for (;;) {
    int r= write(conn->fd, p, todo);
    if (r<0) {
      if (isewouldblock(errno))
	connfail(conn, "blocked writing QUIT to idle connection");
      else
	connfail(conn, "failed to write QUIT to idle connection: %s",
		 strerror(errno));
      break;
    }
    assert(r<=todo);
    todo -= r;
    if (!todo) {
      conn->quitting= why;
      conn->since_activity= 0;
      dbg("C%d is idle (%s), quitting", conn->fd, why);
      break;
    }
  }
}

/*
 * For our last connection, we also shut it down if we have had
 * less than K in the last L
 */
void check_idle_conns(void) {
  Conn *conn;

  int volthisperiod= lowvol_perperiod[lowvol_circptr];
  lowvol_circptr++;
  lowvol_circptr %= lowvol_periods;
  lowvol_total += volthisperiod;
  lowvol_total -= lowvol_perperiod[lowvol_circptr];
  lowvol_perperiod[lowvol_circptr]= 0;

  FOR_CONN(conn)
    conn->since_activity++;

 search_again:
  FOR_CONN(conn) {
    if (conn->since_activity <= need_activity_periods) continue;

    /* We need to shut this down */
    if (conn->quitting)
      connfail(conn,"timed out waiting for response to QUIT (%s)",
	       conn->quitting);
    else if (conn->sent.count)
      connfail(conn,"timed out waiting for responses");
    else if (conn->waiting.count || conn->priority.count)
      connfail(conn,"BUG IN INNDUCT conn has queue but nothing sent");
    else if (conn->xmitu)
      connfail(conn,"peer has been sending responses"
	       " before receiving our commands!");
    else
      conn_idle_close(conn, "no activity");
    
    goto search_again;
  }

  conn= LIST_HEAD(conns);
  if (!volthisperiod &&
      conns.count==1 &&
      lowvol_total < lowvol_thresh &&
      !conn_busy(conn))
    conn_idle_close(conn, "low volume");
}  

/*---------- reporting numbers of connections ----------*/

static int conns_max_reported, conns_idle_reported;

void notice_conns_more(const char *new_kind) {
  if (conns.count > conns_max_reported) {
    notice("up to %d connection(s) (%s)", conns.count, new_kind);
    conns_max_reported= conns.count;
  }
}

void notice_conns_fewer(void) {
  if (!conns.count && !conns_idle_reported) {
    notice("low volume, using intermittent connection");
    conns_idle_reported= 1;
  }
}

void notice_conns_stats(void) {
  notice("currently %d connection(s)", conns.count);
  conns_max_reported= conns.count;
  conns_idle_reported= 0;
}

/*---------- making new connections ----------*/

pid_t connecting_child;
int connecting_fdpass_sock;

static void connect_attempt_discard(void) {
  if (connecting_child) {
    int status= xwaitpid(&connecting_child, "connect");
    if (!(WIFEXITED(status) ||
	  (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL)))
      report_child_status("connect", status);
  }
  if (connecting_fdpass_sock) {
    cancel_fd_read_except(connecting_fdpass_sock);
    xclose_perhaps(&connecting_fdpass_sock, "connecting fdpass socket",0);
  }
}

#define PREP_DECL_MSG_CMSG(msg)			\
  char msgbyte= 0;				\
  struct iovec msgiov;				\
  msgiov.iov_base= &msgbyte;			\
  msgiov.iov_len= 1;				\
  struct msghdr msg;				\
  memset(&msg,0,sizeof(msg));			\
  char msg##cbuf[CMSG_SPACE(sizeof(int))];	\
  msg.msg_iov= &msgiov;				\
  msg.msg_iovlen= 1;				\
  msg.msg_control= msg##cbuf;			\
  msg.msg_controllen= sizeof(msg##cbuf);

static void *connchild_event(oop_source *lp, int fd, oop_event e, void *u) {
  Conn *conn= 0;

  assert(fd == connecting_fdpass_sock);

  PREP_DECL_MSG_CMSG(msg);
  
  ssize_t rs= recvmsg(fd, &msg, 0);
  if (rs<0) {
    if (isewouldblock(errno)) return OOP_CONTINUE;
    syswarn("failed to read socket from connecting child");
    goto x;
  }

  NEW(conn);
  LIST_INIT(conn->waiting);
  LIST_INIT(conn->priority);
  LIST_INIT(conn->sent);

  struct cmsghdr *h= 0;
  if (rs >= 0) h= CMSG_FIRSTHDR(&msg);
  if (!h) {
    int status= xwaitpid(&connecting_child, "connect child (broken)");

    if (WIFEXITED(status)) {
      if (WEXITSTATUS(status) != 0 &&
	  WEXITSTATUS(status) != CONNCHILD_ESTATUS_STREAM &&
	  WEXITSTATUS(status) != CONNCHILD_ESTATUS_NOSTREAM)
	/* child already reported the problem */;
      else {
	if (e == OOP_EXCEPTION)
	  warn("connect: connection child exited code %d but"
	       " unexpected exception on fdpass socket",
	       WEXITSTATUS(status));
	else
	  warn("connect: connection child exited code %d but"
	       " no cmsg (rs=%d)",
	       WEXITSTATUS(status), (int)rs);
      }
    } else if (WIFSIGNALED(status) && WTERMSIG(status) == SIGALRM) {
      warn("connect: connection attempt timed out");
    } else {
      report_child_status("connect", status);
    }
    goto x;
  }

#define CHK(field, val)							   \
  if (h->cmsg_##field != val) {						   \
    crash("connect: child sent cmsg with cmsg_" #field "=%ld, expected %ld", \
	  (long)h->cmsg_##field, (long)val);				   \
    goto x;								   \
  }
  CHK(level, SOL_SOCKET);
  CHK(type,  SCM_RIGHTS);
  CHK(len,   CMSG_LEN(sizeof(conn->fd)));
#undef CHK

  if (CMSG_NXTHDR(&msg,h)) crash("connect: child sent many cmsgs");

  memcpy(&conn->fd, CMSG_DATA(h), sizeof(conn->fd));

  int status;
  pid_t got= waitpid(connecting_child, &status, 0);
  if (got==-1) syscrash("connect: real wait for child");
  assert(got == connecting_child);
  connecting_child= 0;

  if (!WIFEXITED(status)) { report_child_status("connect",status); goto x; }
  int es= WEXITSTATUS(status);
  switch (es) {
  case CONNCHILD_ESTATUS_STREAM:    conn->stream= 1;   break;
  case CONNCHILD_ESTATUS_NOSTREAM:  conn->stream= 0;   break;
  default:
    die("connect: child gave unexpected exit status %d", es);
  }

  /* Phew! */
  conn->max_queue= conn->stream ? max_queue_per_conn : 1;

  loop->on_fd(loop, conn->fd, OOP_EXCEPTION, conn_exception, conn);
  conn->rd= oop_rd_new_fd(loop,conn->fd, 0, 0); /* sets nonblocking, too */
  if (!conn->fd) crash("oop_rd_new_fd conn failed (fd=%d)",conn->fd);
  int r= oop_rd_read(conn->rd, &peer_rd_style, NNTP_MAXLEN_COMMAND+1,
		     &peer_rd_ok, conn,
		     &peer_rd_err, conn);
  if (r) syscrash("oop_rd_read for peer (fd=%d)",conn->fd);

  LIST_ADDHEAD(conns, conn);
  const char *streamdesc= conn->stream ? "streaming" : "plain";
  info("C%d (now %d) connected %s", conn->fd, conns.count, streamdesc);
  notice_conns_more(streamdesc);

  connect_attempt_discard();
  check_assign_articles();
  return OOP_CONTINUE;

 x:
  conn_dispose(conn);
  connect_attempt_discard();
  reconnect_blocking_event();
  return OOP_CONTINUE;
}

int allow_connect_start(void) {
  return conns.count < max_connections
    && !connecting_child
    && !until_connect;
}

void connect_start(void) {
  assert(!connecting_child);
  assert(!connecting_fdpass_sock);

  info("starting connection attempt");
  int ok_until_connect= until_connect;
  reconnect_blocking_event();

  int socks[2];
  int r= socketpair(AF_UNIX, SOCK_STREAM, 0, socks);
  if (r) { syswarn("connect: cannot create socketpair for child"); return; }

  connecting_child= xfork("connection");

  if (!connecting_child) {
    FILE *cn_from, *cn_to;
    char buf[NNTP_MAXLEN_COMMAND+100];
    int exitstatus= CONNCHILD_ESTATUS_NOSTREAM;

    xclose(socks[0], "(in child) parent's connection fdpass socket",0);

    alarm(connection_setup_timeout);
    buf[sizeof(buf)-1] = 0;
    if (NNTPconnect(remote_host, port, &cn_from, &cn_to,
		    buf, sizeof(buf)-1) < 0) {
      int l= strlen(buf);
      int stripped=0;
      while (l>0) {
	unsigned char c= buf[l-1];
	if (!isspace(c)) break;
	if (c=='\n' || c=='\r') stripped=1;
	--l;
      }
      if (!buf[0]) {
	sysdie("connect: connection attempt failed");
      } else {
	buf[l]= 0;
	die("connect: %s: %s", stripped ? "rejected" : "failed",
	    sanitise(buf,-1));
      }
    }
    if (NNTPsendpassword((char*)remote_host, cn_from, cn_to) < 0)
      sysdie("connect: authentication failed");
    if (try_stream) {
      if (fputs("MODE STREAM\r\n", cn_to)==EOF ||
	  fflush(cn_to))
	sysdie("connect: could not send MODE STREAM");
      buf[sizeof(buf)-1]= 0;
      if (!fgets(buf, sizeof(buf)-1, cn_from)) {
	if (ferror(cn_from))
	  sysdie("connect: could not read response to MODE STREAM");
	else
	  die("connect: connection close in response to MODE STREAM");
      }
      int l= strlen(buf);
      assert(l>=1);
      if (buf[l-1]!='\n')
	die("connect: response to MODE STREAM is too long: %.100s...",
	    sanitise(buf,-1));
      l--;  if (l>0 && buf[l-1]=='\r') l--;
      buf[l]= 0;
      char *ep;
      int rcode= strtoul(buf,&ep,10);
      if (ep != &buf[3])
	die("connect: bad response to MODE STREAM: %.50s", sanitise(buf,-1));

      switch (rcode) {
      case 203:
	exitstatus= CONNCHILD_ESTATUS_STREAM;
	break;
      case 480:
      case 500:
	break;
      default:
	warn("connect: unexpected response to MODE STREAM: %.50s",
	     sanitise(buf,-1));
	exitstatus= CONNCHILD_ESTATUS_NOSTREAM;
	break;
      }
    }
    int fd= fileno(cn_from);

    PREP_DECL_MSG_CMSG(msg);
    struct cmsghdr *cmsg= CMSG_FIRSTHDR(&msg);
    cmsg->cmsg_level= SOL_SOCKET;
    cmsg->cmsg_type=  SCM_RIGHTS;
    cmsg->cmsg_len=   CMSG_LEN(sizeof(fd));
    memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));

    msg.msg_controllen= cmsg->cmsg_len;
    r= sendmsg(socks[1], &msg, 0);
    if (r<0) syscrash("sendmsg failed for new connection");
    if (r!=1) crash("sendmsg for new connection gave wrong result %d",r);

    _exit(exitstatus);
  }

  xclose(socks[1], "connecting fdpass child's socket",0);
  connecting_fdpass_sock= socks[0];
  xsetnonblock(connecting_fdpass_sock, 1);
  on_fd_read_except(connecting_fdpass_sock, connchild_event);

  if (!conns.count)
    until_connect= ok_until_connect;
}