int tnet_dhcp6_option_serialize(const tnet_dhcp6_option_t* self, tsk_buffer_t *output) { uint16_t _2bytes; int ret = -1; if(!self || !output){ goto bail; } /*== Code */ _2bytes = tnet_htons(self->code); tsk_buffer_append(output, &(_2bytes), 2); switch(self->code){ case dhcp6_code_clientid: case dhcp6_code_serverid: { break; } case dhcp6_code_oro: default: { if(self->data) { const tnet_dhcp6_option_orequest_t* opt = (const tnet_dhcp6_option_orequest_t*)self->data; if(opt->codes){ /* option-len */ _2bytes = tnet_htons((unsigned short)opt->codes->size); tsk_buffer_append(output, &(_2bytes), 2); /* option-data */ ret = tsk_buffer_append(output, opt->codes->data, opt->codes->size); } } break; } } bail: return ret; }
/**@ingroup tnet_stun_group * Serializes a @ref tnet_stun_attribute_t objet in binary format. * @param attribute The STUN attribute to serialize. * @param output The output binary buffer. * @retval Zero if succeed and non-zero error code otherwise. */ int tnet_stun_attribute_serialize(const tnet_stun_attribute_t* attribute, tsk_buffer_t *output) { if(!attribute || !output){ return -1; } /* Attribute Type */ { uint16_t type = tnet_htons(attribute->type); tsk_buffer_append(output, &(type), 2); } /* Attribute Length */ { uint16_t length = tnet_htons(attribute->length); tsk_buffer_append(output, &(length), 2); } /* Attribute Value */ switch(attribute->type){ /* RFC 5389 - 15.1. MAPPED-ADDRESS */ case stun_mapped_address: { TSK_DEBUG_ERROR("NOT IMPLEMENTED"); return -3; } /* RFC 5389 - 15.2. XOR-MAPPED-ADDRESS*/ case stun_xor_mapped_address: { TSK_DEBUG_ERROR("NOT IMPLEMENTED"); return -3; } /* RFC 5389 - 15.3. USERNAME*/ case stun_username: { tnet_stun_attribute_username_t *username = (tnet_stun_attribute_username_t*)attribute; tsk_buffer_append(output, username->value, tsk_strlen(username->value)); return 0; } /* RFC 5389 - MESSAGE-INTEGRITY*/ case stun_message_integrity: { tnet_stun_attribute_integrity_t *integrity = (tnet_stun_attribute_integrity_t*)attribute; tsk_buffer_append(output, integrity->sha1digest, TSK_SHA1_DIGEST_SIZE); return 0; } /* RFC 5389 - 15.5. FINGERPRINT*/ case stun_fingerprint: { uint32_t fingerprint = /*tnet_htonl*/(((tnet_stun_attribute_fingerprint_t*)attribute)->value); tsk_buffer_append(output, &fingerprint, 4); return 0; } /* RFC 5389 - 15.6. ERROR-CODE*/ case stun_error_code: { TSK_DEBUG_ERROR("NOT IMPLEMENTED"); return -3; } /* RFC 5389 - 15.7. REALM*/ case stun_realm: { tnet_stun_attribute_realm_t *realm = (tnet_stun_attribute_realm_t*)attribute; tsk_buffer_append(output, realm->value, tsk_strlen(realm->value)); return 0; } /* RFC 5389 - 15.8. NONCE*/ case stun_nonce: { tnet_stun_attribute_nonce_t *nonce = (tnet_stun_attribute_nonce_t*)attribute; tsk_buffer_append(output, nonce->value, tsk_strlen(nonce->value)); return 0; } /* RFC 5389 - 15.9. UNKNOWN-ATTRIBUTES*/ case stun_unknown_attributes: { TSK_DEBUG_ERROR("NOT IMPLEMENTED"); return -3; } /* RFC 5389 - 15.10. SOFTWARE */ case stun_software: { tnet_stun_attribute_software_t *software = (tnet_stun_attribute_software_t*)attribute; tsk_buffer_append(output, software->value, tsk_strlen(software->value)); return 0; } /* RFC 5389 - 15.11. ALTERNATE-SERVER */ case stun_alternate_server: { TSK_DEBUG_ERROR("NOT IMPLEMENTED"); return -3; } /* draft-ietf-behave-turn-16 - */ case stun_channel_number: case stun_lifetime: case stun_reserved2: case stun_xor_peer_address: case stun_data: case stun_xor_relayed_address: case stun_even_port: case stun_requested_transport: case stun_dont_fragment: case stun_reserved3: case stun_reservation_token: { return tnet_turn_attribute_serialize(attribute, output); } default: return -2; } }
/** Serializes a DNS RR. */ int tnet_dns_rr_serialize(const tnet_dns_rr_t* rr, tsk_buffer_t *output) { if(!rr || !output){ return -1; } /*=== NAME ===*/ { tnet_dns_rr_qname_serialize(rr->name, output); } /*=== TYPE ===*/ { uint16_t qtype = tnet_htons(rr->qtype); tsk_buffer_append(output, &(qtype), 2); } /*=== CLASS ===*/ { uint16_t qclass = tnet_htons(rr->qclass); tsk_buffer_append(output, &(qclass), 2); } /*=== TTL ===*/ { uint32_t ttl = tnet_htonl(rr->ttl); tsk_buffer_append(output, &(ttl), 4); } /*=== RDLENGTH ===*/ { uint16_t length = tnet_htons(rr->rdlength); tsk_buffer_append(output, &(length), 2); } /*=== RDATA : Request never contains data ===*/ if(!rr->rpdata){ goto done; } switch(rr->qtype){ case qtype_a: case qtype_aaaa: case qtype_cname: case qtype_mx: case qtype_naptr: case qtype_ns: case qtype_opt: case qtype_ptr: case qtype_soa: case qtype_srv: case qtype_txt: default: { TSK_DEBUG_WARN("DNS Request should not contains RDATA (not supported)."); break; } } done: return 0; }