@ -12,8 +12,8 @@
# include <common/initial_channel.h>
# include <common/key_derive.h>
# include <common/peer_failed.h>
# include <common/ping.h>
# include <common/pseudorand.h>
# include <common/read_peer_msg.h>
# include <common/status.h>
# include <common/subdaemon.h>
# include <common/type_to_string.h>
@ -203,88 +203,27 @@ static void temporary_channel_id(struct channel_id *channel_id)
channel_id - > id [ i ] = pseudorand ( 256 ) ;
}
static void handle_ping ( const u8 * msg ,
struct crypto_state * cs ,
const struct channel_id * our_channel_id )
/* This handles the case where there's an error only for this channel */
static void opening_errpkt ( const char * desc ,
bool this_channel_only ,
struct state * state )
{
u8 * pong ;
if ( ! check_ping_make_pong ( msg , msg , & pong ) )
peer_failed ( PEER_FD , cs , our_channel_id , " Bad ping " ) ;
status_trace ( " Got ping, sending %s " , pong ?
wire_type_name ( fromwire_peektype ( pong ) )
: " nothing " ) ;
if ( pong & & ! sync_crypto_write ( cs , PEER_FD , take ( pong ) ) )
status_failed ( STATUS_FAIL_PEER_IO ,
" Failed writing pong: %s " , strerror ( errno ) ) ;
if ( this_channel_only )
negotiation_failed ( state , false , " Error packet: %s " , desc ) ;
else
status_failed ( STATUS_FAIL_PEER_BAD , " Received ERROR %s " , desc ) ;
}
/* Handle random messages we might get, returning NULL if we handled it . */
static u8 * read_peer_msg ( struct state * state )
/* Handle random messages we might get, returning the first non-handled one. */
static u8 * opening_read_peer_msg ( struct state * state )
{
u8 * msg ;
struct channel_id channel_id ;
msg = sync_crypto_read ( state , & state - > cs , PEER_FD ) ;
if ( ! msg )
status_failed ( STATUS_FAIL_PEER_IO ,
" Failed reading from peer: %s " , strerror ( errno ) ) ;
if ( is_gossip_msg ( msg ) ) {
/* Forward to gossip daemon */
wire_sync_write ( GOSSIP_FD , take ( msg ) ) ;
return NULL ;
}
if ( fromwire_peektype ( msg ) = = WIRE_PING ) {
handle_ping ( msg , & state - > cs , & state - > channel_id ) ;
return tal_free ( msg ) ;
}
if ( fromwire_peektype ( msg ) = = WIRE_ERROR ) {
struct channel_id chanid ;
char * err = sanitize_error ( msg , msg , & chanid ) ;
/* BOLT #1:
*
* The channel is referred to by ` channel_id ` , unless
* ` channel_id ` is 0 ( i . e . all bytes are 0 ) , in which
* case it refers to all channels .
* . . .
* The receiving node :
* - upon receiving ` error ` :
* - MUST fail the channel referred to by the error
* message .
* - if no existing channel is referred to by the
* message :
* - MUST ignore the message .
*/
if ( channel_id_is_all ( & chanid ) )
status_failed ( STATUS_FAIL_PEER_BAD ,
" Received ERROR %s " , err ) ;
else if ( structeq ( & chanid , & state - > channel_id ) )
negotiation_failed ( state , false ,
" Error packet: %s " , err ) ;
return tal_free ( msg ) ;
}
/* They're talking about a different channel? */
if ( extract_channel_id ( msg , & channel_id )
& & ! structeq ( & channel_id , & state - > channel_id ) ) {
status_trace ( " Rejecting %s for unknown channel_id %s " ,
wire_type_name ( fromwire_peektype ( msg ) ) ,
type_to_string ( msg , struct channel_id ,
& channel_id ) ) ;
sync_crypto_write ( & state - > cs , PEER_FD ,
take ( towire_errorfmt ( msg , & channel_id ,
" Multiple channels "
" unsupported " ) ) ) ;
return tal_free ( msg ) ;
}
while ( ( msg = read_peer_msg ( state , & state - > cs , & state - > channel_id ,
sync_crypto_write_arg ,
status_fail_io ,
opening_errpkt ,
state ) ) = = NULL ) ;
return msg ;
}
@ -353,7 +292,7 @@ static u8 *funder_channel(struct state *state,
state - > remoteconf = tal ( state , struct channel_config ) ;
while ( ( msg = read_peer_msg ( state ) ) = = NULL ) ;
msg = opening_ read_peer_msg( state ) ;
/* BOLT #2:
*
@ -474,7 +413,7 @@ static u8 *funder_channel(struct state *state,
* commitment transaction , so they can broadcast it knowing they can
* redeem their funds if they need to .
*/
while ( ( msg = read_peer_msg ( state ) ) = = NULL ) ;
msg = opening_ read_peer_msg( state ) ;
if ( ! fromwire_funding_signed ( msg , NULL , & id_in , & sig ) )
peer_failed ( PEER_FD , & state - > cs , & state - > channel_id ,
@ -657,7 +596,7 @@ static u8 *fundee_channel(struct state *state,
status_failed ( STATUS_FAIL_PEER_IO ,
" Writing accept_channel: %s " , strerror ( errno ) ) ;
while ( ( msg = read_peer_msg ( state ) ) = = NULL ) ;
msg = opening_ read_peer_msg( state ) ;
if ( ! fromwire_funding_created ( msg , NULL , & id_in ,
& state - > funding_txid ,