From: Pekka Riikonen Date: Mon, 28 Nov 2005 16:02:46 +0000 (+0000) Subject: Added silc_file_set_nonblock. Fixed FD Stream. Comment fixes. X-Git-Tag: silc.client.1.1.beta1~330 X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=commitdiff_plain;h=7de70a77c609a4b286f6e1f69c6b55f5c19781d1 Added silc_file_set_nonblock. Fixed FD Stream. Comment fixes. --- diff --git a/lib/silcutil/silcapputil.c b/lib/silcutil/silcapputil.c index 606f2215..98ecddaf 100644 --- a/lib/silcutil/silcapputil.c +++ b/lib/silcutil/silcapputil.c @@ -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); } diff --git a/lib/silcutil/silcfdstream.c b/lib/silcutil/silcfdstream.c index d1fc6b53..c47aae8e 100644 --- a/lib/silcutil/silcfdstream.c +++ b/lib/silcutil/silcfdstream.c @@ -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); } diff --git a/lib/silcutil/silcfdstream.h b/lib/silcutil/silcfdstream.h index 3acb8cec..9904fcac 100644 --- a/lib/silcutil/silcfdstream.h +++ b/lib/silcutil/silcfdstream.h @@ -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 diff --git a/lib/silcutil/silcfileutil.h b/lib/silcutil/silcfileutil.h index a1ccd304..3437870a 100644 --- a/lib/silcutil/silcfileutil.h +++ b/lib/silcutil/silcfileutil.h @@ -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 diff --git a/lib/silcutil/silcsocketstream.h b/lib/silcutil/silcsocketstream.h index dec14019..56519096 100644 --- a/lib/silcutil/silcsocketstream.h +++ b/lib/silcutil/silcsocketstream.h @@ -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 diff --git a/lib/silcutil/silcstream.h b/lib/silcutil/silcstream.h index 31c6d078..e3278ab3 100644 --- a/lib/silcutil/silcstream.h +++ b/lib/silcutil/silcstream.h @@ -26,6 +26,10 @@ * 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 diff --git a/lib/silcutil/unix/silcunixutil.c b/lib/silcutil/unix/silcunixutil.c index 300beff0..93a5fdd2 100644 --- a/lib/silcutil/unix/silcunixutil.c +++ b/lib/silcutil/unix/silcunixutil.c @@ -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); +} + diff --git a/lib/silcutil/win32/silcwin32util.c b/lib/silcutil/win32/silcwin32util.c index 7bc2d69a..45c45e6c 100644 --- a/lib/silcutil/win32/silcwin32util.c +++ b/lib/silcutil/win32/silcwin32util.c @@ -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); +}