diff options
author | Test_User <hax@andrewyu.org> | 2024-06-12 19:04:16 -0400 |
---|---|---|
committer | Test_User <hax@andrewyu.org> | 2024-06-12 19:04:16 -0400 |
commit | a785916b1840dca3acec39d17f454a00975282a0 (patch) | |
tree | 338109f8ab63dfa6787ccf65a9557fd39a13776d | |
parent | 47d7e30f6acb1c170c8e5e15f2656b89a33384f7 (diff) | |
download | haxircd-a785916b1840dca3acec39d17f454a00975282a0.tar.gz haxircd-a785916b1840dca3acec39d17f454a00975282a0.zip |
str_clone finally, as well as some other stuff
-rw-r--r-- | general_network.c | 117 | ||||
-rw-r--r-- | general_network.h | 3 | ||||
-rw-r--r-- | haxstring_utils.c | 11 | ||||
-rw-r--r-- | haxstring_utils.h | 2 | ||||
-rw-r--r-- | protocols/inspircd2.c | 23 | ||||
-rw-r--r-- | server_network.c | 17 | ||||
-rw-r--r-- | table.c | 10 |
7 files changed, 70 insertions, 113 deletions
diff --git a/general_network.c b/general_network.c index 6df6ba8..4012687 100644 --- a/general_network.c +++ b/general_network.c @@ -35,6 +35,8 @@ #include "config.h" #include "general_network.h" +#include "haxstring.h" +#include "haxstring_utils.h" #ifdef USE_PLAINTEXT #include "plaintext_network.h" @@ -144,49 +146,20 @@ int init_general_network(void) { struct server_info *own_info; own_info = malloc(sizeof(*own_info)); - if (!own_info) { - free(server_list.array); - return 1; - } + if (!own_info) + goto init_general_network_free_server_list; - own_info->sid.data = malloc(SID.len); - if (!own_info->sid.data) { - free(server_list.array); - free(own_info); - return 1; - } - memcpy(own_info->sid.data, SID.data, SID.len); - own_info->sid.len = SID.len; - - own_info->name.data = malloc(SERVER_NAME.len); - if (!own_info->name.data) { - free(server_list.array); - free(own_info->sid.data); - free(own_info); - return 1; - } - memcpy(own_info->name.data, SERVER_NAME.data, SERVER_NAME.len); - own_info->name.len = SERVER_NAME.len; - - own_info->fullname.data = malloc(SERVER_FULLNAME.len); - if (!own_info->fullname.data) { - free(server_list.array); - free(own_info->name.data); - free(own_info->sid.data); - free(own_info); - return 1; - } - memcpy(own_info->fullname.data, SERVER_FULLNAME.data, SERVER_FULLNAME.len); - own_info->fullname.len = SERVER_FULLNAME.len; - - if (set_table_index(&server_list, SID, own_info) != 0) { - free(server_list.array); - free(own_info->fullname.data); - free(own_info->name.data); - free(own_info->sid.data); - free(own_info); - return 1; - } + if (str_clone(&(own_info->sid), SID) != 0) + goto init_general_network_free_own_info; + + if (str_clone(&(own_info->name), SERVER_NAME) != 0) + goto init_general_network_free_sid; + + if (str_clone(&(own_info->fullname), SERVER_FULLNAME) != 0) + goto init_general_network_free_name; + + if (set_table_index(&server_list, SID, own_info) != 0) + goto init_general_network_free_fullname; own_info->next = SID; own_info->connected_to = (struct table){.array = malloc(0), .len = 0}; @@ -198,6 +171,19 @@ int init_general_network(void) { user_list.array = malloc(0); return 0; + + init_general_network_free_fullname: + free(own_info->fullname.data); + init_general_network_free_name: + free(own_info->name.data); + init_general_network_free_sid: + free(own_info->sid.data); + init_general_network_free_own_info: + free(own_info); + init_general_network_free_server_list: + free(server_list.array); + + return 1; } int add_user(struct string from, struct string attached_to, struct string uid, struct string nick, struct string fullname, struct string ident, struct string vhost, struct string host, struct string address, size_t user_ts, size_t nick_ts, void *handle, size_t protocol, size_t net) { @@ -222,47 +208,32 @@ int add_user(struct string from, struct string attached_to, struct string uid, s new_info->server = attached->sid; - new_info->uid.data = malloc(uid.len); - if (!new_info->uid.data) + if (unsigned_to_str(user_ts, &(new_info->user_ts_str)) != 0) goto add_user_free_info; - memcpy(new_info->uid.data, uid.data, uid.len); - new_info->uid.len = uid.len; - new_info->nick.data = malloc(nick.len); - if (!new_info->nick.data) + if (unsigned_to_str(nick_ts, &(new_info->nick_ts_str)) != 0) + goto add_user_free_user_ts; + + if (str_clone(&(new_info->uid), uid) != 0) + goto add_user_free_nick_ts; + + if (str_clone(&(new_info->nick), nick) != 0) goto add_user_free_uid; - memcpy(new_info->nick.data, nick.data, nick.len); - new_info->nick.len = nick.len; - new_info->fullname.data = malloc(fullname.len); - if (!new_info->fullname.data && fullname.len != 0) + if (str_clone(&(new_info->fullname), fullname) != 0) goto add_user_free_nick; - memcpy(new_info->fullname.data, fullname.data, fullname.len); - new_info->fullname.len = fullname.len; - new_info->ident.data = malloc(ident.len); - if (!new_info->ident.data) + if (str_clone(&(new_info->ident), ident) != 0) goto add_user_free_fullname; - memcpy(new_info->ident.data, ident.data, ident.len); - new_info->ident.len = ident.len; - new_info->vhost.data = malloc(vhost.len); - if (!new_info->vhost.data) + if (str_clone(&(new_info->vhost), vhost) != 0) goto add_user_free_ident; - memcpy(new_info->vhost.data, vhost.data, vhost.len); - new_info->vhost.len = vhost.len; - new_info->host.data = malloc(host.len); - if (!new_info->host.data) + if (str_clone(&(new_info->host), host) != 0) goto add_user_free_vhost; - memcpy(new_info->host.data, host.data, host.len); - new_info->host.len = host.len; - new_info->address.data = malloc(address.len); - if (!new_info->address.data) + if (str_clone(&(new_info->address), address) != 0) goto add_user_free_host; - memcpy(new_info->address.data, address.data, address.len); - new_info->address.len = address.len; if (set_table_index(&user_list, uid, new_info) != 0) goto add_user_free_address; @@ -300,6 +271,10 @@ int add_user(struct string from, struct string attached_to, struct string uid, s free(new_info->nick.data); add_user_free_uid: free(new_info->uid.data); + add_user_free_nick_ts: + free(new_info->nick_ts_str.data); + add_user_free_user_ts: + free(new_info->user_ts_str.data); add_user_free_info: free(new_info); @@ -329,6 +304,8 @@ void remove_user(struct string from, struct user_info *user, struct string reaso clear_table(&(user->channel_list)); free(user->channel_list.array); + free(user->user_ts_str.data); + free(user->nick_ts_str.data); free(user->uid.data); free(user->nick.data); free(user->fullname.data); diff --git a/general_network.h b/general_network.h index 6b76256..dc00e43 100644 --- a/general_network.h +++ b/general_network.h @@ -75,6 +75,8 @@ struct user_info { struct string host; struct string address; + struct string user_ts_str; + struct string nick_ts_str; size_t user_ts; size_t nick_ts; @@ -93,7 +95,6 @@ int resolve(struct string address, struct string port, struct sockaddr *sockaddr int init_general_network(void); int add_user(struct string from, struct string attached_to, struct string uid, struct string nick, struct string fullname, struct string ident, struct string vhost, struct string host, struct string address, size_t user_ts, size_t nick_ts, void *handle, size_t protocol, size_t net); -// Does not propagate, but will inform local clients about it void remove_user(struct string from, struct user_info *user, struct string reason, char propagate); extern char casemap[UCHAR_MAX+1]; diff --git a/haxstring_utils.c b/haxstring_utils.c index 0d2c5fc..2a7ed02 100644 --- a/haxstring_utils.c +++ b/haxstring_utils.c @@ -28,6 +28,7 @@ #include <stddef.h> #include <stdlib.h> +#include <string.h> #include "haxstring.h" #include "haxstring_utils.h" @@ -98,3 +99,13 @@ int unsigned_to_str(size_t number, struct string *str) { return 0; } + +int str_clone(struct string *dest, struct string source) { + dest->data = malloc(source.len); + if (!dest->data) + return 1; + memcpy(dest->data, source.data, source.len); + dest->len = source.len; + + return 0; +} diff --git a/haxstring_utils.h b/haxstring_utils.h index d18208c..7b50e08 100644 --- a/haxstring_utils.h +++ b/haxstring_utils.h @@ -33,3 +33,5 @@ size_t str_to_unsigned(struct string str, char *err); int unsigned_to_str(size_t number, struct string *str); + +int str_clone(struct string *dest, struct string source); diff --git a/protocols/inspircd2.c b/protocols/inspircd2.c index 883dbc5..0650c7d 100644 --- a/protocols/inspircd2.c +++ b/protocols/inspircd2.c @@ -541,17 +541,7 @@ void inspircd2_protocol_propagate_new_user(struct string from, struct user_info networks[adjacent->net].send(adjacent->handle, STRING(" UID ")); networks[adjacent->net].send(adjacent->handle, info->uid); networks[adjacent->net].send(adjacent->handle, STRING(" ")); - - struct string timestamp; - char err = unsigned_to_str(info->nick_ts, ×tamp); - - if (err) { - networks[adjacent->net].send(adjacent->handle, STRING("1")); // TODO: Better fallback - } else { - networks[adjacent->net].send(adjacent->handle, timestamp); - free(timestamp.data); - } - + networks[adjacent->net].send(adjacent->handle, info->nick_ts_str); networks[adjacent->net].send(adjacent->handle, STRING(" ")); networks[adjacent->net].send(adjacent->handle, info->nick); networks[adjacent->net].send(adjacent->handle, STRING(" ")); @@ -563,16 +553,7 @@ void inspircd2_protocol_propagate_new_user(struct string from, struct user_info networks[adjacent->net].send(adjacent->handle, STRING(" ")); networks[adjacent->net].send(adjacent->handle, info->address); networks[adjacent->net].send(adjacent->handle, STRING(" ")); - - err = unsigned_to_str(info->user_ts, ×tamp); - - if (err) { - networks[adjacent->net].send(adjacent->handle, STRING("1")); - } else { - networks[adjacent->net].send(adjacent->handle, timestamp); - free(timestamp.data); - } - + networks[adjacent->net].send(adjacent->handle, info->user_ts_str); networks[adjacent->net].send(adjacent->handle, STRING(" + :")); networks[adjacent->net].send(adjacent->handle, info->fullname); networks[adjacent->net].send(adjacent->handle, STRING("\n")); diff --git a/server_network.c b/server_network.c index ea92d3c..aeca696 100644 --- a/server_network.c +++ b/server_network.c @@ -33,6 +33,8 @@ #include <unistd.h> #include "config.h" +#include "haxstring.h" +#include "haxstring_utils.h" #include "main.h" #include "protocols.h" #include "server_network.h" @@ -187,23 +189,14 @@ int add_server(struct string from, struct string attached_to, struct string sid, new_info->net = net; new_info->handle = handle; - new_info->sid.data = malloc(sid.len); - if (!new_info->sid.data) + if (str_clone(&(new_info->sid), sid) != 0) goto add_server_free_new_info; - memcpy(new_info->sid.data, sid.data, sid.len); - new_info->sid.len = sid.len; - new_info->name.data = malloc(name.len); - if (!new_info->name.data) + if (str_clone(&(new_info->name), name) != 0) goto add_server_free_sid; - memcpy(new_info->name.data, name.data, name.len); - new_info->name.len = name.len; - new_info->fullname.data = malloc(fullname.len); - if (!new_info->fullname.data) + if (str_clone(&(new_info->fullname), fullname) != 0) goto add_server_free_name; - memcpy(new_info->fullname.data, fullname.data, fullname.len); - new_info->fullname.len = fullname.len; // new_info->next shares string with sid of the server it points to @@ -102,15 +102,7 @@ int set_table_index(struct table *tbl, struct string name, void *ptr) { char exists; size_t index = search(*tbl, name, &exists); - if (index == tbl->len) { - void *tmp = realloc(tbl->array, sizeof(*(tbl->array)) * (tbl->len+1)); - if (tmp == 0) - return 1; - - tbl->array = tmp; - - tbl->len++; - } else if (!exists) { + if (!exists) { void *tmp = realloc(tbl->array, sizeof(*(tbl->array)) * (tbl->len+1)); if (tmp == 0) return 1; |