Tizen 2.0 Release
[external/libgnutls26.git] / doc / examples / ex-client-resume.c
1 /* This example code is placed in the public domain. */
2
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6
7 #include <string.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <gnutls/gnutls.h>
11
12 /* Those functions are defined in other examples.
13  */
14 extern void check_alert (gnutls_session_t session, int ret);
15 extern int tcp_connect (void);
16 extern void tcp_close (int sd);
17
18 #define MAX_BUF 1024
19 #define CAFILE "ca.pem"
20 #define MSG "GET / HTTP/1.0\r\n\r\n"
21
22 int
23 main (void)
24 {
25   int ret;
26   int sd, ii;
27   gnutls_session_t session;
28   char buffer[MAX_BUF + 1];
29   gnutls_certificate_credentials_t xcred;
30
31   /* variables used in session resuming 
32    */
33   int t;
34   char *session_data = NULL;
35   size_t session_data_size = 0;
36
37   gnutls_global_init ();
38
39   /* X509 stuff */
40   gnutls_certificate_allocate_credentials (&xcred);
41
42   gnutls_certificate_set_x509_trust_file (xcred, CAFILE, GNUTLS_X509_FMT_PEM);
43
44   for (t = 0; t < 2; t++)
45     {                           /* connect 2 times to the server */
46
47       sd = tcp_connect ();
48
49       gnutls_init (&session, GNUTLS_CLIENT);
50
51       gnutls_priority_set_direct (session, "PERFORMANCE:!ARCFOUR-128", NULL);
52
53       gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
54
55       if (t > 0)
56         {
57           /* if this is not the first time we connect */
58           gnutls_session_set_data (session, session_data, session_data_size);
59           free (session_data);
60         }
61
62       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
63
64       /* Perform the TLS handshake
65        */
66       ret = gnutls_handshake (session);
67
68       if (ret < 0)
69         {
70           fprintf (stderr, "*** Handshake failed\n");
71           gnutls_perror (ret);
72           goto end;
73         }
74       else
75         {
76           printf ("- Handshake was completed\n");
77         }
78
79       if (t == 0)
80         {                       /* the first time we connect */
81           /* get the session data size */
82           gnutls_session_get_data (session, NULL, &session_data_size);
83           session_data = malloc (session_data_size);
84
85           /* put session data to the session variable */
86           gnutls_session_get_data (session, session_data, &session_data_size);
87
88         }
89       else
90         {                       /* the second time we connect */
91
92           /* check if we actually resumed the previous session */
93           if (gnutls_session_is_resumed (session) != 0)
94             {
95               printf ("- Previous session was resumed\n");
96             }
97           else
98             {
99               fprintf (stderr, "*** Previous session was NOT resumed\n");
100             }
101         }
102
103       /* This function was defined in a previous example
104        */
105       /* print_info(session); */
106
107       gnutls_record_send (session, MSG, strlen (MSG));
108
109       ret = gnutls_record_recv (session, buffer, MAX_BUF);
110       if (ret == 0)
111         {
112           printf ("- Peer has closed the TLS connection\n");
113           goto end;
114         }
115       else if (ret < 0)
116         {
117           fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
118           goto end;
119         }
120
121       printf ("- Received %d bytes: ", ret);
122       for (ii = 0; ii < ret; ii++)
123         {
124           fputc (buffer[ii], stdout);
125         }
126       fputs ("\n", stdout);
127
128       gnutls_bye (session, GNUTLS_SHUT_RDWR);
129
130     end:
131
132       tcp_close (sd);
133
134       gnutls_deinit (session);
135
136     }                           /* for() */
137
138   gnutls_certificate_free_credentials (xcred);
139
140   gnutls_global_deinit ();
141
142   return 0;
143 }