corrected verification examples
[gnutls.git] / doc / examples / ex-client-anon.c
blob6a1106b9d268971b9d7e639b2b689f79dd875dfd
1 /* This example code is placed in the public domain. */
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <arpa/inet.h>
13 #include <unistd.h>
14 #include <gnutls/gnutls.h>
16 /* A very basic TLS client, with anonymous authentication.
19 #define MAX_BUF 1024
20 #define MSG "GET / HTTP/1.0\r\n\r\n"
22 extern int tcp_connect (void);
23 extern void tcp_close (int sd);
25 int
26 main (void)
28 int ret, sd, ii;
29 gnutls_session_t session;
30 char buffer[MAX_BUF + 1];
31 gnutls_anon_client_credentials_t anoncred;
32 /* Need to enable anonymous KX specifically. */
34 gnutls_global_init ();
36 gnutls_anon_allocate_client_credentials (&anoncred);
38 /* Initialize TLS session
40 gnutls_init (&session, GNUTLS_CLIENT);
42 /* Use default priorities */
43 gnutls_priority_set_direct (session, "PERFORMANCE:+ANON-ECDH:+ANON-DH",
44 NULL);
46 /* put the anonymous credentials to the current session
48 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
50 /* connect to the peer
52 sd = tcp_connect ();
54 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
55 gnutls_handshake_set_timeout (session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
57 /* Perform the TLS handshake
61 ret = gnutls_handshake (session);
63 while (ret < 0 && gnutls_error_is_fatal (ret) == 0);
65 if (ret < 0)
67 fprintf (stderr, "*** Handshake failed\n");
68 gnutls_perror (ret);
69 goto end;
71 else
73 printf ("- Handshake was completed\n");
76 gnutls_record_send (session, MSG, strlen (MSG));
78 ret = gnutls_record_recv (session, buffer, MAX_BUF);
79 if (ret == 0)
81 printf ("- Peer has closed the TLS connection\n");
82 goto end;
84 else if (ret < 0)
86 fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
87 goto end;
90 printf ("- Received %d bytes: ", ret);
91 for (ii = 0; ii < ret; ii++)
93 fputc (buffer[ii], stdout);
95 fputs ("\n", stdout);
97 gnutls_bye (session, GNUTLS_SHUT_RDWR);
99 end:
101 tcp_close (sd);
103 gnutls_deinit (session);
105 gnutls_anon_free_client_credentials (anoncred);
107 gnutls_global_deinit ();
109 return 0;