Last active
August 28, 2021 14:36
-
-
Save rlipscombe/0c0f6b6057f398df4e36 to your computer and use it in GitHub Desktop.
Playing with TCP_MAXSEG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <netinet/tcp.h> | |
#include <stdio.h> | |
#include <memory.h> | |
#include <unistd.h> | |
int main(int argc, char *argv[]) | |
{ | |
printf("Hello World!\n"); | |
int lsock = socket(AF_INET, SOCK_STREAM, 0); | |
#if 1 | |
int mss = 576; | |
int result = setsockopt(lsock, IPPROTO_TCP, TCP_MAXSEG, &mss, sizeof(mss)); | |
if (result != 0) | |
{ | |
perror(0); | |
return 1; | |
} | |
#endif | |
// Read back the MSS value. | |
int curr_mss; | |
socklen_t curr_mss_len = sizeof(curr_mss); | |
if (getsockopt(lsock, IPPROTO_TCP, TCP_MAXSEG, &curr_mss, &curr_mss_len) < 0) { | |
perror(0); | |
return 1; | |
} | |
printf("mss: %d\n", curr_mss); | |
int optval = 1; | |
setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); | |
// We should check this at the packet level: listen so that we can run a capture. | |
sockaddr_in bind_addr; | |
memset(&bind_addr, 0, sizeof(bind_addr)); | |
bind_addr.sin_family = AF_INET; | |
bind_addr.sin_port = htons(1145); | |
if (bind(lsock, (const sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) { | |
perror(0); | |
return 1; | |
} | |
if (listen(lsock, SOMAXCONN) < 0) { | |
perror(0); | |
return 1; | |
} | |
printf("Listening.\n"); | |
sockaddr peer_addr; | |
socklen_t peer_addr_len = sizeof(peer_addr); | |
int sock = accept(lsock, &peer_addr, &peer_addr_len); | |
if (sock < 0) { | |
perror(0); | |
return 1; | |
} | |
for (;;) { | |
printf("Accepted.\n"); | |
int curr_mss; | |
socklen_t curr_mss_len = sizeof(curr_mss); | |
if (getsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &curr_mss, &curr_mss_len) < 0) { | |
perror(0); | |
return 1; | |
} | |
printf("mss: %d\n", curr_mss); | |
sleep(30); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment