iPXE
Defines | Functions | Variables
peerblk.c File Reference

Peer Content Caching and Retrieval (PeerDist) protocol block downloads. More...

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <ipxe/http.h>
#include <ipxe/iobuf.h>
#include <ipxe/xfer.h>
#include <ipxe/uri.h>
#include <ipxe/timer.h>
#include <ipxe/profile.h>
#include <ipxe/fault.h>
#include <ipxe/pccrr.h>
#include <ipxe/peerblk.h>

Go to the source code of this file.

Defines

#define PEERBLK_DECRYPT_CHUNKSIZE   2048
 PeerDist decryption chunksize.
#define PEERBLK_RAW_OPEN_TIMEOUT   ( 10 * TICKS_PER_SEC )
 PeerDist raw block download attempt initial progress timeout.
#define PEERBLK_RAW_RX_TIMEOUT   ( 15 * TICKS_PER_SEC )
 PeerDist raw block download attempt ongoing progress timeout.
#define PEERBLK_RETRIEVAL_OPEN_TIMEOUT   ( 3 * TICKS_PER_SEC )
 PeerDist retrieval protocol block download attempt initial progress timeout.
#define PEERBLK_RETRIEVAL_RX_TIMEOUT   ( 5 * TICKS_PER_SEC )
 PeerDist retrieval protocol block download attempt ongoing progress timeout.
#define PEERBLK_MAX_ATTEMPT_CYCLES   4
 PeerDist maximum number of full download attempt cycles.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static unsigned long peerblk_timestamp (void)
 Get profiling timestamp.
static void peerblk_free (struct refcnt *refcnt)
 Free PeerDist block download.
static void peerblk_reset (struct peerdist_block *peerblk, int rc)
 Reset PeerDist block download attempt.
static void peerblk_close (struct peerdist_block *peerblk, int rc)
 Close PeerDist block download.
static size_t peerblk_offset (struct peerdist_block *peerblk, size_t pos)
 Calculate offset within overall download.
static int peerblk_deliver (struct peerdist_block *peerblk, struct io_buffer *iobuf, struct xfer_metadata *meta, size_t pos)
 Deliver download attempt data block.
static void peerblk_done (struct peerdist_block *peerblk, int rc)
 Finish PeerDist block download attempt.
static int peerblk_raw_open (struct peerdist_block *peerblk)
 Open PeerDist raw block download attempt.
static int peerblk_raw_rx (struct peerdist_block *peerblk, struct io_buffer *iobuf, struct xfer_metadata *meta)
 Receive PeerDist raw data.
static void peerblk_raw_close (struct peerdist_block *peerblk, int rc)
 Close PeerDist raw block download attempt.
static struct uripeerblk_retrieval_uri (const char *location)
 Construct PeerDist retrieval protocol URI.
static int peerblk_retrieval_open (struct peerdist_block *peerblk, const char *location)
 Open PeerDist retrieval protocol block download attempt.
static int peerblk_retrieval_rx (struct peerdist_block *peerblk, struct io_buffer *iobuf, struct xfer_metadata *meta)
 Receive PeerDist retrieval protocol data.
static int peerblk_parse_header (struct peerdist_block *peerblk)
 Parse retrieval protocol message header.
static int peerblk_parse_block (struct peerdist_block *peerblk, size_t *buf_len)
 Parse retrieval protocol message segment and block details.
static int peerblk_parse_useless (struct peerdist_block *peerblk, size_t buf_len, size_t *vrf_len)
 Parse retrieval protocol message useless details.
static int peerblk_parse_iv (struct peerdist_block *peerblk, size_t buf_len, size_t vrf_len)
 Parse retrieval protocol message initialisation vector details.
static int peerblk_decrypt_read (struct peerdist_block *peerblk, void *data, size_t len)
 Read from decryption buffers.
static int peerblk_decrypt_write (struct peerdist_block *peerblk, const void *data, size_t len)
 Write to decryption buffers and update offsets and lengths.
static void peerblk_decrypt (struct peerdist_block *peerblk)
 Decrypt one chunk of PeerDist retrieval protocol data.
static void peerblk_retrieval_close (struct peerdist_block *peerblk, int rc)
 Close PeerDist retrieval protocol block download attempt.
static void peerblk_expired (struct retry_timer *timer, int over __unused)
 Handle PeerDist retry timer expiry.
static void peerblk_discovered (struct peerdisc_client *discovery)
 Handle PeerDist peer discovery.
int peerblk_open (struct interface *xfer, struct uri *uri, struct peerdist_info_block *block)
 Open PeerDist block download.

Variables

static struct profiler
peerblk_download_profiler 
__profiler
 PeerDist block download profiler.
static struct interface_operation peerblk_xfer_operations []
 PeerDist block download data transfer interface operations.
static struct interface_descriptor peerblk_xfer_desc
 PeerDist block download data transfer interface descriptor.
static struct interface_operation peerblk_raw_operations []
 PeerDist block download raw data interface operations.
static struct interface_descriptor peerblk_raw_desc
 PeerDist block download raw data interface descriptor.
static struct interface_operation peerblk_retrieval_operations []
 PeerDist block download retrieval protocol interface operations.
static struct interface_descriptor peerblk_retrieval_desc
 PeerDist block download retrieval protocol interface descriptor.
static struct process_descriptor peerblk_process_desc
 PeerDist block download decryption process descriptor.
static struct
peerdisc_client_operations 
peerblk_discovery_operations
 PeerDist block download discovery operations.

Detailed Description

Peer Content Caching and Retrieval (PeerDist) protocol block downloads.

Definition in file peerblk.c.


Define Documentation

#define PEERBLK_DECRYPT_CHUNKSIZE   2048

PeerDist decryption chunksize.

This is a policy decision.

Definition at line 49 of file peerblk.c.

Referenced by peerblk_decrypt().

#define PEERBLK_RAW_OPEN_TIMEOUT   ( 10 * TICKS_PER_SEC )

PeerDist raw block download attempt initial progress timeout.

This is a policy decision.

Definition at line 55 of file peerblk.c.

Referenced by peerblk_expired().

#define PEERBLK_RAW_RX_TIMEOUT   ( 15 * TICKS_PER_SEC )

PeerDist raw block download attempt ongoing progress timeout.

This is a policy decision.

Definition at line 61 of file peerblk.c.

Referenced by peerblk_raw_rx().

PeerDist retrieval protocol block download attempt initial progress timeout.

This is a policy decision.

Definition at line 67 of file peerblk.c.

Referenced by peerblk_expired().

PeerDist retrieval protocol block download attempt ongoing progress timeout.

This is a policy decision.

Definition at line 73 of file peerblk.c.

Referenced by peerblk_retrieval_rx().

PeerDist maximum number of full download attempt cycles.

This is the maximum number of times that we will try a full cycle of download attempts (i.e. a retrieval protocol download attempt from each discovered peer plus a raw download attempt from the origin server).

This is a policy decision.

Definition at line 84 of file peerblk.c.

Referenced by peerblk_expired().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static unsigned long peerblk_timestamp ( void  ) [inline, static]

Get profiling timestamp.

Return values:
timestampTimestamp

Definition at line 116 of file peerblk.c.

References currticks().

Referenced by peerblk_close(), peerblk_discovered(), peerblk_done(), peerblk_expired(), and peerblk_open().

                           {

        if ( PROFILING ) {
                return currticks();
        } else {
                return 0;
        }
}
static void peerblk_free ( struct refcnt refcnt) [static]

Free PeerDist block download.

Parameters:
refcntReference count

Definition at line 130 of file peerblk.c.

References peerdist_block::cipherctx, container_of, free, peerdist_block::uri, and uri_put().

Referenced by peerblk_open().

                                                   {
        struct peerdist_block *peerblk =
                container_of ( refcnt, struct peerdist_block, refcnt );

        uri_put ( peerblk->uri );
        free ( peerblk->cipherctx );
        free ( peerblk );
}
static void peerblk_reset ( struct peerdist_block peerblk,
int  rc 
) [static]

