Opus support continued. Negotiating stuff. Working in server loopback mode.
[umurmur.git] / src / client.c
1 /* Copyright (C) 2009-2012, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2012, 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 #include "ban.h"
49
50 extern char system_string[], version_string[];
51
52 static int Client_read(client_t *client);
53 static int Client_write(client_t *client);
54 static int Client_send_udp(client_t *client, uint8_t *data, int len);
55 void Client_free(client_t *client);
56
57 declare_list(clients);
58 static int clientcount; /* = 0 */
59 static int maxBandwidth;
60 bool_t bOpus = true;
61
62 int iCodecAlpha, iCodecBeta;
63 bool_t bPreferAlpha;
64
65 extern int udpsock;
66
67 void Client_init()
68 {
69         maxBandwidth = getIntConf(MAX_BANDWIDTH) / 8; /* From bits/s -> bytes/s */
70 }
71
72 int Client_count()
73 {
74         return clientcount;
75 }
76
77 int Client_getfds(struct pollfd *pollfds)
78 {
79         struct dlist *itr;
80         int i = 0;
81         list_iterate(itr, &clients) {
82                 client_t *c;
83                 c = list_get_entry(itr, client_t, node);
84                 pollfds[i].fd = c->tcpfd;
85                 pollfds[i].events = POLLIN | POLLHUP | POLLERR;
86                 if (c->txsize > 0 || c->readBlockedOnWrite) /* Data waiting to be sent? */
87                         pollfds[i].events |= POLLOUT;
88                 i++;
89         }
90         return i;
91 }
92
93 void Client_janitor()
94 {
95         struct dlist *itr, *save;
96         int bwTop = maxBandwidth + maxBandwidth / 4;
97         list_iterate_safe(itr, save, &clients) {
98                 client_t *c;
99                 c = list_get_entry(itr, client_t, node);
100                 Log_debug("Client %s BW available %d", c->username, c->availableBandwidth);
101                 c->availableBandwidth += maxBandwidth;
102                 if (c->availableBandwidth > bwTop)
103                         c->availableBandwidth = bwTop;
104                 
105                 if (Timer_isElapsed(&c->lastActivity, 1000000LL * INACTIVITY_TIMEOUT)) {
106                         /* No activity from client - assume it is lost and close. */
107                         Log_info_client(c, "Timeout, closing.");
108                         Client_free(c);
109                 }
110         }
111         Ban_pruneBanned();
112 }
113
114 void Client_codec_add(client_t *client, int codec)
115 {
116         codec_t *cd = malloc(sizeof(codec_t));
117         if (cd == NULL)
118                 Log_fatal("Out of memory");
119         init_list_entry(&cd->node);
120         cd->codec = codec;
121         list_add_tail(&cd->node, &client->codecs);
122 }
123
124 void Client_codec_free(client_t *client)
125 {
126         struct dlist *itr, *save;
127         list_iterate_safe(itr, save, &client->codecs) {
128                 list_del(&list_get_entry(itr, codec_t, node)->node);
129                 free(list_get_entry(itr, codec_t, node));
130         }
131 }
132
133 codec_t *Client_codec_iterate(client_t *client, codec_t **codec_itr)
134 {
135         codec_t *cd = *codec_itr;
136
137         if (list_empty(&client->codecs))
138                 return NULL;
139         
140         if (cd == NULL) {
141                 cd = list_get_entry(list_get_first(&client->codecs), codec_t, node);
142         } else {
143                 if (list_get_next(&cd->node) == &client->codecs)
144                         cd = NULL;
145                 else
146                         cd = list_get_entry(list_get_next(&cd->node), codec_t, node);
147         }
148         *codec_itr = cd;
149         return cd;
150 }
151
152 void Client_token_add(client_t *client, char *token_string)
153 {
154         token_t *token;
155
156         if (client->tokencount >= MAX_TOKENS)
157                 return;
158         token = malloc(sizeof(token_t));
159         if (token == NULL)
160                 Log_fatal("Out of memory");
161         init_list_entry(&token->node);
162         token->token = strdup(token_string);
163         if (token->token == NULL)
164                 Log_fatal("Out of memory");
165         list_add_tail(&token->node, &client->tokens);
166         client->tokencount++;
167 }
168
169 bool_t Client_token_match(client_t *client, char *str)
170 {
171         token_t *token;
172         struct dlist *itr;
173         
174         if (list_empty(&client->tokens))
175                 return false;
176         list_iterate(itr, &client->tokens) {
177                 token = list_get_entry(itr, token_t, node);
178                 if (strncasecmp(token->token, str, MAX_TOKENSIZE) == 0)
179                         return true;
180         }
181         return false;
182 }
183
184 void Client_token_free(client_t *client)
185 {
186         struct dlist *itr, *save;
187         token_t *token;
188         
189         list_iterate_safe(itr, save, &client->tokens) {
190                 token = list_get_entry(itr, token_t, node);
191                 list_del(&token->node);
192                 free(token->token);
193                 free(token);
194         }
195         client->tokencount = 0;
196 }
197
198
199 #define OPUS_WARN "<strong>WARNING:</strong> Your client doesn't support the Opus codec the server is using, you won't be able to talk or hear anyone. Please upgrade"
200 void recheckCodecVersions(client_t *connectingClient)
201 {
202         client_t *client_itr = NULL;
203         int max = 0, version, current_version;
204         int users = 0, opus = 0;
205         message_t *sendmsg;
206         struct dlist codec_list, *itr, *save;
207         codec_t *codec_itr, *cd;
208         bool_t found;
209         bool_t enableOpus;
210
211         init_list_entry(&codec_list);
212         
213         while (Client_iterate(&client_itr) != NULL) {
214                 codec_itr = NULL;
215                 if (client_itr->codec_count == 0 && !client_itr->bOpus)
216                         continue;
217                 while (Client_codec_iterate(client_itr, &codec_itr) != NULL) {
218                         found = false;                      
219                         list_iterate(itr, &codec_list) {
220                                 cd = list_get_entry(itr, codec_t, node);
221                                 if (cd->codec == codec_itr->codec) {
222                                         cd->count++;
223                                         found = true;
224                                 }
225                         }
226                         if (!found) {
227                                 cd = malloc(sizeof(codec_t));
228                                 if (!cd)
229                                         Log_fatal("Out of memory");
230                                 memset(cd, 0, sizeof(codec_t));
231                                 init_list_entry(&cd->node);
232                                 cd->codec = codec_itr->codec;
233                                 cd->count = 1;
234                                 list_add_tail(&cd->node, &codec_list);
235                         }
236                 }
237                 users++;
238                 if (client_itr->bOpus)
239                         opus++;
240         }
241
242         if (users == 0) 
243                 return;
244
245         enableOpus = ((opus * 100 / users) >= getIntConf(OPUS_THRESHOLD));
246
247         list_iterate(itr, &codec_list) {
248                 cd = list_get_entry(itr, codec_t, node);
249                 if (cd->count > max) {
250                         max = cd->count;
251                         version = cd->codec;
252                 }
253         }
254         list_iterate_safe(itr, save, &codec_list) {
255                 list_del(&list_get_entry(itr, codec_t, node)->node);
256                 free(list_get_entry(itr, codec_t, node));
257         }
258
259         current_version = bPreferAlpha ? iCodecAlpha : iCodecBeta;
260         if (current_version != version) {
261                 // If we don't already use the compat bitstream version set
262                 // it as alpha and announce it. If another codec now got the
263                 // majority set it as the opposite of the currently valid bPreferAlpha
264                 // and announce it.
265                 if (version == (uint32_t)0x8000000b)
266                         bPreferAlpha = true;
267                 else
268                         bPreferAlpha = !bPreferAlpha;
269                 
270                 if (bPreferAlpha)
271                         iCodecAlpha = version;
272                 else
273                         iCodecBeta = version;
274         } else if (bOpus == enableOpus) {
275                 if (connectingClient && !connectingClient->bOpus) {
276                         char *message;
277                         uint32_t *tree_id;
278                         message_t *sendmsg = NULL;
279
280                         message = malloc(strlen(OPUS_WARN) + 1);
281                         if (!message)
282                                 Log_fatal("Out of memory");
283                         tree_id = malloc(sizeof(uint32_t));
284                         if (!tree_id)
285                                 Log_fatal("Out of memory");
286                         *tree_id = 0;
287                         sendmsg = Msg_create(TextMessage);
288                         sendmsg->payload.textMessage->message = message;
289                         sendmsg->payload.textMessage->n_tree_id = 1;
290                         sendmsg->payload.textMessage->tree_id = tree_id;
291                         sprintf(message, OPUS_WARN);
292                         Client_send_message(connectingClient, sendmsg);
293                         sendmsg = NULL;
294                 }
295                 return;
296         }
297         
298         bOpus = enableOpus;
299         Log_info("OPUS codec is %s", bOpus ? "enabled" : "disabled");
300
301         sendmsg = Msg_create(CodecVersion);
302         sendmsg->payload.codecVersion->alpha = iCodecAlpha;
303         sendmsg->payload.codecVersion->beta = iCodecBeta;
304         sendmsg->payload.codecVersion->prefer_alpha = bPreferAlpha;
305         sendmsg->payload.codecVersion->has_opus = true;
306         sendmsg->payload.codecVersion->opus = bOpus;
307
308         Client_send_message_except(NULL, sendmsg);
309         
310         Log_info("CELT codec switch 0x%x 0x%x (prefer 0x%x)", iCodecAlpha, iCodecBeta,
311                          bPreferAlpha ? iCodecAlpha : iCodecBeta);
312
313         client_itr = NULL;
314         while (Client_iterate(&client_itr) != NULL) {
315                 if ((client_itr->authenticated || client_itr == connectingClient) &&
316                     !client_itr->bOpus) {
317                         char *message;
318                         uint32_t *tree_id;
319                         message_t *sendmsg = NULL;
320
321                         message = malloc(strlen(OPUS_WARN) + 1);
322                         if (!message)
323                                 Log_fatal("Out of memory");
324                         tree_id = malloc(sizeof(uint32_t));
325                         if (!tree_id)
326                                 Log_fatal("Out of memory");
327                         *tree_id = 0;
328                         sendmsg = Msg_create(TextMessage);
329                         sendmsg->payload.textMessage->message = message;
330                         sendmsg->payload.textMessage->n_tree_id = 1;
331                         sendmsg->payload.textMessage->tree_id = tree_id;
332                         sprintf(message, OPUS_WARN);
333                         Client_send_message(client_itr, sendmsg);
334                         sendmsg = NULL;
335                 }
336         }
337         
338 }
339
340 static int findFreeSessionId()
341 {
342         int id;
343         client_t *itr = NULL;
344
345         for (id = 1; id < INT_MAX; id++) {
346                 itr = NULL;
347                 while ((itr = Client_iterate(&itr)) != NULL) {
348                         if (itr->sessionId == id)
349                                 break;
350                 }
351                 if (itr == NULL) /* Found free id */
352                         return id;
353         }
354         return -1;
355 }
356
357 int Client_add(int fd, struct sockaddr_in *remote)
358 {
359         client_t *newclient;
360         message_t *sendmsg;
361
362         if (Ban_isBannedAddr((in_addr_t *)&remote->sin_addr)) {
363                 Log_info("Address %s banned. Disconnecting", inet_ntoa(remote->sin_addr));
364                 return -1;
365         }
366         newclient = malloc(sizeof(client_t));
367         if (newclient == NULL)
368                 Log_fatal("Out of memory");
369         memset(newclient, 0, sizeof(client_t));
370
371         newclient->tcpfd = fd;
372         memcpy(&newclient->remote_tcp, remote, sizeof(struct sockaddr_in));
373         newclient->ssl = SSLi_newconnection(&newclient->tcpfd, &newclient->SSLready);
374         if (newclient->ssl == NULL) {
375                 Log_warn("SSL negotiation failed with %s:%d", inet_ntoa(remote->sin_addr),
376                                  ntohs(remote->sin_port));
377                 free(newclient);
378                 return -1;
379         }
380         newclient->availableBandwidth = maxBandwidth;
381         Timer_init(&newclient->lastActivity);
382         Timer_init(&newclient->connectTime);
383         Timer_init(&newclient->idleTime);
384         newclient->sessionId = findFreeSessionId();
385         if (newclient->sessionId < 0)
386                 Log_fatal("Could not find a free session ID");
387         
388         init_list_entry(&newclient->txMsgQueue);
389         init_list_entry(&newclient->chan_node);
390         init_list_entry(&newclient->node);
391         init_list_entry(&newclient->voicetargets);
392         init_list_entry(&newclient->codecs);
393         init_list_entry(&newclient->tokens);
394         
395         list_add_tail(&newclient->node, &clients);
396         clientcount++;
397         
398         /* Send version message to client */
399         sendmsg = Msg_create(Version);
400         sendmsg->payload.version->has_version = true;
401         sendmsg->payload.version->version = PROTOCOL_VERSION;
402         sendmsg->payload.version->release = strdup(UMURMUR_VERSION);
403         sendmsg->payload.version->os = strdup(system_string);
404         sendmsg->payload.version->os_version = strdup(version_string);
405         Client_send_message(newclient, sendmsg);
406
407         return 0;
408 }
409
410 void Client_free(client_t *client)
411 {
412         struct dlist *itr, *save;
413         message_t *sendmsg;
414
415         if (client->authenticated) {
416                 int leave_id;
417                 leave_id = Chan_userLeave(client);
418                 if (leave_id > 0) { /* Remove temp channel */
419                         sendmsg = Msg_create(ChannelRemove);
420                         sendmsg->payload.channelRemove->channel_id = leave_id;
421                         Client_send_message_except(client, sendmsg);
422                 }
423                 sendmsg = Msg_create(UserRemove);
424                 sendmsg->payload.userRemove->session = client->sessionId;
425                 Client_send_message_except(client, sendmsg);
426                 recheckCodecVersions(NULL); /* Can use better codec now? */
427         }
428         list_iterate_safe(itr, save, &client->txMsgQueue) {
429                 list_del(&list_get_entry(itr, message_t, node)->node);
430                 Msg_free(list_get_entry(itr, message_t, node));
431         }
432         Client_codec_free(client);
433         Voicetarget_free_all(client);
434         Client_token_free(client);
435         
436         list_del(&client->node);
437         if (client->ssl)
438                 SSLi_free(client->ssl);
439         close(client->tcpfd);
440         clientcount--;
441         if (client->release)
442                 free(client->release);
443         if (client->os)
444                 free(client->os);                       
445         if (client->os_version)
446                 free(client->os_version);                       
447         if (client->username)
448                 free(client->username);
449         if (client->context)
450                 free(client->context);
451         free(client);
452 }
453
454 void Client_close(client_t *client)
455 {
456         SSLi_shutdown(client->ssl);
457         client->shutdown_wait = true;
458 }
459
460 void Client_disconnect_all()
461 {
462         struct dlist *itr, *save;
463         
464         list_iterate_safe(itr, save, &clients) {
465                 Client_free(list_get_entry(itr, client_t, node));
466         }
467 }
468
469 int Client_read_fd(int fd)
470 {
471         struct dlist *itr;
472         client_t *client = NULL;
473         
474         list_iterate(itr, &clients) {
475                 if (fd == list_get_entry(itr, client_t, node)->tcpfd) {
476                         client = list_get_entry(itr, client_t, node);
477                         break;
478                 }
479         }
480         if (client != NULL)
481                 return Client_read(client);
482         else
483                 return -1;
484 }
485
486 int Client_read(client_t *client)
487 {
488         int rc;
489
490         Timer_restart(&client->lastActivity);
491         
492         if (client->writeBlockedOnRead) {
493                 client->writeBlockedOnRead = false;
494                 Log_debug("Client_read: writeBlockedOnRead == true");
495                 return Client_write(client);
496         }
497         
498         if (client->shutdown_wait) {
499                 Client_free(client);
500                 return 0;
501         }
502         if (!client->SSLready) {
503                 int rc;
504                 rc = SSLi_nonblockaccept(client->ssl, &client->SSLready);
505                 if (rc < 0) {
506                         Client_free(client);
507                         return -1;
508                 }
509         }
510
511         do {
512                 errno = 0;
513                 if (!client->msgsize) 
514                         rc = SSLi_read(client->ssl, &client->rxbuf[client->rxcount], 6 - client->rxcount);
515                 else
516                         rc = SSLi_read(client->ssl, &client->rxbuf[client->rxcount], client->msgsize);
517                 if (rc > 0) {
518                         message_t *msg;
519                         client->rxcount += rc;
520                         if (!client->msgsize && client->rxcount >= 6) {
521                                 uint32_t msgLen;
522                                 memcpy(&msgLen, &client->rxbuf[2], sizeof(uint32_t));
523                                 client->msgsize = ntohl(msgLen);
524                         }
525                         if (client->msgsize > BUFSIZE - 6) {
526                                 /* XXX - figure out how to handle this. A large size here can represent two cases:
527                                  * 1. A valid size. The only message that is this big is UserState message with a big texture
528                                  * 2. An invalid size = protocol error, e.g. connecting with a 1.1.x client
529                                  */
530                                 Log_warn("Too big message received (%d bytes). Playing safe and disconnecting client %s:%d",
531                                                  client->msgsize, inet_ntoa(client->remote_tcp.sin_addr), ntohs(client->remote_tcp.sin_port));
532                                 Client_free(client);
533                                 return -1;
534                                 /* client->rxcount = client->msgsize = 0; */
535                         }
536                         else if (client->rxcount == client->msgsize + 6) { /* Got all of the message */
537                                 msg = Msg_networkToMessage(client->rxbuf, client->msgsize + 6);
538                                 /* pass messsage to handler */
539                                 if (msg)
540                                         Mh_handle_message(client, msg);
541                                 client->rxcount = client->msgsize = 0;
542                         }
543                 } else /* rc <= 0 */ {
544                         if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_WANT_READ) {
545                                 return 0;
546                         }
547                         else if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_WANT_WRITE) {
548                                 client->readBlockedOnWrite = true;
549                                 return 0;
550                         }
551                         else if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_ZERO_RETURN) {
552                                 Log_info_client(client, "Connection closed by peer");
553                                 if (!client->shutdown_wait)
554                                         Client_close(client);
555                         }
556                         else {
557                                 if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_SYSCALL) {
558                                         Log_info_client(client,"Error: %s  - Closing connection", strerror(errno));
559                                 }
560                                 else if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_CONNRESET) {
561                                         Log_info_client(client, "Connection reset by peer");
562                                 }
563                                 else {
564                                         Log_info_client(client, "SSL error: %d - Closing connection", SSLi_get_error(client->ssl, rc));
565                                 }
566                                 Client_free(client);
567                                 return -1;
568                         }
569                 }
570         } while (SSLi_data_pending(client->ssl));
571         
572         return 0;
573 }
574
575 int Client_write_fd(int fd)
576 {
577         struct dlist *itr;
578         client_t *client = NULL;
579         
580         list_iterate(itr, &clients) {
581                 if(fd == list_get_entry(itr, client_t, node)->tcpfd) {
582                         client = list_get_entry(itr, client_t, node);
583                         break;
584                 }
585         }
586         if (client != NULL)
587                 return Client_write(client);
588         else
589                 return -1;
590 }
591
592 int Client_write(client_t *client)
593 {
594         int rc;
595         
596         if (client->readBlockedOnWrite) {
597                 client->readBlockedOnWrite = false;
598                 Log_debug("Client_write: readBlockedOnWrite == true");
599                 return Client_read(client);
600         }
601         rc = SSLi_write(client->ssl, &client->txbuf[client->txcount], client->txsize - client->txcount);
602         if (rc > 0) {
603                 client->txcount += rc;
604                 if (client->txcount == client->txsize)
605                         client->txsize = client->txcount = 0;
606         }
607         else if (rc < 0) {
608                 if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_WANT_READ) {
609                         client->writeBlockedOnRead = true;
610                         return 0;
611                 }
612                 else if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_WANT_WRITE) {
613                         return 0;
614                 }
615                 else {
616                         if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_SYSCALL) {
617                                 Log_info_client(client, "Error: %s  - Closing connection", strerror(errno));
618                         }
619                         else if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_CONNRESET) {
620                                 Log_info_client(client, "Connection reset by peer");
621                         }
622                         else {
623                                 Log_info_client(client, "SSL error: %d - Closing connection.", SSLi_get_error(client->ssl, rc));
624                         }
625                         Client_free(client);
626                         return -1;
627                 }
628         }
629         if (client->txsize == 0 && !list_empty(&client->txMsgQueue)) {
630                 message_t *msg;
631                 msg = list_get_entry(list_get_first(&client->txMsgQueue), message_t, node);
632                 list_del(list_get_first(&client->txMsgQueue));
633                 client->txQueueCount--;
634                 Client_send_message(client, msg);
635         }
636         return 0;
637 }
638
639 int Client_send_message_ver(client_t *client, message_t *msg, uint32_t version)
640 {
641         if ((version == 0) || (client->version >= version) ||
642                 ((version & 0x80000000) && (client->version < (~version))))
643                 return Client_send_message(client, msg);
644         else
645                 Msg_free(msg);
646 }
647
648 int Client_send_message(client_t *client, message_t *msg)
649 {
650         if (!client->authenticated && msg->messageType != Version) {
651                 Msg_free(msg);
652                 return 0;
653         }
654         if (client->txsize != 0 || !client->SSLready) {
655                 /* Queue message */
656                 if ((client->txQueueCount > 5 &&  msg->messageType == UDPTunnel) ||
657                         client->txQueueCount > 30) {
658                         Msg_free(msg);
659                         return -1;
660                 }
661                 client->txQueueCount++;
662                 list_add_tail(&msg->node, &client->txMsgQueue);
663                 Log_debug("Queueing message");
664         } else {
665                 int len;
666                 len = Msg_messageToNetwork(msg, client->txbuf);
667                 doAssert(len < BUFSIZE);
668
669                 client->txsize = len;
670                 client->txcount = 0;
671                 Client_write(client);
672                 Msg_free(msg);
673         }
674         return 0;
675 }
676
677 client_t *Client_iterate(client_t **client_itr)
678 {
679         client_t *c = *client_itr;
680
681         if (list_empty(&clients))
682                 return NULL;
683         
684         if (c == NULL) {
685                 c = list_get_entry(list_get_first(&clients), client_t, node);
686         } else {
687                 if (list_get_next(&c->node) == &clients)
688                         c = NULL;
689                 else
690                         c = list_get_entry(list_get_next(&c->node), client_t, node);
691         }
692         *client_itr = c;
693         return c;
694 }
695
696
697 int Client_send_message_except(client_t *client, message_t *msg)
698 {
699         client_t *itr = NULL;
700         int count = 0;
701         
702         Msg_inc_ref(msg); /* Make sure a reference is held during the whole iteration. */
703         while (Client_iterate(&itr) != NULL) {
704                 if (itr != client) {
705                         if (count++ > 0)
706                                 Msg_inc_ref(msg); /* One extra reference for each new copy */
707                         Log_debug("Msg %d to %s refcount %d",  msg->messageType, itr->username, msg->refcount);
708                         Client_send_message(itr, msg);
709                 }
710         }
711         Msg_free(msg); /* Free our reference to the message */
712         
713         if (count == 0)
714                 Msg_free(msg); /* If only 1 client is connected then no message is passed
715                                                 * to Client_send_message(). Free it here. */
716                 
717         return 0;
718 }
719
720 int Client_send_message_except_ver(client_t *client, message_t *msg, uint32_t version)
721 {
722         client_t *itr = NULL;
723         int count = 0;
724         
725         Msg_inc_ref(msg); /* Make sure a reference is held during the whole iteration. */
726         while (Client_iterate(&itr) != NULL) {
727                 if (itr != client) {
728                         if (count++ > 0)
729                                 Msg_inc_ref(msg); /* One extra reference for each new copy */
730                         Log_debug("Msg %d to %s refcount %d",  msg->messageType, itr->username, msg->refcount);
731                         Client_send_message_ver(itr, msg, version);
732                 }
733         }
734         Msg_free(msg); /* Free our reference to the message */
735         
736         if (count == 0)
737                 Msg_free(msg); /* If only 1 client is connected then no message is passed
738                                                 * to Client_send_message(). Free it here. */
739                 
740         return 0;
741 }
742
743 static bool_t checkDecrypt(client_t *client, const uint8_t *encrypted, uint8_t *plain, unsigned int len)
744 {
745         if (CryptState_isValid(&client->cryptState) &&
746                 CryptState_decrypt(&client->cryptState, encrypted, plain, len))
747                 return true;
748
749         if (Timer_elapsed(&client->cryptState.tLastGood) > 5000000ULL) {
750                 if (Timer_elapsed(&client->cryptState.tLastRequest) > 5000000ULL) {
751                         message_t *sendmsg;
752                         Timer_restart(&client->cryptState.tLastRequest);
753                         
754                         sendmsg = Msg_create(CryptSetup);
755                         Log_info_client(client, "Requesting voice channel crypt resync");               
756                         Client_send_message(client, sendmsg);
757                 }
758         }
759         return false;
760 }
761
762 #define UDP_PACKET_SIZE 1024
763 int Client_read_udp()
764 {
765         int len;
766         struct sockaddr_in from;
767         socklen_t fromlen = sizeof(struct sockaddr_in);
768         uint64_t key;
769         client_t *itr;
770         UDPMessageType_t msgType;
771         
772 #if defined(__LP64__)
773         uint8_t encbuff[UDP_PACKET_SIZE + 8];
774         uint8_t *encrypted = encbuff + 4;
775 #else
776         uint8_t encrypted[UDP_PACKET_SIZE];
777 #endif
778         uint8_t buffer[UDP_PACKET_SIZE];
779         
780         len = recvfrom(udpsock, encrypted, UDP_PACKET_SIZE, MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
781         if (len == 0) {
782                 return -1;
783         } else if (len < 0) {
784                 return -1;
785         } else if (len < 5) {
786                 // 4 bytes crypt header + type + session
787                 return 0;
788         } else if (len > UDP_PACKET_SIZE) {
789                 return 0;
790         }
791
792         /* Ping packet */
793         if (len == 12 && *encrypted == 0) {
794                 uint32_t *ping = (uint32_t *)encrypted;
795                 ping[0] = htonl((uint32_t)PROTOCOL_VERSION);
796                 // 1 and 2 will be the timestamp, which we return unmodified.
797                 ping[3] = htonl((uint32_t)clientcount);
798                 ping[4] = htonl((uint32_t)getIntConf(MAX_CLIENTS));
799                 ping[5] = htonl((uint32_t)getIntConf(MAX_BANDWIDTH));
800                 
801                 sendto(udpsock, encrypted, 6 * sizeof(uint32_t), 0, (struct sockaddr *)&from, fromlen);
802                 return 0;
803         }
804         
805         key = (((uint64_t)from.sin_addr.s_addr) << 16) ^ from.sin_port;
806         itr = NULL;
807         
808         while (Client_iterate(&itr) != NULL) {
809                 if (itr->key == key) {
810                         if (!checkDecrypt(itr, encrypted, buffer, len))
811                                 goto out;
812                         break;
813                 }
814         }       
815         if (itr == NULL) { /* Unknown peer */
816                 while (Client_iterate(&itr) != NULL) {
817                         if (itr->remote_tcp.sin_addr.s_addr == from.sin_addr.s_addr) {
818                                 if (checkDecrypt(itr, encrypted, buffer, len)) {
819                                         itr->key = key;
820                                         Log_info_client(itr, "New UDP connection port %d", ntohs(from.sin_port));
821                                         memcpy(&itr->remote_udp, &from, sizeof(struct sockaddr_in));
822                                         break;
823                                 }
824                         }
825                 } /* while */
826         }
827         if (itr == NULL) { /* Couldn't find this peer among connected clients */
828                 goto out;
829         }
830         
831         itr->bUDP = true;
832         len -= 4; /* Adjust for crypt header */
833         msgType = (UDPMessageType_t)((buffer[0] >> 5) & 0x7);
834         switch (msgType) {
835         case UDPVoiceSpeex:
836         case UDPVoiceCELTAlpha:
837         case UDPVoiceCELTBeta:
838                 if (bOpus)
839                         break;
840         case UDPVoiceOpus:
841                 Client_voiceMsg(itr, buffer, len);
842                 break;
843         case UDPPing:
844                 Log_debug("UDP Ping reply len %d", len);
845                 Client_send_udp(itr, buffer, len);
846                 break;
847         default:
848                 Log_debug("Unknown UDP message type from %s port %d", inet_ntoa(from.sin_addr), ntohs(from.sin_port));
849                 break;
850         }
851         
852 out:
853         return 0;
854 }
855
856 static inline void Client_send_voice(client_t *src, client_t *dst, uint8_t *data, int len, int poslen)
857 {
858         if (IS_AUTH(dst) && dst != src && !dst->deaf && !dst->self_deaf) {
859                 if (poslen > 0 && /* Has positional data */
860                         src->context != NULL && dst->context != NULL && /* ...both source and destination has context */
861                         strcmp(src->context, dst->context) == 0) /* ...and the contexts match */
862                         Client_send_udp(dst, data, len);
863                 else 
864                         Client_send_udp(dst, data, len - poslen);
865         }
866 }
867
868 /* Handle decrypted voice message */
869 int Client_voiceMsg(client_t *client, uint8_t *data, int len)
870 {
871         uint8_t buffer[UDP_PACKET_SIZE];
872         pds_t *pdi = Pds_create(data + 1, len - 1);
873         pds_t *pds = Pds_create(buffer + 1, UDP_PACKET_SIZE - 1);
874         unsigned int type = data[0] & 0xe0;
875         unsigned int target = data[0] & 0x1f;
876         unsigned int poslen, counter, size;
877         int offset, packetsize;
878         voicetarget_t *vt;
879         
880         channel_t *ch = (channel_t *)client->channel;
881         struct dlist *itr;
882         
883         if (!client->authenticated || client->mute || client->self_mute)
884                 goto out;
885         
886         packetsize = 20 + 8 + 4 + len;
887         if (client->availableBandwidth - packetsize < 0)
888                 goto out; /* Discard */
889         client->availableBandwidth -= packetsize;
890         
891         Timer_restart(&client->idleTime);
892         Timer_restart(&client->lastActivity);
893         
894         if ((type >> 5) != UDPVoiceOpus) {
895                 counter = Pds_get_numval(pdi); /* step past session id */
896                 do {
897                         counter = Pds_next8(pdi);
898                         offset = Pds_skip(pdi, counter & 0x7f);
899                 } while ((counter & 0x80) && offset > 0);
900         } else {
901                 size = Pds_get_numval(pdi);
902                 Pds_skip(pdi, size & 0x1fff);
903         }
904                 
905         poslen = pdi->maxsize - pdi->offset; /* For stripping of positional info */
906         
907         Pds_add_numval(pds, client->sessionId);
908         Pds_append_data_nosize(pds, data + 1, len - 1);
909         
910         if (target == 0x1f) { /* Loopback */
911                 buffer[0] = (uint8_t) type;
912                 Client_send_udp(client, buffer, pds->offset + 1);
913         }
914         else if (target == 0) { /* regular channel speech */
915                 buffer[0] = (uint8_t) type;
916                 
917                 if (ch == NULL)
918                         goto out;
919                 
920                 list_iterate(itr, &ch->clients) {
921                         client_t *c;
922                         c = list_get_entry(itr, client_t, chan_node);
923                         Client_send_voice(client, c, buffer, pds->offset + 1, poslen);
924                 }
925         } else if ((vt = Voicetarget_get_id(client, target)) != NULL) { /* Targeted whisper */
926                 int i;
927                 channel_t *ch;
928                 /* Channels */
929                 for (i = 0; i < TARGET_MAX_CHANNELS && vt->channels[i].channel != -1; i++) {
930                         buffer[0] = (uint8_t) (type | 1);
931                         Log_debug("Whisper channel %d", vt->channels[i]);
932                         ch = Chan_fromId(vt->channels[i].channel);
933                         if (ch == NULL)
934                                 continue;
935                         list_iterate(itr, &ch->clients) {
936                                 client_t *c;
937                                 c = list_get_entry(itr, client_t, chan_node);
938                                 Client_send_voice(client, c, buffer, pds->offset + 1, poslen);
939                         }
940                         /* Channel links */
941                         if (vt->channels[i].linked && !list_empty(&ch->channel_links)) {
942                                 struct dlist *ch_itr;
943                                 list_iterate(ch_itr, &ch->channel_links) {
944                                         channel_t *ch_link;
945                                         ch_link = list_get_entry(ch_itr, channel_t, link_node);
946                                         list_iterate(itr, &ch_link->clients) {
947                                                 client_t *c;
948                                                 c = list_get_entry(itr, client_t, chan_node);
949                                                 Log_debug("Linked voice from %s -> %s", ch->name, ch_link->name);
950                                                 Client_send_voice(client, c, buffer, pds->offset + 1, poslen);
951                                         }
952                                 }
953                         }
954                         /* children */
955                         if (vt->channels[i].children) {
956                                 struct dlist chanlist, *ch_itr;
957                                 init_list_entry(&chanlist);
958                                 Chan_buildTreeList(ch, &chanlist);
959                                 list_iterate(ch_itr, &chanlist) {
960                                         channel_t *sub;
961                                         sub = list_get_entry(ch_itr, channellist_t, node)->chan;
962                                         list_iterate(itr, &sub->clients) {
963                                                 client_t *c;
964                                                 c = list_get_entry(itr, client_t, chan_node);
965                                                 Log_debug("Child voice from %s -> %s", ch->name, sub->name);
966                                                 Client_send_voice(client, c, buffer, pds->offset + 1, poslen);
967                                         }
968                                 }
969                                 Chan_freeTreeList(&chanlist);
970                         }
971                 }                       
972                 /* Sessions */
973                 for (i = 0; i < TARGET_MAX_SESSIONS && vt->sessions[i] != -1; i++) {
974                         client_t *c;
975                         buffer[0] = (uint8_t) (type | 2);
976                         Log_debug("Whisper session %d", vt->sessions[i]);
977                         while (Client_iterate(&c) != NULL) {
978                                 if (c->sessionId == vt->sessions[i]) {
979                                         Client_send_voice(client, c, buffer, pds->offset + 1, poslen);
980                                         break;
981                                 }
982                         }
983                 }
984         }
985 out:
986         Pds_free(pds);
987         Pds_free(pdi);
988         
989         return 0;
990 }
991
992
993 static int Client_send_udp(client_t *client, uint8_t *data, int len)
994 {
995         uint8_t *buf, *mbuf;
996
997         if (client->remote_udp.sin_port != 0 && CryptState_isValid(&client->cryptState) &&
998                 client->bUDP) {
999 #if defined(__LP64__)
1000                 buf = mbuf = malloc(len + 4 + 16);
1001                 buf += 4;
1002 #else
1003                 mbuf = buf = malloc(len + 4);
1004 #endif
1005                 if (mbuf == NULL)
1006                         Log_fatal("Out of memory");
1007                 
1008                 CryptState_encrypt(&client->cryptState, data, buf, len);
1009                 
1010                 sendto(udpsock, buf, len + 4, 0, (struct sockaddr *)&client->remote_udp, sizeof(struct sockaddr_in));
1011                 
1012                 free(mbuf);
1013         } else {
1014                 message_t *msg;
1015                 msg = Msg_CreateVoiceMsg(data, len);
1016                 Client_send_message(client, msg);
1017         }
1018         return 0;
1019 }