Jump to content

Help with IPv4 please

- - - - -

  • Please log in to reply
7 replies to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
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

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

-Michael Sinz

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
toto_7

toto_7

    Programming Professional

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

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

-Michael Sinz

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
toto_7

toto_7

    Programming Professional

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

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

-Michael Sinz

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
memcpy is ok, but I would suggest you sprintf, mainly because of easier formatting.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
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. :(

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

-Michael Sinz

#8
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Could you post your ip structure, please. Also, did you check out pcap file format?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users