From 336d27baf938a957177e4c26c2b5c6c4172aa0a6 Mon Sep 17 00:00:00 2001 From: "Michael J. Pounders" Date: Sun, 14 Sep 2014 18:51:54 -0400 Subject: [PATCH] Initial sharedmemory API release --- src/CMakeLists.txt | 1 + src/main.c | 5 ++- src/server.c | 1 + src/sharedmemory.c | 70 +++++++++++++++++++++++++++++++++++++++ src/sharedmemory.h | 17 ++++++++++ src/sharedmemory_struct.h | 21 ++++++++++++ 6 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/sharedmemory.c create mode 100644 src/sharedmemory.h create mode 100644 src/sharedmemory_struct.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a73a5a2..f916d71 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,7 @@ set(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/crypt.c ${CMAKE_CURRENT_SOURCE_DIR}/log.c ${CMAKE_CURRENT_SOURCE_DIR}/main.c + ${CMAKE_CURRENT_SOURCE_DIR}/sharedmemory.c ${CMAKE_CURRENT_SOURCE_DIR}/messagehandler.c ${CMAKE_CURRENT_SOURCE_DIR}/messages.c ${CMAKE_CURRENT_SOURCE_DIR}/pds.c diff --git a/src/main.c b/src/main.c index c1649b6..929af5c 100644 --- a/src/main.c +++ b/src/main.c @@ -55,6 +55,7 @@ #include "conf.h" #include "version.h" #include "config.h" +#include "sharedmemory.h" char system_string[64], version_string[64]; int bindport; @@ -349,7 +350,8 @@ int main(int argc, char **argv) Chan_init(); Client_init(); Ban_init(); - + Sharedmemory_init(); + #ifdef POSIX_PRIORITY_SCHEDULING if (realtime) setscheduler(); @@ -357,6 +359,7 @@ int main(int argc, char **argv) Server_run(); + Sharedmemory_deinit(); Ban_deinit(); SSLi_deinit(); Chan_free(); diff --git a/src/server.c b/src/server.c index 09e05dd..46175e9 100644 --- a/src/server.c +++ b/src/server.c @@ -194,6 +194,7 @@ void Server_runLoop(struct pollfd* pollfds) if (pollfds[nofServerSocks + i].revents & POLLOUT) Client_write_fd(pollfds[nofServerSocks + i].fd); } + Sharedmemory_update(); } } diff --git a/src/sharedmemory.c b/src/sharedmemory.c new file mode 100644 index 0000000..56e9cea --- /dev/null +++ b/src/sharedmemory.c @@ -0,0 +1,70 @@ +#include "sharedmemory.h" + +void Sharedmemory_init(void) +{ + + key_t key = 0x53021d79; + int server_max_clients = getIntConf(MAX_CLIENTS); + int shmptr_size = sizeof( shm_t ) + (sizeof( shmclient_t ) * server_max_clients); + + //key = ftok( ".", 'S' ); //MJP BUG this needs fixing.. also how to handle multi copys of umurmur bound to diff ports or IPs option to pass key on cmdline? + shmid = shmget( key, shmptr_size, IPC_CREAT | 0666 ); + + Log_info("SHM_KEY: 0x%x", key ); + + if( ( shmptr = ( shm_t *) shmat( shmid, 0, 0 ) ) == (shm_t *) (-1) ) + { + perror("shmat"); + exit(1); //MJP BUG should report error and just not use shm dont exit + } + memset( shmptr, 0, shmptr_size ); + shmptr->umurmurd_pid = getpid(); + shmptr->server_max_clients = server_max_clients; +} + +void Sharedmemory_update(void) +{ + + static size_t bt_end = sizeof( bool_t ) * 8, //compute once + et_end = sizeof( etimer_t ) * 3, + pa_end = sizeof( float ) * 4, + pc_end = sizeof( uint32_t ) * 2; + + unsigned int cc = 0; + client_t *client_itr = NULL; + + memset( &shmptr->client[0], 0, sizeof( shmclient_t ) * shmptr->server_max_clients ); + shmptr->clientcount = Client_count(); + + + + while( Client_iterate(&client_itr) != NULL ) { + + + + if( client_itr->authenticated ) + { + + channel_t *channel = client_itr->channel; + + strncpy( shmptr->client[cc].username, client_itr->username, 120 ); + strncpy( shmptr->client[cc].ipaddress, Util_clientAddressToString( client_itr ) , 45 ); + shmptr->client[cc].tcp_port = Util_clientAddressToPortTCP( client_itr ); + shmptr->client[cc].udp_port = Util_clientAddressToPortUDP( client_itr ); + strncpy( shmptr->client[cc].channel, channel->name, 120 ); + memcpy( &shmptr->client[cc].bUDP, &client_itr->bUDP, bt_end ); + memcpy( &shmptr->client[cc].lastActivity, &client_itr->lastActivity, et_end ); + memcpy( &shmptr->client[cc].UDPPingAvg, &client_itr->UDPPingAvg, pa_end ); + memcpy( &shmptr->client[cc].UDPPackets, &client_itr->UDPPackets, pc_end ); + } + cc++; + + } + +} + +void Sharedmemory_deinit(void) +{ + shmctl( shmid, IPC_RMID, 0 ); //Mark shmid for removal. + shmdt( shmptr ); +} \ No newline at end of file diff --git a/src/sharedmemory.h b/src/sharedmemory.h new file mode 100644 index 0000000..26001f7 --- /dev/null +++ b/src/sharedmemory.h @@ -0,0 +1,17 @@ +#include +#include + +#include + +#include "util.h" +#include "conf.h" +#include "client.h" +#include "channel.h" +#include "sharedmemory_struct.h" + +int shmid; +shm_t *shmptr; + +void Sharedmemory_init(void); +void Sharedmemory_update(void); +void Sharedmemory_deinit(void); diff --git a/src/sharedmemory_struct.h b/src/sharedmemory_struct.h new file mode 100644 index 0000000..88eb0c5 --- /dev/null +++ b/src/sharedmemory_struct.h @@ -0,0 +1,21 @@ +typedef struct +{ + char username[121]; + char ipaddress[46]; + int tcp_port, udp_port; + char channel[121]; + bool_t bUDP, authenticated, deaf, mute, self_deaf, self_mute, recording, bOpus; + etimer_t lastActivity, connectTime, idleTime; + float UDPPingAvg, UDPPingVar, TCPPingAvg, TCPPingVar; + uint32_t UDPPackets, TCPPackets; + +}shmclient_t; + +typedef struct +{ + + int clientcount, server_max_clients; + unsigned int umurmurd_pid; //Use this to make sure umurmurd is still running so I can allow more than one connection. + shmclient_t client[]; //MJP BUG: Use max usersetting from conf file + +}shm_t; \ No newline at end of file -- 2.30.2