#P10347. ICMP Echo Reply Generator

    ID: 12351 Type: Default 1000ms 256MiB

ICMP Echo Reply Generator

ICMP Echo Reply Generator

Given a traffic snippet of network frames, your task is to respond to valid ICMP Echo Request messages. For each valid ICMP Echo Request that meets the following criteria:

  • The destination IP and MAC addresses match your own.
  • The source and destination IP addresses lie in the same subnet (assume a /24 subnet, i.e. the first three octets are identical).

You must construct a correctly formatted ICMP Echo Reply frame. The reply should have:

  • The timestamp updated according to the rule: if the request timestamp is \( (\sec_0, \mathrm{us}_0) \), then the reply timestamp \( (\sec, \mathrm{us}) \) must satisfy \[ \sec \times 10^6 + \mathrm{us} = \sec_0 \times 10^6 + \mathrm{us}_0 + 1, \] with \(0 \le \mathrm{us} < 10^6\).
  • Ethernet and IP addresses swapped appropriately (use your addresses as the source in the reply).
  • If the request carries a payload, the reply must echo the payload exactly.

Ignore packets that are not valid ICMP Echo Requests (for example, ARP or requests for which the source and destination IPs are not in the same subnet).

inputFormat

The input is structured as follows:

  1. The first line contains your local IP address and MAC address (separated by a space).
  2. The second line contains an integer \(N\) representing the number of frames.
  3. Each of the following \(N\) lines describes a network frame with the following space-separated fields:
sec us type src_ip dst_ip src_mac dst_mac ip_len payload

where:

  • sec and us denote the timestamp in seconds and microseconds, respectively.
  • type is a string which can be "ICMP_ECHO_REQUEST", "ICMP_ECHO_REPLY", "ARP", or "OTHER".
  • src_ip and dst_ip are the source and destination IP addresses.
  • src_mac and dst_mac are the source and destination MAC addresses.
  • ip_len is an integer representing the IP packet length in bytes.
  • payload is a string representing the data payload (a single word; "-" indicates an empty payload).

outputFormat

For each valid ICMP Echo Request that meets the criteria, output its corresponding reply frame on a separate line in the following format:

sec us type src_ip dst_ip src_mac dst_mac ip_len payload

In the reply frame:

  • The timestamp is updated such that if the request had timestamp \((\sec_0, \mathrm{us}_0)\), the reply's timestamp \((\sec, \mathrm{us})\) satisfies \(\sec \times 10^6 + \mathrm{us} = \sec_0 \times 10^6 + \mathrm{us}_0 + 1\) with \(0 \leq \mathrm{us} < 10^6\).
  • The type is changed from "ICMP_ECHO_REQUEST" to "ICMP_ECHO_REPLY".
  • The destination and source addresses are swapped appropriately (use your local addresses as the source in the reply).
  • The ip_len and payload remain unchanged from the original request.

sample

192.168.1.10 aa:bb:cc:dd:ee:ff
3
100 999999 ICMP_ECHO_REQUEST 192.168.1.20 192.168.1.10 11:22:33:44:55:66 aa:bb:cc:dd:ee:ff 64 payload1
101 100 ICMP_ECHO_REQUEST 192.168.2.20 192.168.1.10 11:22:33:44:55:67 aa:bb:cc:dd:ee:ff 64 payload2
102 500 ICMP_ECHO_REQUEST 192.168.1.30 192.168.1.10 11:22:33:44:55:68 aa:bb:cc:dd:ee:ff 64 -
101 0 ICMP_ECHO_REPLY 192.168.1.10 192.168.1.20 aa:bb:cc:dd:ee:ff 11:22:33:44:55:66 64 payload1

102 501 ICMP_ECHO_REPLY 192.168.1.10 192.168.1.30 aa:bb:cc:dd:ee:ff 11:22:33:44:55:68 64 -

</p>