Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello. I am writing a simple IDS and I had a problems.. In the code below this code is compiled and running but it just run 1 loop then exited the program.. How to run it all for the loop?
I think it's stopped by strcpy() function..


C#
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
    const struct sniff_ethernet *ethernet;  /* The ethernet header [1] */
    const struct sniff_ip *ip;              /* The IP header */
    

    char *src, *dst;
 



        //Return the first one;
    strcpy(src , inet_ntoa(ip->ip_src));
    strcpy(dst , inet_ntoa(ip->ip_dst));




if(src=dst)
{
printf("LAND");
}
else if(src!=dst)
{
printf("normal");
}
}


I am using pcap library..
Posted
Comments
Sergey Alexandrovich Kryukov 11-Dec-13 3:06am    
And how it that related to C++/CLI? :-)
—SA
CPallini 11-Dec-13 3:41am    
Did you realize you didn't initialize ip?
It is probably pointing to garbage, hence is useless.

Hello,

You comparing the pointers but not the strings.
First you are not allocating the memory for src and dst variables which may cause crash.
To compare strings this way use strcmp or memcmp functions.
C++
char src[100],dst[100];
strcpy(src , inet_ntoa(ip->ip_src));
strcpy(dst , inet_ntoa(ip->ip_dst));
if (strcmp(src,dst) == 0)
{
}
else
{
}

Regards,
Maxim.
 
Share this answer
 
Comments
Purevochir 11-Dec-13 3:27am    
Hello.. I just want to campare ip addresses

this can calculate source ip address inet_ntoa(ip->ip_src) and this calculates destination address inet_ntoa(ip->ip_dst) i just used these functions to equal by if conditions
like

if(inet_ntoa(ip->ip_src)== inet_ntoa(ip->ip_dst))
{
printf("Land")
}

But it always equals and then prints "Land"

So I thought inet_ntoa() function returning just 0 1. Am I right?
Maxim Kartavenkov 11-Dec-13 3:32am    
inet_ntoa - returns the string of IPv4 in format "a.b.c.d" so you should use comparing the strings in this case like I mention. But you can compare directly the addresses without converting then into the strings: ip->ip_dst == ip->ip_src
CPallini 11-Dec-13 3:34am    
5.
nv3 11-Dec-13 3:36am    
Good catch! 5.
Purevochir 11-Dec-13 3:37am    
I used it but it takes this error invalid operands to binary == (have ‘const struct in_addr’ and ‘const struct in_addr’). How to repair this error?
Regarding your question: I don't see any loop in your code.

But anyway, you seem to have confused a couple of simple concepts:
C++
char *src, *dst;
strcpy(src , inet_ntoa(ip->ip_src));
strcpy(dst , inet_ntoa(ip->ip_dst));

src and dst are just uninitialized pointers and they might point anywhere. So the strcpy is going to overwrite randomly some memory in your program.

C++
if(src=dst)
{
    printf("LAND");
}
else if(src!=dst)
{
    printf("normal");
}

In the if-statement you confused "=" with "==". And the "if (src!=dst)" in the else branch is useless. if src is not equal dst it must obviously be non-equal!
 
Share this answer
 
Comments
CPallini 11-Dec-13 3:34am    
5.
nv3 11-Dec-13 4:01am    
Thanks!
Hi!

First of all
C++
if(src=dst)

is wrong. You probably mean:
C++
if(src==dst)


However, that still would not bring the desired results, since you are comparing pointers.
If you want to compare strings in C, you should use the strcmp function.
It is defined in string.h
C++
#include <string.h> //In C
#include <cstring> //In C++


So, for example the code would look something like this:
C++
#include <cstring>

...

if(strcmp(src, dst) == 0)
{
    printf("LAND");
}
else
{
    printf("normal");
}


Hope I didn't misunderstand anything :)
 
Share this answer
 
v3
You have declared src and dst to be pointers to a char.
They are not initialised, and do not point anywhere.
Then you try to copy data to them. This will crash.

Likewise, you declare *ip to be a pointer, but it is not set to point to anything. You then use it in the strcpy.

You need to read and understand about how pointers work in C code, then the errors in this will be easily spotted. I recommend the K&R book on the C language. (although I read that before the internet was around - there are probably some very good web pages on this now)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900