exercises/ex04/trout/trout.c
int process_ip (struct ip *ip, int len)
{
  int hlen1, hlen2, icmplen;
  struct icmp *icmp;
  struct ip *hip;
  struct udphdr *udp;

  hlen1 = ip->ip_hl << 2;                        /* length of IP header */
  icmp = (struct icmp *) (recvbuf + hlen1);
  icmplen = len - hlen1;

  if (icmplen < 8 + 20 + 8) return 0;

  if (icmp->icmp_type != ICMP_TIME_EXCEEDED &&
      icmp->icmp_type != ICMP_DEST_UNREACH)
    return 0;

  /* hip is the header of the enclosed IP packets, supposedly
     the header of the packet that caused the error */

  hip = (struct ip *) (recvbuf + hlen1 + 8);
  if (hip->ip_p != IPPROTO_UDP) return 0;

  hlen2 = hip->ip_hl << 2;
  udp = (struct udphdr *) (recvbuf + hlen1 + 8 + hlen2);

  if (udp->source != htons (sport)) return 0;
  if (udp->dest != htons (dport + seq)) return 0;

  /* now we know it's an ICMP packet caused by a UDP
     datagram sent by us and sent to the port we happen to
     be sending to.  It's probably one of ours. */

  if (icmp->icmp_type == ICMP_TIME_EXCEEDED) {
    if (icmp->icmp_code == ICMP_EXC_TTL) {
      return -2;
    } else {
      return 0;
    }
  }

  if (icmp->icmp_type == ICMP_DEST_UNREACH) {
    if (icmp->icmp_code == ICMP_PORT_UNREACH) {
      return -1;
    } else {
      return 0;
    }
  }
}
/* process_ip: extracts all the info from an incoming ICMP packet

     Just for kicks, I changed the BSD-style names of the ICMP
     errors to Linux-style names, mostly so that they will be
     consistent with the changes I made in the kernel and so my
     head won't explode. */
extracts all the info from an incoming ICMP packet
Just for kicks, I changed the BSD-style names of the ICMP
errors to Linux-style names, mostly so that they will be
consistent with the changes I made in the kernel and so my
head won't explode.
extracts all the info from an incoming ICMP packet Just for kicks I changed the BSD - style names of the ICMP errors to Linux - style names mostly so that they will be consistent with the changes I made in the kernel and so my head won ' t explode .
int process_ip (struct ip *ip, int len)
{
  int hlen1, hlen2, icmplen;
  struct icmp *icmp;
  struct ip *hip;
  struct udphdr *udp;
  hlen1 = ip->ip_hl << 2;                        
  icmp = (struct icmp *) (recvbuf + hlen1);
  icmplen = len - hlen1;
  if (icmplen < 8 + 20 + 8) return 0;
  if (icmp->icmp_type != ICMP_TIME_EXCEEDED &&
      icmp->icmp_type != ICMP_DEST_UNREACH)
    return 0;
  hip = (struct ip *) (recvbuf + hlen1 + 8);
  if (hip->ip_p != IPPROTO_UDP) return 0;
  hlen2 = hip->ip_hl << 2;
  udp = (struct udphdr *) (recvbuf + hlen1 + 8 + hlen2);
  if (udp->source != htons (sport)) return 0;
  if (udp->dest != htons (dport + seq)) return 0;
  if (icmp->icmp_type == ICMP_TIME_EXCEEDED) {
    if (icmp->icmp_code == ICMP_EXC_TTL) {
      return -2;
    } else {
      return 0;
    }
  }
  if (icmp->icmp_type == ICMP_DEST_UNREACH) {
    if (icmp->icmp_code == ICMP_PORT_UNREACH) {
      return -1;
    } else {
      return 0;
    }
  }
}
int process_ip ( struct ip * ip , int len ) { int hlen1 , hlen2 , icmplen ; struct icmp * icmp ; struct ip * hip ; struct udphdr * udp ; hlen1 = ip -> ip_hl << 2 ; icmp = ( struct icmp * ) ( recvbuf + hlen1 ) ; icmplen = len - hlen1 ; if ( icmplen < 8 + 20 + 8 ) return 0 ; if ( icmp -> icmp_type != ICMP_TIME_EXCEEDED && icmp -> icmp_type != ICMP_DEST_UNREACH ) return 0 ; hip = ( struct ip * ) ( recvbuf + hlen1 + 8 ) ; if ( hip -> ip_p != IPPROTO_UDP ) return 0 ; hlen2 = hip -> ip_hl << 2 ; udp = ( struct udphdr * ) ( recvbuf + hlen1 + 8 + hlen2 ) ; if ( udp -> source != htons ( sport ) ) return 0 ; if ( udp -> dest != htons ( dport + seq ) ) return 0 ; if ( icmp -> icmp_type == ICMP_TIME_EXCEEDED ) { if ( icmp -> icmp_code == ICMP_EXC_TTL ) { return -2 ; } else { return 0 ; } } if ( icmp -> icmp_type == ICMP_DEST_UNREACH ) { if ( icmp -> icmp_code == ICMP_PORT_UNREACH ) { return -1 ; } else { return 0 ; } } }
process_ip: extracts all the info from an incoming ICMP packet
Just for kicks, I changed the BSD-style names of the ICMP
errors to Linux-style names, mostly so that they will be
consistent with the changes I made in the kernel and so my
head won't explode.
process_ip : extracts all the info from an incoming ICMP packet Just for kicks I changed the BSD - style names of the ICMP errors to Linux - style names mostly so that they will be consistent with the changes I made in the kernel and so my head won ' t explode .
/* length of IP header */ /* hip is the header of the enclosed IP packets, supposedly
     the header of the packet that caused the error */ /* now we know it's an ICMP packet caused by a UDP
     datagram sent by us and sent to the port we happen to
     be sending to.  It's probably one of ours. */