Advertisement

05.28.2005 at 09:18PM PDT, ID: 21440245
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

Raw Socket Programming using C

Asked by Avik77 in Linux Programming

Tags: , , , ,

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,IPPROTO_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=inet_addr("127.0.0.1");
 address.sin_port=htons(9738);
 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_addr("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,IPPROTO_IP,IP_HDRINCL,val,sizeof(one))<0)
   printf("Warning: Cannot set HDRINCL!\n");
 }
 // connect the socket with the server's socket
 result=connect(sockfd,(struct sockaddr *)&address,len);
 if(result==-1){
  perror("oops: client");
  exit(1);
 }
 //sendto(sockfd,datagram,iph->ip_len,0,(struct sockaddr *)&address,sizeof(address));
 //recvfrom(sockfd,datagram,iph->ip_len,0,(struct 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,IPPROTO_IP,IP_HDRINCL,val,sizeof(one))<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
[+][-]05.29.2005 at 08:40PM PDT, ID: 14106446

View this solution now by starting your 14-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Linux Programming
Tags: socket, raw, c, programming, using
Sign Up Now!
Solution Provided By: NovaDenizen
Participating Experts: 2
Solution Grade: A
 
 
[+][-]06.01.2005 at 09:08PM PDT, ID: 14127814

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.02.2005 at 04:12PM PDT, ID: 14355762

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 14-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]07.06.2005 at 05:48AM PDT, ID: 14377164

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 14-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-43