Tizen 2.0 Release
[external/libgnutls26.git] / doc / examples / ex-alert.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 <stdio.h>
8 #include <stdlib.h>
9 #include <gnutls/gnutls.h>
10
11 #include "examples.h"
12
13 /* This function will check whether the given return code from
14  * a gnutls function (recv/send), is an alert, and will print
15  * that alert.
16  */
17 void
18 check_alert (gnutls_session_t session, int ret)
19 {
20   int last_alert;
21
22   if (ret == GNUTLS_E_WARNING_ALERT_RECEIVED
23       || ret == GNUTLS_E_FATAL_ALERT_RECEIVED)
24     {
25       last_alert = gnutls_alert_get (session);
26
27       /* The check for renegotiation is only useful if we are 
28        * a server, and we had requested a rehandshake.
29        */
30       if (last_alert == GNUTLS_A_NO_RENEGOTIATION &&
31           ret == GNUTLS_E_WARNING_ALERT_RECEIVED)
32         printf ("* Received NO_RENEGOTIATION alert. "
33                 "Client Does not support renegotiation.\n");
34       else
35         printf ("* Received alert '%d': %s.\n", last_alert,
36                 gnutls_alert_get_name (last_alert));
37     }
38 }