473,763 Members | 4,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer arithmetic and packet capture

Ok, I have something that works, but I don't understand why.

I use libpcap to get data of the wire and then initially I casted the
packet to ethernet, then marched along the memory in chunks of bytes for
the length of the ethernet header until I reached the IP header and so
forth...

So, I changed my code and now process the ethernet header after which I
pass the size of the the ethernet header to be deducted and a pointer to
the the location in the memory where the ethernet header starts like

handle_ip(ether _header_size,(v oid*)eptr);

The function that hanldes the ip has the following prototype:

void handle_ip(u_int 32_t ether_hdr_size, void* eptr){

and I use the following function to crawl along the memory (in what I
assume to be byte sized chunks) to the IP header:

ip = (struct ip*)(eptr + ether_hdr_size) ;

My question is why does this work if I pass a void* in to the functions,
but not if I pass a pointer to a ether struct? I get garbage when I don't
do the void* cast.

Thanks for the help in advance.
Pieter

Nov 14 '05 #1
1 1643
Pieter Claassen wrote:
Ok, I have something that works, but I don't understand why.

I use libpcap to get data of the wire and then initially I casted the
packet to ethernet, then marched along the memory in chunks of bytes for
the length of the ethernet header until I reached the IP header and so
forth...

So, I changed my code and now process the ethernet header after which I
pass the size of the the ethernet header to be deducted and a pointer to
the the location in the memory where the ethernet header starts like

handle_ip(ether _header_size,(v oid*)eptr);

The function that hanldes the ip has the following prototype:

void handle_ip(u_int 32_t ether_hdr_size, void* eptr){
Minor point: It's far more natural to measure the size
of something with the Standard type `size_t' than with a
made-up type like `u_int32_t_gizm o'.
and I use the following function to crawl along the memory (in what I
assume to be byte sized chunks) to the IP header:

ip = (struct ip*)(eptr + ether_hdr_size) ;
This is illegal, and the compiler should complain. Since
it didn't I suspect (1) that you may be using gcc, which allows
arithmetic on `void*' pointers as a non-conforming language
extension, and (2) that if so, you're invoking the compiler in
a non-conforming mode. Use the "-ansi -pedantic" switches if
the code's clean enough to withstand them.
My question is why does this work if I pass a void* in to the functions,
but not if I pass a pointer to a ether struct? I get garbage when I don't
do the void* cast.


You need to review pointer arithmetic in your favorite
C textbook. If `p' is a pointer to a Thing object, then
`p+1' is a pointer to the next complete Thing that follows
the first. The "step size" already takes sizeof(Thing) into
account -- more accurately, it takes sizeof(*p) into account,
because it's the type of the pointer that determines the
size of the step. If `p' has the type `Thing*', `p+1' advances
by sizeof(Thing) bytes. If `q' has the type `char*' and
happens to point to the same location `p' does, `q+1' will
advance by just one byte, which will (in general) not be the
same location as `p+1'.

And if `r' is a `void*', `r+1' is an error because
sizeof(*r) is sizeof(void), which is undefined.

--
Er*********@sun .com

Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2294
by: Francois Grieu | last post by:
Are these programs correct ? #include <stdio.h> unsigned char a = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+j-1 )); return 0; }
7
528
by: barikat | last post by:
int a; int *Ptr1, *Ptr2; Ptr1 = a; Ptr1++; Ptr2 = a; printf("Ptr1 : %p\n", Ptr1); printf("Ptr2 : %p\n\n", Ptr2);
26
3062
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
1
5544
by: khaled | last post by:
hi i am developing a java program to capture the network traffic (TCP/IP) using the jpcap.jar files and when i intends to run the examples with it i get the follwing error: cannot access jpcap.packet.EthernetPacket bad class file: C:\j2sdk1.4.2_09\jre\lib\ext\jpcap.jar(jpcap/packet/EthernetPacket.class) class file has wrong version 49.0, should be 48.0 Please remove or make sure it appears in the correct subdirectory of the classpath....
1
6485
by: visal | last post by:
hi Please help me.i am developing a java pgm to capture network packets using jpcap.i downloded jpcap and wpcap dll files. My pgm is: import jpcap.JpcapHandler; import jpcap.Jpcap; import jpcap.Packet; public class sample implements JpcapHandler { public void handlePacket(Packet packet){ System.out.println(packet);
3
3657
by: nexus024 | last post by:
I am trying to write a program that will continuously sniff eth0 for a specific UDP packet thats being sent to a specific destination IP, alter the data of the packet, and finally transmit it to the destination. My script compiles fine and runs fine until it finds the specific packet and tries to alter the payload of the data. Hopefully someone can give me some insight on why it might be doing this... #!/usr/bin/perl -w # # Custom script:...
1
3394
by: sangith | last post by:
Hi, I tried the packet capture module program. I did a file transfer using ftp from this host to another server. But when I ran the program, it was just hanging off and it did not print the src ip, dst ip, src port, dst port. Should I run this program as a Daemon? If so, how do I do that? I would appreciate your response.
6
1706
by: chris.kemmerer | last post by:
I am having a problem with templates and I hope someone here can help. I am writing a library that accepts data packets, parses them and saves the information for later use. One member of the packet is an enumeration that says what type of data the packet contains (int, char, etc.). I have created classes similar to below. The problem I am having is in trying to access the derived class given only a pointer to the base class. I know that...
19
3902
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.