00001 /***************************************************************************** 00002 * Copyright 2005 Daniel Ferullo * 00003 * * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); * 00005 * you may not use this file except in compliance with the License. * 00006 * You may obtain a copy of the License at * 00007 * * 00008 * http://www.apache.org/licenses/LICENSE-2.0 * 00009 * * 00010 * Unless required by applicable law or agreed to in writing, software * 00011 * distributed under the License is distributed on an "AS IS" BASIS, * 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 00013 * See the License for the specific language governing permissions and * 00014 * limitations under the License. * 00015 * * 00016 *****************************************************************************/ 00017 00018 /** 00019 * @file comm.h 00020 * @author Daniel Ferullo (ferullo@cmu.edu) 00021 * 00022 * @brief contains data structures and messages used in communication 00023 * between a peer and the helper 00024 */ 00025 00026 #ifndef __COMM_H__ 00027 #define __COMM_H__ 00028 00029 #include "def.h" 00030 00031 /********************************************************************* 00032 * All communication packets are of the form: * 00033 * * 00034 * Message Type [4 byte long value] * 00035 * Length after this field [4 byte unsigned long] * 00036 * Rest of Message [a predefined structure] * 00037 * * 00038 * When sent across the network, it is important to make sure that * 00039 * EVERYTHING is net network byte order (big endian). That includes * 00040 * the messsage type and message length fields!!! * 00041 * * 00042 * The message length field isn't technically needed, but it is sent * 00043 * in case any future messages can have variable length. * 00044 * * 00045 *********************************************************************/ 00046 00047 /***************************************************************************** 00048 * Useful Communication Constants * 00049 *****************************************************************************/ 00050 /** @brief the length of the message type field */ 00051 #define COMM_TYPE_LEN 4 00052 00053 /** @brief the length of the length field */ 00054 #define COMM_LENGTH_LEN 4 00055 00056 /** @brief the minimum comminication length (header size) */ 00057 #define COMM_MIN_LEN (COMM_TYPE_LEN + COMM_LENGTH_LEN) 00058 00059 /** @brief the length of the header (same as COMM_MIN_LEN) */ 00060 #define COMM_HEADER_LEN COMM_MIN_LEN 00061 00062 /** @brief the absolute maximum a message (including payload) can be */ 00063 #define COMM_MAX_LEN 1024 00064 00065 00066 /***************************************************************************** 00067 * Useful Typedef's for COMM Info * 00068 *****************************************************************************/ 00069 00070 /** @brief a typedef for the comm type field */ 00071 typedef long comm_type_t; 00072 00073 /** @brief a typdef for the comm length field */ 00074 typedef unsigned long comm_len_t; 00075 00076 /***************************************************************************** 00077 * Communication Types * 00078 *****************************************************************************/ 00079 00080 /** @brief an intial message from a peer to helper starting everything 00081 * (message includes a payload of type comm_peer_info_t) */ 00082 #define COMM_MSG_HELLO 0x0001 00083 00084 /** @brief a message from the helper to the peer asking for a second connection 00085 * to facilitate port prediction */ 00086 #define COMM_MSG_CONNECT_AGAIN 0x1000 00087 00088 /** @brief a message from the peer to the helper indicating that a second 00089 port prediction connection has been made */ 00090 #define COMM_MSG_CONNECTED_AGAIN 0x0002 00091 00092 /** @brief a message from the helper to the peer indicating the success of 00093 the first port prediction attempt */ 00094 #define COMM_MSG_PORT_PRED 0x1002 00095 00096 /** @brief a message from the peer to the helper indicating the peer is now 00097 waiting to get buddy info (this message only exists to maintain the 00098 ping/pong message flow) */ 00099 #define COMM_MSG_WAITING_FOR_BUDDY_ALLOC 0x0003 00100 00101 /** @brief a message from the helper to the peer informing the peer of the 00102 * the buddy's port alloc method, and whether there is support for the 00103 * connection */ 00104 #define COMM_MSG_BUDDY_ALLOC 0x1003 00105 00106 /** @brief a message from the peer to the helper asking for the buddy's 00107 * external port */ 00108 #define COMM_MSG_WAITING_FOR_BUDDY_PORT 0x0004 00109 00110 /** @brief a message from the helper to the peer with info about the buddy */ 00111 #define COMM_MSG_BUDDY_PORT 0x1004 00112 00113 /** @brief message from the peer to the helper with sequence number in the SYN 00114 * packet sent to the buddy */ 00115 #define COMM_MSG_BUDDY_SYN_SEQ 0x0005 00116 00117 /** @brief message from helper to peer relaying the sequence number in the SYN 00118 * from the buddy that the peer should base a forged SYN/ACK on */ 00119 #define COMM_MSG_PEER_SYN_SEQ 0x1005 00120 00121 /** @brief a message from the peer to the helper terminating the protocol */ 00122 #define COMM_MSG_GOODBYE 0x0006 00123 00124 /** @brief a message from the peer to the helper indicating the birthay SYN 00125 * flood has be done */ 00126 #define COMM_MSG_SYN_FLOODED 0x0101 00127 00128 /** @brief a message from the helper to the peer indicating that the buddy has 00129 * commenced the bday synack flood */ 00130 #define COMM_MSG_BUDDY_SYN_ACK_FLOODED 0x1101 00131 00132 /** @brief a message from the peer to the helper indicating the port that made 00133 * if through the NAT */ 00134 #define COMM_MSG_BDAY_SUCCESS_PORT 0x0102 00135 00136 /** @brief a message from the peer to the helper indicating the peer is ready 00137 * to be told to do a birthday flood */ 00138 #define COMM_MSG_WAITING_TO_SYN_ACK_FLOOD 0x0201 00139 00140 /** @brief a message from the helper to the peer with the sequence number that 00141 * the peer used in the SYN flood */ 00142 #define COMM_MSG_SYN_ACK_FLOOD_SEQ_NUM 0x1201 00143 00144 /** @brief a message from the peer to the helper indicating that the synack 00145 * flood has been completed */ 00146 #define COMM_MSG_SYN_ACK_FLOOD_DONE 0x0202 00147 00148 /***************************************************************************** 00149 * Port Allocation Types * 00150 *****************************************************************************/ 00151 00152 /** @brief port allocation method is unknown */ 00153 #define COMM_PORT_ALLOC_UNKNOWN -1 00154 00155 /** @brief port allocation method is sequential */ 00156 #define COMM_PORT_ALLOC_SEQ 1 00157 00158 /** @brief port allocation method is random */ 00159 #define COMM_PORT_ALLOC_RAND 2 00160 00161 /***************************************************************************** 00162 * Communication Support Types * 00163 *****************************************************************************/ 00164 /** @brief unsupported connection */ 00165 #define COMM_CONNECTION_UNSUPPORTED 0 00166 00167 /** @brief supported connection */ 00168 #define COMM_CONNECTION_SUPPORTED 1 00169 00170 /***************************************************************************** 00171 * Flags indicating if DBay is needed * 00172 *****************************************************************************/ 00173 /** @brief birthday paradox not needed for peer */ 00174 #define COMM_BDAY_NOT_NEEDED 0 00175 00176 /** @brief birthday paradox needed for peer */ 00177 #define COMM_BDAY_NEEDED 1 00178 00179 /***************************************************************************** 00180 * Possible Communication Payloads * 00181 *****************************************************************************/ 00182 00183 /** @brief structure for a COMM_MSG_HELLO message payload */ 00184 struct comm_msg_hello { 00185 /** @brief the peer's internal ip */ 00186 ip_t peer_ip; 00187 /** @brief the peer's internal port */ 00188 port_t peer_port; 00189 /** @brief the buddy's internal ip */ 00190 ip_t buddy_int_ip; 00191 /** @brief the buddy's internal port */ 00192 port_t buddy_int_port; 00193 /** @brief the buddy's external ip */ 00194 ip_t buddy_ext_ip; 00195 } __attribute__((__packed__)); 00196 00197 /** @brief typedef for the COMM_MSG_HELLO payload structure */ 00198 typedef struct comm_msg_hello comm_msg_hello_t; 00199 00200 00201 00202 /** @brief structure to hold payload for a COMM_MSG_PORT_PRED message */ 00203 struct comm_msg_pred_port { 00204 /** @brief the port allocation method */ 00205 flag_t port_alloc; 00206 } __attribute__((__packed__)); 00207 00208 /** @brief typedef for the COMM_MSG_PORT_PRED payload structure */ 00209 typedef struct comm_msg_pred_port comm_msg_pred_port_t; 00210 00211 00212 00213 /** @brief structure to hold payload for a COMM_MSG_BUDDY_ALLOC message */ 00214 struct comm_msg_buddy_alloc { 00215 /** @brief the buddy's port allocation method */ 00216 flag_t buddy_port_alloc; 00217 /** @brief whether or not there is support for the connection */ 00218 flag_t support; 00219 } __attribute__((__packed__)); 00220 00221 /** @brief typedef for the COMM_MSG_BUDDY_ALLOC payload structure */ 00222 typedef struct comm_msg_buddy_alloc comm_msg_buddy_alloc_t; 00223 00224 00225 00226 /** @brief structure to hold payload for a COMM_MSG_BUDDY_INFO message */ 00227 struct comm_msg_buddy_info { 00228 /** @brief the external port the buddy will work on */ 00229 port_t ext_port; 00230 /** @brief whether the birthday paradox is needed to determine 00231 * peer's external port. if it is, then the peer should start 00232 * the paradox */ 00233 flag_t bday; 00234 } __attribute__((__packed__)); 00235 00236 /** @brief typedef for the COMM_MSG_BUDDY_PORT payload structure */ 00237 typedef struct comm_msg_buddy_info comm_msg_buddy_port_t; 00238 00239 00240 00241 /** @brief structure to hold payload for a COMM_MSG_SYN_TO_BUDDY_SEQ message */ 00242 struct comm_msg_buddy_syn_seq { 00243 /** @brief the sequence number of the SYN */ 00244 seq_num_t seq_num; 00245 } __attribute__((__packed__)); 00246 00247 /** @brief a typedef for the COMM_MSG_BUDDY_SYN_SEQ message */ 00248 typedef struct comm_msg_buddy_syn_seq comm_msg_buddy_syn_seq_t; 00249 00250 00251 00252 /** @brief structure to hold the COMM_MSG_PEER_SYN_SEQ payload */ 00253 struct comm_msg_peer_syn_seq { 00254 /** @brief the sequence number from the peer's SYN to the buddy */ 00255 seq_num_t seq_num; 00256 } __attribute__((__packed__)); 00257 00258 /** @brief typedef for the COMM_MSG_PEER_SYN_SEQ payload structure */ 00259 typedef struct comm_msg_peer_syn_seq comm_msg_peer_syn_seq_t; 00260 00261 00262 00263 /** @brief structure to hold the COMM_MSG_GOODBYE payload */ 00264 struct comm_msg_goodbye { 00265 /** @brief a flag indicating if the connection attempt worked */ 00266 flag_t success_or_failure; 00267 } __attribute__((__packed__)); 00268 00269 /** @brief typedef for the COMM_MSG_GOODBYE structure */ 00270 typedef struct comm_msg_goodbye comm_msg_goodbye_t; 00271 00272 00273 /** @brief structure to hold the COMM_MSG_SYN_FLOODED payload */ 00274 struct comm_msg_syn_flooded { 00275 /** @brief the sequence number used in the flood */ 00276 seq_num_t seq_num; 00277 } __attribute__((__packed__)); 00278 00279 /** @brief a typedef for the COMM_MSG_SYN_FLOODED payload structure */ 00280 typedef struct comm_msg_syn_flooded comm_msg_syn_flooded_t; 00281 00282 00283 /** @brief structure to hold the COMM_MSG_BDAY_SUCCESS_PORT payload */ 00284 struct comm_msg_bday_success_port { 00285 /** @brief the port that a synack was sent to in the bday flood */ 00286 port_t port; 00287 } __attribute__((__packed__)); 00288 00289 /** @brief typedef for the COMM_MSG_BDAY_SUCCESS_PORT payload structure */ 00290 typedef struct comm_msg_bday_success_port comm_msg_bday_success_port_t; 00291 00292 00293 /** @brief structure to hold the COMM_MSG_SYN_ACK_FLOOD_SEQ_NUM payload */ 00294 struct comm_msg_syn_ack_flood_seq_num { 00295 /** @brief the sequence number used in the SYNs sent by the buddy */ 00296 seq_num_t seq_num; 00297 } __attribute__((__packed__)); 00298 00299 /** @brief typedef for the COMM_MSG_SYN_ACK_FLOOD_SEQ_NUM payload structure */ 00300 typedef struct comm_msg_syn_ack_flood_seq_num comm_msg_syn_ack_flood_seq_num_t; 00301 00302 #endif /* __COMM_H__ */ 00303