Make logging more uniform. Fix copyright year
[umurmur.git] / src / client.c
1 /* Copyright (C) 2009-2010, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2010, 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 <errno.h>
34 #include "log.h"
35 #include "list.h"
36 #include "client.h"
37 #include "ssl.h"
38 #include "messages.h"
39 #include "messagehandler.h"
40 #include "conf.h"
41 #include "channel.h"
42 #include "version.h"
43 #include "voicetarget.h"
44
45 static int Client_read(client_t *client);
46 static int Client_write(client_t *client);
47 static int Client_send_udp(client_t *client, uint8_t *data, int len);
48 void Client_free(client_t *client);
49
50 declare_list(clients);
51 static int clientcount; /* = 0 */
52 static int session = 1;
53 static int maxBandwidth;
54
55 int iCodecAlpha, iCodecBeta;
56 bool_t bPreferAlpha;
57
58 extern int udpsock;
59
60 void Client_init()
61 {
62         maxBandwidth = getIntConf(MAX_BANDWIDTH) / 8; /* From bits/s -> bytes/s */
63 }
64
65 int Client_count()
66 {
67         return clientcount;
68 }
69
70 int Client_getfds(struct pollfd *pollfds)
71 {
72         struct dlist *itr;
73         int i = 0;
74         list_iterate(itr, &clients) {
75                 client_t *c;
76                 c = list_get_entry(itr, client_t, node);
77                 pollfds[i].fd = c->tcpfd;
78                 pollfds[i].events = POLLIN | POLLHUP | POLLERR;
79                 if (c->txsize > 0 || c->readBlockedOnWrite) /* Data waiting to be sent? */
80                         pollfds[i].events |= POLLOUT;
81                 i++;
82         }
83         return i;
84 }
85
86 void Client_janitor()
87 {
88         struct dlist *itr;
89         int bwTop = maxBandwidth + maxBandwidth / 4;
90         list_iterate(itr, &clients) {
91                 client_t *c;
92                 c = list_get_entry(itr, client_t, node);
93                 Log_debug("Client %s BW available %d", c->playerName, c->availableBandwidth);
94                 c->availableBandwidth += maxBandwidth;
95                 if (c->availableBandwidth > bwTop)
96                         c->availableBandwidth = bwTop;
97                 
98                 if (Timer_isElapsed(&c->lastActivity, 1000000LL * INACTICITY_TIMEOUT)) {
99                         /* No activity from client - assume it is lost and close. */
100                         Log_info("Timeout, closing session %d - %s@%s:%d",
101                                          c->sessionId,
102                                          c->playerName,
103                                          inet_ntoa(c->remote_tcp.sin_addr),
104                                          ntohs(c->remote_tcp.sin_port));
105                         Client_free(c);
106                 }
107         }
108 }
109
110 void recheckCodecVersions()
111 {
112         int codec_map[MAX_CODECS][2];
113         client_t *itr = NULL;
114         int i, codecindex, max = 0, version, current_version;
115         message_t *sendmsg;
116         
117         memset(codec_map, 0, MAX_CODECS * 2 * sizeof(int));
118         while (Client_iterate(&itr) != NULL) {
119                 for (i = 0; i < itr->codec_count; i++) {
120                         for (codecindex = 0; codecindex < MAX_CODECS; codecindex++) {
121                                 if (codec_map[codecindex][0] == 0) {
122                                         codec_map[codecindex][0] = itr->codecs[i];
123                                         codec_map[codecindex][1] = 1;
124                                         break;
125                                 }
126                                 if (itr->codecs[i] == codec_map[codecindex][0])
127                                         codec_map[codecindex][1]++;
128                         }
129                 }
130         }
131         for (codecindex = 0; codecindex < MAX_CODECS; codecindex++) {
132                 if (codec_map[codecindex][0] == 0)
133                         break;
134                 if (codec_map[codecindex][1] > max) {
135                         max = codec_map[codecindex][1];
136                         version = codec_map[codecindex][0];
137                 }
138         }
139         current_version = bPreferAlpha ? iCodecAlpha : iCodecBeta;
140         if (current_version == version)
141                 return;
142         // If we don't already use the compat bitstream version set
143         // it as alpha and announce it. If another codec now got the
144         // majority set it as the opposite of the currently valid bPreferAlpha
145         // and announce it.
146         if (version == (uint32_t)0x8000000a)
147                 bPreferAlpha = true;
148         else
149                 bPreferAlpha = ! bPreferAlpha;
150
151         if (bPreferAlpha)
152                 iCodecAlpha = version;
153         else
154                 iCodecBeta = version;
155         
156         sendmsg = Msg_create(CodecVersion);
157         sendmsg->payload.codecVersion->alpha = version;
158         sendmsg->payload.codecVersion->beta = version;
159         sendmsg->payload.codecVersion->prefer_alpha = bPreferAlpha;
160         Client_send_message_except(NULL, sendmsg);
161         
162         Log_info("CELT codec switch 0x%x 0x%x (prefer 0x%x)", iCodecAlpha, iCodecBeta,
163                          bPreferAlpha ? iCodecAlpha : iCodecBeta);
164         
165 }
166
167 int Client_add(int fd, struct sockaddr_in *remote)
168 {
169         client_t *newclient;
170         message_t *sendmsg;
171         
172         newclient = malloc(sizeof(client_t));
173         if (newclient == NULL)
174                 Log_fatal("Out of memory");
175         memset(newclient, 0, sizeof(client_t));
176
177         newclient->tcpfd = fd;
178         memcpy(&newclient->remote_tcp, remote, sizeof(struct sockaddr_in));
179         newclient->ssl = SSL_newconnection(newclient->tcpfd, &newclient->SSLready);
180         if (newclient->ssl == NULL) {
181                 Log_warn("SSL negotiation failed");
182                 free(newclient);
183                 return -1;
184         }
185         newclient->availableBandwidth = maxBandwidth;
186         Timer_init(&newclient->lastActivity);
187         newclient->sessionId = session++; /* XXX - more elaborate? */
188         
189         init_list_entry(&newclient->txMsgQueue);
190         init_list_entry(&newclient->chan_node);
191         init_list_entry(&newclient->node);
192         init_list_entry(&newclient->voicetargets);
193         
194         list_add_tail(&newclient->node, &clients);
195         clientcount++;
196         
197         /* Send version message to client */
198         sendmsg = Msg_create(Version);
199         sendmsg->payload.version->has_version = true;
200         sendmsg->payload.version->version = PROTOCOL_VERSION;
201         sendmsg->payload.version->release = strdup(UMURMUR_VERSION);
202         /* XXX - set OS to something relevant? */
203         /* sendmsg->payload.version->os = strdup("Linux/OpenWRT"); */
204                 
205         Client_send_message(newclient, sendmsg);
206
207         return 0;
208 }
209
210 void Client_free(client_t *client)
211 {
212         struct dlist *itr, *save;
213         message_t *sendmsg;
214
215         Log_info("Disconnect session %d - %s@%s:%d",
216                          client->sessionId,
217                          client->playerName,
218                          inet_ntoa(client->remote_tcp.sin_addr),
219                          ntohs(client->remote_tcp.sin_port));
220
221         if (client->authenticated) {
222                 sendmsg = Msg_create(UserRemove);
223                 sendmsg->payload.userRemove->session = client->sessionId;
224                 Client_send_message_except(client, sendmsg);
225         }
226         list_iterate_safe(itr, save, &client->txMsgQueue) {
227                 list_del(&list_get_entry(itr, message_t, node)->node);
228                 Msg_free(list_get_entry(itr, message_t, node));
229         }
230         Voicetarget_free_all(client);
231         
232         list_del(&client->node);
233         list_del(&client->chan_node);
234         if (client->ssl)
235                 SSL_free(client->ssl);
236         close(client->tcpfd);
237         clientcount--;
238         if (client->release)
239                 free(client->release);
240         if (client->os)
241                 free(client->os);                       
242         if (client->playerName)
243                 free(client->playerName);
244         if (client->context)
245                 free(client->context);
246         free(client);
247 }
248
249 void Client_close(client_t *client)
250 {
251         SSL_shutdown(client->ssl);
252         client->shutdown_wait = true;
253 }
254
255 void Client_disconnect_all()
256 {
257         struct dlist *itr, *save;
258         
259         list_iterate_safe(itr, save, &clients) {
260                 Client_free(list_get_entry(itr, client_t, node));
261         }
262 }
263
264 int Client_read_fd(int fd)
265 {
266         struct dlist *itr;
267         client_t *client = NULL;
268         
269         list_iterate(itr, &clients) {
270                 if (fd == list_get_entry(itr, client_t, node)->tcpfd) {
271                         client = list_get_entry(itr, client_t, node);
272                         break;
273                 }
274         }
275         if (client == NULL)
276                 Log_fatal("No client found for fd %d", fd);
277         
278         return Client_read(client);
279 }
280
281 int Client_read(client_t *client)
282 {
283         int rc;
284
285         Timer_restart(&client->lastActivity);
286         
287         if (client->writeBlockedOnRead) {
288                 client->writeBlockedOnRead = false;
289                 Log_debug("Client_read: writeBlockedOnRead == true");
290                 return Client_write(client);
291         }
292         
293         if (client->shutdown_wait) {
294                 Client_free(client);
295                 return 0;
296         }
297         if (!client->SSLready) {
298                 int rc;
299                 rc = SSL_nonblockaccept(client->ssl, &client->SSLready);
300                 if (rc < 0) {
301                         Client_free(client);
302                         return -1;
303                 }
304         }
305
306         do {
307                 errno = 0;
308                 if (!client->msgsize) 
309                         rc = SSL_read(client->ssl, &client->rxbuf[client->rxcount], 6 - client->rxcount);
310                 else if (client->drainleft > 0)
311                         rc = SSL_read(client->ssl, client->rxbuf, client->drainleft > BUFSIZE ? BUFSIZE : client->drainleft);
312                 else
313                         rc = SSL_read(client->ssl, &client->rxbuf[client->rxcount], client->msgsize);
314                 if (rc > 0) {
315                         message_t *msg;
316                         if (client->drainleft > 0)
317                                 client->drainleft -= rc;
318                         else {
319                                 client->rxcount += rc;
320                                 if (!client->msgsize && client->rxcount >= 6) {
321                                         uint32_t msgLen;
322                                         memcpy(&msgLen, &client->rxbuf[2], sizeof(uint32_t));
323                                         client->msgsize = ntohl(msgLen);
324                                 }
325                                 if (client->msgsize > BUFSIZE - 6 && client->drainleft == 0) {
326                                         Log_warn("Too big message received (%d). Discarding.", client->msgsize);
327                                         client->rxcount = client->msgsize = 0;
328                                         client->drainleft = client->msgsize;
329                                 }
330                                 else if (client->rxcount == client->msgsize + 6) { /* Got all of the message */
331                                         msg = Msg_networkToMessage(client->rxbuf, client->msgsize + 6);
332                                         /* pass messsage to handler */
333                                         if (msg)
334                                                         Mh_handle_message(client, msg);
335                                         client->rxcount = client->msgsize = 0;
336                                 }
337                         }
338                 } else /* rc <= 0 */ {
339                         if (SSL_get_error(client->ssl, rc) == SSL_ERROR_WANT_READ) {
340                                 return 0;
341                         }
342                         else if (SSL_get_error(client->ssl, rc) == SSL_ERROR_WANT_WRITE) {
343                                 client->readBlockedOnWrite = true;
344                                 return 0;
345                         }
346                         else if (SSL_get_error(client->ssl, rc) == SSL_ERROR_ZERO_RETURN) {
347                                 Log_warn("Error: Zero return - closing");
348                                 if (!client->shutdown_wait)
349                                         Client_close(client);
350                         }
351                         else {
352                                 if (SSL_get_error(client->ssl, rc) == SSL_ERROR_SYSCALL) {
353                                         /* Hmm. This is where we end up when the client closes its connection.
354                                          * Kind of strange...
355                                          */
356                                         Log_info("Connection closed by peer. Session %d - %s@%s:%d",
357                                                          client->sessionId,
358                                                          client->playerName,
359                                                          inet_ntoa(client->remote_tcp.sin_addr),
360                                                          ntohs(client->remote_tcp.sin_port));
361                                 }
362                                 else {
363                                         Log_warn("SSL error: %d - Closing connection.", SSL_get_error(client->ssl, rc));
364                                 }
365                                 Client_free(client);
366                                 return -1;
367                         }
368                 }
369         } while (SSL_pending(client->ssl));
370         return 0;       
371 }
372
373 int Client_write_fd(int fd)
374 {
375         struct dlist *itr;
376         client_t *client = NULL;
377         
378         list_iterate(itr, &clients) {
379                 if(fd == list_get_entry(itr, client_t, node)->tcpfd) {
380                         client = list_get_entry(itr, client_t, node);
381                         break;
382                 }
383         }
384         if (client == NULL)
385                 Log_fatal("No client found for fd %d", fd);
386         Client_write(client);
387         return 0;
388 }
389
390 int Client_write(client_t *client)
391 {
392         int rc;
393         
394         if (client->readBlockedOnWrite) {
395                 client->readBlockedOnWrite = false;
396                 Log_debug("Client_write: readBlockedOnWrite == true");
397                 return Client_read(client);
398         }
399         rc = SSL_write(client->ssl, &client->txbuf[client->txcount], client->txsize - client->txcount);
400         if (rc > 0) {
401                 client->txcount += rc;
402                 if (client->txcount == client->txsize)
403                         client->txsize = client->txcount = 0;
404         }
405         else if (rc < 0) {
406                 if (SSL_get_error(client->ssl, rc) == SSL_ERROR_WANT_READ) {
407                         client->writeBlockedOnRead = true;
408                         return 0;
409                 }
410                 else if (SSL_get_error(client->ssl, rc) == SSL_ERROR_WANT_WRITE) {
411                         return 0;
412                 }
413                 else {
414                         if (SSL_get_error(client->ssl, rc) == SSL_ERROR_SYSCALL)
415                                 Log_warn("Client_write: Error: %s  - Closing connection", strerror(errno));
416                         else
417                                 Log_warn("Client_write: SSL error: %d - Closing connection.", SSL_get_error(client->ssl, rc));
418                         Client_free(client);
419                         return -1;
420                 }
421         }
422         if (client->txsize == 0 && !list_empty(&client->txMsgQueue)) {
423                 message_t *msg;
424                 msg = list_get_entry(list_get_first(&client->txMsgQueue), message_t, node);
425                 list_del(list_get_first(&client->txMsgQueue));
426                 client->txQueueCount--;
427                 Client_send_message(client, msg);
428         }
429         return 0;
430 }
431
432 int Client_send_message(client_t *client, message_t *msg)
433 {
434         if (!client->authenticated && msg->messageType != Version) {
435                 Msg_free(msg);
436                 return 0;
437         }
438         if (client->txsize != 0 || !client->SSLready) {
439                 /* Queue message */
440                 if ((client->txQueueCount > 5 &&  msg->messageType == UDPTunnel) ||
441                         client->txQueueCount > 30) {
442                         Msg_free(msg);
443                         return -1;
444                 }
445                 client->txQueueCount++;
446                 list_add_tail(&msg->node, &client->txMsgQueue);
447                 Log_debug("Queueing message");
448         } else {
449                 int len;
450                 memset(client->txbuf, 0, BUFSIZE);
451                 len = Msg_messageToNetwork(msg, client->txbuf);
452                 doAssert(len < BUFSIZE);
453
454                 client->txsize = len;
455                 client->txcount = 0;
456                 Client_write(client);
457                 Msg_free(msg);
458         }
459         return 0;
460 }
461
462 client_t *Client_iterate(client_t **client_itr)
463 {
464         client_t *c = *client_itr;
465
466         if (list_empty(&clients))
467                 return NULL;
468         
469         if (c == NULL) {
470                 c = list_get_entry(list_get_first(&clients), client_t, node);
471         } else {
472                 if (list_get_next(&c->node) == &clients)
473                         c = NULL;
474                 else
475                         c = list_get_entry(list_get_next(&c->node), client_t, node);
476         }
477         *client_itr = c;
478         return c;
479 }
480
481
482 int Client_send_message_except(client_t *client, message_t *msg)
483 {
484         client_t *itr = NULL;
485         int count = 0;
486         
487         Msg_inc_ref(msg); /* Make sure a reference is held during the whole iteration. */
488         while (Client_iterate(&itr) != NULL) {
489                 if (itr != client) {
490                         if (count++ > 0)
491                                 Msg_inc_ref(msg); /* One extra reference for each new copy */
492                         Log_debug("Msg %d to %s refcount %d",  msg->messageType, itr->playerName, msg->refcount);
493                         Client_send_message(itr, msg);
494                 }
495         }
496         Msg_free(msg); /* Free our reference to the message */
497         
498         if (count == 0)
499                 Msg_free(msg); /* If only 1 client is connected then no message is passed
500                                                 * to Client_send_message(). Free it here. */
501                 
502         return 0;
503 }
504
505 static bool_t checkDecrypt(client_t *client, const uint8_t *encrypted, uint8_t *plain, unsigned int len)
506 {
507         if (CryptState_isValid(&client->cryptState) &&
508                 CryptState_decrypt(&client->cryptState, encrypted, plain, len))
509                 return true;
510
511         if (Timer_elapsed(&client->cryptState.tLastGood) > 5000000ULL) {
512                 if (Timer_elapsed(&client->cryptState.tLastRequest) > 5000000ULL) {
513                         message_t *sendmsg;
514                         Timer_restart(&client->cryptState.tLastRequest);
515                         
516                         sendmsg = Msg_create(CryptSetup);
517                         Log_info("Requesting voice channel crypt resync. Session %d - %s@%s:%d",
518                                          client->sessionId,
519                                          client->playerName,
520                                          inet_ntoa(client->remote_tcp.sin_addr),
521                                          ntohs(client->remote_tcp.sin_port));
522                 
523                         Client_send_message(client, sendmsg);
524                 }
525         }
526         return false;
527 }
528
529 #define UDP_PACKET_SIZE 1024
530 int Client_read_udp()
531 {
532         int len;
533         struct sockaddr_in from;
534         socklen_t fromlen = sizeof(struct sockaddr_in);
535         uint64_t key;
536         client_t *itr;
537         UDPMessageType_t msgType;
538         
539 #if defined(__LP64__)
540         uint8_t encbuff[UDP_PACKET_SIZE + 8];
541         uint8_t *encrypted = encbuff + 4;
542 #else
543         uint8_t encrypted[UDP_PACKET_SIZE];
544 #endif
545         uint8_t buffer[UDP_PACKET_SIZE];
546         
547         len = recvfrom(udpsock, encrypted, UDP_PACKET_SIZE, MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
548         if (len == 0) {
549                 return -1;
550         } else if (len < 0) {
551                 return -1;
552         } else if (len < 5) {
553                 // 4 bytes crypt header + type + session
554                 return 0;
555         } else if (len > UDP_PACKET_SIZE) {
556                 return 0;
557         }
558
559         /* Ping packet */
560         if (len == 12 && *encrypted == 0) {
561                 uint32_t *ping = (uint32_t *)encrypted;
562                 ping[0] = htonl((uint32_t)PROTOCOL_VERSION);
563                 // 1 and 2 will be the timestamp, which we return unmodified.
564                 ping[3] = htonl((uint32_t)clientcount);
565                 ping[4] = htonl((uint32_t)getIntConf(MAX_CLIENTS));
566                 ping[5] = htonl((uint32_t)getIntConf(MAX_BANDWIDTH));
567                 
568                 sendto(udpsock, encrypted, 6 * sizeof(uint32_t), 0, (struct sockaddr *)&from, fromlen);
569                 return 0;
570         }
571         
572         key = (((uint64_t)from.sin_addr.s_addr) << 16) ^ from.sin_port;
573         itr = NULL;
574         
575         while (Client_iterate(&itr) != NULL) {
576                 if (itr->key == key) {
577                         if (!checkDecrypt(itr, encrypted, buffer, len))
578                                 goto out;
579                         break;
580                 }
581         }       
582         if (itr == NULL) { /* Unknown peer */
583                 while (Client_iterate(&itr) != NULL) {
584                         if (itr->remote_tcp.sin_addr.s_addr == from.sin_addr.s_addr) {
585                                 if (checkDecrypt(itr, encrypted, buffer, len)) {
586                                         itr->key = key;
587                                         Log_info("New UDP connection from session %d - %s@%s:%d",
588                                                          itr->sessionId,
589                                                          itr->playerName,
590                                                          inet_ntoa(from.sin_addr),
591                                                          ntohs(from.sin_port));
592                                         memcpy(&itr->remote_udp, &from, sizeof(struct sockaddr_in));
593                                         break;
594                                 }
595                                 else Log_warn("Bad cryptstate from peer");
596                         }
597                 } /* while */
598         }
599         if (itr == NULL) {
600                 goto out;
601         }
602         
603         msgType = (UDPMessageType_t)((buffer[0] >> 5) & 0x7);
604         switch (msgType) {
605         case UDPVoiceSpeex:
606         case UDPVoiceCELTAlpha:
607         case UDPVoiceCELTBeta:
608                 // u->bUdp = true;
609                 Client_voiceMsg(itr, buffer, len);
610                 break;
611         case UDPPing:
612                 Log_debug("UDP Ping reply len %d", len);
613                 Client_send_udp(itr, buffer, len);
614                 break;
615         default:
616                 Log_debug("Unknown UDP message type from %s port %d", inet_ntoa(from.sin_addr), ntohs(from.sin_port));
617                 break;
618         }
619 out:
620         return 0;
621 }
622
623 static inline void Client_send_voice(client_t *src, client_t *dst, uint8_t *data, int len, int poslen)
624 {
625         if (IS_AUTH(dst) && dst != src && !dst->deaf) {
626                 if (poslen > 0 && strcmp(src->context, dst->context) == 0)
627                         Client_send_udp(dst, data, len);
628                 else
629                         Client_send_udp(dst, data, len - poslen);
630         }
631 }
632
633 /* Handle decrypted voice message */
634 int Client_voiceMsg(client_t *client, uint8_t *data, int len)
635 {
636         uint8_t buffer[UDP_PACKET_SIZE];
637         pds_t *pdi = Pds_create(data + 1, len - 1);
638         pds_t *pds = Pds_create(buffer + 1, UDP_PACKET_SIZE - 1);
639         unsigned int type = data[0] & 0xe0;
640         unsigned int target = data[0] & 0x1f;
641         unsigned int poslen, counter;
642         int offset, packetsize;
643         voicetarget_t *vt;
644         
645         channel_t *ch = (channel_t *)client->channel;
646         struct dlist *itr;
647         
648         if (!client->authenticated || client->mute)
649                 goto out;
650         
651         packetsize = 20 + 8 + 4 + len;
652         if (client->availableBandwidth - packetsize < 0)
653                 goto out; /* Discard */
654         client->availableBandwidth -= packetsize;
655         
656         counter = Pds_get_numval(pdi); /* step past session id */
657         do {
658                 counter = Pds_next8(pdi);
659                 offset = Pds_skip(pdi, counter & 0x7f);
660         } while ((counter & 0x80) && offset > 0);
661
662         poslen = pdi->maxsize - pdi->offset; /* For stripping of positional info */
663         
664         Pds_add_numval(pds, client->sessionId);
665         Pds_append_data_nosize(pds, data + 1, len - 1);
666         
667         if (target == 0x1f) { /* Loopback */
668                 buffer[0] = (uint8_t) type;
669                 Client_send_udp(client, buffer, pds->offset + 1);
670         }
671         else if (target == 0) { /* regular channel speech */
672                 buffer[0] = (uint8_t) type;
673                 
674                 if (ch == NULL)
675                         goto out;
676                 
677                 list_iterate(itr, &ch->clients) {
678                         client_t *c;
679                         c = list_get_entry(itr, client_t, chan_node);
680                         Client_send_voice(client, c, buffer, pds->offset + 1, poslen);
681                 }
682                 /* Channel links */
683                 if (!list_empty(&ch->channel_links)) {
684                         struct dlist *ch_itr;
685                         list_iterate(ch_itr, &ch->channel_links) {
686                                 channel_t *ch_link;
687                                 ch_link = list_get_entry(ch_itr, channel_t, link_node);
688                                 list_iterate(itr, &ch_link->clients) {
689                                         client_t *c;
690                                         c = list_get_entry(itr, client_t, chan_node);
691                                         Log_debug("Linked voice from %s -> %s", ch->name, ch_link->name);
692                                         Client_send_voice(client, c, buffer, pds->offset + 1, poslen);
693                                 }
694                         }
695                 }
696         } else if ((vt = Voicetarget_get_id(client, target)) != NULL) { /* Targeted whisper */
697                 int i;
698                 channel_t *ch;
699                 /* Channels */
700                 for (i = 0; i < TARGET_MAX_CHANNELS && vt->channels[i] != -1; i++) {
701                         Log_debug("Whisper channel %d", vt->channels[i]);
702                         ch = Chan_fromId(vt->channels[i]);
703                         if (ch == NULL)
704                                 continue;
705                         list_iterate(itr, &ch->clients) {
706                                 client_t *c;
707                                 c = list_get_entry(itr, client_t, chan_node);
708                                 Client_send_voice(client, c, buffer, pds->offset + 1, poslen);
709                         }
710                 }                       
711                 /* Sessions */
712                 for (i = 0; i < TARGET_MAX_SESSIONS && vt->sessions[i] != -1; i++) {
713                         client_t *c;
714                         Log_debug("Whisper session %d", vt->sessions[i]);
715                         while (Client_iterate(&c) != NULL) {
716                                 if (c->sessionId == vt->sessions[i]) {
717                                         Client_send_voice(client, c, buffer, pds->offset + 1, poslen);
718                                         break;
719                                 }
720                         }
721                 }
722         }
723 out:
724         Pds_free(pds);
725         Pds_free(pdi);
726         
727         return 0;
728 }
729
730
731 static int Client_send_udp(client_t *client, uint8_t *data, int len)
732 {
733         uint8_t *buf, *mbuf;
734
735         if (client->remote_udp.sin_port != 0 && CryptState_isValid(&client->cryptState)) {
736 #if defined(__LP64__)
737                 buf = mbuf = malloc(len + 4 + 16);
738                 buf += 4;
739 #else
740                 mbuf = buf = malloc(len + 4);
741 #endif
742                 if (mbuf == NULL)
743                         Log_fatal("Out of memory");
744                 
745                 CryptState_encrypt(&client->cryptState, data, buf, len);
746                 
747                 sendto(udpsock, buf, len + 4, 0, (struct sockaddr *)&client->remote_udp, sizeof(struct sockaddr_in));
748                 
749                 free(mbuf);
750         } else {
751                 message_t *msg;
752                 buf = malloc(len);
753                 memcpy(buf, data, len);
754                 msg = Msg_create(UDPTunnel);
755                 
756                 msg->payload.UDPTunnel->packet.data = buf;
757                 msg->payload.UDPTunnel->packet.len = len;
758                 Client_send_message(client, msg);
759         }
760         return 0;
761 }