Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have two functions i need help with. First function will be labeled as : (stru

ID: 3628202 • Letter: I

Question

I have two functions i need help with.
First function will be labeled as :
(struct packet_t *hpot_access(struct honeypot_t *honey, int i);
hpot_access should return a pointer to the packet_t memory block that is found in the list index position specified as a parameter. The memory block should not be removed from the list. If the index is out of bounds or if no packet record is found at the index position, the function returns NULL.

The Second function will be labled as:
(struct packet_t *hpot_remove(struct honeypot_t *honey, int i);)
hpot_remove should return a pointer to the packet_t memory block that is found in the list index position specified as a parameter. The record should be removed from the list, and the resulting list should still be sequential with no gaps between entries in the list. If the index is out of bounds or if no packet record is found at the index position, the function returns NULL.


Both these function refer to two structure with contains the fallowing code:
struct honeypot_t {
int pot_size;
struct packet_t **hpot_ptr;
};

struct packet_t {
int dest_ip_addr; /*IP address of destination*/
int src_ip_addr; /*IP address of source*/
int dest_port_num; /*port number at destination*/
int src_port_num; /*port number at source host*/
int hop_count; /*number of routers in route*/
int protocol; /*TCP=1, UDP=2, SSL=3, RTP=4*/
float threat_score; /*rating of source host*/
int time_received; /*time in seconds packet received*/
};

Explanation / Answer

#include <stdio.h>
#include <malloc.h>
#include <assert.h>

struct honeypot_t {
    int pot_size;
    struct packet_t **hpot_ptr;
};

struct packet_t {
    int dest_ip_addr; /*IP address of destination*/
    int src_ip_addr; /*IP address of source*/
    int dest_port_num; /*port number at destination*/
    int src_port_num; /*port number at source host*/
    int hop_count; /*number of routers in route*/
    int protocol; /*TCP=1, UDP=2, SSL=3, RTP=4*/
    float threat_score; /*rating of source host*/
    int time_received; /*time in seconds packet received*/
};

/*
* hpot_access should return a pointer to the packet_t memory block
* that is found in the list index position specified as a parameter.
* The memory block should not be removed from the list. If the index
* is out of bounds or if no packet record is found at the index position,
* the function returns NULL.
*/
struct packet_t *hpot_access(struct honeypot_t *honey, int i)
{
    struct packet_t *onePacket = NULL;
    if( honey != NULL )
    {
        if( (i > -1) && (honey->pot_size > i) )
        {
            // If not found, pointer will be null.
            // Therefore, return value should be null.
           >         }
    }
    return onePacket;
}

/**
* hpot_remove should return a pointer to the packet_t memory block that is
* found in the list index position specified as a parameter. The record
* should be removed from the list, and the resulting list should still
* be sequential with no gaps between entries in the list. If the index
* is out of bounds or if no packet record is found at the index position,
* the function returns NULL.
*/
struct packet_t *hpot_remove(struct honeypot_t *honey, int i)
{
    struct packet_t *onePacket = NULL;
    struct packet_t **new_hpot_ptr;
    int new_size;
    int x;

    if( honey != NULL )
    {
        if( (i > -1) && (honey->pot_size > i) )
        {
            // If not found, pointer will be null.
            // Therefore, return value should be null.
           >             if( onePacket != NULL )
            {
                // Perform the removal.
                new_size = honey->pot_size - 1;
                if( new_size == 0 )
                {
                    // No data left.
                    honey->hpot_ptr = NULL;
                }
                else
                {
                    // Create new (smaller) array.
                    new_hpot_ptr = (struct packet_t **) calloc (new_size,sizeof(struct packet_t *));

                    // Replicate the portion before the deletion.
                    for( x = 0; x < i ; x++ )
                    {
                        new_hpot_ptr[x] = honey->hpot_ptr[x];
                    }
                    // Replicate the portion after the deletion.
                    // Start after the deletion point.
                    for( x = i+1; x < honey->pot_size ; x++ )
                    {
                        // After the deletion point, the index into the new array is shifted.
                        new_hpot_ptr[x-1] = honey->hpot_ptr[x];
                    }
                    // Replace the array and size.
                    honey->hpot_ptr = new_hpot_ptr;
                }
                honey->pot_size = new_size;
            }
        }
    }
    return onePacket;
}

int main(int argc, char** argv)
{
    // Create honey hot.
    struct honeypot_t myHoney;
    myHoney.pot_size = 3;
    struct packet_t *myPacketArray[3];
    struct packet_t p1, p2, p3;
    myPacketArray[0] = &p1;
    myPacketArray[1] = &p2;
    myPacketArray[2] = &p3;
    myHoney.hpot_ptr = myPacketArray;

    /**
    * Test access
    */
    struct packet_t *result = hpot_access(&myHoney, 3);
    assert( result == NULL );

    result = hpot_access(&myHoney, 2);
    assert( result == &p3 );

    result = hpot_access(&myHoney, 1);
    assert( result == &p2 );

    result = hpot_access(&myHoney, 0);
    assert( result == &p1 );

    result = hpot_access(&myHoney, -1);
    assert( result == NULL );

    /**
    * Test removal
    */
    result = hpot_remove(&myHoney, 3);
    assert( result == NULL );

    result = hpot_remove(&myHoney, -1);
    assert( result == NULL );

    result = hpot_remove(&myHoney, 1);
    assert( result == &p2 );
    assert( myHoney.pot_size == 2 );
    assert( myHoney.hpot_ptr[0] == &p1 );
    assert( myHoney.hpot_ptr[1] == &p3 );

    result = hpot_remove(&myHoney, 1);
    assert( result == &p3 );
    assert( myHoney.pot_size == 1 );
    assert( myHoney.hpot_ptr[0] == &p1 );

    result = hpot_remove(&myHoney, 0);
    assert( result == &p1 );
    assert( myHoney.pot_size == 0 );
    assert( myHoney.hpot_ptr == NULL );

    return 0;
}