22#include <common/json_command.h>
33#include <common/json_param.h>
44#include <connectd/connectd_wiregen.h>
5+ #include <inttypes.h>
56#include <lightningd/channel.h>
67#include <lightningd/jsonrpc.h>
78#include <lightningd/lightningd.h>
89#include <lightningd/peer_control.h>
10+ #include <lightningd/ping.h>
911#include <lightningd/subd.h>
1012
11- static void ping_reply (struct subd * connectd ,
12- const u8 * msg , const int * fds ,
13- struct command * cmd )
13+ struct ping_command {
14+ struct list_node list ;
15+ u64 reqid ;
16+ struct command * cmd ;
17+ };
18+
19+ static void destroy_ping_command (struct ping_command * ping_command ,
20+ struct lightningd * ld )
21+ {
22+ list_del_from (& ld -> ping_commands , & ping_command -> list );
23+ }
24+
25+ static struct ping_command * find_ping_command (struct lightningd * ld ,
26+ u64 reqid )
27+ {
28+ struct ping_command * i ;
29+ list_for_each (& ld -> ping_commands , i , list ) {
30+ if (i -> reqid == reqid )
31+ return i ;
32+ }
33+ return NULL ;
34+ }
35+
36+ void handle_ping_done (struct subd * connectd , const u8 * msg )
1437{
1538 u16 totlen ;
1639 bool sent ;
40+ u64 reqid ;
41+ struct ping_command * ping_command ;
1742
18- log_debug (connectd -> log , "Got ping reply!" );
19-
20- if (!fromwire_connectd_ping_reply (msg , & sent , & totlen )) {
43+ if (!fromwire_connectd_ping_done (msg , & reqid , & sent , & totlen )) {
2144 log_broken (connectd -> log , "Malformed ping reply %s" ,
2245 tal_hex (tmpctx , msg ));
23- was_pending (command_fail (cmd , LIGHTNINGD ,
24- "Bad reply message" ));
2546 return ;
2647 }
2748
49+ ping_command = find_ping_command (connectd -> ld , reqid );
50+ if (!ping_command ) {
51+ log_broken (connectd -> log , "ping reply for unknown reqid %" PRIu64 , reqid );
52+ return ;
53+ }
54+
55+ log_debug (connectd -> log , "Got ping reply!" );
2856 if (!sent )
29- was_pending (command_fail (cmd , LIGHTNINGD ,
57+ was_pending (command_fail (ping_command -> cmd , LIGHTNINGD ,
3058 "Ping already pending" ));
3159 else {
32- struct json_stream * response = json_stream_success (cmd );
60+ struct json_stream * response = json_stream_success (ping_command -> cmd );
3361
3462 json_add_num (response , "totlen" , totlen );
35- was_pending (command_success (cmd , response ));
63+ was_pending (command_success (ping_command -> cmd , response ));
3664 }
3765}
3866
@@ -44,6 +72,8 @@ static struct command_result *json_ping(struct command *cmd,
4472 unsigned int * len , * pongbytes ;
4573 struct node_id * id ;
4674 u8 * msg ;
75+ static u64 reqid ;
76+ struct ping_command * ping_command ;
4777
4878 if (!param_check (cmd , buffer , params ,
4979 p_req ("id" , param_node_id , & id ),
@@ -83,8 +113,14 @@ static struct command_result *json_ping(struct command *cmd,
83113 if (command_check_only (cmd ))
84114 return command_check_done (cmd );
85115
86- msg = towire_connectd_ping (NULL , id , * pongbytes , * len );
87- subd_req (cmd , cmd -> ld -> connectd , take (msg ), -1 , 0 , ping_reply , cmd );
116+ ping_command = tal (cmd , struct ping_command );
117+ ping_command -> cmd = cmd ;
118+ ping_command -> reqid = ++ reqid ;
119+ list_add_tail (& cmd -> ld -> ping_commands , & ping_command -> list );
120+ tal_add_destructor2 (ping_command , destroy_ping_command , cmd -> ld );
121+
122+ msg = towire_connectd_ping (NULL , ping_command -> reqid , id , * pongbytes , * len );
123+ subd_send_msg (cmd -> ld -> connectd , take (msg ));
88124
89125 return command_still_pending (cmd );
90126}
0 commit comments