]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-icmp.c
Fix a typo.
[tcpdump] / print-icmp.c
index 5e50f3accd19ab82d59a27f5d402e481d239f456..f7b1b2b904b0b2381a4330ec46fec3bb5f968669 100644 (file)
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-icmp.c,v 1.40 1999-10-30 05:11:13 itojun Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/print-icmp.c,v 1.48 2000-07-01 03:39:03 assar Exp $ (LBL)";
+#endif
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
 #endif
 
 #include <sys/param.h>
 #include <sys/time.h>
 #include <sys/socket.h>
 
-#if __STDC__
 struct mbuf;
 struct rtentry;
-#endif
 #include <net/if.h>
 
 #include <netinet/in.h>
@@ -162,7 +164,7 @@ struct id_rdiscovery {
 };
 
 void
-icmp_print(register const u_char *bp, register const u_char *bp2)
+icmp_print(register const u_char *bp, u_int plen, register const u_char *bp2)
 {
        register char *cp;
        register const struct icmp *dp;
@@ -192,9 +194,10 @@ icmp_print(register const u_char *bp, register const u_char *bp2)
 
                case ICMP_UNREACH_PROTOCOL:
                        TCHECK(dp->icmp_ip.ip_p);
-                       (void)sprintf(buf, "%s protocol %d unreachable",
-                                      ipaddr_string(&dp->icmp_ip.ip_dst),
-                                      dp->icmp_ip.ip_p);
+                       (void)snprintf(buf, sizeof(buf),
+                           "%s protocol %d unreachable",
+                           ipaddr_string(&dp->icmp_ip.ip_dst),
+                           dp->icmp_ip.ip_p);
                        break;
 
                case ICMP_UNREACH_PORT:
@@ -206,21 +209,21 @@ icmp_print(register const u_char *bp, register const u_char *bp2)
                        switch (oip->ip_p) {
 
                        case IPPROTO_TCP:
-                               (void)sprintf(buf,
+                               (void)snprintf(buf, sizeof(buf),
                                        "%s tcp port %s unreachable",
                                        ipaddr_string(&oip->ip_dst),
                                        tcpport_string(dport));
                                break;
 
                        case IPPROTO_UDP:
-                               (void)sprintf(buf,
+                               (void)snprintf(buf, sizeof(buf),
                                        "%s udp port %s unreachable",
                                        ipaddr_string(&oip->ip_dst),
                                        udpport_string(dport));
                                break;
 
                        default:
-                               (void)sprintf(buf,
+                               (void)snprintf(buf, sizeof(buf),
                                        "%s protocol %d port %d unreachable",
                                        ipaddr_string(&oip->ip_dst),
                                        oip->ip_p, dport);
@@ -229,26 +232,26 @@ icmp_print(register const u_char *bp, register const u_char *bp2)
                        break;
 
                case ICMP_UNREACH_NEEDFRAG:
-                       {
+                   {
                        register const struct mtu_discovery *mp;
-
                        mp = (struct mtu_discovery *)&dp->icmp_void;
                         mtu = EXTRACT_16BITS(&mp->nexthopmtu);
-                        if (mtu)
-                           (void)sprintf(buf,
-                               "%s unreachable - need to frag (mtu %d)",
-                               ipaddr_string(&dp->icmp_ip.ip_dst), mtu);
-                        else
-                           (void)sprintf(buf,
-                               "%s unreachable - need to frag",
-                               ipaddr_string(&dp->icmp_ip.ip_dst));
+                        if (mtu) {
+                               (void)snprintf(buf, sizeof(buf),
+                                   "%s unreachable - need to frag (mtu %d)",
+                                   ipaddr_string(&dp->icmp_ip.ip_dst), mtu);
+                        } else {
+                               (void)snprintf(buf, sizeof(buf),
+                                   "%s unreachable - need to frag",
+                                   ipaddr_string(&dp->icmp_ip.ip_dst));
                        }
+                   }
                        break;
 
                default:
                        fmt = tok2str(unreach2str, "#%d %%s unreachable",
                            dp->icmp_code);
-                       (void)sprintf(buf, fmt,
+                       (void)snprintf(buf, sizeof(buf), fmt,
                            ipaddr_string(&dp->icmp_ip.ip_dst));
                        break;
                }
@@ -258,55 +261,60 @@ icmp_print(register const u_char *bp, register const u_char *bp2)
                TCHECK(dp->icmp_ip.ip_dst);
                fmt = tok2str(type2str, "redirect-#%d %%s to net %%s",
                    dp->icmp_code);
-               (void)sprintf(buf, fmt,
+               (void)snprintf(buf, sizeof(buf), fmt,
                    ipaddr_string(&dp->icmp_ip.ip_dst),
                    ipaddr_string(&dp->icmp_gwaddr));
                break;
 
        case ICMP_ROUTERADVERT:
-               {
+           {
                register const struct ih_rdiscovery *ihp;
                register const struct id_rdiscovery *idp;
                u_int lifetime, num, size;
 
-               (void)strcpy(buf, "router advertisement");
+               (void)snprintf(buf, sizeof(buf), "router advertisement");
                cp = buf + strlen(buf);
 
                ihp = (struct ih_rdiscovery *)&dp->icmp_void;
                TCHECK(*ihp);
-               (void)strcpy(cp, " lifetime ");
+               (void)strncpy(cp, " lifetime ", sizeof(buf) - (cp - buf));
                cp = buf + strlen(buf);
                lifetime = EXTRACT_16BITS(&ihp->ird_lifetime);
-               if (lifetime < 60)
-                       (void)sprintf(cp, "%u", lifetime);
-               else if (lifetime < 60 * 60)
-                       (void)sprintf(cp, "%u:%02u",
+               if (lifetime < 60) {
+                       (void)snprintf(cp, sizeof(buf) - (cp - buf), "%u",
+                           lifetime);
+               } else if (lifetime < 60 * 60) {
+                       (void)snprintf(cp, sizeof(buf) - (cp - buf), "%u:%02u",
                            lifetime / 60, lifetime % 60);
-               else
-                       (void)sprintf(cp, "%u:%02u:%02u",
+               } else {
+                       (void)snprintf(cp, sizeof(buf) - (cp - buf),
+                           "%u:%02u:%02u",
                            lifetime / 3600,
                            (lifetime % 3600) / 60,
                            lifetime % 60);
+               }
                cp = buf + strlen(buf);
 
                num = ihp->ird_addrnum;
-               (void)sprintf(cp, " %d:", num);
+               (void)snprintf(cp, sizeof(buf) - (cp - buf), " %d:", num);
                cp = buf + strlen(buf);
 
                size = ihp->ird_addrsiz;
                if (size != 2) {
-                       (void)sprintf(cp, " [size %d]", size);
+                       (void)snprintf(cp, sizeof(buf) - (cp - buf),
+                           " [size %d]", size);
                        break;
                }
                idp = (struct id_rdiscovery *)&dp->icmp_data;
                while (num-- > 0) {
                        TCHECK(*idp);
-                       (void)sprintf(cp, " {%s %u}",
+                       (void)snprintf(cp, sizeof(buf) - (cp - buf), " {%s %u}",
                            ipaddr_string(&idp->ird_addr),
                            EXTRACT_32BITS(&idp->ird_pref));
                        cp = buf + strlen(buf);
+                       ++idp;
                }
-               }
+           }
                break;
 
        case ICMP_TIMXCEED:
@@ -322,26 +330,44 @@ icmp_print(register const u_char *bp, register const u_char *bp2)
                        break;
 
                default:
-                       (void)sprintf(buf, "time exceeded-#%d", dp->icmp_code);
+                       (void)snprintf(buf, sizeof(buf), "time exceeded-#%d",
+                           dp->icmp_code);
                        break;
                }
                break;
 
        case ICMP_PARAMPROB:
                if (dp->icmp_code)
-                       (void)sprintf(buf, "parameter problem - code %d",
-                                       dp->icmp_code);
+                       (void)snprintf(buf, sizeof(buf),
+                           "parameter problem - code %d", dp->icmp_code);
                else {
                        TCHECK(dp->icmp_pptr);
-                       (void)sprintf(buf, "parameter problem - octet %d",
-                                       dp->icmp_pptr);
+                       (void)snprintf(buf, sizeof(buf),
+                           "parameter problem - octet %d", dp->icmp_pptr);
                }
                break;
 
        case ICMP_MASKREPLY:
                TCHECK(dp->icmp_mask);
-               (void)sprintf(buf, "address mask is 0x%08x",
-                   (u_int32_t)ntohl(dp->icmp_mask));
+               (void)snprintf(buf, sizeof(buf), "address mask is 0x%08x",
+                   (unsigned)ntohl(dp->icmp_mask));
+               break;
+
+       case ICMP_TSTAMP:
+               TCHECK(dp->icmp_seq);
+               (void)sprintf(buf, "time stamp query id %u seq %u",
+                   (unsigned)ntohs(dp->icmp_id),
+                   (unsigned)ntohs(dp->icmp_seq));
+               break;
+
+       case ICMP_TSTAMPREPLY:
+               TCHECK(dp->icmp_ttime);
+               (void)sprintf(buf, "time stamp reply id %u seq %u : org 0x%lx recv 0x%lx xmit 0x%lx",
+                   (unsigned)ntohs(dp->icmp_id),
+                   (unsigned)ntohs(dp->icmp_seq),
+                   (unsigned long)ntohl(dp->icmp_otime),
+                   (unsigned long)ntohl(dp->icmp_rtime),
+                   (unsigned long)ntohl(dp->icmp_ttime));
                break;
 
        default:
@@ -349,6 +375,19 @@ icmp_print(register const u_char *bp, register const u_char *bp2)
                break;
        }
         (void)printf("icmp: %s", str);
+       if (vflag) {
+               if (TTEST2(*bp, plen)) {
+                       if (in_cksum((u_short*)dp, plen, 0))
+                               printf(" (wrong icmp csum)");
+               }
+       }
+       if (vflag > 1 && !ICMP_INFOTYPE(dp->icmp_type)) {
+               bp += 8;
+               (void)printf(" for ");
+               ip = (struct ip *)bp;
+               snaplen = snapend - bp;
+               ip_print(bp, ntohs(ip->ip_len));
+       }
        return;
 trunc:
        fputs("[|icmp]", stdout);