diff options
| author | Richard Nyberg <rnyberg@murmeldjur.se> | 2005-10-08 19:08:10 +0000 |
|---|---|---|
| committer | Richard Nyberg <rnyberg@murmeldjur.se> | 2005-10-08 19:08:10 +0000 |
| commit | e025c4743adc6a7db86f071b81d62db4baf44394 (patch) | |
| tree | ffc566fb26f4e1a2b9b7c05b0b19905ab967d540 | |
| parent | 0cae0e478d29eed009063ba8b69965561503d259 (diff) | |
| download | btpd-e025c4743adc6a7db86f071b81d62db4baf44394.tar.gz btpd-e025c4743adc6a7db86f071b81d62db4baf44394.zip | |
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.
| -rw-r--r-- | btpd/net.c | 13 | ||||
| -rw-r--r-- | btpd/net.h | 1 | ||||
| -rw-r--r-- | 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; |