summary history branches tags files
commit:107cfd9b48f0370f5661da48e093dce60c21cddb
author:Trevor Bentley
committer:Trevor Bentley
date:Sun May 26 14:25:27 2019 +0200
parents:436b10eaf7bb746f6292249263eec99ee4925038
Fix C API return values, improve examples
diff --git a/examples/client.c b/examples/client.c
line changes: +17/-7
index 6ca25bb..bc7f465
--- a/examples/client.c
+++ b/examples/client.c
@@ -46,6 +46,7 @@ int main(int argc, char **argv) {
   int sock;
   int read_len;
   int handshake;
+  int ossuary_res;
   uint16_t read_buf_len;
   uint16_t write_buf_len;
   uint16_t text_buf_len;
@@ -75,16 +76,20 @@ int main(int argc, char **argv) {
     if ((handshake = ossuary_handshake_done(client_conn)) == 0) {
       write_buf_len = sizeof(write_buf);
       // Check if we have any handshake packets to send
-      if (ossuary_send_handshake(client_conn, write_buf, &write_buf_len) < 0) {
-        fprintf(stderr, "ERROR: handshake send failed\n");
-        exit(1);
+      if ((ossuary_res = ossuary_send_handshake(client_conn, write_buf, &write_buf_len)) < 0) {
+        if (ossuary_res != OSSUARY_ERR_WOULDBLOCK) {
+          fprintf(stderr, "ERROR: handshake send failed\n");
+          exit(1);
+        }
       }
       // Check if we have any received handshake packets to parse
       if (read_buf_len > 0) {
         read_len = read_buf_len;
-        if (ossuary_recv_handshake(client_conn, read_buf, (uint16_t*)&read_len) < 0) {
-          fprintf(stderr, "ERROR: handshake recv failed\n");
-          exit(1);
+        if ((ossuary_res = ossuary_recv_handshake(client_conn, read_buf, (uint16_t*)&read_len)) < 0) {
+          if (ossuary_res != OSSUARY_ERR_WOULDBLOCK) {
+            fprintf(stderr, "ERROR: handshake recv failed\n");
+            exit(1);
+          }
         }
         // Consume bytes from read_buf
         memmove(read_buf, read_buf + read_len, read_buf_len - read_len);
@@ -104,7 +109,12 @@ int main(int argc, char **argv) {
       gets((char *)text_buf);
       text_buf_len = strlen((char *)text_buf) + 1;
       write_buf_len = sizeof(write_buf);
-      ossuary_send_data(client_conn, text_buf, text_buf_len, write_buf, &write_buf_len);
+      if ((ossuary_res = ossuary_send_data(client_conn, text_buf, text_buf_len, write_buf, &write_buf_len)) < 0) {
+        if (ossuary_res != OSSUARY_ERR_WOULDBLOCK) {
+          fprintf(stderr, "ERROR: send failed\n");
+          exit(1);
+        }
+      }
     }
 
     // Write any encrypted messages generated by Ossuary

diff --git a/examples/server.c b/examples/server.c
line changes: +27/-13
index ebc2816..267d45c
--- a/examples/server.c
+++ b/examples/server.c
@@ -45,6 +45,7 @@ int main(int argc, char **argv) {
   int read_len;
   int client;
   int handshake;
+  int ossuary_res;
   uint16_t read_buf_len = 0;
   uint16_t write_buf_len = 0;
   uint16_t text_buf_len = 0;
@@ -53,11 +54,10 @@ int main(int argc, char **argv) {
   client = net_connect();
 
   // Create an Ossuary server
-  if ((server_conn = ossuary_create_connection(OSSUARY_CONN_TYPE_UNAUTHENTICATED_SERVER, NULL)) == NULL) {
+  if ((server_conn = ossuary_create_connection(OSSUARY_CONN_TYPE_UNAUTHENTICATED_SERVER, secret_key)) == NULL) {
     fprintf(stderr, "ERROR: could not create Ossuary connection\n");
     exit(1);
   }
-  ossuary_set_secret_key(server_conn, secret_key);
 
   while (1) {
     // Read from network socket
@@ -74,16 +74,20 @@ int main(int argc, char **argv) {
     if ((handshake = ossuary_handshake_done(server_conn)) == 0) {
       write_buf_len = sizeof(write_buf);
       // Check if we have any handshake packets to send
-      if (ossuary_send_handshake(server_conn, write_buf, &write_buf_len) < 0) {
-        fprintf(stderr, "ERROR: handshake send failed\n");
-        exit(1);
+      if ((ossuary_res = ossuary_send_handshake(server_conn, write_buf, &write_buf_len)) < 0) {
+        if (ossuary_res != OSSUARY_ERR_WOULDBLOCK) {
+          fprintf(stderr, "ERROR: handshake send failed\n");
+          exit(1);
+        }
       }
       // Check if we have any received handshake packets to parse
       if (read_buf_len > 0) {
         read_len = read_buf_len;
-        if (ossuary_recv_handshake(server_conn, read_buf, (uint16_t*)&read_len) < 0) {
-          fprintf(stderr, "ERROR: handshake recv failed\n");
-          exit(1);
+        if ((ossuary_res = ossuary_recv_handshake(server_conn, read_buf, (uint16_t*)&read_len)) < 0) {
+          if (ossuary_res != OSSUARY_ERR_WOULDBLOCK) {
+            fprintf(stderr, "ERROR: handshake recv failed\n");
+            exit(1);
+          }
         }
         // Consume bytes from read_buf
         memmove(read_buf, read_buf + read_len, read_buf_len - read_len);
@@ -97,15 +101,25 @@ int main(int argc, char **argv) {
     }
     // Ossuary data exchange over established connection
     else {
-      // If data has been received, decrypt and print it.
+      // If data has been received, decrypt it
       if (read_buf_len > 0) {
         text_buf_len = sizeof(text_buf);
-        if ((read_len = ossuary_recv_data(server_conn, read_buf, read_buf_len, text_buf, &text_buf_len)) > 0) {
+        read_len = read_buf_len;
+        if ((ossuary_res = ossuary_recv_data(server_conn, read_buf,
+                                             (uint16_t*)&read_len, text_buf, &text_buf_len)) < 0) {
+          if (ossuary_res != OSSUARY_ERR_WOULDBLOCK) {
+            fprintf(stderr, "ERROR: recv failed\n");
+            exit(1);
+          }
+        }
+        // Data can be consumed even if ossuary_recv_data() returned no data
+        if (read_len > 0) {
           memmove(read_buf, read_buf + read_len, read_buf_len - read_len);
           read_buf_len -= read_len;
-          if (text_buf_len > 0) {
-            printf("MSG: %s\n", text_buf);
-          }
+        }
+        // Print message if one was decrypted
+        if (text_buf_len > 0) {
+          printf("MSG: %s\n", text_buf);
         }
       }
     }

diff --git a/src/clib.rs b/src/clib.rs
line changes: +22/-6
index 7418285..96fec85
--- a/src/clib.rs
+++ b/src/clib.rs
@@ -112,7 +112,10 @@ pub extern "C" fn ossuary_recv_handshake(conn: *mut OssuaryConnection,
             unsafe { *in_buf_len = b as u16; }
             OSSUARY_ERR_WOULD_BLOCK
         },
-        _ => -1i32,
+        _ => {
+            unsafe { *in_buf_len = 0u16; }
+            -1i32
+        },
     };
     ::std::mem::forget(conn);
     read as i32
@@ -137,7 +140,10 @@ pub extern "C" fn ossuary_send_handshake(conn: *mut OssuaryConnection,
             unsafe { *out_buf_len = w as u16 };
             OSSUARY_ERR_WOULD_BLOCK
         },
-        Err(_) => -1,
+        Err(_) => {
+            unsafe { *out_buf_len = 0u16 };
+            -1
+        },
     };
     ::std::mem::forget(conn);
     wrote
@@ -182,7 +188,10 @@ pub extern "C" fn ossuary_send_data(conn: *mut OssuaryConnection,
             unsafe { *out_buf_len = w as u16; }
             OSSUARY_ERR_WOULD_BLOCK
         },
-        Err(_) => -1i32,
+        Err(_) => {
+            unsafe { *out_buf_len = 0u16; }
+            -1i32
+        },
     };
     ::std::mem::forget(conn);
     bytes_written
@@ -211,13 +220,20 @@ pub extern "C" fn ossuary_recv_data(conn: *mut OssuaryConnection,
             };
             read as i32
         },
-        Err(OssuaryError::WouldBlock(w)) => {
+        Err(OssuaryError::WouldBlock(r)) => {
             unsafe {
-                *out_buf_len = w as u16;
+                *in_buf_len = r as u16;
+                *out_buf_len = 0u16;
             };
             OSSUARY_ERR_WOULD_BLOCK
         },
-        Err(_) => -1i32,
+        Err(_) => {
+            unsafe {
+                *in_buf_len = 0u16;
+                *out_buf_len = 0u16;
+            }
+            -1i32
+        },
     };
     ::std::mem::forget(conn);
     bytes_read as i32