home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / packetdrivers.tar.gz / pd.tar / src / seepkt.c < prev    next >
C/C++ Source or Header  |  1995-06-25  |  3KB  |  143 lines

  1. /* History:126,1 */
  2. /* public domain by Russell Nelson, nelson@crynwr.com.  Politeness dictates
  3.  * that you leave this notice intact */
  4.  
  5. #include <stdio.h>
  6. #include <dos.h>
  7. #include "pktdrvr.h"
  8.  
  9. char buffer[1514];            /* single buffer */
  10. int packet_len;                /* the length of data in buffer */
  11.  
  12. int intno;                /* our handle */
  13. int oldmode;
  14. char myeaddr[6];            /* our Ethernet address */
  15.  
  16. /* Borland C pushes registers in the following order.  MS-C may push them
  17. ** in a different order.
  18. */
  19. int interrupt receiver(bp, di, si, ds, es, dx, cx, bx, ax, ip, cs, flags)
  20. {
  21.     if (packet_len || cx > sizeof(buffer)) {
  22.         es = di = 0;        /* discard this packet */
  23.     } else if (ax == 0) {
  24.         es = FP_SEG(buffer);    /* tell them to stick it in our buffer */
  25.         di = FP_OFF(buffer);
  26.     } else {
  27.         packet_len = cx;    /* second upcall -- remember size. */
  28.     }
  29. }
  30.  
  31. dump_bytes(char *bytes, int count)
  32. {
  33.     int n;
  34.     char buf[16];
  35.     int address;
  36.     void fmtline();
  37.  
  38.     address = 0;
  39.     while(count){
  40.         if (count > 16) n = 16;
  41.         else n = count;
  42.         fmtline(address,bytes,n);
  43.         address += n;
  44.         count -= n;
  45.         bytes += n;
  46.     }
  47. }
  48. /* Print a buffer up to 16 bytes long in formatted hex with ascii
  49.  * translation, e.g.,
  50.  * 0000: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f  0123456789:;<=>?
  51.  */
  52. void
  53. fmtline(addr,buf,len)
  54. int addr;
  55. char *buf;
  56. int len;
  57. {
  58.     char line[80];
  59.     register char *aptr,*cptr;
  60.     unsigned register char c;
  61.     void ctohex();
  62.  
  63.     memset(line,' ',sizeof(line));
  64.     ctohex(line,addr >> 8);
  65.     ctohex(line+2,addr & 0xff);
  66.     aptr = &line[6];
  67.     cptr = &line[55];
  68.     while(len-- != 0){
  69.         c = *buf++;
  70.         ctohex(aptr,c);
  71.         aptr += 3;
  72.         c &= 0x7f;
  73.         *cptr++ = isprint(c) ? c : '.';
  74.     }
  75.     *cptr++ = '\n';
  76.     fwrite(line,1,(unsigned)(cptr-line),stdout);
  77. }
  78. /* Convert byte to two ascii-hex characters */
  79. static
  80. void
  81. ctohex(buf,c)
  82. register char *buf;
  83. register int c;
  84. {
  85.     static char hex[] = "0123456789abcdef";
  86.  
  87.     *buf++ = hex[c >> 4];
  88.     *buf = hex[c & 0xf];
  89. }
  90.  
  91.  
  92. int
  93. main()
  94. {
  95.     int handle;
  96.     char idle_chars[] = "-/|\\";
  97.     int idle_index = 0;
  98.  
  99.     /* search for the first packet driver in memory */
  100.     for (intno = 0x60; intno <= 0x80; intno++) {
  101.         if (test_for_pd(intno))
  102.             break;
  103.     }
  104.     /* if there is none, crap out */
  105.     if (!test_for_pd(intno)) {
  106.         fprintf(stderr, "No packet driver found");
  107.         exit(1);
  108.     }
  109.     /* get a handle so that we can receive packets */
  110.     handle = access_type(intno,
  111.         CL_ETHERNET,        /* has to be an Ethernet driver */
  112.         0xffff,            /* we don't care whose it is. */
  113.         0,            /* we want the first piece of hardware */
  114.         NULL,            /* doesn't matter because we want all */
  115.         0,            /* zero type length, that is, all. */
  116.         receiver);        /* -> our upcall */
  117.     /* get the adaptor's Ethernet address */
  118.     get_address (intno,handle,myeaddr,sizeof myeaddr);
  119.     /* put the interface into promiscuous mode */
  120.     oldmode = set_mode(intno,handle,6);
  121.     while (!kbhit()) {
  122. #if 0
  123.         printf("%c\b", idle_chars[idle_index++]);
  124.         idle_index %= (sizeof(idle_chars)-1);
  125. #endif
  126.         if (packet_len) {
  127.             dump_bytes(buffer, packet_len);
  128. #if 0
  129.             /* send packet back */
  130.             memcpy(buffer,buffer+6,6);
  131.             memcpy(buffer+6,myeaddr,6);
  132.             send_pkt(intno,buffer,packet_len);
  133. #endif
  134.             packet_len = 0;
  135.         }
  136.     }
  137.     getch();
  138.     set_mode(intno,handle,oldmode);
  139.     release_type(intno, handle);
  140.     return 0;
  141. }
  142.  
  143.