Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Help with IPv4 please

extract variable type

  • Please log in to reply
7 replies to this topic

#1 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 17 November 2011 - 09:23 AM

Hello,

Trying to "extract" from a pcap file source and destination IP addresses. Is my first contact with this stuff and networking so I'm lil bit confused. How I will get IP and print it out in dotted decimal notation?
For now I have...
struct ip *ip_hdr = (struct ip *)pkt_pointer;
In this struct there are references for ip_src and ip_dst but those are type of struct in_addr. So, I don't know what to do next in order to get those addresses and in what variable type need to store them. and how to convert them in decimal dotted notation.

Thank you
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#2 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 17 November 2011 - 06:19 PM

unsigned int network_address = ip_hdr->sin_addr.s_addr;

int oct0 = network_address >> 24;
int oct1 = network_address >> 16;
int oct2 = network_address >> 8;
int oct3 = network_address >> 0;

cout << oct0 << "." << oct1 << "." << oct2 << "." << oct3;
Now it's just a matter of putting those in a string.
  • 0

The roots of education are bitter, but the fruit is sweet.


#3 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 19 November 2011 - 02:54 AM

Thank you Flying Dutchman, a question what "cout" stands for? And when you are saying "those" for what part you are talking?
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#4 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 19 November 2011 - 05:12 AM

cout is an C++ object that represents standard output (usually the screen). It's equivalent to stdout in C.

By those I mean oct0 to oct3 and by putting it in a string I mean transforming it to dotted decimal notation.
  • 0

The roots of education are bitter, but the fruit is sweet.


#5 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 19 November 2011 - 06:40 AM

Because I'm not so expert with C, a friend of mine, suggest me to use memcpy(). What do you believe, will work too?
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#6 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 19 November 2011 - 08:14 AM

memcpy is ok, but I would suggest you sprintf, mainly because of easier formatting.
  • 0

The roots of education are bitter, but the fruit is sweet.


#7 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 19 November 2011 - 08:29 AM

So for my example will be
char ip[20];
int i;
i = spintf(ip,"%d.%d.%d.%d",oct0,oct1,oct2,oct3);
printf("%s",ip);

?

---------- Post added at 04:29 PM ---------- Previous post was at 04:20 PM ----------

Until now I have this:
struct ip *ip_hdr = (struct ip *)pkt_pointer;
		memcpy(_ip_hdr, ip_hdr, sizeof(struct ip));
		unsigned int network_address = _ip_hdr->ip_src.s_addr;
		int oct0 = network_address >> 24;
		int oct1 = network_address >> 16;
		int oct2 = network_address >> 8;
		int oct3 = network_address >> 0;

When trying to print out the oct..
printf("%d.%d.%d.%d",oct0,...);

Getting the same weird result for all. "8.2048.524357.134235392"
Didn't understand your previous post, how to store them in a string, and make it looks like IP addr. :(
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#8 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 19 November 2011 - 08:10 PM

Could you post your ip structure, please. Also, did you check out pcap file format?
  • 0

The roots of education are bitter, but the fruit is sweet.






Also tagged with one or more of these keywords: extract, variable type

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download