Reset PeerDist block download attempt.

Parameters:
peerblkPeerDist block download
rcReason for reset

Definition at line 145 of file peerblk.c.

References assert, peerdist_block::buffer, peerdist_block::cipher, peerdist_block::cipherctx, peerdist_block::digest, digest_init(), peerdist_block::digestctx, peerdist_block::end, peerdist_range::end, free, intf_restart(), NULL, peerdist_block::pos, peerdist_block::process, process_del(), peerdist_block::range, peerdist_block::raw, peerdist_block::retrieval, peerdist_block::start, peerdist_range::start, stop_timer(), peerdist_block::timer, peerdist_block::trim, and xferbuf_free().

Referenced by peerblk_close(), peerblk_done(), and peerblk_expired().

                                                                     {

        /* Stop decryption process */
        process_del ( &peerblk->process );

        /* Stop timer */
        stop_timer ( &peerblk->timer );

        /* Abort any current download attempt */
        intf_restart ( &peerblk->raw, rc );
        intf_restart ( &peerblk->retrieval, rc );

        /* Empty received data buffer */
        xferbuf_free ( &peerblk->buffer );
        peerblk->pos = 0;

        /* Reset digest and free cipher context */
        digest_init ( peerblk->digest, peerblk->digestctx );
        free ( peerblk->cipherctx );
        peerblk->cipherctx = NULL;
        peerblk->cipher = NULL;

        /* Reset trim thresholds */
        peerblk->start = ( peerblk->trim.start - peerblk->range.start );
        peerblk->end = ( peerblk->trim.end - peerblk->range.start );
        assert ( peerblk->start <= peerblk->end );
}
static void peerblk_close ( struct peerdist_block peerblk,
int  rc 
) [static]

Close PeerDist block download.

Parameters:
peerblkPeerDist block download
rcReason for close

Definition at line 179 of file peerblk.c.

References peerdist_block::discovery, intf_shutdown(), peerblk_reset(), peerblk_timestamp(), peerdisc_close(), profile_custom(), peerdist_block::raw, peerdist_block::retrieval, peerdist_block::started, and peerdist_block::xfer.

Referenced by peerblk_done(), peerblk_expired(), and peerblk_open().

                                                                     {
        unsigned long now = peerblk_timestamp();

        /* Profile overall block download */
        profile_custom ( &peerblk_download_profiler,
                         ( now - peerblk->started ) );

        /* Reset download attempt */
        peerblk_reset ( peerblk, rc );

        /* Close discovery */
        peerdisc_close ( &peerblk->discovery );

        /* Shut down all interfaces */
        intf_shutdown ( &peerblk->retrieval, rc );
        intf_shutdown ( &peerblk->raw, rc );
        intf_shutdown ( &peerblk->xfer, rc );
}
static size_t peerblk_offset ( struct peerdist_block peerblk,
size_t  pos 
) [inline, static]

Calculate offset within overall download.

Parameters:
peerblkPeerDist block download
posPosition within incoming data stream
Return values:
offsetOffset within overall download

Definition at line 206 of file peerblk.c.

Referenced by peerblk_deliver(), and peerblk_parse_block().

                                                              {

        return ( ( pos - peerblk->start ) + peerblk->offset );
}
static int peerblk_deliver ( struct peerdist_block peerblk,
struct io_buffer iobuf,
struct xfer_metadata meta,
size_t  pos 
) [static]

Deliver download attempt data block.

Parameters:
peerblkPeerDist block download
iobufI/O buffer
metaOriginal data transfer metadata
posPosition within incoming data stream
Return values:
rcReturn status code

Definition at line 220 of file peerblk.c.

References peerdist_block::block, DBGC, peerdist_block::end, end, xfer_metadata::flags, free_iob(), iob_disown, iob_len(), iob_pull, iob_unput, len, memcpy(), xfer_metadata::offset, peerblk_offset(), rc, peerdist_block::segment, peerdist_block::start, start, strerror(), peerdist_block::xfer, xfer_deliver(), and XFER_FL_ABS_OFFSET.

Referenced by peerblk_raw_rx(), and peerblk_retrieval_rx().

                                                                      {
        struct xfer_metadata xfer_meta;
        size_t len = iob_len ( iobuf );
        size_t start = pos;
        size_t end = ( pos + len );
        int rc;

        /* Discard zero-length packets and packets which lie entirely
         * outside the trimmed range.
         */
        if ( ( start >= peerblk->end ) || ( end <= peerblk->start ) ||
             ( len == 0 ) ) {
                free_iob ( iobuf );
                return 0;
        }

        /* Truncate data to within trimmed range */
        if ( start < peerblk->start ) {
                iob_pull ( iobuf, ( peerblk->start - start ) );
                start = peerblk->start;
        }
        if ( end > peerblk->end ) {
                iob_unput ( iobuf, ( end - peerblk->end ) );
                end = peerblk->end;
        }

        /* Construct metadata */
        memcpy ( &xfer_meta, meta, sizeof ( xfer_meta ) );
        xfer_meta.flags |= XFER_FL_ABS_OFFSET;
        xfer_meta.offset = peerblk_offset ( peerblk, start );

        /* Deliver data */
        if ( ( rc = xfer_deliver ( &peerblk->xfer, iob_disown ( iobuf ),
                                   &xfer_meta ) ) != 0 ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d could not deliver data: %s\n",
                       peerblk, peerblk->segment, peerblk->block,
                       strerror ( rc ) );
                return rc;
        }

        return 0;
}
static void peerblk_done ( struct peerdist_block peerblk,
int  rc 
) [static]

Finish PeerDist block download attempt.

Parameters:
peerblkPeerDist block download
rcReason for close

Definition at line 271 of file peerblk.c.

References peerdist_block::attempted, peerdist_block::block, DBGC, DBGC_HDA, digest, peerdist_block::digest, digest_final(), peerdist_block::digestctx, digest_algorithm::digestsize, peerdist_block::digestsize, peerdist_block::discovery, EIO, peerdist_block::hash, hash, head, peerdisc_peer::list, list_entry, memcmp(), NULL, peerdist_block::peer, peerblk_close(), peerblk_reset(), peerblk_timestamp(), peerdisc_stat(), peerdisc_segment::peers, profile_custom(), peerdist_block::rc, rc, peerdisc_client::segment, peerdist_block::segment, segment, start_timer_nodelay(), strerror(), peerdist_block::timer, and peerdist_block::xfer.

Referenced by peerblk_decrypt(), peerblk_raw_close(), peerblk_raw_rx(), peerblk_retrieval_close(), and peerblk_retrieval_rx().

                                                                    {
        struct digest_algorithm *digest = peerblk->digest;
        struct peerdisc_segment *segment = peerblk->discovery.segment;
        struct peerdisc_peer *head;
        struct peerdisc_peer *peer;
        uint8_t hash[digest->digestsize];
        unsigned long now = peerblk_timestamp();

        /* Check for errors on completion */
        if ( rc != 0 ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d attempt failed: %s\n",
                       peerblk, peerblk->segment, peerblk->block,
                       strerror ( rc ) );
                goto err;
        }

        /* Check digest */
        digest_final ( digest, peerblk->digestctx, hash );
        if ( memcmp ( hash, peerblk->hash, peerblk->digestsize ) != 0 ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d digest mismatch:\n",
                       peerblk, peerblk->segment, peerblk->block );
                DBGC_HDA ( peerblk, 0, hash, peerblk->digestsize );
                DBGC_HDA ( peerblk, 0, peerblk->hash, peerblk->digestsize );
                rc = -EIO;
                goto err;
        }

        /* Profile successful attempt */
        profile_custom ( &peerblk_attempt_success_profiler,
                         ( now - peerblk->attempted ) );

        /* Report peer statistics */
        head = list_entry ( &segment->peers, struct peerdisc_peer, list );
        peer = ( ( peerblk->peer == head ) ? NULL : peerblk->peer );
        peerdisc_stat ( &peerblk->xfer, peer, &segment->peers );

        /* Close download */
        peerblk_close ( peerblk, 0 );
        return;

 err:
        /* Record failure reason and schedule a retry attempt */
        profile_custom ( &peerblk_attempt_failure_profiler,
                         ( now - peerblk->attempted ) );
        peerblk_reset ( peerblk, rc );
        peerblk->rc = rc;
        start_timer_nodelay ( &peerblk->timer );
}
static int peerblk_raw_open ( struct peerdist_block peerblk) [static]

