home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / comm / amitcp-3.0ß2.lha / AmiTCP / src / amitcp / kern / amiga_cstat.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  15KB  |  576 lines

  1. RCS_ID_C="$Id: amiga_cstat.c,v 3.1 1994/02/03 03:59:36 ppessi Exp $";
  2. /*
  3.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  4.  *                    Helsinki University of Technology, Finland.
  5.  *                    All rights reserved.
  6.  *
  7.  * HISTORY
  8.  * $Log: amiga_cstat.c,v $
  9.  * Revision 3.1  1994/02/03  03:59:36  ppessi
  10.  * Changed the format of network interface names
  11.  *
  12.  * Revision 1.23  1994/01/18  02:29:17  jraja
  13.  * Added rexx_gethostname() and rexx_sethostname() functions.
  14.  *
  15.  * Revision 1.22  1993/12/31  01:23:24  ppessi
  16.  * Fixed netmask handling.
  17.  *
  18.  * Revision 1.21  1993/11/06  23:51:22  ppessi
  19.  * Added Berkeley licence. Added route tree printing routines.
  20.  * Changed some allocation routines.
  21.  *
  22.  */
  23.  
  24. /*
  25.  * Copyright (c) 1983, 1988 Regents of the University of California.
  26.  * All rights reserved.
  27.  *
  28.  * Redistribution and use in source and binary forms, with or without
  29.  * modification, are permitted provided that the following conditions
  30.  * are met:
  31.  * 1. Redistributions of source code must retain the above copyright
  32.  *    notice, this list of conditions and the following disclaimer.
  33.  * 2. Redistributions in binary form must reproduce the above copyright
  34.  *    notice, this list of conditions and the following disclaimer in the
  35.  *    documentation and/or other materials provided with the distribution.
  36.  * 3. All advertising materials mentioning features or use of this software
  37.  *    must display the following acknowledgement:
  38.  *    This product includes software developed by the University of
  39.  *    California, Berkeley and its contributors.
  40.  * 4. Neither the name of the University nor the names of its contributors
  41.  *    may be used to endorse or promote products derived from this software
  42.  *    without specific prior written permission.
  43.  *
  44.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  45.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  48.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  49.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  50.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  51.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  52.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  53.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  54.  * SUCH DAMAGE.
  55.  */
  56.  
  57. #include <conf.h>
  58.  
  59. #include <sys/param.h>
  60. #include <sys/systm.h>
  61. #include <sys/socket.h>
  62. #include <sys/socketvar.h>
  63. #include <sys/synch.h>
  64. #include <sys/malloc.h>
  65. #include <sys/syslog.h>
  66.  
  67. #include <kern/amiga_includes.h>
  68. #include <kern/amiga_rexx.h>
  69. #include <kern/amiga_config.h>
  70.  
  71. #include <dos/rdargs.h>
  72.  
  73. #if __SASC
  74. #include <proto/dos.h>
  75. #elif __GNUC__
  76. #include <inline/dos.h>
  77. #endif
  78.  
  79. #include <net/route.h>
  80. #include <netinet/in_systm.h>
  81. #include <netinet/in.h>
  82. #include <netinet/ip.h>
  83. #include <netinet/in_pcb.h>
  84. #include <netinet/ip_var.h>
  85. #include <netinet/ip_icmp.h>
  86. #include <netinet/icmp_var.h>
  87. #include <netinet/tcp.h>
  88. #include <netinet/tcp_timer.h>
  89. #include <netinet/tcp_var.h>
  90. #include <netinet/udp.h>
  91. #include <netinet/udp_var.h>
  92.  
  93. #include <api/apicalls.h>
  94.  
  95. long count = 0;
  96.  
  97. int
  98. ultoa(unsigned long ul,char *buffer)
  99. {
  100.   static char buf[10];
  101.   char *p;
  102.   int len;
  103.  
  104.   p = buf;
  105.   *p='\0';
  106.  
  107.   do {
  108.     *++p = (ul % 10)+'0';
  109.   } while (ul /= 10);
  110.  
  111.   len = p - buf;
  112.  
  113.   while (*buffer++ = *p--)
  114.     ;
  115.   return(len);
  116. }
  117.  
  118. int
  119. ltoa(long l, char *buffer)
  120. {
  121.   int len=0;
  122.  
  123.   if(l<0){
  124.     *buffer++='-';
  125.     l=-l;
  126.     len++;
  127.   }
  128.  return (len + ultoa((unsigned long)l, buffer));
  129. }
  130.  
  131. /*
  132.  * Allocate big enough buffer for reply
  133.  */
  134. LONG
  135. CS_Alloc(struct CSource *reply, size_t size)
  136. {
  137.   /* do we have enough space? */
  138.   if (reply->CS_Length < size) {
  139.     char *buffer;
  140.     if ((buffer = bsd_malloc(size, M_TEMP, 0)) == NULL){
  141.       return 0;
  142.     }
  143.     /*
  144.      * Old buffer will be freed by caller
  145.      */
  146.     reply->CS_Buffer = buffer;
  147.     reply->CS_Length = size;
  148.   }
  149.  
  150.   return 1;
  151. }
  152.  
  153. /*
  154.  * getsockets(): a reply statics for all sockets
  155.  *
  156.  */
  157. struct printsocket {
  158.   struct in_addr  inp_faddr;    /* u_long, far addr */
  159.   u_short         inp_fport;
  160.   struct in_addr  inp_laddr;    /* u_long, local addr */
  161.   u_short         inp_lport;
  162.   short           so_type;    /* SOCK_STREAM, _DGRAM... */
  163.   u_short         so_rcv_sb_cc;    /* Recv queue size */
  164.   u_short         so_snd_sb_cc;    /* Send queue */
  165.   short           inp_ppcb_t_state; /* State of TCP connection */
  166. };
  167.  
  168. LONG
  169. getsockets(struct CSource *args, UBYTE **errstrp, struct CSource *res)
  170. {
  171.   int i, count = 0;
  172.   struct inpcb *pcb;
  173.   struct printsocket *pps, *mem;
  174.   spl_t s = splnet();        /* Critical section starts here */
  175.  
  176.   /* Count number of connections */
  177.   for(pcb = udb.inp_next; pcb != &udb ; pcb = pcb->inp_next)
  178.     ++count;
  179.   for(pcb = tcb.inp_next; pcb != &tcb ; pcb = pcb->inp_next)
  180.     ++count;
  181.  
  182.   if (count == 0) {        /* return now if nothing to print */
  183.     splx(s);
  184.     return RETURN_OK;
  185.   }
  186.  
  187.   /* Allocate memory */
  188.   mem = (struct printsocket *)
  189.     bsd_malloc(sizeof(struct printsocket) * (count), M_TEMP, 0);
  190.  
  191.   if (mem == NULL) {
  192.     splx(s);
  193.     *errstrp = ERR_MEMORY;
  194.     return RETURN_FAIL;
  195.   }
  196.  
  197.   /* Proto recv-q send-q laddr lport faddr fport state */
  198.   /*  1   1   4  1   4  1  8  1  4  1   8 1   4 1 1   1 =  42 chars */
  199. #define STATLEN 42
  200.   if (!CS_Alloc(res, STATLEN * count + 1)) {
  201.     /* Allocation failed, free printsocket memory */
  202.     splx(s);
  203.     bsd_free(mem, M_TEMP);
  204.     *errstrp = ERR_MEMORY;
  205.     return RETURN_FAIL;
  206.   }
  207.  
  208.   /* Copy information, TCP first.. */
  209.   for(pcb = tcb.inp_next, pps = mem; pcb != &tcb; pcb = pcb->inp_next, ++pps){
  210.     pps->inp_faddr = pcb->inp_faddr;
  211.     pps->inp_fport = pcb->inp_fport;
  212.     pps->inp_laddr = pcb->inp_laddr;
  213.     pps->inp_lport = pcb->inp_lport;
  214.     pps->so_type = pcb->inp_socket->so_type;
  215.     pps->so_rcv_sb_cc = pcb->inp_socket->so_rcv.sb_cc;
  216.     pps->so_snd_sb_cc = pcb->inp_socket->so_snd.sb_cc;
  217.     pps->inp_ppcb_t_state = ((struct tcpcb *)(pcb->inp_ppcb))->t_state;
  218.   }
  219.   /* ...then UDP */
  220.   for(pcb = udb.inp_next; pcb != &udb; pcb = pcb->inp_next, ++pps){
  221.     pps->inp_faddr = pcb->inp_faddr;
  222.     pps->inp_fport = pcb->inp_fport;
  223.     pps->inp_laddr = pcb->inp_laddr;
  224.     pps->inp_lport = pcb->inp_lport;
  225.     pps->so_type = pcb->inp_socket->so_type;
  226.     pps->so_rcv_sb_cc = pcb->inp_socket->so_rcv.sb_cc;
  227.     pps->so_snd_sb_cc = pcb->inp_socket->so_snd.sb_cc;
  228.     pps->inp_ppcb_t_state = 0;    /* NO state for UDP */
  229.   }
  230.  
  231.   splx(s);            /* Critical section completed now */
  232.  
  233.   /*
  234.    * Print all socket entries
  235.    */
  236.   for(i = 0; i < count; i++)
  237.     csprintf(res, "%lc %04lx %04lx %08lx %04lx %08lx %04lx %1lx%s",
  238.          mem[i].so_type == SOCK_STREAM ? 't': 'u',
  239.          mem[i].so_rcv_sb_cc, mem[i].so_snd_sb_cc,
  240.          mem[i].inp_laddr.s_addr, mem[i].inp_lport,
  241.          mem[i].inp_faddr.s_addr, mem[i].inp_fport,
  242.          mem[i].inp_ppcb_t_state,
  243.          (i < count - 1) ? " " : "");
  244.  
  245. #if DIAGNOSTIC            /* check for overrun */
  246.   if (res->CS_CurChr >= res->CS_Length)
  247.     log(LOG_ERR, "getsockets(): buffer overwritten by %ld bytes\n",
  248.     res->CS_CurChr - res->CS_Length + 1);
  249. #endif
  250.  
  251.   /*
  252.    * free mem
  253.    */
  254.   bsd_free(mem, M_TEMP);
  255.  
  256.   return RETURN_OK;
  257. }
  258.  
  259. /*
  260.  * Get ICMP history profiles
  261.  */
  262. #include <netinet/icmp_var.h>
  263. #include <netinet/ip_icmp.h>
  264.  
  265. LONG 
  266. read_icmphist(struct CSource *args, UBYTE **errstrp, struct CSource *res)
  267. {
  268.   int i;
  269.   UBYTE *p = res->CS_Buffer;
  270.  
  271.   for(i = 0;i <= ICMP_MAXTYPE; i++){
  272.     p += ultoa(icmpstat.icps_outhist[i], p);
  273.     *p++=' ';
  274.   }
  275.  
  276.   for(i = 0;i <= ICMP_MAXTYPE; i++){
  277.     p += ultoa(icmpstat.icps_inhist[i], p);
  278.     *p++ = ' ';
  279.   }
  280.   *--p = '\0';
  281.  
  282.   res->CS_CurChr = p - res->CS_Buffer;
  283.   return RETURN_OK;
  284. }
  285.  
  286. /*
  287.  * Get routing tables
  288.  */
  289. #include <net/route.h>
  290. #include <net/if.h>
  291.  
  292. #define DB(x) ;
  293.  
  294. /* Address families supported */
  295. extern STRPTR KW_Protocols;
  296.  
  297. /* What is stored in the route entry */
  298. #define NORMAL 0
  299. #define MASK   1
  300. #define DUPED  2
  301.  
  302. /* Our recursion depth in route tree. 32 is # of bits in IP address */
  303. /* Other protocols may require deeper stack */
  304. #define MAX_ROUT