*/
#define VERBOSE 0
-
-typedef unsigned int uid_t;
-typedef SOCKET Socket;
-typedef unsigned short ushort;
-#define IPDEFTTL 64
-#define ICMP_ECHO 8
-#define ICMP_TIME_EXCEEDED 11 /* Time Exceeded
-
/**
* Must match IP given in the client.
*/
#define DUMMY_IP "192.0.2.86"
+/**
+ * TTL to use for our outgoing messages.
+ */
+#define IPDEFTTL 64
+
+#define ICMP_ECHO 8
+
+#define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
+
/**
* How often do we send our ICMP messages to receive replies?
*/
/**
* Version (4 bits) + Internet header length (4 bits)
*/
- u_char vers_ihl;
+ uint8_t vers_ihl;
/**
* Type of service
*/
- u_char tos;
+ uint8_t tos;
/**
* Total length
*/
- u_short pkt_len;
+ uint16_t pkt_len;
/**
* Identification
*/
- u_short id;
+ uint16_t id;
/**
* Flags (3 bits) + Fragment offset (13 bits)
*/
- u_short flags_frag_offset;
+ uint16_t flags_frag_offset;
/**
* Time to live
*/
- u_char ttl;
+ uint8_t ttl;
/**
* Protocol
*/
- u_char proto;
+ uint8_t proto;
/**
* Header checksum
*/
- u_short checksum;
+ uint16_t checksum;
/**
* Source address
*/
- u_long src_ip;
+ uint32_t src_ip;
/**
* Destination address
*/
- u_long dst_ip;
+ uint32_t dst_ip;
};
/**
struct icmp_packet
{
uint8_t type;
+
uint8_t code;
+
uint16_t checksum;
+
uint32_t reserved;
};
uint32_t length;
};
-static Socket icmpsock;
+/**
+ * Socket we use to receive "fake" ICMP replies.
+ */
+static SOCKET icmpsock;
-static Socket rawsock;
+/**
+ * Socket we use to send our ICMP requests.
+ */
+static SOCKET rawsock;
+/**
+ * Target "dummy" address.
+ */
static struct in_addr dummy;
+
/**
* CRC-16 for IP/ICMP headers.
+ *
+ * @param data what to calculate the CRC over
+ * @param bytes number of bytes in data (must be multiple of 2)
+ * @return the CRC 16.
*/
static uint16_t
calc_checksum(const uint16_t *data,
return sum;
}
+
/**
* Convert IPv4 address from text to binary form.
*
* @param af address family
* @param cp the address to print
* @param buf where to write the address result
+ * @return 1 on success
*/
static int
inet_pton (int af,
}
+/**
+ * We've received an ICMP response. Process it.
+ */
static void
process_icmp_response ()
{
struct udp_packet udp_pkt;
size_t off;
int have_port;
- int have_udp;
uint32_t port;
have = read (icmpsock, buf, sizeof (buf));
sizeof (sip));
memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
off += sizeof (ip_pkt);
- have_udp = (ip_pkt.proto == IPPROTO_UDP);
if (have_port)
{
buf,
port);
}
- else if (have_udp)
+ else if (ip_pkt.proto == IPPROTO_UDP)
{
memcpy(&udp_pkt,
&buf[off],
fprintf (stdout,
"%s:%d\n",
buf,
- ntohs((int)udp_pkt.length));
+ ntohs((uint16_t)udp_pkt.length));
}
else
{
}
-static Socket
+/**
+ * Create an ICMP raw socket for reading.
+ *
+ * @return INVALID_SOCKET on error
+ */
+static SOCKET
make_icmp_socket ()
{
- Socket ret;
+ SOCKET ret;
ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (INVALID_SOCKET == ret)
fprintf (stderr,
"Error opening RAW socket: %s\n",
strerror (errno));
- return -1;
+ return INVALID_SOCKET;
}
return ret;
}
-static Socket
+/**
+ * Create an ICMP raw socket for writing.
+ *
+ * @return INVALID_SOCKET on error
+ */
+static SOCKET
make_raw_socket ()
{
DWORD bOptVal = TRUE;
fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
return 2;
}
- if (-1 == (icmpsock = make_icmp_socket()))
+ if (INVALID_SOCKET == (icmpsock = make_icmp_socket()))
{
return 3;
}
- if (-1 == (make_raw_socket()))
+ if (INVALID_SOCKET == (make_raw_socket()))
{
closesocket (icmpsock);
return 3;
FD_SET (icmpsock, &rs);
tv.tv_sec = 0;
tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
- if (0 != select (icmpsock + 1, &rs, NULL, NULL, &tv))
+ if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
{
if (errno == EINTR)
continue;
process_icmp_response ();
send_icmp_echo (&external);
}
+ /* select failed (internal error or OS out of resources) */
closesocket(icmpsock);
closesocket(rawsock);
WSACleanup ();
- return 4; /* select failed! */
+ return 4;
}