Open PeerDist raw block download attempt.

Parameters:
peerblkPeerDist block download
Return values:
rcReturn status code

Definition at line 333 of file peerblk.c.

References peerdist_block::block, DBGC, DBGC2, peerdist_range::end, http_get, http_open(), intf_restart(), http_request_range::len, memset(), NULL, PEERBLK_ANNUL_RATE, peerdist_block::range, peerdist_block::raw, rc, peerdist_block::segment, http_request_range::start, peerdist_range::start, strerror(), and peerdist_block::uri.

Referenced by peerblk_expired().

                                                               {
        struct http_request_range range;
        int rc;

        DBGC2 ( peerblk, "PEERBLK %p %d.%d attempting raw range request\n",
                peerblk, peerblk->segment, peerblk->block );

        /* Construct HTTP range */
        memset ( &range, 0, sizeof ( range ) );
        range.start = peerblk->range.start;
        range.len = ( peerblk->range.end - peerblk->range.start );

        /* Initiate range request to retrieve block */
        if ( ( rc = http_open ( &peerblk->raw, &http_get, peerblk->uri,
                                &range, NULL ) ) != 0 ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d could not create range "
                       "request: %s\n", peerblk, peerblk->segment,
                       peerblk->block, strerror ( rc ) );
                return rc;
        }

        /* Annul HTTP connection (for testing) if applicable.  Do not
         * report as an immediate error, in order to test our ability
         * to recover from a totally unresponsive HTTP server.
         */
        if ( inject_fault ( PEERBLK_ANNUL_RATE ) )
                intf_restart ( &peerblk->raw, 0 );

        return 0;
}
static int peerblk_raw_rx ( struct peerdist_block peerblk,
struct io_buffer iobuf,
struct xfer_metadata meta 
) [static]

Receive PeerDist raw data.

Parameters:
peerblkPeerDist block download
iobufI/O buffer
metaData transfer metadata
Return values:
rcReturn status code

Definition at line 372 of file peerblk.c.

References io_buffer::data, peerdist_block::digest, digest_update(), peerdist_block::digestctx, peerdist_range::end, free_iob(), intf_restart(), iob_disown, iob_len(), len, mid, PEERBLK_CORRUPT_RATE, peerblk_deliver(), peerblk_done(), PEERBLK_RAW_RX_TIMEOUT, PEERBLK_STALL_RATE, peerdist_block::pos, peerdist_block::range, peerdist_block::raw, rc, peerdist_range::start, start_timer_fixed(), peerdist_block::timer, and xfer_check_order().

                                                         {
        size_t len = iob_len ( iobuf );
        size_t pos = peerblk->pos;
        size_t mid = ( ( peerblk->range.end - peerblk->range.start ) / 2 );
        int rc;

        /* Corrupt received data (for testing) if applicable */
        inject_corruption ( PEERBLK_CORRUPT_RATE, iobuf->data, len );

        /* Fail if data is delivered out of order, since the streaming
         * digest requires strict ordering.
         */
        if ( ( rc = xfer_check_order ( meta, &peerblk->pos, len ) ) != 0 )
                goto err;

        /* Add data to digest */
        digest_update ( peerblk->digest, peerblk->digestctx, iobuf->data, len );

        /* Deliver data */
        if ( ( rc = peerblk_deliver ( peerblk, iob_disown ( iobuf ), meta,
                                      pos ) ) != 0 )
                goto err;

        /* Extend download attempt timer */
        start_timer_fixed ( &peerblk->timer, PEERBLK_RAW_RX_TIMEOUT );

        /* Stall download attempt (for testing) if applicable */
        if ( ( pos < mid ) && ( ( pos + len ) >= mid ) &&
             ( ( rc = inject_fault ( PEERBLK_STALL_RATE ) ) != 0 ) ) {
                intf_restart ( &peerblk->raw, rc );
        }

        return 0;

 err:
        free_iob ( iobuf );
        peerblk_done ( peerblk, rc );
        return rc;
}
static void peerblk_raw_close ( struct peerdist_block peerblk,
int  rc 
) [static]

Close PeerDist raw block download attempt.

Parameters:
peerblkPeerDist block download
rcReason for close

Definition at line 420 of file peerblk.c.

References done, intf_restart(), PEERBLK_ABORT_RATE, peerblk_done(), and peerdist_block::raw.

                                                                         {

        /* Restart interface */
        intf_restart ( &peerblk->raw, rc );

        /* Fail immediately if we have an error */
        if ( rc != 0 )
                goto done;

        /* Abort download attempt (for testing) if applicable */
        if ( ( rc = inject_fault ( PEERBLK_ABORT_RATE ) ) != 0 )
                goto done;

 done:
        /* Complete download attempt */
        peerblk_done ( peerblk, rc );
}
static struct uri* peerblk_retrieval_uri ( const char *  location) [static, read]

Construct PeerDist retrieval protocol URI.

Parameters:
locationPeer location
Return values:
uriRetrieval URI, or NULL on error

Definition at line 451 of file peerblk.c.

References parse_uri(), PEERDIST_MAGIC_PATH, snprintf(), and strlen().

Referenced by peerblk_retrieval_open().

                                                                   {
        char uri_string[ 7 /* "http://" */ + strlen ( location ) +
                         sizeof ( PEERDIST_MAGIC_PATH /* includes NUL */ ) ];

        /* Construct URI string */
        snprintf ( uri_string, sizeof ( uri_string ),
                   ( "http://%s" PEERDIST_MAGIC_PATH ), location );

        /* Parse URI string */
        return parse_uri ( uri_string );
}
static int peerblk_retrieval_open ( struct peerdist_block peerblk,
const char *  location 
) [static]

Open PeerDist retrieval protocol block download attempt.

Parameters:
peerblkPeerDist block download
locationPeer location
Return values:
rcReturn status code

Definition at line 470 of file peerblk.c.

References peerdist_block::block, http_request_content::data, DBGC, DBGC2, digestsize, peerdist_block::digestsize, peerdist_block::end, ENOMEM, htonl, http_open(), http_post, peerdist_block::id, intf_restart(), http_request_content::len, memcpy(), memset(), msg(), NULL, offsetof, PEERBLK_ANNUL_RATE, peerblk_msg_blk_t, peerblk_retrieval_uri(), PEERDIST_MSG_AES_128_CBC, peerdist_msg_getblks_t, PEERDIST_MSG_GETBLKS_TYPE, PEERDIST_MSG_GETBLKS_VERSION, rc, peerdist_block::retrieval, rsp, peerdist_block::segment, peerdist_block::start, strerror(), and uri_put().

