X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcfdstream.c;h=1ae8d413ca730fd7c67880522ea7604052edcd69;hb=929798ea2747657e5834da459d4abb9bb41d71b0;hp=d1fc6b534dd942660b52cd8e7cb46b36d492476b;hpb=c27a4ecc3e616e8a5ee09b8ca888ed6ff3e501f7;p=silc.git diff --git a/lib/silcutil/silcfdstream.c b/lib/silcutil/silcfdstream.c index d1fc6b53..1ae8d413 100644 --- a/lib/silcutil/silcfdstream.c +++ b/lib/silcutil/silcfdstream.c @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 2005 Pekka Riikonen + Copyright (C) 2005 - 2008 Pekka Riikonen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,9 +17,11 @@ */ -#include "silcincludes.h" +#include "silc.h" -#define SILC_IS_FD_STREAM(s) (s->ops == &silc_fd_stream_ops) +/************************** Types and definitions ***************************/ + +#define SILC_IS_FD_STREAM(s) (s && s->ops == &silc_fd_stream_ops) const SilcStreamOps silc_fd_stream_ops; @@ -34,6 +36,9 @@ typedef struct { int error; } *SilcFDStream; + +/************************ Static utility functions **************************/ + /* The IO process callback that calls the notifier callback to upper layer. */ SILC_TASK_CALLBACK(silc_fd_stream_io) @@ -44,38 +49,37 @@ SILC_TASK_CALLBACK(silc_fd_stream_io) return; switch (type) { - case SILC_TASK_WRITE: - stream->notifier(stream, SILC_STREAM_CAN_WRITE, stream->notifier_context); - break; - case SILC_TASK_READ: stream->notifier(stream, SILC_STREAM_CAN_READ, stream->notifier_context); break; + case SILC_TASK_WRITE: + stream->notifier(stream, SILC_STREAM_CAN_WRITE, stream->notifier_context); + break; + default: break; } } + +/****************************** Public API **********************************/ + /* Create file descriptor stream */ -SilcStream silc_fd_stream_create(int fd, SilcSchedule schedule) +SilcStream silc_fd_stream_create(int fd) { if (fd < 1) return NULL; - return silc_fd_stream_create2(fd, 0, schedule); + return silc_fd_stream_create2(fd, 0); } /* Create stream with two file descriptors */ -SilcStream silc_fd_stream_create2(int read_fd, int write_fd, - SilcSchedule schedule) +SilcStream silc_fd_stream_create2(int read_fd, int write_fd) { SilcFDStream stream; - if (read_fd < 1 && write_fd < 1) - return NULL; - stream = silc_calloc(1, sizeof(*stream)); if (!stream) return NULL; @@ -83,21 +87,59 @@ SilcStream silc_fd_stream_create2(int read_fd, int write_fd, SILC_LOG_DEBUG(("Creating new fd stream %p", stream)); 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); + return stream; +} + +/* Create by opening file */ + +SilcStream silc_fd_stream_file(const char *filename, SilcBool reading, + SilcBool writing) +{ + const char *read_file = NULL, *write_file = NULL; + + if (!filename) + return NULL; + + if (writing) + write_file = filename; + if (reading) + read_file = filename; + + return silc_fd_stream_file2(read_file, write_file); +} + +/* Create by opening two files */ + +SilcStream silc_fd_stream_file2(const char *read_file, const char *write_file) +{ + SilcStream stream; + int fd1 = 0, fd2 = 0; + + SILC_LOG_DEBUG(("Creating new fd stream for reading `%s' and writing `%s'", + read_file ? read_file : "(none)", + write_file ? write_file : "(none)")); + + if (write_file) { + fd2 = silc_file_open(write_file, O_CREAT | O_WRONLY); + if (fd2 < 0) { + silc_file_close(fd1); + return NULL; + } + } + + if (read_file) { + fd1 = silc_file_open(read_file, O_RDONLY); + if (fd1 < 0) + return NULL; } - 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); - if (write_fd < 1) - write_fd = read_fd; + + stream = silc_fd_stream_create2(fd1, fd2); + if (!stream) { + silc_file_close(fd1); + silc_file_close(fd2); } return stream; @@ -105,7 +147,8 @@ SilcStream silc_fd_stream_create2(int read_fd, int write_fd, /* Return fds */ -SilcBool silc_fd_stream_get_info(SilcStream stream, int *read_fd, int *write_fd) +SilcBool silc_fd_stream_get_info(SilcStream stream, int *read_fd, + int *write_fd) { SilcFDStream fd_stream = stream; @@ -140,7 +183,7 @@ int silc_fd_stream_read(SilcStream stream, unsigned char *buf, SilcFDStream fd_stream = stream; int len = 0; - if (!SILC_IS_FD_STREAM(fd_stream)) + if (!fd_stream->notifier) return -2; SILC_LOG_DEBUG(("Reading data from fd %d", fd_stream->fd1)); @@ -176,7 +219,7 @@ int silc_fd_stream_write(SilcStream stream, const unsigned char *data, SilcFDStream fd_stream = stream; int ret; - if (!SILC_IS_FD_STREAM(fd_stream)) + if (!fd_stream->notifier) return -2; SILC_LOG_DEBUG(("Writing data to fd %d", fd_stream->fd2)); @@ -212,13 +255,20 @@ SilcBool silc_fd_stream_close(SilcStream stream) { SilcFDStream fd_stream = 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) + if (fd_stream->schedule) { + silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd1); + silc_schedule_task_del_by_fd(fd_stream->schedule, fd_stream->fd1); + } + } + if (fd_stream->fd2 > 0 && fd_stream->fd2 != fd_stream->fd1) { silc_file_close(fd_stream->fd2); + if (fd_stream->schedule) { + silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd2); + silc_schedule_task_del_by_fd(fd_stream->schedule, fd_stream->fd2); + } + } return TRUE; } @@ -227,30 +277,59 @@ SilcBool silc_fd_stream_close(SilcStream stream) void silc_fd_stream_destroy(SilcStream stream) { - SilcFDStream fd_stream = stream; - - if (!SILC_IS_FD_STREAM(fd_stream)) - return; - silc_fd_stream_close(stream); silc_free(stream); } /* Sets stream notification callback for the stream */ -void silc_fd_stream_notifier(SilcStream stream, - SilcStreamNotifier callback, - void *context) +SilcBool silc_fd_stream_notifier(SilcStream stream, + SilcSchedule schedule, + SilcStreamNotifier callback, + void *context) { SilcFDStream fd_stream = stream; - if (!SILC_IS_FD_STREAM(fd_stream)) - return; - SILC_LOG_DEBUG(("Setting stream notifier callback")); fd_stream->notifier = callback; fd_stream->notifier_context = context; + fd_stream->schedule = schedule; + + /* Schedule the file descriptors */ + if (schedule) { + if (fd_stream->fd2 > 0) { + silc_schedule_task_add_fd(schedule, fd_stream->fd2, + silc_fd_stream_io, stream); + silc_file_set_nonblock(fd_stream->fd2); + } + if (fd_stream->fd1 > 0) { + silc_schedule_task_add_fd(schedule, fd_stream->fd1, + silc_fd_stream_io, stream); + silc_schedule_set_listen_fd(schedule, fd_stream->fd1, + SILC_TASK_READ, FALSE); + silc_file_set_nonblock(fd_stream->fd1); + if (fd_stream->fd2 < 1) + fd_stream->fd2 = fd_stream->fd1; + } + } else { + if (fd_stream->schedule) { + silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd1); + silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd2); + silc_schedule_task_del_by_fd(fd_stream->schedule, fd_stream->fd1); + silc_schedule_task_del_by_fd(fd_stream->schedule, fd_stream->fd2); + } + } + + return TRUE; +} + +/* Return schedule */ + +SilcSchedule silc_fd_stream_get_schedule(SilcStream stream) +{ + SilcFDStream fd_stream = stream; + return fd_stream->schedule; } /* File descriptor stream operations */ @@ -261,4 +340,5 @@ const SilcStreamOps silc_fd_stream_ops = silc_fd_stream_close, silc_fd_stream_destroy, silc_fd_stream_notifier, + silc_fd_stream_get_schedule };