Browse Source

#include <limits.h> to be sure to get IOV_MAX.

Use the net_state enum and change some state names from NET_ to BTP_.
Some minor type fixes.
master
Richard Nyberg 19 years ago
parent
commit
28fcbed3c5
4 changed files with 18 additions and 21 deletions
  1. +1
    -0
      btpd/btpd.h
  2. +11
    -15
      btpd/net.c
  3. +5
    -5
      btpd/net.h
  4. +1
    -1
      btpd/peer.h

+ 1
- 0
btpd/btpd.h View File

@@ -9,6 +9,7 @@
#include <errno.h>
#include <event.h>
#include <inttypes.h>
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>



+ 11
- 15
btpd/net.c View File

@@ -18,10 +18,6 @@

#include "btpd.h"

#ifndef IOV_MAX
#define IOV_MAX 1024
#endif

#define min(x, y) ((x) <= (y) ? (x) : (y))

void
@@ -117,7 +113,7 @@ net_write(struct peer *p, unsigned long wmax)
}

void
net_set_state(struct peer *p, int state, size_t size)
net_set_state(struct peer *p, enum net_state state, size_t size)
{
p->net.state = state;
p->net.st_bytes = size;
@@ -213,7 +209,7 @@ net_mh_ok(struct peer *p)
static void
net_progress(struct peer *p, size_t length)
{
if (p->net.state == NET_MSGBODY && p->net.msg_num == MSG_PIECE) {
if (p->net.state == BTP_MSGBODY && p->net.msg_num == MSG_PIECE) {
p->tp->downloaded += length;
p->rate_to_me[btpd.seconds % RATEHISTORY] += length;
}
@@ -245,28 +241,28 @@ net_state(struct peer *p, const char *buf)
goto bad;
bcopy(buf, p->id, 20);
peer_on_shake(p);
net_set_state(p, NET_MSGSIZE, 4);
net_set_state(p, BTP_MSGSIZE, 4);
break;
case NET_MSGSIZE:
case BTP_MSGSIZE:
p->net.msg_len = net_read32(buf);
if (p->net.msg_len != 0)
net_set_state(p, NET_MSGHEAD, 1);
net_set_state(p, BTP_MSGHEAD, 1);
break;
case NET_MSGHEAD:
case BTP_MSGHEAD:
p->net.msg_num = buf[0];
if (!net_mh_ok(p))
goto bad;
else if (p->net.msg_len == 1) {
if (net_dispatch_msg(p, buf) != 0)
goto bad;
net_set_state(p, NET_MSGSIZE, 4);
net_set_state(p, BTP_MSGSIZE, 4);
} else
net_set_state(p, NET_MSGBODY, p->net.msg_len - 1);
net_set_state(p, BTP_MSGBODY, p->net.msg_len - 1);
break;
case NET_MSGBODY:
case BTP_MSGBODY:
if (net_dispatch_msg(p, buf) != 0)
goto bad;
net_set_state(p, NET_MSGSIZE, 4);
net_set_state(p, BTP_MSGSIZE, 4);
break;
default:
abort();
@@ -332,7 +328,7 @@ net_read(struct peer *p, unsigned long rmax)

iov[1].iov_len = nread - rest;
while (p->net.st_bytes <= iov[1].iov_len) {
ssize_t consumed = p->net.st_bytes;
size_t consumed = p->net.st_bytes;
net_progress(p, consumed);
if (net_state(p, iov[1].iov_base) != 0)
return nread;


+ 5
- 5
btpd/net.h View File

@@ -15,16 +15,16 @@

#define SHAKE_LEN 68

enum shake_state {
enum net_state {
SHAKE_PSTR,
SHAKE_INFO,
SHAKE_ID,
NET_MSGSIZE,
NET_MSGHEAD,
NET_MSGBODY
BTP_MSGSIZE,
BTP_MSGHEAD,
BTP_MSGBODY
};

void net_set_state(struct peer *p, int state, size_t size);
void net_set_state(struct peer *p, enum net_state state, size_t size);

void net_connection_cb(int sd, short type, void *arg);
void net_bw_rate(void);


+ 1
- 1
btpd/peer.h View File

@@ -52,7 +52,7 @@ struct peer {
struct {
uint32_t msg_len;
uint8_t msg_num;
uint8_t state;
enum net_state state;
size_t st_bytes;
char *buf;
size_t off;


Loading…
Cancel
Save