Compare commits
282 Commits
0.2.7
...
0.3.0-fina
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59f67f9e17 | ||
|
|
bfc7c184a7 | ||
|
|
92c448b855 | ||
|
|
304ecda16a | ||
|
|
c670d13369 | ||
|
|
b2e7a2848f | ||
|
|
84bd2591d6 | ||
|
|
4aa65733d0 | ||
|
|
230ca28b46 | ||
|
|
5f353ebe28 | ||
|
|
68a26e1160 | ||
|
|
4711d26c11 | ||
|
|
1bb3bd90c1 | ||
|
|
5e253e8442 | ||
|
|
26a2a384b6 | ||
|
|
8b7f09c7f4 | ||
|
|
b29da11f3b | ||
|
|
78ad9b8572 | ||
|
|
b04a20c66e | ||
|
|
21a5981905 | ||
|
|
c47ea14047 | ||
|
|
9f5aaf0148 | ||
|
|
f645811f37 | ||
|
|
2f09fcea84 | ||
|
|
5e679e2d4f | ||
|
|
efeb36c492 | ||
|
|
7e60919596 | ||
|
|
e7cb4cd277 | ||
|
|
e45511827f | ||
|
|
80c6ad9d76 | ||
|
|
f84f3b1fff | ||
|
|
f84073f7cc | ||
|
|
af8421fa47 | ||
|
|
001fa6739a | ||
|
|
7dfa3162cb | ||
|
|
314707f499 | ||
|
|
1c58a85de1 | ||
|
|
eb5d3936a9 | ||
|
|
23daa42b72 | ||
|
|
a643bb123d | ||
|
|
6a4b9c58f4 | ||
|
|
f35b2c35cb | ||
|
|
b3aa5332c8 | ||
|
|
1f9cfe58c4 | ||
|
|
50f5ce9d32 | ||
|
|
4a4de0f2ac | ||
|
|
2e2f93d0a4 | ||
|
|
7c093dc5da | ||
|
|
8bcdbdc061 | ||
|
|
53536f191d | ||
|
|
e994f23ea0 | ||
|
|
f489e77602 | ||
|
|
2526c8a9ed | ||
|
|
4556623a87 | ||
|
|
828c19cfc6 | ||
|
|
00995a1946 | ||
|
|
f4e82ef503 | ||
|
|
eb7bec585e | ||
|
|
066b3f5243 | ||
|
|
a51e8830ce | ||
|
|
e6cc47070c | ||
|
|
0c5d98c671 | ||
|
|
0f30ebf045 | ||
|
|
4919c8f2f9 | ||
|
|
2910c571b0 | ||
|
|
11e5683a79 | ||
|
|
17416def35 | ||
|
|
234f04f495 | ||
|
|
4898454c91 | ||
|
|
255255ff20 | ||
|
|
f50b40fb92 | ||
|
|
40fc09a7ef | ||
|
|
582bb58ff9 | ||
|
|
7472e9ae4a | ||
|
|
f076c67c33 | ||
|
|
7eef032584 | ||
|
|
de793ea9d2 | ||
|
|
20ef6f8e53 | ||
|
|
310ddf4e9d | ||
|
|
8592aa80d4 | ||
|
|
c3ba8d52ee | ||
|
|
69f6d811bf | ||
|
|
a53c219f05 | ||
|
|
96ce64ee07 | ||
|
|
a3d6646b99 | ||
|
|
4fd182c55e | ||
|
|
993d9ae33b | ||
|
|
fe9b48f14c | ||
|
|
1e380ecf0f | ||
|
|
af7b52b708 | ||
|
|
aeba7188c0 | ||
|
|
4ca5caa52b | ||
|
|
5203ecdadc | ||
|
|
11b288a669 | ||
|
|
52cbec24b6 | ||
|
|
29d6d59d36 | ||
|
|
96c006ee13 | ||
|
|
e67587b997 | ||
|
|
1af613d9ce | ||
|
|
5bd258ca52 | ||
|
|
86ba3ca86f | ||
|
|
cfb450c3fc | ||
|
|
cd389742e6 | ||
|
|
4afa187f9a | ||
|
|
b3c40f7443 | ||
|
|
d45383fe4d | ||
|
|
1642b8dfbe | ||
|
|
ffd455474a | ||
|
|
5baf277f5a | ||
|
|
ab6eda2bbb | ||
|
|
c17ae06b93 | ||
|
|
faaa3d14e4 | ||
|
|
9f784e808e | ||
|
|
b9adaa4f8b | ||
|
|
80dee0c020 | ||
|
|
604c2d8836 | ||
|
|
eca0f931bd | ||
|
|
fbe98d6011 | ||
|
|
4a5993ccc2 | ||
|
|
a297c08bba | ||
|
|
7229ad68ab | ||
|
|
22f9be95d0 | ||
|
|
4e43746c1b | ||
|
|
27c71a75ee | ||
|
|
1d363ed0b4 | ||
|
|
79cf5ee9db | ||
|
|
d2da49d41a | ||
|
|
4666311516 | ||
|
|
1f24bd6812 | ||
|
|
0e1cd903da | ||
|
|
4931dc5dcb | ||
|
|
077bffd74f | ||
|
|
cfb8a9f322 | ||
|
|
653cfb285e | ||
|
|
053fb30192 | ||
|
|
9bd0286c01 | ||
|
|
b78d48795b | ||
|
|
9b58ba6516 | ||
|
|
f322fbb197 | ||
|
|
80348044c3 | ||
|
|
31d4b0f0b1 | ||
|
|
10615a9a1a | ||
|
|
7b44036480 | ||
|
|
112fa2f845 | ||
|
|
945c6be14c | ||
|
|
099ed6dbe2 | ||
|
|
444f991f44 | ||
|
|
b125ffe3c1 | ||
|
|
76b84499bc | ||
|
|
6358c7f9cd | ||
|
|
041ce7a1fb | ||
|
|
78bb1d3527 | ||
|
|
48f3cae22b | ||
|
|
817250c528 | ||
|
|
f176e790e2 | ||
|
|
367871e476 | ||
|
|
2ac5cc19cb | ||
|
|
6e5d28c2d4 | ||
|
|
5048ff9ae5 | ||
|
|
36a07e3f7e | ||
|
|
e281f61472 | ||
|
|
bb27ff617c | ||
|
|
dc90245ade | ||
|
|
0ec4913e15 | ||
|
|
61d639bfa2 | ||
|
|
adeaf23f9c | ||
|
|
03d3ffd20f | ||
|
|
bbae2603b0 | ||
|
|
5f0c84f46a | ||
|
|
b02618d19c | ||
|
|
927faf70fc | ||
|
|
4a173bf066 | ||
|
|
abd097acf0 | ||
|
|
3fdbccb028 | ||
|
|
3db2ec5e22 | ||
|
|
e68cbd0cde | ||
|
|
98bffd93e9 | ||
|
|
e53119e92c | ||
|
|
ee0ea5a427 | ||
|
|
aa7be1dc4b | ||
|
|
0ef248759c | ||
|
|
5b5d9faf96 | ||
|
|
1f7f6a43f9 | ||
|
|
3cf005a08e | ||
|
|
f3c5fced47 | ||
|
|
9c49f07826 | ||
|
|
0b59941102 | ||
|
|
5f3f2d4f4d | ||
|
|
a3d4c8dbc3 | ||
|
|
7e3a26b0f3 | ||
|
|
9ebb38ae15 | ||
|
|
5b29a3b403 | ||
|
|
75c4272a1b | ||
|
|
f272280faf | ||
|
|
b1d4b1288f | ||
|
|
ddba669af0 | ||
|
|
2ad2b66db3 | ||
|
|
ca33461851 | ||
|
|
4abdc3edbd | ||
|
|
90078ad4b6 | ||
|
|
3bd5c36455 | ||
|
|
8f0943621e | ||
|
|
155350b61b | ||
|
|
1c58120c03 | ||
|
|
1d9acece34 | ||
|
|
57fd872f14 | ||
|
|
ca7544df9b | ||
|
|
c7777e2624 | ||
|
|
b2d543d433 | ||
|
|
ff47281197 | ||
|
|
ddc91d1640 | ||
|
|
0d084a5e14 | ||
|
|
e6e26a02e7 | ||
|
|
ea3cd1bd90 | ||
|
|
7f24238ab5 | ||
|
|
1d2d4e74f6 | ||
|
|
5250fdaf57 | ||
|
|
7ddfd52dc7 | ||
|
|
513ab422f2 | ||
|
|
0eb91763fa | ||
|
|
507f429035 | ||
|
|
b1ab64242e | ||
|
|
8b90f79bac | ||
|
|
8f7cc0b7a7 | ||
|
|
4812a5968b | ||
|
|
8b5bfdd922 | ||
|
|
860310caff | ||
|
|
8167d79f5a | ||
|
|
9706a0a501 | ||
|
|
9309c925d3 | ||
|
|
9a3a5bc2de | ||
|
|
e382e24337 | ||
|
|
be4ff1d8cc | ||
|
|
aa18ac047d | ||
|
|
e120d5c76d | ||
|
|
6853e92f89 | ||
|
|
1dbf2640d2 | ||
|
|
caec28f63f | ||
|
|
5ea5efb875 | ||
|
|
326fcc467c | ||
|
|
078470ce64 | ||
|
|
82ac450b4b | ||
|
|
b4f24b21f9 | ||
|
|
968266b22f | ||
|
|
548867de10 | ||
|
|
953db2dcbc | ||
|
|
6e4ac1355f | ||
|
|
604364ffa5 | ||
|
|
6cc1af5671 | ||
|
|
15e2053699 | ||
|
|
df1e832a3c | ||
|
|
4c4cb4fb36 | ||
|
|
e49f9d4d30 | ||
|
|
e73a931243 | ||
|
|
ae017af758 | ||
|
|
7a02f92a4c | ||
|
|
adeebb92a0 | ||
|
|
26f1864c1f | ||
|
|
c14fa3c3a9 | ||
|
|
dfd2191103 | ||
|
|
0042344054 | ||
|
|
8af965c0ca | ||
|
|
b3d3dcdb44 | ||
|
|
cb6190c5b4 | ||
|
|
82ad62602d | ||
|
|
6d34bdd7e2 | ||
|
|
c2832e59c0 | ||
|
|
cf23c82c16 | ||
|
|
8bf0fd424a | ||
|
|
8f8284eb29 | ||
|
|
015c3368bd | ||
|
|
9186b441aa | ||
|
|
74af392e80 | ||
|
|
3a270564d2 | ||
|
|
f0938e8afa | ||
|
|
156c137237 | ||
|
|
7aeb8651ba | ||
|
|
ab4eb6db3d | ||
|
|
13a8700554 | ||
|
|
5d5dda2f9b | ||
|
|
95b741bb5e | ||
|
|
90abf64e3a |
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
*~
|
||||
*.[oa]
|
||||
*.exe
|
||||
*.gch
|
||||
uhub-admin
|
||||
adcrush
|
||||
uhub
|
||||
|
||||
18
ChangeLog
18
ChangeLog
@@ -1,3 +1,21 @@
|
||||
0.3.0:
|
||||
- More user commands: ban, broadcast, mute, rules, history, myip, whoip, log
|
||||
- Experimental SSL support
|
||||
- Large rewrite of the network stack in order to support SSL.
|
||||
- Added rule file for defining hub rules.
|
||||
- Many crash fixes and other important bug fixes.
|
||||
- Optimizations: O(1) timeout scheduler
|
||||
- New sid allocation code.
|
||||
- Added configurable server_listen_backlog (default 50).
|
||||
- Added init.d scripts for RedHat/CentOS
|
||||
|
||||
|
||||
0.2.8:
|
||||
- Fix bug #13: getsockname() failure, use sockaddr from accept() instead.
|
||||
- Fix bug #10: Improve logging, ensure logs are machine readable.
|
||||
- Fix bug #12: asserts in adc_msg_parse -> enabled strict utf8 parsing.
|
||||
|
||||
|
||||
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.
|
||||
|
||||
176
GNUmakefile
176
GNUmakefile
@@ -1,6 +1,6 @@
|
||||
##
|
||||
## Makefile for uhub (Use GNU make)
|
||||
## Copyright (C) 2007-2008, Jan Vidar Krey <janvidar@extatic.org>
|
||||
## Copyright (C) 2007-2010, Jan Vidar Krey <janvidar@extatic.org>
|
||||
#
|
||||
|
||||
CC = gcc
|
||||
@@ -8,12 +8,10 @@ LD := $(CC)
|
||||
MV := mv
|
||||
RANLIB := ranlib
|
||||
CFLAGS += -pipe -Wall
|
||||
USE_PCH ?= YES
|
||||
USE_SSL ?= NO
|
||||
USE_BIGENDIAN ?= AUTO
|
||||
BITS ?= AUTO
|
||||
SILENT ?= YES
|
||||
LDLIBS += -levent
|
||||
TERSE ?= NO
|
||||
STACK_PROTECT ?= NO
|
||||
|
||||
@@ -21,6 +19,12 @@ ifeq ($(OS), Windows_NT)
|
||||
WINDOWS ?= YES
|
||||
endif
|
||||
|
||||
ifeq (SOLARIS,YES)
|
||||
LDLIBS += -lsocket -lnsl
|
||||
endif
|
||||
|
||||
CFLAGS += -I./src/
|
||||
|
||||
ifeq ($(WINDOWS),YES)
|
||||
USE_BIGENDIAN := NO
|
||||
LDLIBS += -lws2_32
|
||||
@@ -29,6 +33,7 @@ UHUB_PREFIX ?= c:/uhub/
|
||||
CFLAGS += -mno-cygwin
|
||||
LDFLAGS += -mno-cygwin
|
||||
BIN_EXT ?= .exe
|
||||
GIT_REVISION ?= NO
|
||||
else
|
||||
DESTDIR ?= /
|
||||
UHUB_CONF_DIR ?= $(DESTDIR)/etc/uhub
|
||||
@@ -40,19 +45,16 @@ endif
|
||||
|
||||
ifeq ($(SILENT),YES)
|
||||
MSG_CC=@echo " CC:" $(notdir $^) &&
|
||||
MSG_PCH=@echo " PCH:" $(notdir $@) &&
|
||||
MSG_LD=@echo " LD:" $(notdir $@) &&
|
||||
MSG_AR=@echo " AR:" $(notdir $@) &&
|
||||
else
|
||||
MSG_CC=
|
||||
MSG_PCH=
|
||||
MSG_LD=
|
||||
MSG_AR=
|
||||
endif
|
||||
|
||||
ifeq ($(TERSE), YES)
|
||||
MSG_CC=@
|
||||
MSG_PCH=@
|
||||
MSG_LD=@
|
||||
MSG_AR=@
|
||||
MSG_CLEAN=-n ""
|
||||
@@ -60,17 +62,12 @@ else
|
||||
MSG_CLEAN="Clean as a whistle"
|
||||
endif
|
||||
|
||||
|
||||
|
||||
|
||||
CFLAGS += -I/source/libevent
|
||||
LDFLAGS += -L/source/libevent
|
||||
|
||||
-include release_setup.mk
|
||||
ifeq ($(RELEASE),YES)
|
||||
CFLAGS += -Os -DNDEBUG
|
||||
CFLAGS += -O3 -DNDEBUG
|
||||
GIT_REVISION ?= NO
|
||||
else
|
||||
CFLAGS += -g -DDEBUG
|
||||
CFLAGS += -ggdb -DDEBUG
|
||||
GIT_REVISION ?= YES
|
||||
endif
|
||||
|
||||
ifeq ($(STACK_PROTECT),YES)
|
||||
@@ -88,13 +85,6 @@ CFLAGS += -finstrument-functions
|
||||
CFLAGS += -DDEBUG_FUNCTION_TRACE
|
||||
endif
|
||||
|
||||
ifeq ($(USE_PCH),YES)
|
||||
PCHSRC=src/uhub.h
|
||||
PCH=src/uhub.h.gch
|
||||
else
|
||||
PCH=
|
||||
endif
|
||||
|
||||
ifneq ($(BITS), AUTO)
|
||||
ifeq ($(BITS), 64)
|
||||
CFLAGS += -m64
|
||||
@@ -122,61 +112,52 @@ CFLAGS += -DSSL_SUPPORT
|
||||
LDLIBS += -lssl
|
||||
endif
|
||||
|
||||
ifneq ($(LIBEVENT_PATH),)
|
||||
CFLAGS += -I$(LIBEVENT_PATH)
|
||||
LDFLAGS += -L$(LIBEVENT_PATH)
|
||||
ifeq ($(GIT_REVISION),YES)
|
||||
CFLAGS += -DGIT_REVISION=\"$(shell git show --abbrev-commit | head -n 1 | cut -f 2 -d " ")\"
|
||||
endif
|
||||
|
||||
# Sources
|
||||
libuhub_SOURCES := \
|
||||
src/auth.c \
|
||||
src/commands.c \
|
||||
src/config.c \
|
||||
src/eventqueue.c \
|
||||
src/hubevent.c \
|
||||
src/hub.c \
|
||||
src/inf.c \
|
||||
src/ipcalc.c \
|
||||
src/list.c \
|
||||
src/log.c \
|
||||
src/memory.c \
|
||||
src/message.c \
|
||||
src/misc.c \
|
||||
src/netevent.c \
|
||||
src/network.c \
|
||||
src/route.c \
|
||||
src/sid.c \
|
||||
src/tiger.c \
|
||||
src/user.c \
|
||||
src/usermanager.c
|
||||
src/core/auth.c \
|
||||
src/core/commands.c \
|
||||
src/core/config.c \
|
||||
src/core/eventqueue.c \
|
||||
src/core/hub.c \
|
||||
src/core/hubevent.c \
|
||||
src/core/hubio.c \
|
||||
src/core/inf.c \
|
||||
src/core/netevent.c \
|
||||
src/core/probe.c \
|
||||
src/core/route.c \
|
||||
src/core/user.c \
|
||||
src/core/usermanager.c \
|
||||
src/network/backend.c \
|
||||
src/network/connection.c \
|
||||
src/network/epoll.c \
|
||||
src/network/network.c \
|
||||
src/network/select.c \
|
||||
src/network/timer.c \
|
||||
src/util/ipcalc.c \
|
||||
src/util/list.c \
|
||||
src/util/log.c \
|
||||
src/util/memory.c \
|
||||
src/util/misc.c \
|
||||
src/util/rbtree.c \
|
||||
src/util/timeout.c \
|
||||
src/util/tiger.c
|
||||
|
||||
uhub_SOURCES := src/main.c
|
||||
libadc_common_SOURCES := \
|
||||
src/adc/message.c \
|
||||
src/adc/sid.c
|
||||
|
||||
adcrush_SOURCES := src/adcrush.c
|
||||
libadc_client_SOURCES := \
|
||||
src/tools/adcclient.c
|
||||
|
||||
uhub_HEADERS := \
|
||||
src/adcconst.h \
|
||||
src/auth.h \
|
||||
src/config.h \
|
||||
src/eventid.h \
|
||||
src/eventqueue.h \
|
||||
src/hubevent.h \
|
||||
src/hub.h \
|
||||
src/inf.h \
|
||||
src/ipcalc.h \
|
||||
src/list.h \
|
||||
src/log.h \
|
||||
src/memory.h \
|
||||
src/message.h \
|
||||
src/misc.h \
|
||||
src/netevent.h \
|
||||
src/network.h \
|
||||
src/route.h \
|
||||
src/sid.h \
|
||||
src/tiger.h \
|
||||
src/uhub.h \
|
||||
src/user.h \
|
||||
src/usermanager.h
|
||||
uhub_SOURCES := src/core/main.c
|
||||
|
||||
adcrush_SOURCES := src/tools/adcrush.c
|
||||
|
||||
admin_SOURCES := src/tools/admin.c
|
||||
|
||||
autotest_SOURCES := \
|
||||
autotest/test_message.tcc \
|
||||
@@ -187,57 +168,49 @@ autotest_SOURCES := \
|
||||
autotest/test_hub.tcc \
|
||||
autotest/test_misc.tcc \
|
||||
autotest/test_tiger.tcc \
|
||||
autotest/test_usermanager.tcc \
|
||||
autotest/test_eventqueue.tcc
|
||||
|
||||
autotest_OBJECTS = autotest.o
|
||||
|
||||
# Source to objects
|
||||
libuhub_OBJECTS := $(libuhub_SOURCES:.c=.o)
|
||||
uhub_OBJECTS := $(uhub_SOURCES:.c=.o)
|
||||
adcrush_OBJECTS := $(adcrush_SOURCES:.c=.o)
|
||||
libuhub_OBJECTS := $(libuhub_SOURCES:.c=.o)
|
||||
libadc_client_OBJECTS := $(libadc_client_SOURCES:.c=.o)
|
||||
libadc_common_OBJECTS := $(libadc_common_SOURCES:.c=.o)
|
||||
|
||||
all_OBJECTS := $(libuhub_OBJECTS) $(uhub_OBJECTS) $(adcrush_OBJECTS) $(autotest_OBJECTS)
|
||||
uhub_OBJECTS := $(uhub_SOURCES:.c=.o)
|
||||
adcrush_OBJECTS := $(adcrush_SOURCES:.c=.o)
|
||||
admin_OBJECTS := $(admin_SOURCES:.c=.o)
|
||||
|
||||
all_OBJECTS := $(libuhub_OBJECTS) $(uhub_OBJECTS) $(adcrush_OBJECTS) $(autotest_OBJECTS) $(admin_OBJECTS) $(libadc_common_OBJECTS) $(libadc_client_OBJECTS)
|
||||
|
||||
LIBUHUB=libuhub.a
|
||||
uhub_BINARY=uhub$(BIN_EXT)
|
||||
adcrush_BINARY=adcrush$(BIN_EXT)
|
||||
admin_BINARY=uhub-admin$(BIN_EXT)
|
||||
autotest_BINARY=autotest/test$(BIN_EXT)
|
||||
|
||||
%.o: %.c
|
||||
$(MSG_CC) $(CC) -c $(CFLAGS) -o $@.tmp $^ && \
|
||||
$(MV) $@.tmp $@
|
||||
$(MSG_CC) $(CC) -c $(CFLAGS) -o $@ $^
|
||||
|
||||
all: $(uhub_BINARY) $(PCH)
|
||||
all: $(uhub_BINARY)
|
||||
|
||||
$(adcrush_BINARY): $(PCH) $(LIBUHUB) $(adcrush_OBJECTS)
|
||||
$(MSG_LD) $(CC) -o $@.tmp $(adcrush_OBJECTS) $(LIBUHUB) $(LDFLAGS) $(LDLIBS) && \
|
||||
$(MV) $@.tmp $@
|
||||
$(adcrush_BINARY): $(adcrush_OBJECTS) $(libuhub_OBJECTS) $(libadc_common_OBJECTS) $(libadc_client_OBJECTS)
|
||||
$(MSG_LD) $(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
$(uhub_BINARY): $(PCH) $(LIBUHUB) $(uhub_OBJECTS)
|
||||
$(MSG_LD) $(CC) -o $@.tmp $(uhub_OBJECTS) $(LIBUHUB) $(LDFLAGS) $(LDLIBS) && \
|
||||
$(MV) $@.tmp $@
|
||||
$(admin_BINARY): $(admin_OBJECTS) $(libuhub_OBJECTS) $(libadc_common_OBJECTS) $(libadc_client_OBJECTS)
|
||||
$(MSG_LD) $(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
$(LIBUHUB): $(libuhub_OBJECTS)
|
||||
$(MSG_AR) $(AR) rc $@.tmp $^ && \
|
||||
$(RANLIB) $@.tmp && \
|
||||
$(MV) $@.tmp $@
|
||||
|
||||
ifeq ($(USE_PCH),YES)
|
||||
$(PCH): $(uhub_HEADERS)
|
||||
$(MSG_PCH) $(CC) $(CFLAGS) -o $@.tmp $(PCHSRC) && \
|
||||
$(MV) $@.tmp $@
|
||||
endif
|
||||
$(uhub_BINARY): $(uhub_OBJECTS) $(libuhub_OBJECTS) $(libadc_common_OBJECTS)
|
||||
$(MSG_LD) $(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
autotest.c: $(autotest_SOURCES)
|
||||
$(shell exotic --standalone $(autotest_SOURCES) > $@)
|
||||
|
||||
$(autotest_OBJECTS): autotest.c
|
||||
$(MSG_CC) $(CC) -c $(CFLAGS) -Isrc -o $@.tmp $< && \
|
||||
$(MV) $@.tmp $@
|
||||
$(MSG_CC) $(CC) -c $(CFLAGS) -Isrc -o $@ $<
|
||||
|
||||
$(autotest_BINARY): $(autotest_OBJECTS) $(LIBUHUB)
|
||||
$(MSG_LD) $(CC) -o $@.tmp $^ $(LDFLAGS) $(LDLIBS) && \
|
||||
$(MV) $@.tmp $@
|
||||
$(autotest_BINARY): $(autotest_OBJECTS) $(libuhub_OBJECTS) $(libadc_common_OBJECTS) $(libadc_client_OBJECTS)
|
||||
$(MSG_LD) $(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
autotest: $(autotest_BINARY)
|
||||
@./$(autotest_BINARY) -s -f
|
||||
@@ -257,11 +230,10 @@ install: $(uhub_BINARY)
|
||||
endif
|
||||
|
||||
dist-clean:
|
||||
@rm -rf $(all_OBJECTS) $(PCH) *~ core
|
||||
@rm -rf $(all_OBJECTS) *~ core
|
||||
|
||||
clean:
|
||||
@rm -rf $(libuhub_OBJECTS) $(PCH) *~ core $(uhub_BINARY) $(LIBUHUB) $(all_OBJECTS) && \
|
||||
@rm -rf $(libuhub_OBJECTS) *~ core $(uhub_BINARY) $(admin_BINARY) $(autotest_BINARY) $(adcrush_BINARY) $(all_OBJECTS) autotest.c && \
|
||||
echo $(MSG_CLEAN)
|
||||
|
||||
-include release_targets.mk
|
||||
|
||||
|
||||
2
README
2
README
@@ -1,5 +1,5 @@
|
||||
Welcome and thanks for downloading uHub, a high performance ADC p2p hub.
|
||||
|
||||
For the official documentation, bugs and other information, please visit:
|
||||
http://www.extatic.org/uhub/
|
||||
http://www.uhub.org/
|
||||
|
||||
|
||||
@@ -6,25 +6,30 @@ else
|
||||
HOST_MACHINE=`uname -m | tr [:upper:] [:lower:] | sed s/i686/i386/ | sed s/x86_64/amd64/ | sed s/ppc64/powerpc/`
|
||||
fi
|
||||
|
||||
BINSUFFIX=
|
||||
MAKEARGS=
|
||||
MAKE=make
|
||||
WANTZIP=0
|
||||
|
||||
if [ "${HOST_SYSTEM}" = "mingw32_nt-5.1" ]; then
|
||||
HOST_SYSTEM=win32
|
||||
BINARY=uhub.exe
|
||||
BINSUFFIX=.exe
|
||||
WANTZIP=1
|
||||
else
|
||||
WANTZIP=0
|
||||
BINARY=uhub
|
||||
MAKEARGS="USE_BIGENDIAN=NO"
|
||||
fi
|
||||
|
||||
BINARY=uhub${BINSUFFIX}
|
||||
|
||||
if [ "${HOST_SYSTEM}" = "freebsd" ]; then
|
||||
MAKE=gmake
|
||||
fi
|
||||
|
||||
VERSION=`grep define\ VERSION version.h | cut -f 3 -d " " | tr -d [=\"=]`
|
||||
SNAPSHOT=`date '+%Y%m%d'`
|
||||
PACKAGE=uhub-${VERSION}
|
||||
PACKAGE_SRC=${PACKAGE}-src
|
||||
PACKAGE_BIN=${PACKAGE}-${HOST_SYSTEM}-${HOST_MACHINE}
|
||||
|
||||
URL_ARCHIVE='build-archive:~/uhub/'
|
||||
URL_PUBLISH='domeneshop:~/www/downloads/uhub/'
|
||||
URL_SNAPSHOT='domeneshop:~/www/downloads/uhub/snapshots/'
|
||||
ARCHIVE='build-archive:~/www/downloads/uhub/'
|
||||
|
||||
function export_source_directory
|
||||
{
|
||||
@@ -51,45 +56,85 @@ function package_zips
|
||||
gzip -c -9 $1.tar > $1.tar.gz
|
||||
bzip2 -c -9 $1.tar > $1.tar.bz2
|
||||
rm -f $1.tar
|
||||
zip -q -9 -r $1.zip $2
|
||||
|
||||
if [ $WANTZIP -eq 1 ]; then
|
||||
zip -q -9 -r $1.zip $2
|
||||
fi
|
||||
}
|
||||
|
||||
function export_sources
|
||||
{
|
||||
export_source_directory
|
||||
make autotest.c && cp autotest.c ${PACKAGE}/autotest.c
|
||||
rm -Rf ${PACKAGE}/admin
|
||||
if [ ! -d ${PACKAGE} ]; then
|
||||
export_source_directory
|
||||
fi
|
||||
|
||||
cd ${PACKAGE}
|
||||
${MAKE} ${MAKEARGS} autotest.c
|
||||
cd ..
|
||||
|
||||
if [ ! -f ${PACKAGE}/autotest.c ]; then
|
||||
echo "Unable to create autotest.c, aborting..."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
rm -Rf ${PACKAGE}/admin
|
||||
package_zips ${PACKAGE_SRC} ${PACKAGE}
|
||||
|
||||
|
||||
rm -Rf ${PACKAGE};
|
||||
cp ChangeLog ChangeLog-${VERSION}
|
||||
}
|
||||
|
||||
function build_binaries
|
||||
{
|
||||
if [ ! -d ${PACKAGE} ]; then
|
||||
export_source_directory
|
||||
fi
|
||||
|
||||
cd ${PACKAGE}
|
||||
${MAKE} ${MAKEARGS} RELEASE=YES
|
||||
cd ..
|
||||
|
||||
if [ ! -x ${PACKAGE}/${BINARY} ]; then
|
||||
echo "Build failed, no binary found..."
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
function export_binaries
|
||||
{
|
||||
export_source_directory
|
||||
build_binaries
|
||||
|
||||
rm -Rf ${PACKAGE}/admin
|
||||
rm -Rf ${PACKAGE}/autotest
|
||||
rm -Rf ${PACKAGE}/src
|
||||
rm -Rf ${PACKAGE}/debian
|
||||
rm -f ${PACKAGE}/autotest.c
|
||||
rm -f ${PACKAGE}/*akefile
|
||||
rm -f ${PACKAGE}/version.h
|
||||
rm -f ${PACKAGE}/doc/Doxyfile
|
||||
rm -f ${PACKAGE}/doc/uhub.dot
|
||||
|
||||
make
|
||||
|
||||
if [ -x ${BINARY} ]; then
|
||||
cp ${BINARY} ${PACKAGE}
|
||||
else
|
||||
echo "No binary found!"
|
||||
exit 1
|
||||
fi
|
||||
rm -f ${PACKAGE}/libuhub*
|
||||
|
||||
package_zips ${PACKAGE_BIN} ${PACKAGE}
|
||||
rm -Rf ${PACKAGE}
|
||||
}
|
||||
|
||||
rm -Rf ${PACKAGE};
|
||||
function upload_pkg
|
||||
{
|
||||
if [ -f $1 ]; then
|
||||
scp $1 ${ARCHIVE}
|
||||
fi
|
||||
}
|
||||
|
||||
function upload_packages
|
||||
{
|
||||
upload_pkg ${PACKAGE_SRC}.tar.gz
|
||||
upload_pkg ${PACKAGE_SRC}.tar.bz2
|
||||
upload_pkg ${PACKAGE_SRC}.zip
|
||||
upload_pkg ChangeLog-${VERSION}
|
||||
upload_pkg ${PACKAGE_BIN}.tar.gz
|
||||
upload_pkg ${PACKAGE_BIN}.tar.bz2
|
||||
upload_pkg ${PACKAGE_BIN}.zip
|
||||
}
|
||||
|
||||
|
||||
|
||||
3
admin/export.sh
Executable file
3
admin/export.sh
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
. admin/common.sh
|
||||
export_source_directory
|
||||
95
admin/make_pkg_deb.sh
Executable file
95
admin/make_pkg_deb.sh
Executable file
@@ -0,0 +1,95 @@
|
||||
#!/bin/sh
|
||||
. admin/common.sh
|
||||
|
||||
export_source_directory
|
||||
build_binaries
|
||||
|
||||
DEB_REVISION=1
|
||||
|
||||
if [ -d deb ]; then
|
||||
rm -Rf deb
|
||||
fi
|
||||
|
||||
mkdir -p \
|
||||
deb/DEBIAN \
|
||||
deb/usr/bin \
|
||||
deb/usr/share/man/man1/ \
|
||||
deb/usr/share/doc/uhub \
|
||||
deb/etc/uhub \
|
||||
|| exit 1
|
||||
|
||||
find deb -type d | xargs chmod 755
|
||||
|
||||
# Copy binaries...
|
||||
cp ${PACKAGE}/${BINARY} deb/usr/bin
|
||||
strip deb/usr/bin/${BINARY}
|
||||
|
||||
# Copy configuration files...
|
||||
cp ${PACKAGE}/doc/uhub.conf deb/etc/uhub
|
||||
cp ${PACKAGE}/doc/users.conf deb/etc/uhub
|
||||
echo "Welcome to uHub" > deb/etc/uhub/motd.txt
|
||||
|
||||
# Copy other files
|
||||
cp ${PACKAGE}/README deb/usr/share/doc/uhub
|
||||
cp ${PACKAGE}/AUTHORS deb/usr/share/doc/uhub
|
||||
gzip -c --best < ${PACKAGE}/ChangeLog > deb/usr/share/doc/uhub/changelog.gz
|
||||
gzip -c --best < ${PACKAGE}/doc/uhub.1 > deb/usr/share/man/man1/uhub.1.gz
|
||||
|
||||
cat > deb/usr/share/doc/uhub/copyright <<EOF
|
||||
uHub - a high performance hub for the ADC peer-to-peer network
|
||||
|
||||
Copyright (C) 2007-2009 Jan Vidar Krey <janvidar@extatic.org>
|
||||
|
||||
uHub is free and open source software, licensed under the
|
||||
GNU General Public License version 3.
|
||||
|
||||
For details, see /usr/share/common-licenses/GPL-3
|
||||
EOF
|
||||
|
||||
gzip -c --best > deb/usr/share/doc/uhub/changelog.Debian.gz <<EOF
|
||||
uhub (${VERSION}) stable; urgency=low
|
||||
|
||||
* See changelog.gz for details.
|
||||
|
||||
-- Jan Vidar Krey <janvidar@extatic.org> `date -R`
|
||||
EOF
|
||||
|
||||
### Write control files
|
||||
cd deb
|
||||
echo "/etc/uhub/uhub.conf" > DEBIAN/conffiles
|
||||
echo "/etc/uhub/users.conf" >> DEBIAN/conffiles
|
||||
echo "/etc/uhub/motd.txt" >> DEBIAN/conffiles
|
||||
|
||||
md5sum `find usr -type f` > DEBIAN/md5sums
|
||||
|
||||
INSTALL_SIZE=`du -s | cut -f 1`
|
||||
|
||||
cat > DEBIAN/control <<EOF
|
||||
Package: uhub
|
||||
Version: ${VERSION}-${DEB_REVISION}
|
||||
Architecture: ${HOST_MACHINE}
|
||||
Maintainer: Jan Vidar Krey <janvidar@extatic.org>
|
||||
Installed-Size: ${INSTALL_SIZE}
|
||||
Depends: libc6 (>= 2.7-1), libevent1 (>= 1.3e-1)
|
||||
Section: net
|
||||
Priority: optional
|
||||
Description: a high performance hub for the ADC peer-to-peer network
|
||||
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.
|
||||
.
|
||||
Homepage: http://www.extatic.org/uhub/
|
||||
EOF
|
||||
cd ..
|
||||
|
||||
### Create deb file
|
||||
fakeroot dpkg-deb --build deb
|
||||
mv deb.deb uhub_${VERSION}-${DEB_REVISION}_${HOST_MACHINE}.deb
|
||||
|
||||
### Check for errors
|
||||
lintian uhub_${VERSION}-${DEB_REVISION}_${HOST_MACHINE}.deb
|
||||
|
||||
### Cleanup
|
||||
rm -Rf deb
|
||||
rm -Rf ${PACKAGE}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# set -x
|
||||
|
||||
ME=`dirname $0`
|
||||
. ${ME}/common.sh
|
||||
|
||||
# Git Export
|
||||
export_sources
|
||||
export_binaries
|
||||
|
||||
3
admin/release_binaries.sh
Executable file
3
admin/release_binaries.sh
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
. admin/common.sh
|
||||
export_binaries
|
||||
4
admin/release_sources.sh
Executable file
4
admin/release_sources.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
. admin/common.sh
|
||||
WANTZIP=1
|
||||
export_sources
|
||||
26
admin/setup_archive.sh
Executable file
26
admin/setup_archive.sh
Executable file
@@ -0,0 +1,26 @@
|
||||
#!/bin/bash
|
||||
|
||||
PUB="${HOME}/.ssh/id_rsa.pub"
|
||||
CFG="${HOME}/.ssh/config"
|
||||
|
||||
if [ ! "`grep build-archive ${CFG}`" ]; then
|
||||
echo "Updating ssh config (${CFG})..."
|
||||
cat >> ${CFG} <<EOF
|
||||
|
||||
Host build-archive
|
||||
ForwardX11 no
|
||||
HostName login.domeneshop.no
|
||||
User extatic
|
||||
EOF
|
||||
else
|
||||
echo "ssh config seems OK (${CFG})"
|
||||
fi
|
||||
|
||||
if [ ! -f ${PUB} ]; then
|
||||
echo "No id_rsa.pub - run ssh-keygen"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Copying public key (${PUB})..."
|
||||
cat ${PUB} | ssh build-archive "cat >> .ssh/authorized_keys"
|
||||
|
||||
3
admin/upload.sh
Executable file
3
admin/upload.sh
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
. admin/common.sh
|
||||
upload_packages
|
||||
@@ -10,8 +10,8 @@ static void create_test_user()
|
||||
if (g_user)
|
||||
return;
|
||||
|
||||
g_user = (struct user*) malloc(sizeof(struct user));
|
||||
memset(g_user, 0, sizeof(struct user));
|
||||
g_user = (struct hub_user*) malloc(sizeof(struct hub_user));
|
||||
memset(g_user, 0, sizeof(struct hub_user));
|
||||
memcpy(g_user->id.nick, "exotic-tester", 13);
|
||||
g_user->sid = 1;
|
||||
}
|
||||
@@ -23,6 +23,7 @@ EXO_TEST(hub_net_startup, {
|
||||
|
||||
EXO_TEST(hub_config_initialize, {
|
||||
config_defaults(&g_config);
|
||||
g_config.server_port = 15111;
|
||||
return 1;
|
||||
});
|
||||
|
||||
@@ -67,5 +68,5 @@ EXO_TEST(hub_service_shutdown, {
|
||||
});
|
||||
|
||||
EXO_TEST(hub_net_shutdown, {
|
||||
return (net_shutdown() != -1);
|
||||
return (net_destroy() != -1);
|
||||
});
|
||||
|
||||
@@ -5,17 +5,17 @@
|
||||
#define USER_NICK "Friend"
|
||||
#define USER_SID "AAAB"
|
||||
|
||||
static struct user* inf_user = 0;
|
||||
static struct hub_user* inf_user = 0;
|
||||
static struct hub_info* inf_hub = 0;
|
||||
|
||||
extern int hub_handle_info_login(struct user* user, struct adc_message* cmd);
|
||||
extern int hub_handle_info_login(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd);
|
||||
|
||||
static void inf_create_hub()
|
||||
{
|
||||
inf_hub = (struct hub_info*) hub_malloc_zero(sizeof(struct hub_info));
|
||||
inf_hub->users = (struct user_manager*) hub_malloc_zero(sizeof(struct user_manager));
|
||||
inf_hub->users = (struct hub_user_manager*) hub_malloc_zero(sizeof(struct hub_user_manager));
|
||||
inf_hub->users->list = list_create();
|
||||
inf_hub->users->free_sid = 1;
|
||||
inf_hub->users->sids = sid_pool_create(500);
|
||||
|
||||
inf_hub->acl = (struct acl_handle*) hub_malloc_zero(sizeof(struct acl_handle));
|
||||
inf_hub->config = (struct hub_config*) hub_malloc_zero(sizeof(struct hub_config));
|
||||
@@ -27,16 +27,23 @@ static void inf_create_hub()
|
||||
static void inf_destroy_hub()
|
||||
{
|
||||
/* FIXME */
|
||||
list_destroy(inf_hub->users->list);
|
||||
sid_pool_destroy(inf_hub->users->sids);
|
||||
acl_shutdown(inf_hub->acl);
|
||||
free_config(inf_hub->config);
|
||||
hub_free(inf_hub->users);
|
||||
hub_free(inf_hub->acl);
|
||||
hub_free(inf_hub->config);
|
||||
hub_free(inf_hub);
|
||||
}
|
||||
|
||||
|
||||
static void inf_create_user()
|
||||
{
|
||||
if (inf_user) return;
|
||||
inf_user = (struct user*) hub_malloc_zero(sizeof(struct user));
|
||||
inf_user = (struct hub_user*) hub_malloc_zero(sizeof(struct hub_user));
|
||||
inf_user->connection = net_con_create();
|
||||
inf_user->id.sid = 1;
|
||||
inf_user->sd = -1;
|
||||
inf_user->hub = inf_hub;
|
||||
inf_user->limits.upload_slots = 1;
|
||||
}
|
||||
|
||||
@@ -55,13 +62,13 @@ EXO_TEST(inf_create_setup,
|
||||
});
|
||||
|
||||
|
||||
|
||||
/* FIXME: MEMORY LEAK - Need to fix hub_handle_info_login */
|
||||
#define CHECK_INF(MSG, EXPECT) \
|
||||
struct adc_message* msg = adc_msg_parse_verify(inf_user, MSG, strlen(MSG)); \
|
||||
int ok = hub_handle_info_login(inf_user, msg); \
|
||||
int ok = hub_handle_info_login(inf_hub, inf_user, msg); /* FIXME: MEMORY LEAK */ \
|
||||
adc_msg_free(msg); \
|
||||
if (ok != EXPECT) \
|
||||
printf("Expected %d, got %d\n", EXPECT, ok); \
|
||||
if (ok == EXPECT) \
|
||||
user_set_info(inf_user, 0); \
|
||||
return ok == EXPECT;
|
||||
|
||||
|
||||
@@ -106,7 +113,7 @@ EXO_TEST(inf_nick_10, {
|
||||
struct adc_message* msg = adc_msg_parse_verify(inf_user, line, strlen(line));
|
||||
|
||||
adc_msg_add_named_argument(msg, "NI", nick);
|
||||
int ok = hub_handle_info_login(inf_user, msg);
|
||||
int ok = hub_handle_info_login(inf_hub, inf_user, msg);
|
||||
adc_msg_free(msg);
|
||||
if (ok != status_msg_inf_error_nick_not_utf8)
|
||||
printf("Expected %d, got %d\n", status_msg_inf_error_nick_not_utf8, ok);
|
||||
|
||||
@@ -9,8 +9,8 @@ static struct ip_addr_encap ip6_a;
|
||||
static struct ip_addr_encap ip6_b;
|
||||
static struct ip_addr_encap ip6_c;
|
||||
static struct ip_addr_encap mask;
|
||||
static struct ip_ban_record ban6;
|
||||
static struct ip_ban_record ban4;
|
||||
static struct ip_range ban6;
|
||||
static struct ip_range ban4;
|
||||
|
||||
EXO_TEST(prepare_network, {
|
||||
return net_initialize() == 0;
|
||||
@@ -405,74 +405,74 @@ EXO_TEST(check_ban_setup_1, {
|
||||
|
||||
EXO_TEST(check_ban_ipv4_1, {
|
||||
struct ip_addr_encap addr; ip_convert_to_binary("192.168.0.0", &addr);
|
||||
return acl_check_ip_range(&addr, &ban4);
|
||||
return ip_in_range(&addr, &ban4);
|
||||
});
|
||||
|
||||
EXO_TEST(check_ban_ipv4_2, {
|
||||
struct ip_addr_encap addr; ip_convert_to_binary("192.168.0.1", &addr);
|
||||
return acl_check_ip_range(&addr, &ban4);
|
||||
return ip_in_range(&addr, &ban4);
|
||||
});
|
||||
|
||||
EXO_TEST(check_ban_ipv4_3, {
|
||||
struct ip_addr_encap addr; ip_convert_to_binary("192.168.0.255", &addr);
|
||||
return acl_check_ip_range(&addr, &ban4);
|
||||
return ip_in_range(&addr, &ban4);
|
||||
});
|
||||
|
||||
EXO_TEST(check_ban_ipv4_4, {
|
||||
struct ip_addr_encap addr; ip_convert_to_binary("192.168.1.0", &addr);
|
||||
return !acl_check_ip_range(&addr, &ban4);
|
||||
return !ip_in_range(&addr, &ban4);
|
||||
});
|
||||
|
||||
EXO_TEST(check_ban_ipv4_5, {
|
||||
struct ip_addr_encap addr; ip_convert_to_binary("192.167.255.255", &addr);
|
||||
return !acl_check_ip_range(&addr, &ban4);
|
||||
return !ip_in_range(&addr, &ban4);
|
||||
});
|
||||
|
||||
EXO_TEST(check_ban_ipv6_1, {
|
||||
if (!ipv6) return 1;
|
||||
struct ip_addr_encap addr; ip_convert_to_binary("2001::201:2ff:fefa:0", &addr);
|
||||
return acl_check_ip_range(&addr, &ban6);
|
||||
return ip_in_range(&addr, &ban6);
|
||||
});
|
||||
|
||||
EXO_TEST(check_ban_ipv6_2, {
|
||||
if (!ipv6) return 1;
|
||||
struct ip_addr_encap addr; ip_convert_to_binary("2001::201:2ff:fefa:1", &addr);
|
||||
return acl_check_ip_range(&addr, &ban6);
|
||||
return ip_in_range(&addr, &ban6);
|
||||
});
|
||||
|
||||
EXO_TEST(check_ban_ipv6_3, {
|
||||
if (!ipv6) return 1;
|
||||
struct ip_addr_encap addr; ip_convert_to_binary("2001::201:2ff:fefa:fffe", &addr);
|
||||
return acl_check_ip_range(&addr, &ban6);
|
||||
return ip_in_range(&addr, &ban6);
|
||||
});
|
||||
|
||||
EXO_TEST(check_ban_ipv6_4, {
|
||||
if (!ipv6) return 1;
|
||||
struct ip_addr_encap addr; ip_convert_to_binary("2001::201:2ff:fefa:ffff", &addr);
|
||||
return acl_check_ip_range(&addr, &ban6);
|
||||
return ip_in_range(&addr, &ban6);
|
||||
});
|
||||
|
||||
EXO_TEST(check_ban_ipv6_5, {
|
||||
if (!ipv6) return 1;
|
||||
struct ip_addr_encap addr; ip_convert_to_binary("2001::201:2ff:fefb:0", &addr);
|
||||
return !acl_check_ip_range(&addr, &ban6);
|
||||
return !ip_in_range(&addr, &ban6);
|
||||
});
|
||||
|
||||
EXO_TEST(check_ban_ipv6_6, {
|
||||
if (!ipv6) return 1;
|
||||
struct ip_addr_encap addr; ip_convert_to_binary("2001::201:2ff:fef9:ffff", &addr);
|
||||
return !acl_check_ip_range(&addr, &ban6);
|
||||
return !ip_in_range(&addr, &ban6);
|
||||
});
|
||||
|
||||
EXO_TEST(check_ban_afmix_1, {
|
||||
if (!ipv6) return 1;
|
||||
struct ip_addr_encap addr; ip_convert_to_binary("2001::201:2ff:fef9:ffff", &addr);
|
||||
return !acl_check_ip_range(&addr, &ban4);
|
||||
return !ip_in_range(&addr, &ban4);
|
||||
});
|
||||
|
||||
EXO_TEST(check_ban_afmix_2, {
|
||||
struct ip_addr_encap addr; ip_convert_to_binary("10.20.30.40", &addr);
|
||||
return !acl_check_ip_range(&addr, &ban6);
|
||||
return !ip_in_range(&addr, &ban6);
|
||||
});
|
||||
|
||||
EXO_TEST(ip4_bitwise_AND_1, {
|
||||
@@ -594,6 +594,28 @@ EXO_TEST(ip6_bitwise_OR_3, {
|
||||
return !strcmp(ip_convert_to_string(&ip6_c), "7777:cccc:3333:1111:ffff:ffff:ffff:1c1c");
|
||||
});
|
||||
|
||||
EXO_TEST(shutdown_network, {
|
||||
return net_shutdown() == 0;
|
||||
EXO_TEST(ip_range_1, {
|
||||
struct ip_range range; memset(&range, 0, sizeof(range));
|
||||
return ip_convert_address_to_range("192.168.0.1", &range) && memcmp(&range.lo, &range.hi, sizeof(struct ip_addr_encap)) == 0;
|
||||
});
|
||||
|
||||
EXO_TEST(ip_range_2, {
|
||||
struct ip_range range; memset(&range, 0, sizeof(range));
|
||||
return ip_convert_address_to_range("192.168.0.0-192.168.255.255", &range) && range.lo.af == range.hi.af && memcmp(&range.lo, &range.hi, sizeof(struct ip_addr_encap)) != 0;
|
||||
});
|
||||
|
||||
EXO_TEST(ip_range_3, {
|
||||
struct ip_range range; memset(&range, 0, sizeof(range));
|
||||
return ip_convert_address_to_range("192.168.0.0/16", &range) && range.lo.af == range.hi.af && memcmp(&range.lo, &range.hi, sizeof(struct ip_addr_encap)) != 0;
|
||||
});
|
||||
|
||||
EXO_TEST(ip_range_4, {
|
||||
struct ip_range range1; memset(&range1, 0, sizeof(range1));
|
||||
struct ip_range range2; memset(&range2, 0, sizeof(range2));
|
||||
return ip_convert_address_to_range("192.168.0.0/16", &range1) && ip_convert_address_to_range("192.168.0.0-192.168.255.255", &range2) && memcmp(&range1, &range2, sizeof(struct ip_range)) == 0;
|
||||
});
|
||||
|
||||
|
||||
EXO_TEST(shutdown_network, {
|
||||
return net_destroy() == 0;
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include <uhub.h>
|
||||
static struct user* g_user = 0;
|
||||
static struct hub_user* g_user = 0;
|
||||
static const char* test_string1 = "IINF AAfoo BBbar CCwhat\n";
|
||||
static const char* test_string2 = "BMSG AAAB Hello\\sWorld!\n";
|
||||
static const char* test_string3 = "BINF AAAB IDAN7ZMSLIEBL53OPTM7WXGSTXUS3XOY6KQS5LBGX NIFriend DEstuff SL3 SS0 SF0 VEQuickDC/0.4.17 US6430 SUADC0,TCP4,UDP4 I4127.0.0.1 HO5 HN1 AW\n";
|
||||
@@ -11,12 +11,17 @@ static void create_test_user()
|
||||
if (g_user)
|
||||
return;
|
||||
|
||||
g_user = (struct user*) malloc(sizeof(struct user));
|
||||
memset(g_user, 0, sizeof(struct user));
|
||||
g_user = (struct hub_user*) malloc(sizeof(struct hub_user));
|
||||
memset(g_user, 0, sizeof(struct hub_user));
|
||||
memcpy(g_user->id.nick, "exotic-tester", 13);
|
||||
g_user->id.sid = 1;
|
||||
}
|
||||
|
||||
EXO_TEST(adc_message_first, {
|
||||
create_test_user();
|
||||
return g_user != 0;
|
||||
});
|
||||
|
||||
EXO_TEST(adc_message_parse_1, {
|
||||
struct adc_message* msg = adc_msg_create("IMSG Hello\\sWorld!");
|
||||
int ok = msg != NULL;
|
||||
@@ -26,11 +31,12 @@ EXO_TEST(adc_message_parse_1, {
|
||||
|
||||
EXO_TEST(adc_message_parse_2, {
|
||||
struct adc_message* msg = adc_msg_create(test_string2);
|
||||
return msg == NULL;
|
||||
int ok = (msg != NULL);
|
||||
adc_msg_free(msg);
|
||||
return ok;
|
||||
});
|
||||
|
||||
EXO_TEST(adc_message_parse_3, {
|
||||
create_test_user();
|
||||
struct adc_message* msg = adc_msg_parse_verify(g_user, "BMSG AAAB Hello\\sWorld!", 23);
|
||||
int ok = msg != NULL;
|
||||
adc_msg_free(msg);
|
||||
@@ -144,12 +150,29 @@ EXO_TEST(adc_message_parse_20, {
|
||||
return ok;
|
||||
});
|
||||
|
||||
|
||||
EXO_TEST(adc_message_parse_21, {
|
||||
struct adc_message* msg = adc_msg_parse_verify(g_user, "EMSG AAAC AAAB Hello\\sthere!\n", 29);
|
||||
return msg == NULL;
|
||||
});
|
||||
|
||||
EXO_TEST(adc_message_parse_22, {
|
||||
struct adc_message* msg = adc_msg_parse_verify(g_user, "\n", 0);
|
||||
return msg == NULL;
|
||||
});
|
||||
|
||||
EXO_TEST(adc_message_parse_23, {
|
||||
struct adc_message* msg = adc_msg_parse_verify(g_user, "\r\n", 1);
|
||||
return msg == NULL;
|
||||
});
|
||||
|
||||
EXO_TEST(adc_message_parse_24, {
|
||||
struct adc_message* msg = adc_msg_parse_verify(g_user, "EMSG AAAC\0AAAB Hello\\sthere!\n", 29);
|
||||
return msg == NULL;
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
EXO_TEST(adc_message_add_arg_1, {
|
||||
struct adc_message* msg = adc_msg_create(test_string1);
|
||||
adc_msg_add_argument(msg, "XXwtf?");
|
||||
@@ -491,13 +514,22 @@ EXO_TEST(adc_message_update_3, {
|
||||
return updater2 != NULL;
|
||||
});
|
||||
|
||||
extern void update_user_info(struct user* u, struct adc_message* cmd);
|
||||
|
||||
EXO_TEST(adc_message_update_4, {
|
||||
update_user_info(g_user, updater2);
|
||||
user_update_info(g_user, updater2);
|
||||
return strlen(g_user->info->cache) == 159;
|
||||
});
|
||||
|
||||
EXO_TEST(adc_message_update_4_cleanup, {
|
||||
adc_msg_free(updater1);
|
||||
updater1 = 0;
|
||||
adc_msg_free(updater2);
|
||||
updater2 = 0;
|
||||
adc_msg_free(g_user->info);
|
||||
g_user->info = 0;
|
||||
return 1;
|
||||
});
|
||||
|
||||
|
||||
EXO_TEST(adc_message_empty_1, {
|
||||
struct adc_message* msg = adc_msg_parse_verify(g_user, test_string2, strlen(test_string2));
|
||||
int ok = adc_msg_is_empty(msg) == 0;
|
||||
@@ -519,3 +551,10 @@ EXO_TEST(adc_message_empty_3, {
|
||||
return ok;
|
||||
});
|
||||
|
||||
EXO_TEST(adc_message_last, {
|
||||
hub_free(g_user);
|
||||
g_user = 0;
|
||||
return g_user == 0;
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -100,4 +100,12 @@ EXO_TEST(utf8_valid_3, { return is_valid_utf8("0123456789"); });
|
||||
EXO_TEST(utf8_valid_4, { return is_valid_utf8( (char[]) { 0x65, 0x00} ); });
|
||||
EXO_TEST(utf8_valid_5, { return !is_valid_utf8( (char[]) { 0xD8, 0x00} ); });
|
||||
|
||||
EXO_TEST(utf8_valid_6, { return is_valid_utf8( (char[]) { 0x24, 0x00} ); });
|
||||
EXO_TEST(utf8_valid_7, { return !is_valid_utf8( (char[]) { 0xC2, 0x24, 0x00} ); });
|
||||
EXO_TEST(utf8_valid_8, { return is_valid_utf8( (char[]) { 0xC2, 0xA2, 0x00} ); });
|
||||
EXO_TEST(utf8_valid_9, { return is_valid_utf8( (char[]) { 0xE2, 0x82, 0xAC, 0x00} ); });
|
||||
EXO_TEST(utf8_valid_10, { return !is_valid_utf8( (char[]) { 0xC2, 0x32, 0x00} ); });
|
||||
EXO_TEST(utf8_valid_11, { return !is_valid_utf8( (char[]) { 0xE2, 0x82, 0x32, 0x00} ); });
|
||||
EXO_TEST(utf8_valid_12, { return !is_valid_utf8( (char[]) { 0xE2, 0x32, 0x82, 0x00} ); });
|
||||
|
||||
|
||||
|
||||
109
autotest/test_usermanager.tcc
Normal file
109
autotest/test_usermanager.tcc
Normal file
@@ -0,0 +1,109 @@
|
||||
#include <uhub.h>
|
||||
|
||||
#define MAX_USERS 64
|
||||
|
||||
static struct hub_info um_hub;
|
||||
static struct hub_user um_user[MAX_USERS];
|
||||
// static struct net_connection um_cons[MAX_USERS];
|
||||
|
||||
EXO_TEST(um_test_setup, {
|
||||
int i = 0;
|
||||
memset(&um_hub, 0, sizeof(um_hub));
|
||||
|
||||
for (i = 0; i < MAX_USERS; i++)
|
||||
{
|
||||
/* memset(&um_cons[i], 0, sizeof(struct net_connection));
|
||||
um_cons[i].sd = -1;
|
||||
|
||||
memset(&um_user[i], 0, sizeof(struct hub_user));
|
||||
um_user[i].id.sid = i+1;
|
||||
um_user[i].connection = &um_cons[i];*/
|
||||
}
|
||||
return 1;
|
||||
});
|
||||
|
||||
EXO_TEST(um_init_1, {
|
||||
return uman_init(0) != 0;
|
||||
});
|
||||
|
||||
EXO_TEST(um_init_2, {
|
||||
return uman_init(&um_hub) == 0;
|
||||
});
|
||||
|
||||
EXO_TEST(um_shutdown_1, {
|
||||
return uman_shutdown(0) == -1;
|
||||
});
|
||||
|
||||
EXO_TEST(um_shutdown_2, {
|
||||
return uman_shutdown(&um_hub) == 0;
|
||||
});
|
||||
|
||||
EXO_TEST(um_shutdown_3, {
|
||||
return uman_shutdown(&um_hub) == -1;
|
||||
});
|
||||
|
||||
EXO_TEST(um_init_3, {
|
||||
return uman_init(&um_hub) == 0;
|
||||
});
|
||||
|
||||
EXO_TEST(um_add_1, {
|
||||
return uman_add(&um_hub, &um_user[0]) == 0;
|
||||
});
|
||||
|
||||
EXO_TEST(um_size_1, {
|
||||
return hub_get_user_count(&um_hub) == 1;
|
||||
});
|
||||
|
||||
|
||||
EXO_TEST(um_remove_1, {
|
||||
return uman_remove(&um_hub, &um_user[0]) == 0;
|
||||
});
|
||||
|
||||
EXO_TEST(um_size_2, {
|
||||
return hub_get_user_count(&um_hub) == 0;
|
||||
});
|
||||
|
||||
|
||||
EXO_TEST(um_add_2, {
|
||||
int i;
|
||||
for (i = 0; i < MAX_USERS; i++)
|
||||
{
|
||||
if (uman_add(&um_hub, &um_user[i]) != 0)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
});
|
||||
|
||||
EXO_TEST(um_size_3, {
|
||||
return hub_get_user_count(&um_hub) == MAX_USERS;
|
||||
});
|
||||
|
||||
EXO_TEST(um_add_3, {
|
||||
return uman_add(&um_hub, &um_user[5]) != 0;
|
||||
});
|
||||
|
||||
EXO_TEST(um_remove_2, {
|
||||
int i;
|
||||
for (i = 0; i < MAX_USERS; i++)
|
||||
{
|
||||
if (uman_remove(&um_hub, &um_user[i]) != 0)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Last test */
|
||||
EXO_TEST(um_shutdown_4, {
|
||||
return uman_shutdown(&um_hub) == 0;
|
||||
});
|
||||
5
debian/changelog
vendored
5
debian/changelog
vendored
@@ -1,5 +0,0 @@
|
||||
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
1
debian/compat
vendored
@@ -1 +0,0 @@
|
||||
7
|
||||
3
debian/conffiles
vendored
3
debian/conffiles
vendored
@@ -1,3 +0,0 @@
|
||||
etc/uhub/uhub.conf
|
||||
etc/uhub/users.conf
|
||||
etc/uhub/motd.txt
|
||||
15
debian/control
vendored
15
debian/control
vendored
@@ -1,15 +0,0 @@
|
||||
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
9
debian/copyright
vendored
@@ -1,9 +0,0 @@
|
||||
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
1
debian/dirs
vendored
@@ -1 +0,0 @@
|
||||
usr/bin
|
||||
3
debian/docs
vendored
3
debian/docs
vendored
@@ -1,3 +0,0 @@
|
||||
BUGS
|
||||
README
|
||||
AUTHORS
|
||||
1
debian/files
vendored
1
debian/files
vendored
@@ -1 +0,0 @@
|
||||
uhub_0.2.6-1_amd64.deb net extra
|
||||
157
debian/init.d
vendored
157
debian/init.d
vendored
@@ -1,157 +0,0 @@
|
||||
#! /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
39
debian/postinst
vendored
@@ -1,39 +0,0 @@
|
||||
#!/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
37
debian/postrm
vendored
@@ -1,37 +0,0 @@
|
||||
#!/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
35
debian/preinst
vendored
@@ -1,35 +0,0 @@
|
||||
#!/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
38
debian/prerm
vendored
@@ -1,38 +0,0 @@
|
||||
#!/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
91
debian/rules
vendored
@@ -1,91 +0,0 @@
|
||||
#!/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
10
debian/uhub.default
vendored
@@ -1,10 +0,0 @@
|
||||
# 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}"
|
||||
101
doc/init.d.RedHat/etc/init.d/uhub
Executable file
101
doc/init.d.RedHat/etc/init.d/uhub
Executable file
@@ -0,0 +1,101 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# chkconfig: - 91 35
|
||||
# description: Starts and stops the Uhub ( http://www.extatic.org/uhub ) daemons on RHEL\CentOS \
|
||||
# used to provide p2p network services.
|
||||
#
|
||||
# pidfile: /var/run/uhub.pid
|
||||
# config: /etc/uhub/uhub.conf
|
||||
|
||||
|
||||
|
||||
# Source function library.
|
||||
if [ -f /etc/init.d/functions ] ; then
|
||||
. /etc/init.d/functions
|
||||
elif [ -f /etc/rc.d/init.d/functions ] ; then
|
||||
. /etc/rc.d/init.d/functions
|
||||
else
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Avoid using root's TMPDIR
|
||||
unset TMPDIR
|
||||
|
||||
# Source networking configuration.
|
||||
. /etc/sysconfig/network
|
||||
|
||||
if [ -f /etc/sysconfig/uhub ]; then
|
||||
. /etc/sysconfig/uhub
|
||||
fi
|
||||
|
||||
# Check that networking is up.
|
||||
[ ${NETWORKING} = "no" ] && exit 1
|
||||
|
||||
# Check that uhub.conf exists.
|
||||
[ -f /etc/uhub/uhub.conf ] || exit 6
|
||||
|
||||
RETVAL=0
|
||||
|
||||
|
||||
start() {
|
||||
KIND="UHUB"
|
||||
echo -n $"Starting $KIND services: "
|
||||
daemon uhub $UHUBOPTIONS
|
||||
RETVAL=$?
|
||||
echo ""
|
||||
return $RETVAL
|
||||
}
|
||||
|
||||
stop() {
|
||||
KIND="UHUB"
|
||||
echo -n $"Shutting down $KIND services: "
|
||||
killproc uhub
|
||||
RETVAL=$?
|
||||
echo ""
|
||||
return $RETVAL
|
||||
}
|
||||
|
||||
restart() {
|
||||
stop
|
||||
start
|
||||
}
|
||||
|
||||
reload() {
|
||||
echo -n $"Reloading uhub.conf / user.conf file: "
|
||||
killproc uhub -HUP
|
||||
RETVAL=$?
|
||||
echo ""
|
||||
return $RETVAL
|
||||
}
|
||||
|
||||
rhstatus() {
|
||||
status uhub
|
||||
RETVAL=$?
|
||||
if [ $RETVAL -ne 0 ] ; then
|
||||
return $RETVAL
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
case "$1" in
|
||||
start)
|
||||
start
|
||||
;;
|
||||
stop)
|
||||
stop
|
||||
;;
|
||||
restart)
|
||||
restart
|
||||
;;
|
||||
reload)
|
||||
reload
|
||||
;;
|
||||
status)
|
||||
rhstatus
|
||||
;;
|
||||
*)
|
||||
echo $"Usage: $0 {start|stop|restart|reload|status}"
|
||||
exit 2
|
||||
esac
|
||||
|
||||
exit $?
|
||||
13
doc/init.d.RedHat/etc/logrotator.d/uhub
Normal file
13
doc/init.d.RedHat/etc/logrotator.d/uhub
Normal file
@@ -0,0 +1,13 @@
|
||||
# Log rotate for Uhub
|
||||
# see man logrotate
|
||||
#
|
||||
#
|
||||
/var/log/uhub.log {
|
||||
compress
|
||||
size 10M
|
||||
rotate 10
|
||||
missingok
|
||||
notifempty
|
||||
|
||||
|
||||
}
|
||||
15
doc/init.d.RedHat/etc/sysconfig/uhub
Normal file
15
doc/init.d.RedHat/etc/sysconfig/uhub
Normal file
@@ -0,0 +1,15 @@
|
||||
# Options to UHUB
|
||||
# -v Verbose mode. Add more -v's for higher verbosity.
|
||||
# -q Quiet mode - no output
|
||||
# -f Fork to background
|
||||
# -l <file> Log messages to given file (default: stderr)
|
||||
# -L Log messages to syslog
|
||||
# -c <file> Specify configuration file (default: /etc/uhub/uhub.conf)
|
||||
# -S Show configuration parameters, but ignore defaults
|
||||
# -u <user> Run as given user
|
||||
# -g <group> Run with given group permissions
|
||||
# -p <file> Store pid in file (process id)
|
||||
|
||||
|
||||
UHUBOPTIONS=" -u uhub -f -p /var/run/uhub.pid -l /var/log/uhub.log"
|
||||
|
||||
5
doc/rules.txt
Normal file
5
doc/rules.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
1. rule #1
|
||||
2. rule #2
|
||||
3. rule #3
|
||||
......
|
||||
34. Rule #34
|
||||
72
doc/uhub.1
Normal file
72
doc/uhub.1
Normal file
@@ -0,0 +1,72 @@
|
||||
.TH UHUB 1 "March 2009"
|
||||
.\" Please adjust this date whenever revising the manpage.
|
||||
.\"
|
||||
.\" Some roff macros, for reference:
|
||||
.\" .nh disable hyphenation
|
||||
.\" .hy enable hyphenation
|
||||
.\" .ad l left justify
|
||||
.\" .ad b justify to both left and right margins
|
||||
.\" .nf disable filling
|
||||
.\" .fi enable filling
|
||||
.\" .br insert line break
|
||||
.\" .sp <n> insert n+1 empty lines
|
||||
.\" for manpage-specific macros, see man(7)
|
||||
.SH NAME
|
||||
uhub \- a high performance ADC peer-to-peer hub
|
||||
.SH SYNOPSIS
|
||||
.B uhub
|
||||
.RI [ options ]
|
||||
.SH 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.
|
||||
.SH "OPTIONS"
|
||||
.TP
|
||||
.BI \^\-v
|
||||
Verbose mode, add more -v's for higher verbosity.
|
||||
.TP
|
||||
.BI \^\-q
|
||||
Quiet mode, if quiet mode is enabled no output or logs are made.
|
||||
.TP
|
||||
.BI \^\-f
|
||||
Fork uhub to background in order to run it as a daemon.
|
||||
.TP
|
||||
.BI \^\-l " logfile"
|
||||
Log messages to the given logfile (default: stderr)
|
||||
.TP
|
||||
.BI \^\-L
|
||||
Log messages to syslog.
|
||||
.TP
|
||||
.BI \^\-c " config"
|
||||
Specify configuration file (default: /etc/uhub/uhub.conf)
|
||||
.TP
|
||||
.BI \^\-C
|
||||
Check configuration files and return. Will print either \"OK\" or \"ERROR\".
|
||||
.TP
|
||||
.BI \^\-s
|
||||
Show all configuration parameters. In a format that is compatible with
|
||||
the configuration files.
|
||||
.TP
|
||||
.BI \^\-S
|
||||
Show all non-default configuration parameters.
|
||||
.TP
|
||||
.BI \^\-h
|
||||
Show the help message.
|
||||
.TP
|
||||
.BI \^\-u " user"
|
||||
Drop privileges and run as the given user.
|
||||
.TP
|
||||
.BI \^\-g " group"
|
||||
Drop privileges and run with the given group permissions.
|
||||
.TP
|
||||
.BI \^\-V
|
||||
Show the version number
|
||||
.SH EXAMPLES
|
||||
To run uhub as a daemon, and log to a file:
|
||||
.TP
|
||||
.B uhub " -f -l /var/log/uhub/uhub.log"
|
||||
.SH AUTHOR
|
||||
This program was written by Jan Vidar Krey <janvidar@extatic.org>
|
||||
.SH "BUG REPORTS"
|
||||
If you find a bug in uhub please report it to
|
||||
.B http://bugs.extatic.org/
|
||||
@@ -4,7 +4,8 @@
|
||||
#
|
||||
# This file is read only to the uhub deamon, and if you
|
||||
# make changes to it while uhub is running you can send a
|
||||
# HUP signal to it, to reparse configuration (only on UNIX).
|
||||
# HUP signal to it ( $ killall -HUP uhub ), to reparse configuration (only on UNIX).
|
||||
# All configuration directives: http://www.uhub.org/config.php
|
||||
|
||||
# Bind to this port and address
|
||||
# server_bind_addr=any means listen to "::" if IPv6 is supported
|
||||
@@ -38,6 +39,28 @@ file_acl=/etc/uhub/users.conf
|
||||
# Normally this message is sent to clients when connecting.
|
||||
file_motd=/etc/uhub/motd.txt
|
||||
|
||||
# This file can contain a rules of the hub.
|
||||
# Normally this message is sent to clients when write in chat !rules
|
||||
file_rules=/etc/uhub/rules.txt
|
||||
|
||||
|
||||
# Slots\share\hubs limits
|
||||
limit_max_hubs_user = 0
|
||||
limit_max_hubs_reg = 0
|
||||
limit_max_hubs_op = 0
|
||||
limit_max_hubs = 0
|
||||
limit_min_hubs_user = 0
|
||||
limit_min_hubs_reg = 0
|
||||
limit_min_hubs_op = 0
|
||||
limit_min_share = 0
|
||||
# Example:
|
||||
# To require users to share at least 1 GB in order to enter the hub:
|
||||
# limit_min_share = 1024
|
||||
limit_max_share = 0
|
||||
limit_min_slots = 0
|
||||
limit_max_slots = 0
|
||||
|
||||
|
||||
# Configure status message as sent to clients in different circumstances.
|
||||
msg_hub_full = Hub is full
|
||||
msg_hub_disabled = Hub is disabled
|
||||
@@ -61,6 +84,12 @@ msg_ban_permanently = Banned permanently
|
||||
msg_ban_temporarily = Banned temporarily
|
||||
msg_auth_invalid_password = Password is wrong
|
||||
msg_auth_user_not_found = User not found in password database
|
||||
msg_user_share_size_low = User is not sharing enough
|
||||
msg_user_share_size_high = User is sharing too much
|
||||
msg_user_slots_low = User have too few upload slots
|
||||
msg_user_slots_high = User have too many upload slots
|
||||
msg_user_hub_limit_low = User is on too few hubs
|
||||
msg_user_hub_limit_high = User is on too many hubs
|
||||
msg_error_no_memory = No memory
|
||||
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
# 'ban_cid' - banned user by cid
|
||||
|
||||
# Administrator
|
||||
user_admin Dj_Offset:uhub
|
||||
user_op janvidar:password
|
||||
# user_admin userA:password1
|
||||
# user_op userB:password2
|
||||
|
||||
# We don't want users with these names
|
||||
deny_nick Hub-Security
|
||||
|
||||
@@ -26,16 +26,71 @@
|
||||
uhub_assert(X->capacity); \
|
||||
uhub_assert(X->length); \
|
||||
uhub_assert(X->length <= X->capacity); \
|
||||
uhub_assert(X->length == strlen(X->cache)); \
|
||||
uhub_assert(X->references >= 0);
|
||||
uhub_assert(X->length == strlen(X->cache));
|
||||
#else
|
||||
#define ADC_MSG_ASSERT(X) do { } while(0)
|
||||
#endif
|
||||
#endif /* DEBUG */
|
||||
|
||||
#ifdef MSG_MEMORY_DEBUG
|
||||
#undef msg_malloc
|
||||
#undef msg_malloc_zero
|
||||
#undef msg_free
|
||||
|
||||
static void* msg_malloc(size_t size)
|
||||
{
|
||||
void* ptr = valloc(size);
|
||||
LOG_MEMORY("msg_malloc: %p %d", ptr, (int) size);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static void* msg_malloc_zero(size_t size)
|
||||
{
|
||||
void* ptr = msg_malloc(size);
|
||||
memset(ptr, 0, size);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static void msg_free(void* ptr)
|
||||
{
|
||||
LOG_MEMORY("msg_free: %p", ptr);
|
||||
// hub_free(ptr);
|
||||
}
|
||||
|
||||
#include <sys/mman.h>
|
||||
static void adc_msg_protect(struct adc_message* cmd)
|
||||
{
|
||||
LOG_MEMORY("msg_prot: %p %d", cmd, cmd->capacity);
|
||||
mprotect(cmd, sizeof(cmd), PROT_READ);
|
||||
mprotect(cmd->cache, sizeof(cmd->capacity), PROT_READ);
|
||||
}
|
||||
|
||||
static void adc_msg_unprotect(struct adc_message* cmd)
|
||||
{
|
||||
LOG_MEMORY("msg_unprot: %p %d", cmd, cmd->capacity);
|
||||
mprotect(cmd, sizeof(cmd), PROT_READ | PROT_WRITE);
|
||||
mprotect(cmd->cache, sizeof(cmd->capacity), PROT_READ | PROT_WRITE);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#define msg_malloc(X) hub_malloc(X)
|
||||
#define msg_malloc_zero(X) hub_malloc_zero(X)
|
||||
#define msg_free(X) hub_free(X)
|
||||
|
||||
#endif /* MSG_MEMORY_DEBUG */
|
||||
|
||||
|
||||
struct adc_message* adc_msg_incref(struct adc_message* msg)
|
||||
{
|
||||
if (!msg) return 0;
|
||||
#ifndef ADC_MESSAGE_INCREF
|
||||
#ifdef MSG_MEMORY_DEBUG
|
||||
adc_msg_unprotect(msg);
|
||||
#endif
|
||||
msg->references++;
|
||||
#ifdef MSG_MEMORY_DEBUG
|
||||
adc_msg_protect(msg);
|
||||
#endif
|
||||
return msg;
|
||||
#else
|
||||
struct adc_message* copy = adc_msg_copy(msg);
|
||||
@@ -55,24 +110,25 @@ static int adc_msg_grow(struct adc_message* msg, size_t size)
|
||||
|
||||
if (msg->capacity > size)
|
||||
return 1;
|
||||
|
||||
|
||||
/* Make sure we align our data */
|
||||
newsize = size;
|
||||
newsize += 2; /* termination */
|
||||
newsize += (newsize % sizeof(size_t)); /* alignment padding */
|
||||
|
||||
buf = hub_malloc_zero(newsize);
|
||||
|
||||
buf = msg_malloc_zero(newsize);
|
||||
if (!buf)
|
||||
return 0;
|
||||
|
||||
|
||||
if (msg->cache)
|
||||
{
|
||||
memcpy(buf, msg->cache, msg->length);
|
||||
hub_free(msg->cache);
|
||||
msg_free(msg->cache);
|
||||
}
|
||||
|
||||
|
||||
msg->cache = buf;
|
||||
msg->capacity = newsize;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -84,10 +140,10 @@ static int adc_msg_cache_append(struct adc_message* msg, const char* string, siz
|
||||
/* FIXME: OOM! */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
memcpy(&msg->cache[msg->length], string, len);
|
||||
adc_msg_set_length(msg, msg->length + len);
|
||||
|
||||
|
||||
assert(msg->capacity > msg->length);
|
||||
msg->cache[msg->length] = 0;
|
||||
return 1;
|
||||
@@ -147,14 +203,23 @@ void adc_msg_free(struct adc_message* msg)
|
||||
if (!msg) return;
|
||||
|
||||
ADC_MSG_ASSERT(msg);
|
||||
|
||||
|
||||
if (msg->references > 0)
|
||||
{
|
||||
#ifdef MSG_MEMORY_DEBUG
|
||||
adc_msg_unprotect(msg);
|
||||
#endif
|
||||
msg->references--;
|
||||
#ifdef MSG_MEMORY_DEBUG
|
||||
adc_msg_protect(msg);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
hub_free(msg->cache);
|
||||
#ifdef MSG_MEMORY_DEBUG
|
||||
adc_msg_unprotect(msg);
|
||||
#endif
|
||||
msg_free(msg->cache);
|
||||
|
||||
if (msg->feature_cast_include)
|
||||
{
|
||||
@@ -170,7 +235,7 @@ void adc_msg_free(struct adc_message* msg)
|
||||
msg->feature_cast_exclude = 0;
|
||||
}
|
||||
|
||||
hub_free(msg);
|
||||
msg_free(msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,11 +243,11 @@ void adc_msg_free(struct adc_message* msg)
|
||||
struct adc_message* adc_msg_copy(const struct adc_message* cmd)
|
||||
{
|
||||
char* tmp = 0;
|
||||
struct adc_message* copy = (struct adc_message*) hub_malloc_zero(sizeof(struct adc_message));
|
||||
struct adc_message* copy = (struct adc_message*) msg_malloc_zero(sizeof(struct adc_message));
|
||||
if (!copy) return NULL; /* OOM */
|
||||
|
||||
ADC_MSG_ASSERT(cmd);
|
||||
|
||||
|
||||
/* deep copy */
|
||||
copy->cmd = cmd->cmd;
|
||||
copy->source = cmd->source;
|
||||
@@ -194,7 +259,7 @@ struct adc_message* adc_msg_copy(const struct adc_message* cmd)
|
||||
copy->references = 0;
|
||||
copy->feature_cast_include = 0;
|
||||
copy->feature_cast_exclude = 0;
|
||||
|
||||
|
||||
if (cmd->cache)
|
||||
{
|
||||
if (!adc_msg_grow(copy, copy->length))
|
||||
@@ -205,7 +270,7 @@ struct adc_message* adc_msg_copy(const struct adc_message* cmd)
|
||||
memcpy(copy->cache, cmd->cache, cmd->length);
|
||||
copy->cache[copy->length] = 0;
|
||||
}
|
||||
|
||||
|
||||
if (cmd->feature_cast_include)
|
||||
{
|
||||
copy->feature_cast_include = list_create();
|
||||
@@ -216,7 +281,7 @@ struct adc_message* adc_msg_copy(const struct adc_message* cmd)
|
||||
tmp = list_get_next(cmd->feature_cast_include);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (cmd->feature_cast_exclude)
|
||||
{
|
||||
copy->feature_cast_exclude = list_create();
|
||||
@@ -227,14 +292,17 @@ struct adc_message* adc_msg_copy(const struct adc_message* cmd)
|
||||
tmp = list_get_next(cmd->feature_cast_exclude);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ADC_MSG_ASSERT(copy);
|
||||
|
||||
|
||||
#ifdef MSG_MEMORY_DEBUG
|
||||
adc_msg_protect(copy);
|
||||
#endif
|
||||
return copy;
|
||||
}
|
||||
|
||||
|
||||
struct adc_message* adc_msg_parse_verify(struct user* u, const char* line, size_t length)
|
||||
struct adc_message* adc_msg_parse_verify(struct hub_user* u, const char* line, size_t length)
|
||||
{
|
||||
struct adc_message* command = adc_msg_parse(line, length);
|
||||
|
||||
@@ -243,6 +311,7 @@ struct adc_message* adc_msg_parse_verify(struct user* u, const char* line, size_
|
||||
|
||||
if (command->source && (!u || command->source != u->id.sid))
|
||||
{
|
||||
LOG_DEBUG("Command does not match user's SID (command->source=%d, user->id.sid=%d)", command->source, (u ? u->id.sid : 0));
|
||||
adc_msg_free(command);
|
||||
return 0;
|
||||
}
|
||||
@@ -253,7 +322,7 @@ struct adc_message* adc_msg_parse_verify(struct user* u, const char* line, size_
|
||||
|
||||
struct adc_message* adc_msg_parse(const char* line, size_t length)
|
||||
{
|
||||
struct adc_message* command = (struct adc_message*) hub_malloc_zero(sizeof(struct adc_message));
|
||||
struct adc_message* command = (struct adc_message*) msg_malloc_zero(sizeof(struct adc_message));
|
||||
char prefix = line[0];
|
||||
size_t n = 0;
|
||||
char temp_sid[5];
|
||||
@@ -263,6 +332,13 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
|
||||
|
||||
if (command == NULL)
|
||||
return NULL; /* OOM */
|
||||
|
||||
if (!is_printable_utf8(line, length))
|
||||
{
|
||||
LOG_DEBUG("Dropped message with non-printable UTF-8 characters.");
|
||||
msg_free(command);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (line[length-1] != '\n')
|
||||
{
|
||||
@@ -271,7 +347,7 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
|
||||
|
||||
if (!adc_msg_grow(command, length + need_terminate))
|
||||
{
|
||||
hub_free(command);
|
||||
msg_free(command);
|
||||
return NULL; /* OOM */
|
||||
}
|
||||
|
||||
@@ -343,8 +419,8 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
|
||||
{
|
||||
list_destroy(command->feature_cast_include);
|
||||
list_destroy(command->feature_cast_exclude);
|
||||
hub_free(command->cache);
|
||||
hub_free(command);
|
||||
msg_free(command->cache);
|
||||
msg_free(command);
|
||||
return NULL; /* OOM */
|
||||
}
|
||||
|
||||
@@ -434,20 +510,23 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
|
||||
}
|
||||
|
||||
ADC_MSG_ASSERT(command);
|
||||
|
||||
|
||||
#ifdef MSG_MEMORY_DEBUG
|
||||
adc_msg_protect(command);
|
||||
#endif
|
||||
return command;
|
||||
}
|
||||
|
||||
|
||||
struct adc_message* adc_msg_create(const char* line)
|
||||
{
|
||||
return adc_msg_parse_verify(NULL, line, strlen(line));
|
||||
return adc_msg_parse(line, strlen(line));
|
||||
}
|
||||
|
||||
|
||||
struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size)
|
||||
{
|
||||
struct adc_message* msg = (struct adc_message*) hub_malloc_zero(sizeof(struct adc_message));
|
||||
struct adc_message* msg = (struct adc_message*) msg_malloc_zero(sizeof(struct adc_message));
|
||||
|
||||
if (!msg)
|
||||
return NULL; /* OOM */
|
||||
@@ -456,7 +535,7 @@ struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size)
|
||||
|
||||
if (!adc_msg_grow(msg, size+1))
|
||||
{
|
||||
hub_free(msg);
|
||||
msg_free(msg);
|
||||
return NULL; /* OOM */
|
||||
}
|
||||
|
||||
@@ -475,7 +554,11 @@ struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size)
|
||||
|
||||
msg->cmd = fourcc;
|
||||
msg->priority = 0;
|
||||
|
||||
|
||||
#ifdef MSG_MEMORY_DEBUG
|
||||
adc_msg_protect(msg);
|
||||
#endif
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
@@ -511,7 +594,6 @@ int adc_msg_remove_named_argument(struct adc_message* cmd, const char prefix_[2]
|
||||
{
|
||||
|
||||
temp_len = &end[0] - &start[0]; // strlen(start);
|
||||
/* hub_log(log_trace, " length=%d", (int) (temp_len)); */
|
||||
endlen = strlen(end);
|
||||
|
||||
memmove(start, end, endlen);
|
||||
@@ -616,12 +698,20 @@ void adc_msg_terminate(struct adc_message* cmd)
|
||||
adc_msg_cache_append(cmd, "\n", 1);
|
||||
}
|
||||
ADC_MSG_ASSERT(cmd);
|
||||
|
||||
#ifdef MSG_MEMORY_DEBUG
|
||||
adc_msg_protect(cmd);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* FIXME: this looks bogus */
|
||||
void adc_msg_unterminate(struct adc_message* cmd)
|
||||
{
|
||||
ADC_MSG_ASSERT(cmd);
|
||||
|
||||
#ifdef MSG_MEMORY_DEBUG
|
||||
adc_msg_unprotect(cmd);
|
||||
#endif
|
||||
|
||||
if (cmd->length > 0 && cmd->cache[cmd->length-1] == '\n')
|
||||
{
|
||||
@@ -646,6 +736,27 @@ int adc_msg_add_named_argument(struct adc_message* cmd, const char prefix[2], co
|
||||
return ret;
|
||||
}
|
||||
|
||||
int adc_msg_add_named_argument_string(struct adc_message* cmd, const char prefix[2], const char* string)
|
||||
{
|
||||
char* escaped = adc_msg_escape(string);
|
||||
int ret = adc_msg_add_named_argument(cmd, prefix, escaped);
|
||||
hub_free(escaped);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int adc_msg_add_named_argument_int(struct adc_message* cmd, const char prefix[2], int num)
|
||||
{
|
||||
const char* s = uhub_itoa(num);
|
||||
int ret = adc_msg_add_named_argument(cmd, prefix, s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int adc_msg_add_named_argument_uint64(struct adc_message* cmd, const char prefix[2], uint64_t num)
|
||||
{
|
||||
const char* s = uhub_ulltoa(num);
|
||||
int ret = adc_msg_add_named_argument(cmd, prefix, s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int adc_msg_add_argument(struct adc_message* cmd, const char* string)
|
||||
{
|
||||
@@ -665,16 +776,15 @@ char* adc_msg_get_argument(struct adc_message* cmd, int offset)
|
||||
char* end;
|
||||
char* argument;
|
||||
int count = 0;
|
||||
|
||||
|
||||
ADC_MSG_ASSERT(cmd);
|
||||
|
||||
|
||||
adc_msg_unterminate(cmd);
|
||||
|
||||
|
||||
start = strchr(&cmd->cache[adc_msg_get_arg_offset(cmd)-1], ' ');
|
||||
while (start)
|
||||
{
|
||||
end = strchr(&start[1], ' ');
|
||||
|
||||
if (count == offset)
|
||||
{
|
||||
if (end)
|
||||
@@ -684,21 +794,27 @@ char* adc_msg_get_argument(struct adc_message* cmd, int offset)
|
||||
else
|
||||
{
|
||||
argument = hub_strdup(&start[1]);
|
||||
if (argument[strlen(argument)-1] == '\n')
|
||||
if (argument && argument[strlen(argument)-1] == '\n')
|
||||
argument[strlen(argument)-1] = 0;
|
||||
}
|
||||
|
||||
|
||||
if (!argument)
|
||||
return 0; // FIXME: OOM
|
||||
|
||||
if (*argument)
|
||||
{
|
||||
adc_msg_terminate(cmd);
|
||||
return argument;
|
||||
}
|
||||
else
|
||||
{
|
||||
hub_free(argument);
|
||||
}
|
||||
}
|
||||
|
||||
count++;
|
||||
start = end;
|
||||
}
|
||||
|
||||
|
||||
adc_msg_terminate(cmd);
|
||||
return 0;
|
||||
}
|
||||
@@ -773,7 +889,7 @@ int adc_msg_unescape_length(const char* str)
|
||||
|
||||
char* adc_msg_unescape(const char* string)
|
||||
{
|
||||
char* new_string = hub_malloc(adc_msg_unescape_length(string)+1);
|
||||
char* new_string = msg_malloc(adc_msg_unescape_length(string)+1);
|
||||
char* ptr = (char*) new_string;
|
||||
char* str = (char*) string;
|
||||
int escaped = 0;
|
||||
@@ -20,7 +20,7 @@
|
||||
#ifndef HAVE_UHUB_COMMAND_H
|
||||
#define HAVE_UHUB_COMMAND_H
|
||||
|
||||
struct user;
|
||||
struct hub_user;
|
||||
|
||||
struct adc_message
|
||||
{
|
||||
@@ -70,7 +70,7 @@ extern struct adc_message* adc_msg_copy(const struct adc_message* cmd);
|
||||
* The message is only considered valid if the user who sent it
|
||||
* is the rightful origin of the message.
|
||||
*/
|
||||
extern struct adc_message* adc_msg_parse_verify(struct user* u, const char* string, size_t length);
|
||||
extern struct adc_message* adc_msg_parse_verify(struct hub_user* u, const char* string, size_t length);
|
||||
|
||||
/**
|
||||
* This will parse 'string' and return it as a adc_message struct, or
|
||||
@@ -173,6 +173,22 @@ extern int adc_msg_add_argument(struct adc_message* cmd, const char* string);
|
||||
*/
|
||||
extern int adc_msg_add_named_argument(struct adc_message* cmd, const char prefix[2], const char* string);
|
||||
|
||||
/**
|
||||
* Append a string as a named argument.
|
||||
* The string will automatcally be escaped, if you do not wish to escape th string use adc_msg_add_named_argument() instead.
|
||||
*
|
||||
* @arg prefix a 2 character argument prefix
|
||||
* @arg string must NOT be escaped
|
||||
* @return 0 if successful, or -1 if an error occured (out of memory).
|
||||
*/
|
||||
extern int adc_msg_add_named_argument_string(struct adc_message* cmd, const char prefix[2], const char* string);
|
||||
|
||||
/**
|
||||
* Append an integer as a named argument.
|
||||
*/
|
||||
extern int adc_msg_add_named_argument_int(struct adc_message* cmd, const char prefix[2], int integer);
|
||||
extern int adc_msg_add_named_argument_uint64(struct adc_message* cmd, const char prefix[2], uint64_t num);
|
||||
|
||||
/**
|
||||
* Convert a ADC command escaped string to a regular string.
|
||||
* @return string or NULL if out of memory
|
||||
146
src/adc/sid.c
Normal file
146
src/adc/sid.c
Normal file
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
const char* BASE32_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
||||
|
||||
char* sid_to_string(sid_t sid_)
|
||||
{
|
||||
static char t_sid[5];
|
||||
sid_t sid = (sid_ & 0xFFFFF); /* 20 bits only */
|
||||
sid_t A, B, C, D = 0;
|
||||
D = (sid % 32);
|
||||
sid = (sid - D) / 32;
|
||||
C = (sid % 32);
|
||||
sid = (sid - C) / 32;
|
||||
B = (sid % 32);
|
||||
sid = (sid - B) / 32;
|
||||
A = (sid % 32);
|
||||
t_sid[0] = BASE32_ALPHABET[A];
|
||||
t_sid[1] = BASE32_ALPHABET[B];
|
||||
t_sid[2] = BASE32_ALPHABET[C];
|
||||
t_sid[3] = BASE32_ALPHABET[D];
|
||||
t_sid[4] = 0;
|
||||
return t_sid;
|
||||
}
|
||||
|
||||
|
||||
sid_t string_to_sid(const char* sid)
|
||||
{
|
||||
sid_t nsid = 0;
|
||||
sid_t n, x;
|
||||
sid_t factors[] = { 32768, 1024, 32, 1};
|
||||
|
||||
if (!sid || strlen(sid) != 4) return 0;
|
||||
|
||||
for (n = 0; n < 4; n++) {
|
||||
for (x = 0; x < strlen(BASE32_ALPHABET); x++)
|
||||
if (sid[n] == BASE32_ALPHABET[x]) break;
|
||||
if (x == 32) return 0;
|
||||
nsid += x * factors[n];
|
||||
}
|
||||
return nsid;
|
||||
}
|
||||
|
||||
/*
|
||||
* Session IDs are heavily reused, since they are a fairly scarce
|
||||
* resource. Only one (2^10)-1 exist, since it is a four byte base32-encoded
|
||||
* value and 'AAAA' (0) is reserved for the hub.
|
||||
*
|
||||
* Initialize with sid_initialize(), which sets min and max to one, and count to 0.
|
||||
*
|
||||
* When allocating a session ID:
|
||||
* - If 'count' is less than the pool size (max-min), then allocate within the pool
|
||||
* - Increase the pool size (see below)
|
||||
* - If unable to do that, hub is really full - don't let anyone in!
|
||||
*
|
||||
* When freeing a session ID:
|
||||
* - If the session ID being freed is 'max', then decrease the pool size by one.
|
||||
*
|
||||
*/
|
||||
|
||||
struct sid_pool
|
||||
{
|
||||
sid_t min;
|
||||
sid_t max;
|
||||
sid_t count;
|
||||
struct hub_user** map;
|
||||
};
|
||||
|
||||
|
||||
struct sid_pool* sid_pool_create(sid_t max)
|
||||
{
|
||||
struct sid_pool* pool = hub_malloc(sizeof(struct sid_pool));
|
||||
if (!pool)
|
||||
return 0;
|
||||
|
||||
pool->min = 1;
|
||||
pool->max = max + 1;
|
||||
pool->count = 0;
|
||||
pool->map = hub_malloc_zero(sizeof(struct hub_user*) * pool->max);
|
||||
if (!pool->map)
|
||||
{
|
||||
hub_free(pool);
|
||||
return 0;
|
||||
}
|
||||
pool->map[0] = (struct hub_user*) pool; /* hack to reserve the first sid. */
|
||||
|
||||
#ifdef DEBUG_SID
|
||||
LOG_DUMP("SID_POOL: max=%d", (int) pool->max);
|
||||
#endif
|
||||
return pool;
|
||||
}
|
||||
|
||||
void sid_pool_destroy(struct sid_pool* pool)
|
||||
{
|
||||
#ifdef DEBUG_SID
|
||||
LOG_DUMP("SID_POOL: destroying, current allocs=%d", (int) pool->count);
|
||||
#endif
|
||||
hub_free(pool->map);
|
||||
hub_free(pool);
|
||||
}
|
||||
|
||||
sid_t sid_alloc(struct sid_pool* pool, struct hub_user* user)
|
||||
{
|
||||
sid_t n = (++pool->count);
|
||||
for (; (pool->map[n % pool->max]); n++) ;
|
||||
|
||||
#ifdef DEBUG_SID
|
||||
LOG_DUMP("SID_ALLOC: %d, user=%p", (int) n, user);
|
||||
#endif
|
||||
pool->map[n] = user;
|
||||
return n;
|
||||
}
|
||||
|
||||
void sid_free(struct sid_pool* pool, sid_t sid)
|
||||
{
|
||||
#ifdef DEBUG_SID
|
||||
LOG_DUMP("SID_FREE: %d", (int) sid);
|
||||
#endif
|
||||
pool->map[sid] = 0;
|
||||
pool->count--;
|
||||
}
|
||||
|
||||
struct hub_user* sid_lookup(struct sid_pool* pool, sid_t sid)
|
||||
{
|
||||
if (!sid || (sid > pool->max))
|
||||
return 0;
|
||||
return pool->map[sid];
|
||||
}
|
||||
@@ -22,44 +22,19 @@
|
||||
|
||||
#define SID_MAX 1048576
|
||||
|
||||
struct sid_pool;
|
||||
struct hub_user;
|
||||
|
||||
extern const char* BASE32_ALPHABET;
|
||||
extern char* sid_to_string(sid_t sid_);
|
||||
extern sid_t string_to_sid(const char* sid);
|
||||
|
||||
struct sid_map
|
||||
{
|
||||
struct user* ptr;
|
||||
struct sid_map* next;
|
||||
};
|
||||
extern struct sid_pool* sid_pool_create(sid_t max);
|
||||
extern void sid_pool_destroy(struct sid_pool*);
|
||||
|
||||
/**
|
||||
* Session IDs are heavily reused, since they are a fairly scarce
|
||||
* resource. Only one (2^10)-1 exist, since it is a four byte base32-encoded
|
||||
* value and 'AAAA' (0) is reserved for the hub.
|
||||
*
|
||||
* Initialize with sid_initialize(), which sets min and max to one, and count to 0.
|
||||
*
|
||||
* When allocating a session ID:
|
||||
* - If 'count' is less than the pool size (max-min), then allocate within the pool
|
||||
* - Increase the pool size (see below)
|
||||
* - If unable to do that, hub is really full - don't let anyone in!
|
||||
*
|
||||
* When freeing a session ID:
|
||||
* - If the session ID being freed is 'max', then decrease the pool size by one.
|
||||
*
|
||||
*/
|
||||
struct sid_pool
|
||||
{
|
||||
sid_t min;
|
||||
sid_t max;
|
||||
sid_t count;
|
||||
struct sid_map* map;
|
||||
};
|
||||
|
||||
|
||||
extern void sid_initialize(struct sid_pool*);
|
||||
extern sid_t sid_alloc(struct sid_pool*, struct user*);
|
||||
extern sid_t sid_alloc(struct sid_pool*, struct hub_user*);
|
||||
extern void sid_free(struct sid_pool*, sid_t);
|
||||
extern struct hub_user* sid_lookup(struct sid_pool*, sid_t);
|
||||
|
||||
|
||||
|
||||
951
src/adcrush.c
951
src/adcrush.c
@@ -1,951 +0,0 @@
|
||||
/**
|
||||
* An ADC client emulator.
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
#define ADC_CLIENTS_DEFAULT 100
|
||||
#define ADC_MAX_CLIENTS 25000
|
||||
|
||||
#define ADC_BUFSIZE 16384
|
||||
#define ADC_SIDSIZE 4
|
||||
#define ADC_CID_SIZE 39
|
||||
|
||||
#define BIG_BUFSIZE 131072
|
||||
#define TIGERSIZE 24
|
||||
|
||||
#define ADC_HANDSHAKE "HSUP ADBASE ADTIGR\n"
|
||||
#define ADCRUSH "adcrush/0.2"
|
||||
#define ADC_NICK "[BOT]adcrush"
|
||||
#define ADC_DESC "crash\\stest\\sdummy"
|
||||
|
||||
struct ADC_client;
|
||||
|
||||
static void ADC_client_on_disconnected(struct ADC_client*);
|
||||
static void ADC_client_on_connected(struct ADC_client*);
|
||||
static void ADC_client_on_login(struct ADC_client*);
|
||||
static void ADC_client_connect(struct ADC_client*);
|
||||
static void ADC_client_disconnect(struct ADC_client*);
|
||||
static int ADC_client_create(struct ADC_client* client, int num);
|
||||
static void ADC_client_destroy(struct ADC_client* client);
|
||||
|
||||
static int cfg_mode = 0; // See enum operationMode
|
||||
static char* cfg_host = 0;
|
||||
static int cfg_port = 0;
|
||||
static int cfg_debug = 0; /* debug level */
|
||||
static int cfg_level = 1; /* activity level (0..3) */
|
||||
static int cfg_chat = 0; /* chat mode, allow sending chat messages */
|
||||
static int cfg_quiet = 0; /* quiet mode (no output) */
|
||||
static int cfg_clients = ADC_CLIENTS_DEFAULT; /* number of clients */
|
||||
|
||||
static int running = 1;
|
||||
|
||||
static struct sockaddr_in saddr;
|
||||
|
||||
enum commandMode
|
||||
{
|
||||
cm_bcast = 0x01, /* B - broadcast */
|
||||
cm_dir = 0x02, /* D - direct message */
|
||||
cm_echo = 0x04, /* E - echo message */
|
||||
cm_fcast = 0x08, /* F - feature cast message */
|
||||
cm_c2h = 0x10, /* H - client to hub message */
|
||||
cm_h2c = 0x20, /* I - hub to client message */
|
||||
cm_c2c = 0x40, /* C - client to client message */
|
||||
cm_udp = 0x80, /* U - udp message (client to client) */
|
||||
};
|
||||
|
||||
enum commandValidity
|
||||
{
|
||||
cv_protocol = 0x01,
|
||||
cv_identify = 0x02,
|
||||
cv_verify = 0x04,
|
||||
cv_normal = 0x08,
|
||||
};
|
||||
|
||||
enum protocolState
|
||||
{
|
||||
ps_none = 0x00, /* none or disconnected */
|
||||
ps_conn = 0x01, /* connecting... */
|
||||
ps_protocol = 0x02,
|
||||
ps_identify = 0x04,
|
||||
ps_verify = 0x08,
|
||||
ps_normal = 0x10,
|
||||
};
|
||||
|
||||
enum operationMode
|
||||
{
|
||||
mode_performance = 0x01,
|
||||
mode_bugs = 0x02,
|
||||
mode_security = 0x04,
|
||||
mode_log = 0x08,
|
||||
};
|
||||
|
||||
struct commandPattern
|
||||
{
|
||||
unsigned char mode; /* see enum commandMode */
|
||||
char cmd[3];
|
||||
unsigned char validity; /* see enum commandValidity */
|
||||
};
|
||||
|
||||
const struct commandPattern patterns[] =
|
||||
{
|
||||
{ cm_c2h | cm_c2c | cm_h2c, "SUP", cv_protocol | cv_normal }, /* protocol support */
|
||||
{ cm_bcast | cm_h2c | cm_c2c, "INF", cv_identify | cv_verify | cv_normal }, /* info message */
|
||||
{ cm_bcast | cm_h2c | cm_c2c | cm_c2h | cm_udp, "STA", cv_protocol | cv_identify | cv_verify | cv_normal }, /* status message */
|
||||
{ cm_bcast | cm_dir | cm_echo | cm_h2c, "MSG", cv_normal }, /* chat message */
|
||||
{ cm_bcast | cm_dir | cm_echo | cm_fcast, "SCH", cv_normal }, /* search */
|
||||
{ cm_dir | cm_udp, "RES", cv_normal }, /* search result */
|
||||
{ cm_dir | cm_echo, "CTM", cv_normal }, /* connect to me */
|
||||
{ cm_dir | cm_echo, "RCM", cv_normal }, /* reversed, connect to me */
|
||||
{ cm_h2c, "QUI", cv_normal }, /* quit message */
|
||||
{ cm_h2c, "GPA", cv_identify }, /* password request */
|
||||
{ cm_c2h, "PAS", cv_verify } /* password response */
|
||||
};
|
||||
|
||||
#define MAX_CHAT_MSGS 35
|
||||
const char* chat_messages[MAX_CHAT_MSGS] = {
|
||||
"hello",
|
||||
"I'm an annoying robot, configured to chat in order to measure performance of the hub.",
|
||||
"I apologize for the inconvenience.",
|
||||
".",
|
||||
":)",
|
||||
"can anyone help me, pls?",
|
||||
"wtf?",
|
||||
"bullshit",
|
||||
"resistance is futile.",
|
||||
"You crossed the line first, sir. You squeezed them, you hammered them to the point of desperation. And in their desperation they turned to a man they didn't fully understand.",
|
||||
"beam me up, scotty",
|
||||
"morning",
|
||||
"You know where Harvey is? You know who he is?",
|
||||
"gtg",
|
||||
"thanks",
|
||||
"*punt*",
|
||||
"*nudge*",
|
||||
"that's ok",
|
||||
"...anyway",
|
||||
"hola",
|
||||
"hey",
|
||||
"hi",
|
||||
"nevermind",
|
||||
"i think so",
|
||||
"dunno",
|
||||
"debian ftw",
|
||||
"oops",
|
||||
"how do I search?",
|
||||
"how do I enable active mode?",
|
||||
"home, sweet home...",
|
||||
"later",
|
||||
"Good evening, ladies and gentlemen. We are tonight's entertainment! I only have one question. Where is Harvey Dent?",
|
||||
"You know where I can find Harvey? I need to talk to him about something. Just something, a little.",
|
||||
"We really should stop fighting, we'll miss the fireworks!",
|
||||
"Wanna know how I got these scars?",
|
||||
};
|
||||
|
||||
#define MAX_SEARCH_MSGS 10
|
||||
const char* search_messages[MAX_SEARCH_MSGS] = {
|
||||
"ANmp3 TOauto",
|
||||
"ANxxx TOauto",
|
||||
"ANdivx TOauto",
|
||||
"ANtest ANfoo TOauto",
|
||||
"ANwmv TO1289718",
|
||||
"ANbabe TO8981884",
|
||||
"ANpr0n TOauto",
|
||||
"ANmusic TOauto",
|
||||
"ANvideo TOauto",
|
||||
"ANburnout ANps3 TOauto",
|
||||
};
|
||||
|
||||
struct ADC_client
|
||||
{
|
||||
int sd;
|
||||
int num;
|
||||
sid_t sid;
|
||||
enum protocolState state;
|
||||
char info[ADC_BUFSIZE];
|
||||
char recvbuf[BIG_BUFSIZE];
|
||||
char sendbuf[BIG_BUFSIZE];
|
||||
size_t s_offset;
|
||||
size_t r_offset;
|
||||
struct event ev_read;
|
||||
struct event ev_write;
|
||||
struct event ev_timer;
|
||||
size_t timeout;
|
||||
};
|
||||
|
||||
|
||||
|
||||
static void bot_output(struct ADC_client* client, const char* format, ...)
|
||||
{
|
||||
char logmsg[1024];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(logmsg, 1024, format, args);
|
||||
va_end(args);
|
||||
if (cfg_mode == mode_log)
|
||||
{
|
||||
fprintf(stdout, "%s\n", logmsg);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cfg_debug)
|
||||
fprintf(stdout, "* [%4d] %s\n", client->num, logmsg);
|
||||
}
|
||||
}
|
||||
|
||||
static void adc_cid_pid(struct ADC_client* client)
|
||||
{
|
||||
char seed[64];
|
||||
char pid[64];
|
||||
char cid[64];
|
||||
uint64_t tiger_res1[3];
|
||||
uint64_t tiger_res2[3];
|
||||
|
||||
/* create cid+pid pair */
|
||||
memset(seed, 0, 64);
|
||||
snprintf(seed, 64, ADCRUSH "%p/%d", client, (int) client->num);
|
||||
|
||||
tiger((uint64_t*) seed, strlen(seed), tiger_res1);
|
||||
base32_encode((unsigned char*) tiger_res1, TIGERSIZE, pid);
|
||||
tiger((uint64_t*) tiger_res1, TIGERSIZE, tiger_res2);
|
||||
base32_encode((unsigned char*) tiger_res2, TIGERSIZE, cid);
|
||||
|
||||
cid[ADC_CID_SIZE] = 0;
|
||||
pid[ADC_CID_SIZE] = 0;
|
||||
|
||||
strcat(client->info, " PD");
|
||||
strcat(client->info, pid);
|
||||
strcat(client->info, " ID");
|
||||
strcat(client->info, cid);
|
||||
}
|
||||
|
||||
static size_t get_wait_rand(size_t max)
|
||||
{
|
||||
static size_t next = 0;
|
||||
if (next == 0) next = (size_t) time(0);
|
||||
next = (next * 1103515245) + 12345;
|
||||
return ((size_t )(next / 65536) % max);
|
||||
}
|
||||
|
||||
static void client_reschedule_timeout(struct ADC_client* client)
|
||||
{
|
||||
size_t next_timeout = 0;
|
||||
struct timeval timeout = { 0, 0 };
|
||||
|
||||
switch (client->state)
|
||||
{
|
||||
case ps_conn: next_timeout = 30; break;
|
||||
case ps_protocol: next_timeout = 30; break;
|
||||
case ps_identify: next_timeout = 30; break;
|
||||
case ps_verify: next_timeout = 30; break;
|
||||
case ps_normal: next_timeout = 120; break;
|
||||
case ps_none: next_timeout = 120; break;
|
||||
}
|
||||
|
||||
if (client->state == ps_normal || client->state == ps_none)
|
||||
{
|
||||
switch (cfg_level)
|
||||
{
|
||||
case 0: /* polite */
|
||||
next_timeout *= 4;
|
||||
break;
|
||||
|
||||
case 1: /* normal */
|
||||
break;
|
||||
|
||||
case 2: /* aggressive */
|
||||
next_timeout /= 8;
|
||||
break;
|
||||
|
||||
case 3: /* excessive */
|
||||
next_timeout /= 16;
|
||||
|
||||
case 4: /* excessive */
|
||||
next_timeout /= 32;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (client->state == ps_conn)
|
||||
client->timeout = MAX(next_timeout, 1);
|
||||
else
|
||||
client->timeout = get_wait_rand(MAX(next_timeout, 1));
|
||||
|
||||
if (!client->timeout) client->timeout++;
|
||||
|
||||
timeout.tv_sec = (time_t) client->timeout;
|
||||
evtimer_add(&client->ev_timer, &timeout);
|
||||
}
|
||||
|
||||
static void set_state_timeout(struct ADC_client* client, enum protocolState state)
|
||||
{
|
||||
client->state = state;
|
||||
client_reschedule_timeout(client);
|
||||
}
|
||||
|
||||
static void send_client(struct ADC_client* client, char* msg)
|
||||
{
|
||||
int ret = net_send(client->sd, msg, strlen(msg), UHUB_SEND_SIGNAL);
|
||||
|
||||
if (cfg_debug > 1)
|
||||
{
|
||||
char* dump = strdup(msg);
|
||||
dump[strlen(msg) - 1] = 0;
|
||||
bot_output(client, "- SEND: '%s'", dump);
|
||||
free(dump);
|
||||
}
|
||||
|
||||
if (ret != strlen(msg))
|
||||
{
|
||||
if (ret == -1)
|
||||
{
|
||||
if (net_error() != EWOULDBLOCK)
|
||||
ADC_client_on_disconnected(client);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* FIXME: Not all data sent! */
|
||||
printf("ret (%d) != msg->length (%d)\n", ret, (int) strlen(msg));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void ADC_client_on_connected(struct ADC_client* client)
|
||||
{
|
||||
send_client(client, ADC_HANDSHAKE);
|
||||
set_state_timeout(client, ps_protocol);
|
||||
bot_output(client, "connected.");
|
||||
}
|
||||
|
||||
static void ADC_client_on_disconnected(struct ADC_client* client)
|
||||
{
|
||||
event_del(&client->ev_read);
|
||||
event_del(&client->ev_write);
|
||||
|
||||
net_close(client->sd);
|
||||
client->sd = -1;
|
||||
|
||||
bot_output(client, "disconnected.");
|
||||
set_state_timeout(client, ps_none);
|
||||
}
|
||||
|
||||
static void ADC_client_on_login(struct ADC_client* client)
|
||||
{
|
||||
bot_output(client, "logged in.");
|
||||
set_state_timeout(client, ps_normal);
|
||||
}
|
||||
|
||||
|
||||
static void send_client_info(struct ADC_client* client)
|
||||
{
|
||||
client->info[0] = 0;
|
||||
strcat(client->info, "BINF ");
|
||||
strcat(client->info, sid_to_string(client->sid));
|
||||
strcat(client->info, " NI" ADC_NICK);
|
||||
if (cfg_clients > 1)
|
||||
{
|
||||
strcat(client->info, "_");
|
||||
strcat(client->info, uhub_itoa(client->num));
|
||||
}
|
||||
strcat(client->info, " VE" ADCRUSH);
|
||||
strcat(client->info, " DE" ADC_DESC);
|
||||
strcat(client->info, " I40.0.0.0");
|
||||
strcat(client->info, " EMuhub@extatic.org");
|
||||
strcat(client->info, " SL3");
|
||||
strcat(client->info, " HN1");
|
||||
strcat(client->info, " HR1");
|
||||
strcat(client->info, " HO1");
|
||||
|
||||
adc_cid_pid(client);
|
||||
|
||||
strcat(client->info, "\n");
|
||||
|
||||
send_client(client, client->info);
|
||||
}
|
||||
|
||||
static void perf_result(struct ADC_client* client, sid_t target, const char* what, const char* token);
|
||||
|
||||
static int recv_client(struct ADC_client* client)
|
||||
{
|
||||
ssize_t size = 0;
|
||||
if (cfg_mode != mode_performance || (cfg_mode == mode_performance && (get_wait_rand(100) < (90 - (15 * cfg_level)))))
|
||||
{
|
||||
size = net_recv(client->sd, &client->recvbuf[client->r_offset], ADC_BUFSIZE - client->r_offset, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (get_wait_rand(1000) == 99)
|
||||
return -1; /* Can break tings badly! :-) */
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
if (size == 0 || ((size == -1 && net_error() != EWOULDBLOCK)))
|
||||
return -1;
|
||||
client->recvbuf[client->r_offset + size] = 0;
|
||||
|
||||
char* start = client->recvbuf;
|
||||
char* pos;
|
||||
char* lastPos;
|
||||
while ((pos = strchr(start, '\n')))
|
||||
{
|
||||
lastPos = pos;
|
||||
pos[0] = 0;
|
||||
|
||||
if (cfg_debug > 1)
|
||||
{
|
||||
bot_output(client, "- RECV: '%s'", start);
|
||||
}
|
||||
|
||||
fourcc_t cmd = 0;
|
||||
if (strlen(start) < 4)
|
||||
{
|
||||
bot_output(client, "Unexpected response from hub: '%s'", start);
|
||||
start = &pos[1];
|
||||
continue;
|
||||
}
|
||||
|
||||
cmd = FOURCC(start[0], start[1], start[2], start[3]);
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case ADC_CMD_ISUP:
|
||||
break;
|
||||
|
||||
case ADC_CMD_ISID:
|
||||
if (client->state == ps_protocol)
|
||||
{
|
||||
client->sid = string_to_sid(&start[5]);
|
||||
client->state = ps_identify;
|
||||
send_client_info(client);
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case ADC_CMD_IINF:
|
||||
break;
|
||||
|
||||
case ADC_CMD_BSCH:
|
||||
case ADC_CMD_FSCH:
|
||||
{
|
||||
if (get_wait_rand(100) > (90 - (10 * cfg_level)) && cfg_mode == mode_performance)
|
||||
{
|
||||
sid_t target = string_to_sid(&start[5]);
|
||||
const char* what = strstr(&start[5], " AN");
|
||||
const char* token = strstr(&start[5], " TO");
|
||||
char* split = 0;
|
||||
if (!token || !what) break;
|
||||
|
||||
token += 3;
|
||||
what += 3;
|
||||
|
||||
split = strchr(what, ' ');
|
||||
if (!split) break;
|
||||
else split[0] = '0';
|
||||
|
||||
split = strchr(token, ' ');
|
||||
if (split) split[0] = '0';
|
||||
|
||||
perf_result(client, target, what, token);
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ADC_CMD_BINF:
|
||||
{
|
||||
if (strlen(start) > 9)
|
||||
{
|
||||
char t = start[9]; start[9] = 0; sid_t sid = string_to_sid(&start[5]); start[9] = t;
|
||||
|
||||
if (sid == client->sid)
|
||||
{
|
||||
if (client->state == ps_verify || client->state == ps_identify)
|
||||
{
|
||||
ADC_client_on_login(client);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ADC_CMD_ISTA:
|
||||
if (strncmp(start, "ISTA 000", 8))
|
||||
{
|
||||
bot_output(client, "status: '%s'\n", (start + 9));
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
start = &pos[1];
|
||||
}
|
||||
|
||||
client->r_offset = strlen(lastPos);
|
||||
memmove(client->recvbuf, lastPos, strlen(lastPos));
|
||||
memset(&client->recvbuf[client->r_offset], 0, ADC_BUFSIZE-client->r_offset);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ADC_client_connect(struct ADC_client* client)
|
||||
{
|
||||
struct timeval timeout = { TIMEOUT_IDLE, 0 };
|
||||
net_connect(client->sd, (struct sockaddr*) &saddr, sizeof(struct sockaddr_in));
|
||||
set_state_timeout(client, ps_conn);
|
||||
event_add(&client->ev_read, &timeout);
|
||||
event_add(&client->ev_write, &timeout);
|
||||
bot_output(client, "connecting...");
|
||||
}
|
||||
|
||||
void ADC_client_wait_connect(struct ADC_client* client)
|
||||
{
|
||||
set_state_timeout(client, ps_none);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ADC_client_disconnect(struct ADC_client* client)
|
||||
{
|
||||
if (client->sd != -1)
|
||||
{
|
||||
net_close(client->sd);
|
||||
client->sd = -1;
|
||||
event_del(&client->ev_read);
|
||||
event_del(&client->ev_write);
|
||||
bot_output(client, "disconnected.");
|
||||
|
||||
if (running)
|
||||
{
|
||||
ADC_client_destroy(client);
|
||||
ADC_client_create(client, client->num);
|
||||
ADC_client_connect(client);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static void perf_chat(struct ADC_client* client, int priv)
|
||||
{
|
||||
char buf[1024] = { 0, };
|
||||
size_t r = get_wait_rand(MAX_CHAT_MSGS-1);
|
||||
char* msg = adc_msg_escape(chat_messages[r]);
|
||||
|
||||
if (priv)
|
||||
{
|
||||
strcat(buf, "EMSG ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, " ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
}
|
||||
else
|
||||
{
|
||||
strcat(buf, "BMSG ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
}
|
||||
strcat(buf, " ");
|
||||
|
||||
strcat(buf, msg);
|
||||
hub_free(msg);
|
||||
|
||||
strcat(buf, "\n");
|
||||
send_client(client, buf);
|
||||
}
|
||||
|
||||
static void perf_search(struct ADC_client* client)
|
||||
{
|
||||
char buf[1024] = { 0, };
|
||||
size_t r = get_wait_rand(MAX_SEARCH_MSGS-1);
|
||||
size_t pst = get_wait_rand(100);
|
||||
|
||||
if (pst > 80)
|
||||
{
|
||||
strcat(buf, "FSCH ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, " +TCP4 ");
|
||||
}
|
||||
else
|
||||
{
|
||||
strcat(buf, "BSCH ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, " ");
|
||||
}
|
||||
strcat(buf, search_messages[r]);
|
||||
strcat(buf, "\n");
|
||||
send_client(client, buf);
|
||||
}
|
||||
|
||||
static void perf_result(struct ADC_client* client, sid_t target, const char* what, const char* token)
|
||||
{
|
||||
char buf[1024] = { 0, };
|
||||
strcat(buf, "DRES ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, " ");
|
||||
strcat(buf, sid_to_string(target));
|
||||
strcat(buf, " FN" "test/");
|
||||
strcat(buf, what);
|
||||
strcat(buf, ".dat");
|
||||
strcat(buf, " SL" "0");
|
||||
strcat(buf, " SI" "908987128912");
|
||||
strcat(buf, " TR" "5T6YJYKO3WECS52BKWVSOP5VUG4IKNSZBZ5YHBA");
|
||||
strcat(buf, " TO");
|
||||
strcat(buf, token);
|
||||
strcat(buf, "\n");
|
||||
send_client(client, buf);
|
||||
}
|
||||
|
||||
static void perf_ctm(struct ADC_client* client)
|
||||
{
|
||||
char buf[1024] = { 0, };
|
||||
strcat(buf, "DCTM ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, " ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, " ");
|
||||
strcat(buf, "ADC/1.0");
|
||||
strcat(buf, " TOKEN111");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, "\n");
|
||||
send_client(client, buf);
|
||||
}
|
||||
|
||||
|
||||
static void perf_update(struct ADC_client* client)
|
||||
{
|
||||
char buf[1024] = { 0, };
|
||||
int n = (int) get_wait_rand(10)+1;
|
||||
|
||||
strcat(buf, "BINF ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, " HN");
|
||||
strcat(buf, uhub_itoa(n));
|
||||
|
||||
strcat(buf, "\n");
|
||||
send_client(client, buf);
|
||||
}
|
||||
|
||||
|
||||
static void perf_normal_action(struct ADC_client* client)
|
||||
{
|
||||
size_t r = get_wait_rand(5);
|
||||
size_t p = get_wait_rand(100);
|
||||
|
||||
switch (r)
|
||||
{
|
||||
case 0:
|
||||
if (p > (90 - (10 * cfg_level)))
|
||||
{
|
||||
if (cfg_debug > 1) bot_output(client, "timeout -> disconnect");
|
||||
ADC_client_disconnect(client);
|
||||
}
|
||||
break;
|
||||
|
||||
case 1:
|
||||
if (cfg_chat)
|
||||
{
|
||||
if (cfg_debug > 1) bot_output(client, "timeout -> chat");
|
||||
perf_chat(client, 0);
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
if (cfg_debug > 1) bot_output(client, "timeout -> search");
|
||||
perf_search(client);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
if (cfg_debug > 1) bot_output(client, "timeout -> update");
|
||||
perf_update(client);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
if (cfg_debug > 1) bot_output(client, "timeout -> privmsg");
|
||||
perf_chat(client, 1);
|
||||
break;
|
||||
|
||||
case 5:
|
||||
if (cfg_debug > 1) bot_output(client, "timeout -> ctm/rcm");
|
||||
perf_ctm(client);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
client_reschedule_timeout(client);
|
||||
}
|
||||
|
||||
void event_callback(int fd, short ev, void *arg)
|
||||
{
|
||||
struct ADC_client* client = (struct ADC_client*) arg;
|
||||
|
||||
if (ev & EV_READ)
|
||||
{
|
||||
if (recv_client(client) == -1)
|
||||
{
|
||||
ADC_client_on_disconnected(client);
|
||||
}
|
||||
}
|
||||
|
||||
if (ev & EV_TIMEOUT)
|
||||
{
|
||||
if (client->state == ps_none)
|
||||
{
|
||||
if (client->sd == -1)
|
||||
{
|
||||
ADC_client_create(client, client->num);
|
||||
}
|
||||
|
||||
ADC_client_connect(client);
|
||||
}
|
||||
|
||||
if (fd == -1)
|
||||
{
|
||||
if (client->state == ps_normal && cfg_mode == mode_performance)
|
||||
{
|
||||
perf_normal_action(client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ev & EV_WRITE)
|
||||
{
|
||||
if (client->state == ps_conn)
|
||||
{
|
||||
ADC_client_on_connected(client);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* FIXME: Call send again */
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
int ADC_client_create(struct ADC_client* client, int num)
|
||||
{
|
||||
struct timeval timeout = { 0, 0 };
|
||||
|
||||
memset(client, 0, sizeof(struct ADC_client));
|
||||
client->num = num;
|
||||
|
||||
client->sd = net_socket_create(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (client->sd == -1) return -1;
|
||||
|
||||
event_set(&client->ev_write, client->sd, EV_WRITE, event_callback, client);
|
||||
event_set(&client->ev_read, client->sd, EV_READ | EV_PERSIST, event_callback, client);
|
||||
|
||||
net_set_nonblocking(client->sd, 1);
|
||||
|
||||
timeout.tv_sec = client->timeout;
|
||||
evtimer_set(&client->ev_timer, event_callback, client);
|
||||
|
||||
set_state_timeout(client, ps_none);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ADC_client_destroy(struct ADC_client* client)
|
||||
{
|
||||
ADC_client_disconnect(client);
|
||||
evtimer_del(&client->ev_timer);
|
||||
}
|
||||
|
||||
|
||||
void runloop(size_t clients)
|
||||
{
|
||||
size_t n = 0;
|
||||
struct ADC_client* client[ADC_MAX_CLIENTS];
|
||||
|
||||
for (n = 0; n < clients; n++)
|
||||
{
|
||||
struct ADC_client* c = malloc(sizeof(struct ADC_client));
|
||||
client[n] = c;
|
||||
|
||||
ADC_client_create(c, n);
|
||||
if (n == 0)
|
||||
{
|
||||
ADC_client_connect(c);
|
||||
}
|
||||
else
|
||||
{
|
||||
ADC_client_wait_connect(c);
|
||||
}
|
||||
}
|
||||
|
||||
event_dispatch();
|
||||
|
||||
for (n = 0; n < clients; n++)
|
||||
{
|
||||
ADC_client_destroy(client[n]);
|
||||
free(client[n]);
|
||||
}
|
||||
}
|
||||
|
||||
static void print_version()
|
||||
{
|
||||
printf(ADCRUSH "\n");
|
||||
printf("Copyright (C) 2008-2009, Jan Vidar Krey\n");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void print_usage(const char* program)
|
||||
{
|
||||
print_version();
|
||||
|
||||
printf("Usage: %s <mode> (adc://<host>:<port>) [options]\n", program);
|
||||
|
||||
printf("\n");
|
||||
printf(" Modes\n");
|
||||
printf(" perf Do performance testing using multiple clients\n");
|
||||
printf(" bugs Bugs mode, use fuzzer to construct pseudo-random commands.\n");
|
||||
printf(" security Perform security tests for the hub.\n");
|
||||
printf(" log Connect one client to the hub and log the output hub.\n");
|
||||
|
||||
printf("\n");
|
||||
printf(" General options\n");
|
||||
printf(" -c Allow broadcasting chat messages.\n");
|
||||
printf(" -d Enable debug output.\n");
|
||||
printf(" -q Quiet mode (no output).\n");
|
||||
|
||||
printf("\n");
|
||||
printf(" Performance options:\n");
|
||||
printf(" -l <0-3> Level: 0=polite, 1=normal (default), 2=aggressive, 3=excessive.\n");
|
||||
printf(" -n <num> Number of concurrent connections\n");
|
||||
|
||||
printf("\n");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
int set_defaults()
|
||||
{
|
||||
switch (cfg_mode)
|
||||
{
|
||||
case mode_performance:
|
||||
break;
|
||||
case mode_bugs:
|
||||
break;
|
||||
case mode_security:
|
||||
break;
|
||||
case mode_log:
|
||||
cfg_quiet = 0;
|
||||
cfg_debug = 2;
|
||||
cfg_clients = 1;
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int parse_mode(const char* arg)
|
||||
{
|
||||
cfg_mode = 0;
|
||||
|
||||
if (!strcmp(arg, "perf"))
|
||||
cfg_mode = mode_performance;
|
||||
else if (!strcmp(arg, "bugs"))
|
||||
cfg_mode = mode_bugs;
|
||||
else if (!strcmp(arg, "security"))
|
||||
cfg_mode = mode_security;
|
||||
else if (!strcmp(arg, "log"))
|
||||
cfg_mode = mode_log;
|
||||
|
||||
return cfg_mode;
|
||||
}
|
||||
|
||||
int parse_address(const char* arg)
|
||||
{
|
||||
char* split;
|
||||
struct hostent* dns;
|
||||
struct in_addr* addr;
|
||||
|
||||
if (!arg)
|
||||
return 0;
|
||||
|
||||
if (strlen(arg) < 9)
|
||||
return 0;
|
||||
|
||||
if (strncmp(arg, "adc://", 6))
|
||||
return 0;
|
||||
|
||||
split = strrchr(arg+6, ':');
|
||||
if (split == 0 || strlen(split) < 2 || strlen(split) > 6)
|
||||
return 0;
|
||||
|
||||
cfg_port = strtol(split+1, NULL, 10);
|
||||
if (cfg_port <= 0 || cfg_port > 65535)
|
||||
return 0;
|
||||
|
||||
split[0] = 0;
|
||||
|
||||
dns = gethostbyname(arg+6);
|
||||
if (dns)
|
||||
{
|
||||
addr = (struct in_addr*) dns->h_addr_list[0];
|
||||
cfg_host = strdup(inet_ntoa(*addr));
|
||||
}
|
||||
|
||||
if (!cfg_host)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int parse_arguments(int argc, char** argv)
|
||||
{
|
||||
int ok = 1;
|
||||
int opt;
|
||||
for (opt = 3; opt < argc; opt++)
|
||||
{
|
||||
if (!strcmp(argv[opt], "-c"))
|
||||
cfg_chat = 1;
|
||||
else if (!strncmp(argv[opt], "-d", 2))
|
||||
cfg_debug += strlen(argv[opt]) - 1;
|
||||
else if (!strcmp(argv[opt], "-q"))
|
||||
cfg_quiet = 1;
|
||||
else if (!strcmp(argv[opt], "-l") && (++opt) < argc)
|
||||
{
|
||||
cfg_level = MIN(MAX(uhub_atoi(argv[opt]), 0), 3);
|
||||
}
|
||||
else if (!strcmp(argv[opt], "-n") && (++opt) < argc)
|
||||
{
|
||||
cfg_clients = MIN(MAX(uhub_atoi(argv[opt]), 1), ADC_MAX_CLIENTS);
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
void parse_command_line(int argc, char** argv)
|
||||
{
|
||||
if (argc < 2 ||
|
||||
!parse_mode(argv[1]) ||
|
||||
!set_defaults() ||
|
||||
!parse_address(argv[2]) ||
|
||||
!parse_arguments(argc, argv))
|
||||
{
|
||||
print_usage(argv[0]);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
saddr.sin_port = htons(cfg_port);
|
||||
net_string_to_address(AF_INET, cfg_host, &saddr.sin_addr);
|
||||
|
||||
runloop(cfg_clients);
|
||||
net_shutdown();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
193
src/commands.c
193
src/commands.c
@@ -1,193 +0,0 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
typedef int (*command_handler)(struct user* user, const char* message);
|
||||
|
||||
struct commands_handler
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
send_message(user, temp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int command_help(struct user* user, const char* message)
|
||||
{
|
||||
#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)
|
||||
{
|
||||
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;
|
||||
|
||||
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, ", ");
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
send_message(user, "*** Powered by " PRODUCT "/" VERSION);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_myip(struct user* user, const char* message)
|
||||
{
|
||||
char tmp[128];
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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 }" }
|
||||
};
|
||||
|
||||
@@ -55,12 +55,12 @@ static int check_cmd_bool(const char* cmd, struct linked_list* list, char* line,
|
||||
data = strip_white_space(data);
|
||||
if (!*data)
|
||||
{
|
||||
hub_log(log_fatal, "ACL parse error on line %d", line_count);
|
||||
LOG_FATAL("ACL parse error on line %d", line_count);
|
||||
return -1;
|
||||
}
|
||||
|
||||
list_append(list, hub_strdup(data));
|
||||
hub_log(log_debug, "ACL: Deny access for: '%s' (%s)", data, cmd);
|
||||
LOG_DEBUG("ACL: Deny access for: '%s' (%s)", data, cmd);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
@@ -70,7 +70,7 @@ static int check_cmd_user(const char* cmd, int status, struct linked_list* list,
|
||||
{
|
||||
char* data;
|
||||
char* data_extra;
|
||||
struct user_access_info* info = 0;
|
||||
struct hub_user_access_info* info = 0;
|
||||
|
||||
if (!strncmp(line, cmd, strlen(cmd)))
|
||||
{
|
||||
@@ -82,15 +82,15 @@ static int check_cmd_user(const char* cmd, int status, struct linked_list* list,
|
||||
data = strip_white_space(data);
|
||||
if (!*data)
|
||||
{
|
||||
hub_log(log_fatal, "ACL parse error on line %d", line_count);
|
||||
LOG_FATAL("ACL parse error on line %d", line_count);
|
||||
return -1;
|
||||
}
|
||||
|
||||
info = hub_malloc_zero(sizeof(struct user_access_info));
|
||||
info = hub_malloc_zero(sizeof(struct hub_user_access_info));
|
||||
|
||||
if (!info)
|
||||
{
|
||||
hub_log(log_error, "ACL parse error. Out of memory!");
|
||||
LOG_ERROR("ACL parse error. Out of memory!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -108,14 +108,14 @@ static int check_cmd_user(const char* cmd, int status, struct linked_list* list,
|
||||
info->password = data_extra ? hub_strdup(data_extra) : 0;
|
||||
info->status = status;
|
||||
list_append(list, info);
|
||||
hub_log(log_debug, "ACL: Added user '%s' (%s)", info->username, get_user_credential_string(info->status));
|
||||
LOG_DEBUG("ACL: Added user '%s' (%s)", info->username, get_user_credential_string(info->status));
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void add_ip_range(struct linked_list* list, struct ip_ban_record* info)
|
||||
static void add_ip_range(struct linked_list* list, struct ip_range* info)
|
||||
{
|
||||
char buf1[INET6_ADDRSTRLEN+1];
|
||||
char buf2[INET6_ADDRSTRLEN+1];
|
||||
@@ -130,125 +130,44 @@ static void add_ip_range(struct linked_list* list, struct ip_ban_record* info)
|
||||
net_address_to_string(AF_INET6, &info->lo.internal_ip_data.in6, buf1, INET6_ADDRSTRLEN);
|
||||
net_address_to_string(AF_INET6, &info->hi.internal_ip_data.in6, buf2, INET6_ADDRSTRLEN);
|
||||
}
|
||||
hub_log(log_debug, "ACL: Deny access for: %s-%s", buf1, buf2);
|
||||
LOG_DEBUG("ACL: Deny access for: %s-%s", buf1, buf2);
|
||||
|
||||
list_append(list, info);
|
||||
}
|
||||
|
||||
|
||||
static int check_ip_range(const char* lo, const char* hi, struct ip_ban_record* info)
|
||||
{
|
||||
int ret1, ret2;
|
||||
|
||||
if ((ip_is_valid_ipv4(lo) && ip_is_valid_ipv4(hi)) ||
|
||||
(ip_is_valid_ipv6(lo) && ip_is_valid_ipv6(hi)))
|
||||
{
|
||||
ret1 = ip_convert_to_binary(lo, &info->lo);
|
||||
ret2 = ip_convert_to_binary(hi, &info->hi);
|
||||
if (ret1 == -1 || ret2 == -1 || ret1 != ret2)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
static int check_ip_mask(const char* text_addr, int bits, struct ip_ban_record* info)
|
||||
{
|
||||
hub_log(log_debug, "ACL: Deny access for: %s/%d", text_addr, bits);
|
||||
|
||||
if (ip_is_valid_ipv4(text_addr) ||
|
||||
ip_is_valid_ipv6(text_addr))
|
||||
{
|
||||
struct ip_addr_encap addr;
|
||||
struct ip_addr_encap mask1;
|
||||
struct ip_addr_encap mask2;
|
||||
int af = ip_convert_to_binary(text_addr, &addr); /* 192.168.1.2 */
|
||||
int maxbits = af == AF_INET6 ? 128 : 32;
|
||||
ip_mask_create_left(af, bits, &mask1); /* 255.255.255.0 */
|
||||
ip_mask_create_right(af, maxbits - bits, &mask2); /* 0.0.0.255 */
|
||||
ip_mask_apply_AND(&addr, &mask1, &info->lo); /* 192.168.1.0 */
|
||||
ip_mask_apply_OR(&info->lo, &mask2, &info->hi); /* 192.168.1.255 */
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
static int check_cmd_addr(const char* cmd, struct linked_list* list, char* line, int line_count)
|
||||
{
|
||||
char* data1;
|
||||
char* data2;
|
||||
struct ip_ban_record* info = 0;
|
||||
int cidr_bits = 0;
|
||||
|
||||
char* data;
|
||||
struct ip_range* range = 0;
|
||||
|
||||
if (!strncmp(line, cmd, strlen(cmd)))
|
||||
{
|
||||
data1 = &line[strlen(cmd)];
|
||||
data2 = 0;
|
||||
data1[0] = '\0';
|
||||
data1++;
|
||||
|
||||
data1 = strip_white_space(data1);
|
||||
if (!*data1)
|
||||
{
|
||||
hub_log(log_fatal, "ACL parse error on line %d", line_count);
|
||||
return -1;
|
||||
}
|
||||
|
||||
info = hub_malloc_zero(sizeof(struct ip_ban_record));
|
||||
|
||||
if (!info)
|
||||
{
|
||||
hub_log(log_error, "ACL parse error. Out of memory!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Extract IP-range */
|
||||
data2 = strrchr(data1, '-');
|
||||
if (data2)
|
||||
{
|
||||
cidr_bits = -1;
|
||||
data2[0] = 0;
|
||||
data2++;
|
||||
|
||||
if (check_ip_range(data1, data2, info) == -1)
|
||||
{
|
||||
hub_free(info);
|
||||
return 0;
|
||||
}
|
||||
|
||||
add_ip_range(list, info);
|
||||
data = &line[strlen(cmd)];
|
||||
data[0] = '\0';
|
||||
data++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
data = strip_white_space(data);
|
||||
if (!*data)
|
||||
{
|
||||
/* Extract IP-bitmask */
|
||||
data2 = strrchr(data1, '/');
|
||||
if (data2)
|
||||
{
|
||||
data2[0] = 0;
|
||||
data2++;
|
||||
cidr_bits = uhub_atoi(data2);
|
||||
}
|
||||
else
|
||||
{
|
||||
cidr_bits = 128;
|
||||
}
|
||||
|
||||
if (check_ip_mask(data1, cidr_bits, info) == -1)
|
||||
{
|
||||
hub_free(info);
|
||||
return 0;
|
||||
}
|
||||
|
||||
add_ip_range(list, info);
|
||||
|
||||
LOG_FATAL("ACL parse error on line %d", line_count);
|
||||
return -1;
|
||||
}
|
||||
|
||||
range = hub_malloc_zero(sizeof(struct ip_range));
|
||||
|
||||
if (!range)
|
||||
{
|
||||
LOG_ERROR("ACL parse error. Out of memory!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ip_convert_address_to_range(data, range))
|
||||
{
|
||||
add_ip_range(list, range);
|
||||
return 1;
|
||||
}
|
||||
hub_free(range);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -260,30 +179,18 @@ static int acl_parse_line(char* line, int line_count, void* ptr_data)
|
||||
char* pos;
|
||||
struct acl_handle* handle = (struct acl_handle*) ptr_data;
|
||||
int ret;
|
||||
|
||||
|
||||
if ((pos = strchr(line, '#')) != NULL)
|
||||
{
|
||||
pos[0] = 0;
|
||||
}
|
||||
|
||||
|
||||
line = strip_white_space(line);
|
||||
if (!*line)
|
||||
return 0;
|
||||
|
||||
#ifdef ACL_DEBUG
|
||||
hub_log(log_trace, "acl_parse_line(): '%s'", line);
|
||||
#endif
|
||||
line = strip_white_space(line);
|
||||
|
||||
if (!*line)
|
||||
{
|
||||
hub_log(log_fatal, "ACL parse error on line %d", line_count);
|
||||
return -1;
|
||||
}
|
||||
LOG_DEBUG("acl_parse_line: '%s'", line);
|
||||
|
||||
#ifdef ACL_DEBUG
|
||||
hub_log(log_trace, "acl_parse_line: '%s'", line);
|
||||
#endif
|
||||
|
||||
ACL_ADD_USER("bot", handle->users, cred_bot);
|
||||
ACL_ADD_USER("user_admin", handle->users, cred_admin);
|
||||
ACL_ADD_USER("user_super", handle->users, cred_super);
|
||||
@@ -295,8 +202,8 @@ static int acl_parse_line(char* line, int line_count, void* ptr_data)
|
||||
ACL_ADD_BOOL("ban_cid", handle->cids);
|
||||
ACL_ADD_ADDR("deny_ip", handle->networks);
|
||||
ACL_ADD_ADDR("nat_ip", handle->nat_override);
|
||||
|
||||
hub_log(log_error, "Unknown ACL command on line %d: '%s'", line_count, line);
|
||||
|
||||
LOG_ERROR("Unknown ACL command on line %d: '%s'", line_count, line);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -315,7 +222,7 @@ int acl_initialize(struct hub_config* config, struct acl_handle* handle)
|
||||
|
||||
if (!handle->users || !handle->cids || !handle->networks || !handle->users_denied || !handle->users_banned || !handle->nat_override)
|
||||
{
|
||||
hub_log(log_fatal, "acl_initialize: Out of memory");
|
||||
LOG_FATAL("acl_initialize: Out of memory");
|
||||
|
||||
list_destroy(handle->users);
|
||||
list_destroy(handle->users_denied);
|
||||
@@ -340,7 +247,7 @@ int acl_initialize(struct hub_config* config, struct acl_handle* handle)
|
||||
|
||||
static void acl_free_access_info(void* ptr)
|
||||
{
|
||||
struct user_access_info* info = (struct user_access_info*) ptr;
|
||||
struct hub_user_access_info* info = (struct hub_user_access_info*) ptr;
|
||||
if (info)
|
||||
{
|
||||
hub_free(info->username);
|
||||
@@ -404,16 +311,16 @@ int acl_shutdown(struct acl_handle* handle)
|
||||
}
|
||||
|
||||
|
||||
struct user_access_info* acl_get_access_info(struct acl_handle* handle, const char* name)
|
||||
struct hub_user_access_info* acl_get_access_info(struct acl_handle* handle, const char* name)
|
||||
{
|
||||
struct user_access_info* info = (struct user_access_info*) list_get_first(handle->users);
|
||||
struct hub_user_access_info* info = (struct hub_user_access_info*) list_get_first(handle->users);
|
||||
while (info)
|
||||
{
|
||||
if (strcasecmp(info->username, name) == 0)
|
||||
{
|
||||
return info;
|
||||
}
|
||||
info = (struct user_access_info*) list_get_next(handle->users);
|
||||
info = (struct hub_user_access_info*) list_get_next(handle->users);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -446,19 +353,75 @@ int acl_is_user_denied(struct acl_handle* handle, const char* data)
|
||||
STR_LIST_CONTAINS(handle->users_denied, data);
|
||||
}
|
||||
|
||||
int acl_user_ban_nick(struct acl_handle* handle, const char* nick)
|
||||
{
|
||||
char* data = 0;
|
||||
struct hub_user_access_info* info = hub_malloc_zero(sizeof(struct hub_user_access_info));
|
||||
|
||||
if (!info)
|
||||
{
|
||||
LOG_ERROR("ACL error: Out of memory!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
data = hub_strdup(nick);
|
||||
if (!data)
|
||||
{
|
||||
LOG_ERROR("ACL error: Out of memory!");
|
||||
hub_free(info);
|
||||
return -1;
|
||||
}
|
||||
|
||||
list_append(handle->users_banned, data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int acl_user_ban_cid(struct acl_handle* handle, const char* cid)
|
||||
{
|
||||
char* data;
|
||||
struct hub_user_access_info* info = hub_malloc_zero(sizeof(struct hub_user_access_info));
|
||||
if (!info)
|
||||
{
|
||||
LOG_ERROR("ACL error: Out of memory!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
data = hub_strdup(cid);
|
||||
if (!data)
|
||||
{
|
||||
LOG_ERROR("ACL error: Out of memory!");
|
||||
hub_free(info);
|
||||
return -1;
|
||||
}
|
||||
|
||||
list_append(handle->cids, data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int acl_user_unban_nick(struct acl_handle* handle, const char* nick)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int acl_user_unban_cid(struct acl_handle* handle, const char* cid)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int acl_is_ip_banned(struct acl_handle* handle, const char* ip_address)
|
||||
{
|
||||
struct ip_addr_encap raw;
|
||||
struct ip_ban_record* info = (struct ip_ban_record*) list_get_first(handle->networks);
|
||||
struct ip_range* info = (struct ip_range*) list_get_first(handle->networks);
|
||||
ip_convert_to_binary(ip_address, &raw);
|
||||
|
||||
while (info)
|
||||
{
|
||||
if (acl_check_ip_range(&raw, info))
|
||||
if (ip_in_range(&raw, info))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
info = (struct ip_ban_record*) list_get_next(handle->networks);
|
||||
info = (struct ip_range*) list_get_next(handle->networks);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -466,71 +429,63 @@ int acl_is_ip_banned(struct acl_handle* handle, const char* ip_address)
|
||||
int acl_is_ip_nat_override(struct acl_handle* handle, const char* ip_address)
|
||||
{
|
||||
struct ip_addr_encap raw;
|
||||
struct ip_ban_record* info = (struct ip_ban_record*) list_get_first(handle->nat_override);
|
||||
struct ip_range* info = (struct ip_range*) list_get_first(handle->nat_override);
|
||||
ip_convert_to_binary(ip_address, &raw);
|
||||
|
||||
while (info)
|
||||
{
|
||||
if (acl_check_ip_range(&raw, info))
|
||||
if (ip_in_range(&raw, info))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
info = (struct ip_ban_record*) list_get_next(handle->nat_override);
|
||||
info = (struct ip_range*) list_get_next(handle->nat_override);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int acl_check_ip_range(struct ip_addr_encap* addr, struct ip_ban_record* info)
|
||||
{
|
||||
return (addr->af == info->lo.af && ip_compare(&info->lo, addr) <= 0 && ip_compare(addr, &info->hi) <= 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* This will generate the same challenge to the same user, always.
|
||||
* The challenge is made up of the time of the user connected
|
||||
* seconds since the unix epoch (modulus 1 million)
|
||||
* and the SID of the user (0-1 million).
|
||||
*/
|
||||
const char* password_generate_challenge(struct user* user)
|
||||
const char* acl_password_generate_challenge(struct acl_handle* acl, struct hub_user* user)
|
||||
{
|
||||
char buf[32];
|
||||
char buf[64];
|
||||
uint64_t tiger_res[3];
|
||||
static char tiger_buf[MAX_CID_LEN+1];
|
||||
|
||||
snprintf(buf, 32, "%d%d%d", (int) user->tm_connected, (int) user->id.sid, (int) user->sd);
|
||||
|
||||
|
||||
// FIXME: Generate a better nonce scheme.
|
||||
snprintf(buf, 64, "%p%d%d", user, (int) user->id.sid, (int) net_con_get_sd(user->connection));
|
||||
|
||||
tiger((uint64_t*) buf, strlen(buf), (uint64_t*) tiger_res);
|
||||
base32_encode((unsigned char*) tiger_res, TIGERSIZE, tiger_buf);
|
||||
tiger_buf[MAX_CID_LEN] = 0;
|
||||
|
||||
#ifdef ACL_DEBUG
|
||||
hub_log(log_trace, "Generating challenge for user %s: '%s'", user->id.nick, tiger_buf);
|
||||
#endif
|
||||
return (const char*) tiger_buf;
|
||||
}
|
||||
|
||||
|
||||
int password_verify(struct user* user, const char* password)
|
||||
int acl_password_verify(struct acl_handle* acl, struct hub_user* user, const char* password)
|
||||
{
|
||||
char buf[1024];
|
||||
struct user_access_info* access;
|
||||
struct hub_user_access_info* access;
|
||||
const char* challenge;
|
||||
char raw_challenge[64];
|
||||
char password_calc[64];
|
||||
uint64_t tiger_res[3];
|
||||
|
||||
if (!password || !user || strlen(password) != MAX_CID_LEN)
|
||||
return password_invalid;
|
||||
return 0;
|
||||
|
||||
access = acl_get_access_info(user->hub->acl, user->id.nick);
|
||||
access = acl_get_access_info(acl, user->id.nick);
|
||||
if (!access || !access->password)
|
||||
return password_invalid;
|
||||
return 0;
|
||||
|
||||
if (TIGERSIZE+strlen(access->password) >= 1024)
|
||||
return password_invalid;
|
||||
return 0;
|
||||
|
||||
challenge = password_generate_challenge(user);
|
||||
challenge = acl_password_generate_challenge(acl, user);
|
||||
|
||||
base32_decode(challenge, (unsigned char*) raw_challenge, MAX_CID_LEN);
|
||||
|
||||
@@ -541,14 +496,11 @@ int password_verify(struct user* user, const char* password)
|
||||
base32_encode((unsigned char*) tiger_res, TIGERSIZE, password_calc);
|
||||
password_calc[MAX_CID_LEN] = 0;
|
||||
|
||||
#ifdef ACL_DEBUG
|
||||
hub_log(log_trace, "Checking password %s against %s", password, password_calc);
|
||||
#endif
|
||||
if (strcasecmp(password, password_calc) == 0)
|
||||
{
|
||||
return password_ok;
|
||||
return 1;
|
||||
}
|
||||
return password_invalid;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,21 +21,9 @@
|
||||
#define HAVE_UHUB_ACL_H
|
||||
|
||||
struct hub_config;
|
||||
struct user;
|
||||
struct hub_user;
|
||||
struct ip_addr_encap;
|
||||
|
||||
enum password_status
|
||||
{
|
||||
password_invalid = 0,
|
||||
password_ok = 1,
|
||||
};
|
||||
|
||||
enum acl_status
|
||||
{
|
||||
acl_not_found = 0,
|
||||
acl_found = 1,
|
||||
};
|
||||
|
||||
enum user_credentials
|
||||
{
|
||||
cred_none, /**<<< "User has no credentials (not yet logged in)" */
|
||||
@@ -50,19 +38,13 @@ enum user_credentials
|
||||
|
||||
const char* get_user_credential_string(enum user_credentials cred);
|
||||
|
||||
struct user_access_info
|
||||
struct hub_user_access_info
|
||||
{
|
||||
char* username; /* name of user, cid or IP range */
|
||||
char* password; /* password */
|
||||
enum user_credentials status;
|
||||
};
|
||||
|
||||
struct ip_ban_record
|
||||
{
|
||||
struct ip_addr_encap lo;
|
||||
struct ip_addr_encap hi;
|
||||
};
|
||||
|
||||
struct acl_handle
|
||||
{
|
||||
struct linked_list* users; /* Known users. See enum user_status */
|
||||
@@ -77,7 +59,7 @@ struct acl_handle
|
||||
extern int acl_initialize(struct hub_config* config, struct acl_handle* handle);
|
||||
extern int acl_shutdown(struct acl_handle* handle);
|
||||
|
||||
extern struct user_access_info* acl_get_access_info(struct acl_handle* handle, const char* name);
|
||||
extern struct hub_user_access_info* acl_get_access_info(struct acl_handle* handle, const char* name);
|
||||
extern int acl_is_cid_banned(struct acl_handle* handle, const char* cid);
|
||||
extern int acl_is_ip_banned(struct acl_handle* handle, const char* ip_address);
|
||||
extern int acl_is_ip_nat_override(struct acl_handle* handle, const char* ip_address);
|
||||
@@ -85,9 +67,19 @@ extern int acl_is_ip_nat_override(struct acl_handle* handle, const char* ip_addr
|
||||
extern int acl_is_user_banned(struct acl_handle* handle, const char* name);
|
||||
extern int acl_is_user_denied(struct acl_handle* handle, const char* name);
|
||||
|
||||
extern int acl_check_ip_range(struct ip_addr_encap* addr, struct ip_ban_record* info);
|
||||
extern int acl_user_ban_nick(struct acl_handle* handle, const char* nick);
|
||||
extern int acl_user_ban_cid(struct acl_handle* handle, const char* cid);
|
||||
extern int acl_user_unban_nick(struct acl_handle* handle, const char* nick);
|
||||
extern int acl_user_unban_cid(struct acl_handle* handle, const char* cid);
|
||||
|
||||
extern const char* password_generate_challenge(struct user* user);
|
||||
extern int password_verify(struct user* user, const char* password);
|
||||
extern const char* acl_password_generate_challenge(struct acl_handle* acl, struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Verify a password.
|
||||
*
|
||||
* @param password the hashed password (based on the nonce).
|
||||
* @return 1 if the password matches, or 0 if the password is incorrect.
|
||||
*/
|
||||
extern int acl_password_verify(struct acl_handle* acl, struct hub_user* user, const char* password);
|
||||
|
||||
#endif /* HAVE_UHUB_ACL_H */
|
||||
613
src/core/commands.c
Normal file
613
src/core/commands.c
Normal file
@@ -0,0 +1,613 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
#ifdef DEBUG
|
||||
#define CRASH_DEBUG
|
||||
#endif
|
||||
|
||||
#define MAX_HELP_MSG 1024
|
||||
|
||||
struct hub_command
|
||||
{
|
||||
const char* message;
|
||||
char* prefix;
|
||||
size_t prefix_len;
|
||||
struct linked_list* args;
|
||||
};
|
||||
|
||||
typedef int (*command_handler)(struct hub_info* hub, struct hub_user* user, struct hub_command*);
|
||||
|
||||
struct commands_handler
|
||||
{
|
||||
const char* prefix;
|
||||
size_t length;
|
||||
const char* args;
|
||||
enum user_credentials cred;
|
||||
command_handler handler;
|
||||
const char* description;
|
||||
};
|
||||
|
||||
static struct commands_handler command_handlers[];
|
||||
|
||||
static void command_destroy(struct hub_command* cmd)
|
||||
{
|
||||
if (!cmd) return;
|
||||
hub_free(cmd->prefix);
|
||||
|
||||
if (cmd->args)
|
||||
{
|
||||
list_clear(cmd->args, &hub_free);
|
||||
list_destroy(cmd->args);
|
||||
}
|
||||
|
||||
hub_free(cmd);
|
||||
}
|
||||
|
||||
static struct hub_command* command_create(const char* message)
|
||||
{
|
||||
struct hub_command* cmd = hub_malloc_zero(sizeof(struct hub_command));
|
||||
if (!cmd) return 0;
|
||||
|
||||
cmd->message = message;
|
||||
cmd->args = list_create();
|
||||
|
||||
int n = split_string(message, "\\s", cmd->args, 0);
|
||||
if (n <= 0)
|
||||
{
|
||||
command_destroy(cmd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* prefix = list_get_first(cmd->args);
|
||||
if (prefix && prefix[0] && prefix[1])
|
||||
{
|
||||
cmd->prefix = hub_strdup(&prefix[1]);
|
||||
cmd->prefix_len = strlen(cmd->prefix);
|
||||
}
|
||||
else
|
||||
{
|
||||
command_destroy(cmd);
|
||||
return 0;
|
||||
}
|
||||
list_remove(cmd->args, prefix);
|
||||
hub_free(prefix);
|
||||
return cmd;
|
||||
}
|
||||
|
||||
static void send_message(struct hub_info* hub, struct hub_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(hub, user, command);
|
||||
adc_msg_free(command);
|
||||
hub_free(buffer);
|
||||
}
|
||||
|
||||
static int command_access_denied(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
char temp[128];
|
||||
snprintf(temp, 128, "*** %s: Access denied.", cmd->prefix);
|
||||
send_message(hub, user, temp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_not_found(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
char temp[128];
|
||||
snprintf(temp, 128, "*** %s: Command not found", cmd->prefix);
|
||||
send_message(hub, user, temp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_status_user_not_found(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd, const char* nick)
|
||||
{
|
||||
char temp[128];
|
||||
snprintf(temp, 128, "*** %s: No user \"%s\"", cmd->prefix, nick);
|
||||
send_message(hub, user, temp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* command_get_syntax(struct commands_handler* handler)
|
||||
{
|
||||
static char args[128];
|
||||
args[0] = 0;
|
||||
size_t n = 0;
|
||||
if (handler->args)
|
||||
{
|
||||
for (n = 0; n < strlen(handler->args); n++)
|
||||
{
|
||||
if (n > 0) strcat(args, " ");
|
||||
switch (handler->args[n])
|
||||
{
|
||||
case 'n': strcat(args, "<nick>"); break;
|
||||
case 'c': strcat(args, "<cid>"); break;
|
||||
case 'a': strcat(args, "<addr>"); break;
|
||||
case 'm': strcat(args, "<message>"); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
static int command_arg_mismatch(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd, struct commands_handler* handler)
|
||||
{
|
||||
char temp[256];
|
||||
const char* args = command_get_syntax(handler);
|
||||
if (args) snprintf(temp, 256, "*** %s: Use: !%s %s", cmd->prefix, cmd->prefix, args);
|
||||
else snprintf(temp, 256, "*** %s: Use: !%s", cmd->prefix, cmd->prefix);
|
||||
send_message(hub, user, temp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_status(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd, const char* message)
|
||||
{
|
||||
char temp[1024];
|
||||
snprintf(temp, 1024, "*** %s: %s", cmd->prefix, message);
|
||||
send_message(hub, user, temp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_stats(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
char temp[128];
|
||||
snprintf(temp, 128, PRINTF_SIZE_T " users, peak: " PRINTF_SIZE_T ". Network (up/down): %d/%d KB/s, peak: %d/%d KB/s",
|
||||
hub->users->count,
|
||||
hub->users->count_peak,
|
||||
(int) hub->stats.net_tx / 1024,
|
||||
(int) hub->stats.net_rx / 1024,
|
||||
(int) hub->stats.net_tx_peak / 1024,
|
||||
(int) hub->stats.net_rx_peak / 1024);
|
||||
return command_status(hub, user, cmd, temp);
|
||||
}
|
||||
|
||||
static int command_help(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
size_t n;
|
||||
char msg[MAX_HELP_MSG];
|
||||
msg[0] = 0;
|
||||
strcat(msg, "Available commands:\n");
|
||||
|
||||
for (n = 0; command_handlers[n].prefix; n++)
|
||||
{
|
||||
if (command_handlers[n].cred <= user->credentials)
|
||||
{
|
||||
strcat(msg, "!");
|
||||
strcat(msg, command_handlers[n].prefix);
|
||||
strcat(msg, " - ");
|
||||
strcat(msg, command_handlers[n].description);
|
||||
strcat(msg, "\n");
|
||||
}
|
||||
}
|
||||
return command_status(hub, user, cmd, msg);
|
||||
}
|
||||
|
||||
static int command_uptime(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
char tmp[128];
|
||||
size_t d;
|
||||
size_t h;
|
||||
size_t m;
|
||||
size_t D = (size_t) difftime(time(0), hub->tm_started);
|
||||
|
||||
d = D / (24 * 3600);
|
||||
D = D % (24 * 3600);
|
||||
h = D / 3600;
|
||||
D = D % 3600;
|
||||
m = D / 60;
|
||||
|
||||
tmp[0] = 0;
|
||||
if (d)
|
||||
{
|
||||
strcat(tmp, uhub_itoa((int) d));
|
||||
strcat(tmp, " day");
|
||||
if (d != 1) strcat(tmp, "s");
|
||||
strcat(tmp, ", ");
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
return command_status(hub, user, cmd, tmp);
|
||||
}
|
||||
|
||||
static int command_kick(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
char* nick = list_get_first(cmd->args);
|
||||
if (!nick)
|
||||
return -1; // FIXME: bad syntax.
|
||||
|
||||
struct hub_user* target = uman_get_user_by_nick(hub, nick);
|
||||
|
||||
if (!target)
|
||||
return command_status_user_not_found(hub, user, cmd, nick);
|
||||
|
||||
if (target == user)
|
||||
return command_status(hub, user, cmd, "Cannot kick yourself");
|
||||
|
||||
hub_disconnect_user(hub, target, quit_kicked);
|
||||
return command_status(hub, user, cmd, nick);
|
||||
}
|
||||
|
||||
static int command_ban(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
char* nick = list_get_first(cmd->args);
|
||||
if (!nick)
|
||||
return -1; // FIXME: bad syntax.
|
||||
|
||||
struct hub_user* target = uman_get_user_by_nick(hub, nick);
|
||||
|
||||
if (!target)
|
||||
return command_status_user_not_found(hub, user, cmd, nick);
|
||||
|
||||
if (target == user)
|
||||
return command_status(hub, user, cmd, "Cannot kick/ban yourself");
|
||||
|
||||
hub_disconnect_user(hub, target, quit_kicked);
|
||||
acl_user_ban_nick(hub->acl, target->id.nick);
|
||||
acl_user_ban_cid(hub->acl, target->id.cid);
|
||||
|
||||
return command_status(hub, user, cmd, nick);
|
||||
}
|
||||
|
||||
static int command_unban(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
return command_status(hub, user, cmd, "Not implemented");
|
||||
}
|
||||
|
||||
static int command_mute(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
char* nick = list_get_first(cmd->args);
|
||||
if (!nick)
|
||||
return -1; // FIXME: bad syntax.
|
||||
|
||||
struct hub_user* target = uman_get_user_by_nick(hub, nick);
|
||||
|
||||
if (!target)
|
||||
return command_status_user_not_found(hub, user, cmd, nick);
|
||||
|
||||
if (strlen(cmd->prefix) == 4)
|
||||
{
|
||||
user_flag_set(target, flag_muted);
|
||||
}
|
||||
else
|
||||
{
|
||||
user_flag_unset(target, flag_muted);
|
||||
}
|
||||
return command_status(hub, user, cmd, nick);
|
||||
}
|
||||
|
||||
static int command_reload(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
hub->status = hub_status_restart;
|
||||
return command_status(hub, user, cmd, "Reloading configuration...");
|
||||
}
|
||||
|
||||
static int command_shutdown(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
hub->status = hub_status_shutdown;
|
||||
return command_status(hub, user, cmd, "Hub shutting down...");
|
||||
}
|
||||
|
||||
static int command_version(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
return command_status(hub, user, cmd, "Powered by " PRODUCT "/" VERSION);
|
||||
}
|
||||
|
||||
static int command_myip(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
char tmp[128];
|
||||
snprintf(tmp, 128, "Your address is \"%s\"", user_get_address(user));
|
||||
return command_status(hub, user, cmd, tmp);
|
||||
}
|
||||
|
||||
static int command_getip(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
char tmp[128];
|
||||
|
||||
char* nick = list_get_first(cmd->args);
|
||||
if (!nick);
|
||||
return -1; // FIXME: bad syntax/OOM
|
||||
|
||||
struct hub_user* target = uman_get_user_by_nick(hub, nick);
|
||||
|
||||
if (!target)
|
||||
return command_status_user_not_found(hub, user, cmd, nick);
|
||||
|
||||
snprintf(tmp, 128, "%s has address \"%s\"", nick, user_get_address(user));
|
||||
return command_status(hub, user, cmd, tmp);
|
||||
}
|
||||
|
||||
static int command_whoip(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
char* address = list_get_first(cmd->args);
|
||||
struct ip_range range;
|
||||
struct linked_list* users;
|
||||
struct hub_user* u;
|
||||
int ret = 0;
|
||||
|
||||
if (!address)
|
||||
return -1; // FIXME: bad syntax.
|
||||
|
||||
ret = ip_convert_address_to_range(address, &range);
|
||||
if (!ret)
|
||||
return command_status(hub, user, cmd, "Invalid IP address/range/mask");
|
||||
|
||||
users = (struct linked_list*) list_create();
|
||||
if (!users)
|
||||
return -1; // FIXME: OOM
|
||||
|
||||
ret = uman_get_user_by_addr(hub, users, &range);
|
||||
|
||||
if (!ret)
|
||||
{
|
||||
list_destroy(users);
|
||||
return command_status(hub, user, cmd, "No users found.");
|
||||
}
|
||||
|
||||
char tmp[128];
|
||||
snprintf(tmp, 128, "*** %s: Found %d match%s:", cmd->prefix, ret, ((ret != 1) ? "es" : ""));
|
||||
|
||||
char* buffer = hub_malloc(((MAX_NICK_LEN + INET6_ADDRSTRLEN + 5) * ret) + strlen(tmp) + 3);
|
||||
if (!buffer)
|
||||
{
|
||||
list_destroy(users);
|
||||
return -1; // FIXME: OOM
|
||||
}
|
||||
|
||||
buffer[0] = 0;
|
||||
strcat(buffer, tmp);
|
||||
strcat(buffer, "\n");
|
||||
|
||||
u = (struct hub_user*) list_get_first(users);
|
||||
while (u)
|
||||
{
|
||||
strcat(buffer, u->id.nick);
|
||||
strcat(buffer, " (");
|
||||
strcat(buffer, user_get_address(u));
|
||||
strcat(buffer, ")\n");
|
||||
u = (struct hub_user*) list_get_next(users);
|
||||
}
|
||||
strcat(buffer, "\n");
|
||||
|
||||
send_message(hub, user, buffer);
|
||||
hub_free(buffer);
|
||||
list_destroy(users);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_broadcast(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
struct adc_message* command = adc_msg_construct(ADC_CMD_IMSG, strlen((cmd->message + 12)) + 6);
|
||||
adc_msg_add_argument(command, (cmd->message + 12));
|
||||
route_to_all(hub, command);
|
||||
adc_msg_free(command);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_history(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
char* buffer;
|
||||
struct linked_list* messages = hub->chat_history;
|
||||
char* message = 0;
|
||||
int ret = (int) list_size(messages);
|
||||
size_t bufsize;
|
||||
|
||||
if (!ret)
|
||||
{
|
||||
return command_status(hub, user, cmd, "No messages.");
|
||||
}
|
||||
|
||||
char tmp[128];
|
||||
snprintf(tmp, 128, "*** %s: Found %d message%s:", cmd->prefix, ret, ((ret != 1) ? "s" : ""));
|
||||
bufsize = strlen(tmp);
|
||||
message = (char*) list_get_first(messages);
|
||||
while (message)
|
||||
{
|
||||
bufsize += strlen(message);
|
||||
message = (char*) list_get_next(messages);
|
||||
}
|
||||
|
||||
buffer = hub_malloc(bufsize+4);
|
||||
if (!buffer)
|
||||
{
|
||||
return command_status(hub, user, cmd, "Not enough memory.");
|
||||
}
|
||||
|
||||
buffer[0] = 0;
|
||||
strcat(buffer, tmp);
|
||||
strcat(buffer, "\n");
|
||||
|
||||
message = (char*) list_get_first(messages);
|
||||
while (message)
|
||||
{
|
||||
strcat(buffer, message);
|
||||
message = (char*) list_get_next(messages);
|
||||
}
|
||||
strcat(buffer, "\n");
|
||||
|
||||
send_message(hub, user, buffer);
|
||||
hub_free(buffer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_log(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
struct linked_list* messages = hub->logout_info;
|
||||
struct hub_logout_info* log;
|
||||
char tmp[1024];
|
||||
char* search = 0;
|
||||
size_t search_len = 0;
|
||||
size_t search_hits = 0;
|
||||
|
||||
if (!list_size(messages))
|
||||
{
|
||||
return command_status(hub, user, cmd, "No entries logged.");
|
||||
}
|
||||
|
||||
search = list_get_first(cmd->args);
|
||||
if (search)
|
||||
{
|
||||
search_len = strlen(search);
|
||||
}
|
||||
|
||||
if (search_len)
|
||||
{
|
||||
sprintf(tmp, "Logged entries: " PRINTF_SIZE_T ", searching for \"%s\"", list_size(messages), search);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(tmp, "Logged entries: " PRINTF_SIZE_T, list_size(messages));
|
||||
}
|
||||
command_status(hub, user, cmd, tmp);
|
||||
|
||||
log = (struct hub_logout_info*) list_get_first(messages);
|
||||
while (log)
|
||||
{
|
||||
const char* address = ip_convert_to_string(&log->addr);
|
||||
int show = 0;
|
||||
|
||||
if (search_len)
|
||||
{
|
||||
if (memmem(log->cid, MAX_CID_LEN, search, search_len) || memmem(log->nick, MAX_NICK_LEN, search, search_len) || memmem(address, strlen(address), search, search_len))
|
||||
{
|
||||
search_hits++;
|
||||
show = 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
show = 1;
|
||||
}
|
||||
|
||||
if (show)
|
||||
{
|
||||
sprintf(tmp, "* %s %s, %s [%s] - %s", get_timestamp(log->time), log->cid, log->nick, ip_convert_to_string(&log->addr), user_get_quit_reason_string(log->reason));
|
||||
send_message(hub, user, tmp);
|
||||
}
|
||||
log = (struct hub_logout_info*) list_get_next(messages);
|
||||
}
|
||||
|
||||
if (search_len)
|
||||
{
|
||||
sprintf(tmp, PRINTF_SIZE_T " entries shown.", search_hits);
|
||||
command_status(hub, user, cmd, tmp);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_rules(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
if (!hub_send_rules(hub, user))
|
||||
return command_status(hub, user, cmd, "no rules defined.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_motd(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
if (!hub_send_motd(hub, user))
|
||||
return command_status(hub, user, cmd, "no motd defined.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef CRASH_DEBUG
|
||||
static int command_crash(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
void (*crash)(void) = NULL;
|
||||
crash();
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int command_dipatcher(struct hub_info* hub, struct hub_user* user, const char* message)
|
||||
{
|
||||
size_t n = 0;
|
||||
int rc;
|
||||
|
||||
/* Parse and validate the command */
|
||||
struct hub_command* cmd = command_create(message);
|
||||
if (!cmd) return 0;
|
||||
|
||||
for (n = 0; command_handlers[n].prefix; n++)
|
||||
{
|
||||
struct commands_handler* handler = &command_handlers[n];
|
||||
if (cmd->prefix_len != handler->length)
|
||||
continue;
|
||||
|
||||
if (!strncmp(cmd->prefix, handler->prefix, handler->length))
|
||||
{
|
||||
if (handler->cred <= user->credentials)
|
||||
{
|
||||
if (!handler->args || (handler->args && list_size(cmd->args) >= strlen(handler->args)))
|
||||
{
|
||||
rc = handler->handler(hub, user, cmd);
|
||||
}
|
||||
else
|
||||
{
|
||||
rc = command_arg_mismatch(hub, user, cmd, handler);
|
||||
}
|
||||
command_destroy(cmd);
|
||||
return rc;
|
||||
}
|
||||
else
|
||||
{
|
||||
rc = command_access_denied(hub, user, cmd);
|
||||
command_destroy(cmd);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
command_not_found(hub, user, cmd);
|
||||
command_destroy(cmd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct commands_handler command_handlers[] = {
|
||||
{ "ban", 3, "n", cred_operator, command_ban, "Ban a user" },
|
||||
{ "broadcast", 9, "m", cred_operator, command_broadcast,"Send a message to all users" },
|
||||
#ifdef CRASH_DEBUG
|
||||
{ "crash", 5, 0, cred_admin, command_crash, "Crash the hub (DEBUG)." },
|
||||
#endif
|
||||
{ "getip", 5, "n", cred_operator, command_getip, "Show IP address for a user" },
|
||||
{ "help", 4, 0, cred_guest, command_help, "Show this help message." },
|
||||
{ "history", 7, 0, cred_guest, command_history, "Show the last chat messages." },
|
||||
{ "kick", 4, "n", cred_operator, command_kick, "Kick a user" },
|
||||
{ "log", 3, 0, cred_operator, command_log, "Display log" },
|
||||
{ "motd", 4, 0, cred_guest, command_motd, "Show the message of the day" },
|
||||
{ "mute", 4, "n", cred_operator, command_mute, "Mute user" },
|
||||
{ "myip", 4, 0, cred_guest, command_myip, "Show your own IP." },
|
||||
{ "reload", 6, 0, cred_admin, command_reload, "Reload configuration files." },
|
||||
{ "rules", 5, 0, cred_guest, command_rules, "Show the hub rules" },
|
||||
{ "shutdown", 8, 0, cred_admin, command_shutdown, "Shutdown hub." },
|
||||
{ "stats", 5, 0, cred_super, command_stats, "Show hub statistics." },
|
||||
{ "unban", 5, "n", cred_operator, command_unban, "Lift ban on a user" },
|
||||
{ "unmute", 6, "n", cred_operator, command_mute, "Unmute user" },
|
||||
{ "uptime", 6, 0, cred_guest, command_uptime, "Display hub uptime info." },
|
||||
{ "version", 7, 0, cred_guest, command_version, "Show hub version info." },
|
||||
{ "whoip", 5, "a", cred_operator, command_whoip, "Show users matching IP range" },
|
||||
{ 0, 0, 0, cred_none, command_help, "" }
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -19,17 +19,4 @@
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
#define CHAT_MSG_HANDLED 1
|
||||
#define CHAT_MSG_IGNORED 0
|
||||
#define CHAT_MSG_INVALID -1
|
||||
|
||||
typedef int (*plugin_event_chat_message)(struct hub_info*, struct user*, struct adc_message*);
|
||||
|
||||
struct command_info
|
||||
{
|
||||
const char* prefix;
|
||||
enum user_credentials cred;
|
||||
plugin_event_chat_message function;
|
||||
};
|
||||
|
||||
int command_dipatcher(struct user* user, const char* message);
|
||||
extern int command_dipatcher(struct hub_info* hub, struct hub_user* user, const char* message);
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -42,7 +42,7 @@
|
||||
else if (strncasecmp(data, "off", 3) == 0) TARGET = 0; \
|
||||
else\
|
||||
{ \
|
||||
hub_log(log_fatal, "Configuration error on line %d: '%s' must be either '1' or '0'", line_count, key); \
|
||||
LOG_FATAL("Configuration error on line %d: '%s' must be either '1' or '0'", line_count, key); \
|
||||
return -1; \
|
||||
} \
|
||||
TARGET |= 0x80000000; \
|
||||
@@ -65,7 +65,7 @@
|
||||
errno = 0; \
|
||||
val = strtol(data, &endptr, 10); \
|
||||
if (((errno == ERANGE && (val == INT_MAX || val == INT_MIN)) || (errno != 0 && val == 0)) || endptr == data) { \
|
||||
hub_log(log_fatal, "Configuration error on line %d: '%s' must be a number", line_count, key); \
|
||||
LOG_FATAL("Configuration error on line %d: '%s' must be a number", line_count, key); \
|
||||
return -1; \
|
||||
} \
|
||||
TARGET = val; \
|
||||
@@ -103,19 +103,23 @@
|
||||
#define IGNORED(NAME) \
|
||||
if (strcmp(#NAME, key) == 0) \
|
||||
{ \
|
||||
hub_log(log_warning, "Configuration option %s deprecated and ingnored.", key); \
|
||||
LOG_WARN("Configuration option %s deprecated and ingnored.", key); \
|
||||
return 0; \
|
||||
} \
|
||||
|
||||
/* default configuration values */
|
||||
#define DEF_SERVER_BIND_ADDR "any"
|
||||
#define DEF_SERVER_PORT 1511
|
||||
#define DEF_SERVER_BACKLOG 50
|
||||
#define DEF_HUB_NAME "uhub"
|
||||
#define DEF_HUB_DESCRIPTION ""
|
||||
#define DEF_HUB_ENABLED 1
|
||||
#define DEF_FILE_ACL ""
|
||||
#define DEF_FILE_MOTD ""
|
||||
#define DEF_FILE_RULES ""
|
||||
#define DEF_MAX_USERS 500
|
||||
#define DEF_MAX_CHAT_HISTORY 20
|
||||
#define DEF_MAX_LOGOUT_LOG 100
|
||||
#define DEF_MAX_RECV_BUFFER 4096
|
||||
#define DEF_MAX_SEND_BUFFER 131072
|
||||
#define DEF_MAX_SEND_BUFFER_SOFT 98304
|
||||
@@ -135,6 +139,10 @@
|
||||
#define DEF_LIMIT_MAX_SHARE 0
|
||||
#define DEF_LIMIT_MIN_SLOTS 0
|
||||
#define DEF_LIMIT_MAX_SLOTS 0
|
||||
#define DEF_TLS_ENABLE 0
|
||||
#define DEF_TLS_REQUIRE 1
|
||||
#define DEF_TLS_PRIVATE_KEY ""
|
||||
#define DEF_TLS_CERTIFICATE ""
|
||||
#define DEF_MSG_HUB_FULL "Hub is full"
|
||||
#define DEF_MSG_HUB_DISABLED "Hub is disabled"
|
||||
#define DEF_MSG_HUB_REGISTERED_USERS_ONLY "Hub is for registered users only"
|
||||
@@ -173,8 +181,12 @@ void config_defaults(struct hub_config* config)
|
||||
DEFAULT_BOOLEAN(hub_enabled, DEF_HUB_ENABLED);
|
||||
DEFAULT_STRING (file_acl, DEF_FILE_ACL);
|
||||
DEFAULT_STRING (file_motd, DEF_FILE_MOTD);
|
||||
DEFAULT_STRING (file_rules, DEF_FILE_RULES);
|
||||
DEFAULT_INTEGER(server_port, DEF_SERVER_PORT);
|
||||
DEFAULT_INTEGER(server_listen_backlog, DEF_SERVER_BACKLOG);
|
||||
DEFAULT_INTEGER(max_users, DEF_MAX_USERS);
|
||||
DEFAULT_INTEGER(max_chat_history, DEF_MAX_CHAT_HISTORY);
|
||||
DEFAULT_INTEGER(max_logout_log, DEF_MAX_LOGOUT_LOG);
|
||||
DEFAULT_INTEGER(max_recv_buffer, DEF_MAX_RECV_BUFFER);
|
||||
DEFAULT_INTEGER(max_send_buffer, DEF_MAX_SEND_BUFFER);
|
||||
DEFAULT_INTEGER(max_send_buffer_soft, DEF_MAX_SEND_BUFFER_SOFT);
|
||||
@@ -228,10 +240,10 @@ void config_defaults(struct hub_config* config)
|
||||
DEFAULT_STRING (msg_user_hub_limit_low, DEF_MSG_USER_HUB_LIMIT_LOW);
|
||||
DEFAULT_STRING (msg_user_hub_limit_high, DEF_MSG_USER_HUB_LIMIT_HIGH);
|
||||
|
||||
DEFAULT_INTEGER(tls_enable, 0);
|
||||
DEFAULT_INTEGER(tls_require, 0);
|
||||
DEFAULT_STRING (tls_certificate, "");
|
||||
DEFAULT_STRING (tls_private_key, "");
|
||||
DEFAULT_INTEGER(tls_enable, DEF_TLS_ENABLE);
|
||||
DEFAULT_INTEGER(tls_require, DEF_TLS_REQUIRE);
|
||||
DEFAULT_STRING (tls_certificate, DEF_TLS_CERTIFICATE);
|
||||
DEFAULT_STRING (tls_private_key, DEF_TLS_PRIVATE_KEY);
|
||||
}
|
||||
|
||||
|
||||
@@ -239,12 +251,16 @@ static int apply_config(struct hub_config* config, char* key, char* data, int li
|
||||
{
|
||||
GET_STR (file_acl);
|
||||
GET_STR (file_motd);
|
||||
GET_STR (file_rules);
|
||||
GET_STR (server_bind_addr);
|
||||
GET_INT (server_port);
|
||||
GET_INT (server_listen_backlog);
|
||||
GET_STR (hub_name);
|
||||
GET_STR (hub_description);
|
||||
GET_BOOL(hub_enabled);
|
||||
GET_INT (max_users);
|
||||
GET_INT (max_chat_history);
|
||||
GET_INT (max_logout_log);
|
||||
GET_INT (max_recv_buffer);
|
||||
GET_INT (max_send_buffer);
|
||||
GET_INT (max_send_buffer_soft);
|
||||
@@ -304,7 +320,7 @@ static int apply_config(struct hub_config* config, char* key, char* data, int li
|
||||
GET_STR (tls_private_key);
|
||||
|
||||
/* Still here -- unknown directive */
|
||||
hub_log(log_fatal, "Unknown configuration directive: '%s'", key);
|
||||
LOG_ERROR("Unknown configuration directive: '%s'", key);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -314,6 +330,7 @@ void free_config(struct hub_config* config)
|
||||
hub_free(config->server_bind_addr);
|
||||
hub_free(config->file_motd);
|
||||
hub_free(config->file_acl);
|
||||
hub_free(config->file_rules);
|
||||
hub_free(config->hub_name);
|
||||
hub_free(config->hub_description);
|
||||
|
||||
@@ -385,12 +402,16 @@ void dump_config(struct hub_config* config, int ignore_defaults)
|
||||
{
|
||||
DUMP_STR (file_acl, DEF_FILE_ACL);
|
||||
DUMP_STR (file_motd, DEF_FILE_MOTD);
|
||||
DUMP_STR (file_rules, DEF_FILE_RULES);
|
||||
DUMP_STR (server_bind_addr, DEF_SERVER_BIND_ADDR);
|
||||
DUMP_INT (server_port, DEF_SERVER_PORT);
|
||||
DUMP_INT (server_listen_backlog, DEF_SERVER_BACKLOG);
|
||||
DUMP_STR (hub_name, DEF_HUB_NAME);
|
||||
DUMP_STR (hub_description, DEF_HUB_DESCRIPTION);
|
||||
DUMP_BOOL(hub_enabled, DEF_HUB_ENABLED);
|
||||
DUMP_INT (max_users, DEF_MAX_USERS);
|
||||
DUMP_INT (max_chat_history, DEF_MAX_CHAT_HISTORY);
|
||||
DUMP_INT (max_logout_log, DEF_MAX_LOGOUT_LOG);
|
||||
DUMP_INT (max_recv_buffer, DEF_MAX_RECV_BUFFER);
|
||||
DUMP_INT (max_send_buffer, DEF_MAX_SEND_BUFFER);
|
||||
DUMP_INT (max_send_buffer_soft, DEF_MAX_SEND_BUFFER_SOFT);
|
||||
@@ -400,6 +421,13 @@ void dump_config(struct hub_config* config, int ignore_defaults)
|
||||
DUMP_BOOL(low_bandwidth_mode, DEF_LOW_BANDWIDTH_MODE);
|
||||
DUMP_BOOL(registered_users_only, DEF_REGISTERED_USERS_ONLY);
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
DUMP_BOOL(tls_enable, DEF_TLS_ENABLE);
|
||||
DUMP_BOOL(tls_require, DEF_TLS_REQUIRE);
|
||||
DUMP_STR (tls_certificate, DEF_TLS_CERTIFICATE);
|
||||
DUMP_STR (tls_private_key, DEF_TLS_PRIVATE_KEY);
|
||||
#endif
|
||||
|
||||
/* Limits enforced on users */
|
||||
DUMP_INT(limit_max_hubs_user, DEF_LIMIT_MAX_HUBS_USER);
|
||||
DUMP_INT(limit_max_hubs_reg, DEF_LIMIT_MAX_HUBS_REG);
|
||||
@@ -460,9 +488,12 @@ static int config_parse_line(char* line, int line_count, void* ptr_data)
|
||||
|
||||
if (!*line) return 0;
|
||||
|
||||
#ifdef CONFIG_DUMP
|
||||
hub_log(log_trace, "config_parse_line(): '%s'", line);
|
||||
#endif
|
||||
LOG_DUMP("config_parse_line(): '%s'", line);
|
||||
|
||||
if (!is_valid_utf8(line))
|
||||
{
|
||||
LOG_WARN("Invalid utf-8 characters on line %d", line_count);
|
||||
}
|
||||
|
||||
if ((pos = strchr(line, '=')) != NULL)
|
||||
{
|
||||
@@ -481,13 +512,11 @@ static int config_parse_line(char* line, int line_count, void* ptr_data)
|
||||
|
||||
if (!*key || !*data)
|
||||
{
|
||||
hub_log(log_fatal, "Configuration parse error on line %d", line_count);
|
||||
LOG_FATAL("Configuration parse error on line %d", line_count);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_DUMP
|
||||
hub_log(log_trace, "config_parse_line: '%s' => '%s'", key, data);
|
||||
#endif
|
||||
LOG_DUMP("config_parse_line: '%s' => '%s'", key, data);
|
||||
|
||||
return apply_config(config, key, data, line_count);
|
||||
}
|
||||
@@ -504,7 +533,7 @@ int read_config(const char* file, struct hub_config* config, int allow_missing)
|
||||
{
|
||||
if (allow_missing && ret == -2)
|
||||
{
|
||||
hub_log(log_debug, "Using default configuration.");
|
||||
LOG_DUMP("Using default configuration.");
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -24,6 +24,7 @@ struct hub_config
|
||||
{
|
||||
int server_port; /**<<< "Server port to bind to (default: 1511)" */
|
||||
char* server_bind_addr; /**<<< "Server bind address (default: '0.0.0.0' or '::')" */
|
||||
int server_listen_backlog; /**<<< "Server listen backlog (default: 50)" */
|
||||
int hub_enabled; /**<<< "Is server enabled (default: 1)" */
|
||||
int show_banner; /**<<< "Show banner on connect (default: 1)" */
|
||||
int max_users; /**<<< "Maximum number of users allowed on the hub (default: 500)" */
|
||||
@@ -32,13 +33,17 @@ struct hub_config
|
||||
int chat_is_privileged; /**<<< "Allow chat for operators and above only (default: 0) */
|
||||
char* file_motd; /**<<< "File containing the 'message of the day' (default: '' - no motd)" */
|
||||
char* file_acl; /**<<< "File containing user database (default: '' - no known users)" */
|
||||
char* file_rules; /**<<< "File containing the rules (default: '' - no rules)" */
|
||||
char* hub_name; /**<<< "Name of hub (default: 'My uhub hub')" */
|
||||
char* hub_description; /**<<< "Name of hub (default: 'no description')" */
|
||||
int max_recv_buffer; /**<<< "Max read buffer before parse, per user (default: 4096)" */
|
||||
int max_send_buffer; /**<<< "Max send buffer before disconnect, per user (default: 128K)" */
|
||||
int max_send_buffer_soft; /**<<< "Max send buffer before message drops, per user (default: 96K)" */
|
||||
int low_bandwidth_mode; /**<<< "If this is enabled, the hub will strip off elements from each user's info message to reduce bandwidth usage" */
|
||||
|
||||
|
||||
int max_chat_history; /**<<< "Number of chat messages kept in history (default: 20)" */
|
||||
int max_logout_log; /**<<< "Number of log entries for people leaving the hub. (default: 100) */
|
||||
|
||||
/* Limits enforced on users */
|
||||
int limit_max_hubs_user; /**<<< "Max concurrent hubs as a user. (0=off, default: 10)" */
|
||||
int limit_max_hubs_reg; /**<<< "Max concurrent hubs as registered user. (0=off, default: 10)" */
|
||||
@@ -51,7 +56,7 @@ struct hub_config
|
||||
int limit_max_share; /**<<< "Limit maximum share size in megabytes (MiB) (0=off, default: 0)" */
|
||||
int limit_min_slots; /**<<< "Limit minimum number of slots open per user (0=off, default: 0)" */
|
||||
int limit_max_slots; /**<<< "Limit maximum number of slots open per user (0=off, default: 0)" */
|
||||
|
||||
|
||||
/* Messages that can be sent to a user */
|
||||
char* msg_hub_full; /**<<< "hub is full" */
|
||||
char* msg_hub_disabled; /**<<< "hub is disabled" */
|
||||
@@ -28,10 +28,14 @@
|
||||
/* Send a broadcast message */
|
||||
#define UHUB_EVENT_BROADCAST 0x2000
|
||||
|
||||
/* Shutdown hub */
|
||||
#define UHUB_EVENT_HUB_SHUTDOWN 0x3001
|
||||
|
||||
/* Statistics, OOM, reconfigure */
|
||||
#define UHUB_EVENT_STATISTICS 0x4000
|
||||
#define UHUB_EVENT_OUT_OF_MEMORY 0x4001
|
||||
#define UHUB_EVENT_RECONFIGURE 0x4002
|
||||
|
||||
|
||||
#endif /* HAVE_UHUB_EVENT_ID_H */
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
#ifdef EQ_DEBUG
|
||||
static void eq_debug(const char* prefix, struct event_data* data)
|
||||
{
|
||||
printf(">>> %s: %p, id: %x, flags=%d\n", prefix, data, data->id, data->flags);
|
||||
LOG_DUMP(">>> %s: %p, id: %x, flags=%d\n", prefix, data, data->id, data->flags);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -130,7 +130,7 @@ void event_queue_post(struct event_queue* queue, struct event_data* message)
|
||||
}
|
||||
else
|
||||
{
|
||||
hub_log(log_error, "event_queue_post: OUT OF MEMORY");
|
||||
LOG_ERROR("event_queue_post: OUT OF MEMORY");
|
||||
}
|
||||
}
|
||||
|
||||
1078
src/core/hub.c
Normal file
1078
src/core/hub.c
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -79,29 +79,36 @@ struct hub_stats
|
||||
size_t net_rx_total;
|
||||
};
|
||||
|
||||
struct hub_logout_info
|
||||
{
|
||||
time_t time;
|
||||
char cid[MAX_CID_LEN+1];
|
||||
char nick[MAX_NICK_LEN+1];
|
||||
struct ip_addr_encap addr;
|
||||
enum user_quit_reason reason;
|
||||
};
|
||||
|
||||
struct hub_info
|
||||
{
|
||||
int fd_tcp;
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
int fd_udp;
|
||||
#endif
|
||||
struct event ev_accept;
|
||||
struct event ev_timer;
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
struct event ev_datagram;
|
||||
#endif
|
||||
struct net_connection* server;
|
||||
struct hub_stats stats;
|
||||
struct event_queue* queue;
|
||||
struct event_base* evbase;
|
||||
struct hub_config* config;
|
||||
struct user_manager* users;
|
||||
struct hub_user_manager* users;
|
||||
struct acl_handle* acl;
|
||||
struct adc_message* command_info; /* The hub's INF command */
|
||||
struct adc_message* command_support; /* The hub's SUP command */
|
||||
struct adc_message* command_motd; /* The message of the day */
|
||||
struct adc_message* command_rules; /* The hub rules */
|
||||
struct adc_message* command_banner; /* The default welcome message */
|
||||
time_t tm_started;
|
||||
int status;
|
||||
char* recvbuf; /* Global receive buffer */
|
||||
char* sendbuf; /* Global send buffer */
|
||||
|
||||
struct linked_list* chat_history; /* Chat history */
|
||||
struct linked_list* logout_info; /* Log of people logging out. */
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
SSL_METHOD* ssl_method;
|
||||
SSL_CTX* ssl_ctx;
|
||||
@@ -116,106 +123,111 @@ struct hub_info
|
||||
*
|
||||
* @return 0 on success, -1 on error
|
||||
*/
|
||||
extern int hub_handle_message(struct user* u, const char* message, size_t length);
|
||||
extern int hub_handle_message(struct hub_info* hub, struct hub_user* u, const char* message, size_t length);
|
||||
|
||||
/**
|
||||
* Handle protocol support/subscription messages received clients.
|
||||
*
|
||||
* @return 0 on success, -1 on error
|
||||
*/
|
||||
extern int hub_handle_support(struct user* u, struct adc_message* cmd);
|
||||
extern int hub_handle_support(struct hub_info* hub, struct hub_user* u, struct adc_message* cmd);
|
||||
|
||||
/**
|
||||
* Handle password messages received from clients.
|
||||
*
|
||||
* @return 0 on success, -1 on error
|
||||
*/
|
||||
extern int hub_handle_password(struct user* u, struct adc_message* cmd);
|
||||
extern int hub_handle_password(struct hub_info* hub, struct hub_user* u, struct adc_message* cmd);
|
||||
|
||||
/**
|
||||
* Handle chat messages received from clients.
|
||||
* @return 0 on success, -1 on error.
|
||||
*/
|
||||
extern int hub_handle_chat_message(struct user* u, struct adc_message* cmd);
|
||||
extern int hub_handle_chat_message(struct hub_info* hub, struct hub_user* u, struct adc_message* cmd);
|
||||
|
||||
/**
|
||||
* Add a chat message to the chat history
|
||||
*/
|
||||
extern void hub_chat_history_add(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd);
|
||||
|
||||
/**
|
||||
* Clear the chat history.
|
||||
*/
|
||||
extern void hub_chat_history_clear(struct hub_info* hub);
|
||||
|
||||
/**
|
||||
* Used internally by hub_handle_info
|
||||
* @return 1 if nickname is OK, or 0 if nickname is not accepted.
|
||||
*/
|
||||
extern int hub_handle_info_check_nick(struct user* u, struct adc_message* cmd);
|
||||
extern int hub_handle_info_check_nick(struct hub_info* hub, struct hub_user* u, struct adc_message* cmd);
|
||||
|
||||
/**
|
||||
* Used internally by hub_handle_info
|
||||
* @return 1 if CID/PID is OK, or 0 if not valid.
|
||||
*/
|
||||
extern int hub_handle_info_check_cid(struct user* u, struct adc_message* cmd);
|
||||
|
||||
/**
|
||||
* Can only be used by administrators or operators.
|
||||
*
|
||||
* @return 0 on success, -1 on error
|
||||
*/
|
||||
extern int hub_handle_kick(struct user* u, struct adc_message* cmd);
|
||||
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
/**
|
||||
* Handle incoming autocheck message.
|
||||
*/
|
||||
extern int hub_handle_autocheck(struct user* u, struct adc_message* cmd);
|
||||
#endif
|
||||
extern int hub_handle_info_check_cid(struct hub_info* hub, struct hub_user* u, struct adc_message* cmd);
|
||||
|
||||
/**
|
||||
* Send the support line for the hub to a particular user.
|
||||
* Only used during the initial handshake.
|
||||
*/
|
||||
extern void hub_send_support(struct user* u);
|
||||
extern void hub_send_support(struct hub_info* hub, struct hub_user* u);
|
||||
|
||||
/**
|
||||
* Send a message assigning a SID for a user.
|
||||
* This is only sent after hub_send_support() during initial handshake.
|
||||
*/
|
||||
extern void hub_send_sid(struct user* u);
|
||||
extern void hub_send_sid(struct hub_info* hub, struct hub_user* u);
|
||||
|
||||
/**
|
||||
* Send a 'ping' message to user.
|
||||
*/
|
||||
extern void hub_send_ping(struct user* user);
|
||||
extern void hub_send_ping(struct hub_info* hub, struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Send a message containing hub information to a particular user.
|
||||
* This is sent during user connection, but can safely be sent at any
|
||||
* point later.
|
||||
*/
|
||||
extern void hub_send_hubinfo(struct user* u);
|
||||
extern void hub_send_hubinfo(struct hub_info* hub, struct hub_user* u);
|
||||
|
||||
/**
|
||||
* Send handshake. This basically calls
|
||||
* hub_send_support() and hub_send_sid()
|
||||
*/
|
||||
extern void hub_send_handshake(struct user* u);
|
||||
extern void hub_send_handshake(struct hub_info* hub, struct hub_user* u);
|
||||
|
||||
/**
|
||||
* Send a welcome message containing the message of the day to
|
||||
* one particular user. This can be sent in any point in time.
|
||||
* @return 1 if the motd were sent.
|
||||
*/
|
||||
extern void hub_send_motd(struct user* u);
|
||||
extern int hub_send_motd(struct hub_info* hub, struct hub_user* u);
|
||||
|
||||
/**
|
||||
* Send the rules if configured.
|
||||
* @return 1 if the rules were sent.
|
||||
*/
|
||||
extern int hub_send_rules(struct hub_info* hub, struct hub_user* u);
|
||||
|
||||
/**
|
||||
* Send a password challenge to a user.
|
||||
* This is only used if the user tries to access the hub using a
|
||||
* password protected nick name.
|
||||
*/
|
||||
extern void hub_send_password_challenge(struct user* u);
|
||||
extern void hub_send_password_challenge(struct hub_info* hub, struct hub_user* u);
|
||||
|
||||
/**
|
||||
* Send an autocheck message to a user.
|
||||
* This is basically a UDP message. The user's client can then determine
|
||||
* if UDP communication works by either hole punching or configuring UPnP.
|
||||
* Sends a status_message to a user.
|
||||
*/
|
||||
extern void hub_send_autocheck(struct user* u, uint16_t port, const char* token);
|
||||
extern void hub_send_status(struct hub_info*, struct hub_user* user, enum status_message msg, enum msg_status_level level);
|
||||
|
||||
/**
|
||||
* This starts the hub.
|
||||
* Allocates memory, initializes the hub based on the configuration,
|
||||
* and returns a hub handle.
|
||||
* This hub handle must be passed to hub_shutdown_service() in order to cleanup before exiting.
|
||||
*
|
||||
* @return a pointer to the hub info.
|
||||
*/
|
||||
extern struct hub_info* hub_start_service(struct hub_config* config);
|
||||
|
||||
@@ -238,11 +250,7 @@ extern void hub_free_variables(struct hub_info* hub);
|
||||
* Returns a string for the given status_message (See enum status_message).
|
||||
*/
|
||||
extern const char* hub_get_status_message(struct hub_info* hub, enum status_message msg);
|
||||
|
||||
/**
|
||||
* Sends a status_message to a user.
|
||||
*/
|
||||
extern void hub_send_status(struct user* user, enum status_message msg, enum msg_status_level level);
|
||||
extern const char* hub_get_status_message_log(struct hub_info* hub, enum status_message msg);
|
||||
|
||||
/**
|
||||
* Returns the number of logged in users on the hub.
|
||||
@@ -340,6 +348,21 @@ extern void hub_schedule_runslice(struct hub_info* hub);
|
||||
*/
|
||||
extern void hub_event_loop(struct hub_info* hub);
|
||||
|
||||
/**
|
||||
* Schedule destroying a user.
|
||||
*/
|
||||
extern void hub_schedule_destroy_user(struct hub_info* hub, struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Disconnect a user from the hub.
|
||||
*/
|
||||
extern void hub_disconnect_user(struct hub_info* hub, struct hub_user* user, int reason);
|
||||
|
||||
/**
|
||||
* Log a user logging out.
|
||||
*/
|
||||
extern void hub_logout_log(struct hub_info* hub, struct hub_user* user);
|
||||
|
||||
|
||||
#endif /* HAVE_UHUB_HUB_H */
|
||||
|
||||
100
src/core/hubevent.c
Normal file
100
src/core/hubevent.c
Normal file
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
static void log_user_login(struct hub_user* u)
|
||||
{
|
||||
const char* cred = get_user_credential_string(u->credentials);
|
||||
const char* addr = user_get_address(u);
|
||||
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 hub_user* u, enum status_message msg)
|
||||
{
|
||||
const char* addr = user_get_address(u);
|
||||
const char* message = hub_get_status_message_log(u->hub, msg);
|
||||
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 hub_user* u, const char* message)
|
||||
{
|
||||
const char* addr = user_get_address(u);
|
||||
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 hub_user* u, const char* nick)
|
||||
{
|
||||
const char* addr = user_get_address(u);
|
||||
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 hub_info* hub, struct hub_user* u)
|
||||
{
|
||||
/* Send user list of all existing users */
|
||||
if (!uman_send_user_list(hub, u))
|
||||
return;
|
||||
|
||||
/* Mark as being in the normal state, and add user to the user list */
|
||||
user_set_state(u, state_normal);
|
||||
uman_add(hub, u);
|
||||
|
||||
/* Print log message */
|
||||
log_user_login(u);
|
||||
|
||||
/* Announce new user to all connected users */
|
||||
if (user_is_logged_in(u))
|
||||
route_info_message(hub, u);
|
||||
|
||||
/* Send message of the day (if any) */
|
||||
if (user_is_logged_in(u)) /* Previous send() can fail! */
|
||||
hub_send_motd(hub, u);
|
||||
|
||||
/* Send message of the day (if any) */
|
||||
if (user_is_logged_in(u)) /* Previous send() can fail! */
|
||||
hub_send_rules(hub, u);
|
||||
|
||||
/* reset timeout */
|
||||
net_con_clear_timeout(u->connection);
|
||||
}
|
||||
|
||||
void on_login_failure(struct hub_info* hub, struct hub_user* u, enum status_message msg)
|
||||
{
|
||||
log_user_login_error(u, msg);
|
||||
hub_send_status(hub, u, msg, status_level_fatal);
|
||||
hub_disconnect_user(hub, u, quit_logon_error);
|
||||
}
|
||||
|
||||
void on_nick_change(struct hub_info* hub, struct hub_user* u, const char* nick)
|
||||
{
|
||||
if (user_is_logged_in(u))
|
||||
{
|
||||
log_user_nick_change(u, nick);
|
||||
}
|
||||
}
|
||||
|
||||
void on_logout_user(struct hub_info* hub, struct hub_user* user)
|
||||
{
|
||||
const char* reason = user_get_quit_reason_string(user->quit_reason);
|
||||
log_user_logout(user, reason);
|
||||
hub_logout_log(hub, user);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -23,22 +23,22 @@
|
||||
/**
|
||||
* This event is triggered whenever a user successfully logs in to the hub.
|
||||
*/
|
||||
extern void on_login_success(struct user* u);
|
||||
extern void on_login_success(struct hub_info* hub, struct hub_user* u);
|
||||
|
||||
/**
|
||||
* This event is triggered whenever a user failed to log in to the hub.
|
||||
*/
|
||||
extern void on_login_failure(struct user* u, enum status_message msg);
|
||||
extern void on_login_failure(struct hub_info* hub, struct hub_user* u, enum status_message msg);
|
||||
|
||||
/**
|
||||
* This event is triggered whenever a previously logged in user leaves the hub.
|
||||
*/
|
||||
extern void on_logout_user(struct user* u);
|
||||
extern void on_logout_user(struct hub_info* hub, struct hub_user* u);
|
||||
|
||||
/**
|
||||
* This event is triggered whenever a user changes his/her nickname.
|
||||
*/
|
||||
extern void on_nick_change(struct user* u, const char* nick);
|
||||
extern void on_nick_change(struct hub_info* hub, struct hub_user* u, const char* nick);
|
||||
|
||||
|
||||
#endif /* HAVE_UHUB_HUB_EVENT_H */
|
||||
171
src/core/hubio.c
Normal file
171
src/core/hubio.c
Normal file
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
#include "hubio.h"
|
||||
|
||||
#ifdef DEBUG_SENDQ
|
||||
static void debug_msg(const char* prefix, struct adc_message* msg)
|
||||
{
|
||||
size_t n;
|
||||
char* buf = strdup(msg->cache);
|
||||
for (n = 0; n < msg->length; n++)
|
||||
{
|
||||
if (buf[n] == '\r' || buf[n] == '\n')
|
||||
buf[n] = '_';
|
||||
}
|
||||
LOG_TRACE("%s: [%s] (%d bytes)", prefix, buf, (int) msg->length);
|
||||
free(buf);
|
||||
}
|
||||
#endif
|
||||
|
||||
struct hub_recvq* hub_recvq_create()
|
||||
{
|
||||
struct hub_recvq* q = hub_malloc_zero(sizeof(struct hub_recvq));
|
||||
return q;
|
||||
}
|
||||
|
||||
void hub_recvq_destroy(struct hub_recvq* q)
|
||||
{
|
||||
if (q)
|
||||
{
|
||||
hub_free(q->buf);
|
||||
hub_free(q);
|
||||
}
|
||||
}
|
||||
|
||||
size_t hub_recvq_get(struct hub_recvq* q, void* buf, size_t bufsize)
|
||||
{
|
||||
assert(bufsize >= q->size);
|
||||
if (q->size)
|
||||
{
|
||||
size_t n = q->size;
|
||||
memcpy(buf, q->buf, n);
|
||||
hub_free(q->buf);
|
||||
q->buf = 0;
|
||||
q->size = 0;
|
||||
return n;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t hub_recvq_set(struct hub_recvq* q, void* buf, size_t bufsize)
|
||||
{
|
||||
if (q->buf)
|
||||
{
|
||||
hub_free(q->buf);
|
||||
q->buf = 0;
|
||||
q->size = 0;
|
||||
}
|
||||
|
||||
if (!bufsize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
q->buf = hub_malloc(bufsize);
|
||||
if (!q->buf)
|
||||
return 0;
|
||||
|
||||
q->size = bufsize;
|
||||
memcpy(q->buf, buf, bufsize);
|
||||
return bufsize;
|
||||
}
|
||||
|
||||
|
||||
struct hub_sendq* hub_sendq_create()
|
||||
{
|
||||
struct hub_sendq* q = hub_malloc_zero(sizeof(struct hub_sendq));
|
||||
if (!q)
|
||||
return 0;
|
||||
|
||||
q->queue = list_create();
|
||||
if (!q->queue)
|
||||
{
|
||||
hub_free(q);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
static void clear_send_queue_callback(void* ptr)
|
||||
{
|
||||
adc_msg_free((struct adc_message*) ptr);
|
||||
}
|
||||
|
||||
void hub_sendq_destroy(struct hub_sendq* q)
|
||||
{
|
||||
if (q)
|
||||
{
|
||||
list_clear(q->queue, &clear_send_queue_callback);
|
||||
list_destroy(q->queue);
|
||||
hub_free(q);
|
||||
}
|
||||
}
|
||||
|
||||
void hub_sendq_add(struct hub_sendq* q, struct adc_message* msg_)
|
||||
{
|
||||
struct adc_message* msg = adc_msg_incref(msg_);
|
||||
#ifdef DEBUG_SENDQ
|
||||
debug_msg("hub_sendq_add", msg);
|
||||
#endif
|
||||
list_append(q->queue, msg);
|
||||
q->size += msg->length;
|
||||
}
|
||||
|
||||
void hub_sendq_remove(struct hub_sendq* q, struct adc_message* msg)
|
||||
{
|
||||
#ifdef DEBUG_SENDQ
|
||||
debug_msg("hub_sendq_remove", msg);
|
||||
#endif
|
||||
list_remove(q->queue, msg);
|
||||
q->size -= msg->length;
|
||||
adc_msg_free(msg);
|
||||
q->offset = 0;
|
||||
}
|
||||
|
||||
int hub_sendq_send(struct hub_sendq* q, struct hub_user* user)
|
||||
{
|
||||
struct adc_message* msg = list_get_first(q->queue);
|
||||
if (!msg) return 0;
|
||||
|
||||
int ret = net_con_send(user->connection, msg->cache + q->offset, msg->length - q->offset);
|
||||
|
||||
if (ret > 0)
|
||||
{
|
||||
q->offset += ret;
|
||||
if (msg->length - q->offset > 0)
|
||||
return 0;
|
||||
|
||||
hub_sendq_remove(q, msg);
|
||||
return 1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int hub_sendq_is_empty(struct hub_sendq* q)
|
||||
{
|
||||
return (q->size - q->offset) == 0;
|
||||
}
|
||||
|
||||
size_t hub_sendq_get_bytes(struct hub_sendq* q)
|
||||
{
|
||||
return q->size - q->offset;
|
||||
}
|
||||
106
src/core/hubio.h
Normal file
106
src/core/hubio.h
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef HAVE_UHUB_HUB_IO_H
|
||||
#define HAVE_UHUB_HUB_IO_H
|
||||
|
||||
struct adc_message;
|
||||
struct linked_list;
|
||||
typedef int (*hub_recvq_write)(void* desc, const void* buf, size_t len);
|
||||
typedef int (*hub_recvq_read)(void* desc, void* buf, size_t len);
|
||||
|
||||
struct hub_sendq
|
||||
{
|
||||
size_t size; /** Size of send queue (in bytes, not messages) */
|
||||
size_t offset; /** Queue byte offset in the first message. Should be 0 unless a partial write. */
|
||||
#ifdef SSL_SUPPORT
|
||||
size_t last_send; /** When using SSL, one have to send the exact same buffer and length if a write cannot complete. */
|
||||
#endif
|
||||
struct linked_list* queue; /** List of queued messages */
|
||||
};
|
||||
|
||||
struct hub_recvq
|
||||
{
|
||||
char* buf;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a send queue
|
||||
*/
|
||||
extern struct hub_sendq* hub_sendq_create();
|
||||
|
||||
/**
|
||||
* Destroy a send queue, and delete any queued messages.
|
||||
*/
|
||||
extern void hub_sendq_destroy(struct hub_sendq*);
|
||||
|
||||
/**
|
||||
* Add a message to the send queue.
|
||||
*/
|
||||
extern void hub_sendq_add(struct hub_sendq*, struct adc_message* msg);
|
||||
|
||||
/**
|
||||
* Process the send queue, and send as many messages as possible.
|
||||
* @returns -1 on error, 0 if unable to send more, 1 if more can be sent.
|
||||
*/
|
||||
extern int hub_sendq_send(struct hub_sendq*, struct hub_user*);
|
||||
|
||||
/**
|
||||
* @returns 1 if send queue is empty, 0 otherwise.
|
||||
*/
|
||||
extern int hub_sendq_is_empty(struct hub_sendq*);
|
||||
|
||||
/**
|
||||
* @returns the number of bytes remaining to be sent in the queue.
|
||||
*/
|
||||
extern size_t hub_sendq_get_bytes(struct hub_sendq*);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create a receive queue.
|
||||
*/
|
||||
extern struct hub_recvq* hub_recvq_create();
|
||||
|
||||
/**
|
||||
* Destroy a receive queue.
|
||||
*/
|
||||
extern void hub_recvq_destroy(struct hub_recvq*);
|
||||
|
||||
/**
|
||||
* Gets the buffer, copies it into buf and deallocates it.
|
||||
* NOTE: bufsize *MUST* be larger than the buffer, otherwise it asserts.
|
||||
* @return the number of bytes copied into buf.
|
||||
*/
|
||||
extern size_t hub_recvq_get(struct hub_recvq*, void* buf, size_t bufsize);
|
||||
|
||||
/**
|
||||
* Sets the buffer
|
||||
*/
|
||||
extern size_t hub_recvq_set(struct hub_recvq*, void* buf, size_t bufsize);
|
||||
|
||||
/**
|
||||
* @return 1 if size is zero, 0 otherwise.
|
||||
*/
|
||||
extern int hub_recvq_is_empty(struct hub_recvq* buf);
|
||||
|
||||
|
||||
|
||||
#endif /* HAVE_UHUB_HUB_IO_H */
|
||||
@@ -39,17 +39,18 @@ static void remove_server_restricted_flags(struct adc_message* cmd)
|
||||
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_REFERER);
|
||||
}
|
||||
|
||||
static int user_is_protected(struct user* user);
|
||||
|
||||
static int set_feature_cast_supports(struct user* u, struct adc_message* cmd)
|
||||
static int set_feature_cast_supports(struct hub_user* u, struct adc_message* cmd)
|
||||
{
|
||||
char *it, *tmp;
|
||||
|
||||
if (adc_msg_has_named_argument(cmd, ADC_INF_FLAG_SUPPORT))
|
||||
{
|
||||
tmp = adc_msg_get_named_argument(cmd, ADC_INF_FLAG_SUPPORT);
|
||||
if (!tmp)
|
||||
return -1; // FIXME: OOM
|
||||
|
||||
user_clear_feature_cast_support(u);
|
||||
|
||||
|
||||
it = tmp;
|
||||
while (strlen(it) > 4)
|
||||
{
|
||||
@@ -89,7 +90,7 @@ static int check_hash_tiger(const char* cid, const char* pid)
|
||||
/*
|
||||
* FIXME: Only works for tiger hash. If a client doesnt support tiger we cannot let it in!
|
||||
*/
|
||||
static int check_cid(struct user* user, struct adc_message* cmd)
|
||||
static int check_cid(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
size_t pos;
|
||||
char* cid = adc_msg_get_named_argument(cmd, ADC_INF_FLAG_CLIENT_ID);
|
||||
@@ -150,7 +151,7 @@ static int check_cid(struct user* user, struct adc_message* cmd)
|
||||
}
|
||||
|
||||
|
||||
static int check_required_login_flags(struct user* user, struct adc_message* cmd)
|
||||
static int check_required_login_flags(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
int num = 0;
|
||||
|
||||
@@ -186,15 +187,15 @@ static int check_required_login_flags(struct user* user, struct adc_message* cmd
|
||||
* remove any wrong address, and replace it with the correct one
|
||||
* as seen by the hub.
|
||||
*/
|
||||
int check_network(struct user* user, struct adc_message* cmd)
|
||||
int check_network(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
const char* address = ip_convert_to_string(&user->ipaddr);
|
||||
const char* address = user_get_address(user);
|
||||
|
||||
/* Check for NAT override address */
|
||||
if (acl_is_ip_nat_override(user->hub->acl, address))
|
||||
if (acl_is_ip_nat_override(hub->acl, address))
|
||||
{
|
||||
char* client_given_ip = adc_msg_get_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR);
|
||||
if (strcmp(client_given_ip, "0.0.0.0") != 0)
|
||||
if (client_given_ip && strcmp(client_given_ip, "0.0.0.0") != 0)
|
||||
{
|
||||
user_set_nat_override(user);
|
||||
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_IPV6_ADDR);
|
||||
@@ -222,6 +223,13 @@ int check_network(struct user* user, struct adc_message* cmd)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void strip_network(struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_IPV6_ADDR);
|
||||
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_IPV6_UDP_PORT);
|
||||
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR);
|
||||
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_IPV4_UDP_PORT);
|
||||
}
|
||||
|
||||
static int nick_length_ok(const char* nick)
|
||||
{
|
||||
@@ -269,7 +277,7 @@ static int nick_is_utf8(const char* nick)
|
||||
}
|
||||
|
||||
|
||||
static int check_nick(struct user* user, struct adc_message* cmd)
|
||||
static int check_nick(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
char* nick;
|
||||
char* tmp;
|
||||
@@ -317,10 +325,10 @@ static int check_nick(struct user* user, struct adc_message* cmd)
|
||||
}
|
||||
|
||||
|
||||
static int check_logged_in(struct user* user, struct adc_message* cmd)
|
||||
static int check_logged_in(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
struct user* lookup1 = get_user_by_nick(user->hub, user->id.nick);
|
||||
struct user* lookup2 = get_user_by_cid(user->hub, user->id.cid);
|
||||
struct hub_user* lookup1 = uman_get_user_by_nick(hub, user->id.nick);
|
||||
struct hub_user* lookup2 = uman_get_user_by_cid(hub, user->id.cid);
|
||||
|
||||
if (lookup1 == user)
|
||||
{
|
||||
@@ -331,20 +339,20 @@ 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_ghost_timeout);
|
||||
LOG_DEBUG("check_logged_in: exact same user is logged in: %s", user->id.nick);
|
||||
hub_disconnect_user(hub, lookup1, quit_ghost_timeout);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (lookup1)
|
||||
{
|
||||
hub_log(log_debug, "check_logged_in: nickname is in use: %s", user->id.nick);
|
||||
LOG_DEBUG("check_logged_in: nickname is in use: %s", user->id.nick);
|
||||
return status_msg_inf_error_nick_taken;
|
||||
}
|
||||
else
|
||||
{
|
||||
hub_log(log_debug, "check_logged_in: CID is in use: %s", user->id.cid);
|
||||
LOG_DEBUG("check_logged_in: CID is in use: %s", user->id.cid);
|
||||
return status_msg_inf_error_cid_taken;
|
||||
}
|
||||
}
|
||||
@@ -358,7 +366,7 @@ static int check_logged_in(struct user* user, struct adc_message* cmd)
|
||||
* But this is not something we want to do, and is deprecated in the ADC specification.
|
||||
* One should rather look at capabilities/features.
|
||||
*/
|
||||
static int check_user_agent(struct user* user, struct adc_message* cmd)
|
||||
static int check_user_agent(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
char* ua_encoded = 0;
|
||||
char* ua = 0;
|
||||
@@ -379,19 +387,19 @@ static int check_user_agent(struct user* user, struct adc_message* cmd)
|
||||
}
|
||||
|
||||
|
||||
static int check_acl(struct user* user, struct adc_message* cmd)
|
||||
static int check_acl(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
if (acl_is_cid_banned(user->hub->acl, user->id.cid))
|
||||
if (acl_is_cid_banned(hub->acl, user->id.cid))
|
||||
{
|
||||
return status_msg_ban_permanently;
|
||||
}
|
||||
|
||||
if (acl_is_user_banned(user->hub->acl, user->id.nick))
|
||||
if (acl_is_user_banned(hub->acl, user->id.nick))
|
||||
{
|
||||
return status_msg_ban_permanently;
|
||||
}
|
||||
|
||||
if (acl_is_user_denied(user->hub->acl, user->id.nick))
|
||||
if (acl_is_user_denied(hub->acl, user->id.nick))
|
||||
{
|
||||
return status_msg_inf_error_nick_restricted;
|
||||
}
|
||||
@@ -399,7 +407,7 @@ static int check_acl(struct user* user, struct adc_message* cmd)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int check_limits(struct user* user, struct adc_message* cmd)
|
||||
static int check_limits(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
char* arg = adc_msg_get_named_argument(cmd, ADC_INF_FLAG_SHARED_SIZE);
|
||||
if (arg)
|
||||
@@ -410,8 +418,8 @@ static int check_limits(struct user* user, struct adc_message* cmd)
|
||||
|
||||
if (user_is_logged_in(user))
|
||||
{
|
||||
user->hub->users->shared_size -= user->limits.shared_size;
|
||||
user->hub->users->shared_size += shared_size;
|
||||
hub->users->shared_size -= user->limits.shared_size;
|
||||
hub->users->shared_size += shared_size;
|
||||
}
|
||||
user->limits.shared_size = shared_size;
|
||||
hub_free(arg);
|
||||
@@ -427,8 +435,8 @@ static int check_limits(struct user* user, struct adc_message* cmd)
|
||||
|
||||
if (user_is_logged_in(user))
|
||||
{
|
||||
user->hub->users->shared_files -= user->limits.shared_files;
|
||||
user->hub->users->shared_files += shared_files;
|
||||
hub->users->shared_files -= user->limits.shared_files;
|
||||
hub->users->shared_files += shared_files;
|
||||
}
|
||||
user->limits.shared_files = shared_files;
|
||||
hub_free(arg);
|
||||
@@ -480,37 +488,37 @@ static int check_limits(struct user* user, struct adc_message* cmd)
|
||||
|
||||
if (!user_is_protected(user))
|
||||
{
|
||||
if (user->limits.shared_size < hub_get_min_share(user->hub) && hub_get_min_share(user->hub))
|
||||
if (user->limits.shared_size < hub_get_min_share(hub) && hub_get_min_share(hub))
|
||||
{
|
||||
return status_msg_user_share_size_low;
|
||||
}
|
||||
|
||||
if (user->limits.shared_size > hub_get_max_share(user->hub) && hub_get_max_share(user->hub))
|
||||
if (user->limits.shared_size > hub_get_max_share(hub) && hub_get_max_share(hub))
|
||||
{
|
||||
return status_msg_user_share_size_high;
|
||||
}
|
||||
|
||||
if ((user->limits.hub_count_user > hub_get_max_hubs_user(user->hub) && hub_get_max_hubs_user(user->hub)) ||
|
||||
(user->limits.hub_count_registered > hub_get_max_hubs_reg(user->hub) && hub_get_max_hubs_reg(user->hub)) ||
|
||||
(user->limits.hub_count_operator > hub_get_max_hubs_op(user->hub) && hub_get_max_hubs_op(user->hub)) ||
|
||||
(user->limits.hub_count_total > hub_get_max_hubs_total(user->hub) && hub_get_max_hubs_total(user->hub)))
|
||||
if ((user->limits.hub_count_user > hub_get_max_hubs_user(hub) && hub_get_max_hubs_user(hub)) ||
|
||||
(user->limits.hub_count_registered > hub_get_max_hubs_reg(hub) && hub_get_max_hubs_reg(hub)) ||
|
||||
(user->limits.hub_count_operator > hub_get_max_hubs_op(hub) && hub_get_max_hubs_op(hub)) ||
|
||||
(user->limits.hub_count_total > hub_get_max_hubs_total(hub) && hub_get_max_hubs_total(hub)))
|
||||
{
|
||||
return status_msg_user_hub_limit_high;
|
||||
}
|
||||
|
||||
if ((user->limits.hub_count_user < hub_get_min_hubs_user(user->hub) && hub_get_min_hubs_user(user->hub)) ||
|
||||
(user->limits.hub_count_registered < hub_get_min_hubs_reg(user->hub) && hub_get_min_hubs_reg(user->hub)) ||
|
||||
(user->limits.hub_count_operator < hub_get_min_hubs_op(user->hub) && hub_get_min_hubs_op(user->hub)))
|
||||
if ((user->limits.hub_count_user < hub_get_min_hubs_user(hub) && hub_get_min_hubs_user(hub)) ||
|
||||
(user->limits.hub_count_registered < hub_get_min_hubs_reg(hub) && hub_get_min_hubs_reg(hub)) ||
|
||||
(user->limits.hub_count_operator < hub_get_min_hubs_op(hub) && hub_get_min_hubs_op(hub)))
|
||||
{
|
||||
return status_msg_user_hub_limit_low;
|
||||
}
|
||||
|
||||
if (user->limits.upload_slots < hub_get_min_slots(user->hub) && hub_get_min_slots(user->hub))
|
||||
if (user->limits.upload_slots < hub_get_min_slots(hub) && hub_get_min_slots(hub))
|
||||
{
|
||||
return status_msg_user_slots_low;
|
||||
}
|
||||
|
||||
if (user->limits.upload_slots > hub_get_max_slots(user->hub) && hub_get_max_slots(user->hub))
|
||||
if (user->limits.upload_slots > hub_get_max_slots(hub) && hub_get_max_slots(hub))
|
||||
{
|
||||
return status_msg_user_slots_high;
|
||||
}
|
||||
@@ -525,10 +533,10 @@ static int check_limits(struct user* user, struct adc_message* cmd)
|
||||
* If the hub is configured to allow only registered users and the user
|
||||
* is not recognized this will return 1.
|
||||
*/
|
||||
static int set_credentials(struct user* user, struct adc_message* cmd)
|
||||
static int set_credentials(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
int ret = 0;
|
||||
struct user_access_info* info = acl_get_access_info(user->hub->acl, user->id.nick);
|
||||
struct hub_user_access_info* info = acl_get_access_info(hub->acl, user->id.nick);
|
||||
|
||||
if (info)
|
||||
{
|
||||
@@ -577,85 +585,14 @@ static int set_credentials(struct user* user, struct adc_message* cmd)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determines if a user is to be let into the hub even if the hub is "full".
|
||||
*/
|
||||
static int user_is_protected(struct user* user)
|
||||
{
|
||||
switch (user->credentials)
|
||||
{
|
||||
case cred_bot:
|
||||
case cred_operator:
|
||||
case cred_super:
|
||||
case cred_admin:
|
||||
case cred_link:
|
||||
return 1;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 1 if a user is registered.
|
||||
* Only registered users will be let in if the hub is configured for registered
|
||||
* users only.
|
||||
*/
|
||||
static int user_is_registered(struct user* user)
|
||||
{
|
||||
switch (user->credentials)
|
||||
{
|
||||
case cred_bot:
|
||||
case cred_user:
|
||||
case cred_operator:
|
||||
case cred_super:
|
||||
case cred_admin:
|
||||
case cred_link:
|
||||
return 1;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void update_user_info(struct user* u, struct adc_message* cmd)
|
||||
{
|
||||
char prefix[2];
|
||||
char* argument;
|
||||
size_t n = 0;
|
||||
struct adc_message* cmd_new = adc_msg_copy(u->info);
|
||||
if (!cmd_new)
|
||||
{
|
||||
/* FIXME: OOM! */
|
||||
return;
|
||||
}
|
||||
|
||||
argument = adc_msg_get_argument(cmd, n++);
|
||||
while (argument)
|
||||
{
|
||||
if (strlen(argument) >= 2)
|
||||
{
|
||||
prefix[0] = argument[0];
|
||||
prefix[1] = argument[1];
|
||||
adc_msg_replace_named_argument(cmd_new, prefix, argument+2);
|
||||
}
|
||||
|
||||
hub_free(argument);
|
||||
argument = adc_msg_get_argument(cmd, n++);
|
||||
}
|
||||
user_set_info(u, cmd_new);
|
||||
adc_msg_free(cmd_new);
|
||||
}
|
||||
|
||||
|
||||
static int check_is_hub_full(struct user* user)
|
||||
static int check_is_hub_full(struct hub_info* hub, struct hub_user* user)
|
||||
{
|
||||
/*
|
||||
* If hub is full, don't let users in, but we still want to allow
|
||||
* operators and admins to enter the hub.
|
||||
*/
|
||||
if (user->hub->config->max_users && user->hub->users->count >= user->hub->config->max_users && !user_is_protected(user))
|
||||
if (hub->config->max_users && hub->users->count >= hub->config->max_users && !user_is_protected(user))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
@@ -663,23 +600,16 @@ static int check_is_hub_full(struct user* user)
|
||||
}
|
||||
|
||||
|
||||
static int check_registered_users_only(struct user* user)
|
||||
static int check_registered_users_only(struct hub_info* hub, struct hub_user* user)
|
||||
{
|
||||
if (user->hub->config->registered_users_only && !user_is_registered(user))
|
||||
if (hub->config->registered_users_only && !user_is_registered(user))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define INF_CHECK(FUNC, USER, CMD) \
|
||||
do { \
|
||||
int ret = FUNC(USER, CMD); \
|
||||
if (ret < 0) \
|
||||
return ret; \
|
||||
} while(0)
|
||||
|
||||
static int hub_handle_info_common(struct user* user, struct adc_message* cmd)
|
||||
static int hub_handle_info_common(struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
/* Remove server restricted flags */
|
||||
remove_server_restricted_flags(cmd);
|
||||
@@ -690,9 +620,9 @@ static int hub_handle_info_common(struct user* user, struct adc_message* cmd)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int hub_handle_info_low_bandwidth(struct user* user, struct adc_message* cmd)
|
||||
static int hub_handle_info_low_bandwidth(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
if (user->hub->config->low_bandwidth_mode)
|
||||
if (hub->config->low_bandwidth_mode)
|
||||
{
|
||||
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_USER_AGENT);
|
||||
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_SHARED_FILES);
|
||||
@@ -711,45 +641,63 @@ static int hub_handle_info_low_bandwidth(struct user* user, struct adc_message*
|
||||
return 0;
|
||||
}
|
||||
|
||||
int hub_handle_info_login(struct user* user, struct adc_message* cmd)
|
||||
#define INF_CHECK(FUNC, HUB, USER, CMD) \
|
||||
do { \
|
||||
int ret = FUNC(HUB, USER, CMD); \
|
||||
if (ret < 0) \
|
||||
return ret; \
|
||||
} while(0)
|
||||
|
||||
int hub_perform_login_checks(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
int need_auth = 0;
|
||||
|
||||
/* Make syntax checks. */
|
||||
INF_CHECK(check_required_login_flags, user, cmd);
|
||||
INF_CHECK(check_cid, user, cmd);
|
||||
INF_CHECK(check_nick, user, cmd);
|
||||
INF_CHECK(check_network, user, cmd);
|
||||
INF_CHECK(check_user_agent, user, cmd);
|
||||
INF_CHECK(check_acl, user, cmd);
|
||||
INF_CHECK(check_logged_in, user, cmd);
|
||||
INF_CHECK(check_required_login_flags, hub, user, cmd);
|
||||
INF_CHECK(check_cid, hub, user, cmd);
|
||||
INF_CHECK(check_nick, hub, user, cmd);
|
||||
INF_CHECK(check_network, hub, user, cmd);
|
||||
INF_CHECK(check_user_agent, hub, user, cmd);
|
||||
INF_CHECK(check_acl, hub, user, cmd);
|
||||
INF_CHECK(check_logged_in, hub, user, cmd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform additional INF checks used at time of login.
|
||||
*
|
||||
* @return 0 if success, <0 if error, >0 if authentication needed.
|
||||
*/
|
||||
int hub_handle_info_login(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
int code = 0;
|
||||
|
||||
INF_CHECK(hub_perform_login_checks, hub, user, cmd);
|
||||
|
||||
/* Private ID must never be broadcasted - drop it! */
|
||||
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_PRIVATE_ID);
|
||||
|
||||
/* FIXME: This needs some cleaning up */
|
||||
need_auth = set_credentials(user, cmd);
|
||||
|
||||
code = set_credentials(hub, user, cmd);
|
||||
|
||||
/* Note: this must be done *after* set_credentials. */
|
||||
if (check_is_hub_full(user))
|
||||
if (check_is_hub_full(hub, user))
|
||||
{
|
||||
return status_msg_hub_full;
|
||||
}
|
||||
|
||||
if (check_registered_users_only(user))
|
||||
if (check_registered_users_only(hub, user))
|
||||
{
|
||||
return status_msg_hub_registered_users_only;
|
||||
}
|
||||
|
||||
INF_CHECK(check_limits, user, cmd);
|
||||
INF_CHECK(check_limits, hub, user, cmd);
|
||||
|
||||
/* strip off stuff if low_bandwidth_mode is enabled */
|
||||
hub_handle_info_low_bandwidth(user, cmd);
|
||||
hub_handle_info_low_bandwidth(hub, user, cmd);
|
||||
|
||||
/* Set initial user info */
|
||||
user_set_info(user, cmd);
|
||||
|
||||
return need_auth;
|
||||
return code;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -765,7 +713,7 @@ int hub_handle_info_login(struct user* user, struct adc_message* cmd)
|
||||
* - CID/PID (valid, not taken, etc).
|
||||
* - IP addresses (IPv4 and IPv6)
|
||||
*/
|
||||
int hub_handle_info(struct user* user, const struct adc_message* cmd_unmodified)
|
||||
int hub_handle_info(struct hub_info* hub, struct hub_user* user, const struct adc_message* cmd_unmodified)
|
||||
{
|
||||
struct adc_message* cmd = adc_msg_copy(cmd_unmodified);
|
||||
if (!cmd) return -1; /* OOM */
|
||||
@@ -789,10 +737,10 @@ int hub_handle_info(struct user* user, const struct adc_message* cmd_unmodified)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ret = hub_handle_info_login(user, cmd);
|
||||
int ret = hub_handle_info_login(hub, user, cmd);
|
||||
if (ret < 0)
|
||||
{
|
||||
on_login_failure(user, ret);
|
||||
on_login_failure(hub, user, ret);
|
||||
adc_msg_free(cmd);
|
||||
return -1;
|
||||
}
|
||||
@@ -804,7 +752,7 @@ int hub_handle_info(struct user* user, const struct adc_message* cmd_unmodified)
|
||||
post.id = UHUB_EVENT_USER_JOIN;
|
||||
post.ptr = user;
|
||||
post.flags = ret; /* 0 - all OK, 1 - need authentication */
|
||||
event_queue_post(user->hub->queue, &post);
|
||||
event_queue_post(hub->queue, &post);
|
||||
adc_msg_free(cmd);
|
||||
return 0;
|
||||
}
|
||||
@@ -821,20 +769,22 @@ int hub_handle_info(struct user* user, const struct adc_message* cmd_unmodified)
|
||||
*/
|
||||
if (adc_msg_has_named_argument(cmd, ADC_INF_FLAG_NICK))
|
||||
{
|
||||
#if ALLOW_CHANGE_NICK
|
||||
if (!check_nick(user, cmd))
|
||||
#ifdef ALLOW_CHANGE_NICK
|
||||
if (!check_nick(hub, user, cmd))
|
||||
#endif
|
||||
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_NICK);
|
||||
}
|
||||
|
||||
/* FIXME - What if limits are not met ? */
|
||||
check_limits(user, cmd);
|
||||
hub_handle_info_low_bandwidth(user, cmd);
|
||||
update_user_info(user, cmd);
|
||||
check_limits(hub, user, cmd);
|
||||
strip_network(user, cmd);
|
||||
hub_handle_info_low_bandwidth(hub, user, cmd);
|
||||
|
||||
user_update_info(user, cmd);
|
||||
|
||||
if (!adc_msg_is_empty(cmd))
|
||||
{
|
||||
route_message(user, cmd);
|
||||
route_message(hub, user, cmd);
|
||||
}
|
||||
|
||||
adc_msg_free(cmd);
|
||||
@@ -47,7 +47,7 @@ enum nick_status
|
||||
*
|
||||
* @return 0 on success, -1 on error
|
||||
*/
|
||||
extern int hub_handle_info(struct user* u, const struct adc_message* cmd);
|
||||
extern int hub_handle_info(struct hub_info* hub, struct hub_user* u, const struct adc_message* cmd);
|
||||
|
||||
|
||||
#endif /* HAVE_UHUB_INF_PARSER_H */
|
||||
@@ -29,24 +29,25 @@ static const char* arg_uid = 0;
|
||||
static const char* arg_gid = 0;
|
||||
static const char* arg_config = 0;
|
||||
static const char* arg_log = 0;
|
||||
static const char* arg_pid = 0;
|
||||
static int arg_log_syslog = 0;
|
||||
|
||||
|
||||
#ifndef WIN32
|
||||
void hub_handle_signal(int fd, short events, void* arg)
|
||||
#if !defined(WIN32)
|
||||
extern struct hub_info* g_hub;
|
||||
void hub_handle_signal(int sig)
|
||||
{
|
||||
struct hub_info* hub = (struct hub_info*) arg;
|
||||
int signal = fd;
|
||||
struct hub_info* hub = g_hub;
|
||||
|
||||
switch (signal)
|
||||
switch (sig)
|
||||
{
|
||||
case SIGINT:
|
||||
hub_log(log_info, "Interrupted. Shutting down...");
|
||||
LOG_INFO("Interrupted. Shutting down...");
|
||||
hub->status = hub_status_shutdown;
|
||||
break;
|
||||
|
||||
case SIGTERM:
|
||||
hub_log(log_info, "Terminated. Shutting down...");
|
||||
LOG_INFO("Terminated. Shutting down...");
|
||||
hub->status = hub_status_shutdown;
|
||||
break;
|
||||
|
||||
@@ -58,13 +59,12 @@ void hub_handle_signal(int fd, short events, void* arg)
|
||||
break;
|
||||
|
||||
default:
|
||||
hub_log(log_trace, "hub_handle_signal(): caught unknown signal: %d", signal);
|
||||
LOG_TRACE("hub_handle_signal(): caught unknown signal: %d", signal);
|
||||
hub->status = hub_status_shutdown;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static struct event signal_events[10];
|
||||
static int signals[] =
|
||||
{
|
||||
SIGINT, /* Interrupt the application */
|
||||
@@ -76,28 +76,26 @@ static int signals[] =
|
||||
|
||||
void setup_signal_handlers(struct hub_info* hub)
|
||||
{
|
||||
sigset_t sig_set;
|
||||
sigemptyset(&sig_set);
|
||||
struct sigaction act;
|
||||
act.sa_mask = sig_set;
|
||||
act.sa_flags = SA_ONSTACK | SA_RESTART;
|
||||
act.sa_handler = hub_handle_signal;
|
||||
int i = 0;
|
||||
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))
|
||||
if (sigaction(signals[i], &act, 0) != 0)
|
||||
{
|
||||
hub_log(log_error, "Error setting signal handler %d", signals[i]);
|
||||
LOG_ERROR("Error setting signal handler %d", signals[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void shutdown_signal_handlers(struct hub_info* hub)
|
||||
{
|
||||
int i = 0;
|
||||
for (i = 0; signals[i]; i++)
|
||||
{
|
||||
signal_del(&signal_events[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* WIN32 */
|
||||
#endif /* !WIN32 */
|
||||
|
||||
|
||||
int main_loop()
|
||||
@@ -113,7 +111,8 @@ int main_loop()
|
||||
{
|
||||
if (hub)
|
||||
{
|
||||
hub_log(log_info, "Reloading configuration files...");
|
||||
LOG_INFO("Reloading configuration files...");
|
||||
LOG_DEBUG("Hub status: %d", (int) hub->status);
|
||||
}
|
||||
|
||||
if (read_config(arg_config, &configuration, !arg_have_config) == -1)
|
||||
@@ -132,7 +131,7 @@ int main_loop()
|
||||
hub = hub_start_service(&configuration);
|
||||
if (!hub)
|
||||
return -1;
|
||||
#ifndef WIN32
|
||||
#if !defined(WIN32)
|
||||
setup_signal_handlers(hub);
|
||||
#endif
|
||||
}
|
||||
@@ -145,9 +144,9 @@ int main_loop()
|
||||
acl_shutdown(&acl);
|
||||
free_config(&configuration);
|
||||
|
||||
} while(hub->status != hub_status_shutdown);
|
||||
} while (hub->status == hub_status_restart);
|
||||
|
||||
#ifndef WIN32
|
||||
#if !defined(WIN32)
|
||||
shutdown_signal_handlers(hub);
|
||||
#endif
|
||||
|
||||
@@ -156,7 +155,7 @@ int main_loop()
|
||||
hub_shutdown_service(hub);
|
||||
}
|
||||
|
||||
net_shutdown();
|
||||
net_destroy();
|
||||
hub_log_shutdown();
|
||||
return 0;
|
||||
}
|
||||
@@ -166,21 +165,22 @@ int check_configuration(int dump)
|
||||
{
|
||||
struct hub_config configuration;
|
||||
int ret = read_config(arg_config, &configuration, 0);
|
||||
|
||||
|
||||
if (dump)
|
||||
{
|
||||
dump_config(&configuration, dump > 1);
|
||||
puts("");
|
||||
}
|
||||
|
||||
if (dump)
|
||||
if (ret != -1)
|
||||
{
|
||||
dump_config(&configuration, dump > 1);
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
if (ret == -1)
|
||||
{
|
||||
fprintf(stderr, "ERROR\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
fprintf(stdout, "OK\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -188,7 +188,7 @@ int check_configuration(int dump)
|
||||
|
||||
void print_version()
|
||||
{
|
||||
fprintf(stdout, "" PRODUCT " " VERSION " " PRODUCT_TITLE "\n");
|
||||
fprintf(stdout, "" PRODUCT " " VERSION "\n");
|
||||
fprintf(stdout, "Copyright (C) 2007-2009, Jan Vidar Krey <janvidar@extatic.org>\n"
|
||||
"This is free software with ABSOLUTELY NO WARRANTY.\n\n");
|
||||
exit(0);
|
||||
@@ -213,6 +213,7 @@ void print_usage(char* program)
|
||||
#ifndef WIN32
|
||||
" -u <user> Run as given user\n"
|
||||
" -g <group> Run with given group permissions\n"
|
||||
" -p <file> Store pid in file (process id)\n"
|
||||
#endif
|
||||
" -V Show version number.\n"
|
||||
);
|
||||
@@ -224,7 +225,7 @@ void print_usage(char* program)
|
||||
void parse_command_line(int argc, char** argv)
|
||||
{
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "vqfc:l:hu:g:VCsSL")) != -1)
|
||||
while ((opt = getopt(argc, argv, "vqfc:l:hu:g:VCsSLp:")) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
@@ -284,6 +285,10 @@ void parse_command_line(int argc, char** argv)
|
||||
arg_gid = optarg;
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
arg_pid = optarg;
|
||||
break;
|
||||
|
||||
default:
|
||||
print_usage(argv[0]);
|
||||
break;
|
||||
@@ -327,15 +332,15 @@ int drop_privileges()
|
||||
|
||||
if (!ret)
|
||||
{
|
||||
hub_log(log_fatal, "Unable to determine group id, check group name.");
|
||||
LOG_FATAL("Unable to determine group id, check group name.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
hub_log(log_trace, "Setting group id %d (%s)", (int) perm_gid, arg_gid);
|
||||
LOG_TRACE("Setting group id %d (%s)", (int) perm_gid, arg_gid);
|
||||
ret = setgid(perm_gid);
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_log(log_fatal, "Unable to change group id, permission denied.");
|
||||
LOG_FATAL("Unable to change group id, permission denied.");
|
||||
return -1;
|
||||
}
|
||||
gid_ok = 1;
|
||||
@@ -360,31 +365,58 @@ int drop_privileges()
|
||||
|
||||
if (!ret)
|
||||
{
|
||||
hub_log(log_fatal, "Unable to determine user id, check user name.");
|
||||
LOG_FATAL("Unable to determine user id, check user name.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!gid_ok) {
|
||||
hub_log(log_trace, "Setting group id %d (%s)", (int) perm_gid, arg_gid);
|
||||
LOG_TRACE("Setting group id %d (%s)", (int) perm_gid, arg_gid);
|
||||
ret = setgid(perm_gid);
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_log(log_fatal, "Unable to change group id, permission denied.");
|
||||
LOG_FATAL("Unable to change group id, permission denied.");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
hub_log(log_trace, "Setting user id %d (%s)", (int) perm_uid, arg_uid);
|
||||
LOG_TRACE("Setting user id %d (%s)", (int) perm_uid, arg_uid);
|
||||
ret = setuid(perm_uid);
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_log(log_fatal, "Unable to change user id, permission denied.");
|
||||
LOG_FATAL("Unable to change user id, permission denied.");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pidfile_create()
|
||||
{
|
||||
if (arg_pid)
|
||||
{
|
||||
FILE* pidfile = fopen(arg_pid, "w");
|
||||
if (!pidfile)
|
||||
{
|
||||
LOG_FATAL("Unable to write pid file: %s\n", arg_pid);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fprintf(pidfile, "%d", (int) getpid());
|
||||
fclose(pidfile);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pidfile_destroy()
|
||||
{
|
||||
if (arg_pid)
|
||||
{
|
||||
return unlink(arg_pid);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* WIN32 */
|
||||
|
||||
|
||||
@@ -405,26 +437,40 @@ int main(int argc, char** argv)
|
||||
ret = fork();
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_log(log_fatal, "Unable to fork to background!");
|
||||
LOG_FATAL("Unable to fork to background!");
|
||||
return -1;
|
||||
}
|
||||
else if (ret == 0)
|
||||
{
|
||||
/* child process */
|
||||
/* child process - detatch from TTY */
|
||||
fclose(stdin);
|
||||
fclose(stdout);
|
||||
fclose(stderr);
|
||||
close(0);
|
||||
close(1);
|
||||
close(2);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* parent process */
|
||||
hub_log(log_debug, "Forked to background\n");
|
||||
LOG_DEBUG("Forked to background\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (pidfile_create() == -1)
|
||||
return -1;
|
||||
|
||||
if (drop_privileges() == -1)
|
||||
return -1;
|
||||
#endif /* WIN32 */
|
||||
|
||||
ret = main_loop();
|
||||
|
||||
#ifndef WIN32
|
||||
pidfile_destroy();
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
244
src/core/netevent.c
Normal file
244
src/core/netevent.c
Normal file
@@ -0,0 +1,244 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <uhub.h>
|
||||
#include "hubio.h"
|
||||
#include "probe.h"
|
||||
|
||||
/* FIXME: This should not be needed! */
|
||||
extern struct hub_info* g_hub;
|
||||
|
||||
#ifdef DEBUG_SENDQ
|
||||
void debug_sendq_send(struct hub_user* user, int sent, int total)
|
||||
{
|
||||
LOG_DUMP("SEND: sd=%d, %d/%d bytes\n", user->connection->sd, sent, total);
|
||||
if (sent == -1)
|
||||
{
|
||||
int err = net_error();
|
||||
LOG_DUMP(" errno: %d - %s\n", err, net_error_string(err));
|
||||
}
|
||||
}
|
||||
|
||||
void debug_sendq_recv(struct hub_user* user, int received, int max, const char* buffer)
|
||||
{
|
||||
LOG_DUMP("RECV: %d/%d bytes\n", received, (int) max);
|
||||
if (received == -1)
|
||||
{
|
||||
int err = net_error();
|
||||
LOG_DUMP(" errno: %d - %s\n", err, net_error_string(err));
|
||||
}
|
||||
else if (received > 0)
|
||||
{
|
||||
char* data = hub_malloc_zero(received + 1);
|
||||
memcpy(data, buffer, received);
|
||||
LOG_DUMP("RECV: \"%s\"\n", data);
|
||||
hub_free(data);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
int handle_net_read(struct hub_user* user)
|
||||
{
|
||||
static char buf[MAX_RECV_BUF];
|
||||
struct hub_recvq* q = user->recv_queue;
|
||||
size_t buf_size = hub_recvq_get(q, buf, MAX_RECV_BUF);
|
||||
ssize_t size = net_con_recv(user->connection, buf, MAX_RECV_BUF);
|
||||
|
||||
if (size > 0)
|
||||
buf_size += size;
|
||||
|
||||
if (size < 0)
|
||||
{
|
||||
if (size == -1)
|
||||
return quit_disconnected;
|
||||
else
|
||||
return quit_socket_error;
|
||||
}
|
||||
else if (size == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
char* lastPos = 0;
|
||||
char* start = buf;
|
||||
char* pos = 0;
|
||||
size_t remaining = buf_size;
|
||||
|
||||
while ((pos = memchr(start, '\n', remaining)))
|
||||
{
|
||||
lastPos = pos;
|
||||
pos[0] = '\0';
|
||||
|
||||
#ifdef DEBUG_SENDQ
|
||||
LOG_DUMP("PROC: \"%s\" (%d)\n", start, (int) (pos - start));
|
||||
#endif
|
||||
|
||||
if (user_flag_get(user, flag_maxbuf))
|
||||
{
|
||||
user_flag_unset(user, flag_maxbuf);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (((pos - start) > 0) && g_hub->config->max_recv_buffer > (pos - start))
|
||||
{
|
||||
if (hub_handle_message(g_hub, user, start, (pos - start)) == -1)
|
||||
{
|
||||
return quit_protocol_error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pos[0] = '\n'; /* FIXME: not needed */
|
||||
pos ++;
|
||||
remaining -= (pos - start);
|
||||
start = pos;
|
||||
}
|
||||
|
||||
if (lastPos || remaining)
|
||||
{
|
||||
if (remaining < g_hub->config->max_recv_buffer)
|
||||
{
|
||||
hub_recvq_set(q, lastPos ? lastPos : buf, remaining);
|
||||
}
|
||||
else
|
||||
{
|
||||
hub_recvq_set(q, 0, 0);
|
||||
user_flag_set(user, flag_maxbuf);
|
||||
LOG_WARN("Received message past max_recv_buffer, dropping message.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
hub_recvq_set(q, 0, 0);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int handle_net_write(struct hub_user* user)
|
||||
{
|
||||
int ret = 0;
|
||||
while (hub_sendq_get_bytes(user->send_queue))
|
||||
{
|
||||
ret = hub_sendq_send(user->send_queue, user);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
return quit_socket_error;
|
||||
|
||||
if (hub_sendq_get_bytes(user->send_queue))
|
||||
{
|
||||
user_net_io_want_write(user);
|
||||
}
|
||||
else
|
||||
{
|
||||
user_net_io_want_read(user);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void net_event(struct net_connection* con, int event, void *arg)
|
||||
{
|
||||
struct hub_user* user = (struct hub_user*) arg;
|
||||
int flag_close = 0;
|
||||
|
||||
#ifdef DEBUG_SENDQ
|
||||
LOG_TRACE("net_event() : fd=%d, ev=%d, arg=%p", con->sd, (int) event, arg);
|
||||
#endif
|
||||
|
||||
if (event == NET_EVENT_TIMEOUT)
|
||||
{
|
||||
if (user_is_connecting(user))
|
||||
{
|
||||
hub_disconnect_user(g_hub, user, quit_timeout);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (event & NET_EVENT_READ)
|
||||
{
|
||||
flag_close = handle_net_read(user);
|
||||
if (flag_close)
|
||||
{
|
||||
hub_disconnect_user(g_hub, user, flag_close);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (event & NET_EVENT_WRITE)
|
||||
{
|
||||
flag_close = handle_net_write(user);
|
||||
if (flag_close)
|
||||
{
|
||||
hub_disconnect_user(g_hub, user, flag_close);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void net_on_accept(struct net_connection* con, int event, void *arg)
|
||||
{
|
||||
struct hub_info* hub = (struct hub_info*) arg;
|
||||
struct hub_probe* probe = 0;
|
||||
struct ip_addr_encap ipaddr;
|
||||
const char* addr;
|
||||
int server_fd = net_con_get_sd(con);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
int fd = net_accept(server_fd, &ipaddr);
|
||||
if (fd == -1)
|
||||
{
|
||||
if (net_error() == EWOULDBLOCK)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("Accept error: %d %s", net_error(), strerror(net_error()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
addr = ip_convert_to_string(&ipaddr);
|
||||
|
||||
/* FIXME: Should have a plugin log this */
|
||||
LOG_TRACE("Got connection from %s", addr);
|
||||
|
||||
/* FIXME: A plugin should perform this check: is IP banned? */
|
||||
if (acl_is_ip_banned(hub->acl, addr))
|
||||
{
|
||||
LOG_INFO("Denied [%s] (IP banned)", addr);
|
||||
net_con_close(con);
|
||||
continue;
|
||||
}
|
||||
|
||||
probe = probe_create(hub, fd, &ipaddr);
|
||||
if (!probe)
|
||||
{
|
||||
LOG_ERROR("Unable to create probe after socket accepted. Out of memory?");
|
||||
net_con_close(con);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -20,32 +20,14 @@
|
||||
#ifndef HAVE_UHUB_NET_EVENT_H
|
||||
#define HAVE_UHUB_NET_EVENT_H
|
||||
|
||||
/**
|
||||
* Network callback for reading data from a socket.
|
||||
*/
|
||||
extern void net_on_read(int fd, short ev, void *arg);
|
||||
|
||||
/**
|
||||
* Network callback for writing data to a socket.
|
||||
*/
|
||||
extern void net_on_write(int fd, short ev, void *arg);
|
||||
|
||||
/**
|
||||
* Network callback for timers.
|
||||
*/
|
||||
extern void net_on_read_timeout(int fd, short ev, void* arg);
|
||||
|
||||
/**
|
||||
* Network callback to accept incoming connections.
|
||||
*/
|
||||
extern void net_on_accept(int fd, short ev, void *arg);
|
||||
extern void net_on_accept(struct net_connection* con, int event, void *arg);
|
||||
extern void net_event(struct net_connection* con, int event, void *arg);
|
||||
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
/**
|
||||
* Network callback to receive incoming UDP datagram.
|
||||
*/
|
||||
extern void net_on_packet(int fd, short ev, void *arg);
|
||||
#endif
|
||||
extern int handle_net_read(struct hub_user* user);
|
||||
extern int handle_net_write(struct hub_user* user);
|
||||
|
||||
|
||||
#endif /* HAVE_UHUB_NET_EVENT_H */
|
||||
119
src/core/probe.c
Normal file
119
src/core/probe.c
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
#include "probe.h"
|
||||
|
||||
#define PROBE_RECV_SIZE 12
|
||||
static char probe_recvbuf[PROBE_RECV_SIZE];
|
||||
|
||||
static void probe_net_event(struct net_connection* con, int events, void *arg)
|
||||
{
|
||||
struct hub_probe* probe = (struct hub_probe*) net_con_get_ptr(con);
|
||||
if (events == NET_EVENT_TIMEOUT)
|
||||
{
|
||||
probe_destroy(probe);
|
||||
return;
|
||||
}
|
||||
|
||||
if (events & NET_EVENT_READ)
|
||||
{
|
||||
int bytes = net_con_peek(con, probe_recvbuf, PROBE_RECV_SIZE);
|
||||
if (bytes < 0)
|
||||
{
|
||||
probe_destroy(probe);
|
||||
return;
|
||||
}
|
||||
|
||||
if (bytes >= 4)
|
||||
{
|
||||
if (memcmp(probe_recvbuf, "HSUP", 4) == 0)
|
||||
{
|
||||
LOG_TRACE("Probed ADC");
|
||||
if (user_create(probe->hub, probe->connection, &probe->addr))
|
||||
{
|
||||
probe->connection = 0;
|
||||
}
|
||||
probe_destroy(probe);
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
if (bytes >= 11 &&
|
||||
probe_recvbuf[0] == 22 &&
|
||||
probe_recvbuf[1] == 3 && /* protocol major version */
|
||||
probe_recvbuf[5] == 1 && /* message type */
|
||||
probe_recvbuf[9] == probe_recvbuf[1] &&
|
||||
probe_recvbuf[10] == probe_recvbuf[2])
|
||||
{
|
||||
if (probe->hub->config->tls_enable)
|
||||
{
|
||||
LOG_TRACE("Probed TLS %d.%d connection", (int) probe_recvbuf[1], (int) probe_recvbuf[2]);
|
||||
if (user_create(probe->hub, probe->connection, &probe->addr))
|
||||
{
|
||||
probe->connection = 0;
|
||||
}
|
||||
net_con_ssl_handshake(con, net_con_ssl_mode_server, probe->hub->ssl_ctx);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_TRACE("Probed TLS %d.%d connection. TLS disabled in hub.", (int) probe_recvbuf[1], (int) probe_recvbuf[2]);
|
||||
}
|
||||
probe_destroy(probe);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_TRACE("Probed TLS %d.%d connection", (int) probe_recvbuf[1], (int) probe_recvbuf[2]);
|
||||
|
||||
net_con_ssl_handshake(con, net_con_ssl_mode_server, probe->hub->ssl_ctx);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
probe_destroy(probe);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct hub_probe* probe_create(struct hub_info* hub, int sd, struct ip_addr_encap* addr)
|
||||
{
|
||||
struct hub_probe* probe = (struct hub_probe*) hub_malloc_zero(sizeof(struct hub_probe));
|
||||
|
||||
if (probe == NULL)
|
||||
return NULL; /* OOM */
|
||||
|
||||
probe->hub = hub;
|
||||
probe->connection = net_con_create();
|
||||
net_con_initialize(probe->connection, sd, probe_net_event, probe, NET_EVENT_READ);
|
||||
net_con_set_timeout(probe->connection, TIMEOUT_CONNECTED);
|
||||
|
||||
memcpy(&probe->addr, addr, sizeof(struct ip_addr_encap));
|
||||
return probe;
|
||||
}
|
||||
|
||||
void probe_destroy(struct hub_probe* probe)
|
||||
{
|
||||
if (probe->connection)
|
||||
{
|
||||
net_con_close(probe->connection);
|
||||
probe->connection = 0;
|
||||
}
|
||||
hub_free(probe);
|
||||
}
|
||||
35
src/core/probe.h
Normal file
35
src/core/probe.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef HAVE_UHUB_PROBE_H
|
||||
#define HAVE_UHUB_PROBE_H
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
struct hub_probe
|
||||
{
|
||||
struct hub_info* hub; /** The hub instance this probe belong to */
|
||||
struct net_connection* connection; /** Connection data */
|
||||
struct ip_addr_encap addr; /** IP address */
|
||||
};
|
||||
|
||||
extern struct hub_probe* probe_create(struct hub_info* hub, int sd, struct ip_addr_encap* addr);
|
||||
extern void probe_destroy(struct hub_probe* probe);
|
||||
|
||||
#endif /* HAVE_UHUB_PROBE_H */
|
||||
228
src/core/route.c
Normal file
228
src/core/route.c
Normal file
@@ -0,0 +1,228 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
int route_message(struct hub_info* hub, struct hub_user* u, struct adc_message* msg)
|
||||
{
|
||||
struct hub_user* target = NULL;
|
||||
|
||||
switch (msg->cache[0])
|
||||
{
|
||||
case 'B': /* Broadcast to all logged in clients */
|
||||
route_to_all(hub, msg);
|
||||
break;
|
||||
|
||||
case 'D':
|
||||
target = uman_get_user_by_sid(hub, msg->target);
|
||||
if (target)
|
||||
{
|
||||
route_to_user(hub, target, msg);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'E':
|
||||
target = uman_get_user_by_sid(hub, msg->target);
|
||||
if (target)
|
||||
{
|
||||
route_to_user(hub, target, msg);
|
||||
route_to_user(hub, u, msg);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'F':
|
||||
route_to_subscribers(hub, msg);
|
||||
break;
|
||||
|
||||
default:
|
||||
/* Ignore the message */
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline size_t get_max_send_queue(struct hub_info* hub)
|
||||
{
|
||||
/* TODO: More dynamic send queue limit, for instance:
|
||||
* return MAX(hub->config->max_send_buffer, (hub->config->max_recv_buffer * hub_get_user_count(hub)));
|
||||
*/
|
||||
return hub->config->max_send_buffer;
|
||||
}
|
||||
|
||||
static inline size_t get_max_send_queue_soft(struct hub_info* hub)
|
||||
{
|
||||
return hub->config->max_send_buffer_soft;
|
||||
}
|
||||
|
||||
/*
|
||||
* @return 1 if send queue is OK.
|
||||
* -1 if send queue is overflowed
|
||||
* 0 if soft send queue is overflowed (not implemented at the moment)
|
||||
*/
|
||||
static inline int check_send_queue(struct hub_info* hub, struct hub_user* user, struct adc_message* msg)
|
||||
{
|
||||
if (user_flag_get(user, flag_user_list))
|
||||
return 1;
|
||||
|
||||
if ((user->send_queue->size + msg->length) > get_max_send_queue(hub))
|
||||
{
|
||||
LOG_WARN("send queue overflowed, message discarded.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (user->send_queue->size > get_max_send_queue_soft(hub))
|
||||
{
|
||||
LOG_WARN("send queue soft overflowed.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int route_to_user(struct hub_info* hub, struct hub_user* user, struct adc_message* msg)
|
||||
{
|
||||
#ifdef DEBUG_SENDQ
|
||||
char* data = strndup(msg->cache, msg->length-1);
|
||||
LOG_PROTO("send %s: \"%s\"", sid_to_string(user->id.sid), data);
|
||||
free(data);
|
||||
#endif
|
||||
|
||||
if (!user->connection)
|
||||
return 0;
|
||||
|
||||
if (hub_sendq_is_empty(user->send_queue) && !user_flag_get(user, flag_pipeline))
|
||||
{
|
||||
/* Perform oportunistic write */
|
||||
hub_sendq_add(user->send_queue, msg);
|
||||
handle_net_write(user);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (check_send_queue(hub, user, msg) >= 0)
|
||||
{
|
||||
hub_sendq_add(user->send_queue, msg);
|
||||
if (!user_flag_get(user, flag_pipeline))
|
||||
user_net_io_want_write(user);
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int route_flush_pipeline(struct hub_info* hub, struct hub_user* u)
|
||||
{
|
||||
if (hub_sendq_is_empty(u->send_queue))
|
||||
return 0;
|
||||
|
||||
handle_net_write(u);
|
||||
user_flag_unset(u, flag_pipeline);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int route_to_all(struct hub_info* hub, struct adc_message* command) /* iterate users */
|
||||
{
|
||||
struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list);
|
||||
while (user)
|
||||
{
|
||||
route_to_user(hub, user, command);
|
||||
user = (struct hub_user*) list_get_next(hub->users->list);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int route_to_subscribers(struct hub_info* hub, struct adc_message* command) /* iterate users */
|
||||
{
|
||||
int do_send;
|
||||
char* tmp;
|
||||
|
||||
struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list);
|
||||
while (user)
|
||||
{
|
||||
if (user->feature_cast)
|
||||
{
|
||||
do_send = 1;
|
||||
|
||||
tmp = list_get_first(command->feature_cast_include);
|
||||
while (tmp)
|
||||
{
|
||||
if (!user_have_feature_cast_support(user, tmp))
|
||||
{
|
||||
do_send = 0;
|
||||
break;
|
||||
}
|
||||
tmp = list_get_next(command->feature_cast_include);;
|
||||
}
|
||||
|
||||
if (!do_send) {
|
||||
user = (struct hub_user*) list_get_next(hub->users->list);
|
||||
continue;
|
||||
}
|
||||
|
||||
tmp = list_get_first(command->feature_cast_exclude);
|
||||
while (tmp)
|
||||
{
|
||||
if (user_have_feature_cast_support(user, tmp))
|
||||
{
|
||||
do_send = 0;
|
||||
break;
|
||||
}
|
||||
tmp = list_get_next(command->feature_cast_exclude);
|
||||
}
|
||||
|
||||
if (do_send)
|
||||
{
|
||||
route_to_user(hub, user, command);
|
||||
}
|
||||
}
|
||||
user = (struct hub_user*) list_get_next(hub->users->list);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int route_info_message(struct hub_info* hub, struct hub_user* u)
|
||||
{
|
||||
if (!user_is_nat_override(u))
|
||||
{
|
||||
return route_to_all(hub, u->info);
|
||||
}
|
||||
else
|
||||
{
|
||||
struct adc_message* cmd = adc_msg_copy(u->info);
|
||||
const char* address = user_get_address(u);
|
||||
struct hub_user* user = 0;
|
||||
|
||||
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR);
|
||||
adc_msg_add_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR, address);
|
||||
|
||||
user = (struct hub_user*) list_get_first(hub->users->list);
|
||||
while (user)
|
||||
{
|
||||
if (user_is_nat_override(user))
|
||||
route_to_user(hub, user, cmd);
|
||||
else
|
||||
route_to_user(hub, user, u->info);
|
||||
|
||||
user = (struct hub_user*) list_get_next(hub->users->list);
|
||||
}
|
||||
adc_msg_free(cmd);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -23,12 +23,17 @@
|
||||
/**
|
||||
* Route a message by sending it to it's final destination.
|
||||
*/
|
||||
extern int route_message(struct user* u, struct adc_message* msg);
|
||||
extern int route_message(struct hub_info* hub, struct hub_user* u, struct adc_message* msg);
|
||||
|
||||
/**
|
||||
* Send queued messages.
|
||||
*/
|
||||
extern int route_flush_pipeline(struct hub_info* hub, struct hub_user* u);
|
||||
|
||||
/**
|
||||
* Transmit message directly to one user.
|
||||
*/
|
||||
extern int route_to_user(struct user*, struct adc_message* command);
|
||||
extern int route_to_user(struct hub_info* hub, struct hub_user*, struct adc_message* command);
|
||||
|
||||
/**
|
||||
* Broadcast message to all users.
|
||||
@@ -45,7 +50,7 @@ extern int route_to_subscribers(struct hub_info* hub, struct adc_message* comman
|
||||
* This will ensure the correct IP is seen by other users
|
||||
* in case nat override is in use.
|
||||
*/
|
||||
extern int route_info_message(struct user* user);
|
||||
extern int route_info_message(struct hub_info* hub, struct hub_user* user);
|
||||
|
||||
|
||||
#endif /* HAVE_UHUB_ROUTE_H */
|
||||
349
src/core/user.c
Normal file
349
src/core/user.c
Normal file
@@ -0,0 +1,349 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
#ifdef DEBUG_SENDQ
|
||||
static const char* user_log_str(struct hub_user* user)
|
||||
{
|
||||
static char buf[128];
|
||||
if (user)
|
||||
{
|
||||
snprintf(buf, 128, "user={ %p, \"%s\", %s/%s}", user, user->id.nick, sid_to_string(user->id.sid), user->id.cid);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(buf, 128, "user={ %p }", user);
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct hub_user* user_create(struct hub_info* hub, struct net_connection* con, struct ip_addr_encap* addr)
|
||||
{
|
||||
struct hub_user* user = NULL;
|
||||
|
||||
LOG_TRACE("user_create(), hub=%p, con[sd=%d]", hub, net_con_get_sd(con));
|
||||
|
||||
user = (struct hub_user*) hub_malloc_zero(sizeof(struct hub_user));
|
||||
|
||||
if (user == NULL)
|
||||
return NULL; /* OOM */
|
||||
|
||||
user->send_queue = hub_sendq_create();
|
||||
user->recv_queue = hub_recvq_create();
|
||||
|
||||
user->connection = con;
|
||||
net_con_reinitialize(user->connection, net_event, user, NET_EVENT_READ);
|
||||
|
||||
memcpy(&user->id.addr, addr, sizeof(struct ip_addr_encap));
|
||||
user_set_state(user, state_protocol);
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
void user_destroy(struct hub_user* user)
|
||||
{
|
||||
LOG_TRACE("user_destroy(), user=%p", user);
|
||||
|
||||
hub_recvq_destroy(user->recv_queue);
|
||||
hub_sendq_destroy(user->send_queue);
|
||||
|
||||
adc_msg_free(user->info);
|
||||
user_clear_feature_cast_support(user);
|
||||
hub_free(user);
|
||||
}
|
||||
|
||||
void user_set_state(struct hub_user* user, enum user_state state)
|
||||
{
|
||||
if ((user->state == state_cleanup && state != state_disconnected) || (user->state == state_disconnected))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
user->state = state;
|
||||
}
|
||||
|
||||
void user_set_info(struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
adc_msg_free(user->info);
|
||||
user->info = adc_msg_incref(cmd);
|
||||
}
|
||||
|
||||
void user_update_info(struct hub_user* u, struct adc_message* cmd)
|
||||
{
|
||||
char prefix[2];
|
||||
char* argument;
|
||||
size_t n = 0;
|
||||
struct adc_message* cmd_new = adc_msg_copy(u->info);
|
||||
if (!cmd_new)
|
||||
{
|
||||
/* FIXME: OOM! */
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* FIXME: Optimization potential:
|
||||
*
|
||||
* remove parts of cmd that do not really change anything in cmd_new.
|
||||
* this can save bandwidth if clients send multiple updates for information
|
||||
* that does not really change anything.
|
||||
*/
|
||||
argument = adc_msg_get_argument(cmd, n++);
|
||||
while (argument)
|
||||
{
|
||||
if (strlen(argument) >= 2)
|
||||
{
|
||||
prefix[0] = argument[0];
|
||||
prefix[1] = argument[1];
|
||||
adc_msg_replace_named_argument(cmd_new, prefix, argument+2);
|
||||
}
|
||||
|
||||
hub_free(argument);
|
||||
argument = adc_msg_get_argument(cmd, n++);
|
||||
}
|
||||
user_set_info(u, cmd_new);
|
||||
adc_msg_free(cmd_new);
|
||||
}
|
||||
|
||||
|
||||
static int convert_support_fourcc(int fourcc)
|
||||
{
|
||||
switch (fourcc)
|
||||
{
|
||||
case FOURCC('B','A','S','0'): /* Obsolete */
|
||||
#ifndef OLD_ADC_SUPPORT
|
||||
return 0;
|
||||
#endif
|
||||
case FOURCC('B','A','S','E'):
|
||||
return feature_base;
|
||||
|
||||
case FOURCC('A','U','T','0'):
|
||||
return feature_auto;
|
||||
|
||||
case FOURCC('U','C','M','0'):
|
||||
case FOURCC('U','C','M','D'):
|
||||
return feature_ucmd;
|
||||
|
||||
case FOURCC('Z','L','I','F'):
|
||||
return feature_zlif;
|
||||
|
||||
case FOURCC('B','B','S','0'):
|
||||
return feature_bbs;
|
||||
|
||||
case FOURCC('T','I','G','R'):
|
||||
return feature_tiger;
|
||||
|
||||
case FOURCC('B','L','O','M'):
|
||||
case FOURCC('B','L','O','0'):
|
||||
return feature_bloom;
|
||||
|
||||
case FOURCC('P','I','N','G'):
|
||||
return feature_ping;
|
||||
|
||||
case FOURCC('L','I','N','K'):
|
||||
return feature_link;
|
||||
|
||||
case FOURCC('A','D','C','S'):
|
||||
return feature_adcs;
|
||||
|
||||
default:
|
||||
LOG_DEBUG("Unknown extension: %x", fourcc);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void user_support_add(struct hub_user* user, int fourcc)
|
||||
{
|
||||
int feature_mask = convert_support_fourcc(fourcc);
|
||||
user->flags |= feature_mask;
|
||||
}
|
||||
|
||||
int user_flag_get(struct hub_user* user, enum user_flags flag)
|
||||
{
|
||||
return user->flags & flag;
|
||||
}
|
||||
|
||||
void user_flag_set(struct hub_user* user, enum user_flags flag)
|
||||
{
|
||||
user->flags |= flag;
|
||||
}
|
||||
|
||||
void user_flag_unset(struct hub_user* user, enum user_flags flag)
|
||||
{
|
||||
user->flags &= ~flag;
|
||||
}
|
||||
|
||||
void user_set_nat_override(struct hub_user* user)
|
||||
{
|
||||
user_flag_set(user, flag_nat);
|
||||
}
|
||||
|
||||
int user_is_nat_override(struct hub_user* user)
|
||||
{
|
||||
return user_flag_get(user, flag_nat);
|
||||
}
|
||||
|
||||
void user_support_remove(struct hub_user* user, int fourcc)
|
||||
{
|
||||
int feature_mask = convert_support_fourcc(fourcc);
|
||||
user->flags &= ~feature_mask;
|
||||
}
|
||||
|
||||
int user_have_feature_cast_support(struct hub_user* user, char feature[4])
|
||||
{
|
||||
char* tmp = list_get_first(user->feature_cast);
|
||||
while (tmp)
|
||||
{
|
||||
if (strncmp(tmp, feature, 4) == 0)
|
||||
return 1;
|
||||
|
||||
tmp = list_get_next(user->feature_cast);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int user_set_feature_cast_support(struct hub_user* u, char feature[4])
|
||||
{
|
||||
if (!u->feature_cast)
|
||||
{
|
||||
u->feature_cast = list_create();
|
||||
}
|
||||
|
||||
if (!u->feature_cast)
|
||||
{
|
||||
return 0; /* OOM! */
|
||||
}
|
||||
|
||||
list_append(u->feature_cast, hub_strndup(feature, 4));
|
||||
return 1;
|
||||
}
|
||||
|
||||
void user_clear_feature_cast_support(struct hub_user* u)
|
||||
{
|
||||
if (u->feature_cast)
|
||||
{
|
||||
list_clear(u->feature_cast, &hub_free);
|
||||
list_destroy(u->feature_cast);
|
||||
u->feature_cast = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int user_is_logged_in(struct hub_user* user)
|
||||
{
|
||||
if (user->state == state_normal)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int user_is_connecting(struct hub_user* user)
|
||||
{
|
||||
if (user->state == state_protocol || user->state == state_identify || user->state == state_verify)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int user_is_protocol_negotiating(struct hub_user* user)
|
||||
{
|
||||
if (user->state == state_protocol)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int user_is_disconnecting(struct hub_user* user)
|
||||
{
|
||||
if (user->state == state_cleanup || user->state == state_disconnected)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int user_is_protected(struct hub_user* user)
|
||||
{
|
||||
switch (user->credentials)
|
||||
{
|
||||
case cred_bot:
|
||||
case cred_operator:
|
||||
case cred_super:
|
||||
case cred_admin:
|
||||
case cred_link:
|
||||
return 1;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 1 if a user is registered.
|
||||
* Only registered users will be let in if the hub is configured for registered
|
||||
* users only.
|
||||
*/
|
||||
int user_is_registered(struct hub_user* user)
|
||||
{
|
||||
switch (user->credentials)
|
||||
{
|
||||
case cred_bot:
|
||||
case cred_user:
|
||||
case cred_operator:
|
||||
case cred_super:
|
||||
case cred_admin:
|
||||
case cred_link:
|
||||
return 1;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void user_net_io_want_write(struct hub_user* user)
|
||||
{
|
||||
net_con_update(user->connection, NET_EVENT_READ | NET_EVENT_WRITE);
|
||||
}
|
||||
|
||||
void user_net_io_want_read(struct hub_user* user)
|
||||
{
|
||||
net_con_update(user->connection, NET_EVENT_READ);
|
||||
}
|
||||
|
||||
const char* user_get_quit_reason_string(enum user_quit_reason reason)
|
||||
{
|
||||
switch (reason)
|
||||
{
|
||||
case quit_unknown: return "unknown"; break;
|
||||
case quit_disconnected: return "disconnected"; break;
|
||||
case quit_kicked: return "kicked"; break;
|
||||
case quit_banned: return "banned"; break;
|
||||
case quit_timeout: return "timeout"; break;
|
||||
case quit_send_queue: return "send queue"; break;
|
||||
case quit_memory_error: return "out of memory"; break;
|
||||
case quit_socket_error: return "socket error"; break;
|
||||
case quit_protocol_error: return "protocol error"; break;
|
||||
case quit_logon_error: return "login error"; break;
|
||||
case quit_hub_disabled: return "hub disabled"; break;
|
||||
case quit_ghost_timeout: return "ghost"; break;
|
||||
}
|
||||
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
const char* user_get_address(struct hub_user* user)
|
||||
{
|
||||
return ip_convert_to_string(&user->id.addr);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -20,9 +20,8 @@
|
||||
#ifndef HAVE_UHUB_USER_H
|
||||
#define HAVE_UHUB_USER_H
|
||||
|
||||
|
||||
struct hub_info;
|
||||
|
||||
struct hub_iobuf;
|
||||
|
||||
enum user_state
|
||||
{
|
||||
@@ -34,7 +33,6 @@ enum user_state
|
||||
state_disconnected = 5, /**<< "User is disconnected" */
|
||||
};
|
||||
|
||||
|
||||
enum user_flags
|
||||
{
|
||||
feature_base = 0x00000001, /** BASE: Basic configuration (required by all clients) */
|
||||
@@ -46,16 +44,18 @@ enum user_flags
|
||||
feature_bloom = 0x00000040, /** BLO0: Bloom filter (not supported) */
|
||||
feature_ping = 0x00000080, /** PING: Hub pinger information extension */
|
||||
feature_link = 0x00000100, /** LINK: Hub link (not supported) */
|
||||
feature_adcs = 0x00000200, /** ADCS: ADC over TLS/SSL */
|
||||
flag_muted = 0x00800000, /** User is muted (cannot chat) */
|
||||
flag_ignore = 0x01000000, /** Ignore further reads */
|
||||
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 */
|
||||
flag_pipeline = 0x40000000, /** Hub message pipelining */
|
||||
flag_nat = 0x80000000, /** nat override enabled */
|
||||
};
|
||||
|
||||
|
||||
enum user_quit_reason
|
||||
{
|
||||
quit_unknown = 0,
|
||||
@@ -72,12 +72,15 @@ enum user_quit_reason
|
||||
quit_ghost_timeout = 11, /** The user is a ghost, and trying to login from another connection */
|
||||
};
|
||||
|
||||
/** Returns an apropriate string for the given quit reason */
|
||||
extern const char* user_get_quit_reason_string(enum user_quit_reason);
|
||||
|
||||
struct user_info
|
||||
struct hub_user_info
|
||||
{
|
||||
sid_t sid; /** session ID */
|
||||
char cid[MAX_CID_LEN+1]; /** global client ID */
|
||||
char nick[MAX_NICK_LEN+1]; /** User's nick name */
|
||||
struct ip_addr_encap addr; /** User's IP address */
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -85,45 +88,33 @@ struct user_info
|
||||
* as the number of bytes and files shared, and the number of hubs the
|
||||
* user is connected to, etc.
|
||||
*/
|
||||
struct user_counts
|
||||
struct hub_user_limits
|
||||
{
|
||||
uint64_t shared_size; /** Shared size in bytes */
|
||||
size_t shared_files; /** The number of shared files */
|
||||
size_t upload_slots; /** The number of upload slots */
|
||||
size_t hub_count_user; /** The number of hubs connected as user */
|
||||
size_t hub_count_registered; /** The number of hubs connected as registered user */
|
||||
size_t hub_count_operator; /** The number of hubs connected as operator */
|
||||
size_t hub_count_total; /** The number of hubs connected to in total */
|
||||
uint64_t shared_size; /** Shared size in bytes */
|
||||
size_t shared_files; /** The number of shared files */
|
||||
size_t upload_slots; /** The number of upload slots */
|
||||
size_t hub_count_user; /** The number of hubs connected as user */
|
||||
size_t hub_count_registered; /** The number of hubs connected as registered user */
|
||||
size_t hub_count_operator; /** The number of hubs connected as operator */
|
||||
size_t hub_count_total; /** The number of hubs connected to in total */
|
||||
};
|
||||
|
||||
struct user
|
||||
struct hub_user
|
||||
{
|
||||
int sd; /** socket descriptor */
|
||||
struct event* ev_read; /** libevent struct for read events */
|
||||
struct event* ev_write; /** libevent struct for write events */
|
||||
enum user_state state; /** see enum user_state */
|
||||
enum user_credentials credentials; /** see enum user_credentials */
|
||||
struct user_info id; /** Contains nick name and CID */
|
||||
int flags; /** see enum user_features */
|
||||
char user_agent[MAX_UA_LEN+1];/** User agent string */
|
||||
time_t tm_connected; /** time when user connected */
|
||||
time_t tm_last_read; /** time the user last received something from the hub */
|
||||
time_t tm_last_write; /** time the user last sent something to the hub */
|
||||
struct linked_list* feature_cast; /** Features supported by feature cast */
|
||||
struct adc_message* info; /** ADC 'INF' message (broadcasted to everyone joining the hub) */
|
||||
size_t send_queue_offset; /** Send queue byte offset */
|
||||
struct linked_list* send_queue; /** Send queue */
|
||||
int send_queue_size; /** Size of send queue (in bytes, not messages) */
|
||||
int send_queue_esize; /** Effective send queue size */
|
||||
char* recv_buf; /** Recv buffer */
|
||||
size_t recv_buf_offset; /** Recv buffer offset */
|
||||
struct hub_info* hub; /** The hub instance this user belong to */
|
||||
int quit_reason; /** Quit reason (see user_quit_reason) */
|
||||
struct ip_addr_encap ipaddr; /** IP address of connected user */
|
||||
struct user_counts limits; /** Data used for limitation */
|
||||
#ifdef SSL_SUPPORT
|
||||
SSL* ssl; /** SSL handle */
|
||||
#endif /* SSL_SUPPORT */
|
||||
enum user_state state; /** see enum user_state */
|
||||
enum user_credentials credentials; /** see enum user_credentials */
|
||||
struct hub_user_info id; /** Contains nick name and CID */
|
||||
uint32_t flags; /** see enum user_features */
|
||||
char user_agent[MAX_UA_LEN+1];/** User agent string */
|
||||
struct linked_list* feature_cast; /** Features supported by feature cast */
|
||||
struct adc_message* info; /** ADC 'INF' message (broadcasted to everyone joining the hub) */
|
||||
struct hub_info* hub; /** The hub instance this user belong to */
|
||||
struct hub_recvq* recv_queue;
|
||||
struct hub_sendq* send_queue;
|
||||
struct net_connection* connection; /** Connection data */
|
||||
struct hub_user_limits limits; /** Data used for limitation */
|
||||
enum user_quit_reason quit_reason; /** Quit reason (see user_quit_reason) */
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -137,64 +128,69 @@ struct user
|
||||
* @param sd socket descriptor associated with the user
|
||||
* @return User object or NULL if not enough memory is available.
|
||||
*/
|
||||
extern struct user* user_create(struct hub_info* hub, int sd);
|
||||
extern struct hub_user* user_create(struct hub_info* hub, struct net_connection* con, struct ip_addr_encap* addr);
|
||||
|
||||
/**
|
||||
* Delete a user.
|
||||
*
|
||||
* !WRONG! If the user is logged in a quit message is issued.
|
||||
*/
|
||||
extern void user_destroy(struct user* user);
|
||||
|
||||
/**
|
||||
* Will post a message that will delete the user later.
|
||||
*/
|
||||
extern void user_schedule_destroy(struct user* user);
|
||||
|
||||
/**
|
||||
* Disconnect a user.
|
||||
* This will mark the user connection ready for being terminated.
|
||||
* A reason can be given using the enum user_quit_reason.
|
||||
*
|
||||
* Things to be done when calling this:
|
||||
* - Mark the user with state_cleanup
|
||||
*
|
||||
* If the user is logged in to the hub:
|
||||
* - post message: UHUB_EVENT_USER_QUIT
|
||||
*
|
||||
* @param user User to disconnect
|
||||
* @param reason See enum user_quit_reason
|
||||
*/
|
||||
extern void user_disconnect(struct user* user, int reason);
|
||||
extern void user_destroy(struct hub_user* user);
|
||||
|
||||
/**
|
||||
* This associates a INF message to the user.
|
||||
* If the user already has a INF message associated, then this is
|
||||
* released before setting the new one.
|
||||
*
|
||||
* @param info new inf message (can be NULL)
|
||||
*/
|
||||
extern void user_set_info(struct user* user, struct adc_message* info);
|
||||
extern void user_set_info(struct hub_user* user, struct adc_message* info);
|
||||
|
||||
/**
|
||||
* Update a user's INF message.
|
||||
* Will parse replace all ellements in the user's inf message with
|
||||
* the parameters from the cmd (merge operation).
|
||||
*/
|
||||
extern void user_update_info(struct hub_user* user, struct adc_message* cmd);
|
||||
|
||||
/**
|
||||
* Specify a user's state.
|
||||
* NOTE: DON'T, unless you know what you are doing.
|
||||
*/
|
||||
extern void user_set_state(struct user* user, enum user_state);
|
||||
extern void user_set_state(struct hub_user* user, enum user_state);
|
||||
|
||||
/**
|
||||
* Returns 1 if the user is in state state_normal, or 0 otherwise.
|
||||
*/
|
||||
extern int user_is_logged_in(struct user* user);
|
||||
extern int user_is_logged_in(struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Returns 1 if the user is in state_protocol.
|
||||
* Returns 0 otherwise.
|
||||
*/
|
||||
extern int user_is_protocol_negotiating(struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Returns 1 if the user is in state_protocol, state_identify or state_verify.
|
||||
* Returns 0 otherwise.
|
||||
*/
|
||||
extern int user_is_connecting(struct user* user);
|
||||
extern int user_is_connecting(struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Returns 1 only if the user is in state_cleanup or state_disconnected.
|
||||
*/
|
||||
extern int user_is_disconnecting(struct user* user);
|
||||
extern int user_is_disconnecting(struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Returns 1 if a user is protected, which includes users
|
||||
* having any form of elevated privileges.
|
||||
*/
|
||||
extern int user_is_protected(struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Returns 1 if a user is registered, with or without privileges.
|
||||
*/
|
||||
extern int user_is_registered(struct hub_user* user);
|
||||
|
||||
/**
|
||||
* User supports the protocol extension as given in fourcc.
|
||||
@@ -204,7 +200,7 @@ extern int user_is_disconnecting(struct user* user);
|
||||
*
|
||||
* @see enum user_flags
|
||||
*/
|
||||
extern void user_support_add(struct user* user, int fourcc);
|
||||
extern void user_support_add(struct hub_user* user, int fourcc);
|
||||
|
||||
/**
|
||||
* User no longer supports the protocol extension as given in fourcc.
|
||||
@@ -212,26 +208,28 @@ extern void user_support_add(struct user* user, int fourcc);
|
||||
* the hub.
|
||||
* @see enum user_flags
|
||||
*/
|
||||
extern void user_support_remove(struct user* user, int fourcc);
|
||||
extern void user_support_remove(struct hub_user* user, int fourcc);
|
||||
|
||||
extern const char* user_get_address(struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Sets the nat override flag for a user, this allows users on the same
|
||||
* subnet as a natted hub to spoof their IP in order to use active mode
|
||||
* on a natted hub.
|
||||
*/
|
||||
extern void user_set_nat_override(struct user* user);
|
||||
extern int user_is_nat_override(struct user* user);
|
||||
extern void user_set_nat_override(struct hub_user* user);
|
||||
extern int user_is_nat_override(struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Set a flag. @see enum user_flags
|
||||
*/
|
||||
extern void user_flag_set(struct user* user, enum user_flags flag);
|
||||
extern void user_flag_unset(struct user* user, enum user_flags flag);
|
||||
extern void user_flag_set(struct hub_user* user, enum user_flags flag);
|
||||
extern void user_flag_unset(struct hub_user* user, enum user_flags flag);
|
||||
|
||||
/**
|
||||
* Get a flag. @see enum user_flags
|
||||
*/
|
||||
extern int user_flag_get(struct user* user, enum user_flags flag);
|
||||
extern int user_flag_get(struct hub_user* user, enum user_flags flag);
|
||||
|
||||
/**
|
||||
* Check if a user supports 'feature' for feature casting (basis for 'Fxxx' messages)
|
||||
@@ -241,7 +239,7 @@ extern int user_flag_get(struct user* user, enum user_flags flag);
|
||||
* @param feature a feature to lookup (example: 'TCP4' or 'UDP4')
|
||||
* @return 1 if 'feature' supported, or 0 otherwise
|
||||
*/
|
||||
extern int user_have_feature_cast_support(struct user* user, char feature[4]);
|
||||
extern int user_have_feature_cast_support(struct hub_user* user, char feature[4]);
|
||||
|
||||
/**
|
||||
* Set feature cast support for feature.
|
||||
@@ -249,14 +247,22 @@ extern int user_have_feature_cast_support(struct user* user, char feature[4]);
|
||||
* @param feature a feature to lookup (example: 'TCP4' or 'UDP4')
|
||||
* @return 1 if 'feature' supported, or 0 otherwise
|
||||
*/
|
||||
extern int user_set_feature_cast_support(struct user* u, char feature[4]);
|
||||
extern int user_set_feature_cast_support(struct hub_user* u, char feature[4]);
|
||||
|
||||
/**
|
||||
* Remove all feature cast support features.
|
||||
*/
|
||||
extern void user_clear_feature_cast_support(struct user* u);
|
||||
extern void user_clear_feature_cast_support(struct hub_user* u);
|
||||
|
||||
/**
|
||||
* Mark the user with a want-write flag, meaning it should poll for writability.
|
||||
*/
|
||||
extern void user_net_io_want_write(struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Mark the user with a want read flag, meaning it should poll for readability.
|
||||
*/
|
||||
extern void user_net_io_want_read(struct hub_user* user);
|
||||
|
||||
#endif /* HAVE_UHUB_USER_H */
|
||||
|
||||
265
src/core/usermanager.c
Normal file
265
src/core/usermanager.c
Normal file
@@ -0,0 +1,265 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
/*
|
||||
* This callback function is used to clear user objects from the userlist.
|
||||
* Should only be used in uman_shutdown().
|
||||
*/
|
||||
static void clear_user_list_callback(void* ptr)
|
||||
{
|
||||
if (ptr)
|
||||
{
|
||||
struct hub_user* u = (struct hub_user*) ptr;
|
||||
|
||||
/* Mark the user as already being disconnected.
|
||||
* This prevents the hub from trying to send
|
||||
* quit messages to other users.
|
||||
*/
|
||||
u->credentials = cred_none;
|
||||
user_destroy(u);
|
||||
}
|
||||
}
|
||||
|
||||
void uman_update_stats(struct hub_info* hub)
|
||||
{
|
||||
const int factor = TIMEOUT_STATS;
|
||||
struct net_statistics* total;
|
||||
struct net_statistics* intermediate;
|
||||
net_stats_get(&intermediate, &total);
|
||||
|
||||
hub->stats.net_tx = (intermediate->tx / factor);
|
||||
hub->stats.net_rx = (intermediate->rx / factor);
|
||||
hub->stats.net_tx_peak = MAX(hub->stats.net_tx, hub->stats.net_tx_peak);
|
||||
hub->stats.net_rx_peak = MAX(hub->stats.net_rx, hub->stats.net_rx_peak);
|
||||
hub->stats.net_tx_total = total->tx;
|
||||
hub->stats.net_rx_total = total->rx;
|
||||
|
||||
net_stats_reset();
|
||||
}
|
||||
|
||||
void uman_print_stats(struct hub_info* hub)
|
||||
{
|
||||
LOG_INFO("Statistics users=" PRINTF_SIZE_T " (peak_users=" PRINTF_SIZE_T "), net_tx=%d KB/s, net_rx=%d KB/s (peak_tx=%d KB/s, peak_rx=%d KB/s)",
|
||||
hub->users->count,
|
||||
hub->users->count_peak,
|
||||
(int) hub->stats.net_tx / 1024,
|
||||
(int) hub->stats.net_rx / 1024,
|
||||
(int) hub->stats.net_tx_peak / 1024,
|
||||
(int) hub->stats.net_rx_peak / 1024);
|
||||
}
|
||||
|
||||
static void timer_statistics(struct timeout_evt* t)
|
||||
{
|
||||
struct hub_info* hub = (struct hub_info*) t->ptr;
|
||||
uman_update_stats(hub);
|
||||
timeout_queue_reschedule(net_backend_get_timeout_queue(), hub->users->timeout, TIMEOUT_STATS);
|
||||
}
|
||||
|
||||
int uman_init(struct hub_info* hub)
|
||||
{
|
||||
struct hub_user_manager* users = NULL;
|
||||
if (!hub)
|
||||
return -1;
|
||||
|
||||
users = (struct hub_user_manager*) hub_malloc_zero(sizeof(struct hub_user_manager));
|
||||
if (!users)
|
||||
return -1;
|
||||
|
||||
users->list = list_create();
|
||||
users->sids = sid_pool_create(net_get_max_sockets());
|
||||
|
||||
if (!users->list)
|
||||
{
|
||||
list_destroy(users->list);
|
||||
hub_free(users);
|
||||
return -1;
|
||||
}
|
||||
|
||||
users->timeout = hub_malloc_zero(sizeof(struct timeout_evt));
|
||||
timeout_evt_initialize(users->timeout, timer_statistics, hub);
|
||||
timeout_queue_insert(net_backend_get_timeout_queue(), users->timeout, TIMEOUT_STATS);
|
||||
|
||||
hub->users = users;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int uman_shutdown(struct hub_info* hub)
|
||||
{
|
||||
if (!hub || !hub->users)
|
||||
return -1;
|
||||
|
||||
timeout_queue_remove(net_backend_get_timeout_queue(), hub->users->timeout);
|
||||
hub_free(hub->users->timeout);
|
||||
|
||||
if (hub->users->list)
|
||||
{
|
||||
list_clear(hub->users->list, &clear_user_list_callback);
|
||||
list_destroy(hub->users->list);
|
||||
}
|
||||
sid_pool_destroy(hub->users->sids);
|
||||
hub_free(hub->users);
|
||||
hub->users = 0;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int uman_add(struct hub_info* hub, struct hub_user* user)
|
||||
{
|
||||
if (!hub || !user)
|
||||
return -1;
|
||||
|
||||
if (user->hub)
|
||||
return -1;
|
||||
|
||||
list_append(hub->users->list, user);
|
||||
hub->users->count++;
|
||||
hub->users->count_peak = MAX(hub->users->count, hub->users->count_peak);
|
||||
|
||||
hub->users->shared_size += user->limits.shared_size;
|
||||
hub->users->shared_files += user->limits.shared_files;
|
||||
|
||||
user->hub = hub;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uman_remove(struct hub_info* hub, struct hub_user* user)
|
||||
{
|
||||
if (!hub || !user)
|
||||
return -1;
|
||||
|
||||
list_remove(hub->users->list, user);
|
||||
|
||||
if (hub->users->count > 0)
|
||||
{
|
||||
hub->users->count--;
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(!"negative count!");
|
||||
}
|
||||
|
||||
hub->users->shared_size -= user->limits.shared_size;
|
||||
hub->users->shared_files -= user->limits.shared_files;
|
||||
|
||||
user->hub = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
struct hub_user* uman_get_user_by_sid(struct hub_info* hub, sid_t sid)
|
||||
{
|
||||
return sid_lookup(hub->users->sids, sid);
|
||||
}
|
||||
|
||||
|
||||
struct hub_user* uman_get_user_by_cid(struct hub_info* hub, const char* cid)
|
||||
{
|
||||
struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list); /* iterate users - only on incoming INF msg */
|
||||
while (user)
|
||||
{
|
||||
if (strcmp(user->id.cid, cid) == 0)
|
||||
return user;
|
||||
user = (struct hub_user*) list_get_next(hub->users->list);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
struct hub_user* uman_get_user_by_nick(struct hub_info* hub, const char* nick)
|
||||
{
|
||||
struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list); /* iterate users - only on incoming INF msg */
|
||||
while (user)
|
||||
{
|
||||
if (strcmp(user->id.nick, nick) == 0)
|
||||
return user;
|
||||
user = (struct hub_user*) list_get_next(hub->users->list);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t uman_get_user_by_addr(struct hub_info* hub, struct linked_list* users, struct ip_range* range)
|
||||
{
|
||||
size_t num = 0;
|
||||
struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list); /* iterate users - only on incoming INF msg */
|
||||
while (user)
|
||||
{
|
||||
if (ip_in_range(&user->id.addr, range))
|
||||
{
|
||||
list_append(users, user);
|
||||
num++;
|
||||
}
|
||||
user = (struct hub_user*) list_get_next(hub->users->list);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
int uman_send_user_list(struct hub_info* hub, struct hub_user* target)
|
||||
{
|
||||
int ret = 1;
|
||||
user_flag_set(target, flag_user_list);
|
||||
struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list); /* iterate users - only on INF or PAS msg */
|
||||
while (user)
|
||||
{
|
||||
if (user_is_logged_in(user))
|
||||
{
|
||||
ret = route_to_user(hub, target, user->info);
|
||||
if (!ret)
|
||||
break;
|
||||
}
|
||||
user = (struct hub_user*) list_get_next(hub->users->list);
|
||||
}
|
||||
|
||||
#if 0
|
||||
FIXME: FIXME FIXME handle send queue excess
|
||||
if (!target->send_queue_size)
|
||||
{
|
||||
user_flag_unset(target, flag_user_list);
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void uman_send_quit_message(struct hub_info* hub, struct hub_user* leaving)
|
||||
{
|
||||
struct adc_message* command = adc_msg_construct(ADC_CMD_IQUI, 6);
|
||||
adc_msg_add_argument(command, (const char*) sid_to_string(leaving->id.sid));
|
||||
|
||||
if (leaving->quit_reason == quit_banned || leaving->quit_reason == quit_kicked)
|
||||
{
|
||||
adc_msg_add_argument(command, ADC_QUI_FLAG_DISCONNECT);
|
||||
}
|
||||
route_to_all(hub, command);
|
||||
adc_msg_free(command);
|
||||
}
|
||||
|
||||
|
||||
sid_t uman_get_free_sid(struct hub_info* hub, struct hub_user* user)
|
||||
{
|
||||
sid_t sid = sid_alloc(hub->users->sids, user);
|
||||
user->id.sid = sid;
|
||||
return sid;
|
||||
}
|
||||
|
||||
@@ -20,69 +20,92 @@
|
||||
#ifndef HAVE_UHUB_USER_MANAGER_H
|
||||
#define HAVE_UHUB_USER_MANAGER_H
|
||||
|
||||
struct user_manager
|
||||
struct hub_user_manager
|
||||
{
|
||||
size_t count; /**<< "Number of all fully connected and logged in users" */
|
||||
size_t count_peak; /**<< "Peak number of users" */
|
||||
sid_t free_sid; /**<< "The next available SID." */
|
||||
struct sid_pool* sids;
|
||||
uint64_t shared_size; /**<< "The total number of shared bytes among fully connected users." */
|
||||
uint64_t shared_files; /**<< "The total number of shared files among fully connected users." */
|
||||
struct linked_list* list; /**<< "Contains all users" */
|
||||
struct linked_list* list; /**<< "Contains all logged in users" */
|
||||
struct timeout_evt* timeout; /**<< "Timeout handler for statistics" */
|
||||
};
|
||||
|
||||
/**
|
||||
* Initializes the user manager.
|
||||
* @return 0 on success, or -1 if error (out of memory).
|
||||
*/
|
||||
extern int user_manager_init(struct hub_info* hub);
|
||||
extern int uman_init(struct hub_info* hub);
|
||||
|
||||
/**
|
||||
* Shuts down the user manager.
|
||||
* All users will be disconnected and deleted as part of this.
|
||||
*
|
||||
* @return 0 on success, or -1 in an error occured (hub is invalid).
|
||||
*/
|
||||
extern void user_manager_shutdown(struct hub_info* hub);
|
||||
extern int uman_shutdown(struct hub_info* hub);
|
||||
|
||||
/**
|
||||
* Generate statistics for logfiles.
|
||||
*/
|
||||
extern void user_manager_update_stats(struct hub_info* hub);
|
||||
extern void user_manager_print_stats(struct hub_info* hub);
|
||||
extern void uman_update_stats(struct hub_info* hub);
|
||||
extern void uman_print_stats(struct hub_info* hub);
|
||||
|
||||
/**
|
||||
* Add a new user to the user manager.
|
||||
* Add a user to the user manager.
|
||||
*
|
||||
* @param hub The hub to add the user to
|
||||
* @param user The user to be added to the hub.
|
||||
*/
|
||||
extern void user_manager_add(struct user* user);
|
||||
extern int uman_add(struct hub_info* hub, struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Remove a user from the user manager.
|
||||
* This user is connected, and will be moved to the leaving queue, pending
|
||||
* all messages in the message queue, and resource cleanup.
|
||||
*
|
||||
* @return 0 if successfully removed, -1 if error.
|
||||
*/
|
||||
extern void user_manager_remove(struct user* user);
|
||||
extern int uman_remove(struct hub_info* hub, struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Returns a free sid for a new user.
|
||||
* Returns and allocates an unused session ID (SID).
|
||||
*/
|
||||
extern sid_t user_manager_get_free_sid(struct hub_info* hub);
|
||||
extern sid_t uman_get_free_sid(struct hub_info* hub, struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Lookup a user based on the session ID (sid).
|
||||
* NOTE: This will only search connected users.
|
||||
* Lookup a user based on the session ID (SID).
|
||||
*
|
||||
* NOTE: This function will only search connected users, which means
|
||||
* that SIDs assigned to users who are not yet completely logged in,
|
||||
* or are in the process of being disconnected will result in this
|
||||
* function returning NULL even though the sid is not freely available.
|
||||
*
|
||||
* FIXME: Is that really safe / sensible ?
|
||||
* - Makes sense from a message routing point of view.
|
||||
*
|
||||
* @return a user if found, or NULL if not found
|
||||
*/
|
||||
extern struct user* get_user_by_sid(struct hub_info* hub, sid_t sid);
|
||||
extern struct hub_user* uman_get_user_by_sid(struct hub_info* hub, sid_t sid);
|
||||
|
||||
/**
|
||||
* Lookup a user based on the client ID (cid).
|
||||
* Lookup a user based on the client ID (CID).
|
||||
* @return a user if found, or NULL if not found
|
||||
*/
|
||||
extern struct user* get_user_by_cid(struct hub_info* hub, const char* cid);
|
||||
extern struct hub_user* uman_get_user_by_cid(struct hub_info* hub, const char* cid);
|
||||
|
||||
/**
|
||||
* Lookup a user based on the nick name.
|
||||
* @return a user if found, or NULL if not found
|
||||
*/
|
||||
extern struct user* get_user_by_nick(struct hub_info* hub, const char* nick);
|
||||
extern struct hub_user* uman_get_user_by_nick(struct hub_info* hub, const char* nick);
|
||||
|
||||
/**
|
||||
* Lookup users based on an ip address range.
|
||||
*
|
||||
* @return The number of users matching the addressess, or -1 on error (mask is wrong).
|
||||
*/
|
||||
extern size_t uman_get_user_by_addr(struct hub_info* hub, struct linked_list* users, struct ip_range* range);
|
||||
|
||||
/**
|
||||
* Send the user list of connected clients to 'user'.
|
||||
@@ -90,13 +113,13 @@ extern struct user* get_user_by_nick(struct hub_info* hub, const char* nick);
|
||||
*
|
||||
* @return 1 if sending the user list succeeded, 0 otherwise.
|
||||
*/
|
||||
extern int send_user_list(struct user* user);
|
||||
extern int uman_send_user_list(struct hub_info* hub, struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Send a quit message to all connected users when 'user' is
|
||||
* leaving the hub (for whatever reason).
|
||||
*/
|
||||
extern void send_quit_message(struct user* user);
|
||||
extern void uman_send_quit_message(struct hub_info* hub, struct hub_user* user);
|
||||
|
||||
|
||||
#endif /* HAVE_UHUB_USER_MANAGER_H */
|
||||
992
src/hub.c
992
src/hub.c
@@ -1,992 +0,0 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
int hub_handle_message(struct user* u, const char* line, size_t length)
|
||||
{
|
||||
int ret = 0;
|
||||
struct adc_message* cmd = 0;
|
||||
|
||||
#ifdef NETWORK_DUMP_DEBUG
|
||||
hub_log(log_protocol, "recv %s: %s", sid_to_string(u->id.sid), line);
|
||||
#endif
|
||||
|
||||
if (user_is_disconnecting(u))
|
||||
return -1;
|
||||
|
||||
cmd = adc_msg_parse_verify(u, line, length);
|
||||
if (cmd)
|
||||
{
|
||||
switch (cmd->cmd)
|
||||
{
|
||||
case ADC_CMD_HSUP: ret = hub_handle_support(u, cmd); break;
|
||||
case ADC_CMD_HPAS: ret = hub_handle_password(u, cmd); break;
|
||||
case ADC_CMD_BINF: ret = hub_handle_info(u, cmd); break;
|
||||
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
case ADC_CMD_HCHK: ret = hub_handle_autocheck(u, cmd); break;
|
||||
#endif
|
||||
case ADC_CMD_DINF:
|
||||
case ADC_CMD_EINF:
|
||||
case ADC_CMD_FINF:
|
||||
/* these must never be allowed for security reasons,
|
||||
so we ignore them. */
|
||||
break;
|
||||
|
||||
case ADC_CMD_EMSG:
|
||||
case ADC_CMD_DMSG:
|
||||
case ADC_CMD_BMSG:
|
||||
case ADC_CMD_FMSG:
|
||||
ret = hub_handle_chat_message(u, cmd);
|
||||
break;
|
||||
|
||||
case ADC_CMD_BSCH:
|
||||
case ADC_CMD_DSCH:
|
||||
case ADC_CMD_ESCH:
|
||||
case ADC_CMD_FSCH:
|
||||
case ADC_CMD_DRES:
|
||||
case ADC_CMD_DRCM:
|
||||
case ADC_CMD_DCTM:
|
||||
cmd->priority = -1;
|
||||
if (u->hub->config->chat_only && u->credentials < cred_operator)
|
||||
{
|
||||
/* These below aren't allowed in chat only hubs */
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
if (user_is_logged_in(u))
|
||||
{
|
||||
ret = route_message(u, cmd);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
adc_msg_free(cmd);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!user_is_logged_in(u))
|
||||
{
|
||||
ret = -1;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int hub_handle_support(struct user* u, struct adc_message* cmd)
|
||||
{
|
||||
int ret = 0;
|
||||
int index = 0;
|
||||
int ok = 1;
|
||||
char* arg = adc_msg_get_argument(cmd, index);
|
||||
struct timeval timeout = { TIMEOUT_HANDSHAKE, 0 };
|
||||
|
||||
if (u->hub->status == hub_status_disabled && u->state == state_protocol)
|
||||
{
|
||||
on_login_failure(u, status_msg_hub_disabled);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (arg)
|
||||
{
|
||||
|
||||
if (strlen(arg) == 6)
|
||||
{
|
||||
fourcc_t fourcc = FOURCC(arg[2], arg[3], arg[4], arg[5]);
|
||||
if (strncmp(arg, ADC_SUP_FLAG_ADD, 2) == 0)
|
||||
{
|
||||
user_support_add(u, fourcc);
|
||||
}
|
||||
else if (strncmp(arg, ADC_SUP_FLAG_REMOVE, 2) == 0)
|
||||
{
|
||||
user_support_remove(u, fourcc);
|
||||
}
|
||||
else
|
||||
{
|
||||
ok = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ok = 0;
|
||||
}
|
||||
|
||||
index++;
|
||||
hub_free(arg);
|
||||
arg = adc_msg_get_argument(cmd, index);
|
||||
}
|
||||
|
||||
if (u->state == state_protocol)
|
||||
{
|
||||
if (index == 0) ok = 0; /* Need to support *SOMETHING*, at least BASE */
|
||||
|
||||
if (ok)
|
||||
{
|
||||
hub_send_handshake(u);
|
||||
if (u->ev_read)
|
||||
event_add(u->ev_read, &timeout);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* disconnect user. Do not send crap during initial handshake! */
|
||||
user_disconnect(u, quit_logon_error);
|
||||
ret = -1;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int hub_handle_password(struct user* u, struct adc_message* cmd)
|
||||
{
|
||||
char* password = adc_msg_get_argument(cmd, 0);
|
||||
int ret = 0;
|
||||
|
||||
if (u->state == state_verify)
|
||||
{
|
||||
if (password_verify(u, password))
|
||||
{
|
||||
on_login_success(u);
|
||||
}
|
||||
else
|
||||
{
|
||||
on_login_failure(u, status_msg_auth_invalid_password);
|
||||
ret = -1;
|
||||
}
|
||||
}
|
||||
|
||||
hub_free(password);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int hub_handle_chat_message(struct user* u, struct adc_message* cmd)
|
||||
{
|
||||
char* message = adc_msg_get_argument(cmd, 0);
|
||||
int ret = 0;
|
||||
int relay = 1;
|
||||
|
||||
/* TODO: Check for hub-commands here. Set relay to 0 and the message will not be sent to other users. */
|
||||
if (message[0] == '!' || message[0] == '+')
|
||||
{
|
||||
relay = command_dipatcher(u, message);
|
||||
}
|
||||
|
||||
if (relay && user_is_logged_in(u))
|
||||
{
|
||||
/* adc_msg_remove_named_argument(cmd, "PM"); */
|
||||
ret = route_message(u, cmd);
|
||||
}
|
||||
|
||||
free(message);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int on_kick(struct user* u, struct adc_message* cmd)
|
||||
{
|
||||
hub_log(log_error, "on_kick() not implemented");
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
int hub_handle_autocheck(struct user* u, struct adc_message* cmd)
|
||||
{
|
||||
char* port_str = adc_msg_get_argument(cmd, 0);
|
||||
char* token = adc_msg_get_argument(cmd, 1);
|
||||
int port = 0;
|
||||
|
||||
if (!port_str || !token || strlen(token) != 4)
|
||||
{
|
||||
hub_free(port_str);
|
||||
hub_free(token);
|
||||
return -1;
|
||||
}
|
||||
|
||||
port = uhub_atoi(port_str);
|
||||
|
||||
if (port == 0 || port > 65535)
|
||||
{
|
||||
hub_free(port_str);
|
||||
hub_free(token);
|
||||
return -1;
|
||||
}
|
||||
|
||||
hub_send_autocheck(u, port, token);
|
||||
|
||||
hub_free(port_str);
|
||||
hub_free(token);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
void hub_send_autocheck(struct user* u, uint16_t port, const char* token)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void hub_send_support(struct user* u)
|
||||
{
|
||||
if (user_is_connecting(u) || user_is_logged_in(u))
|
||||
{
|
||||
route_to_user(u, u->hub->command_support);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void hub_send_sid(struct user* u)
|
||||
{
|
||||
struct adc_message* command;
|
||||
if (user_is_connecting(u))
|
||||
{
|
||||
command = adc_msg_construct(ADC_CMD_ISID, 10);
|
||||
u->id.sid = user_manager_get_free_sid(u->hub);
|
||||
adc_msg_add_argument(command, (const char*) sid_to_string(u->id.sid));
|
||||
route_to_user(u, command);
|
||||
adc_msg_free(command);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void hub_send_ping(struct user* user)
|
||||
{
|
||||
/* This will just send a newline, despite appearing to do more below. */
|
||||
struct adc_message* ping = adc_msg_construct(0, 0);
|
||||
ping->cache[0] = '\n';
|
||||
ping->cache[1] = 0;
|
||||
ping->length = 1;
|
||||
ping->priority = 1;
|
||||
route_to_user(user, ping);
|
||||
adc_msg_free(ping);
|
||||
}
|
||||
|
||||
|
||||
void hub_send_hubinfo(struct user* u)
|
||||
{
|
||||
struct adc_message* info = adc_msg_copy(u->hub->command_info);
|
||||
int value = 0;
|
||||
|
||||
if (user_flag_get(u, feature_ping))
|
||||
{
|
||||
/*
|
||||
FIXME: These are missing:
|
||||
HH - Hub Host address ( DNS or IP )
|
||||
WS - Hub Website
|
||||
NE - Hub Network
|
||||
OW - Hub Owner name
|
||||
*/
|
||||
adc_msg_add_named_argument(info, "UC", uhub_itoa(hub_get_user_count(u->hub)));
|
||||
adc_msg_add_named_argument(info, "MC", uhub_itoa(hub_get_max_user_count(u->hub)));
|
||||
adc_msg_add_named_argument(info, "SS", uhub_ulltoa(hub_get_shared_size(u->hub)));
|
||||
adc_msg_add_named_argument(info, "SF", uhub_itoa(hub_get_shared_files(u->hub)));
|
||||
|
||||
/* Maximum/minimum share size */
|
||||
value = hub_get_max_share(u->hub);
|
||||
if (value) adc_msg_add_named_argument(info, "XS", uhub_itoa(value));
|
||||
value = hub_get_min_share(u->hub);
|
||||
if (value) adc_msg_add_named_argument(info, "MS", uhub_itoa(value));
|
||||
|
||||
/* Maximum/minimum upload slots allowed per user */
|
||||
value = hub_get_max_slots(u->hub);
|
||||
if (value) adc_msg_add_named_argument(info, "XL", uhub_itoa(value));
|
||||
value = hub_get_min_slots(u->hub);
|
||||
if (value) adc_msg_add_named_argument(info, "ML", uhub_itoa(value));
|
||||
|
||||
/* guest users must be on min/max hubs */
|
||||
value = hub_get_max_hubs_user(u->hub);
|
||||
if (value) adc_msg_add_named_argument(info, "XU", uhub_itoa(value));
|
||||
value = hub_get_min_hubs_user(u->hub);
|
||||
if (value) adc_msg_add_named_argument(info, "MU", uhub_itoa(value));
|
||||
|
||||
/* registered users must be on min/max hubs */
|
||||
value = hub_get_max_hubs_reg(u->hub);
|
||||
if (value) adc_msg_add_named_argument(info, "XR", uhub_itoa(value));
|
||||
value = hub_get_min_hubs_reg(u->hub);
|
||||
if (value) adc_msg_add_named_argument(info, "MR", uhub_itoa(value));
|
||||
|
||||
/* operators must be on min/max hubs */
|
||||
value = hub_get_max_hubs_op(u->hub);
|
||||
if (value) adc_msg_add_named_argument(info, "XO", uhub_itoa(value));
|
||||
value = hub_get_min_hubs_op(u->hub);
|
||||
if (value) adc_msg_add_named_argument(info, "MO", uhub_itoa(value));
|
||||
|
||||
/* uptime in seconds */
|
||||
adc_msg_add_named_argument(info, "UP", uhub_itoa((int) difftime(time(0), u->hub->tm_started)));
|
||||
}
|
||||
|
||||
if (user_is_connecting(u) || user_is_logged_in(u))
|
||||
{
|
||||
route_to_user(u, info);
|
||||
}
|
||||
adc_msg_free(info);
|
||||
|
||||
/* Only send banner when connecting */
|
||||
if (u->hub->config->show_banner && user_is_connecting(u))
|
||||
{
|
||||
route_to_user(u, u->hub->command_banner);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void hub_send_handshake(struct user* u)
|
||||
{
|
||||
hub_send_support(u);
|
||||
hub_send_sid(u);
|
||||
hub_send_hubinfo(u);
|
||||
|
||||
if (!user_is_disconnecting(u))
|
||||
{
|
||||
user_set_state(u, state_identify);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void hub_send_motd(struct user* u)
|
||||
{
|
||||
if (u->hub->command_motd)
|
||||
{
|
||||
route_to_user(u, u->hub->command_motd);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void hub_send_password_challenge(struct user* u)
|
||||
{
|
||||
struct adc_message* igpa;
|
||||
igpa = adc_msg_construct(ADC_CMD_IGPA, 38);
|
||||
adc_msg_add_argument(igpa, password_generate_challenge(u));
|
||||
user_set_state(u, state_verify);
|
||||
route_to_user(u, igpa);
|
||||
adc_msg_free(igpa);
|
||||
}
|
||||
|
||||
static void hub_event_dispatcher(void* callback_data, struct event_data* message)
|
||||
{
|
||||
/*
|
||||
struct hub_info* hub = (struct hub_info*) callback_data;
|
||||
hub_log(log_trace, "hub_event_dispatcher: %x (ptr=%p)", message->id, message->ptr);
|
||||
*/
|
||||
|
||||
switch (message->id)
|
||||
{
|
||||
case UHUB_EVENT_USER_JOIN:
|
||||
{
|
||||
if (user_is_disconnecting((struct user*) message->ptr))
|
||||
break;
|
||||
|
||||
if (message->flags)
|
||||
{
|
||||
hub_send_password_challenge((struct user*) message->ptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
on_login_success((struct user*) message->ptr);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case UHUB_EVENT_USER_QUIT:
|
||||
{
|
||||
user_manager_remove((struct user*) message->ptr);
|
||||
send_quit_message((struct user*) message->ptr);
|
||||
on_logout_user((struct user*) message->ptr);
|
||||
user_schedule_destroy((struct user*) message->ptr);
|
||||
break;
|
||||
}
|
||||
|
||||
case UHUB_EVENT_USER_DESTROY:
|
||||
{
|
||||
hub_log(log_trace, "hub_event_dispatcher: UHUB_EVENT_USER_DESTROY (ptr=%p)", message->ptr);
|
||||
user_destroy((struct user*) message->ptr);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
/* FIXME: ignored */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct hub_info* hub_start_service(struct hub_config* config)
|
||||
{
|
||||
struct hub_info* hub = 0;
|
||||
int server_tcp, ret, ipv6_supported, af;
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
int server_udp;
|
||||
#endif
|
||||
struct sockaddr_storage addr;
|
||||
socklen_t sockaddr_size;
|
||||
char address_buf[INET6_ADDRSTRLEN+1];
|
||||
|
||||
hub = hub_malloc_zero(sizeof(struct hub_info));
|
||||
if (!hub)
|
||||
{
|
||||
hub_log(log_fatal, "Unable to allocate memory for hub");
|
||||
return 0;
|
||||
}
|
||||
|
||||
hub->tm_started = time(0);
|
||||
|
||||
ipv6_supported = net_is_ipv6_supported();
|
||||
|
||||
if (ipv6_supported)
|
||||
hub_log(log_debug, "IPv6 supported.");
|
||||
else
|
||||
hub_log(log_debug, "IPv6 not supported.");
|
||||
|
||||
if (ip_convert_address(config->server_bind_addr, config->server_port, (struct sockaddr*) &addr, &sockaddr_size) == -1)
|
||||
{
|
||||
hub_free(hub);
|
||||
return 0;
|
||||
}
|
||||
|
||||
af = addr.ss_family;
|
||||
if (af == AF_INET)
|
||||
{
|
||||
net_address_to_string(AF_INET, &((struct sockaddr_in*) &addr)->sin_addr, address_buf, INET6_ADDRSTRLEN);
|
||||
}
|
||||
else if (af == AF_INET6)
|
||||
{
|
||||
net_address_to_string(AF_INET6, &((struct sockaddr_in6*) &addr)->sin6_addr, address_buf, INET6_ADDRSTRLEN);
|
||||
}
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
server_udp = net_socket_create(af, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (server_udp == -1)
|
||||
{
|
||||
event_base_free(hub->evbase);
|
||||
hub_free(hub);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
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
|
||||
net_close(server_udp);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
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
|
||||
net_close(server_udp);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
ret = net_bind(server_tcp, (struct sockaddr*) &addr, sockaddr_size);
|
||||
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
|
||||
net_close(server_udp);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
ret = net_bind(server_udp, (struct sockaddr*) &addr, sockaddr_size);
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
ret = net_listen(server_tcp, SERVER_BACKLOG);
|
||||
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
|
||||
net_close(server_udp);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
hub->fd_tcp = server_tcp;
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
hub->fd_udp = server_udp;
|
||||
#endif
|
||||
hub->config = config;
|
||||
hub->users = NULL;
|
||||
|
||||
if (user_manager_init(hub) == -1)
|
||||
{
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
net_close(server_udp);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
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);
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
net_close(server_udp);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
#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);
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
net_close(server_udp);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (event_queue_initialize(&hub->queue, hub_event_dispatcher, (void*) hub) == -1)
|
||||
{
|
||||
user_manager_shutdown(hub);
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
net_close(server_udp);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
hub->status = hub_status_running;
|
||||
|
||||
return hub;
|
||||
}
|
||||
|
||||
|
||||
void hub_shutdown_service(struct hub_info* hub)
|
||||
{
|
||||
hub_log(log_trace, "hub_shutdown_service()");
|
||||
|
||||
event_queue_shutdown(hub->queue);
|
||||
event_del(&hub->ev_accept);
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
event_del(&hub->ev_datagram);
|
||||
net_close(hub->fd_udp);
|
||||
#endif
|
||||
net_close(hub->fd_tcp);
|
||||
user_manager_shutdown(hub);
|
||||
hub->status = hub_status_stopped;
|
||||
event_base_free(hub->evbase);
|
||||
hub_free(hub);
|
||||
hub = 0;
|
||||
}
|
||||
|
||||
#define SERVER "" PRODUCT "/" VERSION ""
|
||||
|
||||
void hub_set_variables(struct hub_info* hub, struct acl_handle* acl)
|
||||
{
|
||||
int fd, ret;
|
||||
char buf[MAX_RECV_BUF];
|
||||
char* tmp;
|
||||
|
||||
|
||||
hub->acl = acl;
|
||||
hub->command_info = adc_msg_construct(ADC_CMD_IINF, 15 + strlen(SERVER));
|
||||
if (hub->command_info)
|
||||
{
|
||||
adc_msg_add_named_argument(hub->command_info, ADC_INF_FLAG_CLIENT_TYPE, ADC_CLIENT_TYPE_HUB);
|
||||
adc_msg_add_named_argument(hub->command_info, ADC_INF_FLAG_USER_AGENT, SERVER);
|
||||
|
||||
tmp = adc_msg_escape(hub->config->hub_name);
|
||||
adc_msg_add_named_argument(hub->command_info, ADC_INF_FLAG_NICK, tmp);
|
||||
hub_free(tmp);
|
||||
|
||||
tmp = adc_msg_escape(hub->config->hub_description);
|
||||
adc_msg_add_named_argument(hub->command_info, ADC_INF_FLAG_DESCRIPTION, tmp);
|
||||
hub_free(tmp);
|
||||
}
|
||||
|
||||
/* (Re-)read the message of the day */
|
||||
hub->command_motd = 0;
|
||||
fd = open(hub->config->file_motd, 0);
|
||||
if (fd != -1)
|
||||
{
|
||||
ret = read(fd, buf, MAX_RECV_BUF);
|
||||
if (ret > 0)
|
||||
{
|
||||
buf[ret] = 0;
|
||||
tmp = adc_msg_escape(buf);
|
||||
hub->command_motd = adc_msg_construct(ADC_CMD_IMSG, 6 + strlen(tmp));
|
||||
adc_msg_add_argument(hub->command_motd, tmp);
|
||||
hub_free(tmp);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
hub->command_support = adc_msg_construct(ADC_CMD_ISUP, 6 + strlen(ADC_PROTO_SUPPORT));
|
||||
if (hub->command_support)
|
||||
{
|
||||
adc_msg_add_argument(hub->command_support, ADC_PROTO_SUPPORT);
|
||||
}
|
||||
|
||||
hub->command_banner = adc_msg_construct(ADC_CMD_ISTA, 25 + strlen(SERVER));
|
||||
if (hub->command_banner)
|
||||
{
|
||||
adc_msg_add_argument(hub->command_banner, "000 Powered\\sby\\s" SERVER);
|
||||
}
|
||||
|
||||
hub->status = (hub->config->hub_enabled ? hub_status_running : hub_status_disabled);
|
||||
}
|
||||
|
||||
|
||||
void hub_free_variables(struct hub_info* hub)
|
||||
{
|
||||
adc_msg_free(hub->command_info);
|
||||
adc_msg_free(hub->command_banner);
|
||||
|
||||
if (hub->command_motd)
|
||||
adc_msg_free(hub->command_motd);
|
||||
|
||||
adc_msg_free(hub->command_support);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return 1 if nickname is in use, or 0 if not used.
|
||||
*/
|
||||
static inline int is_nick_in_use(struct hub_info* hub, const char* nick)
|
||||
{
|
||||
struct user* lookup = get_user_by_nick(hub, nick);
|
||||
if (lookup)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return 1 if CID is in use, or 0 if not used.
|
||||
*/
|
||||
static inline int is_cid_in_use(struct hub_info* hub, const char* cid)
|
||||
{
|
||||
struct user* lookup = get_user_by_cid(hub, cid);
|
||||
if (lookup)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static void set_status_code(enum msg_status_level level, int code, char buffer[4])
|
||||
{
|
||||
buffer[0] = ('0' + (int) level);
|
||||
buffer[1] = ('0' + (code / 10));
|
||||
buffer[2] = ('0' + (code % 10));
|
||||
buffer[3] = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param hub The hub instance this message is sent from.
|
||||
* @param user The user this message is sent to.
|
||||
* @param msg See enum status_message
|
||||
* @param level See enum status_level
|
||||
*/
|
||||
void hub_send_status(struct user* user, enum status_message msg, enum msg_status_level level)
|
||||
{
|
||||
struct hub_config* cfg = user->hub->config;
|
||||
struct adc_message* cmd = adc_msg_construct(ADC_CMD_ISTA, 6);
|
||||
if (!cmd) return;
|
||||
char code[4];
|
||||
const char* text = 0;
|
||||
const char* flag = 0;
|
||||
char* escaped_text = 0;
|
||||
|
||||
#define STATUS(CODE, MSG, FLAG) case status_ ## MSG : set_status_code(level, CODE, code); text = cfg->MSG; flag = FLAG; break
|
||||
switch (msg)
|
||||
{
|
||||
STATUS(11, msg_hub_full, 0);
|
||||
STATUS(12, msg_hub_disabled, 0);
|
||||
STATUS(26, msg_hub_registered_users_only, 0);
|
||||
STATUS(43, msg_inf_error_nick_missing, 0);
|
||||
STATUS(43, msg_inf_error_nick_multiple, 0);
|
||||
STATUS(21, msg_inf_error_nick_invalid, 0);
|
||||
STATUS(21, msg_inf_error_nick_long, 0);
|
||||
STATUS(21, msg_inf_error_nick_short, 0);
|
||||
STATUS(21, msg_inf_error_nick_spaces, 0);
|
||||
STATUS(21, msg_inf_error_nick_bad_chars, 0);
|
||||
STATUS(21, msg_inf_error_nick_not_utf8, 0);
|
||||
STATUS(22, msg_inf_error_nick_taken, 0);
|
||||
STATUS(21, msg_inf_error_nick_restricted, 0);
|
||||
STATUS(43, msg_inf_error_cid_invalid, "FBID");
|
||||
STATUS(43, msg_inf_error_cid_missing, "FMID");
|
||||
STATUS(24, msg_inf_error_cid_taken, 0);
|
||||
STATUS(43, msg_inf_error_pid_missing, "FMPD");
|
||||
STATUS(27, msg_inf_error_pid_invalid, "FBPD");
|
||||
STATUS(31, msg_ban_permanently, 0);
|
||||
STATUS(32, msg_ban_temporarily, "TL600"); /* FIXME: Use a proper timeout */
|
||||
STATUS(23, msg_auth_invalid_password, 0);
|
||||
STATUS(20, msg_auth_user_not_found, 0);
|
||||
STATUS(30, msg_error_no_memory, 0);
|
||||
STATUS(43, msg_user_share_size_low, "FB" ADC_INF_FLAG_SHARED_SIZE);
|
||||
STATUS(43, msg_user_share_size_high, "FB" ADC_INF_FLAG_SHARED_SIZE);
|
||||
STATUS(43, msg_user_slots_low, "FB" ADC_INF_FLAG_UPLOAD_SLOTS);
|
||||
STATUS(43, msg_user_slots_high, "FB" ADC_INF_FLAG_UPLOAD_SLOTS);
|
||||
STATUS(43, msg_user_hub_limit_low, 0);
|
||||
STATUS(43, msg_user_hub_limit_high, 0);
|
||||
}
|
||||
#undef STATUS
|
||||
|
||||
escaped_text = adc_msg_escape(text);
|
||||
|
||||
adc_msg_add_argument(cmd, code);
|
||||
adc_msg_add_argument(cmd, escaped_text);
|
||||
|
||||
hub_free(escaped_text);
|
||||
|
||||
if (flag)
|
||||
{
|
||||
adc_msg_add_argument(cmd, flag);
|
||||
}
|
||||
|
||||
route_to_user(user, cmd);
|
||||
adc_msg_free(cmd);
|
||||
|
||||
}
|
||||
|
||||
const char* hub_get_status_message(struct hub_info* hub, enum status_message msg)
|
||||
{
|
||||
#define STATUS(MSG) case status_ ## MSG : return cfg->MSG; break
|
||||
struct hub_config* cfg = hub->config;
|
||||
switch (msg)
|
||||
{
|
||||
STATUS(msg_hub_full);
|
||||
STATUS(msg_hub_disabled);
|
||||
STATUS(msg_hub_registered_users_only);
|
||||
STATUS(msg_inf_error_nick_missing);
|
||||
STATUS(msg_inf_error_nick_multiple);
|
||||
STATUS(msg_inf_error_nick_invalid);
|
||||
STATUS(msg_inf_error_nick_long);
|
||||
STATUS(msg_inf_error_nick_short);
|
||||
STATUS(msg_inf_error_nick_spaces);
|
||||
STATUS(msg_inf_error_nick_bad_chars);
|
||||
STATUS(msg_inf_error_nick_not_utf8);
|
||||
STATUS(msg_inf_error_nick_taken);
|
||||
STATUS(msg_inf_error_nick_restricted);
|
||||
STATUS(msg_inf_error_cid_invalid);
|
||||
STATUS(msg_inf_error_cid_missing);
|
||||
STATUS(msg_inf_error_cid_taken);
|
||||
STATUS(msg_inf_error_pid_missing);
|
||||
STATUS(msg_inf_error_pid_invalid);
|
||||
STATUS(msg_ban_permanently);
|
||||
STATUS(msg_ban_temporarily);
|
||||
STATUS(msg_auth_invalid_password);
|
||||
STATUS(msg_auth_user_not_found);
|
||||
STATUS(msg_error_no_memory);
|
||||
STATUS(msg_user_share_size_low);
|
||||
STATUS(msg_user_share_size_high);
|
||||
STATUS(msg_user_slots_low);
|
||||
STATUS(msg_user_slots_high);
|
||||
STATUS(msg_user_hub_limit_low);
|
||||
STATUS(msg_user_hub_limit_high);
|
||||
}
|
||||
#undef STATUS
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
size_t hub_get_user_count(struct hub_info* hub)
|
||||
{
|
||||
return hub->users->count;
|
||||
}
|
||||
|
||||
size_t hub_get_max_user_count(struct hub_info* hub)
|
||||
{
|
||||
return hub->config->max_users;
|
||||
}
|
||||
|
||||
uint64_t hub_get_shared_size(struct hub_info* hub)
|
||||
{
|
||||
return hub->users->shared_size;
|
||||
}
|
||||
|
||||
uint64_t hub_get_shared_files(struct hub_info* hub)
|
||||
{
|
||||
return hub->users->shared_files;
|
||||
}
|
||||
|
||||
uint64_t hub_get_min_share(struct hub_info* hub)
|
||||
{
|
||||
return 1024 * 1024 * hub->config->limit_min_share;
|
||||
}
|
||||
|
||||
uint64_t hub_get_max_share(struct hub_info* hub)
|
||||
{
|
||||
return 1024 * 1024 * hub->config->limit_max_share;
|
||||
}
|
||||
|
||||
size_t hub_get_min_slots(struct hub_info* hub)
|
||||
{
|
||||
return hub->config->limit_min_slots;
|
||||
}
|
||||
|
||||
size_t hub_get_max_slots(struct hub_info* hub)
|
||||
{
|
||||
return hub->config->limit_max_slots;
|
||||
}
|
||||
|
||||
size_t hub_get_max_hubs_total(struct hub_info* hub)
|
||||
{
|
||||
return hub->config->limit_max_hubs;
|
||||
}
|
||||
|
||||
size_t hub_get_max_hubs_user(struct hub_info* hub)
|
||||
{
|
||||
return hub->config->limit_max_hubs_user;
|
||||
}
|
||||
|
||||
size_t hub_get_min_hubs_user(struct hub_info* hub)
|
||||
{
|
||||
return hub->config->limit_min_hubs_user;
|
||||
}
|
||||
|
||||
size_t hub_get_max_hubs_reg(struct hub_info* hub)
|
||||
{
|
||||
return hub->config->limit_max_hubs_reg;
|
||||
}
|
||||
|
||||
size_t hub_get_min_hubs_reg(struct hub_info* hub)
|
||||
{
|
||||
return hub->config->limit_min_hubs_reg;
|
||||
}
|
||||
|
||||
size_t hub_get_max_hubs_op(struct hub_info* hub)
|
||||
{
|
||||
return hub->config->limit_max_hubs_op;
|
||||
}
|
||||
|
||||
size_t hub_get_min_hubs_op(struct hub_info* hub)
|
||||
{
|
||||
return hub->config->limit_min_hubs_op;
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
122
src/hubevent.c
122
src/hubevent.c
@@ -1,122 +0,0 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#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)
|
||||
{
|
||||
struct timeval timeout = { TIMEOUT_IDLE, 0 };
|
||||
|
||||
/* Send user list of all existing users */
|
||||
if (!send_user_list(u))
|
||||
return;
|
||||
|
||||
/* Mark as being in the normal state, and add user to the user list */
|
||||
user_set_state(u, state_normal);
|
||||
user_manager_add(u);
|
||||
|
||||
/* Print log message */
|
||||
log_user_login(u);
|
||||
|
||||
/* Announce new user to all connected users */
|
||||
if (user_is_logged_in(u))
|
||||
route_info_message(u);
|
||||
|
||||
/* Send message of the day (if any) */
|
||||
if (user_is_logged_in(u)) /* Previous send() can fail! */
|
||||
hub_send_motd(u);
|
||||
|
||||
/* reset to idle timeout */
|
||||
if (u->ev_read)
|
||||
event_add(u->ev_read, &timeout);
|
||||
}
|
||||
|
||||
void on_login_failure(struct user* u, enum status_message msg)
|
||||
{
|
||||
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))
|
||||
{
|
||||
log_user_nick_change(u, nick);
|
||||
}
|
||||
}
|
||||
|
||||
void on_logout_user(struct user* user)
|
||||
{
|
||||
const char* reason = "";
|
||||
|
||||
/* These are used for logging purposes */
|
||||
switch (user->quit_reason)
|
||||
{
|
||||
case quit_disconnected: reason = "disconnected"; break;
|
||||
case quit_kicked: reason = "kicked"; break;
|
||||
case quit_banned: reason = "banned"; break;
|
||||
case quit_timeout: reason = "timeout"; break;
|
||||
case quit_send_queue: reason = "send queue"; break;
|
||||
case quit_memory_error: reason = "out of memory"; break;
|
||||
case quit_socket_error: reason = "socket error"; break;
|
||||
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";
|
||||
else
|
||||
reason = "unknown error";
|
||||
break;
|
||||
}
|
||||
|
||||
log_user_logout(user, reason);
|
||||
user->quit_reason = 0;
|
||||
}
|
||||
|
||||
332
src/netevent.c
332
src/netevent.c
@@ -1,332 +0,0 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
|
||||
void net_on_read(int fd, short ev, void *arg)
|
||||
{
|
||||
static char buf[MAX_RECV_BUF];
|
||||
struct user* user = (struct user*) arg;
|
||||
char* pos;
|
||||
size_t offset;
|
||||
size_t buflen;
|
||||
ssize_t size;
|
||||
int more = 1;
|
||||
int flag_close = 0;
|
||||
|
||||
hub_log(log_trace, "net_on_read() : fd=%d, ev=%d, arg=%p", fd, (int) ev, arg);
|
||||
|
||||
if (ev == EV_TIMEOUT)
|
||||
{
|
||||
more = 0;
|
||||
if (user_is_connecting(user))
|
||||
{
|
||||
flag_close = quit_timeout;
|
||||
}
|
||||
else
|
||||
{
|
||||
hub_send_ping(user);
|
||||
}
|
||||
}
|
||||
|
||||
while (more)
|
||||
{
|
||||
offset = 0;
|
||||
if (user->recv_buf)
|
||||
{
|
||||
memcpy(buf, user->recv_buf, user->recv_buf_offset);
|
||||
offset = user->recv_buf_offset;
|
||||
}
|
||||
|
||||
size = net_recv(fd, &buf[offset], MAX_RECV_BUF - offset, 0);
|
||||
if (size == -1)
|
||||
{
|
||||
if (net_error() != EWOULDBLOCK)
|
||||
flag_close = quit_socket_error;
|
||||
break;
|
||||
}
|
||||
else if (size == 0)
|
||||
{
|
||||
flag_close = quit_disconnected;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
buflen = offset + size;
|
||||
ssize_t handled = 0;
|
||||
|
||||
while ((pos = memchr(&buf[handled], '\n', (buflen - handled))))
|
||||
{
|
||||
pos[0] = '\0';
|
||||
size_t msglen = &pos[0] - &buf[handled];
|
||||
|
||||
if (user_flag_get(user, flag_maxbuf))
|
||||
{
|
||||
user_flag_unset(user, flag_maxbuf);
|
||||
}
|
||||
else
|
||||
{
|
||||
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
|
||||
{
|
||||
if (user->recv_buf)
|
||||
{
|
||||
hub_free(user->recv_buf);
|
||||
user->recv_buf = 0;
|
||||
user->recv_buf_offset = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (flag_close)
|
||||
{
|
||||
user_disconnect(user, flag_close);
|
||||
return;
|
||||
}
|
||||
|
||||
if (user_is_logged_in(user))
|
||||
{
|
||||
if (user->ev_read)
|
||||
{
|
||||
struct timeval timeout = { TIMEOUT_IDLE, 0 };
|
||||
event_add(user->ev_read, &timeout);
|
||||
}
|
||||
}
|
||||
else if (user_is_connecting(user))
|
||||
{
|
||||
if (user->ev_read)
|
||||
{
|
||||
struct timeval timeout = { TIMEOUT_HANDSHAKE, 0 };
|
||||
event_add(user->ev_read, &timeout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void net_on_write(int fd, short ev, void *arg)
|
||||
{
|
||||
struct user* user = (struct user*) arg;
|
||||
struct adc_message* msg;
|
||||
int ret;
|
||||
int length;
|
||||
int close_flag = 0;
|
||||
|
||||
msg = list_get_first(user->send_queue);
|
||||
while (msg)
|
||||
{
|
||||
length = msg->length - user->send_queue_offset;
|
||||
ret = net_send(user->sd, &msg->cache[user->send_queue_offset], length, UHUB_SEND_SIGNAL);
|
||||
|
||||
if (ret == 0 || (ret == -1 && net_error() == EWOULDBLOCK))
|
||||
{
|
||||
close_flag = 0;
|
||||
break;
|
||||
}
|
||||
else if (ret > 0)
|
||||
{
|
||||
|
||||
user->tm_last_write = time(NULL);
|
||||
|
||||
if (ret == length)
|
||||
{
|
||||
#ifdef DEBUG_SENDQ
|
||||
hub_log(log_error, "SENDQ: sent=%d bytes/%d (all), send_queue_size=%d, offset=%d", ret, (int) msg->length, user->send_queue_size, user->send_queue_offset);
|
||||
#endif
|
||||
user->send_queue_size -= ret;
|
||||
user->send_queue_offset = 0;
|
||||
|
||||
#ifdef DEBUG_SENDQ
|
||||
if ((user->send_queue_size < 0) || (user->send_queue_offset < 0))
|
||||
{
|
||||
hub_log(log_error, "INVALID: send_queue_size=%d, send_queue_offset=%d", user->send_queue_size, user->send_queue_offset);
|
||||
}
|
||||
#endif
|
||||
|
||||
list_remove(user->send_queue, msg);
|
||||
|
||||
if (user_flag_get(user, flag_user_list) && (msg == user->info || user->send_queue_size == 0))
|
||||
{
|
||||
user_flag_unset(user, flag_user_list);
|
||||
}
|
||||
|
||||
adc_msg_free(msg);
|
||||
msg = 0;
|
||||
|
||||
if (user->send_queue_size == 0)
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef DEBUG_SENDQ
|
||||
hub_log(log_error, "SENDQ: sent=%d bytes/%d (part), send_queue_size=%d, offset=%d", ret, (int) msg->length, user->send_queue_size, user->send_queue_offset);
|
||||
#endif
|
||||
user->send_queue_size -= ret;
|
||||
user->send_queue_offset += ret;
|
||||
|
||||
#ifdef DEBUG_SENDQ
|
||||
if ((user->send_queue_size < 0) || (user->send_queue_offset < 0) || (user->send_queue_offset > msg->length))
|
||||
{
|
||||
hub_log(log_error, "INVALID: send_queue_size=%d, send_queue_offset=%d", user->send_queue_size, user->send_queue_offset);
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
close_flag = quit_socket_error;
|
||||
break;
|
||||
}
|
||||
msg = list_get_first(user->send_queue);
|
||||
}
|
||||
|
||||
|
||||
if (close_flag)
|
||||
{
|
||||
user_disconnect(user, close_flag);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (user->send_queue_size > 0 && user->ev_write)
|
||||
event_add(user->ev_write, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void net_on_accept(int server_fd, short ev, void *arg)
|
||||
{
|
||||
struct hub_info* hub = (struct hub_info*) arg;
|
||||
struct user* user = 0;
|
||||
const char* addr;
|
||||
struct timeval timeout = { TIMEOUT_CONNECTED, 0 };
|
||||
|
||||
for (;;)
|
||||
{
|
||||
int fd = net_accept(server_fd);
|
||||
if (fd == -1)
|
||||
{
|
||||
if (net_error() == EWOULDBLOCK)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
hub_log(log_error, "Accept error: %d %s", net_error(), strerror(net_error()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
addr = net_get_peer_address(fd);
|
||||
|
||||
/* FIXME: Should have a plugin log this */
|
||||
hub_log(log_trace, "Got connection from %s", addr);
|
||||
|
||||
/* FIXME: A plugin should perform this check: is IP banned? */
|
||||
if (acl_is_ip_banned(hub->acl, addr))
|
||||
{
|
||||
hub_log(log_info, "Denied [%s] (IP banned)", addr);
|
||||
net_close(fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
user = user_create(hub, fd);
|
||||
if (!user)
|
||||
{
|
||||
hub_log(log_error, "Unable to create user after socket accepted. Out of memory?");
|
||||
net_close(fd);
|
||||
break;
|
||||
}
|
||||
|
||||
/* Store IP address in user object */
|
||||
ip_convert_to_binary(addr, &user->ipaddr);
|
||||
|
||||
net_set_nonblocking(fd, 1);
|
||||
net_set_nosigpipe(fd, 1);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ADC_UDP_OPERATION
|
||||
extern void net_on_packet(int fd, short ev, void *arg)
|
||||
{
|
||||
static char buffer[1024] = {0,};
|
||||
// struct hub_info* hub = (struct hub_info*) arg;
|
||||
// struct user* user = 0;
|
||||
ssize_t size;
|
||||
struct sockaddr_storage from;
|
||||
socklen_t fromlen;
|
||||
|
||||
size = recvfrom(fd, buffer, 1024, 0, (struct sockaddr*) &from, &fromlen);
|
||||
|
||||
// FIXME: A plugin should handle this!
|
||||
hub_log(log_info, "Datagram [%s] (%d bytes)", buffer, (int) size);
|
||||
}
|
||||
#endif
|
||||
@@ -1,326 +0,0 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
#ifdef HAVE_EPOLL
|
||||
|
||||
// #define DEBUG_EPOLL
|
||||
static struct epoll_event* events = 0;
|
||||
static int epfd = -1;
|
||||
|
||||
#ifdef DEBUG_EPOLL
|
||||
static void dump_listeners()
|
||||
{
|
||||
int i;
|
||||
struct net_event_listener* listener;
|
||||
|
||||
|
||||
hub_log(log_dump, "listeners: number=%d", num_connections);
|
||||
|
||||
for (i = 0; i < num_connections; i++)
|
||||
{
|
||||
listener = &listeners[i];
|
||||
|
||||
if (listener)
|
||||
{
|
||||
if (listener->fd != -1)
|
||||
{
|
||||
hub_log(log_dump, "epoll_dump_listeners: pos=%d/%d fd=%d, ptr=%p", i, num_connections, listeners->fd, listeners);
|
||||
}
|
||||
else
|
||||
{
|
||||
hub_log(log_dump, "epoll_dump_listeners: pos=%d/%d (unused)", i, num_connections);
|
||||
}
|
||||
|
||||
listener = 0;
|
||||
}
|
||||
}
|
||||
|
||||
getc(stdin);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
static void set_poll_events(struct epoll_event* handle, short trigger)
|
||||
{
|
||||
memset(handle, 0, sizeof(struct epoll_event));
|
||||
|
||||
if (trigger & evt_accept || trigger & evt_read || trigger & evt_close)
|
||||
handle->events |= EPOLLIN;
|
||||
|
||||
if (trigger & evt_write)
|
||||
handle->events |= EPOLLOUT;
|
||||
|
||||
if (trigger & evt_urgent)
|
||||
handle->events |= EPOLLPRI;
|
||||
|
||||
#ifdef EPOLLRDHUP
|
||||
if (triggers & evt_close)
|
||||
handle->events |= EPOLLRDHUP;
|
||||
#endif
|
||||
}
|
||||
|
||||
static short get_poll_events(struct epoll_event* handle)
|
||||
{
|
||||
short trig = handle->events;
|
||||
short evt = 0;
|
||||
|
||||
if (trig & EPOLLIN)
|
||||
evt |= evt_read;
|
||||
|
||||
if (trig & EPOLLPRI)
|
||||
evt |= evt_urgent;
|
||||
|
||||
if (trig & EPOLLOUT)
|
||||
evt |= evt_write;
|
||||
|
||||
if (trig & EPOLLHUP)
|
||||
evt |= evt_close;
|
||||
|
||||
if (trig & EPOLLERR)
|
||||
evt |= evt_error;
|
||||
|
||||
#ifdef EPOLLRDHUP
|
||||
if (trig & EPOLLRDHUP)
|
||||
evt |= evt_close;
|
||||
#endif
|
||||
|
||||
return evt;
|
||||
}
|
||||
|
||||
|
||||
int net_initialize(int capacity)
|
||||
{
|
||||
int i;
|
||||
max_connections = capacity;
|
||||
num_connections = 0;
|
||||
epfd = epoll_create(max_connections);
|
||||
if (epfd == -1)
|
||||
{
|
||||
hub_log(log_error, "net_initialize(): epoll_create failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
events = hub_malloc_zero(sizeof(struct epoll_event) * max_connections);
|
||||
if (!events)
|
||||
{
|
||||
hub_log(log_error, "net_initialize(): hub_malloc failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
monitor_allocate((size_t) capacity);
|
||||
|
||||
|
||||
|
||||
#ifdef DEBUG_EPOLL
|
||||
dump_listeners();
|
||||
#endif
|
||||
|
||||
net_stats_initialize();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int net_shutdown()
|
||||
{
|
||||
hub_log(log_trace, "Shutting down network monitor");
|
||||
if (epfd != -1)
|
||||
{
|
||||
close(epfd);
|
||||
}
|
||||
|
||||
hub_free(events);
|
||||
hub_free(listeners);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_EPOLL
|
||||
uint64_t get_time_difference_in_msec(struct timeval before, struct timeval after)
|
||||
{
|
||||
uint64_t seconds = (after.tv_sec - before.tv_sec);
|
||||
uint64_t out = seconds*1000;
|
||||
if (seconds > 0)
|
||||
out += ((after.tv_usec / 1000) + (1000 - (before.tv_usec / 1000)));
|
||||
else
|
||||
out += ((after.tv_usec - before.tv_usec) / 1000);
|
||||
return out;
|
||||
}
|
||||
#endif
|
||||
|
||||
int net_wait(int timeout_ms)
|
||||
{
|
||||
int fired, n, max, ret;
|
||||
struct net_event_listener* listener;
|
||||
|
||||
#ifdef DEBUG_EPOLL
|
||||
struct timeval tm_before;
|
||||
struct timeval tm_after;
|
||||
gettimeofday(&tm_before, NULL);
|
||||
dump_listeners();
|
||||
#endif
|
||||
|
||||
fired = epoll_wait(epfd, events, num_connections, timeout_ms);
|
||||
if (fired == -1) {
|
||||
if (errno != EINTR)
|
||||
{
|
||||
hub_log(log_error, "net_wait(): epoll_wait failed");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (n = 0; n < fired; n++)
|
||||
{
|
||||
listener = (struct net_event_listener*) events[n].data.ptr;
|
||||
listener->revents = get_poll_events(&events[n]);
|
||||
hub_log(log_dump, "net_wait(): epoll event detected (fd=%d, evt=%d, ptr=%p)", listener->fd, listener->revents, listener);
|
||||
}
|
||||
|
||||
max = num_connections;
|
||||
|
||||
for (n = 0; n < max; n++)
|
||||
{
|
||||
listener = &listeners[n];
|
||||
if (listener && listener->fd != -1 && listener->revents)
|
||||
{
|
||||
hub_log(log_dump, "net_wait(): epoll trigger call (fd=%d, evt=%d, ptr=%p)", listener->fd, listener->revents, listener);
|
||||
ret = listener->handler(listener);
|
||||
listener->revents = 0;
|
||||
}
|
||||
#ifdef DEBUG_EPOLL
|
||||
else
|
||||
{
|
||||
if (listener)
|
||||
hub_log(log_dump, "net_wait(): epoll trigger ignore (fd=%d, evt=%d, ptr=%p)", listener->fd, listener->revents, listener);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef DEBUG_EPOLL
|
||||
gettimeofday(&tm_after, NULL);
|
||||
size_t diff = (size_t) get_time_difference_in_msec(tm_before, tm_after);
|
||||
dump_listeners();
|
||||
hub_log(log_debug, "net_wait(): time=%dms, triggered=%d", diff, fired);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int net_add(int fd, short events, void* data, net_event_handler_t handler)
|
||||
{
|
||||
struct epoll_event ev;
|
||||
struct net_event_listener* listener = monitor_get_free_listener();
|
||||
|
||||
hub_log(log_trace, "net_add(): adding socket (fd=%d, pos=%d)", fd, pos);
|
||||
|
||||
if (!listener)
|
||||
{
|
||||
hub_log(log_error, "net_add(): unable to poll more sockets");
|
||||
return -1;
|
||||
}
|
||||
|
||||
net_event_listener_set(listener, fd, events, data, handler);
|
||||
|
||||
set_poll_events(&ev, events);
|
||||
ev.data.ptr = listener;
|
||||
|
||||
if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) < 0)
|
||||
{
|
||||
hub_log(log_error, "net_add(): epoll_ctl error while adding socket (fd=%d)", fd);
|
||||
net_event_listener_clear(listener);
|
||||
return -1;
|
||||
}
|
||||
|
||||
num_connections++;
|
||||
|
||||
#ifdef DEBUG_EPOLL
|
||||
dump_listeners();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int net_modify(int fd, short events)
|
||||
{
|
||||
struct epoll_event ev;
|
||||
struct net_event_listener* listener = monitor_get_listener(fd);
|
||||
hub_log(log_trace, "net_modify(): modifying socket events (fd=%d)", fd);
|
||||
|
||||
if (!listener)
|
||||
{
|
||||
hub_log(log_error, "net_modify(): unable to find socket.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
listener->events = events;
|
||||
set_poll_events(&ev, events);
|
||||
ev.data.ptr = listener;
|
||||
|
||||
if (epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &ev) < 0)
|
||||
{
|
||||
hub_log(log_error, "net_add(): epoll_ctl error while modifying socket (fd=%d)", fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_EPOLL
|
||||
dump_listeners();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int net_remove(int fd)
|
||||
{
|
||||
struct epoll_event ev;
|
||||
struct net_event_listener* listener = monitor_get_listener(fd);
|
||||
|
||||
hub_log(log_trace, "net_remove(): removing socket (fd=%d, pos=%d)", fd, pos);
|
||||
|
||||
if (!listener)
|
||||
{
|
||||
/* The socket is not being monitored */
|
||||
hub_log(log_error, "net_remove(): unable to remove socket (fd=%d)", fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
net_event_listener_clear(listener);
|
||||
|
||||
if (epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &ev) < 0)
|
||||
{
|
||||
hub_log(log_error, "net_remove(): epoll_ctl error while removing socket (fd=%d)", fd);
|
||||
return -1;
|
||||
}
|
||||
num_connections--;
|
||||
|
||||
#ifdef DEBUG_EPOLL
|
||||
dump_listeners();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#endif /* HAVE_EPOLL */
|
||||
|
||||
|
||||
|
||||
@@ -1,290 +0,0 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
#ifdef HAVE_KQUEUE
|
||||
|
||||
static struct kevent* events = 0;
|
||||
static struct kevent* change = 0;
|
||||
static int kfd = -1;
|
||||
|
||||
|
||||
static void set_poll_events(struct kevent* handle, short trigger)
|
||||
{
|
||||
if (!handle) {
|
||||
hub_log(log_error, "OOOPS!!");
|
||||
return;
|
||||
}
|
||||
|
||||
memset(handle, 0, sizeof(struct kevent));
|
||||
|
||||
if (trigger & evt_accept || trigger & evt_read || trigger & evt_close)
|
||||
handle->filter |= EVFILT_READ;
|
||||
|
||||
if (trigger & evt_write /* || trigger & evt_accept*/)
|
||||
handle->filter |= EVFILT_WRITE;
|
||||
}
|
||||
|
||||
static short get_poll_events(struct kevent* handle)
|
||||
{
|
||||
short trig = handle->flags;
|
||||
short evt = 0;
|
||||
|
||||
if (trig & EVFILT_READ)
|
||||
evt |= evt_read;
|
||||
|
||||
if (trig & EVFILT_WRITE)
|
||||
evt |= evt_write;
|
||||
|
||||
if (trig & EV_EOF)
|
||||
{
|
||||
evt |= evt_close;
|
||||
|
||||
if (handle->fflags)
|
||||
evt |= evt_error;
|
||||
}
|
||||
|
||||
if (handle->filter == -1)
|
||||
{
|
||||
|
||||
evt |= evt_error;
|
||||
}
|
||||
|
||||
if (handle->data)
|
||||
{
|
||||
evt |= evt_accept;
|
||||
}
|
||||
|
||||
if (evt)
|
||||
{
|
||||
hub_log(log_error, "Evt: fd=%d, filter=%d, flags=%d, fflags=%d, data=%d evt=%#x", handle->ident, handle->filter, handle->flags, handle->fflags, (int) handle->data, evt);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
return evt;
|
||||
}
|
||||
|
||||
int net_initialize(int capacity)
|
||||
{
|
||||
int i;
|
||||
max_connections = capacity;
|
||||
num_connections = 0;
|
||||
kfd = kqueue();
|
||||
if (kfd == -1)
|
||||
{
|
||||
hub_log(log_error, "net_initialize(): kqueue failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
events = (void*) hub_malloc_zero(sizeof(struct kevent) * max_connections);
|
||||
if (!events)
|
||||
{
|
||||
hub_log(log_error, "net_initialize(): hub_malloc failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
change = (void*) hub_malloc_zero(sizeof(struct kevent) * max_connections);
|
||||
if (!events)
|
||||
{
|
||||
hub_log(log_error, "net_initialize(): hub_malloc failed");
|
||||
hub_free(events);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
listeners = (void*) hub_malloc_zero(sizeof(struct net_event_listener) * max_connections);
|
||||
if (!listeners)
|
||||
{
|
||||
hub_log(log_error, "net_initialize(): hub_malloc failed");
|
||||
hub_free(change);
|
||||
hub_free(events);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < max_connections; i++)
|
||||
{
|
||||
listeners[i].fd = -1;
|
||||
}
|
||||
|
||||
net_stats_initialize();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int net_shutdown()
|
||||
{
|
||||
if (kfd != -1) {
|
||||
return close(kfd);
|
||||
}
|
||||
|
||||
hub_free(events);
|
||||
hub_free(change);
|
||||
hub_free(listeners);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int net_wait(int timeout_ms)
|
||||
{
|
||||
int fired, n, max, ret;
|
||||
struct net_event_listener* listener;
|
||||
struct timespec timeout = { (timeout_ms / 1000), (timeout_ms % 1000) * 1000 };
|
||||
|
||||
fired = kevent(kfd, events, num_connections, change, num_connections, &timeout);
|
||||
if (fired == -1) {
|
||||
if (errno != EINTR)
|
||||
{
|
||||
hub_log(log_error, "net_wait(): kevent failed");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (n = 0; n < fired; n++)
|
||||
{
|
||||
listener = (struct net_event_listener*) events[n].udata;
|
||||
if (listener)
|
||||
{
|
||||
listener->revents = get_poll_events(&events[n]);
|
||||
hub_log(log_dump, "net_wait(): kqueue event detected (fd=%d, evt=%d, ptr=%p)", listener->fd, listener->revents, listener);
|
||||
}
|
||||
}
|
||||
|
||||
max = num_connections;
|
||||
for (n = 0; n < max; n++)
|
||||
{
|
||||
listener = &listeners[n];
|
||||
if (listener && listener->fd != -1 && listener->revents != 0)
|
||||
{
|
||||
hub_log(log_dump, "net_wait(): kqueue trigger call (fd=%d, evt=%d, ptr=%p)", listener->fd, listener->revents, listener);
|
||||
ret = listener->handler(listener);
|
||||
listener->revents = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int net_add(int fd, short events_, void* data, net_event_handler_t handler)
|
||||
{
|
||||
struct kevent* event;
|
||||
struct net_event_listener* listener = monitor_get_listener(fd);
|
||||
|
||||
hub_log(log_trace, "net_add(): adding socket (fd=%d)", fd);
|
||||
|
||||
if (listener)
|
||||
{
|
||||
/* Already added! */
|
||||
return -1;
|
||||
}
|
||||
|
||||
listener = monitor_get_free_listener();
|
||||
if (!listener)
|
||||
{
|
||||
hub_log(log_error, "net_add(): unable to poll more sockets");
|
||||
return -1;
|
||||
}
|
||||
|
||||
net_event_listener_set(listener, fd, events_, data, handler);
|
||||
|
||||
event = &events[pos];
|
||||
set_poll_events(event, events_);
|
||||
event->ident = fd;
|
||||
|
||||
event->flags |= EV_ADD;
|
||||
event->flags |= EV_ONESHOT;
|
||||
|
||||
#ifdef __APPLE__
|
||||
event->flags |= EV_ENABLE;
|
||||
#endif
|
||||
event->udata = listener;
|
||||
|
||||
num_connections++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int net_modify(int fd, short events_)
|
||||
{
|
||||
struct kevent* event;
|
||||
struct net_event_listener* listener = monitor_get_listener(fd);
|
||||
|
||||
hub_log(log_trace, "net_modify(): modifying socket (fd=%d)", fd);
|
||||
|
||||
if (!listener)
|
||||
{
|
||||
/* The socket is not being monitored */
|
||||
hub_log(log_error, "net_modify(): unable to find socket (fd=%d)", fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
event = &events[pos];
|
||||
// set_poll_events(event, events_);
|
||||
|
||||
event->ident = fd;
|
||||
event->flags |= EV_ADD;
|
||||
event->flags |= EV_ONESHOT;
|
||||
#ifdef __APPLE__
|
||||
event->flags |= EV_ENABLE;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int net_remove(int fd)
|
||||
{
|
||||
struct kevent* event;
|
||||
struct net_event_listener* listener = monitor_get_listener(fd);
|
||||
|
||||
hub_log(log_trace, "net_remove(): removing socket (fd=%d)", fd);
|
||||
|
||||
if (!listener)
|
||||
{
|
||||
/* The socket is not being monitored */
|
||||
hub_log(log_error, "net_remove(): unable to remove socket (fd=%d)", fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
net_event_listener_clear(listener);
|
||||
|
||||
event = &events[pos];
|
||||
event->ident = fd;
|
||||
event->filter = 0;
|
||||
event->flags = EV_DELETE;
|
||||
|
||||
#ifdef __APPLE__
|
||||
event->flasg |= EV_DISABLE;
|
||||
#endif
|
||||
|
||||
event->fflags = 0;
|
||||
event->data = 0;
|
||||
event->udata = 0;
|
||||
|
||||
num_connections--;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#endif /* HAVE_KQUEUE */
|
||||
|
||||
|
||||
|
||||
63
src/network/backend.c
Normal file
63
src/network/backend.c
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
#include "network/connection.h"
|
||||
|
||||
struct net_cleanup_handler
|
||||
{
|
||||
size_t num;
|
||||
size_t max;
|
||||
struct net_connection** queue;
|
||||
};
|
||||
|
||||
struct net_cleanup_handler* net_cleanup_initialize(size_t max)
|
||||
{
|
||||
struct net_cleanup_handler* handler = (struct net_cleanup_handler*) hub_malloc(sizeof(struct net_cleanup_handler));
|
||||
handler->num = 0;
|
||||
handler->max = max;
|
||||
handler->queue = hub_malloc_zero(sizeof(struct net_connection*) * max);
|
||||
return handler;
|
||||
}
|
||||
|
||||
void net_cleanup_shutdown(struct net_cleanup_handler* handler)
|
||||
{
|
||||
hub_free(handler->queue);
|
||||
hub_free(handler);
|
||||
}
|
||||
|
||||
void net_cleanup_delayed_free(struct net_cleanup_handler* handler, struct net_connection* con)
|
||||
{
|
||||
handler->queue[handler->num++] = con;
|
||||
con->flags |= NET_CLEANUP;
|
||||
}
|
||||
|
||||
void net_cleanup_process(struct net_cleanup_handler* handler)
|
||||
{
|
||||
size_t n;
|
||||
for (n = 0; n < handler->num; n++)
|
||||
{
|
||||
struct net_connection* con = handler->queue[n];
|
||||
LOG_TRACE("net_cleanup_process: free: %p", con);
|
||||
hub_free(con);
|
||||
}
|
||||
handler->num = 0;
|
||||
}
|
||||
|
||||
53
src/network/backend.h
Normal file
53
src/network/backend.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef HAVE_UHUB_NETWORK_BACKEND_H
|
||||
#define HAVE_UHUB_NETWORK_BACKEND_H
|
||||
|
||||
struct net_cleanup_handler;
|
||||
struct net_connection;
|
||||
|
||||
/**
|
||||
* Initialize the network backend.
|
||||
* Returns 1 on success, or 0 on failure.
|
||||
*/
|
||||
extern int net_backend_initialize();
|
||||
|
||||
/**
|
||||
* Shutdown the network connection backend.
|
||||
*/
|
||||
extern void net_backend_shutdown();
|
||||
|
||||
/**
|
||||
* Process the network backend.
|
||||
*/
|
||||
extern int net_backend_process();
|
||||
|
||||
extern struct timeout_queue* net_backend_get_timeout_queue();
|
||||
|
||||
struct net_cleanup_handler* net_cleanup_initialize(size_t max);
|
||||
|
||||
void net_cleanup_shutdown(struct net_cleanup_handler* handler);
|
||||
|
||||
void net_cleanup_delayed_free(struct net_cleanup_handler* handler, struct net_connection* con);
|
||||
|
||||
void net_cleanup_process(struct net_cleanup_handler* handler);
|
||||
|
||||
|
||||
#endif /* HAVE_UHUB_NETWORK_BACKEND_H */
|
||||
51
src/network/common.h
Normal file
51
src/network/common.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#define NET_WANT_READ NET_EVENT_READ
|
||||
#define NET_WANT_WRITE NET_EVENT_WRITE
|
||||
#define NET_WANT_ACCEPT NET_EVENT_READ
|
||||
#define NET_WANT_SSL_READ 0x0010
|
||||
#define NET_WANT_SSL_WRITE 0x0020
|
||||
#define NET_WANT_SSL_ACCEPT 0x0040
|
||||
#define NET_WANT_SSL_CONNECT 0x0080
|
||||
#define NET_WANT_SSL_X509_LOOKUP 0x0100
|
||||
|
||||
#define NET_CLEANUP 0x8000
|
||||
|
||||
#define NET_CON_STRUCT_BASIC \
|
||||
int sd; /** socket descriptor */ \
|
||||
uint32_t flags; /** Connection flags */ \
|
||||
void* ptr; /** data pointer */ \
|
||||
net_connection_cb callback; /** Callback function */ \
|
||||
struct timeout_evt* timeout; /** timeout event handler */
|
||||
|
||||
#define NET_CON_STRUCT_SSL \
|
||||
SSL* ssl; /** SSL handle */ \
|
||||
uint32_t ssl_state; /** SSL state */ \
|
||||
size_t write_len; /** Length of last SSL_write(), only used if flags is NET_WANT_SSL_READ. */ \
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
#define NET_CON_STRUCT_COMMON \
|
||||
NET_CON_STRUCT_BASIC \
|
||||
NET_CON_STRUCT_SSL
|
||||
#else
|
||||
#define NET_CON_STRUCT_COMMON \
|
||||
NET_CON_STRUCT_BASIC
|
||||
#endif /* SSL_SUPPORT */
|
||||
|
||||
323
src/network/connection.c
Normal file
323
src/network/connection.c
Normal file
@@ -0,0 +1,323 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
#include "network/common.h"
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
|
||||
enum uhub_tls_state
|
||||
{
|
||||
tls_st_none,
|
||||
tls_st_error,
|
||||
tls_st_accepting,
|
||||
tls_st_connecting,
|
||||
tls_st_connected,
|
||||
tls_st_disconnecting,
|
||||
};
|
||||
|
||||
static int handle_openssl_error(struct net_connection* con, int ret)
|
||||
{
|
||||
uhub_assert(con);
|
||||
|
||||
int error = SSL_get_error(con->ssl, ret);
|
||||
switch (error)
|
||||
{
|
||||
case SSL_ERROR_ZERO_RETURN:
|
||||
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_ZERO_RETURN", ret, error);
|
||||
con->ssl_state = tls_st_error;
|
||||
return -1;
|
||||
|
||||
case SSL_ERROR_WANT_READ:
|
||||
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_WANT_READ", ret, error);
|
||||
con->flags |= NET_WANT_SSL_READ;
|
||||
net_con_update(con, NET_EVENT_READ);
|
||||
return 0;
|
||||
|
||||
case SSL_ERROR_WANT_WRITE:
|
||||
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_WANT_WRITE", ret, error);
|
||||
con->flags |= NET_WANT_SSL_WRITE;
|
||||
net_con_update(con, NET_EVENT_READ | NET_EVENT_WRITE);
|
||||
return 0;
|
||||
|
||||
case SSL_ERROR_SYSCALL:
|
||||
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_SYSCALL", ret, error);
|
||||
/* if ret == 0, connection closed, if ret == -1, check with errno */
|
||||
if (ret == 0)
|
||||
return -1;
|
||||
else
|
||||
return -net_error();
|
||||
|
||||
case SSL_ERROR_SSL:
|
||||
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_SSL", ret, error);
|
||||
/* internal openssl error */
|
||||
con->ssl_state = tls_st_error;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t net_con_ssl_accept(struct net_connection* con)
|
||||
{
|
||||
uhub_assert(con);
|
||||
con->ssl_state = tls_st_accepting;
|
||||
ssize_t ret = SSL_accept(con->ssl);
|
||||
#ifdef NETWORK_DUMP_DEBUG
|
||||
LOG_PROTO("SSL_accept() ret=%d", ret);
|
||||
#endif
|
||||
if (ret > 0)
|
||||
{
|
||||
net_con_update(con, NET_EVENT_READ);
|
||||
con->ssl_state = tls_st_connected;
|
||||
}
|
||||
else
|
||||
{
|
||||
return handle_openssl_error(con, ret);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t net_con_ssl_connect(struct net_connection* con)
|
||||
{
|
||||
uhub_assert(con);
|
||||
|
||||
con->ssl_state = tls_st_connecting;
|
||||
ssize_t ret = SSL_connect(con->ssl);
|
||||
#ifdef NETWORK_DUMP_DEBUG
|
||||
LOG_PROTO("SSL_connect() ret=%d", ret);
|
||||
#endif
|
||||
if (ret > 0)
|
||||
{
|
||||
con->ssl_state = tls_st_connected;
|
||||
net_con_update(con, NET_EVENT_READ);
|
||||
}
|
||||
else
|
||||
{
|
||||
return handle_openssl_error(con, ret);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t net_con_ssl_handshake(struct net_connection* con, enum net_con_ssl_mode ssl_mode, SSL_CTX* ssl_ctx)
|
||||
{
|
||||
uhub_assert(con);
|
||||
SSL* ssl = 0;
|
||||
|
||||
if (ssl_mode == net_con_ssl_mode_server)
|
||||
{
|
||||
ssl = SSL_new(ssl_ctx);
|
||||
SSL_set_fd(ssl, con->sd);
|
||||
net_con_set_ssl(con, ssl);
|
||||
return net_con_ssl_accept(con);
|
||||
}
|
||||
else
|
||||
{
|
||||
ssl = SSL_new(SSL_CTX_new(TLSv1_method()));
|
||||
SSL_set_fd(ssl, con->sd);
|
||||
net_con_set_ssl(con, ssl);
|
||||
return net_con_ssl_connect(con);
|
||||
}
|
||||
}
|
||||
#endif /* SSL_SUPPORT */
|
||||
|
||||
|
||||
ssize_t net_con_send(struct net_connection* con, const void* buf, size_t len)
|
||||
{
|
||||
int ret;
|
||||
#ifdef SSL_SUPPORT
|
||||
if (!con->ssl)
|
||||
{
|
||||
#endif
|
||||
ret = net_send(con->sd, buf, len, UHUB_SEND_SIGNAL);
|
||||
if (ret == -1)
|
||||
{
|
||||
if (net_error() == EWOULDBLOCK || net_error() == EINTR)
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
#ifdef SSL_SUPPORT
|
||||
}
|
||||
else
|
||||
{
|
||||
con->write_len = len;
|
||||
ret = SSL_write(con->ssl, buf, len);
|
||||
LOG_PROTO("SSL_write(con=%p, buf=%p, len=" PRINTF_SIZE_T ") => %d", con, buf, len, ret);
|
||||
if (ret <= 0)
|
||||
{
|
||||
return -handle_openssl_error(con, ret);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len)
|
||||
{
|
||||
int ret;
|
||||
#ifdef SSL_SUPPORT
|
||||
if (!net_con_is_ssl(con))
|
||||
{
|
||||
#endif
|
||||
ret = net_recv(con->sd, buf, len, 0);
|
||||
if (ret == -1)
|
||||
{
|
||||
if (net_error() == EWOULDBLOCK || net_error() == EINTR)
|
||||
return 0;
|
||||
return -net_error();
|
||||
}
|
||||
else if (ret == 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
#ifdef SSL_SUPPORT
|
||||
}
|
||||
else
|
||||
{
|
||||
if (con->ssl_state == tls_st_error)
|
||||
return -1;
|
||||
|
||||
ret = SSL_read(con->ssl, buf, len);
|
||||
LOG_PROTO("SSL_read(con=%p, buf=%p, len=" PRINTF_SIZE_T ") => %d", con, buf, len, ret);
|
||||
if (ret > 0)
|
||||
{
|
||||
net_con_update(con, NET_EVENT_READ);
|
||||
}
|
||||
else
|
||||
{
|
||||
return -handle_openssl_error(con, ret);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t net_con_peek(struct net_connection* con, void* buf, size_t len)
|
||||
{
|
||||
int ret = net_recv(con->sd, buf, len, MSG_PEEK);
|
||||
if (ret == -1)
|
||||
{
|
||||
if (net_error() == EWOULDBLOCK || net_error() == EINTR)
|
||||
return 0;
|
||||
return -net_error();
|
||||
}
|
||||
else if (ret == 0)
|
||||
return -1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
int net_con_is_ssl(struct net_connection* con)
|
||||
{
|
||||
return con->ssl != 0;
|
||||
}
|
||||
|
||||
SSL* net_con_get_ssl(struct net_connection* con)
|
||||
{
|
||||
return con->ssl;
|
||||
}
|
||||
|
||||
void net_con_set_ssl(struct net_connection* con, SSL* ssl)
|
||||
{
|
||||
con->ssl = ssl;
|
||||
}
|
||||
#endif /* SSL_SUPPORT */
|
||||
|
||||
int net_con_get_sd(struct net_connection* con)
|
||||
{
|
||||
return con->sd;
|
||||
}
|
||||
|
||||
void* net_con_get_ptr(struct net_connection* con)
|
||||
{
|
||||
return con->ptr;
|
||||
}
|
||||
|
||||
void net_con_callback(struct net_connection* con, int events)
|
||||
{
|
||||
if (con->flags & NET_CLEANUP)
|
||||
return;
|
||||
|
||||
if (events == NET_EVENT_TIMEOUT)
|
||||
{
|
||||
LOG_TRACE("net_con_callback(%p, TIMEOUT", con);
|
||||
con->callback(con, events, con->ptr);
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
if (!con->ssl)
|
||||
{
|
||||
#endif
|
||||
con->callback(con, events, con->ptr);
|
||||
#ifdef SSL_SUPPORT
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef NETWORK_DUMP_DEBUG
|
||||
LOG_PROTO("net_con_event: events=%d, con=%p, state=%d", events, con, con->ssl_state);
|
||||
#endif
|
||||
switch (con->ssl_state)
|
||||
{
|
||||
case tls_st_none:
|
||||
con->callback(con, events, con->ptr);
|
||||
break;
|
||||
|
||||
case tls_st_error:
|
||||
con->callback(con, NET_EVENT_READ, con->ptr);
|
||||
break;
|
||||
|
||||
case tls_st_accepting:
|
||||
if (net_con_ssl_accept(con) < 0)
|
||||
{
|
||||
con->callback(con, NET_EVENT_READ, con->ptr);
|
||||
}
|
||||
break;
|
||||
|
||||
case tls_st_connecting:
|
||||
if (net_con_ssl_connect(con) < 0)
|
||||
{
|
||||
con->callback(con, NET_EVENT_READ, con->ptr);
|
||||
}
|
||||
break;
|
||||
|
||||
case tls_st_connected:
|
||||
LOG_PROTO("tls_st_connected, events=%s%s, ssl_flags=%s%s", (events & NET_EVENT_READ ? "R" : ""), (events & NET_EVENT_WRITE ? "W" : ""), con->flags & NET_WANT_SSL_READ ? "R" : "", con->flags & NET_WANT_SSL_WRITE ? "W" : "");
|
||||
if (events & NET_EVENT_WRITE && con->flags & NET_WANT_SSL_READ)
|
||||
{
|
||||
con->callback(con, events & NET_EVENT_READ, con->ptr);
|
||||
return;
|
||||
}
|
||||
|
||||
if (events & NET_EVENT_READ && con->flags & NET_WANT_SSL_WRITE)
|
||||
{
|
||||
con->callback(con, events & NET_EVENT_READ, con->ptr);
|
||||
return;
|
||||
}
|
||||
|
||||
con->callback(con, events, con->ptr);
|
||||
break;
|
||||
|
||||
case tls_st_disconnecting:
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
115
src/network/connection.h
Normal file
115
src/network/connection.h
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef HAVE_UHUB_NETWORK_CONNECTION_H
|
||||
#define HAVE_UHUB_NETWORK_CONNECTION_H
|
||||
|
||||
#include "uhub.h"
|
||||
#include "network/common.h"
|
||||
#include "network/backend.h"
|
||||
|
||||
#define NET_EVENT_TIMEOUT 0x0001
|
||||
#define NET_EVENT_READ 0x0002
|
||||
#define NET_EVENT_WRITE 0x0004
|
||||
|
||||
struct net_connection;
|
||||
|
||||
typedef void (*net_connection_cb)(struct net_connection*, int event, void* ptr);
|
||||
|
||||
struct net_connection
|
||||
{
|
||||
NET_CON_STRUCT_COMMON
|
||||
};
|
||||
|
||||
extern int net_con_get_sd(struct net_connection* con);
|
||||
extern void* net_con_get_ptr(struct net_connection* con);
|
||||
|
||||
extern struct net_connection* net_con_create();
|
||||
extern void net_con_destroy(struct net_connection*);
|
||||
extern void net_con_initialize(struct net_connection* con, int sd, net_connection_cb callback, const void* ptr, int events);
|
||||
extern void net_con_reinitialize(struct net_connection* con, net_connection_cb callback, const void* ptr, int events);
|
||||
extern void net_con_update(struct net_connection* con, int events);
|
||||
extern void net_con_callback(struct net_connection* con, int events);
|
||||
|
||||
/**
|
||||
* Close the connection.
|
||||
* This will ensure a connection is closed properly and will generate a NET_EVENT_DESTROYED event which indicates
|
||||
* that the con can safely be deleted (or set to NULL).
|
||||
*/
|
||||
extern void net_con_close(struct net_connection* con);
|
||||
|
||||
/**
|
||||
* Send data
|
||||
*
|
||||
* @return returns the number of bytes sent.
|
||||
* 0 if no data is sent, and this function should be called again (EWOULDBLOCK/EINTR)
|
||||
* <0 if an error occured, the negative number contains the error code.
|
||||
*/
|
||||
extern ssize_t net_con_send(struct net_connection* con, const void* buf, size_t len);
|
||||
|
||||
/**
|
||||
* Receive data
|
||||
*
|
||||
* @return returns the number of bytes sent.
|
||||
* 0 if no data is sent, and this function should be called again (EWOULDBLOCK/EINTR)
|
||||
* <0 if an error occured, the negative number contains the error code.
|
||||
*/
|
||||
extern ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len);
|
||||
|
||||
/**
|
||||
* Receive data without removing them from the recv() buffer.
|
||||
* NOTE: This does not currently work for SSL connections after the SSL handshake has been
|
||||
* performed.
|
||||
*/
|
||||
extern ssize_t net_con_peek(struct net_connection* con, void* buf, size_t len);
|
||||
|
||||
/**
|
||||
* Set timeout for connetion.
|
||||
*
|
||||
* @param seconds the number of seconds into the future.
|
||||
*/
|
||||
extern void net_con_set_timeout(struct net_connection* con, int seconds);
|
||||
extern void net_con_clear_timeout(struct net_connection* con);
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
/**
|
||||
* Start SSL_accept()
|
||||
*/
|
||||
extern ssize_t net_con_ssl_accept(struct net_connection*);
|
||||
|
||||
/**
|
||||
* Start SSL_connect()
|
||||
*/
|
||||
extern ssize_t net_con_ssl_connect(struct net_connection*);
|
||||
|
||||
enum net_con_ssl_mode
|
||||
{
|
||||
net_con_ssl_mode_server,
|
||||
net_con_ssl_mode_client,
|
||||
};
|
||||
|
||||
extern ssize_t net_con_ssl_handshake(struct net_connection* con, enum net_con_ssl_mode, SSL_CTX* ssl_ctx);
|
||||
|
||||
extern int net_con_is_ssl(struct net_connection* con);
|
||||
extern SSL* net_con_get_ssl(struct net_connection* con);
|
||||
extern void net_con_set_ssl(struct net_connection* con, SSL*);
|
||||
#endif /* SSL_SUPPORT */
|
||||
|
||||
#endif /* HAVE_UHUB_NETWORK_CONNECTION_H */
|
||||
|
||||
231
src/network/epoll.c
Normal file
231
src/network/epoll.c
Normal file
@@ -0,0 +1,231 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
#ifdef USE_EPOLL
|
||||
|
||||
#include "network/connection.h"
|
||||
#include "network/common.h"
|
||||
#include "network/backend.h"
|
||||
|
||||
#define EPOLL_EVBUFFER 512
|
||||
|
||||
struct net_connection_epoll
|
||||
{
|
||||
NET_CON_STRUCT_COMMON
|
||||
struct epoll_event ev;
|
||||
};
|
||||
|
||||
struct net_backend
|
||||
{
|
||||
int epfd;
|
||||
size_t num;
|
||||
size_t max;
|
||||
struct net_connection_epoll** conns;
|
||||
struct epoll_event events[EPOLL_EVBUFFER];
|
||||
time_t now;
|
||||
struct timeout_queue timeout_queue;
|
||||
struct net_cleanup_handler* cleaner;
|
||||
};
|
||||
|
||||
static struct net_backend* g_backend = 0;
|
||||
|
||||
static void net_con_print(const char* prefix, struct net_connection_epoll* con)
|
||||
{
|
||||
char buf[512];
|
||||
int off = snprintf(buf, 512, "%s: net_connection={ sd=%d, flags=%u, callback=%p, ptr=%p, ev={ events=%s%s, data.ptr=%p }",
|
||||
prefix, con->sd, con->flags, con->callback, con->ptr, (con->ev.events & EPOLLIN ? "R" : ""),(con->ev.events & EPOLLOUT ? "W" : "") , con->ev.data.ptr);
|
||||
if (con->timeout)
|
||||
{
|
||||
sprintf(buf + off, ", timeout={ %d seconds left }", (int) (con->timeout->timestamp - g_backend->now));
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(buf + off, ", timeout=NULL");
|
||||
}
|
||||
LOG_TRACE(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the network backend.
|
||||
* Returns 1 on success, or 0 on failure.
|
||||
*/
|
||||
int net_backend_initialize()
|
||||
{
|
||||
size_t max = net_get_max_sockets();
|
||||
g_backend = hub_malloc(sizeof(struct net_backend));
|
||||
g_backend->epfd = epoll_create(max);
|
||||
if (g_backend->epfd == -1)
|
||||
{
|
||||
LOG_WARN("Unable to create epoll socket.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
g_backend->num = 0;
|
||||
g_backend->max = max;
|
||||
g_backend->conns = hub_malloc_zero(sizeof(struct net_connection_epoll*) * max);
|
||||
memset(g_backend->events, 0, sizeof(g_backend->events));
|
||||
|
||||
g_backend->now = time(0);
|
||||
timeout_queue_initialize(&g_backend->timeout_queue, g_backend->now, 600); /* look max 10 minutes into the future. */
|
||||
g_backend->cleaner = net_cleanup_initialize(max);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the network connection backend.
|
||||
*/
|
||||
void net_backend_shutdown()
|
||||
{
|
||||
close(g_backend->epfd);
|
||||
timeout_queue_shutdown(&g_backend->timeout_queue);
|
||||
net_cleanup_shutdown(g_backend->cleaner);
|
||||
hub_free(g_backend->conns);
|
||||
hub_free(g_backend);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the network backend.
|
||||
*/
|
||||
int net_backend_process()
|
||||
{
|
||||
int n;
|
||||
size_t secs = timeout_queue_get_next_timeout(&g_backend->timeout_queue, g_backend->now);
|
||||
LOG_TRACE("epoll_wait: fd=%d, events=%x, max=%zu, seconds=%d", g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER), (int) secs);
|
||||
int res = epoll_wait(g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER), secs * 1000);
|
||||
|
||||
g_backend->now = time(0);
|
||||
timeout_queue_process(&g_backend->timeout_queue, g_backend->now);
|
||||
|
||||
if (res == -1)
|
||||
{
|
||||
LOG_WARN("epoll_wait returned -1");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (n = 0; n < res; n++)
|
||||
{
|
||||
struct net_connection_epoll* con = (struct net_connection_epoll*) g_backend->events[n].data.ptr;
|
||||
int ev = 0;
|
||||
if (g_backend->events[n].events & EPOLLIN) ev |= NET_EVENT_READ;
|
||||
if (g_backend->events[n].events & EPOLLOUT) ev |= NET_EVENT_WRITE;
|
||||
net_con_callback((struct net_connection*) con, ev);
|
||||
}
|
||||
|
||||
net_cleanup_process(g_backend->cleaner);
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct timeout_queue* net_backend_get_timeout_queue()
|
||||
{
|
||||
return &g_backend->timeout_queue;
|
||||
}
|
||||
|
||||
struct net_connection* net_con_create()
|
||||
{
|
||||
struct net_connection* con = (struct net_connection*) hub_malloc_zero(sizeof(struct net_connection_epoll));
|
||||
con->sd = -1;
|
||||
return con;
|
||||
}
|
||||
|
||||
void net_con_destroy(struct net_connection* con)
|
||||
{
|
||||
hub_free(con);
|
||||
}
|
||||
|
||||
void net_con_initialize(struct net_connection* con_, int sd, net_connection_cb callback, const void* ptr, int events)
|
||||
{
|
||||
struct net_connection_epoll* con = (struct net_connection_epoll*) con_;
|
||||
con->sd = sd;
|
||||
con->flags = 0;
|
||||
con->callback = callback;
|
||||
con->ev.events = 0;
|
||||
con->ptr = (void*) ptr;
|
||||
con->ev.data.ptr = (void*) con;
|
||||
|
||||
net_set_nonblocking(con->sd, 1);
|
||||
net_set_nosigpipe(con->sd, 1);
|
||||
|
||||
if (events & NET_EVENT_READ) con->ev.events |= EPOLLIN;
|
||||
if (events & NET_EVENT_WRITE) con->ev.events |= EPOLLOUT;
|
||||
|
||||
g_backend->conns[sd] = con;
|
||||
g_backend->num++;
|
||||
|
||||
if (epoll_ctl(g_backend->epfd, EPOLL_CTL_ADD, con->sd, &con->ev) == -1)
|
||||
{
|
||||
LOG_TRACE("epoll_ctl() add failed.");
|
||||
}
|
||||
|
||||
net_con_print("ADD", con);
|
||||
}
|
||||
|
||||
void net_con_reinitialize(struct net_connection* con, net_connection_cb callback, const void* ptr, int events)
|
||||
{
|
||||
con->callback = callback;
|
||||
con->ptr = (void*) ptr;
|
||||
net_con_update(con, events);
|
||||
}
|
||||
|
||||
void net_con_update(struct net_connection* con_, int events)
|
||||
{
|
||||
struct net_connection_epoll* con = (struct net_connection_epoll*) con_;
|
||||
int newev = 0;
|
||||
if (events & NET_EVENT_READ) newev |= EPOLLIN;
|
||||
if (events & NET_EVENT_WRITE) newev |= EPOLLOUT;
|
||||
|
||||
if (newev == con->ev.events)
|
||||
return;
|
||||
|
||||
con->ev.events = newev;
|
||||
if (epoll_ctl(g_backend->epfd, EPOLL_CTL_MOD, con->sd, &con->ev) == -1)
|
||||
{
|
||||
LOG_TRACE("epoll_ctl() modify failed.");
|
||||
}
|
||||
net_con_print("MOD", con);
|
||||
}
|
||||
|
||||
void net_con_close(struct net_connection* con_)
|
||||
{
|
||||
struct net_connection_epoll* con = (struct net_connection_epoll*) con_;
|
||||
if (con->flags & NET_CLEANUP)
|
||||
return;
|
||||
|
||||
if (con->sd != -1)
|
||||
{
|
||||
g_backend->conns[con->sd] = 0;
|
||||
g_backend->num--;
|
||||
}
|
||||
|
||||
net_con_clear_timeout(con_);
|
||||
|
||||
if (epoll_ctl(g_backend->epfd, EPOLL_CTL_DEL, con->sd, &con->ev) == -1)
|
||||
{
|
||||
LOG_WARN("epoll_ctl() delete failed.");
|
||||
}
|
||||
|
||||
net_close(con->sd);
|
||||
con->sd = -1;
|
||||
|
||||
net_con_print("DEL", con);
|
||||
net_cleanup_delayed_free(g_backend->cleaner, con_);
|
||||
}
|
||||
|
||||
#endif /* USE_EPOLL */
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
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;
|
||||
|
||||
@@ -35,21 +36,25 @@ int net_initialize()
|
||||
{
|
||||
if (!net_initialized)
|
||||
{
|
||||
LOG_TRACE("Initializing network monitor.");
|
||||
|
||||
#ifdef WINSOCK
|
||||
struct WSAData wsa;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsa) != NO_ERROR)
|
||||
{
|
||||
hub_log(log_error, "Unable to initialize winsock.");
|
||||
LOG_ERROR("Unable to initialize winsock.");
|
||||
return -1;
|
||||
}
|
||||
#endif /* WINSOCK */
|
||||
|
||||
hub_log(log_trace, "Initializing network monitor.");
|
||||
net_backend_initialize();
|
||||
net_stats_initialize();
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
/* FIXME: Initialize OpenSSL here. */
|
||||
LOG_TRACE("Initializing OpenSSL...");
|
||||
SSL_library_init();
|
||||
SSL_load_error_strings();
|
||||
OpenSSL_add_all_algorithms();
|
||||
#endif /* SSL_SUPPORT */
|
||||
|
||||
net_initialized = 1;
|
||||
@@ -58,13 +63,36 @@ int net_initialize()
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t net_get_max_sockets()
|
||||
{
|
||||
#ifdef HAVE_GETRLIMIT
|
||||
struct rlimit limits;
|
||||
if (getrlimit(RLIMIT_NOFILE, &limits) == 0)
|
||||
{
|
||||
return limits.rlim_max;
|
||||
}
|
||||
LOG_ERROR("getrlimit() failed");
|
||||
return 1024;
|
||||
#else
|
||||
#ifdef WIN32
|
||||
LOG_WARN("Windows system, limited to 4096 connections.");
|
||||
return 4096;
|
||||
#else
|
||||
LOG_WARN("System does not have getrlimit(): constrained to 1024 sockets");
|
||||
return 1024;
|
||||
#endif
|
||||
#endif /* HAVE_GETRLIMIT */
|
||||
}
|
||||
|
||||
int net_shutdown()
|
||||
|
||||
int net_destroy()
|
||||
{
|
||||
if (net_initialized)
|
||||
{
|
||||
hub_log(log_trace, "Shutting down network monitor");
|
||||
LOG_TRACE("Shutting down network monitor");
|
||||
|
||||
net_backend_shutdown();
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
/* FIXME: Shutdown OpenSSL here. */
|
||||
#endif
|
||||
@@ -78,6 +106,18 @@ int net_shutdown()
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef USE_LIBEVENT
|
||||
struct event_base* net_get_evbase()
|
||||
{
|
||||
return net_evbase;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void net_error_out(int fd, const char* func)
|
||||
{
|
||||
int err = net_error();
|
||||
LOG_ERROR("%s, fd=%d: %s (%d)", func, fd, net_error_string(err), err);
|
||||
}
|
||||
|
||||
int net_error()
|
||||
{
|
||||
@@ -103,60 +143,81 @@ const char* net_error_string(int code)
|
||||
|
||||
static int net_setsockopt(int fd, int level, int opt, const void* optval, socklen_t optlen)
|
||||
{
|
||||
int ret = -1;
|
||||
#ifdef WINSOCK
|
||||
return setsockopt(fd, level, opt, (const char*) optval, optlen);
|
||||
ret = setsockopt(fd, level, opt, (const char*) optval, optlen);
|
||||
#else
|
||||
return setsockopt(fd, level, opt, optval, optlen);
|
||||
ret = setsockopt(fd, level, opt, optval, optlen);
|
||||
#endif
|
||||
|
||||
if (ret == -1)
|
||||
{
|
||||
net_error_out(fd, "net_setsockopt");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int net_getsockopt(int fd, int level, int opt, void* optval, socklen_t* optlen)
|
||||
{
|
||||
int ret = -1;
|
||||
#ifdef WINSOCK
|
||||
ret = getsockopt(fd, level, opt, (char*) optval, optlen);
|
||||
#else
|
||||
ret = getsockopt(fd, level, opt, optval, optlen);
|
||||
#endif
|
||||
|
||||
if (ret == -1)
|
||||
{
|
||||
net_error_out(fd, "net_getsockopt");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int net_set_nonblocking(int fd, int toggle)
|
||||
{
|
||||
int ret;
|
||||
|
||||
#ifdef NETAPI_DUMP
|
||||
hub_log(log_dump, "net_set_nonblocking(): fd=%d", fd);
|
||||
#endif
|
||||
|
||||
int ret = -1;
|
||||
#ifdef WINSOCK
|
||||
u_long on = toggle ? 1 : 0;
|
||||
ret = ioctlsocket(fd, FIONBIO, &on);
|
||||
#else
|
||||
#ifdef __sun__
|
||||
int flags = fcntl(fd, F_GETFL, 0);
|
||||
if (flags != -1)
|
||||
{
|
||||
if (toggle) flags |= O_NONBLOCK;
|
||||
else flags &= ~O_NONBLOCK;
|
||||
ret = fcntl(fd, F_SETFL, flags);
|
||||
}
|
||||
#else
|
||||
ret = ioctl(fd, FIONBIO, &toggle);
|
||||
#endif
|
||||
#endif
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_log(log_error, "net_set_nonblocking(): ioctl failed (fd=%d): %s", fd, net_error_string(net_error()));
|
||||
return -1;
|
||||
net_error_out(fd, "net_set_nonblocking");
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* NOTE: Possibly only supported on BSD and OSX? */
|
||||
int net_set_nosigpipe(int fd, int toggle)
|
||||
{
|
||||
int ret = -1;
|
||||
#ifdef SO_NOSIGPIPE
|
||||
int ret;
|
||||
#ifdef NETAPI_DUMP
|
||||
hub_log(log_dump, "net_set_nosigpipe(): fd=%d", fd);
|
||||
#endif
|
||||
ret = net_setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &toggle, sizeof(toggle));
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_log(log_error, "net_set_linger(): setsockopt failed (fd=%d): %s", fd, net_error_string(net_error()));
|
||||
return -1;
|
||||
net_error_out(fd, "net_set_nosigpipe");
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int net_set_close_on_exec(int fd, int toggle)
|
||||
{
|
||||
#ifdef NETAPI_DUMP
|
||||
hub_log(log_dump, "net_set_close_on_exec(): fd=%d", fd);
|
||||
#endif
|
||||
#ifdef WINSOCK
|
||||
return -1; /* FIXME: How is this done on Windows? */
|
||||
#else
|
||||
@@ -164,54 +225,63 @@ int net_set_close_on_exec(int fd, int toggle)
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int net_set_linger(int fd, int toggle)
|
||||
{
|
||||
int ret;
|
||||
#ifdef NETAPI_DUMP
|
||||
hub_log(log_dump, "net_set_linger(): fd=%d", fd);
|
||||
#endif
|
||||
ret = net_setsockopt(fd, SOL_SOCKET, SO_LINGER, &toggle, sizeof(toggle));
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_log(log_error, "net_set_linger(): setsockopt failed (fd=%d): %s", fd, net_error_string(net_error()));
|
||||
return -1;
|
||||
net_error_out(fd, "net_set_linger");
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int net_set_keepalive(int fd, int toggle)
|
||||
{
|
||||
int ret;
|
||||
#ifdef NETAPI_DUMP
|
||||
hub_log(log_dump, "net_set_keepalive(): fd=%d", fd);
|
||||
#endif
|
||||
ret = net_setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &toggle, sizeof(toggle));
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_log(log_error, "net_set_keepalive(): setsockopt failed (fd=%d): %s", fd, net_error_string(net_error()));
|
||||
return -1;
|
||||
net_error_out(fd, "net_set_keepalive");
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int net_set_reuseaddress(int fd, int toggle)
|
||||
{
|
||||
int ret;
|
||||
#ifdef NETAPI_DUMP
|
||||
hub_log(log_dump, "net_set_reuseaddress(): fd=%d", fd);
|
||||
#endif
|
||||
ret = net_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &toggle, sizeof(toggle));
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_log(log_error, "net_set_reuseaddress(): setsockopt failed (fd=%d): %s", fd, net_error_string(net_error()));
|
||||
return -1;
|
||||
net_error_out(fd, "net_set_reuseaddress");
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int net_set_sendbuf_size(int fd, size_t size)
|
||||
{
|
||||
return net_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
|
||||
}
|
||||
|
||||
int net_get_sendbuf_size(int fd, size_t* size)
|
||||
{
|
||||
socklen_t sz = sizeof(*size);
|
||||
return net_getsockopt(fd, SOL_SOCKET, SO_SNDBUF, size, &sz);
|
||||
}
|
||||
|
||||
int net_set_recvbuf_size(int fd, size_t size)
|
||||
{
|
||||
return net_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
|
||||
}
|
||||
|
||||
int net_get_recvbuf_size(int fd, size_t* size)
|
||||
{
|
||||
socklen_t sz = sizeof(*size);
|
||||
return net_getsockopt(fd, SOL_SOCKET, SO_RCVBUF, size, &sz);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int net_close(int fd)
|
||||
{
|
||||
@@ -227,7 +297,7 @@ int net_close(int fd)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fd != -1)
|
||||
if (ret != -1)
|
||||
{
|
||||
net_stats_add_error();
|
||||
}
|
||||
@@ -235,14 +305,46 @@ int net_close(int fd)
|
||||
return ret;
|
||||
}
|
||||
|
||||
int net_shutdown_r(int fd)
|
||||
{
|
||||
#ifdef WINSOCK
|
||||
return shutdown(fd, SD_RECEIVE);
|
||||
#else
|
||||
return shutdown(fd, SHUT_RD);
|
||||
#endif
|
||||
}
|
||||
|
||||
int net_accept(int fd)
|
||||
int net_shutdown_w(int fd)
|
||||
{
|
||||
#ifdef WINSOCK
|
||||
return shutdown(fd, SD_SEND);
|
||||
#else
|
||||
return shutdown(fd, SHUT_WR);
|
||||
#endif
|
||||
}
|
||||
|
||||
int net_shutdown_rw(int fd)
|
||||
{
|
||||
#ifdef WINSOCK
|
||||
return shutdown(fd, SD_BOTH);
|
||||
#else
|
||||
return shutdown(fd, SHUT_RDWR);
|
||||
#endif
|
||||
}
|
||||
|
||||
int net_accept(int fd, struct ip_addr_encap* ipaddr)
|
||||
{
|
||||
struct sockaddr_storage addr;
|
||||
struct sockaddr_in* addr4;
|
||||
struct sockaddr_in6* addr6;
|
||||
socklen_t addr_size;
|
||||
int ret = 0;
|
||||
addr_size = sizeof(struct sockaddr_storage);
|
||||
|
||||
memset(&addr, 0, addr_size);
|
||||
addr4 = (struct sockaddr_in*) &addr;
|
||||
addr6 = (struct sockaddr_in6*) &addr;
|
||||
|
||||
ret = accept(fd, (struct sockaddr*) &addr, &addr_size);
|
||||
|
||||
if (ret == -1)
|
||||
@@ -262,7 +364,7 @@ int net_accept(int fd)
|
||||
case EWOULDBLOCK:
|
||||
break;
|
||||
default:
|
||||
hub_log(log_error, "net_accept(): accept failed (fd=%d, errno=%d, msg=%s)", fd, net_error(), net_error_string(net_error()));
|
||||
net_error_out(fd, "net_accept");
|
||||
net_stats_add_error();
|
||||
return -1;
|
||||
}
|
||||
@@ -270,6 +372,31 @@ int net_accept(int fd)
|
||||
else
|
||||
{
|
||||
net_stats_add_accept();
|
||||
|
||||
if (ipaddr)
|
||||
{
|
||||
memset(ipaddr, 0, sizeof(struct ip_addr_encap));
|
||||
ipaddr->af = addr.ss_family;;
|
||||
if (ipaddr->af == AF_INET6)
|
||||
{
|
||||
char address[INET6_ADDRSTRLEN+1] = { 0, };
|
||||
net_address_to_string(AF_INET6, (void*) &addr6->sin6_addr, address, INET6_ADDRSTRLEN+1);
|
||||
if (strchr(address, '.'))
|
||||
{
|
||||
/* Hack to convert IPv6 mapped IPv4 addresses to true IPv4 addresses */
|
||||
ipaddr->af = AF_INET;
|
||||
net_string_to_address(AF_INET, address, (void*) &ipaddr->internal_ip_data.in);
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(&ipaddr->internal_ip_data.in6, &addr6->sin6_addr, sizeof(struct in6_addr));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(&ipaddr->internal_ip_data.in, &addr4->sin_addr, sizeof(struct in_addr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -283,7 +410,7 @@ int net_connect(int fd, const struct sockaddr *serv_addr, socklen_t addrlen)
|
||||
{
|
||||
if (net_error() != EINPROGRESS)
|
||||
{
|
||||
hub_log(log_error, "net_connect(): connect failed (fd=%d, errno=%d, msg=%s)", fd, net_error(), net_error_string(net_error()));
|
||||
net_error_out(fd, "net_connect");
|
||||
net_stats_add_error();
|
||||
}
|
||||
}
|
||||
@@ -305,13 +432,12 @@ int net_is_ipv6_supported()
|
||||
if (net_error() == EAFNOSUPPORT)
|
||||
#endif
|
||||
{
|
||||
hub_log(log_trace, "net_is_ipv6_supported(): IPv6 is not supported on this system.");
|
||||
LOG_TRACE("net_is_ipv6_supported(): IPv6 is not supported on this system.");
|
||||
is_ipv6_supported = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
hub_log(log_error, "net_is_ipv6_supported(): Unknown error (errno=%d, msg=%s)", net_error(), net_error_string(net_error()));
|
||||
|
||||
net_error_out(ret, "net_is_ipv6_supported");
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -319,7 +445,7 @@ int net_is_ipv6_supported()
|
||||
int off = 0;
|
||||
if (net_setsockopt(ret, IPPROTO_IPV6, SOCK_DUAL_STACK_OPT, (char*) &off, sizeof(off)) < 0)
|
||||
{
|
||||
hub_log(log_error, "net_socket_create(): Dual stack IPv6/IPv4 is not supported.");
|
||||
LOG_ERROR("net_socket_create(): Dual stack IPv6/IPv4 is not supported.");
|
||||
is_ipv6_supported = 0;
|
||||
}
|
||||
else
|
||||
@@ -341,7 +467,8 @@ int net_socket_create(int af, int type, int protocol)
|
||||
int sd = socket(af, type, protocol);
|
||||
if (sd == -1)
|
||||
{
|
||||
hub_log(log_error, "net_socket_create(): socket failed (errno=%d, msg=%s)", net_error(), net_error_string(net_error()));
|
||||
net_error_out(sd, "net_socket_create");
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef SOCK_DUAL_STACK_OPT
|
||||
@@ -351,7 +478,7 @@ int net_socket_create(int af, int type, int protocol)
|
||||
int off = 0;
|
||||
if (net_setsockopt(sd, IPPROTO_IPV6, SOCK_DUAL_STACK_OPT, (char*) &off, sizeof(off)) < 0)
|
||||
{
|
||||
hub_log(log_error, "net_socket_create(): Cannot set socket to dual stack mode IPv6/IPv4 (%d - %s).", net_error(), net_error_string(net_error()));
|
||||
LOG_ERROR("net_socket_create(): Cannot set socket to dual stack mode IPv6/IPv4 (%d - %s).", net_error(), net_error_string(net_error()));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -399,7 +526,15 @@ const char* net_address_to_string(int af, const void* src, char* dst, socklen_t
|
||||
|
||||
return NULL;
|
||||
#else
|
||||
return inet_ntop(af, src, dst, cnt);
|
||||
if (inet_ntop(af, src, dst, cnt))
|
||||
{
|
||||
if (af == AF_INET6 && strncmp(dst, "::ffff:", 7) == 0) /* IPv6 mapped IPv4 address. */
|
||||
{
|
||||
memmove(dst, dst + 7, cnt - 7);
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -446,9 +581,6 @@ int net_string_to_address(int af, const char* src, void* dst)
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const char* net_get_peer_address(int fd)
|
||||
{
|
||||
static char address[INET6_ADDRSTRLEN+1];
|
||||
@@ -467,32 +599,65 @@ const char* net_get_peer_address(int fd)
|
||||
|
||||
if (getpeername(fd, (struct sockaddr*) name, &namelen) != -1)
|
||||
{
|
||||
int af = name4->sin_family;
|
||||
int af = storage.ss_family;
|
||||
if (af == AF_INET6)
|
||||
{
|
||||
net_address_to_string(af, (void*) &name6->sin6_addr, address, INET6_ADDRSTRLEN);
|
||||
if (strncmp(address, "::ffff:", 7) == 0) /* IPv6 mapped IPv4 address. */
|
||||
{
|
||||
return &address[7];
|
||||
}
|
||||
hub_log(log_trace, "net_get_peer_address(): address=%s", address);
|
||||
return address;
|
||||
}
|
||||
else
|
||||
{
|
||||
net_address_to_string(af, (void*) &name4->sin_addr, address, INET6_ADDRSTRLEN);
|
||||
hub_log(log_trace, "net_get_peer_address(): address=%s", address);
|
||||
return address;
|
||||
}
|
||||
return address;
|
||||
}
|
||||
else
|
||||
{
|
||||
hub_log(log_error, "net_get_peer_address(): getsockname failed (fd=%d, errno=%d, msg=%s)", fd, net_error(), net_error_string(net_error()));
|
||||
net_error_out(fd, "net_get_peer_address");
|
||||
net_stats_add_error();
|
||||
}
|
||||
|
||||
return "0.0.0.0";
|
||||
}
|
||||
|
||||
const char* net_get_local_address(int fd)
|
||||
{
|
||||
static char address[INET6_ADDRSTRLEN+1];
|
||||
struct sockaddr_storage storage;
|
||||
struct sockaddr_in6* name6;
|
||||
struct sockaddr_in* name4;
|
||||
struct sockaddr* name;
|
||||
|
||||
memset(address, 0, INET6_ADDRSTRLEN);
|
||||
socklen_t namelen = sizeof(struct sockaddr_storage);
|
||||
memset(&storage, 0, namelen);
|
||||
|
||||
name6 = (struct sockaddr_in6*) &storage;
|
||||
name4 = (struct sockaddr_in*) &storage;
|
||||
name = (struct sockaddr*) &storage;
|
||||
|
||||
if (getsockname(fd, (struct sockaddr*) name, &namelen) != -1)
|
||||
{
|
||||
int af = storage.ss_family;
|
||||
if (af == AF_INET6)
|
||||
{
|
||||
net_address_to_string(af, (void*) &name6->sin6_addr, address, INET6_ADDRSTRLEN);
|
||||
}
|
||||
else
|
||||
{
|
||||
net_address_to_string(af, (void*) &name4->sin_addr, address, INET6_ADDRSTRLEN);
|
||||
}
|
||||
return address;
|
||||
}
|
||||
else
|
||||
{
|
||||
net_error_out(fd, "net_get_local_address");
|
||||
net_stats_add_error();
|
||||
}
|
||||
|
||||
return "0.0.0.0";
|
||||
}
|
||||
|
||||
|
||||
|
||||
ssize_t net_recv(int fd, void* buf, size_t len, int flags)
|
||||
{
|
||||
@@ -505,7 +670,7 @@ ssize_t net_recv(int fd, void* buf, size_t len, int flags)
|
||||
{
|
||||
if (net_error() != EWOULDBLOCK)
|
||||
{
|
||||
hub_log(log_debug, "net_recv(): failed (fd=%d, errno=%d, msg=%s)", fd, net_error(), net_error_string(net_error()));
|
||||
/* net_error_out(fd, "net_recv"); */
|
||||
net_stats_add_error();
|
||||
}
|
||||
}
|
||||
@@ -513,7 +678,7 @@ ssize_t net_recv(int fd, void* buf, size_t len, int flags)
|
||||
}
|
||||
|
||||
|
||||
ssize_t net_send(int fd, void* buf, size_t len, int flags)
|
||||
ssize_t net_send(int fd, const void* buf, size_t len, int flags)
|
||||
{
|
||||
ssize_t ret = send(fd, buf, len, flags);
|
||||
if (ret >= 0)
|
||||
@@ -524,7 +689,7 @@ ssize_t net_send(int fd, void* buf, size_t len, int flags)
|
||||
{
|
||||
if (net_error() != EWOULDBLOCK)
|
||||
{
|
||||
hub_log(log_debug, "net_send(): failed (fd=%d, errno=%d, msg=%s)", fd, net_error(), net_error_string(net_error()));
|
||||
/* net_error_out(fd, "net_send"); */
|
||||
net_stats_add_error();
|
||||
}
|
||||
}
|
||||
@@ -537,7 +702,7 @@ int net_bind(int fd, const struct sockaddr *my_addr, socklen_t addrlen)
|
||||
int ret = bind(fd, my_addr, addrlen);
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_log(log_error, "net_bind(): failed (fd=%d, errno=%d, msg=%s)", fd, net_error(), net_error_string(net_error()));
|
||||
net_error_out(fd, "net_bind");
|
||||
net_stats_add_error();
|
||||
}
|
||||
return ret;
|
||||
@@ -549,7 +714,7 @@ int net_listen(int fd, int backlog)
|
||||
int ret = listen(fd, backlog);
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_log(log_error, "net_listen(): failed (fd=%d, errno=%d, msg=%s)", fd, net_error(), net_error_string(net_error()));
|
||||
net_error_out(fd, "net_listen");
|
||||
net_stats_add_error();
|
||||
}
|
||||
return ret;
|
||||
@@ -565,14 +730,12 @@ void net_stats_initialize()
|
||||
stats.timestamp = time(NULL);
|
||||
}
|
||||
|
||||
|
||||
void net_stats_get(struct net_statistics** intermediate, struct net_statistics** total)
|
||||
{
|
||||
*intermediate = &stats;
|
||||
*total = &stats_total;
|
||||
}
|
||||
|
||||
|
||||
void net_stats_reset()
|
||||
{
|
||||
stats_total.tx += stats.tx;
|
||||
@@ -585,37 +748,31 @@ void net_stats_reset()
|
||||
stats.timestamp = time(NULL);
|
||||
}
|
||||
|
||||
|
||||
int net_stats_timeout()
|
||||
{
|
||||
return (difftime(time(NULL), stats.timestamp) > TIMEOUT_STATS) ? 1 : 0;
|
||||
}
|
||||
|
||||
|
||||
void net_stats_add_tx(size_t bytes)
|
||||
{
|
||||
stats.tx += bytes;
|
||||
}
|
||||
|
||||
|
||||
void net_stats_add_rx(size_t bytes)
|
||||
{
|
||||
stats.rx += bytes;
|
||||
}
|
||||
|
||||
|
||||
void net_stats_add_accept()
|
||||
{
|
||||
stats.accept++;
|
||||
}
|
||||
|
||||
|
||||
void net_stats_add_error()
|
||||
{
|
||||
stats.errors++;
|
||||
}
|
||||
|
||||
|
||||
void net_stats_add_close()
|
||||
{
|
||||
stats.closed++;
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -30,6 +30,8 @@ struct net_statistics
|
||||
size_t errors;
|
||||
};
|
||||
|
||||
struct net_socket_t;
|
||||
|
||||
/**
|
||||
* Initialize the socket monitor subsystem.
|
||||
* On some operating systems this will also involve loading the TCP/IP stack
|
||||
@@ -47,7 +49,9 @@ extern int net_initialize();
|
||||
*
|
||||
* @return -1 on error, 0 on success
|
||||
*/
|
||||
extern int net_shutdown();
|
||||
extern int net_destroy();
|
||||
|
||||
extern struct event_base* net_get_evbase();
|
||||
|
||||
/**
|
||||
* @return the number of sockets currrently being monitored.
|
||||
@@ -76,15 +80,27 @@ extern const char* net_error_string(int code);
|
||||
*/
|
||||
extern int net_socket_create(int af, int type, int protocol);
|
||||
|
||||
/**
|
||||
* Returns the maximum number of file/socket descriptors.
|
||||
*/
|
||||
extern size_t net_get_max_sockets();
|
||||
|
||||
/**
|
||||
* A wrapper for the close() function call.
|
||||
*/
|
||||
extern int net_close(int fd);
|
||||
|
||||
extern int net_shutdown_r(int fd);
|
||||
extern int net_shutdown_w(int fd);
|
||||
extern int net_shutdown_rw(int fd);
|
||||
|
||||
/**
|
||||
* A wrapper for the accept() function call.
|
||||
* @param fd socket descriptor
|
||||
* @param ipaddr (in/out) if non-NULL the ip address of the
|
||||
* accepted peer is filled in.
|
||||
*/
|
||||
extern int net_accept(int fd);
|
||||
extern int net_accept(int fd, struct ip_addr_encap* ipaddr);
|
||||
|
||||
/**
|
||||
* A wrapper for the connect() call.
|
||||
@@ -148,6 +164,38 @@ extern int net_set_linger(int fd, int toggle);
|
||||
*/
|
||||
extern int net_set_reuseaddress(int fd, int toggle);
|
||||
|
||||
/**
|
||||
* Set the send buffer size for the socket.
|
||||
* @param fd socket descriptor
|
||||
* @param size size to set
|
||||
* @return -1 on error, 0 on success.
|
||||
*/
|
||||
extern int net_set_sendbuf_size(int fd, size_t size);
|
||||
|
||||
/**
|
||||
* Get the send buffer size for the socket.
|
||||
* @param fd socket descriptor
|
||||
* @param[out] size existing size, cannot be NULL.
|
||||
* @return -1 on error, 0 on success.
|
||||
*/
|
||||
extern int net_get_sendbuf_size(int fd, size_t* size);
|
||||
|
||||
/**
|
||||
* Set the receive buffer size for the socket.
|
||||
* @param fd socket descriptor
|
||||
* @param size size to set
|
||||
* @return -1 on error, 0 on success.
|
||||
*/
|
||||
extern int net_set_recvbuf_size(int fd, size_t size);
|
||||
|
||||
/**
|
||||
* Get the receive buffer size for the socket.
|
||||
* @param fd socket descriptor
|
||||
* @param[out] size existing size, cannot be NULL.
|
||||
* @return -1 on error, 0 on success.
|
||||
*/
|
||||
extern int net_get_recvbuf_size(int fd, size_t* size);
|
||||
|
||||
/**
|
||||
* A wrapper for the recv() function call.
|
||||
*/
|
||||
@@ -156,7 +204,7 @@ extern ssize_t net_recv(int fd, void* buf, size_t len, int flags);
|
||||
/**
|
||||
* A wrapper for the send() function call.
|
||||
*/
|
||||
extern ssize_t net_send(int fd, void* buf, size_t len, int flags);
|
||||
extern ssize_t net_send(int fd, const void* buf, size_t len, int flags);
|
||||
|
||||
/**
|
||||
* This tries to create a AF_INET6 socket.
|
||||
@@ -176,6 +224,8 @@ extern int net_is_ipv6_supported();
|
||||
*/
|
||||
extern const char* net_get_peer_address(int fd);
|
||||
|
||||
extern const char* net_get_local_address(int fd);
|
||||
|
||||
/**
|
||||
* See man(3) inet_ntop.
|
||||
*/
|
||||
@@ -207,11 +257,6 @@ extern void net_stats_get(struct net_statistics** intermediate, struct net_stati
|
||||
|
||||
#if defined(WINSOCK) && !defined(__CYGWIN__)
|
||||
|
||||
// #define EINTR WSAEINTR
|
||||
// #define EACCES WSAEACCES
|
||||
// #define EFAULT WSAEFAULT
|
||||
// #define EINVAL WSAEINVAL
|
||||
// #define EMFILE WSAEMFILE
|
||||
#define EWOULDBLOCK WSAEWOULDBLOCK
|
||||
#define EINPROGRESS WSAEINPROGRESS
|
||||
#define EALREADY WSAEALREADY
|
||||
@@ -240,10 +285,8 @@ extern void net_stats_get(struct net_statistics** intermediate, struct net_stati
|
||||
#define ETIMEDOUT WSAETIMEDOUT
|
||||
#define ECONNREFUSED WSAECONNREFUSED
|
||||
#define ELOOP WSAELOOP
|
||||
// #define ENAMETOOLONG WSAENAMETOOLONG
|
||||
#define EHOSTDOWN WSAEHOSTDOWN
|
||||
#define EHOSTUNREACH WSAEHOSTUNREACH
|
||||
// #define ENOTEMPTY WSAENOTEMPTY
|
||||
#define EPROCLIM WSAEPROCLIM
|
||||
#define EUSERS WSAEUSERS
|
||||
#define EDQUOT WSAEDQUOT
|
||||
216
src/network/select.c
Normal file
216
src/network/select.c
Normal file
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
#ifdef USE_SELECT
|
||||
|
||||
#include "network/connection.h"
|
||||
#include "network/common.h"
|
||||
#include "network/backend.h"
|
||||
|
||||
struct net_connection_select
|
||||
{
|
||||
NET_CON_STRUCT_COMMON
|
||||
};
|
||||
|
||||
struct net_backend
|
||||
{
|
||||
size_t num;
|
||||
size_t max;
|
||||
struct net_connection_select** conns;
|
||||
fd_set rfds;
|
||||
fd_set wfds;
|
||||
time_t now;
|
||||
struct timeout_queue timeout_queue;
|
||||
struct net_cleanup_handler* cleaner;
|
||||
};
|
||||
|
||||
static struct net_backend* g_backend = 0;
|
||||
|
||||
static void net_con_print(const char* prefix, struct net_connection_select* con)
|
||||
{
|
||||
char buf[512];
|
||||
int off = snprintf(buf, 512, "%s: net_connection={ sd=%d, flags=%u, callback=%p, ptr=%p, events=%s%s",
|
||||
prefix, con->sd, con->flags, con->callback, con->ptr, (con->flags & NET_EVENT_READ ? "R" : ""),(con->flags & NET_EVENT_WRITE ? "W" : ""));
|
||||
if (con->timeout)
|
||||
{
|
||||
sprintf(buf + off, ", timeout={ %d seconds left }", (int) (con->timeout->timestamp - g_backend->now));
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(buf + off, ", timeout=NULL");
|
||||
}
|
||||
LOG_TRACE(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the network backend.
|
||||
* Returns 1 on success, or 0 on failure.
|
||||
*/
|
||||
int net_backend_initialize()
|
||||
{
|
||||
size_t max = net_get_max_sockets();
|
||||
g_backend = hub_malloc(sizeof(struct net_backend));
|
||||
g_backend->num = 0;
|
||||
g_backend->max = max;
|
||||
g_backend->conns = hub_malloc_zero(sizeof(struct net_connection_select*) * max);
|
||||
FD_ZERO(&g_backend->rfds);
|
||||
FD_ZERO(&g_backend->wfds);
|
||||
g_backend->now = time(0);
|
||||
timeout_queue_initialize(&g_backend->timeout_queue, g_backend->now, 600); /* look max 10 minutes into the future. */
|
||||
g_backend->cleaner = net_cleanup_initialize(max);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the network connection backend.
|
||||
*/
|
||||
void net_backend_shutdown()
|
||||
{
|
||||
timeout_queue_shutdown(&g_backend->timeout_queue);
|
||||
net_cleanup_shutdown(g_backend->cleaner);
|
||||
hub_free(g_backend->conns);
|
||||
hub_free(g_backend);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the network backend.
|
||||
*/
|
||||
int net_backend_process()
|
||||
{
|
||||
int n, found, maxfd;
|
||||
struct timeval tval;
|
||||
FD_ZERO(&g_backend->rfds);
|
||||
FD_ZERO(&g_backend->wfds);
|
||||
|
||||
size_t secs = timeout_queue_get_next_timeout(&g_backend->timeout_queue, g_backend->now);
|
||||
tval.tv_secs = secs;
|
||||
tval.tv_usecs = 0;
|
||||
|
||||
for (n = 0, found = 0; found < g_backend->num && n < g_backend->max; n++)
|
||||
{
|
||||
struct net_connection_select* con = g_backend->conns[n];
|
||||
if (con)
|
||||
{
|
||||
if (con->flags & NET_EVENT_READ) FD_SET(con->sd, &g_backend->rfds);
|
||||
if (con->flags & NET_EVENT_WRITE) FD_SET(con->sd, &g_backend->wfds);
|
||||
found++;
|
||||
maxfd = con->sd;
|
||||
}
|
||||
}
|
||||
|
||||
int res = select(maxfd+1, &g_backend->rfds, &g_backend->wfds, 0, &tval);
|
||||
g_backend->now = time(0);
|
||||
timeout_queue_process(&g_backend->timeout_queue, g_backend->now);
|
||||
|
||||
if (res == -1)
|
||||
{
|
||||
LOG_WARN("select returned -1");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (n = 0, found = 0; found < res && n < (maxfd+1); n++)
|
||||
{
|
||||
struct net_connection_select* con = g_backend->conns[n];
|
||||
if (con)
|
||||
{
|
||||
int ev = 0;
|
||||
if (FD_ISSET(con->sd, &g_backend->rfds)) ev |= NET_EVENT_READ;
|
||||
if (FD_ISSET(con->sd, &g_backend->wfds)) ev |= NET_EVENT_WRITE;
|
||||
|
||||
if (ev)
|
||||
{
|
||||
net_con_callback((struct net_connection*) con, ev);
|
||||
found++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
net_cleanup_process(g_backend->cleaner);
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct timeout_queue* net_backend_get_timeout_queue()
|
||||
{
|
||||
return &g_backend->timeout_queue;
|
||||
}
|
||||
|
||||
struct net_connection* net_con_create()
|
||||
{
|
||||
struct net_connection* con = (struct net_connection*) hub_malloc_zero(sizeof(struct net_connection_select));
|
||||
con->sd = -1;
|
||||
return con;
|
||||
}
|
||||
|
||||
void net_con_destroy(struct net_connection* con)
|
||||
{
|
||||
hub_free(con);
|
||||
}
|
||||
|
||||
void net_con_initialize(struct net_connection* con_, int sd, net_connection_cb callback, const void* ptr, int events)
|
||||
{
|
||||
struct net_connection_select* con = (struct net_connection_select*) con_;
|
||||
con->sd = sd;
|
||||
con->flags = events;
|
||||
con->callback = callback;
|
||||
con->ptr = (void*) ptr;
|
||||
|
||||
net_set_nonblocking(con->sd, 1);
|
||||
net_set_nosigpipe(con->sd, 1);
|
||||
|
||||
g_backend->conns[sd] = con;
|
||||
g_backend->num++;
|
||||
net_con_print("ADD", con);
|
||||
}
|
||||
|
||||
void net_con_reinitialize(struct net_connection* con, net_connection_cb callback, const void* ptr, int events)
|
||||
{
|
||||
con->callback = callback;
|
||||
con->ptr = (void*) ptr;
|
||||
net_con_update(con, events);
|
||||
}
|
||||
|
||||
void net_con_update(struct net_connection* con, int events)
|
||||
{
|
||||
con->flags = events;
|
||||
net_con_print("MOD", (struct net_connection_select*) con);
|
||||
}
|
||||
|
||||
void net_con_close(struct net_connection* con)
|
||||
{
|
||||
if (con->flags & NET_CLEANUP)
|
||||
return;
|
||||
|
||||
if (con->sd != -1)
|
||||
{
|
||||
g_backend->conns[con->sd] = 0;
|
||||
g_backend->num--;
|
||||
}
|
||||
|
||||
net_con_clear_timeout(con);
|
||||
|
||||
net_close(con->sd);
|
||||
con->sd = -1;
|
||||
|
||||
net_con_print("DEL", (struct net_connection_select*) con);
|
||||
net_cleanup_delayed_free(g_backend->cleaner, con);
|
||||
}
|
||||
|
||||
#endif /* USE_SELECT */
|
||||
50
src/network/timer.c
Normal file
50
src/network/timer.c
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
#include "network/connection.h"
|
||||
|
||||
static void timeout_callback(struct timeout_evt* evt)
|
||||
{
|
||||
net_con_callback((struct net_connection*) evt->ptr, NET_EVENT_TIMEOUT);
|
||||
}
|
||||
|
||||
void net_con_set_timeout(struct net_connection* con, int seconds)
|
||||
{
|
||||
if (!con->timeout)
|
||||
{
|
||||
con->timeout = hub_malloc_zero(sizeof(struct timeout_evt));
|
||||
timeout_evt_initialize(con->timeout, timeout_callback, con);
|
||||
timeout_queue_insert(net_backend_get_timeout_queue(), con->timeout, seconds);
|
||||
}
|
||||
else
|
||||
{
|
||||
timeout_queue_reschedule(net_backend_get_timeout_queue(), con->timeout, seconds);
|
||||
}
|
||||
}
|
||||
|
||||
void net_con_clear_timeout(struct net_connection* con)
|
||||
{
|
||||
if (con->timeout && timeout_evt_is_scheduled(con->timeout))
|
||||
{
|
||||
timeout_queue_remove(net_backend_get_timeout_queue(), con->timeout);
|
||||
hub_free(con->timeout);
|
||||
con->timeout = 0;
|
||||
}
|
||||
}
|
||||
52
src/plugin.h
52
src/plugin.h
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
typedef void (*plugin_event_startup)(struct hub*);
|
||||
typedef void (*plugin_event_shutdown)(struct hub*);
|
||||
typedef void (*plugin_event_user_login)(struct hub*, struct user*);
|
||||
typedef void (*plugin_event_user_logout)(struct hub*, struct user*);
|
||||
typedef int (*plugin_event_connect)(struct hub*, struct ip_addr_encap);
|
||||
typedef void (*plugin_event_disconnect)(struct hub*, struct user*);
|
||||
typedef int (*plugin_event_message)(struct hub*, struct user*, struct adc_message*);
|
||||
typedef void (*plugin_event_support)(struct hub*, struct user*, int);
|
||||
|
||||
struct uhub_plugin
|
||||
{
|
||||
/** Starting the hub */
|
||||
plugin_event_startup evt_startup;
|
||||
|
||||
/** Shutting down the hub */
|
||||
plugin_event_shutdown evt_shutdown;
|
||||
|
||||
/** Someone connected to the hub (we only have IP at this point). */
|
||||
plugin_event_connect evt_connect;
|
||||
|
||||
/** Someone disconnected from the hub (but was not successfully logged in). */
|
||||
plugin_event_disconnect evt_disconnect;
|
||||
|
||||
/** A client sent a message about which protocol extensions it supports */
|
||||
plugin_event_support evt_support;
|
||||
|
||||
/** A client was successfully logged in to the hub */
|
||||
plugin_event_user_login evt_login;
|
||||
|
||||
/** A client (previously logged in) has disconnected. */
|
||||
plugin_event_user_logout evt_logout;
|
||||
};
|
||||
|
||||
266
src/route.c
266
src/route.c
@@ -1,266 +0,0 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
|
||||
int route_message(struct user* u, struct adc_message* msg)
|
||||
{
|
||||
struct user* target = NULL;
|
||||
|
||||
switch (msg->cache[0])
|
||||
{
|
||||
case 'B': /* Broadcast to all logged in clients */
|
||||
route_to_all(u->hub, msg);
|
||||
break;
|
||||
|
||||
case 'D':
|
||||
target = get_user_by_sid(u->hub, msg->target);
|
||||
if (target)
|
||||
{
|
||||
route_to_user(target, msg);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'E':
|
||||
target = get_user_by_sid(u->hub, msg->target);
|
||||
if (target)
|
||||
{
|
||||
route_to_user(target, msg);
|
||||
route_to_user(u, msg);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'F':
|
||||
route_to_subscribers(u->hub, msg);
|
||||
break;
|
||||
|
||||
default:
|
||||
/* Ignore the message */
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void queue_command(struct user* user, struct adc_message* msg__, int offset)
|
||||
{
|
||||
struct adc_message* msg = adc_msg_incref(msg__);
|
||||
list_append(user->send_queue, msg);
|
||||
|
||||
#ifdef DEBUG_SENDQ
|
||||
hub_log(log_trace, "SENDQ: user=%p, msg=%p (%zu), offset=%d, length=%d, total_length=%d", user, msg, msg->references, offset, msg->length, user->send_queue_size);
|
||||
#endif
|
||||
|
||||
user->send_queue_size += msg->length - offset;
|
||||
if (list_size(user->send_queue) == 1)
|
||||
{
|
||||
user->send_queue_offset = offset;
|
||||
user->tm_last_write = time(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
// #define ALWAYS_QUEUE_MESSAGES
|
||||
static size_t get_max_send_queue(struct hub_info* hub)
|
||||
{
|
||||
/* TODO: More dynamic send queue limit, for instance:
|
||||
* return MAX(hub->config->max_send_buffer, (hub->config->max_recv_buffer * hub_get_user_count(hub)));
|
||||
*/
|
||||
return hub->config->max_send_buffer;
|
||||
}
|
||||
|
||||
static size_t get_max_send_queue_soft(struct hub_info* hub)
|
||||
{
|
||||
return hub->config->max_send_buffer_soft;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* @return 1 if send queue is OK.
|
||||
* -1 if send queue is overflowed
|
||||
* 0 if soft send queue is overflowed (not implemented at the moment)
|
||||
*/
|
||||
static int check_send_queue(struct user* user, struct adc_message* msg)
|
||||
{
|
||||
if (user_flag_get(user, flag_user_list))
|
||||
return 1;
|
||||
|
||||
if ((user->send_queue_size + msg->length) > get_max_send_queue(user->hub))
|
||||
return -1;
|
||||
|
||||
if (user->send_queue_size > get_max_send_queue_soft(user->hub) && msg->priority < 0)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int route_to_user(struct user* user, struct adc_message* msg)
|
||||
{
|
||||
int ret;
|
||||
|
||||
#if LOG_SEND_MESSAGES_WHEN_ROUTED
|
||||
char* data = strndup(msg->cache, msg->length-1);
|
||||
hub_log(log_protocol, "send %s: %s", sid_to_string(user->sid), data);
|
||||
free(data);
|
||||
#endif
|
||||
|
||||
#ifndef ALWAYS_QUEUE_MESSAGES
|
||||
if (user->send_queue_size == 0 && !user_is_disconnecting(user))
|
||||
{
|
||||
ret = net_send(user->sd, msg->cache, msg->length, UHUB_SEND_SIGNAL);
|
||||
|
||||
if (ret == msg->length)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ret >= 0 || (ret == -1 && net_error() == EWOULDBLOCK))
|
||||
{
|
||||
queue_command(user, msg, ret);
|
||||
|
||||
if (user->send_queue_size && user->ev_write)
|
||||
event_add(user->ev_write, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* A socket error occured */
|
||||
user_disconnect(user, quit_socket_error);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
ret = check_send_queue(user, msg);
|
||||
if (ret == -1)
|
||||
{
|
||||
/* User is not able to swallow the data, let's cut our losses and disconnect. */
|
||||
user_disconnect(user, quit_send_queue);
|
||||
}
|
||||
else if (ret == 1)
|
||||
{
|
||||
/* queue command */
|
||||
queue_command(user, msg, 0);
|
||||
if (user->ev_write)
|
||||
event_add(user->ev_write, NULL);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
/* do not queue command as our soft-limits are exceeded */
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int route_to_all(struct hub_info* hub, struct adc_message* command) /* iterate users */
|
||||
{
|
||||
struct user* user = (struct user*) list_get_first(hub->users->list);
|
||||
while (user)
|
||||
{
|
||||
route_to_user(user, command);
|
||||
user = (struct user*) list_get_next(hub->users->list);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int route_to_subscribers(struct hub_info* hub, struct adc_message* command) /* iterate users */
|
||||
{
|
||||
int do_send;
|
||||
char* tmp;
|
||||
|
||||
struct user* user = (struct user*) list_get_first(hub->users->list);
|
||||
while (user)
|
||||
{
|
||||
if (user->feature_cast)
|
||||
{
|
||||
do_send = 1;
|
||||
|
||||
tmp = list_get_first(command->feature_cast_include);
|
||||
while (tmp)
|
||||
{
|
||||
if (!user_have_feature_cast_support(user, tmp))
|
||||
{
|
||||
do_send = 0;
|
||||
break;
|
||||
}
|
||||
tmp = list_get_next(command->feature_cast_include);;
|
||||
}
|
||||
|
||||
if (!do_send) {
|
||||
user = (struct user*) list_get_next(hub->users->list);
|
||||
continue;
|
||||
}
|
||||
|
||||
tmp = list_get_first(command->feature_cast_exclude);
|
||||
while (tmp)
|
||||
{
|
||||
if (user_have_feature_cast_support(user, tmp))
|
||||
{
|
||||
do_send = 0;
|
||||
break;
|
||||
}
|
||||
tmp = list_get_next(command->feature_cast_exclude);
|
||||
}
|
||||
|
||||
if (do_send)
|
||||
{
|
||||
route_to_user(user, command);
|
||||
}
|
||||
}
|
||||
user = (struct user*) list_get_next(hub->users->list);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int route_info_message(struct user* u)
|
||||
{
|
||||
if (!user_is_nat_override(u))
|
||||
{
|
||||
return route_to_all(u->hub, u->info);
|
||||
}
|
||||
else
|
||||
{
|
||||
struct adc_message* cmd = adc_msg_copy(u->info);
|
||||
const char* address = ip_convert_to_string(&u->ipaddr);
|
||||
struct user* user = 0;
|
||||
|
||||
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR);
|
||||
adc_msg_add_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR, address);
|
||||
|
||||
user = (struct user*) list_get_first(u->hub->users->list);
|
||||
while (user)
|
||||
{
|
||||
if (user_is_nat_override(user))
|
||||
route_to_user(user, cmd);
|
||||
else
|
||||
route_to_user(user, u->info);
|
||||
|
||||
user = (struct user*) list_get_next(u->hub->users->list);
|
||||
}
|
||||
adc_msg_free(cmd);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
61
src/sid.c
61
src/sid.c
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
const char* BASE32_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
||||
|
||||
char* sid_to_string(sid_t sid_)
|
||||
{
|
||||
static char t_sid[5];
|
||||
sid_t sid = (sid_ & 0xFFFFF); /* 20 bits only */
|
||||
sid_t A, B, C, D = 0;
|
||||
D = (sid % 32);
|
||||
sid = (sid - D) / 32;
|
||||
C = (sid % 32);
|
||||
sid = (sid - C) / 32;
|
||||
B = (sid % 32);
|
||||
sid = (sid - B) / 32;
|
||||
A = (sid % 32);
|
||||
t_sid[0] = BASE32_ALPHABET[A];
|
||||
t_sid[1] = BASE32_ALPHABET[B];
|
||||
t_sid[2] = BASE32_ALPHABET[C];
|
||||
t_sid[3] = BASE32_ALPHABET[D];
|
||||
t_sid[4] = 0;
|
||||
return t_sid;
|
||||
}
|
||||
|
||||
|
||||
sid_t string_to_sid(const char* sid)
|
||||
{
|
||||
sid_t nsid = 0;
|
||||
sid_t n, x;
|
||||
sid_t factors[] = { 32768, 1024, 32, 1};
|
||||
|
||||
if (!sid || strlen(sid) != 4) return 0;
|
||||
|
||||
for (n = 0; n < 4; n++) {
|
||||
for (x = 0; x < strlen(BASE32_ALPHABET); x++)
|
||||
if (sid[n] == BASE32_ALPHABET[x]) break;
|
||||
if (x == 32) return 0;
|
||||
nsid += x * factors[n];
|
||||
}
|
||||
return nsid;
|
||||
}
|
||||
|
||||
136
src/system.h
Normal file
136
src/system.h
Normal file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef HAVE_UHUB_SYSTEM_H
|
||||
#define HAVE_UHUB_SYSTEM_H
|
||||
|
||||
#define _FILE_OFFSET_BITS 64
|
||||
|
||||
#if USE_REGPARM && __GNUC__ >= 3
|
||||
#define REGPRM1 __attribute__((regparm(1)))
|
||||
#define REGPRM2 __attribute__((regparm(2)))
|
||||
#define REGPRM3 __attribute__((regparm(3)))
|
||||
#else
|
||||
#define REGPRM1
|
||||
#define REGPRM2
|
||||
#define REGPRM3
|
||||
#endif
|
||||
|
||||
#ifndef FORCEINLINE
|
||||
#if __GNUC__ < 3
|
||||
#define FORCEINLINE inline
|
||||
#else
|
||||
#define FORCEINLINE inline __attribute__((always_inline))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef _GNU_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
|
||||
#if defined(__CYGWIN__) || defined(__MINGW32__)
|
||||
#ifndef WINSOCK
|
||||
#define WINSOCK
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef WINSOCK
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <getopt.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if !defined(WIN32)
|
||||
#include <grp.h>
|
||||
#include <pwd.h>
|
||||
#include <sys/resource.h>
|
||||
#define HAVE_STRNDUP
|
||||
#define HAVE_MEMMEM
|
||||
#define HAVE_GETRLIMIT
|
||||
#endif
|
||||
|
||||
/* printf and size_t support */
|
||||
#if defined(WIN32)
|
||||
/* Windows uses %Iu for size_t */
|
||||
#define PRINTF_SIZE_T "%Iu"
|
||||
#else
|
||||
#define PRINTF_SIZE_T "%zu"
|
||||
#endif
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
#endif
|
||||
|
||||
#include "../version.h"
|
||||
|
||||
#define uhub_assert assert
|
||||
|
||||
#ifdef __linux__
|
||||
#define USE_EPOLL
|
||||
#include <sys/epoll.h>
|
||||
#else
|
||||
#define USE_SELECT
|
||||
#include <sys/select.h>
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__sun__)
|
||||
#undef HAVE_STRNDUP
|
||||
#undef HAVE_MEMMEM
|
||||
#endif
|
||||
|
||||
#ifdef MSG_NOSIGNAL
|
||||
#define UHUB_SEND_SIGNAL MSG_NOSIGNAL
|
||||
#else
|
||||
#ifdef MSG_NOPIPE
|
||||
#define UHUB_SEND_SIGNAL MSG_NOPIPE
|
||||
#else
|
||||
#define UHUB_SEND_SIGNAL 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef INET6_ADDRSTRLEN
|
||||
#define INET6_ADDRSTRLEN 46
|
||||
#endif
|
||||
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||
|
||||
|
||||
#endif /* HAVE_UHUB_SYSTEM_H */
|
||||
485
src/tools/adcclient.c
Normal file
485
src/tools/adcclient.c
Normal file
@@ -0,0 +1,485 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "tools/adcclient.h"
|
||||
|
||||
#define ADC_HANDSHAKE "HSUP ADBASE ADTIGR ADPING\n"
|
||||
#define ADC_CID_SIZE 39
|
||||
#define BIG_BUFSIZE 32768
|
||||
#define TIGERSIZE 24
|
||||
|
||||
static ssize_t ADC_client_recv(struct ADC_client* client);
|
||||
static void ADC_client_send_info(struct ADC_client* client);
|
||||
static void ADC_client_on_connected(struct ADC_client* client);
|
||||
static void ADC_client_on_disconnected(struct ADC_client* client);
|
||||
static void ADC_client_on_login(struct ADC_client* client);
|
||||
static int ADC_client_parse_address(struct ADC_client* client, const char* arg);
|
||||
static void ADC_client_on_recv_line(struct ADC_client* client, const char* line, size_t length);
|
||||
|
||||
static void ADC_client_debug(struct ADC_client* client, const char* format, ...)
|
||||
{
|
||||
char logmsg[1024];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(logmsg, 1024, format, args);
|
||||
va_end(args);
|
||||
fprintf(stdout, "* [%p] %s\n", client, logmsg);
|
||||
}
|
||||
|
||||
static void ADC_client_set_state(struct ADC_client* client, enum ADC_client_state state)
|
||||
{
|
||||
client->state = state;
|
||||
}
|
||||
|
||||
|
||||
static void adc_cid_pid(struct ADC_client* client)
|
||||
{
|
||||
char seed[64];
|
||||
char pid[64];
|
||||
char cid[64];
|
||||
uint64_t tiger_res1[3];
|
||||
uint64_t tiger_res2[3];
|
||||
|
||||
/* create cid+pid pair */
|
||||
memset(seed, 0, 64);
|
||||
snprintf(seed, 64, VERSION "%p", client);
|
||||
|
||||
tiger((uint64_t*) seed, strlen(seed), tiger_res1);
|
||||
base32_encode((unsigned char*) tiger_res1, TIGERSIZE, pid);
|
||||
tiger((uint64_t*) tiger_res1, TIGERSIZE, tiger_res2);
|
||||
base32_encode((unsigned char*) tiger_res2, TIGERSIZE, cid);
|
||||
cid[ADC_CID_SIZE] = 0;
|
||||
pid[ADC_CID_SIZE] = 0;
|
||||
|
||||
adc_msg_add_named_argument(client->info, ADC_INF_FLAG_PRIVATE_ID, pid);
|
||||
adc_msg_add_named_argument(client->info, ADC_INF_FLAG_CLIENT_ID, cid);
|
||||
}
|
||||
|
||||
static void event_callback(struct net_connection* con, int events, void *arg)
|
||||
{
|
||||
struct ADC_client* client = (struct ADC_client*) net_con_get_ptr(con);
|
||||
|
||||
if (events == NET_EVENT_TIMEOUT)
|
||||
{
|
||||
if (client->state == ps_conn)
|
||||
{
|
||||
client->callback(client, ADC_CLIENT_DISCONNECTED, 0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (events & NET_EVENT_READ)
|
||||
{
|
||||
if (ADC_client_recv(client) == -1)
|
||||
{
|
||||
ADC_client_on_disconnected(client);
|
||||
}
|
||||
}
|
||||
|
||||
if (events & NET_EVENT_WRITE)
|
||||
{
|
||||
if (client->state == ps_conn)
|
||||
{
|
||||
ADC_client_connect(client, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* FIXME: Call send again */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define UNESCAPE_ARG(TMP, TARGET) \
|
||||
if (TMP) \
|
||||
TARGET = adc_msg_unescape(TMP); \
|
||||
else \
|
||||
TARGET = NULL; \
|
||||
hub_free(TMP);
|
||||
|
||||
#define EXTRACT_NAMED_ARG(MSG, NAME, TARGET) \
|
||||
do { \
|
||||
char* tmp = adc_msg_get_named_argument(MSG, NAME); \
|
||||
UNESCAPE_ARG(tmp, TARGET); \
|
||||
} while (0)
|
||||
|
||||
#define EXTRACT_POS_ARG(MSG, POS, TARGET) \
|
||||
do { \
|
||||
char* tmp = adc_msg_get_argument(MSG, POS); \
|
||||
UNESCAPE_ARG(tmp, TARGET); \
|
||||
} while (0)
|
||||
|
||||
|
||||
static void ADC_client_on_recv_line(struct ADC_client* client, const char* line, size_t length)
|
||||
{
|
||||
#ifdef ADC_CLIENT_DEBUG_PROTO
|
||||
ADC_client_debug(client, "- LINE: '%s'", start);
|
||||
#endif
|
||||
|
||||
/* Parse message */
|
||||
struct adc_message* msg = adc_msg_parse(line, length);
|
||||
if (!msg)
|
||||
{
|
||||
ADC_client_debug(client, "WARNING: Message cannot be decoded: \"%s\"", line);
|
||||
return;
|
||||
}
|
||||
|
||||
if (length < 4)
|
||||
{
|
||||
ADC_client_debug(client, "Unexpected response from hub: '%s'", line);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (msg->cmd)
|
||||
{
|
||||
case ADC_CMD_ISUP:
|
||||
break;
|
||||
|
||||
case ADC_CMD_ISID:
|
||||
if (client->state == ps_protocol)
|
||||
{
|
||||
client->sid = string_to_sid(&line[5]);
|
||||
client->callback(client, ADC_CLIENT_LOGGING_IN, 0);
|
||||
ADC_client_set_state(client, ps_identify);
|
||||
ADC_client_send_info(client);
|
||||
}
|
||||
break;
|
||||
|
||||
case ADC_CMD_BMSG:
|
||||
case ADC_CMD_EMSG:
|
||||
case ADC_CMD_DMSG:
|
||||
case ADC_CMD_IMSG:
|
||||
{
|
||||
struct ADC_chat_message chat;
|
||||
struct ADC_client_callback_data data;
|
||||
chat.from_sid = msg->source;
|
||||
chat.to_sid = msg->target;
|
||||
data.chat = &chat;
|
||||
EXTRACT_POS_ARG(msg, 0, chat.message);
|
||||
client->callback(client, ADC_CLIENT_MESSAGE, &data);
|
||||
hub_free(chat.message);
|
||||
break;
|
||||
}
|
||||
|
||||
case ADC_CMD_IINF:
|
||||
{
|
||||
struct ADC_hub_info hubinfo;
|
||||
EXTRACT_NAMED_ARG(msg, "NI", hubinfo.name);
|
||||
EXTRACT_NAMED_ARG(msg, "DE", hubinfo.description);
|
||||
EXTRACT_NAMED_ARG(msg, "VE", hubinfo.version);
|
||||
|
||||
struct ADC_client_callback_data data;
|
||||
data.hubinfo = &hubinfo;
|
||||
client->callback(client, ADC_CLIENT_HUB_INFO, &data);
|
||||
hub_free(hubinfo.name);
|
||||
hub_free(hubinfo.description);
|
||||
hub_free(hubinfo.version);
|
||||
break;
|
||||
}
|
||||
|
||||
case ADC_CMD_BSCH:
|
||||
case ADC_CMD_FSCH:
|
||||
{
|
||||
client->callback(client, ADC_CLIENT_SEARCH_REQ, 0);
|
||||
break;
|
||||
}
|
||||
|
||||
case ADC_CMD_BINF:
|
||||
{
|
||||
if (msg->source == client->sid)
|
||||
{
|
||||
if (client->state == ps_verify || client->state == ps_identify)
|
||||
{
|
||||
ADC_client_on_login(client);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (adc_msg_has_named_argument(msg, "ID"))
|
||||
{
|
||||
struct ADC_user user;
|
||||
EXTRACT_NAMED_ARG(msg, "NI", user.name);
|
||||
EXTRACT_NAMED_ARG(msg, "DE", user.description);
|
||||
EXTRACT_NAMED_ARG(msg, "VE", user.version);
|
||||
EXTRACT_NAMED_ARG(msg, "ID", user.cid);
|
||||
EXTRACT_NAMED_ARG(msg, "I4", user.address);
|
||||
|
||||
struct ADC_client_callback_data data;
|
||||
data.user = &user;
|
||||
client->callback(client, ADC_CLIENT_USER_JOIN, &data);
|
||||
|
||||
hub_free(user.name);
|
||||
hub_free(user.description);
|
||||
hub_free(user.version);
|
||||
hub_free(user.cid);
|
||||
hub_free(user.address);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case ADC_CMD_ISTA:
|
||||
/*
|
||||
if (strncmp(line, "ISTA 000", 8))
|
||||
{
|
||||
ADC_client_debug(client, "status: '%s'\n", (start + 9));
|
||||
}
|
||||
*/
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
adc_msg_free(msg);
|
||||
}
|
||||
|
||||
static ssize_t ADC_client_recv(struct ADC_client* client)
|
||||
{
|
||||
ssize_t size = net_con_recv(client->con, &client->recvbuf[client->r_offset], ADC_BUFSIZE - client->r_offset);
|
||||
if (size <= 0)
|
||||
return size;
|
||||
|
||||
client->r_offset += size;
|
||||
client->recvbuf[client->r_offset] = 0;
|
||||
|
||||
char* start = client->recvbuf;
|
||||
char* pos;
|
||||
char* lastPos = 0;
|
||||
size_t remaining = client->r_offset;
|
||||
|
||||
while ((pos = memchr(start, '\n', remaining)))
|
||||
{
|
||||
pos[0] = 0;
|
||||
|
||||
ADC_client_on_recv_line(client, start, pos - start);
|
||||
|
||||
pos++;
|
||||
remaining -= (pos - start);
|
||||
start = pos;
|
||||
lastPos = pos;
|
||||
}
|
||||
|
||||
if (lastPos)
|
||||
{
|
||||
memmove(client->recvbuf, lastPos, remaining);
|
||||
client->r_offset = remaining;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void ADC_client_send(struct ADC_client* client, char* msg)
|
||||
{
|
||||
int ret = net_con_send(client->con, msg, strlen(msg));
|
||||
|
||||
#ifdef ADC_CLIENT_DEBUG_PROTO
|
||||
char* dump = strdup(msg);
|
||||
dump[strlen(msg) - 1] = 0;
|
||||
ADC_client_debug(client, "- SEND: '%s'", dump);
|
||||
free(dump);
|
||||
#endif
|
||||
|
||||
if (ret != strlen(msg))
|
||||
{
|
||||
if (ret == -1)
|
||||
{
|
||||
if (net_error() != EWOULDBLOCK)
|
||||
ADC_client_on_disconnected(client);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* FIXME: Not all data sent! */
|
||||
printf("ret (%d) != msg->length (%d)\n", ret, (int) strlen(msg));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ADC_client_send_info(struct ADC_client* client)
|
||||
{
|
||||
char binf[11];
|
||||
snprintf(binf, 11, "BINF %s\n", sid_to_string(client->sid));
|
||||
client->info = adc_msg_create(binf);
|
||||
|
||||
adc_msg_add_named_argument_string(client->info, ADC_INF_FLAG_NICK, client->nick);
|
||||
|
||||
if (client->desc)
|
||||
{
|
||||
adc_msg_add_named_argument_string(client->info, ADC_INF_FLAG_DESCRIPTION, client->desc);
|
||||
}
|
||||
|
||||
adc_msg_add_named_argument_string(client->info, ADC_INF_FLAG_USER_AGENT, PRODUCT "/" VERSION);
|
||||
|
||||
adc_cid_pid(client);
|
||||
ADC_client_send(client, client->info->cache);
|
||||
}
|
||||
|
||||
int ADC_client_create(struct ADC_client* client, const char* nickname, const char* description)
|
||||
{
|
||||
memset(client, 0, sizeof(struct ADC_client));
|
||||
|
||||
int sd = net_socket_create(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (sd == -1) return -1;
|
||||
|
||||
client->con = net_con_create();
|
||||
#if 0
|
||||
/* FIXME */
|
||||
client->timer = 0; /* FIXME: hub_malloc(sizeof(struct net_timer)); */
|
||||
#endif
|
||||
net_con_initialize(client->con, sd, event_callback, client, 0);
|
||||
#if 0
|
||||
/* FIXME */
|
||||
net_timer_initialize(client->timer, timer_callback, client);
|
||||
#endif
|
||||
ADC_client_set_state(client, ps_none);
|
||||
|
||||
client->nick = hub_strdup(nickname);
|
||||
client->desc = hub_strdup(description);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ADC_client_destroy(struct ADC_client* client)
|
||||
{
|
||||
ADC_client_disconnect(client);
|
||||
#if 0
|
||||
/* FIXME */
|
||||
net_timer_shutdown(client->timer);
|
||||
#endif
|
||||
hub_free(client->timer);
|
||||
adc_msg_free(client->info);
|
||||
hub_free(client->nick);
|
||||
hub_free(client->desc);
|
||||
hub_free(client->hub_address);
|
||||
}
|
||||
|
||||
int ADC_client_connect(struct ADC_client* client, const char* address)
|
||||
{
|
||||
if (!client->hub_address)
|
||||
{
|
||||
if (!ADC_client_parse_address(client, address))
|
||||
return 0;
|
||||
|
||||
client->callback(client, ADC_CLIENT_CONNECTING, 0);
|
||||
}
|
||||
|
||||
int ret = net_connect(net_con_get_sd(client->con), (struct sockaddr*) &client->addr, sizeof(struct sockaddr_in));
|
||||
if (ret == 0 || (ret == -1 && net_error() == EISCONN))
|
||||
{
|
||||
ADC_client_on_connected(client);
|
||||
}
|
||||
else if (ret == -1 && (net_error() == EALREADY || net_error() == EINPROGRESS || net_error() == EWOULDBLOCK || net_error() == EINTR))
|
||||
{
|
||||
if (client->state != ps_conn)
|
||||
{
|
||||
net_con_update(client->con, NET_EVENT_READ | NET_EVENT_WRITE);
|
||||
ADC_client_set_state(client, ps_conn);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ADC_client_on_disconnected(client);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void ADC_client_on_connected(struct ADC_client* client)
|
||||
{
|
||||
net_con_update(client->con, NET_EVENT_READ);
|
||||
client->callback(client, ADC_CLIENT_CONNECTED, 0);
|
||||
ADC_client_send(client, ADC_HANDSHAKE);
|
||||
ADC_client_set_state(client, ps_protocol);
|
||||
}
|
||||
|
||||
static void ADC_client_on_disconnected(struct ADC_client* client)
|
||||
{
|
||||
net_con_close(client->con);
|
||||
client->con = 0;
|
||||
ADC_client_set_state(client, ps_none);
|
||||
}
|
||||
|
||||
static void ADC_client_on_login(struct ADC_client* client)
|
||||
{
|
||||
ADC_client_set_state(client, ps_normal);
|
||||
client->callback(client, ADC_CLIENT_LOGGED_IN, 0);
|
||||
}
|
||||
|
||||
void ADC_client_disconnect(struct ADC_client* client)
|
||||
{
|
||||
if (client->con && net_con_get_sd(client->con) != -1)
|
||||
{
|
||||
net_con_close(client->con);
|
||||
client->con = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int ADC_client_parse_address(struct ADC_client* client, const char* arg)
|
||||
{
|
||||
char* split;
|
||||
int ssl = 0;
|
||||
struct hostent* dns;
|
||||
struct in_addr* addr;
|
||||
|
||||
if (!arg)
|
||||
return 0;
|
||||
|
||||
client->hub_address = hub_strdup(arg);
|
||||
|
||||
/* Minimum length of a valid address */
|
||||
if (strlen(arg) < 9)
|
||||
return 0;
|
||||
|
||||
/* Check for ADC or ADCS */
|
||||
if (!strncmp(arg, "adc://", 6))
|
||||
ssl = 0;
|
||||
else if (!strncmp(arg, "adcs://", 7))
|
||||
ssl = 1;
|
||||
else
|
||||
return 0;
|
||||
|
||||
/* Split hostname and port (if possible) */
|
||||
split = strrchr(client->hub_address + 6 + ssl, ':');
|
||||
if (split == 0 || strlen(split) < 2 || strlen(split) > 6)
|
||||
return 0;
|
||||
|
||||
/* Ensure port number is valid */
|
||||
int port = strtol(split+1, NULL, 10);
|
||||
if (port <= 0 || port > 65535)
|
||||
return 0;
|
||||
|
||||
split[0] = 0;
|
||||
|
||||
/* Resolve IP address (FIXME: blocking call) */
|
||||
dns = gethostbyname(client->hub_address + 6 + ssl);
|
||||
if (dns)
|
||||
{
|
||||
addr = (struct in_addr*) dns->h_addr_list[0];
|
||||
}
|
||||
|
||||
// Initialize the sockaddr struct.
|
||||
memset(&client->addr, 0, sizeof(client->addr));
|
||||
client->addr.sin_family = AF_INET;
|
||||
client->addr.sin_port = htons(port);
|
||||
memcpy(&client->addr.sin_addr, addr, sizeof(struct in_addr));
|
||||
return 1;
|
||||
}
|
||||
|
||||
void ADC_client_set_callback(struct ADC_client* client, adc_client_cb cb)
|
||||
{
|
||||
client->callback = cb;
|
||||
}
|
||||
129
src/tools/adcclient.h
Normal file
129
src/tools/adcclient.h
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef HAVE_UHUB_ADC_CLIENT_H
|
||||
#define HAVE_UHUB_ADC_CLIENT_H
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
#define ADC_BUFSIZE 16384
|
||||
|
||||
enum ADC_client_state
|
||||
{
|
||||
ps_none = 0x00, /* Not connected */
|
||||
ps_conn = 0x01, /* Connecting... */
|
||||
ps_protocol = 0x02, /* Have sent HSUP */
|
||||
ps_identify = 0x04, /* Have sent BINF */
|
||||
ps_verify = 0x08, /* Have sent HPAS */
|
||||
ps_normal = 0x10, /* Are fully logged in */
|
||||
};
|
||||
|
||||
struct ADC_client;
|
||||
|
||||
enum ADC_client_callback_type
|
||||
{
|
||||
ADC_CLIENT_CONNECTING = 1001,
|
||||
ADC_CLIENT_CONNECTED = 1002,
|
||||
ADC_CLIENT_DISCONNECTED = 1003,
|
||||
|
||||
ADC_CLIENT_LOGGING_IN = 2001,
|
||||
ADC_CLIENT_PASSWORD_REQ = 2002,
|
||||
ADC_CLIENT_LOGGED_IN = 2003,
|
||||
ADC_CLIENT_LOGIN_ERROR = 2004,
|
||||
|
||||
ADC_CLIENT_PROTOCOL_STATUS = 3001,
|
||||
ADC_CLIENT_MESSAGE = 3002,
|
||||
ADC_CLIENT_CONNECT_REQ = 3003,
|
||||
ADC_CLIENT_REVCONNECT_REQ = 3004,
|
||||
ADC_CLIENT_SEARCH_REQ = 3005,
|
||||
ADC_CLIENT_SEARCH_REP = 3006,
|
||||
|
||||
ADC_CLIENT_USER_JOIN = 4001,
|
||||
ADC_CLIENT_USER_QUIT = 4002,
|
||||
ADC_CLIENT_USER_UPDATE = 4003,
|
||||
|
||||
ADC_CLIENT_HUB_INFO = 5001,
|
||||
};
|
||||
|
||||
struct ADC_hub_info
|
||||
{
|
||||
char* name;
|
||||
char* description;
|
||||
char* version;
|
||||
};
|
||||
|
||||
struct ADC_chat_message
|
||||
{
|
||||
sid_t from_sid;
|
||||
sid_t to_sid;
|
||||
char* message;
|
||||
int flags;
|
||||
};
|
||||
|
||||
struct ADC_user
|
||||
{
|
||||
sid_t sid;
|
||||
char* cid;
|
||||
char* name;
|
||||
char* description;
|
||||
char* address;
|
||||
char* version;
|
||||
};
|
||||
|
||||
|
||||
struct ADC_client_callback_data
|
||||
{
|
||||
union {
|
||||
struct ADC_hub_info* hubinfo;
|
||||
struct ADC_chat_message* chat;
|
||||
struct ADC_user* user;
|
||||
};
|
||||
};
|
||||
|
||||
typedef int (*adc_client_cb)(struct ADC_client*, enum ADC_client_callback_type, struct ADC_client_callback_data* data);
|
||||
|
||||
struct ADC_client
|
||||
{
|
||||
sid_t sid;
|
||||
enum ADC_client_state state;
|
||||
struct adc_message* info;
|
||||
char recvbuf[ADC_BUFSIZE];
|
||||
char sendbuf[ADC_BUFSIZE];
|
||||
adc_client_cb callback;
|
||||
size_t s_offset;
|
||||
size_t r_offset;
|
||||
size_t timeout;
|
||||
struct net_connection* con;
|
||||
struct net_timer* timer;
|
||||
struct sockaddr_in addr;
|
||||
char* hub_address;
|
||||
char* nick;
|
||||
char* desc;
|
||||
};
|
||||
|
||||
int ADC_client_create(struct ADC_client* client, const char* nickname, const char* description);
|
||||
void ADC_client_set_callback(struct ADC_client* client, adc_client_cb);
|
||||
void ADC_client_destroy(struct ADC_client* client);
|
||||
int ADC_client_connect(struct ADC_client* client, const char* address);
|
||||
void ADC_client_disconnect(struct ADC_client* client);
|
||||
void ADC_client_send(struct ADC_client* client, char* msg);
|
||||
|
||||
#endif /* HAVE_UHUB_ADC_CLIENT_H */
|
||||
|
||||
|
||||
429
src/tools/adcrush.c
Normal file
429
src/tools/adcrush.c
Normal file
@@ -0,0 +1,429 @@
|
||||
/**
|
||||
* An ADC client emulator.
|
||||
*/
|
||||
|
||||
#include "adcclient.h"
|
||||
|
||||
#define ADC_CLIENTS_DEFAULT 100
|
||||
#define ADC_MAX_CLIENTS 25000
|
||||
#define ADC_CID_SIZE 39
|
||||
#define BIG_BUFSIZE 32768
|
||||
#define TIGERSIZE 24
|
||||
#define ADCRUSH "adcrush/0.3"
|
||||
#define ADC_NICK "[BOT]adcrush"
|
||||
#define ADC_DESC "crash\\stest\\sdummy"
|
||||
|
||||
#define LVL_INFO 1
|
||||
#define LVL_DEBUG 2
|
||||
#define LVL_VERBOSE 3
|
||||
|
||||
static const char* cfg_uri = 0; /* address */
|
||||
static int cfg_debug = 0; /* debug level */
|
||||
static int cfg_level = 1; /* activity level (0..3) */
|
||||
static int cfg_chat = 0; /* chat mode, allow sending chat messages */
|
||||
static int cfg_quiet = 0; /* quiet mode (no output) */
|
||||
static int cfg_clients = ADC_CLIENTS_DEFAULT; /* number of clients */
|
||||
static int running = 1;
|
||||
|
||||
#define MAX_CHAT_MSGS 35
|
||||
const char* chat_messages[MAX_CHAT_MSGS] = {
|
||||
"hello",
|
||||
"I'm an annoying robot, configured to chat in order to measure performance of the hub.",
|
||||
"I apologize for the inconvenience.",
|
||||
".",
|
||||
":)",
|
||||
"can anyone help me, pls?",
|
||||
"wtf?",
|
||||
"bullshit",
|
||||
"resistance is futile.",
|
||||
"You crossed the line first, sir. You squeezed them, you hammered them to the point of desperation. And in their desperation they turned to a man they didn't fully understand.",
|
||||
"beam me up, scotty",
|
||||
"morning",
|
||||
"You know where Harvey is? You know who he is?",
|
||||
"gtg",
|
||||
"thanks",
|
||||
"*punt*",
|
||||
"*nudge*",
|
||||
"that's ok",
|
||||
"...anyway",
|
||||
"hola",
|
||||
"hey",
|
||||
"hi",
|
||||
"nevermind",
|
||||
"i think so",
|
||||
"dunno",
|
||||
"debian ftw",
|
||||
"oops",
|
||||
"how do I search?",
|
||||
"how do I enable active mode?",
|
||||
"home, sweet home...",
|
||||
"later",
|
||||
"Good evening, ladies and gentlemen. We are tonight's entertainment! I only have one question. Where is Harvey Dent?",
|
||||
"You know where I can find Harvey? I need to talk to him about something. Just something, a little.",
|
||||
"We really should stop fighting, we'll miss the fireworks!",
|
||||
"Wanna know how I got these scars?",
|
||||
};
|
||||
|
||||
#define MAX_SEARCH_MSGS 10
|
||||
const char* search_messages[MAX_SEARCH_MSGS] = {
|
||||
"ANmp3 TOauto",
|
||||
"ANxxx TOauto",
|
||||
"ANdivx TOauto",
|
||||
"ANtest ANfoo TOauto",
|
||||
"ANwmv TO1289718",
|
||||
"ANbabe TO8981884",
|
||||
"ANpr0n TOauto",
|
||||
"ANmusic TOauto",
|
||||
"ANvideo TOauto",
|
||||
"ANburnout ANps3 TOauto",
|
||||
};
|
||||
|
||||
|
||||
|
||||
static void bot_output(struct ADC_client* client, int level, const char* format, ...)
|
||||
{
|
||||
char logmsg[1024];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(logmsg, 1024, format, args);
|
||||
va_end(args);
|
||||
|
||||
if (cfg_debug >= level)
|
||||
fprintf(stdout, "* [%p] %s\n", client, logmsg);
|
||||
}
|
||||
|
||||
#if 0
|
||||
static size_t get_wait_rand(size_t max)
|
||||
{
|
||||
static size_t next = 0;
|
||||
if (next == 0) next = (size_t) time(0);
|
||||
next = (next * 1103515245) + 12345;
|
||||
return ((size_t )(next / 65536) % max);
|
||||
}
|
||||
|
||||
|
||||
static void perf_result(struct ADC_client* client, sid_t target, const char* what, const char* token);
|
||||
|
||||
static void perf_chat(struct ADC_client* client, int priv)
|
||||
{
|
||||
char buf[1024] = { 0, };
|
||||
size_t r = get_wait_rand(MAX_CHAT_MSGS-1);
|
||||
char* msg = adc_msg_escape(chat_messages[r]);
|
||||
|
||||
if (priv)
|
||||
{
|
||||
strcat(buf, "EMSG ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, " ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
}
|
||||
else
|
||||
{
|
||||
strcat(buf, "BMSG ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
}
|
||||
strcat(buf, " ");
|
||||
|
||||
strcat(buf, msg);
|
||||
hub_free(msg);
|
||||
|
||||
strcat(buf, "\n");
|
||||
ADC_client_send(client, buf);
|
||||
}
|
||||
|
||||
static void perf_search(struct ADC_client* client)
|
||||
{
|
||||
char buf[1024] = { 0, };
|
||||
size_t r = get_wait_rand(MAX_SEARCH_MSGS-1);
|
||||
size_t pst = get_wait_rand(100);
|
||||
|
||||
if (pst > 80)
|
||||
{
|
||||
strcat(buf, "FSCH ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, " +TCP4 ");
|
||||
}
|
||||
else
|
||||
{
|
||||
strcat(buf, "BSCH ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, " ");
|
||||
}
|
||||
strcat(buf, search_messages[r]);
|
||||
strcat(buf, "\n");
|
||||
ADC_client_send(client, buf);
|
||||
}
|
||||
|
||||
static void perf_result(struct ADC_client* client, sid_t target, const char* what, const char* token)
|
||||
{
|
||||
char buf[1024] = { 0, };
|
||||
strcat(buf, "DRES ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, " ");
|
||||
strcat(buf, sid_to_string(target));
|
||||
strcat(buf, " FN" "test/");
|
||||
strcat(buf, what);
|
||||
strcat(buf, ".dat");
|
||||
strcat(buf, " SL" "0");
|
||||
strcat(buf, " SI" "908987128912");
|
||||
strcat(buf, " TR" "5T6YJYKO3WECS52BKWVSOP5VUG4IKNSZBZ5YHBA");
|
||||
strcat(buf, " TO");
|
||||
strcat(buf, token);
|
||||
strcat(buf, "\n");
|
||||
ADC_client_send(client, buf);
|
||||
}
|
||||
|
||||
static void perf_ctm(struct ADC_client* client)
|
||||
{
|
||||
char buf[1024] = { 0, };
|
||||
strcat(buf, "DCTM ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, " ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, " ");
|
||||
strcat(buf, "ADC/1.0");
|
||||
strcat(buf, " TOKEN111");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, "\n");
|
||||
ADC_client_send(client, buf);
|
||||
}
|
||||
|
||||
|
||||
static void perf_update(struct ADC_client* client)
|
||||
{
|
||||
char buf[1024] = { 0, };
|
||||
int n = (int) get_wait_rand(10)+1;
|
||||
|
||||
strcat(buf, "BINF ");
|
||||
strcat(buf, sid_to_string(client->sid));
|
||||
strcat(buf, " HN");
|
||||
strcat(buf, uhub_itoa(n));
|
||||
|
||||
strcat(buf, "\n");
|
||||
ADC_client_send(client, buf);
|
||||
}
|
||||
|
||||
static void perf_normal_action(struct ADC_client* client)
|
||||
{
|
||||
size_t r = get_wait_rand(5);
|
||||
size_t p = get_wait_rand(100);
|
||||
|
||||
switch (r)
|
||||
{
|
||||
case 0:
|
||||
if (p > (90 - (10 * cfg_level)))
|
||||
{
|
||||
bot_output(client, LVL_VERBOSE, "timeout -> disconnect");
|
||||
ADC_client_disconnect(client);
|
||||
}
|
||||
break;
|
||||
|
||||
case 1:
|
||||
if (cfg_chat)
|
||||
{
|
||||
bot_output(client, LVL_VERBOSE, "timeout -> chat");
|
||||
perf_chat(client, 0);
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
bot_output(client, LVL_VERBOSE, "timeout -> search");
|
||||
perf_search(client);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
bot_output(client, LVL_VERBOSE, "timeout -> update");
|
||||
perf_update(client);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
bot_output(client, LVL_VERBOSE, "timeout -> privmsg");
|
||||
perf_chat(client, 1);
|
||||
break;
|
||||
|
||||
case 5:
|
||||
bot_output(client, LVL_VERBOSE, "timeout -> ctm/rcm");
|
||||
perf_ctm(client);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static int handle(struct ADC_client* client, enum ADC_client_callback_type type, struct ADC_client_callback_data* data)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case ADC_CLIENT_CONNECTING:
|
||||
bot_output(client, LVL_DEBUG, "*** Connecting...");
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_CONNECTED:
|
||||
bot_output(client, LVL_DEBUG, "*** Connected.");
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_DISCONNECTED:
|
||||
bot_output(client, LVL_DEBUG, "*** Disconnected.");
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_LOGGING_IN:
|
||||
bot_output(client, LVL_DEBUG, "*** Logging in...");
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_PASSWORD_REQ:
|
||||
bot_output(client, LVL_DEBUG, "*** Requesting password.");
|
||||
|
||||
case ADC_CLIENT_LOGGED_IN:
|
||||
bot_output(client, LVL_DEBUG, "*** Logged in.");
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_LOGIN_ERROR:
|
||||
bot_output(client, LVL_DEBUG, "*** Login error");
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_MESSAGE:
|
||||
bot_output(client, LVL_DEBUG, " <%s> %s", sid_to_string(data->chat->from_sid), data->chat->message);
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_USER_JOIN:
|
||||
bot_output(client, LVL_VERBOSE, " JOIN: %s", data->user->name);
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_USER_QUIT:
|
||||
bot_output(client, LVL_VERBOSE, " QUIT");
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_SEARCH_REQ:
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_HUB_INFO:
|
||||
bot_output(client, LVL_DEBUG, " Hub: \"%s\" [%s]\n"
|
||||
" \"%s\"\n", data->hubinfo->name, data->hubinfo->version, data->hubinfo->description);
|
||||
break;
|
||||
|
||||
default:
|
||||
bot_output(client, LVL_DEBUG, "Not handled event=%d\n", (int) type);
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void runloop(size_t clients)
|
||||
{
|
||||
size_t n = 0;
|
||||
struct ADC_client* client[ADC_MAX_CLIENTS];
|
||||
|
||||
for (n = 0; n < clients; n++)
|
||||
{
|
||||
struct ADC_client* c = malloc(sizeof(struct ADC_client));
|
||||
client[n] = c;
|
||||
|
||||
char nick[20];
|
||||
snprintf(nick, 20, "adcrush_%d", (int) n);
|
||||
|
||||
ADC_client_create(c, nick, "stresstester");
|
||||
ADC_client_set_callback(c, handle);
|
||||
ADC_client_connect(c, cfg_uri);
|
||||
}
|
||||
|
||||
while (running)
|
||||
{
|
||||
net_backend_process();
|
||||
}
|
||||
|
||||
for (n = 0; n < clients; n++)
|
||||
{
|
||||
ADC_client_destroy(client[n]);
|
||||
free(client[n]);
|
||||
client[n] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void print_version()
|
||||
{
|
||||
printf(ADCRUSH "\n");
|
||||
printf("Copyright (C) 2008-2009, Jan Vidar Krey\n");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void print_usage(const char* program)
|
||||
{
|
||||
print_version();
|
||||
|
||||
printf("Usage: %s [adc[s]://<host>:<port>] [options]\n", program);
|
||||
|
||||
printf("\n");
|
||||
printf(" OPTIONS\n");
|
||||
printf(" -l <0-3> Level: 0=polite, 1=normal (default), 2=aggressive, 3=excessive.\n");
|
||||
printf(" -n <num> Number of concurrent connections\n");
|
||||
printf(" -c Allow broadcasting chat messages.\n");
|
||||
printf(" -d Enable debug output.\n");
|
||||
printf(" -q Quiet mode (no output).\n");
|
||||
printf("\n");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int parse_address(const char* arg)
|
||||
{
|
||||
if (!arg || strlen(arg) < 9)
|
||||
return 0;
|
||||
|
||||
if (strncmp(arg, "adc://", 6) && strncmp(arg, "adcs://", 7))
|
||||
return 0;
|
||||
|
||||
cfg_uri = arg;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int parse_arguments(int argc, char** argv)
|
||||
{
|
||||
int ok = 1;
|
||||
int opt;
|
||||
for (opt = 2; opt < argc; opt++)
|
||||
{
|
||||
if (!strcmp(argv[opt], "-c"))
|
||||
cfg_chat = 1;
|
||||
else if (!strncmp(argv[opt], "-d", 2))
|
||||
cfg_debug += strlen(argv[opt]) - 1;
|
||||
else if (!strcmp(argv[opt], "-q"))
|
||||
cfg_quiet = 1;
|
||||
else if (!strcmp(argv[opt], "-l") && (++opt) < argc)
|
||||
{
|
||||
cfg_level = MIN(MAX(uhub_atoi(argv[opt]), 0), 3);
|
||||
}
|
||||
else if (!strcmp(argv[opt], "-n") && (++opt) < argc)
|
||||
{
|
||||
cfg_clients = MIN(MAX(uhub_atoi(argv[opt]), 1), ADC_MAX_CLIENTS);
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
void parse_command_line(int argc, char** argv)
|
||||
{
|
||||
if (argc < 2 ||
|
||||
!parse_address(argv[1]) ||
|
||||
!parse_arguments(argc, argv))
|
||||
{
|
||||
print_usage(argv[0]);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
parse_command_line(argc, argv);
|
||||
|
||||
net_initialize();
|
||||
|
||||
hub_log_initialize(NULL, 0);
|
||||
hub_set_log_verbosity(1000);
|
||||
|
||||
runloop(cfg_clients);
|
||||
|
||||
net_destroy();
|
||||
return 0;
|
||||
}
|
||||
93
src/tools/admin.c
Normal file
93
src/tools/admin.c
Normal file
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* A remote uhub admin client.
|
||||
*/
|
||||
|
||||
#include "adcclient.h"
|
||||
|
||||
static int handle(struct ADC_client* client, enum ADC_client_callback_type type, struct ADC_client_callback_data* data)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case ADC_CLIENT_CONNECTING:
|
||||
puts("*** Connecting...");
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_CONNECTED:
|
||||
puts("*** Connected.");
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_DISCONNECTED:
|
||||
puts("*** Disconnected.");
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_LOGGING_IN:
|
||||
puts("*** Logging in...");
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_PASSWORD_REQ:
|
||||
puts("*** Requesting password.");
|
||||
|
||||
case ADC_CLIENT_LOGGED_IN:
|
||||
puts("*** Logged in.");
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_LOGIN_ERROR:
|
||||
puts("*** Login error");
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_MESSAGE:
|
||||
printf(" <%s> %s\n", sid_to_string(data->chat->from_sid), data->chat->message);
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_USER_JOIN:
|
||||
printf(" JOIN: %s\n", data->user->name);
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_USER_QUIT:
|
||||
printf(" QUIT\n");
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_SEARCH_REQ:
|
||||
break;
|
||||
|
||||
case ADC_CLIENT_HUB_INFO:
|
||||
printf(" Hub: \"%s\" [%s]\n"
|
||||
" \"%s\"\n", data->hubinfo->name, data->hubinfo->version, data->hubinfo->description);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("Not handled event=%d\n", (int) type);
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int running = 1;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
printf("Usage: %s adc[s]://host:port\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct ADC_client client;
|
||||
net_initialize();
|
||||
|
||||
ADC_client_create(&client, "uhub-admin", "stresstester");
|
||||
ADC_client_set_callback(&client, handle);
|
||||
ADC_client_connect(&client, argv[1]);
|
||||
|
||||
while (running)
|
||||
{
|
||||
net_backend_process();
|
||||
}
|
||||
|
||||
ADC_client_destroy(&client);
|
||||
net_destroy();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
35
src/tools/fuzzer.h
Normal file
35
src/tools/fuzzer.h
Normal file
@@ -0,0 +1,35 @@
|
||||
enum commandMode
|
||||
{
|
||||
cm_bcast = 0x01, /* B - broadcast */
|
||||
cm_dir = 0x02, /* D - direct message */
|
||||
cm_echo = 0x04, /* E - echo message */
|
||||
cm_fcast = 0x08, /* F - feature cast message */
|
||||
cm_c2h = 0x10, /* H - client to hub message */
|
||||
cm_h2c = 0x20, /* I - hub to client message */
|
||||
cm_c2c = 0x40, /* C - client to client message */
|
||||
cm_udp = 0x80, /* U - udp message (client to client) */
|
||||
};
|
||||
|
||||
enum commandValidity
|
||||
{
|
||||
cv_protocol = 0x01,
|
||||
cv_identify = 0x02,
|
||||
cv_verify = 0x04,
|
||||
cv_normal = 0x08,
|
||||
};
|
||||
|
||||
const struct commandPattern patterns[] =
|
||||
{
|
||||
{ cm_c2h | cm_c2c | cm_h2c, "SUP", cv_protocol | cv_normal }, /* protocol support */
|
||||
{ cm_bcast | cm_h2c | cm_c2c, "INF", cv_identify | cv_verify | cv_normal }, /* info message */
|
||||
{ cm_bcast | cm_h2c | cm_c2c | cm_c2h | cm_udp, "STA", cv_protocol | cv_identify | cv_verify | cv_normal }, /* status message */
|
||||
{ cm_bcast | cm_dir | cm_echo | cm_h2c, "MSG", cv_normal }, /* chat message */
|
||||
{ cm_bcast | cm_dir | cm_echo | cm_fcast, "SCH", cv_normal }, /* search */
|
||||
{ cm_dir | cm_udp, "RES", cv_normal }, /* search result */
|
||||
{ cm_dir | cm_echo, "CTM", cv_normal }, /* connect to me */
|
||||
{ cm_dir | cm_echo, "RCM", cv_normal }, /* reversed, connect to me */
|
||||
{ cm_h2c, "QUI", cv_normal }, /* quit message */
|
||||
{ cm_h2c, "GPA", cv_identify }, /* password request */
|
||||
{ cm_c2h, "PAS", cv_verify } /* password response */
|
||||
};
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user