From e025c4743adc6a7db86f071b81d62db4baf44394 Mon Sep 17 00:00:00 2001 From: Richard Nyberg Date: Sat, 8 Oct 2005 19:08:10 +0000 Subject: [PATCH] Add a new net state to get the index and begin fields from piece messages before we read the piece data. This can be used to test for junk earlier. --- btpd/net.c | 13 +++++++++---- btpd/net.h | 1 + btpd/peer.h | 2 ++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/btpd/net.c b/btpd/net.c index dc65e4a..3e32beb 100644 --- a/btpd/net.c +++ b/btpd/net.c @@ -171,10 +171,8 @@ net_dispatch_msg(struct peer *p, const char *buf) peer_on_cancel(p, index, begin, length); break; case MSG_PIECE: - index = net_read32(buf); - begin = net_read32(buf + 4); length = p->net.msg_len - 9; - peer_on_piece(p, index, begin, length, buf + 8); + peer_on_piece(p, p->net.pc_index, p->net.pc_begin, length, buf); break; default: abort(); @@ -258,9 +256,16 @@ net_state(struct peer *p, const char *buf) if (net_dispatch_msg(p, buf) != 0) goto bad; net_set_state(p, BTP_MSGSIZE, 4); - } else + } else if (p->net.msg_num == MSG_PIECE) + net_set_state(p, BTP_PIECEMETA, 8); + else net_set_state(p, BTP_MSGBODY, p->net.msg_len - 1); break; + case BTP_PIECEMETA: + p->net.pc_index = net_read32(buf); + p->net.pc_begin = net_read32(buf + 4); + net_set_state(p, BTP_MSGBODY, p->net.msg_len - 9); + break; case BTP_MSGBODY: if (net_dispatch_msg(p, buf) != 0) goto bad; diff --git a/btpd/net.h b/btpd/net.h index 4f77900..33b5424 100644 --- a/btpd/net.h +++ b/btpd/net.h @@ -21,6 +21,7 @@ enum net_state { SHAKE_ID, BTP_MSGSIZE, BTP_MSGHEAD, + BTP_PIECEMETA, BTP_MSGBODY }; diff --git a/btpd/peer.h b/btpd/peer.h index 2a91f6d..0070182 100644 --- a/btpd/peer.h +++ b/btpd/peer.h @@ -52,6 +52,8 @@ struct peer { struct { uint32_t msg_len; uint8_t msg_num; + uint32_t pc_index; + uint32_t pc_begin; enum net_state state; size_t st_bytes; char *buf;