Added silc_file_set_nonblock. Fixed FD Stream. Comment fixes.
authorPekka Riikonen <priikone@silcnet.org>
Mon, 28 Nov 2005 16:02:46 +0000 (16:02 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Mon, 28 Nov 2005 16:02:46 +0000 (16:02 +0000)
lib/silcutil/silcapputil.c
lib/silcutil/silcfdstream.c
lib/silcutil/silcfdstream.h
lib/silcutil/silcfileutil.h
lib/silcutil/silcsocketstream.h
lib/silcutil/silcstream.h
lib/silcutil/unix/silcunixutil.c
lib/silcutil/win32/silcwin32util.c

index 606f2215539d25395c629cdac6c590eefdfc44df..98ecddaf4501df378b3f53569c6a1c7a8b0a2213 100644 (file)
@@ -197,7 +197,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
   }
 
   /* Generate keys */
-  silc_pkcs_alloc(alg, &pkcs);
+  silc_pkcs_alloc(alg, SILC_PKCS_SILC, &pkcs);
   silc_pkcs_generate_key(pkcs, key_len_bits, rng);
 
   /* Save public key into file */
@@ -290,7 +290,7 @@ SilcBool silc_load_key_pair(const char *pub_filename,
     }
 
   if (return_pkcs) {
-    silc_pkcs_alloc((*return_public_key)->name, return_pkcs);
+    silc_pkcs_alloc((*return_public_key)->name, SILC_PKCS_SILC, return_pkcs);
     silc_pkcs_public_key_set(*return_pkcs, *return_public_key);
     silc_pkcs_private_key_set(*return_pkcs, *return_private_key);
   }
@@ -326,7 +326,7 @@ SilcBool silc_show_public_key(const char *pub_filename)
   fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
   babbleprint = silc_hash_babbleprint(NULL, pk, pk_len);
 
-  if (silc_pkcs_alloc(public_key->name, &pkcs)) {
+  if (silc_pkcs_alloc(public_key->name, SILC_PKCS_SILC, &pkcs)) {
     key_len = silc_pkcs_public_key_set(pkcs, public_key);
     silc_pkcs_free(pkcs);
   }
index d1fc6b534dd942660b52cd8e7cb46b36d492476b..c47aae8e393f0e93ef9f367dbe322c8c541e0fec 100644 (file)
@@ -73,7 +73,7 @@ SilcStream silc_fd_stream_create2(int read_fd, int write_fd,
 {
   SilcFDStream stream;
 
-  if (read_fd < 1 && write_fd < 1)
+  if (read_fd < 1)
     return NULL;
 
   stream = silc_calloc(1, sizeof(*stream));
@@ -85,19 +85,19 @@ SilcStream silc_fd_stream_create2(int read_fd, int write_fd,
   stream->ops = &silc_fd_stream_ops;
   stream->schedule = schedule;
   stream->fd1 = read_fd;
-  stream->fd1 = write_fd;
+  stream->fd2 = write_fd;
 
   /* Schedule the file descriptors */
   if (write_fd > 0) {
     silc_schedule_task_add_fd(schedule, write_fd, silc_fd_stream_io, stream);
-    silc_net_set_socket_nonblock(write_fd);
+    silc_file_set_nonblock(write_fd);
   }
   if (read_fd > 0) {
     silc_schedule_task_add_fd(schedule, read_fd, silc_fd_stream_io, stream);
     silc_schedule_set_listen_fd(schedule, read_fd, SILC_TASK_READ, FALSE);
-    silc_net_set_socket_nonblock(read_fd);
+    silc_file_set_nonblock(read_fd);
     if (write_fd < 1)
-      write_fd = read_fd;
+      stream->fd2 = stream->fd1;
   }
 
   return stream;
@@ -215,10 +215,14 @@ SilcBool silc_fd_stream_close(SilcStream stream)
   if (!SILC_IS_FD_STREAM(fd_stream))
     return FALSE;
 
-  if (fd_stream->fd1 > 0)
+  if (fd_stream->fd1 > 0) {
     silc_file_close(fd_stream->fd1);
-  if (fd_stream->fd2 > 0 && fd_stream->fd2 != fd_stream->fd1)
+    silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd1);
+  }
+  if (fd_stream->fd2 > 0 && fd_stream->fd2 != fd_stream->fd1) {
     silc_file_close(fd_stream->fd2);
+    silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd2);
+  }
 
   return TRUE;
 }
@@ -233,6 +237,8 @@ void silc_fd_stream_destroy(SilcStream stream)
     return;
 
   silc_fd_stream_close(stream);
+  silc_schedule_task_del_by_fd(fd_stream->schedule, fd_stream->fd1);
+  silc_schedule_task_del_by_fd(fd_stream->schedule, fd_stream->fd2);
   silc_free(stream);
 }
 
