Merge branch 'master' of git://github.com/Diaoul/umurmur into Diaoul/master
[umurmur.git] / src / client.c
1 /* Copyright (C) 2009-2011, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2011, Thorvald Natvig <thorvald@natvig.com>
3
4    All rights reserved.
5
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9
10    - Redistributions of source code must retain the above copyright notice,
11      this list of conditions and the following disclaimer.
12    - Redistributions in binary form must reproduce the above copyright notice,
13      this list of conditions and the following disclaimer in the documentation
14      and/or other materials provided with the distribution.
15    - Neither the name of the Developers nor the names of its contributors may
16      be used to endorse or promote products derived from this software without
17      specific prior written permission.
18
19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/poll.h>
32 #include <sys/socket.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "log.h"
39 #include "list.h"
40 #include "client.h"
41 #include "ssl.h"
42 #include "messages.h"
43 #include "messagehandler.h"
44 #include "conf.h"
45 #include "channel.h"
46 #include "version.h"
47 #include "voicetarget.h"
48
49 extern char system_string[], version_string[];
50
51 static int Client_read(client_t *client);
52 static int Client_write(client_t *client);
53 static int Client_send_udp(client_t *client, uint8_t *data, int len);
54 void Client_free(client_t *client);
55
56 declare_list(clients);
57 static int clientcount; /* = 0 */
58 static int maxBandwidth;
59
60 int iCodecAlpha, iCodecBeta;
61 bool_t bPreferAlpha;
62
63 extern int udpsock;
64
65 void Client_init()
66 {
67         maxBandwidth = getIntConf(MAX_BANDWIDTH) / 8; /* From bits/s -> bytes/s */
68 }
69
70 int Client_count()
71 {
72         return clientcount;
73 }
74
75 int Client_getfds(struct pollfd *pollfds)
76 {
77         struct dlist *itr;
78         int i = 0;
79         list_iterate(itr, &clients) {
80                 client_t *c;
81                 c = list_get_entry(itr, client_t, node);
82                 pollfds[i].fd = c->tcpfd;
83                 pollfds[i].events = POLLIN | POLLHUP | POLLERR;
84                 if (c->txsize > 0 || c->readBlockedOnWrite) /* Data waiting to be sent? */
85                         pollfds[i].events |= POLLOUT;
86                 i++;
87         }
88         return i;
89 }
90
91 void Client_janitor()
92 {
93         struct dlist *itr;
94         int bwTop = maxBandwidth + maxBandwidth / 4;
95         list_iterate(itr, &clients) {
96                 client_t *c;
97                 c = list_get_entry(itr, client_t, node);
98                 Log_debug("Client %s BW available %d", c->username, c->availableBandwidth);
99                 c->availableBandwidth += maxBandwidth;
100                 if (c->availableBandwidth > bwTop)
101                         c->availableBandwidth = bwTop;
102                 
103                 if (Timer_isElapsed(&c->lastActivity, 1000000LL * INACTICITY_TIMEOUT)) {
104                         /* No activity from client - assume it is lost and close. */
105                         Log_info_client(c, "Timeout, closing.");
106                         Client_free(c);
107                 }
108         }
109 }
110
111 void Client_codec_add(client_t *client, int codec)
112 {
113         codec_t *cd = malloc(sizeof(codec_t));
114         if (cd == NULL)
115                 Log_fatal("Out of memory");
116         init_list_entry(&cd->node);
117         cd->codec = codec;
118         list_add_tail(&cd->node, &client->codecs);
119 }
120
121 void Client_codec_free(client_t *client)
122 {
123         struct dlist *itr, *save;
124         list_iterate_safe(itr, save, &client->codecs) {
125                 list_del(&list_get_entry(itr, codec_t, node)->node);
126                 free(list_get_entry(itr, codec_t, node));
127         }
128 }
129
130 codec_t *Client_codec_iterate(client_t *client, codec_t **codec_itr)
131 {
132         codec_t *cd = *codec_itr;
133
134         if (list_empty(&client->codecs))
135                 return NULL;
136         
137         if (cd == NULL) {
138                 cd = list_get_entry(list_get_first(&client->codecs), codec_t, node);
139         } else {
140                 if (list_get_next(&cd->node) == &client->codecs)
141                         cd = NULL;
142                 else
143                         cd = list_get_entry(list_get_next(&cd->node), codec_t, node);
144         }
145         *codec_itr = cd;
146         return cd;
147 }
148
149 void recheckCodecVersions()
150 {
151         client_t *client_itr = NULL;
152         int max = 0, version, current_version;
153         message_t *sendmsg;
154         struct dlist codec_list, *itr, *save;
155         codec_t *codec_itr, *cd;
156         bool_t found;
157         
158         init_list_entry(&codec_list);
159         
160         while (Client_iterate(&client_itr) != NULL) {
161                 codec_itr = NULL;
162                 while (Client_codec_iterate(client_itr, &codec_itr) != NULL) {
163                         found = false;
164                         list_iterate(itr, &codec_list) {
165                                 cd = list_get_entry(itr, codec_t, node);
166                                 if (cd->codec == codec_itr->codec) {
167                                         cd->count++;
168                                         found = true;
169                                 }
170                         }
171                         if (!found) {
172                                 cd = malloc(sizeof(codec_t));
173                                 if (!cd)
174                                         Log_fatal("Out of memory");
175                                 memset(cd, 0, sizeof(codec_t));
176                                 init_list_entry(&cd->node);
177                                 cd->codec = codec_itr->codec;
178                                 cd->count = 1;
179                                 list_add_tail(&cd->node, &codec_list);
180                         }
181                 }
182         }
183         
184         list_iterate(itr, &codec_list) {
185                 cd = list_get_entry(itr, codec_t, node);
186                 if (cd->count > max) {
187                         max = cd->count;
188                         version = cd->codec;
189                 }
190         }
191         list_iterate_safe(itr, save, &codec_list) {
192                 list_del(&list_get_entry(itr, codec_t, node)->node);
193                 free(list_get_entry(itr, codec_t, node));
194         }
195         
196         current_version = bPreferAlpha ? iCodecAlpha : iCodecBeta;
197         if (current_version == version)
198                 return;
199         // If we don't already use the compat bitstream version set
200         // it as alpha and announce it. If another codec now got the
201         // majority set it as the opposite of the currently valid bPreferAlpha
202         // and announce it.
203         if (version == (uint32_t)0x8000000b)
204                 bPreferAlpha = true;
205         else
206                 bPreferAlpha = ! bPreferAlpha;
207
208         if (bPreferAlpha)
209                 iCodecAlpha = version;
210         else
211                 iCodecBeta = version;
212         
213         sendmsg = Msg_create(CodecVersion);
214         sendmsg->payload.codecVersion->alpha = iCodecAlpha;
215         sendmsg->payload.codecVersion->beta = iCodecBeta;
216         sendmsg->payload.codecVersion->prefer_alpha = bPreferAlpha;
217         Client_send_message_except(NULL, sendmsg);
218         
219         Log_info("CELT codec switch 0x%x 0x%x (prefer 0x%x)", iCodecAlpha, iCodecBeta,
220                          bPreferAlpha ? iCodecAlpha : iCodecBeta);
221         
222 }
223
224 static int findFreeSessionId()
225 {
226         int id;
227         client_t *itr = NULL;
228
229         for (id = 1; id < INT_MAX; id++) {
230                 itr = NULL;
231                 while ((itr = Client_iterate(&itr)) != NULL) {
232                         if (itr->sessionId == id)
233                                 break;
234                 }
235                 if (itr == NULL) /* Found free id */
236                         return id;
237         }
238         return -1;
239 }
240
241 int Client_add(int fd, struct sockaddr_in *remote)
242 {
243         client_t *newclient;
244         message_t *sendmsg;
245         
246         newclient = malloc(sizeof(client_t));
247         if (newclient == NULL)
248                 Log_fatal("Out of memory");
249         memset(newclient, 0, sizeof(client_t));
250
251         newclient->tcpfd = fd;
252         memcpy(&newclient->remote_tcp, remote, sizeof(struct sockaddr_in));
253         newclient->ssl = SSLi_newconnection(&newclient->tcpfd, &newclient->SSLready);
254         if (newclient->ssl == NULL) {
255                 Log_warn("SSL negotiation failed with %s:%d", inet_ntoa(remote->sin_addr),
256                                  ntohs(remote->sin_port));
257                 free(newclient);
258                 return -1;
259         }
260         newclient->availableBandwidth = maxBandwidth;
261         Timer_init(&newclient->lastActivity);
262         Timer_init(&newclient->connectTime);
263         Timer_init(&newclient->idleTime);
264         newclient->sessionId = findFreeSessionId();
265         if (newclient->sessionId < 0)
266                 Log_fatal("Could not find a free session ID");
267         
268         init_list_entry(&newclient->txMsgQueue);
269         init_list_entry(&newclient->chan_node);
270         init_list_entry(&newclient->node);
271         init_list_entry(&newclient->voicetargets);
272         init_list_entry(&newclient->codecs);
273         
274         list_add_tail(&newclient->node, &clients);
275         clientcount++;
276         
277         /* Send version message to client */
278         sendmsg = Msg_create(Version);
279         sendmsg->payload.version->has_version = true;
280         sendmsg->payload.version->version = PROTOCOL_VERSION;
281         sendmsg->payload.version->release = strdup(UMURMUR_VERSION);
282         sendmsg->payload.version->os = strdup(system_string);
283         sendmsg->payload.version->os_version = strdup(version_string);
284         Client_send_message(newclient, sendmsg);
285
286         return 0;
287 }
288
289 void Client_free(client_t *client)
290 {
291         struct dlist *itr, *save;
292         message_t *sendmsg;
293
294         if (client->authenticated) {
295                 int leave_id;
296                 leave_id = Chan_userLeave(client);
297                 if (leave_id > 0) { /* Remove temp channel */
298                         sendmsg = Msg_create(ChannelRemove);
299                         sendmsg->payload.channelRemove->channel_id = leave_id;
300                         Client_send_message_except(client, sendmsg);
301                 }
302                 sendmsg = Msg_create(UserRemove);
303                 sendmsg->payload.userRemove->session = client->sessionId;
304                 Client_send_message_except(client, sendmsg);
305         }
306         list_iterate_safe(itr, save, &client->txMsgQueue) {
307                 list_del(&list_get_entry(itr, message_t, node)->node);
308                 Msg_free(list_get_entry(itr, message_t, node));
309         }
310         Client_codec_free(client);
311         Voicetarget_free_all(client);
312         
313         list_del(&client->node);
314         if (client->ssl)
315                 SSLi_free(client->ssl);
316         close(client->tcpfd);
317         clientcount--;
318         if (client->release)
319                 free(client->release);
320         if (client->os)
321                 free(client->os);                       
322         if (client->os_version)
323                 free(client->os_version);                       
324         if (client->username)
325                 free(client->username);
326         if (client->context)
327                 free(client->context);
328         free(client);
329 }
330
331 void Client_close(client_t *client)
332 {
333         SSLi_shutdown(client->ssl);
334         client->shutdown_wait = true;
335 }
336
337 void Client_disconnect_all()
338 {
339         struct dlist *itr, *save;
340         
341         list_iterate_safe(itr, save, &clients) {
342                 Client_free(list_get_entry(itr, client_t, node));
343         }
344 }
345
346 int Client_read_fd(int fd)
347 {
348         struct dlist *itr;
349         client_t *client = NULL;
350         
351         list_iterate(itr, &clients) {
352                 if (fd == list_get_entry(itr, client_t, node)->tcpfd) {
353                         client = list_get_entry(itr, client_t, node);
354                         break;
355                 }
356         }
357         if (client == NULL)
358                 Log_fatal("No client found for fd %d", fd);
359         
360         return Client_read(client);
361 }
362
363 int Client_read(client_t *client)
364 {
365         int rc;
366
367         Timer_restart(&client->lastActivity);
368         
369         if (client->writeBlockedOnRead) {
370                 client->writeBlockedOnRead = false;
371                 Log_debug("Client_read: writeBlockedOnRead == true");
372                 return Client_write(client);
373         }
374         
375         if (client->shutdown_wait) {
376                 Client_free(client);
377                 return 0;
378         }
379         if (!client->SSLready) {
380                 int rc;
381                 rc = SSLi_nonblockaccept(client->ssl, &client->SSLready);
382                 if (rc < 0) {
383                         Client_free(client);
384                         return -1;
385                 }
386         }
387
388         do {
389                 errno = 0;
390                 if (!client->msgsize) 
391                         rc = SSLi_read(client->ssl, &client->rxbuf[client->rxcount], 6 - client->rxcount);
392                 else
393                         rc = SSLi_read(client->ssl, &client->rxbuf[client->rxcount], client->msgsize);
394                 if (rc > 0) {
395                         message_t *msg;
396                         client->rxcount += rc;
397                         if (!client->msgsize && client->rxcount >= 6) {
398                                 uint32_t msgLen;
399                                 memcpy(&msgLen, &client->rxbuf[2], sizeof(uint32_t));
400                                 client->msgsize = ntohl(msgLen);
401                         }
402                         if (client->msgsize > BUFSIZE - 6) {
403                                 /* XXX - figure out how to handle this. A large size here can represent two cases:
404                                  * 1. A valid size. The only message that is this big is UserState message with a big texture
405                                  * 2. An invalid size = protocol error, e.g. connecting with a 1.1.x client
406                                  */
407                                 Log_warn("Too big message received (%d bytes). Playing safe and disconnecting client %s:%d",
408                                                  client->msgsize, inet_ntoa(client->remote_tcp.sin_addr), ntohs(client->remote_tcp.sin_port));
409                                 Client_free(client);
410                                 return -1;
411                                 /* client->rxcount = client->msgsize = 0; */
412                         }
413                         else if (client->rxcount == client->msgsize + 6) { /* Got all of the message */
414                                 msg = Msg_networkToMessage(client->rxbuf, client->msgsize + 6);
415                                 /* pass messsage to handler */
416                                 if (msg)
417                                         Mh_handle_message(client, msg);
418                                 client->rxcount = client->msgsize = 0;
419                         }
420                 } else /* rc <= 0 */ {
421                         if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_WANT_READ) {
422                                 return 0;
423                         }
424                         else if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_WANT_WRITE) {
425                                 client->readBlockedOnWrite = true;
426                                 return 0;
427                         }
428                         else if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_ZERO_RETURN) {
429                                 Log_info_client(client, "Connection closed by peer");
430                                 if (!client->shutdown_wait)
431                                         Client_close(client);
432                         }
433                         else {
434                                 if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_SYSCALL) {
435                                         Log_info_client(client, "Connection closed by peer");
436                                 }
437                                 else {
438                                         Log_info_client(client, "SSL error: %d - Closing connection", SSLi_get_error(client->ssl, rc));
439                                 }
440                                 Client_free(client);
441                                 return -1;
442                         }
443                 }
444         } while (SSLi_data_pending(client->ssl));
445         
446         return 0;
447 }
448
449 int Client_write_fd(int fd)
450 {
451         struct dlist *itr;
452         client_t *client = NULL;
453         
454         list_iterate(itr, &clients) {
455                 if(fd == list_get_entry(itr, client_t, node)->tcpfd) {
456                         client = list_get_entry(itr, client_t, node);
457                         break;
458                 }
459         }
460         if (client == NULL)
461                 Log_fatal("No client found for fd %d", fd);
462         Client_write(client);
463         return 0;
464 }
465
466 int Client_write(client_t *client)
467 {
468         int rc;
469         
470         if (client->readBlockedOnWrite) {
471                 client->readBlockedOnWrite = false;
472                 Log_debug("Client_write: readBlockedOnWrite == true");
473                 return Client_read(client);
474         }
475         rc = SSLi_write(client->ssl, &client->txbuf[client->txcount], client->txsize - client->txcount);
476         if (rc > 0) {
477                 client->txcount += rc;
478                 if (client->txcount == client->txsize)
479                         client->txsize = client->txcount = 0;
480         }
481         else if (rc < 0) {
482                 if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_WANT_READ) {
483                         client->writeBlockedOnRead = true;
484                         return 0;
485                 }
486                 else if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_WANT_WRITE) {
487                         return 0;
488                 }
489                 else {
490                         if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_SYSCALL)
491                                 Log_warn("Client_write: Error: %s  - Closing connection", strerror(errno));
492                         else
493                                 Log_warn("Client_write: SSL error: %d - Closing connection.", SSLi_get_error(client->ssl, rc));
494                         Client_free(client);
495                         return -1;
496                 }
497         }
498         if (client->txsize == 0 && !list_empty(&client->txMsgQueue)) {
499                 message_t *msg;
500                 msg = list_get_entry(list_get_first(&client->txMsgQueue), message_t, node);
501                 list_del(list_get_first(&client->txMsgQueue));
502                 client->txQueueCount--;
503                 Client_send_message(client, msg);
504         }
505         return 0;
506 }
507
508 int Client_send_message_ver(client_t *client, message_t *msg, uint32_t version)
509 {
510         if ((version == 0) || (client->version >= version) ||
511                 ((version & 0x80000000) && (client->version < (~version))))
512                 return Client_send_message(client, msg);
513         else
514                 Msg_free(msg);
515 }
516
517 int Client_send_message(client_t *client, message_t *msg)
518 {
519         if (!client->authenticated && msg->messageType != Version) {
520                 Msg_free(msg);
521                 return 0;
522         }
523         if (client->txsize != 0 || !client->SSLready) {
524                 /* Queue message */
525                 if ((client->txQueueCount > 5 &&  msg->messageType == UDPTunnel) ||
526                         client->txQueueCount > 30) {
527                         Msg_free(msg);
528                         return -1;
529                 }
530                 client->txQueueCount++;
531                 list_add_tail(&msg->node, &client->txMsgQueue);
532                 Log_debug("Queueing message");
533         } else {
534                 int len;
535                 len = Msg_messageToNetwork(msg, client->txbuf);
536                 doAssert(len < BUFSIZE);
537
538                 client->txsize = len;
539                 client->txcount = 0;
540                 Client_write(client);
541                 Msg_free(msg);
542         }
543         return 0;
544 }
545
546 client_t *Client_iterate(client_t **client_itr)
547 {
548         client_t *c = *client_itr;
549
550         if (list_empty(&clients))
551                 return NULL;
552         
553         if (c == NULL) {
554                 c = list_get_entry(list_get_first(&clients), client_t, node);
555         } else {
556                 if (list_get_next(&c->node) == &clients)
557                         c = NULL;
558                 else
559                         c = list_get_entry(list_get_next(&c->node), client_t, node);
560         }
561         *client_itr = c;
562         return c;
563 }
564
565
566 int Client_send_message_except(client_t *client, message_t *msg)
567 {
568         client_t *itr = NULL;
569         int count = 0;
570         
571         Msg_inc_ref(msg); /* Make sure a reference is held during the whole iteration. */
572         while (Client_iterate(&itr) != NULL) {
573                 if (itr != client) {
574                         if (count++ > 0)
575                                 Msg_inc_ref(msg); /* One extra reference for each new copy */
576                         Log_debug("Msg %d to %s refcount %d",  msg->messageType, itr->username, msg->refcount);
577                         Client_send_message(itr, msg);
578                 }
579         }
580         Msg_free(msg); /* Free our reference to the message */
581         
582         if (count == 0)
583                 Msg_free(msg); /* If only 1 client is connected then no message is passed
584                                                 * to Client_send_message(). Free it here. */
585                 
586         return 0;
587 }
588
589 int Client_send_message_except_ver(client_t *client, message_t *msg, uint32_t version)
590 {
591         client_t *itr = NULL;
592         int count = 0;
593         
594         Msg_inc_ref(msg); /* Make sure a reference is held during the whole iteration. */
595         while (Client_iterate(&itr) != NULL) {
596                 if (itr != client) {
597                         if (count++ > 0)
598                                 Msg_inc_ref(msg); /* One extra reference for each new copy */
599                         Log_debug("Msg %d to %s refcount %d",  msg->messageType, itr->username, msg->refcount);
600                         Client_send_message_ver(itr, msg, version);
601                 }
602         }
603         Msg_free(msg); /* Free our reference to the message */
604         
605         if (count == 0)
606                 Msg_free(msg); /* If only 1 client is connected then no message is passed
607                                                 * to Client_send_message(). Free it here. */
608                 
609         return 0;
610 }
611
612 static bool_t checkDecrypt(client_t *client, const uint8_t *encrypted, uint8_t *plain, unsigned int len)
613 {
614         if (CryptState_isValid(&client->cryptState) &&
615                 CryptState_decrypt(&client->cryptState, encrypted, plain, len))
616                 return true;
617
618         if (Timer_elapsed(&client->cryptState.tLastGood) > 5000000ULL) {
619                 if (Timer_elapsed(&client->cryptState.tLastRequest) > 5000000ULL) {
620                         message_t *sendmsg;
621                         Timer_restart(&client->cryptState.tLastRequest);
622                         
623                         sendmsg = Msg_create(CryptSetup);
624                         Log_info_client(client, "Requesting voice channel crypt resync");               
625                         Client_send_message(client, sendmsg);
626                 }
627         }
628         return false;
629 }
630
631 #define UDP_PACKET_SIZE 1024
632 int Client_read_udp()
633 {
634         int len;
635         struct sockaddr_in from;
636         socklen_t fromlen = sizeof(struct sockaddr_in);
637         uint64_t key;
638         client_t *itr;
639         UDPMessageType_t msgType;
640         
641 #if defined(__LP64__)
642         uint8_t encbuff[UDP_PACKET_SIZE + 8];
643         uint8_t *encrypted = encbuff + 4;
644 #else
645         uint8_t encrypted[UDP_PACKET_SIZE];
646 #endif
647         uint8_t buffer[UDP_PACKET_SIZE];
648         
649         len = recvfrom(udpsock, encrypted, UDP_PACKET_SIZE, MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
650         if (len == 0) {
651                 return -1;
652         } else if (len < 0) {
653                 return -1;
654         } else if (len < 5) {
655                 // 4 bytes crypt header + type + session
656                 return 0;
657         } else if (len > UDP_PACKET_SIZE) {
658                 return 0;
659         }
660
661         /* Ping packet */
662         if (len == 12 && *encrypted == 0) {
663                 uint32_t *ping = (uint32_t *)encrypted;
664                 ping[0] = htonl((uint32_t)PROTOCOL_VERSION);
665                 // 1 and 2 will be the timestamp, which we return unmodified.
666                 ping[3] = htonl((uint32_t)clientcount);
667                 ping[4] = htonl((uint32_t)getIntConf(MAX_CLIENTS));
668                 ping[5] = htonl((uint32_t)getIntConf(MAX_BANDWIDTH));
669                 
670                 sendto(udpsock, encrypted, 6 * sizeof(uint32_t), 0, (struct sockaddr *)&from, fromlen);
671                 return 0;
672         }
673         
674         key = (((uint64_t)from.sin_addr.s_addr) << 16) ^ from.sin_port;
675         itr = NULL;
676         
677         while (Client_iterate(&itr) != NULL) {
678                 if (itr->key == key) {
679                         if (!checkDecrypt(itr, encrypted, buffer, len))
680                                 goto out;
681                         break;
682                 }
683         }       
684         if (itr == NULL) { /* Unknown peer */
685                 while (Client_iterate(&itr) != NULL) {
686                         if (itr->remote_tcp.sin_addr.s_addr == from.sin_addr.s_addr) {
687                                 if (checkDecrypt(itr, encrypted, buffer, len)) {
688                                         itr->key = key;
689                                         Log_info_client(itr, "New UDP connection port %d", ntohs(from.sin_port));
690                                         memcpy(&itr->remote_udp, &from, sizeof(struct sockaddr_in));
691                                         break;
692                                 }
693                         }
694                 } /* while */
695         }
696         if (itr == NULL) { /* Couldn't find this peer among connected clients */
697                 goto out;
698         }
699         
700         itr->bUDP = true;
701         len -= 4; /* Adjust for crypt header */
702         msgType = (UDPMessageType_t)((buffer[0] >> 5) & 0x7);
703         switch (msgType) {
704         case UDPVoiceSpeex:
705         case UDPVoiceCELTAlpha:
706         case UDPVoiceCELTBeta:
707                 Client_voiceMsg(itr, buffer, len);
708                 break;
709         case UDPPing:
710                 Log_debug("UDP Ping reply len %d", len);
711                 Client_send_udp(itr, buffer, len);
712                 break;
713         default:
714                 Log_debug("Unknown UDP message type from %s port %d", inet_ntoa(from.sin_addr), ntohs(from.sin_port));
715                 break;
716         }
717         
718 out:
719         return 0;
720 }
721
722 static inline void Client_send_voice(client_t *src, client_t *dst, uint8_t *data, int len, int poslen)
723 {
724         if (IS_AUTH(dst) && dst != src && !dst->deaf) {
725                 if (poslen > 0 && /* Has positional data */
726                         src->context != NULL && dst->context != NULL && /* ...both source and destination has context */
727                         strcmp(src->context, dst->context) == 0) /* ...and the contexts match */
728                         Client_send_udp(dst, data, len);
729                 else 
730                         Client_send_udp(dst, data, len - poslen);
731         }
732 }
733
734 /* Handle decrypted voice message */
735 int Client_voiceMsg(client_t *client, uint8_t *data, int len)
736 {
737         uint8_t buffer[UDP_PACKET_SIZE];
738         pds_t *pdi = Pds_create(data + 1, len - 1);
739         pds_t *pds = Pds_create(buffer + 1, UDP_PACKET_SIZE - 1);
740         unsigned int type = data[0] & 0xe0;
741         unsigned int target = data[0] & 0x1f;
742         unsigned int poslen, counter;
743         int offset, packetsize;
744         voicetarget_t *vt;
745         
746         channel_t *ch = (channel_t *)client->channel;
747         struct dlist *itr;
748         
749         if (!client->authenticated || client->mute)
750                 goto out;
751         
752         packetsize = 20 + 8 + 4 + len;
753         if (client->availableBandwidth - packetsize < 0)
754                 goto out; /* Discard */
755         client->availableBandwidth -= packetsize;
756         
757         Timer_restart(&client->idleTime);
758         
759         counter = Pds_get_numval(pdi); /* step past session id */
760         do {
761                 counter = Pds_next8(pdi);
762                 offset = Pds_skip(pdi, counter & 0x7f);
763         } while ((counter & 0x80) && offset > 0);
764
765         poslen = pdi->maxsize - pdi->offset; /* For stripping of positional info */
766         
767         Pds_add_numval(pds, client->sessionId);
768         Pds_append_data_nosize(pds, data + 1, len - 1);
769         
770         if (target == 0x1f) { /* Loopback */
771                 buffer[0] = (uint8_t) type;
772                 Client_send_udp(client, buffer, pds->offset + 1);
773         }
774         else if (target == 0) { /* regular channel speech */
775                 buffer[0] = (uint8_t) type;
776                 
777                 if (ch == NULL)
778                         goto out;
779                 
780                 list_iterate(itr, &ch->clients) {
781                         client_t *c;
782                         c = list_get_entry(itr, client_t, chan_node);
783                         Client_send_voice(client, c, buffer, pds->offset + 1, poslen);
784                 }
785         } else if ((vt = Voicetarget_get_id(client, target)) != NULL) { /* Targeted whisper */
786                 int i;
787                 channel_t *ch;
788                 /* Channels */
789                 for (i = 0; i < TARGET_MAX_CHANNELS && vt->channels[i].channel != -1; i++) {
790                         buffer[0] = (uint8_t) (type | 1);
791                         Log_debug("Whisper channel %d", vt->channels[i]);
792                         ch = Chan_fromId(vt->channels[i].channel);
793                         if (ch == NULL)
794                                 continue;
795                         list_iterate(itr, &ch->clients) {
796                                 client_t *c;
797                                 c = list_get_entry(itr, client_t, chan_node);
798                                 Client_send_voice(client, c, buffer, pds->offset + 1, poslen);
799                         }
800                         /* Channel links */
801                         if (vt->channels[i].linked && !list_empty(&ch->channel_links)) {
802                                 struct dlist *ch_itr;
803                                 list_iterate(ch_itr, &ch->channel_links) {
804                                         channel_t *ch_link;
805                                         ch_link = list_get_entry(ch_itr, channel_t, link_node);
806                                         list_iterate(itr, &ch_link->clients) {
807                                                 client_t *c;
808                                                 c = list_get_entry(itr, client_t, chan_node);
809                                                 Log_debug("Linked voice from %s -> %s", ch->name, ch_link->name);
810                                                 Client_send_voice(client, c, buffer, pds->offset + 1, poslen);
811                                         }
812                                 }
813                         }
814                         /* children */
815                         if (vt->channels[i].children) {
816                                 struct dlist chanlist, *ch_itr;
817                                 init_list_entry(&chanlist);
818                                 Chan_buildTreeList(ch, &chanlist);
819                                 list_iterate(ch_itr, &chanlist) {
820                                         channel_t *sub;
821                                         sub = list_get_entry(ch_itr, channellist_t, node)->chan;
822                                         list_iterate(itr, &sub->clients) {
823                                                 client_t *c;
824                                                 c = list_get_entry(itr, client_t, chan_node);
825                                                 Log_debug("Child voice from %s -> %s", ch->name, sub->name);
826                                                 Client_send_voice(client, c, buffer, pds->offset + 1, poslen);
827                                         }
828                                 }
829                                 Chan_freeTreeList(&chanlist);
830                         }
831                 }                       
832                 /* Sessions */
833                 for (i = 0; i < TARGET_MAX_SESSIONS && vt->sessions[i] != -1; i++) {
834                         client_t *c;
835                         buffer[0] = (uint8_t) (type | 2);
836                         Log_debug("Whisper session %d", vt->sessions[i]);
837                         while (Client_iterate(&c) != NULL) {
838                                 if (c->sessionId == vt->sessions[i]) {
839                                         Client_send_voice(client, c, buffer, pds->offset + 1, poslen);
840                                         break;
841                                 }
842                         }
843                 }
844         }
845 out:
846         Pds_free(pds);
847         Pds_free(pdi);
848         
849         return 0;
850 }
851
852
853 static int Client_send_udp(client_t *client, uint8_t *data, int len)
854 {
855         uint8_t *buf, *mbuf;
856
857         if (client->remote_udp.sin_port != 0 && CryptState_isValid(&client->cryptState) &&
858                 client->bUDP) {
859 #if defined(__LP64__)
860                 buf = mbuf = malloc(len + 4 + 16);
861                 buf += 4;
862 #else
863                 mbuf = buf = malloc(len + 4);
864 #endif
865                 if (mbuf == NULL)
866                         Log_fatal("Out of memory");
867                 
868                 CryptState_encrypt(&client->cryptState, data, buf, len);
869                 
870                 sendto(udpsock, buf, len + 4, 0, (struct sockaddr *)&client->remote_udp, sizeof(struct sockaddr_in));
871                 
872                 free(mbuf);
873         } else {
874                 message_t *msg;
875                 msg = Msg_CreateVoiceMsg(data, len);
876                 Client_send_message(client, msg);
877         }
878         return 0;
879 }