Add client Key hash to shm_area
[umurmur.git] / src / sharedmemory.c
1 #include "sharedmemory.h"
2
3 int shm_fd;
4 shm_t *shmptr = NULL;
5 char shm_file_name[128];
6
7 void Sharedmemory_init( int bindport, int bindport6 )
8 {
9
10         int server_max_clients = getIntConf(MAX_CLIENTS);
11         int shmtotal_size =  sizeof( shm_t  ) + (sizeof( shmclient_t ) * server_max_clients);
12
13         if( !bindport )
14         {
15                 bindport = getIntConf(BINDPORT);
16         }
17
18         sprintf( shm_file_name, "/umurmurd:%i", bindport );
19         Log_info("SHM_API: shm_fd=\"%s\"", shm_file_name  );
20
21         shm_fd = shm_open( shm_file_name, O_CREAT | O_RDWR, 0660 );
22         if(shm_fd == -1)
23         {
24                 Log_fatal( "SHM_API: Open failed:%s\n", strerror(errno));
25                 exit(EXIT_FAILURE);
26         }
27
28         if( ftruncate( shm_fd, shmtotal_size ) == -1 )
29         {
30                 Sharedmemory_deinit();
31                 Log_fatal( "SHM_API: ftruncate : %s\n", strerror(errno));
32                 exit(EXIT_FAILURE);
33         }
34
35         shmptr = mmap( 0, shmtotal_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0 );
36         if (shmptr == MAP_FAILED)
37         {
38                 Log_fatal( "SHM_API: mmap failed : %s\n", strerror(errno));
39                 exit(EXIT_FAILURE);
40         }
41
42         memset( shmptr, 0, shmtotal_size );
43
44         shmptr->umurmurd_pid = getpid();
45         shmptr->server_max_clients = server_max_clients;
46         shmptr->shmtotal_size = shmtotal_size;
47         shmptr->shmclient_size = sizeof( shmclient_t ) * shmptr->server_max_clients;
48
49 }
50
51 void Sharedmemory_update(void)
52 {
53
54         uint64_t now;
55         unsigned int cc = 0;
56         client_t *client_itr = NULL;
57
58         memset( &shmptr->client[0], 0, shmptr->shmclient_size );
59         shmptr->clientcount = Client_count();
60
61         if( shmptr->clientcount )
62         {
63                 Timer_init( &now );
64                 while( Client_iterate(&client_itr) != NULL )
65                 {
66                         if( client_itr->authenticated && !client_itr->shutdown_wait )
67                         {
68                                 channel_t *channel = client_itr->channel;
69
70                                 char* clientAddressString = Util_clientAddressToString( client_itr );
71
72                                 strncpy( shmptr->client[cc].username, client_itr->username, 120 );
73                                 strncpy( shmptr->client[cc].ipaddress, clientAddressString, INET6_ADDRSTRLEN - 1 );
74                                 strncpy( shmptr->client[cc].channel, channel->name, 120 );
75
76                                 strncpy( shmptr->client[cc].os, client_itr->os, 120 );
77                                 strncpy( shmptr->client[cc].release, client_itr->release, 120 );
78                                 strncpy( shmptr->client[cc].os_version, client_itr->os_version, 120 );
79
80         strncpy( shmptr->client[cc].hash, client_itr->hash, 20 );
81         
82                                 shmptr->client[cc].tcp_port = Util_clientAddressToPortTCP( client_itr );
83                                 shmptr->client[cc].udp_port = Util_clientAddressToPortUDP( client_itr );
84
85                                 shmptr->client[cc].online_secs = ( now - client_itr->connectTime ) / 1000000LL;
86                                 shmptr->client[cc].idle_secs = ( now - client_itr->idleTime ) / 1000000LL;
87
88                                 shmptr->client[cc].bUDP  = client_itr->bUDP;
89                                 shmptr->client[cc].deaf  = client_itr->deaf;
90                                 shmptr->client[cc].mute  = client_itr->mute;
91                                 shmptr->client[cc].bOpus  = client_itr->bOpus;
92                                 shmptr->client[cc].self_deaf  = client_itr->self_deaf;
93                                 shmptr->client[cc].self_mute  = client_itr->self_mute;
94                                 shmptr->client[cc].recording  = client_itr->recording;
95                                 shmptr->client[cc].authenticated  = client_itr->authenticated;
96
97                                 shmptr->client[cc].availableBandwidth  = client_itr->availableBandwidth;
98
99                                 shmptr->client[cc].UDPPingAvg = client_itr->UDPPingAvg;
100                                 shmptr->client[cc].UDPPingVar = client_itr->UDPPingVar;
101                                 shmptr->client[cc].TCPPingAvg = client_itr->TCPPingAvg;
102                                 shmptr->client[cc].TCPPingVar = client_itr->TCPPingVar;
103
104                                 
105
106                                 shmptr->client[cc].isAdmin = client_itr->isAdmin;
107                                 shmptr->client[cc].isSuppressed = client_itr->isSuppressed;
108
109                                 shmptr->client[cc].UDPPackets = client_itr->UDPPackets;
110                                 shmptr->client[cc].TCPPackets = client_itr->TCPPackets;
111
112                                 free(clientAddressString);
113                         }
114                         cc++;
115                 }
116         }
117 }
118 void Sharedmemory_alivetick(void)
119 {
120         shmptr->alive++;
121 }
122
123 void Sharedmemory_deinit(void)
124 {
125         close( shm_fd );
126         shm_unlink( shm_file_name );
127         shmptr->umurmurd_pid = 0;
128 }