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