+/*
+ * If source-routing is present, return the final destination.
+ * Otherwise, return IP destination.
+ *
+ * This is used for UDP and TCP pseudo-header in the checksum
+ * calculation.
+ */
+u_int32_t
+ip_finddst(const struct ip *ip)
+{
+ int length;
+ int len;
+ const u_char *cp;
+ u_int32_t retval;
+
+ cp = (const u_char *)(ip + 1);
+ length = (IP_HL(ip) << 2) - sizeof(struct ip);
+
+ for (; length > 0; cp += len, length -= len) {
+ int tt = *cp;
+
+ if (tt == IPOPT_NOP || tt == IPOPT_EOL)
+ len = 1;
+ else {
+ if (&cp[1] >= snapend) {
+ return 0;
+ }
+ len = cp[1];
+ }
+ if (len <= 0) {
+ return 0;
+ }
+ if (&cp[1] >= snapend || cp + len > snapend) {
+ return 0;
+ }
+ switch (tt) {
+
+ case IPOPT_SSRR:
+ case IPOPT_LSRR:
+ memcpy(&retval, cp + len - 4, 4);
+ return retval;
+ }
+ }
+ return ip->ip_dst.s_addr;
+}
+
+static void
+ip_printts(register const u_char *cp, u_int length)
+{
+ register u_int ptr = cp[2] - 1;
+ register u_int len = 0;
+ int hoplen;
+ const char *type;
+
+ printf(" TS{");
+ hoplen = ((cp[3]&0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
+ if ((length - 4) & (hoplen-1))
+ printf("[bad length %d]", length);
+ if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
+ printf("[bad ptr %d]", cp[2]);
+ switch (cp[3]&0xF) {
+ case IPOPT_TS_TSONLY:
+ printf("TSONLY");
+ break;
+ case IPOPT_TS_TSANDADDR:
+ printf("TS+ADDR");
+ break;
+ /*
+ * prespecified should really be 3, but some ones might send 2
+ * instead, and the IPOPT_TS_PRESPEC constant can apparently
+ * have both values, so we have to hard-code it here.
+ */
+
+ case 2:
+ printf("PRESPEC2.0");
+ break;
+ case 3: /* IPOPT_TS_PRESPEC */
+ printf("PRESPEC");
+ break;
+ default:
+ printf("[bad ts type %d]", cp[3]&0xF);
+ goto done;
+ }
+
+ type = " ";
+ for (len = 4; len < length; len += hoplen) {
+ if (ptr == len)
+ type = " ^ ";
+ printf("%s%d@%s", type, EXTRACT_32BITS(&cp[len+hoplen-4]),
+ hoplen!=8 ? "" : ipaddr_string(&cp[len]));
+ type = " ";
+ }
+
+done:
+ printf("%s", ptr == len ? " ^ " : "");
+
+ if (cp[3]>>4)
+ printf(" [%d hops not recorded]} ", cp[3]>>4);
+ else
+ printf("}");
+}
+