I am implementing a trivial ICMP ping where in sending IP packet down the wire is giving me "Invalid argument".
Here's the code:
/* Create IP Packet */
int packet_size = IPv4_HDRLEN + (int)sizeof(ICMPHeader) + (int)[payload length];
char *packet = (char *) malloc (packet_size);
if (!packet)
{
perror("out of memory");
return;
}
//zero out the packet buffer
memset (packet, 0, packet_size);
//ip header
struct ip *iphdr = (struct ip *) packet;
iphdr->ip_v = 4;
iphdr->ip_hl = 5;
iphdr->ip_tos = 0;
iphdr->ip_len = htons(packet_size);
iphdr->ip_id = (ushort) rand();
iphdr->ip_off = 0;
iphdr->ip_ttl = 255;
iphdr->ip_p = IPPROTO_ICMP;
iphdr->ip_sum = in_cksum((uint16_t *)iphdr, IPv4_HDRLEN);
int status = 0;
char *src_ip = (char *) [#"127.0.0.1" UTF8String];
char *dst_ip = (char *) [#"2.3.4.5" UTF8String];
// Source IPv4 address (32 bits)
if ((status = inet_pton (AF_INET, src_ip, &(iphdr->ip_src))) != 1) {
fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status));
exit (EXIT_FAILURE);
}
// Destination IPv4 address (32 bits)
if ((status = inet_pton (AF_INET, dst_ip, &(iphdr->ip_dst))) != 1) {
fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status));
exit (EXIT_FAILURE);
}
icmpPacket = [NSMutableData dataWithLength:sizeof(*icmpPtr) + [payload length]];
assert(icmpPacket != nil);
icmpPtr = [icmpPacket mutableBytes];
icmpPtr->type = kICMPTypeEchoRequest;
icmpPtr->code = 0;
icmpPtr->checksum = 0;
icmpPtr->identifier = OSSwapHostToBigInt16(self.identifier);
icmpPtr->sequenceNumber = OSSwapHostToBigInt16(self.nextSequenceNumber);
memcpy(&icmpPtr[1], [payload bytes], [payload length]);
icmpPtr->checksum = in_cksum([icmpPacket bytes], [icmpPacket length]);
int on = 1;
/* Copy icmp and data into the packet */
memcpy(packet + IPv4_HDRLEN, icmpPtr, [icmpPacket length]);
// Set flag so socket expects us to provide IPv4 header.
if (setsockopt (CFSocketGetNative(self->_socket), IPPROTO_IP, IP_HDRINCL, &on, sizeof (on)) < 0) {
perror ("setsockopt() failed to set IP_HDRINCL ");
exit (EXIT_FAILURE);
}
/* Send the packet */
if (self->_socket == NULL) {
bytesSent = -1;
err = EBADF;
} else {
bytesSent = sendto(
CFSocketGetNative(self->_socket),
packet,
packet_size,
0,
(struct sockaddr *) &sin,
sizeof (struct sockaddr)
);
err = 0;
if (bytesSent < 0) {
err = errno;
}
}
This is actually an extension to Apple's simple ping: https://developer.apple.com/library/mac/samplecode/SimplePing/Listings/SimplePing_m.html#//apple_ref/doc/uid/DTS10000716-SimplePing_m-DontLinkElementID_5
IP Packet that's being framed looks straight forward but for some reason, it's always giving errno 22. I am not sure what else is wrong with the code.
Socket is created using:
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
I cannot use raw sockets as i am building this for an iOS app.
Thanks
In case someone encounters the same issue, the problem was exactly as it's mentioned in this post: Raw socket sendto() failure in OS X
Related
int main(int argc, char *argv[]) {
struct sockaddr_in src_socket_address, dest_socket_address;
struct ifreq ifreq_i;
struct ifreq if_mac;
struct ifreq ifreq_ip;
int packet_size;
char ifName[IFNAMSIZ];
char sendbuf[BUF_SIZE];
int tx_len = 0;
unsigned char *buffer = (unsigned char*)malloc(65536);
int sock = socket (AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
int send_soc = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW);
struct ethhdr *eth = (struct ethhdr*)sendbuf;
struct sockaddr saddr;
struct sockaddr_ll sadr_ll;
int saddr_len = sizeof(saddr);
/*Get interface name*/
if(argc < 1) {
strcpy(ifName, argv[1]);
} else {
strcpy(ifName, DEFAULT_IF);
}
if(sock < 0) {
printf("Failed to create socket");
exit(1);
}
if(send_soc < -1) {
printf("Failed to create socket send");
exit(1);
}
/* Get the index of the interface to send on */
memset(&ifreq_i, 0, sizeof(struct ifreq));
strncpy(ifreq_i.ifr_name, ifName, IFNAMSIZ-1);
if((ioctl(send_soc, SIOCGIFINDEX, (void*)&ifreq_i)) < 0){
printf("Error in index ioctl reading %s\n", strerror(errno));
close(send_soc);
exit(EXIT_FAILURE);
}
/*Get the mac address of the interface*/
memset(&if_mac, 0, sizeof(struct ifreq));
strncpy(if_mac.ifr_name, ifName, IFNAMSIZ-1);
if((ioctl(send_soc, SIOCGIFHWADDR, &if_mac)) <0){
printf("Error in SIOCGIFHWADDR ioctl reading %s\n", strerror(errno));
close(send_soc);
exit(EXIT_FAILURE);
}
/*Get the ip address of the interface"
memset(&ifreq_ip, 0, sizeof(struct ifreq));
strncpy(ifreq_ip.ifr_name, ifName, IFNAMSIZ-1);
if((ioctl(send_soc, SIOCGIFADDR, &ifreq_ip)) <0)
printf("error in SIOCGIFADDR ip address reading\n");*/
while (1) {
packet_size = recvfrom(sock, buffer, 65536, 0, &saddr, (socklen_t*)&saddr_len);
if(packet_size == -1) {
printf("Failed to get packets\n");
return 1;
} else {
printf("Received packets");
}
// struct iphdr *ip_packet = (struct iphdr*)buffer;
// memset(&src_socket_address, 0, sizeof(src_socket_address));
// src_socket_address.sin_addr.s_addr = ip_packet->saddr;
// memset(&dest_socket_address, 0, sizeof(dest_socket_address));
// dest_socket_address.sin_addr.s_addr = ip_packet->daddr;
memset(sendbuf, 0, BUF_SIZE);
/*Construct ethernet header*/
eth->h_source[0] = (unsigned char)(if_mac.ifr_hwaddr.sa_data[0]);
eth->h_source[1] = (unsigned char)(if_mac.ifr_hwaddr.sa_data[1]);
eth->h_source[2] = (unsigned char)(if_mac.ifr_hwaddr.sa_data[2]);
eth->h_source[3] = (unsigned char)(if_mac.ifr_hwaddr.sa_data[3]);
eth->h_source[4] = (unsigned char)(if_mac.ifr_hwaddr.sa_data[4]);
eth->h_source[5] = (unsigned char)(if_mac.ifr_hwaddr.sa_data[5]);
/*filling destination mac address*/
eth->h_dest[0] = DESTMAC0;
eth->h_dest[1] = DESTMAC1;
eth->h_dest[2] = DESTMAC2;
eth->h_dest[3] = DESTMAC3;
eth->h_dest[4] = DESTMAC4;
eth->h_dest[5] = DESTMAC5;
eth->h_proto = htons(ETH_P_IP);
/* end of ethernet header*/
tx_len += sizeof(struct ethhdr);
/*Packet data for dummy data*/
sendbuf[tx_len++] = 0xAA;
sendbuf[tx_len++] = 0xBB;
sendbuf[tx_len++] = 0xCC;
sendbuf[tx_len++] = 0xDD;
sendbuf[tx_len++] = 0xEE;
sadr_ll.sll_ifindex = ifreq_i.ifr_ifindex;
sadr_ll.sll_halen = ETH_ALEN;
sadr_ll.sll_addr[0] = DESTMAC0;
sadr_ll.sll_addr[1] = DESTMAC1;
sadr_ll.sll_addr[2] = DESTMAC2;
sadr_ll.sll_addr[3] = DESTMAC3;
sadr_ll.sll_addr[4] = DESTMAC4;
sadr_ll.sll_addr[5] = DESTMAC5;
/* Send Packet */
if (sendto(send_soc, buffer, packet_size, 0, (const struct sockaddr*)&sadr_ll, sizeof(struct sockaddr_ll)) < 0) {
printf("Sending packet failed\n");
close(send_soc);
} else {
printf("Sending packet successful");
}
printf("================================================\n");
}
close(send_soc);
return 0;
}
I want to receive ethernet packets using raw sockets and duplicate the packets to send to another port in the same machine to analyse the traffic. I can receive the packets however after a while I get an error while sending the packets as "No such device or address".
Can you please explain whats the problem here?
Thanks!
Nivetha
I try to make a function that sends a icmp packet to another computer and when the other computer sends something back, the function returns a 1, else a 0. But the recvfrom function returns an error: "errno: Resource temporarily unavailable". I work on an Mac OS X, so I don't include headers from the linux kernel. Can anyone help me, cause I'm stuck.
#include "info.h"
char *getip()
{
char buffer[256];
struct hostent *h;
gethostname(buffer, 256);
h = gethostbyname(buffer);
return inet_ntoa(*(struct in_addr *)h->h_addr);
}
int host_alive(char *dst_addr, char *src_addr)
{
struct ip *ippacket;
struct ip *ip_reply;
struct icmp *icmppacket;
struct sockaddr_in connection;
struct timeval tv;
char *packet;
char *buffer;
int optval;
int addrlen;
int size;
int sock = 0;
packet = malloc(sizeof(struct ip) + sizeof(struct icmp));
buffer = malloc(sizeof(struct ip) + sizeof(struct icmp));
check(getuid() == 0, "Root priviliges are needed. Try: sudo ./bin/main");
ippacket = (struct ip *) packet;
icmppacket = (struct icmp *) (packet + sizeof(struct ip));
ippacket->ip_hl = 5;
ippacket->ip_v = 4;
ippacket->ip_tos = 0;
ippacket->ip_len = sizeof(struct ip) + sizeof(struct icmp);
ippacket->ip_id = htons(random());
ippacket->ip_ttl = 255;
ippacket->ip_p = IPPROTO_ICMP;
inet_aton(src_addr, &ippacket->ip_src);
inet_aton(dst_addr, &ippacket->ip_dst);
tv.tv_sec = 5;
tv.tv_usec = 0;
check((sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) != -1,\
"Failed to create socket");
check(setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &optval, sizeof(int)) != -1,\
"Failed to set the option to the socket.");
check(setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof(struct timeval)) != -1,\
"Failed to set the option to the socket.");
icmppacket->icmp_type = ICMP_ECHO;
icmppacket->icmp_code = 0;
icmppacket->icmp_id = 0;
icmppacket->icmp_seq = 0;
icmppacket->icmp_cksum = in_cksum((unsigned short *)icmppacket, sizeof(struct icmp));
ippacket->ip_sum = in_cksum((unsigned short *)ippacket, sizeof(struct ip));
connection.sin_family = AF_INET;
connection.sin_addr.s_addr = inet_addr(dst_addr);
sendto(sock, packet, ippacket->ip_len, 0, (struct sockaddr *)&connection,\
sizeof(struct sockaddr));
addrlen = sizeof(connection);
check((size = recvfrom(sock, buffer, sizeof(struct ip) + sizeof(struct icmp), 0,\
(struct sockaddr *)&connection, (socklen_t *)&addrlen)) != -1,\
"Failed to receive a message.");
printf("Received %d byte reply from %s:\n", size , dst_addr);
ip_reply = (struct ip*) buffer;
printf("ID: %d\n", ntohs(ip_reply->ip_id));
printf("TTL: %d\n", ip_reply->ip_ttl);
close(sock);
free(packet);
free(buffer);
return 1;
error:
if (sock)
close(sock);
free(packet);
free(buffer);
return 0;
}
unsigned short in_cksum(unsigned short *addr, int len)
{
int sum = 0;
u_short answer = 0;
u_short *w = addr;
int nleft = len;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
if (nleft == 1) {
*(u_char *) (&answer) = *(u_char *) w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return (answer);
}
According to the documentation for recvfrom this is expected if you're using a non-blocking call:
If no messages are available at the socket, the receive call waits for a message to arrive, unless the socket is nonblocking (see fcntl(2)) in which case the value -1 is returned and the external vari-
able errno set to EAGAIN. The receive calls normally return any data available, up to the requested amount, rather than waiting for receipt of the full amount requested; this behavior is affected by the
socket-level options SO_RCVLOWAT and SO_RCVTIMEO described in getsockopt(2).
You can look up errno values in /usr/include/sys/errno.h if you're curious as to what values map to what errors.
If you want this function to block you may need to set the MSG_WAITALL flag which "requests that the operation block until the full request is satisfied".
Normally in low-level UNIX socket code you'd do a select on that socket to wait for a read signal, then call recvfrom to receive the data if and only if that signal triggered. You can also do a non-blocking receive and on an EAGAIN just wait a short period of time before trying again, though that is less efficient.
Hello I would like to make a programm which would work similarly to "SendIP a command-line tool to send arbitrary IP packets"
I have a programm to send "IPv4 + UDP". I try to validate my program by Wireshark but nothing is received and I don't know why.
I send it by command ./Project 192.168.1.15 21 192.168.1.15 8080 5
Here is my code: (In ipv4.h and udp.h I have ip and udp headers)
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include "ipv4.h"
#include "udp.h"
// Source IP, source port, target IP, target port from the command line arguments
int main(int argc, char *argv[])
{
if (argc != 6)
{
printf("- Invalid parameters!!!\n");
printf(
"- Usage %s <source hostname/IP> <source port> <target hostname/IP> <target port>\n",
argv[0]);
exit(-1);
}
else {
int sd;
char buffer[PCKT_LEN];
struct ipheader *ip = (struct ipheader *) buffer;
struct udpheader *udp = (struct udpheader *) (buffer
+ sizeof(struct ipheader));
// Source and destination addresses: IP and port
struct sockaddr_in sin, din;
int one = 1;
const int *val = &one;
memset(buffer, 0, PCKT_LEN);
// Create a raw socket with UDP protocol
sd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
if (sd < 0)
{
perror("socket() error");
// If something wrong just exit
exit(-1);
}
else
printf(
"socket() - Using SOCK_RAW socket and UDP protocol is OK.\n");
// The address family
sin.sin_family = AF_INET;
din.sin_family = AF_INET;
// Port numbers
sin.sin_port = htons(atoi(argv[2]));
din.sin_port = htons(atoi(argv[4]));
// IP addresses
sin.sin_addr.s_addr = inet_addr(argv[1]);
din.sin_addr.s_addr = inet_addr(argv[3]);
ip->iph_ihl = 5;
ip->iph_ver = 4;
ip->iph_tos = 16; // Low delay
ip->iph_len = sizeof(struct ipheader) + sizeof(struct udpheader);
ip->iph_ident = htons(54321);
ip->iph_ttl = 64; // hops
ip->iph_protocol = 17; // UDP
ip->iph_sourceip = inet_addr(argv[1]);
// The destination IP address
ip->iph_destip = inet_addr(argv[3]);
// Fabricate the UDP header. Source port number, redundant
udp->udph_srcport = htons(atoi(argv[2]));
// Destination port number
udp->udph_destport = htons(atoi(argv[4]));
udp->udph_len = htons(sizeof(struct udpheader));
// Calculate the checksum for integrity
ip->iph_chksum = csum((unsigned short *) buffer,
sizeof(struct ipheader) + sizeof(struct udpheader));
if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
{
perror("setsockopt() error");
exit(-1);
}
else
printf("setsockopt() is OK.\n");
printf("Trying...\n");
printf("Using raw socket and UDP protocol\n");
printf("Using Source IP: %s port: %u, Target IP: %s port: %u.\n",
argv[1], atoi(argv[2]), argv[3], atoi(argv[4]));
//printf("Interfejs=%d\n", *val);
int count;
int number = (atoi(argv[5]));
printf("Number=%d\n", number);
for (count = 1; count <= number; count++)
{
if (sendto(sd, buffer, ip->iph_len, 0, (struct sockaddr *) &sin,
sizeof(sin)) < 0)
{
perror("sendto() error");
exit(-1);
}
else
{
printf("Count #%u - sendto() is OK.\n", count);
sleep(2);
}
}
close(sd);
return 0;
}
}
Ok I fix it. The problem was with int one = 1; which specife number of interface. I changed to 0 and it works. What a stupid mistake :D
Hi I'm writing 2 Programs (Server, Client) which should communicate with each other over sockets. The Client is able to send its first message to the server with no problem, but when the server tries to answer, the client receives just an empty msg: recv(...) is 0.
The server suddenly stops after the send(...) function is called.
Here is my Code:
Server:
/* Create a new TCP/IP socket `sockfd`, and set the SO_REUSEADDR
option for this socket. Then bind the socket to localhost:portno,
listen, and wait for new connections, which should be assigned to
`connfd`. Terminate the program in case of an error.
*/
struct sockaddr_in sin,
peer_addr;
//-----gen socket-----//
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
bail_out(EXIT_PARITY_ERROR, "could not create Socket");
//-----bind-----//
memset(&sin, 0, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(options.portno);
sin.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0)
bail_out(EXIT_PARITY_ERROR, "Failed to bind to Port");
//-----listen-----//
if (listen(sockfd, 5) < 0)
bail_out(EXIT_PARITY_ERROR, "Server can't accepted connection");
//-----accept-----//
int sock_len = sizeof peer_addr;
if ((connfd = accept(sockfd, (struct sockaddr*)&peer_addr, (socklen_t *)&sock_len)) < 0) //fragen
bail_out(EXIT_PARITY_ERROR, "Can't accept connection to Client");
/* accepted the connection */
//Some other Code which has nothing to do with my Error!
/* read from client (WORKS FINE!!)*/
if (read_from_client(connfd, &buffer[0], READ_BYTES) == NULL) {
if (quit) break; /* caught signal */
bail_out(EXIT_FAILURE, "read_from_client");
}
request = (buffer[1] << 8) | buffer[0];
DEBUG("Round %d: Received 0x%x\n", round, request);
/* compute answer */
correct_guesses = compute_answer(request, buffer, options.secret);
if (round == MAX_TRIES && correct_guesses != SLOTS) {
buffer[0] |= 1 << GAME_LOST_ERR_BIT;
}
DEBUG("Sending byte 0x%x\n", buffer[0]);
/* send message to client */
if (send_to_client(sockfd, &buffer[0], WRITE_BYTES) == NULL) { //Error in this Method!
if (quit) break; /* caught signal */
bail_out(EXIT_FAILURE, "can't send message!");
}
Methods:
static uint8_t *send_to_client(int fd, uint8_t *buffer, size_t n)
{
/* loop, as packet can arrive in several partial reads */
size_t bytes_send = 0;
do {
ssize_t r = send(fd, buffer + bytes_send, n - bytes_send, 0); //Program stops HERE!
printf("%d\n", (int)r); //This and the following lines will not be executed!
if (r <= 0) {
return NULL;
}
bytes_send += r;
} while (bytes_send < n);
if (bytes_send < n) {
return NULL;
}
return buffer;
}
Client: (Might be usefull)
sockfd = cnt_to_server(argv[1], argv[2]);
uint8_t buffer;
uint16_t msg_buffer;
do
{
msg_buffer = generate_msg(&msg);
printf("Sending byte 0x%x\n", msg_buffer);
if (send_to_server(sockfd, &msg_buffer, WRITE_BYTES) == NULL) //works
error_exit(EXIT_FAILURE, "can't send message!");
if (read_from_server(sockfd, &buffer, READ_BYTES) == NULL) //NULL
error_exit(EXIT_FAILURE, "can't read message!");
printf("received byte 0x%x\n", buffer);
} while (game_continue(buffer, &msg));
(void)close(sockfd);
Methods:
uint8_t* read_from_server(int fd, uint8_t *buffer, int n)
{
/* loop, as packet can arrive in several partial reads */
size_t bytes_recv = 0;
do {
ssize_t r;
r = recv(fd, buffer + bytes_recv, n - bytes_recv, 0); //0
printf("%d\n", (int)r);
if (r <= 0) {
return NULL;
}
bytes_recv += r;
} while (bytes_recv < n);
if (bytes_recv < n) {
return NULL;
}
return buffer;
}
int cnt_to_server(const char *par_server, const char *par_port)
{
struct sockaddr_in server;
struct hostent *hp;
int sockfd;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
error_exit(EXIT_FAILURE, "could not create Socket");
server.sin_family = AF_INET;
if ((hp = gethostbyname(par_server)) == 0)
error_exit(EXIT_FAILURE, "host error!");
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_port = htons(parse_port(par_port));
if (connect(sockfd, (struct sockaddr*) &server, sizeof server) < 0)
error_exit(EXIT_FAILURE, "could not connect!");
return sockfd;
}
Thx for helping me out with this!
Change
if (send_to_client(sockfd, &buffer[0], WRITE_BYTES) == NULL)
to
if (send_to_client(connfd, &buffer[0], WRITE_BYTES) == NULL)
The solution is to use connfd (File descriptor for connection socket) instead of sockfd:
/* read from client */
if (read_from_client(connfd, &buffer[0], READ_BYTES) == NULL) {
if (quit) break; /* caught signal */
bail_out(EXIT_FAILURE, "read_from_client");
}
is it possible someone can help me workout why my ICMP sequence number isn't incrementing on every request, when this was used as a ping program it would increment the sequence number on each ping.
Also does anyone have any idea why my Round Trip Times are displaying as negative numbers? This also worked fine when this was a ping program.
Please assume all the code 'works', i have removed some for easier reading.
void
respond (int signum) {
struct sockaddr_storage peer_addr;
socklen_t peer_addrlen;
struct sockaddr_in addr;
struct sockaddr_in dstaddr;
struct iphdr * ip;
struct icmphdr * icmp;
struct timeval * sent;
int skt;
int sequence = 0;
long int length;
fd_set rdfds;
int ready;
int rtt;
char buff [BUF_SIZE];
/* Create and check Socket Number */
skt = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
int ttl = 0;
setsockopt(skt, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0;
/* Check Socket */
if (skt < 0) {
perror ("socket()");
exit (1);
}
/* Set IP Addresses */
addr.sin_family = AF_INET;
addr.sin_port = 0;
addr.sin_addr.s_addr = INADDR_ANY;
/* Check Socket Bind */
if (bind (skt, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) {
perror ("Can't bind socket");
exit (1);
}
/* START SEND LOOP*/
int i;
for (i = 0; i < 7; i++){
ttl+=1;
setsockopt(skt, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
/* IP Buffer */
ip = (struct iphdr *)buff;
peer_addrlen = (socklen_t) sizeof (struct sockaddr_storage);
memset (&dstaddr, 0, sizeof(struct sockaddr_in));
dstaddr.sin_addr.s_addr = inet_addr(HOSTADDR);
dstaddr.sin_family = AF_INET;
/* ICMP Buffer */
memset (buff, 0, sizeof(buff));
icmp = (struct icmphdr *) buff;
icmp->type = ECHO_REQ;
icmp->id = htons(getpid( ) & 0xffff);
icmp->seqNum = htons(sequence++);
/* Check Send Time */
if (gettimeofday ((struct timeval *)icmp->data, NULL)) {
perror ("Can't establish send time");
exit (1);
}
/*Calculating packet size*/
length = sizeof(struct icmphdr) + sizeof(struct timeval);
icmp->checksum = ~(sum (0, buff, length));
/* Packet too small, ERROR
SEND Request */
if (sendto (skt, buff, length, 0,
(struct sockaddr *) &dstaddr, sizeof(struct sockaddr_in)) <= 0) {
perror ("sendto()");
exit (1);
}
/* Define File Descriptor */
timeout.tv_sec = 1;
timeout.tv_usec = 1;
FD_ZERO(&rdfds);
FD_SET (skt, &rdfds);
/* Select Data from File Descriptor */
ready = select (skt + 1, &rdfds, NULL, NULL, &timeout);
if (ready < 0) {
perror ("Select()");
exit (1);
}
/* Recieve Reply */
memset (buff, 0, sizeof(buff));
if (recvfrom (skt, buff, sizeof(buff), 0,
(struct sockaddr *) &peer_addr, &peer_addrlen) <= 0) exit (1);
/* Check Time Stamp */
if (gettimeofday (&end, NULL)) { // Timestamp reception
perror ("Can't establish time of receipt");
exit (1);
}
/* Check IP Protocol */
if (ip->version != 4 ||
sum (0, buff, sizeof(struct iphdr)) != 0xffff ||
ip->protocol != ICMP)
exit(1);
/* Get IP Payload legth and ICMP Address*/
length = ntohs(ip->length) - ip->hdrlen * 4; // Length of IP payload
icmp = (struct icmphdr *)((uint32_t *)ip + ip->hdrlen); // Find ICMP hdr
/* Check ICMP response type*/
if (icmp->type == 11){
printf("");
}
/* if (icmp->type != ECHO_REPL || sum (0, icmp, length) != 0xffff) {
fprintf (stderr, "Received %s\n", messages[icmp->type]);
//exit (1);
} */
/* Find the difference between sent and end times in 10s of ms */
sent = (struct timeval *)icmp->data;
if ((rtt = (end.tv_usec - sent->tv_usec) / 10) < 0)
rtt += 10000; // We've cycled to a new second
rtt += (end.tv_sec - sent->tv_sec) * 10000; // Add any seconds
/* PRINT ICMP REPLY*/
printf ("%ld bytes from %s: icmp_req=%d ttl=%d time= %0.1f ms\n",
length,
iptos(ntohl(ip->srcip)),
ntohs(icmp->seqNum),
ip->ttl,
((float)rtt) / 10);
} /*END SEND LOOP
/* 3 Second Probe */
alarm (5);
}
You're never setting sequence to 0 in this code, only declaring that it's an int.
In this piece of code:
/* IP Buffer */
ip = (struct iphdr *)buff;
peer_addrlen = (socklen_t) sizeof (struct sockaddr_storage);
memset (&dstaddr, 0, sizeof(struct sockaddr_in));
dstaddr.sin_addr.s_addr = inet_addr(HOSTADDR);
dstaddr.sin_family = AF_INET;
/* ICMP Buffer */
memset (buff, 0, sizeof(buff));
icmp = (struct icmphdr *) buff;
icmp->type = ECHO_REQ;
icmp->id = htons(getpid( ) & 0xffff);
icmp->seqNum = htons(sequence++);
you are treating the buffer buff as starting with both an IP header and an ICMP header. I believe you need to offset the icmphdr pointer by the size of the IP header, unless your icmphdr structure includes an iphdr at its start (which the Linux version does not):
icmp = (struct icmphdr*)(buff + sizeof(iphdr));
Also, you are overswriting the IP header by doing memset (buff, 0, sizeof(buff)); after writing the IP header to the buffer.