Originally Posted by
Cactus_Hugger
Given that the OP's code has the good ole undefined behaviour in it, I think we should concentrate on that before looking at it's output. This section:
Code:
char buffer[1];
sprintf(buffer, %02X, packet[i]);
printf("%s", buffer);
buffer is _way_ too small there. It needs to be at least 3 chars long.
Also, OP: is your program small enough to post? I'd like to see a bit more of it, and if possible, run it locally.
Edit: Man, my eyes are not reading today. That code segment won't even compile. OP: post what you actually have...
Sorry for the late reply
I'm writing a ping application for the iPhone . However I'm not using SOCK_RAW as this requires root privilege. I'm using SOCK_DGRAM and IPPROTO_ICMP.
I can't post the full source code as I've encapsulated most C calls in Objective-C methods of Classes, so it's quite a few number of files.
Here's a bigger snapshot though
Code:
struct sockaddr_in sourceAddressStructure;
socklen_t sourceAddressStructureLength = sizeof(sourceAddressStructure);
bzero(&sourceAddressStructure, sourceAddressStructureLength);
unsigned char packet[IP_MAXPACKET];
int packetLength = [self->socket receiveFrom:packet
length:IP_MAXPACKET
flags:0
source:(struct sockaddr *)&sourceAddressStructure
sourceLength:&sourceAddressStructureLength];
printf("Packet Length = %d\n", packetLength); # This prints 84
for (int i = 0; i < packetLength; i++)
{
char buf[1];
sprintf(buf, "%02X", packet[i]);
printf("%s", buf);
if (i % 2)
printf(" ");
if (i % 16 == 15)
printf("\n");
}
printf("\n");
struct ip *test = (struct ip *)packet;
printf("Test Version = %d\n", test->ip_v); // This prints 4
printf("Test Header Length = %d\n", test->ip_hl); // This prints 5
printf("Test TOS = %d\n", test->ip_tos); // This prints 0
printf("Test Length = %d\n", (u_short)test->ip_len); // This prints 64!!!!!!!
printf("Test Identification = %d\n", test->ip_id);
printf("Test Offset = %d\n", test->ip_off);
printf("Test TTL = %d\n", test->ip_ttl);
printf("Test Protocol = %d\n", test->ip_p); // This prints 1
printf("Test Checksum = %d\n", test->ip_sum);
printf("Test Source Address = %s\n", inet_ntoa(test->ip_src));
printf("Test Destination Address = %s\n", inet_ntoa(test->ip_dst));
P.S. I'm not showing any of the error handling here as it's all being handled by a try block in another file.
Hope this helps a bit more
Thanks again and sorry for the delay
Cheers