X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=tests%2Fclient.c;h=41ddf5d1a3a2573de54a85e901f533ca42e08dd1;hb=5c495b3f7f1e4553c7f8212675b212f2b2a6fdb2;hp=d4edf7f8ba6f911fe674c1d1a697e21eb8bb908b;hpb=1d507eaf013fdffb6c17f0f02f6ee9a91d44aa62;p=tlsproxy%2Ftlsproxy.git diff --git a/tests/client.c b/tests/client.c index d4edf7f..41ddf5d 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; @@ -53,9 +54,10 @@ int main (int argc, char *argv[]) { const gnutls_datum_t *cert_list; unsigned int cert_list_size; - if (argc != 5) { + if (argc != 5 && argc != 6) { fprintf(stderr, - "Usage: %s \n", + "Usage: %s " + "[]\n", argv[0]); return EXIT_FAILURE; } @@ -74,17 +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, "\r\n"); - fflush(fd); - if (read_http_request(fd, buffer, sizeof(buffer)) == -1) { + fprintf(fd_write, "CONNECT %s:%s HTTP/1.0\r\n", argv[2], argv[3]); + if (argc == 6) { + fprintf(fd_write, "Proxy-Authorization: Basic %s\r\n", argv[5]); + } + 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; } @@ -150,8 +153,14 @@ int main (int argc, char *argv[]) { gnutls_x509_crt_deinit(cert); + /* Send a bogus request to the server. Otherwise recent gnutls-serv won't + * terminate the connection when gnutls_bye() is used. */ + gnutls_record_send(session, "GET / HTTP/1.0\r\n\r\n", + strlen("GET / HTTP/1.0\r\n\r\n")); + gnutls_bye(session, GNUTLS_SHUT_RDWR); - fclose(fd); + fclose(fd_read); + fclose(fd_write); gnutls_deinit(session); gnutls_certificate_free_credentials(xcred); @@ -163,6 +172,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; @@ -200,12 +227,12 @@ static int connect_to_host(const char *hostname, const char *port) { server_socket = socket(server->ai_family, server->ai_socktype, server->ai_protocol); - if (server_socket == -1) { + if (server_socket < 0) { perror("connect_to_host(): socket(), trying next"); continue; } - if (connect(server_socket, server->ai_addr, server->ai_addrlen) != -1) { + if (connect(server_socket, server->ai_addr, server->ai_addrlen) == 0) { break; } perror("connect_to_host(): connect(), trying next"); @@ -231,11 +258,11 @@ static int read_http_request(FILE *client_fd, char *request, size_t length) { perror("read_http_request(): fgets()"); return -1; } - + /* EOF */ return -2; } - while (fgets(buffer, MAX_REQUEST_LINE, client_fd) != NULL) { + while (fgets(buffer, sizeof(buffer), client_fd) != NULL) { /* End of header. */ if (!strcmp(buffer, "\n") || !strcmp(buffer, "\r\n")) { break;