Referenced by peerblk_expired().

                                                           {
        size_t digestsize = peerblk->digestsize;
        peerdist_msg_getblks_t ( digestsize, 1, 0 ) req;
        peerblk_msg_blk_t ( digestsize, 0, 0, 0 ) *rsp;
        struct http_request_content content;
        struct uri *uri;
        int rc;

        DBGC2 ( peerblk, "PEERBLK %p %d.%d attempting retrieval from %s\n",
                peerblk, peerblk->segment, peerblk->block, location );

        /* Construct block fetch request */
        memset ( &req, 0, sizeof ( req ) );
        req.getblks.hdr.version.raw = htonl ( PEERDIST_MSG_GETBLKS_VERSION );
        req.getblks.hdr.type = htonl ( PEERDIST_MSG_GETBLKS_TYPE );
        req.getblks.hdr.len = htonl ( sizeof ( req ) );
        req.getblks.hdr.algorithm = htonl ( PEERDIST_MSG_AES_128_CBC );
        req.segment.segment.digestsize = htonl ( digestsize );
        memcpy ( req.segment.id, peerblk->id, digestsize );
        req.ranges.ranges.count = htonl ( 1 );
        req.ranges.range[0].first = htonl ( peerblk->block );
        req.ranges.range[0].count = htonl ( 1 );

        /* Construct POST request content */
        memset ( &content, 0, sizeof ( content ) );
        content.data = &req;
        content.len = sizeof ( req );

        /* Construct URI */
        if ( ( uri = peerblk_retrieval_uri ( location ) ) == NULL ) {
                rc = -ENOMEM;
                goto err_uri;
        }

        /* Update trim thresholds */
        peerblk->start += offsetof ( typeof ( *rsp ), msg.vrf );
        peerblk->end += offsetof ( typeof ( *rsp ), msg.vrf );

        /* Initiate HTTP POST to retrieve block */
        if ( ( rc = http_open ( &peerblk->retrieval, &http_post, uri,
                                NULL, &content ) ) != 0 ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d could not create retrieval "
                       "request: %s\n", peerblk, peerblk->segment,
                       peerblk->block, strerror ( rc ) );
                goto err_open;
        }

        /* Annul HTTP connection (for testing) if applicable.  Do not
         * report as an immediate error, in order to test our ability
         * to recover from a totally unresponsive HTTP server.
         */
        if ( inject_fault ( PEERBLK_ANNUL_RATE ) )
                intf_restart ( &peerblk->retrieval, 0 );

 err_open:
        uri_put ( uri );
 err_uri:
        return rc;
}
static int peerblk_retrieval_rx ( struct peerdist_block peerblk,
struct io_buffer iobuf,
struct xfer_metadata meta 
) [static]

Receive PeerDist retrieval protocol data.

Parameters:
peerblkPeerDist block download
iobufI/O buffer
metaData transfer metadata
Return values:
rcReturn status code

Definition at line 539 of file peerblk.c.

References peerdist_block::block, peerdist_block::buffer, io_buffer::data, DBGC, peerdist_block::end, end, xfer_metadata::flags, free_iob(), intf_restart(), iob_disown, iob_len(), len, xfer_metadata::offset, PEERBLK_CORRUPT_RATE, peerblk_deliver(), peerblk_done(), PEERBLK_RETRIEVAL_RX_TIMEOUT, PEERBLK_STALL_RATE, peerdist_block::pos, rc, peerdist_block::retrieval, peerdist_block::segment, peerdist_block::start, start, start_timer_fixed(), strerror(), peerdist_block::timer, XFER_FL_ABS_OFFSET, and xferbuf_write().

                                                               {
        size_t len = iob_len ( iobuf );
        size_t start;
        size_t end;
        size_t before;
        size_t after;
        size_t cut;
        int rc;

        /* Some genius at Microsoft thought it would be a great idea
         * to place the AES-CBC initialisation vector *after* the
         * encrypted data, thereby making it logically impossible to
         * decrypt each packet as it arrives.
         *
         * To work around this mindless stupidity, we deliver the
         * ciphertext as-is and later use xfer_buffer() to obtain
         * access to the underlying data transfer buffer in order to
         * perform the decryption.
         *
         * There will be some data both before and after the bytes
         * corresponding to the trimmed plaintext: a MSG_BLK
         * header/footer, some block padding for the AES-CBC cipher,
         * and a possibly large quantity of unwanted ciphertext which
         * is excluded from the trimmed content range.  We store this
         * data in a local data transfer buffer.  If the amount of
         * data to be stored is too large, we will fail allocation and
         * so eventually fall back to using a range request (which
         * does not require this kind of temporary storage
         * allocation).
         */

        /* Corrupt received data (for testing) if applicable */
        inject_corruption ( PEERBLK_CORRUPT_RATE, iobuf->data, len );

        /* Calculate start and end positions of this buffer */
        start = peerblk->pos;
        if ( meta->flags & XFER_FL_ABS_OFFSET )
                start = 0;
        start += meta->offset;
        end = ( start + len );

        /* Buffer any data before the trimmed content */
        if ( ( start < peerblk->start ) && ( len > 0 ) ) {

                /* Calculate length of data before the trimmed content */
                before = ( peerblk->start - start );
                if ( before > len )
                        before = len;

                /* Buffer data before the trimmed content */
                if ( ( rc = xferbuf_write ( &peerblk->buffer, start,
                                            iobuf->data, before ) ) != 0 ) {
                        DBGC ( peerblk, "PEERBLK %p %d.%d could not buffer "
                               "data: %s\n", peerblk, peerblk->segment,
                               peerblk->block, strerror ( rc ) );
                        goto err;
                }
        }

        /* Buffer any data after the trimmed content */
        if ( ( end > peerblk->end ) && ( len > 0 ) ) {

                /* Calculate length of data after the trimmed content */
                after = ( end - peerblk->end );
                if ( after > len )
                        after = len;

                /* Buffer data after the trimmed content */
                cut = ( peerblk->end - peerblk->start );
                if ( ( rc = xferbuf_write ( &peerblk->buffer,
                                            ( end - after - cut ),
                                            ( iobuf->data + len - after ),
                                            after ) ) != 0 ) {
                        DBGC ( peerblk, "PEERBLK %p %d.%d could not buffer "
                               "data: %s\n", peerblk, peerblk->segment,
                               peerblk->block, strerror ( rc ) );
                        goto err;
                }
        }

        /* Deliver any remaining data */
        if ( ( rc = peerblk_deliver ( peerblk, iob_disown ( iobuf ), meta,
                                      start ) ) != 0 )
                goto err;

        /* Update position */
        peerblk->pos = end;

        /* Extend download attempt timer */
        start_timer_fixed ( &peerblk->timer, PEERBLK_RETRIEVAL_RX_TIMEOUT );

        /* Stall download attempt (for testing) if applicable */
        if ( ( start < peerblk->end ) && ( end >= peerblk->end ) &&
             ( ( rc = inject_fault ( PEERBLK_STALL_RATE ) ) != 0 ) ) {
                intf_restart ( &peerblk->retrieval, rc );
        }

        return 0;

 err:
        free_iob ( iobuf );
        peerblk_done ( peerblk, rc );
        return rc;
}
static int peerblk_parse_header ( struct peerdist_block peerblk) [static]

Parse retrieval protocol message header.

Parameters:
peerblkPeerDist block download
Return values:
rcReturn status code

Definition at line 652 of file peerblk.c.

References __attribute__, aes_cbc_algorithm, assert, peerdist_block::block, peerdist_block::buffer, peerdist_block::cipher, cipher_setkey(), peerdist_block::cipherctx, cipher_algorithm::ctxsize, xfer_buffer::data, DBGC, DBGC2, peerdist_block::digestsize, ENOMEM, ENOTSUP, EPROTO, ERANGE, htonl, xfer_buffer::len, len, malloc(), msg(), cipher_algorithm::name, ntohl, NULL, PEERDIST_MSG_AES_128_CBC, PEERDIST_MSG_AES_192_CBC, PEERDIST_MSG_AES_256_CBC, PEERDIST_MSG_BLK_TYPE, PEERDIST_MSG_PLAINTEXT, rc, peerdist_block::secret, peerdist_block::segment, and strerror().

