2

I was writing a simple C server and experimenting with setsockopt, so I wrote the following:

int lsock = socket(AF_INET, SOCK_STREAM, 0);

int mss = 576;
if (setsockopt(lsock, IPPROTO_TCP, TCP_MAXSEG, &mss, sizeof(mss)) != 0) {
    perror("setsockopt");
    return 1;
}

However, this code failed with the following error:

setsockopt: Invalid argument

According to the man page, setsockopt sets errno to EINVAL in two cases:

  • The option is invalid at the level indicated.
  • The socket has been shut down.

Neither case applies here, so what am I doing wrong?

10
  • @SteffenUllrich, what version of MacOS or what MacOS ABI features 64-bit int? I wasn't aware that any did. Or are you perhaps thinking of 64-bit long? Commented Jul 3 at 19:42
  • Is u_int32_t your own typedef? It's not one of those defined by stdint.h (though there is uint32_t, which might be what you meant). Commented Jul 3 at 19:46
  • @JohnBollinger: I was probably wrong, thanks for the comment Commented Jul 3 at 20:55
  • Can you retrieve the TCP_MAXSEG option for your socket via getsockopt()? That speaks to whether your system recognizes the specified layer and option. If so, then can you set the retrieved value on the socket you read it from? That speaks to whether the specific value you are trying to set for the option may be the issue. Commented Jul 4 at 0:00
  • 1
    I can't explain why, but it sounds like the underlying system just won't accept a different MSS from you. setsockopt() is probably just the messenger. I guess this could be a manifestation of some kind of security measure, but I haven't found any documentation to support that (or any other) explanation. Commented Jul 6 at 14:08

1 Answer 1

0

Extract from man 7 tcp:

Socket options: To set or get a TCP socket option, call getsockopt(2) to read or setsockopt(2) to write the option with the option level argument set to IPPROTO_TCP. Unless otherwise noted, optval is a pointer to an int.

So u_int32_t mss is wrong. It should be int mss.

Sign up to request clarification or add additional context in comments.

5 Comments

Taking u_int32_t to be an unsigned integer type, u_int32_t mss is wrong here only if it is not the unsigned version of a type compatible with int. But it probably is such a type. int would probably be a better choice, but, other than the possible misspelling of the type name, I see no reason to think that u_int32_t is wrong.
u_int32_t is always 32 bits, but int can be (and is at modern computers) 64 bits. unsigned int is possible.
int can be 64 bits in principle. But it is 32 bits on x86 and x86_64 Linux, Windows, and MacOS. That's a whole lot of modern computers. I'm not positive about the ARM-based Macs, but I don't think they are different from the Intel based ones in this regard. Exactly which computers are you thinking of where int is 64 bits?
I tried both of them, none worked. but I fixed the question to reflect this.
seems that apple deprecated the option for setting tcp mss.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.