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