| 162 | } |
| 163 | |
| 164 | static struct command_result *json_connect(struct command *cmd, |
| 165 | const char *buffer, |
| 166 | const jsmntok_t *obj UNNEEDED, |
| 167 | const jsmntok_t *params) |
| 168 | { |
| 169 | struct wireaddr_internal *addr; |
| 170 | const char *err_msg; |
| 171 | struct id_and_addr id_addr; |
| 172 | struct peer *peer; |
| 173 | |
| 174 | id_addr.host = NULL; |
| 175 | id_addr.port = NULL; |
| 176 | if (!param_check(cmd, buffer, params, |
| 177 | p_req("id", param_id_maybe_addr, &id_addr), |
| 178 | p_opt("host", param_id_addr_string, &id_addr.host), |
| 179 | p_opt("port", param_id_addr_u16, &id_addr.port), |
| 180 | NULL)) |
| 181 | return command_param_failed(); |
| 182 | |
| 183 | /* If we have a host, convert */ |
| 184 | if (id_addr.host) { |
| 185 | u16 port = id_addr.port ? *id_addr.port : chainparams_get_ln_port(chainparams); |
| 186 | addr = tal(cmd, struct wireaddr_internal); |
| 187 | err_msg = parse_wireaddr_internal(tmpctx, id_addr.host, port, |
| 188 | !cmd->ld->always_use_proxy |
| 189 | && !cmd->ld->pure_tor_setup, addr); |
| 190 | if (err_msg) { |
| 191 | return command_fail(cmd, LIGHTNINGD, |
| 192 | "Host %s:%u not valid: %s", |
| 193 | id_addr.host, port, err_msg); |
| 194 | } |
| 195 | /* Check they didn't specify some weird type! */ |
| 196 | switch (addr->itype) { |
| 197 | case ADDR_INTERNAL_SOCKNAME: |
| 198 | case ADDR_INTERNAL_WIREADDR: |
| 199 | /* Can happen if we're disable DNS */ |
| 200 | case ADDR_INTERNAL_FORPROXY: |
| 201 | break; |
| 202 | case ADDR_INTERNAL_ALLPROTO: |
| 203 | case ADDR_INTERNAL_AUTOTOR: |
| 204 | case ADDR_INTERNAL_STATICTOR: |
| 205 | return command_fail(cmd, LIGHTNINGD, |
| 206 | "Host %s:%u not a simple type", |
| 207 | id_addr.host, port); |
| 208 | } |
| 209 | } else { |
| 210 | addr = NULL; |
| 211 | /* Port without host name? */ |
| 212 | if (id_addr.port) |
| 213 | return command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
| 214 | "Can't specify port without host"); |
| 215 | } |
| 216 | |
| 217 | if (command_check_only(cmd)) |
| 218 | return command_check_done(cmd); |
| 219 | |
| 220 | /* If we know about peer, see if it's already connected. */ |
| 221 | peer = peer_by_id(cmd->ld, &id_addr.id); |
nothing calls this directly
no test coverage detected