Referenced by peerblk_retrieval_close().

                                                                   {
        struct {
                struct peerdist_msg_transport_header hdr;
                struct peerdist_msg_header msg;
        } __attribute__ (( packed )) *msg = peerblk->buffer.data;
        struct cipher_algorithm *cipher;
        size_t len = peerblk->buffer.len;
        size_t keylen = 0;
        int rc;

        /* Check message length */
        if ( len < sizeof ( *msg ) ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d message too short for header "
                       "(%zd bytes)\n", peerblk, peerblk->segment,
                       peerblk->block, len );
                return -ERANGE;
        }

        /* Check message type */
        if ( msg->msg.type != htonl ( PEERDIST_MSG_BLK_TYPE ) ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d unexpected message type "
                       "%#08x\n", peerblk, peerblk->segment, peerblk->block,
                       ntohl ( msg->msg.type ) );
                return -EPROTO;
        }

        /* Determine cipher algorithm and key length */
        cipher = &aes_cbc_algorithm;
        switch ( msg->msg.algorithm ) {
        case htonl ( PEERDIST_MSG_PLAINTEXT ) :
                cipher = NULL;
                break;
        case htonl ( PEERDIST_MSG_AES_128_CBC ) :
                keylen = ( 128 / 8 );
                break;
        case htonl ( PEERDIST_MSG_AES_192_CBC ) :
                keylen = ( 192 / 8 );
                break;
        case htonl ( PEERDIST_MSG_AES_256_CBC ) :
                keylen = ( 256 / 8 );
                break;
        default:
                DBGC ( peerblk, "PEERBLK %p %d.%d unrecognised algorithm "
                       "%#08x\n", peerblk, peerblk->segment, peerblk->block,
                       ntohl ( msg->msg.algorithm ) );
                return -ENOTSUP;
        }
        DBGC2 ( peerblk, "PEERBLK %p %d.%d using %s with %zd-bit key\n",
                peerblk, peerblk->segment, peerblk->block,
                ( cipher ? cipher->name : "plaintext" ), ( 8 * keylen ) );

        /* Sanity check key length against maximum secret length */
        if ( keylen > peerblk->digestsize ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d %zd-byte secret too short "
                       "for %zd-bit key\n", peerblk, peerblk->segment,
                       peerblk->block, peerblk->digestsize, ( 8 * keylen ) );
                return -EPROTO;
        }

        /* Allocate cipher context, if applicable.  Freeing the cipher
         * context (on error or otherwise) is handled by peerblk_reset().
         */
        peerblk->cipher = cipher;
        assert ( peerblk->cipherctx == NULL );
        if ( cipher ) {
                peerblk->cipherctx = malloc ( cipher->ctxsize );
                if ( ! peerblk->cipherctx )
                        return -ENOMEM;
        }

        /* Initialise cipher, if applicable */
        if ( cipher &&
             ( rc = cipher_setkey ( cipher, peerblk->cipherctx, peerblk->secret,
                                    keylen ) ) != 0 ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d could not set key: %s\n",
                       peerblk, peerblk->segment, peerblk->block,
                       strerror ( rc ) );
                return rc;
        }

        return 0;
}
static int peerblk_parse_block ( struct peerdist_block peerblk,
size_t buf_len 
) [static]

Parse retrieval protocol message segment and block details.

Parameters:
peerblkPeerDist block download
buf_lenLength of buffered data to fill in
Return values:
rcReturn status code

Definition at line 742 of file peerblk.c.

References assert, be32_to_cpu, peerdist_block::block, peerdist_block::buffer, peerdist_block::cipher_remaining, xfer_buffer::data, DBGC, peerdist_block::decrypt, peerdist_block::digest_remaining, digestsize, peerdist_block::digestsize, peerdist_block::end, peerdist_range::end, ENOENT, EPROTO, ERANGE, peerdist_block::id, xfer_buffer::len, peerdist_block_decrypt::len, len, memcmp(), msg(), ntohl, peerdist_block_decrypt::offset, offsetof, PEERBLK_AFTER, PEERBLK_BEFORE, PEERBLK_DURING, peerblk_msg_blk_t, peerblk_offset(), peerdist_block::range, peerdist_block::segment, peerdist_block::start, peerdist_range::start, total, and peerdist_block_decrypt::xferbuf.

Referenced by peerblk_retrieval_close().

                                                   {
        size_t digestsize = peerblk->digestsize;
        peerblk_msg_blk_t ( digestsize, 0, 0, 0 ) *msg = peerblk->buffer.data;
        size_t len = peerblk->buffer.len;
        size_t data_len;
        size_t total;

        /* Check message length */
        if ( len < offsetof ( typeof ( *msg ), msg.block.data ) ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d message too short for "
                       "zero-length data (%zd bytes)\n", peerblk,
                       peerblk->segment, peerblk->block, len );
                return -ERANGE;
        }

        /* Check digest size */
        if ( ntohl ( msg->msg.segment.segment.digestsize ) != digestsize ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d incorrect digest size %d\n",
                       peerblk, peerblk->segment, peerblk->block,
                       ntohl ( msg->msg.segment.segment.digestsize ) );
                return -EPROTO;
        }

        /* Check segment ID */
        if ( memcmp ( msg->msg.segment.id, peerblk->id, digestsize ) != 0 ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d segment ID mismatch\n",
                       peerblk, peerblk->segment, peerblk->block );
                return -EPROTO;
        }

        /* Check block ID */
        if ( ntohl ( msg->msg.index ) != peerblk->block ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d block ID mismatch (got %d)\n",
                       peerblk, peerblk->segment, peerblk->block,
                       ntohl ( msg->msg.index ) );
                return -EPROTO;
        }

        /* Check for missing blocks */
        data_len = be32_to_cpu ( msg->msg.block.block.len );
        if ( ! data_len ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d block not found\n",
                       peerblk, peerblk->segment, peerblk->block );
                return -ENOENT;
        }

        /* Check for underlength blocks */
        if ( data_len < ( peerblk->range.end - peerblk->range.start ) ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d underlength block (%zd "
                       "bytes)\n", peerblk, peerblk->segment, peerblk->block,
                       data_len );
                return -ERANGE;
        }

        /* Calculate buffered data length (i.e. excluding data which
         * was delivered to the final data transfer buffer).
         */
        *buf_len = ( data_len - ( peerblk->end - peerblk->start ) );

        /* Describe data before the trimmed content */
        peerblk->decrypt[PEERBLK_BEFORE].xferbuf = &peerblk->buffer;
        peerblk->decrypt[PEERBLK_BEFORE].offset =
                offsetof ( typeof ( *msg ), msg.block.data );
        peerblk->decrypt[PEERBLK_BEFORE].len =
                ( peerblk->start -
                  offsetof ( typeof ( *msg ), msg.block.data ) );
        total = peerblk->decrypt[PEERBLK_BEFORE].len;

        /* Describe data within the trimmed content */
        peerblk->decrypt[PEERBLK_DURING].offset =
                peerblk_offset ( peerblk, peerblk->start );
        peerblk->decrypt[PEERBLK_DURING].len =
                ( peerblk->end - peerblk->start );
        total += peerblk->decrypt[PEERBLK_DURING].len;

        /* Describe data after the trimmed content */
        peerblk->decrypt[PEERBLK_AFTER].xferbuf = &peerblk->buffer;
        peerblk->decrypt[PEERBLK_AFTER].offset = peerblk->start;
        peerblk->decrypt[PEERBLK_AFTER].len =
                ( offsetof ( typeof ( *msg ), msg.block.data )
                  + *buf_len - peerblk->start );
        total += peerblk->decrypt[PEERBLK_AFTER].len;

        /* Sanity check */
        assert ( total == be32_to_cpu ( msg->msg.block.block.len ) );

        /* Initialise cipher and digest lengths */
        peerblk->cipher_remaining = total;
        peerblk->digest_remaining =
                ( peerblk->range.end - peerblk->range.start );
        assert ( peerblk->cipher_remaining >= peerblk->digest_remaining );

        return 0;
}
static int peerblk_parse_useless ( struct peerdist_block peerblk,
size_t  buf_len,
size_t vrf_len 
) [static]

Parse retrieval protocol message useless details.

Parameters:
peerblkPeerDist block download
buf_lenLength of buffered data
vrf_lenLength of uselessness to fill in
Return values:
rcReturn status code

Definition at line 846 of file peerblk.c.

