MCPcopy Create free account
hub / github.com/ElementsProject/lightning / json_connect

Function json_connect

lightningd/connect_control.c:178–234  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

176}
177
178static struct command_result *json_connect(struct command *cmd,
179 const char *buffer,
180 const jsmntok_t *obj UNNEEDED,
181 const jsmntok_t *params)
182{
183 struct wireaddr_internal *addr;
184 const char *err_msg;
185 struct id_and_addr id_addr;
186 struct peer *peer;
187
188 id_addr.host = NULL;
189 id_addr.port = NULL;
190 if (!param(cmd, buffer, params,
191 p_req("id", param_id_maybe_addr, &id_addr),
192 p_opt("host", param_id_addr_string, &id_addr.host),
193 p_opt("port", param_id_addr_u16, &id_addr.port),
194 NULL))
195 return command_param_failed();
196
197 /* If we have a host, convert */
198 if (id_addr.host) {
199 u16 port = id_addr.port ? *id_addr.port : chainparams_get_ln_port(chainparams);
200 addr = tal(cmd, struct wireaddr_internal);
201 if (!parse_wireaddr_internal(id_addr.host, addr, port, false,
202 !cmd->ld->always_use_proxy
203 && !cmd->ld->pure_tor_setup,
204 true,
205 &err_msg)) {
206 return command_fail(cmd, LIGHTNINGD,
207 "Host %s:%u not valid: %s",
208 id_addr.host, port, err_msg);
209 }
210 } else {
211 addr = NULL;
212 /* Port without host name? */
213 if (id_addr.port)
214 return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
215 "Can't specify port without host");
216 }
217
218 /* If we know about peer, see if it's already connected. */
219 peer = peer_by_id(cmd->ld, &id_addr.id);
220 if (peer && peer->connected == PEER_CONNECTED) {
221 log_debug(cmd->ld->log, "Already connected via %s",
222 type_to_string(tmpctx, struct wireaddr_internal,
223 &peer->addr));
224 return connect_cmd_succeed(cmd, peer,
225 peer->connected_incoming,
226 &peer->addr);
227 }
228
229 try_connect(cmd, cmd->ld, &id_addr.id, 0, addr);
230
231 /* Leave this here for peer_connected, connect_failed or peer_disconnect_done. */
232 new_connect(cmd->ld, &id_addr.id, cmd);
233 return command_still_pending(cmd);
234}
235

Callers

nothing calls this directly

Calls 11

parse_wireaddr_internalFunction · 0.85
peer_by_idFunction · 0.85
connect_cmd_succeedFunction · 0.85
try_connectFunction · 0.85
new_connectFunction · 0.85
command_param_failedFunction · 0.70
command_failFunction · 0.70
command_still_pendingFunction · 0.70
paramFunction · 0.50
chainparams_get_ln_portFunction · 0.50
type_to_stringClass · 0.50

Tested by

no test coverage detected