diff options
author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2012-12-28 16:40:21 +0200 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2012-12-28 16:40:42 +0200 |
commit | 8ebb2e84fc27ca7770c2b5775d6a41576da01a7e (patch) | |
tree | 1147deeaa6ec6e7cb8bfb6d63b0b7bde29badbfd /doc/examples | |
parent | 7730c6cba3e4d5e1d2c4acf3f433125910635a6e (diff) | |
download | gnutls-8ebb2e84fc27ca7770c2b5775d6a41576da01a7e.tar.gz |
better code in client and server examples
Diffstat (limited to 'doc/examples')
-rw-r--r-- | doc/examples/ex-client-anon.c | 15 | ||||
-rw-r--r-- | doc/examples/ex-client-dtls.c | 15 | ||||
-rw-r--r-- | doc/examples/ex-client-psk.c | 15 | ||||
-rw-r--r-- | doc/examples/ex-client-resume.c | 15 | ||||
-rw-r--r-- | doc/examples/ex-client-x509.c | 15 | ||||
-rw-r--r-- | doc/examples/ex-serv-anon.c | 7 | ||||
-rw-r--r-- | doc/examples/ex-serv-dtls.c | 9 | ||||
-rw-r--r-- | doc/examples/ex-serv-pgp.c | 7 | ||||
-rw-r--r-- | doc/examples/ex-serv-psk.c | 7 | ||||
-rw-r--r-- | doc/examples/ex-serv-srp.c | 7 | ||||
-rw-r--r-- | doc/examples/ex-serv-x509.c | 7 |
11 files changed, 88 insertions, 31 deletions
diff --git a/doc/examples/ex-client-anon.c b/doc/examples/ex-client-anon.c index 6a1106b9d2..a85071bbce 100644 --- a/doc/examples/ex-client-anon.c +++ b/doc/examples/ex-client-anon.c @@ -81,18 +81,25 @@ main (void) printf ("- Peer has closed the TLS connection\n"); goto end; } + else if (ret < 0 && gnutls_error_is_fatal (ret) == 0) + { + fprintf (stderr, "*** Warning: %s\n", gnutls_strerror (ret)); + } else if (ret < 0) { fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret)); goto end; } - printf ("- Received %d bytes: ", ret); - for (ii = 0; ii < ret; ii++) + if (ret > 0) { - fputc (buffer[ii], stdout); + printf ("- Received %d bytes: ", ret); + for (ii = 0; ii < ret; ii++) + { + fputc (buffer[ii], stdout); + } + fputs ("\n", stdout); } - fputs ("\n", stdout); gnutls_bye (session, GNUTLS_SHUT_RDWR); diff --git a/doc/examples/ex-client-dtls.c b/doc/examples/ex-client-dtls.c index 412e149be1..3f005782f4 100644 --- a/doc/examples/ex-client-dtls.c +++ b/doc/examples/ex-client-dtls.c @@ -97,18 +97,25 @@ main (void) printf ("- Peer has closed the TLS connection\n"); goto end; } + else if (ret < 0 && gnutls_error_is_fatal (ret) == 0) + { + fprintf (stderr, "*** Warning: %s\n", gnutls_strerror (ret)); + } else if (ret < 0) { fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret)); goto end; } - printf ("- Received %d bytes: ", ret); - for (ii = 0; ii < ret; ii++) + if (ret > 0) { - fputc (buffer[ii], stdout); + printf ("- Received %d bytes: ", ret); + for (ii = 0; ii < ret; ii++) + { + fputc (buffer[ii], stdout); + } + fputs ("\n", stdout); } - fputs ("\n", stdout); /* It is suggested not to use GNUTLS_SHUT_RDWR in DTLS * connections because the peer's closure message might diff --git a/doc/examples/ex-client-psk.c b/doc/examples/ex-client-psk.c index a456f6cf4c..a68944a49b 100644 --- a/doc/examples/ex-client-psk.c +++ b/doc/examples/ex-client-psk.c @@ -91,18 +91,25 @@ main (void) printf ("- Peer has closed the TLS connection\n"); goto end; } + else if (ret < 0 && gnutls_error_is_fatal (ret) == 0) + { + fprintf (stderr, "*** Warning: %s\n", gnutls_strerror (ret)); + } else if (ret < 0) { fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret)); goto end; } - printf ("- Received %d bytes: ", ret); - for (ii = 0; ii < ret; ii++) + if (ret > 0) { - fputc (buffer[ii], stdout); + printf ("- Received %d bytes: ", ret); + for (ii = 0; ii < ret; ii++) + { + fputc (buffer[ii], stdout); + } + fputs ("\n", stdout); } - fputs ("\n", stdout); gnutls_bye (session, GNUTLS_SHUT_RDWR); diff --git a/doc/examples/ex-client-resume.c b/doc/examples/ex-client-resume.c index 0fe2a8860e..de3b403857 100644 --- a/doc/examples/ex-client-resume.c +++ b/doc/examples/ex-client-resume.c @@ -117,18 +117,25 @@ main (void) printf ("- Peer has closed the TLS connection\n"); goto end; } + else if (ret < 0 && gnutls_error_is_fatal (ret) == 0) + { + fprintf (stderr, "*** Warning: %s\n", gnutls_strerror (ret)); + } else if (ret < 0) { fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret)); goto end; } - printf ("- Received %d bytes: ", ret); - for (ii = 0; ii < ret; ii++) + if (ret > 0) { - fputc (buffer[ii], stdout); + printf ("- Received %d bytes: ", ret); + for (ii = 0; ii < ret; ii++) + { + fputc (buffer[ii], stdout); + } + fputs ("\n", stdout); } - fputs ("\n", stdout); gnutls_bye (session, GNUTLS_SHUT_RDWR); diff --git a/doc/examples/ex-client-x509.c b/doc/examples/ex-client-x509.c index e4a5293abc..d7f57dabbe 100644 --- a/doc/examples/ex-client-x509.c +++ b/doc/examples/ex-client-x509.c @@ -107,18 +107,25 @@ int main (void) printf ("- Peer has closed the TLS connection\n"); goto end; } + else if (ret < 0 && gnutls_error_is_fatal (ret) == 0) + { + fprintf (stderr, "*** Warning: %s\n", gnutls_strerror (ret)); + } else if (ret < 0) { fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret)); goto end; } - printf ("- Received %d bytes: ", ret); - for (ii = 0; ii < ret; ii++) + if (ret > 0) { - fputc (buffer[ii], stdout); + printf ("- Received %d bytes: ", ret); + for (ii = 0; ii < ret; ii++) + { + fputc (buffer[ii], stdout); + } + fputs ("\n", stdout); } - fputs ("\n", stdout); gnutls_bye (session, GNUTLS_SHUT_RDWR); diff --git a/doc/examples/ex-serv-anon.c b/doc/examples/ex-serv-anon.c index c0ae557019..48f22fabfa 100644 --- a/doc/examples/ex-serv-anon.c +++ b/doc/examples/ex-serv-anon.c @@ -122,7 +122,6 @@ main (void) for (;;) { - memset (buffer, 0, MAX_BUF + 1); ret = gnutls_record_recv (session, buffer, MAX_BUF); if (ret == 0) @@ -130,6 +129,10 @@ main (void) printf ("\n- Peer has closed the GnuTLS connection\n"); break; } + else if (ret < 0 && gnutls_error_is_fatal (ret) == 0) + { + fprintf (stderr, "*** Warning: %s\n", gnutls_strerror (ret)); + } else if (ret < 0) { fprintf (stderr, "\n*** Received corrupted " @@ -140,7 +143,7 @@ main (void) { /* echo data back to the client */ - gnutls_record_send (session, buffer, strlen (buffer)); + gnutls_record_send (session, buffer, ret); } } printf ("\n"); diff --git a/doc/examples/ex-serv-dtls.c b/doc/examples/ex-serv-dtls.c index d2c4eda397..098d6f2ede 100644 --- a/doc/examples/ex-serv-dtls.c +++ b/doc/examples/ex-serv-dtls.c @@ -214,17 +214,24 @@ main (void) } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED); - if (ret < 0) + if (ret < 0 && gnutls_error_is_fatal (ret) == 0) + { + fprintf (stderr, "*** Warning: %s\n", gnutls_strerror (ret)); + continue; + } + else if (ret < 0) { fprintf (stderr, "Error in recv(): %s\n", gnutls_strerror (ret)); break; } + if (ret == 0) { printf ("EOF\n\n"); break; } + buffer[ret] = 0; printf ("received[%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x]: %s\n", sequence[0], sequence[1], sequence[2], sequence[3], diff --git a/doc/examples/ex-serv-pgp.c b/doc/examples/ex-serv-pgp.c index 0eacb0db73..aa4f8457c2 100644 --- a/doc/examples/ex-serv-pgp.c +++ b/doc/examples/ex-serv-pgp.c @@ -133,7 +133,6 @@ main (void) for (;;) { - memset (buffer, 0, MAX_BUF + 1); ret = gnutls_record_recv (session, buffer, MAX_BUF); if (ret == 0) @@ -141,6 +140,10 @@ main (void) printf ("\n- Peer has closed the GnuTLS connection\n"); break; } + else if (ret < 0 && gnutls_error_is_fatal (ret) == 0) + { + fprintf (stderr, "*** Warning: %s\n", gnutls_strerror (ret)); + } else if (ret < 0) { fprintf (stderr, "\n*** Received corrupted " @@ -151,7 +154,7 @@ main (void) { /* echo data back to the client */ - gnutls_record_send (session, buffer, strlen (buffer)); + gnutls_record_send (session, buffer, ret); } } printf ("\n"); diff --git a/doc/examples/ex-serv-psk.c b/doc/examples/ex-serv-psk.c index 0c100cfe7b..498beeef02 100644 --- a/doc/examples/ex-serv-psk.c +++ b/doc/examples/ex-serv-psk.c @@ -164,7 +164,6 @@ main (void) for (;;) { - memset (buffer, 0, MAX_BUF + 1); ret = gnutls_record_recv (session, buffer, MAX_BUF); if (ret == 0) @@ -172,6 +171,10 @@ main (void) printf ("\n- Peer has closed the GnuTLS connection\n"); break; } + else if (ret < 0 && gnutls_error_is_fatal (ret) == 0) + { + fprintf (stderr, "*** Warning: %s\n", gnutls_strerror (ret)); + } else if (ret < 0) { fprintf (stderr, "\n*** Received corrupted " @@ -182,7 +185,7 @@ main (void) { /* echo data back to the client */ - gnutls_record_send (session, buffer, strlen (buffer)); + gnutls_record_send (session, buffer, ret); } } printf ("\n"); diff --git a/doc/examples/ex-serv-srp.c b/doc/examples/ex-serv-srp.c index bf92a56966..c3dcae8455 100644 --- a/doc/examples/ex-serv-srp.c +++ b/doc/examples/ex-serv-srp.c @@ -125,7 +125,6 @@ main (void) for (;;) { - memset (buffer, 0, MAX_BUF + 1); ret = gnutls_record_recv (session, buffer, MAX_BUF); if (ret == 0) @@ -133,6 +132,10 @@ main (void) printf ("\n- Peer has closed the GnuTLS connection\n"); break; } + else if (ret < 0 && gnutls_error_is_fatal (ret) == 0) + { + fprintf (stderr, "*** Warning: %s\n", gnutls_strerror (ret)); + } else if (ret < 0) { fprintf (stderr, "\n*** Received corrupted " @@ -143,7 +146,7 @@ main (void) { /* echo data back to the client */ - gnutls_record_send (session, buffer, strlen (buffer)); + gnutls_record_send (session, buffer, ret); } } printf ("\n"); diff --git a/doc/examples/ex-serv-x509.c b/doc/examples/ex-serv-x509.c index 8765d4368c..3f6c305b6e 100644 --- a/doc/examples/ex-serv-x509.c +++ b/doc/examples/ex-serv-x509.c @@ -145,7 +145,6 @@ main (void) for (;;) { - memset (buffer, 0, MAX_BUF + 1); ret = gnutls_record_recv (session, buffer, MAX_BUF); if (ret == 0) @@ -153,6 +152,10 @@ main (void) printf ("\n- Peer has closed the GnuTLS connection\n"); break; } + else if (ret < 0 && gnutls_error_is_fatal (ret) == 0) + { + fprintf (stderr, "*** Warning: %s\n", gnutls_strerror (ret)); + } else if (ret < 0) { fprintf (stderr, "\n*** Received corrupted " @@ -163,7 +166,7 @@ main (void) { /* echo data back to the client */ - gnutls_record_send (session, buffer, strlen (buffer)); + gnutls_record_send (session, buffer, ret); } } printf ("\n"); |