index 3acb8cecaf78a135f1c50ddb05c781d818b5014f..9904fcac0dafcffe8290a8242084a0340fcb8ce8 100644 (file)
@@ -26,6 +26,9 @@
  * interface should be used only with real file descriptors, not with
  * sockets.  Use the SILC Socket Stream for sockets.
  *
+ * SILC File Descriptor Stream is not thread-safe.  If same stream must be
+ * used in multithreaded environment concurrency control must be employed.
+ *
  ***/
 
 #ifndef SILCFDSTREAM_H
index a1ccd3049953cf54e80f241edf980383893f8ef4..3437870aa36cc30e7bc1fe40d532f869cc46e661 100644 (file)
@@ -100,6 +100,19 @@ int silc_file_write(int fd, const char *buffer, SilcUInt32 len);
  ***/
 int silc_file_close(int fd);
 
+/****f* silcutil/SilcNetAPI/silc_file_set_nonblock
+ *
+ * SYNOPSIS
+ *
+ *    int silc_file_set_nonblock(int fd);
+ *
+ * DESCRIPTION
+ *
+ *    Sets the file descriptor to non-blocking mode.
+ *
+ ***/
+int silc_file_set_nonblock(int fd);
+
 /****f* silcutil/SilcFileUtilAPI/silc_file_readfile
  *
  * SYNOPSIS
index dec1401912fbe6dd614aecba3997bbfd32399e6f..565190966b9eba97198fa2227c4a787aa9a3f141 100644 (file)
@@ -26,6 +26,9 @@
  * Stream provides also Quality of Service (QoS) support that can be used
  * to control the throughput of the stream.
  *
+ * SILC Socket Stream is not thread-safe.  If the same socket stream must be
+ * used in multithreaded environment concurrency control must be employed.
+ *
  ***/
 
 #ifndef SILCSOCKETSTREAM_H
index 31c6d078942933daa27f1286945951ba4e545cc5..e3278ab30ba260870cf2f4f2f83e2342d2be52c3 100644 (file)
  * other stream API derived from this API can use this same interface for
  * sending and receiving.
  *
+ * Note that stream implementations usually are not thread-safe.  Always
+ * verify whether a stream implementation is thread-safe by checking their
+ * corresponding documentation.
+ *
  ***/
 
 #ifndef SILCSTREAM_H
index 300beff0b391571d13d637c87de9ec13d3c46a9b..93a5fdd2b3c0a4906f0bcd5cbfa9bc9ba04b6cbe 100644 (file)
@@ -176,3 +176,9 @@ int silc_gettimeofday(struct timeval *p)
 {
   return gettimeofday(p, NULL);
 }
+
+int silc_file_set_nonblock(int fd)
+{
+  return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
+}
+
index 7bc2d69a98981964d1109786057a0bf657252052..45c45e6cb2f5be1980ca6f44b457857b70ff4b62 100644 (file)
@@ -73,3 +73,8 @@ char *silc_get_real_name(void)
 {
   return silc_get_username();
 }
+
+int silc_file_set_nonblock(int fd)
+{
+  return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
+}