home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / ppp / dp-2.3 / dpd / calllog.c next >
Encoding:
C/C++ Source or Header  |  1992-12-22  |  5.5 KB  |  231 lines

  1. /*
  2.  * Copyright (c) 1992 Purdue University
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by Purdue University.  The name of the University may not be used
  11.  * to endorse or promote products derived * from this software without
  12.  * specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * Note: this copyright applies to portions of this software developed
  18.  * at Purdue beyond the software covered by the original copyright.
  19.  */
  20.  
  21. #include <time.h>
  22. #include <fcntl.h>
  23. #include <netdb.h>
  24. #include <memory.h>
  25. #include <string.h>
  26.  
  27. #include <stdio.h>
  28. #include <sys/types.h>
  29. #include <sys/ioctl.h>
  30. #include <sys/time.h>
  31. #include <sys/file.h>
  32. #include <sys/param.h>
  33. #include <sys/stream.h>
  34. #include <netinet/in.h>
  35. #include <netinet/in_systm.h>
  36. #include <netinet/ip.h>
  37. #include <netinet/tcp.h>
  38. #include <netinet/udp.h>
  39. #include <sys/socket.h>
  40. #include <net/if.h>
  41.  
  42. #include "slip_var.h"
  43. #include "dp.h"
  44. #include "dp_str.h"
  45.  
  46. char *sctime(),
  47.      *port_str(),
  48.      *cnts_str();
  49.  
  50. void
  51. writelog(sitename, dev, starttime, stoptime, ss, req)
  52. char *sitename, *dev;
  53. time_t starttime, stoptime;
  54. struct slipstat *ss;
  55. struct dp_req *req;
  56. {
  57.     static char    WHERE[] = "writelog";
  58.     int        f;
  59.     long    hours;
  60.     long    minutes;
  61.     long    seconds;
  62.     long    duration = stoptime - starttime;
  63.     extern    char *ctime();
  64.     char    lbuf[132+1], *l = lbuf;
  65.     char    *cl;
  66.  
  67.     /* Just in case... */
  68.     if (starttime == 0)
  69.     return;
  70.  
  71.     hours = duration / 3600;
  72.     minutes = (duration - hours * 3600) / 60;
  73.     seconds = duration - hours * 3600 - minutes * 60;
  74.  
  75.     (void)sprintf(l, "%-12.12s%-5.5s %s %02ld:%02ld:%02ld %-7.7s ",
  76.     sitename, dev, sctime(&starttime), hours, minutes, seconds, progname);
  77.     l += strlen(l);
  78.  
  79.     (void)sprintf(l, "%-11s ", req ? port_str(req) : "");
  80.     l += strlen(l);
  81.  
  82.     (void)sprintf(l, "I %-12s ",
  83.           cnts_str(ss->sl_ipackets, ss->sl_ibytes, ss->sl_ierrors));
  84.     l += strlen(l);
  85.     (void)sprintf(l, "O %-12s",
  86.           cnts_str(ss->sl_opackets, ss->sl_obytes, ss->sl_oerrors));
  87.     l += strlen(l);
  88.  
  89.     while (*(l-1) == ' ')
  90.     l--;
  91.  
  92.     *l++ = '\n';
  93.  
  94.     cl = expand_dir_file("$DPLOG_DIR", CALL_LOG);
  95.     if ((f = open(cl, O_WRONLY|O_APPEND|O_CREAT, 0666)) < 0) {
  96.     d_log(DLOG_DIAG, WHERE, "Can't open call log, %m");
  97.     (void)free(cl);
  98.     return;
  99.     }
  100.     (void)write(f, lbuf, l-lbuf);
  101.     (void)close(f);
  102.     (void)free(cl);
  103. }
  104.  
  105. char *
  106. cnts_str(pkts, bytes, errs)
  107. u_int pkts, bytes, errs;
  108. {
  109.     static char cbuf[32];
  110.     if (errs)
  111.     (void)sprintf(cbuf, "%d/%d/%d", pkts, bytes, errs);
  112.     else
  113.     (void)sprintf(cbuf, "%d/%d", pkts, bytes);
  114.     return cbuf;
  115. }
  116.  
  117. char *
  118. sctime(t)
  119. time_t *t;
  120. {
  121.     static char tbuf[24];
  122.     (void)strftime(tbuf, sizeof(tbuf), "%D %T", localtime(t));
  123.     return tbuf;
  124. }
  125.  
  126. char *
  127. port_str(req)
  128. struct dp_req *req;
  129. {
  130.     struct ip *ip = &req->dr_ip;
  131.     struct protoent *pe;
  132.     struct servent *se;
  133.     char *proto, *port = (char *)0, prbuf[10], ptbuf[10];
  134.     static char pbuf[50];
  135.  
  136.     if (pe = getprotobynumber((int)ip->ip_p)) {
  137.     proto = pe->p_name;
  138.  
  139.     switch (ip->ip_p) {
  140.      case IPPROTO_TCP:
  141.         {
  142.         struct tcphdr *th;
  143.         int iplen = ip->ip_hl << 2;
  144.         if (req->dr_hdrlen < iplen + 2 * sizeof(u_short))
  145.             break;
  146.         th = (struct tcphdr *)&((char *)ip)[iplen];
  147.         if ((se = getservbyport((int)th->th_dport, proto)) ||
  148.             (se = getservbyport((int)th->th_sport, proto)))
  149.             port = se->s_name;
  150.         else {
  151.             (void)sprintf(ptbuf, "%d", th->th_dport);
  152.             port = ptbuf;
  153.         }
  154.         }
  155.         break;
  156.      case IPPROTO_UDP:
  157.         {
  158.         struct udphdr *uh;
  159.         int iplen = ip->ip_hl << 2;
  160.         if (req->dr_hdrlen < iplen + 2 * sizeof(u_short))
  161.             break;
  162.         uh = (struct udphdr *)&((char *)ip)[iplen];
  163.         if ((se = getservbyport((int)uh->uh_dport, proto)) ||
  164.             (se = getservbyport((int)uh->uh_sport, proto)))
  165.             port = se->s_name;
  166.         else {
  167.             (void)sprintf(ptbuf, "%d", uh->uh_dport);
  168.             port = ptbuf;
  169.         }
  170.         }
  171.         break;
  172.     }
  173.     }
  174.     else {
  175.     (void)sprintf(prbuf, "%d", ip->ip_p);
  176.     proto = prbuf;
  177.     }
  178.  
  179.     if (port)
  180.     (void)sprintf(pbuf, "%s/%s", proto, port);
  181.     else
  182.     (void)sprintf(pbuf, "%s", proto);
  183.     return pbuf;
  184. }
  185.  
  186. /*
  187.  *  Get the packet counts from the interface.
  188.  */
  189. #define    GETIFUINTAR(ifr, i)    (((int *)((ifr).ifr_data))[i])
  190.  
  191. getpacketcounts(ifnm, ss)
  192. char *ifnm;
  193. struct slipstat *ss;
  194. {
  195.     static char    WHERE[] = "getpacketcounts";
  196.     struct ifreq ifr;
  197.     int s;
  198.  
  199.     (void)memset((char *)&ifr, 0, sizeof(ifr));
  200.     (void)strcpy(ifr.ifr_name, ifnm);
  201.  
  202.     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  203.     d_log(DLOG_GENERAL, WHERE, "Can't create socket for \"%s\", %m",
  204.         ifnm);
  205.     unlock_pid();
  206.     exit(1);
  207.     }
  208.     if (ioctl(s, SIOCGDPIISTATS, (caddr_t)&ifr) < 0) {
  209.     d_log(DLOG_GENERAL, WHERE, "Can't SIOCGDPIISTATS for \"%s\", %m",
  210.           ifnm);
  211.     unlock_pid();
  212.     exit(1);
  213.     }
  214.  
  215.     ss->sl_ibytes   = GETIFUINTAR(ifr, 0);
  216.     ss->sl_ipackets = GETIFUINTAR(ifr, 1);
  217.     ss->sl_ierrors  = GETIFUINTAR(ifr, 2);
  218.  
  219.     if (ioctl(s, SIOCGDPIOSTATS, (caddr_t)&ifr) < 0) {
  220.     d_log(DLOG_GENERAL, WHERE, "Can't SIOCGDPIOSTATS for \"%s\", %m",
  221.           ifnm);
  222.     unlock_pid();
  223.     exit(1);
  224.     }
  225.  
  226.     ss->sl_obytes   = GETIFUINTAR(ifr, 0);
  227.     ss->sl_opackets = GETIFUINTAR(ifr, 1);
  228.     ss->sl_oerrors  = GETIFUINTAR(ifr, 2);
  229.     (void)close(s);
  230. }
  231.