| 194 | } |
| 195 | |
| 196 | struct onionpacket *parse_onionpacket(const tal_t *ctx, |
| 197 | const u8 *src, |
| 198 | const size_t srclen, |
| 199 | enum onion_wire *failcode) |
| 200 | { |
| 201 | struct onionpacket *dest = tal(ctx, struct onionpacket); |
| 202 | const u8 *cursor = src; |
| 203 | size_t max = srclen; |
| 204 | |
| 205 | dest->version = fromwire_u8(&cursor, &max); |
| 206 | if (dest->version != 0x00) { |
| 207 | // FIXME add logging |
| 208 | *failcode = WIRE_INVALID_ONION_VERSION; |
| 209 | return tal_free(dest); |
| 210 | } |
| 211 | |
| 212 | fromwire_pubkey(&cursor, &max, &dest->ephemeralkey); |
| 213 | if (cursor == NULL) { |
| 214 | *failcode = WIRE_INVALID_ONION_KEY; |
| 215 | return tal_free(dest); |
| 216 | } |
| 217 | |
| 218 | /* If max underflows, this returns NULL and fromwire fails. */ |
| 219 | dest->routinginfo = fromwire_tal_arrn(dest, &cursor, &max, |
| 220 | max - HMAC_SIZE); |
| 221 | fromwire_hmac(&cursor, &max, &dest->hmac); |
| 222 | |
| 223 | assert(max == 0); |
| 224 | if (cursor == NULL) { |
| 225 | *failcode = WIRE_INVALID_ONION_HMAC; |
| 226 | return tal_free(dest); |
| 227 | } |
| 228 | |
| 229 | return dest; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Generate a pseudo-random byte stream of length `dstlen` from key `k` and |