From: Simon Ruderich Date: Mon, 29 Jul 2013 11:22:01 +0000 (+0200) Subject: tests/client.c: Don't use fdopen(socket, "a+"). X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=72f63c90dd442da6139399d3a3b7fd574d209e08;p=tlsproxy%2Ftlsproxy.git tests/client.c: Don't use fdopen(socket, "a+"). --- diff --git a/tests/client.c b/tests/client.c index 2b7fb07..5142910 100644 --- a/tests/client.c +++ b/tests/client.c @@ -35,6 +35,7 @@ #define MAX_REQUEST_LINE 4096 +static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd); static int connect_to_host(const char *hostname, const char *port); static int read_http_request(FILE *client_fd, char *request, size_t length); @@ -44,7 +45,7 @@ int main (int argc, char *argv[]) { unsigned int status; char buffer[MAX_REQUEST_LINE]; int server; - FILE *fd; + FILE *fd_read, *fd_write; gnutls_session_t session; gnutls_certificate_credentials_t xcred; @@ -75,20 +76,18 @@ int main (int argc, char *argv[]) { if (server == -1) { return EXIT_FAILURE; } - fd = fdopen(server, "a+"); - if (fd == NULL) { - perror("fdopen()"); + if (fdopen_read_write(server, &fd_read, &fd_write) != 0) { return EXIT_FAILURE; } /* Talk to tlsproxy. */ - fprintf(fd, "CONNECT %s:%s HTTP/1.0\r\n", argv[2], argv[3]); + fprintf(fd_write, "CONNECT %s:%s HTTP/1.0\r\n", argv[2], argv[3]); if (argc == 6) { - fprintf(fd, "Proxy-Authorization: Basic %s\r\n", argv[5]); + fprintf(fd_write, "Proxy-Authorization: Basic %s\r\n", argv[5]); } - fprintf(fd, "\r\n"); - fflush(fd); - if (read_http_request(fd, buffer, sizeof(buffer)) == -1) { + fprintf(fd_write, "\r\n"); + fflush(fd_write); + if (read_http_request(fd_read, buffer, sizeof(buffer)) == -1) { fprintf(stderr, "invalid proxy response\n"); return EXIT_FAILURE; } @@ -155,7 +154,8 @@ int main (int argc, char *argv[]) { gnutls_x509_crt_deinit(cert); gnutls_bye(session, GNUTLS_SHUT_RDWR); - fclose(fd); + fclose(fd_read); + fclose(fd_write); gnutls_deinit(session); gnutls_certificate_free_credentials(xcred); @@ -167,6 +167,24 @@ int main (int argc, char *argv[]) { /* Copied from src/connection.c (and removed LOG_* stuff)! Don't modify. */ +static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd) { + *read_fd = fdopen(socket, "r"); + if (*read_fd == NULL) { + perror("fdopen_read_write(): fdopen(\"r\") failed"); + return -1; + } + + *write_fd = fdopen(dup(socket), "w"); + if (*write_fd == NULL) { + perror("fdopen_read_write(): fdopen(\"w\") failed"); + fclose(*read_fd); + *read_fd = NULL; /* "tell" caller read_fd is already closed */ + return -1; + } + + return 0; +} + static int connect_to_host(const char *hostname, const char *port) { struct addrinfo gai_hints; struct addrinfo *gai_result;