source: trunk/libtransmission/peermessages.h @ 1579

Last change on this file since 1579 was 1579, checked in by joshe, 15 years ago

Merge PEX branch, I hope this works.

  • Property svn:keywords set to Date Rev Author Id
File size: 12.0 KB
Line 
1/******************************************************************************
2 * $Id: peermessages.h 1579 2007-03-23 08:28:01Z joshe $
3 *
4 * Copyright (c) 2005-2007 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25/***********************************************************************
26 * This file handles all outgoing messages
27 **********************************************************************/
28
29#define PEER_MSG_CHOKE          0
30#define PEER_MSG_UNCHOKE        1
31#define PEER_MSG_INTERESTED     2
32#define PEER_MSG_UNINTERESTED   3
33#define PEER_MSG_HAVE           4
34#define PEER_MSG_BITFIELD       5
35#define PEER_MSG_REQUEST        6
36#define PEER_MSG_PIECE          7
37#define PEER_MSG_CANCEL         8
38#define PEER_MSG_PORT           9
39
40static uint8_t * messagesPending( tr_peer_t * peer, int * size )
41{
42    if( peer->outBlockSending || peer->outMessagesPos < 1 )
43    {
44        return NULL;
45    }
46
47    *size = MIN( peer->outMessagesPos, 1024 );
48
49    return peer->outMessages;
50}
51
52static void messagesSent( tr_peer_t * peer, int size )
53{
54    peer->outMessagesPos -= size;
55    memmove( peer->outMessages, &peer->outMessages[size],
56             peer->outMessagesPos );
57}
58
59static uint8_t * blockPending( tr_torrent_t * tor, tr_peer_t * peer,
60                               int * size )
61{
62    if( !peer->outBlockLoaded )
63    {
64        uint8_t * p;
65        tr_request_t * r;
66
67        if( peer->amChoking || peer->outRequestCount < 1 )
68        {
69            /* No piece to send */
70            return NULL;
71        }
72
73        /* We need to load the block for the next request */
74        r = &peer->outRequests[0];
75
76        /* Sanity check */
77        if( !tr_cpPieceIsComplete( tor->completion, r->index ) )
78        {
79            /* We have been asked for something we don't have, buggy client?
80               Let's just drop this request */
81            tr_inf( "Block %d/%d/%d was requested but we don't have it",
82                    r->index, r->begin, r->length );
83            (peer->outRequestCount)--;
84            memmove( &peer->outRequests[0], &peer->outRequests[1],
85                     peer->outRequestCount * sizeof( tr_request_t ) );
86            return NULL;
87        }
88       
89        p = (uint8_t *) peer->outBlock;
90
91        TR_HTONL( 9 + r->length, p );
92        p[4] = PEER_MSG_PIECE;
93        TR_HTONL( r->index, p + 5 );
94        TR_HTONL( r->begin, p + 9 );
95
96        tr_ioRead( tor->io, r->index, r->begin, r->length, &p[13] );
97
98        if( peer->outRequestCount < 1 )
99        {
100            /* We were choked during the read */
101            return NULL;
102        }
103
104        peer_dbg( "SEND piece %d/%d (%d bytes)",
105                  r->index, r->begin, r->length );
106
107        peer->outBlockSize   = 13 + r->length;
108        peer->outBlockLoaded = 1;
109
110        (peer->outRequestCount)--;
111        memmove( &peer->outRequests[0], &peer->outRequests[1],
112                 peer->outRequestCount * sizeof( tr_request_t ) );
113    }
114
115    *size = MIN( 1024, peer->outBlockSize );
116
117    return (uint8_t *) peer->outBlock;
118}
119
120static void blockSent( tr_peer_t * peer, int size )
121{
122    peer->outBlockSize -= size;
123    memmove( peer->outBlock, &peer->outBlock[size], peer->outBlockSize );
124
125    if( peer->outBlockSize > 0 )
126    {
127        /* We can't send messages until we are done sending the block */
128        peer->outBlockSending = 1;
129    }
130    else
131    {
132        /* Block fully sent */
133        peer->outBlockSending = 0;
134        peer->outBlockLoaded  = 0;
135    }
136}
137
138static uint8_t * getMessagePointer( tr_peer_t * peer, int size, int id )
139{
140    uint8_t * p;
141    int       index = 0;
142
143    size += 4;
144    if( peer->azproto )
145    {
146        index = azmsgIdIndex( id );
147        assert( 0 <= index );
148        size += 4 + azmsgLen( index ) + 1;
149    }
150    else if( 0 <= id )
151    {
152        size++;
153    }
154
155    if( peer->outMessagesPos + size > peer->outMessagesSize )
156    {
157        peer->outMessagesSize = peer->outMessagesPos + size;
158        peer->outMessages     = realloc( peer->outMessages,
159                                         peer->outMessagesSize );
160    }
161
162    p                     = &peer->outMessages[peer->outMessagesPos];
163    peer->outMessagesPos += size;
164
165    TR_HTONL( size - 4, p );
166    p += 4;
167    if( peer->azproto )
168    {
169        TR_HTONL( azmsgLen( index ), p );
170        memcpy( p + 4, azmsgStr( index ), azmsgLen( index ) );
171        p[ 4 + azmsgLen( index ) ] = AZ_EXT_VERSION;
172        p += 4 + azmsgLen( index ) + 1;
173    }
174    else if( 0 <= id )
175    {
176        *p = id;
177        p++;
178    }
179
180    return p;
181}
182
183/***********************************************************************
184 * sendKeepAlive
185 ***********************************************************************
186 *
187 **********************************************************************/
188static void sendKeepAlive( tr_peer_t * peer )
189{
190    getMessagePointer( peer, 0, AZ_MSG_BT_KEEP_ALIVE );
191
192    peer_dbg( "SEND keep-alive" );
193}
194
195
196/***********************************************************************
197 * sendChoke
198 ***********************************************************************
199 *
200 **********************************************************************/
201static void sendChoke( tr_peer_t * peer, int yes )
202{
203    int id;
204
205    id = ( yes ? PEER_MSG_CHOKE : PEER_MSG_UNCHOKE );
206    getMessagePointer( peer, 0, id );
207
208    peer->amChoking = yes;
209
210    if( !yes )
211    {
212        /* Drop older requests from the last time it was unchoked,
213           if any */
214        peer->outRequestCount = 0;
215    }
216
217    peer_dbg( "SEND %schoke", yes ? "" : "un" );
218}
219
220/***********************************************************************
221 * sendInterest
222 ***********************************************************************
223 *
224 **********************************************************************/
225static void sendInterest( tr_peer_t * peer, int yes )
226{
227    int id;
228
229    id = ( yes ? PEER_MSG_INTERESTED : PEER_MSG_UNINTERESTED );
230    getMessagePointer( peer, 0, id );
231
232    peer->amInterested = yes;
233
234    peer_dbg( "SEND %sinterested", yes ? "" : "un" );
235}
236
237/***********************************************************************
238 * sendHave
239 ***********************************************************************
240 *
241 **********************************************************************/
242static void sendHave( tr_peer_t * peer, int piece )
243{
244    uint8_t * p;
245
246    p = getMessagePointer( peer, 4, PEER_MSG_HAVE );
247
248    TR_HTONL( piece, p );
249
250    peer_dbg( "SEND have %d", piece );
251}
252
253/***********************************************************************
254 * sendBitfield
255 ***********************************************************************
256 * Builds a 'bitfield' message:
257 *  - size = 5 + X (4 bytes)
258 *  - id   = 5     (1 byte)
259 *  - bitfield     (X bytes)
260 **********************************************************************/
261static void sendBitfield( tr_torrent_t * tor, tr_peer_t * peer )
262{
263    uint8_t       * p;
264    tr_bitfield_t * bitfield;
265
266    bitfield = tr_cpPieceBitfield( tor->completion );
267    p = getMessagePointer( peer, bitfield->len, PEER_MSG_BITFIELD );
268
269    memcpy( p, bitfield->bits, bitfield->len );
270
271    peer_dbg( "SEND bitfield" );
272}
273
274/***********************************************************************
275 * sendRequest
276 ***********************************************************************
277 *
278 **********************************************************************/
279static void sendRequest( tr_torrent_t * tor, tr_peer_t * peer, int block )
280{
281    tr_info_t * inf = &tor->info;
282    tr_request_t * r;
283    uint8_t * p;
284
285    /* Get the piece the block is a part of, its position in the piece
286       and its size */
287    r         = &peer->inRequests[peer->inRequestCount];
288    r->index  = block / ( inf->pieceSize / tor->blockSize );
289    r->begin  = ( block % ( inf->pieceSize / tor->blockSize ) ) *
290                    tor->blockSize;
291    r->length = tr_blockSize( block );
292    (peer->inRequestCount)++;
293
294    /* Build the "ask" message */
295    p = getMessagePointer( peer, 12, PEER_MSG_REQUEST );
296
297    TR_HTONL( r->index,  p     );
298    TR_HTONL( r->begin,  p + 4 );
299    TR_HTONL( r->length, p + 8 );
300
301    tr_cpDownloaderAdd( tor->completion, block );
302
303    peer_dbg( "SEND request %d/%d (%d bytes)",
304              r->index, r->begin, r->length );
305}
306
307/***********************************************************************
308 * sendCancel
309 ***********************************************************************
310 *
311 **********************************************************************/
312static void sendCancel( tr_torrent_t * tor, int block )
313{
314    int i, j;
315    uint8_t * p;
316    tr_peer_t * peer;
317    tr_request_t * r;
318
319    for( i = 0; i < tor->peerCount; i++ )
320    {
321        peer = tor->peers[i];
322
323        for( j = 1; j < peer->inRequestCount; j++ )
324        {
325            r = &peer->inRequests[j];
326
327            if( block != tr_block( r->index, r->begin ) )
328            {
329                continue;
330            }
331
332            p = getMessagePointer( peer, 12, PEER_MSG_CANCEL );
333       
334            /* Build the "cancel" message */
335            TR_HTONL( r->index,  p     );
336            TR_HTONL( r->begin,  p + 4 );
337            TR_HTONL( r->length, p + 8 );
338
339            peer_dbg( "SEND cancel %d/%d (%d bytes)",
340                      r->index, r->begin, r->length );
341
342            (peer->inRequestCount)--;
343            memmove( &peer->inRequests[j], &peer->inRequests[j+1],
344                     ( peer->inRequestCount - j ) * sizeof( tr_request_t ) );
345            break;
346        }
347    }
348}
349
350/***********************************************************************
351 * sendExtended
352 ***********************************************************************
353 * Builds an extended message:
354 *  - size = 6 + X (4 bytes)
355 *  - id   = 20    (1 byte)
356 *  - eid  = Y     (1 byte)
357 *  - data         (X bytes)
358 **********************************************************************/
359static int sendExtended( tr_torrent_t * tor, tr_peer_t * peer, int id )
360{
361    uint8_t * p;
362    char    * buf;
363    int       len;
364
365    buf = NULL;
366    switch( id )
367    {
368        case EXTENDED_HANDSHAKE_ID:
369            buf = makeExtendedHandshake( tor, peer, &len );
370            break;
371        case EXTENDED_PEX_ID:
372            buf = makeUTPex( tor, peer, &len );
373            break;
374        default:
375            assert( 0 );
376            break;
377    }
378    if( NULL == buf )
379    {
380        return 1;
381    }
382
383    /* add header and queue it to be sent */
384    p = getMessagePointer( peer, 1 + len, PEER_MSG_EXTENDED );
385    p[0] = id;
386    memcpy( p + 1, buf, len );
387    free( buf );
388
389    return 0;
390}
391
392/***********************************************************************
393 * sendAZPex
394 ***********************************************************************
395 *
396 **********************************************************************/
397static int sendAZPex( tr_torrent_t * tor, tr_peer_t * peer )
398{
399    uint8_t * p;
400    char    * buf;
401    int       len;
402
403    buf = makeAZPex( tor, peer, &len );
404    if( NULL == buf )
405    {
406        return 1;
407    }
408
409    /* add header and queue it to be sent */
410    p = getMessagePointer( peer, len, AZ_MSG_AZ_PEER_EXCHANGE );
411    memcpy( p, buf, len );
412    free( buf );
413
414    return 0;
415}
Note: See TracBrowser for help on using the repository browser.