home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3298 / netinp.c < prev    next >
C/C++ Source or Header  |  1991-05-06  |  2KB  |  125 lines

  1. /* History:
  2. 5/1/91 DJB baseline public domain. todo: eliminate this file.
  3. Derived from authd 3.01, DJB.
  4. */
  5.  
  6. #ifdef USENETSTAT
  7.  
  8. #include "confnonsfinet.h"
  9. #ifdef NONSFINET
  10. #define NETSTAT "/usr/bin/netstat -n -A"
  11. #endif
  12.  
  13. /* End of machine define-mod section. */
  14.  
  15. #ifndef NETSTAT
  16. #define NETSTAT "/usr/ucb/netstat -n -A -f inet"
  17. #endif
  18. #ifndef NETSTATBUF
  19. #define NETSTATBUF 200 /* 80 would suffice */
  20. #endif
  21. #ifndef NETSTATREMOTE
  22. #define NETSTATREMOTE 49 /* has to be 53 for SunOS 4.1.1, I think */
  23. #endif
  24. #ifndef NETSTATWIDTH
  25. #define NETSTATWIDTH 17
  26. #endif
  27. #ifndef REMOTESIZE
  28. #define REMOTESIZE 100 /* guaranteed to be enough */
  29. #endif
  30.  
  31. #endif
  32.  
  33. #include <stdio.h>
  34. FILE *popen(); /* grrrr */
  35. #include "kmem.h"
  36. #include "structinpcb.h"
  37. #ifdef USENETSTAT
  38. #include "structtcp.h"
  39. #else
  40. #include "inpcblist.h"
  41. #endif
  42. #include "netinp.h"
  43.  
  44. #ifdef USENETSTAT
  45. static FILE *fi;
  46. static char remote[REMOTESIZE];
  47. static char s[NETSTATBUF];
  48. static int header;
  49. static int pcb;
  50. static struct tcpcb tcp;
  51. static struct inpcb inp;
  52.  
  53. char *inploc;
  54.  
  55. int netinpinit(r1,r2,r3,r4,rp)
  56. int r1;
  57. int r2;
  58. int r3;
  59. int r4;
  60. int rp;
  61. {
  62.  fi = popen(NETSTAT,"r");
  63.  if (!fi)
  64.    return -1;
  65.  (void) sprintf(remote,"%d.%d.%d.%d.%d                    ",r1,r2,r3,r4,rp);
  66.  header = 0;
  67.  return 0;
  68. }
  69.  
  70. struct inpcb *nextnetinp()
  71. {
  72.  if (!header)
  73.   {
  74.    header = 1;
  75.    if (!fgets(s,sizeof(s),fi))
  76.      return 0;
  77.    if (!fgets(s,sizeof(s),fi))
  78.      return 0;
  79.   }
  80.  do
  81.   {
  82.    if (!fgets(s,sizeof(s),fi))
  83.      return 0; /* XXX: cannot distinguish from error */
  84.    if (sscanf(s,"%8x",&pcb) != 1)
  85.      return 0;
  86.   }
  87.  while (strncmp(s + NETSTATREMOTE,remote,NETSTATWIDTH));
  88.  if (kmemcpy((char *) &tcp,(char *) pcb,sizeof(tcp)) == -1)
  89.    return 0;
  90.  if (!tcp.t_inpcb)
  91.    return 0;
  92.  if (kmemcpy((char *) &inp,(char *) tcp.t_inpcb,sizeof(inp)) == -1)
  93.    return 0;
  94.  if (inp.inp_ppcb != (char *) pcb)
  95.    return 0;
  96.  inploc = tcp.t_inpcb;
  97.  return &inp;
  98. }
  99.  
  100. #else
  101.  
  102. char *inploc;
  103. static struct inpcb *inp;
  104.  
  105. int netinpinit(r1,r2,r3,r4,rp)
  106. int r1;
  107. int r2;
  108. int r3;
  109. int r4;
  110. int rp;
  111. {
  112.  if (inpcblistinit() == -1)
  113.    return -1;
  114.  inp = 0;
  115. }
  116.  
  117. struct inpcb *nextnetinp()
  118. {
  119.  inp = nextinpcb(inp);
  120.  inploc = (char *) inpcbloc;
  121.  return inp;
  122. }
  123.  
  124. #endif
  125.