Changeset 1534


Ignore:
Timestamp:
Mar 5, 2007, 11:03:38 PM (15 years ago)
Author:
joshe
Message:

Do bounds checking on bitfields.

Location:
trunk/libtransmission
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/completion.c

    r1356 r1534  
    3131    cp                   = malloc( sizeof( tr_completion_t ) );
    3232    cp->tor              = tor;
    33     cp->blockBitfield    = malloc( ( tor->blockCount + 7 ) / 8 );
     33    cp->blockBitfield    = tr_bitfieldNew( tor->blockCount );
    3434    cp->blockDownloaders = malloc( tor->blockCount );
    35     cp->pieceBitfield    = malloc( ( tor->info.pieceCount + 7 ) / 8 );
     35    cp->pieceBitfield    = tr_bitfieldNew( tor->info.pieceCount );
    3636    cp->missingBlocks    = malloc( tor->info.pieceCount * sizeof( int ) );
    3737
     
    4343void tr_cpClose( tr_completion_t * cp )
    4444{
    45     free( cp->blockBitfield );
    46     free( cp->blockDownloaders );
    47     free( cp->pieceBitfield );
    48     free( cp->missingBlocks );
    49     free( cp );
     45    tr_bitfieldFree( cp->blockBitfield );
     46    free(            cp->blockDownloaders );
     47    tr_bitfieldFree( cp->pieceBitfield );
     48    free(            cp->missingBlocks );
     49    free(            cp );
    5050}
    5151
     
    5656
    5757    cp->blockCount = 0;
    58     memset( cp->blockBitfield,    0, ( tor->blockCount + 7 ) / 8 );
     58    tr_bitfieldClear( cp->blockBitfield );
    5959    memset( cp->blockDownloaders, 0, tor->blockCount );
    60     memset( cp->pieceBitfield,    0, ( tor->info.pieceCount + 7 ) / 8 );
     60    tr_bitfieldClear( cp->pieceBitfield );
    6161    for( i = 0; i < tor->info.pieceCount; i++ )
    6262    {
     
    107107}
    108108
    109 uint8_t * tr_cpPieceBitfield( tr_completion_t * cp )
     109tr_bitfield_t * tr_cpPieceBitfield( tr_completion_t * cp )
    110110{
    111111    return cp->pieceBitfield;
     
    196196}
    197197
    198 uint8_t * tr_cpBlockBitfield( tr_completion_t * cp )
     198tr_bitfield_t * tr_cpBlockBitfield( tr_completion_t * cp )
    199199{
    200200    return cp->blockBitfield;
    201201}
    202202
    203 void tr_cpBlockBitfieldSet( tr_completion_t * cp, uint8_t * bitfield )
     203void tr_cpBlockBitfieldSet( tr_completion_t * cp, tr_bitfield_t * bitfield )
    204204{
    205205    tr_torrent_t * tor = cp->tor;
     
    238238    int blockCount, startBlock, endBlock;
    239239    int complete;
    240     uint8_t * bitfield;
     240    tr_bitfield_t * bitfield;
    241241   
    242242    blockCount  = tr_pieceCountBlocks( piece );
  • trunk/libtransmission/completion.h

    r1356 r1534  
    2525struct tr_completion_s
    2626{
    27     tr_torrent_t * tor;
    28     uint8_t      * blockBitfield;
    29     uint8_t      * blockDownloaders;
    30     int            blockCount;
    31     uint8_t      * pieceBitfield;
    32     int          * missingBlocks;
     27    tr_torrent_t      * tor;
     28    tr_bitfield_t     * blockBitfield;
     29    uint8_t           * blockDownloaders;
     30    int                 blockCount;
     31    tr_bitfield_t     * pieceBitfield;
     32    int               * missingBlocks;
    3333};
    3434
     
    4848int               tr_cpPieceHasAllBlocks( tr_completion_t *, int piece );
    4949int               tr_cpPieceIsComplete( tr_completion_t *, int piece );
    50 uint8_t         * tr_cpPieceBitfield( tr_completion_t * );
     50tr_bitfield_t   * tr_cpPieceBitfield( tr_completion_t * );
    5151void              tr_cpPieceAdd( tr_completion_t *, int piece );
    5252void              tr_cpPieceRem( tr_completion_t *, int piece );
     
    5858void              tr_cpBlockAdd( tr_completion_t *, int block );
    5959void              tr_cpBlockRem( tr_completion_t *, int block );
    60 uint8_t         * tr_cpBlockBitfield( tr_completion_t * );
    61 void              tr_cpBlockBitfieldSet( tr_completion_t *, uint8_t * );
     60tr_bitfield_t   * tr_cpBlockBitfield( tr_completion_t * );
     61void              tr_cpBlockBitfieldSet( tr_completion_t *, tr_bitfield_t * );
    6262float             tr_cpPercentBlocksInPiece( tr_completion_t * cp, int piece );
    6363/* Missing = we don't have it and we are not getting it from any peer yet */
  • trunk/libtransmission/fastresume.h

    r1419 r1534  
    192192    uint8_t * buf;
    193193    size_t    len;
     194    tr_bitfield_t bitfield;
    194195
    195196    len = FR_PROGRESS_LEN( tor );
     
    220221
    221222    /* Copy the bitfield for blocks and fill blockHave */
    222     tr_cpBlockBitfieldSet( tor->completion, buf + FR_MTIME_LEN( tor ) );
     223    memset( &bitfield, 0, sizeof bitfield );
     224    bitfield.len = FR_BLOCK_BITFIELD_LEN( tor );
     225    bitfield.bits = buf + FR_MTIME_LEN( tor );
     226    tr_cpBlockBitfieldSet( tor->completion, &bitfield );
    223227
    224228    /* Copy the 'slotPiece' table */
  • trunk/libtransmission/internal.h

    r1462 r1534  
    125125typedef struct tr_completion_s tr_completion_t;
    126126typedef struct tr_shared_s tr_shared_t;
     127typedef struct tr_bitfield_s tr_bitfield_t;
    127128
    128129typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t;
  • trunk/libtransmission/peer.c

    r1452 r1534  
    3939struct tr_peer_s
    4040{
    41     tr_torrent_t   * tor;
    42 
    43     struct in_addr addr;
    44     in_port_t      port;  /* peer's listening port, 0 if not known */
    45 
    46 #define PEER_STATUS_IDLE       1 /* Need to connect */
    47 #define PEER_STATUS_CONNECTING 2 /* Trying to send handshake */
    48 #define PEER_STATUS_HANDSHAKE  4 /* Waiting for peer's handshake */
    49 #define PEER_STATUS_CONNECTED  8 /* Got peer's handshake */
    50     int            status;
    51     int            socket;
    52     char           incoming;
    53     uint64_t       date;
    54     uint64_t       keepAlive;
    55 
    56     char           amChoking;
    57     char           amInterested;
    58     char           peerChoking;
    59     char           peerInterested;
    60 
    61     int            optimistic;
    62     uint64_t       lastChoke;
    63 
    64     uint8_t        id[20];
     41    tr_torrent_t      * tor;
     42
     43    struct in_addr      addr;
     44    in_port_t           port;  /* peer's listening port, 0 if not known */
     45
     46#define PEER_STATUS_IDLE        1 /* Need to connect */
     47#define PEER_STATUS_CONNECTING  2 /* Trying to send handshake */
     48#define PEER_STATUS_HANDSHAKE   4 /* Waiting for peer's handshake */
     49#define PEER_STATUS_CONNECTED   8 /* Got peer's handshake */
     50    int                 status;
     51    int                 socket;
     52    char                incoming;
     53    uint64_t            date;
     54    uint64_t            keepAlive;
     55
     56    char                amChoking;
     57    char                amInterested;
     58    char                peerChoking;
     59    char                peerInterested;
     60
     61    int                 optimistic;
     62    uint64_t            lastChoke;
     63
     64    uint8_t             id[20];
    6565
    6666    /* The pieces that the peer has */
    67     uint8_t      * bitfield;
    68     int            pieceCount;
    69     float          progress;
    70 
    71     int            goodPcs;
    72     int            badPcs;
    73     int            banned;
     67    tr_bitfield_t     * bitfield;
     68    int                 pieceCount;
     69    float               progress;
     70
     71    int                 goodPcs;
     72    int                 badPcs;
     73    int                 banned;
    7474    /* The pieces that the peer is contributing to */
    75     uint8_t      * blamefield;
     75    tr_bitfield_t     * blamefield;
    7676    /* The bad pieces that the peer has contributed to */
    77     uint8_t      * banfield;
    78 
    79     uint8_t      * buf;
    80     int            size;
    81     int            pos;
    82 
    83     uint8_t      * outMessages;
    84     int            outMessagesSize;
    85     int            outMessagesPos;
    86     uint8_t        outBlock[13+16384];
    87     int            outBlockSize;
    88     int            outBlockLoaded;
    89     int            outBlockSending;
    90 
    91     int            inRequestCount;
    92     tr_request_t   inRequests[OUR_REQUEST_COUNT];
    93     int            inIndex;
    94     int            inBegin;
    95     int            inLength;
    96     uint64_t       inTotal;
    97 
    98     int            outRequestCount;
    99     tr_request_t   outRequests[MAX_REQUEST_COUNT];
    100     uint64_t       outTotal;
    101     uint64_t       outDate;
    102 
    103     tr_ratecontrol_t * download;
    104     tr_ratecontrol_t * upload;
     77    tr_bitfield_t     * banfield;
     78
     79    uint8_t           * buf;
     80    int                 size;
     81    int                 pos;
     82
     83    uint8_t           * outMessages;
     84    int                 outMessagesSize;
     85    int                 outMessagesPos;
     86    uint8_t             outBlock[13+16384];
     87    int                 outBlockSize;
     88    int                 outBlockLoaded;
     89    int                 outBlockSending;
     90
     91    int                 inRequestCount;
     92    tr_request_t        inRequests[OUR_REQUEST_COUNT];
     93    int                 inIndex;
     94    int                 inBegin;
     95    int                 inLength;
     96    uint64_t            inTotal;
     97
     98    int                 outRequestCount;
     99    tr_request_t        outRequests[MAX_REQUEST_COUNT];
     100    uint64_t            outTotal;
     101    uint64_t            outDate;
     102
     103    tr_ratecontrol_t  * download;
     104    tr_ratecontrol_t  * upload;
    105105};
    106106
     
    163163        tr_cpDownloaderRem( tor->completion, block );
    164164    }
    165     if( peer->bitfield )
    166     {
    167         free( peer->bitfield );
    168     }
    169     if( peer->blamefield )
    170     {
    171         free( peer->blamefield );
    172     }
    173     if( peer->banfield )
    174     {
    175         free( peer->banfield );
    176     }
     165    tr_bitfieldFree( peer->bitfield );
     166    tr_bitfieldFree( peer->blamefield );
     167    tr_bitfieldFree( peer->banfield );
    177168    if( peer->buf )
    178169    {
     
    533524 *
    534525 **********************************************************************/
    535 uint8_t * tr_peerBitfield( tr_peer_t * peer )
     526tr_bitfield_t * tr_peerBitfield( tr_peer_t * peer )
    536527{
    537528    return peer->bitfield;
     
    602593            /* Assume the peer wasn't responsible for the bad pieces
    603594               we was banned for */
    604             memset( peer->banfield, 0x00, ( tor->info.pieceCount + 7 ) / 8 );
     595            tr_bitfieldClear( peer->banfield );
    605596        }
    606597    }
     
    612603        if( !peer->banfield )
    613604        {
    614             peer->banfield = calloc( ( tor->info.pieceCount + 7 ) / 8, 1 );
     605            peer->banfield = tr_bitfieldNew( tor->info.pieceCount );
    615606        }
    616607        tr_bitfieldAdd( peer->banfield, piece );
  • trunk/libtransmission/peer.h

    r1422 r1534  
    4646float       tr_peerProgress      ( tr_peer_t * );
    4747int         tr_peerPort          ( tr_peer_t * );
    48 uint8_t *   tr_peerBitfield      ( tr_peer_t * );
     48tr_bitfield_t * tr_peerBitfield  ( tr_peer_t * );
    4949float       tr_peerDownloadRate  ( tr_peer_t * );
    5050float       tr_peerUploadRate    ( tr_peer_t * );
  • trunk/libtransmission/peerparse.h

    r1442 r1534  
    105105    if( !peer->bitfield )
    106106    {
    107         peer->bitfield = calloc( ( tor->info.pieceCount + 7 ) / 8, 1 );
     107        peer->bitfield = tr_bitfieldNew( tor->info.pieceCount );
    108108    }
    109109    if( !tr_bitfieldHas( peer->bitfield, piece ) )
     
    155155    if( !peer->bitfield )
    156156    {
    157         peer->bitfield = malloc( bitfieldSize );
    158     }
    159     memcpy( peer->bitfield, p, bitfieldSize );
     157        peer->bitfield = tr_bitfieldNew( inf->pieceCount );
     158    }
     159    assert( bitfieldSize == peer->bitfield->len );
     160    memcpy( peer->bitfield->bits, p, bitfieldSize );
    160161
    161162    peer->pieceCount = 0;
     
    295296    if( !peer->blamefield )
    296297    {
    297         peer->blamefield = calloc( ( tor->info.pieceCount + 7 ) / 8, 1 );
     298        peer->blamefield = tr_bitfieldNew( tor->info.pieceCount );
    298299    }
    299300    tr_bitfieldAdd( peer->blamefield, index );
  • trunk/libtransmission/peerutils.h

    r1425 r1534  
    105105static int isInteresting( tr_torrent_t * tor, tr_peer_t * peer )
    106106{
    107     tr_info_t * inf = &tor->info;
    108 
    109     int i;
    110     int bitfieldSize = ( inf->pieceCount + 7 ) / 8;
    111     uint8_t * bitfield = tr_cpPieceBitfield( tor->completion );
     107    int ii;
     108    tr_bitfield_t * bitfield = tr_cpPieceBitfield( tor->completion );
    112109
    113110    if( !peer->bitfield )
     
    117114    }
    118115
    119     for( i = 0; i < bitfieldSize; i++ )
    120     {
    121         if( ( peer->bitfield[i] & ~(bitfield[i]) ) & 0xFF )
     116    assert( bitfield->len == peer->bitfield->len );
     117    for( ii = 0; ii < bitfield->len; ii++ )
     118    {
     119        if( ( peer->bitfield->bits[ii] & ~(bitfield->bits[ii]) ) & 0xFF )
    122120        {
    123121            return 1;
     
    198196        /* All pieces in 'pool' have 'minMissing' missing blocks. Find
    199197           the rarest ones. */
    200         uint8_t * bitfield;
     198        tr_bitfield_t * bitfield;
    201199        int piece;
    202200        int min, foo, j;
  • trunk/libtransmission/utils.h

    r1444 r1534  
    102102}
    103103
     104struct tr_bitfield_s
     105{
     106    uint8_t * bits;
     107    int       len;
     108};
     109
     110/* note that the argument is how many bits are needed, not bytes */
     111static inline tr_bitfield_t * tr_bitfieldNew( int bitcount )
     112{
     113    tr_bitfield_t * ret;
     114
     115    ret = calloc( 1, sizeof *ret );
     116    if( NULL == ret )
     117    {
     118        return NULL;
     119    }
     120    ret->len = ( bitcount + 7 ) / 8;
     121    ret->bits = calloc( ret->len, 1 );
     122    if( NULL == ret->bits )
     123    {
     124        free( ret );
     125        return NULL;
     126    }
     127
     128    return ret;
     129}
     130
     131static inline void tr_bitfieldFree( tr_bitfield_t * bitfield )
     132{
     133    if( bitfield )
     134    {
     135        free( bitfield->bits );
     136        free( bitfield );
     137    }
     138}
     139
     140static inline void tr_bitfieldClear( tr_bitfield_t * bitfield )
     141{
     142    memset( bitfield->bits, 0, bitfield->len );
     143}
     144
    104145/***********************************************************************
    105146 * tr_bitfieldHas
    106147 **********************************************************************/
    107 static inline int tr_bitfieldHas( uint8_t * bitfield, int piece )
    108 {
    109     return ( bitfield[ piece / 8 ] & ( 1 << ( 7 - ( piece % 8 ) ) ) );
     148static inline int tr_bitfieldHas( tr_bitfield_t * bitfield, int piece )
     149{
     150    assert( piece / 8 < bitfield->len );
     151    return ( bitfield->bits[ piece / 8 ] & ( 1 << ( 7 - ( piece % 8 ) ) ) );
    110152}
    111153
     
    113155 * tr_bitfieldAdd
    114156 **********************************************************************/
    115 static inline void tr_bitfieldAdd( uint8_t * bitfield, int piece )
    116 {
    117     bitfield[ piece / 8 ] |= ( 1 << ( 7 - ( piece % 8 ) ) );
    118 }
    119 
    120 static inline void tr_bitfieldRem( uint8_t * bitfield, int piece )
    121 {
    122     bitfield[ piece / 8 ] &= ~( 1 << ( 7 - ( piece % 8 ) ) );
     157static inline void tr_bitfieldAdd( tr_bitfield_t * bitfield, int piece )
     158{
     159    assert( piece / 8 < bitfield->len );
     160    bitfield->bits[ piece / 8 ] |= ( 1 << ( 7 - ( piece % 8 ) ) );
     161}
     162
     163static inline void tr_bitfieldRem( tr_bitfield_t * bitfield, int piece )
     164{
     165    assert( piece / 8 < bitfield->len );
     166    bitfield->bits[ piece / 8 ] &= ~( 1 << ( 7 - ( piece % 8 ) ) );
    123167}
    124168
Note: See TracChangeset for help on using the changeset viewer.