Skip to content
Extraits de code Groupes Projets
packet_interface.c 4,37 ko
Newer Older
  • Learn to ignore specific revisions
  • #include "packet_interface.h"
    
    #include <arpa/inet.h>
    #include <zlib.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    /*************** Structures definition ***************/
    struct __attribute__((__packed__)) pkt {
        header_t header;
        uint8_t payload[512];
    };
    
    typedef uint8_t header_t[32];
    
    /************* Functions definition *************/
    pkt_t* pkt_new()
    {
        pkt_t * toReturn = (pkt_t *) malloc(sizeof(pkt_t));
        return  toReturn;
    }
    
    void pkt_del(pkt_t *pkt)
    {
        free(pkt);
    }
    
    /**
     * @brief Calculates the CRC32
     * 
     * @param buffer    : The buffer on which to calculate CRC32.
     * @param len       : The number of bytes of the buffer.
     * @return uint32_t 
     */
    uint32_t calculate_crc(char * buffer, uint32_t len)
    {
        return (uint32_t) crc32(0, (const void *) buffer, len);
    }
    
    pkt_status_code pkt_decode_data_fec(const char *data, const size_t len, pkt_t *pkt)
    {
        
    }
    
    pkt_status_code pkt_decode_ack_nack(const char *data, const size_t len, pkt_t *pkt)
    {
        if (len < 8)
            return E_NOHEADER;
        
        // REMINDER : *data will read the first byte
        ptypes_t type = ((*data) & 0xC0) >> 6; 
        if ( type != PTYPE_ACK && type != PTYPE_NACK )
            return E_TYPE;
    
        if ( ((*data) & 0x20) != 0)
            return E_TR;
        
        uint8_t seqnum, window;
        memcpy((char *) &seqnum, data+1, 1);
        memcpy((char *) &window, data, 1);
        window = window & 0x1F;
        if (seqnum > window)
            return E_SEQNUM;
    
        // CheckSum no need to set TR at 0 as it should be at 0
        char crc[4];
        memcpy(crc, data+4, 4);
        if (calculate_crc(data, 4) != ntohl((uint32_t) crc))
            return E_CRC;    
    }
    
    pkt_status_code pkt_decode(const char *data, const size_t len, pkt_t *pkt)
    {
        int is_ack_nack = len <= 8; // Maximum ACK or NACK size is 12 bytes
        if (is_ack_nack)
            return pkt_decode_ack_nack(data, len, pkt);
        else
            return pkt_decode_data_fec(data, len, pkt);
    }
    
    pkt_status_code pkt_encode(const pkt_t* pkt, char *buf, size_t *len)
    {
        /* Your code will be inserted here */
    }
    
    ptypes_t pkt_get_type  (const pkt_t* pkt)
    {
        /* Your code will be inserted here */
    }
    
    uint8_t  pkt_get_tr(const pkt_t* pkt)
    {
        /* Your code will be inserted here */
    }
    
    uint8_t  pkt_get_window(const pkt_t* pkt)
    {
        /* Your code will be inserted here */
    }
    
    uint8_t  pkt_get_seqnum(const pkt_t* pkt)
    {
        /* Your code will be inserted here */
    }
    
    uint16_t pkt_get_length(const pkt_t* pkt)
    {
        /* Your code will be inserted here */
    }
    
    uint32_t pkt_get_timestamp   (const pkt_t* pkt)
    {
        /* Your code will be inserted here */
    }
    
    uint32_t pkt_get_crc1   (const pkt_t* pkt)
    {
        /* Your code will be inserted here */
    }
    
    uint32_t pkt_get_crc2   (const pkt_t* pkt)
    {
        /* Your code will be inserted here */
    }
    
    const char* pkt_get_payload(const pkt_t* pkt)
    {
        /* Your code will be inserted here */
    }
    
    
    pkt_status_code pkt_set_type(pkt_t *pkt, const ptypes_t type)
    {
        /* Your code will be inserted here */
    }
    
    pkt_status_code pkt_set_tr(pkt_t *pkt, const uint8_t tr)
    {
        /* Your code will be inserted here */
    }
    
    pkt_status_code pkt_set_window(pkt_t *pkt, const uint8_t window)
    {
        /* Your code will be inserted here */
    }
    
    pkt_status_code pkt_set_seqnum(pkt_t *pkt, const uint8_t seqnum)
    {
        /* Your code will be inserted here */
    }
    
    pkt_status_code pkt_set_length(pkt_t *pkt, const uint16_t length)
    {
        /* Your code will be inserted here */
    }
    
    pkt_status_code pkt_set_timestamp(pkt_t *pkt, const uint32_t timestamp)
    {
        /* Your code will be inserted here */
    }
    
    pkt_status_code pkt_set_crc1(pkt_t *pkt, const uint32_t crc1)
    {
        /* Your code will be inserted here */
    }
    
    pkt_status_code pkt_set_crc2(pkt_t *pkt, const uint32_t crc2)
    {
        /* Your code will be inserted here */
    }
    
    pkt_status_code pkt_set_payload(pkt_t *pkt,
                                    const char *data,
                                    const uint16_t length)
    {
        /* Your code will be inserted here */
    }
    
    
    ssize_t varuint_decode(const uint8_t *data, const size_t len, uint16_t *retval)
    {
        /* Your code will be inserted here */
    }
    
    
    ssize_t varuint_encode(uint16_t val, uint8_t *data, const size_t len)
    {
        /* Your code will be inserted here */
    }
    
    size_t varuint_len(const uint8_t *data)
    {
        /* Your code will be inserted here */
    }
    
    
    ssize_t varuint_predict_len(uint16_t val)
    {
        /* Your code will be inserted here */
    }
    
    
    ssize_t predict_header_length(const pkt_t *pkt)
    {
        /* Your code will be inserted here */
    }