3 * tailing reliable realtime streaming feeder for inn
4 * conn.c - connection establishment and teardown
6 * Copyright (C) 2010 Ian Jackson <ijackson@chiark.greenend.org.uk>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * (I believe that when you compile and link this as part of the inn2
22 * build, with the Makefile runes I have provided, all the libraries
23 * and files which end up included in innduct are licence-compatible
24 * with GPLv3. If not then please let me know. -Ian Jackson.)
29 /*========== management of connections ==========*/
31 static void reconnect_blocking_event(void) {
32 until_connect= reconnect_delay_periods;
35 void conn_closefd(Conn *conn, const char *msgprefix) {
36 int r= close_perhaps(&conn->fd);
37 if (r) info("C%d %serror closing socket: %s",
38 conn->fd, msgprefix, strerror(errno));
41 int conn_busy(Conn *conn) {
43 conn->waiting.count ||
44 conn->priority.count ||
49 void conn_dispose(Conn *conn) {
52 oop_rd_cancel(conn->rd);
53 oop_rd_delete_kill(conn->rd);
57 loop->cancel_fd(loop, conn->fd, OOP_WRITE);
58 loop->cancel_fd(loop, conn->fd, OOP_EXCEPTION);
60 conn_closefd(conn,"");
64 static void *conn_exception(oop_source *lp, int fd,
65 oop_event ev, void *conn_v) {
68 assert(fd == conn->fd);
69 assert(ev == OOP_EXCEPTION);
70 int r= read(conn->fd, &ch, 1);
71 if (r<0) connfail(conn,"read failed: %s",strerror(errno));
72 else connfail(conn,"exceptional condition on socket (peer sent urgent"
73 " data? read(,&ch,1)=%d,ch='\\x%02x')",r,ch);
77 void vconnfail(Conn *conn, const char *fmt, va_list al) {
78 int requeue[art_MaxState];
79 memset(requeue,0,sizeof(requeue));
83 while ((art= LIST_REMHEAD(conn->priority)))
84 LIST_ADDTAIL(art->ipf->queue, art);
86 while ((art= LIST_REMHEAD(conn->waiting)))
87 LIST_ADDTAIL(art->ipf->queue, art);
89 while ((art= LIST_REMHEAD(conn->sent))) {
90 requeue[art->state]++;
91 if (art->state==art_Unsolicited) art->state= art_Unchecked;
92 LIST_ADDTAIL(art->ipf->queue,art);
93 check_reading_pause_resume(art->ipf);
98 for (i=0, d=conn->xmitd; i<conn->xmitu; i++, d++)
101 LIST_REMOVE(conns,conn);
103 char *m= xvasprintf(fmt,al);
104 warn("C%d (now %d) connection failed requeueing " RCI_TRIPLE_FMT_BASE ": %s",
105 conn->fd, conns.count, RCI_TRIPLE_VALS_BASE(requeue, /*nothing*/), m);
108 reconnect_blocking_event();
110 check_assign_articles();
113 void connfail(Conn *conn, const char *fmt, ...) {
116 vconnfail(conn,fmt,al);
120 static void conn_idle_close(Conn *conn, const char *why) {
121 static const char quitcmd[]= "QUIT\r\n";
122 int todo= sizeof(quitcmd)-1;
123 const char *p= quitcmd;
125 int r= write(conn->fd, p, todo);
127 if (isewouldblock(errno))
128 connfail(conn, "blocked writing QUIT to idle connection");
130 connfail(conn, "failed to write QUIT to idle connection: %s",
138 conn->since_activity= 0;
139 dbg("C%d is idle (%s), quitting", conn->fd, why);
146 * For our last connection, we also shut it down if we have had
147 * less than K in the last L
149 void check_idle_conns(void) {
152 int volthisperiod= lowvol_perperiod[lowvol_circptr];
154 lowvol_circptr %= lowvol_periods;
155 lowvol_total += volthisperiod;
156 lowvol_total -= lowvol_perperiod[lowvol_circptr];
157 lowvol_perperiod[lowvol_circptr]= 0;
160 conn->since_activity++;
164 if (conn->since_activity <= need_activity_periods) continue;
166 /* We need to shut this down */
168 connfail(conn,"timed out waiting for response to QUIT (%s)",
170 else if (conn->sent.count)
171 connfail(conn,"timed out waiting for responses");
172 else if (conn->waiting.count || conn->priority.count)
173 connfail(conn,"BUG IN INNDUCT conn has queue but nothing sent");
174 else if (conn->xmitu)
175 connfail(conn,"peer has been sending responses"
176 " before receiving our commands!");
178 conn_idle_close(conn, "no activity");
183 conn= LIST_HEAD(conns);
184 if (!volthisperiod &&
186 lowvol_total < lowvol_thresh &&
188 conn_idle_close(conn, "low volume");
191 /*---------- making new connections ----------*/
193 pid_t connecting_child;
194 int connecting_fdpass_sock;
196 static void connect_attempt_discard(void) {
197 if (connecting_child) {
198 int status= xwaitpid(&connecting_child, "connect");
199 if (!(WIFEXITED(status) ||
200 (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL)))
201 report_child_status("connect", status);
203 if (connecting_fdpass_sock) {
204 cancel_fd_read_except(connecting_fdpass_sock);
205 xclose_perhaps(&connecting_fdpass_sock, "connecting fdpass socket",0);
209 #define PREP_DECL_MSG_CMSG(msg) \
211 struct iovec msgiov; \
212 msgiov.iov_base= &msgbyte; \
215 memset(&msg,0,sizeof(msg)); \
216 char msg##cbuf[CMSG_SPACE(sizeof(int))]; \
217 msg.msg_iov= &msgiov; \
219 msg.msg_control= msg##cbuf; \
220 msg.msg_controllen= sizeof(msg##cbuf);
222 static void *connchild_event(oop_source *lp, int fd, oop_event e, void *u) {
225 assert(fd == connecting_fdpass_sock);
227 PREP_DECL_MSG_CMSG(msg);
229 ssize_t rs= recvmsg(fd, &msg, 0);
231 if (isewouldblock(errno)) return OOP_CONTINUE;
232 syswarn("failed to read socket from connecting child");
237 LIST_INIT(conn->waiting);
238 LIST_INIT(conn->priority);
239 LIST_INIT(conn->sent);
241 struct cmsghdr *h= 0;
242 if (rs >= 0) h= CMSG_FIRSTHDR(&msg);
244 int status= xwaitpid(&connecting_child, "connect child (broken)");
246 if (WIFEXITED(status)) {
247 if (WEXITSTATUS(status) != 0 &&
248 WEXITSTATUS(status) != CONNCHILD_ESTATUS_STREAM &&
249 WEXITSTATUS(status) != CONNCHILD_ESTATUS_NOSTREAM)
250 /* child already reported the problem */;
252 if (e == OOP_EXCEPTION)
253 warn("connect: connection child exited code %d but"
254 " unexpected exception on fdpass socket",
255 WEXITSTATUS(status));
257 warn("connect: connection child exited code %d but"
259 WEXITSTATUS(status), (int)rs);
261 } else if (WIFSIGNALED(status) && WTERMSIG(status) == SIGALRM) {
262 warn("connect: connection attempt timed out");
264 report_child_status("connect", status);
269 #define CHK(field, val) \
270 if (h->cmsg_##field != val) { \
271 crash("connect: child sent cmsg with cmsg_" #field "=%d, expected %d", \
272 h->cmsg_##field, val); \
275 CHK(level, SOL_SOCKET);
276 CHK(type, SCM_RIGHTS);
277 CHK(len, CMSG_LEN(sizeof(conn->fd)));
280 if (CMSG_NXTHDR(&msg,h)) crash("connect: child sent many cmsgs");
282 memcpy(&conn->fd, CMSG_DATA(h), sizeof(conn->fd));
285 pid_t got= waitpid(connecting_child, &status, 0);
286 if (got==-1) syscrash("connect: real wait for child");
287 assert(got == connecting_child);
290 if (!WIFEXITED(status)) { report_child_status("connect",status); goto x; }
291 int es= WEXITSTATUS(status);
293 case CONNCHILD_ESTATUS_STREAM: conn->stream= 1; break;
294 case CONNCHILD_ESTATUS_NOSTREAM: conn->stream= 0; break;
296 die("connect: child gave unexpected exit status %d", es);
300 conn->max_queue= conn->stream ? max_queue_per_conn : 1;
302 loop->on_fd(loop, conn->fd, OOP_EXCEPTION, conn_exception, conn);
303 conn->rd= oop_rd_new_fd(loop,conn->fd, 0, 0); /* sets nonblocking, too */
304 if (!conn->fd) crash("oop_rd_new_fd conn failed (fd=%d)",conn->fd);
305 int r= oop_rd_read(conn->rd, &peer_rd_style, NNTP_STRLEN,
308 if (r) syscrash("oop_rd_read for peer (fd=%d)",conn->fd);
310 LIST_ADDHEAD(conns, conn);
311 notice("C%d (now %d) connected %s",
312 conn->fd, conns.count, conn->stream ? "streaming" : "plain");
314 connect_attempt_discard();
315 check_assign_articles();
320 connect_attempt_discard();
321 reconnect_blocking_event();
325 int allow_connect_start(void) {
326 return conns.count < max_connections
331 void connect_start(void) {
332 assert(!connecting_child);
333 assert(!connecting_fdpass_sock);
335 info("starting connection attempt");
336 int ok_until_connect= until_connect;
337 reconnect_blocking_event();
340 int r= socketpair(AF_UNIX, SOCK_STREAM, 0, socks);
341 if (r) { syswarn("connect: cannot create socketpair for child"); return; }
343 connecting_child= xfork("connection");
345 if (!connecting_child) {
346 FILE *cn_from, *cn_to;
347 char buf[NNTP_STRLEN+100];
348 int exitstatus= CONNCHILD_ESTATUS_NOSTREAM;
350 xclose(socks[0], "(in child) parent's connection fdpass socket",0);
352 alarm(connection_setup_timeout);
353 if (NNTPconnect((char*)remote_host, port, &cn_from, &cn_to, buf) < 0) {
357 unsigned char c= buf[l-1];
358 if (!isspace(c)) break;
359 if (c=='\n' || c=='\r') stripped=1;
363 sysdie("connect: connection attempt failed");
366 die("connect: %s: %s", stripped ? "rejected" : "failed",
370 if (NNTPsendpassword((char*)remote_host, cn_from, cn_to) < 0)
371 sysdie("connect: authentication failed");
373 if (fputs("MODE STREAM\r\n", cn_to)==EOF ||
375 sysdie("connect: could not send MODE STREAM");
376 buf[sizeof(buf)-1]= 0;
377 if (!fgets(buf, sizeof(buf)-1, cn_from)) {
379 sysdie("connect: could not read response to MODE STREAM");
381 die("connect: connection close in response to MODE STREAM");
386 die("connect: response to MODE STREAM is too long: %.100s...",
388 l--; if (l>0 && buf[l-1]=='\r') l--;
391 int rcode= strtoul(buf,&ep,10);
393 die("connect: bad response to MODE STREAM: %.50s", sanitise(buf,-1));
397 exitstatus= CONNCHILD_ESTATUS_STREAM;
403 warn("connect: unexpected response to MODE STREAM: %.50s",
409 int fd= fileno(cn_from);
411 PREP_DECL_MSG_CMSG(msg);
412 struct cmsghdr *cmsg= CMSG_FIRSTHDR(&msg);
413 cmsg->cmsg_level= SOL_SOCKET;
414 cmsg->cmsg_type= SCM_RIGHTS;
415 cmsg->cmsg_len= CMSG_LEN(sizeof(fd));
416 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
418 msg.msg_controllen= cmsg->cmsg_len;
419 r= sendmsg(socks[1], &msg, 0);
420 if (r<0) syscrash("sendmsg failed for new connection");
421 if (r!=1) crash("sendmsg for new connection gave wrong result %d",r);
426 xclose(socks[1], "connecting fdpass child's socket",0);
427 connecting_fdpass_sock= socks[0];
428 xsetnonblock(connecting_fdpass_sock, 1);
429 on_fd_read_except(connecting_fdpass_sock, connchild_event);
432 until_connect= ok_until_connect;