Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 333083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:55:42+00:00 2026-05-12T09:55:42+00:00

I am putting together a port scanner as a learning exercise. My problem is

  • 0

I am putting together a port scanner as a learning exercise. My problem is I’m trying to set the maximum segment size option(MSS) in the TCP header. I had a look at tcp.h, but I’m having trouble figuring out how to set it. I was hoping there would be an option like this:

tcp_header->mss(32000);

Something similar to the above was in tcp.h but not in the right struct. Admittedly, I’m still fairly new to reading struct definitions and I couldn’t make much sense out of tcp.h so in the end I tried just tacking on the necessary bytes to the end of the TCP header:

struct tcphdr *CreateTcpHeader()
{
    struct tcphdr *tcp_header;

    tcp_header = (struct tcphdr *)malloc(sizeof(struct tcphdr)+4*sizeof(int));


    tcp_header->source = htons(SRC_PORT);
    tcp_header->dest = htons(DST_PORT);
    tcp_header->seq = htonl(0);             
    tcp_header->ack_seq = htonl(0);         
    tcp_header->res1 = 0;
    tcp_header->doff = (sizeof(struct tcphdr))/4;
    tcp_header->syn = 1;
    tcp_header->window = htons(4096);
    tcp_header->check = 0; /* Will calculate the checksum with pseudo-header later */
    tcp_header->urg_ptr = 0;


    /*memcpy the mss data onto the end of the tcp header. */
    int mssCode = 2;
    int mssLength = 4;
    uint16_t mss = htonl(32000);
    int offset = sizeof(struct tcphdr);
    memcpy( (tcp_header+offset), &mssCode, 1 );
    memcpy( (tcp_header+offset+1), &mssLength, 1 );
    memcpy( (tcp_header+offset+2), &mss, 2);

    return (tcp_header);
}

But after I wrote it it was clear that is wasn’t a real solution, plus it still doesn’t work 😛 So is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:55:42+00:00Added an answer on May 12, 2026 at 9:55 am

    The struct tcphdr in tcp.h defines the mandatory part of the TCP header. (Look at the TCP header and you can match the definitions in struct tcphdr to your the actual bits to appear in the header.) Structs in C have a constant size, but TCP allows optional data. The header length field (doff in the structure) is the total length of the header, including options, so you’ll need to add one word to account for the MSS option:

    tcp_header->doff = (sizeof(struct tcphdr))/4 + 1;
    

    Let’s define a structure for the MSS option:

    struct tcp_option_mss {
        uint8_t kind; /* 2 */
        uint8_t len; /* 4 */
        uint16_t mss;
    } __attribute__((packed));
    

    Now you can populate the structure, in the right order:

    /*memcpy the mss data onto the end of the tcp header. */
    struct tcp_option_mss mss;
    mss.kind = 2;
    mss.len = 4;
    mss.mss = htons(32000);
    

    Let’s go one step further and define a single structure for your packet, to let the compiler
    help us out:

    struct tcphdr_mss {
        struct tcphdr tcp_header;
        struct tcp_option_mss mss;
    };
    

    (You may need to add an end-of-option-list option at the end, and nop options to pad the option list to 8 bytes.)

    Now we can put all the pieces together:

    struct tcphdr *CreateTcpHeader()
    {
        struct tcphdr_mss *tcp_header;
    
        tcp_header = malloc(sizeof(struct tcphdr_mss));
    
        tcp_header->tcp_header.source = htons(SRC_PORT);
        tcp_header->tcp_header.dest = htons(DST_PORT);
        tcp_header->tcp_header.seq = htonl(0);             
        tcp_header->tcp_header.ack_seq = htonl(0);         
        tcp_header->tcp_header.res1 = 0;
        tcp_header->tcp_header.doff = (sizeof(struct tcphdr_mss))/4;
        tcp_header->tcp_header.syn = 1;
        tcp_header->tcp_header.window = htons(4096);
        tcp_header->tcp_header.check = 0; /* Will calculate the checksum with pseudo-header later */
        tcp_header->tcp_header.urg_ptr = 0;
    
        tcp_header->mss.kind = 2;
        tcp_header->mss.len = 2;
        tcp_header->mss.mss = htons(32000);
    
        return (tcp_header);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Categories extend the original class, but they don't subclass it,… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer Haven't tested this, but it's something like: RewriteRule \.php$ -… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer "0:0:0:0:0:0:0:1" is the IPv6 loopback address as defined in RFC… May 12, 2026 at 12:54 pm

Related Questions

I am putting together some ideas for our automated testing platform and have been
I am putting together a proposal for a large multinational company for our licenced
I am putting together a build system and wanted to know if there is
I am putting together a Samba-based server as a Primary Domain Controller, and ran
I am putting together a REST API and as I'm unsure how it will

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.