home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / tcpdumpb.zip / print-udp.c < prev    next >
C/C++ Source or Header  |  1997-02-25  |  16KB  |  422 lines

  1. /*
  2.  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
  3.  *      The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. #ifndef lint
  23. static char rcsid[] =
  24.     "@(#) $Header: print-udp.c,v 1.55 96/07/23 14:17:28 leres Exp $ (LBL)";
  25. #endif
  26.  
  27. #include <sys/param.h>
  28. #include <sys/time.h>
  29. #include <sys/socket.h>
  30.  
  31. #include <netinet/in.h>
  32. #include <netinet/in_systm.h>
  33. #include <netinet/ip.h>
  34. #ifndef __EMX__
  35. #include <netinet/ip_var.h>
  36. #endif
  37. #include <netinet/udp.h>
  38. #include <netinet/udp_var.h>
  39.  
  40. #undef NOERROR                                  /* Solaris sucks */
  41. #undef T_UNSPEC                                 /* SINIX does too */
  42. #include <arpa/nameser.h>
  43. #ifndef __EMX__
  44. #include <arpa/tftp.h>
  45. #endif
  46.  
  47. #include <rpc/rpc.h>
  48.  
  49. #include <stdio.h>
  50.  
  51. #include "interface.h"
  52. #include "addrtoname.h"
  53. #include "appletalk.h"
  54.  
  55. #include "nfsv2.h"
  56. #include "bootp.h"
  57.  
  58. struct rtcphdr {
  59.         u_short rh_flags;       /* T:2 P:1 CNT:5 PT:8 */
  60.         u_short rh_len;         /* length of message (in bytes) */
  61.         u_int rh_ssrc;          /* synchronization src id */
  62. };
  63.  
  64. typedef struct {
  65.         u_int upper;            /* more significant 32 bits */
  66.         u_int lower;            /* less significant 32 bits */
  67. } ntp64;
  68.  
  69. /*
  70.  * Sender report.
  71.  */
  72. struct rtcp_sr {
  73.         ntp64 sr_ntp;           /* 64-bit ntp timestamp */
  74.         u_int sr_ts;            /* reference media timestamp */
  75.         u_int sr_np;            /* no. packets sent */
  76.         u_int sr_nb;            /* no. bytes sent */
  77. };
  78.  
  79. /*
  80.  * Receiver report.
  81.  * Time stamps are middle 32-bits of ntp timestamp.
  82.  */
  83. struct rtcp_rr {
  84.         u_int rr_srcid; /* sender being reported */
  85.         u_int rr_nr;            /* no. packets received */
  86.         u_int rr_np;            /* no. packets predicted */
  87.         u_int rr_dv;            /* jitter (delay variance) */
  88.         u_int rr_lsr;           /* orig. ts from last rr from this src  */
  89.         u_int rr_dlsr;          /* time from recpt of last rr to xmit time */
  90. };
  91.  
  92. /*XXX*/
  93. #define RTCP_PT_SR      0
  94. #define RTCP_PT_RR      1
  95. #define RTCP_PT_SDES    2
  96. #define         RTCP_SDES_CNAME 1
  97. #define         RTCP_SDES_NAME  2
  98. #define         RTCP_SDES_EMAIL 3
  99. #define         RTCP_SDES_PHONE 4
  100. #define         RTCP_SDES_LOC   5
  101. #define         RTCP_SDES_TOOL  6
  102. #define         RTCP_SDES_TXT   7
  103. #define RTCP_PT_BYE     3
  104. #define RTCP_PT_APP     4
  105.  
  106. static void
  107. vat_print(const void *hdr, u_int len, register const struct udphdr *up)
  108. {
  109.         /* vat/vt audio */
  110.         u_int ts = *(u_short *)hdr;
  111.         if ((ts & 0xf060) != 0) {
  112.                 /* probably vt */
  113.                 (void)printf(" udp/vt %u %d / %d",
  114.                              (u_int32_t)(ntohs(up->uh_ulen) - sizeof(*up)),
  115.                              ts & 0x3ff, ts >> 10);
  116.         } else {
  117.                 /* probably vat */
  118.                 u_int i0 = ntohl(((u_int *)hdr)[0]);
  119.                 u_int i1 = ntohl(((u_int *)hdr)[1]);
  120.                 printf(" udp/vat %u c%d %u%s",
  121.                         (u_int32_t)(ntohs(up->uh_ulen) - sizeof(*up) - 8),
  122.                         i0 & 0xffff,
  123.                         i1, i0 & 0x800000? "*" : "");
  124.                 /* audio format */
  125.                 if (i0 & 0x1f0000)
  126.                         printf(" f%d", (i0 >> 16) & 0x1f);
  127.                 if (i0 & 0x3f000000)
  128.                         printf(" s%d", (i0 >> 24) & 0x3f);
  129.         }
  130. }
  131.  
  132. static void
  133. rtp_print(const void *hdr, u_int len, register const struct udphdr *up)
  134. {
  135.         /* rtp v1 or v2 */
  136.         u_int *ip = (u_int *)hdr;
  137.         u_int hasopt, contype, hasmarker;
  138.         u_int i0 = ntohl(((u_int *)hdr)[0]);
  139.         u_int i1 = ntohl(((u_int *)hdr)[1]);
  140.         u_int dlen = ntohs(up->uh_ulen) - sizeof(*up) - 8;
  141.         const char* ptype;
  142.  
  143.         ip += 2;
  144.         len >>= 2;
  145.         len -= 2;
  146.         if ((i0 >> 30) == 1) {
  147.                 /* rtp v1 */
  148.                 hasopt = i0 & 0x800000;
  149.                 contype = (i0 >> 16) & 0x3f;
  150.                 hasmarker = i0 & 0x400000;
  151.                 ptype = "rtpv1";
  152.         } else { /*XXX*/
  153.                 /* rtp v2 */
  154.                 hasopt = i0 & 0x20000000;
  155.                 contype = (i0 >> 16) & 0x7f;
  156.                 hasmarker = i0 & 0x800000;
  157.                 dlen -= 4;
  158.                 ptype = "rtp";
  159.                 ip += 1;
  160.                 len -= 1;
  161.         }
  162.         printf(" udp/%s %d c%d %s%s %d",
  163.                 ptype,
  164.                 dlen,
  165.                 contype,
  166.                 hasopt? "+" : "",
  167.                 hasmarker? "*" : "",
  168.                 i0 & 0xffff);
  169.         if (vflag) {
  170.                 if (hasopt) {
  171.                         u_int i2, optlen;
  172.                         do {
  173.                                 i2 = ip[0];
  174.                                 optlen = (i2 >> 16) & 0xff;
  175.                                 if (optlen == 0 || optlen > len) {
  176.                                         printf(" !opt");
  177.                                         return;
  178.                                 }
  179.                                 ip += optlen;
  180.                         } while ((int)i2 >= 0);
  181.                 }
  182.                 if (contype == 0x1f)
  183.                         printf(" 0x%04x", ip[0] >> 16);
  184.                 printf(" %u", i1);
  185.         }
  186. }
  187.  
  188. static const u_char*
  189. rtcp_print(const u_char *hdr)
  190. {
  191.         /* rtp v2 control (rtcp) */
  192.         struct rtcp_rr* rr = 0;
  193.         struct rtcp_sr* sr;
  194.         struct rtcphdr* rh = (struct rtcphdr*)hdr;
  195.         u_int len = (ntohs(rh->rh_len) + 1) * 4;
  196.         u_short flags = ntohs(rh->rh_flags);
  197.         int cnt = (flags >> 8) & 0x1f;
  198.         double ts, dts, jitter;
  199.         if (vflag)
  200.                 printf(" %u", (u_int32_t)ntohl(rh->rh_ssrc));
  201.         switch (flags & 0xff) {
  202.         case RTCP_PT_SR:
  203.                 sr = (struct rtcp_sr*)(rh + 1);
  204.                 printf(" sr");
  205.                 if (len != cnt * sizeof(*rr) + sizeof(*sr) + sizeof(*rh))
  206.                         printf(" [%d]", len);
  207.                 ts = (double)((u_int32_t)ntohl(sr->sr_ts)) / 65536.;
  208.                 printf(" @%.2f %up %ub", ts, (u_int32_t)ntohl(sr->sr_np),
  209.                        (u_int32_t)ntohl(sr->sr_nb));
  210.                 rr = (struct rtcp_rr*)(sr + 1);
  211.                 break;
  212.         case RTCP_PT_RR:
  213.                 printf(" rr");
  214.                 if (len != cnt * sizeof(*rr) + sizeof(*rh))
  215.                         printf(" [%d]", len);
  216.                 rr = (struct rtcp_rr*)(rh + 1);
  217.                 break;
  218.         case RTCP_PT_SDES:
  219.                 printf(" sdes %d", len);
  220.                 cnt = 0;
  221.                 break;
  222.         case RTCP_PT_BYE:
  223.                 printf(" bye %d", len);
  224.                 cnt = 0;
  225.                 break;
  226.         default:
  227.                 printf(" type-0x%x %d", flags & 0xff, len);
  228.                 cnt = 0;
  229.                 break;
  230.         }
  231.         if (cnt > 1)
  232.                 printf(" c%d", cnt);
  233.         while (--cnt >= 0) {
  234.                 if ((u_char*)(rr + 1) > snapend) {
  235.                         printf(" [|rtcp]");
  236.                         return (snapend);
  237.                 }
  238.                 if (vflag)
  239.                         printf(" %u", (u_int32_t)ntohl(rr->rr_srcid));
  240.                 ts = (double)((u_int32_t)ntohl(rr->rr_lsr)) / 65536.;
  241.                 dts = (double)((u_int32_t)ntohl(rr->rr_dlsr)) / 65536.;
  242.                 jitter = (double)((u_int32_t)ntohl(rr->rr_dv)) / 65536.;
  243.                 printf(" %ur %ue %.2fj @%.2f+%.2f",
  244.                     (u_int32_t)ntohl(rr->rr_nr),
  245.                     (u_int32_t)ntohl(rr->rr_np),
  246.                     jitter, ts, dts);
  247.         }
  248.         return (hdr + len);
  249. }
  250.  
  251. /* XXX probably should use getservbyname() and cache answers */
  252. #define TFTP_PORT 69            /*XXX*/
  253. #define KERBEROS_PORT 88        /*XXX*/
  254. #define SUNRPC_PORT 111         /*XXX*/
  255. #define SNMP_PORT 161           /*XXX*/
  256. #define NTP_PORT 123            /*XXX*/
  257. #define SNMPTRAP_PORT 162       /*XXX*/
  258. #define RIP_PORT 520            /*XXX*/
  259. #define KERBEROS_SEC_PORT 750   /*XXX*/
  260. #define NETBIOS_NS_PORT   137
  261. #define NETBIOS_DGRAM_PORT   138
  262.  
  263. void
  264. udp_print(register const u_char *bp, u_int length, register const u_char *bp2)
  265. {
  266.         register const struct udphdr *up;
  267.         register const struct ip *ip;
  268.         register const u_char *cp;
  269.         u_short sport, dport, ulen;
  270.  
  271.         up = (struct udphdr *)bp;
  272.         ip = (struct ip *)bp2;
  273.         cp = (u_char *)(up + 1);
  274.         if (cp > snapend) {
  275.                 printf("[|udp]");
  276.                 return;
  277.         }
  278.         if (length < sizeof(struct udphdr)) {
  279.                 (void)printf(" truncated-udp %d", length);
  280.                 return;
  281.         }
  282.         length -= sizeof(struct udphdr);
  283.  
  284.         sport = ntohs(up->uh_sport);
  285.         dport = ntohs(up->uh_dport);
  286.         ulen = ntohs(up->uh_ulen);
  287.         if (packettype) {
  288.                 register struct rpc_msg *rp;
  289.                 enum msg_type direction;
  290.  
  291.                 switch (packettype) {
  292.  
  293.                 case PT_VAT:
  294.                         (void)printf("%s.%s > %s.%s:",
  295.                                 ipaddr_string(&ip->ip_src),
  296.                                 udpport_string(sport),
  297.                                 ipaddr_string(&ip->ip_dst),
  298.                                 udpport_string(dport));
  299.                         vat_print((void *)(up + 1), length, up);
  300.                         break;
  301.  
  302.                 case PT_WB:
  303.                         (void)printf("%s.%s > %s.%s:",
  304.                                 ipaddr_string(&ip->ip_src),
  305.                                 udpport_string(sport),
  306.                                 ipaddr_string(&ip->ip_dst),
  307.                                 udpport_string(dport));
  308.                         wb_print((void *)(up + 1), length);
  309.                         break;
  310.  
  311.                 case PT_RPC:
  312.                         rp = (struct rpc_msg *)(up + 1);
  313.                         direction = (enum msg_type)ntohl(rp->rm_direction);
  314.                         if (direction == CALL)
  315.                                 sunrpcrequest_print((u_char *)rp, length,
  316.                                     (u_char *)ip);
  317.                         else
  318.                                 nfsreply_print((u_char *)rp, length,
  319.                                     (u_char *)ip);                      /*XXX*/
  320.                         break;
  321.  
  322.                 case PT_RTP:
  323.                         (void)printf("%s.%s > %s.%s:",
  324.                                 ipaddr_string(&ip->ip_src),
  325.                                 udpport_string(sport),
  326.                                 ipaddr_string(&ip->ip_dst),
  327.                                 udpport_string(dport));
  328.                         rtp_print((void *)(up + 1), length, up);
  329.                         break;
  330.  
  331.                 case PT_RTCP:
  332.                         (void)printf("%s.%s > %s.%s:",
  333.                                 ipaddr_string(&ip->ip_src),
  334.                                 udpport_string(sport),
  335.                                 ipaddr_string(&ip->ip_dst),
  336.                                 udpport_string(dport));
  337.                         while (cp < snapend)
  338.                                 cp = rtcp_print(cp);
  339.                         break;
  340.                 }
  341.                 return;
  342.         }
  343.  
  344.         if (! qflag) {
  345.                 register struct rpc_msg *rp;
  346.                 enum msg_type direction;
  347.  
  348.                 rp = (struct rpc_msg *)(up + 1);
  349.                 TCHECK(rp->rm_direction);
  350.                 direction = (enum msg_type)ntohl(rp->rm_direction);
  351.                 if (dport == NFS_PORT && direction == CALL) {
  352.                         nfsreq_print((u_char *)rp, length, (u_char *)ip);
  353.                         return;
  354.                 }
  355.                 else if (sport == NFS_PORT && direction == REPLY) {
  356.                         nfsreply_print((u_char *)rp, length, (u_char *)ip);
  357.                         return;
  358.                 }
  359. #ifdef notdef
  360.                 else if (dport == SUNRPC_PORT && direction == CALL) {
  361.                         sunrpcrequest_print((u_char *)rp, length, (u_char *)ip);
  362.                         return;
  363.                 }
  364. #endif
  365.                 else {
  366.                         TCHECK2(cp[0], 1);
  367.                         if (((struct LAP *)cp)->type == lapDDP &&
  368.                             (atalk_port(sport) || atalk_port(dport))) {
  369.                                 if (vflag)
  370.                                         fputs("kip ", stdout);
  371.                                 atalk_print(cp, length);
  372.                                 return;
  373.                         }
  374.                 }
  375.         }
  376.         (void)printf("%s.%s > %s.%s:",
  377.                 ipaddr_string(&ip->ip_src), udpport_string(sport),
  378.                 ipaddr_string(&ip->ip_dst), udpport_string(dport));
  379.  
  380.         if (!qflag) {
  381. #define ISPORT(p) (dport == (p) || sport == (p))
  382.                 if (ISPORT(NAMESERVER_PORT))
  383.                         ns_print((const u_char *)(up + 1), length);
  384.                 else if (ISPORT(TFTP_PORT))
  385.                         tftp_print((const u_char *)(up + 1), length);
  386.                 else if (ISPORT(IPPORT_BOOTPC) || ISPORT(IPPORT_BOOTPS))
  387.                         bootp_print((const u_char *)(up + 1), length,
  388.                             sport, dport);
  389.                 else if (ISPORT(RIP_PORT))
  390.                         rip_print((const u_char *)(up + 1), length);
  391.                 else if (ISPORT(SNMP_PORT) || ISPORT(SNMPTRAP_PORT))
  392.                         snmp_print((const u_char *)(up + 1), length);
  393.                 else if (ISPORT(NTP_PORT))
  394.                         ntp_print((const u_char *)(up + 1), length);
  395.                 else if (ISPORT(KERBEROS_PORT) || ISPORT(KERBEROS_SEC_PORT))
  396.                         krb_print((const void *)(up + 1), length);
  397.                 else if (dport == 3456)
  398.                         vat_print((const void *)(up + 1), length, up);
  399.                 else if (ISPORT(NETBIOS_NS_PORT)) {
  400.                   unsigned char *data = (unsigned char *)(up + 1);
  401.                   nbt_udp137_print(data,data+length>snapend?snapend:data+length);
  402.                 }
  403.                 else if (ISPORT(NETBIOS_DGRAM_PORT)) {
  404.                   unsigned char *data = (unsigned char *)(up + 1);
  405.                   nbt_udp138_print(data,data+length>snapend?snapend:data+length);
  406.                 }
  407.                 /*
  408.                  * Kludge in test for whiteboard packets.
  409.                  */
  410.                 else if (dport == 4567)
  411.                         wb_print((const void *)(up + 1), length);
  412.                 else
  413.                         (void)printf(" udp %u",
  414.                             (u_int32_t)(ulen - sizeof(*up)));
  415. #undef ISPORT
  416.         } else
  417.                 (void)printf(" udp %u", (u_int32_t)(ulen - sizeof(*up)));
  418.         return;
  419. trunc:
  420.         fputs("[|udp]", stdout);
  421. }
  422.