From: Valeriy Yatsko Date: Tue, 2 Dec 2008 23:49:39 +0000 (+0300) Subject: Copied libratbox and related stuff from shadowircd upstream. X-Git-Url: https://jfr.im/git/irc/rqf/shadowircd.git/commitdiff_plain/94b4fbf93a7653bd9acb52649d6a4a8d5f994d0a Copied libratbox and related stuff from shadowircd upstream. --- diff --git a/libratbox/ChangeLog b/libratbox/ChangeLog index 78a85b8..a3f8288 100644 --- a/libratbox/ChangeLog +++ b/libratbox/ChangeLog @@ -1,3 +1,488 @@ +androsyn 2008/11/11 00:00:12 UTC (20081111_0-26180) + Log: + fix build errors on os x leopard + + Modified: + libratbox/trunk/src/unix.c (File Modified) + + +androsyn 2008/11/07 02:10:19 UTC (20081107_0-26178) + Log: + untested fix for building on os x -- environ is not accessible to shared libraries... + + Modified: + libratbox/trunk/src/unix.c (File Modified) + + +androsyn 2008/10/26 20:59:07 UTC (20081026_2-26170) + Log: + add rb_basename and rb_dirname + + Modified: + libratbox/trunk/include/rb_tools.h (File Modified) + libratbox/trunk/src/export-syms.txt (File Modified) + libratbox/trunk/src/tools.c (File Modified) + + +androsyn 2008/10/26 20:58:40 UTC (20081026_1-26168) + Log: + remove -O0 from configure.ac and configure + + Modified: + libratbox/trunk/configure (File Modified) + libratbox/trunk/configure.ac (File Modified) + + +androsyn 2008/10/26 20:48:56 UTC (20081026_0-26166) + Log: + do an ERR_get_error() if opening the dhparams file fails + + Modified: + libratbox/trunk/src/openssl.c (File Modified) + + +androsyn 2008/10/03 18:02:13 UTC (20081003_0-26126) + Log: + fix up the timerfd autoconf stuff a bit better + + Modified: + libratbox/trunk/acinclude.m4 (File Modified) + libratbox/trunk/configure (File Modified) + + +androsyn 2008/09/29 22:58:42 UTC (20080929_0-26122) + Log: + fix the timerfd_create check + + Modified: + libratbox/trunk/acinclude.m4 (File Modified) + libratbox/trunk/configure (File Modified) + + +androsyn 2008/09/20 01:27:19 UTC (20080920_1-26100) + Log: + properly cast a few things when doing pointer math and the like + + Modified: + libratbox/trunk/src/balloc.c (File Modified) + libratbox/trunk/src/openssl.c (File Modified) + + +androsyn 2008/09/20 01:05:42 UTC (20080920_0-26096) + Log: + make rb_close_all be a no-op on win32, its not too useful there either as windows doesn't inherit sockets/files by default + + Modified: + libratbox/trunk/src/commio.c (File Modified) + + +androsyn 2008/09/19 15:13:52 UTC (20080919_0-26092) + Log: + update indent.pro and rerun through indent + + Modified: + libratbox/trunk/.indent.pro (File Modified) + libratbox/trunk/include/arc4random.h (File Modified) + libratbox/trunk/include/commio-int.h (File Modified) + libratbox/trunk/include/commio-ssl.h (File Modified) + libratbox/trunk/include/rb_balloc.h (File Modified) + libratbox/trunk/include/rb_commio.h (File Modified) + libratbox/trunk/include/rb_event.h (File Modified) + libratbox/trunk/include/rb_helper.h (File Modified) + libratbox/trunk/include/rb_linebuf.h (File Modified) + libratbox/trunk/include/rb_memory.h (File Modified) + libratbox/trunk/include/rb_patricia.h (File Modified) + libratbox/trunk/include/rb_rawbuf.h (File Modified) + libratbox/trunk/include/rb_snprintf.h (File Modified) + libratbox/trunk/include/rb_tools.h (File Modified) + libratbox/trunk/src/arc4random.c (File Modified) + libratbox/trunk/src/balloc.c (File Modified) + libratbox/trunk/src/commio.c (File Modified) + libratbox/trunk/src/crypt.c (File Modified) + libratbox/trunk/src/devpoll.c (File Modified) + libratbox/trunk/src/epoll.c (File Modified) + libratbox/trunk/src/event.c (File Modified) + libratbox/trunk/src/gnutls.c (File Modified) + libratbox/trunk/src/helper.c (File Modified) + libratbox/trunk/src/kqueue.c (File Modified) + libratbox/trunk/src/linebuf.c (File Modified) + libratbox/trunk/src/nossl.c (File Modified) + libratbox/trunk/src/openssl.c (File Modified) + libratbox/trunk/src/patricia.c (File Modified) + libratbox/trunk/src/poll.c (File Modified) + libratbox/trunk/src/ports.c (File Modified) + libratbox/trunk/src/ratbox_lib.c (File Modified) + libratbox/trunk/src/rawbuf.c (File Modified) + libratbox/trunk/src/rb_memory.c (File Modified) + libratbox/trunk/src/select.c (File Modified) + libratbox/trunk/src/sigio.c (File Modified) + libratbox/trunk/src/snprintf.c (File Modified) + libratbox/trunk/src/tools.c (File Modified) + libratbox/trunk/src/unix.c (File Modified) + libratbox/trunk/src/win32.c (File Modified) + + +androsyn 2008/09/18 18:56:55 UTC (20080918_1-26088) + Log: + on win32 add randomness from RtlGenRandom if its available + + Modified: + libratbox/trunk/src/arc4random.c (File Modified) + + +androsyn 2008/09/18 18:41:19 UTC (20080918_0-26086) + Log: + add timerfd_create support on linux with glibc 2.8 + + Modified: + libratbox/trunk/acinclude.m4 (File Modified) + libratbox/trunk/configure (File Modified) + libratbox/trunk/configure.ac (File Modified) + libratbox/trunk/include/libratbox_config.h.in (File Modified) + libratbox/trunk/src/Makefile.in (File Modified) + libratbox/trunk/src/epoll.c (File Modified) + libratbox/trunk/src/version.c.SH (File Modified) + + +androsyn 2008/09/15 15:47:42 UTC (20080915_0-26082) + Log: + cast svptr a bit harder to deal with 32bit systems. Also use sizeof our_signalfd_siginfo. + + Modified: + libratbox/trunk/src/epoll.c (File Modified) + + +jilles 2008/09/14 22:24:27 UTC (20080914_0-26078) + Log: + Fix infinite recursion crash on non-win32. + + + Modified: + libratbox/trunk/src/unix.c (File Modified) + + +androsyn 2008/09/10 01:45:22 UTC (20080910_1-26072) + Log: + fix a typo + + Modified: + libratbox/trunk/src/select.c (File Modified) + + +androsyn 2008/09/10 00:23:41 UTC (20080910_0-26068) + Log: + attempt to override FD_SETSIZE for select and if rb_maxconnections is greater than FD_SETSIZE, lower rb_maxconnections. Also prefer select to win32 + + Modified: + libratbox/trunk/src/commio.c (File Modified) + libratbox/trunk/src/select.c (File Modified) + + +androsyn 2008/09/09 22:58:11 UTC (20080909_8-26066) + Log: + actually make that use BIO_new_file + + Modified: + libratbox/trunk/src/openssl.c (File Modified) + + +androsyn 2008/09/09 22:19:14 UTC (20080909_7-26064) + Log: + avoid using FILE with openssl as this causes openssl to need OPENSSL_applink on some platforms which is annoying + + Modified: + libratbox/trunk/src/openssl.c (File Modified) + + +androsyn 2008/09/09 17:08:10 UTC (20080909_6-26060) + Log: + those symbols don't exist..oops + + Modified: + libratbox/trunk/src/export-syms.txt (File Modified) + + +androsyn 2008/09/09 17:05:19 UTC (20080909_5-26058) + Log: + move that down a little + + Modified: + libratbox/trunk/src/unix.c (File Modified) + + +androsyn 2008/09/09 16:47:03 UTC (20080909_4-26052) + Log: + change ifdef WIN32 to _WIN32 + + Modified: + libratbox/trunk/include/commio-int.h (File Modified) + libratbox/trunk/include/ratbox_lib.h (File Modified) + libratbox/trunk/include/rb_commio.h (File Modified) + libratbox/trunk/src/balloc.c (File Modified) + libratbox/trunk/src/commio.c (File Modified) + libratbox/trunk/src/helper.c (File Modified) + libratbox/trunk/src/openssl.c (File Modified) + libratbox/trunk/src/unix.c (File Modified) + libratbox/trunk/src/win32.c (File Modified) + + +androsyn 2008/09/09 16:40:19 UTC (20080909_3-26048) + Log: + - Implement fd passing for win32 so that ssld will work on there. + - Add a few symbols to the library for portability issues + Note that this commit changes the API for rb_send_fd_buf as it now + also takes a pid option as this is needed on windows. This process + id should be the target process. Note that unix will just ignore + this. + + + + Modified: + libratbox/trunk/Makefile.in (File Modified) + libratbox/trunk/include/ratbox_lib.h (File Modified) + libratbox/trunk/include/rb_commio.h (File Modified) + libratbox/trunk/src/commio.c (File Modified) + libratbox/trunk/src/export-syms.txt (File Modified) + libratbox/trunk/src/helper.c (File Modified) + libratbox/trunk/src/unix.c (File Modified) + libratbox/trunk/src/win32.c (File Modified) + + +androsyn 2008/09/09 16:37:30 UTC (20080909_2-26046) + Log: + pass libraries to LDFLAGS as well + + Modified: + libratbox/trunk/src/Makefile.am (File Modified) + libratbox/trunk/src/Makefile.in (File Modified) + + +androsyn 2008/09/09 16:36:15 UTC (20080909_1-26044) + Log: + make SSL_LIBS actually pass the path to the openssl libraries + + Modified: + libratbox/trunk/configure (File Modified) + libratbox/trunk/configure.ac (File Modified) + + +androsyn 2008/09/09 14:40:41 UTC (20080909_0-26036) + Log: + make that an ifdef + + Modified: + libratbox/trunk/src/commio.c (File Modified) + + +androsyn 2008/09/08 16:54:58 UTC (20080908_1-26034) + Log: + add a datagram socketpair for platforms that don't have it..mostly based on what is in perl for this + + Modified: + libratbox/trunk/src/commio.c (File Modified) + + +androsyn 2008/09/08 16:53:53 UTC (20080908_0-26032) + Log: + make rb_strerror be char instead of const char + + Modified: + libratbox/trunk/include/ratbox_lib.h (File Modified) + + +androsyn 2008/09/05 23:44:06 UTC (20080905_1-26022) + Log: + fix the define of the fake rb_sockaddr_storage so that m4 does not eat up our [ ] + + Modified: + libratbox/trunk/configure (File Modified) + libratbox/trunk/configure.ac (File Modified) + + +androsyn 2008/09/05 18:19:25 UTC (20080905_0-26012) + Log: + revert part of r25994 + + Modified: + libratbox/trunk/configure (File Modified) + libratbox/trunk/configure.ac (File Modified) + + +androsyn 2008/09/04 21:43:03 UTC (20080904_3-26008) + Log: + change hard asserts to call both the soft assert code and the C library assert so that it gets in the ircd log files as well + + Modified: + libratbox/trunk/include/ratbox_lib.h (File Modified) + + +androsyn 2008/09/04 19:32:55 UTC (20080904_2-26000) + Log: + If somebody really wants to do profiling work..they can change rb_close_all the hard way. Also stop doing the weird stderr.log hack in libratbox let callers deal with stderr on their own + + Modified: + libratbox/trunk/src/commio.c (File Modified) + + +androsyn 2008/09/04 16:08:27 UTC (20080904_1-25998) + Log: + add missing rb_strerror prototypes + + Modified: + libratbox/trunk/include/ratbox_lib.h (File Modified) + libratbox/trunk/src/unix.c (File Modified) + libratbox/trunk/src/win32.c (File Modified) + + +androsyn 2008/09/04 15:06:21 UTC (20080904_0-25994) + Log: + some portability cleanups + + Modified: + libratbox/trunk/aclocal.m4 (File Modified) + libratbox/trunk/config.guess (File Modified) + libratbox/trunk/config.sub (File Modified) + libratbox/trunk/configure (File Modified) + libratbox/trunk/configure.ac (File Modified) + libratbox/trunk/include/arc4random.h (File Modified) + libratbox/trunk/include/libratbox_config.h.in (File Modified) + libratbox/trunk/include/ratbox_lib.h (File Modified) + libratbox/trunk/ltmain.sh (File Modified) + libratbox/trunk/src/Makefile.in (File Modified) + libratbox/trunk/src/commio.c (File Modified) + libratbox/trunk/src/export-syms.txt (File Modified) + libratbox/trunk/src/helper.c (File Modified) + libratbox/trunk/src/nossl.c (File Modified) + libratbox/trunk/src/unix.c (File Modified) + libratbox/trunk/src/win32.c (File Modified) + + +androsyn 2008/09/03 00:20:22 UTC (20080903_0-25992) + Log: + change the event stuff to just strndup the event name + + Modified: + libratbox/trunk/include/event-int.h (File Modified) + libratbox/trunk/src/event.c (File Modified) + + +androsyn 2008/09/02 21:40:49 UTC (20080902_0-25990) + Log: + fix makefile on bsd make + + Modified: + libratbox/trunk/src/Makefile.am (File Modified) + libratbox/trunk/src/Makefile.in (File Modified) + + +androsyn 2008/09/01 05:00:31 UTC (20080901_3-25984) + Log: + make extern not nested + + Modified: + libratbox/trunk/src/ratbox_lib.c (File Modified) + + +androsyn 2008/09/01 04:59:19 UTC (20080901_2-25982) + Log: + and add version.c.SH too + + Modified: + libratbox/trunk/src/version.c.SH (File Added) + + +androsyn 2008/09/01 04:58:51 UTC (20080901_1-25980) + Log: + fix up version output stuff + + Modified: + libratbox/trunk/src/Makefile.am (File Modified) + libratbox/trunk/src/Makefile.in (File Modified) + libratbox/trunk/src/ratbox_lib.c (File Modified) + + +androsyn 2008/09/01 04:50:17 UTC (20080901_0-25978) + Log: + test commit + + Modified: + libratbox/trunk/README (File Modified) + + +androsyn 2008/09/01 03:18:34 UTC (25975) + Log: + remove NOBALLOC code from linebuf.c and fill in the rb_bh_usage stuff for NOBALLOC + + Modified: + libratbox/trunk/src/balloc.c (File Modified) + libratbox/trunk/src/linebuf.c (File Modified) + + +androsyn 2008/09/01 03:12:32 UTC (25973) + Log: + remove an #if 0 + + Modified: + libratbox/trunk/src/event.c (File Modified) + + +androsyn 2008/09/01 03:07:46 UTC (25971) + Log: + change u_char to uint8_t + + Modified: + libratbox/trunk/src/crypt.c (File Modified) + + +androsyn 2008/08/27 19:28:26 UTC (25963) + Log: + deal with rb_vsnprintf_append having a string passed to it that is longer than len, in such case we truncate the string.. + + Modified: + libratbox/trunk/src/snprintf.c (File Modified) + + +androsyn 2008/08/27 14:59:09 UTC (25961) + Log: + the way ev->name was assigned was pretty bogus and pretty much only worked because we always used strings that always were statically allocated in the executable. fix this and do it correctly + + Modified: + libratbox/trunk/include/event-int.h (File Modified) + libratbox/trunk/src/event.c (File Modified) + + +androsyn 2008/08/25 19:19:35 UTC (25955) + Log: + make that a %s instead of a %d + + Modified: + libratbox/trunk/src/commio.c (File Modified) + + +androsyn 2008/08/15 16:29:35 UTC (25897) + Log: + order of operations in C can be a real bitch sometimes + + Modified: + libratbox/trunk/src/balloc.c (File Modified) + + +androsyn 2008/08/14 18:23:23 UTC (25885) + Log: + fix block heap garbage collection properly + + Modified: + libratbox/trunk/src/balloc.c (File Modified) + + +androsyn 2008/08/12 15:43:58 UTC (25871) + Log: + fix a crash in the rb_bh_gc code + + Modified: + libratbox/trunk/src/balloc.c (File Modified) + + androsyn 2008/08/06 19:51:44 UTC (25861) Log: put back in the pad to even pointer boundaries stuff diff --git a/libratbox/Makefile.in b/libratbox/Makefile.in index d4fc067..9a82ffb 100644 --- a/libratbox/Makefile.in +++ b/libratbox/Makefile.in @@ -123,7 +123,6 @@ INSTALL_DATA = @INSTALL_DATA@ INSTALL_PROGRAM = @INSTALL_PROGRAM@ INSTALL_SCRIPT = @INSTALL_SCRIPT@ INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ -LD = @LD@ LDFLAGS = @LDFLAGS@ LIBOBJS = @LIBOBJS@ LIBS = @LIBS@ @@ -205,7 +204,6 @@ psdir = @psdir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ -subdirs = @subdirs@ sysconfdir = @sysconfdir@ target_alias = @target_alias@ top_builddir = @top_builddir@ diff --git a/libratbox/README b/libratbox/README index f9ee561..1028021 100644 --- a/libratbox/README +++ b/libratbox/README @@ -7,5 +7,4 @@ This is libircd from ircd-ratbox. A few notes about this library: discard. For some non-irc purposes, this can be a problem, but for ircd stuff its fine. 3. The helper code when transmitting data between helpers, the same 512 byte - limit applies there as we recycle the linebuf code for this. - + limit applies there as we recycle the linebuf code for this. diff --git a/libratbox/acinclude.m4 b/libratbox/acinclude.m4 index 7e963d5..4a0c6e1 100644 --- a/libratbox/acinclude.m4 +++ b/libratbox/acinclude.m4 @@ -111,3 +111,42 @@ case $rb__cv_timer_create_works in esac ]) + + +AC_DEFUN([RB_CHECK_TIMERFD_CREATE], + [AC_CACHE_CHECK([for a working timerfd_create(CLOCK_REALTIME)], + [rb__cv_timerfd_create_works], + [AC_TRY_RUN([ +#ifdef HAVE_TIME_H +#include +#endif +#ifdef HAVE_SIGNAL_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_SYS_TIMERFD_H +#include +#endif +int main(int argc, char *argv[]) +{ +#if defined(HAVE_TIMERFD_CREATE) && defined(HAVE_SYS_TIMERFD_H) + if (timerfd_create(CLOCK_REALTIME, 0) < 0) { + return 1; + } +#else + return 1; +#endif + return 0; +} + ], + [rb__cv_timerfd_create_works=yes], + [rb__cv_timerfd_create_works=no]) + ]) +case $rb__cv_timerfd_create_works in + yes) AC_DEFINE([USE_TIMERFD_CREATE], 1, + [Define to 1 if we can use timerfd_create(CLOCK_REALTIME,...)]);; +esac +]) + diff --git a/libratbox/config.guess b/libratbox/config.guess index 278f9e9..f32079a 100755 --- a/libratbox/config.guess +++ b/libratbox/config.guess @@ -1,10 +1,10 @@ #! /bin/sh # Attempt to guess a canonical system name. # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, -# 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, -# Inc. +# 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 +# Free Software Foundation, Inc. -timestamp='2007-07-22' +timestamp='2008-01-23' # This file is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by @@ -56,8 +56,8 @@ version="\ GNU config.guess ($timestamp) Originally written by Per Bothner. -Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 -Free Software Foundation, Inc. +Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, +2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." @@ -532,7 +532,7 @@ EOF echo rs6000-ibm-aix3.2 fi exit ;; - *:AIX:*:[45]) + *:AIX:*:[456]) IBM_CPU_ID=`/usr/sbin/lsdev -C -c processor -S available | sed 1q | awk '{ print $1 }'` if /usr/sbin/lsattr -El ${IBM_CPU_ID} | grep ' POWER' >/dev/null 2>&1; then IBM_ARCH=rs6000 @@ -799,6 +799,9 @@ EOF EM64T | authenticamd) echo x86_64-unknown-interix${UNAME_RELEASE} exit ;; + IA64) + echo ia64-unknown-interix${UNAME_RELEASE} + exit ;; esac ;; [345]86:Windows_95:* | [345]86:Windows_98:* | [345]86:Windows_NT:*) echo i${UNAME_MACHINE}-pc-mks @@ -833,7 +836,14 @@ EOF echo ${UNAME_MACHINE}-pc-minix exit ;; arm*:Linux:*:*) - echo ${UNAME_MACHINE}-unknown-linux-gnu + eval $set_cc_for_build + if echo __ARM_EABI__ | $CC_FOR_BUILD -E - 2>/dev/null \ + | grep -q __ARM_EABI__ + then + echo ${UNAME_MACHINE}-unknown-linux-gnu + else + echo ${UNAME_MACHINE}-unknown-linux-gnueabi + fi exit ;; avr32*:Linux:*:*) echo ${UNAME_MACHINE}-unknown-linux-gnu @@ -954,8 +964,8 @@ EOF x86_64:Linux:*:*) echo x86_64-unknown-linux-gnu exit ;; - xtensa:Linux:*:*) - echo xtensa-unknown-linux-gnu + xtensa*:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-gnu exit ;; i*86:Linux:*:*) # The BFD linker knows what the default object file format is, so @@ -1474,9 +1484,9 @@ This script, last modified $timestamp, has failed to recognize the operating system you are using. It is advised that you download the most up to date version of the config scripts from - http://savannah.gnu.org/cgi-bin/viewcvs/*checkout*/config/config/config.guess + http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD and - http://savannah.gnu.org/cgi-bin/viewcvs/*checkout*/config/config/config.sub + http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD If the version you run ($0) is already up to date, please send the following data and any information you think might be diff --git a/libratbox/config.sub b/libratbox/config.sub index 1761d8b..6759825 100755 --- a/libratbox/config.sub +++ b/libratbox/config.sub @@ -1,10 +1,10 @@ #! /bin/sh # Configuration validation subroutine script. # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, -# 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, -# Inc. +# 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 +# Free Software Foundation, Inc. -timestamp='2007-06-28' +timestamp='2008-01-16' # This file is (in principle) common to ALL GNU software. # The presence of a machine in this file suggests that SOME GNU software @@ -72,8 +72,8 @@ Report bugs and patches to ." version="\ GNU config.sub ($timestamp) -Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 -Free Software Foundation, Inc. +Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, +2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." @@ -369,10 +369,14 @@ case $basic_machine in | v850-* | v850e-* | vax-* \ | we32k-* \ | x86-* | x86_64-* | xc16x-* | xps100-* | xscale-* | xscalee[bl]-* \ - | xstormy16-* | xtensa-* \ + | xstormy16-* | xtensa*-* \ | ymp-* \ | z8k-*) ;; + # Recognize the basic CPU types without company name, with glob match. + xtensa*) + basic_machine=$basic_machine-unknown + ;; # Recognize the various machine names and aliases which stand # for a CPU type and a company and sometimes even an OS. 386bsd) @@ -443,6 +447,14 @@ case $basic_machine in basic_machine=ns32k-sequent os=-dynix ;; + blackfin) + basic_machine=bfin-unknown + os=-linux + ;; + blackfin-*) + basic_machine=bfin-`echo $basic_machine | sed 's/^[^-]*-//'` + os=-linux + ;; c90) basic_machine=c90-cray os=-unicos @@ -668,6 +680,14 @@ case $basic_machine in basic_machine=m68k-isi os=-sysv ;; + m68knommu) + basic_machine=m68k-unknown + os=-linux + ;; + m68knommu-*) + basic_machine=m68k-`echo $basic_machine | sed 's/^[^-]*-//'` + os=-linux + ;; m88k-omron*) basic_machine=m88k-omron ;; @@ -813,6 +833,14 @@ case $basic_machine in basic_machine=i860-intel os=-osf ;; + parisc) + basic_machine=hppa-unknown + os=-linux + ;; + parisc-*) + basic_machine=hppa-`echo $basic_machine | sed 's/^[^-]*-//'` + os=-linux + ;; pbd) basic_machine=sparc-tti ;; @@ -1021,6 +1049,10 @@ case $basic_machine in basic_machine=tic6x-unknown os=-coff ;; + tile*) + basic_machine=tile-unknown + os=-linux-gnu + ;; tx39) basic_machine=mipstx39-unknown ;; diff --git a/libratbox/configure b/libratbox/configure index c0875e3..0098abe 100755 --- a/libratbox/configure +++ b/libratbox/configure @@ -2,7 +2,7 @@ # Guess values for system-dependent variables and create Makefiles. # Generated by GNU Autoconf 2.61 for libratbox devel. # -# $Id: configure.ac 25689 2008-07-10 17:13:27Z androsyn $ +# $Id: configure.ac 26168 2008-10-26 20:58:40Z androsyn $ # # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, # 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. @@ -855,9 +855,6 @@ RM CP MV LN -AR -LD -RANLIB TOUCH build build_cpu @@ -869,6 +866,8 @@ host_vendor host_os LN_S ECHO +AR +RANLIB DSYMUTIL NMEDIT CPP @@ -883,7 +882,6 @@ F77 FFLAGS ac_ct_F77 LIBTOOL -subdirs MINGW_TRUE MINGW_FALSE NEED_CRYPT_TRUE @@ -919,7 +917,7 @@ FFLAGS PKG_CONFIG GNUTLS_CFLAGS GNUTLS_LIBS' -ac_subdirs_all='libltdl' + # Initialize some variables set by options. ac_init_help= @@ -1616,7 +1614,7 @@ Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, This configure script is free software; the Free Software Foundation gives unlimited permission to copy, distribute and modify it. -$Id: configure.ac 25689 2008-07-10 17:13:27Z androsyn $ +$Id: configure.ac 26168 2008-10-26 20:58:40Z androsyn $ _ACEOF exit fi @@ -4853,126 +4851,6 @@ echo "${ECHO_T}no" >&6; } fi -# Extract the first word of "ar", so it can be a program name with args. -set dummy ar; ac_word=$2 -{ echo "$as_me:$LINENO: checking for $ac_word" >&5 -echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6; } -if test "${ac_cv_path_AR+set}" = set; then - echo $ECHO_N "(cached) $ECHO_C" >&6 -else - case $AR in - [\\/]* | ?:[\\/]*) - ac_cv_path_AR="$AR" # Let the user override the test with a path. - ;; - *) - as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then - ac_cv_path_AR="$as_dir/$ac_word$ac_exec_ext" - echo "$as_me:$LINENO: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done -done -IFS=$as_save_IFS - - ;; -esac -fi -AR=$ac_cv_path_AR -if test -n "$AR"; then - { echo "$as_me:$LINENO: result: $AR" >&5 -echo "${ECHO_T}$AR" >&6; } -else - { echo "$as_me:$LINENO: result: no" >&5 -echo "${ECHO_T}no" >&6; } -fi - - -# Extract the first word of "ld", so it can be a program name with args. -set dummy ld; ac_word=$2 -{ echo "$as_me:$LINENO: checking for $ac_word" >&5 -echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6; } -if test "${ac_cv_path_LD+set}" = set; then - echo $ECHO_N "(cached) $ECHO_C" >&6 -else - case $LD in - [\\/]* | ?:[\\/]*) - ac_cv_path_LD="$LD" # Let the user override the test with a path. - ;; - *) - as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then - ac_cv_path_LD="$as_dir/$ac_word$ac_exec_ext" - echo "$as_me:$LINENO: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done -done -IFS=$as_save_IFS - - ;; -esac -fi -LD=$ac_cv_path_LD -if test -n "$LD"; then - { echo "$as_me:$LINENO: result: $LD" >&5 -echo "${ECHO_T}$LD" >&6; } -else - { echo "$as_me:$LINENO: result: no" >&5 -echo "${ECHO_T}no" >&6; } -fi - - -# Extract the first word of "ranlib", so it can be a program name with args. -set dummy ranlib; ac_word=$2 -{ echo "$as_me:$LINENO: checking for $ac_word" >&5 -echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6; } -if test "${ac_cv_path_RANLIB+set}" = set; then - echo $ECHO_N "(cached) $ECHO_C" >&6 -else - case $RANLIB in - [\\/]* | ?:[\\/]*) - ac_cv_path_RANLIB="$RANLIB" # Let the user override the test with a path. - ;; - *) - as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then - ac_cv_path_RANLIB="$as_dir/$ac_word$ac_exec_ext" - echo "$as_me:$LINENO: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done -done -IFS=$as_save_IFS - - ;; -esac -fi -RANLIB=$ac_cv_path_RANLIB -if test -n "$RANLIB"; then - { echo "$as_me:$LINENO: result: $RANLIB" >&5 -echo "${ECHO_T}$RANLIB" >&6; } -else - { echo "$as_me:$LINENO: result: no" >&5 -echo "${ECHO_T}no" >&6; } -fi - - # Extract the first word of "touch", so it can be a program name with args. set dummy touch; ac_word=$2 { echo "$as_me:$LINENO: checking for $ac_word" >&5 @@ -5617,7 +5495,7 @@ ia64-*-hpux*) ;; *-*-irix6*) # Find out which ABI we are using. - echo '#line 5620 "configure"' > conftest.$ac_ext + echo '#line 5498 "configure"' > conftest.$ac_ext if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5 (eval $ac_compile) 2>&5 ac_status=$? @@ -8669,11 +8547,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:8672: $lt_compile\"" >&5) + (eval echo "\"\$as_me:8550: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:8676: \$? = $ac_status" >&5 + echo "$as_me:8554: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -8959,11 +8837,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:8962: $lt_compile\"" >&5) + (eval echo "\"\$as_me:8840: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:8966: \$? = $ac_status" >&5 + echo "$as_me:8844: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -9063,11 +8941,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:9066: $lt_compile\"" >&5) + (eval echo "\"\$as_me:8944: $lt_compile\"" >&5) (eval "$lt_compile" 2>out/conftest.err) ac_status=$? cat out/conftest.err >&5 - echo "$as_me:9070: \$? = $ac_status" >&5 + echo "$as_me:8948: \$? = $ac_status" >&5 if (exit $ac_status) && test -s out/conftest2.$ac_objext then # The compiler can only warn and ignore the option if not recognized @@ -11440,7 +11318,7 @@ else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext < conftest.$ac_ext <&5) + (eval echo "\"\$as_me:13822: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:13948: \$? = $ac_status" >&5 + echo "$as_me:13826: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -14045,11 +13923,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:14048: $lt_compile\"" >&5) + (eval echo "\"\$as_me:13926: $lt_compile\"" >&5) (eval "$lt_compile" 2>out/conftest.err) ac_status=$? cat out/conftest.err >&5 - echo "$as_me:14052: \$? = $ac_status" >&5 + echo "$as_me:13930: \$? = $ac_status" >&5 if (exit $ac_status) && test -s out/conftest2.$ac_objext then # The compiler can only warn and ignore the option if not recognized @@ -15643,11 +15521,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:15646: $lt_compile\"" >&5) + (eval echo "\"\$as_me:15524: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:15650: \$? = $ac_status" >&5 + echo "$as_me:15528: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -15747,11 +15625,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:15750: $lt_compile\"" >&5) + (eval echo "\"\$as_me:15628: $lt_compile\"" >&5) (eval "$lt_compile" 2>out/conftest.err) ac_status=$? cat out/conftest.err >&5 - echo "$as_me:15754: \$? = $ac_status" >&5 + echo "$as_me:15632: \$? = $ac_status" >&5 if (exit $ac_status) && test -s out/conftest2.$ac_objext then # The compiler can only warn and ignore the option if not recognized @@ -17967,11 +17845,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:17970: $lt_compile\"" >&5) + (eval echo "\"\$as_me:17848: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:17974: \$? = $ac_status" >&5 + echo "$as_me:17852: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -18257,11 +18135,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:18260: $lt_compile\"" >&5) + (eval echo "\"\$as_me:18138: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:18264: \$? = $ac_status" >&5 + echo "$as_me:18142: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -18361,11 +18239,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:18364: $lt_compile\"" >&5) + (eval echo "\"\$as_me:18242: $lt_compile\"" >&5) (eval "$lt_compile" 2>out/conftest.err) ac_status=$? cat out/conftest.err >&5 - echo "$as_me:18368: \$? = $ac_status" >&5 + echo "$as_me:18246: \$? = $ac_status" >&5 if (exit $ac_status) && test -s out/conftest2.$ac_objext then # The compiler can only warn and ignore the option if not recognized @@ -21054,10 +20932,6 @@ LIBTOOL='$(SHELL) $(top_builddir)/libtool' LIBTOOL="$LIBTOOL --silent" -subdirs="$subdirs libltdl" - - - case "$host_os" in *cygwin*) @@ -21350,6 +21224,16 @@ echo "$as_me: error: ** MinGW and no winsock2.h. I give up." >&2;} fi + +cat >>confdefs.h <<_ACEOF +#define HAVE_WINSOCK2_H 1 +_ACEOF + + +cat >>confdefs.h <<_ACEOF +#define HAVE_WINSOCK_H 1 +_ACEOF + LIBS="$LIBS -lws2_32 -liphlpapi" is_mingw="yes" ;; @@ -21374,7 +21258,7 @@ fi # backwards support for IRC_CFLAGS -CFLAGS="$IRC_CFLAGS $CFLAGS -O0 -Wall" +CFLAGS="$IRC_CFLAGS $CFLAGS -Wall" if test "$libexecdir" = '${exec_prefix}/libexec' && test "$localstatedir" = '${prefix}/var'; then @@ -21805,7 +21689,8 @@ fi -for ac_header in crypt.h unistd.h sys/socket.h sys/stat.h sys/time.h time.h netinet/in.h arpa/inet.h errno.h sys/uio.h spawn.h sys/poll.h sys/epoll.h sys/select.h sys/devpoll.h sys/event.h port.h signal.h sys/signalfd.h + +for ac_header in crypt.h unistd.h sys/socket.h sys/stat.h sys/time.h time.h netinet/in.h arpa/inet.h errno.h sys/uio.h spawn.h sys/poll.h sys/epoll.h sys/select.h sys/devpoll.h sys/event.h port.h signal.h sys/signalfd.h sys/timerfd.h do as_ac_Header=`echo "ac_cv_header_$ac_header" | $as_tr_sh` if { as_var=$as_ac_Header; eval "test \"\${$as_var+set}\" = set"; }; then @@ -22293,9 +22178,15 @@ cat confdefs.h >>conftest.$ac_ext cat >>conftest.$ac_ext <<_ACEOF /* end confdefs.h. */ +#ifdef HAVE_SYS_TYPES_H #include +#endif +#ifdef HAVE_SYS_SOCKET_H #include - +#endif +#ifdef HAVE_WINSOCK2_H +#include +#endif int getpeername (int, $arg2 *, $t *); int @@ -22342,9 +22233,9 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext done if test "x$rb_socklen_t" = x; then - { { echo "$as_me:$LINENO: error: Cannot find a type to use in place of socklen_t" >&5 -echo "$as_me: error: Cannot find a type to use in place of socklen_t" >&2;} - { (exit 1); exit 1; }; } + { echo "$as_me:$LINENO: WARNING: Cannot find a type to use in place of socklen_t using int" >&5 +echo "$as_me: WARNING: Cannot find a type to use in place of socklen_t using int" >&2;} + rb_socklen_t=int fi fi @@ -22936,7 +22827,8 @@ fi -for ac_func in socketpair gettimeofday writev sendmsg gmtime_r strtok_r usleep posix_spawn strlcpy strlcat strnlen fstat signalfd select poll kevent port_create epoll_ctl arc4random getrusage + +for ac_func in socketpair gettimeofday writev sendmsg gmtime_r strtok_r usleep posix_spawn strlcpy strlcat strnlen fstat signalfd select poll kevent port_create epoll_ctl arc4random getrusage timerfd_create do as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` { echo "$as_me:$LINENO: checking for $ac_func" >&5 @@ -23294,6 +23186,95 @@ _ACEOF ;; esac +{ echo "$as_me:$LINENO: checking for a working timerfd_create(CLOCK_REALTIME)" >&5 +echo $ECHO_N "checking for a working timerfd_create(CLOCK_REALTIME)... $ECHO_C" >&6; } +if test "${rb__cv_timerfd_create_works+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + if test "$cross_compiling" = yes; then + { { echo "$as_me:$LINENO: error: cannot run test program while cross compiling +See \`config.log' for more details." >&5 +echo "$as_me: error: cannot run test program while cross compiling +See \`config.log' for more details." >&2;} + { (exit 1); exit 1; }; } +else + cat >conftest.$ac_ext <<_ACEOF +/* confdefs.h. */ +_ACEOF +cat confdefs.h >>conftest.$ac_ext +cat >>conftest.$ac_ext <<_ACEOF +/* end confdefs.h. */ + +#ifdef HAVE_TIME_H +#include +#endif +#ifdef HAVE_SIGNAL_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_SYS_TIMERFD_H +#include +#endif +int main(int argc, char *argv[]) +{ +#if defined(HAVE_TIMERFD_CREATE) && defined(HAVE_SYS_TIMERFD_H) + if (timerfd_create(CLOCK_REALTIME, 0) < 0) { + return 1; + } +#else + return 1; +#endif + return 0; +} + +_ACEOF +rm -f conftest$ac_exeext +if { (ac_try="$ac_link" +case "(($ac_try" in + *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; + *) ac_try_echo=$ac_try;; +esac +eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5 + (eval "$ac_link") 2>&5 + ac_status=$? + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); } && { ac_try='./conftest$ac_exeext' + { (case "(($ac_try" in + *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; + *) ac_try_echo=$ac_try;; +esac +eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5 + (eval "$ac_try") 2>&5 + ac_status=$? + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); }; }; then + rb__cv_timerfd_create_works=yes +else + echo "$as_me: program exited with status $ac_status" >&5 +echo "$as_me: failed program was:" >&5 +sed 's/^/| /' conftest.$ac_ext >&5 + +( exit $ac_status ) +rb__cv_timerfd_create_works=no +fi +rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext conftest.$ac_objext conftest.$ac_ext +fi + + + +fi +{ echo "$as_me:$LINENO: result: $rb__cv_timerfd_create_works" >&5 +echo "${ECHO_T}$rb__cv_timerfd_create_works" >&6; } +case $rb__cv_timerfd_create_works in + yes) +cat >>confdefs.h <<\_ACEOF +#define USE_TIMERFD_CREATE 1 +_ACEOF +;; +esac + # The Ultrix 4.2 mips builtin alloca declared by alloca.h only works # for constant arguments. Useless! @@ -24783,7 +24764,6 @@ fi fi - if test "$cf_enable_openssl" != no; then CPPFLAGS="$CPPFLAGS $SSL_LIBS" { echo "$as_me:$LINENO: checking for SSL_read in -lssl" >&5 @@ -24848,7 +24828,7 @@ fi { echo "$as_me:$LINENO: result: $ac_cv_lib_ssl_SSL_read" >&5 echo "${ECHO_T}$ac_cv_lib_ssl_SSL_read" >&6; } if test $ac_cv_lib_ssl_SSL_read = yes; then - SSL_LIBS="-lssl -lcrypto" + SSL_LIBS="$SSL_LIBS -lssl -lcrypto" else cf_enable_openssl=no fi @@ -26054,9 +26034,6 @@ RM!$RM$ac_delim CP!$CP$ac_delim MV!$MV$ac_delim LN!$LN$ac_delim -AR!$AR$ac_delim -LD!$LD$ac_delim -RANLIB!$RANLIB$ac_delim TOUCH!$TOUCH$ac_delim build!$build$ac_delim build_cpu!$build_cpu$ac_delim @@ -26065,6 +26042,9 @@ build_os!$build_os$ac_delim host!$host$ac_delim host_cpu!$host_cpu$ac_delim host_vendor!$host_vendor$ac_delim +host_os!$host_os$ac_delim +LN_S!$LN_S$ac_delim +ECHO!$ECHO$ac_delim _ACEOF if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 97; then @@ -26106,9 +26086,8 @@ _ACEOF ac_delim='%!_!# ' for ac_last_try in false false false false false :; do cat >conf$$subs.sed <<_ACEOF -host_os!$host_os$ac_delim -LN_S!$LN_S$ac_delim -ECHO!$ECHO$ac_delim +AR!$AR$ac_delim +RANLIB!$RANLIB$ac_delim DSYMUTIL!$DSYMUTIL$ac_delim NMEDIT!$NMEDIT$ac_delim CPP!$CPP$ac_delim @@ -26123,7 +26102,6 @@ F77!$F77$ac_delim FFLAGS!$FFLAGS$ac_delim ac_ct_F77!$ac_ct_F77$ac_delim LIBTOOL!$LIBTOOL$ac_delim -subdirs!$subdirs$ac_delim MINGW_TRUE!$MINGW_TRUE$ac_delim MINGW_FALSE!$MINGW_FALSE$ac_delim NEED_CRYPT_TRUE!$NEED_CRYPT_TRUE$ac_delim @@ -26142,7 +26120,7 @@ LIBOBJS!$LIBOBJS$ac_delim LTLIBOBJS!$LTLIBOBJS$ac_delim _ACEOF - if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 34; then + if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 32; then break elif $ac_last_try; then { { echo "$as_me:$LINENO: error: could not make $CONFIG_STATUS" >&5 @@ -26802,7 +26780,7 @@ fi if test "x$rb_sockaddr_storage" = "xyes"; then echo '#define rb_sockaddr_storage sockaddr_storage' >> $outfile else - echo 'struct rb_sockaddr_storage { uint8_t _padding128; };' >> $outfile + echo 'struct rb_sockaddr_storage { uint8_t _padding[128]; };' >> $outfile fi cat >> $outfile <<\_______EOF @@ -26850,186 +26828,6 @@ if test "$no_create" != yes; then $ac_cs_success || { (exit 1); exit 1; } fi -# -# CONFIG_SUBDIRS section. -# -if test "$no_recursion" != yes; then - - # Remove --cache-file and --srcdir arguments so they do not pile up. - ac_sub_configure_args= - ac_prev= - eval "set x $ac_configure_args" - shift - for ac_arg - do - if test -n "$ac_prev"; then - ac_prev= - continue - fi - case $ac_arg in - -cache-file | --cache-file | --cache-fil | --cache-fi \ - | --cache-f | --cache- | --cache | --cach | --cac | --ca | --c) - ac_prev=cache_file ;; - -cache-file=* | --cache-file=* | --cache-fil=* | --cache-fi=* \ - | --cache-f=* | --cache-=* | --cache=* | --cach=* | --cac=* | --ca=* \ - | --c=*) - ;; - --config-cache | -C) - ;; - -srcdir | --srcdir | --srcdi | --srcd | --src | --sr) - ac_prev=srcdir ;; - -srcdir=* | --srcdir=* | --srcdi=* | --srcd=* | --src=* | --sr=*) - ;; - -prefix | --prefix | --prefi | --pref | --pre | --pr | --p) - ac_prev=prefix ;; - -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*) - ;; - *) - case $ac_arg in - *\'*) ac_arg=`echo "$ac_arg" | sed "s/'/'\\\\\\\\''/g"` ;; - esac - ac_sub_configure_args="$ac_sub_configure_args '$ac_arg'" ;; - esac - done - - # Always prepend --prefix to ensure using the same prefix - # in subdir configurations. - ac_arg="--prefix=$prefix" - case $ac_arg in - *\'*) ac_arg=`echo "$ac_arg" | sed "s/'/'\\\\\\\\''/g"` ;; - esac - ac_sub_configure_args="'$ac_arg' $ac_sub_configure_args" - - # Pass --silent - if test "$silent" = yes; then - ac_sub_configure_args="--silent $ac_sub_configure_args" - fi - - ac_popdir=`pwd` - for ac_dir in : $subdirs; do test "x$ac_dir" = x: && continue - - # Do not complain, so a configure script can configure whichever - # parts of a large source tree are present. - test -d "$srcdir/$ac_dir" || continue - - ac_msg="=== configuring in $ac_dir (`pwd`/$ac_dir)" - echo "$as_me:$LINENO: $ac_msg" >&5 - echo "$ac_msg" >&6 - { as_dir="$ac_dir" - case $as_dir in #( - -*) as_dir=./$as_dir;; - esac - test -d "$as_dir" || { $as_mkdir_p && mkdir -p "$as_dir"; } || { - as_dirs= - while :; do - case $as_dir in #( - *\'*) as_qdir=`echo "$as_dir" | sed "s/'/'\\\\\\\\''/g"`;; #( - *) as_qdir=$as_dir;; - esac - as_dirs="'$as_qdir' $as_dirs" - as_dir=`$as_dirname -- "$as_dir" || -$as_expr X"$as_dir" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ - X"$as_dir" : 'X\(//\)[^/]' \| \ - X"$as_dir" : 'X\(//\)$' \| \ - X"$as_dir" : 'X\(/\)' \| . 2>/dev/null || -echo X"$as_dir" | - sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ - s//\1/ - q - } - /^X\(\/\/\)[^/].*/{ - s//\1/ - q - } - /^X\(\/\/\)$/{ - s//\1/ - q - } - /^X\(\/\).*/{ - s//\1/ - q - } - s/.*/./; q'` - test -d "$as_dir" && break - done - test -z "$as_dirs" || eval "mkdir $as_dirs" - } || test -d "$as_dir" || { { echo "$as_me:$LINENO: error: cannot create directory $as_dir" >&5 -echo "$as_me: error: cannot create directory $as_dir" >&2;} - { (exit 1); exit 1; }; }; } - ac_builddir=. - -case "$ac_dir" in -.) ac_dir_suffix= ac_top_builddir_sub=. ac_top_build_prefix= ;; -*) - ac_dir_suffix=/`echo "$ac_dir" | sed 's,^\.[\\/],,'` - # A ".." for each directory in $ac_dir_suffix. - ac_top_builddir_sub=`echo "$ac_dir_suffix" | sed 's,/[^\\/]*,/..,g;s,/,,'` - case $ac_top_builddir_sub in - "") ac_top_builddir_sub=. ac_top_build_prefix= ;; - *) ac_top_build_prefix=$ac_top_builddir_sub/ ;; - esac ;; -esac -ac_abs_top_builddir=$ac_pwd -ac_abs_builddir=$ac_pwd$ac_dir_suffix -# for backward compatibility: -ac_top_builddir=$ac_top_build_prefix - -case $srcdir in - .) # We are building in place. - ac_srcdir=. - ac_top_srcdir=$ac_top_builddir_sub - ac_abs_top_srcdir=$ac_pwd ;; - [\\/]* | ?:[\\/]* ) # Absolute name. - ac_srcdir=$srcdir$ac_dir_suffix; - ac_top_srcdir=$srcdir - ac_abs_top_srcdir=$srcdir ;; - *) # Relative name. - ac_srcdir=$ac_top_build_prefix$srcdir$ac_dir_suffix - ac_top_srcdir=$ac_top_build_prefix$srcdir - ac_abs_top_srcdir=$ac_pwd/$srcdir ;; -esac -ac_abs_srcdir=$ac_abs_top_srcdir$ac_dir_suffix - - - cd "$ac_dir" - - # Check for guested configure; otherwise get Cygnus style configure. - if test -f "$ac_srcdir/configure.gnu"; then - ac_sub_configure=$ac_srcdir/configure.gnu - elif test -f "$ac_srcdir/configure"; then - ac_sub_configure=$ac_srcdir/configure - elif test -f "$ac_srcdir/configure.in"; then - # This should be Cygnus configure. - ac_sub_configure=$ac_aux_dir/configure - else - { echo "$as_me:$LINENO: WARNING: no configuration information is in $ac_dir" >&5 -echo "$as_me: WARNING: no configuration information is in $ac_dir" >&2;} - ac_sub_configure= - fi - - # The recursion is here. - if test -n "$ac_sub_configure"; then - # Make the cache file name correct relative to the subdirectory. - case $cache_file in - [\\/]* | ?:[\\/]* ) ac_sub_cache_file=$cache_file ;; - *) # Relative name. - ac_sub_cache_file=$ac_top_build_prefix$cache_file ;; - esac - - { echo "$as_me:$LINENO: running $SHELL $ac_sub_configure $ac_sub_configure_args --cache-file=$ac_sub_cache_file --srcdir=$ac_srcdir" >&5 -echo "$as_me: running $SHELL $ac_sub_configure $ac_sub_configure_args --cache-file=$ac_sub_cache_file --srcdir=$ac_srcdir" >&6;} - # The eval makes quoting arguments work. - eval "\$SHELL \"\$ac_sub_configure\" $ac_sub_configure_args \ - --cache-file=\"\$ac_sub_cache_file\" --srcdir=\"\$ac_srcdir\"" || - { { echo "$as_me:$LINENO: error: $ac_sub_configure failed for $ac_dir" >&5 -echo "$as_me: error: $ac_sub_configure failed for $ac_dir" >&2;} - { (exit 1); exit 1; }; } - fi - - cd "$ac_popdir" - done -fi - echo diff --git a/libratbox/configure.ac b/libratbox/configure.ac index b51ecb1..1414397 100644 --- a/libratbox/configure.ac +++ b/libratbox/configure.ac @@ -1,14 +1,14 @@ -dnl $Id: configure.ac 25689 2008-07-10 17:13:27Z androsyn $ +dnl $Id: configure.ac 26168 2008-10-26 20:58:40Z androsyn $ dnl Process this file with autoconf to produce a configure script. dnl TODO: clean up all the OpenSSL and shared module checking stuff; dnl the most major changes have already been made and it looks like dnl said functions need to be just about as complex as they already are. -AC_PREREQ(2.60) +AC_PREREQ(2.61) AUTOMAKE_OPTIONS = 1.10 dnl Sneaky way to get an Id tag into the configure script -AC_COPYRIGHT([$Id: configure.ac 25689 2008-07-10 17:13:27Z androsyn $]) +AC_COPYRIGHT([$Id: configure.ac 26168 2008-10-26 20:58:40Z androsyn $]) AC_INIT([libratbox],[devel]) AM_INIT_AUTOMAKE(AC_PACKAGE_TARNAME, AC_PACKAGE_VERSION, -) @@ -50,9 +50,6 @@ AC_PATH_PROG(RM, rm) AC_PATH_PROG(CP, cp) AC_PATH_PROG(MV, mv) AC_PATH_PROG(LN, ln) -AC_PATH_PROG(AR, ar) -AC_PATH_PROG(LD, ld) -AC_PATH_PROG(RANLIB, ranlib) AC_PATH_PROG(TOUCH, touch) AC_LANG(C) @@ -60,9 +57,6 @@ AC_PROG_LIBTOOL LIBTOOL="$LIBTOOL --silent" -AC_CONFIG_SUBDIRS(libltdl) - - case "$host_os" in *cygwin*) AC_DEFINE_UNQUOTED(CYGWIN,1,[This is a Cygwin system]) @@ -73,6 +67,8 @@ case "$host_os" in AC_DEFINE_UNQUOTED(WINDOWS,1,[This is a Windows system]) AC_CHECK_HEADER(windows.h, , [AC_MSG_ERROR([** MinGW and no windows.h. I give up.])]) AC_CHECK_HEADER(winsock2.h, , [AC_MSG_ERROR([** MinGW and no winsock2.h. I give up.])]) + AC_DEFINE_UNQUOTED(HAVE_WINSOCK2_H, 1, [Have WINSOCK2_H]) + AC_DEFINE_UNQUOTED(HAVE_WINSOCK_H, 1, [Have WINSOCK_H]) LIBS="$LIBS -lws2_32 -liphlpapi" is_mingw="yes" ;; @@ -90,7 +86,7 @@ AM_CONDITIONAL([MINGW], [test "$is_mingw" = "yes"]) # backwards support for IRC_CFLAGS -CFLAGS="$IRC_CFLAGS $CFLAGS -O0 -Wall" +CFLAGS="$IRC_CFLAGS $CFLAGS -Wall" dnl use directory structure of cached as default (hack) if test "$libexecdir" = '${exec_prefix}/libexec' && @@ -108,7 +104,7 @@ AC_TYPE_UID_T dnl Checks for header files. AC_HEADER_STDC -AC_CHECK_HEADERS([crypt.h unistd.h sys/socket.h sys/stat.h sys/time.h time.h netinet/in.h arpa/inet.h errno.h sys/uio.h spawn.h sys/poll.h sys/epoll.h sys/select.h sys/devpoll.h sys/event.h port.h signal.h sys/signalfd.h]) +AC_CHECK_HEADERS([crypt.h unistd.h sys/socket.h sys/stat.h sys/time.h time.h netinet/in.h arpa/inet.h errno.h sys/uio.h spawn.h sys/poll.h sys/epoll.h sys/select.h sys/devpoll.h sys/event.h port.h signal.h sys/signalfd.h sys/timerfd.h]) AC_HEADER_TIME dnl Networking Functions @@ -140,9 +136,15 @@ AC_CHECK_TYPE([socklen_t], rb_socklen_t=socklen_t,[ for arg2 in "struct sockaddr" void; do for t in int size_t unsigned long "unsigned long"; do AC_TRY_COMPILE([ +#ifdef HAVE_SYS_TYPES_H #include +#endif +#ifdef HAVE_SYS_SOCKET_H #include - +#endif +#ifdef HAVE_WINSOCK2_H +#include +#endif int getpeername (int, $arg2 *, $t *); ],[ $t len; @@ -155,7 +157,8 @@ getpeername(0,0,&len); done if test "x$rb_socklen_t" = x; then - AC_MSG_ERROR([Cannot find a type to use in place of socklen_t]) + AC_MSG_WARN([Cannot find a type to use in place of socklen_t using int]) + rb_socklen_t=int fi ]) AC_MSG_RESULT($rb_socklen_t)], @@ -224,11 +227,12 @@ AC_CHECK_TYPE([sa_family_t], [], dnl check for various functions... -AC_CHECK_FUNCS([socketpair gettimeofday writev sendmsg gmtime_r strtok_r usleep posix_spawn strlcpy strlcat strnlen fstat signalfd select poll kevent port_create epoll_ctl arc4random getrusage]) +AC_CHECK_FUNCS([socketpair gettimeofday writev sendmsg gmtime_r strtok_r usleep posix_spawn strlcpy strlcat strnlen fstat signalfd select poll kevent port_create epoll_ctl arc4random getrusage timerfd_create]) AC_SEARCH_LIBS(nanosleep, rt posix4, AC_DEFINE(HAVE_NANOSLEEP, 1, [Define if you have nanosleep])) AC_SEARCH_LIBS(timer_create, rt, AC_DEFINE(HAVE_TIMER_CREATE, 1, [Define if you have timer_create])) RB_CHECK_TIMER_CREATE +RB_CHECK_TIMERFD_CREATE AC_FUNC_ALLOCA AC_FUNC_VFORK @@ -343,11 +347,10 @@ if test "$cf_enable_openssl" != no; then [cf_enable_openssl=no]) fi - if test "$cf_enable_openssl" != no; then CPPFLAGS="$CPPFLAGS $SSL_LIBS" AC_CHECK_LIB(ssl, SSL_read, - [SSL_LIBS="-lssl -lcrypto"], + [SSL_LIBS="$SSL_LIBS -lssl -lcrypto"], [cf_enable_openssl=no], [-lcrypto]) fi @@ -557,7 +560,7 @@ fi if test "x$rb_sockaddr_storage" = "xyes"; then echo '#define rb_sockaddr_storage sockaddr_storage' >> $outfile else - echo 'struct rb_sockaddr_storage { uint8_t _padding[128]; };' >> $outfile + echo 'struct rb_sockaddr_storage { uint8_t _padding[[128]]; };' >> $outfile fi cat >> $outfile <<\_______EOF @@ -656,7 +659,7 @@ fi AC_CONFIG_FILES( \ src/Makefile \ Makefile \ - libratbox.pc \ + libratbox.pc \ ) AC_OUTPUT diff --git a/libratbox/include/arc4random.h b/libratbox/include/arc4random.h index 49e0a96..6bdd701 100644 --- a/libratbox/include/arc4random.h +++ b/libratbox/include/arc4random.h @@ -4,4 +4,4 @@ void arc4random_stir(void); uint32_t arc4random(void); void arc4random_addrandom(uint8_t *dat, int datlen); -#endif \ No newline at end of file +#endif diff --git a/libratbox/include/commio-int.h b/libratbox/include/commio-int.h index d13d9ea..da281da 100644 --- a/libratbox/include/commio-int.h +++ b/libratbox/include/commio-int.h @@ -32,7 +32,7 @@ #define FD_DESC_SZ 128 /* hostlen + comment */ -#ifdef WIN32 +#ifdef _WIN32 #define rb_get_errno() do { errno = WSAGetLastError(); WSASetLastError(errno); } while(0) #else #define rb_get_errno() @@ -41,31 +41,31 @@ #define rb_hash_fd(x) ((x ^ (x >> RB_FD_HASH_BITS) ^ (x >> (RB_FD_HASH_BITS * 2))) & RB_FD_HASH_MASK) #ifdef HAVE_WRITEV - #ifndef UIO_MAXIOV - # if defined(__FreeBSD__) || defined(__APPLE__) || defined(__NetBSD__) +#ifndef UIO_MAXIOV +# if defined(__FreeBSD__) || defined(__APPLE__) || defined(__NetBSD__) /* FreeBSD 4.7 defines it in sys/uio.h only if _KERNEL is specified */ - # define RB_UIO_MAXIOV 1024 - # elif defined(__sgi) +# define RB_UIO_MAXIOV 1024 +# elif defined(__sgi) /* IRIX 6.5 has sysconf(_SC_IOV_MAX) which might return 512 or bigger */ - # define RB_UIO_MAXIOV 512 - # elif defined(__sun) +# define RB_UIO_MAXIOV 512 +# elif defined(__sun) /* Solaris (and SunOS?) defines IOV_MAX instead */ - # ifndef IOV_MAX - # define RB_UIO_MAXIOV 16 - # else - # define RB_UIO_MAXIOV IOV_MAX - # endif - - # elif defined(IOV_MAX) - # define RB_UIO_MAXIOV IOV_MAX - # else - # define RB_UIO_MAXIOV 16 - # endif - #else - #define RB_UIO_MAXIOV UIO_MAXIOV - #endif +# ifndef IOV_MAX +# define RB_UIO_MAXIOV 16 +# else +# define RB_UIO_MAXIOV IOV_MAX +# endif + +# elif defined(IOV_MAX) +# define RB_UIO_MAXIOV IOV_MAX +# else +# define RB_UIO_MAXIOV 16 +# endif #else - #define RB_UIO_MAXIOV 16 +#define RB_UIO_MAXIOV UIO_MAXIOV +#endif +#else +#define RB_UIO_MAXIOV 16 #endif struct conndata { @@ -83,7 +83,7 @@ struct acceptdata struct rb_sockaddr_storage S; rb_socklen_t addrlen; ACCB *callback; - ACPRE *precb; + ACPRE *precb; void *data; }; @@ -119,12 +119,13 @@ struct _fde unsigned long ssl_errno; }; -typedef void (*comm_event_cb_t)(void *); +typedef void (*comm_event_cb_t) (void *); #ifdef USE_TIMER_CREATE -typedef struct timer_data { - timer_t td_timer_id; - comm_event_cb_t td_cb; +typedef struct timer_data +{ + timer_t td_timer_id; + comm_event_cb_t td_cb; void *td_udata; int td_repeat; } *comm_event_id; @@ -137,7 +138,7 @@ rb_find_fd(int fd) { rb_dlink_list *hlist; rb_dlink_node *ptr; - + if(rb_unlikely(fd < 0)) return NULL; @@ -151,7 +152,7 @@ rb_find_fd(int fd) rb_fde_t *F = ptr->data; if(F->fd == fd) return F; - } + } return NULL; } @@ -161,7 +162,7 @@ void rb_connect_callback(rb_fde_t *F, int status); int rb_io_sched_event(struct ev_entry *ev, int when); -void rb_io_unsched_event(struct ev_entry *ev); +void rb_io_unsched_event(struct ev_entry *ev); int rb_io_supports_event(void); void rb_io_init_event(void); @@ -175,7 +176,7 @@ void rb_epoll_init_event(void); int rb_epoll_sched_event(struct ev_entry *event, int when); void rb_epoll_unsched_event(struct ev_entry *event); int rb_epoll_supports_event(void); - + /* poll versions */ void rb_setselect_poll(rb_fde_t *F, unsigned int type, PF * handler, void *client_data); @@ -230,4 +231,3 @@ void rb_setselect_win32(rb_fde_t *F, unsigned int type, PF * handler, void *clie int rb_init_netio_win32(void); int rb_select_win32(long); int rb_setup_fd_win32(rb_fde_t *F); - diff --git a/libratbox/include/commio-ssl.h b/libratbox/include/commio-ssl.h index 5392106..107f2f0 100644 --- a/libratbox/include/commio-ssl.h +++ b/libratbox/include/commio-ssl.h @@ -6,12 +6,11 @@ int rb_init_prng(const char *path, prng_seed_t seed_type); int rb_get_random(void *buf, size_t length); const char *rb_get_ssl_strerror(rb_fde_t *F); -void rb_ssl_start_accepted(rb_fde_t *new_F, ACCB *cb, void *data, int timeout); -void rb_ssl_start_connected(rb_fde_t *F, CNCB *callback, void *data, int timeout); -void rb_connect_tcp_ssl(rb_fde_t *F, struct sockaddr *dest, struct sockaddr *clocal, int socklen, CNCB *callback, void *data, int timeout); +void rb_ssl_start_accepted(rb_fde_t *new_F, ACCB * cb, void *data, int timeout); +void rb_ssl_start_connected(rb_fde_t *F, CNCB * callback, void *data, int timeout); +void rb_connect_tcp_ssl(rb_fde_t *F, struct sockaddr *dest, struct sockaddr *clocal, int socklen, + CNCB * callback, void *data, int timeout); void rb_ssl_accept_setup(rb_fde_t *F, rb_fde_t *new_F, struct sockaddr *st, int addrlen); void rb_ssl_shutdown(rb_fde_t *F); ssize_t rb_ssl_read(rb_fde_t *F, void *buf, size_t count); ssize_t rb_ssl_write(rb_fde_t *F, const void *buf, size_t count); - - diff --git a/libratbox/include/event-int.h b/libratbox/include/event-int.h index 66e402b..bd91de2 100644 --- a/libratbox/include/event-int.h +++ b/libratbox/include/event-int.h @@ -20,16 +20,15 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: event-int.h 24840 2008-01-03 19:42:17Z androsyn $ + * $Id: event-int.h 25992 2008-09-03 00:20:22Z androsyn $ */ - struct ev_entry { rb_dlink_node node; EVH *func; void *arg; - const char *name; + char *name; time_t frequency; time_t when; void *data; diff --git a/libratbox/include/libratbox_config.h.in b/libratbox/include/libratbox_config.h.in index 3f4fd7f..fddbab7 100644 --- a/libratbox/include/libratbox_config.h.in +++ b/libratbox/include/libratbox_config.h.in @@ -165,6 +165,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_SYS_STAT_H +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_TIMERFD_H + /* Define to 1 if you have the header file. */ #undef HAVE_SYS_TIME_H @@ -174,6 +177,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_SYS_UIO_H +/* Define to 1 if you have the `timerfd_create' function. */ +#undef HAVE_TIMERFD_CREATE + /* Define if you have timer_create */ #undef HAVE_TIMER_CREATE @@ -195,6 +201,12 @@ /* Define to 1 if you are on windows */ #undef HAVE_WIN32 +/* Have WINSOCK2_H */ +#undef HAVE_WINSOCK2_H + +/* Have WINSOCK_H */ +#undef HAVE_WINSOCK_H + /* Define to 1 if `fork' works. */ #undef HAVE_WORKING_FORK @@ -257,6 +269,9 @@ /* Define to 1 if you can safely include both and . */ #undef TIME_WITH_SYS_TIME +/* Define to 1 if we can use timerfd_create(CLOCK_REALTIME,...) */ +#undef USE_TIMERFD_CREATE + /* Define to 1 if we can use timer_create(CLOCK_REALTIME,...) */ #undef USE_TIMER_CREATE diff --git a/libratbox/include/libratbox_config.h_vms b/libratbox/include/libratbox_config.h_vms old mode 100755 new mode 100644 diff --git a/libratbox/include/ratbox_lib.h b/libratbox/include/ratbox_lib.h index 21ebc0d..3241abb 100644 --- a/libratbox/include/ratbox_lib.h +++ b/libratbox/include/ratbox_lib.h @@ -1,5 +1,5 @@ /* - * $Id: ratbox_lib.h 25375 2008-05-16 15:19:51Z androsyn $ + * $Id: ratbox_lib.h 26052 2008-09-09 16:47:03Z androsyn $ */ #ifndef RB_LIB_H @@ -68,20 +68,19 @@ char *alloca(); -#ifdef WIN32 +#ifdef _WIN32 #include #ifndef MAXPATHLEN #define MAXPATHLEN 128 #endif - #ifdef strerror #undef strerror #endif -#define strerror(x) wsock_strerror(x) -const char *wsock_strerror(int error); +#define strerror(x) rb_strerror(x) +char *rb_strerror(int error); #define ENOBUFS WSAENOBUFS @@ -96,18 +95,13 @@ const char *wsock_strerror(int error); #define pipe(x) _pipe(x, 1024, O_BINARY) #define ioctl(x,y,z) ioctlsocket(x,y, (u_long *)z) -int setenv(const char *, const char *, int); -int kill(int pid, int sig); #define WNOHANG 1 -pid_t waitpid(pid_t pid, int *status, int options); -pid_t getpid(void); -unsigned int geteuid(void); #ifndef SIGKILL #define SIGKILL SIGTERM #endif -#endif /* WIN32 */ +#endif /* _WIN32 */ @@ -115,9 +109,8 @@ unsigned int geteuid(void); #define HOSTIPLEN 53 #endif -#ifdef SOFT_ASSERT #ifdef __GNUC__ -#define lrb_assert(expr) do \ +#define slrb_assert(expr) do \ if(rb_unlikely(!(expr))) { \ rb_lib_log( \ "file: %s line: %d (%s): Assertion failed: (%s)", \ @@ -125,7 +118,7 @@ unsigned int geteuid(void); } \ while(0) #else -#define lrb_assert(expr) do \ +#define slrb_assert(expr) do \ if(rb_unlikely(!(expr))) { \ rb_lib_log(L_MAIN, \ "file: %s line: %d: Assertion failed: (%s)", \ @@ -133,16 +126,19 @@ unsigned int geteuid(void); } \ while(0) #endif + +#ifdef SOFT_ASSERT +#define lrb_assert(expr) slrb_assert(expr) #else -#define lrb_assert(expr) assert(expr) +#define lrb_assert(expr) do { slrb_assert(expr); assert(expr); } while(0) #endif #ifdef RB_SOCKADDR_HAS_SA_LEN #define ss_len sa_len #endif -#define GET_SS_FAMILY(x) (((struct sockaddr *)(x))->sa_family) - +#define GET_SS_FAMILY(x) (((const struct sockaddr *)(x))->sa_family) +#define SET_SS_FAMILY(x, y) ((((struct sockaddr *)(x))->sa_family) = y) #ifdef RB_SOCKADDR_HAS_SA_LEN #define SET_SS_LEN(x, y) do { \ struct sockaddr *storage; \ @@ -201,8 +197,15 @@ char *rb_crypt(const char *, const char *); unsigned char *rb_base64_encode(const unsigned char *str, int length); unsigned char *rb_base64_decode(const unsigned char *str, int length, int *ret); +int rb_kill(pid_t, int); +char *rb_strerror(int); +int rb_setenv(const char *, const char *, int); +int rb_kill(int pid, int sig); +pid_t rb_waitpid(pid_t pid, int *status, int options); +pid_t rb_getpid(void); +//unsigned int rb_geteuid(void); #include #include diff --git a/libratbox/include/rb_balloc.h b/libratbox/include/rb_balloc.h index aea1f79..6966fbf 100644 --- a/libratbox/include/rb_balloc.h +++ b/libratbox/include/rb_balloc.h @@ -21,7 +21,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: rb_balloc.h 24324 2007-08-31 22:05:45Z androsyn $ + * $Id: rb_balloc.h 26092 2008-09-19 15:13:52Z androsyn $ */ #ifndef RB_LIB_H @@ -34,17 +34,18 @@ struct rb_bh; typedef struct rb_bh rb_bh; -typedef void rb_bh_usage_cb(size_t bused, size_t bfree, size_t bmemusage, size_t heapalloc, const char *desc, void *data); +typedef void rb_bh_usage_cb (size_t bused, size_t bfree, size_t bmemusage, size_t heapalloc, + const char *desc, void *data); int rb_bh_free(rb_bh *, void *); void *rb_bh_alloc(rb_bh *); rb_bh *rb_bh_create(size_t elemsize, int elemsperblock, const char *desc); -int rb_bh_destroy(rb_bh * bh); +int rb_bh_destroy(rb_bh *bh); int rb_bh_gc(rb_bh *bh); void rb_init_bh(void); -void rb_bh_usage(rb_bh * bh, size_t * bused, size_t * bfree, size_t * bmemusage, const char **desc); +void rb_bh_usage(rb_bh *bh, size_t *bused, size_t *bfree, size_t *bmemusage, const char **desc); void rb_bh_usage_all(rb_bh_usage_cb *cb, void *data); void rb_bh_total_usage(size_t *total_alloc, size_t *total_used); diff --git a/libratbox/include/rb_commio.h b/libratbox/include/rb_commio.h index 9549aa5..64aa889 100644 --- a/libratbox/include/rb_commio.h +++ b/libratbox/include/rb_commio.h @@ -21,18 +21,18 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: rb_commio.h 25693 2008-07-10 18:27:23Z androsyn $ + * $Id: rb_commio.h 26092 2008-09-19 15:13:52Z androsyn $ */ - + #ifndef RB_LIB_H -# error "Do not use commio.h directly" +# error "Do not use commio.h directly" #endif #ifndef INCLUDED_commio_h #define INCLUDED_commio_h -struct sockaddr; +struct sockaddr; struct _fde; typedef struct _fde rb_fde_t; @@ -64,7 +64,7 @@ enum #define RB_FD_NONE 0x01 #define RB_FD_FILE 0x02 #define RB_FD_SOCKET 0x04 -#ifndef WIN32 +#ifndef _WIN32 #define RB_FD_PIPE 0x08 #else #define RB_FD_PIPE RB_FD_SOCKET @@ -88,7 +88,7 @@ struct rb_iovec void rb_fdlist_init(int closeall, int maxfds, size_t heapsize); -rb_fde_t * rb_open(int, uint8_t, const char *); +rb_fde_t *rb_open(int, uint8_t, const char *); void rb_close(rb_fde_t *); void rb_dump_fd(DUMPCB *, void *xdata); void rb_note(rb_fde_t *, const char *); @@ -107,17 +107,16 @@ int rb_get_sockerr(rb_fde_t *); void rb_settimeout(rb_fde_t *, time_t, PF *, void *); void rb_checktimeouts(void *); -void rb_connect_tcp(rb_fde_t *, struct sockaddr *, - struct sockaddr *, int, CNCB *, void *, int); -void rb_connect_tcp_ssl(rb_fde_t *, struct sockaddr *, - struct sockaddr *, int, CNCB *, void *, int); +void rb_connect_tcp(rb_fde_t *, struct sockaddr *, struct sockaddr *, int, CNCB *, void *, int); +void rb_connect_tcp_ssl(rb_fde_t *, struct sockaddr *, struct sockaddr *, int, CNCB *, void *, int); int rb_connect_sockaddr(rb_fde_t *, struct sockaddr *addr, int len); const char *rb_errstr(int status); rb_fde_t *rb_socket(int family, int sock_type, int proto, const char *note); -int rb_socketpair(int family, int sock_type, int proto, rb_fde_t **F1, rb_fde_t **F2, const char *note); +int rb_socketpair(int family, int sock_type, int proto, rb_fde_t **F1, rb_fde_t **F2, + const char *note); -void rb_accept_tcp(rb_fde_t *, ACPRE *precb, ACCB *callback, void *data); +void rb_accept_tcp(rb_fde_t *, ACPRE * precb, ACCB * callback, void *data); ssize_t rb_write(rb_fde_t *, const void *buf, int count); ssize_t rb_writev(rb_fde_t *, struct rb_iovec *vector, int count); @@ -145,7 +144,7 @@ const char *rb_get_ssl_strerror(rb_fde_t *F); rb_fde_t *rb_get_fde(int fd); -int rb_send_fd_buf(rb_fde_t *xF, rb_fde_t **F, int count, void *data, size_t datasize); +int rb_send_fd_buf(rb_fde_t *xF, rb_fde_t **F, int count, void *data, size_t datasize, pid_t pid); int rb_recv_fd_buf(rb_fde_t *F, void *data, size_t datasize, rb_fde_t **xF, int count); void rb_set_type(rb_fde_t *F, uint8_t type); @@ -153,10 +152,11 @@ uint8_t rb_get_type(rb_fde_t *F); const char *rb_get_iotype(void); -typedef enum { +typedef enum +{ RB_PRNG_EGD, RB_PRNG_FILE, -#ifdef WIN32 +#ifdef _WIN32 RB_PRNGWIN32, #endif RB_PRNG_DEFAULT, @@ -165,11 +165,15 @@ typedef enum { int rb_init_prng(const char *path, prng_seed_t seed_type); int rb_get_random(void *buf, size_t len); int rb_get_pseudo_random(void *buf, size_t len); -void rb_ssl_start_accepted(rb_fde_t *new_F, ACCB *cb, void *data, int timeout); -void rb_ssl_start_connected(rb_fde_t *F, CNCB *callback, void *data, int timeout); +void rb_ssl_start_accepted(rb_fde_t *new_F, ACCB * cb, void *data, int timeout); +void rb_ssl_start_connected(rb_fde_t *F, CNCB * callback, void *data, int timeout); int rb_supports_ssl(void); unsigned int rb_ssl_handshake_count(rb_fde_t *F); void rb_ssl_clear_handshake_count(rb_fde_t *F); - + + +int rb_pass_fd_to_process(rb_fde_t *, pid_t, rb_fde_t *); +rb_fde_t *rb_recv_fd(rb_fde_t *); + #endif /* INCLUDED_commio_h */ diff --git a/libratbox/include/rb_event.h b/libratbox/include/rb_event.h index 2da9cf3..61b6efa 100644 --- a/libratbox/include/rb_event.h +++ b/libratbox/include/rb_event.h @@ -21,11 +21,11 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: rb_event.h 25151 2008-03-28 17:19:12Z androsyn $ + * $Id: rb_event.h 26092 2008-09-19 15:13:52Z androsyn $ */ #ifndef RB_LIB_H -# error "Do not use event.h directly" +# error "Do not use event.h directly" #endif #ifndef INCLUDED_event_h @@ -43,7 +43,7 @@ void rb_event_delete(struct ev_entry *); void rb_event_find_delete(EVH * func, void *); void rb_event_update(struct ev_entry *, time_t freq); void rb_set_back_events(time_t); -void rb_dump_events(void (*func)(char *, void *), void *ptr); +void rb_dump_events(void (*func) (char *, void *), void *ptr); void rb_run_event(struct ev_entry *); time_t rb_event_next(void); diff --git a/libratbox/include/rb_helper.h b/libratbox/include/rb_helper.h index 3ec9b16..c04968e 100644 --- a/libratbox/include/rb_helper.h +++ b/libratbox/include/rb_helper.h @@ -19,7 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: rb_helper.h 24936 2008-01-14 20:43:23Z androsyn $ + * $Id: rb_helper.h 26092 2008-09-19 15:13:52Z androsyn $ */ #ifndef RB_LIB_H @@ -36,25 +36,28 @@ typedef void rb_helper_cb(rb_helper *); -rb_helper *rb_helper_start(const char *name, const char *fullpath, rb_helper_cb *read_cb, rb_helper_cb *error_cb); +rb_helper *rb_helper_start(const char *name, const char *fullpath, rb_helper_cb * read_cb, + rb_helper_cb * error_cb); -rb_helper *rb_helper_child(rb_helper_cb *read_cb, rb_helper_cb *error_cb, - log_cb *ilog, restart_cb *irestart, die_cb *idie, - int maxcon, size_t lb_heap_size, size_t dh_size, size_t fd_heap_size); +rb_helper *rb_helper_child(rb_helper_cb * read_cb, rb_helper_cb * error_cb, + log_cb * ilog, restart_cb * irestart, die_cb * idie, + int maxcon, size_t lb_heap_size, size_t dh_size, size_t fd_heap_size); void rb_helper_restart(rb_helper *helper); #ifdef __GNUC__ -void rb_helper_write(rb_helper *helper, const char *format, ...) __attribute((format(printf, 2, 3))); -void rb_helper_write_queue(rb_helper *helper, const char *format, ...) __attribute((format(printf, 2, 3))); +void +rb_helper_write(rb_helper *helper, const char *format, ...) +__attribute((format(printf, 2, 3))); + void rb_helper_write_queue(rb_helper *helper, const char *format, ...) + __attribute((format(printf, 2, 3))); #else void rb_helper_write(rb_helper *helper, const char *format, ...); void rb_helper_write_queue(rb_helper *helper, const char *format, ...); #endif void rb_helper_write_flush(rb_helper *helper); - + void rb_helper_run(rb_helper *helper); void rb_helper_close(rb_helper *helper); int rb_helper_read(rb_helper *helper, void *buf, size_t bufsize); void rb_helper_loop(rb_helper *helper, long delay); #endif - diff --git a/libratbox/include/rb_linebuf.h b/libratbox/include/rb_linebuf.h index 1d231e3..7bd892e 100644 --- a/libratbox/include/rb_linebuf.h +++ b/libratbox/include/rb_linebuf.h @@ -21,11 +21,11 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: rb_linebuf.h 25675 2008-07-06 04:13:05Z androsyn $ + * $Id: rb_linebuf.h 26092 2008-09-19 15:13:52Z androsyn $ */ #ifndef RB_LIB_H -# error "Do not use linebuf.h directly" +# error "Do not use linebuf.h directly" #endif #ifndef __LINEBUF_H__ @@ -46,7 +46,7 @@ typedef struct _buf_line { char buf[BUF_DATA_SIZE + 2]; uint8_t terminated; /* Whether we've terminated the buffer */ - uint8_t raw; /* Whether this linebuf may hold 8-bit data */ + uint8_t raw; /* Whether this linebuf may hold 8-bit data */ int len; /* How much data we've got */ int refcount; /* how many linked lists are we in? */ } buf_line_t; @@ -74,7 +74,7 @@ int rb_linebuf_parse(buf_head_t *, char *, int, int); int rb_linebuf_get(buf_head_t *, char *, int, int, int); void rb_linebuf_putmsg(buf_head_t *, const char *, va_list *, const char *, ...); void rb_linebuf_put(buf_head_t *, const char *, ...); -void rb_linebuf_putbuf(buf_head_t *bufhead, const char *buffer); +void rb_linebuf_putbuf(buf_head_t * bufhead, const char *buffer); void rb_linebuf_attach(buf_head_t *, buf_head_t *); void rb_count_rb_linebuf_memory(size_t *, size_t *); int rb_linebuf_flush(rb_fde_t *F, buf_head_t *); diff --git a/libratbox/include/rb_memory.h b/libratbox/include/rb_memory.h index 1cb3d3c..d4dd6f0 100644 --- a/libratbox/include/rb_memory.h +++ b/libratbox/include/rb_memory.h @@ -21,7 +21,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: rb_memory.h 25375 2008-05-16 15:19:51Z androsyn $ + * $Id: rb_memory.h 26092 2008-09-19 15:13:52Z androsyn $ */ #ifndef RB_LIB_H @@ -62,7 +62,7 @@ rb_strndup(const char *x, size_t y) if(rb_unlikely(ret == NULL)) rb_outofmemory(); rb_strlcpy(ret, x, y); - return(ret); + return (ret); } static inline char * @@ -72,16 +72,15 @@ rb_strdup(const char *x) if(rb_unlikely(ret == NULL)) rb_outofmemory(); strcpy(ret, x); - return(ret); + return (ret); } static inline void rb_free(void *ptr) { - if(rb_likely(ptr != NULL)) - free(ptr); + if(rb_likely(ptr != NULL)) + free(ptr); } #endif /* _I_MEMORY_H */ - diff --git a/libratbox/include/rb_patricia.h b/libratbox/include/rb_patricia.h index 353b301..e16e12e 100644 --- a/libratbox/include/rb_patricia.h +++ b/libratbox/include/rb_patricia.h @@ -32,8 +32,8 @@ typedef struct _rb_prefix_t { - unsigned short family; /* AF_INET | AF_INET6 */ - unsigned short bitlen; /* same as mask? */ + unsigned short family; /* AF_INET | AF_INET6 */ + unsigned short bitlen; /* same as mask? */ int ref_count; /* reference count */ union { @@ -49,7 +49,7 @@ rb_prefix_t; typedef struct _rb_patricia_node_t { - unsigned int bit; /* flag if this node used */ + unsigned int bit; /* flag if this node used */ rb_prefix_t *prefix; /* who we are in patricia tree */ struct _rb_patricia_node_t *l, *r; /* left and right children */ struct _rb_patricia_node_t *parent; /* may be used */ @@ -60,35 +60,36 @@ rb_patricia_node_t; typedef struct _rb_patricia_tree_t { rb_patricia_node_t *head; - unsigned int maxbits; /* for IP, 32 bit addresses */ + unsigned int maxbits; /* for IP, 32 bit addresses */ int num_active_node; /* for debug purpose */ } rb_patricia_tree_t; -rb_patricia_node_t *rb_match_ip(rb_patricia_tree_t * tree, struct sockaddr *ip); -rb_patricia_node_t *rb_match_ip_exact(rb_patricia_tree_t * tree, struct sockaddr *ip, unsigned int len); -rb_patricia_node_t *rb_match_string(rb_patricia_tree_t * tree, const char *string); -rb_patricia_node_t *rb_match_exact_string(rb_patricia_tree_t * tree, const char *string); -rb_patricia_node_t *rb_patricia_search_exact(rb_patricia_tree_t * patricia, rb_prefix_t * prefix); -rb_patricia_node_t *rb_patricia_search_best(rb_patricia_tree_t * patricia, rb_prefix_t * prefix); -rb_patricia_node_t *rb_patricia_search_best2(rb_patricia_tree_t * patricia, - rb_prefix_t * prefix, int inclusive); -rb_patricia_node_t *rb_patricia_lookup(rb_patricia_tree_t * patricia, rb_prefix_t * prefix); +rb_patricia_node_t *rb_match_ip(rb_patricia_tree_t *tree, struct sockaddr *ip); +rb_patricia_node_t *rb_match_ip_exact(rb_patricia_tree_t *tree, struct sockaddr *ip, + unsigned int len); +rb_patricia_node_t *rb_match_string(rb_patricia_tree_t *tree, const char *string); +rb_patricia_node_t *rb_match_exact_string(rb_patricia_tree_t *tree, const char *string); +rb_patricia_node_t *rb_patricia_search_exact(rb_patricia_tree_t *patricia, rb_prefix_t *prefix); +rb_patricia_node_t *rb_patricia_search_best(rb_patricia_tree_t *patricia, rb_prefix_t *prefix); +rb_patricia_node_t *rb_patricia_search_best2(rb_patricia_tree_t *patricia, + rb_prefix_t *prefix, int inclusive); +rb_patricia_node_t *rb_patricia_lookup(rb_patricia_tree_t *patricia, rb_prefix_t *prefix); -void rb_patricia_remove(rb_patricia_tree_t * patricia, rb_patricia_node_t * node); +void rb_patricia_remove(rb_patricia_tree_t *patricia, rb_patricia_node_t *node); rb_patricia_tree_t *rb_new_patricia(int maxbits); -void rb_clear_patricia(rb_patricia_tree_t * patricia, void (*func)(void *)); -void rb_destroy_patricia(rb_patricia_tree_t * patricia, void (*func)(void *)); -void rb_patricia_process(rb_patricia_tree_t * patricia, void (*func)(rb_prefix_t *, void *)); +void rb_clear_patricia(rb_patricia_tree_t *patricia, void (*func) (void *)); +void rb_destroy_patricia(rb_patricia_tree_t *patricia, void (*func) (void *)); +void rb_patricia_process(rb_patricia_tree_t *patricia, void (*func) (rb_prefix_t *, void *)); void rb_init_patricia(void); #if 0 rb_prefix_t *ascii2prefix(int family, char *string); #endif -rb_patricia_node_t *make_and_lookup(rb_patricia_tree_t * tree, const char *string); -rb_patricia_node_t *make_and_lookup_ip(rb_patricia_tree_t * tree, struct sockaddr *, int bitlen); +rb_patricia_node_t *make_and_lookup(rb_patricia_tree_t *tree, const char *string); +rb_patricia_node_t *make_and_lookup_ip(rb_patricia_tree_t *tree, struct sockaddr *, int bitlen); #define RB_PATRICIA_MAXBITS 128 diff --git a/libratbox/include/rb_rawbuf.h b/libratbox/include/rb_rawbuf.h index debbdaa..523a17b 100644 --- a/libratbox/include/rb_rawbuf.h +++ b/libratbox/include/rb_rawbuf.h @@ -24,7 +24,7 @@ */ #ifndef RB_LIB_H -# error "Do not use rawbuf.h directly" +# error "Do not use rawbuf.h directly" #endif #ifndef INCLUDED_RAWBUF_H__ @@ -41,6 +41,6 @@ rawbuf_head_t *rb_new_rawbuffer(void); int rb_rawbuf_get(rawbuf_head_t *, void *data, int len); void rb_rawbuf_append(rawbuf_head_t *, void *data, int len); int rb_rawbuf_flush(rawbuf_head_t *, rb_fde_t *F); -int rb_rawbuf_length(rawbuf_head_t *rb); +int rb_rawbuf_length(rawbuf_head_t * rb); #endif diff --git a/libratbox/include/rb_snprintf.h b/libratbox/include/rb_snprintf.h index c12dd78..b9979b0 100644 --- a/libratbox/include/rb_snprintf.h +++ b/libratbox/include/rb_snprintf.h @@ -21,11 +21,11 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: rb_snprintf.h 24324 2007-08-31 22:05:45Z androsyn $ + * $Id: rb_snprintf.h 26092 2008-09-19 15:13:52Z androsyn $ */ #ifndef RB_LIB_H -# error "Do not use snprintf.h directly" +# error "Do not use snprintf.h directly" #endif #ifndef SPRINTF_IRC @@ -40,10 +40,14 @@ * rb_sprintf - optimized sprintf */ #ifdef __GNUC__ -int rb_sprintf(char *str, const char *fmt, ...) __attribute((format(printf, 2, 3))); -int rb_snprintf(char *str, const size_t size, const char *, ...) __attribute__ ((format(printf, 3, 4))); -int rb_sprintf_append(char *str, const char *format, ...) __attribute((format(printf, 2, 3))); -int rb_snprintf_append(char *str, size_t len, const char *format, ...) __attribute__ ((format(printf, 3, 4))); +int +rb_sprintf(char *str, const char *fmt, ...) +__attribute((format(printf, 2, 3))); + int rb_snprintf(char *str, const size_t size, const char *, ...) + __attribute__ ((format(printf, 3, 4))); + int rb_sprintf_append(char *str, const char *format, ...) __attribute((format(printf, 2, 3))); + int rb_snprintf_append(char *str, size_t len, const char *format, ...) + __attribute__ ((format(printf, 3, 4))); #else int rb_sprintf(char *str, const char *format, ...); int rb_snprintf(char *str, const size_t size, const char *, ...); diff --git a/libratbox/include/rb_tools.h b/libratbox/include/rb_tools.h index 14bfda6..208d365 100644 --- a/libratbox/include/rb_tools.h +++ b/libratbox/include/rb_tools.h @@ -21,11 +21,11 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: rb_tools.h 25042 2008-01-23 16:14:08Z androsyn $ + * $Id: rb_tools.h 26170 2008-10-26 20:59:07Z androsyn $ */ #ifndef RB_LIB_H -# error "Do not use tools.h directly" +# error "Do not use tools.h directly" #endif #ifndef __TOOLS_H__ @@ -34,6 +34,8 @@ size_t rb_strlcpy(char *dst, const char *src, size_t siz); size_t rb_strlcat(char *dst, const char *src, size_t siz); size_t rb_strnlen(const char *s, size_t count); +char *rb_basename(const char *); +char *rb_dirname(const char *); int rb_string_to_array(char *string, char **parv, int maxpara); @@ -59,7 +61,7 @@ struct _rb_dlink_list }; rb_dlink_node *rb_make_rb_dlink_node(void); -void rb_free_rb_dlink_node(rb_dlink_node * lp); +void rb_free_rb_dlink_node(rb_dlink_node *lp); void rb_init_rb_dlink_nodes(size_t dh_size); /* This macros are basically swiped from the linux kernel @@ -99,7 +101,7 @@ void rb_init_rb_dlink_nodes(size_t dh_size); */ static inline void -rb_dlinkMoveNode(rb_dlink_node * m, rb_dlink_list * oldlist, rb_dlink_list * newlist) +rb_dlinkMoveNode(rb_dlink_node *m, rb_dlink_list *oldlist, rb_dlink_list *newlist) { /* Assumption: If m->next == NULL, then list->tail == m * and: If m->prev == NULL, then list->head == m @@ -131,7 +133,7 @@ rb_dlinkMoveNode(rb_dlink_node * m, rb_dlink_list * oldlist, rb_dlink_list * new } static inline void -rb_dlinkAdd(void *data, rb_dlink_node * m, rb_dlink_list * list) +rb_dlinkAdd(void *data, rb_dlink_node *m, rb_dlink_list *list) { assert(data != NULL); assert(m != NULL); @@ -152,7 +154,7 @@ rb_dlinkAdd(void *data, rb_dlink_node * m, rb_dlink_list * list) } static inline void -rb_dlinkAddBefore(rb_dlink_node * b, void *data, rb_dlink_node * m, rb_dlink_list * list) +rb_dlinkAddBefore(rb_dlink_node *b, void *data, rb_dlink_node *m, rb_dlink_list *list) { assert(b != NULL); assert(data != NULL); @@ -180,11 +182,11 @@ rb_dlinkMoveTail(rb_dlink_node *m, rb_dlink_list *list) { if(list->tail == m) return; - + /* From here assume that m->next != NULL as that can only * be at the tail and assume that the node is on the list */ - + m->next->prev = m->prev; if(m->prev != NULL) @@ -199,7 +201,7 @@ rb_dlinkMoveTail(rb_dlink_node *m, rb_dlink_list *list) } static inline void -rb_dlinkAddTail(void *data, rb_dlink_node * m, rb_dlink_list * list) +rb_dlinkAddTail(void *data, rb_dlink_node *m, rb_dlink_list *list) { assert(m != NULL); assert(list != NULL); @@ -223,7 +225,7 @@ rb_dlinkAddTail(void *data, rb_dlink_node * m, rb_dlink_list * list) * often of all non-spontaneous functions. So it had better be * efficient. */ static inline void -rb_dlinkDelete(rb_dlink_node * m, rb_dlink_list * list) +rb_dlinkDelete(rb_dlink_node *m, rb_dlink_list *list) { assert(m != NULL); assert(list != NULL); @@ -302,7 +304,7 @@ rb_dlinkFind(void *data, rb_dlink_list *list) rb_dlink_node *ptr; assert(list != NULL); assert(data != NULL); - + RB_DLINK_FOREACH(ptr, list->head) { if(ptr->data == data) @@ -312,7 +314,7 @@ rb_dlinkFind(void *data, rb_dlink_list *list) } static inline void -rb_dlinkMoveList(rb_dlink_list * from, rb_dlink_list * to) +rb_dlinkMoveList(rb_dlink_list *from, rb_dlink_list *to) { assert(from != NULL); assert(to != NULL); diff --git a/libratbox/ltmain.sh b/libratbox/ltmain.sh index f924d30..0bf3848 100644 --- a/libratbox/ltmain.sh +++ b/libratbox/ltmain.sh @@ -2,7 +2,7 @@ # NOTE: Changing this file will not affect anything until you rerun configure. # # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, -# 2007 Free Software Foundation, Inc. +# 2007, 2008 Free Software Foundation, Inc. # Originally by Gordon Matzigkeit , 1996 # # This program is free software; you can redistribute it and/or modify @@ -43,8 +43,8 @@ EXIT_FAILURE=1 PROGRAM=ltmain.sh PACKAGE=libtool -VERSION="1.5.24 Debian 1.5.24-1" -TIMESTAMP=" (1.1220.2.456 2007/06/24 02:25:32)" +VERSION="1.5.26 Debian 1.5.26-4" +TIMESTAMP=" (1.1220.2.493 2008/02/01 16:58:18)" # Be Bourne compatible (taken from Autoconf:_AS_BOURNE_COMPATIBLE). if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then @@ -113,15 +113,21 @@ esac # These must not be set unconditionally because not all systems understand # e.g. LANG=C (notably SCO). # We save the old values to restore during execute mode. -for lt_var in LANG LC_ALL LC_CTYPE LC_COLLATE LC_MESSAGES +lt_env= +for lt_var in LANG LANGUAGE LC_ALL LC_CTYPE LC_COLLATE LC_MESSAGES do eval "if test \"\${$lt_var+set}\" = set; then save_$lt_var=\$$lt_var + lt_env=\"$lt_var=\$$lt_var \$lt_env\" $lt_var=C export $lt_var fi" done +if test -n "$lt_env"; then + lt_env="env $lt_env" +fi + # Make sure IFS has a sensible default lt_nl=' ' @@ -485,7 +491,7 @@ do echo "\ $PROGRAM (GNU $PACKAGE) $VERSION$TIMESTAMP -Copyright (C) 2007 Free Software Foundation, Inc. +Copyright (C) 2008 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." exit $? @@ -788,6 +794,7 @@ if test -z "$show_help"; then *.for) xform=for ;; *.java) xform=java ;; *.obj) xform=obj ;; + *.sx) xform=sx ;; esac libobj=`$echo "X$libobj" | $Xsed -e "s/\.$xform$/.lo/"` @@ -956,7 +963,7 @@ EOF $run $rm "$lobj" "$output_obj" $show "$command" - if $run eval "$command"; then : + if $run eval $lt_env "$command"; then : else test -n "$output_obj" && $run $rm $removelist exit $EXIT_FAILURE @@ -1028,7 +1035,7 @@ EOF command="$command$suppress_output" $run $rm "$obj" "$output_obj" $show "$command" - if $run eval "$command"; then : + if $run eval $lt_env "$command"; then : else $run $rm $removelist exit $EXIT_FAILURE @@ -1161,6 +1168,7 @@ EOF thread_safe=no vinfo= vinfo_number=no + single_module="${wl}-single_module" func_infer_tag $base_compile @@ -1646,6 +1654,11 @@ EOF continue ;; + -multi_module) + single_module="${wl}-multi_module" + continue + ;; + -module) module=yes continue @@ -2152,7 +2165,12 @@ EOF continue fi name=`$echo "X$deplib" | $Xsed -e 's/^-l//'` - for searchdir in $newlib_search_path $lib_search_path $sys_lib_search_path $shlib_search_path; do + if test "$linkmode" = lib; then + searchdirs="$newlib_search_path $lib_search_path $compiler_lib_search_dirs $sys_lib_search_path $shlib_search_path" + else + searchdirs="$newlib_search_path $lib_search_path $sys_lib_search_path $shlib_search_path" + fi + for searchdir in $searchdirs; do for search_ext in .la $std_shrext .so .a; do # Search the libtool library lib="$searchdir/lib${name}${search_ext}" @@ -2948,12 +2966,18 @@ EOF # we do not want to link against static libs, # but need to link against shared eval deplibrary_names=`${SED} -n -e 's/^library_names=\(.*\)$/\1/p' $deplib` + eval deplibdir=`${SED} -n -e 's/^libdir=\(.*\)$/\1/p' $deplib` if test -n "$deplibrary_names" ; then for tmp in $deplibrary_names ; do depdepl=$tmp done - if test -f "$path/$depdepl" ; then + if test -f "$deplibdir/$depdepl" ; then + depdepl="$deplibdir/$depdepl" + elif test -f "$path/$depdepl" ; then depdepl="$path/$depdepl" + else + # Can't find it, oh well... + depdepl= fi # do not add paths which are already there case " $newlib_search_path " in @@ -3101,9 +3125,10 @@ EOF case $linkmode in oldlib) - if test -n "$deplibs"; then - $echo "$modename: warning: \`-l' and \`-L' are ignored for archives" 1>&2 - fi + case " $deplibs" in + *\ -l* | *\ -L*) + $echo "$modename: warning: \`-l' and \`-L' are ignored for archives" 1>&2 ;; + esac if test -n "$dlfiles$dlprefiles" || test "$dlself" != no; then $echo "$modename: warning: \`-dlopen' is ignored for archives" 1>&2 @@ -4245,9 +4270,10 @@ EOF ;; obj) - if test -n "$deplibs"; then - $echo "$modename: warning: \`-l' and \`-L' are ignored for objects" 1>&2 - fi + case " $deplibs" in + *\ -l* | *\ -L*) + $echo "$modename: warning: \`-l' and \`-L' are ignored for objects" 1>&2 ;; + esac if test -n "$dlfiles$dlprefiles" || test "$dlself" != no; then $echo "$modename: warning: \`-dlopen' is ignored for objects" 1>&2 @@ -6486,7 +6512,7 @@ relink_command=\"$relink_command\"" fi # Restore saved environment variables - for lt_var in LANG LC_ALL LC_CTYPE LC_COLLATE LC_MESSAGES + for lt_var in LANG LANGUAGE LC_ALL LC_CTYPE LC_COLLATE LC_MESSAGES do eval "if test \"\${save_$lt_var+set}\" = set; then $lt_var=\$save_$lt_var; export $lt_var diff --git a/libratbox/src/Makefile.am b/libratbox/src/Makefile.am index 0355434..ff750d1 100644 --- a/libratbox/src/Makefile.am +++ b/libratbox/src/Makefile.am @@ -1,9 +1,18 @@ -# $Id: Makefile.am 25689 2008-07-10 17:13:27Z androsyn $ +# $Id: Makefile.am 26046 2008-09-09 16:37:30Z androsyn $ AUTOMAKE_OPTIONS = foreign INCLUDES = -I. -I../include @SSL_CFLAGS@ @GNUTLS_CFLAGS@ +BUILT_SOURCES = version.c + +version.c: version.c.SH ../CREDITS + $(SHELL) ./version.c.SH + $(CP) version.c version.c.last + +version.lo: version.c ../include/serno.h + $(LIBTOOL) --mode=compile $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ + libratbox_la_SOURCES = \ unix.c \ win32.c \ @@ -29,10 +38,11 @@ libratbox_la_SOURCES = \ kqueue.c \ rawbuf.c \ patricia.c \ - arc4random.c + arc4random.c \ + version.c -libratbox_la_LDFLAGS = -avoid-version -no-undefined -export-symbols export-syms.txt +libratbox_la_LDFLAGS = @CRYPT_LIB@ @SSL_LIBS@ @GNUTLS_LIBS@ -avoid-version -no-undefined -export-symbols export-syms.txt libratbox_la_LIBADD = @CRYPT_LIB@ @SSL_LIBS@ @GNUTLS_LIBS@ lib_LTLIBRARIES = libratbox.la diff --git a/libratbox/src/Makefile.in b/libratbox/src/Makefile.in index 52d824e..4ad93e5 100644 --- a/libratbox/src/Makefile.in +++ b/libratbox/src/Makefile.in @@ -14,7 +14,7 @@ @SET_MAKE@ -# $Id: Makefile.am 25689 2008-07-10 17:13:27Z androsyn $ +# $Id: Makefile.am 26046 2008-09-09 16:37:30Z androsyn $ VPATH = @srcdir@ pkgdatadir = $(datadir)/@PACKAGE@ @@ -58,7 +58,7 @@ am_libratbox_la_OBJECTS = unix.lo win32.lo crypt.lo balloc.lo \ commio.lo openssl.lo gnutls.lo nossl.lo event.lo ratbox_lib.lo \ rb_memory.lo linebuf.lo snprintf.lo tools.lo helper.lo \ devpoll.lo epoll.lo poll.lo ports.lo sigio.lo select.lo \ - kqueue.lo rawbuf.lo patricia.lo arc4random.lo + kqueue.lo rawbuf.lo patricia.lo arc4random.lo version.lo libratbox_la_OBJECTS = $(am_libratbox_la_OBJECTS) libratbox_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \ $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ @@ -120,7 +120,6 @@ INSTALL_DATA = @INSTALL_DATA@ INSTALL_PROGRAM = @INSTALL_PROGRAM@ INSTALL_SCRIPT = @INSTALL_SCRIPT@ INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ -LD = @LD@ LDFLAGS = @LDFLAGS@ LIBOBJS = @LIBOBJS@ LIBS = @LIBS@ @@ -202,13 +201,13 @@ psdir = @psdir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ -subdirs = @subdirs@ sysconfdir = @sysconfdir@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ AUTOMAKE_OPTIONS = foreign INCLUDES = -I. -I../include @SSL_CFLAGS@ @GNUTLS_CFLAGS@ +BUILT_SOURCES = version.c libratbox_la_SOURCES = \ unix.c \ win32.c \ @@ -234,12 +233,14 @@ libratbox_la_SOURCES = \ kqueue.c \ rawbuf.c \ patricia.c \ - arc4random.c + arc4random.c \ + version.c -libratbox_la_LDFLAGS = -avoid-version -no-undefined -export-symbols export-syms.txt +libratbox_la_LDFLAGS = @CRYPT_LIB@ @SSL_LIBS@ @GNUTLS_LIBS@ -avoid-version -no-undefined -export-symbols export-syms.txt libratbox_la_LIBADD = @CRYPT_LIB@ @SSL_LIBS@ @GNUTLS_LIBS@ lib_LTLIBRARIES = libratbox.la -all: all-am +all: $(BUILT_SOURCES) + $(MAKE) $(AM_MAKEFLAGS) all-am .SUFFIXES: .SUFFIXES: .c .lo .o .obj @@ -332,6 +333,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/snprintf.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tools.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/unix.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/version.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/win32.Plo@am__quote@ .c.o: @@ -435,13 +437,15 @@ distdir: $(DISTFILES) fi; \ done check-am: all-am -check: check-am +check: $(BUILT_SOURCES) + $(MAKE) $(AM_MAKEFLAGS) check-am all-am: Makefile $(LTLIBRARIES) installdirs: for dir in "$(DESTDIR)$(libdir)"; do \ test -z "$$dir" || $(MKDIR_P) "$$dir"; \ done -install: install-am +install: $(BUILT_SOURCES) + $(MAKE) $(AM_MAKEFLAGS) install-am install-exec: install-exec-am install-data: install-data-am uninstall: uninstall-am @@ -465,6 +469,7 @@ distclean-generic: maintainer-clean-generic: @echo "This command is intended for maintainers to use" @echo "it deletes files that may require special tools to rebuild." + -test -z "$(BUILT_SOURCES)" || rm -f $(BUILT_SOURCES) clean: clean-am clean-am: clean-generic clean-libLTLIBRARIES clean-libtool \ @@ -540,6 +545,13 @@ uninstall-am: uninstall-libLTLIBRARIES mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \ tags uninstall uninstall-am uninstall-libLTLIBRARIES + +version.c: version.c.SH ../CREDITS + $(SHELL) ./version.c.SH + $(CP) version.c version.c.last + +version.lo: version.c ../include/serno.h + $(LIBTOOL) --mode=compile $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ # Tell versions [3.59,3.63) of GNU make to not export all variables. # Otherwise a system limit (for SysV at least) may be exceeded. .NOEXPORT: diff --git a/libratbox/src/arc4random.c b/libratbox/src/arc4random.c index 534d223..f88e0b3 100644 --- a/libratbox/src/arc4random.c +++ b/libratbox/src/arc4random.c @@ -1,4 +1,4 @@ -/* $Id: arc4random.c 25705 2008-07-11 18:21:57Z androsyn $ */ +/* $Id: arc4random.c 26092 2008-09-19 15:13:52Z androsyn $ */ /* $$$: arc4random.c 2005/02/08 robert */ /* $NetBSD: arc4random.c,v 1.5.2.1 2004/03/26 22:52:50 jmc Exp $ */ /* $OpenBSD: arc4random.c,v 1.6 2001/06/05 05:05:38 pvalchev Exp $ */ @@ -41,14 +41,15 @@ -struct arc4_stream { +struct arc4_stream +{ uint8_t i; uint8_t j; uint8_t s[256]; }; -static int rs_initialized; +static int rs_initialized; static struct arc4_stream rs; static inline void arc4_init(struct arc4_stream *); @@ -60,9 +61,9 @@ static inline uint32_t arc4_getword(struct arc4_stream *); static inline void arc4_init(struct arc4_stream *as) { - int n; + int n; - for (n = 0; n < 256; n++) + for(n = 0; n < 256; n++) as->s[n] = n; as->i = 0; as->j = 0; @@ -71,11 +72,12 @@ arc4_init(struct arc4_stream *as) static inline void arc4_addrandom(struct arc4_stream *as, uint8_t *dat, int datlen) { - int n; + int n; uint8_t si; as->i--; - for (n = 0; n < 256; n++) { + for(n = 0; n < 256; n++) + { as->i = (as->i + 1); si = as->s[as->i]; as->j = (as->j + si + dat[n % datlen]); @@ -94,42 +96,57 @@ arc4_stir(struct arc4_stream *as) /* XXX this doesn't support egd sources or similiar */ pid = getpid(); - arc4_addrandom(as, (void *)&pid, sizeof(pid)); + arc4_addrandom(as, (void *)&pid, sizeof(pid)); rb_gettimeofday(&tv, NULL); arc4_addrandom(as, (void *)&tv.tv_sec, sizeof(&tv.tv_sec)); arc4_addrandom(as, (void *)&tv.tv_usec, sizeof(&tv.tv_usec)); rb_gettimeofday(&tv, NULL); arc4_addrandom(as, (void *)&tv.tv_usec, sizeof(&tv.tv_usec)); - + #if defined(HAVE_GETRUSAGE) && RUSAGE_SELF { struct rusage buf; getrusage(RUSAGE_SELF, &buf); arc4_addrandom(as, (void *)&buf, sizeof(buf)); - memset(&buf, 0, sizeof(buf)) - } -#endif + memset(&buf, 0, sizeof(buf))} +#endif -#if !defined(WIN32) +#if !defined(_WIN32) { uint8_t rnd[128]; int fd; fd = open("/dev/urandom", O_RDONLY); - if (fd != -1) + if(fd != -1) { read(fd, rnd, sizeof(rnd)); close(fd); arc4_addrandom(as, (void *)rnd, sizeof(rnd)); + memset(&rnd, 0, sizeof(rnd)); } + } #else { LARGE_INTEGER performanceCount; - if (QueryPerformanceCounter (&performanceCount)) + if(QueryPerformanceCounter(&performanceCount)) { arc4_addrandom(as, (void *)&performanceCount, sizeof(performanceCount)); } + HMODULE lib = LoadLibrary("ADVAPI32.DLL"); + if(lib) + { + uint8_t rnd[128]; + BOOLEAN(APIENTRY * pfn) (void *, ULONG) = + (BOOLEAN(APIENTRY *) (void *, ULONG))GetProcAddress(lib, + "SystemFunction036"); + if(pfn) + { + if(pfn(rnd, sizeof(rnd)) == TRUE) + arc4_addrandom(as, (void *)rnd, sizeof(rnd)); + memset(&rnd, 0, sizeof(rnd)); + } + } } #endif @@ -141,7 +158,7 @@ arc4_stir(struct arc4_stream *as) * http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps * N = 256 in our case. */ - for (n = 0; n < 256 * 4; n++) + for(n = 0; n < 256 * 4; n++) arc4_getbyte(as); } @@ -173,7 +190,8 @@ arc4_getword(struct arc4_stream *as) void arc4random_stir(void) { - if (!rs_initialized) { + if(!rs_initialized) + { arc4_init(&rs); rs_initialized = 1; } @@ -183,7 +201,7 @@ arc4random_stir(void) void arc4random_addrandom(uint8_t *dat, int datlen) { - if (!rs_initialized) + if(!rs_initialized) arc4random_stir(); arc4_addrandom(&rs, dat, datlen); } @@ -191,10 +209,9 @@ arc4random_addrandom(uint8_t *dat, int datlen) uint32_t arc4random(void) { - if (!rs_initialized) + if(!rs_initialized) arc4random_stir(); return arc4_getword(&rs); } #endif - diff --git a/libratbox/src/balloc.c b/libratbox/src/balloc.c index 7c06e30..c827614 100644 --- a/libratbox/src/balloc.c +++ b/libratbox/src/balloc.c @@ -28,7 +28,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: balloc.c 25861 2008-08-06 19:51:44Z androsyn $ + * $Id: balloc.c 26100 2008-09-20 01:27:19Z androsyn $ */ /* @@ -59,6 +59,7 @@ #include #include +#ifndef NOBALLOC #ifdef HAVE_MMAP /* We've got mmap() that is good */ #include /* HP-UX sucks */ @@ -68,6 +69,7 @@ #endif #endif #endif +#endif static uintptr_t offset_pad; @@ -93,7 +95,7 @@ struct rb_bh }; #ifndef NOBALLOC -static int newblock(rb_bh * bh); +static int newblock(rb_bh *bh); static void rb_bh_gc_event(void *unused); #endif /* !NOBALLOC */ static rb_dlink_list *heap_lists; @@ -110,7 +112,7 @@ _rb_bh_fail(const char *reason, const char *file, int line) rb_lib_log("rb_heap_blockheap failure: %s (%s:%d)", reason, file, line); abort(); } - + #ifndef NOBALLOC /* * static inline void free_block(void *ptr, size_t size) @@ -125,7 +127,7 @@ free_block(void *ptr, size_t size) #ifdef HAVE_MMAP munmap(ptr, size); #else -#ifdef WIN32 +#ifdef _WIN32 HeapFree(block_heap, 0, ptr); #else free(ptr); @@ -156,11 +158,11 @@ rb_init_bh(void) offset_pad += __alignof__(long long); offset_pad &= ~(__alignof__(long long) - 1); } -#endif - +#endif + #ifndef NOBALLOC -#ifdef WIN32 - block_heap = HeapCreate(HEAP_NO_SERIALIZE, 0, 0); +#ifdef _WIN32 + block_heap = HeapCreate(HEAP_NO_SERIALIZE, 0, 0); #endif rb_event_addish("rb_bh_gc_event", rb_bh_gc_event, NULL, 300); #endif /* !NOBALLOC */ @@ -190,15 +192,15 @@ get_block(size_t size) close(zero_fd); #endif /* MAP_ANON */ if(ptr == MAP_FAILED) - ptr = NULL; + ptr = NULL; #else -#ifdef WIN32 +#ifdef _WIN32 ptr = HeapAlloc(block_heap, 0, size); -#else +#else ptr = malloc(size); #endif #endif - return(ptr); + return (ptr); } @@ -224,7 +226,7 @@ rb_bh_gc_event(void *unused) /* ************************************************************************ */ static int -newblock(rb_bh * bh) +newblock(rb_bh *bh) { rb_heap_block *b; unsigned long i; @@ -242,14 +244,14 @@ newblock(rb_bh * bh) } offset = (uintptr_t)b->elems; /* Setup our blocks now */ - for (i = 0; i < bh->elemsPerBlock; i++, offset += bh->elemSize) + for(i = 0; i < bh->elemsPerBlock; i++, offset += bh->elemSize) { *((void **)offset) = b; node = (void *)(offset + offset_pad); rb_dlinkAdd((void *)offset, node, &bh->free_list); } rb_dlinkAdd(b, &b->node, &bh->block_list); - b->free_count = bh->elemsPerBlock; + b->free_count = bh->elemsPerBlock; return (0); } #endif /* !NOBALLOC */ @@ -281,10 +283,10 @@ rb_bh_create(size_t elemsize, int elemsperblock, const char *desc) { rb_bh_fail("Attempting to rb_bh_create idiotic sizes"); } - + if(elemsize < sizeof(rb_dlink_node)) rb_bh_fail("Attempt to rb_bh_create smaller than sizeof(rb_dlink_node)"); - + /* Allocate our new rb_bh */ bh = rb_malloc(sizeof(rb_bh)); #ifndef NOBALLOC @@ -334,13 +336,13 @@ rb_bh_create(size_t elemsize, int elemsperblock, const char *desc) /* ************************************************************************ */ void * -rb_bh_alloc(rb_bh * bh) +rb_bh_alloc(rb_bh *bh) { #ifndef NOBALLOC rb_dlink_node *new_node; - rb_heap_block *block; + rb_heap_block **block; void *ptr; -#endif +#endif lrb_assert(bh != NULL); if(rb_unlikely(bh == NULL)) { @@ -348,7 +350,7 @@ rb_bh_alloc(rb_bh * bh) } #ifdef NOBALLOC - return(rb_malloc(bh->elemSize)); + return (rb_malloc(bh->elemSize)); #else if(bh->free_list.head == NULL) { @@ -368,11 +370,12 @@ rb_bh_alloc(rb_bh * bh) } new_node = bh->free_list.head; - block = new_node->data; - ptr = new_node->data + offset_pad; + block = (rb_heap_block **) new_node->data; + ptr = (void *)((uintptr_t)new_node->data + (uintptr_t)offset_pad); rb_dlinkDelete(new_node, &bh->free_list); + (*block)->free_count--; memset(ptr, 0, bh->elemSize - offset_pad); - return(ptr); + return (ptr); #endif } @@ -389,7 +392,7 @@ rb_bh_alloc(rb_bh * bh) /* 0 if successful, 1 if element not contained within rb_bh. */ /* ************************************************************************ */ int -rb_bh_free(rb_bh * bh, void *ptr) +rb_bh_free(rb_bh *bh, void *ptr) { #ifndef NOBALLOC rb_heap_block *block; @@ -413,14 +416,17 @@ rb_bh_free(rb_bh * bh, void *ptr) #ifdef NOBALLOC rb_free(ptr); #else - data = (void *)(ptr - offset_pad); - block = *(rb_heap_block **)data; + data = (void *)((uintptr_t)ptr - (uintptr_t)offset_pad); + block = *(rb_heap_block **) data; /* XXX */ - if(rb_unlikely(!((uintptr_t)ptr >= (uintptr_t)block->elems && (uintptr_t)ptr < (uintptr_t)block->elems + (uintptr_t)block->alloc_size))) + if(rb_unlikely + (!((uintptr_t)ptr >= (uintptr_t)block->elems + && (uintptr_t)ptr < (uintptr_t)block->elems + (uintptr_t)block->alloc_size))) { rb_bh_fail("rb_bh_free() bogus pointer"); } block->free_count++; + rb_dlinkAdd(data, (rb_dlink_node *)ptr, &bh->free_list); #endif /* !NOBALLOC */ return (0); @@ -438,7 +444,7 @@ rb_bh_free(rb_bh * bh, void *ptr) /* 0 if successful, 1 if bh == NULL */ /* ************************************************************************ */ int -rb_bh_destroy(rb_bh * bh) +rb_bh_destroy(rb_bh *bh) { #ifndef NOBALLOC rb_dlink_node *ptr, *next; @@ -455,7 +461,7 @@ rb_bh_destroy(rb_bh * bh) rb_free(b); } #endif /* !NOBALLOC */ - + rb_dlinkDelete(&bh->hlist, heap_lists); rb_free(bh->desc); rb_free(bh); @@ -464,8 +470,9 @@ rb_bh_destroy(rb_bh * bh) } void -rb_bh_usage(rb_bh * bh, size_t * bused, size_t * bfree, size_t * bmemusage, const char **desc) +rb_bh_usage(rb_bh *bh, size_t *bused, size_t *bfree, size_t *bmemusage, const char **desc) { +#ifndef NOBALLOC size_t used, freem, memusage; if(bh == NULL) @@ -484,9 +491,17 @@ rb_bh_usage(rb_bh * bh, size_t * bused, size_t * bfree, size_t * bmemusage, cons *bmemusage = memusage; if(desc != NULL) *desc = bh->desc; +#else + static char *noballoc = "no blockheap"; + *bused = 0; + *bfree = 0; + *bmemusage = 0; + *desc = noballoc; +#endif } -void rb_bh_usage_all(rb_bh_usage_cb *cb, void *data) +void +rb_bh_usage_all(rb_bh_usage_cb *cb, void *data) { rb_dlink_node *ptr; rb_bh *bh; @@ -499,14 +514,14 @@ void rb_bh_usage_all(rb_bh_usage_cb *cb, void *data) RB_DLINK_FOREACH(ptr, heap_lists->head) { - bh = (rb_bh *)ptr->data; + bh = (rb_bh *)ptr->data; freem = rb_dlink_list_length(&bh->free_list); used = (rb_dlink_list_length(&bh->block_list) * bh->elemsPerBlock) - freem; memusage = used * bh->elemSize; heapalloc = (freem + used) * bh->elemSize; if(bh->desc != NULL) desc = bh->desc; - cb(used, freem, memusage, heapalloc, desc, data); + cb(used, freem, memusage, heapalloc, desc, data); } return; } @@ -517,7 +532,7 @@ rb_bh_total_usage(size_t *total_alloc, size_t *total_used) rb_dlink_node *ptr; size_t total_memory = 0, used_memory = 0, used, freem; rb_bh *bh; - + RB_DLINK_FOREACH(ptr, heap_lists->head) { bh = (rb_bh *)ptr->data; @@ -526,7 +541,7 @@ rb_bh_total_usage(size_t *total_alloc, size_t *total_used) used_memory += used * bh->elemSize; total_memory += (freem + used) * bh->elemSize; } - + if(total_alloc != NULL) *total_alloc = total_memory; if(total_used != NULL) @@ -535,20 +550,21 @@ rb_bh_total_usage(size_t *total_alloc, size_t *total_used) #ifndef NOBALLOC int -rb_bh_gc(rb_bh * bh) +rb_bh_gc(rb_bh *bh) { rb_heap_block *b; rb_dlink_node *ptr, *next; unsigned long i; uintptr_t offset; - + if(bh == NULL) { /* somebody is smoking some craq..(probably lee, but don't tell him that) */ return (1); } - if((rb_dlink_list_length(&bh->free_list) < bh->elemsPerBlock) || rb_dlink_list_length(&bh->block_list) == 1) + if((rb_dlink_list_length(&bh->free_list) < bh->elemsPerBlock) + || rb_dlink_list_length(&bh->block_list) == 1) { /* There couldn't possibly be an entire free block. Return. */ return (0); @@ -559,21 +575,22 @@ rb_bh_gc(rb_bh * bh) b = ptr->data; if(rb_dlink_list_length(&bh->block_list) == 1) return (0); - + if(b->free_count == bh->elemsPerBlock) { /* i'm seriously going to hell for this.. */ offset = (uintptr_t)b->elems; - for (i = 0; i < bh->elemsPerBlock; i++, offset += (uintptr_t)bh->elemSize) + for(i = 0; i < bh->elemsPerBlock; i++, offset += (uintptr_t)bh->elemSize) { - rb_dlinkDelete(((rb_dlink_node *)offset), &bh->free_list); + rb_dlinkDelete((rb_dlink_node *)(offset + offset_pad), + &bh->free_list); } rb_dlinkDelete(&b->node, &bh->block_list); free_block(b->elems, b->alloc_size); rb_free(b); } - + } return (0); } diff --git a/libratbox/src/commio.c b/libratbox/src/commio.c index 59eb02d..7a46b2b 100644 --- a/libratbox/src/commio.c +++ b/libratbox/src/commio.c @@ -21,7 +21,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: commio.c 25795 2008-07-29 15:26:55Z androsyn $ + * $Id: commio.c 26096 2008-09-20 01:05:42Z androsyn $ */ #include #include @@ -64,7 +64,7 @@ static const char *rb_err_str[] = { "Comm OK", "Error during bind()", /* Highest FD and number of open FDs .. */ static int number_fd = 0; -static int rb_maxconnections = 0; +int rb_maxconnections = 0; static PF rb_connect_timeout; static PF rb_connect_tryconnect; @@ -74,6 +74,7 @@ static void mangle_mapped_sockaddr(struct sockaddr *in); #ifndef HAVE_SOCKETPAIR static int rb_inet_socketpair(int d, int type, int protocol, int sv[2]); +static int rb_inet_socketpair_udp(rb_fde_t **newF1, rb_fde_t **newF2); #endif static inline rb_fde_t * @@ -83,12 +84,12 @@ add_fd(int fd) /* look up to see if we have it already */ if(F != NULL) - return F; - + return F; + F = rb_bh_alloc(fd_heap); F->fd = fd; rb_dlinkAdd(F, &F->node, &rb_fd_table[rb_hash_fd(fd)]); - return(F); + return (F); } static inline void @@ -96,11 +97,11 @@ remove_fd(rb_fde_t *F) { if(F == NULL || !IsFDOpen(F)) return; - + rb_dlinkMoveNode(&F->node, &rb_fd_table[rb_hash_fd(F->fd)], &closed_list); } -static void +static void free_fds(void) { rb_fde_t *F; @@ -142,28 +143,15 @@ rb_fd_hack(int *fd) static void rb_close_all(void) { +#ifndef _WIN32 int i; -#ifndef NDEBUG - int fd; -#endif /* XXX someone tell me why we care about 4 fd's ? */ /* XXX btw, fd 3 is used for profiler ! */ - for (i = 4; i < rb_maxconnections; ++i) + for(i = 3; i < rb_maxconnections; ++i) { close(i); } - - /* XXX should his hack be done in all cases? */ -#ifndef NDEBUG - /* fugly hack to reserve fd == 2 */ - (void) close(2); - fd = open("stderr.log", O_WRONLY | O_CREAT | O_APPEND, 0644); - if(fd >= 0) - { - dup2(fd, 2); - close(fd); - } #endif } @@ -188,7 +176,8 @@ rb_get_sockerr(rb_fde_t *F) errtmp = errno; #ifdef SO_ERROR - if(F != NULL && !getsockopt(rb_get_fd(F), SOL_SOCKET, SO_ERROR, (char *) &err, (rb_socklen_t *) & len)) + if(F != NULL + && !getsockopt(rb_get_fd(F), SOL_SOCKET, SO_ERROR, (char *)&err, (rb_socklen_t *) & len)) { if(err) errtmp = err; @@ -204,7 +193,7 @@ rb_get_sockerr(rb_fde_t *F) int rb_getmaxconnect(void) { - return(rb_maxconnections); + return (rb_maxconnections); } /* @@ -221,8 +210,8 @@ rb_set_buffers(rb_fde_t *F, int size) if(F == NULL) return 0; if(setsockopt - (F->fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) - || setsockopt(F->fd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size))) + (F->fd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) + || setsockopt(F->fd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) return 0; return 1; } @@ -244,7 +233,7 @@ rb_set_nb(rb_fde_t *F) if(F == NULL) return 0; fd = F->fd; - + if((res = rb_setup_fd(F))) return res; #ifdef O_NONBLOCK @@ -277,14 +266,14 @@ rb_settimeout(rb_fde_t *F, time_t timeout, PF * callback, void *cbdata) lrb_assert(IsFDOpen(F)); td = F->timeout; - if(callback == NULL) /* user wants to remove */ + if(callback == NULL) /* user wants to remove */ { if(td == NULL) return; rb_dlinkDelete(&td->node, &timeout_list); rb_free(td); F->timeout = NULL; - if(rb_dlink_list_length(&timeout_list) == 0) + if(rb_dlink_list_length(&timeout_list) == 0) { rb_event_delete(rb_timeout_ev); rb_timeout_ev = NULL; @@ -293,8 +282,8 @@ rb_settimeout(rb_fde_t *F, time_t timeout, PF * callback, void *cbdata) } if(F->timeout == NULL) - td = F->timeout = rb_malloc(sizeof(struct timeout_data)); - + td = F->timeout = rb_malloc(sizeof(struct timeout_data)); + td->F = F; td->timeout = rb_current_time() + timeout; td->timeout_handler = callback; @@ -302,7 +291,7 @@ rb_settimeout(rb_fde_t *F, time_t timeout, PF * callback, void *cbdata) rb_dlinkAdd(td, &td->node, &timeout_list); if(rb_timeout_ev == NULL) { - rb_timeout_ev = rb_event_add("rb_checktimeouts", rb_checktimeouts, NULL, 5); + rb_timeout_ev = rb_event_add("rb_checktimeouts", rb_checktimeouts, NULL, 5); } } @@ -337,7 +326,7 @@ rb_checktimeouts(void *notused) F->timeout = NULL; rb_free(td); hdl(F, data); - } + } } } @@ -365,7 +354,9 @@ rb_accept_tryaccept(rb_fde_t *F, void *data) if(new_F == NULL) { - rb_lib_log("rb_accept: new_F == NULL on incoming connection. Closing new_fd == %d\n", new_fd); + rb_lib_log + ("rb_accept: new_F == NULL on incoming connection. Closing new_fd == %d\n", + new_fd); close(new_fd); continue; } @@ -380,21 +371,22 @@ rb_accept_tryaccept(rb_fde_t *F, void *data) #ifdef RB_IPV6 mangle_mapped_sockaddr((struct sockaddr *)&st); #endif - + if(F->accept->precb != NULL) { - if(!F->accept->precb(new_F, (struct sockaddr *)&st, addrlen, F->accept->data)) /* pre-callback decided to drop it */ + if(!F->accept->precb(new_F, (struct sockaddr *)&st, addrlen, F->accept->data)) /* pre-callback decided to drop it */ continue; } #ifdef HAVE_SSL if(F->type & RB_FD_SSL) { rb_ssl_accept_setup(F, new_F, (struct sockaddr *)&st, addrlen); - } + } else -#endif /* HAVE_SSL */ +#endif /* HAVE_SSL */ { - F->accept->callback(new_F, RB_OK, (struct sockaddr *)&st, addrlen, F->accept->data); + F->accept->callback(new_F, RB_OK, (struct sockaddr *)&st, addrlen, + F->accept->data); } } @@ -402,7 +394,7 @@ rb_accept_tryaccept(rb_fde_t *F, void *data) /* try to accept a TCP connection */ void -rb_accept_tcp(rb_fde_t *F, ACPRE *precb, ACCB *callback, void *data) +rb_accept_tcp(rb_fde_t *F, ACPRE * precb, ACCB * callback, void *data) { if(F == NULL) return; @@ -430,11 +422,11 @@ rb_accept_tcp(rb_fde_t *F, ACPRE *precb, ACCB *callback, void *data) */ void rb_connect_tcp(rb_fde_t *F, struct sockaddr *dest, - struct sockaddr *clocal, int socklen, CNCB * callback, void *data, int timeout) + struct sockaddr *clocal, int socklen, CNCB * callback, void *data, int timeout) { if(F == NULL) return; - + lrb_assert(callback); F->connect = rb_malloc(sizeof(struct conndata)); F->connect->callback = callback; @@ -472,7 +464,7 @@ rb_connect_callback(rb_fde_t *F, int status) { CNCB *hdl; void *data; - int errtmp = errno; /* save errno as rb_settimeout clobbers it sometimes */ + int errtmp = errno; /* save errno as rb_settimeout clobbers it sometimes */ /* This check is gross..but probably necessary */ if(F == NULL || F->connect == NULL || F->connect->callback == NULL) @@ -485,7 +477,7 @@ rb_connect_callback(rb_fde_t *F, int status) /* Clear the timeout handler */ rb_settimeout(F, 0, NULL, NULL); - errno = errtmp; + errno = errtmp; /* Call the handler */ hdl(F, status, data); } @@ -520,7 +512,8 @@ rb_connect_tryconnect(rb_fde_t *F, void *notused) return; /* Try the connect() */ retval = connect(F->fd, - (struct sockaddr *) &F->connect->hostaddr, GET_SS_LEN(&F->connect->hostaddr)); + (struct sockaddr *)&F->connect->hostaddr, + GET_SS_LEN(&F->connect->hostaddr)); /* Error? */ if(retval < 0) { @@ -534,8 +527,7 @@ rb_connect_tryconnect(rb_fde_t *F, void *notused) rb_connect_callback(F, RB_OK); else if(rb_ignore_errno(errno)) /* Ignore error? Reschedule */ - rb_setselect(F, RB_SELECT_CONNECT, - rb_connect_tryconnect, NULL); + rb_setselect(F, RB_SELECT_CONNECT, rb_connect_tryconnect, NULL); else /* Error? Fail with RB_ERR_CONNECT */ rb_connect_callback(F, RB_ERR_CONNECT); @@ -578,10 +570,15 @@ rb_socketpair(int family, int sock_type, int proto, rb_fde_t **F1, rb_fde_t **F2 return -1; } -#ifndef WIN32 +#ifdef HAVE_SOCKETPAIR if(socketpair(family, sock_type, proto, nfd)) #else - if(rb_inet_socketpair(AF_INET, SOCK_STREAM, proto, nfd)) + if(sock_type == SOCK_DGRAM) + { + return rb_inet_socketpair_udp(F1, F2); + } + + if(rb_inet_socketpair(AF_INET, sock_type, proto, nfd)) #endif return -1; @@ -628,7 +625,7 @@ rb_socketpair(int family, int sock_type, int proto, rb_fde_t **F1, rb_fde_t **F2 int rb_pipe(rb_fde_t **F1, rb_fde_t **F2, const char *desc) { -#ifndef WIN32 +#ifndef _WIN32 int fd[2]; if(number_fd >= rb_maxconnections) { @@ -664,7 +661,7 @@ rb_pipe(rb_fde_t **F1, rb_fde_t **F2, const char *desc) /* Its not a pipe..but its selectable. I'll take dirty hacks * for $500 Alex. */ - return rb_socketpair(AF_INET, SOCK_STREAM, 0, F1, F2, desc); + return rb_socketpair(AF_INET, SOCK_STREAM, 0, F1, F2, desc); #endif } @@ -708,7 +705,7 @@ rb_socket(int family, int sock_type, int proto, const char *note) if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off)) == -1) { rb_lib_log("rb_socket: Could not set IPV6_V6ONLY option to 1 on FD %d: %s", - fd, strerror(errno)); + fd, strerror(errno)); close(fd); return NULL; } @@ -718,7 +715,8 @@ rb_socket(int family, int sock_type, int proto, const char *note) F = rb_open(fd, RB_FD_SOCKET, note); if(F == NULL) { - rb_lib_log("rb_socket: rb_open returns NULL on FD %d: %s, closing fd", fd, strerror(errno)); + rb_lib_log("rb_socket: rb_open returns NULL on FD %d: %s, closing fd", fd, + strerror(errno)); close(fd); return NULL; } @@ -741,7 +739,7 @@ rb_socket(int family, int sock_type, int proto, const char *note) static void mangle_mapped_sockaddr(struct sockaddr *in) { - struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) in; + struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)in; if(in->sa_family == AF_INET) return; @@ -752,7 +750,7 @@ mangle_mapped_sockaddr(struct sockaddr *in) memset(&in4, 0, sizeof(struct sockaddr_in)); in4.sin_family = AF_INET; in4.sin_port = in6->sin6_port; - in4.sin_addr.s_addr = ((uint32_t *) & in6->sin6_addr)[3]; + in4.sin_addr.s_addr = ((uint32_t *)&in6->sin6_addr)[3]; memcpy(in, &in4, sizeof(struct sockaddr_in)); } return; @@ -765,7 +763,7 @@ mangle_mapped_sockaddr(struct sockaddr *in) int rb_listen(rb_fde_t *F, int backlog) { - F->type = RB_FD_SOCKET|RB_FD_LISTEN; + F->type = RB_FD_SOCKET | RB_FD_LISTEN; /* Currently just a simple wrapper for the sake of being complete */ return listen(F->fd, backlog); } @@ -774,11 +772,11 @@ void rb_fdlist_init(int closeall, int maxfds, size_t heapsize) { static int initialized = 0; -#ifdef WIN32 +#ifdef _WIN32 WSADATA wsaData; int err; int vers = MAKEWORD(2, 0); - + err = WSAStartup(vers, &wsaData); if(err != 0) { @@ -816,7 +814,7 @@ rb_open(int fd, uint8_t type, const char *desc) fdesc = F->desc; else fdesc = "NULL"; - rb_lib_log("Trying to rb_open an already open FD: %d desc: %d", fd, fdesc); + rb_lib_log("Trying to rb_open an already open FD: %d desc: %s", fd, fdesc); return NULL; } F->fd = fd; @@ -835,12 +833,12 @@ void rb_close(rb_fde_t *F) { int type, fd; - + if(F == NULL) return; fd = F->fd; - type = F->type; + type = F->type; lrb_assert(IsFDOpen(F)); lrb_assert(!(type & RB_FD_FILE)); @@ -868,14 +866,15 @@ rb_close(rb_fde_t *F) number_fd--; -#ifdef WIN32 - if(type & (RB_FD_SOCKET|RB_FD_PIPE)) +#ifdef _WIN32 + if(type & (RB_FD_SOCKET | RB_FD_PIPE)) { closesocket(fd); return; - } else + } + else #endif - close(fd); + close(fd); } @@ -939,7 +938,7 @@ rb_get_type(rb_fde_t *F) return F->type; } -int +int rb_fd_ssl(rb_fde_t *F) { if(F == NULL) @@ -949,12 +948,12 @@ rb_fd_ssl(rb_fde_t *F) return 0; } -int +int rb_get_fd(rb_fde_t *F) { if(F == NULL) return -1; - return(F->fd); + return (F->fd); } rb_fde_t * @@ -969,7 +968,7 @@ rb_read(rb_fde_t *F, void *buf, int count) ssize_t ret; if(F == NULL) return 0; - + /* This needs to be *before* RB_FD_SOCKET otherwise you'll process * an SSL socket as a regular socket */ @@ -977,7 +976,7 @@ rb_read(rb_fde_t *F, void *buf, int count) if(F->type & RB_FD_SSL) { return rb_ssl_read(F, buf, count); - } + } #endif if(F->type & RB_FD_SOCKET) { @@ -1006,12 +1005,13 @@ rb_write(rb_fde_t *F, const void *buf, int count) if(F->type & RB_FD_SSL) { return rb_ssl_write(F, buf, count); - } + } #endif if(F->type & RB_FD_SOCKET) { ret = send(F->fd, buf, count, MSG_NOSIGNAL); - if(ret < 0) { + if(ret < 0) + { rb_get_errno(); } return ret; @@ -1026,11 +1026,11 @@ rb_fake_writev(rb_fde_t *F, const struct rb_iovec *vp, size_t vpcount) { ssize_t count = 0; - while (vpcount-- > 0) + while(vpcount-- > 0) { ssize_t written = rb_write(F, vp->iov_base, vp->iov_len); - if (written <= 0) + if(written <= 0) { if(count > 0) return count; @@ -1046,23 +1046,24 @@ rb_fake_writev(rb_fde_t *F, const struct rb_iovec *vp, size_t vpcount) #if defined(WIN32) || !defined(HAVE_WRITEV) ssize_t -rb_writev(rb_fde_t *F, struct rb_iovec *vecount, int count) +rb_writev(rb_fde_t *F, struct rb_iovec * vecount, int count) { return rb_fake_writev(F, vecount, count); } #else ssize_t -rb_writev(rb_fde_t *F, struct rb_iovec *vector, int count) +rb_writev(rb_fde_t *F, struct rb_iovec * vector, int count) { - if(F == NULL) { + if(F == NULL) + { errno = EBADF; return -1; } #ifdef HAVE_SSL if(F->type & RB_FD_SSL) { - return rb_fake_writev(F, vector, count); + return rb_fake_writev(F, vector, count); } #endif /* HAVE_SSL */ #ifdef HAVE_SENDMSG @@ -1073,12 +1074,12 @@ rb_writev(rb_fde_t *F, struct rb_iovec *vector, int count) msg.msg_iov = (struct iovec *)vector; msg.msg_iovlen = count; return sendmsg(F->fd, &msg, MSG_NOSIGNAL); - } + } #endif /* HAVE_SENDMSG */ return writev(F->fd, (struct iovec *)vector, count); } -#endif +#endif /* @@ -1129,23 +1130,23 @@ inetntoa(const char *in) { static char buf[16]; char *bufptr = buf; - const unsigned char *a = (const unsigned char *) in; + const unsigned char *a = (const unsigned char *)in; const char *n; n = IpQuadTab[*a++]; - while (*n) + while(*n) *bufptr++ = *n++; *bufptr++ = '.'; n = IpQuadTab[*a++]; - while (*n) + while(*n) *bufptr++ = *n++; *bufptr++ = '.'; n = IpQuadTab[*a++]; - while (*n) + while(*n) *bufptr++ = *n++; *bufptr++ = '.'; n = IpQuadTab[*a]; - while (*n) + while(*n) *bufptr++ = *n++; *bufptr = '\0'; return buf; @@ -1176,9 +1177,9 @@ inetntoa(const char *in) * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. */ -static const char *inet_ntop4(const unsigned char * src, char *dst, unsigned int size); +static const char *inet_ntop4(const unsigned char *src, char *dst, unsigned int size); #ifdef RB_IPV6 -static const char *inet_ntop6(const unsigned char * src, char *dst, unsigned int size); +static const char *inet_ntop6(const unsigned char *src, char *dst, unsigned int size); #endif /* const char * @@ -1197,7 +1198,7 @@ inet_ntop4(const unsigned char *src, char *dst, unsigned int size) { if(size < 16) return NULL; - return strcpy(dst, inetntoa((const char *) src)); + return strcpy(dst, inetntoa((const char *)src)); } /* const char * @@ -1232,13 +1233,13 @@ inet_ntop6(const unsigned char *src, char *dst, unsigned int size) * Find the longest run of 0x00's in src[] for :: shorthanding. */ memset(words, '\0', sizeof words); - for (i = 0; i < IN6ADDRSZ; i += 2) + for(i = 0; i < IN6ADDRSZ; i += 2) words[i / 2] = (src[i] << 8) | src[i + 1]; best.base = -1; best.len = 0; cur.base = -1; cur.len = 0; - for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) + for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) { if(words[i] == 0) { @@ -1269,7 +1270,7 @@ inet_ntop6(const unsigned char *src, char *dst, unsigned int size) * Format the result. */ tp = tmp; - for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) + for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) { /* Are we inside the best run of 0x00's? */ if(best.base != -1 && i >= best.base && i < (best.base + best.len)) @@ -1305,7 +1306,7 @@ inet_ntop6(const unsigned char *src, char *dst, unsigned int size) * Check for overflow, copy, and we're done. */ - if((unsigned int) (tp - tmp) > size) + if((unsigned int)(tp - tmp) > size) { return (NULL); } @@ -1316,18 +1317,18 @@ inet_ntop6(const unsigned char *src, char *dst, unsigned int size) int rb_inet_pton_sock(const char *src, struct sockaddr *dst) { - if(rb_inet_pton(AF_INET, src, &((struct sockaddr_in *) dst)->sin_addr)) + if(rb_inet_pton(AF_INET, src, &((struct sockaddr_in *)dst)->sin_addr)) { - ((struct sockaddr_in *) dst)->sin_port = 0; - ((struct sockaddr_in *) dst)->sin_family = AF_INET; + ((struct sockaddr_in *)dst)->sin_port = 0; + ((struct sockaddr_in *)dst)->sin_family = AF_INET; SET_SS_LEN(dst, sizeof(struct sockaddr_in)); return 1; } #ifdef RB_IPV6 - else if(rb_inet_pton(AF_INET6, src, &((struct sockaddr_in6 *) dst)->sin6_addr)) + else if(rb_inet_pton(AF_INET6, src, &((struct sockaddr_in6 *)dst)->sin6_addr)) { - ((struct sockaddr_in6 *) dst)->sin6_port = 0; - ((struct sockaddr_in6 *) dst)->sin6_family = AF_INET6; + ((struct sockaddr_in6 *)dst)->sin6_port = 0; + ((struct sockaddr_in6 *)dst)->sin6_family = AF_INET6; SET_SS_LEN(dst, sizeof(struct sockaddr_in6)); return 1; } @@ -1341,11 +1342,12 @@ rb_inet_ntop_sock(struct sockaddr *src, char *dst, unsigned int size) switch (src->sa_family) { case AF_INET: - return (rb_inet_ntop(AF_INET, &((struct sockaddr_in *) src)->sin_addr, dst, size)); + return (rb_inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, dst, size)); break; #ifdef RB_IPV6 case AF_INET6: - return (rb_inet_ntop(AF_INET6, &((struct sockaddr_in6 *) src)->sin6_addr, dst, size)); + return (rb_inet_ntop + (AF_INET6, &((struct sockaddr_in6 *)src)->sin6_addr, dst, size)); break; #endif default: @@ -1371,11 +1373,11 @@ rb_inet_ntop(int af, const void *src, char *dst, unsigned int size) return (inet_ntop4(src, dst, size)); #ifdef RB_IPV6 case AF_INET6: - if(IN6_IS_ADDR_V4MAPPED((const struct in6_addr *) src) || - IN6_IS_ADDR_V4COMPAT((const struct in6_addr *) src)) + if(IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)src) || + IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)src)) return (inet_ntop4 - ((const unsigned char *) - &((const struct in6_addr *) src)->s6_addr[12], dst, size)); + ((const unsigned char *)&((const struct in6_addr *)src)-> + s6_addr[12], dst, size)); else return (inet_ntop6(src, dst, size)); @@ -1423,7 +1425,7 @@ inet_pton4(const char *src, unsigned char *dst) saw_digit = 0; octets = 0; *(tp = tmp) = 0; - while ((ch = *src++) != '\0') + while((ch = *src++) != '\0') { if(ch >= '0' && ch <= '9') @@ -1490,7 +1492,7 @@ inet_pton6(const char *src, unsigned char *dst) curtok = src; saw_xdigit = 0; val = 0; - while ((ch = tolower(*src++)) != '\0') + while((ch = tolower(*src++)) != '\0') { const char *pch; @@ -1520,8 +1522,8 @@ inet_pton6(const char *src, unsigned char *dst) } if(tp + INT16SZ > endp) return (0); - *tp++ = (unsigned char) (val >> 8) & 0xff; - *tp++ = (unsigned char) val & 0xff; + *tp++ = (unsigned char)(val >> 8) & 0xff; + *tp++ = (unsigned char)val & 0xff; saw_xdigit = 0; val = 0; continue; @@ -1543,8 +1545,8 @@ inet_pton6(const char *src, unsigned char *dst) { if(tp + INT16SZ > endp) return (0); - *tp++ = (unsigned char) (val >> 8) & 0xff; - *tp++ = (unsigned char) val & 0xff; + *tp++ = (unsigned char)(val >> 8) & 0xff; + *tp++ = (unsigned char)val & 0xff; } if(colonp != NULL) { @@ -1557,7 +1559,7 @@ inet_pton6(const char *src, unsigned char *dst) if(tp == endp) return (0); - for (i = 1; i <= n; i++) + for(i = 1; i <= n; i++) { endp[-i] = colonp[n - i]; colonp[n - i] = 0; @@ -1597,6 +1599,117 @@ rb_inet_pton(int af, const char *src, void *dst) #ifndef HAVE_SOCKETPAIR + +/* mostly based on perl's emulation of socketpair udp */ +static int +rb_inet_socketpair_udp(rb_fde_t **newF1, rb_fde_t **newF2) +{ + struct sockaddr_in addr[2]; + rb_socklen_t size = sizeof(struct sockaddr_in); + rb_fde_t *F[2]; + unsigned int fd[2]; + int i, got; + unsigned short port; + + memset(&addr, 0, sizeof(addr)); + + for(i = 0; i < 2; i++) + { + F[i] = rb_socket(AF_INET, SOCK_DGRAM, 0, "udp socketpair"); + if(F[i] == NULL) + goto failed; + addr[i].sin_family = AF_INET; + addr[i].sin_addr.s_addr = htonl(INADDR_LOOPBACK); + addr[i].sin_port = 0; + if(bind(rb_get_fd(F[i]), (struct sockaddr *)&addr[i], sizeof(struct sockaddr_in))) + goto failed; + fd[i] = rb_get_fd(F[i]); + } + + for(i = 0; i < 2; i++) + { + if(getsockname(fd[i], (struct sockaddr *)&addr[i], &size)) + goto failed; + if(size != sizeof(struct sockaddr_in)) + goto failed; + if(connect(fd[!i], (struct sockaddr *)&addr[i], sizeof(struct sockaddr_in)) == -1) + goto failed; + } + + for(i = 0; i < 2; i++) + { + port = addr[i].sin_port; + got = rb_write(F[i], &port, sizeof(port)); + if(got != sizeof(port)) + { + if(got == -1) + goto failed; + goto abort_failed; + } + } + + + struct timeval wait = { 0, 100000 }; + + int max = fd[1] > fd[0] ? fd[1] : fd[0]; + fd_set rset; + FD_ZERO(&rset); + FD_SET(fd[0], &rset); + FD_SET(fd[1], &rset); + got = select(max + 1, &rset, NULL, NULL, &wait); + if(got != 2 || !FD_ISSET(fd[0], &rset) || !FD_ISSET(fd[1], &rset)) + { + if(got == -1) + goto failed; + goto abort_failed; + } + + struct sockaddr_in readfrom; + unsigned short buf[2]; + for(i = 0; i < 2; i++) + { +#ifdef MSG_DONTWAIT + int flag = MSG_DONTWAIT +#else + int flag = 0; +#endif + got = recvfrom(rb_get_fd(F[i]), (char *)&buf, sizeof(buf), flag, + (struct sockaddr *)&readfrom, &size); + if(got == -1) + goto failed; + if(got != sizeof(port) + || size != sizeof(struct sockaddr_in) + || buf[0] != (unsigned short)addr[!i].sin_port + || readfrom.sin_family != addr[!i].sin_family + || readfrom.sin_addr.s_addr != addr[!i].sin_addr.s_addr + || readfrom.sin_port != addr[!i].sin_port) + goto abort_failed; + } + + *newF1 = F[0]; + *newF2 = F[1]; + return 0; + +#ifdef _WIN32 +#define ECONNABORTED WSAECONNABORTED +#endif + + abort_failed: + rb_get_errno(); + errno = ECONNABORTED; + failed: + if(errno != ECONNABORTED) + rb_get_errno(); + int o_errno = errno; + if(F[0] != NULL) + rb_close(F[0]); + if(F[1] != NULL) + rb_close(F[1]); + errno = o_errno; + return -1; +} + + int rb_inet_socketpair(int family, int type, int protocol, int fd[2]) { @@ -1605,7 +1718,7 @@ rb_inet_socketpair(int family, int type, int protocol, int fd[2]) int acceptor = -1; struct sockaddr_in listen_addr; struct sockaddr_in connect_addr; - size_t size; + rb_socklen_t size; if(protocol || family != AF_INET) { @@ -1625,7 +1738,7 @@ rb_inet_socketpair(int family, int type, int protocol, int fd[2]) listen_addr.sin_family = AF_INET; listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); listen_addr.sin_port = 0; /* kernel choses port. */ - if(bind(listener, (struct sockaddr *) &listen_addr, sizeof(listen_addr)) == -1) + if(bind(listener, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) == -1) goto tidy_up_and_fail; if(listen(listener, 1) == -1) goto tidy_up_and_fail; @@ -1635,15 +1748,15 @@ rb_inet_socketpair(int family, int type, int protocol, int fd[2]) goto tidy_up_and_fail; /* We want to find out the port number to connect to. */ size = sizeof(connect_addr); - if(getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1) + if(getsockname(listener, (struct sockaddr *)&connect_addr, &size) == -1) goto tidy_up_and_fail; if(size != sizeof(connect_addr)) goto abort_tidy_up_and_fail; - if(connect(connector, (struct sockaddr *) &connect_addr, sizeof(connect_addr)) == -1) + if(connect(connector, (struct sockaddr *)&connect_addr, sizeof(connect_addr)) == -1) goto tidy_up_and_fail; size = sizeof(listen_addr); - acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size); + acceptor = accept(listener, (struct sockaddr *)&listen_addr, &size); if(acceptor == -1) goto tidy_up_and_fail; if(size != sizeof(listen_addr)) @@ -1651,7 +1764,7 @@ rb_inet_socketpair(int family, int type, int protocol, int fd[2]) close(listener); /* Now check we are talking to ourself by matching port and host on the two sockets. */ - if(getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1) + if(getsockname(connector, (struct sockaddr *)&connect_addr, &size) == -1) goto tidy_up_and_fail; if(size != sizeof(connect_addr) || listen_addr.sin_family != connect_addr.sin_family @@ -1720,7 +1833,7 @@ try_kqueue(void) rb_strlcpy(iotype, "kqueue", sizeof(iotype)); return 0; } - return -1; + return -1; } static int @@ -1748,7 +1861,7 @@ try_ports(void) { setselect_handler = rb_setselect_ports; select_handler = rb_select_ports; - setup_fd_handler = rb_setup_fd_ports; + setup_fd_handler = rb_setup_fd_ports; io_sched_event = NULL; io_unsched_event = NULL; io_init_event = NULL; @@ -1766,7 +1879,7 @@ try_devpoll(void) { setselect_handler = rb_setselect_devpoll; select_handler = rb_select_devpoll; - setup_fd_handler = rb_setup_fd_devpoll; + setup_fd_handler = rb_setup_fd_devpoll; io_sched_event = NULL; io_unsched_event = NULL; io_init_event = NULL; @@ -1784,26 +1897,26 @@ try_sigio(void) { setselect_handler = rb_setselect_sigio; select_handler = rb_select_sigio; - setup_fd_handler = rb_setup_fd_sigio; + setup_fd_handler = rb_setup_fd_sigio; io_sched_event = rb_sigio_sched_event; - io_unsched_event = rb_sigio_unsched_event; - io_supports_event = rb_sigio_supports_event; - io_init_event = rb_sigio_init_event; - + io_unsched_event = rb_sigio_unsched_event; + io_supports_event = rb_sigio_supports_event; + io_init_event = rb_sigio_init_event; + rb_strlcpy(iotype, "sigio", sizeof(iotype)); return 0; } return -1; } -static int +static int try_poll(void) { if(!rb_init_netio_poll()) { setselect_handler = rb_setselect_poll; select_handler = rb_select_poll; - setup_fd_handler = rb_setup_fd_poll; + setup_fd_handler = rb_setup_fd_poll; io_sched_event = NULL; io_unsched_event = NULL; io_init_event = NULL; @@ -1821,7 +1934,7 @@ try_win32(void) { setselect_handler = rb_setselect_win32; select_handler = rb_select_win32; - setup_fd_handler = rb_setup_fd_win32; + setup_fd_handler = rb_setup_fd_win32; io_sched_event = NULL; io_unsched_event = NULL; io_init_event = NULL; @@ -1834,12 +1947,12 @@ try_win32(void) static int try_select(void) -{ +{ if(!rb_init_netio_select()) { setselect_handler = rb_setselect_select; select_handler = rb_select_select; - setup_fd_handler = rb_setup_fd_select; + setup_fd_handler = rb_setup_fd_select; io_sched_event = NULL; io_unsched_event = NULL; io_init_event = NULL; @@ -1854,7 +1967,8 @@ try_select(void) int rb_io_sched_event(struct ev_entry *ev, int when) { - if(ev == NULL || io_supports_event == NULL || io_sched_event == NULL || !io_supports_event()) + if(ev == NULL || io_supports_event == NULL || io_sched_event == NULL + || !io_supports_event()) return 0; return io_sched_event(ev, when); } @@ -1862,10 +1976,12 @@ rb_io_sched_event(struct ev_entry *ev, int when) void rb_io_unsched_event(struct ev_entry *ev) { - if(ev == NULL || io_supports_event == NULL || io_unsched_event == NULL || !io_supports_event()) + if(ev == NULL || io_supports_event == NULL || io_unsched_event == NULL + || !io_supports_event()) return; - io_unsched_event(ev); + io_unsched_event(ev); } + int rb_io_supports_event(void) { @@ -1887,50 +2003,50 @@ rb_init_netio(void) char *ioenv = getenv("LIBRB_USE_IOTYPE"); rb_fd_table = rb_malloc(RB_FD_HASH_SIZE * sizeof(rb_dlink_list)); rb_init_ssl(); - + if(ioenv != NULL) { if(!strcmp("epoll", ioenv)) { if(!try_epoll()) return; - } else - if(!strcmp("kqueue", ioenv)) + } + else if(!strcmp("kqueue", ioenv)) { if(!try_kqueue()) return; - } else - if(!strcmp("ports", ioenv)) + } + else if(!strcmp("ports", ioenv)) { if(!try_ports()) return; - } else - if(!strcmp("poll", ioenv)) + } + else if(!strcmp("poll", ioenv)) { if(!try_poll()) return; - } else - if(!strcmp("devpoll", ioenv)) + } + else if(!strcmp("devpoll", ioenv)) { if(!try_devpoll()) return; - } else - if(!strcmp("sigio", ioenv)) + } + else if(!strcmp("sigio", ioenv)) { if(!try_sigio()) return; - } else - if(!strcmp("win32", ioenv)) + } + else if(!strcmp("select", ioenv)) { - if(!try_win32()) + if(!try_select()) return; } - if(!strcmp("select", ioenv)) + if(!strcmp("win32", ioenv)) { - if(!try_select()) + if(!try_win32()) return; } - + } if(!try_kqueue()) @@ -1955,13 +2071,13 @@ rb_init_netio(void) abort(); } -void +void rb_setselect(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { setselect_handler(F, type, handler, client_data); } -int +int rb_select(unsigned long timeout) { int ret = select_handler(timeout); @@ -1969,7 +2085,7 @@ rb_select(unsigned long timeout) return ret; } -int +int rb_setup_fd(rb_fde_t *F) { return setup_fd_handler(F); @@ -1980,34 +2096,35 @@ rb_setup_fd(rb_fde_t *F) int rb_ignore_errno(int error) { - switch(error) + switch (error) { #ifdef EINPROGRESS - case EINPROGRESS: + case EINPROGRESS: #endif #if defined EWOULDBLOCK - case EWOULDBLOCK: + case EWOULDBLOCK: #endif #if defined(EAGAIN) && (EWOULDBLOCK != EAGAIN) - case EAGAIN: + case EAGAIN: #endif #ifdef EINTR - case EINTR: + case EINTR: #endif #ifdef ERESTART - case ERESTART: + case ERESTART: #endif #ifdef ENOBUFS - case ENOBUFS: -#endif - return 1; - default: - break; - } + case ENOBUFS: +#endif + return 1; + default: + break; + } return 0; } +#if defined(HAVE_SENDMSG) && !defined(WIN32) int rb_recv_fd_buf(rb_fde_t *F, void *data, size_t datasize, rb_fde_t **xF, int nfds) { @@ -2023,7 +2140,7 @@ rb_recv_fd_buf(rb_fde_t *F, void *data, size_t datasize, rb_fde_t **xF, int nfds iov[0].iov_base = data; iov[0].iov_len = datasize; - + msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_iov = iov; @@ -2036,7 +2153,8 @@ rb_recv_fd_buf(rb_fde_t *F, void *data, size_t datasize, rb_fde_t **xF, int nfds if((len = recvmsg(rb_get_fd(F), &msg, 0)) <= 0) return len; - if(msg.msg_controllen > 0 && msg.msg_control != NULL && (cmsg = CMSG_FIRSTHDR(&msg)) != NULL) + if(msg.msg_controllen > 0 && msg.msg_control != NULL + && (cmsg = CMSG_FIRSTHDR(&msg)) != NULL) { rfds = (msg.msg_controllen - sizeof(struct cmsghdr)) / sizeof(int); @@ -2065,14 +2183,15 @@ rb_recv_fd_buf(rb_fde_t *F, void *data, size_t datasize, rb_fde_t **xF, int nfds } xF[x] = rb_open(fd, stype, desc); } - } else + } + else *xF = NULL; - return len; + return len; } int -rb_send_fd_buf(rb_fde_t *xF, rb_fde_t **F, int count, void *data, size_t datasize) +rb_send_fd_buf(rb_fde_t *xF, rb_fde_t **F, int count, void *data, size_t datasize, pid_t pid) { int n; struct msghdr msg; @@ -2085,8 +2204,10 @@ rb_send_fd_buf(rb_fde_t *xF, rb_fde_t **F, int count, void *data, size_t datasiz if(datasize == 0) { iov[0].iov_base = ∅ - iov[0].iov_len = 1; - } else { + iov[0].iov_len = 1; + } + else + { iov[0].iov_base = data; iov[0].iov_len = datasize; } @@ -2112,12 +2233,28 @@ rb_send_fd_buf(rb_fde_t *xF, rb_fde_t **F, int count, void *data, size_t datasiz cmsg->cmsg_len = CMSG_LEN(sizeof(int) * count); for(i = 0; i < count; i++) - { - ((int *)CMSG_DATA(cmsg))[i] = rb_get_fd(F[i]); + { + ((int *)CMSG_DATA(cmsg))[i] = rb_get_fd(F[i]); } msg.msg_controllen = cmsg->cmsg_len; } n = sendmsg(rb_get_fd(xF), &msg, MSG_NOSIGNAL); return n; } +#else +#ifndef _WIN32 +int +rb_recv_fd_buf(rb_fde_t *F, void *data, size_t datasize, rb_fde_t **xF, int nfds) +{ + errno = ENOSYS; + return -1; +} +int +rb_send_fd_buf(rb_fde_t *xF, rb_fde_t **F, int count, void *data, size_t datasize, pid_t pid) +{ + errno = ENOSYS; + return -1; +} +#endif +#endif diff --git a/libratbox/src/crypt.c b/libratbox/src/crypt.c index 58e054b..c6d7135 100644 --- a/libratbox/src/crypt.c +++ b/libratbox/src/crypt.c @@ -36,19 +36,19 @@ extern char *crypt(const char *key, const char *salt); char * rb_crypt(const char *key, const char *salt) { - return(crypt(key, salt)); + return (crypt(key, salt)); } #else -static char * __md5_crypt( const char *pw, const char *salt); -static char * __des_crypt( const char *pw, const char *salt); +static char *__md5_crypt(const char *pw, const char *salt); +static char *__des_crypt(const char *pw, const char *salt); -char * +char * rb_crypt(const char *key, const char *salt) { /* First, check if we are supposed to be using the MD5 replacement * instead of DES... */ - if (salt[0]=='$' && salt[1]=='1' && salt[2]=='$') + if(salt[0] == '$' && salt[1] == '1' && salt[2] == '$') return __md5_crypt(key, salt); else return __des_crypt(key, salt); @@ -117,10 +117,10 @@ rb_crypt(const char *key, const char *salt) /* Re-entrantify me -- all this junk needs to be in * struct crypt_data to make this really reentrant... */ -static u_char inv_key_perm[64]; -static u_char inv_comp_perm[56]; -static u_char u_sbox[8][64]; -static u_char un_pbox[32]; +static uint8_t inv_key_perm[64]; +static uint8_t inv_comp_perm[56]; +static uint8_t u_sbox[8][64]; +static uint8_t un_pbox[32]; static uint32_t en_keysl[16], en_keysr[16]; static uint32_t de_keysl[16], de_keysr[16]; static uint32_t ip_maskl[8][256], ip_maskr[8][256]; @@ -135,37 +135,37 @@ static uint32_t old_rawkey0, old_rawkey1; /* Static stuff that stays resident and doesn't change after * being initialized, and therefore doesn't need to be made * reentrant. */ -static u_char init_perm[64], final_perm[64]; -static u_char m_sbox[4][4096]; +static uint8_t init_perm[64], final_perm[64]; +static uint8_t m_sbox[4][4096]; static uint32_t psbox[4][256]; /* A pile of data */ -static const u_char ascii64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; +static const uint8_t ascii64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; -static const u_char IP[64] = { - 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, - 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, - 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, - 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 +static const uint8_t IP[64] = { + 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, + 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, + 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, + 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 }; -static const u_char key_perm[56] = { - 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, - 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, - 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, - 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 +static const uint8_t key_perm[56] = { + 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, + 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, + 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, + 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 }; -static const u_char key_shifts[16] = { +static const uint8_t key_shifts[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; -static const u_char comp_perm[48] = { - 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, - 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, +static const uint8_t comp_perm[48] = { + 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, + 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 }; @@ -174,64 +174,55 @@ static const u_char comp_perm[48] = { * No E box is used, as it's replaced by some ANDs, shifts, and ORs. */ -static const u_char sbox[8][64] = { +static const uint8_t sbox[8][64] = { { - 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, - 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, - 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, - 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 - }, + 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, + 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, + 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, + 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}, { - 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, - 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, - 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, - 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 - }, + 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, + 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, + 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, + 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}, { - 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, - 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, - 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, - 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 - }, + 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, + 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, + 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, + 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}, { - 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, - 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, - 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, - 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 - }, + 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, + 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, + 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, + 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}, { - 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, - 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, - 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, - 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 - }, + 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, + 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, + 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, + 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}, { - 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, - 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, - 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, - 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 - }, + 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, + 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, + 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, + 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}, { - 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, - 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, - 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, - 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 - }, + 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, + 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, + 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, + 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}, { - 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, - 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, - 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, - 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 - } + 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, + 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, + 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, + 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11} }; -static const u_char pbox[32] = { - 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, - 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 +static const uint8_t pbox[32] = { + 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, + 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 }; -static const uint32_t bits32[32] = -{ +static const uint32_t bits32[32] = { 0x80000000, 0x40000000, 0x20000000, 0x10000000, 0x08000000, 0x04000000, 0x02000000, 0x01000000, 0x00800000, 0x00400000, 0x00200000, 0x00100000, @@ -242,37 +233,38 @@ static const uint32_t bits32[32] = 0x00000008, 0x00000004, 0x00000002, 0x00000001 }; -static const u_char bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; +static const uint8_t bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; + static const uint32_t *bits28, *bits24; -static int +static int ascii_to_bin(char ch) { - if (ch > 'z') - return(0); - if (ch >= 'a') - return(ch - 'a' + 38); - if (ch > 'Z') - return(0); - if (ch >= 'A') - return(ch - 'A' + 12); - if (ch > '9') - return(0); - if (ch >= '.') - return(ch - '.'); - return(0); + if(ch > 'z') + return (0); + if(ch >= 'a') + return (ch - 'a' + 38); + if(ch > 'Z') + return (0); + if(ch >= 'A') + return (ch - 'A' + 12); + if(ch > '9') + return (0); + if(ch >= '.') + return (ch - '.'); + return (0); } static void des_init(void) { - int i, j, b, k, inbit, obit; - uint32_t *p, *il, *ir, *fl, *fr; + int i, j, b, k, inbit, obit; + uint32_t *p, *il, *ir, *fl, *fr; static int des_initialised = 0; - if (des_initialised==1) - return; + if(des_initialised == 1) + return; old_rawkey0 = old_rawkey1 = 0L; saltbits = 0L; @@ -282,8 +274,9 @@ des_init(void) /* * Invert the S-boxes, reordering the input bits. */ - for (i = 0; i < 8; i++) - for (j = 0; j < 64; j++) { + for(i = 0; i < 8; i++) + for(j = 0; j < 64; j++) + { b = (j & 0x20) | ((j & 1) << 4) | ((j >> 1) & 0xf); u_sbox[i][j] = sbox[i][b]; } @@ -292,19 +285,20 @@ des_init(void) * Convert the inverted S-boxes into 4 arrays of 8 bits. * Each will handle 12 bits of the S-box input. */ - for (b = 0; b < 4; b++) - for (i = 0; i < 64; i++) - for (j = 0; j < 64; j++) + for(b = 0; b < 4; b++) + for(i = 0; i < 64; i++) + for(j = 0; j < 64; j++) m_sbox[b][(i << 6) | j] = - (u_char)((u_sbox[(b << 1)][i] << 4) | - u_sbox[(b << 1) + 1][j]); + (uint8_t)((u_sbox[(b << 1)][i] << 4) | + u_sbox[(b << 1) + 1][j]); /* * Set up the initial & final permutations into a useful form, and * initialise the inverted key permutation. */ - for (i = 0; i < 64; i++) { - init_perm[final_perm[i] = IP[i] - 1] = (u_char)i; + for(i = 0; i < 64; i++) + { + init_perm[final_perm[i] = IP[i] - 1] = (uint8_t)i; inv_key_perm[i] = 255; } @@ -312,51 +306,60 @@ des_init(void) * Invert the key permutation and initialise the inverted key * compression permutation. */ - for (i = 0; i < 56; i++) { - inv_key_perm[key_perm[i] - 1] = (u_char)i; + for(i = 0; i < 56; i++) + { + inv_key_perm[key_perm[i] - 1] = (uint8_t)i; inv_comp_perm[i] = 255; } /* * Invert the key compression permutation. */ - for (i = 0; i < 48; i++) { - inv_comp_perm[comp_perm[i] - 1] = (u_char)i; + for(i = 0; i < 48; i++) + { + inv_comp_perm[comp_perm[i] - 1] = (uint8_t)i; } /* * Set up the OR-mask arrays for the initial and final permutations, * and for the key initial and compression permutations. */ - for (k = 0; k < 8; k++) { - for (i = 0; i < 256; i++) { + for(k = 0; k < 8; k++) + { + for(i = 0; i < 256; i++) + { *(il = &ip_maskl[k][i]) = 0L; *(ir = &ip_maskr[k][i]) = 0L; *(fl = &fp_maskl[k][i]) = 0L; *(fr = &fp_maskr[k][i]) = 0L; - for (j = 0; j < 8; j++) { + for(j = 0; j < 8; j++) + { inbit = 8 * k + j; - if (i & bits8[j]) { - if ((obit = init_perm[inbit]) < 32) + if(i & bits8[j]) + { + if((obit = init_perm[inbit]) < 32) *il |= bits32[obit]; else - *ir |= bits32[obit-32]; - if ((obit = final_perm[inbit]) < 32) + *ir |= bits32[obit - 32]; + if((obit = final_perm[inbit]) < 32) *fl |= bits32[obit]; else *fr |= bits32[obit - 32]; } } } - for (i = 0; i < 128; i++) { + for(i = 0; i < 128; i++) + { *(il = &key_perm_maskl[k][i]) = 0L; *(ir = &key_perm_maskr[k][i]) = 0L; - for (j = 0; j < 7; j++) { + for(j = 0; j < 7; j++) + { inbit = 8 * k + j; - if (i & bits8[j + 1]) { - if ((obit = inv_key_perm[inbit]) == 255) + if(i & bits8[j + 1]) + { + if((obit = inv_key_perm[inbit]) == 255) continue; - if (obit < 28) + if(obit < 28) *il |= bits28[obit]; else *ir |= bits28[obit - 28]; @@ -364,12 +367,14 @@ des_init(void) } *(il = &comp_maskl[k][i]) = 0L; *(ir = &comp_maskr[k][i]) = 0L; - for (j = 0; j < 7; j++) { + for(j = 0; j < 7; j++) + { inbit = 7 * k + j; - if (i & bits8[j + 1]) { - if ((obit=inv_comp_perm[inbit]) == 255) + if(i & bits8[j + 1]) + { + if((obit = inv_comp_perm[inbit]) == 255) continue; - if (obit < 24) + if(obit < 24) *il |= bits24[obit]; else *ir |= bits24[obit - 24]; @@ -382,14 +387,16 @@ des_init(void) * Invert the P-box permutation, and convert into OR-masks for * handling the output of the S-box arrays setup above. */ - for (i = 0; i < 32; i++) - un_pbox[pbox[i] - 1] = (u_char)i; + for(i = 0; i < 32; i++) + un_pbox[pbox[i] - 1] = (uint8_t)i; - for (b = 0; b < 4; b++) - for (i = 0; i < 256; i++) { + for(b = 0; b < 4; b++) + for(i = 0; i < 256; i++) + { *(p = &psbox[b][i]) = 0L; - for (j = 0; j < 8; j++) { - if (i & bits8[j]) + for(j = 0; j < 8; j++) + { + if(i & bits8[j]) *p |= bits32[un_pbox[8 * b + j]]; } } @@ -401,18 +408,19 @@ des_init(void) static void setup_salt(long salt) { - uint32_t obit, saltbit; - int i; + uint32_t obit, saltbit; + int i; - if (salt == (long)old_salt) + if(salt == (long)old_salt) return; old_salt = salt; saltbits = 0L; saltbit = 1; obit = 0x800000; - for (i = 0; i < 24; i++) { - if (salt & saltbit) + for(i = 0; i < 24; i++) + { + if(salt & saltbit) saltbits |= obit; saltbit <<= 1; obit >>= 1; @@ -423,53 +431,53 @@ setup_salt(long salt) static int des_setkey(const char *key) { - uint32_t k0, k1, rawkey0, rawkey1; - int shifts, round; + uint32_t k0, k1, rawkey0, rawkey1; + int shifts, round; des_init(); - rawkey0 = ntohl(*(const uint32_t *) key); - rawkey1 = ntohl(*(const uint32_t *) (key + 4)); + rawkey0 = ntohl(*(const uint32_t *)key); + rawkey1 = ntohl(*(const uint32_t *)(key + 4)); - if ((rawkey0 | rawkey1) - && rawkey0 == old_rawkey0 - && rawkey1 == old_rawkey1) { + if((rawkey0 | rawkey1) && rawkey0 == old_rawkey0 && rawkey1 == old_rawkey1) + { /* * Already setup for this key. * This optimisation fails on a zero key (which is weak and * has bad parity anyway) in order to simplify the starting * conditions. */ - return(0); + return (0); } old_rawkey0 = rawkey0; old_rawkey1 = rawkey1; /* - * Do key permutation and split into two 28-bit subkeys. + * Do key permutation and split into two 28-bit subkeys. */ k0 = key_perm_maskl[0][rawkey0 >> 25] - | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f] - | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f] - | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f] - | key_perm_maskl[4][rawkey1 >> 25] - | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f] - | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f] - | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f]; + | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f] + | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f] + | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f] + | key_perm_maskl[4][rawkey1 >> 25] + | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f] + | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f] + | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f]; k1 = key_perm_maskr[0][rawkey0 >> 25] - | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f] - | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f] - | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f] - | key_perm_maskr[4][rawkey1 >> 25] - | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f] - | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f] - | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f]; + | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f] + | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f] + | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f] + | key_perm_maskr[4][rawkey1 >> 25] + | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f] + | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f] + | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f]; /* - * Rotate subkeys and do compression permutation. + * Rotate subkeys and do compression permutation. */ shifts = 0; - for (round = 0; round < 16; round++) { - uint32_t t0, t1; + for(round = 0; round < 16; round++) + { + uint32_t t0, t1; shifts += key_shifts[round]; @@ -477,48 +485,51 @@ des_setkey(const char *key) t1 = (k1 << shifts) | (k1 >> (28 - shifts)); de_keysl[15 - round] = - en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f] - | comp_maskl[1][(t0 >> 14) & 0x7f] - | comp_maskl[2][(t0 >> 7) & 0x7f] - | comp_maskl[3][t0 & 0x7f] - | comp_maskl[4][(t1 >> 21) & 0x7f] - | comp_maskl[5][(t1 >> 14) & 0x7f] - | comp_maskl[6][(t1 >> 7) & 0x7f] - | comp_maskl[7][t1 & 0x7f]; + en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f] + | comp_maskl[1][(t0 >> 14) & 0x7f] + | comp_maskl[2][(t0 >> 7) & 0x7f] + | comp_maskl[3][t0 & 0x7f] + | comp_maskl[4][(t1 >> 21) & 0x7f] + | comp_maskl[5][(t1 >> 14) & 0x7f] + | comp_maskl[6][(t1 >> 7) & 0x7f] | comp_maskl[7][t1 & 0x7f]; de_keysr[15 - round] = - en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f] - | comp_maskr[1][(t0 >> 14) & 0x7f] - | comp_maskr[2][(t0 >> 7) & 0x7f] - | comp_maskr[3][t0 & 0x7f] - | comp_maskr[4][(t1 >> 21) & 0x7f] - | comp_maskr[5][(t1 >> 14) & 0x7f] - | comp_maskr[6][(t1 >> 7) & 0x7f] - | comp_maskr[7][t1 & 0x7f]; + en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f] + | comp_maskr[1][(t0 >> 14) & 0x7f] + | comp_maskr[2][(t0 >> 7) & 0x7f] + | comp_maskr[3][t0 & 0x7f] + | comp_maskr[4][(t1 >> 21) & 0x7f] + | comp_maskr[5][(t1 >> 14) & 0x7f] + | comp_maskr[6][(t1 >> 7) & 0x7f] | comp_maskr[7][t1 & 0x7f]; } - return(0); + return (0); } static int -do_des( uint32_t l_in, uint32_t r_in, uint32_t *l_out, uint32_t *r_out, int count) +do_des(uint32_t l_in, uint32_t r_in, uint32_t *l_out, uint32_t *r_out, int count) { /* - * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. + * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. */ - uint32_t l, r, *kl, *kr, *kl1, *kr1; - uint32_t f, r48l, r48r; - int round; + uint32_t l, r, *kl, *kr, *kl1, *kr1; + uint32_t f, r48l, r48r; + int round; - if (count == 0) { - return(1); - } else if (count > 0) { + if(count == 0) + { + return (1); + } + else if(count > 0) + { /* * Encrypting */ kl1 = en_keysl; kr1 = en_keysr; - } else { + } + else + { /* * Decrypting */ @@ -528,47 +539,45 @@ do_des( uint32_t l_in, uint32_t r_in, uint32_t *l_out, uint32_t *r_out, int coun } /* - * Do initial permutation (IP). + * Do initial permutation (IP). */ l = ip_maskl[0][l_in >> 24] - | ip_maskl[1][(l_in >> 16) & 0xff] - | ip_maskl[2][(l_in >> 8) & 0xff] - | ip_maskl[3][l_in & 0xff] - | ip_maskl[4][r_in >> 24] - | ip_maskl[5][(r_in >> 16) & 0xff] - | ip_maskl[6][(r_in >> 8) & 0xff] - | ip_maskl[7][r_in & 0xff]; + | ip_maskl[1][(l_in >> 16) & 0xff] + | ip_maskl[2][(l_in >> 8) & 0xff] + | ip_maskl[3][l_in & 0xff] + | ip_maskl[4][r_in >> 24] + | ip_maskl[5][(r_in >> 16) & 0xff] + | ip_maskl[6][(r_in >> 8) & 0xff] | ip_maskl[7][r_in & 0xff]; r = ip_maskr[0][l_in >> 24] - | ip_maskr[1][(l_in >> 16) & 0xff] - | ip_maskr[2][(l_in >> 8) & 0xff] - | ip_maskr[3][l_in & 0xff] - | ip_maskr[4][r_in >> 24] - | ip_maskr[5][(r_in >> 16) & 0xff] - | ip_maskr[6][(r_in >> 8) & 0xff] - | ip_maskr[7][r_in & 0xff]; - - while (count--) { + | ip_maskr[1][(l_in >> 16) & 0xff] + | ip_maskr[2][(l_in >> 8) & 0xff] + | ip_maskr[3][l_in & 0xff] + | ip_maskr[4][r_in >> 24] + | ip_maskr[5][(r_in >> 16) & 0xff] + | ip_maskr[6][(r_in >> 8) & 0xff] | ip_maskr[7][r_in & 0xff]; + + while(count--) + { /* * Do each round. */ kl = kl1; kr = kr1; round = 16; - while (round--) { + while(round--) + { /* * Expand R to 48 bits (simulate the E-box). */ - r48l = ((r & 0x00000001) << 23) + r48l = ((r & 0x00000001) << 23) | ((r & 0xf8000000) >> 9) | ((r & 0x1f800000) >> 11) - | ((r & 0x01f80000) >> 13) - | ((r & 0x001f8000) >> 15); + | ((r & 0x01f80000) >> 13) | ((r & 0x001f8000) >> 15); - r48r = ((r & 0x0001f800) << 7) + r48r = ((r & 0x0001f800) << 7) | ((r & 0x00001f80) << 5) | ((r & 0x000001f8) << 3) - | ((r & 0x0000001f) << 1) - | ((r & 0x80000000) >> 31); + | ((r & 0x0000001f) << 1) | ((r & 0x80000000) >> 31); /* * Do salting for crypt() and friends, and * XOR with the permuted key. @@ -581,9 +590,9 @@ do_des( uint32_t l_in, uint32_t r_in, uint32_t *l_out, uint32_t *r_out, int coun * and do the pbox permutation at the same time. */ f = psbox[0][m_sbox[0][r48l >> 12]] - | psbox[1][m_sbox[1][r48l & 0xfff]] - | psbox[2][m_sbox[2][r48r >> 12]] - | psbox[3][m_sbox[3][r48r & 0xfff]]; + | psbox[1][m_sbox[1][r48l & 0xfff]] + | psbox[2][m_sbox[2][r48r >> 12]] + | psbox[3][m_sbox[3][r48r & 0xfff]]; /* * Now that we've permuted things, complete f(). */ @@ -597,23 +606,21 @@ do_des( uint32_t l_in, uint32_t r_in, uint32_t *l_out, uint32_t *r_out, int coun /* * Do final permutation (inverse of IP). */ - *l_out = fp_maskl[0][l >> 24] + *l_out = fp_maskl[0][l >> 24] | fp_maskl[1][(l >> 16) & 0xff] | fp_maskl[2][(l >> 8) & 0xff] | fp_maskl[3][l & 0xff] | fp_maskl[4][r >> 24] | fp_maskl[5][(r >> 16) & 0xff] - | fp_maskl[6][(r >> 8) & 0xff] - | fp_maskl[7][r & 0xff]; - *r_out = fp_maskr[0][l >> 24] + | fp_maskl[6][(r >> 8) & 0xff] | fp_maskl[7][r & 0xff]; + *r_out = fp_maskr[0][l >> 24] | fp_maskr[1][(l >> 16) & 0xff] | fp_maskr[2][(l >> 8) & 0xff] | fp_maskr[3][l & 0xff] | fp_maskr[4][r >> 24] | fp_maskr[5][(r >> 16) & 0xff] - | fp_maskr[6][(r >> 8) & 0xff] - | fp_maskr[7][r & 0xff]; - return(0); + | fp_maskr[6][(r >> 8) & 0xff] | fp_maskr[7][r & 0xff]; + return (0); } @@ -621,11 +628,12 @@ do_des( uint32_t l_in, uint32_t r_in, uint32_t *l_out, uint32_t *r_out, int coun static int des_cipher(const char *in, char *out, uint32_t salt, int count) { - uint32_t l_out, r_out, rawl, rawr; - int retval; - union { - uint32_t *ui32; - const char *c; + uint32_t l_out, r_out, rawl, rawr; + int retval; + union + { + uint32_t *ui32; + const char *c; } trans; des_init(); @@ -641,7 +649,7 @@ des_cipher(const char *in, char *out, uint32_t salt, int count) trans.c = out; *trans.ui32++ = htonl(l_out); *trans.ui32 = htonl(r_out); - return(retval); + return (retval); } #endif @@ -649,16 +657,17 @@ des_cipher(const char *in, char *out, uint32_t salt, int count) void setkey(const char *key) { - int i, j; - uint32_t packed_keys[2]; - u_char *p; + int i, j; + uint32_t packed_keys[2]; + uint8_t *p; - p = (u_char *) packed_keys; + p = (uint8_t *)packed_keys; - for (i = 0; i < 8; i++) { + for(i = 0; i < 8; i++) + { p[i] = 0; - for (j = 0; j < 8; j++) - if (*key++ & 1) + for(j = 0; j < 8; j++) + if(*key++ & 1) p[i] |= bits8[j]; } des_setkey((char *)p); @@ -668,23 +677,24 @@ setkey(const char *key) void encrypt(char *block, int flag) { - uint32_t io[2]; - u_char *p; - int i, j; + uint32_t io[2]; + uint8_t *p; + int i, j; des_init(); setup_salt(0L); - p = (u_char*)block; - for (i = 0; i < 2; i++) { + p = (uint8_t *)block; + for(i = 0; i < 2; i++) + { io[i] = 0L; - for (j = 0; j < 32; j++) - if (*p++ & 1) + for(j = 0; j < 32; j++) + if(*p++ & 1) io[i] |= bits32[j]; } do_des(io[0], io[1], io, io + 1, flag ? -1 : 1); - for (i = 0; i < 2; i++) - for (j = 0; j < 32; j++) + for(i = 0; i < 2; i++) + for(j = 0; j < 32; j++) block[(i << 5) | j] = (io[i] & bits32[j]) ? 1 : 0; } #endif @@ -692,9 +702,9 @@ encrypt(char *block, int flag) static char * __des_crypt(const char *key, const char *setting) { - uint32_t count, salt, l, r0, r1, keybuf[2]; - u_char *p, *q; - static char output[21]; + uint32_t count, salt, l, r0, r1, keybuf[2]; + uint8_t *p, *q; + static char output[21]; des_init(); @@ -702,44 +712,47 @@ __des_crypt(const char *key, const char *setting) * Copy the key, shifting each character up by one bit * and padding with zeros. */ - q = (u_char *)keybuf; - while (q - (u_char *)keybuf - 8) { + q = (uint8_t *)keybuf; + while(q - (uint8_t *)keybuf - 8) + { *q++ = *key << 1; - if (*(q - 1)) + if(*(q - 1)) key++; } - if (des_setkey((char *)keybuf)) - return(NULL); + if(des_setkey((char *)keybuf)) + return (NULL); #if 0 - if (*setting == _PASSWORD_EFMT1) { - int i; + if(*setting == _PASSWORD_EFMT1) + { + int i; /* * "new"-style: - * setting - underscore, 4 bytes of count, 4 bytes of salt - * key - unlimited characters + * setting - underscore, 4 bytes of count, 4 bytes of salt + * key - unlimited characters */ - for (i = 1, count = 0L; i < 5; i++) + for(i = 1, count = 0L; i < 5; i++) count |= ascii_to_bin(setting[i]) << ((i - 1) * 6); - for (i = 5, salt = 0L; i < 9; i++) + for(i = 5, salt = 0L; i < 9; i++) salt |= ascii_to_bin(setting[i]) << ((i - 5) * 6); - while (*key) { + while(*key) + { /* * Encrypt the key with itself. */ - if (des_cipher((char *)keybuf, (char *)keybuf, 0L, 1)) - return(NULL); + if(des_cipher((char *)keybuf, (char *)keybuf, 0L, 1)) + return (NULL); /* * And XOR with the next 8 characters of the key. */ - q = (u_char *)keybuf; - while (q - (u_char *)keybuf - 8 && *key) + q = (uint8_t *)keybuf; + while(q - (uint8_t *)keybuf - 8 && *key) *q++ ^= *key++ << 1; - if (des_setkey((char *)keybuf)) - return(NULL); + if(des_setkey((char *)keybuf)) + return (NULL); } strncpy(output, setting, 9); @@ -751,19 +764,19 @@ __des_crypt(const char *key, const char *setting) * NUL in it. */ output[9] = '\0'; - p = (u_char *)output + strlen(output); - } else + p = (uint8_t *)output + strlen(output); + } + else #endif { /* * "old"-style: - * setting - 2 bytes of salt - * key - up to 8 characters + * setting - 2 bytes of salt + * key - up to 8 characters */ count = 25; - salt = (ascii_to_bin(setting[1]) << 6) - | ascii_to_bin(setting[0]); + salt = (ascii_to_bin(setting[1]) << 6) | ascii_to_bin(setting[0]); output[0] = setting[0]; /* @@ -774,14 +787,14 @@ __des_crypt(const char *key, const char *setting) */ output[1] = setting[1] ? setting[1] : output[0]; - p = (u_char *)output + 2; + p = (uint8_t *)output + 2; } setup_salt(salt); /* * Do it. */ - if (do_des(0L, 0L, &r0, &r1, (int)count)) - return(NULL); + if(do_des(0L, 0L, &r0, &r1, (int)count)) + return (NULL); /* * Now encode the result... */ @@ -803,7 +816,7 @@ __des_crypt(const char *key, const char *setting) *p++ = ascii64[l & 0x3f]; *p = 0; - return(output); + return (output); } /* Now md5 crypt */ @@ -884,22 +897,23 @@ __des_crypt(const char *key, const char *setting) /**********************************************************************/ /* MD5 context. */ -struct MD5Context { - uint32_t state[4]; /* state (ABCD) */ - uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */ - unsigned char buffer[64]; /* input buffer */ +struct MD5Context +{ + uint32_t state[4]; /* state (ABCD) */ + uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */ + unsigned char buffer[64]; /* input buffer */ }; -static void __md5_Init (struct MD5Context *); -static void __md5_Update (struct MD5Context *, const char *, unsigned int); -static void __md5_Pad (struct MD5Context *); -static void __md5_Final (char [16], struct MD5Context *); -static void __md5_Transform (uint32_t [4], const unsigned char [64]); +static void __md5_Init(struct MD5Context *); +static void __md5_Update(struct MD5Context *, const char *, unsigned int); +static void __md5_Pad(struct MD5Context *); +static void __md5_Final(char[16], struct MD5Context *); +static void __md5_Transform(uint32_t[4], const unsigned char[64]); static const char __md5__magic[] = "$1$"; /* This string is magic for this algorithm. Having it this way, we can get better later on */ -static const unsigned char __md5_itoa64[] = /* 0 ... 63 => ascii - 64 */ +static const unsigned char __md5_itoa64[] = /* 0 ... 63 => ascii - 64 */ "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -915,15 +929,16 @@ static const unsigned char __md5_itoa64[] = /* 0 ... 63 => ascii - 64 */ */ static void -__md5_Encode (unsigned char *output, uint32_t *input, unsigned int len) +__md5_Encode(unsigned char *output, uint32_t *input, unsigned int len) { unsigned int i, j; - for (i = 0, j = 0; j < len; i++, j += 4) { + for(i = 0, j = 0; j < len; i++, j += 4) + { output[j] = (unsigned char)(input[i] & 0xff); - output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); - output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); - output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); + output[j + 1] = (unsigned char)((input[i] >> 8) & 0xff); + output[j + 2] = (unsigned char)((input[i] >> 16) & 0xff); + output[j + 3] = (unsigned char)((input[i] >> 24) & 0xff); } } @@ -933,13 +948,13 @@ __md5_Encode (unsigned char *output, uint32_t *input, unsigned int len) */ static void -__md5_Decode (uint32_t *output, const unsigned char *input, unsigned int len) +__md5_Decode(uint32_t *output, const unsigned char *input, unsigned int len) { unsigned int i, j; - for (i = 0, j = 0; j < len; i++, j += 4) - output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) | - (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24); + for(i = 0, j = 0; j < len; i++, j += 4) + output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j + 1]) << 8) | + (((uint32_t)input[j + 2]) << 16) | (((uint32_t)input[j + 3]) << 24); } #endif /* i386 */ @@ -979,7 +994,8 @@ __md5_Decode (uint32_t *output, const unsigned char *input, unsigned int len) /* MD5 initialization. Begins an MD5 operation, writing a new context. */ -static void __md5_Init (struct MD5Context *context) +static void +__md5_Init(struct MD5Context *context) { context->count[0] = context->count[1] = 0; @@ -996,29 +1012,29 @@ static void __md5_Init (struct MD5Context *context) * context. */ -static void __md5_Update ( struct MD5Context *context, const char *xinput, unsigned int inputLen) +static void +__md5_Update(struct MD5Context *context, const char *xinput, unsigned int inputLen) { unsigned int i, lindex, partLen; - const unsigned char *input = (const unsigned char *)xinput; /* i hate gcc */ + const unsigned char *input = (const unsigned char *)xinput; /* i hate gcc */ /* Compute number of bytes mod 64 */ lindex = (unsigned int)((context->count[0] >> 3) & 0x3F); /* Update number of bits */ - if ((context->count[0] += ((uint32_t)inputLen << 3)) - < ((uint32_t)inputLen << 3)) + if((context->count[0] += ((uint32_t)inputLen << 3)) < ((uint32_t)inputLen << 3)) context->count[1]++; context->count[1] += ((uint32_t)inputLen >> 29); partLen = 64 - lindex; /* Transform as many times as possible. */ - if (inputLen >= partLen) { - memcpy(&context->buffer[lindex], input, - partLen); - __md5_Transform (context->state, context->buffer); + if(inputLen >= partLen) + { + memcpy(&context->buffer[lindex], input, partLen); + __md5_Transform(context->state, context->buffer); - for (i = partLen; i + 63 < inputLen; i += 64) - __md5_Transform (context->state, &input[i]); + for(i = partLen; i + 63 < inputLen; i += 64) + __md5_Transform(context->state, &input[i]); lindex = 0; } @@ -1026,15 +1042,15 @@ static void __md5_Update ( struct MD5Context *context, const char *xinput, unsig i = 0; /* Buffer remaining input */ - memcpy (&context->buffer[lindex], &input[i], - inputLen-i); + memcpy(&context->buffer[lindex], &input[i], inputLen - i); } /* * MD5 padding. Adds padding followed by original length. */ -static void __md5_Pad ( struct MD5Context *context) +static void +__md5_Pad(struct MD5Context *context) { char bits[8]; unsigned int lindex, padLen; @@ -1044,15 +1060,15 @@ static void __md5_Pad ( struct MD5Context *context) PADDING[0] = 0x80; /* Save number of bits */ - __md5_Encode (bits, context->count, 8); + __md5_Encode(bits, context->count, 8); /* Pad out to 56 mod 64. */ lindex = (unsigned int)((context->count[0] >> 3) & 0x3f); padLen = (lindex < 56) ? (56 - lindex) : (120 - lindex); - __md5_Update (context, PADDING, padLen); + __md5_Update(context, PADDING, padLen); /* Append length (before padding) */ - __md5_Update (context, bits, 8); + __md5_Update(context, bits, 8); } /* @@ -1060,25 +1076,26 @@ static void __md5_Pad ( struct MD5Context *context) * the message digest and zeroizing the context. */ -static void __md5_Final ( char xdigest[16], struct MD5Context *context) +static void +__md5_Final(char xdigest[16], struct MD5Context *context) { unsigned char *digest = (unsigned char *)xdigest; /* Do padding. */ - __md5_Pad (context); + __md5_Pad(context); /* Store state in digest */ - __md5_Encode (digest, context->state, 16); + __md5_Encode(digest, context->state, 16); /* Zeroize sensitive information. */ - memset (context, 0, sizeof (*context)); + memset(context, 0, sizeof(*context)); } /* MD5 basic transformation. Transforms state based on block. */ static void -__md5_Transform (state, block) - uint32_t state[4]; - const unsigned char block[64]; +__md5_Transform(state, block) + uint32_t state[4]; + const unsigned char block[64]; { uint32_t a, b, c, d, x[16]; @@ -1100,22 +1117,22 @@ __md5_Transform (state, block) int i; static const uint32_t C[] = { - /* round 1 */ + /* round 1 */ 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, - /* round 2 */ + /* round 2 */ 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, - 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8, + 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, - /* round 3 */ + /* round 3 */ 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, - /* round 4 */ + /* round 4 */ 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, @@ -1123,103 +1140,141 @@ __md5_Transform (state, block) }; static const char P[] = { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */ - 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */ - 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */ - 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */ + 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */ + 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */ + 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */ }; #endif /* MD5_SIZE_OVER_SPEED > 0 */ - __md5_Decode (x, block, 64); + __md5_Decode(x, block, 64); - a = state[0]; b = state[1]; c = state[2]; d = state[3]; + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; #if MD5_SIZE_OVER_SPEED > 2 - pc = C; pp = P; ps = S - 4; + pc = C; + pp = P; + ps = S - 4; - for ( i = 0 ; i < 64 ; i++ ) { - if ((i&0x0f) == 0) ps += 4; + for(i = 0; i < 64; i++) + { + if((i & 0x0f) == 0) + ps += 4; temp = a; - switch (i>>4) { - case 0: - temp += F(b,c,d); - break; - case 1: - temp += G(b,c,d); - break; - case 2: - temp += H(b,c,d); - break; - case 3: - temp += I(b,c,d); - break; + switch (i >> 4) + { + case 0: + temp += F(b, c, d); + break; + case 1: + temp += G(b, c, d); + break; + case 2: + temp += H(b, c, d); + break; + case 3: + temp += I(b, c, d); + break; } temp += x[(int)(*pp++)] + *pc++; - temp = ROTATE_LEFT(temp, ps[i&3]); + temp = ROTATE_LEFT(temp, ps[i & 3]); temp += b; - a = d; d = c; c = b; b = temp; + a = d; + d = c; + c = b; + b = temp; } #elif MD5_SIZE_OVER_SPEED > 1 - pc = C; pp = P; ps = S; + pc = C; + pp = P; + ps = S; /* Round 1 */ - for ( i = 0 ; i < 16 ; i++ ) { - FF (a, b, c, d, x[(int)(*pp++)], ps[i&0x3], *pc++); - temp = d; d = c; c = b; b = a; a = temp; + for(i = 0; i < 16; i++) + { + FF(a, b, c, d, x[(int)(*pp++)], ps[i & 0x3], *pc++); + temp = d; + d = c; + c = b; + b = a; + a = temp; } /* Round 2 */ ps += 4; - for ( ; i < 32 ; i++ ) { - GG (a, b, c, d, x[(int)(*pp++)], ps[i&0x3], *pc++); - temp = d; d = c; c = b; b = a; a = temp; + for(; i < 32; i++) + { + GG(a, b, c, d, x[(int)(*pp++)], ps[i & 0x3], *pc++); + temp = d; + d = c; + c = b; + b = a; + a = temp; } /* Round 3 */ ps += 4; - for ( ; i < 48 ; i++ ) { - HH (a, b, c, d, x[(int)(*pp++)], ps[i&0x3], *pc++); - temp = d; d = c; c = b; b = a; a = temp; + for(; i < 48; i++) + { + HH(a, b, c, d, x[(int)(*pp++)], ps[i & 0x3], *pc++); + temp = d; + d = c; + c = b; + b = a; + a = temp; } /* Round 4 */ ps += 4; - for ( ; i < 64 ; i++ ) { - II (a, b, c, d, x[(int)(*pp++)], ps[i&0x3], *pc++); - temp = d; d = c; c = b; b = a; a = temp; + for(; i < 64; i++) + { + II(a, b, c, d, x[(int)(*pp++)], ps[i & 0x3], *pc++); + temp = d; + d = c; + c = b; + b = a; + a = temp; } #elif MD5_SIZE_OVER_SPEED > 0 - pc = C; pp = P; + pc = C; + pp = P; /* Round 1 */ - for ( i = 0 ; i < 4 ; i++ ) { - FF (a, b, c, d, x[(int)(*pp++)], 7, *pc++); - FF (d, a, b, c, x[(int)(*pp++)], 12, *pc++); - FF (c, d, a, b, x[(int)(*pp++)], 17, *pc++); - FF (b, c, d, a, x[(int)(*pp++)], 22, *pc++); + for(i = 0; i < 4; i++) + { + FF(a, b, c, d, x[(int)(*pp++)], 7, *pc++); + FF(d, a, b, c, x[(int)(*pp++)], 12, *pc++); + FF(c, d, a, b, x[(int)(*pp++)], 17, *pc++); + FF(b, c, d, a, x[(int)(*pp++)], 22, *pc++); } /* Round 2 */ - for ( i = 0 ; i < 4 ; i++ ) { - GG (a, b, c, d, x[(int)(*pp++)], 5, *pc++); - GG (d, a, b, c, x[(int)(*pp++)], 9, *pc++); - GG (c, d, a, b, x[(int)(*pp++)], 14, *pc++); - GG (b, c, d, a, x[(int)(*pp++)], 20, *pc++); + for(i = 0; i < 4; i++) + { + GG(a, b, c, d, x[(int)(*pp++)], 5, *pc++); + GG(d, a, b, c, x[(int)(*pp++)], 9, *pc++); + GG(c, d, a, b, x[(int)(*pp++)], 14, *pc++); + GG(b, c, d, a, x[(int)(*pp++)], 20, *pc++); } /* Round 3 */ - for ( i = 0 ; i < 4 ; i++ ) { - HH (a, b, c, d, x[(int)(*pp++)], 4, *pc++); - HH (d, a, b, c, x[(int)(*pp++)], 11, *pc++); - HH (c, d, a, b, x[(int)(*pp++)], 16, *pc++); - HH (b, c, d, a, x[(int)(*pp++)], 23, *pc++); + for(i = 0; i < 4; i++) + { + HH(a, b, c, d, x[(int)(*pp++)], 4, *pc++); + HH(d, a, b, c, x[(int)(*pp++)], 11, *pc++); + HH(c, d, a, b, x[(int)(*pp++)], 16, *pc++); + HH(b, c, d, a, x[(int)(*pp++)], 23, *pc++); } /* Round 4 */ - for ( i = 0 ; i < 4 ; i++ ) { - II (a, b, c, d, x[(int)(*pp++)], 6, *pc++); - II (d, a, b, c, x[(int)(*pp++)], 10, *pc++); - II (c, d, a, b, x[(int)(*pp++)], 15, *pc++); - II (b, c, d, a, x[(int)(*pp++)], 21, *pc++); + for(i = 0; i < 4; i++) + { + II(a, b, c, d, x[(int)(*pp++)], 6, *pc++); + II(d, a, b, c, x[(int)(*pp++)], 10, *pc++); + II(c, d, a, b, x[(int)(*pp++)], 15, *pc++); + II(b, c, d, a, x[(int)(*pp++)], 21, *pc++); } #else /* Round 1 */ @@ -1227,88 +1282,88 @@ __md5_Transform (state, block) #define S12 12 #define S13 17 #define S14 22 - FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ - FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ - FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ - FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ - FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ - FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ - FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ - FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ - FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ - FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ - FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ - FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ - FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ - FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ - FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ - FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ + FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */ + FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */ + FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */ + FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */ + FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */ + FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */ + FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */ + FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */ + FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */ + FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */ + FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ + FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ + FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ + FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ + FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ + FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ /* Round 2 */ #define S21 5 #define S22 9 #define S23 14 #define S24 20 - GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ - GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ - GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ - GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ - GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ - GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ - GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ - GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ - GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ - GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ - GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ - GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ - GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ - GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ - GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ - GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ + GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */ + GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */ + GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ + GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */ + GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */ + GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */ + GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ + GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */ + GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */ + GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ + GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */ + GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */ + GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ + GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */ + GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */ + GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ /* Round 3 */ #define S31 4 #define S32 11 #define S33 16 #define S34 23 - HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ - HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ - HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ - HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ - HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ - HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ - HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ - HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ - HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ - HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ - HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ - HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ - HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ - HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ - HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ - HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ + HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */ + HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */ + HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ + HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ + HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */ + HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */ + HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */ + HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ + HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ + HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */ + HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */ + HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */ + HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */ + HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ + HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ + HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */ /* Round 4 */ #define S41 6 #define S42 10 #define S43 15 #define S44 21 - II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ - II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ - II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ - II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ - II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ - II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ - II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ - II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ - II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ - II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ - II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ - II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ - II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ - II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ - II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ - II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ + II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */ + II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */ + II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ + II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */ + II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ + II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */ + II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ + II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */ + II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */ + II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ + II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */ + II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ + II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */ + II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ + II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */ + II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */ #endif state[0] += a; @@ -1317,14 +1372,16 @@ __md5_Transform (state, block) state[3] += d; /* Zeroize sensitive information. */ - memset (x, 0, sizeof (x)); + memset(x, 0, sizeof(x)); } -static void __md5_to64( char *s, unsigned long v, int n) +static void +__md5_to64(char *s, unsigned long v, int n) { - while (--n >= 0) { - *s++ = __md5_itoa64[v&0x3f]; + while(--n >= 0) + { + *s++ = __md5_itoa64[v & 0x3f]; v >>= 6; } } @@ -1335,15 +1392,16 @@ static void __md5_to64( char *s, unsigned long v, int n) * Use MD5 for what it is best at... */ -static char * __md5_crypt( const char *pw, const char *salt) +static char * +__md5_crypt(const char *pw, const char *salt) { /* Static stuff */ static const char *sp, *ep; static char passwd[120], *p; - char final[17]; /* final[16] exists only to aid in looping */ - int sl,pl,i,__md5__magic_len,pw_len; - struct MD5Context ctx,ctx1; + char final[17]; /* final[16] exists only to aid in looping */ + int sl, pl, i, __md5__magic_len, pw_len; + struct MD5Context ctx, ctx1; unsigned long l; /* Refine the Salt first */ @@ -1351,11 +1409,11 @@ static char * __md5_crypt( const char *pw, const char *salt) /* If it starts with the magic string, then skip that */ __md5__magic_len = strlen(__md5__magic); - if(!strncmp(sp,__md5__magic,__md5__magic_len)) + if(!strncmp(sp, __md5__magic, __md5__magic_len)) sp += __md5__magic_len; /* It stops at the first '$', max 8 chars */ - for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++) + for(ep = sp; *ep && *ep != '$' && ep < (sp + 8); ep++) continue; /* get the length of the true salt */ @@ -1365,79 +1423,83 @@ static char * __md5_crypt( const char *pw, const char *salt) /* The password first, since that is what is most unknown */ pw_len = strlen(pw); - __md5_Update(&ctx,pw,pw_len); + __md5_Update(&ctx, pw, pw_len); /* Then our magic string */ - __md5_Update(&ctx,__md5__magic,__md5__magic_len); + __md5_Update(&ctx, __md5__magic, __md5__magic_len); /* Then the raw salt */ - __md5_Update(&ctx,sp,sl); + __md5_Update(&ctx, sp, sl); /* Then just as many characters of the MD5(pw,salt,pw) */ __md5_Init(&ctx1); - __md5_Update(&ctx1,pw,pw_len); - __md5_Update(&ctx1,sp,sl); - __md5_Update(&ctx1,pw,pw_len); - __md5_Final(final,&ctx1); + __md5_Update(&ctx1, pw, pw_len); + __md5_Update(&ctx1, sp, sl); + __md5_Update(&ctx1, pw, pw_len); + __md5_Final(final, &ctx1); for(pl = pw_len; pl > 0; pl -= 16) - __md5_Update(&ctx,final,pl>16 ? 16 : pl); + __md5_Update(&ctx, final, pl > 16 ? 16 : pl); /* Don't leave anything around in vm they could use. */ - memset(final,0,sizeof final); + memset(final, 0, sizeof final); /* Then something really weird... */ - for (i = pw_len; i ; i >>= 1) { - __md5_Update(&ctx, ((i&1) ? final : pw), 1); + for(i = pw_len; i; i >>= 1) + { + __md5_Update(&ctx, ((i & 1) ? final : pw), 1); } /* Now make the output string */ - strcpy(passwd,__md5__magic); - strncat(passwd,sp,sl); - strcat(passwd,"$"); + strcpy(passwd, __md5__magic); + strncat(passwd, sp, sl); + strcat(passwd, "$"); - __md5_Final(final,&ctx); + __md5_Final(final, &ctx); /* * and now, just to make sure things don't run too fast * On a 60 Mhz Pentium this takes 34 msec, so you would * need 30 seconds to build a 1000 entry dictionary... */ - for(i=0;i<1000;i++) { + for(i = 0; i < 1000; i++) + { __md5_Init(&ctx1); if(i & 1) - __md5_Update(&ctx1,pw,pw_len); + __md5_Update(&ctx1, pw, pw_len); else - __md5_Update(&ctx1,final,16); + __md5_Update(&ctx1, final, 16); if(i % 3) - __md5_Update(&ctx1,sp,sl); + __md5_Update(&ctx1, sp, sl); if(i % 7) - __md5_Update(&ctx1,pw,pw_len); + __md5_Update(&ctx1, pw, pw_len); if(i & 1) - __md5_Update(&ctx1,final,16); + __md5_Update(&ctx1, final, 16); else - __md5_Update(&ctx1,pw,pw_len); - __md5_Final(final,&ctx1); + __md5_Update(&ctx1, pw, pw_len); + __md5_Final(final, &ctx1); } p = passwd + strlen(passwd); final[16] = final[5]; - for ( i=0 ; i < 5 ; i++ ) { - l = (final[i]<<16) | (final[i+6]<<8) | final[i+12]; - __md5_to64(p,l,4); p += 4; + for(i = 0; i < 5; i++) + { + l = (final[i] << 16) | (final[i + 6] << 8) | final[i + 12]; + __md5_to64(p, l, 4); + p += 4; } l = final[11]; - __md5_to64(p,l,2); p += 2; + __md5_to64(p, l, 2); + p += 2; *p = '\0'; /* Don't leave anything around in vm they could use. */ - memset(final,0,sizeof final); + memset(final, 0, sizeof final); return passwd; } #endif /* NEED_CRYPT */ - diff --git a/libratbox/src/devpoll.c b/libratbox/src/devpoll.c index 162130f..f026ca2 100644 --- a/libratbox/src/devpoll.c +++ b/libratbox/src/devpoll.c @@ -22,7 +22,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: devpoll.c 25038 2008-01-23 16:03:08Z androsyn $ + * $Id: devpoll.c 26092 2008-09-19 15:13:52Z androsyn $ */ #include #include @@ -41,13 +41,13 @@ static void devpoll_update_events(int, short, PF *); static void devpoll_write_update(int, int); -int +int rb_setup_fd_devpoll(rb_fde_t *F) { - return 0; + return 0; } - - + + /* * Write an update to the devpoll filter. * See, we end up having to do a seperate (?) remove before we do an @@ -68,7 +68,8 @@ devpoll_write_update(int fd, int events) /* Write the thing to our poll fd */ retval = write(dpfd, &pollfds[0], sizeof(struct pollfd)); if(retval != sizeof(struct pollfd)) - rb_lib_log("devpoll_write_update: dpfd write failed %d: %s", errno, strerror(errno)); + rb_lib_log("devpoll_write_update: dpfd write failed %d: %s", errno, + strerror(errno)); /* Done! */ } @@ -150,7 +151,7 @@ rb_init_netio_devpoll(void) { return errno; } - maxfd = getdtablesize() - 2; /* This makes more sense than HARD_FDLIMIT */ + maxfd = getdtablesize() - 2; /* This makes more sense than HARD_FDLIMIT */ fdmask = rb_malloc(sizeof(fdmask) * maxfd + 1); rb_open(dpfd, RB_FD_UNKNOWN, "/dev/poll file descriptor"); } @@ -162,8 +163,7 @@ rb_init_netio_devpoll(void) * and deregister interest in a pending IO state for a given FD. */ void -rb_setselect_devpoll(rb_fde_t *F, unsigned int type, PF * handler, - void *client_data) +rb_setselect_devpoll(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { lrb_assert(IsFDOpen(F)); @@ -205,7 +205,7 @@ rb_select_devpoll(long delay) do { - for (;;) + for(;;) { dopoll.dp_timeout = delay; dopoll.dp_nfds = maxfd; @@ -223,14 +223,13 @@ rb_select_devpoll(long delay) if(num == 0) continue; - for (i = 0; i < num; i++) + for(i = 0; i < num; i++) { int fd = dopoll.dp_fds[i].fd; PF *hdl = NULL; rb_fde_t *F = rb_find_fd(fd); - if((dopoll.dp_fds[i]. - revents & (POLLRDNORM | POLLIN | POLLHUP | - POLLERR)) + if((dopoll.dp_fds[i].revents & (POLLRDNORM | POLLIN | POLLHUP | + POLLERR)) && (dopoll.dp_fds[i].events & (POLLRDNORM | POLLIN))) { if((hdl = F->read_handler) != NULL) @@ -243,16 +242,14 @@ rb_select_devpoll(long delay) * poll set *if* the handler changes state (active -> * NULL or vice versa.) */ - devpoll_update_events(fd, - RB_SELECT_READ, F->read_handler); + devpoll_update_events(fd, RB_SELECT_READ, F->read_handler); } } if(!IsFDOpen(F)) continue; /* Read handler closed us..go on to do something more useful */ - if((dopoll.dp_fds[i]. - revents & (POLLWRNORM | POLLOUT | POLLHUP | - POLLERR)) + if((dopoll.dp_fds[i].revents & (POLLWRNORM | POLLOUT | POLLHUP | + POLLERR)) && (dopoll.dp_fds[i].events & (POLLWRNORM | POLLOUT))) { if((hdl = F->write_handler) != NULL) @@ -268,13 +265,13 @@ rb_select_devpoll(long delay) } return RB_OK; } - while (0); + while(0); /* XXX Get here, we broke! */ return 0; } #else /* /dev/poll not supported */ -int +int rb_init_netio_devpoll(void) { return ENOSYS; @@ -301,4 +298,3 @@ rb_setup_fd_devpoll(rb_fde_t *F) return -1; } #endif - diff --git a/libratbox/src/epoll.c b/libratbox/src/epoll.c index 1f99568..1c28cac 100644 --- a/libratbox/src/epoll.c +++ b/libratbox/src/epoll.c @@ -23,7 +23,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: epoll.c 25675 2008-07-06 04:13:05Z androsyn $ + * $Id: epoll.c 26092 2008-09-19 15:13:52Z androsyn $ */ #define _GNU_SOURCE 1 @@ -31,7 +31,6 @@ #include #include #include - #if defined(HAVE_EPOLL_CTL) && (HAVE_SYS_EPOLL_H) #define USING_EPOLL #include @@ -44,6 +43,10 @@ #define EPOLL_SCHED_EVENT 1 #endif +#if defined(USE_TIMERFD_CREATE) +#include +#endif + #define RTSIGNAL SIGRTMIN struct epoll_info { @@ -54,8 +57,7 @@ struct epoll_info static struct epoll_info *ep_info; static int can_do_event; - -//static void setup_signalfd(void); +static int can_do_timerfd; /* * rb_init_netio @@ -66,7 +68,7 @@ static int can_do_event; int rb_init_netio_epoll(void) { - can_do_event = 0; /* shut up gcc */ + can_do_event = 0; /* shut up gcc */ ep_info = rb_malloc(sizeof(struct epoll_info)); ep_info->pfd_size = getdtablesize(); ep_info->ep = epoll_create(ep_info->pfd_size); @@ -81,7 +83,7 @@ rb_init_netio_epoll(void) } int -rb_setup_fd_epoll(rb_fde_t * F) +rb_setup_fd_epoll(rb_fde_t *F) { return 0; } @@ -94,7 +96,7 @@ rb_setup_fd_epoll(rb_fde_t * F) * and deregister interest in a pending IO state for a given FD. */ void -rb_setselect_epoll(rb_fde_t * F, unsigned int type, PF * handler, void *client_data) +rb_setselect_epoll(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { struct epoll_event ep_event; int old_flags = F->pflags; @@ -180,7 +182,7 @@ rb_select_epoll(long delay) if(num <= 0) return RB_OK; - for (i = 0; i < num; i++) + for(i = 0; i < num; i++) { PF *hdl; rb_fde_t *F = ep_info->pfd[i].data.ptr; @@ -244,7 +246,7 @@ rb_select_epoll(long delay) return RB_OK; } -#ifdef EPOLL_SCHED_EVENT +#ifdef EPOLL_SCHED_EVENT int rb_epoll_supports_event(void) { @@ -253,12 +255,22 @@ rb_epoll_supports_event(void) struct sigevent ev; int fd; sigset_t set; - + if(can_do_event == 1) return 1; if(can_do_event == -1) return 0; - + +#ifdef USE_TIMERFD_CREATE + if((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0) + { + close(fd); + can_do_event = 1; + can_do_timerfd = 1; + return 0; + } +#endif + ev.sigev_signo = SIGVTALRM; ev.sigev_notify = SIGEV_SIGNAL; if(timer_create(CLOCK_REALTIME, &ev, &timer) != 0) @@ -269,7 +281,7 @@ rb_epoll_supports_event(void) timer_delete(timer); sigemptyset(&set); fd = signalfd(-1, &set, 0); - if(fd < 0) + if(fd < 0) { can_do_event = -1; return 0; @@ -281,36 +293,37 @@ rb_epoll_supports_event(void) /* bleh..work around a glibc header bug on 32bit systems */ -struct our_signalfd_siginfo { - uint32_t signo; - int32_t err; - int32_t code; - uint32_t pid; - uint32_t uid; - int32_t fd; - uint32_t tid; - uint32_t band; - uint32_t overrun; - uint32_t trapno; - int32_t status; - int32_t svint; - uint64_t svptr; - uint64_t utime; - uint64_t stime; - uint64_t addr; - uint8_t pad[48]; +struct our_signalfd_siginfo +{ + uint32_t signo; + int32_t err; + int32_t code; + uint32_t pid; + uint32_t uid; + int32_t fd; + uint32_t tid; + uint32_t band; + uint32_t overrun; + uint32_t trapno; + int32_t status; + int32_t svint; + uint64_t svptr; + uint64_t utime; + uint64_t stime; + uint64_t addr; + uint8_t pad[48]; }; #define SIGFDIOV_COUNT 16 -static void +static void signalfd_handler(rb_fde_t *F, void *data) { static struct our_signalfd_siginfo fdsig[SIGFDIOV_COUNT]; static struct iovec iov[SIGFDIOV_COUNT]; struct ev_entry *ev; int ret, x; - + for(x = 0; x < SIGFDIOV_COUNT; x++) { iov[x].iov_base = &fdsig[x]; @@ -326,15 +339,15 @@ signalfd_handler(rb_fde_t *F, void *data) rb_epoll_init_event(); return; } - - if(ret < 0) + + if(ret < 0) { rb_setselect(F, RB_SELECT_READ, signalfd_handler, NULL); return; } - for(x = 0; x < ret / (int)sizeof(struct signalfd_siginfo); x++) + for(x = 0; x < ret / (int)sizeof(struct our_signalfd_siginfo); x++) { - ev = (struct ev_entry *)fdsig[x].svptr; + ev = (struct ev_entry *)((uintptr_t)fdsig[x].svptr); if(ev == NULL) continue; rb_run_event(ev); @@ -345,26 +358,32 @@ signalfd_handler(rb_fde_t *F, void *data) void rb_epoll_init_event(void) { + sigset_t ss; rb_fde_t *F; int sfd; - sigemptyset(&ss); - sigaddset(&ss, RTSIGNAL); - sigprocmask(SIG_BLOCK, &ss, 0); - sigemptyset(&ss); - sigaddset(&ss, RTSIGNAL); - sfd = signalfd(-1, &ss, 0); - if(sfd == -1) { - can_do_event = -1; - return; + rb_epoll_supports_event(); + if(!can_do_timerfd) + { + sigemptyset(&ss); + sigaddset(&ss, RTSIGNAL); + sigprocmask(SIG_BLOCK, &ss, 0); + sigemptyset(&ss); + sigaddset(&ss, RTSIGNAL); + sfd = signalfd(-1, &ss, 0); + if(sfd == -1) + { + can_do_event = -1; + return; + } + F = rb_open(sfd, RB_FD_UNKNOWN, "signalfd"); + rb_set_nb(F); + signalfd_handler(F, NULL); } - F = rb_open(sfd, RB_FD_UNKNOWN, "signalfd"); - rb_set_nb(F); - signalfd_handler(F, NULL); } -int -rb_epoll_sched_event(struct ev_entry *event, int when) +static int +rb_epoll_sched_event_signalfd(struct ev_entry *event, int when) { timer_t *id; struct sigevent ev; @@ -377,7 +396,7 @@ rb_epoll_sched_event(struct ev_entry *event, int when) ev.sigev_signo = RTSIGNAL; ev.sigev_value.sival_ptr = event; - if (timer_create(CLOCK_REALTIME, &ev, id) < 0) + if(timer_create(CLOCK_REALTIME, &ev, id) < 0) { rb_lib_log("timer_create: %s\n", strerror(errno)); return 0; @@ -387,8 +406,8 @@ rb_epoll_sched_event(struct ev_entry *event, int when) ts.it_value.tv_nsec = 0; if(event->frequency != 0) ts.it_interval = ts.it_value; - - if(timer_settime(*id, 0, &ts, NULL) < 0) + + if(timer_settime(*id, 0, &ts, NULL) < 0) { rb_lib_log("timer_settime: %s\n", strerror(errno)); return 0; @@ -396,10 +415,94 @@ rb_epoll_sched_event(struct ev_entry *event, int when) return 1; } +#ifdef USE_TIMERFD_CREATE +static void +rb_read_timerfd(rb_fde_t *F, void *data) +{ + struct ev_entry *event = (struct ev_entry *)data; + int retlen; + uint64_t count; + + if(event == NULL) + { + rb_close(F); + return; + } + + retlen = rb_read(F, &count, sizeof(count)); + if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) + { + rb_close(F); + rb_lib_log("rb_read_timerfd: timerfd[%s] closed on error: %s", event->name, + strerror(errno)); + return; + } + rb_setselect(F, RB_SELECT_READ, rb_read_timerfd, event); + rb_run_event(event); +} + + +static int +rb_epoll_sched_event_timerfd(struct ev_entry *event, int when) +{ + struct itimerspec ts; + static char buf[FD_DESC_SZ + 8]; + int fd; + rb_fde_t *F; + + if((fd = timerfd_create(CLOCK_REALTIME, 0)) < 0) + { + rb_lib_log("timerfd_create: %s\n", strerror(errno)); + return 0; + } + + memset(&ts, 0, sizeof(ts)); + ts.it_value.tv_sec = when; + ts.it_value.tv_nsec = 0; + if(event->frequency != 0) + ts.it_interval = ts.it_value; + + if(timerfd_settime(fd, 0, &ts, NULL) < 0) + { + rb_lib_log("timerfd_settime: %s\n", strerror(errno)); + close(fd); + return 0; + } + rb_snprintf(buf, sizeof(buf), "timerfd: %s", event->name); + F = rb_open(fd, RB_FD_UNKNOWN, buf); + rb_set_nb(F); + event->comm_ptr = F; + rb_setselect(F, RB_SELECT_READ, rb_read_timerfd, event); + return 1; +} +#endif + + + +int +rb_epoll_sched_event(struct ev_entry *event, int when) +{ +#ifdef USE_TIMERFD_CREATE + if(can_do_timerfd) + { + return rb_epoll_sched_event_timerfd(event, when); + } +#endif + return rb_epoll_sched_event_signalfd(event, when); +} + void rb_epoll_unsched_event(struct ev_entry *event) { - timer_delete(*((timer_t *)event->comm_ptr)); +#ifdef USE_TIMERFD_CREATE + if(can_do_timerfd) + { + rb_close((rb_fde_t *)event->comm_ptr); + event->comm_ptr = NULL; + return; + } +#endif + timer_delete(*((timer_t *) event->comm_ptr)); rb_free(event->comm_ptr); event->comm_ptr = NULL; } @@ -413,7 +516,7 @@ rb_init_netio_epoll(void) } void -rb_setselect_epoll(rb_fde_t * F, unsigned int type, PF * handler, void *client_data) +rb_setselect_epoll(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { errno = ENOSYS; return; @@ -427,7 +530,7 @@ rb_select_epoll(long delay) } int -rb_setup_fd_epoll(rb_fde_t * F) +rb_setup_fd_epoll(rb_fde_t *F) { errno = ENOSYS; return -1; @@ -437,7 +540,8 @@ rb_setup_fd_epoll(rb_fde_t * F) #endif #if !defined(USING_EPOLL) || !defined(EPOLL_SCHED_EVENT) -void rb_epoll_init_event(void) +void +rb_epoll_init_event(void) { return; } diff --git a/libratbox/src/event.c b/libratbox/src/event.c index 132d01e..76f8426 100644 --- a/libratbox/src/event.c +++ b/libratbox/src/event.c @@ -39,7 +39,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: event.c 25147 2008-03-28 17:15:47Z androsyn $ + * $Id: event.c 26092 2008-09-19 15:13:52Z androsyn $ */ #include @@ -47,24 +47,11 @@ #include #include -static const char *last_event_ran = NULL; +#define EV_NAME_LEN 33 +static char last_event_ran[EV_NAME_LEN]; static rb_dlink_list event_list; static time_t event_time_min = -1; -/* The list of event processes */ - -#if 0 -struct ev_entry -{ - rb_dlink_node node; - EVH *func; - void *arg; - const char *name; - time_t frequency; - time_t when; - void *data; -}; -#endif /* * struct ev_entry * @@ -104,7 +91,7 @@ rb_event_add(const char *name, EVH * func, void *arg, time_t when) struct ev_entry *ev; ev = rb_malloc(sizeof(struct ev_entry)); ev->func = func; - ev->name = name; + ev->name = rb_strndup(name, EV_NAME_LEN); ev->arg = arg; ev->when = rb_current_time() + when; ev->frequency = when; @@ -117,13 +104,14 @@ rb_event_add(const char *name, EVH * func, void *arg, time_t when) rb_io_sched_event(ev, when); return ev; } + struct ev_entry * rb_event_addonce(const char *name, EVH * func, void *arg, time_t when) { struct ev_entry *ev; ev = rb_malloc(sizeof(struct ev_entry)); ev->func = func; - ev->name = name; + ev->name = rb_strndup(name, EV_NAME_LEN); ev->arg = arg; ev->when = rb_current_time() + when; ev->frequency = 0; @@ -151,6 +139,7 @@ rb_event_delete(struct ev_entry *ev) rb_dlinkDelete(&ev->node, &event_list); rb_io_unsched_event(ev); + rb_free(ev->name); rb_free(ev); } @@ -162,7 +151,7 @@ rb_event_delete(struct ev_entry *ev) * Side Effects: Removes the event from the event list */ void -rb_event_find_delete(EVH *func, void *arg) +rb_event_find_delete(EVH * func, void *arg) { rb_event_delete(rb_event_find(func, arg)); } @@ -196,7 +185,7 @@ rb_event_addish(const char *name, EVH * func, void *arg, time_t delta_ish) void rb_run_event(struct ev_entry *ev) { - last_event_ran = ev->name; + rb_strlcpy(last_event_ran, ev->name, sizeof(last_event_ran)); ev->func(ev->arg); if(!ev->frequency) { @@ -222,7 +211,7 @@ rb_event_run(void) { rb_dlink_node *ptr, *next; struct ev_entry *ev; - + if(rb_io_supports_event()) return; @@ -232,11 +221,11 @@ rb_event_run(void) ev = ptr->data; if(ev->when <= rb_current_time()) { - last_event_ran = ev->name; + rb_strlcpy(last_event_ran, ev->name, sizeof(last_event_ran)); ev->func(ev->arg); /* event is scheduled more than once */ - if(ev->frequency) + if(ev->frequency) { ev->when = rb_current_time() + ev->frequency; if((ev->when < event_time_min) || (event_time_min == -1)) @@ -247,7 +236,9 @@ rb_event_run(void) rb_dlinkDelete(&ev->node, &event_list); rb_free(ev); } - } else { + } + else + { if((ev->when < event_time_min) || (event_time_min == -1)) event_time_min = ev->when; } @@ -262,14 +253,15 @@ rb_event_io_register_all(void) int when; if(!rb_io_supports_event()) return; - + RB_DLINK_FOREACH(ptr, event_list.head) - { + { ev = ptr->data; when = ev->when - rb_current_time(); rb_io_sched_event(ev, when); } } + /* * void rb_event_init(void) * @@ -280,7 +272,7 @@ rb_event_io_register_all(void) void rb_event_init(void) { - last_event_ran = NULL; + rb_strlcpy(last_event_ran, "NONE", sizeof(last_event_ran)); } void @@ -291,11 +283,10 @@ rb_dump_events(void (*func) (char *, void *), void *ptr) rb_dlink_node *dptr; struct ev_entry *ev; len = sizeof(buf); - if(last_event_ran) - { - rb_snprintf(buf, len, "Last event to run: %s", last_event_ran); - func(buf, ptr); - } + + rb_snprintf(buf, len, "Last event to run: %s", last_event_ran); + func(buf, ptr); + rb_strlcpy(buf, "Operation Next Execution", len); func(buf, ptr); @@ -303,7 +294,7 @@ rb_dump_events(void (*func) (char *, void *), void *ptr) { ev = dptr->data; rb_snprintf(buf, len, "%-28s %-4ld seconds", ev->name, - ev->when - (long) rb_current_time()); + ev->when - (long)rb_current_time()); func(buf, ptr); } } diff --git a/libratbox/src/export-syms.txt b/libratbox/src/export-syms.txt index d742850..188b47a 100644 --- a/libratbox/src/export-syms.txt +++ b/libratbox/src/export-syms.txt @@ -146,3 +146,10 @@ rb_supports_ssl rb_ssl_handshake_count rb_ssl_clear_handshake_count rb_get_pseudo_random +rb_strerror +rb_kill +rb_setenv +rb_getpid +rb_waitpid +rb_basename +rb_dirname diff --git a/libratbox/src/gnutls.c b/libratbox/src/gnutls.c index 217dc54..51a25de 100644 --- a/libratbox/src/gnutls.c +++ b/libratbox/src/gnutls.c @@ -20,7 +20,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: gnutls.c 25793 2008-07-29 14:47:48Z androsyn $ + * $Id: gnutls.c 26092 2008-09-19 15:13:52Z androsyn $ */ #include @@ -40,12 +40,12 @@ static gnutls_dh_params dh_params; #define SSL_P(x) *((gnutls_session_t *)F->ssl) void -rb_ssl_shutdown(rb_fde_t * F) +rb_ssl_shutdown(rb_fde_t *F) { int i; if(F == NULL || F->ssl == NULL) return; - for (i = 0; i < 4; i++) + for(i = 0; i < 4; i++) { if(gnutls_bye(SSL_P(F), GNUTLS_SHUT_RDWR) == GNUTLS_E_SUCCESS) break; @@ -67,17 +67,18 @@ rb_ssl_clear_handshake_count(rb_fde_t *F) } static void -rb_ssl_timeout(rb_fde_t * F, void *notused) +rb_ssl_timeout(rb_fde_t *F, void *notused) { lrb_assert(F->accept != NULL); F->accept->callback(F, RB_ERR_TIMEOUT, NULL, 0, F->accept->data); } -static int do_ssl_handshake(rb_fde_t *F, PF *callback) +static int +do_ssl_handshake(rb_fde_t *F, PF * callback) { int ret; - int flags; + int flags; ret = gnutls_handshake(SSL_P(F)); if(ret < 0) @@ -88,17 +89,17 @@ static int do_ssl_handshake(rb_fde_t *F, PF *callback) flags = RB_SELECT_READ; else flags = RB_SELECT_WRITE; - rb_setselect(F, flags, callback, NULL); + rb_setselect(F, flags, callback, NULL); return 0; } F->ssl_errno = ret; return -1; - } - return 1; /* handshake is finished..go about life */ + } + return 1; /* handshake is finished..go about life */ } static void -rb_ssl_tryaccept(rb_fde_t * F, void *data) +rb_ssl_tryaccept(rb_fde_t *F, void *data) { int ret; struct acceptdata *ad; @@ -106,46 +107,45 @@ rb_ssl_tryaccept(rb_fde_t * F, void *data) lrb_assert(F->accept != NULL); ret = do_ssl_handshake(F, rb_ssl_tryaccept); - - switch(ret) + + switch (ret) { - case -1: - F->accept->callback(F, RB_ERROR_SSL, NULL, 0, F->accept->data); - break; - case 0: - /* do_ssl_handshake does the rb_setselect stuff */ - return; - default: - break; - - + case -1: + F->accept->callback(F, RB_ERROR_SSL, NULL, 0, F->accept->data); + break; + case 0: + /* do_ssl_handshake does the rb_setselect stuff */ + return; + default: + break; + + } rb_settimeout(F, 0, NULL, NULL); rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL); - + ad = F->accept; F->accept = NULL; - ad->callback(F, RB_OK, (struct sockaddr *) &ad->S, ad->addrlen, - ad->data); + ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data); rb_free(ad); } void -rb_ssl_start_accepted(rb_fde_t * new_F, ACCB * cb, void *data, int timeout) +rb_ssl_start_accepted(rb_fde_t *new_F, ACCB * cb, void *data, int timeout) { gnutls_session_t *ssl; new_F->type |= RB_FD_SSL; ssl = new_F->ssl = rb_malloc(sizeof(gnutls_session_t)); new_F->accept = rb_malloc(sizeof(struct acceptdata)); - + new_F->accept->callback = cb; new_F->accept->data = data; rb_settimeout(new_F, timeout, rb_ssl_timeout, NULL); new_F->accept->addrlen = 0; - gnutls_init(ssl, GNUTLS_SERVER); + gnutls_init(ssl, GNUTLS_SERVER); gnutls_set_default_priority(*ssl); gnutls_credentials_set(*ssl, GNUTLS_CRD_CERTIFICATE, x509); gnutls_dh_set_prime_bits(*ssl, 1024); @@ -154,8 +154,8 @@ rb_ssl_start_accepted(rb_fde_t * new_F, ACCB * cb, void *data, int timeout) { struct acceptdata *ad = new_F->accept; new_F->accept = NULL; - ad->callback(new_F, RB_OK, (struct sockaddr *) &ad->S, ad->addrlen, ad->data); - rb_free(ad); + ad->callback(new_F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data); + rb_free(ad); } } @@ -164,7 +164,7 @@ rb_ssl_start_accepted(rb_fde_t * new_F, ACCB * cb, void *data, int timeout) void -rb_ssl_accept_setup(rb_fde_t * F, rb_fde_t *new_F, struct sockaddr *st, int addrlen) +rb_ssl_accept_setup(rb_fde_t *F, rb_fde_t *new_F, struct sockaddr *st, int addrlen) { new_F->type |= RB_FD_SSL; new_F->ssl = rb_malloc(sizeof(gnutls_session_t)); @@ -176,7 +176,7 @@ rb_ssl_accept_setup(rb_fde_t * F, rb_fde_t *new_F, struct sockaddr *st, int addr memcpy(&new_F->accept->S, st, addrlen); new_F->accept->addrlen = addrlen; - gnutls_init((gnutls_session_t *)new_F->ssl, GNUTLS_SERVER); + gnutls_init((gnutls_session_t *) new_F->ssl, GNUTLS_SERVER); gnutls_set_default_priority(SSL_P(new_F)); gnutls_credentials_set(SSL_P(new_F), GNUTLS_CRD_CERTIFICATE, x509); gnutls_dh_set_prime_bits(SSL_P(new_F), 1024); @@ -185,8 +185,8 @@ rb_ssl_accept_setup(rb_fde_t * F, rb_fde_t *new_F, struct sockaddr *st, int addr { struct acceptdata *ad = F->accept; F->accept = NULL; - ad->callback(F, RB_OK, (struct sockaddr *) &ad->S, ad->addrlen, ad->data); - rb_free(ad); + ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data); + rb_free(ad); } } @@ -194,7 +194,7 @@ rb_ssl_accept_setup(rb_fde_t * F, rb_fde_t *new_F, struct sockaddr *st, int addr static ssize_t -rb_ssl_read_or_write(int r_or_w, rb_fde_t * F, void *rbuf, const void *wbuf, size_t count) +rb_ssl_read_or_write(int r_or_w, rb_fde_t *F, void *rbuf, const void *wbuf, size_t count) { ssize_t ret; gnutls_session_t *ssl = F->ssl; @@ -206,7 +206,7 @@ rb_ssl_read_or_write(int r_or_w, rb_fde_t * F, void *rbuf, const void *wbuf, siz if(ret < 0) { - switch(ret) + switch (ret) { case GNUTLS_E_AGAIN: case GNUTLS_E_INTERRUPTED: @@ -228,13 +228,13 @@ rb_ssl_read_or_write(int r_or_w, rb_fde_t * F, void *rbuf, const void *wbuf, siz } ssize_t -rb_ssl_read(rb_fde_t * F, void *buf, size_t count) +rb_ssl_read(rb_fde_t *F, void *buf, size_t count) { return rb_ssl_read_or_write(0, F, buf, NULL, count); } ssize_t -rb_ssl_write(rb_fde_t * F, const void *buf, size_t count) +rb_ssl_write(rb_fde_t *F, const void *buf, size_t count) { return rb_ssl_read_or_write(1, F, NULL, buf, count); } @@ -249,7 +249,7 @@ int rb_init_ssl(void) { gnutls_global_init(); - + if(gnutls_certificate_allocate_credentials(&x509) != GNUTLS_E_SUCCESS) { rb_lib_log("rb_init_ssl: Unable to allocate SSL/TLS certificate credentials"); @@ -260,7 +260,7 @@ rb_init_ssl(void) } static void -rb_free_datum_t(gnutls_datum_t *d) +rb_free_datum_t(gnutls_datum_t * d) { rb_free(d->data); rb_free(d); @@ -279,15 +279,15 @@ rb_load_file_into_datum_t(const char *file) datum = rb_malloc(sizeof(gnutls_datum_t)); - if(fileinfo.st_size > 131072) /* deal with retards */ + if(fileinfo.st_size > 131072) /* deal with retards */ datum->size = 131072; else - datum->size = fileinfo.st_size; - + datum->size = fileinfo.st_size; + datum->data = rb_malloc(datum->size + 1); fread(datum->data, datum->size, 1, f); fclose(f); - return datum; + return datum; } int @@ -312,16 +312,19 @@ rb_setup_ssl_server(const char *cert, const char *keyfile, const char *dhfile) rb_lib_log("rb_setup_ssl_server: Error loading key: %s", strerror(errno)); return 0; } - - - if((ret = gnutls_certificate_set_x509_key_mem(x509, d_cert, d_key, GNUTLS_X509_FMT_PEM)) != GNUTLS_E_SUCCESS) + + + if((ret = + gnutls_certificate_set_x509_key_mem(x509, d_cert, d_key, + GNUTLS_X509_FMT_PEM)) != GNUTLS_E_SUCCESS) { - rb_lib_log("rb_setup_ssl_server: Error loading certificate or key file: %s", gnutls_strerror(ret)); + rb_lib_log("rb_setup_ssl_server: Error loading certificate or key file: %s", + gnutls_strerror(ret)); return 0; } rb_free_datum_t(d_cert); rb_free_datum_t(d_key); - + if(dhfile != NULL) { if(gnutls_dh_params_init(&dh_params) == GNUTLS_E_SUCCESS) @@ -331,20 +334,24 @@ rb_setup_ssl_server(const char *cert, const char *keyfile, const char *dhfile) data = rb_load_file_into_datum_t(dhfile); if(data != NULL) { - xret = gnutls_dh_params_import_pkcs3(dh_params, data, GNUTLS_X509_FMT_PEM); + xret = gnutls_dh_params_import_pkcs3(dh_params, data, + GNUTLS_X509_FMT_PEM); if(xret < 0) - rb_lib_log("rb_setup_ssl_server: Error parsing DH file: %s\n", gnutls_strerror(xret)); + rb_lib_log + ("rb_setup_ssl_server: Error parsing DH file: %s\n", + gnutls_strerror(xret)); rb_free_datum_t(data); } gnutls_certificate_set_dh_params(x509, dh_params); - } else + } + else rb_lib_log("rb_setup_ssl_server: Unable to setup DH parameters"); } return 1; } int -rb_ssl_listen(rb_fde_t * F, int backlog) +rb_ssl_listen(rb_fde_t *F, int backlog) { F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL; return listen(F->fd, backlog); @@ -358,7 +365,7 @@ struct ssl_connect }; static void -rb_ssl_connect_realcb(rb_fde_t * F, int status, struct ssl_connect *sconn) +rb_ssl_connect_realcb(rb_fde_t *F, int status, struct ssl_connect *sconn) { F->connect->callback = sconn->callback; F->connect->data = sconn->data; @@ -367,37 +374,37 @@ rb_ssl_connect_realcb(rb_fde_t * F, int status, struct ssl_connect *sconn) } static void -rb_ssl_tryconn_timeout_cb(rb_fde_t * F, void *data) +rb_ssl_tryconn_timeout_cb(rb_fde_t *F, void *data) { rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data); } static void -rb_ssl_tryconn_cb(rb_fde_t * F, void *data) +rb_ssl_tryconn_cb(rb_fde_t *F, void *data) { struct ssl_connect *sconn = data; int ret; - ret = do_ssl_handshake(F, rb_ssl_tryconn_cb); - - switch(ret) - { - case -1: - rb_ssl_connect_realcb(F, RB_ERROR_SSL, sconn); - break; - case 0: - /* do_ssl_handshake does the rb_setselect stuff */ - return; - default: - break; - - - } + ret = do_ssl_handshake(F, rb_ssl_tryconn_cb); + + switch (ret) + { + case -1: + rb_ssl_connect_realcb(F, RB_ERROR_SSL, sconn); + break; + case 0: + /* do_ssl_handshake does the rb_setselect stuff */ + return; + default: + break; + + + } rb_ssl_connect_realcb(F, RB_OK, sconn); } static void -rb_ssl_tryconn(rb_fde_t * F, int status, void *data) +rb_ssl_tryconn(rb_fde_t *F, int status, void *data) { struct ssl_connect *sconn = data; if(status != RB_OK) @@ -408,11 +415,11 @@ rb_ssl_tryconn(rb_fde_t * F, int status, void *data) F->type |= RB_FD_SSL; - + rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); F->ssl = rb_malloc(sizeof(gnutls_session_t)); - gnutls_init(F->ssl, GNUTLS_CLIENT); - gnutls_set_default_priority(SSL_P(F)); + gnutls_init(F->ssl, GNUTLS_CLIENT); + gnutls_set_default_priority(SSL_P(F)); gnutls_dh_set_prime_bits(SSL_P(F), 1024); gnutls_transport_set_ptr(SSL_P(F), (gnutls_transport_ptr_t) (long int)F->fd); @@ -423,7 +430,7 @@ rb_ssl_tryconn(rb_fde_t * F, int status, void *data) } void -rb_connect_tcp_ssl(rb_fde_t * F, struct sockaddr *dest, +rb_connect_tcp_ssl(rb_fde_t *F, struct sockaddr *dest, struct sockaddr *clocal, int socklen, CNCB * callback, void *data, int timeout) { struct ssl_connect *sconn; @@ -439,7 +446,7 @@ rb_connect_tcp_ssl(rb_fde_t * F, struct sockaddr *dest, } void -rb_ssl_start_connected(rb_fde_t * F, CNCB * callback, void *data, int timeout) +rb_ssl_start_connected(rb_fde_t *F, CNCB * callback, void *data, int timeout) { struct ssl_connect *sconn; if(F == NULL) @@ -454,12 +461,12 @@ rb_ssl_start_connected(rb_fde_t * F, CNCB * callback, void *data, int timeout) F->connect->data = data; F->type |= RB_FD_SSL; F->ssl = rb_malloc(sizeof(gnutls_session_t)); - - gnutls_init(F->ssl, GNUTLS_CLIENT); - gnutls_set_default_priority(SSL_P(F)); + + gnutls_init(F->ssl, GNUTLS_CLIENT); + gnutls_set_default_priority(SSL_P(F)); gnutls_dh_set_prime_bits(SSL_P(F), 1024); gnutls_transport_set_ptr(SSL_P(F), (gnutls_transport_ptr_t) (long int)F->fd); - + rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); if(do_ssl_handshake(F, rb_ssl_tryconn_cb)) @@ -490,7 +497,7 @@ rb_get_pseudo_random(void *buf, size_t length) } const char * -rb_get_ssl_strerror(rb_fde_t * F) +rb_get_ssl_strerror(rb_fde_t *F) { return gnutls_strerror(F->ssl_errno); } diff --git a/libratbox/src/helper.c b/libratbox/src/helper.c index a4a6260..a51fa5d 100644 --- a/libratbox/src/helper.c +++ b/libratbox/src/helper.c @@ -19,7 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: helper.c 25245 2008-04-21 18:38:23Z androsyn $ + * $Id: helper.c 26092 2008-09-19 15:13:52Z androsyn $ */ #include #include @@ -41,31 +41,32 @@ struct _rb_helper /* setup all the stuff a new child needs */ rb_helper * -rb_helper_child(rb_helper_cb *read_cb, rb_helper_cb *error_cb, log_cb *ilog, restart_cb *irestart, die_cb *idie, - int maxcon, size_t lb_heap_size, size_t dh_size, size_t fd_heap_size) +rb_helper_child(rb_helper_cb * read_cb, rb_helper_cb * error_cb, log_cb * ilog, + restart_cb * irestart, die_cb * idie, int maxcon, size_t lb_heap_size, + size_t dh_size, size_t fd_heap_size) { rb_helper *helper; int maxfd, x = 0; int ifd, ofd; char *tifd, *tofd, *tmaxfd; - + tifd = getenv("IFD"); tofd = getenv("OFD"); tmaxfd = getenv("MAXFD"); - + if(tifd == NULL || tofd == NULL || tmaxfd == NULL) return NULL; helper = rb_malloc(sizeof(rb_helper)); - ifd = (int)strtol(tifd, NULL, 10); + ifd = (int)strtol(tifd, NULL, 10); ofd = (int)strtol(tofd, NULL, 10); maxfd = (int)strtol(tmaxfd, NULL, 10); -#ifndef WIN32 - for(x = 0; x < maxfd; x++) - { - if(x != ifd && x != ofd) - close(x); +#ifndef _WIN32 + for(x = 0; x < maxfd; x++) + { + if(x != ifd && x != ofd) + close(x); } x = open("/dev/null", O_RDWR); if(ifd != 0 && ofd != 0) @@ -74,10 +75,10 @@ rb_helper_child(rb_helper_cb *read_cb, rb_helper_cb *error_cb, log_cb *ilog, res dup2(x, 1); if(ifd != 2 && ofd != 2) dup2(x, 2); - if(x > 2) /* don't undo what we just did */ + if(x > 2) /* don't undo what we just did */ close(x); #else - x = 0; /* shut gcc up */ + x = 0; /* shut gcc up */ #endif rb_lib_init(ilog, irestart, idie, 0, maxfd, dh_size, fd_heap_size); @@ -89,7 +90,7 @@ rb_helper_child(rb_helper_cb *read_cb, rb_helper_cb *error_cb, log_cb *ilog, res helper->ofd = rb_open(ofd, RB_FD_PIPE, "outgoing connection"); rb_set_nb(helper->ifd); rb_set_nb(helper->ofd); - + helper->read_cb = read_cb; helper->error_cb = error_cb; return helper; @@ -102,7 +103,8 @@ rb_helper_child(rb_helper_cb *read_cb, rb_helper_cb *error_cb, log_cb *ilog, res */ rb_helper * -rb_helper_start(const char *name, const char *fullpath, rb_helper_cb *read_cb, rb_helper_cb *error_cb) +rb_helper_start(const char *name, const char *fullpath, rb_helper_cb * read_cb, + rb_helper_cb * error_cb) { rb_helper *helper; const char *parv[2]; @@ -111,14 +113,14 @@ rb_helper_start(const char *name, const char *fullpath, rb_helper_cb *read_cb, r rb_fde_t *in_f[2]; rb_fde_t *out_f[2]; pid_t pid; - + if(access(fullpath, X_OK) == -1) return NULL; - + helper = rb_malloc(sizeof(rb_helper)); rb_snprintf(buf, sizeof(buf), "%s helper - read", name); - if(rb_pipe(&in_f[0], &in_f[1], buf) < 0) + if(rb_pipe(&in_f[0], &in_f[1], buf) < 0) { rb_free(helper); return NULL; @@ -129,30 +131,30 @@ rb_helper_start(const char *name, const char *fullpath, rb_helper_cb *read_cb, r rb_free(helper); return NULL; } - + rb_snprintf(fx, sizeof(fx), "%d", rb_get_fd(in_f[1])); rb_snprintf(fy, sizeof(fy), "%d", rb_get_fd(out_f[0])); - + rb_set_nb(in_f[0]); rb_set_nb(in_f[1]); rb_set_nb(out_f[0]); rb_set_nb(out_f[1]); - - setenv("IFD", fy, 1); - setenv("OFD", fx, 1); - setenv("MAXFD", "256", 1); - + + rb_setenv("IFD", fy, 1); + rb_setenv("OFD", fx, 1); + rb_setenv("MAXFD", "256", 1); + rb_snprintf(buf, sizeof(buf), "-ircd %s daemon", name); parv[0] = buf; parv[1] = NULL; -#ifdef WIN32 - SetHandleInformation((HANDLE)rb_get_fd(in_f[1]), HANDLE_FLAG_INHERIT, 1); - SetHandleInformation((HANDLE)rb_get_fd(out_f[0]), HANDLE_FLAG_INHERIT, 1); +#ifdef _WIN32 + SetHandleInformation((HANDLE) rb_get_fd(in_f[1]), HANDLE_FLAG_INHERIT, 1); + SetHandleInformation((HANDLE) rb_get_fd(out_f[0]), HANDLE_FLAG_INHERIT, 1); #endif - + pid = rb_spawn_process(fullpath, (const char **)parv); - + if(pid == -1) { rb_close(in_f[0]); @@ -165,14 +167,14 @@ rb_helper_start(const char *name, const char *fullpath, rb_helper_cb *read_cb, r rb_close(in_f[1]); rb_close(out_f[0]); - + rb_linebuf_newbuf(&helper->sendq); rb_linebuf_newbuf(&helper->recvq); - + helper->ifd = in_f[0]; helper->ofd = out_f[1]; helper->read_cb = read_cb; - helper->error_cb = error_cb; + helper->error_cb = error_cb; helper->fork_count = 0; helper->pid = pid; @@ -192,12 +194,13 @@ rb_helper_write_sendq(rb_fde_t *F, void *helper_ptr) { rb_helper *helper = helper_ptr; int retlen; - + if(rb_linebuf_len(&helper->sendq) > 0) { while((retlen = rb_linebuf_flush(F, &helper->sendq)) > 0) ;; - if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) { + if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) + { rb_helper_restart(helper); return; } @@ -207,7 +210,7 @@ rb_helper_write_sendq(rb_fde_t *F, void *helper_ptr) rb_setselect(helper->ofd, RB_SELECT_WRITE, rb_helper_write_sendq, helper); } -void +void rb_helper_write_queue(rb_helper *helper, const char *format, ...) { va_list ap; @@ -219,7 +222,7 @@ rb_helper_write_queue(rb_helper *helper, const char *format, ...) void rb_helper_write_flush(rb_helper *helper) { - rb_helper_write_sendq(helper->ofd, helper); + rb_helper_write_sendq(helper->ofd, helper); } @@ -241,7 +244,7 @@ rb_helper_read_cb(rb_fde_t *F, void *data) int length; if(helper == NULL) return; - + while((length = rb_read(helper->ifd, buf, sizeof(buf))) > 0) { rb_linebuf_parse(&helper->recvq, buf, length, 0); @@ -253,7 +256,7 @@ rb_helper_read_cb(rb_fde_t *F, void *data) rb_helper_restart(helper); return; } - + rb_setselect(helper->ifd, RB_SELECT_READ, rb_helper_read_cb, helper); } @@ -271,10 +274,10 @@ rb_helper_close(rb_helper *helper) { if(helper == NULL) return; - kill(helper->pid, SIGKILL); + rb_kill(helper->pid, SIGKILL); rb_close(helper->ifd); rb_close(helper->ofd); - rb_free(helper); + rb_free(helper); } int @@ -292,4 +295,3 @@ rb_helper_loop(rb_helper *helper, long delay) rb_lib_loop(delay); } } - diff --git a/libratbox/src/kqueue.c b/libratbox/src/kqueue.c index 51d92c8..08f3666 100644 --- a/libratbox/src/kqueue.c +++ b/libratbox/src/kqueue.c @@ -22,7 +22,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: kqueue.c 25364 2008-05-14 17:55:22Z jilles $ + * $Id: kqueue.c 26092 2008-09-19 15:13:52Z androsyn $ */ #include @@ -63,13 +63,13 @@ static int kqoff; /* offset into the buffer */ int -rb_setup_fd_kqueue(rb_fde_t * F) +rb_setup_fd_kqueue(rb_fde_t *F) { return 0; } static void -kq_update_events(rb_fde_t * F, short filter, PF * handler) +kq_update_events(rb_fde_t *F, short filter, PF * handler) { PF *cur_handler; int kep_flags; @@ -103,7 +103,7 @@ kq_update_events(rb_fde_t * F, short filter, PF * handler) kep_flags = EV_DELETE; } - EV_SET(kep, (uintptr_t) F->fd, filter, kep_flags, 0, 0, (void *) F); + EV_SET(kep, (uintptr_t)F->fd, filter, kep_flags, 0, 0, (void *)F); if(++kqoff == kqmax) { @@ -117,13 +117,14 @@ kq_update_events(rb_fde_t * F, short filter, PF * handler) * because that would also return events we cannot * process at this point. */ - for (i = 0; i < kqoff; i++) + for(i = 0; i < kqoff; i++) { ret = kevent(kq, kqlst + i, 1, NULL, 0, &zero_timespec); /* jdc -- someone needs to do error checking... */ /* EBADF is normal here -- jilles */ if(ret == -1 && errno != EBADF) - rb_lib_log("kq_update_events(): kevent(): %s", strerror(errno)); + rb_lib_log("kq_update_events(): kevent(): %s", + strerror(errno)); } kqoff = 0; } @@ -167,7 +168,7 @@ rb_init_netio_kqueue(void) * and deregister interest in a pending IO state for a given FD. */ void -rb_setselect_kqueue(rb_fde_t * F, unsigned int type, PF * handler, void *client_data) +rb_setselect_kqueue(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { lrb_assert(IsFDOpen(F)); @@ -209,16 +210,18 @@ rb_select_kqueue(long delay) rb_fde_t *F; - if(delay < 0) { + if(delay < 0) + { pt = NULL; } - else { + else + { pt = &poll_time; poll_time.tv_sec = delay / 1000; poll_time.tv_nsec = (delay % 1000) * 1000000; } - for (;;) + for(;;) { num = kevent(kq, kqlst, kqoff, kqout, kqmax, pt); kqoff = 0; @@ -241,7 +244,7 @@ rb_select_kqueue(long delay) if(num == 0) return RB_OK; /* No error.. */ - for (i = 0; i < num; i++) + for(i = 0; i < num; i++) { PF *hdl = NULL; @@ -305,7 +308,7 @@ rb_kqueue_supports_event(void) ts.tv_nsec = 1000; - EV_SET(&kv, (uintptr_t) 0x0, EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, 1, 0); + EV_SET(&kv, (uintptr_t)0x0, EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, 1, 0); if(kevent(xkq, &kv, 1, NULL, 0, NULL) < 0) { can_do_event = -1; @@ -326,9 +329,9 @@ rb_kqueue_sched_event(struct ev_entry *event, int when) kep_flags = EV_ADD; if(event->frequency == 0) kep_flags |= EV_ONESHOT; - EV_SET(&kev, (uintptr_t) event, EVFILT_TIMER, kep_flags, 0, when * 1000, event); + EV_SET(&kev, (uintptr_t)event, EVFILT_TIMER, kep_flags, 0, when * 1000, event); if(kevent(kq, &kev, 1, NULL, 0, NULL) < 0) - return 0; + return 0; return 1; } @@ -336,7 +339,7 @@ void rb_kqueue_unsched_event(struct ev_entry *event) { struct kevent kev; - EV_SET(&kev, (uintptr_t) event, EVFILT_TIMER, EV_DELETE, 0, 0, event); + EV_SET(&kev, (uintptr_t)event, EVFILT_TIMER, EV_DELETE, 0, 0, event); kevent(kq, &kev, 1, NULL, 0, NULL); } @@ -356,7 +359,7 @@ rb_init_netio_kqueue(void) } void -rb_setselect_kqueue(rb_fde_t * F, unsigned int type, PF * handler, void *client_data) +rb_setselect_kqueue(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { errno = ENOSYS; return; @@ -370,7 +373,7 @@ rb_select_kqueue(long delay) } int -rb_setup_fd_kqueue(rb_fde_t * F) +rb_setup_fd_kqueue(rb_fde_t *F) { errno = ENOSYS; return -1; diff --git a/libratbox/src/linebuf.c b/libratbox/src/linebuf.c index a6c05f3..5e7fbb4 100644 --- a/libratbox/src/linebuf.c +++ b/libratbox/src/linebuf.c @@ -21,7 +21,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: linebuf.c 25375 2008-05-16 15:19:51Z androsyn $ + * $Id: linebuf.c 26092 2008-09-19 15:13:52Z androsyn $ */ #include @@ -47,20 +47,14 @@ static int bufline_count = 0; void rb_linebuf_init(size_t heap_size) { -#ifndef NOBALLOC rb_linebuf_heap = rb_bh_create(sizeof(buf_line_t), heap_size, "librb_linebuf_heap"); -#endif } static buf_line_t * rb_linebuf_allocate(void) { buf_line_t *t; -#ifndef NOBALLOC t = rb_bh_alloc(rb_linebuf_heap); -#else - t = rb_malloc(sizeof(buf_line_t)); -#endif return (t); } @@ -68,11 +62,7 @@ rb_linebuf_allocate(void) static void rb_linebuf_free(buf_line_t * p) { -#ifndef NOBALLOC rb_bh_free(rb_linebuf_heap, p); -#else - rb_free(p); -#endif } /* @@ -113,7 +103,7 @@ rb_linebuf_new_line(buf_head_t * bufhead) * We've finished with the given line, so deallocate it */ static void -rb_linebuf_done_line(buf_head_t * bufhead, buf_line_t * bufline, rb_dlink_node * node) +rb_linebuf_done_line(buf_head_t * bufhead, buf_line_t * bufline, rb_dlink_node *node) { /* Remove it from the linked list */ rb_dlinkDestroy(node, &bufhead->list); @@ -188,7 +178,8 @@ rb_linebuf_donebuf(buf_head_t * bufhead) { while(bufhead->list.head != NULL) { - rb_linebuf_done_line(bufhead, (buf_line_t *) bufhead->list.head->data, bufhead->list.head); + rb_linebuf_done_line(bufhead, (buf_line_t *) bufhead->list.head->data, + bufhead->list.head); } } @@ -515,7 +506,8 @@ rb_linebuf_attach(buf_head_t * bufhead, buf_head_t * new) * Then format/va_args is appended to the buffer. */ void -rb_linebuf_putmsg(buf_head_t * bufhead, const char *format, va_list * va_args, const char *prefixfmt, ...) +rb_linebuf_putmsg(buf_head_t * bufhead, const char *format, va_list * va_args, + const char *prefixfmt, ...) { buf_line_t *bufline; int len = 0; @@ -562,7 +554,8 @@ rb_linebuf_putmsg(buf_head_t * bufhead, const char *format, va_list * va_args, c else { /* Chop trailing CRLF's .. */ - while((bufline->buf[len] == '\r') || (bufline->buf[len] == '\n') || (bufline->buf[len] == '\0')) + while((bufline->buf[len] == '\r') || (bufline->buf[len] == '\n') + || (bufline->buf[len] == '\0')) { len--; } @@ -577,7 +570,7 @@ rb_linebuf_putmsg(buf_head_t * bufhead, const char *format, va_list * va_args, c } void -rb_linebuf_putbuf(buf_head_t *bufhead, const char *buffer) +rb_linebuf_putbuf(buf_head_t * bufhead, const char *buffer) { buf_line_t *bufline; int len = 0; @@ -614,7 +607,8 @@ rb_linebuf_putbuf(buf_head_t *bufhead, const char *buffer) else { /* Chop trailing CRLF's .. */ - while((bufline->buf[len] == '\r') || (bufline->buf[len] == '\n') || (bufline->buf[len] == '\0')) + while((bufline->buf[len] == '\r') || (bufline->buf[len] == '\n') + || (bufline->buf[len] == '\0')) { len--; } @@ -627,7 +621,7 @@ rb_linebuf_putbuf(buf_head_t *bufhead, const char *buffer) bufline->len = len; bufhead->len += len; - + } void @@ -673,7 +667,8 @@ rb_linebuf_put(buf_head_t * bufhead, const char *format, ...) else { /* Chop trailing CRLF's .. */ - while((bufline->buf[len] == '\r') || (bufline->buf[len] == '\n') || (bufline->buf[len] == '\0')) + while((bufline->buf[len] == '\r') || (bufline->buf[len] == '\n') + || (bufline->buf[len] == '\0')) { len--; } @@ -729,7 +724,7 @@ rb_linebuf_flush(rb_fde_t *F, buf_head_t * bufhead) } ptr = bufhead->list.head; - + bufline = ptr->data; if(!bufline->terminated) { @@ -750,12 +745,13 @@ rb_linebuf_flush(rb_fde_t *F, buf_head_t * bufhead) bufline = ptr->data; if(!bufline->terminated) break; - + vec[x].iov_base = bufline->buf; vec[x].iov_len = bufline->len; ptr = ptr->next; - } while(++x < RB_UIO_MAXIOV); + } + while(++x < RB_UIO_MAXIOV); if(x == 0) { @@ -780,7 +776,7 @@ rb_linebuf_flush(rb_fde_t *F, buf_head_t * bufhead) rb_linebuf_done_line(bufhead, bufline, bufhead->list.head); bufhead->writeofs = 0; } - else + else { bufhead->writeofs += xret; break; @@ -789,10 +785,10 @@ rb_linebuf_flush(rb_fde_t *F, buf_head_t * bufhead) return retval; } -#endif +#endif + + /* this is the non-writev case */ - /* this is the non-writev case */ - /* Check we actually have a first buffer */ if(bufhead->list.head == NULL) { @@ -839,12 +835,7 @@ rb_linebuf_flush(rb_fde_t *F, buf_head_t * bufhead) */ void -rb_count_rb_linebuf_memory(size_t * count, size_t * rb_linebuf_memory_used) +rb_count_rb_linebuf_memory(size_t *count, size_t *rb_linebuf_memory_used) { -#ifndef NOBALLOC rb_bh_usage(rb_linebuf_heap, count, NULL, rb_linebuf_memory_used, NULL); -#else - *count = 0; - *rb_linebuf_memory_used = 0; -#endif } diff --git a/libratbox/src/nossl.c b/libratbox/src/nossl.c index f3758c5..125bed9 100644 --- a/libratbox/src/nossl.c +++ b/libratbox/src/nossl.c @@ -33,7 +33,7 @@ #include #include -int +int rb_setup_ssl_server(const char *cert, const char *keyfile, const char *dhfile) { errno = ENOSYS; @@ -60,9 +60,10 @@ rb_stir_arc4random(void *unused) { arc4random_stir(); } - -int rb_init_prng(const char *path, prng_seed_t seed_type) + +int +rb_init_prng(const char *path, prng_seed_t seed_type) { /* xxx this ignores the parameters above */ arc4random_stir(); @@ -74,8 +75,8 @@ int rb_get_random(void *buf, size_t length) { uint32_t rnd = 0, i; - uint8_t *xbuf = buf; - for (i = 0; i < sizeof(length); i++) + uint8_t *xbuf = buf; + for(i = 0; i < length; i++) { if(i % 4 == 0) rnd = arc4random(); @@ -88,7 +89,7 @@ rb_get_random(void *buf, size_t length) int rb_get_pseudo_random(void *buf, size_t length) { - return rb_get_random(buf, length); + return rb_get_random(buf, length); } @@ -99,21 +100,21 @@ rb_get_ssl_strerror(rb_fde_t *F) return nosupport; } -void -rb_ssl_start_accepted(rb_fde_t *new_F, ACCB *cb, void *data, int timeout) +void +rb_ssl_start_accepted(rb_fde_t *new_F, ACCB * cb, void *data, int timeout) { return; } -void -rb_ssl_start_connected(rb_fde_t *F, CNCB *callback, void *data, int timeout) +void +rb_ssl_start_connected(rb_fde_t *F, CNCB * callback, void *data, int timeout) { return; } void -rb_connect_tcp_ssl(rb_fde_t *F, struct sockaddr *dest, - struct sockaddr *clocal, int socklen, CNCB *callback, void *data, int timeout) +rb_connect_tcp_ssl(rb_fde_t *F, struct sockaddr *dest, + struct sockaddr *clocal, int socklen, CNCB * callback, void *data, int timeout) { return; } @@ -125,26 +126,26 @@ rb_supports_ssl(void) } void -rb_ssl_shutdown(rb_fde_t * F) -{ +rb_ssl_shutdown(rb_fde_t *F) +{ return; -} +} void -rb_ssl_accept_setup(rb_fde_t * F, rb_fde_t *new_F, struct sockaddr *st, int addrlen) +rb_ssl_accept_setup(rb_fde_t *F, rb_fde_t *new_F, struct sockaddr *st, int addrlen) { return; } ssize_t -rb_ssl_read(rb_fde_t * F, void *buf, size_t count) +rb_ssl_read(rb_fde_t *F, void *buf, size_t count) { errno = ENOSYS; return -1; } ssize_t -rb_ssl_write(rb_fde_t * F, const void *buf, size_t count) +rb_ssl_write(rb_fde_t *F, const void *buf, size_t count) { errno = ENOSYS; return -1; @@ -155,12 +156,11 @@ rb_ssl_handshake_count(rb_fde_t *F) { return 0; } - -void + +void rb_ssl_clear_handshake_count(rb_fde_t *F) { return; } - -#endif /* !HAVE_OPENSSL */ +#endif /* !HAVE_OPENSSL */ diff --git a/libratbox/src/openssl.c b/libratbox/src/openssl.c index d8efaa7..9a76063 100644 --- a/libratbox/src/openssl.c +++ b/libratbox/src/openssl.c @@ -39,13 +39,14 @@ static SSL_CTX *ssl_server_ctx; static SSL_CTX *ssl_client_ctx; static int libratbox_index = -1; -static unsigned long get_last_err(void) +static unsigned long +get_last_err(void) { unsigned long t_err, err = 0; err = ERR_get_error(); if(err == 0) return 0; - + while((t_err = ERR_get_error()) > 0) err = t_err; @@ -53,14 +54,14 @@ static unsigned long get_last_err(void) } void -rb_ssl_shutdown(rb_fde_t * F) +rb_ssl_shutdown(rb_fde_t *F) { int i; if(F == NULL || F->ssl == NULL) return; SSL_set_shutdown((SSL *) F->ssl, SSL_RECEIVED_SHUTDOWN); - for (i = 0; i < 4; i++) + for(i = 0; i < 4; i++) { if(SSL_shutdown((SSL *) F->ssl)) break; @@ -82,14 +83,15 @@ rb_ssl_clear_handshake_count(rb_fde_t *F) } static void -rb_ssl_timeout(rb_fde_t * F, void *notused) +rb_ssl_timeout(rb_fde_t *F, void *notused) { lrb_assert(F->accept != NULL); F->accept->callback(F, RB_ERR_TIMEOUT, NULL, 0, F->accept->data); } -static void rb_ssl_info_callback(SSL *ssl, int where, int ret) +static void +rb_ssl_info_callback(SSL * ssl, int where, int ret) { if(where & SSL_CB_HANDSHAKE_START) { @@ -97,18 +99,18 @@ static void rb_ssl_info_callback(SSL *ssl, int where, int ret) if(F == NULL) return; F->handshake_count++; - } + } } static void rb_setup_ssl_cb(rb_fde_t *F) { SSL_set_ex_data(F->ssl, libratbox_index, (char *)F); - SSL_set_info_callback((SSL *)F->ssl, (void *)rb_ssl_info_callback); + SSL_set_info_callback((SSL *) F->ssl, (void (*)(const SSL *,int,int))rb_ssl_info_callback); } static void -rb_ssl_tryaccept(rb_fde_t * F, void *data) +rb_ssl_tryaccept(rb_fde_t *F, void *data) { int ssl_err; lrb_assert(F->accept != NULL); @@ -143,11 +145,10 @@ rb_ssl_tryaccept(rb_fde_t * F, void *data) } rb_settimeout(F, 0, NULL, NULL); rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL); - + ad = F->accept; F->accept = NULL; - ad->callback(F, RB_OK, (struct sockaddr *) &ad->S, ad->addrlen, - ad->data); + ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data); rb_free(ad); } @@ -184,7 +185,7 @@ rb_ssl_accept_common(rb_fde_t *new_F) } void -rb_ssl_start_accepted(rb_fde_t * new_F, ACCB * cb, void *data, int timeout) +rb_ssl_start_accepted(rb_fde_t *new_F, ACCB * cb, void *data, int timeout) { new_F->type |= RB_FD_SSL; new_F->ssl = SSL_new(ssl_server_ctx); @@ -204,7 +205,7 @@ rb_ssl_start_accepted(rb_fde_t * new_F, ACCB * cb, void *data, int timeout) void -rb_ssl_accept_setup(rb_fde_t * F, rb_fde_t *new_F, struct sockaddr *st, int addrlen) +rb_ssl_accept_setup(rb_fde_t *F, rb_fde_t *new_F, struct sockaddr *st, int addrlen) { new_F->type |= RB_FD_SSL; new_F->ssl = SSL_new(ssl_server_ctx); @@ -222,16 +223,16 @@ rb_ssl_accept_setup(rb_fde_t * F, rb_fde_t *new_F, struct sockaddr *st, int addr } static ssize_t -rb_ssl_read_or_write(int r_or_w, rb_fde_t * F, void *rbuf, const void *wbuf, size_t count) +rb_ssl_read_or_write(int r_or_w, rb_fde_t *F, void *rbuf, const void *wbuf, size_t count) { ssize_t ret; unsigned long err; SSL *ssl = F->ssl; if(r_or_w == 0) - ret = (ssize_t)SSL_read(ssl, rbuf, (int) count); + ret = (ssize_t) SSL_read(ssl, rbuf, (int)count); else - ret = (ssize_t)SSL_write(ssl, wbuf, (int) count); + ret = (ssize_t) SSL_write(ssl, wbuf, (int)count); if(ret < 0) { @@ -269,13 +270,13 @@ rb_ssl_read_or_write(int r_or_w, rb_fde_t * F, void *rbuf, const void *wbuf, siz } ssize_t -rb_ssl_read(rb_fde_t * F, void *buf, size_t count) +rb_ssl_read(rb_fde_t *F, void *buf, size_t count) { return rb_ssl_read_or_write(0, F, buf, NULL, count); } ssize_t -rb_ssl_write(rb_fde_t * F, const void *buf, size_t count) +rb_ssl_write(rb_fde_t *F, const void *buf, size_t count) { return rb_ssl_read_or_write(1, F, NULL, buf, count); } @@ -297,7 +298,7 @@ rb_init_ssl(void) } /* Disable SSLv2, make the client use our settings */ SSL_CTX_set_options(ssl_server_ctx, SSL_OP_NO_SSLv2 | SSL_OP_CIPHER_SERVER_PREFERENCE); - + ssl_client_ctx = SSL_CTX_new(TLSv1_client_method()); if(ssl_client_ctx == NULL) @@ -313,7 +314,6 @@ rb_init_ssl(void) int rb_setup_ssl_server(const char *cert, const char *keyfile, const char *dhfile) { - FILE *param; DH *dh; unsigned long err; if(cert == NULL) @@ -347,28 +347,34 @@ rb_setup_ssl_server(const char *cert, const char *keyfile, const char *dhfile) if(dhfile != NULL) { /* DH parameters aren't necessary, but they are nice..if they didn't pass one..that is their problem */ - param = fopen(dhfile, "r"); - if(param != NULL) + BIO *bio = BIO_new_file(dhfile, "r"); + if(bio != NULL) { - dh = PEM_read_DHparams(param, NULL, NULL, NULL); + dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); if(dh == NULL) { err = ERR_get_error(); rb_lib_log ("rb_setup_ssl_server: Error loading DH params file [%s]: %s", - param, ERR_error_string(err, NULL)); - fclose(param); + dhfile, ERR_error_string(err, NULL)); + BIO_free(bio); return 0; } + BIO_free(bio); SSL_CTX_set_tmp_dh(ssl_server_ctx, dh); - fclose(param); + } + else + { + err = ERR_get_error(); + rb_lib_log("rb_setup_ssl_server: Error loading DH params file [%s]: %s", + dhfile, ERR_error_string(err, NULL)); } } return 1; } int -rb_ssl_listen(rb_fde_t * F, int backlog) +rb_ssl_listen(rb_fde_t *F, int backlog) { F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL; return listen(F->fd, backlog); @@ -382,7 +388,7 @@ struct ssl_connect }; static void -rb_ssl_connect_realcb(rb_fde_t * F, int status, struct ssl_connect *sconn) +rb_ssl_connect_realcb(rb_fde_t *F, int status, struct ssl_connect *sconn) { F->connect->callback = sconn->callback; F->connect->data = sconn->data; @@ -391,13 +397,13 @@ rb_ssl_connect_realcb(rb_fde_t * F, int status, struct ssl_connect *sconn) } static void -rb_ssl_tryconn_timeout_cb(rb_fde_t * F, void *data) +rb_ssl_tryconn_timeout_cb(rb_fde_t *F, void *data) { rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data); } static void -rb_ssl_tryconn_cb(rb_fde_t * F, void *data) +rb_ssl_tryconn_cb(rb_fde_t *F, void *data) { struct ssl_connect *sconn = data; int ssl_err; @@ -431,7 +437,7 @@ rb_ssl_tryconn_cb(rb_fde_t * F, void *data) } static void -rb_ssl_tryconn(rb_fde_t * F, int status, void *data) +rb_ssl_tryconn(rb_fde_t *F, int status, void *data) { struct ssl_connect *sconn = data; int ssl_err; @@ -473,7 +479,7 @@ rb_ssl_tryconn(rb_fde_t * F, int status, void *data) } void -rb_connect_tcp_ssl(rb_fde_t * F, struct sockaddr *dest, +rb_connect_tcp_ssl(rb_fde_t *F, struct sockaddr *dest, struct sockaddr *clocal, int socklen, CNCB * callback, void *data, int timeout) { struct ssl_connect *sconn; @@ -489,7 +495,7 @@ rb_connect_tcp_ssl(rb_fde_t * F, struct sockaddr *dest, } void -rb_ssl_start_connected(rb_fde_t * F, CNCB * callback, void *data, int timeout) +rb_ssl_start_connected(rb_fde_t *F, CNCB * callback, void *data, int timeout) { struct ssl_connect *sconn; int ssl_err; @@ -505,7 +511,7 @@ rb_ssl_start_connected(rb_fde_t * F, CNCB * callback, void *data, int timeout) F->connect->data = data; F->type |= RB_FD_SSL; F->ssl = SSL_new(ssl_client_ctx); - + SSL_set_fd((SSL *) F->ssl, F->fd); rb_setup_ssl_cb(F); rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); @@ -540,7 +546,7 @@ rb_init_prng(const char *path, prng_seed_t seed_type) { if(seed_type == RB_PRNG_DEFAULT) { -#ifdef WIN32 +#ifdef _WIN32 RAND_screen(); #endif return RAND_status(); @@ -558,7 +564,7 @@ rb_init_prng(const char *path, prng_seed_t seed_type) if(RAND_load_file(path, -1) == -1) return -1; break; -#ifdef WIN32 +#ifdef _WIN32 case RB_PRNGWIN32: RAND_screen(); break; @@ -574,11 +580,11 @@ int rb_get_random(void *buf, size_t length) { int ret; - + if((ret = RAND_bytes(buf, length)) == 0) { /* remove the error from the queue */ - ERR_get_error(); + ERR_get_error(); } return ret; } @@ -594,7 +600,7 @@ rb_get_pseudo_random(void *buf, size_t length) } const char * -rb_get_ssl_strerror(rb_fde_t * F) +rb_get_ssl_strerror(rb_fde_t *F) { return ERR_error_string(F->ssl_errno, NULL); } diff --git a/libratbox/src/patricia.c b/libratbox/src/patricia.c index 9e92bb3..33484f8 100644 --- a/libratbox/src/patricia.c +++ b/libratbox/src/patricia.c @@ -27,7 +27,7 @@ * #define NOTYET 1 * #define PATRICIA_DEBUG 1 */ - + #define PREFIX_HEAP_COUNT 1024 #define NODE_HEAP_COUNT 1024 #define PATRICIA_HEAP_COUNT 128 @@ -42,12 +42,12 @@ rb_init_patricia(void) * convert prefix information to bytes */ static uint8_t * -prefix_tochar(rb_prefix_t * prefix) +prefix_tochar(rb_prefix_t *prefix) { if(prefix == NULL) return (NULL); - return ((uint8_t *) & prefix->add.sin); + return ((uint8_t *)&prefix->add.sin); } static int @@ -59,14 +59,15 @@ comp_with_mask(void *addr, void *dest, unsigned int mask) int n = mask / 8; int m = ((-1) << (8 - (mask % 8))); - if(mask % 8 == 0 || (((uint8_t *) addr)[n] & m) == (((uint8_t *) dest)[n] & m)) + if(mask % 8 == 0 || (((uint8_t *)addr)[n] & m) == (((uint8_t *)dest)[n] & m)) return (1); } return (0); } + #ifdef NOTYET static char * -prefix_toa2x(rb_prefix_t * prefix, char *buf, int buf_len, int with_len) +prefix_toa2x(rb_prefix_t *prefix, char *buf, int buf_len, int with_len) { static char tmp[6]; if(prefix == NULL) @@ -88,12 +89,13 @@ prefix_toa2x(rb_prefix_t * prefix, char *buf, int buf_len, int with_len) */ static char * -prefix_toa2(rb_prefix_t * prefix, char *buff, int buf_len) +prefix_toa2(rb_prefix_t *prefix, char *buff, int buf_len) { return (prefix_toa2x(prefix, buff, buf_len, 0)); } + static char * -prefix_toa(rb_prefix_t * prefix) +prefix_toa(rb_prefix_t *prefix) { #ifdef RB_IPV6 static char buf[INET6_ADDRSTRLEN + 6]; @@ -104,7 +106,7 @@ prefix_toa(rb_prefix_t * prefix) } #endif static rb_prefix_t * -New_Prefix2(int family, void *dest, int bitlen, rb_prefix_t * prefix) +New_Prefix2(int family, void *dest, int bitlen, rb_prefix_t *prefix) { int dynamic_allocated = 0; #ifdef RB_IPV6 @@ -130,7 +132,7 @@ New_Prefix2(int family, void *dest, int bitlen, rb_prefix_t * prefix) { if(prefix == NULL) { - prefix = rb_malloc(sizeof(rb_prefix_t)); + prefix = rb_malloc(sizeof(rb_prefix_t)); dynamic_allocated++; } memcpy(&prefix->add.sin, dest, 4); @@ -229,7 +231,7 @@ ascii2prefix(int family, const char *string) } static rb_prefix_t * -Ref_Prefix(rb_prefix_t * prefix) +Ref_Prefix(rb_prefix_t *prefix) { if(prefix == NULL) return (NULL); @@ -243,7 +245,7 @@ Ref_Prefix(rb_prefix_t * prefix) } static void -Deref_Prefix(rb_prefix_t * prefix) +Deref_Prefix(rb_prefix_t *prefix) { if(prefix == NULL) return; @@ -287,7 +289,7 @@ rb_new_patricia(int maxbits) */ void -rb_clear_patricia(rb_patricia_tree_t * patricia, void (*func)(void *)) +rb_clear_patricia(rb_patricia_tree_t *patricia, void (*func) (void *)) { assert(patricia); if(patricia->head) @@ -297,7 +299,7 @@ rb_clear_patricia(rb_patricia_tree_t * patricia, void (*func)(void *)) rb_patricia_node_t **Xsp = Xstack; rb_patricia_node_t *Xrn = patricia->head; - while (Xrn) + while(Xrn) { rb_patricia_node_t *l = Xrn->l; rb_patricia_node_t *r = Xrn->r; @@ -333,7 +335,7 @@ rb_clear_patricia(rb_patricia_tree_t * patricia, void (*func)(void *)) } else { - Xrn = (rb_patricia_node_t *) 0; + Xrn = (rb_patricia_node_t *)0; } } } @@ -343,7 +345,7 @@ rb_clear_patricia(rb_patricia_tree_t * patricia, void (*func)(void *)) void -rb_destroy_patricia(rb_patricia_tree_t * patricia, void (*func)(void *)) +rb_destroy_patricia(rb_patricia_tree_t *patricia, void (*func) (void *)) { rb_clear_patricia(patricia, func); num_active_patricia--; @@ -355,7 +357,7 @@ rb_destroy_patricia(rb_patricia_tree_t * patricia, void (*func)(void *)) */ void -rb_patricia_process(rb_patricia_tree_t * patricia, void (*func)(rb_prefix_t *, void *)) +rb_patricia_process(rb_patricia_tree_t *patricia, void (*func) (rb_prefix_t *, void *)) { rb_patricia_node_t *node; assert(func); @@ -368,7 +370,7 @@ rb_patricia_process(rb_patricia_tree_t * patricia, void (*func)(rb_prefix_t *, v } rb_patricia_node_t * -rb_patricia_search_exact(rb_patricia_tree_t * patricia, rb_prefix_t * prefix) +rb_patricia_search_exact(rb_patricia_tree_t *patricia, rb_prefix_t *prefix) { rb_patricia_node_t *node; uint8_t *addr; @@ -385,7 +387,7 @@ rb_patricia_search_exact(rb_patricia_tree_t * patricia, rb_prefix_t * prefix) addr = rb_prefix_touchar(prefix); bitlen = prefix->bitlen; - while (node->bit < bitlen) + while(node->bit < bitlen) { if(BIT_TEST(addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) @@ -443,7 +445,7 @@ rb_patricia_search_exact(rb_patricia_tree_t * patricia, rb_prefix_t * prefix) /* if inclusive != 0, "best" may be the given prefix itself */ rb_patricia_node_t * -rb_patricia_search_best2(rb_patricia_tree_t * patricia, rb_prefix_t * prefix, int inclusive) +rb_patricia_search_best2(rb_patricia_tree_t *patricia, rb_prefix_t *prefix, int inclusive) { rb_patricia_node_t *node; rb_patricia_node_t *stack[RB_PATRICIA_MAXBITS + 1]; @@ -462,7 +464,7 @@ rb_patricia_search_best2(rb_patricia_tree_t * patricia, rb_prefix_t * prefix, in addr = rb_prefix_touchar(prefix); bitlen = prefix->bitlen; - while (node->bit < bitlen) + while(node->bit < bitlen) { if(node->prefix) @@ -522,7 +524,7 @@ rb_patricia_search_best2(rb_patricia_tree_t * patricia, rb_prefix_t * prefix, in if(cnt <= 0) return (NULL); - while (--cnt >= 0) + while(--cnt >= 0) { node = stack[cnt]; #ifdef PATRICIA_DEBUG @@ -545,14 +547,14 @@ rb_patricia_search_best2(rb_patricia_tree_t * patricia, rb_prefix_t * prefix, in rb_patricia_node_t * -rb_patricia_search_best(rb_patricia_tree_t * patricia, rb_prefix_t * prefix) +rb_patricia_search_best(rb_patricia_tree_t *patricia, rb_prefix_t *prefix) { return (rb_patricia_search_best2(patricia, prefix, 1)); } rb_patricia_node_t * -rb_patricia_lookup(rb_patricia_tree_t * patricia, rb_prefix_t * prefix) +rb_patricia_lookup(rb_patricia_tree_t *patricia, rb_prefix_t *prefix) { rb_patricia_node_t *node, *new_node, *parent, *glue; uint8_t *addr, *test_addr; @@ -565,7 +567,7 @@ rb_patricia_lookup(rb_patricia_tree_t * patricia, rb_prefix_t * prefix) if(patricia->head == NULL) { - node = rb_malloc(sizeof(rb_patricia_node_t)); + node = rb_malloc(sizeof(rb_patricia_node_t)); node->bit = prefix->bitlen; node->prefix = Ref_Prefix(prefix); node->parent = NULL; @@ -585,7 +587,7 @@ rb_patricia_lookup(rb_patricia_tree_t * patricia, rb_prefix_t * prefix) bitlen = prefix->bitlen; node = patricia->head; - while (node->bit < bitlen || node->prefix == NULL) + while(node->bit < bitlen || node->prefix == NULL) { if(node->bit < patricia->maxbits && @@ -631,7 +633,7 @@ rb_patricia_lookup(rb_patricia_tree_t * patricia, rb_prefix_t * prefix) /* find the first bit different */ check_bit = (node->bit < bitlen) ? node->bit : bitlen; differ_bit = 0; - for (i = 0; i * 8 < check_bit; i++) + for(i = 0; i * 8 < check_bit; i++) { if((r = (addr[i] ^ test_addr[i])) == 0) { @@ -639,7 +641,7 @@ rb_patricia_lookup(rb_patricia_tree_t * patricia, rb_prefix_t * prefix) continue; } /* I know the better way, but for now */ - for (j = 0; j < 8; j++) + for(j = 0; j < 8; j++) { if(BIT_TEST(r, (0x80 >> j))) break; @@ -656,7 +658,7 @@ rb_patricia_lookup(rb_patricia_tree_t * patricia, rb_prefix_t * prefix) #endif /* PATRICIA_DEBUG */ parent = node->parent; - while (parent && parent->bit >= differ_bit) + while(parent && parent->bit >= differ_bit) { node = parent; parent = node->parent; @@ -689,7 +691,7 @@ rb_patricia_lookup(rb_patricia_tree_t * patricia, rb_prefix_t * prefix) return (node); } - new_node = rb_malloc(sizeof(rb_patricia_node_t)); + new_node = rb_malloc(sizeof(rb_patricia_node_t)); new_node->bit = prefix->bitlen; new_node->prefix = Ref_Prefix(prefix); new_node->parent = NULL; @@ -797,7 +799,7 @@ rb_patricia_lookup(rb_patricia_tree_t * patricia, rb_prefix_t * prefix) void -rb_patricia_remove(rb_patricia_tree_t * patricia, rb_patricia_node_t * node) +rb_patricia_remove(rb_patricia_tree_t *patricia, rb_patricia_node_t *node) { rb_patricia_node_t *parent, *child; @@ -914,18 +916,18 @@ rb_patricia_remove(rb_patricia_tree_t * patricia, rb_patricia_node_t * node) } rb_patricia_node_t * -make_and_lookup_ip(rb_patricia_tree_t * tree, struct sockaddr *in, int bitlen) +make_and_lookup_ip(rb_patricia_tree_t *tree, struct sockaddr *in, int bitlen) { rb_prefix_t *prefix; rb_patricia_node_t *node; void *ipptr = NULL; #ifdef RB_IPV6 if(in->sa_family == AF_INET6) - ipptr = &((struct sockaddr_in6 *)in)->sin6_addr; - else + ipptr = &((struct sockaddr_in6 *)in)->sin6_addr; + else #endif ipptr = &((struct sockaddr_in *)in)->sin_addr; - + prefix = New_Prefix(in->sa_family, ipptr, bitlen); if(prefix == NULL) @@ -941,7 +943,7 @@ make_and_lookup_ip(rb_patricia_tree_t * tree, struct sockaddr *in, int bitlen) rb_patricia_node_t * -make_and_lookup(rb_patricia_tree_t * tree, const char *string) +make_and_lookup(rb_patricia_tree_t *tree, const char *string) { rb_prefix_t *prefix; rb_patricia_node_t *node; @@ -968,7 +970,7 @@ make_and_lookup(rb_patricia_tree_t * tree, const char *string) #ifdef NOTYET static rb_patricia_node_t * -try_search_exact(rb_patricia_tree_t * tree, char *string) +try_search_exact(rb_patricia_tree_t *tree, char *string) { rb_prefix_t *prefix; rb_patricia_node_t *node; @@ -991,7 +993,7 @@ try_search_exact(rb_patricia_tree_t * tree, char *string) } void -lookup_then_remove(rb_patricia_tree_t * tree, char *string) +lookup_then_remove(rb_patricia_tree_t *tree, char *string) { rb_patricia_node_t *node; @@ -1001,7 +1003,7 @@ lookup_then_remove(rb_patricia_tree_t * tree, char *string) #endif rb_patricia_node_t * -rb_match_ip(rb_patricia_tree_t * tree, struct sockaddr *ip) +rb_match_ip(rb_patricia_tree_t *tree, struct sockaddr *ip) { rb_prefix_t *prefix; rb_patricia_node_t *node; @@ -1018,13 +1020,15 @@ rb_match_ip(rb_patricia_tree_t * tree, struct sockaddr *ip) len = 128; family = AF_INET6; ipptr = &((struct sockaddr_in6 *)ip)->sin6_addr; - } else { + } + else + { len = 32; family = AF_INET; ipptr = &((struct sockaddr_in *)ip)->sin_addr; } #endif - + if((prefix = New_Prefix(family, ipptr, len)) != NULL) { node = rb_patricia_search_best(tree, prefix); @@ -1035,7 +1039,7 @@ rb_match_ip(rb_patricia_tree_t * tree, struct sockaddr *ip) } rb_patricia_node_t * -rb_match_ip_exact(rb_patricia_tree_t * tree, struct sockaddr *ip, unsigned int len) +rb_match_ip_exact(rb_patricia_tree_t *tree, struct sockaddr *ip, unsigned int len) { rb_prefix_t *prefix; rb_patricia_node_t *node; @@ -1054,14 +1058,16 @@ rb_match_ip_exact(rb_patricia_tree_t * tree, struct sockaddr *ip, unsigned int l len = 128; family = AF_INET6; ipptr = &((struct sockaddr_in6 *)ip)->sin6_addr; - } else { + } + else + { if(len > 32) len = 32; family = AF_INET; ipptr = &((struct sockaddr_in *)ip)->sin_addr; } #endif - + if((prefix = New_Prefix(family, ipptr, len)) != NULL) { node = rb_patricia_search_exact(tree, prefix); @@ -1074,7 +1080,7 @@ rb_match_ip_exact(rb_patricia_tree_t * tree, struct sockaddr *ip, unsigned int l rb_patricia_node_t * -rb_match_string(rb_patricia_tree_t * tree, const char *string) +rb_match_string(rb_patricia_tree_t *tree, const char *string) { rb_patricia_node_t *node; rb_prefix_t *prefix; @@ -1098,7 +1104,7 @@ rb_match_string(rb_patricia_tree_t * tree, const char *string) } rb_patricia_node_t * -rb_match_exact_string(rb_patricia_tree_t * tree, const char *string) +rb_match_exact_string(rb_patricia_tree_t *tree, const char *string) { rb_prefix_t *prefix; rb_patricia_node_t *node; diff --git a/libratbox/src/poll.c b/libratbox/src/poll.c index 237fcab..f4b0dcf 100644 --- a/libratbox/src/poll.c +++ b/libratbox/src/poll.c @@ -22,7 +22,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: poll.c 25375 2008-05-16 15:19:51Z androsyn $ + * $Id: poll.c 26092 2008-09-19 15:13:52Z androsyn $ */ #include #include @@ -52,7 +52,7 @@ typedef struct _pollfd_list pollfd_list_t; static pollfd_list_t pollfd_list; int -rb_setup_fd_poll(rb_fde_t * F) +rb_setup_fd_poll(rb_fde_t *F) { return 0; } @@ -70,7 +70,7 @@ rb_init_netio_poll(void) int fd; pollfd_list.pollfds = rb_malloc(rb_getmaxconnect() * (sizeof(struct pollfd))); pollfd_list.allocated = rb_getmaxconnect(); - for (fd = 0; fd < rb_getmaxconnect(); fd++) + for(fd = 0; fd < rb_getmaxconnect(); fd++) { pollfd_list.pollfds[fd].fd = -1; } @@ -89,7 +89,7 @@ resize_pollarray(int fd) rb_realloc(pollfd_list.pollfds, pollfd_list.allocated * (sizeof(struct pollfd))); memset(&pollfd_list.pollfds[old_value + 1], 0, sizeof(struct pollfd) * 1024); - for (x = old_value + 1; x < pollfd_list.allocated; x++) + for(x = old_value + 1; x < pollfd_list.allocated; x++) { pollfd_list.pollfds[x].fd = -1; } @@ -103,7 +103,7 @@ resize_pollarray(int fd) * and deregister interest in a pending IO state for a given FD. */ void -rb_setselect_poll(rb_fde_t * F, unsigned int type, PF * handler, void *client_data) +rb_setselect_poll(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { if(F == NULL) return; @@ -134,8 +134,8 @@ rb_setselect_poll(rb_fde_t * F, unsigned int type, PF * handler, void *client_da pollfd_list.pollfds[F->fd].fd = -1; if(F->fd == pollfd_list.maxindex) { - while (pollfd_list.maxindex >= 0 - && pollfd_list.pollfds[pollfd_list.maxindex].fd == -1) + while(pollfd_list.maxindex >= 0 + && pollfd_list.pollfds[pollfd_list.maxindex].fd == -1) pollfd_list.maxindex--; } } @@ -175,7 +175,7 @@ rb_select_poll(long delay) int revents; num = poll(pollfd_list.pollfds, pollfd_list.maxindex + 1, delay); - rb_set_time(); + rb_set_time(); if(num < 0) { if(!rb_ignore_errno(errno)) @@ -184,23 +184,23 @@ rb_select_poll(long delay) return RB_ERROR; } if(num == 0) - return RB_OK; - + return RB_OK; + /* XXX we *could* optimise by falling out after doing num fds ... */ - for (ci = 0; ci < pollfd_list.maxindex + 1; ci++) + for(ci = 0; ci < pollfd_list.maxindex + 1; ci++) { rb_fde_t *F; pfd = &pollfd_list.pollfds[ci]; revents = pfd->revents; - fd = pfd->fd; + fd = pfd->fd; if(revents == 0 || fd == -1) continue; F = rb_find_fd(fd); if(F == NULL) continue; - + if(revents & (POLLRDNORM | POLLIN | POLLHUP | POLLERR)) { hdl = F->read_handler; @@ -217,7 +217,7 @@ rb_select_poll(long delay) data = F->write_data; F->write_handler = NULL; F->write_data = NULL; - if(hdl) + if(hdl) hdl(F, data); } @@ -225,7 +225,7 @@ rb_select_poll(long delay) rb_setselect_poll(F, RB_SELECT_READ, NULL, NULL); if(F->write_handler == NULL) rb_setselect_poll(F, RB_SELECT_WRITE, NULL, NULL); - + } return 0; } @@ -239,7 +239,7 @@ rb_init_netio_poll(void) } void -rb_setselect_poll(rb_fde_t * F, unsigned int type, PF * handler, void *client_data) +rb_setselect_poll(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { errno = ENOSYS; return; @@ -253,7 +253,7 @@ rb_select_poll(long delay) } int -rb_setup_fd_poll(rb_fde_t * F) +rb_setup_fd_poll(rb_fde_t *F) { errno = ENOSYS; return -1; diff --git a/libratbox/src/ports.c b/libratbox/src/ports.c index 353b07b..0802e1d 100644 --- a/libratbox/src/ports.c +++ b/libratbox/src/ports.c @@ -23,7 +23,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: ports.c 25038 2008-01-23 16:03:08Z androsyn $ + * $Id: ports.c 26092 2008-09-19 15:13:52Z androsyn $ */ #include @@ -44,26 +44,26 @@ static struct timespec zero_timespec; static port_event_t *pelst; /* port buffer */ static int pemax; /* max structs to buffer */ -int -rb_setup_fd_ports(rb_fde_t *F) +int +rb_setup_fd_ports(int fd) { - return 0; + return 0; } - + static void -pe_update_events(rb_fde_t * F, short filter, PF * handler) +pe_update_events(rb_fde_t *F, short filter, PF * handler) { PF *cur_handler = NULL; - if (filter == POLLRDNORM) + if(filter == POLLRDNORM) cur_handler = F->read_handler; - else if (filter == POLLWRNORM) + else if(filter == POLLWRNORM) cur_handler = F->write_handler; - if (!cur_handler && handler) + if(!cur_handler && handler) port_associate(pe, PORT_SOURCE_FD, F->fd, filter, F); - else if (cur_handler && !handler) + else if(cur_handler && !handler) port_dissociate(pe, PORT_SOURCE_FD, F->fd); } @@ -80,7 +80,8 @@ pe_update_events(rb_fde_t * F, short filter, PF * handler) int rb_init_netio_ports(void) { - if((pe = port_create()) < 0) { + if((pe = port_create()) < 0) + { return errno; } pemax = getdtablesize(); @@ -96,18 +97,19 @@ rb_init_netio_ports(void) * and deregister interest in a pending IO state for a given FD. */ void -rb_setselect_ports(rb_fde_t *F, unsigned int type, PF * handler, - void *client_data) +rb_setselect_ports(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { lrb_assert(IsFDOpen(F)); /* Update the list, even though we're not using it .. */ - if(type & RB_SELECT_READ) { + if(type & RB_SELECT_READ) + { pe_update_events(F, POLLRDNORM, handler); F->read_handler = handler; F->read_data = client_data; } - if(type & RB_SELECT_WRITE) { + if(type & RB_SELECT_WRITE) + { pe_update_events(F, POLLWRNORM, handler); F->write_handler = handler; F->write_data = client_data; @@ -126,10 +128,10 @@ rb_setselect_ports(rb_fde_t *F, unsigned int type, PF * handler, int rb_select_ports(long delay) { - int i, fd; - uint nget = 1; -struct timespec poll_time; -struct timer_data *tdata; + int i, fd; + uint nget = 1; + struct timespec poll_time; + struct timer_data *tdata; poll_time.tv_sec = delay / 1000; poll_time.tv_nsec = (delay % 1000) * 1000000; @@ -137,21 +139,25 @@ struct timer_data *tdata; i = port_getn(pe, pelst, pemax, &nget, &poll_time); rb_set_time(); - if (i == -1) + if(i == -1) return RB_OK; - for (i = 0; i < nget; i++) { - switch(pelst[i].portev_source) { + for(i = 0; i < nget; i++) + { + switch (pelst[i].portev_source) + { case PORT_SOURCE_FD: fd = pelst[i].portev_object; PF *hdl = NULL; rb_fde_t *F = rb_find_fd(fd); - if ((pelst[i].portev_events & POLLRDNORM) && (hdl = F->read_handler)) { + if((pelst[i].portev_events & POLLRDNORM) && (hdl = F->read_handler)) + { F->read_handler = NULL; hdl(F, F->read_data); } - if ((pelst[i].portev_events & POLLWRNORM) && (hdl = F->write_handler)) { + if((pelst[i].portev_events & POLLWRNORM) && (hdl = F->write_handler)) + { F->write_handler = NULL; hdl(F, F->write_data); } @@ -162,7 +168,7 @@ struct timer_data *tdata; } #else /* ports not supported */ -int +int rb_init_netio_ports(void) { return ENOSYS; @@ -171,23 +177,23 @@ rb_init_netio_ports(void) void rb_setselect_ports(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { - errno = ENOSYS; - return; + errno = ENOSYS; + return; } - + int rb_select_ports(long delay) { - errno = ENOSYS; - return -1; + errno = ENOSYS; + return -1; } - + int rb_setup_fd_ports(rb_fde_t *F) { - errno = ENOSYS; - return -1; + errno = ENOSYS; + return -1; } - + #endif diff --git a/libratbox/src/ratbox_lib.c b/libratbox/src/ratbox_lib.c index e80f5d1..c6aa0de 100644 --- a/libratbox/src/ratbox_lib.c +++ b/libratbox/src/ratbox_lib.c @@ -20,7 +20,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: ratbox_lib.c 25375 2008-05-16 15:19:51Z androsyn $ + * $Id: ratbox_lib.c 26092 2008-09-19 15:13:52Z androsyn $ */ #include @@ -37,20 +37,20 @@ static char errbuf[512]; /* this doesn't do locales...oh well i guess */ static const char *months[] = { - "January", "February", "March", "April", - "May", "June", "July", "August", - "September", "October", "November", "December" + "January", "February", "March", "April", + "May", "June", "July", "August", + "September", "October", "November", "December" }; static const char *weekdays[] = { - "Sunday", "Monday", "Tuesday", "Wednesday", - "Thursday", "Friday", "Saturday" + "Sunday", "Monday", "Tuesday", "Wednesday", + "Thursday", "Friday", "Saturday" }; static const char *s_month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" -}; +}; static const char *s_weekdays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" @@ -72,23 +72,24 @@ rb_ctime(const time_t t, char *buf, size_t len) if(rb_unlikely(tp == NULL)) { strcpy(buf, ""); - return(buf); + return (buf); } if(buf == NULL) { p = timex; tlen = sizeof(timex); - } else { + } + else + { p = buf; tlen = len; } rb_snprintf(p, tlen, "%s %s %d %02u:%02u:%02u %d", - s_weekdays[tp->tm_wday], s_month[tp->tm_mon], - tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec, - tp->tm_year + 1900); - return(p); + s_weekdays[tp->tm_wday], s_month[tp->tm_mon], + tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec, tp->tm_year + 1900); + return (p); } @@ -106,14 +107,14 @@ rb_date(const time_t t, char *buf, size_t len) if(rb_unlikely(gm == NULL)) { - rb_strlcpy(buf, "", len); - return(buf); + rb_strlcpy(buf, "", len); + return (buf); } - - rb_snprintf(buf, len, "%s %s %d %d -- %02u:%02u:%02u +00:00", - weekdays[gm->tm_wday], months[gm->tm_mon], gm->tm_mday, - gm->tm_year + 1900, gm->tm_hour, gm->tm_min, gm->tm_sec); - return(buf); + + rb_snprintf(buf, len, "%s %s %d %d -- %02u:%02u:%02u +00:00", + weekdays[gm->tm_wday], months[gm->tm_mon], gm->tm_mday, + gm->tm_year + 1900, gm->tm_hour, gm->tm_min, gm->tm_sec); + return (buf); } time_t @@ -135,7 +136,7 @@ rb_lib_log(const char *format, ...) if(rb_log == NULL) return; va_start(args, format); - rb_vsnprintf(errbuf, sizeof(errbuf), format, args); + rb_vsnprintf(errbuf, sizeof(errbuf), format, args); va_end(args); rb_log(errbuf); } @@ -147,7 +148,7 @@ rb_lib_die(const char *format, ...) if(rb_die == NULL) abort(); va_start(args, format); - rb_vsnprintf(errbuf, sizeof(errbuf), format, args); + rb_vsnprintf(errbuf, sizeof(errbuf), format, args); va_end(args); rb_die(errbuf); } @@ -159,7 +160,7 @@ rb_lib_restart(const char *format, ...) if(rb_restart == NULL) abort(); va_start(args, format); - rb_vsnprintf(errbuf, sizeof(errbuf), format, args); + rb_vsnprintf(errbuf, sizeof(errbuf), format, args); va_end(args); rb_restart(errbuf); } @@ -181,15 +182,17 @@ rb_set_time(void) memcpy(&rb_time, &newtime, sizeof(struct timeval)); } +extern const char *libratbox_serno; + const char * rb_lib_version(void) { - static const char *id = "$Rev: 25375 $"; - return id; + return libratbox_serno; } void -rb_lib_init(log_cb *ilog, restart_cb *irestart, die_cb *idie, int closeall, int maxcon, size_t dh_size, size_t fd_heap_size) +rb_lib_init(log_cb * ilog, restart_cb * irestart, die_cb * idie, int closeall, int maxcon, + size_t dh_size, size_t fd_heap_size) { rb_set_time(); rb_log = ilog; @@ -219,24 +222,25 @@ rb_lib_loop(long delay) while(1) rb_select(-1); } - + while(1) { if(delay == 0) { - if((next = rb_event_next()) > 0) + if((next = rb_event_next()) > 0) { next -= rb_current_time(); if(next <= 0) next = 1000; - else + else next *= 1000; } else next = -1; rb_select(next); - } else + } + else rb_select(delay); rb_event_run(); } @@ -244,17 +248,17 @@ rb_lib_loop(long delay) #ifndef HAVE_STRTOK_R char * -rb_strtok_r (char *s, const char *delim, char **save) +rb_strtok_r(char *s, const char *delim, char **save) { char *token; - if (s == NULL) + if(s == NULL) s = *save; /* Scan leading delimiters. */ s += strspn(s, delim); - if (*s == '\0') + if(*s == '\0') { *save = s; return NULL; @@ -262,19 +266,19 @@ rb_strtok_r (char *s, const char *delim, char **save) token = s; s = strpbrk(token, delim); - - if (s == NULL) + + if(s == NULL) *save = (token + strlen(token)); else { - *s = '\0'; + *s = '\0'; *save = s + 1; } return token; } #else -char -*rb_strtok_r(char *s, const char *delim, char **save) +char * +rb_strtok_r(char *s, const char *delim, char **save) { return strtok_r(s, delim, save); } @@ -283,11 +287,11 @@ char static const char base64_table[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' - }; + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' +}; static const char base64_pad = '='; @@ -296,7 +300,7 @@ static const short base64_reverse_table[256] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, @@ -317,31 +321,36 @@ rb_base64_encode(const unsigned char *str, int length) unsigned char *p; unsigned char *result; - if ((length + 2) < 0 || ((length + 2) / 3) >= (1 << (sizeof(int) * 8 - 2))) { + if((length + 2) < 0 || ((length + 2) / 3) >= (1 << (sizeof(int) * 8 - 2))) + { return NULL; } result = rb_malloc(((length + 2) / 3) * 5); p = result; - while (length > 2) - { + while(length > 2) + { *p++ = base64_table[current[0] >> 2]; *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; *p++ = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)]; *p++ = base64_table[current[2] & 0x3f]; current += 3; - length -= 3; + length -= 3; } - if (length != 0) { + if(length != 0) + { *p++ = base64_table[current[0] >> 2]; - if (length > 1) { + if(length > 1) + { *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; *p++ = base64_table[(current[1] & 0x0f) << 2]; *p++ = base64_pad; - } else { + } + else + { *p++ = base64_table[(current[0] & 0x03) << 4]; *p++ = base64_pad; *p++ = base64_pad; @@ -357,16 +366,20 @@ rb_base64_decode(const unsigned char *str, int length, int *ret) const unsigned char *current = str; int ch, i = 0, j = 0, k; unsigned char *result; - + result = rb_malloc(length + 1); - while ((ch = *current++) != '\0' && length-- > 0) { - if (ch == base64_pad) break; + while((ch = *current++) != '\0' && length-- > 0) + { + if(ch == base64_pad) + break; ch = base64_reverse_table[ch]; - if (ch < 0) continue; + if(ch < 0) + continue; - switch(i % 4) { + switch (i % 4) + { case 0: result[j] = ch << 2; break; @@ -375,7 +388,7 @@ rb_base64_decode(const unsigned char *str, int length, int *ret) result[j] = (ch & 0x0f) << 4; break; case 2: - result[j++] |= ch >>2; + result[j++] |= ch >> 2; result[j] = (ch & 0x03) << 6; break; case 3: @@ -387,8 +400,10 @@ rb_base64_decode(const unsigned char *str, int length, int *ret) k = j; - if (ch == base64_pad) { - switch(i % 4) { + if(ch == base64_pad) + { + switch (i % 4) + { case 1: free(result); return NULL; @@ -402,5 +417,3 @@ rb_base64_decode(const unsigned char *str, int length, int *ret) *ret = j; return result; } - - diff --git a/libratbox/src/rawbuf.c b/libratbox/src/rawbuf.c index edc2958..65cc06e 100644 --- a/libratbox/src/rawbuf.c +++ b/libratbox/src/rawbuf.c @@ -125,9 +125,9 @@ rb_rawbuf_flush_writev(rawbuf_head_t * rb, rb_fde_t *F) rb->len -= buf->len - rb->written; rb_rawbuf_done(rb, buf); continue; - } + } } - + if(xret >= buf->len) { xret -= buf->len; @@ -179,7 +179,7 @@ rb_rawbuf_flush(rawbuf_head_t * rb, rb_fde_t *F) rb_bh_free(rawbuf_heap, buf); } rb->len -= retval; - lrb_assert(rb->len >= 0); + lrb_assert(rb->len >= 0); return retval; } @@ -197,7 +197,7 @@ rb_rawbuf_append(rawbuf_head_t * rb, void *data, int len) { buf = (rawbuf_t *) rb->list.tail->data; clen = RAWBUF_SIZE - buf->len; - ptr = (void *) (buf->data + buf->len); + ptr = (void *)(buf->data + buf->len); if(len < clen) clen = len; @@ -211,7 +211,7 @@ rb_rawbuf_append(rawbuf_head_t * rb, void *data, int len) } - while (len > 0) + while(len > 0) { buf = rb_rawbuf_newbuf(rb); @@ -241,7 +241,7 @@ rb_rawbuf_get(rawbuf_head_t * rb, void *data, int len) buf = rb->list.head->data; if(buf->flushing) - ptr = (void *) (buf->data + rb->written); + ptr = (void *)(buf->data + rb->written); else ptr = buf->data; diff --git a/libratbox/src/rb_memory.c b/libratbox/src/rb_memory.c index 7af6a57..0322419 100644 --- a/libratbox/src/rb_memory.c +++ b/libratbox/src/rb_memory.c @@ -21,7 +21,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: rb_memory.c 25038 2008-01-23 16:03:08Z androsyn $ + * $Id: rb_memory.c 26092 2008-09-19 15:13:52Z androsyn $ */ #include #include @@ -39,4 +39,3 @@ rb_outofmemory(void) rb_lib_log("Out of memory: restarting server..."); rb_lib_restart("Out of Memory"); } - diff --git a/libratbox/src/select.c b/libratbox/src/select.c index 7b36c3d..c0b736c 100644 --- a/libratbox/src/select.c +++ b/libratbox/src/select.c @@ -22,13 +22,22 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: select.c 25038 2008-01-23 16:03:08Z androsyn $ + * $Id: select.c 26092 2008-09-19 15:13:52Z androsyn $ */ +#define FD_SETSIZE 65535 #include #include #include -#if defined(HAVE_SELECT) +#if defined(HAVE_SELECT) || defined(_WIN32) + +#ifdef _WIN32 +#define MY_FD_SET(x, y) FD_SET((SOCKET)x, y) +#define MY_FD_CLR(x, y) FD_CLR((SOCKET)x, y) +#else +#define MY_FD_SET(x, y) FD_SET(x, y) +#define MY_FD_CLR(x, y) FD_CLR(x, y) +#endif #ifdef HAVE_SYS_SELECT_H #include @@ -65,43 +74,44 @@ select_update_selectfds(rb_fde_t *F, short event, PF * handler) /* Update the read / write set */ if(event & RB_SELECT_READ) { - if(handler) + if(handler) { - FD_SET(F->fd, &select_readfds); - F->pflags |= RB_SELECT_READ; - } - else + MY_FD_SET(F->fd, &select_readfds); + F->pflags |= RB_SELECT_READ; + } + else { - FD_CLR(F->fd, &select_readfds); + MY_FD_CLR(F->fd, &select_readfds); F->pflags &= ~RB_SELECT_READ; } } if(event & RB_SELECT_WRITE) { - if(handler) + if(handler) { - FD_SET(F->fd, &select_writefds); + MY_FD_SET(F->fd, &select_writefds); F->pflags |= RB_SELECT_WRITE; } - else + else { - FD_CLR(F->fd, &select_writefds); + MY_FD_CLR(F->fd, &select_writefds); F->pflags &= ~RB_SELECT_WRITE; } } - if(F->pflags & (RB_SELECT_READ|RB_SELECT_WRITE)) + if(F->pflags & (RB_SELECT_READ | RB_SELECT_WRITE)) { if(F->fd > rb_maxfd) { - rb_maxfd = F->fd; + rb_maxfd = F->fd; } - } + } else if(F->fd <= rb_maxfd) { - while(rb_maxfd >= 0 && !FD_ISSET(rb_maxfd, &select_readfds) && !FD_ISSET(rb_maxfd, &select_writefds)) - rb_maxfd--; + while(rb_maxfd >= 0 && !FD_ISSET(rb_maxfd, &select_readfds) + && !FD_ISSET(rb_maxfd, &select_writefds)) + rb_maxfd--; } } @@ -112,7 +122,7 @@ select_update_selectfds(rb_fde_t *F, short event, PF * handler) int rb_setup_fd_select(rb_fde_t *F) { - return 0; + return 0; } @@ -122,9 +132,12 @@ rb_setup_fd_select(rb_fde_t *F) * This is a needed exported function which will be called to initialise * the network loop code. */ +extern int rb_maxconnections; int rb_init_netio_select(void) { + if(rb_maxconnections > FD_SETSIZE) + rb_maxconnections = FD_SETSIZE; /* override this */ FD_ZERO(&select_readfds); FD_ZERO(&select_writefds); return 0; @@ -137,8 +150,7 @@ rb_init_netio_select(void) * and deregister interest in a pending IO state for a given FD. */ void -rb_setselect_select(rb_fde_t *F, unsigned int type, PF * handler, - void *client_data) +rb_setselect_select(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { lrb_assert(IsFDOpen(F)); @@ -181,7 +193,7 @@ rb_select_select(long delay) memcpy(&tmpreadfds, &select_readfds, sizeof(fd_set)); memcpy(&tmpwritefds, &select_writefds, sizeof(fd_set)); - for (;;) + for(;;) { to.tv_sec = 0; to.tv_usec = delay * 1000; @@ -201,7 +213,7 @@ rb_select_select(long delay) return 0; /* XXX we *could* optimise by falling out after doing num fds ... */ - for (fd = 0; fd < rb_maxfd + 1; fd++) + for(fd = 0; fd < rb_maxfd + 1; fd++) { F = rb_find_fd(fd); if(F == NULL) @@ -234,7 +246,7 @@ rb_select_select(long delay) } #else /* select not supported..what sort of garbage is this? */ -int +int rb_init_netio_select(void) { return ENOSYS; @@ -243,22 +255,22 @@ rb_init_netio_select(void) void rb_setselect_select(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { - errno = ENOSYS; - return; + errno = ENOSYS; + return; } - + int rb_select_select(long delay) { - errno = ENOSYS; - return -1; + errno = ENOSYS; + return -1; } - + int rb_setup_fd_select(rb_fde_t *F) { - errno = ENOSYS; - return -1; + errno = ENOSYS; + return -1; } #endif diff --git a/libratbox/src/sigio.c b/libratbox/src/sigio.c index cd0addb..085b7a4 100644 --- a/libratbox/src/sigio.c +++ b/libratbox/src/sigio.c @@ -23,7 +23,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: sigio.c 25375 2008-05-16 15:19:51Z androsyn $ + * $Id: sigio.c 26092 2008-09-19 15:13:52Z androsyn $ */ #ifndef _GNU_SOURCE @@ -81,7 +81,7 @@ rb_init_netio_sigio(void) int fd; pollfd_list.pollfds = rb_malloc(rb_getmaxconnect() * (sizeof(struct pollfd))); pollfd_list.allocated = rb_getmaxconnect(); - for (fd = 0; fd < rb_getmaxconnect(); fd++) + for(fd = 0; fd < rb_getmaxconnect(); fd++) { pollfd_list.pollfds[fd].fd = -1; } @@ -111,7 +111,7 @@ resize_pollarray(int fd) rb_realloc(pollfd_list.pollfds, pollfd_list.allocated * (sizeof(struct pollfd))); memset(&pollfd_list.pollfds[old_value + 1], 0, sizeof(struct pollfd) * 1024); - for (x = old_value + 1; x < pollfd_list.allocated; x++) + for(x = old_value + 1; x < pollfd_list.allocated; x++) { pollfd_list.pollfds[x].fd = -1; } @@ -128,7 +128,7 @@ resize_pollarray(int fd) */ int -rb_setup_fd_sigio(rb_fde_t * F) +rb_setup_fd_sigio(rb_fde_t *F) { int flags = 0; int fd = F->fd; @@ -162,7 +162,7 @@ rb_setup_fd_sigio(rb_fde_t * F) * and deregister interest in a pending IO state for a given FD. */ void -rb_setselect_sigio(rb_fde_t * F, unsigned int type, PF * handler, void *client_data) +rb_setselect_sigio(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { if(F == NULL) return; @@ -194,8 +194,8 @@ rb_setselect_sigio(rb_fde_t * F, unsigned int type, PF * handler, void *client_d pollfd_list.pollfds[F->fd].fd = -1; if(F->fd == pollfd_list.maxindex) { - while (pollfd_list.maxindex >= 0 - && pollfd_list.pollfds[pollfd_list.maxindex].fd == -1) + while(pollfd_list.maxindex >= 0 + && pollfd_list.pollfds[pollfd_list.maxindex].fd == -1) pollfd_list.maxindex--; } } @@ -244,7 +244,7 @@ rb_select_sigio(long delay) timeout.tv_nsec = (delay % 1000) * 1000000; } - for (;;) + for(;;) { if(!sigio_is_screwed) { @@ -268,7 +268,7 @@ rb_select_sigio(long delay) #ifdef SIGIO_SCHED_EVENT if(sig == RTSIGTIM && can_do_event) { - struct ev_entry *ev = (struct ev_entry *) si.si_ptr; + struct ev_entry *ev = (struct ev_entry *)si.si_ptr; if(ev == NULL) continue; rb_run_event(ev); @@ -335,7 +335,7 @@ rb_select_sigio(long delay) return RB_OK; /* XXX we *could* optimise by falling out after doing num fds ... */ - for (ci = 0; ci < pollfd_list.maxindex + 1; ci++) + for(ci = 0; ci < pollfd_list.maxindex + 1; ci++) { if(((revents = pollfd_list.pollfds[ci].revents) == 0) || (pollfd_list.pollfds[ci].fd) == -1) @@ -458,7 +458,7 @@ rb_init_netio_sigio(void) } void -rb_setselect_sigio(rb_fde_t * F, unsigned int type, PF * handler, void *client_data) +rb_setselect_sigio(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { errno = ENOSYS; return; @@ -472,7 +472,7 @@ rb_select_sigio(long delay) } int -rb_setup_fd_sigio(rb_fde_t * F) +rb_setup_fd_sigio(rb_fde_t *F) { errno = ENOSYS; return -1; diff --git a/libratbox/src/snprintf.c b/libratbox/src/snprintf.c index f05c296..593fe81 100644 --- a/libratbox/src/snprintf.c +++ b/libratbox/src/snprintf.c @@ -4,7 +4,7 @@ * The original headers are below.. * Note that this implementation does not process floating point numbers so * you will likely need to fall back to using sprintf yourself to do those... - * $Id: snprintf.c 25375 2008-05-16 15:19:51Z androsyn $ + * $Id: snprintf.c 26092 2008-09-19 15:13:52Z androsyn $ */ /* @@ -27,12 +27,13 @@ #include #include -static int skip_atoi(const char **s) +static int +skip_atoi(const char **s) { - int i=0; + int i = 0; - while (isdigit(**s)) - i = i*10 + *((*s)++) - '0'; + while(isdigit(**s)) + i = i * 10 + *((*s)++) - '0'; return i; } @@ -46,50 +47,56 @@ static int skip_atoi(const char **s) /* Formats correctly any integer in [0,99999]. * Outputs from one to five digits depending on input. * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ -static char* put_dec_trunc(char *buf, unsigned q) +static char * +put_dec_trunc(char *buf, unsigned q) { unsigned d3, d2, d1, d0; - d1 = (q>>4) & 0xf; - d2 = (q>>8) & 0xf; - d3 = (q>>12); + d1 = (q >> 4) & 0xf; + d2 = (q >> 8) & 0xf; + d3 = (q >> 12); - d0 = 6*(d3 + d2 + d1) + (q & 0xf); + d0 = 6 * (d3 + d2 + d1) + (q & 0xf); q = (d0 * 0xcd) >> 11; - d0 = d0 - 10*q; - *buf++ = d0 + '0'; /* least significant digit */ - d1 = q + 9*d3 + 5*d2 + d1; - if (d1 != 0) { + d0 = d0 - 10 * q; + *buf++ = d0 + '0'; /* least significant digit */ + d1 = q + 9 * d3 + 5 * d2 + d1; + if(d1 != 0) + { q = (d1 * 0xcd) >> 11; - d1 = d1 - 10*q; - *buf++ = d1 + '0'; /* next digit */ + d1 = d1 - 10 * q; + *buf++ = d1 + '0'; /* next digit */ - d2 = q + 2*d2; - if ((d2 != 0) || (d3 != 0)) { + d2 = q + 2 * d2; + if((d2 != 0) || (d3 != 0)) + { q = (d2 * 0xd) >> 7; - d2 = d2 - 10*q; - *buf++ = d2 + '0'; /* next digit */ + d2 = d2 - 10 * q; + *buf++ = d2 + '0'; /* next digit */ - d3 = q + 4*d3; - if (d3 != 0) { + d3 = q + 4 * d3; + if(d3 != 0) + { q = (d3 * 0xcd) >> 11; - d3 = d3 - 10*q; - *buf++ = d3 + '0'; /* next digit */ - if (q != 0) - *buf++ = q + '0'; /* most sign. digit */ + d3 = d3 - 10 * q; + *buf++ = d3 + '0'; /* next digit */ + if(q != 0) + *buf++ = q + '0'; /* most sign. digit */ } } } return buf; } + /* Same with if's removed. Always emits five digits */ -static char* put_dec_full(char *buf, unsigned q) +static char * +put_dec_full(char *buf, unsigned q) { /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ /* but anyway, gcc produces better code with full-sized ints */ unsigned d3, d2, d1, d0; - d1 = (q>>4) & 0xf; - d2 = (q>>8) & 0xf; - d3 = (q>>12); + d1 = (q >> 4) & 0xf; + d2 = (q >> 8) & 0xf; + d3 = (q >> 12); /* Possible ways to approx. divide by 10 */ /* gcc -O2 replaces multiply with shifts and adds */ @@ -99,34 +106,36 @@ static char* put_dec_full(char *buf, unsigned q) // (x * 0x1a) >> 8: 11010 - same // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) - d0 = 6*(d3 + d2 + d1) + (q & 0xf); + d0 = 6 * (d3 + d2 + d1) + (q & 0xf); q = (d0 * 0xcd) >> 11; - d0 = d0 - 10*q; + d0 = d0 - 10 * q; *buf++ = d0 + '0'; - d1 = q + 9*d3 + 5*d2 + d1; - q = (d1 * 0xcd) >> 11; - d1 = d1 - 10*q; - *buf++ = d1 + '0'; - - d2 = q + 2*d2; - q = (d2 * 0xd) >> 7; - d2 = d2 - 10*q; - *buf++ = d2 + '0'; - - d3 = q + 4*d3; - q = (d3 * 0xcd) >> 11; /* - shorter code */ - /* q = (d3 * 0x67) >> 10; - would also work */ - d3 = d3 - 10*q; - *buf++ = d3 + '0'; - *buf++ = q + '0'; + d1 = q + 9 * d3 + 5 * d2 + d1; + q = (d1 * 0xcd) >> 11; + d1 = d1 - 10 * q; + *buf++ = d1 + '0'; + + d2 = q + 2 * d2; + q = (d2 * 0xd) >> 7; + d2 = d2 - 10 * q; + *buf++ = d2 + '0'; + + d3 = q + 4 * d3; + q = (d3 * 0xcd) >> 11; /* - shorter code */ + /* q = (d3 * 0x67) >> 10; - would also work */ + d3 = d3 - 10 * q; + *buf++ = d3 + '0'; + *buf++ = q + '0'; return buf; } -static char* put_dec(char *buf, unsigned long long int num) +static char * +put_dec(char *buf, unsigned long long int num) { - while (1) { + while(1) + { unsigned rem; - if (num < 100000) + if(num < 100000) return put_dec_trunc(buf, num); rem = num % 100000; num = num / 100000; @@ -142,115 +151,140 @@ static char* put_dec(char *buf, unsigned long long int num) #define SPECIAL 32 /* 0x */ #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */ -static char *number(char *buf, char *end, unsigned long long int num, int base, int size, int precision, int type) +static char * +number(char *buf, char *end, unsigned long long int num, int base, int size, int precision, + int type) { - char sign,tmp[66]; + char sign, tmp[66]; const char *digits; /* we are called with base 8, 10 or 16, only, thus don't need "g..." */ - static const char small_digits[] = "0123456789abcdefx"; /* "ghijklmnopqrstuvwxyz"; */ - static const char large_digits[] = "0123456789ABCDEFX"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ + static const char small_digits[] = "0123456789abcdefx"; /* "ghijklmnopqrstuvwxyz"; */ + static const char large_digits[] = "0123456789ABCDEFX"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ int need_pfx = ((type & SPECIAL) && base != 10); int i; digits = (type & LARGE) ? large_digits : small_digits; - if (type & LEFT) + if(type & LEFT) type &= ~ZEROPAD; - if (base < 2 || base > 36) + if(base < 2 || base > 36) return NULL; sign = 0; - if (type & SIGN) { - if ((signed long long int) num < 0) { + if(type & SIGN) + { + if((signed long long int)num < 0) + { sign = '-'; - num = - (signed long long int) num; + num = -(signed long long int)num; size--; - } else if (type & PLUS) { + } + else if(type & PLUS) + { sign = '+'; size--; - } else if (type & SPACE) { + } + else if(type & SPACE) + { sign = ' '; size--; } } - if (need_pfx) { + if(need_pfx) + { size--; - if (base == 16) + if(base == 16) size--; } /* generate full string in tmp[], in reverse order */ i = 0; - if (num == 0) + if(num == 0) tmp[i++] = '0'; /* Generic code, for any base: - else do { - tmp[i++] = digits[do_div(num,base)]; - } while (num != 0); - */ - else if (base != 10) { /* 8 or 16 */ + else do { + tmp[i++] = digits[do_div(num,base)]; + } while (num != 0); + */ + else if(base != 10) + { /* 8 or 16 */ int mask = base - 1; int shift = 3; - if (base == 16) shift = 4; - do { + if(base == 16) + shift = 4; + do + { tmp[i++] = digits[((unsigned char)num) & mask]; num >>= shift; - } while (num); - } else { /* base 10 */ + } + while(num); + } + else + { /* base 10 */ i = put_dec(tmp, num) - tmp; } /* printing 100 using %2d gives "100", not "00" */ - if (i > precision) + if(i > precision) precision = i; /* leading space padding */ size -= precision; - if (!(type & (ZEROPAD+LEFT))) { - while(--size >= 0) { - if (buf < end) + if(!(type & (ZEROPAD + LEFT))) + { + while(--size >= 0) + { + if(buf < end) *buf = ' '; ++buf; } } /* sign */ - if (sign) { - if (buf < end) + if(sign) + { + if(buf < end) *buf = sign; ++buf; } /* "0x" / "0" prefix */ - if (need_pfx) { - if (buf < end) + if(need_pfx) + { + if(buf < end) *buf = '0'; ++buf; - if (base == 16) { - if (buf < end) - *buf = digits[16]; /* for arbitrary base: digits[33]; */ + if(base == 16) + { + if(buf < end) + *buf = digits[16]; /* for arbitrary base: digits[33]; */ ++buf; } } /* zero or space padding */ - if (!(type & LEFT)) { + if(!(type & LEFT)) + { char c = (type & ZEROPAD) ? '0' : ' '; - while (--size >= 0) { - if (buf < end) + while(--size >= 0) + { + if(buf < end) *buf = c; ++buf; } } /* hmm even more zero padding? */ - while (i <= --precision) { - if (buf < end) + while(i <= --precision) + { + if(buf < end) *buf = '0'; ++buf; } /* actual digits of result */ - while (--i >= 0) { - if (buf < end) + while(--i >= 0) + { + if(buf < end) *buf = tmp[i]; ++buf; } /* trailing space padding */ - while (--size >= 0) { - if (buf < end) + while(--size >= 0) + { + if(buf < end) *buf = ' '; ++buf; } @@ -275,7 +309,8 @@ static char *number(char *buf, char *end, unsigned long long int num, int base, * Call this function if you are already dealing with a va_list. * You probably want snprintf() instead. */ -int rb_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) +int +rb_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) { int len; unsigned long long int num; @@ -289,13 +324,14 @@ int rb_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) int precision; /* min. # of digits for integers; max number of chars for from string */ int qualifier; /* 'h', 'l', or 'L' for integer fields */ - /* 'z' support added 23/7/1999 S.H. */ - /* 'z' changed to 'Z' --davidm 1/25/99 */ - /* 't' added for ptrdiff_t */ + /* 'z' support added 23/7/1999 S.H. */ + /* 'z' changed to 'Z' --davidm 1/25/99 */ + /* 't' added for ptrdiff_t */ /* Reject out-of-range values early. Large positive sizes are used for unknown buffer sizes. */ - if (rb_unlikely((int) size < 0)) { + if(rb_unlikely((int)size < 0)) + { return 0; } @@ -303,14 +339,17 @@ int rb_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) end = buf + size; /* Make sure end is always >= buf */ - if (end < buf) { + if(end < buf) + { end = ((void *)-1); size = end - buf; } - for (; *fmt ; ++fmt) { - if (*fmt != '%') { - if (str < end) + for(; *fmt; ++fmt) + { + if(*fmt != '%') + { + if(str < end) *str = *fmt; ++str; continue; @@ -318,25 +357,38 @@ int rb_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) /* process flags */ flags = 0; - repeat: - ++fmt; /* this also skips first '%' */ - switch (*fmt) { - case '-': flags |= LEFT; goto repeat; - case '+': flags |= PLUS; goto repeat; - case ' ': flags |= SPACE; goto repeat; - case '#': flags |= SPECIAL; goto repeat; - case '0': flags |= ZEROPAD; goto repeat; - } + repeat: + ++fmt; /* this also skips first '%' */ + switch (*fmt) + { + case '-': + flags |= LEFT; + goto repeat; + case '+': + flags |= PLUS; + goto repeat; + case ' ': + flags |= SPACE; + goto repeat; + case '#': + flags |= SPECIAL; + goto repeat; + case '0': + flags |= ZEROPAD; + goto repeat; + } /* get field width */ field_width = -1; - if (isdigit(*fmt)) + if(isdigit(*fmt)) field_width = skip_atoi(&fmt); - else if (*fmt == '*') { + else if(*fmt == '*') + { ++fmt; /* it's the next argument */ field_width = va_arg(args, int); - if (field_width < 0) { + if(field_width < 0) + { field_width = -field_width; flags |= LEFT; } @@ -344,26 +396,30 @@ int rb_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) /* get the precision */ precision = -1; - if (*fmt == '.') { - ++fmt; - if (isdigit(*fmt)) + if(*fmt == '.') + { + ++fmt; + if(isdigit(*fmt)) precision = skip_atoi(&fmt); - else if (*fmt == '*') { + else if(*fmt == '*') + { ++fmt; /* it's the next argument */ precision = va_arg(args, int); } - if (precision < 0) + if(precision < 0) precision = 0; } /* get the conversion qualifier */ qualifier = -1; - if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || - *fmt =='Z' || *fmt == 'z' || *fmt == 't') { + if(*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || + *fmt == 'Z' || *fmt == 'z' || *fmt == 't') + { qualifier = *fmt; ++fmt; - if (qualifier == 'l' && *fmt == 'l') { + if(qualifier == 'l' && *fmt == 'l') + { qualifier = 'L'; ++fmt; } @@ -372,144 +428,172 @@ int rb_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) /* default base */ base = 10; - switch (*fmt) { - case 'c': - if (!(flags & LEFT)) { - while (--field_width > 0) { - if (str < end) - *str = ' '; - ++str; - } - } - c = (unsigned char) va_arg(args, int); - if (str < end) - *str = c; - ++str; - while (--field_width > 0) { - if (str < end) + switch (*fmt) + { + case 'c': + if(!(flags & LEFT)) + { + while(--field_width > 0) + { + if(str < end) *str = ' '; ++str; } - continue; + } + c = (unsigned char)va_arg(args, int); + if(str < end) + *str = c; + ++str; + while(--field_width > 0) + { + if(str < end) + *str = ' '; + ++str; + } + continue; - case 's': - s = va_arg(args, char *); - if (s == NULL) { - abort(); /* prefer blowing up vs corrupt data */ - } - len = rb_strnlen(s, precision); - - if (!(flags & LEFT)) { - while (len < field_width--) { - if (str < end) - *str = ' '; - ++str; - } - } - for (i = 0; i < len; ++i) { - if (str < end) - *str = *s; - ++str; ++s; - } - while (len < field_width--) { - if (str < end) + case 's': + s = va_arg(args, char *); + if(s == NULL) + { + abort(); /* prefer blowing up vs corrupt data */ + } + len = rb_strnlen(s, precision); + + if(!(flags & LEFT)) + { + while(len < field_width--) + { + if(str < end) *str = ' '; ++str; } - continue; + } + for(i = 0; i < len; ++i) + { + if(str < end) + *str = *s; + ++str; + ++s; + } + while(len < field_width--) + { + if(str < end) + *str = ' '; + ++str; + } + continue; - case 'p': - if (field_width == -1) { - field_width = 2*sizeof(void *); - flags |= ZEROPAD; - } - str = number(str, end, - (unsigned long) va_arg(args, void *), - 16, field_width, precision, flags); - continue; - - - case 'n': - /* FIXME: - * What does C99 say about the overflow case here? */ - if (qualifier == 'l') { - long * ip = va_arg(args, long *); - *ip = (str - buf); - } else if (qualifier == 'Z' || qualifier == 'z') { - size_t * ip = va_arg(args, size_t *); - *ip = (str - buf); - } else { - int * ip = va_arg(args, int *); - *ip = (str - buf); - } - continue; + case 'p': + if(field_width == -1) + { + field_width = 2 * sizeof(void *); + flags |= ZEROPAD; + } + str = number(str, end, + (unsigned long)va_arg(args, void *), + 16, field_width, precision, flags); + continue; - case '%': - if (str < end) - *str = '%'; - ++str; - continue; - - /* integer number formats - set up the flags and "break" */ - case 'o': - base = 8; - break; - - case 'X': - flags |= LARGE; - case 'x': - base = 16; - break; - - case 'd': - case 'i': - flags |= SIGN; - case 'u': - break; - - default: - if (str < end) - *str = '%'; + + case 'n': + /* FIXME: + * What does C99 say about the overflow case here? */ + if(qualifier == 'l') + { + long *ip = va_arg(args, long *); + *ip = (str - buf); + } + else if(qualifier == 'Z' || qualifier == 'z') + { + size_t *ip = va_arg(args, size_t *); + *ip = (str - buf); + } + else + { + int *ip = va_arg(args, int *); + *ip = (str - buf); + } + continue; + + case '%': + if(str < end) + *str = '%'; + ++str; + continue; + + /* integer number formats - set up the flags and "break" */ + case 'o': + base = 8; + break; + + case 'X': + flags |= LARGE; + case 'x': + base = 16; + break; + + case 'd': + case 'i': + flags |= SIGN; + case 'u': + break; + + default: + if(str < end) + *str = '%'; + ++str; + if(*fmt) + { + if(str < end) + *str = *fmt; ++str; - if (*fmt) { - if (str < end) - *str = *fmt; - ++str; - } else { - --fmt; - } - continue; + } + else + { + --fmt; + } + continue; } - if (qualifier == 'L') + if(qualifier == 'L') num = va_arg(args, long long int); - else if (qualifier == 'l') { + else if(qualifier == 'l') + { num = va_arg(args, unsigned long); - if (flags & SIGN) - num = (signed long) num; - } else if (qualifier == 'Z' || qualifier == 'z') { + if(flags & SIGN) + num = (signed long)num; + } + else if(qualifier == 'Z' || qualifier == 'z') + { num = va_arg(args, size_t); - } else if (qualifier == 't') { + } + else if(qualifier == 't') + { num = va_arg(args, ptrdiff_t); - } else if (qualifier == 'h') { - num = (unsigned short) va_arg(args, int); - if (flags & SIGN) - num = (signed short) num; - } else { + } + else if(qualifier == 'h') + { + num = (unsigned short)va_arg(args, int); + if(flags & SIGN) + num = (signed short)num; + } + else + { num = va_arg(args, unsigned int); - if (flags & SIGN) - num = (signed int) num; + if(flags & SIGN) + num = (signed int)num; } - str = number(str, end, num, base, - field_width, precision, flags); + str = number(str, end, num, base, field_width, precision, flags); } - if (size > 0) { - if (str < end) + if(size > 0) + { + if(str < end) *str = '\0'; else end[-1] = '\0'; } /* the trailing null byte doesn't count towards the total */ - return str-buf; + return str - buf; } /** @@ -524,13 +608,14 @@ int rb_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) * as per ISO C99. If the return is greater than or equal to * @size, the resulting string is truncated. */ -int rb_snprintf(char * buf, size_t size, const char *fmt, ...) +int +rb_snprintf(char *buf, size_t size, const char *fmt, ...) { va_list args; int i; va_start(args, fmt); - i=rb_vsnprintf(buf,size,fmt,args); + i = rb_vsnprintf(buf, size, fmt, args); va_end(args); return i; } @@ -548,7 +633,8 @@ int rb_snprintf(char * buf, size_t size, const char *fmt, ...) * Call this function if you are already dealing with a va_list. * You probably want sprintf() instead. */ -int rb_vsprintf(char *buf, const char *fmt, va_list args) +int +rb_vsprintf(char *buf, const char *fmt, va_list args) { return rb_vsnprintf(buf, INT_MAX, fmt, args); } @@ -563,13 +649,14 @@ int rb_vsprintf(char *buf, const char *fmt, va_list args) * into @buf. Use snprintf() or scnprintf() in order to avoid * buffer overflows. */ -int rb_sprintf(char * buf, const char *fmt, ...) +int +rb_sprintf(char *buf, const char *fmt, ...) { va_list args; int i; va_start(args, fmt); - i=rb_vsnprintf(buf, INT_MAX, fmt, args); + i = rb_vsnprintf(buf, INT_MAX, fmt, args); va_end(args); return i; } @@ -578,12 +665,12 @@ int rb_sprintf(char * buf, const char *fmt, ...) * rb_vsprintf_append() * appends sprintf formatted string to the end of the buffer */ - + int rb_vsprintf_append(char *str, const char *format, va_list ap) { - size_t x = strlen(str); - return(rb_vsprintf(str+x, format, ap) + x); + size_t x = strlen(str); + return (rb_vsprintf(str + x, format, ap) + x); } /* @@ -593,12 +680,12 @@ rb_vsprintf_append(char *str, const char *format, va_list ap) int rb_sprintf_append(char *str, const char *format, ...) { - int x; - va_list ap; - va_start(ap, format); - x = rb_vsprintf_append(str, format, ap); - va_end(ap); - return(x); + int x; + va_list ap; + va_start(ap, format); + x = rb_vsprintf_append(str, format, ap); + va_end(ap); + return (x); } /* @@ -610,8 +697,17 @@ rb_sprintf_append(char *str, const char *format, ...) int rb_vsnprintf_append(char *str, size_t len, const char *format, va_list ap) { - size_t x = strlen(str); - return(rb_vsnprintf(str+x, len - x, format, ap) + x); + size_t x; + if(len == 0) + return 0; + x = strlen(str); + + if(len < x) + { + str[len - 1] = '\0'; + return len - 1; + } + return (rb_vsnprintf(str + x, len - x, format, ap) + x); } /* @@ -623,11 +719,10 @@ rb_vsnprintf_append(char *str, size_t len, const char *format, va_list ap) int rb_snprintf_append(char *str, size_t len, const char *format, ...) { - int x; - va_list ap; - va_start(ap, format); - x = rb_vsnprintf_append(str, len, format, ap); - va_end(ap); - return(x); + int x; + va_list ap; + va_start(ap, format); + x = rb_vsnprintf_append(str, len, format, ap); + va_end(ap); + return (x); } - diff --git a/libratbox/src/tools.c b/libratbox/src/tools.c index 2ee3e14..77d612f 100644 --- a/libratbox/src/tools.c +++ b/libratbox/src/tools.c @@ -20,7 +20,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: tools.c 25040 2008-01-23 16:11:34Z androsyn $ + * $Id: tools.c 26170 2008-10-26 20:59:07Z androsyn $ * * Here is the original header: * @@ -62,7 +62,7 @@ rb_init_rb_dlink_nodes(size_t dh_size) rb_dlink_node * rb_make_rb_dlink_node(void) { - return(rb_bh_alloc(dnode_heap)); + return (rb_bh_alloc(dnode_heap)); } /* @@ -73,7 +73,7 @@ rb_make_rb_dlink_node(void) * side effects - free given rb_dlink_node */ void -rb_free_rb_dlink_node(rb_dlink_node * ptr) +rb_free_rb_dlink_node(rb_dlink_node *ptr) { assert(ptr != NULL); rb_bh_free(dnode_heap, ptr); @@ -97,7 +97,7 @@ rb_string_to_array(char *string, char **parv, int maxpara) if(string == NULL || string[0] == '\0') return x; - while (*xbuf == ' ') /* skip leading spaces */ + while(*xbuf == ' ') /* skip leading spaces */ xbuf++; if(*xbuf == '\0') /* ignore all-space args */ return x; @@ -123,12 +123,12 @@ rb_string_to_array(char *string, char **parv, int maxpara) else return x; } - while (*xbuf == ' ') + while(*xbuf == ' ') xbuf++; if(*xbuf == '\0') return x; } - while (x < maxpara - 1); + while(x < maxpara - 1); if(*p == ':') p++; @@ -139,20 +139,20 @@ rb_string_to_array(char *string, char **parv, int maxpara) } #ifndef HAVE_STRLCAT -size_t +size_t rb_strlcat(char *dest, const char *src, size_t count) { - size_t dsize = strlen(dest); - size_t len = strlen(src); - size_t res = dsize + len; - - dest += dsize; - count -= dsize; - if (len >= count) - len = count-1; - memcpy(dest, src, len); - dest[len] = 0; - return res; + size_t dsize = strlen(dest); + size_t len = strlen(src); + size_t res = dsize + len; + + dest += dsize; + count -= dsize; + if(len >= count) + len = count - 1; + memcpy(dest, src, len); + dest[len] = 0; + return res; } #else size_t @@ -161,19 +161,20 @@ rb_strlcat(char *dest, const char *src, size_t count) return strlcat(dest, src, count); } #endif - + #ifndef HAVE_STRLCPY -size_t +size_t rb_strlcpy(char *dest, const char *src, size_t size) { - size_t ret = strlen(src); - - if (size) { - size_t len = (ret >= size) ? size-1 : ret; - memcpy(dest, src, len); - dest[len] = '\0'; - } - return ret; + size_t ret = strlen(src); + + if(size) + { + size_t len = (ret >= size) ? size - 1 : ret; + memcpy(dest, src, len); + dest[len] = '\0'; + } + return ret; } #else size_t @@ -185,14 +186,14 @@ rb_strlcpy(char *dest, const char *src, size_t size) #ifndef HAVE_STRNLEN -size_t +size_t rb_strnlen(const char *s, size_t count) -{ +{ const char *sc; - for (sc = s; count-- && *sc != '\0'; ++sc) + for(sc = s; count-- && *sc != '\0'; ++sc) ;; return sc - s; -} +} #else size_t rb_strnlen(const char *s, size_t count) @@ -201,3 +202,42 @@ rb_strnlen(const char *s, size_t count) } #endif +/* rb_basename + * + * input - + * output - + * side effects - + */ +char * +rb_basename(const char *path) +{ + const char *s; + + if(!(s = strrchr(path, '/'))) + s = path; + else + s++; + return rb_strdup(s); +} + +/* + * rb_dirname + */ + +char * +rb_dirname (const char *path) +{ + char *s; + + s = strrchr(path, '/'); + if(s == NULL) + { + return rb_strdup("."); + } + + /* remove extra slashes */ + while(s > path && *s == '/') + --s; + + return rb_strndup(path, ((uintptr_t)s - (uintptr_t)path) + 2); +} diff --git a/libratbox/src/unix.c b/libratbox/src/unix.c index a7b2efd..1472253 100644 --- a/libratbox/src/unix.c +++ b/libratbox/src/unix.c @@ -21,26 +21,44 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: unix.c 25038 2008-01-23 16:03:08Z androsyn $ + * $Id: unix.c 26180 2008-11-11 00:00:12Z androsyn $ */ #include #include -#ifndef WINDOWS + +#ifndef _WIN32 + +#include + + #if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN) #include + +#ifdef __APPLE__ +#include +#endif + +#ifndef __APPLE__ extern char **environ; +#endif pid_t rb_spawn_process(const char *path, const char **argv) { pid_t pid; const void *arghack = argv; + char **myenviron; posix_spawnattr_t spattr; posix_spawnattr_init(&spattr); #ifdef POSIX_SPAWN_USEVFORK posix_spawnattr_setflags(&spattr, POSIX_SPAWN_USEVFORK); #endif - if(posix_spawn(&pid, path, NULL, &spattr, arghack, environ)) +#ifdef __APPLE__ + myenviron = *_NSGetEnviron(); /* apple needs to go fuck themselves for this */ +#else + myenviron = environ; +#endif + if(posix_spawn(&pid, path, NULL, &spattr, arghack, myenviron)) { return -1; } @@ -53,10 +71,10 @@ rb_spawn_process(const char *path, const char **argv) pid_t pid; if(!(pid = vfork())) { - execv(path, (const void *)argv); /* make gcc shut up */ - _exit(1); /* if we're still here, we're screwed */ + execv(path, (const void *)argv); /* make gcc shut up */ + _exit(1); /* if we're still here, we're screwed */ } - return(pid); + return (pid); } #endif @@ -78,7 +96,7 @@ rb_gettimeofday(struct timeval *tv, void *tz) int rb_gettimeofday(struct timeval *tv, void *tz) { - return(gettimeofday(tv, tz)); + return (gettimeofday(tv, tz)); } #endif @@ -90,13 +108,44 @@ rb_sleep(unsigned int seconds, unsigned int useconds) tv.tv_nsec = (useconds * 1000); tv.tv_sec = seconds; nanosleep(&tv, NULL); -#else +#else struct timeval tv; tv.tv_sec = seconds; tv.tv_usec = useconds; select(0, NULL, NULL, NULL, &tv); #endif } -#endif /* !WINDOWS */ + +/* this is to keep some linkers from bitching about exporting a non-existant symbol..bleh */ +char * +rb_strerror(int error) +{ + return strerror(error); +} + +int +rb_kill(pid_t pid, int sig) +{ + return kill(pid, sig); +} + +int +rb_setenv(const char *name, const char *value, int overwrite) +{ + return setenv(name, value, overwrite); +} + +pid_t +rb_waitpid(pid_t pid, int *status, int options) +{ + return waitpid(pid, status, options); +} + +pid_t +rb_getpid(void) +{ + return getpid(); +} +#endif /* !WIN32 */ diff --git a/libratbox/src/win32.c b/libratbox/src/win32.c index abc8cc0..691149a 100644 --- a/libratbox/src/win32.c +++ b/libratbox/src/win32.c @@ -23,14 +23,14 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: win32.c 25038 2008-01-23 16:03:08Z androsyn $ + * $Id: win32.c 26092 2008-09-19 15:13:52Z androsyn $ */ #include #include #include -#ifdef WIN32 +#ifdef _WIN32 static HWND hwnd; @@ -54,7 +54,7 @@ typedef union #define EPOCH_BIAS Const64(116444736000000000) pid_t -getpid() +rb_getpid() { return GetCurrentProcessId(); } @@ -69,10 +69,10 @@ rb_gettimeofday(struct timeval *tp, void *not_used) GetSystemTimeAsFileTime(&ft.ft_val); /* seconds since epoch */ - tp->tv_sec = (long) ((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000)); + tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000)); /* microseconds remaining */ - tp->tv_usec = (long) ((ft.ft_i64 / Const64(10)) % Const64(1000000)); + tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(1000000)); return 0; } @@ -94,7 +94,7 @@ rb_spawn_process(const char *path, const char **argv) } pid_t -waitpid(int pid, int *status, int flags) +rb_waitpid(int pid, int *status, int flags) { DWORD timeout = (flags & WNOHANG) ? 0 : INFINITE; HANDLE hProcess; @@ -113,7 +113,7 @@ waitpid(int pid, int *status, int flags) { if(GetExitCodeProcess(hProcess, &waitcode)) { - *status = (int) ((waitcode & 0xff) << 8); + *status = (int)((waitcode & 0xff) << 8); CloseHandle(hProcess); return pid; } @@ -127,7 +127,7 @@ waitpid(int pid, int *status, int flags) } int -setenv(const char *name, const char *value, int overwrite) +rb_setenv(const char *name, const char *value, int overwrite) { char *buf; int len; @@ -152,7 +152,7 @@ setenv(const char *name, const char *value, int overwrite) } int -kill(int pid, int sig) +rb_kill(int pid, int sig) { HANDLE hProcess; hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid); @@ -180,15 +180,134 @@ kill(int pid, int sig) } -int -rb_pass_fd_to_process(int fd, pid_t process, rb_fde_t *F) +/* + * packet format is + uint32_t magic + uint8_t protocol count + WSAPROTOCOL_INFO * count + size_t datasize + data + + */ + +static int +make_wsaprotocol_info(pid_t process, rb_fde_t *F, WSAPROTOCOL_INFO * inf) { WSAPROTOCOL_INFO info; - WSADuplicateSocket((SOCKET)fd, process, &info); - rb_write(F, &info, sizeof(info)); + if(!WSADuplicateSocket((SOCKET) rb_get_fd(F), process, &info)) + { + memcpy(inf, &info, sizeof(WSAPROTOCOL_INFO)); + return 1; + } return 0; } +static rb_fde_t * +make_fde_from_wsaprotocol_info(void *data) +{ + WSAPROTOCOL_INFO *info = data; + SOCKET t; + t = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, info, 0, 0); + if(t == INVALID_SOCKET) + { + rb_get_errno(); + return NULL; + } + return rb_open(t, RB_FD_SOCKET, "remote_socket"); +} + +static uint8_t fd_buf[16384]; +#define MAGIC_CONTROL 0xFF0ACAFE + +int +rb_send_fd_buf(rb_fde_t *xF, rb_fde_t **F, int count, void *data, size_t datasize, pid_t pid) +{ + size_t bufsize = + sizeof(uint32_t) + sizeof(uint8_t) + (sizeof(WSAPROTOCOL_INFO) * (size_t)count) + + sizeof(size_t) + datasize; + int i; + uint32_t magic = MAGIC_CONTROL; + void *ptr; + if(count > 4) + { + errno = EINVAL; + return -1; + } + if(bufsize > sizeof(fd_buf)) + { + errno = EINVAL; + return -1; + } + memset(fd_buf, 0, sizeof(fd_buf)); + + ptr = fd_buf; + memcpy(ptr, &magic, sizeof(magic)); + ptr = (void *)((uintptr_t)ptr + (uintptr_t)sizeof(magic)); + *((uint8_t *)ptr) = count; + ptr = (void *)((uintptr_t)ptr + (uintptr_t)sizeof(uint8_t)); + + for(i = 0; i < count; i++) + { + make_wsaprotocol_info(pid, F[i], (WSAPROTOCOL_INFO *) ptr); + ptr = (void *)((uintptr_t)ptr + (uintptr_t)sizeof(WSAPROTOCOL_INFO)); + } + memcpy(ptr, &datasize, sizeof(size_t)); + ptr = (void *)((uintptr_t)ptr + (uintptr_t)sizeof(size_t)); + memcpy(ptr, data, datasize); + return rb_write(xF, fd_buf, bufsize); +} + +#ifdef MYMIN +#undef MYMIN +#endif +#define MYMIN(a, b) ((a) < (b) ? (a) : (b)) + +int +rb_recv_fd_buf(rb_fde_t *F, void *data, size_t datasize, rb_fde_t **xF, int nfds) +{ + size_t minsize = sizeof(uint32_t) + sizeof(uint8_t) + sizeof(size_t); + size_t datalen; + ssize_t retlen; + uint32_t magic; + uint8_t count; + unsigned int i; + void *ptr; + ssize_t ret; + memset(fd_buf, 0, sizeof(fd_buf)); /* some paranoia here... */ + ret = rb_read(F, fd_buf, sizeof(fd_buf)); + if(ret <= 0) + { + return ret; + } + if(ret < (ssize_t) minsize) + { + errno = EINVAL; + return -1; + } + ptr = fd_buf; + memcpy(&magic, ptr, sizeof(uint32_t)); + if(magic != MAGIC_CONTROL) + { + errno = EAGAIN; + return -1; + } + ptr = (void *)((uintptr_t)ptr + (uintptr_t)sizeof(uint32_t)); + memcpy(&count, ptr, sizeof(uint8_t)); + ptr = (void *)((uintptr_t)ptr + (uintptr_t)sizeof(uint8_t)); + for(i = 0; i < count && i < (unsigned int)nfds; i++) + { + rb_fde_t *tF = make_fde_from_wsaprotocol_info(ptr); + if(tF == NULL) + return -1; + xF[i] = tF; + ptr = (void *)((uintptr_t)ptr + (uintptr_t)sizeof(WSAPROTOCOL_INFO)); + } + memcpy(&datalen, ptr, sizeof(size_t)); + ptr = (void *)((uintptr_t)ptr + (uintptr_t)sizeof(size_t)); + retlen = MYMIN(datalen, datasize); + memcpy(data, ptr, datalen); + return retlen; +} static LRESULT CALLBACK rb_process_events(HWND nhwnd, UINT umsg, WPARAM wparam, LPARAM lparam) @@ -280,14 +399,12 @@ rb_init_netio_win32(void) void rb_sleep(unsigned int seconds, unsigned int useconds) { - struct timeval tv; - tv.tv_sec = seconds; - tv.tv_usec = useconds; - select(0, NULL, NULL, NULL, &tv); + DWORD msec = seconds * 1000;; + Sleep(msec); } int -rb_setup_fd_win32(rb_fde_t * F) +rb_setup_fd_win32(rb_fde_t *F) { if(F == NULL) return 0; @@ -312,7 +429,7 @@ rb_setup_fd_win32(rb_fde_t * F) } void -rb_setselect_win32(rb_fde_t * F, unsigned int type, PF * handler, void *client_data) +rb_setselect_win32(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { int old_flags = F->pflags; @@ -379,8 +496,8 @@ rb_select_win32(long delay) #undef strerror #endif -const char * -wsock_strerror(int error) +static const char * +_rb_strerror(int error) { switch (error) { @@ -488,6 +605,14 @@ wsock_strerror(int error) return strerror(error); } }; + +char * +rb_strerror(int error) +{ + static char buf[128]; + rb_strlcpy(buf, _rb_strerror(error), sizeof(buf)); + return buf; +} #else /* win32 not supported */ int rb_init_netio_win32(void) @@ -497,7 +622,7 @@ rb_init_netio_win32(void) } void -rb_setselect_win32(rb_fde_t * F, unsigned int type, PF * handler, void *client_data) +rb_setselect_win32(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) { errno = ENOSYS; return; @@ -511,9 +636,9 @@ rb_select_win32(long delay) } int -rb_setup_fd_win32(rb_fde_t * F) +rb_setup_fd_win32(rb_fde_t *F) { errno = ENOSYS; return -1; } -#endif /* WIN32 */ +#endif /* _WIN32 */ diff --git a/src/sslproc.c b/src/sslproc.c index 4f9af8b..3827343 100644 --- a/src/sslproc.c +++ b/src/sslproc.c @@ -18,11 +18,13 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: sslproc.c 25677 2008-07-06 04:21:42Z androsyn $ + * $Id$ */ #include #include "stdinc.h" + + #include "s_conf.h" #include "logger.h" #include "listener.h" @@ -37,10 +39,10 @@ #define ZIPSTATS_TIME 60 static void collect_zipstats(void *unused); -static void ssl_read_ctl(rb_fde_t *F, void *data); +static void ssl_read_ctl(rb_fde_t * F, void *data); static int ssld_count; -static char tmpbuf[READBUF_SIZE]; +static char tmpbuf[READBUF_SIZE]; static char nul = '\0'; #define MAXPASSFD 4 @@ -67,34 +69,39 @@ struct _ssl_ctl uint8_t dead; }; -static void send_new_ssl_certs_one(ssl_ctl_t *ctl, const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params); -static void send_init_prng(ssl_ctl_t *ctl, prng_seed_t seedtype, const char *path); +static void send_new_ssl_certs_one(ssl_ctl_t * ctl, const char *ssl_cert, + const char *ssl_private_key, const char *ssl_dh_params); +static void send_init_prng(ssl_ctl_t * ctl, prng_seed_t seedtype, const char *path); static rb_dlink_list ssl_daemons; -static inline int32_t buf_to_int32(char *buf) +static inline int32_t +buf_to_int32(char *buf) { int32_t x; memcpy(&x, buf, sizeof(x)); return x; } -static inline void int32_to_buf(char *buf, int32_t x) +static inline void +int32_to_buf(char *buf, int32_t x) { memcpy(buf, &x, sizeof(x)); return; } -static inline uint16_t buf_to_uint16(char *buf) +static inline uint16_t +buf_to_uint16(char *buf) { uint16_t x; memcpy(&x, buf, sizeof(x)); return x; } -static inline void uint16_to_buf(char *buf, uint16_t x) +static inline void +uint16_to_buf(char *buf, uint16_t x) { memcpy(buf, &x, sizeof(x)); return; @@ -102,13 +109,13 @@ static inline void uint16_to_buf(char *buf, uint16_t x) static ssl_ctl_t * -allocate_ssl_daemon(rb_fde_t *F, rb_fde_t *P, int pid) +allocate_ssl_daemon(rb_fde_t * F, rb_fde_t * P, int pid) { ssl_ctl_t *ctl; - + if(F == NULL || pid < 0) return NULL; - ctl = rb_malloc(sizeof(ssl_ctl_t)); + ctl = rb_malloc(sizeof(ssl_ctl_t)); ctl->F = F; ctl->P = P; ctl->pid = pid; @@ -118,22 +125,22 @@ allocate_ssl_daemon(rb_fde_t *F, rb_fde_t *P, int pid) } static void -free_ssl_daemon(ssl_ctl_t *ctl) +free_ssl_daemon(ssl_ctl_t * ctl) { rb_dlink_node *ptr; ssl_ctl_buf_t *ctl_buf; int x; if(ctl->cli_count) return; - + RB_DLINK_FOREACH(ptr, ctl->readq.head) { ctl_buf = ptr->data; for(x = 0; x < ctl_buf->nfds; x++) - rb_close(ctl_buf->F[x]); + rb_close(ctl_buf->F[x]); rb_free(ctl_buf->buf); - rb_free(ctl_buf); + rb_free(ctl_buf); } RB_DLINK_FOREACH(ptr, ctl->writeq.head) @@ -170,26 +177,26 @@ ssl_killall(void) continue; ctl->dead = 1; ssld_count--; - kill(ctl->pid, SIGKILL); + rb_kill(ctl->pid, SIGKILL); } } static void -ssl_dead(ssl_ctl_t *ctl) +ssl_dead(ssl_ctl_t * ctl) { if(ctl->dead) return; - + ctl->dead = 1; ssld_count--; - kill(ctl->pid, SIGKILL); /* make sure the process is really gone */ + rb_kill(ctl->pid, SIGKILL); /* make sure the process is really gone */ ilog(L_MAIN, "ssld helper died - attempting to restart"); sendto_realops_snomask(SNO_GENERAL, L_ALL, "ssld helper died - attempting to restart"); start_ssldaemon(1, ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params); } static void -ssl_do_pipe(rb_fde_t *F, void *data) +ssl_do_pipe(rb_fde_t * F, void *data) { int retlen; ssl_ctl_t *ctl = data; @@ -222,10 +229,17 @@ start_ssldaemon(int count, const char *ssl_cert, const char *ssl_private_key, co { rb_fde_t *F1, *F2; rb_fde_t *P1, *P2; +#ifdef _WIN32 + const char *suffix = ".exe"; +#else + const char *suffix = ""; +#endif + char fullpath[PATH_MAX + 1]; char fdarg[6]; const char *parv[2]; char buf[128]; + char s_pid[10]; pid_t pid; int started = 0, i; @@ -235,7 +249,8 @@ start_ssldaemon(int count, const char *ssl_cert, const char *ssl_private_key, co if(ssld_spin_count > 20 && (rb_current_time() - last_spin < 5)) { ilog(L_MAIN, "ssld helper is spinning - will attempt to restart in 1 minute"); - sendto_realops_snomask(SNO_GENERAL, L_ALL, "ssld helper is spinning - will attempt to restart in 1 minute"); + sendto_realops_snomask(SNO_GENERAL, L_ALL, + "ssld helper is spinning - will attempt to restart in 1 minute"); rb_event_add("restart_ssld_event", restart_ssld_event, NULL, 60); ssld_wait = 1; return 0; @@ -243,22 +258,25 @@ start_ssldaemon(int count, const char *ssl_cert, const char *ssl_private_key, co ssld_spin_count++; last_spin = rb_current_time(); + if(ssld_path == NULL) { - rb_snprintf(fullpath, sizeof(fullpath), "%s/ssld", BINPATH); - + rb_snprintf(fullpath, sizeof(fullpath), "%s/ssld%s", BINPATH, suffix); + if(access(fullpath, X_OK) == -1) { - rb_snprintf(fullpath, sizeof(fullpath), "%s/bin/ssld", ConfigFileEntry.dpath); + rb_snprintf(fullpath, sizeof(fullpath), "%s/bin/ssld%s", + ConfigFileEntry.dpath, suffix); if(access(fullpath, X_OK) == -1) { - ilog(L_MAIN, "Unable to execute ssld in %s/bin or %s", ConfigFileEntry.dpath, BINPATH); - return 0 ; + ilog(L_MAIN, + "Unable to execute ssld%s in %s/bin or %s", + ConfigFileEntry.dpath, suffix, BINPATH); + return 0; } } ssld_path = rb_strdup(fullpath); } - rb_strlcpy(buf, "-ircd ssld daemon helper", sizeof(buf)); parv[0] = buf; parv[1] = NULL; @@ -270,12 +288,18 @@ start_ssldaemon(int count, const char *ssl_cert, const char *ssl_private_key, co rb_set_buffers(F1, READBUF_SIZE); rb_set_buffers(F2, READBUF_SIZE); rb_snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(F2)); - setenv("CTL_FD", fdarg, 1); + rb_setenv("CTL_FD", fdarg, 1); rb_pipe(&P1, &P2, "SSL/TLS pipe"); rb_snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(P1)); - setenv("CTL_PIPE", fdarg, 1); - - pid = rb_spawn_process(ssld_path, (const char **)parv); + rb_setenv("CTL_PIPE", fdarg, 1); + rb_snprintf(s_pid, sizeof(s_pid), "%d", getpid()); + rb_setenv("CTL_PPID", s_pid, 1); +#ifdef _WIN32 + SetHandleInformation((HANDLE) rb_get_fd(F2), HANDLE_FLAG_INHERIT, 1); + SetHandleInformation((HANDLE) rb_get_fd(P1), HANDLE_FLAG_INHERIT, 1); +#endif + + pid = rb_spawn_process(ssld_path, (const char **) parv); if(pid == -1) { ilog(L_MAIN, "Unable to create ssld: %s\n", strerror(errno)); @@ -297,55 +321,57 @@ start_ssldaemon(int count, const char *ssl_cert, const char *ssl_private_key, co send_init_prng(ctl, RB_PRNG_DEFAULT, NULL); } if(ssl_ok && ssl_cert != NULL && ssl_private_key != NULL) - send_new_ssl_certs_one(ctl, ssl_cert, ssl_private_key, ssl_dh_params != NULL ? ssl_dh_params : ""); + send_new_ssl_certs_one(ctl, ssl_cert, ssl_private_key, + ssl_dh_params != NULL ? ssl_dh_params : ""); ssl_read_ctl(ctl->F, ctl); ssl_do_pipe(P2, ctl); + } - return started; + return started; } static void -ssl_process_zipstats(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf) +ssl_process_zipstats(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf) { struct Client *server; struct ZipStats *zips; int parc; char *parv[6]; - parc = rb_string_to_array(ctl_buf->buf, parv, 6); + parc = rb_string_to_array(ctl_buf->buf, parv, 6); server = find_server(NULL, parv[1]); if(server == NULL || server->localClient == NULL || !IsCapable(server, CAP_ZIP)) return; if(server->localClient->zipstats == NULL) server->localClient->zipstats = rb_malloc(sizeof(struct ZipStats)); - + zips = server->localClient->zipstats; zips->in += strtoull(parv[2], NULL, 10); zips->in_wire += strtoull(parv[3], NULL, 10); zips->out += strtoull(parv[4], NULL, 10); zips->out_wire += strtoull(parv[5], NULL, 10); - + if(zips->in > 0) - zips->in_ratio = ((double)(zips->in - zips->in_wire) / (double) zips->in) * 100.00; + zips->in_ratio = ((double) (zips->in - zips->in_wire) / (double) zips->in) * 100.00; else zips->in_ratio = 0; - + if(zips->out > 0) - zips->out_ratio = ((double)(zips->out - zips->out_wire) / (double) zips->out) * 100.00; + zips->out_ratio = ((double) (zips->out - zips->out_wire) / (double) zips->out) * 100.00; else zips->out_ratio = 0; } static void -ssl_process_dead_fd(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf) +ssl_process_dead_fd(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf) { struct Client *client_p; char reason[256]; int32_t fd; if(ctl_buf->buflen < 6) - return; /* bogus message..drop it.. XXX should warn here */ - + return; /* bogus message..drop it.. XXX should warn here */ + fd = buf_to_int32(&ctl_buf->buf[1]); rb_strlcpy(reason, &ctl_buf->buf[5], sizeof(reason)); client_p = find_cli_fd_hash(fd); @@ -353,7 +379,7 @@ ssl_process_dead_fd(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf) return; if(IsAnyServer(client_p)) { - sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL, "ssld error for %s: %s", client_p->name, reason); + sendto_realops_snomask(SNO_GENERAL, L_ALL, "ssld error for %s: %s", client_p->name, reason); ilog(L_SERVER, "ssld error for %s: %s", log_client_name(client_p, SHOW_IP), reason); } exit_client(client_p, client_p, &me, reason); @@ -361,14 +387,14 @@ ssl_process_dead_fd(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf) static void -ssl_process_zip_ready(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf) +ssl_process_zip_ready(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf) { struct Client *client_p; int32_t fd; if(ctl_buf->buflen < 5) - return; /* bogus message..drop it.. XXX should warn here */ - + return; /* bogus message..drop it.. XXX should warn here */ + fd = buf_to_int32(&ctl_buf->buf[1]); client_p = find_cli_fd_hash(fd); if(client_p == NULL) @@ -383,49 +409,49 @@ ssl_process_zip_ready(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf) static void -ssl_process_cmd_recv(ssl_ctl_t *ctl) +ssl_process_cmd_recv(ssl_ctl_t * ctl) { static const char *cannot_setup_ssl = "ssld cannot setup ssl, check your certificates and private key"; static const char *no_ssl_or_zlib = "ssld has neither SSL/TLS or zlib support killing all sslds"; - rb_dlink_node *ptr, *next; + rb_dlink_node *ptr, *next; ssl_ctl_buf_t *ctl_buf; if(ctl->dead) return; RB_DLINK_FOREACH_SAFE(ptr, next, ctl->readq.head) { - ctl_buf = ptr->data; - switch(*ctl_buf->buf) + ctl_buf = ptr->data; + switch (*ctl_buf->buf) { - case 'N': - ssl_ok = 0; /* ssld says it can't do ssl/tls */ - break; - case 'D': - ssl_process_dead_fd(ctl, ctl_buf); - break; - case 'S': - ssl_process_zipstats(ctl, ctl_buf); - break; - case 'I': - ssl_ok = 0; - ilog(L_MAIN, cannot_setup_ssl); - sendto_realops_snomask(SNO_GENERAL, L_ALL, cannot_setup_ssl); - case 'U': - zlib_ok = 0; - ssl_ok = 0; - ilog(L_MAIN, no_ssl_or_zlib); - sendto_realops_snomask(SNO_GENERAL, L_ALL, no_ssl_or_zlib); - ssl_killall(); - break; - case 'R': - ssl_process_zip_ready(ctl, ctl_buf); - break; - case 'z': - zlib_ok = 0; - break; - default: - ilog(L_MAIN, "Received invalid command from ssld: %s", ctl_buf->buf); - sendto_realops_snomask(SNO_GENERAL, L_ALL, "Received invalid command from ssld"); - break; + case 'N': + ssl_ok = 0; /* ssld says it can't do ssl/tls */ + break; + case 'D': + ssl_process_dead_fd(ctl, ctl_buf); + break; + case 'S': + ssl_process_zipstats(ctl, ctl_buf); + break; + case 'I': + ssl_ok = 0; + ilog(L_MAIN, cannot_setup_ssl); + sendto_realops_snomask(SNO_GENERAL, L_ALL, cannot_setup_ssl); + case 'U': + zlib_ok = 0; + ssl_ok = 0; + ilog(L_MAIN, no_ssl_or_zlib); + sendto_realops_snomask(SNO_GENERAL, L_ALL, no_ssl_or_zlib); + ssl_killall(); + break; + case 'R': + ssl_process_zip_ready(ctl, ctl_buf); + break; + case 'z': + zlib_ok = 0; + break; + default: + ilog(L_MAIN, "Received invalid command from ssld: %s", ctl_buf->buf); + sendto_realops_snomask(SNO_GENERAL, L_ALL, "Received invalid command from ssld"); + break; } rb_dlinkDelete(ptr, &ctl->readq); rb_free(ctl_buf->buf); @@ -436,7 +462,7 @@ ssl_process_cmd_recv(ssl_ctl_t *ctl) static void -ssl_read_ctl(rb_fde_t *F, void *data) +ssl_read_ctl(rb_fde_t * F, void *data) { ssl_ctl_buf_t *ctl_buf; ssl_ctl_t *ctl = data; @@ -450,19 +476,21 @@ ssl_read_ctl(rb_fde_t *F, void *data) ctl_buf->buf = rb_malloc(READSIZE); retlen = rb_recv_fd_buf(ctl->F, ctl_buf->buf, READSIZE, ctl_buf->F, 4); ctl_buf->buflen = retlen; - if(retlen <= 0) { + if(retlen <= 0) + { rb_free(ctl_buf->buf); rb_free(ctl_buf); } else rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq); - } while(retlen > 0); - + } + while(retlen > 0); + if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) { ssl_dead(ctl); return; - } + } ssl_process_cmd_recv(ctl); rb_setselect(ctl->F, RB_SELECT_READ, ssl_read_ctl, ctl); } @@ -472,24 +500,25 @@ which_ssld(void) { ssl_ctl_t *ctl, *lowest = NULL; rb_dlink_node *ptr; - + RB_DLINK_FOREACH(ptr, ssl_daemons.head) { ctl = ptr->data; if(ctl->dead) continue; - if(lowest == NULL) { + if(lowest == NULL) + { lowest = ctl; continue; } if(ctl->cli_count < lowest->cli_count) lowest = ctl; } - return(lowest); + return (lowest); } static void -ssl_write_ctl(rb_fde_t *F, void *data) +ssl_write_ctl(rb_fde_t * F, void *data) { ssl_ctl_t *ctl = data; ssl_ctl_buf_t *ctl_buf; @@ -503,7 +532,7 @@ ssl_write_ctl(rb_fde_t *F, void *data) { ctl_buf = ptr->data; /* in theory unix sock_dgram shouldn't ever short write this.. */ - retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf, ctl_buf->buflen); + retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf, ctl_buf->buflen, ctl->pid); if(retlen > 0) { rb_dlinkDelete(ptr, &ctl->writeq); @@ -511,36 +540,38 @@ ssl_write_ctl(rb_fde_t *F, void *data) rb_close(ctl_buf->F[x]); rb_free(ctl_buf->buf); rb_free(ctl_buf); - + } if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) { ssl_dead(ctl); return; - } else { + } + else + { rb_setselect(ctl->F, RB_SELECT_WRITE, ssl_write_ctl, ctl); } } } static void -ssl_cmd_write_queue(ssl_ctl_t *ctl, rb_fde_t **F, int count, const void *buf, size_t buflen) +ssl_cmd_write_queue(ssl_ctl_t * ctl, rb_fde_t ** F, int count, const void *buf, size_t buflen) { ssl_ctl_buf_t *ctl_buf; - int x; + int x; /* don't bother */ if(ctl->dead) return; - + ctl_buf = rb_malloc(sizeof(ssl_ctl_buf_t)); ctl_buf->buf = rb_malloc(buflen); memcpy(ctl_buf->buf, buf, buflen); ctl_buf->buflen = buflen; - + for(x = 0; x < count && x < MAXPASSFD; x++) { - ctl_buf->F[x] = F[x]; + ctl_buf->F[x] = F[x]; } ctl_buf->nfds = count; rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->writeq); @@ -549,26 +580,28 @@ ssl_cmd_write_queue(ssl_ctl_t *ctl, rb_fde_t **F, int count, const void *buf, si static void -send_new_ssl_certs_one(ssl_ctl_t *ctl, const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params) +send_new_ssl_certs_one(ssl_ctl_t * ctl, const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params) { size_t len; - len = strlen(ssl_cert) + strlen(ssl_private_key) + strlen(ssl_dh_params) + 5; + len = strlen(ssl_cert) + strlen(ssl_private_key) + strlen(ssl_dh_params) + 5; if(len > sizeof(tmpbuf)) { - sendto_realops_snomask(SNO_GENERAL, L_ALL, - "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...", - len, sizeof(tmpbuf)); - ilog(L_MAIN, "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...", - len, sizeof(tmpbuf)); + sendto_realops_snomask(SNO_GENERAL, L_ALL, + "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...", + len, sizeof(tmpbuf)); + ilog(L_MAIN, + "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...", + len, sizeof(tmpbuf)); return; } - len = rb_snprintf(tmpbuf, sizeof(tmpbuf), "K%c%s%c%s%c%s%c", nul, ssl_cert, nul, ssl_private_key, nul, ssl_dh_params, nul); + len = rb_snprintf(tmpbuf, sizeof(tmpbuf), "K%c%s%c%s%c%s%c", nul, ssl_cert, nul, + ssl_private_key, nul, ssl_dh_params, nul); ssl_cmd_write_queue(ctl, NULL, 0, tmpbuf, len); } static void -send_init_prng(ssl_ctl_t *ctl, prng_seed_t seedtype, const char *path) +send_init_prng(ssl_ctl_t * ctl, prng_seed_t seedtype, const char *path) { size_t len; const char *s; @@ -582,14 +615,15 @@ send_init_prng(ssl_ctl_t *ctl, prng_seed_t seedtype, const char *path) len = strlen(s) + 3; if(len > sizeof(tmpbuf)) { - sendto_realops_snomask(SNO_GENERAL, L_ALL, - "Parameters for send_init_prng too long (%zd > %zd) to pass to ssld, not sending...", - len, sizeof(tmpbuf)); - ilog(L_MAIN, "Parameters for send_init_prng too long (%zd > %zd) to pass to ssld, not sending...", - len, sizeof(tmpbuf)); + sendto_realops_snomask(SNO_GENERAL, L_ALL, + "Parameters for send_init_prng too long (%zd > %zd) to pass to ssld, not sending...", + len, sizeof(tmpbuf)); + ilog(L_MAIN, + "Parameters for send_init_prng too long (%zd > %zd) to pass to ssld, not sending...", + len, sizeof(tmpbuf)); return; - - } + + } len = rb_snprintf(tmpbuf, sizeof(tmpbuf), "I%c%s%c", seed, s, nul); ssl_cmd_write_queue(ctl, NULL, 0, tmpbuf, len); } @@ -606,13 +640,13 @@ send_new_ssl_certs(const char *ssl_cert, const char *ssl_private_key, const char RB_DLINK_FOREACH(ptr, ssl_daemons.head) { ssl_ctl_t *ctl = ptr->data; - send_new_ssl_certs_one(ctl, ssl_cert, ssl_private_key, ssl_dh_params); + send_new_ssl_certs_one(ctl, ssl_cert, ssl_private_key, ssl_dh_params); } } -ssl_ctl_t * -start_ssld_accept(rb_fde_t *sslF, rb_fde_t *plainF, int32_t id) +ssl_ctl_t * +start_ssld_accept(rb_fde_t * sslF, rb_fde_t * plainF, int32_t id) { rb_fde_t *F[2]; ssl_ctl_t *ctl; @@ -629,7 +663,7 @@ start_ssld_accept(rb_fde_t *sslF, rb_fde_t *plainF, int32_t id) } ssl_ctl_t * -start_ssld_connect(rb_fde_t *sslF, rb_fde_t *plainF, int32_t id) +start_ssld_connect(rb_fde_t * sslF, rb_fde_t * plainF, int32_t id) { rb_fde_t *F[2]; ssl_ctl_t *ctl; @@ -643,11 +677,11 @@ start_ssld_connect(rb_fde_t *sslF, rb_fde_t *plainF, int32_t id) ctl = which_ssld(); ctl->cli_count++; ssl_cmd_write_queue(ctl, F, 2, buf, sizeof(buf)); - return ctl; + return ctl; } -void -ssld_decrement_clicount(ssl_ctl_t *ctl) +void +ssld_decrement_clicount(ssl_ctl_t * ctl) { if(ctl == NULL) return; @@ -671,7 +705,7 @@ ssld_decrement_clicount(ssl_ctl_t *ctl) void start_zlib_session(void *data) { - struct Client *server = (struct Client *)data; + struct Client *server = (struct Client *) data; uint16_t recvqlen; uint8_t level; void *xbuf; @@ -688,25 +722,26 @@ start_zlib_session(void *data) server->localClient->event = NULL; recvqlen = rb_linebuf_len(&server->localClient->buf_recvq); - + len = recvqlen + hdr; if(len > READBUF_SIZE) { - rb_free(buf); - sendto_realops_snomask(SNO_GENERAL, L_ALL, "ssld - attempted to pass message of %zd len, max len %d, giving up", len, READBUF_SIZE); + sendto_realops_snomask(SNO_GENERAL, L_ALL, + "ssld - attempted to pass message of %zd len, max len %d, giving up", + len, READBUF_SIZE); ilog(L_MAIN, "ssld - attempted to pass message of %zd len, max len %d, giving up", len, READBUF_SIZE); exit_client(server, server, server, "ssld readbuf exceeded"); return; } - buf = rb_malloc(len); + buf = rb_malloc(len); level = ConfigFileEntry.compression_level; int32_to_buf(&buf[1], rb_get_fd(server->localClient->F)); - buf[5] = (char)level; + buf[5] = (char) level; - recvq_start = &buf[6]; + recvq_start = &buf[6]; server->localClient->zipstats = rb_malloc(sizeof(struct ZipStats)); xbuf = recvq_start; @@ -716,16 +751,17 @@ start_zlib_session(void *data) { cpylen = rb_linebuf_get(&server->localClient->buf_recvq, xbuf, left, LINEBUF_PARTIAL, LINEBUF_RAW); left -= cpylen; - xbuf += cpylen; - } while(cpylen > 0); + xbuf = (void *) (((uintptr_t) xbuf) + cpylen); + } + while(cpylen > 0); /* Pass the socket to ssld. */ *buf = 'Z'; rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF1, &xF2, "Initial zlib socketpairs"); - F[0] = server->localClient->F; + F[0] = server->localClient->F; F[1] = xF1; - del_from_cli_fd_hash(server); + del_from_cli_fd_hash(server); server->localClient->F = xF2; /* need to redo as what we did before isn't valid now */ int32_to_buf(&buf[1], rb_get_fd(server->localClient->F)); @@ -758,9 +794,9 @@ collect_zipstats(void *unused) id = rb_get_fd(target_p->localClient->F); int32_to_buf(&buf[1], rb_get_fd(target_p->localClient->F)); - rb_strlcpy(odata, target_p->name, (sizeof(buf)-len)); - len += strlen(odata) + 1; /* Get the \0 as well */ - ssl_cmd_write_queue(target_p->localClient->ssl_ctl, NULL, 0, buf, len); + rb_strlcpy(odata, target_p->name, (sizeof(buf) - len)); + len += strlen(odata) + 1; /* Get the \0 as well */ + ssl_cmd_write_queue(target_p->localClient->ssl_ctl, NULL, 0, buf, len); } } } @@ -775,7 +811,7 @@ cleanup_dead_ssl(void *unused) ctl = ptr->data; if(ctl->dead && !ctl->cli_count) { - free_ssl_daemon(ctl); + free_ssl_daemon(ctl); } } } @@ -786,9 +822,9 @@ get_ssld_count(void) return ssld_count; } -void init_ssld(void) +void +init_ssld(void) { rb_event_addish("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME); rb_event_addish("cleanup_dead_ssld", cleanup_dead_ssl, NULL, 1200); } - diff --git a/ssld/ssld.c b/ssld/ssld.c index 83ba765..d7d8970 100644 --- a/ssld/ssld.c +++ b/ssld/ssld.c @@ -18,7 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA * - * $Id: ssld.c 25677 2008-07-06 04:21:42Z androsyn $ + * $Id$ */ @@ -34,33 +34,38 @@ #endif static void setup_signals(void); +static pid_t ppid; -static inline int32_t buf_to_int32(char *buf) +static inline int32_t +buf_to_int32(char *buf) { int32_t x; memcpy(&x, buf, sizeof(x)); return x; } -static inline void int32_to_buf(char *buf, int32_t x) +static inline void +int32_to_buf(char *buf, int32_t x) { memcpy(buf, &x, sizeof(x)); return; } -static inline uint16_t buf_to_uint16(char *buf) +static inline uint16_t +buf_to_uint16(char *buf) { uint16_t x; memcpy(&x, buf, sizeof(x)); return x; } -static inline void uint16_to_buf(char *buf, uint16_t x) +static inline void +uint16_to_buf(char *buf, uint16_t x) { memcpy(buf, &x, sizeof(x)); return; } - + static char inbuf[READBUF_SIZE]; #ifdef HAVE_LIBZ @@ -120,8 +125,8 @@ typedef struct _conn #define FLAG_ZIP 0x02 #define FLAG_CORK 0x04 #define FLAG_DEAD 0x08 -#define FLAG_SSL_W_WANTS_R 0x10 /* output needs to wait until input possible */ -#define FLAG_SSL_R_WANTS_W 0x20 /* input needs to wait until output possible */ +#define FLAG_SSL_W_WANTS_R 0x10 /* output needs to wait until input possible */ +#define FLAG_SSL_R_WANTS_W 0x20 /* input needs to wait until output possible */ #define IsSSL(x) ((x)->flags & FLAG_SSL) #define IsZip(x) ((x)->flags & FLAG_ZIP) @@ -157,12 +162,12 @@ typedef struct _conn static rb_dlink_list connid_hash_table[CONN_HASH_SIZE]; static rb_dlink_list dead_list; -static void conn_mod_read_cb(rb_fde_t * fd, void *data); +static void conn_mod_read_cb(rb_fde_t *fd, void *data); static void conn_mod_write_sendq(rb_fde_t *, void *data); static void conn_plain_write_sendq(rb_fde_t *, void *data); static void mod_write_ctl(rb_fde_t *, void *data); -static void conn_plain_read_cb(rb_fde_t * fd, void *data); -static void mod_cmd_write_queue(mod_ctl_t *ctl, const void *data, size_t len); +static void conn_plain_read_cb(rb_fde_t *fd, void *data); +static void mod_cmd_write_queue(mod_ctl_t * ctl, const void *data, size_t len); static const char *remote_closed = "Remote host closed the connection"; static int ssl_ok; #ifdef HAVE_LIBZ @@ -182,7 +187,7 @@ ssld_alloc(void *unused, size_t count, size_t size) static void ssld_free(void *unused, void *ptr) { - rb_free(ptr); + rb_free(ptr); } #endif @@ -218,7 +223,7 @@ free_conn(conn_t * conn) { zlib_stream_t *stream = conn->stream; inflateEnd(&stream->instream); - deflateEnd(&stream->outstream); + deflateEnd(&stream->outstream); } #endif rb_free(conn); @@ -242,12 +247,12 @@ static void close_conn(conn_t * conn, int wait_plain, const char *fmt, ...) { va_list ap; - char reason[128]; /* must always be under 250 bytes */ + char reason[128]; /* must always be under 250 bytes */ char buf[256]; int len; if(IsDead(conn)) return; - + rb_rawbuf_flush(conn->modbuf_out, conn->mod_fd); rb_rawbuf_flush(conn->plainbuf_out, conn->plain_fd); rb_close(conn->mod_fd); @@ -256,13 +261,13 @@ close_conn(conn_t * conn, int wait_plain, const char *fmt, ...) if(!wait_plain || fmt == NULL) { rb_close(conn->plain_fd); - + if(conn->id >= 0) rb_dlinkDelete(&conn->node, connid_hash(conn->id)); rb_dlinkAdd(conn, &conn->node, &dead_list); return; } - rb_setselect(conn->plain_fd, RB_SELECT_WRITE|RB_SELECT_READ, NULL, NULL); + rb_setselect(conn->plain_fd, RB_SELECT_WRITE | RB_SELECT_READ, NULL, NULL); va_start(ap, fmt); rb_vsnprintf(reason, sizeof(reason), fmt, ap); va_end(ap); @@ -275,7 +280,7 @@ close_conn(conn_t * conn, int wait_plain, const char *fmt, ...) } static conn_t * -make_conn(mod_ctl_t *ctl, rb_fde_t * mod_fd, rb_fde_t * plain_fd) +make_conn(mod_ctl_t * ctl, rb_fde_t *mod_fd, rb_fde_t *plain_fd) { conn_t *conn = rb_malloc(sizeof(conn_t)); conn->ctl = ctl; @@ -302,20 +307,18 @@ check_handshake_flood(void *unused) conn = ptr->data; if(!IsSSL(conn)) continue; - + count = rb_ssl_handshake_count(conn->mod_fd); /* nothing needs to do this more than twice in ten seconds i don't think */ if(count > 2) close_conn(conn, WAIT_PLAIN, "Handshake flooding"); else rb_ssl_clear_handshake_count(conn->mod_fd); - } - HASH_WALK_END - -} + } +HASH_WALK_END} static void -conn_mod_write_sendq(rb_fde_t * fd, void *data) +conn_mod_write_sendq(rb_fde_t *fd, void *data) { conn_t *conn = data; const char *err; @@ -331,7 +334,7 @@ conn_mod_write_sendq(rb_fde_t * fd, void *data) return; } - while ((retlen = rb_rawbuf_flush(conn->modbuf_out, fd)) > 0) + while((retlen = rb_rawbuf_flush(conn->modbuf_out, fd)) > 0) conn->mod_out += retlen; if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) @@ -401,7 +404,7 @@ static void common_zlib_deflate(conn_t * conn, void *buf, size_t len) { int ret, have; - z_stream *outstream = &((zlib_stream_t *)conn->stream)->outstream; + z_stream *outstream = &((zlib_stream_t *) conn->stream)->outstream; outstream->next_in = buf; outstream->avail_in = len; outstream->next_out = (Bytef *) outbuf; @@ -422,7 +425,7 @@ common_zlib_deflate(conn_t * conn, void *buf, size_t len) } if(outstream->avail_in != 0) { - /* avail_in isn't empty...*/ + /* avail_in isn't empty... */ close_conn(conn, WAIT_PLAIN, "error compressing data, avail_in != 0"); return; } @@ -433,15 +436,15 @@ common_zlib_deflate(conn_t * conn, void *buf, size_t len) static void common_zlib_inflate(conn_t * conn, void *buf, size_t len) { - int ret, have; - ((zlib_stream_t *)conn->stream)->instream.next_in = buf; - ((zlib_stream_t *)conn->stream)->instream.avail_in = len; - ((zlib_stream_t *)conn->stream)->instream.next_out = (Bytef *) outbuf; - ((zlib_stream_t *)conn->stream)->instream.avail_out = sizeof(outbuf); + int ret, have = 0; + ((zlib_stream_t *) conn->stream)->instream.next_in = buf; + ((zlib_stream_t *) conn->stream)->instream.avail_in = len; + ((zlib_stream_t *) conn->stream)->instream.next_out = (Bytef *) outbuf; + ((zlib_stream_t *) conn->stream)->instream.avail_out = sizeof(outbuf); - while (((zlib_stream_t *)conn->stream)->instream.avail_in) + while(((zlib_stream_t *) conn->stream)->instream.avail_in) { - ret = inflate(&((zlib_stream_t *)conn->stream)->instream, Z_NO_FLUSH); + ret = inflate(&((zlib_stream_t *) conn->stream)->instream, Z_NO_FLUSH); if(ret != Z_OK) { if(!strncmp("ERROR ", buf, 6)) @@ -452,14 +455,14 @@ common_zlib_inflate(conn_t * conn, void *buf, size_t len) close_conn(conn, WAIT_PLAIN, "Inflate failed: %s", zError(ret)); return; } - have = sizeof(outbuf) - ((zlib_stream_t *)conn->stream)->instream.avail_out; + have = sizeof(outbuf) - ((zlib_stream_t *) conn->stream)->instream.avail_out; - if(((zlib_stream_t *)conn->stream)->instream.avail_in) + if(((zlib_stream_t *) conn->stream)->instream.avail_in) { conn_plain_write(conn, outbuf, have); have = 0; - ((zlib_stream_t *)conn->stream)->instream.next_out = (Bytef *) outbuf; - ((zlib_stream_t *)conn->stream)->instream.avail_out = sizeof(outbuf); + ((zlib_stream_t *) conn->stream)->instream.next_out = (Bytef *) outbuf; + ((zlib_stream_t *) conn->stream)->instream.avail_out = sizeof(outbuf); } } if(have == 0) @@ -487,7 +490,7 @@ plain_check_cork(conn_t * conn) static void -conn_plain_read_cb(rb_fde_t * fd, void *data) +conn_plain_read_cb(rb_fde_t *fd, void *data) { conn_t *conn = data; int length = 0; @@ -500,7 +503,7 @@ conn_plain_read_cb(rb_fde_t * fd, void *data) if(plain_check_cork(conn)) return; - while (1) + while(1) { if(IsDead(conn)) return; @@ -535,7 +538,7 @@ conn_plain_read_cb(rb_fde_t * fd, void *data) } static void -conn_mod_read_cb(rb_fde_t * fd, void *data) +conn_mod_read_cb(rb_fde_t *fd, void *data) { conn_t *conn = data; const char *err = remote_closed; @@ -553,7 +556,7 @@ conn_mod_read_cb(rb_fde_t * fd, void *data) return; } - while (1) + while(1) { if(IsDead(conn)) return; @@ -562,7 +565,8 @@ conn_mod_read_cb(rb_fde_t * fd, void *data) if(length == 0 || (length < 0 && !rb_ignore_errno(errno))) { - if(length == 0) { + if(length == 0) + { close_conn(conn, WAIT_PLAIN, "%s", remote_closed); return; } @@ -586,7 +590,7 @@ conn_mod_read_cb(rb_fde_t * fd, void *data) } conn_plain_write_sendq(conn->plain_fd, conn); return; - } + } conn->mod_in += length; #ifdef HAVE_LIBZ if(IsZip(conn)) @@ -598,7 +602,7 @@ conn_mod_read_cb(rb_fde_t * fd, void *data) } static void -conn_plain_write_sendq(rb_fde_t * fd, void *data) +conn_plain_write_sendq(rb_fde_t *fd, void *data) { conn_t *conn = data; int retlen; @@ -606,7 +610,7 @@ conn_plain_write_sendq(rb_fde_t * fd, void *data) if(IsDead(conn)) return; - while ((retlen = rb_rawbuf_flush(conn->plainbuf_out, fd)) > 0) + while((retlen = rb_rawbuf_flush(conn->plainbuf_out, fd)) > 0) { conn->plain_out += retlen; } @@ -615,7 +619,7 @@ conn_plain_write_sendq(rb_fde_t * fd, void *data) close_conn(data, NO_WAIT, NULL); return; } - + if(rb_rawbuf_length(conn->plainbuf_out) > 0) rb_setselect(conn->plain_fd, RB_SELECT_WRITE, conn_plain_write_sendq, conn); @@ -638,7 +642,7 @@ maxconn(void) } static void -ssl_process_accept_cb(rb_fde_t * F, int status, struct sockaddr *addr, rb_socklen_t len, void *data) +ssl_process_accept_cb(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t len, void *data) { conn_t *conn = data; if(status == RB_OK) @@ -653,7 +657,7 @@ ssl_process_accept_cb(rb_fde_t * F, int status, struct sockaddr *addr, rb_sockle } static void -ssl_process_connect_cb(rb_fde_t * F, int status, void *data) +ssl_process_connect_cb(rb_fde_t *F, int status, void *data) { conn_t *conn = data; if(status == RB_OK) @@ -686,7 +690,7 @@ ssl_process_accept(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb) if(rb_get_type(conn->mod_fd) & RB_FD_UNKNOWN) { - + rb_set_type(conn->mod_fd, RB_FD_SOCKET); } if(rb_get_type(conn->mod_fd) == RB_FD_UNKNOWN) @@ -730,7 +734,7 @@ process_stats(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb) if(id < 0) return; - + odata = &ctlb->buf[5]; conn = conn_find_by_id(id); @@ -748,10 +752,10 @@ process_stats(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb) #ifdef HAVE_LIBZ static void -zlib_send_zip_ready(mod_ctl_t *ctl, conn_t *conn) +zlib_send_zip_ready(mod_ctl_t * ctl, conn_t * conn) { char buf[5]; - + buf[0] = 'R'; int32_to_buf(&buf[1], conn->id); mod_cmd_write_queue(conn->ctl, buf, sizeof(buf)); @@ -778,22 +782,22 @@ zlib_process(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb) id = buf_to_int32(&ctlb->buf[1]); conn_add_id_hash(conn, id); - level = (uint8_t) ctlb->buf[5]; + level = (uint8_t)ctlb->buf[5]; recvqlen = ctlb->buflen - hdr; recvq_start = &ctlb->buf[6]; SetZip(conn); conn->stream = rb_malloc(sizeof(zlib_stream_t)); - instream = &((zlib_stream_t *)conn->stream)->instream; - outstream = &((zlib_stream_t *)conn->stream)->outstream; - + instream = &((zlib_stream_t *) conn->stream)->instream; + outstream = &((zlib_stream_t *) conn->stream)->outstream; + instream->total_in = 0; instream->total_out = 0; instream->zalloc = (alloc_func) ssld_alloc; instream->zfree = (free_func) ssld_free; instream->data_type = Z_ASCII; - inflateInit(&((zlib_stream_t *)conn->stream)->instream); + inflateInit(&((zlib_stream_t *) conn->stream)->instream); outstream->total_in = 0; outstream->total_out = 0; @@ -804,7 +808,7 @@ zlib_process(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb) if(level > 9) level = Z_DEFAULT_COMPRESSION; - deflateInit(&((zlib_stream_t *)conn->stream)->outstream, level); + deflateInit(&((zlib_stream_t *) conn->stream)->outstream, level); if(recvqlen > 0) common_zlib_inflate(conn, recvq_start, recvqlen); zlib_send_zip_ready(ctl, conn); @@ -820,8 +824,8 @@ init_prng(mod_ctl_t * ctl, mod_ctl_buf_t * ctl_buf) { char *path; prng_seed_t seed_type; - - seed_type = (prng_seed_t)ctl_buf->buf[1]; + + seed_type = (prng_seed_t) ctl_buf->buf[1]; path = &ctl_buf->buf[2]; rb_init_prng(path, seed_type); } @@ -847,37 +851,37 @@ ssl_new_keys(mod_ctl_t * ctl, mod_ctl_buf_t * ctl_buf) const char *invalid = "I"; mod_cmd_write_queue(ctl, invalid, strlen(invalid)); return; - } + } } static void -send_nossl_support(mod_ctl_t *ctl, mod_ctl_buf_t *ctlb) +send_nossl_support(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb) { static const char *nossl_cmd = "N"; conn_t *conn; int32_t id; if(ctlb != NULL) - { + { conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]); id = buf_to_int32(&ctlb->buf[1]); if(id >= 0) conn_add_id_hash(conn, id); close_conn(conn, WAIT_PLAIN, "libratbox reports no SSL/TLS support"); - } - mod_cmd_write_queue(ctl, nossl_cmd, strlen(nossl_cmd)); + } + mod_cmd_write_queue(ctl, nossl_cmd, strlen(nossl_cmd)); } static void -send_i_am_useless(mod_ctl_t *ctl) +send_i_am_useless(mod_ctl_t * ctl) { static const char *useless = "U"; mod_cmd_write_queue(ctl, useless, strlen(useless)); } static void -send_nozlib_support(mod_ctl_t *ctl, mod_ctl_buf_t *ctlb) +send_nozlib_support(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb) { static const char *nozlib_cmd = "z"; conn_t *conn; @@ -890,7 +894,7 @@ send_nozlib_support(mod_ctl_t *ctl, mod_ctl_buf_t *ctlb) if(id >= 0) conn_add_id_hash(conn, id); close_conn(conn, WAIT_PLAIN, "libratbox reports no zlib support"); - } + } mod_cmd_write_queue(ctl, nozlib_cmd, strlen(nozlib_cmd)); } @@ -938,8 +942,8 @@ mod_process_cmd_recv(mod_ctl_t * ctl) break; } case 'I': - init_prng(ctl, ctl_buf); - break; + init_prng(ctl, ctl_buf); + break; case 'S': { process_stats(ctl, ctl_buf); @@ -957,7 +961,7 @@ mod_process_cmd_recv(mod_ctl_t * ctl) case 'Z': send_nozlib_support(ctl, ctl_buf); break; - + #endif default: break; @@ -973,7 +977,7 @@ mod_process_cmd_recv(mod_ctl_t * ctl) static void -mod_read_ctl(rb_fde_t * F, void *data) +mod_read_ctl(rb_fde_t *F, void *data) { mod_ctl_buf_t *ctl_buf; mod_ctl_t *ctl = data; @@ -997,7 +1001,7 @@ mod_read_ctl(rb_fde_t * F, void *data) rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq); } } - while (retlen > 0); + while(retlen > 0); if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) exit(0); @@ -1007,7 +1011,7 @@ mod_read_ctl(rb_fde_t * F, void *data) } static void -mod_write_ctl(rb_fde_t * F, void *data) +mod_write_ctl(rb_fde_t *F, void *data) { mod_ctl_t *ctl = data; mod_ctl_buf_t *ctl_buf; @@ -1018,29 +1022,29 @@ mod_write_ctl(rb_fde_t * F, void *data) { ctl_buf = ptr->data; retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf, - ctl_buf->buflen); + ctl_buf->buflen, ppid); if(retlen > 0) { rb_dlinkDelete(ptr, &ctl->writeq); - for (x = 0; x < ctl_buf->nfds; x++) + for(x = 0; x < ctl_buf->nfds; x++) rb_close(ctl_buf->F[x]); rb_free(ctl_buf->buf); rb_free(ctl_buf); } if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) - exit(0); - + exit(0); + rb_setselect(ctl->F, RB_SELECT_WRITE, mod_write_ctl, ctl); } } static void -read_pipe_ctl(rb_fde_t * F, void *data) +read_pipe_ctl(rb_fde_t *F, void *data) { int retlen; - while ((retlen = rb_read(F, inbuf, sizeof(inbuf))) > 0) + while((retlen = rb_read(F, inbuf, sizeof(inbuf))) > 0) { ;; /* we don't do anything with the pipe really, just care if the other process dies.. */ } @@ -1053,29 +1057,34 @@ read_pipe_ctl(rb_fde_t * F, void *data) int main(int argc, char **argv) { - const char *s_ctlfd, *s_pipe; + const char *s_ctlfd, *s_pipe, *s_pid; int ctlfd, pipefd, x, maxfd; maxfd = maxconn(); + s_ctlfd = getenv("CTL_FD"); s_pipe = getenv("CTL_PIPE"); + s_pid = getenv("CTL_PPID"); - if(s_ctlfd == NULL || s_pipe == NULL) + if(s_ctlfd == NULL || s_pipe == NULL || s_pid == NULL) { - fprintf(stderr, "This is ircd-ratbox ssld. You know you aren't supposed to run me directly?\n"); - fprintf(stderr, "You get an Id tag for this: $Id: ssld.c 25677 2008-07-06 04:21:42Z androsyn $\n"); + fprintf(stderr, + "This is ircd-ratbox ssld. You know you aren't supposed to run me directly?\n"); + fprintf(stderr, + "You get an Id tag for this: $Id$\n"); fprintf(stderr, "Have a nice life\n"); exit(1); } ctlfd = atoi(s_ctlfd); pipefd = atoi(s_pipe); - - for (x = 0; x < maxfd; x++) + ppid = atoi(s_pid); + x = 0; +#ifndef _WIN32 + for(x = 0; x < maxfd; x++) { if(x != ctlfd && x != pipefd && x > 2) close(x); } - x = open("/dev/null", O_RDWR); if(x >= 0) { @@ -1088,12 +1097,11 @@ main(int argc, char **argv) if(x > 2) close(x); } - +#endif setup_signals(); rb_lib_init(NULL, NULL, NULL, 0, maxfd, 1024, 4096); rb_init_rawbuffers(1024); - ssl_ok = rb_supports_ssl(); - + ssl_ok = rb_supports_ssl(); mod_ctl = rb_malloc(sizeof(mod_ctl_t)); mod_ctl->F = rb_open(ctlfd, RB_FD_SOCKET, "ircd control socket"); mod_ctl->F_pipe = rb_open(pipefd, RB_FD_PIPE, "ircd pipe"); @@ -1108,7 +1116,7 @@ main(int argc, char **argv) /* this is really useless... */ send_i_am_useless(mod_ctl); /* sleep until the ircd kills us */ - rb_sleep(2<<30, 0); + rb_sleep(2 << 30, 0); exit(1); } @@ -1121,16 +1129,18 @@ main(int argc, char **argv) } - +#ifndef _WIN32 static void dummy_handler(int sig) { return; } +#endif static void setup_signals() { +#ifndef _WIN32 struct sigaction act; act.sa_flags = 0; @@ -1153,5 +1163,5 @@ setup_signals() act.sa_handler = dummy_handler; sigaction(SIGALRM, &act, 0); +#endif } -