diff options
-rw-r--r-- | src/transport/gnunet-nat-client.c | 84 | ||||
-rw-r--r-- | src/transport/gnunet-nat-server.c | 37 |
2 files changed, 108 insertions, 13 deletions
diff --git a/src/transport/gnunet-nat-client.c b/src/transport/gnunet-nat-client.c index b9c61a143..cbfc0a28c 100644 --- a/src/transport/gnunet-nat-client.c +++ b/src/transport/gnunet-nat-client.c | |||
@@ -58,6 +58,7 @@ | |||
58 | * Must match IP given in the server. | 58 | * Must match IP given in the server. |
59 | */ | 59 | */ |
60 | #define DUMMY_IP "1.2.3.4" | 60 | #define DUMMY_IP "1.2.3.4" |
61 | #define HAVE_PORT 1 | ||
61 | 62 | ||
62 | struct ip_packet | 63 | struct ip_packet |
63 | { | 64 | { |
@@ -79,7 +80,18 @@ struct icmp_packet | |||
79 | uint8_t code; | 80 | uint8_t code; |
80 | uint16_t checksum; | 81 | uint16_t checksum; |
81 | uint32_t reserved; | 82 | uint32_t reserved; |
83 | |||
82 | }; | 84 | }; |
85 | |||
86 | struct icmp_echo_packet | ||
87 | { | ||
88 | uint8_t type; | ||
89 | uint8_t code; | ||
90 | uint16_t checksum; | ||
91 | uint32_t reserved; | ||
92 | uint32_t data; | ||
93 | }; | ||
94 | |||
83 | 95 | ||
84 | static int rawsock; | 96 | static int rawsock; |
85 | 97 | ||
@@ -87,6 +99,9 @@ static struct in_addr dummy; | |||
87 | 99 | ||
88 | static struct in_addr target; | 100 | static struct in_addr target; |
89 | 101 | ||
102 | #if HAVE_PORT | ||
103 | static uint32_t port; | ||
104 | #endif | ||
90 | 105 | ||
91 | static uint16_t | 106 | static uint16_t |
92 | calc_checksum(const uint16_t *data, | 107 | calc_checksum(const uint16_t *data, |
@@ -104,18 +119,36 @@ calc_checksum(const uint16_t *data, | |||
104 | } | 119 | } |
105 | 120 | ||
106 | 121 | ||
122 | |||
123 | #if HAVE_PORT | ||
124 | |||
107 | static void | 125 | static void |
108 | make_echo (const struct in_addr *src_ip, | 126 | make_echo (const struct in_addr *src_ip, |
109 | struct icmp_packet *echo) | 127 | struct icmp_echo_packet *echo, uint32_t num) |
110 | { | 128 | { |
111 | memset(echo, 0, sizeof(struct icmp_packet)); | 129 | memset(echo, 0, sizeof(struct icmp_echo_packet)); |
112 | echo->type = ICMP_ECHO; | 130 | echo->type = ICMP_ECHO; |
113 | echo->code = 0; | 131 | echo->code = 0; |
114 | echo->reserved = 0; | 132 | echo->reserved = 0; |
115 | echo->checksum = 0; | 133 | echo->checksum = 0; |
134 | echo->data = htons(num); | ||
116 | echo->checksum = htons(calc_checksum((uint16_t*)echo, | 135 | echo->checksum = htons(calc_checksum((uint16_t*)echo, |
117 | sizeof (struct icmp_packet))); | 136 | sizeof (struct icmp_echo_packet))); |
118 | } | 137 | } |
138 | #else | ||
139 | static void | ||
140 | make_echo (const struct in_addr *src_ip, | ||
141 | struct icmp_packet *echo) | ||
142 | { | ||
143 | memset(echo, 0, sizeof(struct icmp_packet)); | ||
144 | echo->type = ICMP_ECHO; | ||
145 | echo->code = 0; | ||
146 | echo->reserved = 0; | ||
147 | echo->checksum = 0; | ||
148 | echo->checksum = htons(calc_checksum((uint16_t*)echo, | ||
149 | sizeof (struct icmp_packet))); | ||
150 | } | ||
151 | #endif | ||
119 | 152 | ||
120 | 153 | ||
121 | /** | 154 | /** |
@@ -130,9 +163,17 @@ send_icmp (const struct in_addr *my_ip, | |||
130 | { | 163 | { |
131 | struct ip_packet ip_pkt; | 164 | struct ip_packet ip_pkt; |
132 | struct icmp_packet *icmp_pkt; | 165 | struct icmp_packet *icmp_pkt; |
166 | #if HAVE_PORT | ||
167 | struct icmp_echo_packet icmp_echo; | ||
168 | #else | ||
133 | struct icmp_packet icmp_echo; | 169 | struct icmp_packet icmp_echo; |
170 | #endif | ||
134 | struct sockaddr_in dst; | 171 | struct sockaddr_in dst; |
172 | #if HAVE_PORT | ||
173 | char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)]; | ||
174 | #else | ||
135 | char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet)*2]; | 175 | char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet)*2]; |
176 | #endif | ||
136 | size_t off; | 177 | size_t off; |
137 | int err; | 178 | int err; |
138 | 179 | ||
@@ -159,12 +200,18 @@ send_icmp (const struct in_addr *my_ip, | |||
159 | icmp_pkt->code = 0; | 200 | icmp_pkt->code = 0; |
160 | icmp_pkt->reserved = 0; | 201 | icmp_pkt->reserved = 0; |
161 | icmp_pkt->checksum = 0; | 202 | icmp_pkt->checksum = 0; |
203 | |||
162 | off += sizeof (struct icmp_packet); | 204 | off += sizeof (struct icmp_packet); |
163 | 205 | ||
164 | /* ip header of the presumably 'lost' udp packet */ | 206 | /* ip header of the presumably 'lost' udp packet */ |
165 | ip_pkt.vers_ihl = 0x45; | 207 | ip_pkt.vers_ihl = 0x45; |
166 | ip_pkt.tos = 0; | 208 | ip_pkt.tos = 0; |
209 | #if HAVE_PORT | ||
210 | ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet)); | ||
211 | #else | ||
167 | ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_packet)); | 212 | ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_packet)); |
213 | #endif | ||
214 | |||
168 | ip_pkt.id = 1; | 215 | ip_pkt.id = 1; |
169 | ip_pkt.flags_frag_offset = 0; | 216 | ip_pkt.flags_frag_offset = 0; |
170 | ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ | 217 | ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ |
@@ -175,11 +222,28 @@ send_icmp (const struct in_addr *my_ip, | |||
175 | ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet))); | 222 | ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet))); |
176 | memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet)); | 223 | memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet)); |
177 | off += sizeof (struct ip_packet); | 224 | off += sizeof (struct ip_packet); |
225 | |||
226 | #if HAVE_PORT | ||
227 | make_echo (other, &icmp_echo, port); | ||
228 | memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet)); | ||
229 | off += sizeof (struct icmp_echo_packet); | ||
230 | #else | ||
178 | make_echo (other, &icmp_echo); | 231 | make_echo (other, &icmp_echo); |
179 | memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_packet)); | 232 | memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_packet)); |
180 | off += sizeof (struct icmp_packet); | 233 | off += sizeof (struct icmp_packet); |
234 | #endif | ||
235 | |||
236 | #if HAVE_PORT | ||
237 | icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt, | ||
238 | sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet))); | ||
239 | |||
240 | #else | ||
181 | icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt, | 241 | icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt, |
182 | sizeof (struct icmp_packet)*2 + sizeof(struct ip_packet))); | 242 | sizeof (struct icmp_packet)*2 + sizeof(struct ip_packet))); |
243 | |||
244 | #endif | ||
245 | |||
246 | |||
183 | 247 | ||
184 | memset (&dst, 0, sizeof (dst)); | 248 | memset (&dst, 0, sizeof (dst)); |
185 | dst.sin_family = AF_INET; | 249 | dst.sin_family = AF_INET; |
@@ -243,12 +307,22 @@ main (int argc, char *const *argv) | |||
243 | fprintf (stderr, | 307 | fprintf (stderr, |
244 | "Failed to setresuid: %s\n", | 308 | "Failed to setresuid: %s\n", |
245 | strerror (errno)); | 309 | strerror (errno)); |
310 | #if HAVE_PORT | ||
311 | if (argc != 4) | ||
312 | { | ||
313 | fprintf (stderr, | ||
314 | "This program must be started with our IP, the targets external IP, and our port as arguments.\n"); | ||
315 | return 1; | ||
316 | } | ||
317 | port = atoi(argv[3]); | ||
318 | #else | ||
246 | if (argc != 3) | 319 | if (argc != 3) |
247 | { | 320 | { |
248 | fprintf (stderr, | 321 | fprintf (stderr, |
249 | "This program must be started with our IP and the targets external IP as arguments.\n"); | 322 | "This program must be started with our IP and the targets external IP as arguments.\n"); |
250 | return 1; | 323 | return 1; |
251 | } | 324 | } |
325 | #endif | ||
252 | if ( (1 != inet_pton (AF_INET, argv[1], &external)) || | 326 | if ( (1 != inet_pton (AF_INET, argv[1], &external)) || |
253 | (1 != inet_pton (AF_INET, argv[2], &target)) ) | 327 | (1 != inet_pton (AF_INET, argv[2], &target)) ) |
254 | { | 328 | { |
diff --git a/src/transport/gnunet-nat-server.c b/src/transport/gnunet-nat-server.c index df6b69c65..6bead4c31 100644 --- a/src/transport/gnunet-nat-server.c +++ b/src/transport/gnunet-nat-server.c | |||
@@ -62,7 +62,7 @@ | |||
62 | */ | 62 | */ |
63 | #define DUMMY_IP "1.2.3.4" | 63 | #define DUMMY_IP "1.2.3.4" |
64 | 64 | ||
65 | #define VERBOSE GNUNET_NO | 65 | #define VERBOSE 1 |
66 | 66 | ||
67 | /** | 67 | /** |
68 | * How often do we send our ICMP messages to receive replies? | 68 | * How often do we send our ICMP messages to receive replies? |
@@ -193,6 +193,8 @@ process_icmp_response () | |||
193 | struct ip_packet ip_pkt; | 193 | struct ip_packet ip_pkt; |
194 | struct icmp_packet icmp_pkt; | 194 | struct icmp_packet icmp_pkt; |
195 | size_t off; | 195 | size_t off; |
196 | int have_port; | ||
197 | uint32_t port; | ||
196 | 198 | ||
197 | have = read (icmpsock, buf, sizeof (buf)); | 199 | have = read (icmpsock, buf, sizeof (buf)); |
198 | if (have == -1) | 200 | if (have == -1) |
@@ -202,7 +204,12 @@ process_icmp_response () | |||
202 | strerror (errno)); | 204 | strerror (errno)); |
203 | return; | 205 | return; |
204 | } | 206 | } |
205 | if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2) | 207 | have_port = 0; |
208 | if (have == sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2 + sizeof(uint32_t)) | ||
209 | { | ||
210 | have_port = 1; | ||
211 | } | ||
212 | else if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2) | ||
206 | { | 213 | { |
207 | #if VERBOSE | 214 | #if VERBOSE |
208 | fprintf (stderr, | 215 | fprintf (stderr, |
@@ -226,12 +233,26 @@ process_icmp_response () | |||
226 | memcpy(&sip, | 233 | memcpy(&sip, |
227 | &ip_pkt.src_ip, | 234 | &ip_pkt.src_ip, |
228 | sizeof (sip)); | 235 | sizeof (sip)); |
229 | fprintf (stdout, | 236 | if (have_port) |
230 | "%s\n", | 237 | { |
231 | inet_ntop (AF_INET, | 238 | memcpy(&port, &buf[sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2], sizeof(uint32_t)); |
232 | &sip, | 239 | port = ntohs(port); |
233 | buf, | 240 | fprintf (stdout, |
234 | sizeof (buf))); | 241 | "%s:%d\n", |
242 | inet_ntop (AF_INET, | ||
243 | &sip, | ||
244 | buf, | ||
245 | sizeof (buf)), port); | ||
246 | } | ||
247 | else | ||
248 | { | ||
249 | fprintf (stdout, | ||
250 | "%s\n", | ||
251 | inet_ntop (AF_INET, | ||
252 | &sip, | ||
253 | buf, | ||
254 | sizeof (buf))); | ||
255 | } | ||
235 | fflush (stdout); | 256 | fflush (stdout); |
236 | } | 257 | } |
237 | 258 | ||