/* * Copyright (C) 2004 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2, or (at your option) any * later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Authors: * Mark McLoughlin */ #include #include #include #include #include #include #include #include #undef DEBUG #ifdef DEBUG static void debug_log (int level, const char *str) { fputs (str, stderr); } #endif static int do_client (int fd) { gnutls_session session; static const int kx_priority[] = { GNUTLS_KX_ANON_DH, 0 }; gnutls_anon_server_credentials anon_cred; char buf[256]; int n; int err; int retval = 1; gnutls_init (&session, GNUTLS_CLIENT); gnutls_set_default_priority (session); gnutls_kx_set_priority (session, kx_priority); gnutls_anon_allocate_client_credentials (&anon_cred); gnutls_credentials_set (session, GNUTLS_CRD_ANON, anon_cred); gnutls_transport_set_ptr (session, (gnutls_transport_ptr) fd); do { fprintf (stderr, "Trying to complete TLS handshake\n"); err = gnutls_handshake (session); } while (err != GNUTLS_E_SUCCESS && !gnutls_error_is_fatal (err)); if (err != GNUTLS_E_SUCCESS) { fprintf (stderr, "TLS Handshake failed: %s\n", gnutls_strerror (err)); goto session_deinit_and_return; } while ((n = gnutls_record_recv (session, &buf, 256 - 1)) > 0) { buf [n] = '\0'; printf ("%s\n", buf); } if (n < 0) { fprintf (stderr, "Error reading from server: %s\n", gnutls_strerror (n)); retval = 0; } else if (n == 0) { fprintf (stderr, "Got EOF from recv\n"); } session_deinit_and_return: do { fprintf (stderr, "Trying to complete shutdown\n"); err = gnutls_bye (session, GNUTLS_SHUT_WR); } while (err != GNUTLS_E_SUCCESS && !gnutls_error_is_fatal (err)); if (err != GNUTLS_E_SUCCESS) { fprintf (stderr, "TLS shutdown failed: %s\n", gnutls_strerror (err)); retval = 0; } gnutls_anon_free_client_credentials (anon_cred); gnutls_deinit (session); return retval; } int main (int argc, char *argv[]) { int sock; struct sockaddr_in servaddr; int retval = 0; gnutls_global_init (); #ifdef DEBUG gnutls_global_set_log_level (10); gnutls_global_set_log_function (debug_log); #endif if ((sock = socket (AF_INET, SOCK_STREAM, 0)) == -1) { fprintf (stderr, "Error creating socket: %s\n", strerror (errno)); retval = 1; goto global_deinit_and_return; } memset (&servaddr, 0, sizeof (struct sockaddr_in)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons (5899); servaddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); if ((connect (sock, (struct sockaddr *) &servaddr, sizeof (struct sockaddr_in)) == -1)) { fprintf (stderr, "Error connecting to server: %s\n", strerror (errno)); retval = 1; goto global_deinit_and_return; } if (do_client (sock)) do_client (sock); close (sock); global_deinit_and_return: gnutls_global_deinit (); return retval; }