| 206 | } |
| 207 | |
| 208 | static void lightningd_to_websocket(int lightningfd, int wsfd) |
| 209 | { |
| 210 | /* We prepend ws header */ |
| 211 | u8 buf[4 + 65535]; |
| 212 | int len; |
| 213 | /* Not continued frame (0x80), opcode = 2 (binary) */ |
| 214 | const u8 firstbyte = 0x82; |
| 215 | size_t off; |
| 216 | |
| 217 | len = read(lightningfd, 4 + buf, sizeof(buf) - 4); |
| 218 | if (len <= 0) |
| 219 | exit(0); |
| 220 | |
| 221 | if (len > 125) { |
| 222 | buf[0] = firstbyte; |
| 223 | buf[1] = 126; |
| 224 | buf[2] = (len >> 8); |
| 225 | buf[3] = len; |
| 226 | off = 0; |
| 227 | len += 4; |
| 228 | } else { |
| 229 | buf[2] = firstbyte; |
| 230 | buf[3] = len; |
| 231 | off = 2; |
| 232 | len += 2; |
| 233 | } |
| 234 | if (!write_all(wsfd, buf + off, len)) |
| 235 | exit(0); |
| 236 | } |
| 237 | |
| 238 | /* Returns payload size, sets inmask, is_binframe */ |
| 239 | static size_t read_payload_header(int fd, u8 inmask[4], bool *is_binframe) |