Thread: Sending TCP ACK + Bytesize (Socket Programming)

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    45

    Sending TCP ACK + Bytesize (Socket Programming)

    Hello!

    For sending TCP ACK do I have to send a whole packet like this:

    Code:
    struct tcpheader {  unsigned short int th_sport;  unsigned short int th_dport;  unsigned int th_seq;  unsigned int th_ack;  unsigned char th_x2:4, th_off:4;  unsigned char th_flags;  unsigned short int th_win;  unsigned short int th_sum;  unsigned short int th_urp; };
    Or can I just send something like 'ACK 1024' ?

    Kind regards.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Would this be correct:
    Code:
        header.th_sport= 3232267203;
        header.th_dport= 2130706433;
        header.th_seq= 1;
        header.th_ack= 1024;
        header.th_off= 20;
        header.th_flags='0x10';
        header.th_win= 32;
        header.th_sum= 0x088a;
        header.th_urp= 0;

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I rarely ever work at the TCP level, since it's already handled by any good networking/sockets library. The two biggies, WinSock for Windows and BSD sockets for most other OSes (which WinSock was based on) handle everything for you. That being said, your example is definitely wrong (methinks you didn't even try to compile it):
    Code:
    header.th_sport= 3232267203;
    header.th_dport= 2130706433;
    Those wont fit in a unsigned short, the max value is 65535.
    Code:
    header.th_flags='0x10';
    This is syntactically incorrect, 0x10 doesn't belong inside single ' ' quotes. They're for character literals, not byte values.

    The acknowledgement number should be (I think), one more than the sequence number sent by the other end. You'll have to verify that though. Google TCP and do lots of reading. Look at the source code for the BSD sockets library if you're brave (it's open source). A place to start: Transmission Control Protocol - Wikipedia, the free encyclopedia.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    I rarely ever work at the TCP level, since it's already handled by any good networking/sockets library. The two biggies, WinSock for Windows and BSD sockets for most other OSes (which WinSock was based on) handle everything for you. That being said, your example is definitely wrong (methinks you didn't even try to compile it):
    Code:
    header.th_sport= 3232267203;
    header.th_dport= 2130706433;
    Those wont fit in a unsigned short, the max value is 65535.
    Code:
    header.th_flags='0x10';
    This is syntactically incorrect, 0x10 doesn't belong inside single ' ' quotes. They're for character literals, not byte values.

    The acknowledgement number should be (I think), one more than the sequence number sent by the other end. You'll have to verify that though. Google TCP and do lots of reading. Look at the source code for the BSD sockets library if you're brave (it's open source). A place to start: Transmission Control Protocol - Wikipedia, the free encyclopedia.
    Thanks man! Respect for Long Beach dream place to live! Much warmer then Belgium :/

    I can't figure something out: It seems that I can't send my TCP Header via TCP but that it wants UDP?

    The TCP is in comment:

    Code:
    // struct test.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <WinSock2.h>
    #include <WS2tcpip.h>
    #include <ctime>
    #pragma comment(lib,"Ws2_32.lib")
    
    
    int main()
    {
    
        struct sockaddr_storage their_addr;
        socklen_t addr_size;
        struct addrinfo hints, *servinfo, *p;
        int sockfd, new_fd ,connectfd,len, bytes_sent,bytes_received,their_addrsize = sizeof(their_addr);
        char message[100];
        time_t recvtime;
    
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
            fprintf(stderr, "WSAStartup failed.\n");
            exit(1);
        }
    
        struct tcpheader {
            unsigned long int th_sport;
            unsigned long int th_dport;
            unsigned int th_seq;
            unsigned int th_ack;
            unsigned char th_x2:4, th_off:4;
            unsigned char th_flags;
            unsigned short int th_win;
            unsigned short int th_sum;
            unsigned short int th_urp;
        };
        struct tcpheader header;
    
        header.th_sport= 3232267203;
        header.th_dport= 2130706433;
        header.th_seq= 1;
        header.th_ack= 1025;
        header.th_off= 20;
        header.th_flags= 0x10;
        header.th_win= 32;
        header.th_sum= 0;
        header.th_urp= 0;
    
         // tcpheader->th_seq = seq;        /* Sequence Number                         */
      //tcpheader->th_ack = htonl(1);   /* Acknowledgement Number                  */
      //tcpheader->th_x2 = 0;           /* Variable in 4 byte blocks. (Deprecated) */
      //tcpheader->th_off = 5;          /* Segment offset (Lenght of the header)   */
      //tcpheader->th_flags = TH_RST;   /* TCP Flags. We set the Reset Flag        */
      //tcpheader->th_win = htons(4500) + rand()%1000;/* Window size               */
      //tcpheader->th_urp = 0;          /* Urgent pointer.                         */
      //tcpheader->th_sport = src_prt;  /* Source Port                             */
      //tcpheader->th_dport = dst_prt;  /* Destination Port                        */
      //tcpheader->th_sum=0;  
    
    
        memset(&hints, 0, sizeof hints);
        hints.ai_family = AF_UNSPEC;  // use IPv4 or IPv6, whichever
        hints.ai_socktype = SOCK_DGRAM; //UDP
        //hints.ai_socktype = SOCK_STREAM; //TCP
        hints.ai_flags = AI_PASSIVE;   
    
    
        getaddrinfo("127.0.0.1", "6667", &hints, &servinfo); 
    
        //Create Socket
        for(p = servinfo; p != NULL; p = p->ai_next){
            if((sockfd = socket(servinfo->ai_family,servinfo->ai_socktype,servinfo->ai_protocol)) == -1){
                perror("server: socket");
                continue;
            }
        }
        printf("\n\nSocket: %i\n\n",sockfd);
    
        bytes_sent = sendto(sockfd, (char *) &header,sizeof(struct tcpheader),0,  servinfo->ai_addr,servinfo->ai_addrlen);
        //bytes_sent = send(sockfd, (char *) &header,sizeof(struct tcpheader),0);
        printf("Bytes sent: %i\n\n",bytes_sent);
        if (bytes_sent == -1)
        {
            wprintf(L"### Send failed with error: %ld\n\n", WSAGetLastError());
        }
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by MaSSaSLaYeR View Post
    Thanks man! Respect for Long Beach dream place to live! Much warmer then Belgium :/
    Haha! Thanks, Long Beach is a pretty good place. I'd trade your for a year or two if I could. I'd love to live in Europe. Where are you in Belgium?


    I can't figure something out: It seems that I can't send my TCP Header via TCP but that it wants UDP?
    Well, you didn't specify exactly what error you encountered. Unfortunately, I use Linux, so I can't help you with WinSock specifics, but there's nothing obvious that I can see in your code except that the checksum is probably wrong, but you don't use the checksum you specified in establishing the TCP connection, that checksum happens in the socket() call. You will need to read up on how TCP expects it to be calculated when the time comes.

    I don't do much raw socket programming, but I pretty sure you should not use the SOCK_STREAM or SOCK_DGRAM to do TCP/UDP layer programming. Those socket modes already cover TCP or UDP protocol stuff for you like the SYNs, ACKs, FINs, checksums and sequence numbers. Sending a TCP header over a TCP or UDP link just doesn't make sense since the TCP link needed the SYN/ACK stuff to establish the connection in the first place. You need to use raw sockets. You should be able to look up the info for your socket() function to learn how to use it to get a raw socket (SOCK_RAW for starters), and what to do with it. Try reading this tutorial: The Linux socket TCP/IP protocols network programming tutorials using open source GNU compiler with C language through working program examples and code samples on Linux client-server configuration too. The info you want is in the "Advanced" chapters, but I strongly suggest you read through the whole thing, as a thorough understanding of regular network/socket programming is immensely helpful in working with raw sockets.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    Haha! Thanks, Long Beach is a pretty good place. I'd trade your for a year or two if I could. I'd love to live in Europe. Where are you in Belgium?



    Well, you didn't specify exactly what error you encountered. Unfortunately, I use Linux, so I can't help you with WinSock specifics, but there's nothing obvious that I can see in your code except that the checksum is probably wrong, but you don't use the checksum you specified in establishing the TCP connection, that checksum happens in the socket() call. You will need to read up on how TCP expects it to be calculated when the time comes.

    I don't do much raw socket programming, but I pretty sure you should not use the SOCK_STREAM or SOCK_DGRAM to do TCP/UDP layer programming. Those socket modes already cover TCP or UDP protocol stuff for you like the SYNs, ACKs, FINs, checksums and sequence numbers. Sending a TCP header over a TCP or UDP link just doesn't make sense since the TCP link needed the SYN/ACK stuff to establish the connection in the first place. You need to use raw sockets. You should be able to look up the info for your socket() function to learn how to use it to get a raw socket (SOCK_RAW for starters), and what to do with it. Try reading this tutorial: The Linux socket TCP/IP protocols network programming tutorials using open source GNU compiler with C language through working program examples and code samples on Linux client-server configuration too. The info you want is in the "Advanced" chapters, but I strongly suggest you read through the whole thing, as a thorough understanding of regular network/socket programming is immensely helpful in working with raw sockets.
    Thanks for the Tips I normally use Beej's Guide but I see now that it is incomplete.


    I'm living at 2 place at the moment in Mechelen near Antwerp in the weekends and in the weekdays I live near hasselt .

    Damn I wish I could go to the Gold Gym and to the long beach peer. I think it's a pretty awesome place.

    In Belgium people are not nice to people who they don't know. And a lot of stress and traffic jams. Atleast the food is nice.

    And don't get me started about the woman!

    But one thing 'owns' in europe CARS Forza Italia

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Never been to Mechelen or Hasselt. I used to help a guy in Antwerp buy old Ford Mustangs from Los Angeles area and ship them to Belgium. He had a shop that fixed them up and sold them. I have a friend in Maastricht, Netherlands, and I've been to Brussels, but only for a couple days. I hear Bruges is a good place to travel. Long Beach is close to Los Angeles, so we get lots of traffic, but people are fairly nice. When I was in Belgium, the people weren't mean, they just weren't friendly, i.e. they didn't talk to you much. I find that the more crowded a city, and the worse the weather, the less friendly the people (at least, until they've had a couple beers). I'd go back in a heartbeat though, for the food, the culture, and being so much closer to so many other cool places. One nice thing about European women, they usually have a sexy accent when they speak English .

    Italian cars are nice and sporty, and German cars are good too, but I like good old American cars (actually, I like old cars from anywhere -- they're have much more interesting designs). I have one of these: http://local.aaca.org/antelope/image...c Firebird.jpg that I'm fixing up, though mine will be either black or a dark orange color, like rust. I'm not sure which yet. It's no Ferrari or Lamborghini, but still pretty fast with a 5.7 liter engine, and handles well for how heavy it is. It uses a lot of gas though. Not practical over there given European gas prices.

    If you do ever make it over here, to live or just visit, don't hesitate to get in touch, it would be interesting to meet another cprogramming person in person.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    Never been to Mechelen or Hasselt. I used to help a guy in Antwerp buy old Ford Mustangs from Los Angeles area and ship them to Belgium. He had a shop that fixed them up and sold them. I have a friend in Maastricht, Netherlands, and I've been to Brussels, but only for a couple days. I hear Bruges is a good place to travel. Long Beach is close to Los Angeles, so we get lots of traffic, but people are fairly nice. When I was in Belgium, the people weren't mean, they just weren't friendly, i.e. they didn't talk to you much. I find that the more crowded a city, and the worse the weather, the less friendly the people (at least, until they've had a couple beers). I'd go back in a heartbeat though, for the food, the culture, and being so much closer to so many other cool places. One nice thing about European women, they usually have a sexy accent when they speak English .

    Italian cars are nice and sporty, and German cars are good too, but I like good old American cars (actually, I like old cars from anywhere -- they're have much more interesting designs). I have one of these: http://local.aaca.org/antelope/image...c Firebird.jpg that I'm fixing up, though mine will be either black or a dark orange color, like rust. I'm not sure which yet. It's no Ferrari or Lamborghini, but still pretty fast with a 5.7 liter engine, and handles well for how heavy it is. It uses a lot of gas though. Not practical over there given European gas prices.

    If you do ever make it over here, to live or just visit, don't hesitate to get in touch, it would be interesting to meet another cprogramming person in person.


    I will let you know! Damn that's a nice Pontiac, you know what I miss in Belgium these beauties:

    Sending TCP ACK + Bytesize (Socket Programming)-l-rat-rod-jpg

    It's illegal to have in Belgium with those wheels 'naked'.

    My dad and I we have an Alfa Romeo Montreals pretty nice cars:


    • Engine: 2,593 cc (158.2 cu in) quad-cam 90° V8
    • Top speed: 220 km/h (140 mph)
    • 0–100 km/h (62 mph): 7.4 seconds
      • (measured top speed 224 km/h (139 mph), 7.1 seconds 0–100 km/h (62 mph) by Quattroruote magazine)[1]

    • Standing quarter mile: 15.1 seconds
    • Standing km: 27.6 seconds
    • Power: 200 PS (147 kW; 197 hp) (230 bhp SAE) at 6500 rpm[1]
    • Torque: 235 N·m (173 ft·lbf) (24 m·kgf) at 4750 rpm


    But yeah pontiac is a true muscle car! I like the whole Mopar scene haha I watch those shows all the time on Discovery Channel

    Greetz

    PS: I always wanted to eat a real texas steak and Beef Jerkey haha Btw Belgian Fries Rule mwhahwa

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So are we done here, if you've started a new thread?
    Socket Overflow by sending / receiving ACK's
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by Salem View Post
    So are we done here, if you've started a new thread?
    Socket Overflow by sending / receiving ACK's
    This thread was about the ACK's to find them, now they only work in certain cases so it's a different problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending/Receiving packets over socket
    By boblettoj99 in forum C Programming
    Replies: 8
    Last Post: 10-22-2010, 10:01 AM
  2. Casting a struct for sending to socket
    By chrisjmoss in forum Networking/Device Communication
    Replies: 6
    Last Post: 04-08-2008, 09:11 AM
  3. Sending null-bytes over a socket?
    By Siphon in forum C Programming
    Replies: 2
    Last Post: 10-08-2007, 03:21 PM
  4. sending in socket
    By fairyjerry14 in forum C Programming
    Replies: 4
    Last Post: 10-08-2007, 07:11 AM
  5. sending zero bytes over TCP socket
    By nantonop in forum Networking/Device Communication
    Replies: 4
    Last Post: 09-03-2007, 08:10 AM