diff options
author | Aleksey Filippov <alekseyf@google.com> | 2015-01-23 19:14:28 +0300 |
---|---|---|
committer | Aleksey Filippov <alekseyf@google.com> | 2015-01-23 19:14:37 +0300 |
commit | 8dd08e2cd29b2b0c1174adac4935e256b38c08b7 (patch) | |
tree | f2e22cf2957638caad4b6afeee45e33df35173e2 /tests | |
parent | ea51cdac29026b94eeb00baefb568b82b9d05ca2 (diff) |
add sendto hook to handle MSG_FASTOPEN flag
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_sendto.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/test_sendto.c b/tests/test_sendto.c new file mode 100644 index 000000000..77d2e9c --- /dev/null +++ b/tests/test_sendto.c @@ -0,0 +1,62 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netdb.h> + +void error(const char *msg) +{ + perror(msg); + exit(1); +} + +int main(int argc, char *argv[]) +{ + if (argc < 4) { + printf("Usage: %s host port method(connect or sendto)\n", argv[0]); + return 1; + } + const char *hostname = argv[1]; + const int portno = atoi(argv[2]); + const char *method = argv[3]; + char request[BUFSIZ]; + sprintf(request, "GET / HTTP/1.0\r\nHost: %s\r\n\r\n", hostname); + int sockfd, n; + struct sockaddr_in serv_addr; + struct hostent *server; + + char buffer[BUFSIZ]; + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) error("ERROR opening socket"); + server = gethostbyname(hostname); + if (server == NULL) { + fprintf(stderr, "%s: no such host\n", hostname); + return 1; + } + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length); + serv_addr.sin_port = htons(portno); + if (!strcmp(method, "connect")) { + if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) + error("connect"); + n = send(sockfd, request, strlen(request), 0); + } else if (!strcmp(method, "sendto")) { + n = sendto(sockfd, request, strlen(request), MSG_FASTOPEN, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); + } else { + printf("Unknown method %s\n", method); + return 1; + } + if (n < 0) + error("send"); + memset(buffer, 0, BUFSIZ); + n = read(sockfd, buffer, BUFSIZ - 1); + if (n < 0) + error("ERROR reading from socket"); + printf("%s\n", buffer); + close(sockfd); + return 0; +} |