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