Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e9915df87c | ||
|
|
408829e330 | ||
|
|
1a1b5bdb38 | ||
|
|
abedec692a | ||
|
|
7738baf38e | ||
|
|
3c4c52d0f9 | ||
|
|
76588fb1bd | ||
|
|
f9e95ec582 | ||
|
|
7985d4fed0 | ||
|
|
77343d3c21 | ||
|
|
f8d5b72a89 | ||
|
|
cf2994b570 | ||
|
|
7a9d40d8dd | ||
|
|
b1f84b5b4a | ||
|
|
998c6c36ce | ||
|
|
4112b8111a | ||
|
|
1929f2fae7 | ||
|
|
77e41371b5 | ||
|
|
0266c8bb9c | ||
|
|
5e3eabb677 | ||
|
|
669ab14022 | ||
|
|
1d491055c5 | ||
|
|
08351ea8e7 | ||
|
|
e2ac690164 | ||
|
|
3bb563b432 | ||
|
|
e10582fec2 | ||
|
|
84e278658b | ||
|
|
c671851be8 | ||
|
|
4f8edfe9c8 | ||
|
|
4d0ed61a05 |
12
ChangeLog
12
ChangeLog
@@ -1,3 +1,15 @@
|
||||
0.2.7:
|
||||
- Fixed a nasty crash (bug #11), Thanks Toast for finding it.
|
||||
- Fix bug #9 - net_get_peer_address() failure on CentOS/Xen configurations.
|
||||
- Write a log message if an operator reloads the config file.
|
||||
- Don't print OK or ERROR when using '-s' or '-S' show the configuration.
|
||||
- Cleanup credential string handling
|
||||
- Made sure "!help" only display accessible commands.
|
||||
- Cleanup in-hub command parsing
|
||||
- Fix a possible crash if multiple INF messages are sent during login.
|
||||
- Rewrote the mainloop to not use a timer.
|
||||
|
||||
|
||||
0.2.6:
|
||||
- Better "!uptime" command formatting.
|
||||
- Better "!stats"; can display peak and current bandwidth usage.
|
||||
|
||||
@@ -30,8 +30,9 @@ CFLAGS += -mno-cygwin
|
||||
LDFLAGS += -mno-cygwin
|
||||
BIN_EXT ?= .exe
|
||||
else
|
||||
UHUB_CONF_DIR ?= /etc/uhub
|
||||
UHUB_PREFIX ?= /usr/local
|
||||
DESTDIR ?= /
|
||||
UHUB_CONF_DIR ?= $(DESTDIR)/etc/uhub
|
||||
UHUB_PREFIX ?= $(DESTDIR)/usr/local
|
||||
CFLAGS += -I/usr/local/include
|
||||
LDFLAGS += -L/usr/local/lib
|
||||
BIN_EXT ?=
|
||||
@@ -251,7 +252,7 @@ install: $(uhub_BINARY)
|
||||
@if [ ! -d $(UHUB_CONF_DIR) ]; then echo Creating $(UHUB_CONF_DIR); mkdir -p $(UHUB_CONF_DIR); fi
|
||||
@if [ ! -f $(UHUB_CONF_DIR)/uhub.conf ]; then cp doc/uhub.conf $(UHUB_CONF_DIR); fi
|
||||
@if [ ! -f $(UHUB_CONF_DIR)/users.conf ]; then cp doc/users.conf $(UHUB_CONF_DIR); fi
|
||||
@touch $(UHUB_CONF_DIR)/motd
|
||||
@touch $(UHUB_CONF_DIR)/motd.txt
|
||||
@echo done.
|
||||
endif
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
static struct event_queue* eq;
|
||||
static int eq_val;
|
||||
struct event* libevent_handle;
|
||||
|
||||
static void eq_callback(void* callback_data, struct event_data* event_data)
|
||||
{
|
||||
@@ -17,8 +16,6 @@ EXO_TEST(eventqueue_init_1, {
|
||||
|
||||
EXO_TEST(eventqueue_init_2, {
|
||||
/* hack */
|
||||
libevent_handle = eq->event;
|
||||
eq->event = 0;
|
||||
return eq->callback_data == &eq_val && eq->callback == eq_callback && eq->q1 && eq->q2 && !eq->locked;
|
||||
});
|
||||
|
||||
@@ -81,7 +78,6 @@ EXO_TEST(eventqueue_size_4, {
|
||||
});
|
||||
|
||||
EXO_TEST(eventqueue_shutdown_1, {
|
||||
eq->event = libevent_handle;
|
||||
event_queue_shutdown(eq);
|
||||
return 1;
|
||||
});
|
||||
|
||||
5
debian/changelog
vendored
Normal file
5
debian/changelog
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
uhub (0.2.6-1) unstable; urgency=low
|
||||
|
||||
* Initial release (Closes: #nnnn) <nnnn is the bug number of your ITP>
|
||||
|
||||
-- Jan Vidar Krey <janvidar@extatic.org> Tue, 17 Mar 2009 00:38:12 +0100
|
||||
1
debian/compat
vendored
Normal file
1
debian/compat
vendored
Normal file
@@ -0,0 +1 @@
|
||||
7
|
||||
3
debian/conffiles
vendored
Normal file
3
debian/conffiles
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
etc/uhub/uhub.conf
|
||||
etc/uhub/users.conf
|
||||
etc/uhub/motd.txt
|
||||
15
debian/control
vendored
Normal file
15
debian/control
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
Source: uhub
|
||||
Section: net
|
||||
Priority: extra
|
||||
Maintainer: Jan Vidar Krey <janvidar@extatic.org>
|
||||
Build-Depends: debhelper (>= 7)
|
||||
Standards-Version: 3.8.1
|
||||
Homepage: http://www.extatic.org/uhub/
|
||||
|
||||
Package: uhub
|
||||
Architecture: any
|
||||
Depends: ${shlibs:Depends}, ${misc:Depends}
|
||||
Description: uHub is a high performance peer-to-peer hub for the ADC network.
|
||||
Its low memory footprint allows it to handle several thousand users on high-end servers,
|
||||
or a small private hub on embedded hardware.
|
||||
uHub uses the ADC protocol, and is compatible with DC++, jUCy and other ADC clients.
|
||||
9
debian/copyright
vendored
Normal file
9
debian/copyright
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
Copyright:
|
||||
|
||||
Copyright (C) 2008-2009 Jan Vidar Krey <janvidar@extatic.org>
|
||||
|
||||
License:
|
||||
|
||||
Licensed under the GPL version 3,
|
||||
see `/usr/share/common-licenses/GPL-3'.
|
||||
|
||||
1
debian/dirs
vendored
Normal file
1
debian/dirs
vendored
Normal file
@@ -0,0 +1 @@
|
||||
usr/bin
|
||||
3
debian/docs
vendored
Normal file
3
debian/docs
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
BUGS
|
||||
README
|
||||
AUTHORS
|
||||
1
debian/files
vendored
Normal file
1
debian/files
vendored
Normal file
@@ -0,0 +1 @@
|
||||
uhub_0.2.6-1_amd64.deb net extra
|
||||
157
debian/init.d
vendored
Normal file
157
debian/init.d
vendored
Normal file
@@ -0,0 +1,157 @@
|
||||
#! /bin/sh
|
||||
#
|
||||
# skeleton example file to build /etc/init.d/ scripts.
|
||||
# This file should be used to construct scripts for /etc/init.d.
|
||||
#
|
||||
# Written by Miquel van Smoorenburg <miquels@cistron.nl>.
|
||||
# Modified for Debian
|
||||
# by Ian Murdock <imurdock@gnu.ai.mit.edu>.
|
||||
# Further changes by Javier Fernandez-Sanguino <jfs@debian.org>
|
||||
#
|
||||
# Version: @(#)skeleton 1.9 26-Feb-2001 miquels@cistron.nl
|
||||
#
|
||||
|
||||
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
|
||||
DAEMON=/usr/bin/uhub
|
||||
NAME=uhub
|
||||
DESC=uhub
|
||||
|
||||
test -x $DAEMON || exit 0
|
||||
|
||||
LOGDIR=/var/log/uhub
|
||||
PIDFILE=/var/run/$NAME.pid
|
||||
DODTIME=1 # Time to wait for the server to die, in seconds
|
||||
# If this value is set too low you might not
|
||||
# let some servers to die gracefully and
|
||||
# 'restart' will not work
|
||||
|
||||
# Include uhub defaults if available
|
||||
if [ -f /etc/default/uhub ] ; then
|
||||
. /etc/default/uhub
|
||||
fi
|
||||
|
||||
set -e
|
||||
|
||||
running_pid()
|
||||
{
|
||||
# Check if a given process pid's cmdline matches a given name
|
||||
pid=$1
|
||||
name=$2
|
||||
[ -z "$pid" ] && return 1
|
||||
[ ! -d /proc/$pid ] && return 1
|
||||
cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
|
||||
# Is this the expected child?
|
||||
[ "$cmd" != "$name" ] && return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
running()
|
||||
{
|
||||
# Check if the process is running looking at /proc
|
||||
# (works for all users)
|
||||
|
||||
# No pidfile, probably no daemon present
|
||||
[ ! -f "$PIDFILE" ] && return 1
|
||||
# Obtain the pid and check it against the binary name
|
||||
pid=`cat $PIDFILE`
|
||||
running_pid $pid $DAEMON || return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
force_stop() {
|
||||
# Forcefully kill the process
|
||||
[ ! -f "$PIDFILE" ] && return
|
||||
if running ; then
|
||||
kill -15 $pid
|
||||
# Is it really dead?
|
||||
[ -n "$DODTIME" ] && sleep "$DODTIME"s
|
||||
if running ; then
|
||||
kill -9 $pid
|
||||
[ -n "$DODTIME" ] && sleep "$DODTIME"s
|
||||
if running ; then
|
||||
echo "Cannot kill $LABEL (pid=$pid)!"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
rm -f $PIDFILE
|
||||
return 0
|
||||
}
|
||||
|
||||
case "$1" in
|
||||
start)
|
||||
echo -n "Starting $DESC: "
|
||||
start-stop-daemon --start --quiet --pidfile $PIDFILE \
|
||||
--exec $DAEMON -- $DAEMON_OPTS
|
||||
if running ; then
|
||||
echo "$NAME."
|
||||
else
|
||||
echo " ERROR."
|
||||
fi
|
||||
;;
|
||||
stop)
|
||||
echo -n "Stopping $DESC: "
|
||||
start-stop-daemon --stop --quiet --pidfile $PIDFILE \
|
||||
--exec $DAEMON
|
||||
echo "$NAME."
|
||||
;;
|
||||
force-stop)
|
||||
echo -n "Forcefully stopping $DESC: "
|
||||
force_stop
|
||||
if ! running ; then
|
||||
echo "$NAME."
|
||||
else
|
||||
echo " ERROR."
|
||||
fi
|
||||
;;
|
||||
reload)
|
||||
#
|
||||
# If the daemon can reload its config files on the fly
|
||||
# for example by sending it SIGHUP, do it here.
|
||||
#
|
||||
# If the daemon responds to changes in its config file
|
||||
# directly anyway, make this a do-nothing entry.
|
||||
#
|
||||
echo "Reloading $DESC configuration files."
|
||||
start-stop-daemon --stop --signal 1 --quiet --pidfile \
|
||||
/var/run/$NAME.pid --exec $DAEMON
|
||||
#;;
|
||||
force-reload)
|
||||
#
|
||||
# If the "reload" option is implemented, move the "force-reload"
|
||||
# option to the "reload" entry above. If not, "force-reload" is
|
||||
# just the same as "restart" except that it does nothing if the
|
||||
# daemon isn't already running.
|
||||
# check wether $DAEMON is running. If so, restart
|
||||
start-stop-daemon --stop --test --quiet --pidfile \
|
||||
/var/run/$NAME.pid --exec $DAEMON \
|
||||
&& $0 restart \
|
||||
|| exit 0
|
||||
;;
|
||||
restart)
|
||||
echo -n "Restarting $DESC: "
|
||||
start-stop-daemon --stop --quiet --pidfile \
|
||||
/var/run/$NAME.pid --exec $DAEMON
|
||||
[ -n "$DODTIME" ] && sleep $DODTIME
|
||||
start-stop-daemon --start --quiet --pidfile \
|
||||
/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
|
||||
echo "$NAME."
|
||||
;;
|
||||
status)
|
||||
echo -n "$LABEL is "
|
||||
if running ; then
|
||||
echo "running"
|
||||
else
|
||||
echo " not running."
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
N=/etc/init.d/$NAME
|
||||
# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
|
||||
echo "Usage: $N {start|stop|restart|force-reload|status|force-stop}" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
exit 0
|
||||
39
debian/postinst
vendored
Normal file
39
debian/postinst
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
#!/bin/sh
|
||||
# postinst script for uhub
|
||||
#
|
||||
# see: dh_installdeb(1)
|
||||
|
||||
set -e
|
||||
|
||||
# summary of how this script can be called:
|
||||
# * <postinst> `configure' <most-recently-configured-version>
|
||||
# * <old-postinst> `abort-upgrade' <new version>
|
||||
# * <conflictor's-postinst> `abort-remove' `in-favour' <package>
|
||||
# <new-version>
|
||||
# * <postinst> `abort-remove'
|
||||
# * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
|
||||
# <failed-install-package> <version> `removing'
|
||||
# <conflicting-package> <version>
|
||||
# for details, see http://www.debian.org/doc/debian-policy/ or
|
||||
# the debian-policy package
|
||||
|
||||
|
||||
case "$1" in
|
||||
configure)
|
||||
;;
|
||||
|
||||
abort-upgrade|abort-remove|abort-deconfigure)
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "postinst called with unknown argument \`$1'" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# dh_installdeb will replace this with shell code automatically
|
||||
# generated by other debhelper scripts.
|
||||
|
||||
#DEBHELPER#
|
||||
|
||||
exit 0
|
||||
37
debian/postrm
vendored
Normal file
37
debian/postrm
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
#!/bin/sh
|
||||
# postrm script for uhub
|
||||
#
|
||||
# see: dh_installdeb(1)
|
||||
|
||||
set -e
|
||||
|
||||
# summary of how this script can be called:
|
||||
# * <postrm> `remove'
|
||||
# * <postrm> `purge'
|
||||
# * <old-postrm> `upgrade' <new-version>
|
||||
# * <new-postrm> `failed-upgrade' <old-version>
|
||||
# * <new-postrm> `abort-install'
|
||||
# * <new-postrm> `abort-install' <old-version>
|
||||
# * <new-postrm> `abort-upgrade' <old-version>
|
||||
# * <disappearer's-postrm> `disappear' <overwriter>
|
||||
# <overwriter-version>
|
||||
# for details, see http://www.debian.org/doc/debian-policy/ or
|
||||
# the debian-policy package
|
||||
|
||||
|
||||
case "$1" in
|
||||
purge|remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "postrm called with unknown argument \`$1'" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# dh_installdeb will replace this with shell code automatically
|
||||
# generated by other debhelper scripts.
|
||||
|
||||
#DEBHELPER#
|
||||
|
||||
exit 0
|
||||
35
debian/preinst
vendored
Normal file
35
debian/preinst
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
#!/bin/sh
|
||||
# preinst script for uhub
|
||||
#
|
||||
# see: dh_installdeb(1)
|
||||
|
||||
set -e
|
||||
|
||||
# summary of how this script can be called:
|
||||
# * <new-preinst> `install'
|
||||
# * <new-preinst> `install' <old-version>
|
||||
# * <new-preinst> `upgrade' <old-version>
|
||||
# * <old-preinst> `abort-upgrade' <new-version>
|
||||
# for details, see http://www.debian.org/doc/debian-policy/ or
|
||||
# the debian-policy package
|
||||
|
||||
|
||||
case "$1" in
|
||||
install|upgrade)
|
||||
;;
|
||||
|
||||
abort-upgrade)
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "preinst called with unknown argument \`$1'" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# dh_installdeb will replace this with shell code automatically
|
||||
# generated by other debhelper scripts.
|
||||
|
||||
#DEBHELPER#
|
||||
|
||||
exit 0
|
||||
38
debian/prerm
vendored
Normal file
38
debian/prerm
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
#!/bin/sh
|
||||
# prerm script for uhub
|
||||
#
|
||||
# see: dh_installdeb(1)
|
||||
|
||||
set -e
|
||||
|
||||
# summary of how this script can be called:
|
||||
# * <prerm> `remove'
|
||||
# * <old-prerm> `upgrade' <new-version>
|
||||
# * <new-prerm> `failed-upgrade' <old-version>
|
||||
# * <conflictor's-prerm> `remove' `in-favour' <package> <new-version>
|
||||
# * <deconfigured's-prerm> `deconfigure' `in-favour'
|
||||
# <package-being-installed> <version> `removing'
|
||||
# <conflicting-package> <version>
|
||||
# for details, see http://www.debian.org/doc/debian-policy/ or
|
||||
# the debian-policy package
|
||||
|
||||
|
||||
case "$1" in
|
||||
remove|upgrade|deconfigure)
|
||||
;;
|
||||
|
||||
failed-upgrade)
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "prerm called with unknown argument \`$1'" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# dh_installdeb will replace this with shell code automatically
|
||||
# generated by other debhelper scripts.
|
||||
|
||||
#DEBHELPER#
|
||||
|
||||
exit 0
|
||||
91
debian/rules
vendored
Executable file
91
debian/rules
vendored
Executable file
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/make -f
|
||||
# -*- makefile -*-
|
||||
# Sample debian/rules that uses debhelper.
|
||||
# This file was originally written by Joey Hess and Craig Small.
|
||||
# As a special exception, when this file is copied by dh-make into a
|
||||
# dh-make output file, you may use that output file without restriction.
|
||||
# This special exception was added by Craig Small in version 0.37 of dh-make.
|
||||
|
||||
# Uncomment this to turn on verbose mode.
|
||||
#export DH_VERBOSE=1
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
configure: configure-stamp
|
||||
configure-stamp:
|
||||
dh_testdir
|
||||
# Add here commands to configure the package.
|
||||
|
||||
touch configure-stamp
|
||||
|
||||
|
||||
build: build-stamp
|
||||
|
||||
build-stamp: configure-stamp
|
||||
dh_testdir
|
||||
|
||||
# Add here commands to compile the package.
|
||||
$(MAKE)
|
||||
#docbook-to-man debian/uhub.sgml > uhub.1
|
||||
|
||||
touch $@
|
||||
|
||||
clean:
|
||||
dh_testdir
|
||||
dh_testroot
|
||||
rm -f build-stamp configure-stamp
|
||||
|
||||
# Add here commands to clean up after the build process.
|
||||
$(MAKE) clean
|
||||
|
||||
dh_clean
|
||||
|
||||
install: build
|
||||
dh_testdir
|
||||
dh_testroot
|
||||
dh_prep
|
||||
dh_installdirs
|
||||
|
||||
# Add here commands to install the package into debian/uhub.
|
||||
$(MAKE) DESTDIR=$(CURDIR)/debian/uhub install
|
||||
|
||||
|
||||
# Build architecture-independent files here.
|
||||
binary-indep: install
|
||||
# We have nothing to do by default.
|
||||
|
||||
# Build architecture-dependent files here.
|
||||
binary-arch: install
|
||||
dh_testdir
|
||||
dh_testroot
|
||||
dh_installchangelogs ChangeLog
|
||||
dh_installdocs
|
||||
dh_installexamples
|
||||
# dh_install
|
||||
# dh_installmenu
|
||||
# dh_installdebconf
|
||||
# dh_installlogrotate
|
||||
# dh_installemacsen
|
||||
# dh_installpam
|
||||
# dh_installmime
|
||||
# dh_python
|
||||
# dh_installinit
|
||||
# dh_installcron
|
||||
# dh_installinfo
|
||||
dh_installman
|
||||
dh_link
|
||||
dh_strip
|
||||
dh_compress
|
||||
dh_fixperms
|
||||
# dh_perl
|
||||
# dh_makeshlibs
|
||||
dh_installdeb
|
||||
dh_shlibdeps
|
||||
dh_gencontrol
|
||||
dh_md5sums
|
||||
dh_builddeb
|
||||
|
||||
binary: binary-indep binary-arch
|
||||
.PHONY: build clean binary-indep binary-arch binary install configure
|
||||
10
debian/uhub.default
vendored
Normal file
10
debian/uhub.default
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
# Defaults for uhub initscript
|
||||
# sourced by /etc/init.d/uhub
|
||||
# installed at /etc/default/uhub by the maintainer scripts
|
||||
|
||||
# Additional options that are passed to the Daemon.
|
||||
ENABLED=1
|
||||
LOGFILE="/var/log/uhug.log"
|
||||
USER=nobody
|
||||
GROUP=nogroup
|
||||
DAEMON_OPTS="-f -l ${LOGFILE} -u ${USER} -g {GROUP}"
|
||||
@@ -936,6 +936,7 @@ int main(int argc, char** argv)
|
||||
parse_command_line(argc, argv);
|
||||
|
||||
net_initialize();
|
||||
event_init();
|
||||
|
||||
memset(&saddr, 0, sizeof(saddr));
|
||||
saddr.sin_family = AF_INET;
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
#define ACL_ADD_BOOL(S, L) do { ret = check_cmd_bool(S, L, line, line_count); if (ret != 0) return ret; } while(0)
|
||||
#define ACL_ADD_ADDR(S, L) do { ret = check_cmd_addr(S, L, line, line_count); if (ret != 0) return ret; } while(0)
|
||||
|
||||
static const char* get_user_credential_string(enum user_credentials cred)
|
||||
const char* get_user_credential_string(enum user_credentials cred)
|
||||
{
|
||||
switch (cred)
|
||||
{
|
||||
|
||||
@@ -48,6 +48,8 @@ enum user_credentials
|
||||
cred_link, /**<<< "User is a link (not used currently)" */
|
||||
};
|
||||
|
||||
const char* get_user_credential_string(enum user_credentials cred);
|
||||
|
||||
struct user_access_info
|
||||
{
|
||||
char* username; /* name of user, cid or IP range */
|
||||
|
||||
238
src/commands.c
238
src/commands.c
@@ -19,165 +19,175 @@
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
static int command_access_denied(struct user* user)
|
||||
typedef int (*command_handler)(struct user* user, const char* message);
|
||||
|
||||
struct commands_handler
|
||||
{
|
||||
struct adc_message* command;
|
||||
char* buffer = adc_msg_escape("Access denied.");
|
||||
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
|
||||
adc_msg_add_argument(command, buffer);
|
||||
route_to_user(user, command);
|
||||
adc_msg_free(command);
|
||||
hub_free(buffer);
|
||||
return 0;
|
||||
const char* prefix;
|
||||
size_t length;
|
||||
enum user_credentials cred;
|
||||
command_handler handler;
|
||||
const char* description;
|
||||
};
|
||||
|
||||
static struct commands_handler command_handlers[];
|
||||
|
||||
static void send_message(struct user* user, const char* message)
|
||||
{
|
||||
char* buffer = adc_msg_escape(message);
|
||||
struct adc_message* command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
|
||||
adc_msg_add_argument(command, buffer);
|
||||
route_to_user(user, command);
|
||||
adc_msg_free(command);
|
||||
hub_free(buffer);
|
||||
}
|
||||
|
||||
static int command_access_denied(struct user* user, const char* command)
|
||||
{
|
||||
char temp[64];
|
||||
snprintf(temp, 64, "*** Access denied: \"%s\"", command);
|
||||
send_message(user, temp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int command_stats(struct user* user, const char* message)
|
||||
{
|
||||
struct adc_message* command;
|
||||
|
||||
if (user->credentials < cred_super)
|
||||
return command_access_denied(user);
|
||||
|
||||
char temp[128];
|
||||
|
||||
snprintf(temp, 128, "*** Stats: %zu users, peak: %zu. Network (up/down): %d/%d KB/s, peak: %d/%d KB/s",
|
||||
char temp[128];
|
||||
snprintf(temp, 128, "*** Stats: %zu users, peak: %zu. Network (up/down): %d/%d KB/s, peak: %d/%d KB/s",
|
||||
user->hub->users->count,
|
||||
user->hub->users->count_peak,
|
||||
(int) user->hub->stats.net_tx / 1024,
|
||||
(int) user->hub->stats.net_rx / 1024,
|
||||
(int) user->hub->stats.net_tx_peak / 1024,
|
||||
(int) user->hub->stats.net_rx_peak / 1024);
|
||||
|
||||
char* buffer = adc_msg_escape(temp);
|
||||
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
|
||||
adc_msg_add_argument(command, buffer);
|
||||
route_to_user(user, command);
|
||||
adc_msg_free(command);
|
||||
hub_free(buffer);
|
||||
return 0;
|
||||
|
||||
send_message(user, temp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int command_help(struct user* user, const char* message)
|
||||
{
|
||||
struct adc_message* command;
|
||||
char* buffer = adc_msg_escape("\n"
|
||||
"*** Available commands:\n"
|
||||
"!help - Show this help message\n"
|
||||
"!stats - Show hub stats (super)\n"
|
||||
"!version - Show this help message\n"
|
||||
"!uptime - Display hub uptime\n"
|
||||
"!kick <user> - Kick user (operator)\n"
|
||||
);
|
||||
|
||||
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
|
||||
adc_msg_add_argument(command, buffer);
|
||||
route_to_user(user, command);
|
||||
adc_msg_free(command);
|
||||
hub_free(buffer);
|
||||
#define MAX_HELP_MSG 1024
|
||||
size_t n;
|
||||
char msg[MAX_HELP_MSG];
|
||||
msg[0] = 0;
|
||||
strcat(msg, "\n*** Available commands:\n");
|
||||
|
||||
for (n = 0; command_handlers[n].prefix; n++)
|
||||
{
|
||||
if (command_handlers[n].cred <= user->credentials)
|
||||
{
|
||||
strcat(msg, command_handlers[n].prefix);
|
||||
strcat(msg, " - ");
|
||||
strcat(msg, command_handlers[n].description);
|
||||
strcat(msg, "\n");
|
||||
}
|
||||
}
|
||||
send_message(user, msg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_uptime(struct user* user, const char* message)
|
||||
{
|
||||
struct adc_message* command;
|
||||
char tmp[128];
|
||||
size_t d;
|
||||
size_t h;
|
||||
size_t m;
|
||||
size_t D = (size_t) difftime(time(0), user->hub->tm_started);
|
||||
char tmp[128];
|
||||
size_t d;
|
||||
size_t h;
|
||||
size_t m;
|
||||
size_t D = (size_t) difftime(time(0), user->hub->tm_started);
|
||||
|
||||
d = D / (24 * 3600);
|
||||
D = D % (24 * 3600);
|
||||
h = D / 3600;
|
||||
D = D % 3600;
|
||||
m = D / 60;
|
||||
d = D / (24 * 3600);
|
||||
D = D % (24 * 3600);
|
||||
h = D / 3600;
|
||||
D = D % 3600;
|
||||
m = D / 60;
|
||||
|
||||
tmp[0] = 0;
|
||||
strcat(tmp, "*** Uptime: ");
|
||||
|
||||
if (d)
|
||||
{
|
||||
strcat(tmp, uhub_itoa((int) d));
|
||||
strcat(tmp, " day");
|
||||
if (d != 1) strcat(tmp, "s");
|
||||
strcat(tmp, ", ");
|
||||
}
|
||||
tmp[0] = 0;
|
||||
strcat(tmp, "*** Uptime: ");
|
||||
|
||||
if (h < 10) strcat(tmp, "0");
|
||||
strcat(tmp, uhub_itoa((int) h));
|
||||
strcat(tmp, ":");
|
||||
if (m < 10) strcat(tmp, "0");
|
||||
strcat(tmp, uhub_itoa((int) m));
|
||||
if (d)
|
||||
{
|
||||
strcat(tmp, uhub_itoa((int) d));
|
||||
strcat(tmp, " day");
|
||||
if (d != 1) strcat(tmp, "s");
|
||||
strcat(tmp, ", ");
|
||||
}
|
||||
|
||||
char* buffer = adc_msg_escape(tmp);
|
||||
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
|
||||
adc_msg_add_argument(command, buffer);
|
||||
route_to_user(user, command);
|
||||
adc_msg_free(command);
|
||||
hub_free(buffer);
|
||||
return 0;
|
||||
if (h < 10) strcat(tmp, "0");
|
||||
strcat(tmp, uhub_itoa((int) h));
|
||||
strcat(tmp, ":");
|
||||
if (m < 10) strcat(tmp, "0");
|
||||
strcat(tmp, uhub_itoa((int) m));
|
||||
|
||||
send_message(user, tmp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_kick(struct user* user, const char* message)
|
||||
{
|
||||
struct adc_message* command;
|
||||
|
||||
if (user->credentials < cred_operator)
|
||||
return command_access_denied(user);
|
||||
|
||||
char* buffer = adc_msg_escape("*** Kick not implemented!");
|
||||
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
|
||||
adc_msg_add_argument(command, buffer);
|
||||
route_to_user(user, command);
|
||||
adc_msg_free(command);
|
||||
hub_free(buffer);
|
||||
return 0;
|
||||
send_message(user, "*** Kick not implemented!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_reload(struct user* user, const char* message)
|
||||
{
|
||||
send_message(user, "*** Reloading configuration");
|
||||
user->hub->status = hub_status_restart;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_shutdown(struct user* user, const char* message)
|
||||
{
|
||||
send_message(user, "*** Hub shuting down...");
|
||||
user->hub->status = hub_status_shutdown;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int command_version(struct user* user, const char* message)
|
||||
{
|
||||
struct adc_message* command;
|
||||
char* buffer = adc_msg_escape("*** Powered by " PRODUCT "/" VERSION);
|
||||
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
|
||||
adc_msg_add_argument(command, buffer);
|
||||
route_to_user(user, command);
|
||||
adc_msg_free(command);
|
||||
hub_free(buffer);
|
||||
send_message(user, "*** Powered by " PRODUCT "/" VERSION);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_myip(struct user* user, const char* message)
|
||||
{
|
||||
struct adc_message* command;
|
||||
char tmp[128];
|
||||
char* buffer;
|
||||
|
||||
tmp[0] = 0;
|
||||
strcat(tmp, "*** Your IP: ");
|
||||
strcat(tmp, ip_convert_to_string(&user->ipaddr));
|
||||
|
||||
buffer = adc_msg_escape(tmp);
|
||||
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
|
||||
adc_msg_add_argument(command, buffer);
|
||||
route_to_user(user, command);
|
||||
adc_msg_free(command);
|
||||
hub_free(buffer);
|
||||
snprintf(tmp, 128, "*** Your IP: %s", ip_convert_to_string(&user->ipaddr));
|
||||
send_message(user, tmp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int command_dipatcher(struct user* user, const char* message)
|
||||
{
|
||||
if (!strncmp(message, "!stats", 6)) command_stats(user, message);
|
||||
else if (!strncmp(message, "!help", 5)) command_help(user, message);
|
||||
else if (!strncmp(message, "!kick", 5)) command_kick(user, message);
|
||||
else if (!strncmp(message, "!version", 8)) command_version(user, message);
|
||||
else if (!strncmp(message, "!uptime", 7)) command_uptime(user, message);
|
||||
else if (!strncmp(message, "+myip", 5)) command_myip(user, message);
|
||||
else
|
||||
size_t n = 0;
|
||||
for (n = 0; command_handlers[n].prefix; n++)
|
||||
{
|
||||
if (!strncmp(message, command_handlers[n].prefix, command_handlers[n].length))
|
||||
{
|
||||
if (command_handlers[n].cred <= user->credentials)
|
||||
{
|
||||
return command_handlers[n].handler(user, message);
|
||||
}
|
||||
else
|
||||
{
|
||||
return command_access_denied(user, &command_handlers[n].prefix[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct commands_handler command_handlers[] = {
|
||||
{ "!help", 5, cred_guest, command_help, "Show this help message." },
|
||||
{ "!stats", 6, cred_super, command_stats, "Show hub statistics." },
|
||||
{ "!version", 8, cred_guest, command_version, "Show hub version info." },
|
||||
{ "!uptime", 7, cred_guest, command_uptime, "Display hub uptime info." },
|
||||
{ "!kick", 5, cred_operator, command_kick, "Kick a user" },
|
||||
{ "!reload", 7, cred_admin, command_reload, "Reload configuration files." },
|
||||
{ "!shutdown", 9, cred_admin, command_shutdown, "Shutdown hub." },
|
||||
{ "+myip", 5, cred_guest, command_myip, "Show your own IP." },
|
||||
{ 0, 0, cred_none, command_help, "{ Last dummy option }" }
|
||||
};
|
||||
|
||||
|
||||
@@ -35,9 +35,8 @@ int event_queue_initialize(struct event_queue** queue, event_queue_callback call
|
||||
|
||||
(*queue)->q1 = list_create();
|
||||
(*queue)->q2 = list_create();
|
||||
(*queue)->event = (struct event*) hub_malloc_zero(sizeof(struct event));
|
||||
|
||||
if (!(*queue)->q1 || !(*queue)->q2 || !(*queue)->event)
|
||||
if (!(*queue)->q1 || !(*queue)->q2)
|
||||
{
|
||||
list_destroy((*queue)->q1);
|
||||
list_destroy((*queue)->q2);
|
||||
@@ -47,7 +46,6 @@ int event_queue_initialize(struct event_queue** queue, event_queue_callback call
|
||||
(*queue)->callback = callback;
|
||||
(*queue)->callback_data = ptr;
|
||||
|
||||
evtimer_set((*queue)->event, libevent_queue_process, *queue);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -57,12 +55,6 @@ void event_queue_shutdown(struct event_queue* queue)
|
||||
/* Should be empty at this point! */
|
||||
list_destroy(queue->q1);
|
||||
list_destroy(queue->q2);
|
||||
|
||||
if (queue->event)
|
||||
{
|
||||
evtimer_del(queue->event);
|
||||
hub_free(queue->event);
|
||||
}
|
||||
hub_free(queue);
|
||||
}
|
||||
|
||||
@@ -135,12 +127,6 @@ void event_queue_post(struct event_queue* queue, struct event_data* message)
|
||||
#endif
|
||||
|
||||
list_append(q, data);
|
||||
|
||||
|
||||
if (!queue->locked && queue->event)
|
||||
{
|
||||
libevent_queue_schedule(queue);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -154,19 +140,5 @@ size_t event_queue_size(struct event_queue* queue)
|
||||
return list_size(queue->q1) + list_size(queue->q2);
|
||||
}
|
||||
|
||||
void libevent_queue_schedule(struct event_queue* queue)
|
||||
{
|
||||
struct timeval zero = { 0, };
|
||||
evtimer_add(queue->event, &zero);
|
||||
}
|
||||
|
||||
void libevent_queue_process(int fd, short events, void* arg)
|
||||
{
|
||||
struct event_queue* queue = (struct event_queue*) arg;
|
||||
if (event_queue_process(queue))
|
||||
{
|
||||
libevent_queue_schedule(queue);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -36,7 +36,6 @@ struct event_queue
|
||||
struct linked_list* q2; /* secondary, when primary is locked */
|
||||
event_queue_callback callback;
|
||||
void* callback_data;
|
||||
struct event* event; /* libevent handle */
|
||||
};
|
||||
|
||||
extern int event_queue_initialize(struct event_queue** queue, event_queue_callback callback, void* ptr);
|
||||
@@ -45,11 +44,5 @@ extern void event_queue_shutdown(struct event_queue* queue);
|
||||
extern void event_queue_post(struct event_queue* queue, struct event_data* message);
|
||||
extern size_t event_queue_size(struct event_queue* queue);
|
||||
|
||||
/**
|
||||
* Only used internally with libevent.
|
||||
*/
|
||||
extern void libevent_queue_process(int fd, short events, void* arg);
|
||||
extern void libevent_queue_schedule(struct event_queue* queue);
|
||||
|
||||
#endif /* HAVE_UHUB_EVENT_QUEUE_H */
|
||||
|
||||
|
||||
42
src/hub.c
42
src/hub.c
@@ -479,11 +479,24 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
net_address_to_string(AF_INET6, &((struct sockaddr_in6*) &addr)->sin6_addr, address_buf, INET6_ADDRSTRLEN);
|
||||
}
|
||||
|
||||
hub_log(log_info, "Starting server, listening on %s:%d...", address_buf, config->server_port);
|
||||
#ifdef LIBEVENT_1_4
|
||||
hub->evbase = event_base_new();
|
||||
#else
|
||||
hub->evbase = event_init();
|
||||
#endif
|
||||
if (!hub->evbase)
|
||||
{
|
||||
hub_log(log_error, "Unable to initialize libevent.");
|
||||
hub_free(hub);
|
||||
return 0;
|
||||
}
|
||||
|
||||
hub_log(log_info, "Starting " PRODUCT "/" VERSION ", listening on %s:%d...", address_buf, config->server_port);
|
||||
|
||||
server_tcp = net_socket_create(af, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (server_tcp == -1)
|
||||
{
|
||||
event_base_free(hub->evbase);
|
||||
hub_free(hub);
|
||||
return 0;
|
||||
}
|
||||
@@ -492,6 +505,7 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
server_udp = net_socket_create(af, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (server_udp == -1)
|
||||
{
|
||||
event_base_free(hub->evbase);
|
||||
hub_free(hub);
|
||||
return 0;
|
||||
}
|
||||
@@ -500,6 +514,7 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
ret = net_set_reuseaddress(server_tcp, 1);
|
||||
if (ret == -1)
|
||||
{
|
||||
event_base_free(hub->evbase);
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
@@ -512,6 +527,7 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
ret = net_set_reuseaddress(server_udp, 1);
|
||||
if (ret == -1)
|
||||
{
|
||||
event_base_free(hub->evbase);
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
net_close(server_udp);
|
||||
@@ -523,6 +539,7 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
ret = net_set_nonblocking(server_tcp, 1);
|
||||
if (ret == -1)
|
||||
{
|
||||
event_base_free(hub->evbase);
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
@@ -535,6 +552,7 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
ret = net_set_nonblocking(server_udp, 1);
|
||||
if (ret == -1)
|
||||
{
|
||||
event_base_free(hub->evbase);
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
net_close(server_udp);
|
||||
@@ -547,6 +565,7 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_log(log_fatal, "hub_start_service(): Unable to bind to TCP local address. errno=%d, str=%s", net_error(), net_error_string(net_error()));
|
||||
event_base_free(hub->evbase);
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
@@ -560,6 +579,7 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_log(log_fatal, "hub_start_service(): Unable to bind to UDP local address. errno=%d, str=%s", net_error(), net_error_string(net_error()));
|
||||
event_base_free(hub->evbase);
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
net_close(server_udp);
|
||||
@@ -571,6 +591,7 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_log(log_fatal, "hub_start_service(): Unable to listen to socket");
|
||||
event_base_free(hub->evbase);
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
@@ -597,6 +618,7 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
}
|
||||
|
||||
event_set(&hub->ev_accept, hub->fd_tcp, EV_READ | EV_PERSIST, net_on_accept, hub);
|
||||
event_base_set(hub->evbase, &hub->ev_accept);
|
||||
if (event_add(&hub->ev_accept, NULL) == -1)
|
||||
{
|
||||
user_manager_shutdown(hub);
|
||||
@@ -610,6 +632,7 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
event_set(&hub->ev_datagram, hub->fd_udp, EV_READ | EV_PERSIST, net_on_packet, hub);
|
||||
event_base_set(hub->evbase, &hub->ev_datagram);
|
||||
if (event_add(&hub->ev_datagram, NULL) == -1)
|
||||
{
|
||||
user_manager_shutdown(hub);
|
||||
@@ -650,6 +673,7 @@ void hub_shutdown_service(struct hub_info* hub)
|
||||
net_close(hub->fd_tcp);
|
||||
user_manager_shutdown(hub);
|
||||
hub->status = hub_status_stopped;
|
||||
event_base_free(hub->evbase);
|
||||
hub_free(hub);
|
||||
hub = 0;
|
||||
}
|
||||
@@ -950,3 +974,19 @@ size_t hub_get_min_hubs_op(struct hub_info* hub)
|
||||
}
|
||||
|
||||
|
||||
void hub_event_loop(struct hub_info* hub)
|
||||
{
|
||||
#if 0
|
||||
event_dispatch();
|
||||
#endif
|
||||
int ret;
|
||||
do
|
||||
{
|
||||
ret = event_base_loop(hub->evbase, EVLOOP_ONCE);
|
||||
if (ret != 0)
|
||||
break;
|
||||
|
||||
event_queue_process(hub->queue);
|
||||
}
|
||||
while (hub->status == hub_status_running);
|
||||
}
|
||||
|
||||
@@ -92,6 +92,7 @@ struct hub_info
|
||||
#endif
|
||||
struct hub_stats stats;
|
||||
struct event_queue* queue;
|
||||
struct event_base* evbase;
|
||||
struct hub_config* config;
|
||||
struct user_manager* users;
|
||||
struct acl_handle* acl;
|
||||
@@ -334,6 +335,10 @@ extern size_t hub_get_max_hubs_total(struct hub_info* hub);
|
||||
*/
|
||||
extern void hub_schedule_runslice(struct hub_info* hub);
|
||||
|
||||
/**
|
||||
* Run event loop.
|
||||
*/
|
||||
extern void hub_event_loop(struct hub_info* hub);
|
||||
|
||||
|
||||
#endif /* HAVE_UHUB_HUB_H */
|
||||
|
||||
@@ -19,12 +19,36 @@
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
static void log_user_login(struct user* u)
|
||||
{
|
||||
const char* cred = get_user_credential_string(u->credentials);
|
||||
const char* addr = ip_convert_to_string(&u->ipaddr);
|
||||
hub_log(log_user, "LoginOK %s/%s %s \"%s\" (%s) \"%s\"", sid_to_string(u->id.sid), u->id.cid, addr, u->id.nick, cred, u->user_agent);
|
||||
}
|
||||
|
||||
static void log_user_login_error(struct user* u, enum status_message msg)
|
||||
{
|
||||
const char* addr = ip_convert_to_string(&u->ipaddr);
|
||||
const char* message = hub_get_status_message(u->hub, msg);
|
||||
hub_log(log_user, "LoginError %s/%s %s \"%s\" (%s) \"%s\"", sid_to_string(u->id.sid), u->id.cid, addr, u->id.nick, message, u->user_agent);
|
||||
}
|
||||
|
||||
static void log_user_logout(struct user* u, const char* message)
|
||||
{
|
||||
const char* addr = ip_convert_to_string(&u->ipaddr);
|
||||
hub_log(log_user, "Logout %s/%s %s \"%s\" (%s)", sid_to_string(u->id.sid), u->id.cid, addr, u->id.nick, message);
|
||||
}
|
||||
|
||||
static void log_user_nick_change(struct user* u, const char* nick)
|
||||
{
|
||||
const char* addr = ip_convert_to_string(&u->ipaddr);
|
||||
hub_log(log_user, "NickChange %s/%s %s \"%s\" -> \"%s\"", sid_to_string(u->id.sid), u->id.cid, addr, u->id.nick, nick);
|
||||
}
|
||||
|
||||
|
||||
/* Send MOTD, do logging etc */
|
||||
void on_login_success(struct user* u)
|
||||
{
|
||||
/* Logging - FIXME: Move this to a plugin */
|
||||
const char* addr = ip_convert_to_string(&u->ipaddr);
|
||||
const char* credentials_string[] = { "!none!", "link", "guest", "user", "operator", "super", "admin" };
|
||||
struct timeval timeout = { TIMEOUT_IDLE, 0 };
|
||||
|
||||
/* Send user list of all existing users */
|
||||
@@ -36,7 +60,7 @@ void on_login_success(struct user* u)
|
||||
user_manager_add(u);
|
||||
|
||||
/* Print log message */
|
||||
hub_log(log_user, "Login OK %s/%s \"%s\" [%s] (%s) \"%s\"", sid_to_string(u->id.sid), u->id.cid, u->id.nick, addr, credentials_string[u->credentials], u->user_agent);
|
||||
log_user_login(u);
|
||||
|
||||
/* Announce new user to all connected users */
|
||||
if (user_is_logged_in(u))
|
||||
@@ -51,36 +75,29 @@ void on_login_success(struct user* u)
|
||||
event_add(u->ev_read, &timeout);
|
||||
}
|
||||
|
||||
|
||||
void on_login_failure(struct user* u, enum status_message msg)
|
||||
{
|
||||
const char* addr = ip_convert_to_string(&u->ipaddr);
|
||||
const char* message = hub_get_status_message(u->hub, msg);
|
||||
hub_log(log_user, "Login FAIL %s/%s \"%s\" [%s] (%s) \"%s\"", sid_to_string(u->id.sid), u->id.cid, u->id.nick, addr, message, u->user_agent);
|
||||
|
||||
log_user_login_error(u, msg);
|
||||
hub_send_status(u, msg, status_level_fatal);
|
||||
user_disconnect(u, quit_logon_error);
|
||||
}
|
||||
|
||||
|
||||
void on_nick_change(struct user* u, const char* nick)
|
||||
{
|
||||
if (user_is_logged_in(u))
|
||||
{
|
||||
hub_log(log_user, "Nick change %s/%s \"%s\" -> \"%s\"", sid_to_string(u->id.sid), u->id.cid, u->id.nick, nick);
|
||||
log_user_nick_change(u, nick);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void on_logout_user(struct user* user)
|
||||
{
|
||||
const char* reason = "";
|
||||
const char* addr;
|
||||
|
||||
/* These are used for logging purposes */
|
||||
switch (user->quit_reason)
|
||||
{
|
||||
case quit_disconnected: reason = "disconnected"; break;
|
||||
case quit_disconnected: reason = "disconnected"; break;
|
||||
case quit_kicked: reason = "kicked"; break;
|
||||
case quit_banned: reason = "banned"; break;
|
||||
case quit_timeout: reason = "timeout"; break;
|
||||
@@ -90,6 +107,7 @@ void on_logout_user(struct user* user)
|
||||
case quit_protocol_error: reason = "protocol error"; break;
|
||||
case quit_logon_error: reason = "login error"; break;
|
||||
case quit_hub_disabled: reason = "hub disabled"; break;
|
||||
case quit_ghost_timeout: reason = "ghost"; break;
|
||||
default:
|
||||
if (user->hub->status == hub_status_shutdown)
|
||||
reason = "hub shutdown";
|
||||
@@ -98,10 +116,7 @@ void on_logout_user(struct user* user)
|
||||
break;
|
||||
}
|
||||
|
||||
addr = ip_convert_to_string(&user->ipaddr);
|
||||
hub_log(log_user, "Logout %s/%s \"%s\" [%s] (%s)", sid_to_string(user->id.sid), user->id.cid, user->id.nick, addr, reason);
|
||||
|
||||
|
||||
log_user_logout(user, reason);
|
||||
user->quit_reason = 0;
|
||||
}
|
||||
|
||||
|
||||
15
src/inf.c
15
src/inf.c
@@ -332,7 +332,7 @@ static int check_logged_in(struct user* user, struct adc_message* cmd)
|
||||
if (lookup1 == lookup2)
|
||||
{
|
||||
hub_log(log_debug, "check_logged_in: exact same user is logged in: %s", user->id.nick);
|
||||
user_disconnect(lookup1, quit_timeout);
|
||||
user_disconnect(lookup1, quit_ghost_timeout);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
@@ -645,6 +645,7 @@ void update_user_info(struct user* u, struct adc_message* cmd)
|
||||
argument = adc_msg_get_argument(cmd, n++);
|
||||
}
|
||||
user_set_info(u, cmd_new);
|
||||
adc_msg_free(cmd_new);
|
||||
}
|
||||
|
||||
|
||||
@@ -766,7 +767,7 @@ int hub_handle_info_login(struct user* user, struct adc_message* cmd)
|
||||
*/
|
||||
int hub_handle_info(struct user* user, const struct adc_message* cmd_unmodified)
|
||||
{
|
||||
struct adc_message* cmd = adc_msg_copy(cmd_unmodified); /* FIXME: Have a small memory leak here! */
|
||||
struct adc_message* cmd = adc_msg_copy(cmd_unmodified);
|
||||
if (!cmd) return -1; /* OOM */
|
||||
|
||||
cmd->priority = 1;
|
||||
@@ -778,6 +779,16 @@ int hub_handle_info(struct user* user, const struct adc_message* cmd_unmodified)
|
||||
*/
|
||||
if (user_is_connecting(user))
|
||||
{
|
||||
/*
|
||||
* Don't allow the user to send multiple INF messages in this stage!
|
||||
* Since that can have serious side-effects.
|
||||
*/
|
||||
if (user->info)
|
||||
{
|
||||
adc_msg_free(cmd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ret = hub_handle_info_login(user, cmd);
|
||||
if (ret < 0)
|
||||
{
|
||||
|
||||
@@ -177,8 +177,8 @@ void hub_log(int log_verbosity, const char *format, ...)
|
||||
if (log_verbosity < verbosity)
|
||||
{
|
||||
t = time(NULL);
|
||||
tmp = gmtime(&t);
|
||||
strftime(timestamp, 32, "%a, %d %b %Y %H:%M:%S +0000", tmp);
|
||||
tmp = localtime(&t);
|
||||
strftime(timestamp, 32, "%Y-%m-%d %H:%M:%S", tmp);
|
||||
va_start(args, format);
|
||||
vsnprintf(logmsg, 1024, format, args);
|
||||
va_end(args);
|
||||
|
||||
31
src/main.c
31
src/main.c
@@ -37,47 +37,29 @@ void hub_handle_signal(int fd, short events, void* arg)
|
||||
{
|
||||
struct hub_info* hub = (struct hub_info*) arg;
|
||||
int signal = fd;
|
||||
struct timeval now = {0, 0};
|
||||
|
||||
switch (signal)
|
||||
{
|
||||
case SIGINT:
|
||||
hub_log(log_info, "Interrupted. Shutting down...");
|
||||
hub->status = hub_status_shutdown;
|
||||
event_loopexit(&now);
|
||||
break;
|
||||
|
||||
case SIGTERM:
|
||||
hub_log(log_info, "Terminated. Shutting down...");
|
||||
hub->status = hub_status_shutdown;
|
||||
event_loopexit(&now);
|
||||
break;
|
||||
|
||||
case SIGPIPE:
|
||||
hub_log(log_trace, "hub_handle_signal(): caught SIGPIPE (ignoring)");
|
||||
break;
|
||||
|
||||
case SIGHUP:
|
||||
hub_log(log_info, "Caught hangup signal. Reloading configuration files...");
|
||||
hub->status = hub_status_restart;
|
||||
event_loopexit(&now);
|
||||
break;
|
||||
|
||||
case SIGUSR1:
|
||||
hub_log(log_trace, "hub_handle_signal(): caught SIGUSR1 -- FIXME");
|
||||
break;
|
||||
|
||||
case SIGUSR2:
|
||||
hub_log(log_trace, "hub_handle_signal(): caught SIGUSR2");
|
||||
{
|
||||
user_manager_print_stats(hub);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
hub_log(log_trace, "hub_handle_signal(): caught unknown signal: %d", signal);
|
||||
hub->status = hub_status_shutdown;
|
||||
event_loopexit(&now);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -89,8 +71,6 @@ static int signals[] =
|
||||
SIGTERM, /* Terminate the application */
|
||||
SIGPIPE, /* prevent sigpipe from kills the application */
|
||||
SIGHUP, /* reload configuration */
|
||||
SIGUSR1, /* dump statistics */
|
||||
SIGUSR2, /* (unused) */
|
||||
0
|
||||
};
|
||||
|
||||
@@ -100,6 +80,7 @@ void setup_signal_handlers(struct hub_info* hub)
|
||||
for (i = 0; signals[i]; i++)
|
||||
{
|
||||
signal_set(&signal_events[i], signals[i], hub_handle_signal, hub);
|
||||
event_base_set(hub->evbase, &signal_events[i]);
|
||||
if (signal_add(&signal_events[i], NULL))
|
||||
{
|
||||
hub_log(log_error, "Error setting signal handler %d", signals[i]);
|
||||
@@ -130,6 +111,11 @@ int main_loop()
|
||||
|
||||
do
|
||||
{
|
||||
if (hub)
|
||||
{
|
||||
hub_log(log_info, "Reloading configuration files...");
|
||||
}
|
||||
|
||||
if (read_config(arg_config, &configuration, !arg_have_config) == -1)
|
||||
return -1;
|
||||
|
||||
@@ -153,7 +139,7 @@ int main_loop()
|
||||
|
||||
hub_set_variables(hub, &acl);
|
||||
|
||||
event_dispatch();
|
||||
hub_event_loop(hub);
|
||||
|
||||
hub_free_variables(hub);
|
||||
acl_shutdown(&acl);
|
||||
@@ -187,6 +173,9 @@ int check_configuration(int dump)
|
||||
puts("");
|
||||
}
|
||||
|
||||
if (dump)
|
||||
return 0;
|
||||
|
||||
if (ret == -1)
|
||||
{
|
||||
fprintf(stderr, "ERROR\n");
|
||||
|
||||
@@ -25,10 +25,9 @@ void net_on_read(int fd, short ev, void *arg)
|
||||
static char buf[MAX_RECV_BUF];
|
||||
struct user* user = (struct user*) arg;
|
||||
char* pos;
|
||||
char* start;
|
||||
ssize_t offset;
|
||||
size_t offset;
|
||||
size_t buflen;
|
||||
ssize_t size;
|
||||
ssize_t buflen;
|
||||
int more = 1;
|
||||
int flag_close = 0;
|
||||
|
||||
@@ -55,11 +54,7 @@ void net_on_read(int fd, short ev, void *arg)
|
||||
memcpy(buf, user->recv_buf, user->recv_buf_offset);
|
||||
offset = user->recv_buf_offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
|
||||
size = net_recv(fd, &buf[offset], MAX_RECV_BUF - offset, 0);
|
||||
if (size == -1)
|
||||
{
|
||||
@@ -75,40 +70,63 @@ void net_on_read(int fd, short ev, void *arg)
|
||||
else
|
||||
{
|
||||
buflen = offset + size;
|
||||
start = buf;
|
||||
while ((pos = strchr(start, '\n')))
|
||||
ssize_t handled = 0;
|
||||
|
||||
while ((pos = memchr(&buf[handled], '\n', (buflen - handled))))
|
||||
{
|
||||
pos[0] = '\0';
|
||||
if (*start && strlen(start) < user->hub->config->max_recv_buffer)
|
||||
{
|
||||
if (hub_handle_message(user, start, &pos[0]-&start[0]) == -1)
|
||||
{
|
||||
flag_close = quit_protocol_error;
|
||||
more = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
start = &pos[1];
|
||||
}
|
||||
|
||||
if (!more) break;
|
||||
|
||||
if (&buf[offset + size] > &start[0])
|
||||
{
|
||||
if (!user->recv_buf)
|
||||
{
|
||||
user->recv_buf = hub_malloc(user->hub->config->max_recv_buffer);
|
||||
}
|
||||
size_t msglen = &pos[0] - &buf[handled];
|
||||
|
||||
if (!user->recv_buf)
|
||||
if (user_flag_get(user, flag_maxbuf))
|
||||
{
|
||||
flag_close = quit_memory_error;
|
||||
break;
|
||||
user_flag_unset(user, flag_maxbuf);
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(user->recv_buf, start, &buf[offset + size] - &start[0]);
|
||||
user->recv_buf_offset = &buf[offset + size] - &start[0];
|
||||
if (msglen < user->hub->config->max_recv_buffer)
|
||||
{
|
||||
if (hub_handle_message(user, &buf[handled], msglen) == -1)
|
||||
{
|
||||
flag_close = quit_protocol_error;
|
||||
more = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
handled += msglen;
|
||||
handled++;
|
||||
}
|
||||
|
||||
if (handled == 0 && user_flag_get(user, flag_maxbuf))
|
||||
handled = buflen;
|
||||
|
||||
if (!more)
|
||||
break;
|
||||
|
||||
if (handled < buflen)
|
||||
{
|
||||
if ((buflen - handled) > user->hub->config->max_recv_buffer)
|
||||
{
|
||||
user_flag_set(user, flag_maxbuf);
|
||||
hub_free(user->recv_buf);
|
||||
user->recv_buf = 0;
|
||||
user->recv_buf_offset = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!user->recv_buf)
|
||||
user->recv_buf = hub_malloc(user->hub->config->max_recv_buffer);
|
||||
|
||||
if (user->recv_buf)
|
||||
{
|
||||
memcpy(user->recv_buf, &buf[handled], buflen - handled);
|
||||
user->recv_buf_offset = buflen - handled;
|
||||
}
|
||||
else
|
||||
{
|
||||
flag_close = quit_memory_error;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -242,11 +260,10 @@ void net_on_accept(int server_fd, short ev, void *arg)
|
||||
{
|
||||
struct hub_info* hub = (struct hub_info*) arg;
|
||||
struct user* user = 0;
|
||||
int accept_more = 1;
|
||||
const char* addr;
|
||||
struct timeval timeout = { TIMEOUT_CONNECTED, 0 };
|
||||
|
||||
while (accept_more)
|
||||
for (;;)
|
||||
{
|
||||
int fd = net_accept(server_fd);
|
||||
if (fd == -1)
|
||||
@@ -291,6 +308,8 @@ void net_on_accept(int server_fd, short ev, void *arg)
|
||||
|
||||
event_set(user->ev_read, fd, EV_READ | EV_PERSIST, net_on_read, user);
|
||||
event_set(user->ev_write, fd, EV_WRITE, net_on_write, user);
|
||||
event_base_set(hub->evbase, user->ev_read);
|
||||
event_base_set(hub->evbase, user->ev_write);
|
||||
event_add(user->ev_read, &timeout);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ static int is_ipv6_supported = -1; /* -1 = CHECK, 0 = NO, 1 = YES */
|
||||
static int net_initialized = 0;
|
||||
static struct net_statistics stats;
|
||||
static struct net_statistics stats_total;
|
||||
static struct event_base* evbase;
|
||||
|
||||
#if defined(IPV6_BINDV6ONLY)
|
||||
#define SOCK_DUAL_STACK_OPT IPV6_BINDV6ONLY
|
||||
@@ -53,16 +52,6 @@ int net_initialize()
|
||||
/* FIXME: Initialize OpenSSL here. */
|
||||
#endif /* SSL_SUPPORT */
|
||||
|
||||
#ifdef OLD_LIBEVENT
|
||||
event_init();
|
||||
#else
|
||||
evbase = event_init();
|
||||
if (!evbase)
|
||||
{
|
||||
hub_log(log_error, "Unable to initialize libevent.");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
net_initialized = 1;
|
||||
return 0;
|
||||
}
|
||||
@@ -80,11 +69,6 @@ int net_shutdown()
|
||||
/* FIXME: Shutdown OpenSSL here. */
|
||||
#endif
|
||||
|
||||
#ifndef OLD_LIBEVENT
|
||||
event_base_free(evbase);
|
||||
#endif
|
||||
evbase = 0;
|
||||
|
||||
#ifdef WINSOCK
|
||||
WSACleanup();
|
||||
#endif
|
||||
@@ -481,11 +465,9 @@ const char* net_get_peer_address(int fd)
|
||||
name4 = (struct sockaddr_in*) &storage;
|
||||
name = (struct sockaddr*) &storage;
|
||||
|
||||
|
||||
int af = net_is_ipv6_supported() ? AF_INET6 : AF_INET;
|
||||
|
||||
if (getpeername(fd, (struct sockaddr*) name, &namelen) != -1)
|
||||
{
|
||||
int af = name4->sin_family;
|
||||
if (af == AF_INET6)
|
||||
{
|
||||
net_address_to_string(af, (void*) &name6->sin6_addr, address, INET6_ADDRSTRLEN);
|
||||
|
||||
16
src/user.h
16
src/user.h
@@ -35,7 +35,7 @@ enum user_state
|
||||
};
|
||||
|
||||
|
||||
enum user_flags
|
||||
enum user_flags
|
||||
{
|
||||
feature_base = 0x00000001, /** BASE: Basic configuration (required by all clients) */
|
||||
feature_auto = 0x00000002, /** AUT0: Automatic nat detection traversal */
|
||||
@@ -47,11 +47,12 @@ enum user_flags
|
||||
feature_ping = 0x00000080, /** PING: Hub pinger information extension */
|
||||
feature_link = 0x00000100, /** LINK: Hub link (not supported) */
|
||||
flag_ignore = 0x01000000, /** Ignore further reads */
|
||||
flag_choke = 0x02000000, /** Choked: Cannot send, waiting for write event */
|
||||
flag_want_read = 0x04000000, /** Need to read (SSL) */
|
||||
flag_want_write = 0x08000000, /** Need to write (SSL) */
|
||||
flag_user_list = 0x10000000, /** Send queue bypass (when receiving the send queue) */
|
||||
flag_nat = 0x20000000, /** nat override enabled */
|
||||
flag_maxbuf = 0x02000000, /** Hit max buf read, ignore msg */
|
||||
flag_choke = 0x04000000, /** Choked: Cannot send, waiting for write event */
|
||||
flag_want_read = 0x08000000, /** Need to read (SSL) */
|
||||
flag_want_write = 0x10000000, /** Need to write (SSL) */
|
||||
flag_user_list = 0x20000000, /** Send queue bypass (when receiving the send queue) */
|
||||
flag_nat = 0x40000000, /** nat override enabled */
|
||||
};
|
||||
|
||||
|
||||
@@ -68,12 +69,13 @@ enum user_quit_reason
|
||||
quit_protocol_error = 8, /** Fatal protocol error */
|
||||
quit_logon_error = 9, /** Unable to login (wrong password, CID/PID, etc) */
|
||||
quit_hub_disabled = 10, /** Hub is disabled. No new connections allowed */
|
||||
quit_ghost_timeout = 11, /** The user is a ghost, and trying to login from another connection */
|
||||
};
|
||||
|
||||
|
||||
struct user_info
|
||||
{
|
||||
sid_t sid; /** session ID */
|
||||
sid_t sid; /** session ID */
|
||||
char cid[MAX_CID_LEN+1]; /** global client ID */
|
||||
char nick[MAX_NICK_LEN+1]; /** User's nick name */
|
||||
};
|
||||
|
||||
@@ -74,6 +74,7 @@ static void timer_statistics(int fd, short ev, void *arg)
|
||||
struct timeval timeout = { TIMEOUT_STATS, 0 };
|
||||
user_manager_update_stats(hub);
|
||||
evtimer_set(&hub->ev_timer, timer_statistics, hub);
|
||||
event_base_set(hub->evbase, &hub->ev_timer);
|
||||
evtimer_add(&hub->ev_timer, &timeout);
|
||||
}
|
||||
|
||||
@@ -97,6 +98,7 @@ int user_manager_init(struct hub_info* hub)
|
||||
hub->users = users;
|
||||
|
||||
evtimer_set(&hub->ev_timer, timer_statistics, hub);
|
||||
event_base_set(hub->evbase, &hub->ev_timer);
|
||||
evtimer_add(&hub->ev_timer, &timeout);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user