References be32_to_cpu, peerdist_block::block, peerdist_block::buffer, xfer_buffer::data, DBGC, digestsize, peerdist_block::digestsize, ERANGE, xfer_buffer::len, len, msg(), offsetof, peerblk_msg_blk_t, and peerdist_block::segment.

Referenced by peerblk_retrieval_close().

                                                                     {
        size_t digestsize = peerblk->digestsize;
        peerblk_msg_blk_t ( digestsize, buf_len, 0, 0 ) *msg =
                peerblk->buffer.data;
        size_t len = peerblk->buffer.len;

        /* Check message length */
        if ( len < offsetof ( typeof ( *msg ), msg.vrf.data ) ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d message too short for "
                       "zero-length uselessness (%zd bytes)\n", peerblk,
                       peerblk->segment, peerblk->block, len );
                return -ERANGE;
        }

        /* Extract length of uselessness */
        *vrf_len = be32_to_cpu ( msg->msg.vrf.vrf.len );

        return 0;
}
static int peerblk_parse_iv ( struct peerdist_block peerblk,
size_t  buf_len,
size_t  vrf_len 
) [static]

Parse retrieval protocol message initialisation vector details.

Parameters:
peerblkPeerDist block download
buf_lenLength of buffered data
vrf_lenLength of uselessness
Return values:
rcReturn status code

Definition at line 875 of file peerblk.c.

References blksize, peerdist_block::block, cipher_algorithm::blocksize, peerdist_block::buffer, peerdist_block::cipher, cipher_setiv(), peerdist_block::cipherctx, xfer_buffer::data, DBGC, digestsize, peerdist_block::digestsize, EPROTO, ERANGE, xfer_buffer::len, len, msg(), ntohl, peerblk_msg_blk_t, and peerdist_block::segment.

Referenced by peerblk_retrieval_close().

                                               {
        size_t digestsize = peerblk->digestsize;
        size_t blksize = peerblk->cipher->blocksize;
        peerblk_msg_blk_t ( digestsize, buf_len, vrf_len, blksize ) *msg =
                peerblk->buffer.data;
        size_t len = peerblk->buffer.len;

        /* Check message length */
        if ( len < sizeof ( *msg ) ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d message too short for "
                       "initialisation vector (%zd bytes)\n", peerblk,
                       peerblk->segment, peerblk->block, len );
                return -ERANGE;
        }

        /* Check initialisation vector size */
        if ( ntohl ( msg->msg.iv.iv.blksize ) != blksize ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d incorrect IV size %d\n",
                       peerblk, peerblk->segment, peerblk->block,
                       ntohl ( msg->msg.iv.iv.blksize ) );
                return -EPROTO;
        }

        /* Set initialisation vector */
        cipher_setiv ( peerblk->cipher, peerblk->cipherctx, msg->msg.iv.data );

        return 0;
}
static int peerblk_decrypt_read ( struct peerdist_block peerblk,
void *  data,
size_t  len 
) [static]

Read from decryption buffers.

Parameters:
peerblkPeerDist block download
dataData buffer
lenLength to read
Return values:
rcReturn status code

Definition at line 913 of file peerblk.c.

References peerdist_block::decrypt, peerdist_block_decrypt::len, len, peerdist_block_decrypt::offset, rc, peerdist_block_decrypt::xferbuf, and xferbuf_read().

Referenced by peerblk_decrypt().

                                                           {
        struct peerdist_block_decrypt *decrypt = peerblk->decrypt;
        size_t frag_len;
        int rc;

        /* Read from each decryption buffer in turn */
        for ( ; len ; decrypt++, data += frag_len, len -= frag_len ) {

                /* Calculate length to use from this buffer */
                frag_len = decrypt->len;
                if ( frag_len > len )
                        frag_len = len;
                if ( ! frag_len )
                        continue;

                /* Read from this buffer */
                if ( ( rc = xferbuf_read ( decrypt->xferbuf, decrypt->offset,
                                           data, frag_len ) ) != 0 )
                        return rc;
        }

        return 0;
}
static int peerblk_decrypt_write ( struct peerdist_block peerblk,
const void *  data,
size_t  len 
) [static]

Write to decryption buffers and update offsets and lengths.

Parameters:
peerblkPeerDist block download
dataData buffer
lenLength to read
Return values:
rcReturn status code

Definition at line 946 of file peerblk.c.

References peerdist_block::decrypt, peerdist_block_decrypt::len, len, peerdist_block_decrypt::offset, rc, peerdist_block_decrypt::xferbuf, and xferbuf_write().

Referenced by peerblk_decrypt().

                                                                  {
        struct peerdist_block_decrypt *decrypt = peerblk->decrypt;
        size_t frag_len;
        int rc;

        /* Write to each decryption buffer in turn */
        for ( ; len ; decrypt++, data += frag_len, len -= frag_len ) {

                /* Calculate length to use from this buffer */
                frag_len = decrypt->len;
                if ( frag_len > len )
                        frag_len = len;
                if ( ! frag_len )
                        continue;

                /* Write to this buffer */
                if ( ( rc = xferbuf_write ( decrypt->xferbuf, decrypt->offset,
                                            data, frag_len ) ) != 0 )
                        return rc;

                /* Update offset and length */
                decrypt->offset += frag_len;
                decrypt->len -= frag_len;
        }

        return 0;
}
static void peerblk_decrypt ( struct peerdist_block peerblk) [static]

Decrypt one chunk of PeerDist retrieval protocol data.

Parameters:
peerblkPeerDist block download

Definition at line 980 of file peerblk.c.

References assert, peerdist_block::block, cipher_algorithm::blocksize, peerdist_block::cipher, cipher_decrypt, peerdist_block::cipher_remaining, peerdist_block::cipherctx, data, DBGC, peerdist_block::decrypt, digest, peerdist_block::digest, peerdist_block::digest_remaining, digest_update(), peerdist_block::digestctx, ENOMEM, ENOTSUP, free, malloc(), PEERBLK_DECRYPT_CHUNKSIZE, peerblk_decrypt_read(), peerblk_decrypt_write(), peerblk_done(), PEERBLK_DURING, rc, peerdist_block::segment, strerror(), peerdist_block::xfer, xfer_buffer(), and peerdist_block_decrypt::xferbuf.

                                                               {
        struct cipher_algorithm *cipher = peerblk->cipher;
        struct digest_algorithm *digest = peerblk->digest;
        struct xfer_buffer *xferbuf;
        size_t cipher_len;
        size_t digest_len;
        void *data;
        int rc;

        /* Sanity check */
        assert ( ( PEERBLK_DECRYPT_CHUNKSIZE % cipher->blocksize ) == 0 );

        /* Get the underlying data transfer buffer */
        xferbuf = xfer_buffer ( &peerblk->xfer );
        if ( ! xferbuf ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d has no underlying data "
                       "transfer buffer\n", peerblk, peerblk->segment,
                       peerblk->block );
                rc = -ENOTSUP;
                goto err_xfer_buffer;
        }
        peerblk->decrypt[PEERBLK_DURING].xferbuf = xferbuf;

        /* Calculate cipher and digest lengths */
        cipher_len = PEERBLK_DECRYPT_CHUNKSIZE;
        if ( cipher_len > peerblk->cipher_remaining )
                cipher_len = peerblk->cipher_remaining;
        digest_len = cipher_len;
        if ( digest_len > peerblk->digest_remaining )
                digest_len = peerblk->digest_remaining;
        assert ( ( cipher_len & ( cipher->blocksize - 1 ) ) == 0 );

        /* Allocate temporary data buffer */
        data = malloc ( cipher_len );
        if ( ! data ) {
                rc = -ENOMEM;
                goto err_alloc_data;
        }

        /* Read ciphertext */
        if ( ( rc = peerblk_decrypt_read ( peerblk, data, cipher_len ) ) != 0 ){
                DBGC ( peerblk, "PEERBLK %p %d.%d could not read ciphertext: "
                       "%s\n", peerblk, peerblk->segment, peerblk->block,
                       strerror ( rc ) );
                goto err_read;
        }

        /* Decrypt data */
        cipher_decrypt ( cipher, peerblk->cipherctx, data, data, cipher_len );

        /* Add data to digest */
        digest_update ( digest, peerblk->digestctx, data, digest_len );

        /* Write plaintext */
        if ( ( rc = peerblk_decrypt_write ( peerblk, data, cipher_len ) ) != 0){
                DBGC ( peerblk, "PEERBLK %p %d.%d could not write plaintext: "
                       "%s\n", peerblk, peerblk->segment, peerblk->block,
                       strerror ( rc ) );
                goto err_write;
        }

        /* Consume input */
        peerblk->cipher_remaining -= cipher_len;
        peerblk->digest_remaining -= digest_len;

        /* Free temporary data buffer */
        free ( data );

        /* Continue processing until all input is consumed */
        if ( peerblk->cipher_remaining )
                return;

        /* Complete download attempt */
        peerblk_done ( peerblk, 0 );
        return;

 err_write:
 err_read:
        free ( data );
 err_alloc_data:
 err_xfer_buffer:
        peerblk_done ( peerblk, rc );
}
static void peerblk_retrieval_close ( struct peerdist_block peerblk,
int  rc 
) [static]

