#pragma once

#ifdef __cplusplus
extern "C"
{
#endif

struct ip_reassembly_options
{
    uint8_t enable;
    uint32_t timeout;        // range: [1, 60000]
    uint32_t bucket_entries; // range: [1, 4294967295] (must be power of 2)
    uint32_t bucket_num;     // range: [1, 4294967295]
};

struct __attribute__((aligned(64))) ip_reassembly_stat
{
    // IPv4 frag stat
    uint64_t ip4_defrags_expected;
    uint64_t ip4_defrags_succeed;
    uint64_t ip4_defrags_failed_timeout;
    uint64_t ip4_defrags_failed_invalid_length;
    uint64_t ip4_defrags_failed_overlap;
    uint64_t ip4_defrags_failed_too_many_frag;

    uint64_t ip4_frags;
    uint64_t ip4_frags_freed;
    uint64_t ip4_frags_buffered;
    uint64_t ip4_frags_bypass_no_buffer;
    uint64_t ip4_frags_bypass_dup_fist_frag;
    uint64_t ip4_frags_bypass_dup_last_frag;

    // IPv6 frag stat
    uint64_t ip6_defrags_expected;
    uint64_t ip6_defrags_succeed;
    uint64_t ip6_defrags_failed_timeout;
    uint64_t ip6_defrags_failed_invalid_length;
    uint64_t ip6_defrags_failed_overlap;
    uint64_t ip6_defrags_failed_too_many_frag;

    uint64_t ip6_frags;
    uint64_t ip6_frags_freed;
    uint64_t ip6_frags_buffered;
    uint64_t ip6_frags_bypass_no_buffer;
    uint64_t ip6_frags_bypass_dup_fist_frag;
    uint64_t ip6_frags_bypass_dup_last_frag;
};

struct ip_reassembly *ip_reassembly_new(const struct ip_reassembly_options *opts);
void ip_reassembly_free(struct ip_reassembly *assy);
void ip_reassembly_expire(struct ip_reassembly *assy, uint64_t max_free, uint64_t now);
struct ip_reassembly_stat *ip_reassembly_stat(struct ip_reassembly *assy);

/*
 * Returns the reassembled packet, or NULL if the packet is not reassembled
 * The returned packet should be freed by calling the packet_free() function
 */
struct packet *ip_reassembly_packet(struct ip_reassembly *assy, const struct packet *pkt, uint64_t now);
struct packet *ipv4_reassembly_packet(struct ip_reassembly *assy, const struct packet *pkt, uint64_t now);
struct packet *ipv6_reassembly_packet(struct ip_reassembly *assy, const struct packet *pkt, uint64_t now);

#ifdef __cplusplus
}
#endif
