Thanks Salem for your last post. It's a pity the forum doesn't have a "like" button so that people who are assisted by responses can acknowledge those useful responses without having to add a separate post (or not acknowledging them at all).
BTW every time I copy & paste code into the message preview, for every space in my code I get two spaces when I copy & paste. I have tried using notepad rather than copying directly from the IDE. But that made no difference. Has anybody else encountered this behaviour? And if so, is there a workaround?
I now have a working version of the code, however there are two warnings which I don't understand - and ideally I would like to.
The warnings are:
Code:
main.c|74|warning: passing argument 1 of ‘set_pcap_user_data’ from incompatible pointer type [enabled by default]|
Statement:
Code:
set_pcap_user_data(&pcap_user_data);
And
Code:
main.c|76|warning: passing argument 1 of ‘dump_fwd_process_packet_options_list’ from incompatible pointer type [enabled by default]|
Statement:
Code:
dump_fwd_process_packet_options_list(&pcap_user_data);
Full code
Code:
#include <stdio.h>
#include <pthread.h>
// new_activity list
struct new_activity_record
{
char c_ip_address[16];
int i_local_port;
};
typedef struct new_activity_record new_activity_record_t;
struct node_new_activity
{
new_activity_record_t new_activity_record;
struct node_new_activity *next;
struct node_new_activity *prev;
};
typedef struct node_new_activity node_new_activity_t;
struct new_activity_list
{
pthread_mutex_t mutex;
node_new_activity_t *head;
node_new_activity_t *tail;
};
typedef struct new_activity_list new_activity_list_t;
struct process_packet_options_record
{
char c_local_subnet[12];
int i_subnet_mask;
};
typedef struct process_packet_options_record process_packet_options_record_t;
struct process_packet_options_list
{
process_packet_options_record_t *process_packet_options_record;
struct process_packet_options_list *prev;
struct process_packet_options_list *next;
};
typedef struct process_packet_options_list process_packet_options_list_t;
struct pcap_user_data
{
new_activity_list_t *new_activity_list;
process_packet_options_list_t *process_packet_options_list;
};
typedef struct pcap_user_data pcap_user_data_t;
// declare functions
void set_pcap_user_data(pcap_user_data_t *pcap_user_data);
process_packet_options_list_t *add_at_process_packet_options_list(int pos, process_packet_options_list_t *process_packet_options_list, process_packet_options_record_t *process_packet_options_record);
process_packet_options_list_t *add_beg_process_packet_options_list(process_packet_options_list_t *process_packet_options_list, process_packet_options_record_t *process_packet_options_record);
void dump_fwd_process_packet_options_list(pcap_user_data_t *pcap_user_data);
int main()
{
// Initialise pcap_user_data
pcap_user_data_t *pcap_user_data = malloc(sizeof(pcap_user_data_t) * 1);
if (pcap_user_data == NULL)
{
printf("Malloc failed in: Initialise pcap_user_data\n");
getchar();
}
pcap_user_data = NULL;
set_pcap_user_data(&pcap_user_data);
dump_fwd_process_packet_options_list(&pcap_user_data);
return 0;
} // main
void set_pcap_user_data(pcap_user_data_t *pcap_user_data)
{
// Initialise New_Activity_List
new_activity_list_t *new_activity_list = malloc(sizeof(new_activity_list_t) * 1);
if (new_activity_list == NULL)
{
printf("Malloc failed in new_activity_list: set_pcap_user_data\n");
getchar();
}
pcap_user_data->new_activity_list = new_activity_list;
pthread_mutex_init(&pcap_user_data->new_activity_list->mutex,NULL);
pcap_user_data->new_activity_list->head = pcap_user_data->new_activity_list->tail = NULL;
// Initialise Process_Packet_Options_List
process_packet_options_list_t *process_packet_options_list = malloc(sizeof(process_packet_options_list_t) * 1);
if (process_packet_options_list == NULL)
{
printf("Malloc failed in process_packet_options_list: set_pcap_user_data\n");
getchar();
}
pcap_user_data->process_packet_options_list = process_packet_options_list;
pcap_user_data->process_packet_options_list = NULL;
// Initialise record
process_packet_options_record_t *process_packet_options_record = malloc(sizeof(process_packet_options_record_t) * 1);
if (process_packet_options_record == NULL)
{
printf("Malloc failed in process_packet_options_record: set_pcap_user_data\n");
getchar();
}
// Create process_packet_options_record
strcpy(process_packet_options_record->c_local_subnet,"192.168.200");
process_packet_options_record->i_subnet_mask = 24;
// Add process_packet_options_record to process_packet_options_list
pcap_user_data->process_packet_options_list = add_beg_process_packet_options_list(pcap_user_data->process_packet_options_list, process_packet_options_record);
// Create process_packet_options_record
strcpy(process_packet_options_record->c_local_subnet,"192.168.201");
process_packet_options_record->i_subnet_mask = 24;
// Add process_packet_options_record to process_packet_options_list
pcap_user_data->process_packet_options_list = add_beg_process_packet_options_list(pcap_user_data->process_packet_options_list, process_packet_options_record);
return;
} // set_pcap_user_data
process_packet_options_list_t *add_at_process_packet_options_list(int pos, process_packet_options_list_t *process_packet_options_list, process_packet_options_record_t *process_packet_options_record)
{
process_packet_options_list_t *node = malloc(sizeof(process_packet_options_list_t) * 1);
if (node == NULL)
{
printf("Malloc failed in node: add_at_process_packet_options_list\n");
getchar();
}
node->process_packet_options_record = malloc(sizeof(process_packet_options_record_t) * 1);
if (node->process_packet_options_record == NULL)
{
printf("Malloc failed in: add_at_process_packet_options_list: node->process_packet_options_record\n");
getchar();
}
strcpy(node->process_packet_options_record->c_local_subnet,process_packet_options_record->c_local_subnet);
node->process_packet_options_record->i_subnet_mask = process_packet_options_record->i_subnet_mask;
node->prev = NULL;
node->next = NULL;
// handle case where linked_list is empty
if (process_packet_options_list == NULL)
{
process_packet_options_list = node;
return process_packet_options_list;
}
int idx = 0;
process_packet_options_list_t *prev = NULL;
process_packet_options_list_t *cur = process_packet_options_list;
// walk through linked_list until pos or end is reached
while (cur != NULL && idx != pos)
{
++idx;
prev = cur;
cur = cur->next;
}
// insertion point reached
// beginning, includes linked_list update
if (idx == 0)
{
process_packet_options_list = node;
node->next = cur;
cur->prev = node;
return process_packet_options_list;
}
// end
if (cur == NULL)
{
prev->next = node;
node->prev = prev;
return process_packet_options_list;
}
// middle
prev->next = node;
node->prev = prev;
node->next = cur;
cur->prev = node;
return process_packet_options_list;
} // *add_at_process_packet_options_list
process_packet_options_list_t *add_beg_process_packet_options_list(process_packet_options_list_t *process_packet_options_list, process_packet_options_record_t *process_packet_options_record)
{
process_packet_options_list = add_at_process_packet_options_list(0, process_packet_options_list, process_packet_options_record);
return process_packet_options_list;
} // *add_beg_process_packet_options_list
void dump_fwd_process_packet_options_list(pcap_user_data_t *pcap_user_data)
{
printf("Process Packet Options:\n");
printf("-----------------------\n");
process_packet_options_list_t *linked_list;
linked_list = pcap_user_data->process_packet_options_list;
while (linked_list != NULL)
{
printf("Subnet: %s\n", linked_list->process_packet_options_record->c_local_subnet);
printf("Subnet mask: %d\n", linked_list->process_packet_options_record->i_subnet_mask);
printf("\n");
linked_list = linked_list->next;
}
return;
} // dump_fwd_process_packet_options_list