Index: libroar/stream.c
===================================================================
--- libroar/stream.c	(revision 5072)
+++ libroar/stream.c	(revision 5145)
@@ -85,6 +85,8 @@
                      unsigned int channels, unsigned int bits, unsigned int codec) {
 
- if ( s == NULL )
-  return -1;
+ if ( s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  s->fh         = -1;
@@ -115,6 +117,8 @@
 
 int roar_stream_set_rel_id(struct roar_stream * s, int id) {
- if ( s == NULL )
-  return -1;
+ if ( s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  s->pos_rel_id = id;
@@ -124,6 +128,8 @@
 
 int roar_stream_get_rel_id(struct roar_stream * s) {
- if ( s == NULL )
-  return -1;
+ if ( s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  return s->pos_rel_id;
@@ -131,6 +137,8 @@
 
 int roar_stream_new_by_id(struct roar_stream * s, int id) {
- if ( s == NULL )
-  return -1;
+ if ( s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  if ( roar_stream_new_empty(s) == -1 )
@@ -141,6 +149,8 @@
 
 int roar_stream_new_empty(struct roar_stream * s) {
- if ( s == NULL )
-  return -1;
+ if ( s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  return roar_stream_new(s, 0, 0, 0, 0);
@@ -148,6 +158,8 @@
 
 int roar_stream_set_id (struct roar_stream * s, int id) {
- if ( s == NULL )
-  return -1;
+ if ( s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  s->id = id;
@@ -157,6 +169,8 @@
 
 int roar_stream_get_id (struct roar_stream * s) {
- if ( s == NULL )
-  return -1;
+ if ( s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  return s->id;
@@ -164,6 +178,8 @@
 
 int roar_stream_set_fh (struct roar_stream * s, int fh) {
- if ( s == NULL )
-  return -1;
+ if ( s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  s->fh = fh;
@@ -173,6 +189,8 @@
 
 int roar_stream_get_fh (struct roar_stream * s) {
- if ( s == NULL )
-  return -1;
+ if ( s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  return s->fh;
@@ -180,6 +198,8 @@
 
 int roar_stream_set_dir (struct roar_stream * s, int dir) {
- if ( s == NULL )
-  return -1;
+ if ( s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  s->dir = dir;
@@ -189,6 +209,8 @@
 
 int roar_stream_get_dir (struct roar_stream * s) {
- if ( s == NULL )
-  return -1;
+ if ( s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  return s->dir;
@@ -232,6 +254,8 @@
  int len = 0;
 
- if ( host == NULL )
-  return -1;
+ if ( host == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  ROAR_DBG("roar_stream_connect_to_ask(*): Ask the server to connect to: %s:%i", host, port);
@@ -249,6 +273,8 @@
  len = strlen(host);
 
- if ( len > 76 )
-  return -1;
+ if ( len > 76 ) {
+  roar_err_set(ROAR_ERROR_NAMETOOLONG);
+  return -1;
+ }
 
  strncpy(&(m.data[4]), host, len);
@@ -265,4 +291,14 @@
  struct roar_message m;
  int confh;
+
+ if ( con == NULL || s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
+
+ if ( fh < 0 ) {
+  roar_err_set(ROAR_ERROR_INVAL);
+  return -1;
+ }
 
  memset(&m,  0, sizeof(m));
@@ -310,4 +346,14 @@
  int i;
 
+ if ( con == NULL || s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
+
+ if ( client < 0 ) {
+  roar_err_set(ROAR_ERROR_INVAL);
+  return -1;
+ }
+
  memset(&m,  0, sizeof(m));
 
@@ -337,4 +383,9 @@
  struct roar_message m;
 
+ if ( con == NULL || s == NULL || data == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
+
  memset(&m,  0, sizeof(m));
 
@@ -361,10 +412,14 @@
  roar_debug_warn_obsolete("roar_stream_send_data", "roar_vio_write", NULL);
 
- if ( s == NULL )
-  return -1;
+ if ( s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  if ( s->fh == -1 ) {
-  if ( con == NULL )
+  if ( con == NULL ) {
+   roar_err_set(ROAR_ERROR_NOTSUP);
    return -1;
+  }
 
   if ( roar_stream_add_data(con, s, data, len) == -1 )
@@ -375,7 +430,8 @@
 
 #ifdef ROAR_HAVE_IO_POSIX
- return write(s->fh, data, len);
+ return ROAR_NETWORK_WRITE(s->fh, data, len);
 #endif
 
+ roar_err_set(ROAR_ERROR_NOTSUP);
  return -1;
 }
@@ -385,4 +441,9 @@
  uint16_t * data = (uint16_t *) m.data;
  int i;
+
+ if ( con == NULL || s == NULL || info == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  memset(&m,  0, sizeof(m));
@@ -410,9 +471,13 @@
  }
 
- if ( m.datalen < 7*2 )
-  return -1;
-
- if ( data[0] != 0 || data[1] != 1 )
-  return -1;
+ if ( m.datalen < 7*2 ) {
+  roar_err_set(ROAR_ERROR_MSGSIZE);
+  return -1;
+ }
+
+ if ( data[0] != 0 || data[1] != 1 ) {
+  roar_err_set(ROAR_ERROR_NSVERSION);
+  return -1;
+ }
 
  memset(info, 0, sizeof(struct roar_stream_info));
@@ -459,6 +524,8 @@
  uint16_t * data = (uint16_t *) m.data;
 
- if ( con == NULL || s == NULL || name == NULL || len == 0 )
-  return -1;
+ if ( con == NULL || s == NULL || name == NULL || len == 0 ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  name[0] = 0; // just in case...
@@ -489,6 +556,8 @@
  ROAR_DBG("roar_stream_get_name(*) = ?");
 
- if ( m.datalen < 4 )
-  return -1;
+ if ( m.datalen < 4 ) {
+  roar_err_set(ROAR_ERROR_MSGSIZE);
+  return -1;
+ }
 
  data[0] = ROAR_NET2HOST16(data[0]);
@@ -497,6 +566,13 @@
  ROAR_DBG("roar_stream_get_name(*) = ?");
 
- if ( data[0] != 0 || data[1] != ROAR_STREAM_PARA_NAME )
-  return -1;
+ if ( data[0] != 0 ) {
+  roar_err_set(ROAR_ERROR_NSVERSION);
+  return -1;
+ }
+
+ if ( data[1] != ROAR_STREAM_PARA_NAME ) {
+  roar_err_set(ROAR_ERROR_TYPEMM);
+  return -1;
+ }
 
  m.datalen -= 4;
@@ -521,9 +597,13 @@
  ROAR_DBG("roar_stream_get_chanmap(con=%p, s=%p, map=%p, len=%p) = ?", con, s, map, len);
 
- if ( con == NULL || s == NULL || map == NULL || len == NULL )
-  return -1;
-
- if ( *len == 0 )
-  return -1;
+ if ( con == NULL || s == NULL || map == NULL || len == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
+
+ if ( *len == 0 ) {
+  roar_err_set(ROAR_ERROR_INVAL);
+  return -1;
+ }
 
  memset(&m, 0, sizeof(m));
@@ -549,6 +629,8 @@
  ROAR_DBG("roar_stream_get_chanmap(con=%p, s=%p{.id=%i}, map=%p, len=%p) = ?", con, s, s->id, map, len);
 
- if ( m.datalen < 4 )
-  return -1;
+ if ( m.datalen < 4 ) {
+  roar_err_set(ROAR_ERROR_MSGSIZE);
+  return -1;
+ }
 
  data[0] = ROAR_NET2HOST16(data[0]);
@@ -557,6 +639,13 @@
  ROAR_DBG("roar_stream_get_chanmap(con=%p, s=%p{.id=%i}, map=%p, len=%p) = ?", con, s, s->id, map, len);
 
- if ( data[0] != 0 || data[1] != ROAR_STREAM_PARA_CHANMAP )
-  return -1;
+ if ( data[0] != 0 ) {
+  roar_err_set(ROAR_ERROR_NSVERSION);
+  return -1;
+ }
+
+ if ( data[1] != ROAR_STREAM_PARA_CHANMAP ) {
+  roar_err_set(ROAR_ERROR_TYPEMM);
+  return -1;
+ }
 
  ROAR_DBG("roar_stream_get_chanmap(con=%p, s=%p{.id=%i}, map=%p, len=%p) = ?", con, s, s->id, map, len);
@@ -564,6 +653,8 @@
  m.datalen -= 4;
 
- if ( m.datalen > *len )
-  return -1;
+ if ( m.datalen > *len ) {
+  roar_err_set(ROAR_ERROR_NOMEM);
+  return -1;
+ }
 
  ROAR_DBG("roar_stream_get_chanmap(con=%p, s=%p{.id=%i}, map=%p, len=%p) = ?", con, s, s->id, map, len);
@@ -581,9 +672,13 @@
  uint16_t * data = (uint16_t *) m.data;
 
- if ( con == NULL || s == NULL || map == NULL )
-  return -1;
-
- if ( len == 0 )
+ if ( con == NULL || s == NULL || map == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
+
+ if ( len == 0 ) {
+  roar_err_set(ROAR_ERROR_INVAL);
   return 0;
+ }
 
  memset(&m, 0, sizeof(m));
@@ -622,4 +717,9 @@
  uint16_t * data = (uint16_t *) m.data;
  int i;
+
+ if ( con == NULL || s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  memset(&m,  0, sizeof(m));
@@ -662,4 +762,9 @@
  int i;
 
+ if ( con == NULL || s == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
+
  memset(&m,  0, sizeof(m));
 
@@ -692,6 +797,8 @@
  int i;
 
- if ( !(s && m) )
-  return -1;
+ if ( s == NULL || m == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
 
  m->datalen = _ROAR_STREAM_MESSAGE_LEN;
@@ -719,9 +826,13 @@
  int i;
 
- if ( !(s && m) )
-  return -1;
-
- if ( m->datalen != _ROAR_STREAM_MESSAGE_LEN )
-  return -1;
+ if ( s == NULL || m == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
+
+ if ( m->datalen != _ROAR_STREAM_MESSAGE_LEN ) {
+  roar_err_set(ROAR_ERROR_MSGSIZE);
+  return -1;
+ }
 
  s->pos = m->pos;
@@ -925,11 +1036,9 @@
   return guess;
 
- if ( codec == NULL || *codec == 0 )
-  return ROAR_CODEC_DEFAULT;
-
  for (i = 0; _libroar_codec[i].codec != -1; i++)
   if ( strcasecmp(_libroar_codec[i].name, codec) == 0 )
    return _libroar_codec[i].codec;
 
+ roar_err_set(ROAR_ERROR_NOENT);
  return -1;
 }
@@ -949,6 +1058,13 @@
  int i;
 
- if ( mime == NULL || *mime == 0 )
-  return -1;
+ if ( mime == NULL ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
+
+ if ( *mime == 0 ) {
+  roar_err_set(ROAR_ERROR_INVAL);
+  return -1;
+ }
 
  for (i = 0; _libroar_codec[i].codec != -1; i++)
@@ -957,4 +1073,5 @@
     return _libroar_codec[i].codec;
 
+ roar_err_set(ROAR_ERROR_NOENT);
  return -1;
 }
@@ -967,4 +1084,5 @@
    return _libroar_codec[i].mime;
 
+ roar_err_set(ROAR_ERROR_NOENT);
  return NULL;
 }
@@ -981,4 +1099,5 @@
 
  if ( ret == 0 && rate[0] != '0' ) {
+  roar_err_set(ROAR_ERROR_NOENT);
   return -1;
  }
@@ -998,4 +1117,5 @@
 
  if ( ret == 0 && bits[0] != '0' ) {
+  roar_err_set(ROAR_ERROR_NOENT);
   return -1;
  }
@@ -1021,4 +1141,5 @@
 
  if ( ret == 0 && channels[0] != '0' ) {
+  roar_err_set(ROAR_ERROR_NOENT);
   return -1;
  }
@@ -1065,8 +1186,11 @@
  int i;
 
+ roar_err_clear();
+
  for (i = 0; _libroar_role[i].name != NULL; i++)
   if ( !strcasecmp(_libroar_role[i].name, role) )
    return _libroar_role[i].role;
 
+ roar_err_set(ROAR_ERROR_NOENT);
  return ROAR_ROLE_UNKNOWN;
 }