Close PeerDist retrieval protocol block download attempt.

Parameters:
peerblkPeerDist block download
rcReason for close

Definition at line 1070 of file peerblk.c.

References peerdist_block::block, cipher_algorithm::blocksize, peerdist_block::cipher, peerdist_block::cipher_remaining, DBGC, done, EPROTO, intf_restart(), PEERBLK_ABORT_RATE, peerblk_done(), peerblk_parse_block(), peerblk_parse_header(), peerblk_parse_iv(), peerblk_parse_useless(), peerdist_block::process, process_add(), peerdist_block::retrieval, peerdist_block::segment, stop_timer(), and peerdist_block::timer.

                                                                               {
        size_t buf_len;
        size_t vrf_len;

        /* Restart interface */
        intf_restart ( &peerblk->retrieval, rc );

        /* Fail immediately if we have an error */
        if ( rc != 0 )
                goto done;

        /* Abort download attempt (for testing) if applicable */
        if ( ( rc = inject_fault ( PEERBLK_ABORT_RATE ) ) != 0 )
                goto done;

        /* Parse message header */
        if ( ( rc = peerblk_parse_header ( peerblk ) ) != 0 )
                goto done;

        /* Parse message segment and block details */
        if ( ( rc = peerblk_parse_block ( peerblk, &buf_len ) ) != 0 )
                goto done;

        /* If the block was plaintext, then there is nothing more to do */
        if ( ! peerblk->cipher )
                goto done;

        /* Parse message useless details */
        if ( ( rc = peerblk_parse_useless ( peerblk, buf_len, &vrf_len ) ) != 0)
                goto done;

        /* Parse message initialisation vector details */
        if ( ( rc = peerblk_parse_iv ( peerblk, buf_len, vrf_len ) ) != 0 )
                goto done;

        /* Fail if decryption length is not aligned to the cipher block size */
        if ( peerblk->cipher_remaining & ( peerblk->cipher->blocksize - 1 ) ) {
                DBGC ( peerblk, "PEERBLK %p %d.%d unaligned data length %zd\n",
                       peerblk, peerblk->segment, peerblk->block,
                       peerblk->cipher_remaining );
                rc = -EPROTO;
                goto done;
        }

        /* Stop the download attempt timer: there is no point in
         * timing out while decrypting.
         */
        stop_timer ( &peerblk->timer );

        /* Start decryption process */
        process_add ( &peerblk->process );
        return;

 done:
        /* Complete download attempt */
        peerblk_done ( peerblk, rc );
}
static void peerblk_expired ( struct retry_timer timer,
int over  __unused 
) [static]

Handle PeerDist retry timer expiry.

Parameters:
timerRetry timer
overFailure indicator

Definition at line 1141 of file peerblk.c.

References assert, peerdist_block::attempted, peerdist_block::block, container_of, peerdist_block::cycles, DBGC, peerdist_block::discovery, ETIMEDOUT, head, peerdisc_peer::list, list_entry, list_for_each_entry_continue, peerdisc_peer::location, NULL, peerdist_block::peer, peerblk_close(), PEERBLK_MAX_ATTEMPT_CYCLES, peerblk_raw_open(), PEERBLK_RAW_OPEN_TIMEOUT, peerblk_reset(), peerblk_retrieval_open(), PEERBLK_RETRIEVAL_OPEN_TIMEOUT, peerblk_timestamp(), peerdisc_segment::peers, profile_custom(), peerdist_block::rc, rc, peerdisc_client::segment, peerdist_block::segment, segment, start_timer_fixed(), peerdist_block::started, retry_timer::timeout, and peerdist_block::timer.

Referenced by peerblk_open().

                                                                             {
        struct peerdist_block *peerblk =
                container_of ( timer, struct peerdist_block, timer );
        struct peerdisc_segment *segment = peerblk->discovery.segment;
        struct peerdisc_peer *head;
        unsigned long now = peerblk_timestamp();
        const char *location;
        int rc;

        /* Profile discovery timeout, if applicable */
        if ( ( peerblk->peer == NULL ) && ( timer->timeout != 0 ) ) {
                profile_custom ( &peerblk_discovery_timeout_profiler,
                                 ( now - peerblk->started ) );
                DBGC ( peerblk, "PEERBLK %p %d.%d discovery timed out after "
                       "%ld ticks\n", peerblk, peerblk->segment,
                       peerblk->block, timer->timeout );
        }

        /* Profile download timeout, if applicable */
        if ( ( peerblk->peer != NULL ) && ( timer->timeout != 0 ) ) {
                profile_custom ( &peerblk_attempt_timeout_profiler,
                                 ( now - peerblk->attempted ) );
                DBGC ( peerblk, "PEERBLK %p %d.%d timed out after %ld ticks\n",
                       peerblk, peerblk->segment, peerblk->block,
                       timer->timeout );
        }

        /* Abort any current download attempt */
        peerblk_reset ( peerblk, -ETIMEDOUT );

        /* Record attempt start time */
        peerblk->attempted = now;

        /* If we have exceeded our maximum number of attempt cycles
         * (each cycle comprising a retrieval protocol download from
         * each peer in the list followed by a raw download from the
         * origin server), then abort the overall download.
         */
        head = list_entry ( &segment->peers, struct peerdisc_peer, list );
        if ( ( peerblk->peer == head ) &&
             ( ++peerblk->cycles >= PEERBLK_MAX_ATTEMPT_CYCLES ) ) {
                rc = peerblk->rc;
                assert ( rc != 0 );
                goto err;
        }

        /* If we have not yet made any download attempts, then move to
         * the start of the peer list.
         */
        if ( peerblk->peer == NULL )
                peerblk->peer = head;

        /* Attempt retrieval protocol download from next usable peer */
        list_for_each_entry_continue ( peerblk->peer, &segment->peers, list ) {

                /* Attempt retrieval protocol download from this peer */
                location = peerblk->peer->location;
                if ( ( rc = peerblk_retrieval_open ( peerblk,
                                                     location ) ) != 0 ) {
                        /* Non-fatal: continue to try next peer */
                        continue;
                }

                /* Start download attempt timer */
                peerblk->rc = -ETIMEDOUT;
                start_timer_fixed ( &peerblk->timer,
                                    PEERBLK_RETRIEVAL_OPEN_TIMEOUT );
                return;
        }

        /* Attempt raw download */
        if ( ( rc = peerblk_raw_open ( peerblk ) ) != 0 )
                goto err;

        /* Start download attempt timer */
        peerblk->rc = -ETIMEDOUT;
        start_timer_fixed ( &peerblk->timer, PEERBLK_RAW_OPEN_TIMEOUT );
        return;

 err:
        peerblk_close ( peerblk, rc );
}
static void peerblk_discovered ( struct peerdisc_client discovery) [static]

