Added <stdbool.h> for standard bool definitions
[dockapps/wmnotify.git] / src / network.c
index 2520000..0863cd8 100644 (file)
 #define RECV_FLAGS 0
 
 
+/* Common buffers for IMAP4 and POP3. */
+char tx_buffer[WMNOTIFY_BUFSIZE + 1];
+char rx_buffer[WMNOTIFY_BUFSIZE + 1];
+
+
 int
 SocketOpen( char *server_name, int port )
 {
@@ -40,7 +45,7 @@ SocketOpen( char *server_name, int port )
   
   hostinfo = gethostbyname(server_name);
   if( hostinfo == NULL ) {
-    perror( PACKAGE );
+    herror( PACKAGE );
     ErrorLocation( __FILE__, __LINE__ );
     goto error;
   }
@@ -101,7 +106,7 @@ ConnectionEstablish( char *server_name, int port )
   }
 
 #if HAVE_SSL
-  if( wmnotify_infos.use_ssl == TRUE ) {
+  if( wmnotify_infos.use_ssl == true ) {
     int status;
     status = InitSSL( wmnotify_infos.sock_fd );
     if( status != EXIT_SUCCESS ) {
@@ -132,7 +137,7 @@ int
 ConnectionTerminate( void )
 {
 #if HAVE_SSL
-  if( wmnotify_infos.use_ssl == TRUE ) {
+  if( wmnotify_infos.use_ssl == true ) {
     SSL_free( ssl_infos.ssl ); /* release connection state */
   }
 #endif
@@ -140,7 +145,7 @@ ConnectionTerminate( void )
   close( wmnotify_infos.sock_fd ); /* close socket */
 
 #if HAVE_SSL
-  if( wmnotify_infos.use_ssl == TRUE ) {
+  if( wmnotify_infos.use_ssl == true ) {
     SSL_CTX_free( ssl_infos.ctx ); /* release context */
   }
 #endif
@@ -155,7 +160,7 @@ WmnotifySendData( char *buffer, int size )
   int len;
 
 #if HAVE_SSL
-  if( wmnotify_infos.use_ssl == TRUE ) {
+  if( wmnotify_infos.use_ssl == true ) {
     len = SSL_write( ssl_infos.ssl, buffer, size ); /* Encrypt & send message */
     if( len <= 0 ) {
       SSL_get_error( ssl_infos.ssl, len );
@@ -188,14 +193,30 @@ WmnotifyGetResponse( char *buffer, int max_size )
   int len;
 
 #if HAVE_SSL
-  if( wmnotify_infos.use_ssl == TRUE ) {
-    len = SSL_read( ssl_infos.ssl, buffer, max_size ); /* Get reply & decrypt */
-    if( len <= 0 ) {
-      SSL_get_error( ssl_infos.ssl, len );
-      len = -1;
+  if( wmnotify_infos.use_ssl == true ) {
+    len = SSL_read( ssl_infos.ssl, buffer, max_size ); /* Get reply & decrypt. */
+    switch( SSL_get_error( ssl_infos.ssl, len ) ) {
+    case SSL_ERROR_NONE:
+      /* Success. */
+      break;
+    case SSL_ERROR_ZERO_RETURN:
+      fprintf( stderr, "%s: SSL_read() connection closed.\n", PACKAGE );
+      break;
+    case SSL_ERROR_SYSCALL:
+      fprintf( stderr, "%s: SSL_read() I/O error.\n", PACKAGE );
+      goto ssl_error;
+    case SSL_ERROR_SSL:
+      fprintf( stderr, "%s: SSL_read() protocol error.\n", PACKAGE );
+      goto ssl_error;
+    default:
+      fprintf( stderr, "%s: SSL_read() error.\n", PACKAGE );
+      goto ssl_error;
     }
-    
+
     return len;
+
+  ssl_error:
+    return -1;
   }
 #endif /* HAVE_SSL */