| 182 | } |
| 183 | |
| 184 | static struct client *new_client(const tal_t *ctx, |
| 185 | const struct chainparams *chainparams, |
| 186 | const struct node_id *id, |
| 187 | u64 dbid, |
| 188 | const u64 capabilities, |
| 189 | int fd) |
| 190 | { |
| 191 | struct client *c = tal(ctx, struct client); |
| 192 | |
| 193 | c->msg_in = NULL; |
| 194 | |
| 195 | /*~ All-zero pubkey is used for the initial master connection */ |
| 196 | if (id) { |
| 197 | c->id = *id; |
| 198 | if (!node_id_valid(id)) |
| 199 | status_failed(STATUS_FAIL_INTERNAL_ERROR, |
| 200 | "Invalid node id %s", |
| 201 | fmt_node_id(tmpctx, id)); |
| 202 | } else { |
| 203 | memset(&c->id, 0, sizeof(c->id)); |
| 204 | } |
| 205 | c->dbid = dbid; |
| 206 | |
| 207 | c->capabilities = capabilities; |
| 208 | c->chainparams = chainparams; |
| 209 | |
| 210 | /*~ This is the core of ccan/io: the connection creation calls a |
| 211 | * callback which returns the initial plan to execute: in our case, |
| 212 | * read a message.*/ |
| 213 | c->conn = io_new_conn(ctx, fd, client_read_next, c); |
| 214 | |
| 215 | /*~ tal_steal() moves a pointer to a new parent. At this point, the |
| 216 | * hierarchy is: |
| 217 | * |
| 218 | * ctx -> c |
| 219 | * ctx -> c->conn |
| 220 | * |
| 221 | * We want to the c->conn to own 'c', so that if the io_conn closes, |
| 222 | * the client is freed: |
| 223 | * |
| 224 | * ctx -> c->conn -> c. |
| 225 | */ |
| 226 | tal_steal(c->conn, c); |
| 227 | |
| 228 | /* We put the special zero-db HSM connections into an array, the rest |
| 229 | * go into the map. */ |
| 230 | if (dbid == 0) { |
| 231 | assert(num_dbid_zero_clients < ARRAY_SIZE(dbid_zero_clients)); |
| 232 | dbid_zero_clients[num_dbid_zero_clients++] = c; |
| 233 | c->hsmd_client = hsmd_client_new_main(c, c->capabilities, c); |
| 234 | } else { |
| 235 | struct client *old_client = uintmap_get(&clients, dbid); |
| 236 | |
| 237 | /* Close conn and free any old client of this dbid. */ |
| 238 | if (old_client) |
| 239 | io_close(old_client->conn); |
| 240 | |
| 241 | if (!uintmap_add(&clients, dbid, c)) |
no test coverage detected