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;
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
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);
}
// 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);
}
}
}
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
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
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
};
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