<commit_before>#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include "scion.h"

uint8_t L4PROTOCOLS[] = {L4_SCMP, L4_TCP, L4_UDP, L4_SSP};

/*
 * Initialize common header fields
 * buf: Pointer to start of SCION packet
 * src_type: Address type of src host addr
 * dst_type: Address type of dst host addr
 * next_hdr: L4 protocol number or extension type
 */
void build_cmn_hdr(uint8_t *buf, int src_type, int dst_type, int next_hdr)
{
    SCIONCommonHeader *sch = (SCIONCommonHeader *)buf;
    uint16_t vsd = 0;
    vsd |= src_type << 6;
    vsd |= dst_type;
    sch->ver_src_dst = htons(vsd);
    sch->next_header = next_hdr;

    int addr_len = padded_addr_len(buf);
    sch->header_len = sizeof(SCIONCommonHeader) + addr_len;
    sch->total_len = htons(sch->header_len);
    /* Set of pointers to start of path (which has not been set yet) */
    sch->current_iof = sch->header_len;
    sch->current_hof = sch->current_iof;
}

/*
 * Initialize address fields
 * buf: Pointer to start of SCION packet
 * src: Src SCION addr
 * dst: Dst SCION addr
 */
void build_addr_hdr(uint8_t *buf, SCIONAddr *src, SCIONAddr *dst)
{
    SCIONCommonHeader *sch = (SCIONCommonHeader *)buf;
    int src_len = get_src_len(buf);
    int dst_len = get_dst_len(buf);
    uint8_t *ptr = (uint8_t *)(sch + 1);
    *(uint32_t *)ptr = htonl(src->isd_as);
    ptr += ISD_AS_LEN;
    memcpy(ptr, src->host.addr, src_len);
    ptr += src_len;
    *(uint32_t *)ptr = htonl(dst->isd_as);
    ptr += ISD_AS_LEN;
    memcpy(ptr, dst->host.addr, dst_len);
}

/*
 * Get total length of addresses with padding
 * buf: Pointer to start of SCION packet
 * return value: Total padded addr length
 */
int padded_addr_len(uint8_t *buf)
{
    int addr_len = get_src_len(buf) + get_dst_len(buf) + 2 * ISD_AS_LEN;
    return (addr_len + SCION_ADDR_PAD - 1) & ~(SCION_ADDR_PAD - 1);
}

/*
 * Set SCION path
 * buf: Pointer to start of SCION packet
 * path: Pointer to start of path data to be copied
 */
void set_path(uint8_t *buf, uint8_t *path, int len)
{
    if (len < 0)
        return;

    SCIONCommonHeader *sch = (SCIONCommonHeader *)buf;
    /* pre-condition: header_len points to end of address header */
    memcpy(buf + sch->header_len, path, len);
    sch->header_len += len;
    sch->total_len = htons(sch->header_len);
}

/*
 * Get SCION path
 * buf: Pointer to start of SCION packet
 * return value: Pointer to start of SCION path in packet
 */
uint8_t * get_path(uint8_t *buf)
{
    return buf + sizeof(SCIONCommonHeader) + padded_addr_len(buf);
}

/*
 * Get length of SCION path
 * buf: Pointer to start of SCION packet
 * return value: Length of SCION path in packet
 */
int get_path_len(uint8_t *buf)
{
    SCIONCommonHeader *sch = (SCIONCommonHeader *)buf;
    return ntohs(sch->header_len) - sizeof(SCIONCommonHeader) - padded_addr_len(buf);
}

/*
 * Initialize OF indices (pointers)
 * buf: Pointer to start of SCION packet<commit_msg>Bug fix in get_path_len (#720)<commit_after>#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include "scion.h"

uint8_t L4PROTOCOLS[] = {L4_SCMP, L4_TCP, L4_UDP, L4_SSP};

/*
 * Initialize common header fields
 * buf: Pointer to start of SCION packet
 * src_type: Address type of src host addr
 * dst_type: Address type of dst host addr
 * next_hdr: L4 protocol number or extension type
 */
void build_cmn_hdr(uint8_t *buf, int src_type, int dst_type, int next_hdr)
{
    SCIONCommonHeader *sch = (SCIONCommonHeader *)buf;
    uint16_t vsd = 0;
    vsd |= src_type << 6;
    vsd |= dst_type;
    sch->ver_src_dst = htons(vsd);
    sch->next_header = next_hdr;

    int addr_len = padded_addr_len(buf);
    sch->header_len = sizeof(SCIONCommonHeader) + addr_len;
    sch->total_len = htons(sch->header_len);
    /* Set of pointers to start of path (which has not been set yet) */
    sch->current_iof = sch->header_len;
    sch->current_hof = sch->current_iof;
}

/*
 * Initialize address fields
 * buf: Pointer to start of SCION packet
 * src: Src SCION addr
 * dst: Dst SCION addr
 */
void build_addr_hdr(uint8_t *buf, SCIONAddr *src, SCIONAddr *dst)
{
    SCIONCommonHeader *sch = (SCIONCommonHeader *)buf;
    int src_len = get_src_len(buf);
    int dst_len = get_dst_len(buf);
    uint8_t *ptr = (uint8_t *)(sch + 1);
    *(uint32_t *)ptr = htonl(src->isd_as);
    ptr += ISD_AS_LEN;
    memcpy(ptr, src->host.addr, src_len);
    ptr += src_len;
    *(uint32_t *)ptr = htonl(dst->isd_as);
    ptr += ISD_AS_LEN;
    memcpy(ptr, dst->host.addr, dst_len);
}

/*
 * Get total length of addresses with padding
 * buf: Pointer to start of SCION packet
 * return value: Total padded addr length
 */
int padded_addr_len(uint8_t *buf)
{
    int addr_len = get_src_len(buf) + get_dst_len(buf) + 2 * ISD_AS_LEN;
    return (addr_len + SCION_ADDR_PAD - 1) & ~(SCION_ADDR_PAD - 1);
}

/*
 * Set SCION path
 * buf: Pointer to start of SCION packet
 * path: Pointer to start of path data to be copied
 */
void set_path(uint8_t *buf, uint8_t *path, int len)
{
    if (len < 0)
        return;

    SCIONCommonHeader *sch = (SCIONCommonHeader *)buf;
    /* pre-condition: header_len points to end of address header */
    memcpy(buf + sch->header_len, path, len);
    sch->header_len += len;
    sch->total_len = htons(sch->header_len);
}

/*
 * Get SCION path
 * buf: Pointer to start of SCION packet
 * return value: Pointer to start of SCION path in packet
 */
uint8_t * get_path(uint8_t *buf)
{
    return buf + sizeof(SCIONCommonHeader) + padded_addr_len(buf);
}

/*
 * Get length of SCION path
 * buf: Pointer to start of SCION packet
 * return value: Length of SCION path in packet
 */
int get_path_len(uint8_t *buf)
{
    SCIONCommonHeader *sch = (SCIONCommonHeader *)buf;
    return sch->header_len - sizeof(SCIONCommonHeader) - padded_addr_len(buf);
}

/*
 * Initialize OF indices (pointers)
 * buf: Pointer to start of SCION packet
{'fraction_non_alphanumeric': 0.12008660892738175, 'fraction_numerical': 0.010493004663557627, 'mean_word_length': 3.65143299767622, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '473610', 'n_tokens_mistral': 2230, 'n_tokens_neox': 2188, 'n_words': 633}