Hi I am new to socket Programming and C. I have done a part of my project which includes passive analysis of packets, extracting the 5 tuple values needed to compare for getting the flows and and errors in a .pcap bundled file I have been provided below is a part of my code.
Code:
#include<libtrace.h>
#include<stdio.h>
#include<inttypes.h>
#include<arpa/inet.h>
#include<netinet/tcp.h>
uint64_t count = 0;
typedef struct {
int srport, dsport, seq;
char src[16], dest[16];
}flows;
flows new_flow[1051], flow;
void per_packet(libtrace_packet_t *packet)
{
struct sockaddr addr;
struct sockaddr *sa;
struct sockaddr *sd;
struct sockaddr daddr;
char buffer[5000];
int sport, dport, count_flow=0;
count +=1;
sa = trace_get_source_address(packet, &addr);
printf("packet is type %d af_inet=%d\n", sa->sa_family, AF_INET);
if (sa->sa_family == AF_INET) {
/* ipv4 packet */
struct sockaddr_in *sa_i = (struct sockaddr_in *) sa;
printf("Source Address:%s\n", inet_ntoa(sa_i->sin_addr));
buffer[5000];
inet_ntop(AF_INET, &(sa_i->sin_addr), new_flow->src, INET_ADDRSTRLEN);
} else if (sa->sa_family == AF_INET6) {
//inet_ntop(AF_INET, &(sa_i->sin_addr), flow->src, INET6_ADDRSTRLEN);
/* ipv6 packet */
} else {
/* unknown packet */
}
sport=trace_get_source_port(packet);
printf("Source Port is: %d\n", htons(sport));
flow->srport = htons(sport);
sd=trace_get_destination_address(packet, &daddr);
struct sockaddr_in *sd_in = (struct sockaddr_in *) sd;
printf("Destination Adress:%s\n", inet_ntoa(sd_in->sin_addr));
inet_ntop(AF_INET, &(sd_in->sin_addr), new_flow->dest, INET_ADDRSTRLEN);
dport=trace_get_destination_port(packet);
printf("Destination port is: %d\n", htons(dport));
libtrace_tcp_t *t=trace_get_tcp(packet);
printf("Sequence No. for this packet is: %u\n", ntohl(t->seq));
I am using libtrace library for extracting information from the packets. Now what I have to do is to store all these values in an array where I can later compare the values to get the no. of flows and errors. I was using
Code:
inet_ntop(AF_INET, &(sd_in->sin_addr), new_flow->dest, INET_ADDRSTRLEN);
This function I was using to store the destination address and
Code:
inet_ntop(AF_INET, &(sa_i->sin_addr), new_flow->src, INET_ADDRSTRLEN);
to store the source address but when I try to print specific values in the new_flow array for instance flow[1000].dest It gives me garbled data not the data I was expecting. Please help me with this, I would be really grateful to you
Thanks.