Hi everybody,
i am stucked doing a client-server program using raw sockets. I have Red Hat 8.0 Linux installed in my m/c and here is my client module (server also has the same problem)
#define __USE_BSD
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#define __FAVOR_BSD
#include<netinet/tcp.h>
#include<arpa/inet.h>
#include<unistd.h>
unsigned short csum(unsigned short * buf, int nwords)
{
unsigned long sum;
for(sum=0;nwords>0;nwords-
-)
sum+= *buf++;
sum=(sum >> 16)+(sum & 0xffff);
sum+=(sum >> 16);
return ~sum;
}
int main()
{
int sockfd=socket(PF_INET,SOCK
_RAW,IPPRO
TO_IP);
int len;
char datagram[4096];
struct ip *iph = (struct ip*)datagram;
struct tcphdr *tcph=(struct tcphdr *)datagram + sizeof(struct ip);
struct sockaddr_in address;
int result;
memset(datagram,0,4096);
address.sin_family=AF_INET
;
address.sin_addr.s_addr=in
et_addr("1
27.0.0.1")
;
address.sin_port=htons(973
8);
len=sizeof(address);
iph->ip_hl=5;
iph->ip_v=4;
iph->ip_tos=0;
iph->ip_len=sizeof(struct ip)+sizeof(struct tcphdr);
iph->ip_id=htonl(54321);
iph->ip_off=0;
iph->ip_ttl=255;
iph->ip_p=6;
iph->ip_sum=0;
iph->ip_src.s_addr=inet_ad
dr("127.0.
0.1");
iph->ip_dst.s_addr = address.sin_addr.s_addr;
tcph->th_sport = htons (9738); /* arbitrary port */
tcph->th_dport = htons (1234);
tcph->th_seq = random ();/* in a SYN packet, the sequence is a random */
tcph->th_ack = 0;/* number, and the ack sequence is 0 in the 1st packet */
tcph->th_x2 = 0;
tcph->th_off = 0; /* first and only tcp segment */
tcph->th_flags = TH_SYN; /* initial connection request */
tcph->th_win = htonl (65535); /* maximum allowed window size */
tcph->th_sum = 0;/* if you set a checksum to zero, your kernel's IP stack
should fill in the correct checksum during transmission */
tcph->th_urp = 0;
iph->ip_sum=csum((unsigned
short *)datagram, iph->ip_len>>1);
{
int one=1;
const int *val=&one;
if(setsockopt(sockfd,IPPRO
TO_IP,IP_H
DRINCL,val
,sizeof(on
e))<0)
printf("Warning: Cannot set HDRINCL!\n");
}
// connect the socket with the server's socket
result=connect(sockfd,(str
uct sockaddr *)&address,len);
if(result==-1){
perror("oops: client");
exit(1);
}
//sendto(sockfd,datagram,i
ph->ip_len
,0,(struct
sockaddr *)&address,sizeof(address)
);
//recvfrom(sockfd,datagram
,iph->ip_l
en,0,(stru
ct sockaddr *)&address,&len);
write(sockfd,datagram,4096
);
read(sockfd,datagram,4096)
;
//printf("char from server = %c\n",datagram[4095]);
close(sockfd);
exit(0);
}
the problem is what i guess the kernel is not able to distinguish my packet and issuing the following message
Warning: Cannot set HDRINCL!
by capturing the error here.
{
int one=1;
const int *val=&one;
if(setsockopt(sockfd,IPPRO
TO_IP,IP_H
DRINCL,val
,sizeof(on
e))<0)
printf("Warning: Cannot set HDRINCL!\n");
}
If i ommit this block of code then it is issuing "bad File descriptor". What is the actual problem and what may be the remedies.
Thanking in advance,
Avik.
Start Free Trial