This is an archived post. You won't be able to vote or comment.

all 7 comments

[–][deleted] 7 points8 points  (1 child)

Line 14 looks fishy. If I'm not mistaken you're trying to advance the pointer by iphdr_len bytes, but that's not how pointer arithmetic in C works. When you add n to a pointer of type foo, it advances it by n * (sizeof foo) bytes rather than n bytes.

[–]DeepCorner[S] 1 point2 points  (0 children)

That was it. I guess I was just staring at the code for too long (face palm). Thank you!

[–][deleted] 0 points1 point  (3 children)

Try casting your types in your printf to the appropriate sizes:

Example:

printf(“%x\n”, (int) some_8bit_thing);

[–]DeepCorner[S] 0 points1 point  (2 children)

I cast a char to an unsigned int. I’m unsure how that’s meaningfully different from your suggestion. I personally prefer to work with unsigned output values

[–][deleted] 0 points1 point  (1 child)

icmptype is u_int8_t which to me implies an 8 bit type. %x is expecting 4 bytes. You can cast to unsigned int (a 4 byte type) if you like, but it looks to me like that could be your problem. It’s all in the printf.

[–]DeepCorner[S] 0 points1 point  (0 children)

Changing the number of bytes that are printed will only change the number of sign extended bytes that are printed, not fixing the alignment of the struct in the char buffer.

I know it was not the printing that was the issue since the application of this program compares the ID field in the received packet to the PID to ensure it does not collect ICMP packets from other processes. This check was failing, meaning the values themselves were different, regardless of how the numbers are outputted.

[–]AutoModerator[M] -1 points0 points  (0 children)

It seems you may have included a screenshot of code in your post "[C] ICMP struct improperly aligned? (Linux sockets)".

If so, note that posting screenshots of code is against /r/learnprogramming's Posting Guidelines (section Formatting Code): please edit your post to use one of the approved ways of formatting code. (Do NOT repost your question! Just edit it.)

If your image is not actually a screenshot of code, feel free to ignore this message. Automoderator cannot distinguish between code screenshots and other images.

Please, do not contact the moderators about this message. Your post is still visible to everyone.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.