view src/gt_packet.c @ 0:d39e1d0d75b6

initial add
author paulo@hit-nxdomain.opendns.com
date Sat, 20 Feb 2010 21:18:28 -0800
parents
children
line source
1 /*
2 * $Id: gt_packet.c,v 1.35 2004/04/17 06:05:53 hipnod Exp $
3 *
4 * Copyright (C) 2001-2003 giFT project (gift.sourceforge.net)
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 */
17 #include "gt_gnutella.h"
19 #include "gt_packet.h"
21 #include "gt_node.h"
22 #include "gt_netorg.h"
24 #include "gt_utils.h"
26 #include "gt_query_route.h"
27 #include "message/gt_message.h" /* gt_vendor_msg_get_{id,version} */
29 #include <stdarg.h>
31 /*****************************************************************************/
33 GtPacket *gt_packet_new (uint8_t cmd, uint8_t ttl, gt_guid_t *guid)
34 {
35 GtPacket *packet;
36 unsigned char *data;
38 if (!(packet = MALLOC (sizeof (GtPacket))))
39 return NULL;
41 if (!(data = MALLOC (GT_PACKET_INITIAL)))
42 {
43 free (packet);
44 return NULL;
45 }
47 packet->data = data;
48 packet->len = GNUTELLA_HDR_LEN;
49 packet->data_len = GT_PACKET_INITIAL;
51 /*
52 * Caller could provide GUID or request it be randomly initialized.
53 */
54 if (guid)
55 gt_packet_set_guid (packet, guid);
56 else
57 gt_guid_init ((gt_guid_t *)packet->data);
59 gt_packet_set_command (packet, cmd);
60 gt_packet_set_ttl (packet, ttl);
61 gt_packet_set_payload_len (packet, 0);
62 gt_packet_set_hops (packet, 0);
64 /* set the offset to start at the end of the header */
65 packet->offset = GNUTELLA_HDR_LEN;
67 return packet;
68 }
70 GtPacket *gt_packet_reply (GtPacket *src, uint8_t cmd)
71 {
72 GtPacket *packet;
73 uint8_t hops;
74 gt_guid_t *guid;
76 hops = gt_packet_hops (src);
77 guid = gt_packet_guid (src);
79 if (!(packet = gt_packet_new (cmd, hops + 1, guid)))
80 return NULL;
82 return packet;
83 }
85 GtPacket *gt_packet_vendor (const gt_vendor_msg_t *vmsg)
86 {
87 GtPacket *pkt;
89 if (!(pkt = gt_packet_new (GT_MSG_VENDOR, 1, NULL)))
90 return NULL;
92 gt_packet_put_ustr (pkt, vmsg->vendor_id, 4);
93 gt_packet_put_uint16 (pkt, vmsg->id);
94 gt_packet_put_uint16 (pkt, 1); /* vmsg version */
96 if (gt_packet_error (pkt))
97 {
98 gt_packet_free (pkt);
99 return NULL;
100 }
102 return pkt;
103 }
105 void gt_packet_free (GtPacket *packet)
106 {
107 if (!packet)
108 return;
110 free (packet->data);
111 free (packet);
112 }
114 /*****************************************************************************/
116 gt_guid_t *gt_packet_guid (GtPacket *packet)
117 {
118 assert (packet->data_len >= GNUTELLA_HDR_LEN);
120 return packet->data;
121 }
123 uint32_t gt_packet_payload_len (GtPacket *packet)
124 {
125 assert (packet->data_len >= GNUTELLA_HDR_LEN);
127 return get_payload_len (packet->data);
128 }
130 uint32_t gt_packet_command (GtPacket *packet)
131 {
132 assert (packet->data_len >= GNUTELLA_HDR_LEN);
134 return get_command (packet->data);
135 }
137 uint8_t gt_packet_hops (GtPacket *packet)
138 {
139 assert (packet->data_len >= GNUTELLA_HDR_LEN);
141 return get_hops (packet->data);
142 }
144 uint8_t gt_packet_ttl (GtPacket *packet)
145 {
146 assert (packet->data_len >= GNUTELLA_HDR_LEN);
148 return get_ttl (packet->data);
149 }
151 /*****************************************************************************/
153 void gt_packet_set_guid (GtPacket *packet, gt_guid_t *guid)
154 {
155 assert (packet->data_len >= GNUTELLA_HDR_LEN);
157 memcpy (packet->data, guid, 16);
158 }
160 void gt_packet_set_command (GtPacket *packet, uint8_t cmd)
161 {
162 assert (packet->data_len >= GNUTELLA_HDR_LEN);
164 get_command (packet->data) = cmd;
165 }
167 void gt_packet_set_ttl (GtPacket *packet, uint8_t ttl)
168 {
169 assert (packet->data_len >= GNUTELLA_HDR_LEN);
171 get_ttl (packet->data) = ttl;
172 }
174 void gt_packet_set_hops (GtPacket *packet, uint8_t hops)
175 {
176 assert (packet->data_len >= GNUTELLA_HDR_LEN);
178 get_hops (packet->data) = hops;
179 }
181 void gt_packet_set_payload_len (GtPacket *packet, uint32_t len)
182 {
183 assert (packet->data_len >= GNUTELLA_HDR_LEN);
185 len = htovl (len);
187 memcpy (packet->data + 19, &len, 4);
188 }
190 /*****************************************************************************/
191 /* functions to read data from packets */
193 static uint8_t gt_get8 (unsigned char *src)
194 {
195 return *src;
196 }
198 #define copy_bytes(src,sz,endian,swap,sizedvar_t,swap_net,swap_vax) \
199 sizedvar_t r; \
200 sizedvar_t v; \
201 sizedvar_t n; \
202 \
203 memcpy (&r, src, sz); \
204 \
205 n = swap_net (r); \
206 \
207 assert_at_compile (BIG == 0x01); \
208 if (endian & swap) \
209 r = n; \
210 \
211 v = swap_vax (r); \
212 \
213 assert_at_compile (LITTLE == 0x00); \
214 if (~endian & swap) \
215 r = v; \
216 \
217 return r;
219 static uint16_t gt_get16 (unsigned char *src, int endian, int swap)
220 {
221 copy_bytes (src, 2, endian, swap, uint16_t, ntohs, vtohs);
222 }
224 static uint32_t gt_get32 (unsigned char *src, int endian, int swap)
225 {
226 copy_bytes (src, 4, endian, swap, uint32_t, ntohl, vtohl);
227 }
229 /*****************************************************************************/
231 static BOOL gt_packet_resize (GtPacket *packet, size_t len)
232 {
233 size_t resize_len;
234 unsigned char *resized;
236 if (!packet)
237 return FALSE;
239 assert (len >= GNUTELLA_HDR_LEN);
240 assert (len < GT_PACKET_MAX);
242 /* the buffer we have allocated is already large enough */
243 if (packet->data_len >= len)
244 return TRUE;
246 /* determine an appropriate resize length */
247 for (resize_len = packet->data_len; resize_len < len; )
248 {
249 if (resize_len == 0)
250 resize_len = GT_PACKET_INITIAL;
251 else
252 resize_len *= 2;
253 }
255 if (!(resized = realloc (packet->data, resize_len)))
256 {
257 packet->error = TRUE;
258 return FALSE;
259 }
261 memset (resized + packet->data_len, 0, resize_len - packet->data_len);
263 /* modify the packet structure to reflect this resize */
264 packet->data_len = resize_len;
265 packet->data = resized;
267 return TRUE;
268 }
270 int gt_packet_error (GtPacket *packet)
271 {
272 return packet->error;
273 }
275 int gt_packet_seek (GtPacket *packet, int offset)
276 {
277 int old_offset;
279 /* should we be setting packet->error here? */
280 if (offset >= packet->len || offset < 0)
281 {
282 packet->error = TRUE;
283 return -1;
284 }
286 old_offset = packet->offset;
287 packet->offset = offset;
289 return old_offset;
290 }
292 GtPacket *gt_packet_unserialize (unsigned char *data, size_t len)
293 {
294 GtPacket *packet;
296 if (!(packet = gt_packet_new (0, 0, NULL)))
297 return NULL;
299 if (len >= GT_PACKET_MAX)
300 {
301 gt_packet_free (packet);
302 return NULL;
303 }
305 if (!gt_packet_resize (packet, len))
306 {
307 GIFT_ERROR (("error resizing packet"));
308 gt_packet_free (packet);
309 return NULL;
310 }
312 memcpy (packet->data, data, len);
314 packet->len = len;
316 /* Hmm, this should never happen, time to valgrind */
317 if (gt_packet_payload_len (packet) != len - GNUTELLA_HDR_LEN)
318 {
319 GIFT_ERROR (("corrupt packet"));
320 gt_packet_free (packet);
321 return NULL;
322 }
324 return packet;
325 }
327 /*****************************************************************************/
329 uint8_t gt_packet_get_uint8 (GtPacket *packet)
330 { return gt_packet_get_uint (packet, 1, LITTLE, FALSE); }
332 uint16_t gt_packet_get_uint16 (GtPacket *packet)
333 { return gt_packet_get_uint (packet, 2, LITTLE, TRUE); }
335 uint32_t gt_packet_get_uint32 (GtPacket *packet)
336 { return gt_packet_get_uint (packet, 4, LITTLE, TRUE); }
338 in_addr_t gt_packet_get_ip (GtPacket *packet)
339 { return gt_packet_get_uint (packet, 4, BIG, FALSE); }
341 in_port_t gt_packet_get_port (GtPacket *packet)
342 { return gt_packet_get_uint (packet, 2, LITTLE, TRUE); }
344 /*****************************************************************************/
346 uint32_t gt_packet_get_uint (GtPacket *packet, size_t size,
347 int endian, int swap)
348 {
349 uint32_t data32 = 0;
350 char *offs;
352 assert (packet);
354 /* check overrun */
355 if (packet->offset + size > packet->len)
356 {
357 packet->error = TRUE;
358 return 0;
359 }
361 offs = packet->data + packet->offset;
363 switch (size)
364 {
365 case 1:
366 data32 = (uint32_t)gt_get8 (offs);
367 break;
368 case 2:
369 data32 = (uint32_t)gt_get16 (offs, endian, swap);
370 break;
371 case 4:
372 data32 = gt_get32 (offs, endian, swap);
373 break;
374 default:
375 printf ("%s: wtf are you doing?\n", __PRETTY_FUNCTION__);
376 return data32;
377 }
379 packet->offset += size;
381 return data32;
382 }
384 /*****************************************************************************/
386 static int gt_packet_append (GtPacket *packet, const void *data, size_t size)
387 {
388 size_t required;
390 if (!packet || !data || size == 0)
391 return FALSE;
393 if (packet->data_len + size >= GT_PACKET_MAX)
394 {
395 packet->error = TRUE;
396 return FALSE;
397 }
399 /* determine the total required space to append size bytes */
400 required = packet->len + size;
402 /* if we can't resize, gracefully fail...the caller should determine
403 * how bad this truly is */
404 if (!gt_packet_resize (packet, required))
405 return FALSE;
407 /* append size bytes of data */
408 memcpy (packet->data + packet->len, data, size);
410 /* increment the length of this packet */
411 packet->len += size;
413 gt_packet_set_payload_len (packet, gt_packet_payload_len (packet) + size);
414 return TRUE;
415 }
417 int gt_packet_put_uint (GtPacket *packet, void *data, size_t size,
418 int endian, int swap)
419 {
420 int ret;
421 uint16_t unet16;
422 uint32_t unet32;
424 if (!data || size < 0 || size > sizeof (uint32_t))
425 return FALSE;
427 switch (size)
428 {
429 case 2:
430 unet16 = gt_get16 (data, endian, swap);
431 ret = gt_packet_append (packet, &unet16, size);
432 break;
433 case 4:
434 unet32 = gt_get32 (data, endian, swap);
435 ret = gt_packet_append (packet, &unet32, size);
436 break;
437 default:
438 ret = gt_packet_append (packet, data, size);
439 break;
440 }
442 return ret;
443 }
445 /*****************************************************************************/
447 int gt_packet_put_uint8 (GtPacket *packet, uint8_t byte)
448 { return gt_packet_put_uint (packet, &byte, 1, LITTLE, FALSE); }
450 int gt_packet_put_uint16 (GtPacket *packet, uint16_t bytes)
451 { return gt_packet_put_uint (packet, &bytes, 2, LITTLE, TRUE); }
453 int gt_packet_put_uint32 (GtPacket *packet, uint32_t bytes)
454 { return gt_packet_put_uint (packet, &bytes, 4, LITTLE, TRUE); }
456 int gt_packet_put_ip (GtPacket *packet, in_addr_t ip)
457 { return gt_packet_put_uint (packet, &ip, 4, BIG, FALSE); }
459 int gt_packet_put_port (GtPacket *packet, in_port_t port)
460 { return gt_packet_put_uint (packet, &port, 2, LITTLE, TRUE); }
462 int gt_packet_put_ustr (GtPacket *packet, const unsigned char *str, size_t len)
463 {
464 assert (len > 0);
465 return gt_packet_append (packet, str, len);
466 }
468 int gt_packet_put_str (GtPacket *packet, const char *str)
469 {
470 size_t len;
472 if (!str)
473 return gt_packet_put_uint8 (packet, 0);
475 len = strlen (str) + 1;
476 return gt_packet_put_ustr (packet, (const unsigned char *)str, len);
477 }
479 /*****************************************************************************/
481 /* checks for the gt_packet_get_array sentinel given the size. the sentinel is
482 * defined as a full element filled with zeroes */
483 static int array_sentinel (char *ptr, size_t size)
484 {
485 while (size--)
486 {
487 /* non-zero byte, this element couldnt be the sentinel */
488 if (*ptr != 0)
489 return FALSE;
491 ptr++;
492 }
494 return TRUE;
495 }
497 void *gt_packet_get_array (GtPacket *packet, size_t nmemb, size_t size,
498 int term, int endian, int swap)
499 {
500 char *start;
501 char *ptr;
502 char *end;
503 int n;
505 assert (packet);
507 /* we tried to read past the end of the packet */
508 if (packet->offset >= packet->len)
509 {
510 packet->error = TRUE;
511 return NULL;
512 }
514 start = packet->data + packet->offset;
515 end = packet->data + packet->len;
517 /* TODO - optimize compares inside this loop */
518 n = 0;
519 for (ptr = start; ptr + size < end; ptr += size, n++)
520 {
521 if (term && array_sentinel (ptr, size))
522 break;
524 if (nmemb > 0 && n >= nmemb)
525 break;
527 if (swap)
528 {
529 switch (size)
530 {
531 case 2:
532 net_put16 (ptr, gt_get16 (ptr, endian, swap));
533 break;
534 case 4:
535 net_put32 (ptr, gt_get32 (ptr, endian, swap));
536 break;
537 default:
538 assert (0);
539 break;
540 }
541 }
542 }
544 /* If the array was not null terminated, then terminate it now */
545 if (term && !array_sentinel (ptr, size))
546 {
547 uint32_t zero = 0;
548 size_t len;
550 /* this is the length of the array we read */
551 len = (ptr - start + size);
553 /* we must have hit the end of the packet */
554 assert (packet->offset + len == packet->len);
556 if (!gt_packet_resize (packet, packet->len + size))
557 {
558 packet->offset = packet->len;
559 return NULL;
560 }
562 /* Hrm, this changes the payload, which we really dont want to do. */
563 if (!gt_packet_append (packet, &zero, size))
564 {
565 packet->offset = packet->len;
566 return NULL;
567 }
568 }
570 /* invalid data...no sentinel found */
571 if (ptr + size > end)
572 {
573 packet->offset = packet->len;
574 return NULL;
575 }
577 /* ptr is at the sentinel right now, move one element out to calculate the
578 * next packet start */
579 if (term)
580 ptr += size;
582 /* this includes the sentinel now */
583 packet->offset += (ptr - start);
585 return start;
586 }
588 char *gt_packet_get_str (GtPacket *packet)
589 {
590 return gt_packet_get_array (packet, 0, 1, TRUE, FALSE, FALSE);
591 }
593 unsigned char *gt_packet_get_ustr (GtPacket *packet, size_t len)
594 {
595 return gt_packet_get_array (packet, len, 1, FALSE, FALSE, FALSE);
596 }
598 /******************************************************************************/
600 static char *packet_command_str (unsigned char cmd)
601 {
602 static char buf[16];
604 switch (cmd)
605 {
606 case GT_MSG_PING: return "PING";
607 case GT_MSG_PING_REPLY: return "PONG";
608 case GT_MSG_BYE: return "BYE";
609 case GT_MSG_QUERY_ROUTE: return "QROUTE";
610 case GT_MSG_VENDOR: return "VMSG";
611 case GT_MSG_VENDOR_STD: return "VMSG-S";
612 case GT_MSG_PUSH: return "PUSH";
613 case GT_MSG_QUERY: return "QUERY";
614 case GT_MSG_QUERY_REPLY: return "HITS";
616 default:
617 snprintf (buf, sizeof (buf), "[<%02hx>]", cmd);
618 return buf;
619 }
620 }
622 static void packet_log (char *data, int len, int sent, char *user_agent,
623 in_addr_t ip)
624 {
625 uint8_t cmd;
626 char user_buf[32];
627 char *file;
628 static FILE *ascii_log;
630 if (!ascii_log)
631 {
632 file = PACKET_ASCII_LOG;
634 if (!(ascii_log = fopen (file, "w")))
635 return;
636 }
638 cmd = get_command (data);
640 user_buf[0] = 0;
642 /* copy the first few chars of user_agent to make it line up */
643 if (user_agent)
644 {
645 strncpy (user_buf, user_agent, 21);
646 user_buf[21] = 0;
647 }
649 fprintf (ascii_log, "%2s %-6s sz: %-5hu peer: %-22s [%s]\n",
650 (sent ? "<=" : "=>"),
651 packet_command_str (cmd), len,
652 (user_buf[0] ? user_buf : "(None)"),
653 (ip == 0 ? "None" : net_ip_str (ip)));
655 fprint_hex (ascii_log, data, len);
656 }
658 void gt_packet_log (GtPacket *packet, TCPC *src, int sent)
659 {
660 char *user_agent;
661 in_addr_t ip;
663 if (!PACKET_DEBUG)
664 return;
666 user_agent = NULL;
667 ip = 0;
669 /* append the user-agent string to the packet log */
670 if (src)
671 {
672 ip = src->host;
673 user_agent = dataset_lookupstr (GT_NODE(src)->hdr, "user-agent");
674 }
676 packet_log (packet->data, packet->len, sent, user_agent, ip);
677 }
679 /******************************************************************************/
681 int gt_packet_send (TCPC *c, GtPacket *packet)
682 {
683 if (!c || c->fd < 0)
684 return -1;
686 if (!gt_node_send (GT_NODE(c), packet))
687 return -1;
689 return 0;
690 }
692 /******************************************************************************/
694 static int send_packetva (TCPC *c, uint8_t cmd,
695 gt_guid_t *guid, uint8_t ttl,
696 uint8_t hops, char *fmt, va_list args)
697 {
698 GtPacket *pkt;
699 char *p;
700 int short_fmt = FALSE;
701 int field_width = 0;
702 int ret;
704 if (!(pkt = gt_packet_new (cmd, ttl, guid)))
705 return -1;
707 for (p = fmt; *p; p++)
708 {
709 switch (*p)
710 {
711 case '%':
712 short_fmt = FALSE;
713 break;
714 case 'h':
715 short_fmt = TRUE;
716 break;
717 case 'l':
718 break;
719 case 'c':
720 {
721 uint8_t bits8 = (uint8_t) va_arg (args, int);
722 gt_packet_put_uint8 (pkt, bits8);
723 }
724 break;
725 case 'u':
726 if (short_fmt)
727 {
728 uint16_t bits16 = (uint16_t) va_arg (args, int);
729 gt_packet_put_uint16 (pkt, bits16);
730 }
731 else
732 {
733 uint32_t bits32 = (uint32_t) va_arg (args, int);
734 gt_packet_put_uint32 (pkt, bits32);
735 }
736 break;
737 case 's':
738 {
739 char *str = va_arg (args, char *);
740 gt_packet_put_str (pkt, str);
741 }
742 break;
743 case '0': case '1': case '2': case '3': case '4': case '5':
744 case '6': case '7': case '8': case '9':
745 field_width = field_width * 10 + (*p - '0');
746 break;
747 case '*':
748 field_width = va_arg (args, int);
749 break;
750 case 'p':
751 {
752 unsigned char *ustr = va_arg (args, unsigned char *);
754 gt_packet_put_ustr (pkt, ustr, field_width);
755 field_width = 0;
756 }
757 break;
758 default:
759 abort ();
760 break;
761 }
762 }
764 if (gt_packet_error (pkt))
765 {
766 gt_packet_free (pkt);
767 return -1;
768 }
770 ret = gt_packet_send (c, pkt);
771 gt_packet_free (pkt);
773 return ret;
774 }
776 int gt_packet_send_fmt (TCPC *c, uint8_t cmd,
777 gt_guid_t *guid, uint8_t ttl,
778 uint8_t hops, char *fmt, ...)
779 {
780 va_list args;
781 int ret;
783 va_start (args, fmt);
784 ret = send_packetva (c, cmd, guid, ttl, hops, fmt, args);
785 va_end (args);
787 return ret;
788 }
790 int gt_packet_reply_fmt (TCPC *c, GtPacket *packet,
791 uint8_t cmd, char *fmt, ...)
792 {
793 va_list args;
794 int ret;
795 size_t hops;
796 gt_guid_t *guid;
798 guid = gt_packet_guid (packet);
799 hops = gt_packet_hops (packet);
801 va_start (args, fmt);
802 ret = send_packetva (c, cmd, guid, hops + 1, 0, fmt, args);
803 va_end (args);
805 return ret;
806 }
808 /******************************************************************************/
810 int gt_packet_forward (GtPacket *packet, TCPC *c)
811 {
812 return -1;
813 }