From c3b8728ca466d0e592e2c8bd504af99dcee72356 Mon Sep 17 00:00:00 2001 From: Egor Date: Fri, 27 May 2022 12:12:09 +0300 Subject: [PATCH] use a linked list for clients --- common.h | 3 ++- config.h | 1 - irc.c | 9 ++++---- main.c | 64 +++++++++++++++++++++++++++----------------------------- net.c | 9 +++++--- 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/common.h b/common.h index b3d0c32..449d17e 100644 --- a/common.h +++ b/common.h @@ -23,6 +23,7 @@ struct client_data { SOCKET FD; struct tls *cctx; char type; + struct client_data *next; char *nick; char *key; // case-mapped nick char *username; @@ -76,7 +77,7 @@ struct channel { #define is_chanvoice(chan, client) (trie_get(&(chan)->v, (client)->key)) extern struct trie_node *client_map; extern struct trie_node *channel_map; -extern struct client_data *clients[CLIENTS]; +extern struct client_data *clients; extern fd_set readfds; extern struct sockaddr_in serverAddrV4; extern struct sockaddr_in6 serverAddrV6; diff --git a/config.h b/config.h index 7b19556..5245c54 100644 --- a/config.h +++ b/config.h @@ -1,4 +1,3 @@ -#define CLIENTS 30 #define MAX_NICKLEN 32 #ifndef ADMINLOC1 #define ADMINLOC1 "Location 1" diff --git a/irc.c b/irc.c index 821d97d..4118b1d 100644 --- a/irc.c +++ b/irc.c @@ -996,17 +996,16 @@ void sendLUSERS(struct client_data *client) do { channels++; } while (trie_findnext(&f) == 1); - for (int i = 0; i < CLIENTS; i++) - if (clients[i]) - total++; + for (struct client_data *cl = clients; cl; cl = cl->next) + total++; rpl_luserclient(client, registered, invisible, 1), sendbuf(client); rpl_luserop(client, ops), sendbuf(client); rpl_luserunknown(client, total-registered), sendbuf(client); rpl_luserchannels(client, channels), sendbuf(client); rpl_luserme(client, registered, 0), sendbuf(client); - rpl_localusers(client, registered, CLIENTS), sendbuf(client); - rpl_globalusers(client, registered, CLIENTS), sendbuf(client); + rpl_localusers(client, registered, 666), sendbuf(client); + rpl_globalusers(client, registered, 666), sendbuf(client); } void sendMOTD(struct client_data *client) { diff --git a/main.c b/main.c index 5d3838b..efcd80f 100644 --- a/main.c +++ b/main.c @@ -67,7 +67,7 @@ err: #endif // Client linked list -struct client_data *clients[CLIENTS]; +struct client_data *clients = NULL; // FDSET List fd_set readfds; @@ -165,23 +165,20 @@ void accept_from_sock(struct listener *listener) #endif } // Add to list of sockets - for(int i=0;iFD = newSocket; - clients[i]->cctx = cctx; - clients[i]->type = addr.ss_family == AF_INET6 ? 6 : 4; - clients[i]->peername = mystrdup(client_addr); - if (!clients[i]->peername) abort(); - clients[i]->peerport = port; - if (cctx) - clients[i]->umode |= UMODE_SECURE; - extern const char *hostname; - copy_strf(&databuf, ":%s NOTICE * :Hello welcome to PatchyIRCD!\r\n", hostname); - sendbuf(clients[i]); - break; - } - } + struct client_data *cl = allocClient(); + cl->FD = newSocket; + cl->cctx = cctx; + cl->type = addr.ss_family == AF_INET6 ? 6 : 4; + cl->peername = mystrdup(client_addr); + if (!cl->peername) abort(); + cl->peerport = port; + if (cctx) + cl->umode |= UMODE_SECURE; + extern const char *hostname; + copy_strf(&databuf, ":%s NOTICE * :Hello welcome to PatchyIRCD!\r\n", hostname); + sendbuf(cl); + cl->next = clients; + clients = cl; } } @@ -198,9 +195,6 @@ int main(int argc, char *argv[]) databuf.buf = malloc(sizeof(char) * 512); databuf.cap = 512; - // Zero stuff out - memset(clients, 0, sizeof(clients)); - struct tls *tls_ctx = NULL; #ifdef HAS_TLS struct tls_config *tls_cfg; @@ -318,9 +312,8 @@ int main(int argc, char *argv[]) if (self_sock != INVALID_SOCKET) FD_SET(self_sock, &readfds); #endif - for (int i = 0; i < CLIENTS; i++) - if (clients[i]) - FD_SET(clients[i]->FD, &readfds); + for (struct client_data *cl = clients; cl; cl = cl->next) + FD_SET(cl->FD, &readfds); // Set highestFD #ifdef _WIN32 @@ -330,9 +323,9 @@ int main(int argc, char *argv[]) for (int i = 0; i < listener_count; i++) if (highestFD < listeners[i].sock) highestFD = listeners[i].sock; - for (int i = 0; i < CLIENTS; i++) - if (clients[i] && highestFD < clients[i]->FD) - highestFD = clients[i]->FD; + for (struct client_data *cl = clients; cl; cl = cl->next) + if (highestFD < cl->FD) + highestFD = cl->FD; #endif // Wait until activity on sockets if(select(highestFD+1,&readfds,NULL,NULL,NULL) == -1) { @@ -355,15 +348,20 @@ int main(int argc, char *argv[]) if (FD_ISSET(listeners[i].sock, &readfds)) accept_from_sock(&listeners[i]); // Respond to already connected clients - for (int i = 0; i < CLIENTS; i++) - if (clients[i] && FD_ISSET(clients[i]->FD, &readfds)) - recvProc(clients[i]); + for (struct client_data *cl = clients; cl;) { + struct client_data *next = cl->next; + if (FD_ISSET(cl->FD, &readfds)) + recvProc(cl); + cl = next; + } } fprintf(stderr, "[PatchyIRCD]: Cleaning up...\n"); - for (int i = 0; i < CLIENTS; i++) - if (clients[i]) - killConnection(clients[i], false); + for (struct client_data *cl = clients; cl;) { + struct client_data *next = cl->next; + killConnection(cl, false); + cl = next; + } net_cleanup(); for (int i = 0; i < listener_count; i++) closesocket(listeners[i].sock); diff --git a/net.c b/net.c index 56c87f8..0101443 100644 --- a/net.c +++ b/net.c @@ -11,9 +11,12 @@ struct client_data *allocClient(){ void killConnection(struct client_data *cl, bool byChoice) { if (!cl) return; - for (int i = 0; i < CLIENTS; i++) - if(clients[i] == cl) - clients[i] = 0; + for (struct client_data **next = &clients; *next; next = &(*next)->next) { + if (*next == cl) { + *next = cl->next; + break; + } + } if (cl->peername) fprintf(stderr, "Client disconnected from Socket: %"PRISocket", IP: %s PORT: %d\n", cl->FD, cl->peername, cl->peerport); if(!byChoice && cl->nick && cl->username) { -- 2.56.0