diff options
author | Test_User <hax@andrewyu.org> | 2024-06-18 02:18:46 -0400 |
---|---|---|
committer | Test_User <hax@andrewyu.org> | 2024-06-18 02:18:46 -0400 |
commit | c82ee1c2cdf492a05e4bf3d3d151205e50c9b603 (patch) | |
tree | d8e71d3c7b7c03bd512d427ab93b4c75ca4f990f | |
parent | f67a7bd24b2e655c21ffdc6160d663d8d5ba5be6 (diff) | |
download | haxircd-c82ee1c2cdf492a05e4bf3d3d151205e50c9b603.tar.gz haxircd-c82ee1c2cdf492a05e4bf3d3d151205e50c9b603.zip |
Optional futex support, increased spam limit
Diffstat (limited to '')
-rw-r--r-- | Makefile | 19 | ||||
-rw-r--r-- | gnutls_network.c | 31 | ||||
-rw-r--r-- | main.h | 5 | ||||
-rw-r--r-- | mutex.c | 34 | ||||
-rw-r--r-- | mutex.h | 83 | ||||
-rw-r--r-- | openssl_network.c | 31 | ||||
-rw-r--r-- | protocols/inspircd2.c | 15 | ||||
-rw-r--r-- | protocols/inspircd3.c | 15 | ||||
-rw-r--r-- | pseudoclients/haxserv.c | 2 | ||||
-rw-r--r-- | real_main.c | 9 |
10 files changed, 193 insertions, 51 deletions
@@ -45,6 +45,7 @@ LDFLAGS = -lpthread printf '%s\n' 'LAST_INSPIRCD3_PROTOCOL = $(INSPIRCD3_PROTOCOL)' >> .makeopts printf '%s\n' 'LAST_HAXSERV_PSEUDOCLIENT = $(HAXSERV_PSEUDOCLIENT)' >> .makeopts printf '%s\n' 'LAST_SAFE_STACK = $(SAFE_STACK)' >> .makeopts + printf '%s\n' 'LAST_FUTEX = $(FUTEX)' >> .makeopts printf '%s\n' 'LAST_CFLAGS = $(ORIGINAL_CFLAGS)' >> .makeopts printf '%s\n' 'LAST_CC = $(CC)' >> .makeopts @@ -150,6 +151,14 @@ else CC = $(LAST_CC) endif +ifneq ($(FUTEX),) +ifneq ($(FUTEX),$(LAST_FUTEX)) +rebuild = 1 +endif +else +FUXEX = $(LAST_FUTEX) +endif + ifeq ($(rebuild),1) .PHONY: .makeopts endif @@ -159,7 +168,7 @@ USE_CLIENT = 0 USE_GNUTLS = 0 USE_SERVER = 0 -OFILES = config.o general_network.o haxstring_utils.o real_main.o table.o +OFILES = config.o general_network.o haxstring_utils.o real_main.o table.o mutex.o SOFILES = HaxIRCd.so ifeq ($(PLAINTEXT_CLIENT),1) @@ -270,6 +279,12 @@ endif +ifeq ($(FUTEX),1) +CFLAGS += -DUSE_FUTEX +endif + + + ifeq ($(SAFE_STACK),1) CFLAGS += -fstack-check endif @@ -304,6 +319,8 @@ $(call DEPS,real_main,o) $(call DEPS,main,o) +$(call DEPS,mutex,o) + $(call DEPS,protocols,o) $(call DEPS,table,o) diff --git a/gnutls_network.c b/gnutls_network.c index be1e569..659d934 100644 --- a/gnutls_network.c +++ b/gnutls_network.c @@ -41,10 +41,11 @@ #include "general_network.h" #include "gnutls_network.h" #include "main.h" +#include "mutex.h" struct gnutls_handle { gnutls_session_t session; - pthread_mutex_t mutex; + MUTEX_TYPE mutex; int fd; char valid; }; @@ -73,7 +74,7 @@ int gnutls_send(void *handle, struct string msg) { struct gnutls_handle *gnutls_handle = handle; - pthread_mutex_lock(&(gnutls_handle->mutex)); + mutex_lock(&(gnutls_handle->mutex)); if (!gnutls_handle->valid) goto gnutls_send_error_unlock; @@ -120,15 +121,15 @@ int gnutls_send(void *handle, struct string msg) { goto gnutls_send_error_unlock; } while (1); - pthread_mutex_unlock(&(gnutls_handle->mutex)); + mutex_unlock(&(gnutls_handle->mutex)); return 0; gnutls_send_error_unlock: if (gnutls_handle->valid) { - pthread_mutex_unlock(&(gnutls_handle->mutex)); + mutex_unlock(&(gnutls_handle->mutex)); gnutls_shutdown(gnutls_handle); } else { - pthread_mutex_unlock(&(gnutls_handle->mutex)); + mutex_unlock(&(gnutls_handle->mutex)); } return 1; } @@ -142,16 +143,16 @@ size_t gnutls_recv(void *handle, char *data, size_t len, char *err) { ssize_t gnutls_res; do { int poll_res; - pthread_mutex_lock(&(gnutls_handle->mutex)); + mutex_lock(&(gnutls_handle->mutex)); if (!gnutls_handle->valid) { - pthread_mutex_unlock(&(gnutls_handle->mutex)); + mutex_unlock(&(gnutls_handle->mutex)); *err = 3; return 0; } do { gnutls_res = gnutls_record_recv(gnutls_handle->session, data, len); } while (gnutls_res == GNUTLS_E_INTERRUPTED); - pthread_mutex_unlock(&(gnutls_handle->mutex)); + mutex_unlock(&(gnutls_handle->mutex)); if (gnutls_res < 0) { if (gnutls_res == GNUTLS_E_AGAIN) { pollfd.events = POLLIN | POLLOUT; @@ -206,7 +207,7 @@ int gnutls_connect(void **handle, struct string address, struct string port, str *handle = gnutls_handle; - int res = pthread_mutex_init(&(gnutls_handle->mutex), &pthread_mutexattr); + int res = mutex_init(&(gnutls_handle->mutex)); if (res != 0) goto gnutls_connect_free_gnutls_handle; @@ -301,7 +302,7 @@ int gnutls_connect(void **handle, struct string address, struct string port, str gnutls_connect_close: close(fd); gnutls_connect_destroy_mutex: - pthread_mutex_destroy(&(gnutls_handle->mutex)); + mutex_destroy(&(gnutls_handle->mutex)); gnutls_connect_free_gnutls_handle: free(gnutls_handle); @@ -338,7 +339,7 @@ int gnutls_accept(int listen_fd, void **handle, struct string *addr) { gnutls_handle->valid = 1; gnutls_handle->fd = con_fd; - int res = pthread_mutex_init(&(gnutls_handle->mutex), &(pthread_mutexattr)); + int res = mutex_init(&(gnutls_handle->mutex)); if (res != 0) goto gnutls_accept_free_gnutls_handle; @@ -410,7 +411,7 @@ int gnutls_accept(int listen_fd, void **handle, struct string *addr) { gnutls_accept_free_addr_data: free(addr->data); gnutls_accept_destroy_mutex: - pthread_mutex_destroy(&(gnutls_handle->mutex)); + mutex_destroy(&(gnutls_handle->mutex)); gnutls_accept_free_gnutls_handle: free(gnutls_handle); gnutls_accept_close: @@ -420,15 +421,15 @@ int gnutls_accept(int listen_fd, void **handle, struct string *addr) { void gnutls_shutdown(void *handle) { struct gnutls_handle *gnutls_handle = handle; - pthread_mutex_lock(&(gnutls_handle->mutex)); + mutex_lock(&(gnutls_handle->mutex)); shutdown(gnutls_handle->fd, SHUT_RDWR); gnutls_handle->valid = 0; - pthread_mutex_unlock(&(gnutls_handle->mutex)); + mutex_unlock(&(gnutls_handle->mutex)); } void gnutls_close(int fd, void *handle) { struct gnutls_handle *gnutls_handle = handle; - pthread_mutex_destroy(&(gnutls_handle->mutex)); + mutex_destroy(&(gnutls_handle->mutex)); gnutls_deinit(gnutls_handle->session); free(gnutls_handle); close(fd); @@ -26,9 +26,10 @@ #pragma once +#include "mutex.h" + extern pthread_attr_t pthread_attr; -extern pthread_mutexattr_t pthread_mutexattr; -extern pthread_mutex_t state_lock; +extern MUTEX_TYPE state_lock; int main(void); @@ -0,0 +1,34 @@ +// One of the code files for HaxServ +// +// Written by: Test_User <hax@andrewyu.org> +// +// This is free and unencumbered software released into the public +// domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +#include "mutex.h" + +#ifdef USE_FUTEX +#else +pthread_mutexattr_t pthread_mutexattr; +#endif @@ -0,0 +1,83 @@ +// Written by: Test_User <hax@andrewyu.org> +// +// This is free and unencumbered software released into the public +// domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +#pragma once + +#ifdef USE_FUTEX + +#include <unistd.h> +#include <sys/syscall.h> +#include <stdint.h> +#include <linux/futex.h> + +#define SETUP_MUTEX() 0 +#define MUTEX_TYPE uint32_t + +inline int mutex_init(uint32_t *futex) { + *futex = 0; + return 0; +} + +inline void mutex_lock(uint32_t *futex) { + uint32_t val; + while (val = __sync_lock_test_and_set(futex, 1)) + syscall(SYS_futex, futex, FUTEX_WAIT, val, 0, 0, 0); +} + +inline void mutex_unlock(uint32_t *futex) { + __sync_lock_release(futex); + syscall(SYS_futex, futex, FUTEX_WAKE, 1, 0, 0, 0); +} + +inline void mutex_destroy(uint32_t *futex) { + return; +} + +#else + +#include <pthread.h> + +#define SETUP_MUTEX() pthread_mutexattr_init(&pthread_mutexattr) +#define MUTEX_TYPE pthread_mutex_t + +extern pthread_mutexattr_t pthread_mutexattr; +inline int mutex_init(pthread_mutex_t *mutex) { + return pthread_mutex_init(mutex, &pthread_mutexattr); +} + +inline void mutex_lock(pthread_mutex_t *mutex) { + pthread_mutex_lock(mutex); +} + +inline void mutex_unlock(pthread_mutex_t *mutex) { + pthread_mutex_unlock(mutex); +} + +inline void mutex_destroy(pthread_mutex_t *mutex) { + pthread_mutex_destroy(mutex); +} + +#endif diff --git a/openssl_network.c b/openssl_network.c index ca59c82..b8bd462 100644 --- a/openssl_network.c +++ b/openssl_network.c @@ -37,11 +37,12 @@ #include "config.h" #include "main.h" +#include "mutex.h" #include "openssl_network.h" struct openssl_handle { SSL *ssl; - pthread_mutex_t mutex; + MUTEX_TYPE mutex; int fd; char valid; }; @@ -74,7 +75,7 @@ int openssl_send(void *handle, struct string msg) { struct openssl_handle *openssl_handle = handle; - pthread_mutex_lock(&(openssl_handle->mutex)); + mutex_lock(&(openssl_handle->mutex)); if (!openssl_handle->valid) goto openssl_send_error_unlock; @@ -111,15 +112,15 @@ int openssl_send(void *handle, struct string msg) { goto openssl_send_error_unlock; } while (1); - pthread_mutex_unlock(&(openssl_handle->mutex)); + mutex_unlock(&(openssl_handle->mutex)); return 0; openssl_send_error_unlock: if (openssl_handle->valid) { - pthread_mutex_unlock(&(openssl_handle->mutex)); + mutex_unlock(&(openssl_handle->mutex)); openssl_shutdown(handle); } else { - pthread_mutex_unlock(&(openssl_handle->mutex)); + mutex_unlock(&(openssl_handle->mutex)); } return 1; } @@ -132,14 +133,14 @@ size_t openssl_recv(void *handle, char *data, size_t len, char *err) { }; int res; do { - pthread_mutex_lock(&(openssl_handle->mutex)); + mutex_lock(&(openssl_handle->mutex)); if (!openssl_handle->valid) { - pthread_mutex_unlock(&(openssl_handle->mutex)); + mutex_unlock(&(openssl_handle->mutex)); *err = 3; return 0; } res = SSL_read(openssl_handle->ssl, data, len); - pthread_mutex_unlock(&(openssl_handle->mutex)); + mutex_unlock(&(openssl_handle->mutex)); if (res <= 0) { switch(SSL_get_error(openssl_handle->ssl, res)) { case SSL_ERROR_WANT_READ: @@ -234,7 +235,7 @@ int openssl_connect(void **handle, struct string address, struct string port, st goto openssl_connect_free_openssl_handle; SSL_set_fd(openssl_handle->ssl, fd); - res = pthread_mutex_init(&(openssl_handle->mutex), &(pthread_mutexattr)); + res = mutex_init(&(openssl_handle->mutex)); if (res != 0) goto openssl_connect_free_ssl; @@ -279,7 +280,7 @@ int openssl_connect(void **handle, struct string address, struct string port, st return fd; openssl_connect_destroy_mutex: - pthread_mutex_destroy(&(openssl_handle->mutex)); + mutex_destroy(&(openssl_handle->mutex)); openssl_connect_free_ssl: SSL_free(openssl_handle->ssl); openssl_connect_free_openssl_handle: @@ -335,7 +336,7 @@ int openssl_accept(int listen_fd, void **handle, struct string *addr) { SSL_set_fd(openssl_handle->ssl, con_fd); - int res = pthread_mutex_init(&(openssl_handle->mutex), &(pthread_mutexattr)); + int res = mutex_init(&(openssl_handle->mutex)); if (res != 0) goto openssl_accept_free_ssl; @@ -380,7 +381,7 @@ int openssl_accept(int listen_fd, void **handle, struct string *addr) { return con_fd; openssl_accept_destroy_mutex: - pthread_mutex_destroy(&(openssl_handle->mutex)); + mutex_destroy(&(openssl_handle->mutex)); openssl_accept_free_ssl: SSL_free(openssl_handle->ssl); openssl_accept_free_openssl_handle: @@ -395,15 +396,15 @@ int openssl_accept(int listen_fd, void **handle, struct string *addr) { void openssl_shutdown(void *handle) { struct openssl_handle *openssl_handle = handle; - pthread_mutex_lock(&(openssl_handle->mutex)); + mutex_lock(&(openssl_handle->mutex)); shutdown(openssl_handle->fd, SHUT_RDWR); openssl_handle->valid = 0; - pthread_mutex_unlock(&(openssl_handle->mutex)); + mutex_unlock(&(openssl_handle->mutex)); } void openssl_close(int fd, void *handle) { struct openssl_handle *openssl_handle = handle; - pthread_mutex_destroy(&(openssl_handle->mutex)); + mutex_destroy(&(openssl_handle->mutex)); SSL_free(openssl_handle->ssl); free(openssl_handle); close(fd); diff --git a/protocols/inspircd2.c b/protocols/inspircd2.c index 8111742..6c16598 100644 --- a/protocols/inspircd2.c +++ b/protocols/inspircd2.c @@ -38,6 +38,7 @@ #include "../haxstring.h" #include "../haxstring_utils.h" #include "../main.h" +#include "../mutex.h" #include "../server_network.h" #include "inspircd2.h" @@ -216,7 +217,7 @@ void * inspircd2_protocol_connection(void *type) { goto inspircd2_protocol_handle_connection_close; timeout++; - pthread_mutex_lock(&(state_lock)); + mutex_lock(&(state_lock)); networks[net].send(handle, STRING(":")); networks[net].send(handle, SID); networks[net].send(handle, STRING(" PING ")); @@ -229,7 +230,7 @@ void * inspircd2_protocol_connection(void *type) { server->awaiting_pong = 1; gettimeofday(&(server->last_ping), 0); - pthread_mutex_unlock(&(state_lock)); + mutex_unlock(&(state_lock)); } else { goto inspircd2_protocol_handle_connection_close; } @@ -374,7 +375,7 @@ void * inspircd2_protocol_connection(void *type) { i++; } - pthread_mutex_lock(&state_lock); + mutex_lock(&state_lock); if (source.len != 0) { struct server_info *server; @@ -423,7 +424,7 @@ void * inspircd2_protocol_connection(void *type) { WRITES(2, STRING("\n")); do_trivial_reloads(); - pthread_mutex_unlock(&state_lock); + mutex_unlock(&state_lock); memmove(full_msg.data, full_msg.data + msg_len + 1, full_msg.len - msg_len - 1); full_msg.len -= msg_len + 1; void *tmp = realloc(full_msg.data, full_msg.len); @@ -433,14 +434,14 @@ void * inspircd2_protocol_connection(void *type) { } inspircd2_protocol_handle_connection_unlock_close: - pthread_mutex_unlock(&state_lock); + mutex_unlock(&state_lock); inspircd2_protocol_handle_connection_close: free(full_msg.data); if (ready) { - pthread_mutex_lock(&(state_lock)); + mutex_lock(&(state_lock)); unlink_server(config->sid, get_table_index(server_list, config->sid), self, INSPIRCD2_PROTOCOL); - pthread_mutex_unlock(&(state_lock)); + mutex_unlock(&(state_lock)); } networks[net].close(fd, handle); diff --git a/protocols/inspircd3.c b/protocols/inspircd3.c index 3c74a79..864b0d1 100644 --- a/protocols/inspircd3.c +++ b/protocols/inspircd3.c @@ -38,6 +38,7 @@ #include "../haxstring.h" #include "../haxstring_utils.h" #include "../main.h" +#include "../mutex.h" #include "../server_network.h" #include "inspircd3.h" @@ -211,7 +212,7 @@ void * inspircd3_protocol_connection(void *type) { goto inspircd3_protocol_handle_connection_close; timeout++; - pthread_mutex_lock(&(state_lock)); + mutex_lock(&(state_lock)); networks[net].send(handle, STRING(":")); networks[net].send(handle, SID); networks[net].send(handle, STRING(" PING :")); @@ -222,7 +223,7 @@ void * inspircd3_protocol_connection(void *type) { server->awaiting_pong = 1; gettimeofday(&(server->last_ping), 0); - pthread_mutex_unlock(&(state_lock)); + mutex_unlock(&(state_lock)); } else { goto inspircd3_protocol_handle_connection_close; } @@ -381,7 +382,7 @@ void * inspircd3_protocol_connection(void *type) { i++; } - pthread_mutex_lock(&state_lock); + mutex_lock(&state_lock); if (source.len != 0) { struct server_info *server; @@ -430,7 +431,7 @@ void * inspircd3_protocol_connection(void *type) { WRITES(2, STRING("\n")); do_trivial_reloads(); - pthread_mutex_unlock(&state_lock); + mutex_unlock(&state_lock); memmove(full_msg.data, full_msg.data + msg_len + 1, full_msg.len - msg_len - 1); full_msg.len -= msg_len + 1; void *tmp = realloc(full_msg.data, full_msg.len); @@ -440,14 +441,14 @@ void * inspircd3_protocol_connection(void *type) { } inspircd3_protocol_handle_connection_unlock_close: - pthread_mutex_unlock(&state_lock); + mutex_unlock(&state_lock); inspircd3_protocol_handle_connection_close: free(full_msg.data); if (ready) { - pthread_mutex_lock(&(state_lock)); + mutex_lock(&(state_lock)); unlink_server(config->sid, get_table_index(server_list, config->sid), self, INSPIRCD3_PROTOCOL); - pthread_mutex_unlock(&(state_lock)); + mutex_unlock(&(state_lock)); } networks[net].close(fd, handle); diff --git a/pseudoclients/haxserv.c b/pseudoclients/haxserv.c index 797857a..677a10b 100644 --- a/pseudoclients/haxserv.c +++ b/pseudoclients/haxserv.c @@ -494,7 +494,7 @@ int haxserv_pseudoclient_spam_command(struct string from, struct string sender, size_t count = str_to_unsigned(argv[0], &err); if (err) { notice(SID, HAXSERV_UID, respond_to, STRING("Unknown number.")); - } else if (count > 5000) { + } else if (count > 50000) { notice(SID, HAXSERV_UID, respond_to, STRING("Number exceeds the limit.")); } diff --git a/real_main.c b/real_main.c index f12ee6b..2b6d71a 100644 --- a/real_main.c +++ b/real_main.c @@ -32,6 +32,7 @@ #include "config.h" #include "general_network.h" #include "main.h" +#include "mutex.h" #ifdef USE_PLAINTEXT #include "plaintext_network.h" @@ -59,11 +60,13 @@ #endif pthread_attr_t pthread_attr; -pthread_mutexattr_t pthread_mutexattr; -pthread_mutex_t state_lock = PTHREAD_MUTEX_INITIALIZER; +MUTEX_TYPE state_lock; int real_main(void) { + if (mutex_init(&state_lock) != 0) + return 1; + if (init_general_network() != 0) return 1; @@ -107,7 +110,7 @@ int real_main(void) { if (pthread_attr_init(&pthread_attr) != 0) return 1; - if (pthread_mutexattr_init(&pthread_mutexattr) != 0) + if (SETUP_MUTEX() != 0) return 1; if (pthread_attr_setdetachstate(&pthread_attr, PTHREAD_CREATE_DETACHED) != 0) // shouldn't actually happen |