------------------------------------------------------------ revno: 5262 committer: Henrik Ziegeldorf <henrik.ziegeldorf@xxxxxxxxxxxxxx> branch nick: hipl timestamp: Wed 2010-12-08 19:30:17 +0100 message: Added functions to convert between compressed (on the wire) format and uncompressed (internal) format of host identities. These two functions are needed to fix bug 612029. Internally host identities are handled within a struct that reserves place for 4096 bit rsa keys and 64 byte hostnames. For transmission on the wire unused space should not be sent thus we need to compress the host identity before sending it as a message parameter on the wire. This is done by funciton hip_build_param_host_id. When receiving a message that contains a host_id parameter we need to decompress it to the internal format. This is done my function hip_build_host_id_from_param. modified: lib/core/builder.c lib/core/builder.h -- lp:~toxedvirus/hipl/options https://code.launchpad.net/~toxedvirus/hipl/options Your team HIPL core team is subscribed to branch lp:~toxedvirus/hipl/options. To unsubscribe from this branch go to https://code.launchpad.net/~toxedvirus/hipl/options/+edit-subscription
=== modified file 'lib/core/builder.c' --- lib/core/builder.c 2010-12-02 07:16:57 +0000 +++ lib/core/builder.c 2010-12-08 18:30:17 +0000 @@ -3138,6 +3138,72 @@ } /** + * Convert a host id parameter from its compressed on the wire format to + * the uncompressed internal format. + * + * @param param the host id parameter + * @param peer_host_id pointer to memory, where the uncompressed host id is written to + * + * @return 0 on success, negative on error (if parameter was of wrong type) + */ +int hip_build_host_id_from_param(const struct hip_host_id *param, struct hip_host_id *peer_host_id) { + int err = 0; + uint16_t header_len; + uint16_t key_len; + uint16_t fqdn_len; + HIP_IFEL(!(hip_get_param_type(param) == HIP_PARAM_HOST_ID), + -1, "Param has wrong type (not HIP_PARAM_HOST_ID)"); + + // copy the header, key and fqdn + header_len = sizeof(struct hip_host_id) - sizeof(peer_host_id->key) - sizeof(peer_host_id->hostname); + fqdn_len = ntohs(param->di_type_length) & 0x0FFF; + key_len = ntohs(param->hi_length) - sizeof(struct hip_host_id_key_rdata); + memcpy(peer_host_id, param, header_len); + memcpy(peer_host_id->key, ¶m->key[0], key_len); + memcpy(peer_host_id->hostname, ¶m->key[key_len], fqdn_len); + + // with the header we also copied the compressed length value, so correct this + hip_set_param_contents_len((struct hip_tlv_common *) peer_host_id, + sizeof(struct hip_host_id) - sizeof(struct hip_tlv_common)); + +out_err: + return err; +} + +/** + * Build a host id parameter and insert it into a message. + * + * @param msg the message where the parameter is inserted + * @param host_id the host identity from which the parameter is built + * + * @return zero on success, negative on error value on error + * @see hip_build_param() + */ +int hip_build_param_host_id(struct hip_common *msg, + const struct hip_host_id *host_id) { + struct hip_host_id new_host_id; + uint16_t header_len; + uint16_t fqdn_len; + uint16_t key_len; + uint16_t par_len; + + // eliminate unused space by copying fqdn directly behind the keyrr + header_len = sizeof(struct hip_host_id) - sizeof(host_id->key) - sizeof(host_id->hostname); + fqdn_len = ntohs(host_id->di_type_length) & 0x0FFF; + key_len = ntohs(host_id->hi_length) - sizeof(struct hip_host_id_key_rdata); + memcpy(&new_host_id, host_id, header_len); + memcpy(&new_host_id.key[0], host_id->key, key_len); + memcpy(&new_host_id.key[key_len], host_id->hostname, fqdn_len); + + // set the new contents length + // = | length fields | + | keyrr header | + | HI | + | FQDN | + par_len = header_len + key_len + fqdn_len; + hip_set_param_contents_len((struct hip_tlv_common *) &new_host_id, par_len); + + return hip_build_param(msg, &new_host_id); +} + +/** * build the header of a host id parameter * * @param host_id_hdr the header === modified file 'lib/core/builder.h' --- lib/core/builder.h 2010-11-22 14:03:57 +0000 +++ lib/core/builder.h 2010-12-08 18:30:17 +0000 @@ -97,6 +97,10 @@ int hip_create_msg_pseudo_hmac2(const struct hip_common *msg, struct hip_common *msg_copy, struct hip_host_id *host_id); +int hip_build_host_id_from_param(const struct hip_host_id *param, + struct hip_host_id *peer_host_id); +int hip_build_param_host_id(struct hip_common *msg, + const struct hip_host_id *host_id); void hip_build_param_host_id_hdr(struct hip_host_id *host_id_hdr, const char *hostname, hip_tlv_len_t rr_data_len,