forked from acassen/keepalived
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request acassen#2150 from pqarmitage/updates
Update parser recalculating buffer length
- Loading branch information
Showing
4 changed files
with
179 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
tcp_server | ||
tcp_client |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <netdb.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <errno.h> | ||
#include <stdbool.h> | ||
|
||
static void | ||
print_usage(FILE *fp, const char *name) | ||
{ | ||
fprintf(fp, "Usage: %s [options]\n", name); | ||
fprintf(fp, "\t-a addr\t\tbind to addr\n"); | ||
fprintf(fp, "\t-p port\t\tlisten on port\n"); | ||
fprintf(fp, "\t-s\t\tsilent\n"); | ||
fprintf(fp, "\t-u\t\tuse UDP\n"); | ||
fprintf(fp, "\t-d dly\tdelay dly seconds after connect\n"); | ||
fprintf(fp, "\t-e\t\techo\n"); | ||
fprintf(fp, "\t-f\t\tenable tcp_fastopen\n"); | ||
fprintf(fp, "\t-h\t\tprint this\n"); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
struct addrinfo hint = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM }; | ||
struct addrinfo *res; | ||
int ret; | ||
int sock; | ||
char *addr_str; | ||
char *port_str; | ||
bool silent = false; | ||
bool echo_data = false; | ||
int sock_type = SOCK_STREAM; | ||
bool tcp_fastopen = false; | ||
ssize_t r; | ||
ssize_t len; | ||
int opt; | ||
char *endptr; | ||
int msglen = 4000; | ||
char *msg = malloc(msglen); | ||
uint8_t *buf = malloc(msglen); | ||
unsigned delay_after_connect = 0; | ||
|
||
|
||
while ((opt = getopt(argc, argv, ":ha:p:sud:ef")) != -1) { | ||
switch (opt) { | ||
case 'a': | ||
addr_str = optarg; | ||
break; | ||
case 'p': | ||
#if 0 | ||
port_num = strtol(optarg, &endptr, 10); | ||
if (*endptr || port_num <= 0 || port_num > 65535) { | ||
fprintf(stderr, "Port number '%s' invalid\n", optarg); | ||
exit(EXIT_FAILURE); | ||
} | ||
port = port_num; | ||
#endif | ||
port_str = optarg; | ||
break; | ||
case 's': | ||
silent = true; | ||
break; | ||
case 'u': | ||
sock_type = SOCK_DGRAM; | ||
break; | ||
case 'd': | ||
delay_after_connect = strtol(optarg, &endptr, 10); | ||
break; | ||
case 'e': | ||
echo_data = true; | ||
break; | ||
case 'h': | ||
print_usage(stdout, argv[0]); | ||
exit(0); | ||
case 'f': | ||
tcp_fastopen = true; | ||
break; | ||
case ':': | ||
fprintf(stderr, "Option '%c' is missing an argument\n", optopt); | ||
break; | ||
default: /* '?' */ | ||
print_usage(stderr, argv[0]); | ||
exit(EXIT_FAILURE); | ||
} | ||
} | ||
|
||
ret = getaddrinfo(addr_str, port_str, &hint, &res); | ||
if (ret == -1) { | ||
printf("getaddrinfo failed %d (%s)\n", ret, gai_strerror(ret)); | ||
exit(1); | ||
} | ||
|
||
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | ||
if (sock == -1) { | ||
printf("socket failed %d (%m)\n", errno); | ||
exit(1); | ||
} | ||
|
||
if (tcp_fastopen) { | ||
r = sendto(sock, &msg, msglen, MSG_FASTOPEN, res->ai_addr, res->ai_addrlen); | ||
if (r != msglen) | ||
printf("fastopen sendto returned %d (errno %d)\n", r, errno); | ||
} else { | ||
r = connect(sock, res->ai_addr, res->ai_addrlen); | ||
if (r) | ||
printf("connect returned %d (errno %d)\n", r, errno); | ||
} | ||
|
||
if (delay_after_connect) { | ||
sleep(delay_after_connect); | ||
printf("Woken up\n"); | ||
} | ||
|
||
int i = 0; | ||
while (i++ < 5) { | ||
uint16_t *p = (uint16_t *)msg; | ||
for (int h = 0; h < msglen / sizeof(*p); h++) | ||
*p++ = h + i; | ||
write(sock, msg, msglen / 2); | ||
if ((len = read(sock, buf, msglen - 1)) <= 0) { | ||
printf("read returned %d (%m)\n", errno); | ||
exit(1); | ||
} else | ||
printf("read read %zd bytes %u %u %u %u\n", len, buf[0], buf[1], buf[2], buf[3]); | ||
|
||
sleep(1); | ||
} | ||
|
||
shutdown(sock, SHUT_WR); | ||
len = read(sock, buf, msglen - 1); | ||
printf("Final read returned %d\n", len); | ||
shutdown(sock, SHUT_RD); | ||
close(sock); | ||
sleep(1); | ||
|
||
freeaddrinfo(res); | ||
} |
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