LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2021, 09:46 PM   #1
m.r_m0j0
LQ Newbie
 
Registered: Nov 2021
Posts: 3

Rep: Reputation: Disabled
Question creating IP header using IP_HDRINCL option is not working


i wanted to send icmp packet with custom ip header so i wrote the following code but when i run the program nothing happens there is no packet in wireshark and there is no error from the program and i don't know what is happening

Code:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/ip_icmp.h>
#include <netinet/ip.h>
#include <netdb.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define BUFFSIZE 2048

char sendbuf[BUFFSIZE];
int seqno=0;

unsigned short chksum(void *data,int bytes){
	unsigned int sum=0;
	unsigned short *octate=data,result;

	while(bytes>1){
		sum+=*octate++;
		bytes-=2;
	}
	if(bytes==1){
		sum+=*(unsigned char *)octate;
	}
	while(sum>>16){
		sum=(sum>>16)+(sum&0xffff);
	}
	result=~sum;
	return result;
}

struct addrinfo * getaddr(char *name){                               //convert user input host to address structure
	int e;
	struct addrinfo hints,*res;

	memset(&hints,0,sizeof(hints));
	hints.ai_flags=AI_CANONNAME;
	hints.ai_family=AF_INET;
	if(e=getaddrinfo(name,0,&hints,&res)!=0){
		printf("getaddrinfo error: %s\n",gai_strerror(e));
	}
	return res;
}

void create_packet(struct sockaddr *d){

	/*     	Creating IP Packet           */

	struct ip *ip;
	ip=(struct ip*)sendbuf;
	ip->ip_v=4;                                                   
	ip->ip_hl=5;                                                 
	ip->ip_tos=0;						     
	ip->ip_len=20+8;                                             
	ip->ip_id=8848;						     
	ip->ip_off=IP_DF;					     
	ip->ip_ttl=7;                                             
	ip->ip_p=IPPROTO_ICMP;
	
	char srcip[]="192.168.1.69";
	struct addrinfo *source = getaddr(srcip);

	struct sockaddr_in *dest=(struct sockaddr_in *)d;
	struct sockaddr_in *src=(struct sockaddr_in *)source->ai_addr;
	ip->ip_src=src->sin_addr;
	ip->ip_dst=dest->sin_addr;

	ip->ip_sum=0;
	ip->ip_sum=chksum(ip,sizeof(*ip));

	
	/*     	Creating ICMP Packet           */
	struct icmp *icmp;
	
	icmp=(struct icmp *)(sendbuf+20);
	icmp->icmp_type=ICMP_ECHO;
	icmp->icmp_code=0;
	icmp->icmp_id=getpid();
	icmp->icmp_seq=++seqno;
	icmp->icmp_cksum=0;
	icmp->icmp_cksum=chksum(icmp,8);
}


void main(int argc,char **argv){
	int sock;
	int on=1;
	struct addrinfo *addr=getaddr(argv[1]);
	if((sock=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))==-1){
		perror("socket error: ");
		return;
	}
	if(setsockopt(sock,IPPROTO_IP,IP_HDRINCL,&on,sizeof(on))==-1){
		perror("setsockopt error");
		return;
	} 
	create_packet(addr->ai_addr);

	if(sendto(sock,sendbuf,28,0,addr->ai_addr,addr->ai_addrlen)==-1){
		perror("sendto error");
		return;
	}

}
 
Old 11-24-2021, 07:29 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Welcome to LQ and the Programming forum!

I have not had a chance to look at your code and have not recently done much socket programming, so no specific advice to offer.

It is always helpful to those offering help if you can tell us what you have done to troubleshoot it so far, and where you are getting stuck understanding what is happening. That helps others to more quickly understand the problem and will often lead you to a better understanding yourself, and sometimes lead to a solution!

I would suggest that you check each of those lib function calls and verify the args are valid and that you are correctly handling their return values. And add few temporary messages to show you the state of important values at various points, which should lead you to the point that something is failing or not as expected.

Good luck!
 
Old 11-25-2021, 05:24 AM   #3
m.r_m0j0
LQ Newbie
 
Registered: Nov 2021
Posts: 3

Original Poster
Rep: Reputation: Disabled
Yes I have solved the problem. I forgot to change the endianness while creating the header using htons() for fields having multiple bytes(ip_off,ip_len) worked
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot get past GDISK- Caution: Invalid backup GPT header, but valid main header, regenerating backup header from main header. murde Linux - Newbie 2 05-30-2020 08:23 PM
Using the option field in IP header guam Programming 5 08-25-2011 04:52 AM
How to check missing header files included from another header file adisan82 Linux - Software 1 01-28-2011 03:57 AM
c header files in linux in place of header files in windows? harun_acs Programming 1 03-17-2004 02:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration