Problem with Altenative Client Application.

This is a discussion on Problem with Altenative Client Application. within the C Programming forums, part of the General Programming Boards category; Hello!Thanks for your time reading this.I was doing a Client/Server app programming the server had following routines: Code: socket(); bind(); ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    9

    Problem with Altenative Client Application.

    Hello!Thanks for your time reading this.I was doing a Client/Server app programming
    the server had following routines:

    Code:
    socket();
    bind();
    listen();
    connect();
    client:

    Code:
    socket();
    connect();
    Using Wireshark 5 packets where sniffed:
    Then I wanted to make my own client to trigger the same number of packets as before

    my program is like:
    Code:
    socket(PF_INET,SOCK_RAW,IPPROTO_TCP);
    setsockopt();//tell the kernel not to bother about inserting its ip header
    CreateIPHeader();
    CreateTCPHeader();//with SYN flag set
    sendto();//with  address and port of my server
    After running the program several times the server doesn't send back SYN-ACK packet.Help Please!

    My platform is:Ubuntu Jaunty 9.04 kernel v2.6.28
    Thanks!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you want help, how about providing some actual code instead of a few lines of gibberish?

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    Quote Originally Posted by CommonTater View Post
    If you want help, how about providing some actual code instead of a few lines of gibberish?

    OK ,Here is the code:
    Code:
    #include<stdio.h>
    #include<sys/socket.h>
    #include<stdlib.h>
    #include<features.h>
    #include<linux/if_packet.h>
    #include<linux/if_ether.h>
    #include<errno.h>
    #include<sys/ioctl.h>
    #include<net/if.h>
    #include<net/ethernet.h>
    #include<linux/ip.h>
    #include<linux/tcp.h>
    #include<string.h>
    #include<arpa/inet.h>
    #include<sys/time.h>
    unsigned short		
    checksum (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;
    }
    #define P 80
    int 
    main (void)
    {
      int s = socket (PF_INET, SOCK_RAW,IPPROTO_TCP);	/* open raw socket */
     unsigned char*packet;	
      struct iphdr *iph = (struct iphdr *) malloc(20);
      struct tcphdr *tcph = (struct tcphdr *) malloc(20);
      packet=(unsigned char*)malloc(sizeof(iph)+sizeof(tcph)+sizeof(struct ethhdr));
    
      struct sockaddr_in sin;
    		
    
      sin.sin_family = PF_INET;
      sin.sin_port = htons (P);
      sin.sin_addr.s_addr = inet_addr ("1.1.1.1");
      memset(&sin.sin_zero,'\0',8);
        int one = 1;
        const int *val = &one;
        if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
        perror("Warning: Cannot set HDRINCL!\n");
      
      iph->ihl = 5;
      iph->version = 4;
      iph->tos = 0;
      iph->tot_len = htons(sizeof (struct iphdr) + sizeof (struct tcphdr));	
      iph->id = htonl (54321);	
      iph->frag_off = 0;
      iph->ttl = 255;
      iph->protocol =IPPROTO_TCP;
      iph->check = 0;		
      iph->saddr = inet_addr ("1.1.1.2");//address of my client pc
      iph->daddr = sin.sin_addr.s_addr;
    
      /*TCP Header*/
      tcph->source = htons (12983);
      tcph->dest = htons (P);
      tcph->seq = random ();
      tcph->ack = 0;
      tcph->res1 = 0;
      tcph->doff = 5;		
      tcph->syn =1;	/* initial connection request */
      tcph->window= htonl (65535);	
      tcph->check = 0;
      tcph->urg_ptr= 0;
    
      iph->check = checksum ((unsigned short *) packet, ntohs(iph->tot_len)>> 1);
      memcpy(packet,iph,20);
      memcpy((packet+20),tcph,20);
      while (1)
        { 
          if (sendto (s,		/* our socket */
    		  packet,	
    		 40,	
    		  0,		
    		  (struct sockaddr *) &sin,	
    		  sizeof (sin)) < 0)		
    	perror ("sendto()\n");
          else
    	printf ("Sent!");
    	sleep(2);
        }
    free(iph);
    free(tcph);
    free(packet);
      return 0;
    }
    I've removed two routines
    Code:
    (CreateIPHeader() & CreateTCPHeader())
    seems fine though!!??

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I don't get why you need to make your own TCP header when sockets will do that for you automatically...
    Also you never call connect() ...

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    Here Tater,According to RFC 793 ,For a TCP connection to initiated a process referred as 3-way handshake is involved where by a host A sends a SYN packet to host B,host B sends a SYN-ACK packet back to host A,host A sends an ACK packet to host B ,after that they can exchange data.I've been taking Networking Programming tutorials lately.Am trying to see the standard work from my own work.The Client have just
    Code:
    socket() and connect()
    i know the kernel does the job of adding the headers for this client and its the same header as mine as I view it from Wireshark.Thanks!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lvandaz View Post
    Here Tater,According to RFC 793 ,For a TCP connection to initiated a process referred as 3-way handshake is involved where by a host A sends a SYN packet to host B,host B sends a SYN-ACK packet back to host A,host A sends an ACK packet to host B ,after that they can exchange data.I've been taking Networking Programming tutorials lately.Am trying to see the standard work from my own work.The Client have just
    Code:
    socket() and connect()
    i know the kernel does the job of adding the headers for this client and its the same header as mine as I view it from Wireshark.Thanks!
    Ok so you write your own header... then the kernel adds it's own header... it's not seeing an empty syn package it's seeing a packet from an unconnected machine and rejecting it....

    You need to stop fooling with headers and use connect()

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    //Tater have u done this before??I don't know if u understood my code coz i don't thinks your trying to help here kernel adds its own headers???
    do u know what this part of the code does ?
    Code:
        int one = 1;
        const int *val = &one;
        if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
        perror("Warning: Cannot set HDRINCL!\n");
    Just try to guide not block!Thanks

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, let me summarize my point in the form of an old adage...

    "Why reinvent the wheel when you can use the one you already have?"

    Sockets includes all necessary header features as an integral part of itself, inserting your own (faulty?) headers is obviously not helping you.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    First I know what sockets and its functions does...And I don't think u understand what am trying to do....Lastly am not the first trying to do this so there got to be some who already succeeded with this..Thanks anyway!
    Last edited by lvandaz; 05-01-2011 at 09:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server application for hybrid file transfer
    By ejjjjj in forum C Programming
    Replies: 6
    Last Post: 05-16-2010, 03:27 PM
  2. Win32 Server\Client Application
    By IndioDoido in forum Windows Programming
    Replies: 7
    Last Post: 11-09-2008, 05:00 PM
  3. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 10:54 PM
  4. an altenative PRNG seed (other than time(0))
    By major_small in forum C++ Programming
    Replies: 15
    Last Post: 08-05-2005, 09:05 AM
  5. Client application having problem receiving from server side?
    By dp_76 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-04-2005, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21