/**
* IPv4 header.
*/
-struct ip_packet
+struct ip_header
{
/**
/**
* Time to live
*/
- uint8_t ttl;
+ uint8_t ttl;
/**
* Protocol
*/
- uint8_t proto;
+ uint8_t proto;
/**
* Header checksum
/**
* Source address
*/
- uint32_t src_ip;
+ uint32_t src_ip;
/**
* Destination address
*/
- uint32_t dst_ip;
+ uint32_t dst_ip;
};
/**
* Format of ICMP packet.
*/
-struct icmp_packet
+struct icmp_ttl_exceeded_header
{
uint8_t type;
uint16_t checksum;
- uint32_t reserved;
+ uint32_t unused;
+
+ /* followed by original payload */
};
-struct icmp_echo_packet
+struct icmp_echo_header
{
uint8_t type;
+
uint8_t code;
+
uint16_t checksum;
+
uint32_t reserved;
- uint32_t data;
};
/**
* Beginning of UDP packet.
*/
-struct udp_packet
+struct udp_header
{
uint16_t src_port;
uint16_t dst_port;
- uint32_t length;
+ uint16_t length;
+
+ uint16_t crc;
};
send_icmp_udp (const struct in_addr *my_ip,
const struct in_addr *other)
{
- struct ip_packet ip_pkt;
- struct icmp_packet icmp_pkt;
- struct udp_packet udp_pkt;
-
+ char packet[sizeof(struct ip_header) * 2 +
+ sizeof(struct icmp_ttl_exceeded_header) +
+ sizeof(struct udp_header)];
+ struct ip_header ip_pkt;
+ struct icmp_ttl_exceeded_header icmp_pkt;
+ struct udp_header udp_pkt;
struct sockaddr_in dst;
- char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
-
size_t off;
int err;
ip_pkt.checksum = 0;
ip_pkt.src_ip = my_ip->s_addr;
ip_pkt.dst_ip = other->s_addr;
- ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
- memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
- off += sizeof(ip_pkt);
-
- /* ip header of the presumably 'lost' udp packet */
- ip_pkt.vers_ihl = 0x45;
- ip_pkt.tos = 0;
- ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
-
- icmp_pkt.type = 11; /* TTL exceeded */
+ ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
+ sizeof (struct ip_header)));
+ memcpy(&packet[off],
+ &ip_pkt,
+ sizeof(struct ip_header));
+ off += sizeof(struct ip_header);
+
+ icmp_pkt.type = ICMP_TIME_EXCEEDED;
icmp_pkt.code = 0;
icmp_pkt.checksum = 0;
- icmp_pkt.reserved = 0;
- memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
- off += sizeof(icmp_pkt);
+ icmp_pkt.unused = 0;
+ memcpy(&packet[off],
+ &icmp_pkt,
+ sizeof(struct icmp_ttl_exceeded_header));
+ off += sizeof(struct icmp_ttl_exceeded_header);
- /* build inner IP header */
- memset(&ip_pkt, 0, sizeof(ip_pkt));
+ /* ip header of the presumably 'lost' udp packet */
ip_pkt.vers_ihl = 0x45;
ip_pkt.tos = 0;
- ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
+ ip_pkt.pkt_len = htons(sizeof (struct ip_header) +
+ sizeof (struct udp_header));
ip_pkt.id = htons(0);
ip_pkt.flags_frag_offset = 0;
ip_pkt.ttl = 128;
ip_pkt.checksum = 0;
ip_pkt.src_ip = other->s_addr;
ip_pkt.dst_ip = dummy.s_addr;
- ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
- memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
- off += sizeof(ip_pkt);
+ ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
+ sizeof (struct ip_header)));
+ memcpy(&packet[off],
+ &ip_pkt,
+ sizeof(struct ip_header));
+ off += sizeof(struct ip_header);
/* build UDP header */
udp_pkt.src_port = htons(NAT_TRAV_PORT);
- udp_pkt.dst_port = htons(NAT_TRAV_PORT);
-
- memset(&udp_pkt.length, 0, sizeof(uint32_t));
+ udp_pkt.dst_port = htons(NAT_TRAV_PORT);
udp_pkt.length = htons (port);
- memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
- off += sizeof(udp_pkt);
+ udp_pkt.crc = 0;
+ memcpy(&packet[off],
+ &udp_pkt,
+ sizeof(struct udp_header));
+ off += sizeof(struct udp_header);
/* no go back to calculate ICMP packet checksum */
- off = sizeof (ip_pkt);
icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[off],
- sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
- memcpy (&packet[off], &icmp_pkt, sizeof (icmp_pkt));
-
+ sizeof (struct icmp_ttl_exceeded_header) +
+ sizeof (struct ip_header) +
+ sizeof (struct udp_header)));
+ memcpy (&packet[sizeof (struct ip_header)],
+ &icmp_pkt,
+ sizeof (struct icmp_ttl_exceeded_header));
memset (&dst, 0, sizeof (dst));
dst.sin_family = AF_INET;
sizeof (packet), 0,
(struct sockaddr*)&dst,
sizeof(dst));
-
if (err < 0)
{
fprintf(stderr,
"sendto failed: %s\n", strerror(errno));
}
- else if (err != sizeof (packet))
+ else if (sizeof (packet) != (size_t) err)
{
fprintf(stderr,
"Error: partial send of ICMP message\n");
send_icmp (const struct in_addr *my_ip,
const struct in_addr *other)
{
- struct ip_packet ip_pkt;
- struct icmp_packet *icmp_pkt;
- struct icmp_echo_packet icmp_echo;
+ struct ip_header ip_pkt;
+ struct icmp_ttl_exceeded_header icmp_ttl;
+ struct icmp_echo_header icmp_echo;
struct sockaddr_in dst;
- char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
-
+ char packet[sizeof (struct ip_header) * 2 +
+ sizeof (struct icmp_ttl_exceeded_header) +
+ sizeof(struct icmp_echo_header)];
size_t off;
int err;
- memset(packet, 0, sizeof(packet));
/* ip header: send to (known) ip address */
off = 0;
- memset(&ip_pkt, 0, sizeof(ip_pkt));
ip_pkt.vers_ihl = 0x45;
ip_pkt.tos = 0;
- ip_pkt.pkt_len = sizeof (packet); /* huh? */
- ip_pkt.id = 1;
+ ip_pkt.pkt_len = htons (sizeof (packet));
+ ip_pkt.id = htons(256);
ip_pkt.flags_frag_offset = 0;
ip_pkt.ttl = IPDEFTTL;
ip_pkt.proto = IPPROTO_ICMP;
ip_pkt.checksum = 0;
ip_pkt.src_ip = my_ip->s_addr;
ip_pkt.dst_ip = other->s_addr;
- ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
-
- memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
+ ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
+ sizeof (struct ip_header)));
+ memcpy (&packet[off],
+ &ip_pkt,
+ sizeof (struct ip_header));
off += sizeof (ip_pkt);
- memset (&dst, 0, sizeof (dst));
- dst.sin_family = AF_INET;
- dst.sin_addr = *other;
/* icmp reply: time exceeded */
- icmp_pkt = (struct icmp_packet*) &packet[off];
- memset(icmp_pkt, 0, sizeof(struct icmp_packet));
- icmp_pkt->type = ICMP_TIME_EXCEEDED;
- icmp_pkt->code = 0;
- icmp_pkt->reserved = 0;
- icmp_pkt->checksum = 0;
-
- off += sizeof (struct icmp_packet);
+ icmp_ttl.type = ICMP_TIME_EXCEEDED;
+ icmp_ttl.code = 0;
+ icmp_ttl.reserved = 0;
+ icmp_ttl.checksum = 0;
+ memcpy (&packet[off],
+ &icmp_ttl,
+ sizeof (struct icmp_ttl_exceeded_header));
+ off += sizeof (struct icmp_ttl_exceeded_header);
/* ip header of the presumably 'lost' udp packet */
ip_pkt.vers_ihl = 0x45;
ip_pkt.tos = 0;
- ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
- ip_pkt.id = 1;
+ ip_pkt.pkt_len = (sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
+ ip_pkt.id = htons (256);
ip_pkt.flags_frag_offset = 0;
ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
ip_pkt.proto = IPPROTO_ICMP;
ip_pkt.src_ip = other->s_addr;
ip_pkt.dst_ip = dummy.s_addr;
ip_pkt.checksum = 0;
- ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
- memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
- off += sizeof (struct ip_packet);
+ ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
+ sizeof (struct ip_header)));
+ memcpy (&packet[off],
+ &ip_pkt,
+ sizeof (struct ip_header));
+ off += sizeof (struct ip_header);
icmp_echo.type = ICMP_ECHO;
icmp_echo.code = 0;
icmp_echo.checksum = 0;
icmp_echo.data = htons(port);
icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
- sizeof (struct icmp_echo_packet)));
+ sizeof (struct icmp_echo_header)));
+ memcpy (&packet[off],
+ &icmp_echo,
+ sizeof(struct icmp_echo_header));
- memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet));
- off += sizeof (struct icmp_echo_packet);
+ /* no go back to calculate ICMP packet checksum */
+ off = sizeof (struct ip_header);
+ icmp_ttl.checksum = htons(calc_checksum((uint16_t*) &packet[off],
+ sizeof (struct icmp_ttl_exceeded_header) +
+ sizeof (struct ip_header) +
+ sizeof (struct icmp_echo_header)));
+ memcpy (&packet[off],
+ &icmp_ttl,
+ sizeof (struct icmp_ttl_exceeded_header));
- icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
- sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
+ memset (&dst, 0, sizeof (dst));
+ dst.sin_family = AF_INET;
+ dst.sin_addr = *other;
err = sendto(rawsock,
- &packet[0],
- off, 0,
+ packet,
+ sizeof (packet), 0,
(struct sockaddr*)&dst,
- sizeof(dst)); /* or sizeof 'struct sockaddr'? */
+ sizeof(dst));
if (err < 0)
{
fprintf(stderr,
"sendto failed: %s\n", strerror(errno));
}
- else if (err != off)
+ else if (sizeof (packet) != (size_t) err)
{
fprintf(stderr,
"Error: partial send of ICMP message\n");
strerror (errno));
return INVALID_SOCKET;
}
- if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen) != 0)
+ if (0 != setsockopt(ret, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen))
{
fprintf(stderr,
"Error setting SO_BROADCAST to ON: %s\n",
return INVALID_SOCKET;
}
- if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL, (char*)&bOptVal, bOptLen) != 0)
+ if (0 != setsockopt(ret, IPPROTO_IP, IP_HDRINCL, (char*)&bOptVal, bOptLen))
{
fprintf(stderr,
"Error setting IP_HDRINCL to ON: %s\n",
return 1;
}
if ( (1 != sscanf (argv[3], "%u", &p) ) ||
- (p == 0) ||
- (p > 0xFFFF) )
+ (0 == p) ||
+ (0xFFFF < p) )
{
fprintf (stderr,
"Error parsing port value `%s'\n",
}
port = (uint16_t) p;
- if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
+ if (0 != WSAStartup (MAKEWORD (2, 1), &wsaData))
{
fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
return 2;
off = 0;
ip_pkt.vers_ihl = 0x45;
ip_pkt.tos = 0;
- ip_pkt.pkt_len = htons(sizeof (packet));
+ ip_pkt.pkt_len = htons (sizeof (packet));
ip_pkt.id = htons(256);
ip_pkt.flags_frag_offset = 0;
ip_pkt.ttl = 128;
ip_pkt.vers_ihl = 0x45;
ip_pkt.tos = 0;
ip_pkt.pkt_len = htons(sizeof (struct ip_header) +
- sizeof(struct udp_header));
+ sizeof (struct udp_header));
ip_pkt.id = htons(0);
ip_pkt.flags_frag_offset = 0;
ip_pkt.ttl = 128;
memcpy (&packet[sizeof(struct ip_header)],
&icmp_pkt,
sizeof (struct icmp_ttl_exceeded_header));
+
memset (&dst, 0, sizeof (dst));
dst.sin_family = AF_INET;
#if HAVE_SOCKADDR_IN_SIN_LEN
dst.sin_addr = *other;
err = sendto(rawsock,
packet,
- off, 0,
+ sizeof (packet), 0,
(struct sockaddr*)&dst,
sizeof(dst));
if (err < 0)
fprintf(stderr,
"sendto failed: %s\n", strerror(errno));
}
- else if (off != (size_t) err)
+ else if (sizeof (packet) != (size_t) err)
{
fprintf(stderr,
"Error: partial send of ICMP message\n");
off = 0;
ip_pkt.vers_ihl = 0x45;
ip_pkt.tos = 0;
- ip_pkt.pkt_len = sizeof (packet);
- ip_pkt.id = 1;
+ ip_pkt.pkt_len = htons (sizeof (packet));
+ ip_pkt.id = htons (256);
ip_pkt.flags_frag_offset = 0;
ip_pkt.ttl = IPDEFTTL;
ip_pkt.proto = IPPROTO_ICMP;
/* ip header of the presumably 'lost' udp packet */
ip_pkt.vers_ihl = 0x45;
ip_pkt.tos = 0;
- ip_pkt.pkt_len = (sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
- ip_pkt.id = 1;
+ ip_pkt.pkt_len = htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
+ ip_pkt.id = htons (256);
ip_pkt.flags_frag_offset = 0;
ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
ip_pkt.proto = IPPROTO_ICMP;
off = sizeof (struct ip_header);
icmp_ttl.checksum = htons(calc_checksum((uint16_t*) &packet[off],
sizeof (struct icmp_ttl_exceeded_header) +
- sizeof(struct ip_header) +
- sizeof(struct icmp_echo_header)));
+ sizeof (struct ip_header) +
+ sizeof (struct icmp_echo_header)));
memcpy (&packet[off],
&icmp_ttl,
sizeof (struct icmp_ttl_exceeded_header));
packet,
sizeof (packet), 0,
(struct sockaddr*)&dst,
- sizeof(struct sockaddr_in));
+ sizeof(dst));
if (err < 0)
{
fprintf(stderr,
strerror (errno));
return -1;
}
- if (-1 == setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
- (char *)&one, sizeof(one)))
+ if (0 != setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
+ (char *)&one, sizeof(one)))
{
fprintf(stderr,
"setsockopt failed: %s\n",
close (ret);
return -1;
}
- if (-1 == setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
- (char *)&one, sizeof(one)))
+ if (0 != setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
+ (char *)&one, sizeof(one)))
{
fprintf(stderr,
"setsockopt failed: %s\n",
#define ICMP_ECHO 8
-#define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
+#define ICMP_TIME_EXCEEDED 11
/**
* How often do we send our ICMP messages to receive replies?
/**
* IPv4 header.
*/
-struct ip_packet
+struct ip_header
{
/**
/**
* Format of ICMP packet.
*/
-struct icmp_packet
+struct icmp_ttl_exceeded_header
+{
+ uint8_t type;
+
+ uint8_t code;
+
+ uint16_t checksum;
+
+ uint32_t unused;
+
+ /* followed by original payload */
+};
+
+struct icmp_echo_header
{
uint8_t type;
/**
* Beginning of UDP packet.
*/
-struct udp_packet
+struct udp_header
{
uint16_t src_port;
uint16_t dst_port;
- uint32_t length;
+ uint16_t length;
+
+ uint16_t crc;
};
/**
static void
send_icmp_echo (const struct in_addr *my_ip)
{
- struct icmp_packet icmp_echo;
+ char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)];
+ struct icmp_ttl_exceeded_header icmp_echo;
+ struct ip_header ip_pkt;
struct sockaddr_in dst;
size_t off;
int err;
- struct ip_packet ip_pkt;
- struct icmp_packet icmp_pkt;
- char packet[sizeof (ip_pkt) + sizeof (icmp_pkt)];
off = 0;
- memset(&ip_pkt, 0, sizeof(ip_pkt));
ip_pkt.vers_ihl = 0x45;
ip_pkt.tos = 0;
- ip_pkt.pkt_len = sizeof (packet);
+ ip_pkt.pkt_len = htons (sizeof (packet));
ip_pkt.id = htons (256);
ip_pkt.flags_frag_offset = 0;
ip_pkt.ttl = IPDEFTTL;
ip_pkt.checksum = 0;
ip_pkt.src_ip = my_ip->s_addr;
ip_pkt.dst_ip = dummy.s_addr;
- ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
- memcpy (&packet[off], &ip_pkt, sizeof (ip_pkt));
- off += sizeof (ip_pkt);
+ ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
+ sizeof (struct ip_header)));
+ memcpy (&packet[off],
+ &ip_pkt,
+ sizeof (struct ip_header));
+ off += sizeof (struct ip_header);
icmp_echo.type = ICMP_ECHO;
icmp_echo.code = 0;
icmp_echo.reserved = 0;
icmp_echo.checksum = 0;
icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
- sizeof (struct icmp_packet)));
- memcpy (&packet[off], &icmp_echo, sizeof (icmp_echo));
- off += sizeof (icmp_echo);
+ sizeof (struct icmp_echo_header)));
+ memcpy (&packet[off],
+ &icmp_echo,
+ sizeof (struct icmp_echo_header));
+ off += sizeof (struct icmp_echo_header);
memset (&dst, 0, sizeof (dst));
dst.sin_family = AF_INET;
/**
* Send a UDP message to the dummy IP.
- *
- * @param my_ip source address (our ip address)
*/
static void
-send_udp (const struct in_addr *my_ip)
+send_udp ()
{
struct sockaddr_in dst;
ssize_t err;
"sendto failed: %s\n", strerror(errno));
#endif
}
- else if (err != 0)
+ else if (0 != err)
{
fprintf(stderr,
"Error: partial send of ICMP message\n");
{
char buf[65536];
ssize_t have;
- struct in_addr sip;
- struct ip_packet ip_pkt;
- struct icmp_packet icmp_pkt;
- struct udp_packet udp_pkt;
+ struct in_addr source_ip;
+ struct ip_header ip_pkt;
+ struct icmp_ttl_exceeded_header icmp_pkt;
+ struct icmp_echo_header icmp_echo;
+ struct udp_header udp_pkt;
size_t off;
- int have_port;
- uint32_t port;
+ uint16_t port;
+ DWORD ssize;
have = read (icmpsock, buf, sizeof (buf));
if (have == -1)
"Received message of %u bytes\n",
(unsigned int) have);
#endif
- if (have == sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2 + sizeof(uint32_t))
+ if (have < (ssize_t) (sizeof (struct ip_header) + sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct ip_header)))
{
- have_port = 1;
- }
- else if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2)
- {
-#if VERBOSE
- fprintf (stderr,
- "Received ICMP message of unexpected size: %u bytes\n",
- (unsigned int) have);
-#endif
+ /* malformed */
return;
}
off = 0;
- memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
- off += sizeof (ip_pkt);
- memcpy (&icmp_pkt, &buf[off], sizeof (icmp_pkt));
- off += sizeof (icmp_pkt);
- if ( ((ip_pkt.proto != IPPROTO_ICMP) && (ip_pkt.proto != IPPROTO_UDP)) ||
- (icmp_pkt.type != ICMP_TIME_EXCEEDED) ||
- (icmp_pkt.code != 0) )
- {
- /* maybe we got an actual reply back... */
- return;
- }
- memcpy(&sip,
+ memcpy (&ip_pkt,
+ &buf[off],
+ sizeof (struct ip_header));
+ off += sizeof (struct ip_header);
+ memcpy(&source_ip,
&ip_pkt.src_ip,
- sizeof (sip));
- memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
- off += sizeof (ip_pkt);
-
- if (have_port)
+ sizeof (source_ip));
+ memcpy (&icmp_ttl,
+ &buf[off],
+ sizeof (struct icmp_ttl_exceeded_header));
+ off += sizeof (struct icmp_ttl_exceeded_header);
+ if ( (ICMP_TIME_EXCEEDED != icmp_ttl.type) ||
+ (0 != icmp_ttl.code) )
{
- memcpy(&port,
- &buf[sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2],
- sizeof(uint32_t));
- port = ntohs(port);
- DWORD ssize = sizeof(buf);
- WSAAddressToString((LPSOCKADDR)&sip,
- sizeof(sip),
- NULL,
- buf,
- &ssize);
- fprintf (stdout,
- "%s:%d\n",
- buf,
- port);
+ /* different type than what we want */
+ return;
}
- else if (ip_pkt.proto == IPPROTO_UDP)
+ /* skip 2nd IP header */
+ memcpy (&ip_pkt,
+ &buf[off],
+ sizeof (struct ip_header));
+ off += sizeof (struct ip_header);
+
+ switch (ip_pkt.proto)
{
- memcpy(&udp_pkt,
- &buf[off],
- sizeof(udp_pkt));
- DWORD ssize = sizeof(buf);
- WSAAddressToString((LPSOCKADDR)&sip,
- sizeof(sip),
- NULL,
- buf,
- &ssize);
- fprintf (stdout,
- "%s:%d\n",
- buf,
- ntohs((uint16_t)udp_pkt.length));
+ case IPPROTO_ICMP:
+ if (have != (sizeof (struct ip_header) * 2 +
+ sizeof (struct icmp_ttl_exceeded_header) +
+ sizeof (struct icmp_echo_header)) )
+ {
+ /* malformed */
+ return;
+ }
+ /* grab ICMP ECHO content */
+ memcpy (&icmp_echo,
+ &buf[off],
+ sizeof (struct icmp_echo_header));
+ port = (uint16_t) ntohl (icmp_echo.reserved);
+ break;
+ case IPPROTO_UDP:
+ if (have != (sizeof (struct ip_header) * 2 +
+ sizeof (struct icmp_ttl_exceeded_header) +
+ sizeof (struct udp_header)) )
+ {
+ /* malformed */
+ return;
+ }
+ /* grab UDP content */
+ memcpy (&udp_pkt,
+ &buf[off],
+ sizeof (struct udp_header));
+ port = ntohs (udp_pkt.length);
+ break;
+ default:
+ /* different type than what we want */
+ return;
}
+
+ ssize = sizeof(buf);
+ WSAAddressToString((LPSOCKADDR)&source_ip,
+ sizeof(source_ip),
+ NULL,
+ buf,
+ &ssize);
+ if (port == 0)
+ fprintf (stdout,
+ "%s\n",
+ buf);
else
- {
- DWORD ssize = sizeof(buf);
- WSAAddressToString((LPSOCKADDR)&sip,
- sizeof(sip),
- NULL,
- buf,
- &ssize);
- fprintf (stdout,
- "%s\n",
- buf);
- }
+ fprintf (stdout,
+ "%s:%u\n",
+ buf,
+ (unsigned int) port);
fflush (stdout);
}
return INVALID_SOCKET;
}
- if (setsockopt(rawsock,
- SOL_SOCKET,
- SO_BROADCAST,
- (char*)&bOptVal, bOptLen) != 0)
+ if (0 != setsockopt(rawsock,
+ SOL_SOCKET,
+ SO_BROADCAST,
+ (char*)&bOptVal, bOptLen))
{
fprintf(stderr,
"Error setting SO_BROADCAST to ON: %s\n",
closesocket(rawsock);
return INVALID_SOCKET;
}
- if (setsockopt(rawsock,
- IPPROTO_IP,
- IP_HDRINCL,
- (char*)&bOptVal, bOptLen) != 0)
+ if (0 != setsockopt(rawsock,
+ IPPROTO_IP,
+ IP_HDRINCL,
+ (char*)&bOptVal, bOptLen))
{
fprintf(stderr,
"Error setting IP_HDRINCL to ON: %s\n",
/**
* Create a UDP socket for writinging.
*
- * @return -1 on error
+ * @param my_ip source address (our ip address)
+ * @return INVALID_SOCKET on error
*/
static SOCKET
-make_udp_socket ()
+make_udp_socket (const struct in_addr *my_ip)
{
SOCKET ret;
struct sockaddr_in addr;
}
memset (&addr, 0, sizeof (addr));
addr.sin_family = AF_INET;
- /* addr.sin_addr zero == ours (hopefully...) */
+ addr.sin_addr = *my_ip;
addr.sin_port = htons (NAT_TRAV_PORT);
-
if (0 != bind (ret,
&addr,
sizeof(addr)))
WSADATA wsaData;
unsigned int alt;
- if (argc != 2)
+ if (2 != argc)
{
fprintf (stderr,
"This program must be started with our (internal NAT) IP as the only argument.\n");
closesocket (icmpsock);
return 3;
}
- if (INVALID_SOCKET == (udpsock = make_udp_socket()))
+ if (INVALID_SOCKET == (udpsock = make_udp_socket(&external)))
{
closesocket (icmpsock);
closesocket (rawsock);
if (0 == (++alt % 2))
send_icmp_echo (&external);
else
- send_udp (&external);
+ send_udp ();
}
/* select failed (internal error or OS out of resources) */
closesocket(icmpsock);
off = 0;
ip_pkt.vers_ihl = 0x45;
ip_pkt.tos = 0;
- ip_pkt.pkt_len = sizeof (packet);
- ip_pkt.id = 1;
+ ip_pkt.pkt_len = htons (sizeof (packet));
+ ip_pkt.id = htons (256);
ip_pkt.flags_frag_offset = 0;
ip_pkt.ttl = IPDEFTTL;
ip_pkt.proto = IPPROTO_ICMP;
err = sendto(rawsock,
packet, off, 0,
(struct sockaddr*)&dst,
- sizeof(struct sockaddr_in));
+ sizeof(dst));
if (err < 0)
{
#if VERBOSE
/**
* Send a UDP message to the dummy IP.
- *
- * @param my_ip source address (our ip address)
*/
static void
send_udp ()
struct icmp_echo_header icmp_echo;
struct udp_header udp_pkt;
size_t off;
- uint32_t port;
+ uint16_t port;
have = read (icmpsock, buf, sizeof (buf));
if (-1 == have)
sizeof (buf)));
else
fprintf (stdout,
- "%s:%d\n",
+ "%s:%u\n",
inet_ntop (AF_INET,
&source_ip,
buf,
sizeof (buf)),
- port);
+ (unsigned int) port);
fflush (stdout);
}
/**
* Create a UDP socket for writinging.
*
+ * @param my_ip source address (our ip address)
* @return -1 on error
*/
static int