From 6b773ff581867c2817c53bca0cff3f92d433c69b Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Wed, 20 Feb 2002 16:52:04 +0000 Subject: [PATCH] updates. --- CHANGES | 7 ++ TODO | 4 + configure.in.pre | 5 ++ includes/silcbeos.h | 27 ++++++ includes/silcincludes.h | 16 ++++ lib/silcutil/Makefile.am | 4 + lib/silcutil/beos/Makefile.am | 31 +++++++ lib/silcutil/beos/silcbeosmutex.c | 73 +++++++++++++++ lib/silcutil/beos/silcbeosnet.c | 24 +++++ lib/silcutil/beos/silcbeosschedule.c | 23 +++++ lib/silcutil/beos/silcbeossockconn.c | 129 +++++++++++++++++++++++++++ lib/silcutil/beos/silcbeosthread.c | 107 ++++++++++++++++++++++ lib/silcutil/beos/silcbeosutil.c | 24 +++++ 13 files changed, 474 insertions(+) create mode 100644 includes/silcbeos.h create mode 100644 lib/silcutil/beos/Makefile.am create mode 100644 lib/silcutil/beos/silcbeosmutex.c create mode 100644 lib/silcutil/beos/silcbeosnet.c create mode 100644 lib/silcutil/beos/silcbeosschedule.c create mode 100644 lib/silcutil/beos/silcbeossockconn.c create mode 100644 lib/silcutil/beos/silcbeosthread.c create mode 100644 lib/silcutil/beos/silcbeosutil.c diff --git a/CHANGES b/CHANGES index 88e5b1a2..b2edc870 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,10 @@ +Wed Feb 20 18:48:49 EET 2002 Pekka Riikonen + + * Added preliminary BeOS support into the util library. + Created lib/silcutil/beos/, and implemented all the needed + functions to support SILC on BeOS. Created also file + includes/silcbeos.h. + Mon Feb 18 15:49:22 EET 2002 Timo Sirainen * Added proper initializations to silc's irssi code, so it's diff --git a/TODO b/TODO index abc1a8ac..21c58b76 100644 --- a/TODO +++ b/TODO @@ -106,6 +106,10 @@ Manual. and naming conventions used in the Toolkit (should not be actually the CodingStyle document, but something more general). + o Write "Platform Implementations" document to describe what platforms + Toolkit support, what has been implemented, what has not been, what + wors differently etc. + TODO in SILC Protocol ===================== diff --git a/configure.in.pre b/configure.in.pre index 8fd771e7..df04c313 100644 --- a/configure.in.pre +++ b/configure.in.pre @@ -615,6 +615,11 @@ AM_CONDITIONAL(SILC_WIN32, test x$win32-support = xtrue) # AM_CONDITIONAL(SILC_EPOC, test xfalse = xtrue) +# +# Native BeOS support (disabled by default) +# +AM_CONDITIONAL(SILC_BEOS, test xfalse = xtrue) + # # IPv6 support # diff --git a/includes/silcbeos.h b/includes/silcbeos.h new file mode 100644 index 00000000..cf024ed1 --- /dev/null +++ b/includes/silcbeos.h @@ -0,0 +1,27 @@ +/* + + silcbeos.h + + Author: Pekka Riikonen + + Copyright (C) 2002 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 + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*/ +/* Native BeOS specific includes and definitions. */ + +#ifndef SILCBEOS_H +#define SILCBEOS_H + +#include +#include + +#endif /* SILCBEOS_H */ diff --git a/includes/silcincludes.h b/includes/silcincludes.h index c7182023..c0c2d6e8 100644 --- a/includes/silcincludes.h +++ b/includes/silcincludes.h @@ -40,6 +40,18 @@ #endif #endif +#ifdef BEOS +#ifndef SILC_BEOS +#define SILC_BEOS +#endif +#elif defined(__BEOS__) +#ifndef SILC_BEOS +#define SILC_BEOS +#endif +#endif + +/* Platform specific includes */ + #ifdef SILC_WIN32 #include "silcwin32.h" #endif @@ -48,6 +60,10 @@ #include "silcepoc.h" #endif +#ifdef SILC_BEOS +#include "silcbeos.h" +#endif + #ifndef DLLAPI #define DLLAPI #endif diff --git a/lib/silcutil/Makefile.am b/lib/silcutil/Makefile.am index ac4e73e0..c4406000 100644 --- a/lib/silcutil/Makefile.am +++ b/lib/silcutil/Makefile.am @@ -23,9 +23,13 @@ else if SILC_EPOC SUBDIRS=epoc else +if SILC_BEOS +SUBDIRS=beos +else SUBDIRS=unix endif endif +endif noinst_LIBRARIES = libsilcutil.a diff --git a/lib/silcutil/beos/Makefile.am b/lib/silcutil/beos/Makefile.am new file mode 100644 index 00000000..5cfe911c --- /dev/null +++ b/lib/silcutil/beos/Makefile.am @@ -0,0 +1,31 @@ +# +# Makefile.am +# +# Author: Pekka Riikonen +# +# Copyright (C) 2002 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 +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# + +AUTOMAKE_OPTIONS = 1.0 no-dependencies foreign + +noinst_LIBRARIES = libsilcbeosutil.a + +libsilcbeosutil_a_SOURCES = \ + silcbeosschedule.c \ + silcbeosnet.c \ + silcbeosutil.c \ + silcbeossockconn.c \ + silcbeosmutex.c \ + silcbeosthread.c + +include $(top_srcdir)/Makefile.defines.in diff --git a/lib/silcutil/beos/silcbeosmutex.c b/lib/silcutil/beos/silcbeosmutex.c new file mode 100644 index 00000000..74544928 --- /dev/null +++ b/lib/silcutil/beos/silcbeosmutex.c @@ -0,0 +1,73 @@ +/* + + silcbeosmutex.c + + Author: Pekka Riikonen + + Copyright (C) 2002 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 + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*/ +/* $Id$ */ + +#include "silcincludes.h" + +#ifdef SILC_THREADS + +/* SILC Mutex structure */ +struct SilcMutexStruct { + int sema_count; + sem_id sema; +}; + +bool silc_mutex_alloc(SilcMutex *mutex) +{ + int ret; + + *mutex = silc_calloc(1, sizeof(**mutex)); + if (*mutex == NULL) + return FALSE; + + ret = create_sem(0, "SILC_MUTEX"); + if (ret < B_NO_ERROR) { + silc_free(*mutex); + return FALSE; + } + + (*mutex)->sema_count = 0; + (*mutex)->sema = ret; + + return TRUE; +} + +void silc_mutex_free(SilcMutex mutex) +{ + delete_sem(mutex->sema); + silc_free(mutex); +} + +void silc_mutex_lock(SilcMutex mutex) +{ + if (atomic_add(&mutex->sema_count, 1) > 0) { + if (acquire_sem(mutex->sema) < B_NO_ERROR) + assert(FALSE); + } +} + +void silc_mutex_unlock(SilcMutex mutex) +{ + if (atomic_add(&mutes->sema_count, -1) > 1) { + if (release_sem(mutex->sema) < B_NO_ERROR) + assert(FALSE); + } +} + +#endif /* SILC_THREADS */ diff --git a/lib/silcutil/beos/silcbeosnet.c b/lib/silcutil/beos/silcbeosnet.c new file mode 100644 index 00000000..616649df --- /dev/null +++ b/lib/silcutil/beos/silcbeosnet.c @@ -0,0 +1,24 @@ +/* + + silcbeosnet.c + + Author: Pekka Riikonen + + Copyright (C) 2002 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 + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*/ +/* $Id$ */ + +#include "silcincludes.h" + +/* Maybe works, or maybe not */ +#include "../unix/silcunixnet.c" diff --git a/lib/silcutil/beos/silcbeosschedule.c b/lib/silcutil/beos/silcbeosschedule.c new file mode 100644 index 00000000..5ff15493 --- /dev/null +++ b/lib/silcutil/beos/silcbeosschedule.c @@ -0,0 +1,23 @@ +/* + + silcbeosschedule.c + + Author: Pekka Riikonen + + Copyright (C) 2002 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 + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*/ +/* $Id$ */ + +/* Maybe works, or maybe not. The wakeup may cause problems, except + on BONE... */ +#include "../unix/silcunixschedule.c" diff --git a/lib/silcutil/beos/silcbeossockconn.c b/lib/silcutil/beos/silcbeossockconn.c new file mode 100644 index 00000000..3b7c057c --- /dev/null +++ b/lib/silcutil/beos/silcbeossockconn.c @@ -0,0 +1,129 @@ +/* + + silcbeossockconn.c + + Author: Pekka Riikonen + + Copyright (C) 2002 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 + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*/ +/* $Id$ */ + +/* On BONE we can use Unix code */ +#ifdef SILC_BEOS_BONE +#include "../unix/silcunixsockconn.c" +#else +#include "silcincludes.h" + +/* Writes data from encrypted buffer to the socket connection. If the + data cannot be written at once, it will be written later with a timeout. + The data is written from the data section of the buffer, not from head + or tail section. This automatically pulls the data section towards end + after writing the data. */ + +int silc_socket_write(SilcSocketConnection sock) +{ + int ret = 0; + SilcBuffer src = sock->outbuf; + + if (SILC_IS_DISABLED(sock)) + return -1; + + SILC_LOG_DEBUG(("Writing data to socket %d", sock->sock)); + + if (src->len > 0) { + ret = send(sock->sock, src->data, src->len, 0); + if (ret == -1) { + if (errno == EWOULDBLOCK) { + SILC_LOG_DEBUG(("Could not write immediately, will do it later")); + return -2; + } + SILC_LOG_ERROR(("Cannot write to socket: %d", sock->sock)); + sock->sock_error = errno; + return -1; + } + + silc_buffer_pull(src, ret); + } + + SILC_LOG_DEBUG(("Wrote data %d bytes", ret)); + + return ret; +} + +/* Reads data from the socket connection into the incoming data buffer. + It reads as much as possible from the socket connection. This returns + amount of bytes read or -1 on error or -2 on case where all of the + data could not be read at once. */ + +int silc_socket_read(SilcSocketConnection sock) +{ + int len = 0; + unsigned char buf[SILC_SOCKET_READ_SIZE]; + + if (SILC_IS_DISABLED(sock)) + return -1; + + SILC_LOG_DEBUG(("Reading data from socket %d", sock->sock)); + + /* Read the data from the socket. */ + len = recv(sock->sock, buf, sizeof(buf), 0); + if (len == -1) { + if (errno == EWOULDBLOCK || errno == EINTR) { + SILC_LOG_DEBUG(("Could not read immediately, will do it later")); + return -2; + } + SILC_LOG_ERROR(("Cannot read from socket: %d", sock->sock)); + sock->sock_error = errno; + return -1; + } + + if (!len) + return 0; + + /* Insert the data to the buffer. */ + + if (!sock->inbuf) + sock->inbuf = silc_buffer_alloc(SILC_SOCKET_BUF_SIZE); + + /* If the data does not fit to the buffer reallocate it */ + if ((sock->inbuf->end - sock->inbuf->tail) < len) + sock->inbuf = silc_buffer_realloc(sock->inbuf, sock->inbuf->truelen + + (len * 2)); + silc_buffer_put_tail(sock->inbuf, buf, len); + silc_buffer_pull_tail(sock->inbuf, len); + + SILC_LOG_DEBUG(("Read %d bytes", len)); + + return len; +} + +/* Returns human readable socket error message */ + +bool silc_socket_get_error(SilcSocketConnection sock, char *error, + uint32 error_len) +{ + char *err; + + if (!sock->sock_error) + return FALSE; + + err = strerror(sock->sock_error); + if (strlen(err) > error_len) + return FALSE; + + memset(error, 0, error_len); + memcpy(error, err, strlen(err)); + return TRUE; +} + +#endif /* SILC_BEOS_BONE */ diff --git a/lib/silcutil/beos/silcbeosthread.c b/lib/silcutil/beos/silcbeosthread.c new file mode 100644 index 00000000..d6e88be3 --- /dev/null +++ b/lib/silcutil/beos/silcbeosthread.c @@ -0,0 +1,107 @@ +/* + + silcbeosthread.c + + Author: Pekka Riikonen + + Copyright (C) 2002 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 + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*/ +/* $Id$ */ + +#include "silcincludes.h" + +#ifdef SILC_THREADS + +/* Thread structure for BeOS */ +typedef struct { + thread_id thread; + SilcThreadStart start_func; + void *context; + bool waitable; +} *SilcBeosThread; + +/* Actual routine that is called by BeOS when the thread is created. + We will call the start_func from here. */ + +static void *silc_thread_beos_start(void *context) +{ + SilcBeosThread thread = (SilcBeosThread)context; + return (*thread->start_func)(thread->context); +} + +#endif + +SilcThread silc_thread_create(SilcThreadStart start_func, void *context, + bool waitable) +{ +#ifdef SILC_THREADS + int ret; + SilcBeosThread thread = silc_calloc(1, sizeof(*thread)); + if (!thread) + return NULL; + + thread->start_func = start_func; + thread->context = context; + thread->waitable = waitable; + + /* Create the thread, and run it */ + thread->thread = spawn_thread((thread_func)silc_thread_beos_start, + B_NORMAL_PRIORITY, thread); + ret = resume_thread(thread->thread); + if (ret < B_NO_ERROR) { + SILC_LOG_ERROR(("Could not create new thread")); + silc_free(thread); + return NULL; + } + + return (SilcThread)thread->thread; +#else + /* Call thread callback immediately */ + (*start_func)(context); + return NULL; +#endif +} + +void silc_thread_exit(void *exit_value) +{ +#ifdef SILC_THREADS + exit_thread((status_t)exit_value); +#endif +} + +SilcThread silc_thread_self(void) +{ +#ifdef SILC_THREADS + return (SilcThread)find_thread(NULL); +#else + return NULL; +#endif +} + +bool silc_thread_wait(SilcThread thread, void **exit_value) +{ +#ifdef SILC_THREADS + status_t ret, retval; + + ret = wait_for_thread((thread_id)thread, &retval); + if (ret == B_NO_ERROR) { + if (exit_value) + *exit_value = retval; + return TRUE; + } + + return FALSE; +#else + return FALSE; +#endif +} diff --git a/lib/silcutil/beos/silcbeosutil.c b/lib/silcutil/beos/silcbeosutil.c new file mode 100644 index 00000000..555839df --- /dev/null +++ b/lib/silcutil/beos/silcbeosutil.c @@ -0,0 +1,24 @@ +/* + + silcbeosutil.c + + Author: Pekka Riikonen + + Copyright (C) 2002 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 + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*/ +/* $Id$ */ + +#include "silcincludes.h" + +/* Maybe works, or maybe not */ +#include "../unix/silcunixutil.c" -- 2.24.0