Mercurial > hg > index.fcgi > gift-gnutella > gift-gnutella-0.0.11-1pba
comparison src/gt_packet.h @ 0:d39e1d0d75b6
initial add
author | paulo@hit-nxdomain.opendns.com |
---|---|
date | Sat, 20 Feb 2010 21:18:28 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:6bd3cabfa7d6 |
---|---|
1 /* | |
2 * $Id: gt_packet.h,v 1.27 2004/05/02 10:09:41 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 */ | |
16 | |
17 #ifndef GIFT_GT_PACKET_H_ | |
18 #define GIFT_GT_PACKET_H_ | |
19 | |
20 /*****************************************************************************/ | |
21 | |
22 #define PACKET_DEBUG gt_config_get_int("packet/debug=0") | |
23 #define PACKET_ASCII_LOG gt_config_get_str("packet/ascii_log_file=/dev/tty") | |
24 | |
25 /*****************************************************************************/ | |
26 | |
27 #define GNUTELLA_HDR_LEN 23 | |
28 | |
29 #define GT_PACKET_INITIAL (48) | |
30 #define GT_PACKET_MAX (65536) /* smallest invalid, including header */ | |
31 | |
32 /*****************************************************************************/ | |
33 | |
34 struct gt_packet | |
35 { | |
36 uint32_t offset; /* Pointer within packet */ | |
37 uint32_t len; /* size of packet structure */ | |
38 uint32_t data_len; /* how big the packet data is */ | |
39 uint32_t error; /* packet encountered memory error */ | |
40 | |
41 unsigned char *data; /* packet header + arbtrary packet data */ | |
42 }; | |
43 | |
44 typedef struct gt_packet GtPacket; | |
45 | |
46 typedef enum gt_packet_type | |
47 { | |
48 GT_MSG_PING = 0x00, | |
49 GT_MSG_PING_REPLY = 0x01, | |
50 GT_MSG_BYE = 0x02, | |
51 GT_MSG_QUERY_ROUTE = 0x30, | |
52 GT_MSG_VENDOR = 0x31, | |
53 GT_MSG_VENDOR_STD = 0x32, | |
54 GT_MSG_PUSH = 0x40, | |
55 GT_MSG_QUERY = 0x80, | |
56 GT_MSG_QUERY_REPLY = 0x81, | |
57 } gt_packet_type_t; | |
58 | |
59 /*****************************************************************************/ | |
60 | |
61 #define VMSG_HDR_LEN (8) | |
62 | |
63 typedef struct gt_vendor_msg | |
64 { | |
65 char vendor_id[4]; | |
66 uint16_t id; | |
67 } gt_vendor_msg_t; | |
68 | |
69 /* | |
70 * Pretend these global variables are enumerated constants...don't squint | |
71 * too hard and you won't see they are global variables. | |
72 */ | |
73 extern const gt_vendor_msg_t *GT_VMSG_MESSAGES_SUPP, | |
74 *GT_VMSG_TCP_CONNECT_BACK, | |
75 *GT_VMSG_HOPS_FLOW, | |
76 *GT_VMSG_PUSH_PROXY_REQ, | |
77 *GT_VMSG_PUSH_PROXY_ACK; | |
78 | |
79 /*****************************************************************************/ | |
80 | |
81 #define get_guid(pkt) ((pkt)[0]) | |
82 #define get_command(pkt) ((pkt)[16]) | |
83 #define get_ttl(pkt) ((pkt)[17]) | |
84 #define get_hops(pkt) ((pkt)[18]) | |
85 | |
86 #define get_payload_len(pkt) \ | |
87 (((pkt)[19]) | ((pkt)[20] << 8) | \ | |
88 ((pkt)[21] << 16) | ((pkt)[22] << 24)) | |
89 | |
90 /*****************************************************************************/ | |
91 | |
92 /* handle little-endian ordering on the network */ | |
93 | |
94 #ifdef WORDS_BIGENDIAN | |
95 /* | |
96 * It's best to watch out for sign extension, so we copy to temporary | |
97 * unsigned variables here. In case the arguments are signed, all is | |
98 * well. | |
99 * | |
100 * NOTE: this depends on a gcc-extension. If someone wants to port to | |
101 * a big-endian, non-gcc compiler, more work here is needed. | |
102 * | |
103 * NOTE2: the uint{16,32}_t types are guaranteed to be _at least_ the | |
104 * number of bits they say they are (i think), so we discard possibly | |
105 * extra bits by using bitwise-and. | |
106 */ | |
107 #define vtohl(x) \ | |
108 ({ \ | |
109 uint32_t _tmp = (x); \ | |
110 (((_tmp & 0xff) << 24) | ((_tmp & 0xff00) << 8) | \ | |
111 ((_tmp & 0xff0000) >> 8) | ((_tmp >> 24) & 0xff)); \ | |
112 }) | |
113 | |
114 #define vtohs(x) \ | |
115 ({ \ | |
116 uint16_t _tmp = (x); \ | |
117 (((_tmp & 0xff) << 8) | ((_tmp >> 8) & 0xff)); \ | |
118 }) | |
119 #else | |
120 #define vtohs(x) (x) | |
121 #define vtohl(x) (x) | |
122 #endif /* WORDS_BIGENDIAN */ | |
123 | |
124 #define htovs(x) vtohs(x) | |
125 #define htovl(x) vtohl(x) | |
126 | |
127 /* endianness defines */ | |
128 #define BIG 1 | |
129 #define LITTLE 0 | |
130 | |
131 /*****************************************************************************/ | |
132 | |
133 #ifdef __GNUC__ | |
134 #define FORMAT_ATTRIBUTE(kind, fmt_arg, first_arg) \ | |
135 __attribute__ ((format (kind, fmt_arg, first_arg))) | |
136 #else | |
137 #define FORMAT_ATTRIBUTE(kind, fmt_arg, first_arg) | |
138 #endif | |
139 | |
140 /*****************************************************************************/ | |
141 | |
142 GtPacket *gt_packet_new (uint8_t cmd, uint8_t ttl, gt_guid_t *guid); | |
143 GtPacket *gt_packet_reply (GtPacket *packet, uint8_t cmd); | |
144 GtPacket *gt_packet_vendor (const gt_vendor_msg_t *vmsg); | |
145 void gt_packet_free (GtPacket *packet); | |
146 int gt_packet_error (GtPacket *packet); | |
147 int gt_packet_seek (GtPacket *packet, int offset); | |
148 | |
149 GtPacket *gt_packet_unserialize (unsigned char *data, size_t len); | |
150 | |
151 /*****************************************************************************/ | |
152 | |
153 gt_guid_t *gt_packet_guid (GtPacket *packet); | |
154 uint32_t gt_packet_payload_len (GtPacket *packet); | |
155 uint32_t gt_packet_command (GtPacket *packet); | |
156 uint8_t gt_packet_hops (GtPacket *packet); | |
157 uint8_t gt_packet_ttl (GtPacket *packet); | |
158 | |
159 /*****************************************************************************/ | |
160 | |
161 void gt_packet_set_guid (GtPacket *packet, gt_guid_t *guid); | |
162 void gt_packet_set_payload_len (GtPacket *packet, uint32_t len); | |
163 void gt_packet_set_command (GtPacket *packet, uint8_t cmd); | |
164 void gt_packet_set_hops (GtPacket *packet, uint8_t hops); | |
165 void gt_packet_set_ttl (GtPacket *packet, uint8_t ttl); | |
166 | |
167 /*****************************************************************************/ | |
168 | |
169 uint32_t gt_packet_get_uint (GtPacket *packet, size_t size, | |
170 int endian, int swap); | |
171 | |
172 uint8_t gt_packet_get_uint8 (GtPacket *packet); | |
173 uint16_t gt_packet_get_uint16 (GtPacket *packet); | |
174 uint32_t gt_packet_get_uint32 (GtPacket *packet); | |
175 | |
176 in_addr_t gt_packet_get_ip (GtPacket *packet); | |
177 in_port_t gt_packet_get_port (GtPacket *packet); | |
178 | |
179 char *gt_packet_get_str (GtPacket *packet); | |
180 unsigned char *gt_packet_get_ustr (GtPacket *packet, size_t len); | |
181 | |
182 /*****************************************************************************/ | |
183 | |
184 int gt_packet_put_uint8 (GtPacket *packet, uint8_t byte); | |
185 int gt_packet_put_uint16 (GtPacket *packet, uint16_t bytes); | |
186 int gt_packet_put_uint32 (GtPacket *packet, uint32_t bytes); | |
187 | |
188 int gt_packet_put_str (GtPacket *packet, const char *str); | |
189 int gt_packet_put_ustr (GtPacket *packet, const unsigned char *ustr, | |
190 size_t len); | |
191 | |
192 int gt_packet_put_ip (GtPacket *packet, in_addr_t ip); | |
193 int gt_packet_put_port (GtPacket *packet, in_port_t port); | |
194 | |
195 /*****************************************************************************/ | |
196 | |
197 int gt_packet_send (TCPC *c, GtPacket *packet); | |
198 | |
199 int gt_packet_send_fmt (TCPC *c, uint8_t cmd, | |
200 gt_guid_t *guid, uint8_t ttl, | |
201 uint8_t hops, char *fmt, ...) | |
202 FORMAT_ATTRIBUTE (printf, 6, 7); | |
203 | |
204 int gt_packet_reply_fmt (TCPC *c, GtPacket *packet, | |
205 uint8_t cmd, char *fmt, ...) | |
206 FORMAT_ATTRIBUTE (printf, 4, 5); | |
207 | |
208 | |
209 /*****************************************************************************/ | |
210 | |
211 void gt_packet_log (GtPacket *packet, struct tcp_conn *src, int sent); | |
212 | |
213 /*****************************************************************************/ | |
214 | |
215 #endif /* GIFT_GT_PACKET_H_ */ |