简体   繁体   中英

Using raw sockets in C

When using raw sockets in C, I am trying to set the max segment size of a tcp packet, and I am getting a compile error when trying to implement the answer to a previously asked question -> setting the maximum segment size in the tcp header

I have tried other things on google, forgot to document my trials and the errors they made.. I have my latest attempt, which I will post here.

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

struct tcp_option_mss mss;
mss.kind = 2;
mss.len = 4;
mss.mss = htons(32000);

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

void setup_tcp_header(struct tcphdr *tcp_hdr)
{
    struct tcphdr_mss *tcp_header;

    tcp_header = malloc(sizeof(struct tcphdr_mss));

    tcp_hdr->source = htons(5678);
    tcp_hdr->seq = rand();
    tcp_hdr->ack_seq = 0;
    tcp_hdr->res2 = 0;
    tcp_hdr->doff = 5;
    tcp_hdr->syn = 1;
    tcp_hdr->window = htons(0);
    tcp_hdr->check = 0;
    tcp_hdr->urg_ptr = 0;

    tcp_header->mss.kind = 2;
    tcp_header->mss.len = 4;
    tcp_header->mss.mss = htons(32000);
}

With this, I am getting 3 errors, each at the mss. lines, saying the. is unexpected

error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
 mss.kind = 2;

This is just 1 of the errors, the other 2 are the same, just for the other 2 mss. lines. If you have any tips for fixing this compile issue/if you know if my setup of code wont work, please give me a tip. Also, if there is a way to condense my code to fewer lines, would also be appreciated! Thank you!!

This line is a statement performing an assignment:

mss.kind = 2;

Executable statements (including function calls) cannot exist outside of a function.

You could initialize mss at the point it is defined, but you won't be able to call htons . You'll instead need to move these lines inside of a function.

However, your code doesn't seem to be using mss , so you can just remove it completely.

Also, take note of the answer you accepted in the prior question regarding the function that sets the tcp header struct. Your function is not doing the same as that function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM