1 /*========== handling responses from peer ==========*/
3 static const oop_rd_style peer_rd_style= {
4 OOP_RD_DELIM_STRIP, '\n',
9 static void *peer_rd_err(oop_source *lp, oop_read *oread, oop_rd_event ev,
10 const char *errmsg, int errnoval,
11 const char *data, size_t recsz, void *conn_v) {
13 connfail(conn, "error receiving from peer: %s", errmsg);
17 static Article *article_reply_check(Conn *conn, const char *response,
18 int code_indicates_streaming,
20 /* 1:yes, -1:no, 0:dontcare */,
21 const char *sanitised_response) {
22 Article *art= LIST_HEAD(conn->sent);
26 "peer gave unexpected response when no commands outstanding: %s",
31 if (code_indicates_streaming) {
32 assert(!memchr(response, 0, 4)); /* ensured by peer_rd_ok */
34 connfail(conn, "peer gave streaming response code "
35 " to IHAVE or subsequent body: %s", sanitised_response);
38 const char *got_mid= response+4;
39 int got_midlen= strcspn(got_mid, " \n\r");
40 if (got_midlen<3 || got_mid[0]!='<' || got_mid[got_midlen-1]!='>') {
41 connfail(conn, "peer gave streaming response with syntactically invalid"
42 " messageid: %s", sanitised_response);
45 if (got_midlen != art->midlen ||
46 memcmp(got_mid, art->messageid, got_midlen)) {
47 connfail(conn, "peer gave streaming response code to wrong article -"
48 " probable synchronisation problem; we offered: %s;"
50 art->messageid, sanitised_response);
55 connfail(conn, "peer gave non-streaming response code to"
56 " CHECK/TAKETHIS: %s", sanitised_response);
61 if (must_have_sent>0 && art->state < art_Wanted) {
62 connfail(conn, "peer says article accepted but"
63 " we had not sent the body: %s", sanitised_response);
66 if (must_have_sent<0 && art->state >= art_Wanted) {
67 connfail(conn, "peer says please sent the article but we just did: %s",
72 Article *art_again= LIST_REMHEAD(conn->sent);
73 assert(art_again == art);
77 static void update_nocheck(int accepted) {
78 accept_proportion *= nocheck_decay;
79 accept_proportion += accepted * (1.0 - nocheck_decay);
80 int new_nocheck= accept_proportion >= nocheck_thresh;
81 if (new_nocheck && !nocheck_reported) {
82 notice("entering nocheck mode for the first time");
84 } else if (new_nocheck != nocheck) {
85 dbg("nocheck mode %s", new_nocheck ? "start" : "stop");
90 static void article_done(Article *art, int whichcount) {
91 if (whichcount>=0 && !art->missing)
92 art->ipf->counts[art->state][whichcount]++;
94 if (whichcount == RC_accepted) update_nocheck(1);
95 else if (whichcount == RC_unwanted) update_nocheck(0);
97 InputFile *ipf= art->ipf;
99 while (art->blanklen) {
100 static const char spaces[]=
110 int nspaces= sizeof(spaces)-1;
111 int w= art->blanklen; if (w > nspaces) w= nspaces;
112 int r= pwrite(ipf->fd, spaces, w, art->offset);
114 if (errno==EINTR) continue;
115 syscrash("failed to blank entry for %s (length %d at offset %lu) in %s",
116 art->messageid, art->blanklen,
117 (unsigned long)art->offset, ipf->path);
119 assert(r>=0 && r<=w);
125 assert(ipf->inprogress >= 0);
128 if (!ipf->inprogress && ipf != main_input_file)
129 queue_check_input_done();
132 static void *peer_rd_ok(oop_source *lp, oop_read *oread, oop_rd_event ev,
133 const char *errmsg, int errnoval,
134 const char *data, size_t recsz, void *conn_v) {
137 if (ev == OOP_RD_EOF) {
138 connfail(conn, "unexpected EOF from peer");
141 assert(ev == OOP_RD_OK);
143 char *sani= sanitise(data,-1);
146 unsigned long code= strtoul(data, &ep, 10);
147 if (ep != data+3 || *ep != ' ' || data[0]=='0') {
148 connfail(conn, "badly formatted response from peer: %s", sani);
152 int busy= conn_busy(conn);
154 if (conn->quitting) {
155 if (code!=205 && code!=400) {
156 connfail(conn, "peer gave unexpected response to QUIT (%s): %s",
157 conn->quitting, sani);
159 LIST_REMOVE(conns,conn);
160 notice("C%d (now %d) idle connection closed (%s)",
161 conn->fd, conns.count, conn->quitting);
168 conn->since_activity= 0;
171 #define GET_ARTICLE(musthavesent) do{ \
172 art= article_reply_check(conn, data, code_streaming, musthavesent, sani); \
173 if (!art) return OOP_CONTINUE; /* reply_check has failed the conn */ \
176 #define ARTICLE_DEALTWITH(streaming,musthavesent,how) do{ \
177 code_streaming= (streaming); \
178 GET_ARTICLE(musthavesent); \
179 article_done(art, RC_##how); \
183 #define PEERBADMSG(m) do { \
184 connfail(conn, m ": %s", sani); return OOP_CONTINUE; \
187 int code_streaming= 0;
191 default: PEERBADMSG("peer sent unexpected message");
195 PEERBADMSG("peer timed us out or stopped accepting articles");
197 LIST_REMOVE(conns,conn);
198 notice("C%d (now %d) idle connection closed by peer",
199 conns.count, conn->fd);
203 case 435: ARTICLE_DEALTWITH(0,0,unwanted); /* IHAVE says they have it */
204 case 438: ARTICLE_DEALTWITH(1,0,unwanted); /* CHECK/TAKETHIS: they have it */
206 case 235: ARTICLE_DEALTWITH(0,1,accepted); /* IHAVE says thanks */
207 case 239: ARTICLE_DEALTWITH(1,1,accepted); /* TAKETHIS says thanks */
209 case 437: ARTICLE_DEALTWITH(0,0,rejected); /* IHAVE says rejected */
210 case 439: ARTICLE_DEALTWITH(1,0,rejected); /* TAKETHIS says rejected */
212 case 238: /* CHECK says send it */
214 case 335: /* IHAVE says send it */
216 assert(art->state == art_Unchecked);
217 art->ipf->counts[art->state][RC_accepted]++;
218 art->state= art_Wanted;
219 LIST_ADDTAIL(conn->priority, art);
222 case 431: /* CHECK or TAKETHIS says try later */
224 case 436: /* IHAVE says try later */
226 article_defer(art, RC_deferred);
232 conn_maybe_write(conn);
233 check_assign_articles();