Mercurial > hg > index.fcgi > gift-gnutella > gift-gnutella-0.0.11-1pba
diff 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 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/gt_packet.h Sat Feb 20 21:18:28 2010 -0800 1.3 @@ -0,0 +1,215 @@ 1.4 +/* 1.5 + * $Id: gt_packet.h,v 1.27 2004/05/02 10:09:41 hipnod Exp $ 1.6 + * 1.7 + * Copyright (C) 2001-2003 giFT project (gift.sourceforge.net) 1.8 + * 1.9 + * This program is free software; you can redistribute it and/or modify it 1.10 + * under the terms of the GNU General Public License as published by the 1.11 + * Free Software Foundation; either version 2, or (at your option) any 1.12 + * later version. 1.13 + * 1.14 + * This program is distributed in the hope that it will be useful, but 1.15 + * WITHOUT ANY WARRANTY; without even the implied warranty of 1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1.17 + * General Public License for more details. 1.18 + */ 1.19 + 1.20 +#ifndef GIFT_GT_PACKET_H_ 1.21 +#define GIFT_GT_PACKET_H_ 1.22 + 1.23 +/*****************************************************************************/ 1.24 + 1.25 +#define PACKET_DEBUG gt_config_get_int("packet/debug=0") 1.26 +#define PACKET_ASCII_LOG gt_config_get_str("packet/ascii_log_file=/dev/tty") 1.27 + 1.28 +/*****************************************************************************/ 1.29 + 1.30 +#define GNUTELLA_HDR_LEN 23 1.31 + 1.32 +#define GT_PACKET_INITIAL (48) 1.33 +#define GT_PACKET_MAX (65536) /* smallest invalid, including header */ 1.34 + 1.35 +/*****************************************************************************/ 1.36 + 1.37 +struct gt_packet 1.38 +{ 1.39 + uint32_t offset; /* Pointer within packet */ 1.40 + uint32_t len; /* size of packet structure */ 1.41 + uint32_t data_len; /* how big the packet data is */ 1.42 + uint32_t error; /* packet encountered memory error */ 1.43 + 1.44 + unsigned char *data; /* packet header + arbtrary packet data */ 1.45 +}; 1.46 + 1.47 +typedef struct gt_packet GtPacket; 1.48 + 1.49 +typedef enum gt_packet_type 1.50 +{ 1.51 + GT_MSG_PING = 0x00, 1.52 + GT_MSG_PING_REPLY = 0x01, 1.53 + GT_MSG_BYE = 0x02, 1.54 + GT_MSG_QUERY_ROUTE = 0x30, 1.55 + GT_MSG_VENDOR = 0x31, 1.56 + GT_MSG_VENDOR_STD = 0x32, 1.57 + GT_MSG_PUSH = 0x40, 1.58 + GT_MSG_QUERY = 0x80, 1.59 + GT_MSG_QUERY_REPLY = 0x81, 1.60 +} gt_packet_type_t; 1.61 + 1.62 +/*****************************************************************************/ 1.63 + 1.64 +#define VMSG_HDR_LEN (8) 1.65 + 1.66 +typedef struct gt_vendor_msg 1.67 +{ 1.68 + char vendor_id[4]; 1.69 + uint16_t id; 1.70 +} gt_vendor_msg_t; 1.71 + 1.72 +/* 1.73 + * Pretend these global variables are enumerated constants...don't squint 1.74 + * too hard and you won't see they are global variables. 1.75 + */ 1.76 +extern const gt_vendor_msg_t *GT_VMSG_MESSAGES_SUPP, 1.77 + *GT_VMSG_TCP_CONNECT_BACK, 1.78 + *GT_VMSG_HOPS_FLOW, 1.79 + *GT_VMSG_PUSH_PROXY_REQ, 1.80 + *GT_VMSG_PUSH_PROXY_ACK; 1.81 + 1.82 +/*****************************************************************************/ 1.83 + 1.84 +#define get_guid(pkt) ((pkt)[0]) 1.85 +#define get_command(pkt) ((pkt)[16]) 1.86 +#define get_ttl(pkt) ((pkt)[17]) 1.87 +#define get_hops(pkt) ((pkt)[18]) 1.88 + 1.89 +#define get_payload_len(pkt) \ 1.90 + (((pkt)[19]) | ((pkt)[20] << 8) | \ 1.91 + ((pkt)[21] << 16) | ((pkt)[22] << 24)) 1.92 + 1.93 +/*****************************************************************************/ 1.94 + 1.95 +/* handle little-endian ordering on the network */ 1.96 + 1.97 +#ifdef WORDS_BIGENDIAN 1.98 +/* 1.99 + * It's best to watch out for sign extension, so we copy to temporary 1.100 + * unsigned variables here. In case the arguments are signed, all is 1.101 + * well. 1.102 + * 1.103 + * NOTE: this depends on a gcc-extension. If someone wants to port to 1.104 + * a big-endian, non-gcc compiler, more work here is needed. 1.105 + * 1.106 + * NOTE2: the uint{16,32}_t types are guaranteed to be _at least_ the 1.107 + * number of bits they say they are (i think), so we discard possibly 1.108 + * extra bits by using bitwise-and. 1.109 + */ 1.110 +#define vtohl(x) \ 1.111 + ({ \ 1.112 + uint32_t _tmp = (x); \ 1.113 + (((_tmp & 0xff) << 24) | ((_tmp & 0xff00) << 8) | \ 1.114 + ((_tmp & 0xff0000) >> 8) | ((_tmp >> 24) & 0xff)); \ 1.115 + }) 1.116 + 1.117 +#define vtohs(x) \ 1.118 + ({ \ 1.119 + uint16_t _tmp = (x); \ 1.120 + (((_tmp & 0xff) << 8) | ((_tmp >> 8) & 0xff)); \ 1.121 + }) 1.122 +#else 1.123 +#define vtohs(x) (x) 1.124 +#define vtohl(x) (x) 1.125 +#endif /* WORDS_BIGENDIAN */ 1.126 + 1.127 +#define htovs(x) vtohs(x) 1.128 +#define htovl(x) vtohl(x) 1.129 + 1.130 +/* endianness defines */ 1.131 +#define BIG 1 1.132 +#define LITTLE 0 1.133 + 1.134 +/*****************************************************************************/ 1.135 + 1.136 +#ifdef __GNUC__ 1.137 +#define FORMAT_ATTRIBUTE(kind, fmt_arg, first_arg) \ 1.138 + __attribute__ ((format (kind, fmt_arg, first_arg))) 1.139 +#else 1.140 +#define FORMAT_ATTRIBUTE(kind, fmt_arg, first_arg) 1.141 +#endif 1.142 + 1.143 +/*****************************************************************************/ 1.144 + 1.145 +GtPacket *gt_packet_new (uint8_t cmd, uint8_t ttl, gt_guid_t *guid); 1.146 +GtPacket *gt_packet_reply (GtPacket *packet, uint8_t cmd); 1.147 +GtPacket *gt_packet_vendor (const gt_vendor_msg_t *vmsg); 1.148 +void gt_packet_free (GtPacket *packet); 1.149 +int gt_packet_error (GtPacket *packet); 1.150 +int gt_packet_seek (GtPacket *packet, int offset); 1.151 + 1.152 +GtPacket *gt_packet_unserialize (unsigned char *data, size_t len); 1.153 + 1.154 +/*****************************************************************************/ 1.155 + 1.156 +gt_guid_t *gt_packet_guid (GtPacket *packet); 1.157 +uint32_t gt_packet_payload_len (GtPacket *packet); 1.158 +uint32_t gt_packet_command (GtPacket *packet); 1.159 +uint8_t gt_packet_hops (GtPacket *packet); 1.160 +uint8_t gt_packet_ttl (GtPacket *packet); 1.161 + 1.162 +/*****************************************************************************/ 1.163 + 1.164 +void gt_packet_set_guid (GtPacket *packet, gt_guid_t *guid); 1.165 +void gt_packet_set_payload_len (GtPacket *packet, uint32_t len); 1.166 +void gt_packet_set_command (GtPacket *packet, uint8_t cmd); 1.167 +void gt_packet_set_hops (GtPacket *packet, uint8_t hops); 1.168 +void gt_packet_set_ttl (GtPacket *packet, uint8_t ttl); 1.169 + 1.170 +/*****************************************************************************/ 1.171 + 1.172 +uint32_t gt_packet_get_uint (GtPacket *packet, size_t size, 1.173 + int endian, int swap); 1.174 + 1.175 +uint8_t gt_packet_get_uint8 (GtPacket *packet); 1.176 +uint16_t gt_packet_get_uint16 (GtPacket *packet); 1.177 +uint32_t gt_packet_get_uint32 (GtPacket *packet); 1.178 + 1.179 +in_addr_t gt_packet_get_ip (GtPacket *packet); 1.180 +in_port_t gt_packet_get_port (GtPacket *packet); 1.181 + 1.182 +char *gt_packet_get_str (GtPacket *packet); 1.183 +unsigned char *gt_packet_get_ustr (GtPacket *packet, size_t len); 1.184 + 1.185 +/*****************************************************************************/ 1.186 + 1.187 +int gt_packet_put_uint8 (GtPacket *packet, uint8_t byte); 1.188 +int gt_packet_put_uint16 (GtPacket *packet, uint16_t bytes); 1.189 +int gt_packet_put_uint32 (GtPacket *packet, uint32_t bytes); 1.190 + 1.191 +int gt_packet_put_str (GtPacket *packet, const char *str); 1.192 +int gt_packet_put_ustr (GtPacket *packet, const unsigned char *ustr, 1.193 + size_t len); 1.194 + 1.195 +int gt_packet_put_ip (GtPacket *packet, in_addr_t ip); 1.196 +int gt_packet_put_port (GtPacket *packet, in_port_t port); 1.197 + 1.198 +/*****************************************************************************/ 1.199 + 1.200 +int gt_packet_send (TCPC *c, GtPacket *packet); 1.201 + 1.202 +int gt_packet_send_fmt (TCPC *c, uint8_t cmd, 1.203 + gt_guid_t *guid, uint8_t ttl, 1.204 + uint8_t hops, char *fmt, ...) 1.205 + FORMAT_ATTRIBUTE (printf, 6, 7); 1.206 + 1.207 +int gt_packet_reply_fmt (TCPC *c, GtPacket *packet, 1.208 + uint8_t cmd, char *fmt, ...) 1.209 + FORMAT_ATTRIBUTE (printf, 4, 5); 1.210 + 1.211 + 1.212 +/*****************************************************************************/ 1.213 + 1.214 +void gt_packet_log (GtPacket *packet, struct tcp_conn *src, int sent); 1.215 + 1.216 +/*****************************************************************************/ 1.217 + 1.218 +#endif /* GIFT_GT_PACKET_H_ */