home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / crh / freebsd / rootkit / netstat / atalk.c next >
Encoding:
C/C++ Source or Header  |  2002-05-27  |  7.5 KB  |  287 lines

  1. /*
  2.  * Copyright (c) 1983, 1988, 1993
  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 the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. /*
  36. static char sccsid[] = "@(#)atalk.c    1.1 (Whistle) 6/6/96";
  37. */
  38. static const char rcsid[] =
  39.     "$Id: atalk.c,v 1.6.2.1 1997/07/30 06:52:04 charnier Exp $";
  40. #endif /* not lint */
  41.  
  42. #include <sys/param.h>
  43. #include <sys/queue.h>
  44. #include <sys/socket.h>
  45. #include <sys/socketvar.h>
  46. #include <sys/protosw.h>
  47.  
  48. #include <net/route.h>
  49. #include <net/if.h>
  50.  
  51. #include <netinet/tcp_fsm.h>
  52.  
  53. #include <netatalk/at.h>
  54. #include <netatalk/ddp_var.h>
  55.  
  56. #include <nlist.h>
  57. #include <errno.h>
  58. #include <stdio.h>
  59. #include <string.h>
  60. #include "netstat.h"
  61.  
  62. struct    ddpcb ddpcb;
  63. struct    socket sockb;
  64.  
  65. static    int first = 1;
  66.  
  67. /*
  68.  * Print a summary of connections related to a Network Systems
  69.  * protocol.  For XXX, also give state of connection.
  70.  * Listening processes (aflag) are suppressed unless the
  71.  * -a (all) flag is specified.
  72.  */
  73.  
  74. static char *
  75. at_pr_net(struct sockaddr_at *sat, int numeric)
  76. {
  77. static    char mybuf[50];
  78.  
  79.     if (!numeric) {
  80.         switch(sat->sat_addr.s_net) {
  81.         case 0xffff:
  82.             return "????";
  83.         case ATADDR_ANYNET:
  84.             return("*");
  85.         }
  86.     }
  87.     sprintf(mybuf,"%hu",ntohs(sat->sat_addr.s_net));
  88.     return mybuf;
  89. }
  90.  
  91. static char *
  92. at_pr_host(struct sockaddr_at *sat, int numeric)
  93. {
  94. static    char mybuf[50];
  95.  
  96.     if (!numeric) {
  97.         switch(sat->sat_addr.s_node) {
  98.         case ATADDR_BCAST:
  99.             return "bcast";
  100.         case ATADDR_ANYNODE:
  101.             return("*");
  102.         }
  103.     }
  104.     sprintf(mybuf,"%d",(unsigned int)sat->sat_addr.s_node);
  105.     return mybuf;
  106. }
  107.  
  108. char *
  109. at_pr_port(struct sockaddr_at *sat)
  110. {
  111. static    char mybuf[50];
  112.  
  113.     switch(sat->sat_port) {
  114.     case ATADDR_ANYPORT:
  115.         return("*");
  116.     case 0xff:
  117.         return "????";
  118.     default:
  119.         sprintf(mybuf,"%d",(unsigned int)sat->sat_port);
  120.     }
  121.     return mybuf;
  122. }
  123.  
  124. static char *
  125. at_pr_range(struct sockaddr_at *sat)
  126. {
  127. static    char mybuf[50];
  128.  
  129.     if(sat->sat_range.r_netrange.nr_firstnet
  130.            != sat->sat_range.r_netrange.nr_lastnet) {
  131.         sprintf(mybuf,"%d-%d",
  132.             ntohs(sat->sat_range.r_netrange.nr_firstnet),
  133.             ntohs(sat->sat_range.r_netrange.nr_lastnet));
  134.     } else {
  135.         sprintf(mybuf,"%d",
  136.             ntohs(sat->sat_range.r_netrange.nr_firstnet));
  137.     }
  138.     return mybuf;
  139. }
  140.  
  141.  
  142. /* what == 0 for addr only == 3 */
  143. /*         1 for net */
  144. /*         2 for host */
  145. /*         4 for port */
  146. /*         8 for numeric only */
  147. char *
  148. atalk_print(sa,what)
  149.     register struct sockaddr *sa;
  150. {
  151.     struct sockaddr_at *sat = (struct sockaddr_at *)sa;
  152.     static    char mybuf[50];
  153.     int numeric = (what & 0x08);
  154.  
  155.     mybuf[0] = 0;
  156.     switch (what & 0x13) {
  157.     case 0:
  158.         mybuf[0] = 0;
  159.         break;
  160.     case 1:
  161.         sprintf(mybuf,"%s",at_pr_net(sat, numeric));
  162.         break;
  163.     case 2:
  164.         sprintf(mybuf,"%s",at_pr_host(sat, numeric));
  165.         break;
  166.     case 3:
  167.         sprintf(mybuf,"%s.%s",
  168.                 at_pr_net(sat, numeric),
  169.                 at_pr_host(sat, numeric));
  170.         break;
  171.     case 0x10:
  172.         sprintf(mybuf,"%s", at_pr_range(sat));
  173.     }
  174.     if (what & 4) {
  175.         sprintf(mybuf+strlen(mybuf),".%s",at_pr_port(sat));
  176.     }
  177.     return mybuf;
  178. }
  179.  
  180. char *
  181. atalk_print2(struct sockaddr *sa, struct sockaddr *mask, int what)
  182. {
  183.   int n;
  184.   static char buf[100];
  185.   struct sockaddr_at *sat1, *sat2;
  186.   struct sockaddr_at thesockaddr;
  187.   struct sockaddr *sa2;
  188.  
  189.   sat1 = (struct sockaddr_at *)sa;
  190.   sat2 = (struct sockaddr_at *)mask;
  191.   sa2 = (struct sockaddr *)&thesockaddr;
  192.  
  193.   thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net & sat2->sat_addr.s_net;
  194.   n = snprintf(buf, sizeof(buf), "%s", atalk_print(sa2, 1 |(what & 8)));
  195.   if(sat2->sat_addr.s_net != 0xFFFF) {
  196.     thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net | ~sat2->sat_addr.s_net;
  197.     n += snprintf(buf + n, sizeof(buf) - n,
  198.         "-%s", atalk_print(sa2, 1 |(what & 8)));
  199.   }
  200.   if(what & 2)
  201.   n += snprintf(buf + n, sizeof(buf) - n, ".%s", atalk_print(sa, what&(~1)));
  202.   return(buf);
  203. }
  204.  
  205. void
  206. atalkprotopr(off, name)
  207.     u_long off;
  208.     char *name;
  209. {
  210.     struct ddpcb cb;
  211.     register struct ddpcb *prev, *next;
  212.     struct ddpcb *initial;
  213.  
  214.     if (off == 0)
  215.         return;
  216.     kread(off, (char *)&initial, sizeof (struct ddpcb *));
  217.     ddpcb = cb;
  218.     prev = (struct ddpcb *)off;
  219.     for (next = initial ;next != NULL; prev = next) {
  220.         u_long ppcb;
  221.  
  222.         kread((u_long)next, (char *)&ddpcb, sizeof (ddpcb));
  223.         next = ddpcb.ddp_next;
  224. #if 0
  225.         if (!aflag && atalk_nullhost(ddpcb.ddp_lsat) ) {
  226.             continue;
  227.         }
  228. #endif
  229.         kread((u_long)ddpcb.ddp_socket,
  230.                 (char *)&sockb, sizeof (sockb));
  231.         if (first) {
  232.             printf("Active ATALK connections");
  233.             if (aflag)
  234.                 printf(" (including servers)");
  235.             putchar('\n');
  236.             if (Aflag)
  237.                 printf("%-8.8s ", "PCB");
  238.             printf(Aflag ?
  239.                 "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
  240.                 "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
  241.                 "Proto", "Recv-Q", "Send-Q",
  242.                 "Local Address", "Foreign Address", "(state)");
  243.             first = 0;
  244.         }
  245.         if (Aflag)
  246.             printf("%8x ", ppcb);
  247.         printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc,
  248.             sockb.so_snd.sb_cc);
  249.         printf(Aflag?" %-18.18s":" %-22.22s", atalk_print(
  250.                     (struct sockaddr *)&ddpcb.ddp_lsat,7));
  251.         printf(Aflag?" %-18.18s":" %-22.22s", atalk_print(
  252.                     (struct sockaddr *)&ddpcb.ddp_fsat,7));
  253.         putchar('\n');
  254.     }
  255. }
  256. #define ANY(x,y,z) \
  257.     ((x) ? printf("\t%d %s%s%s\n",x,y,plural(x),z) : 0)
  258.  
  259. /*
  260.  * Dump DDP statistics structure.
  261.  */
  262. void
  263. ddp_stats(off, name)
  264.     u_long off;
  265.     char *name;
  266. {
  267.     struct ddpstat ddpstat;
  268.  
  269.     if (off == 0)
  270.         return;
  271.     kread(off, (char *)&ddpstat, sizeof (ddpstat));
  272.     printf("%s:\n", name);
  273.     ANY(ddpstat.ddps_short, "packet", " with short headers ");
  274.     ANY(ddpstat.ddps_long, "packet", " with long headers ");
  275.     ANY(ddpstat.ddps_nosum, "packet", " with no checksum ");
  276.     ANY(ddpstat.ddps_tooshort, "packet", " too short ");
  277.     ANY(ddpstat.ddps_badsum, "packet", " with bad checksum ");
  278.     ANY(ddpstat.ddps_toosmall, "packet", " with not enough data ");
  279.     ANY(ddpstat.ddps_forward, "packet", " forwarded ");
  280.     ANY(ddpstat.ddps_encap, "packet", " encapsulated ");
  281.     ANY(ddpstat.ddps_cantforward, "packet", " rcvd for unreachable dest ");
  282.     ANY(ddpstat.ddps_nosockspace, "packet", " dropped due to no socket space ");
  283. }
  284. #undef ANY
  285.  
  286.  
  287.