Created
June 17, 2022 18:57
-
-
Save szaydel/9adeb8d427fb9e101f79393012a46c65 to your computer and use it in GitHub Desktop.
This was created to validate that on MACOS fixed source port makes it impossible to immediately restart the client due to the socket going into TIME_WAIT state for 30 seconds or so.
This file contains hidden or 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 <assert.h> | |
#include <ctype.h> | |
#include <ifaddrs.h> | |
#include <netdb.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <arpa/inet.h> | |
#include <sys/socket.h> | |
#include <sys/time.h> | |
int main(void) { | |
int ret, fd; | |
struct sockaddr_in sa_dst; | |
struct sockaddr_in sa_loc; | |
const char buffer[] = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n"; | |
char resp_buffer[16384]; | |
fd = socket(AF_INET, SOCK_STREAM, 0); | |
assert(fd != -1); | |
// Client-side configuration | |
memset(&sa_loc, 0, sizeof(sa_dst)); | |
sa_loc.sin_family = AF_INET; | |
sa_loc.sin_port = htons(10456); /* pinned port */ | |
sa_loc.sin_addr.s_addr = inet_addr("0.0.0.0"); | |
struct timeval tv; | |
tv.tv_sec = 2; | |
tv.tv_usec = 0; | |
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv)); | |
// This does not do anything on a client socket, which means that if a | |
// service were to crash it could not restart until the connection left | |
// TIME_WAIT state. | |
int optval = 1; | |
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)); | |
assert(ret != -1); | |
ret = bind(fd, (struct sockaddr *)&sa_loc, sizeof(struct sockaddr)); | |
assert(ret != -1); | |
// Remote end | |
memset(&sa_dst, 0, sizeof(struct sockaddr_in)); | |
sa_dst.sin_family = AF_INET; | |
sa_dst.sin_port = htons(80); | |
sa_dst.sin_addr.s_addr = inet_addr("93.184.216.34"); // example.com | |
ret = connect(fd, (struct sockaddr *)&sa_dst, sizeof(struct sockaddr)); | |
assert(ret != -1); | |
send(fd, buffer, strlen(buffer), 0); | |
ssize_t recv_ret; | |
int newline = 0; | |
int size_nchars = 0; | |
long body_length = -1; | |
ssize_t total_recvd = 0; | |
char body_length_s[12]; | |
while (1) { | |
if (body_length > 0 && total_recvd >= body_length) { | |
close(fd); | |
exit(EXIT_SUCCESS); | |
} | |
recv_ret = recv(fd, resp_buffer, sizeof(resp_buffer), 0); | |
if (recv_ret < 0) { | |
perror("Failed to recv"); | |
exit(EXIT_FAILURE); | |
} | |
total_recvd += recv_ret; | |
printf("%s", resp_buffer); | |
if (body_length < 0) { | |
for (size_t i = 0 ; i < recv_ret; i++) { | |
if (newline) { | |
newline = 0; | |
int diff = strncasecmp(&resp_buffer[i], "Content-Length:", strlen("Content-Length:")); | |
if (!diff) { | |
char *start = &resp_buffer[i] + strlen("Content-Length: "); | |
char *p = start; | |
while (isdigit(*p)) { | |
size_nchars++; | |
p++; | |
} | |
strncpy(body_length_s, start, p - start); | |
body_length = strtol(body_length_s, NULL, 10); | |
break; | |
} | |
} | |
if (resp_buffer[i] == '\n') { | |
newline = 1; | |
continue; | |
} | |
} | |
} | |
resp_buffer[0] = '\0'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment