Browse Source

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.
master
Richard Nyberg 19 years ago
parent
commit
e025c4743a
3 changed files with 12 additions and 4 deletions
  1. +9
    -4
      btpd/net.c
  2. +1
    -0
      btpd/net.h
  3. +2
    -0
      btpd/peer.h

+ 9
- 4
btpd/net.c View File

@@ -171,10 +171,8 @@ net_dispatch_msg(struct peer *p, const char *buf)
peer_on_cancel(p, index, begin, length); peer_on_cancel(p, index, begin, length);
break; break;
case MSG_PIECE: case MSG_PIECE:
index = net_read32(buf);
begin = net_read32(buf + 4);
length = p->net.msg_len - 9; 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; break;
default: default:
abort(); abort();
@@ -258,9 +256,16 @@ net_state(struct peer *p, const char *buf)
if (net_dispatch_msg(p, buf) != 0) if (net_dispatch_msg(p, buf) != 0)
goto bad; goto bad;
net_set_state(p, BTP_MSGSIZE, 4); 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); net_set_state(p, BTP_MSGBODY, p->net.msg_len - 1);
break; 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: case BTP_MSGBODY:
if (net_dispatch_msg(p, buf) != 0) if (net_dispatch_msg(p, buf) != 0)
goto bad; goto bad;


+ 1
- 0
btpd/net.h View File

@@ -21,6 +21,7 @@ enum net_state {
SHAKE_ID, SHAKE_ID,
BTP_MSGSIZE, BTP_MSGSIZE,
BTP_MSGHEAD, BTP_MSGHEAD,
BTP_PIECEMETA,
BTP_MSGBODY BTP_MSGBODY
}; };




+ 2
- 0
btpd/peer.h View File

@@ -52,6 +52,8 @@ struct peer {
struct { struct {
uint32_t msg_len; uint32_t msg_len;
uint8_t msg_num; uint8_t msg_num;
uint32_t pc_index;
uint32_t pc_begin;
enum net_state state; enum net_state state;
size_t st_bytes; size_t st_bytes;
char *buf; char *buf;


||||||
x
 
000:0
Loading…
Cancel
Save