aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2016-01-13 20:10:05 -0500
committerFranklin Wei <git@fwei.tk>2016-01-13 20:11:03 -0500
commitabe9f416501c5354be3ce292b4220000edf021ab (patch)
treeac8b809e18683ea9eefff58c744c6613b8805a0d
parentcc9c177672edcc65933b15ba91831bf09bbec023 (diff)
downloadnetcosm-abe9f416501c5354be3ce292b4220000edf021ab.zip
netcosm-abe9f416501c5354be3ce292b4220000edf021ab.tar.gz
netcosm-abe9f416501c5354be3ce292b4220000edf021ab.tar.bz2
netcosm-abe9f416501c5354be3ce292b4220000edf021ab.tar.xz
WIP on master: d274aa8 rewrite everything to use libev
-rw-r--r--Makefile2
-rw-r--r--src/client.c14
-rw-r--r--src/server.c29
-rw-r--r--src/server_reqs.c11
-rw-r--r--src/telnet.h2
-rw-r--r--src/test.c21
-rw-r--r--src/userdb.c2
7 files changed, 44 insertions, 37 deletions
diff --git a/Makefile b/Makefile
index 211a81f..6b5b0b3 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ PLATFORM = unix
NETCOSM_SRC = $(shell cat SOURCES)
NETCOSM_OBJ := $(NETCOSM_SRC:.c=.o)
-CFLAGS = -O3 -g -I src/ -I target/$(PLATFORM) -Wall -Wextra -Wshadow -std=gnu99
+CFLAGS = -O3 -g -I src/ -I target/$(PLATFORM) -Wall -Wextra -Wshadow -std=gnu99 -fno-strict-aliasing
LDFLAGS = -lgcrypt -lev
HEADERS = src/netcosm.h src/hash.h src/telnet.h src/userdb.h
diff --git a/src/client.c b/src/client.c
index 77072e2..7b4a9cc 100644
--- a/src/client.c
+++ b/src/client.c
@@ -26,6 +26,8 @@ static room_id current_room = 0;
static volatile sig_atomic_t output_locked = 0;
+char *current_user = NULL;
+
void out_raw(const unsigned char *buf, size_t len)
{
try_again:
@@ -374,8 +376,6 @@ auth:
int failures = 0;
int authlevel;
-
- char *current_user;
struct userdata_t *current_data = NULL;
client_change_state(STATE_AUTH);
@@ -409,6 +409,7 @@ auth:
{
client_change_state(STATE_FAILED);
free(current_user);
+ current_user = NULL;
out("Access Denied.\n\n");
if(++failures >= MAX_FAILURES)
return;
@@ -550,12 +551,18 @@ auth:
/* weird pointer voodoo */
/* TODO: simplify */
char pidbuf[MAX(sizeof(pid_t), MSG_MAX)];
- pid_t pid = strtol(pid_s, NULL, 0);
+ char *end;
+ pid_t pid = strtol(pid_s, &end, 0);
if(pid == getpid())
{
out("You cannot kick yourself. Use EXIT instead.\n");
goto next_cmd;
}
+ else if(*end != '\0')
+ {
+ out("Expected a child PID after KICK.\n");
+ goto next_cmd;
+ }
memcpy(pidbuf, &pid, sizeof(pid));
int len = sizeof(pid_t) + snprintf(pidbuf + sizeof(pid_t),
sizeof(pidbuf) - sizeof(pid_t),
@@ -624,4 +631,5 @@ auth:
done:
free(current_user);
+ current_user = NULL;
}
diff --git a/src/server.c b/src/server.c
index 53d627e..b04e7d1 100644
--- a/src/server.c
+++ b/src/server.c
@@ -19,7 +19,7 @@
#include "netcosm.h"
#define PORT 1234
-#define BACKLOG 16
+#define BACKLOG 100
/* global data */
bool are_child = false;
@@ -49,7 +49,17 @@ static void free_child_data(void *ptr)
{
struct child_data *child = ptr;
if(child->user)
+ {
free(child->user);
+ child->user = NULL;
+ }
+ if(child->io_watcher)
+ {
+ ev_io_stop(child->loop, child->io_watcher);
+
+ free(child->io_watcher);
+ child->io_watcher = NULL;
+ }
free(ptr);
}
@@ -61,9 +71,7 @@ static void handle_disconnects(void)
while((pid = waitpid(-1, NULL, WNOHANG)) > 0)
{
sig_debugf("Client disconnect.\n");
- struct child_data *child = hash_lookup(child_map, &pid);
- ev_io_stop(child->loop, child->io_watcher);
- free(child->io_watcher);
+ //struct child_data *child = hash_lookup(child_map, &pid);
--num_clients;
@@ -91,18 +99,22 @@ static void __attribute__((noreturn)) serv_cleanup(void)
if(shutdown(server_socket, SHUT_RDWR) > 0)
error("shutdown");
+
close(server_socket);
world_free();
-
reqmap_free();
-
hash_free(child_map);
child_map = NULL;
- ev_default_destroy();
userdb_shutdown();
+ extern char *current_user;
+ if(current_user)
+ free(current_user);
+
+ ev_default_destroy();
+
_exit(0);
}
@@ -334,13 +346,14 @@ int main(int argc, char *argv[])
debugf("Listening on port %d\n", port);
- struct ev_loop *loop = ev_default_loop(0);
+ struct ev_loop *loop = EV_DEFAULT;
/* set up signal handlers AFTER creating the default loop, because it will grab SIGCHLD */
init_signals();
ev_io server_watcher;
ev_io_init(&server_watcher, new_connection_cb, server_socket, EV_READ);
+ //ev_set_priority(&server_watcher, EV_MAXPRI);
ev_io_start(EV_A_ &server_watcher);
ev_loop(loop, 0);
diff --git a/src/server_reqs.c b/src/server_reqs.c
index 10f6ad3..cdafe7b 100644
--- a/src/server_reqs.c
+++ b/src/server_reqs.c
@@ -210,6 +210,15 @@ static void req_add_user(unsigned char *data, size_t datalen, struct child_data
write(sender->outpipe[1], &success, sizeof(success));
}
+static void req_send_geninfo(unsigned char *data, size_t datalen, struct child_data *sender)
+{
+ (void) data;
+ (void) datalen;
+ char buf[128];
+ int len = snprintf(buf, sizeof(buf), "Total clients: %d\n", num_clients);
+ write(sender->outpipe[1], buf, len);
+}
+
static const struct child_request {
unsigned char code;
@@ -229,7 +238,7 @@ static const struct child_request {
} requests[] = {
{ REQ_NOP, false, CHILD_NONE, NULL, NULL, REQ_NOP },
{ REQ_BCASTMSG, true, CHILD_ALL, req_pass_msg, NULL, REQ_BCASTMSG },
- { REQ_LISTCLIENTS, false, CHILD_ALL, req_send_clientinfo, NULL, REQ_BCASTMSG },
+ { REQ_LISTCLIENTS, false, CHILD_ALL, req_send_clientinfo, req_send_geninfo, REQ_BCASTMSG },
{ REQ_CHANGESTATE, true, CHILD_SENDER, req_change_state, NULL, REQ_NOP },
{ REQ_CHANGEUSER, true, CHILD_SENDER, req_change_user, NULL, REQ_NOP },
{ REQ_KICK, true, CHILD_ALL, req_kick_client, NULL, REQ_NOP },
diff --git a/src/telnet.h b/src/telnet.h
index 673ad1c..f54643f 100644
--- a/src/telnet.h
+++ b/src/telnet.h
@@ -45,7 +45,7 @@ void telnet_init(void);
#define TELNET_OK 0
#define TELNET_EXIT 1
-/* returns either 0 or TELNET_EXIT */
+/* returns either TELNET_OK or TELNET_EXIT */
int telnet_handle_command(const unsigned char*);
void telnet_echo_on(void);
diff --git a/src/test.c b/src/test.c
deleted file mode 100644
index b194352..0000000
--- a/src/test.c
+++ /dev/null
@@ -1,21 +0,0 @@
-#include "hash.h"
-#include <string.h>
-#include <stdio.h>
-
-int main()
-{
- void *map = hash_init(10000, hash_djb, compare_strings);
- hash_insert(map, "a",1);
- hash_insert(map, "b",2);
- hash_resize(map, 2);
- void *ptr = map, *save, *key;
- while(1)
- {
- void *data = hash_iterate(ptr, &save, &key);
- ptr = NULL;
- if(data)
- printf("%s %d\n", key, data);
- else
- break;
- }
-}
diff --git a/src/userdb.c b/src/userdb.c
index 749115c..ddf1ced 100644
--- a/src/userdb.c
+++ b/src/userdb.c
@@ -105,10 +105,8 @@ struct userdata_t *userdb_add(struct userdata_t *data)
void userdb_shutdown(void)
{
- debugf("shutting down userdb with %d entries\n", hash_size(map));
if(map)
{
- debugf("shutting down userdb\n");
hash_free(map);
map = NULL;
}