Handle PeerDist peer discovery.

Parameters:
discoveryPeerDist discovery client

Definition at line 1229 of file peerblk.c.

References container_of, NULL, peerdist_block::peer, peerblk_timestamp(), profile_custom(), start_timer_nodelay(), peerdist_block::started, retry_timer::timeout, and peerdist_block::timer.

                                                                     {
        struct peerdist_block *peerblk =
                container_of ( discovery, struct peerdist_block, discovery );
        unsigned long now = peerblk_timestamp();

        /* Do nothing unless we are still waiting for the initial
         * discovery timeout.
         */
        if ( ( peerblk->peer != NULL ) || ( peerblk->timer.timeout == 0 ) )
                return;

        /* Schedule an immediate retry */
        start_timer_nodelay ( &peerblk->timer );

        /* Profile discovery success */
        profile_custom ( &peerblk_discovery_success_profiler,
                         ( now - peerblk->started ) );
}
int peerblk_open ( struct interface xfer,
struct uri uri,
struct peerdist_info_block block 
)

Open PeerDist block download.

Parameters:
xferData transfer interface
uriOriginal URI
infoContent information block
Return values:
rcReturn status code

Definition at line 1302 of file peerblk.c.

References peerdist_block::block, peerdist_block::buffer, digest_algorithm::ctxsize, DBGC2, digest, peerdist_block::digest, peerdist_info::digest, peerdist_block::digestctx, digestsize, peerdist_block::digestsize, peerdist_info::digestsize, peerdist_block::discovery, peerdist_range::end, ENOMEM, peerdist_block::hash, peerdist_info_block::hash, peerdist_block::id, peerdist_info_segment::id, peerdist_info_segment::index, peerdist_info_block::index, info, peerdist_info_segment::info, intf_init(), intf_plug_plug(), list_empty, memcpy(), peerdist_block::offset, peerblk_close(), peerblk_expired(), peerblk_free(), peerblk_timestamp(), peerdisc_init(), peerdisc_open(), peerdisc_timeout_secs, peerdisc_segment::peers, peerdist_block::process, process_init_stopped(), peerdist_block::range, peerdist_info_block::range, peerdist_block::raw, rc, ref_init, ref_put, peerdist_block::refcnt, peerdist_block::retrieval, peerdist_block::secret, peerdist_info_segment::secret, peerdisc_client::segment, peerdist_block::segment, segment, peerdist_info_block::segment, peerdist_range::start, start_timer_fixed(), peerdist_block::started, TICKS_PER_SEC, timeout(), peerdist_block::timer, peerdist_block::trim, peerdist_info::trim, peerdist_info_block::trim, peerdist_block::uri, uri_get(), peerdist_block::xfer, and zalloc().

Referenced by peermux_step().

                                                       {
        const struct peerdist_info_segment *segment = block->segment;
        const struct peerdist_info *info = segment->info;
        struct digest_algorithm *digest = info->digest;
        struct peerdist_block *peerblk;
        unsigned long timeout;
        size_t digestsize;
        int rc;

        /* Allocate and initialise structure */
        peerblk = zalloc ( sizeof ( *peerblk ) + digest->ctxsize );
        if ( ! peerblk ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        ref_init ( &peerblk->refcnt, peerblk_free );
        intf_init ( &peerblk->xfer, &peerblk_xfer_desc, &peerblk->refcnt );
        intf_init ( &peerblk->raw, &peerblk_raw_desc, &peerblk->refcnt );
        intf_init ( &peerblk->retrieval, &peerblk_retrieval_desc,
                    &peerblk->refcnt );
        peerblk->uri = uri_get ( uri );
        memcpy ( &peerblk->range, &block->range, sizeof ( peerblk->range ) );
        memcpy ( &peerblk->trim, &block->trim, sizeof ( peerblk->trim ) );
        peerblk->offset = ( block->trim.start - info->trim.start );
        peerblk->digest = info->digest;
        peerblk->digestsize = digestsize = info->digestsize;
        peerblk->digestctx = ( ( ( void * ) peerblk ) + sizeof ( *peerblk ) );
        peerblk->segment = segment->index;
        memcpy ( peerblk->id, segment->id, sizeof ( peerblk->id ) );
        memcpy ( peerblk->secret, segment->secret, sizeof ( peerblk->secret ) );
        peerblk->block = block->index;
        memcpy ( peerblk->hash, block->hash, sizeof ( peerblk->hash ) );
        xferbuf_malloc_init ( &peerblk->buffer );
        process_init_stopped ( &peerblk->process, &peerblk_process_desc,
                               &peerblk->refcnt );
        peerdisc_init ( &peerblk->discovery, &peerblk_discovery_operations );
        timer_init ( &peerblk->timer, peerblk_expired, &peerblk->refcnt );
        DBGC2 ( peerblk, "PEERBLK %p %d.%d id %02x%02x%02x%02x%02x..."
                "%02x%02x%02x [%08zx,%08zx)", peerblk, peerblk->segment,
                peerblk->block, peerblk->id[0], peerblk->id[1], peerblk->id[2],
                peerblk->id[3], peerblk->id[4], peerblk->id[ digestsize - 3 ],
                peerblk->id[ digestsize - 2 ], peerblk->id[ digestsize - 1 ],
                peerblk->range.start, peerblk->range.end );
        if ( ( peerblk->trim.start != peerblk->range.start ) ||
             ( peerblk->trim.end != peerblk->range.end ) ) {
                DBGC2 ( peerblk, " covers [%08zx,%08zx)",
                        peerblk->trim.start, peerblk->trim.end );
        }
        DBGC2 ( peerblk, "\n" );

        /* Open discovery */
        if ( ( rc = peerdisc_open ( &peerblk->discovery, peerblk->id,
                                    peerblk->digestsize ) ) != 0 )
                goto err_open_discovery;

        /* Schedule a retry attempt either immediately (if we already
         * have some peers) or after the discovery timeout.
         */
        timeout = ( list_empty ( &peerblk->discovery.segment->peers ) ?
                    ( peerdisc_timeout_secs * TICKS_PER_SEC ) : 0 );
        start_timer_fixed ( &peerblk->timer, timeout );

        /* Record start time */
        peerblk->started = peerblk_timestamp();

        /* Attach to parent interface, mortalise self, and return */
        intf_plug_plug ( xfer, &peerblk->xfer );
        ref_put ( &peerblk->refcnt );
        return 0;

 err_open_discovery:
        peerblk_close ( peerblk, rc );
 err_alloc:
        return rc;
}

Variable Documentation

struct profiler peerblk_discovery_timeout_profiler __profiler [static]
Initial value:
        { .name = "peerblk.download" }

PeerDist block download profiler.

PeerDist block download discovery timeout profiler.

PeerDist block download discovery success profiler.

PeerDist block download attempt timeout profiler.

PeerDist block download attempt failure profiler.

PeerDist block download attempt success profiler.

Definition at line 87 of file peerblk.c.

Initial value:

PeerDist block download data transfer interface operations.

Definition at line 1256 of file peerblk.c.

Initial value:

PeerDist block download data transfer interface descriptor.

Definition at line 1261 of file peerblk.c.

Initial value:

PeerDist block download raw data interface operations.

Definition at line 1265 of file peerblk.c.

Initial value:

PeerDist block download raw data interface descriptor.

Definition at line 1271 of file peerblk.c.

Initial value:

PeerDist block download retrieval protocol interface operations.

Definition at line 1275 of file peerblk.c.

Initial value:

PeerDist block download retrieval protocol interface descriptor.

Definition at line 1281 of file peerblk.c.

Initial value:

PeerDist block download decryption process descriptor.

Definition at line 1286 of file peerblk.c.

Initial value:
 {
        .discovered = peerblk_discovered,
}

PeerDist block download discovery operations.

Definition at line 1290 of file peerblk.c.