| 166 | } |
| 167 | |
| 168 | struct onionpacket *parse_onionpacket(const tal_t *ctx, |
| 169 | const u8 *src, |
| 170 | const size_t srclen, |
| 171 | enum onion_wire *failcode) |
| 172 | { |
| 173 | struct onionpacket *dest = tal(ctx, struct onionpacket); |
| 174 | const u8 *cursor = src; |
| 175 | size_t max = srclen; |
| 176 | |
| 177 | dest->version = fromwire_u8(&cursor, &max); |
| 178 | if (dest->version != 0x00) { |
| 179 | // FIXME add logging |
| 180 | *failcode = WIRE_INVALID_ONION_VERSION; |
| 181 | return tal_free(dest); |
| 182 | } |
| 183 | |
| 184 | fromwire_pubkey(&cursor, &max, &dest->ephemeralkey); |
| 185 | if (cursor == NULL) { |
| 186 | *failcode = WIRE_INVALID_ONION_KEY; |
| 187 | return tal_free(dest); |
| 188 | } |
| 189 | |
| 190 | /* If max underflows, this returns NULL and fromwire fails. */ |
| 191 | dest->routinginfo = fromwire_tal_arrn(dest, &cursor, &max, |
| 192 | max - HMAC_SIZE); |
| 193 | fromwire_hmac(&cursor, &max, &dest->hmac); |
| 194 | |
| 195 | assert(max == 0); |
| 196 | if (cursor == NULL) { |
| 197 | *failcode = WIRE_INVALID_REALM; |
| 198 | return tal_free(dest); |
| 199 | } |
| 200 | |
| 201 | return dest; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Generate a pseudo-random byte stream of length `dstlen` from key `k` and |