I have problem with my code. my global variable not changed. I have assigned its address to a pointer. This is my struct initialization:
struct PortData {
int port;
int sent;
int received;
int total;
struct PortData *Next;
};
struct IPData {
time_t timestamp;
uint32_t ip;
struct PortData Record;
};
this is my function which returned the address:
inline struct IPData *FindIp(uint32_t ipaddr) {
unsigned int counter;
for (counter = 0; counter < IpCount; counter++)
if (IpTable[counter].ip == ipaddr)
return (&IpTable[counter]);
if (IpCount >= IP_NUM) {
syslog(LOG_ERR, "IP_NUM is too low, dropping ip....");
return (NULL);
}
memset(&IpTable[IpCount], 0, sizeof (struct IPData));
IpTable[IpCount].ip = ipaddr;
return (&IpTable[IpCount++]);
}
and here where the pointer assigned to the address of IpTable:
struct IPData *ptrIPData;
for (Count = 0; Count < SubnetCount; Count++) {
if (SubnetTable[Count].ip == (iph->saddr & SubnetTable[Count].mask)) {
ptrIPData = FindIp(iph->saddr);
Credit(&(ptrIPData->Record), iph, tcph, srcip);
}
if (SubnetTable[Count].ip == (iph->daddr & SubnetTable[Count].mask)) {
ptrIPData = FindIp(iph->daddr);
Credit(&(ptrIPData->Record), iph, tcph, dstip);
}
}
This is my Credit() function:
inline void Credit(struct PortData *pordt, struct iphdr *iph, struct tcphdr *tcph, struct in_addr sipaddr) {
unsigned int sport, dport;
memset(&source, 0, sizeof (source));
source.sin_addr.s_addr = iph->saddr; //init source ip
sport = ntohs(tcph->source);
memset(&dest, 0, sizeof (dest));
dest.sin_addr.s_addr = iph->daddr; //init dest ip
dport = ntohs(tcph->dest);
packet_size = ntohs(iph->tot_len);
if (iph->protocol == 6) //6 is protocol TCP
{
prev = portdt;
int sameport = 0;
while (prev != NULL) {
if (dport == prev->port || sport == prev->port) {
if (dport == prev->port) {
prev->sent += packet_size;
}
if (sport == prev->port) {
prev->received += packet_size;
}
sameport = 1;
break;
}
}
if (sameport == 0) {
printf("create new node\n");
newnode = (struct PortData*) malloc(sizeof (struct PortData));
newnode->received = 0;
newnode->sent = 0;
if (sipaddr.s_addr == source.sin_addr.s_addr) {
if (tcph->syn == 1 || tcph->ack == 1) {
newnode->port = dport;
newnode->sent = packet_size;
}
}
if (sipaddr.s_addr == dest.sin_addr.s_addr) {
if (tcph->syn == 1 && tcph->ack == 1) {
newnode->port = sport;
newnode->received = packet_size;
}
}
newnode->Next = portdt;
portdt = newnode;
}//==end-sameport==
}//iph->protocol//
prev = portdt;
while (prev != NULL) {
fprintf(logfile, "ip = %s port=%d sent=%d bytes received=%d bytes\n", inet_ntoa(sipaddr), prev->port, prev->sent, prev->received);
prev = prev->Next;
}
}
I assumed after executed the Credit() function, value of Iptable[Counter].Record must changed because ptrIPData pointed to it address. but why it does not?
if (tcph->syn == 1 && tcph->ack == 1)twice by moving it. Be consistent in your variable naming: CamelCase or lower?iph->protocolis really6?while (prev != NULL)can become infinite loop ifprevis notNULLbefore entering loop, becauseprevis never assigned inside loop.Credit()function works properly. The number of sent and received has increased by thepacket_sizewhen this function executed. okay, I will assigned theprevinto NULL.