home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / pktsend.zip / PKTSEND.C < prev    next >
C/C++ Source or Header  |  1995-02-24  |  22KB  |  598 lines

  1. /*  PKTSEND.C
  2.  
  3.     Author:        Patrick Whittle
  4.     Date:          November 4, 1994
  5.     Revisions:     February 24, 1995
  6.  
  7.     Purpose:       Provide interface to network packet driver.
  8.  
  9.     The accompanying file 8086.H must be included for a successful
  10.     compile.  Two key functions used in PKTSEND for communicating with a
  11.     packet driver are drvrchk(), and sendpkt().  These are both found in
  12.     the file 8086.H.
  13.  
  14.     To implement certain driver calls such as Get Address (function 06h),
  15.     or Get Driver Info (function 01h, subfunction FFh), a C function
  16.     is available in the public domain by Phil Karn.  This function,
  17.     access_type(), can be found in the file PKTDRVR.C.  Following is
  18.     an example for using my functions with this Public Domain function:
  19.  
  20.     if (intno = drvrchk())
  21.         handle = access_type(intno,if_class,if_type,if_number,
  22.                 type,typelen,receiver);
  23.  
  24.     Once this completes successfully, PKTSEND.EXE will be registered with
  25.     the packet driver, and may begin using its internal functions.  Note
  26.     that the function sendpkt() used in this program is available without
  27.     first calling access_type.
  28.  
  29. */
  30. #include <stdio.h>
  31. #include <conio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <ctype.h>
  35. #include "8086.h"
  36.  
  37. #define TRUE          -1
  38. #define FALSE          0
  39. #define BELL           7
  40. #define ESCAPE         27
  41.  
  42. /* Global variables are next */
  43.  
  44. int pane = 10; /* made global Feb. 24 to correct problem with field changes */
  45.  
  46. /* Function prototypes are next */
  47. void sendframe(int times, char *data, char ieee[], int intno);
  48. int  validkey(int *code, char addr[], char *data);
  49. void newfield(char *data);
  50. void menu(char ary[]);
  51. void fldset(char disp[], int pos);
  52. void reset(void);
  53. void leave(int *code);
  54.  
  55. int main(void) {
  56.  
  57.     int intno, keystr = 0, extended = 0, col;
  58.     char *txt = "A network packet driver was ";  /* found, or not found? */
  59.  
  60.     char ieee[14] = {0x08,0x0A,0x21,0x36,0x2C,0x98,0x0B,
  61.                      0x81,0x26,0x37,0xAE,0x71,0x08,0x00}, *pkt;
  62.  
  63.     intno = drvrchk(); /* Function returns currently hooked interrupt in
  64.                           intno if it exists, or FALSE condition otherwise */
  65.     if (intno) {
  66.         menu(ieee);    /* pass initial network address to menu function */
  67.         gotoxy(19, 2);
  68.         cprintf("%sfound at 0x%X.", txt, intno);
  69.  
  70.         /* allocate memory for packet string */
  71.         pkt = (char *) calloc(104, sizeof(char)); // 90 bytes for packet, 14 for header
  72.         pkt = (char *) memcpy(pkt, ieee, 14);
  73.  
  74.         pkt += 14;  /* Point to end of ieee address.  This pointer is passed
  75.                        between sendframe() and main function for the purpose
  76.                        of concatination of address, and data. */
  77.  
  78.         textbackground(LIGHTGREEN);  /* defined in CONIO.H */
  79.         window(32, 11, 48, 11);      /* dimension of destination field */
  80.  
  81.         while (keystr != ESCAPE) {   /* escape key will break loop */
  82.             keystr = getch();
  83.             if (!keystr)
  84.                 extended = getch();  /* i.e. F1-F10, arrow keys, etc. */
  85.  
  86.             if (extended) {
  87.                 switch (extended) {
  88.                     case 'K':                               /* left arrow */
  89.                         gotoxy(wherex()-1, wherey());
  90.                         if (pane == 10 || pane == 20) {
  91.                             col = wherex();
  92.                             if (col==3||col==6||col==9||col==12||col==15)
  93.                                 gotoxy(col-1, wherey());
  94.                         }
  95.                         break;
  96.                     case 'M':                              /* right arrow */
  97.                         gotoxy(wherex()+1, wherey());
  98.                         if (pane == 10 || pane == 20) {
  99.                             col = wherex();
  100.                             if (col==3||col==6||col==9||col==12||col==15)
  101.                                 gotoxy(col+1, wherey());
  102.                         }
  103.                         break;
  104.                     case 'H':                                    /* up */
  105.  
  106.                         break;
  107.                     case 'P':                                    /* down */
  108.  
  109.                         break;
  110.                     case ';':                                    /* F1 */
  111.                         sendframe(1, pkt, ieee, intno);
  112.                         break;
  113.                     case '<':                                    /* F2 */
  114.                         sendframe(10, pkt, ieee, intno);
  115.                         break;
  116.                     case '=':                                    /* F3 */
  117.                         sendframe(100, pkt, ieee, intno);
  118.                         break;
  119.                 }
  120.                 extended = 0;   /* reset for next iteration */
  121.             }
  122.             else  /* else, normal keystroke */
  123.             {
  124.                 if (!validkey(&keystr, ieee, pkt))
  125.                     cprintf("%c", BELL);          /* Invalid key */
  126.                 else
  127.                 {
  128.                     if (keystr == ESCAPE)   /* Does the user want to exit? */
  129.                         leave(&keystr);
  130.                     else                /* Does not want to exit! */
  131.                     {
  132.                         col = wherex();
  133.                         if ((col == 3)||(col == 6)||(col == 9)||(col == 12)||(col == 15)) {
  134.  
  135.                             /* The "pane" variable will hold a value
  136.                                indicating the current field that is active
  137.                                to accept user input. Delimeters in the
  138.                                source and destination fields (colons) must
  139.                                by recognized with the code below.           */
  140.  
  141.                             if (pane == 10 || pane == 20)
  142.                                 gotoxy(col+1, wherey());
  143.                         }
  144.                     }
  145.                 }
  146.             }
  147.         }
  148.         reset();            /* restore default DOS window and colours */
  149.     }
  150.     else
  151.         printf("%sNOT found.\n", txt);  /* ftp packet driver wasn't found. */
  152.  
  153.     return(0);
  154. }
  155. /***************************************************************************
  156. Function  : sendframe
  157. Parameters:
  158. Returns   : nothing
  159.  
  160. ***************************************************************************/
  161. void sendframe(int times, char *data, char ieee[], int intno) {
  162.  
  163.     int i;
  164.     data -= 14;  // Point back to start.  See the line "pkt += 14" in main()
  165.     data = (char *) memcpy(data, ieee, 14);
  166.  
  167.     _setcursortype(_NOCURSOR);
  168.     textbackground(BLUE);
  169.     window(2, 3, 79, 23);
  170.     gotoxy(32, 20);
  171.  
  172.     for (i = 1; i <= times; i++) {
  173.         if (sendpkt(data, 104, intno)) {
  174.             gotoxy(22, 20);
  175.             clreol();
  176.             cprintf("Packet sent successfully %d time(s).", i);
  177.         }
  178.         else {
  179.             cprintf("Send was not successful.");
  180.             i = times;
  181.         }
  182.     }
  183.     data += 14;                     /* Start accepting data again */
  184.     textbackground(LIGHTGREEN);
  185.  
  186.     window(32, 11, 48, 11);
  187.     _setcursortype(_NORMALCURSOR);
  188.     _wscroll = 0;                   /* scrolling disabled */
  189.     pane = 10;                      /* a global variable */
  190. }
  191. /***************************************************************************
  192. Function  : validkey
  193. Parameters:
  194. Returns   : TRUE or FALSE condition
  195.  
  196. ***************************************************************************/
  197. int validkey(int *code, char addr[], char *data) {
  198.  
  199.     int temp, temp2, c, extended = 0, offset, flag = TRUE;
  200.     static int row, col = 0, nibble = 1;
  201.     char i, *ptr, save;
  202.  
  203.     switch (*code) {
  204.         case 9:                                 /* tab */
  205.             newfield(data);
  206.             break;
  207.         case 13:                                /* carriage return */
  208.             newfield(data);
  209.             break;
  210.         case ESCAPE:                            /* escape */
  211.  
  212.             break;
  213.         default:                                /* case else */
  214.             if (pane >= 10 && pane <= 30) {
  215.                 if (*code >=97 && *code <=102)
  216.                     *code -= 32;                /* change to upper case */
  217.  
  218.                 /* Is key pressed 0-9, A-F? */
  219.                 if ((*code >= 48 && *code <=57)||(*code >= 65 && *code <= 70)) {
  220.                     i = wherex();
  221.                     cprintf("%c", *code);
  222.  
  223.                     /* Next, adjust for colons that delimit each byte. These
  224.                        are displayed as part of the addresses for source and
  225.                        destination. */
  226.  
  227.                     if (i == 2) {
  228.                         nibble = FALSE;
  229.                         i = 1;
  230.                     }
  231.                     if (pane == 10 || pane == 20) { /* only adjust source
  232.                                                          and dest fields */
  233.                         if (i == 4 || i == 5) {
  234.                             if (i == 5) nibble = FALSE;
  235.                             i = 2;
  236.                         }
  237.                         if (i == 7 || i == 8) {
  238.                             if (i == 8) nibble = FALSE;
  239.                             i = 3;
  240.                         }
  241.                         if (i == 10|| i == 11) {
  242.                             if (i == 11) nibble = FALSE;
  243.                             i = 4;
  244.                         }
  245.                         if (i == 13|| i == 14) {
  246.                             if (i == 14) nibble = FALSE;
  247.                             i = 5;
  248.                         }
  249.                         if (i == 16|| i == 17) {
  250.                             if (i == 17) nibble = FALSE;
  251.                             i = 6;
  252.                         }
  253.                     }
  254.                     else {                    /* Adjust the "type" field */
  255.                         if (i == 2 || i == 4)
  256.                             nibble = FALSE;
  257.                         if (i == 3 || i == 4)
  258.                             i = 2;
  259.                     }
  260.                     /* Next, ASCII character 48 (zero) is converted to 0x00
  261.                        else A-F characters are converted to 0x0A, 0x0B, etc. */
  262.  
  263.                     if (*code >= 48 && *code <=57)
  264.                         *code -= 48;
  265.                     else
  266.                         *code -= 55;   /* ASCII character "A" for example is
  267.                                           65 in decimal.  Subtracting 55 from
  268.                                           it produces 0A in hex */
  269.                     ptr = (char *)addr;
  270.  
  271.                     switch(pane) {
  272.                         case 20:     ptr += 6;     break;
  273.                         case 30:     ptr += 12;    break;
  274.                     }
  275.                     ptr += --i;
  276.  
  277.                     if (nibble == 1) {
  278.                         save = *ptr;
  279.                         if (save >= 0x10 || save < 0) {
  280.                             temp = (int)save;
  281.                             temp2 = temp;
  282.                             temp2 = temp2 << 4;
  283.  
  284.                             temp = temp >> 4;
  285.                             temp = temp << 8;
  286.                             temp2 -= temp;
  287.                             temp2 = temp2 >> 4;
  288.                             save  = (char)temp2;
  289.                         }
  290.                         strncpy(ptr, (const char *)code, 1);
  291.                         *ptr = *ptr << 4;
  292.                         *ptr += save;
  293.                         nibble = FALSE;
  294.                     }
  295.                     else {
  296.                         save = *ptr;
  297.                         strncpy(ptr, (const char *)code, 1);
  298.                         save = save >> 4;
  299.                         save = save << 4;
  300.                         *ptr += save;
  301.                         nibble = 1;
  302.                     }
  303.                 }
  304.                 else
  305.                     flag = FALSE;  /* else, key was not 0-9, A-F */
  306.             }
  307.             else {             /* else, pane is 40 (i.e. the data window) */
  308.                 col = wherex();
  309.                 ungetch(*code); /* Force key just pressed back onto the keyboard */
  310.  
  311.                 for (i = 0; i <= 80; i++) {
  312.                     c = getch();
  313.                     if (!c)
  314.                         extended = getch();
  315.  
  316.                     if (extended)
  317.                         extended = 0;
  318.                     else {
  319.                         switch(c) {
  320.                             case 8:                      /* backspace */
  321.                                 i-=2;
  322.                                 gotoxy(wherex() - 1, wherey());
  323.                                 cprintf(" ");
  324.                                 gotoxy(wherex() - 1, wherey());
  325.                                 break;
  326.                             case 9:                      /* tab */
  327.  
  328.                                 break;
  329.                             case 13:
  330.                                 data[i] = '\0';      /* Terminate string */
  331.                                 i = 81;
  332.                                 newfield(data);
  333.                             case ESCAPE:
  334.  
  335.                                 break;
  336.                             default:
  337.                                 col = wherex();
  338.                                 row = wherey();
  339.  
  340.                                 if (c == 32)
  341.                                     offset = i;
  342.  
  343.                                 /* next, perform word wrap */
  344.                                 if (col == 29 && row != 3) {
  345.                                     if (offset < i) {
  346.                                         gotoxy(((offset + 1)-(i-28)), row);
  347.                                         clreol();
  348.                                         data[i] = '\0';
  349.                                         (char *)temp = data;
  350.                                         data += (offset + 1);
  351.                                         cprintf("\r\n%s", data);
  352.                                         data = (char *)temp;
  353.                                     }
  354.                                 }
  355.                                 if (col == 29 && row == 3) {
  356.                                     _wscroll = 0;  /* disable scrolling */
  357.                                     ungetch(13);   /* cause "for" exit */
  358.                                 }
  359.                                 data[i] = c;
  360.                                 cprintf("%c", c);
  361.  
  362.                         }     /* End of case structure */
  363.  
  364.                     }
  365.  
  366.                 }  /* Bottom of for loop */
  367.             }
  368.             break;            /* the end of default, or "case else" */
  369.     }
  370.     return(flag);
  371. }
  372. /***************************************************************************
  373. Function  : newfield
  374. Parameters:
  375. Returns   : Changes current field, "toggling" each call
  376.  
  377. ***************************************************************************/
  378. void newfield(char *data) {
  379.  
  380.     int i;
  381.     pane += 10;           /* pane changed from static to global Feb. 24 */
  382.     _wscroll = 0;         /* defined in CONIO.H */
  383.  
  384.     switch(pane) {
  385.         case 10:       window(32, 11, 48, 11);  /* select desination */
  386.                        break;
  387.         case 20:       window(32, 12, 48, 12);  /* select source */
  388.                        break;
  389.         case 30:       window(32, 14, 35, 14);  /* select type */
  390.                        break;
  391.         case 40:       window(32, 16, 60, 18);  /* select packet window */
  392.                        clrscr();
  393.                        _wscroll = 1;
  394.                        pane = 0;
  395.                        for (i = 0; i < 90; i++)
  396.                            data[i] = '\0';
  397.     }
  398. }
  399. /***************************************************************************
  400. Function  : menu
  401. Parameters: ethernet address to be printed
  402. Returns   : nothing
  403.  
  404. ***************************************************************************/
  405. void menu(char ary[]) {
  406.  
  407.     int coord, i;
  408.  
  409.     _setcursortype(_NOCURSOR);
  410.     textcolor(BLACK);           /* reverse the colours (black on white) */
  411.     textbackground(WHITE);
  412.     _wscroll = 0;          /* Do not scroll when maximum row is reached */
  413.  
  414.     /* Draw bars on top & bottom */
  415.     for (coord = 1; coord <= 80; coord++) {
  416.         gotoxy(coord, 1);
  417.         cprintf("%c", 32);
  418.         gotoxy(coord, 25);
  419.         cprintf("%c", 32);
  420.     }
  421.     gotoxy(30, 1);
  422.     cprintf("Packet Send Utility");
  423.     gotoxy(4, 25);
  424.     cprintf("F1=Send  %c  F2=10 Frames  %c  F3=100 Frames", 179, 179);
  425.  
  426.     window(1, 2, 80, 24);
  427.     textcolor(LIGHTGRAY);
  428.     textbackground(BLUE);
  429.     clrscr();               /* clear based on most recent call to window() */
  430.  
  431.     /* Draw horozontal lines on top and bottom of window */
  432.     for (coord = 2; coord < 80; coord++) {
  433.         gotoxy(coord, 1);
  434.         cprintf("%c", 196);
  435.         gotoxy(coord, 23);
  436.         cprintf("%c", 196);
  437.     }
  438.     /* Place corners of box */
  439.     gotoxy(1, 1);
  440.     cprintf("%c", 218);
  441.     gotoxy(80, 1);
  442.     cprintf("%c", 191);
  443.     gotoxy(1, 23);
  444.     cprintf("%c", 192);
  445.     gotoxy(80, 23);
  446.     cprintf("%c", 217);
  447.  
  448.     /* Draw left and right verticle lines */
  449.     for (coord = 2; coord < 23; coord++) {
  450.         gotoxy(1, coord);
  451.         cprintf("%c", 179);
  452.         gotoxy(80, coord);
  453.         cprintf("%c", 179);
  454.     }
  455.     textcolor(DARKGRAY);
  456.     textbackground(LIGHTGRAY);
  457.     /****************** Draw inner box ******************/
  458.  
  459.     /* Draw horozontal lines on top and bottom of inner window */
  460.     for (coord = 13; coord < 68; coord++) {
  461.         gotoxy(coord, 5);
  462.         cprintf("%c", 205);
  463.         gotoxy(coord, 19);
  464.         cprintf("%c", 205);
  465.     }
  466.     /* Place corners of inner box */
  467.     gotoxy(12, 5);                     /* row 5 to 19, col 12 to 68 */
  468.     cprintf("%c", 201);
  469.     gotoxy(68, 5);
  470.     cprintf("%c", 187);
  471.     gotoxy(12, 19);
  472.     cprintf("%c", 200);
  473.     gotoxy(68, 19);
  474.     cprintf("%c", 188);
  475.  
  476.     /* Draw left and right verticle lines for inner box */
  477.     for (coord = 6; coord < 19; coord++) {
  478.         gotoxy(12, coord);
  479.         cprintf("%c", 186);
  480.         gotoxy(68, coord);
  481.         cprintf("%c", 186);
  482.     }
  483.     textcolor(YELLOW);
  484.     window(13, 7, 67, 19);
  485.     clrscr();               /* clear based on most recent call to window() */
  486.     gotoxy(13, 2);
  487.     cprintf("Enter the Packet Frame to Send:");
  488.     textcolor(BLUE);
  489.     gotoxy(6, 5);
  490.     cprintf("Destination:");
  491.     gotoxy(11, 6);
  492.     cprintf("Source:");
  493.     gotoxy(13, 8);
  494.     cprintf("Type:");
  495.     gotoxy(13, 10);
  496.     cprintf("Data:");
  497.     textcolor(WHITE);
  498.     textbackground(LIGHTGREEN);
  499.     window(32, 11, 48, 11);     /* Destination window */
  500.     clrscr();
  501.     fldset(ary, 0);             /* Print 1 hex byte as two ASCII characters */
  502.  
  503.     window(32, 12, 48, 12);     /* Source window */
  504.     clrscr();
  505.     fldset(ary, 6);             /* offset 6 into "ary" array */
  506.  
  507.     window(32, 14, 35, 14);     /* Type window */
  508.     clrscr();
  509.  
  510.     for (i = 12; i < 14; i++)
  511.         cprintf("%02X", ary[i]);    /* Print in hexadecimal */
  512.  
  513.     window(32, 16, 61, 18);         /* Packet window */
  514.     textcolor(YELLOW);
  515.     clrscr();
  516.  
  517.     for (coord = 1; coord <= 3; coord++) {
  518.         gotoxy(30, coord);
  519.         cprintf("%c", 174);         /* print "«" to right of window. */
  520.     }
  521.     window(32, 16, 60, 18);
  522.     textcolor(WHITE);
  523.     clrscr();
  524.     window(2, 3, 79, 23);
  525.     textbackground(BLUE);
  526.     _setcursortype(_NORMALCURSOR);
  527. }
  528. /***************************************************************************
  529. Function  : fldset
  530. Parameters: This function is only used by menu()
  531. Returns   : nothing
  532.  
  533. ***************************************************************************/
  534. void fldset(char disp[], int pos) {
  535.  
  536.     int i, j, limit, delimit;
  537.     limit = pos + 6;
  538.     delimit = (limit - 1);
  539.  
  540.     for (i = pos; i < limit; i++) {
  541.         j = abs(disp[i]);
  542.         if (disp[i] < 0)
  543.             j = (128 + (128-j));   /* adjust large numbers */
  544.  
  545.         if (i < delimit)
  546.             cprintf("%02X:", j);
  547.         else
  548.             cprintf("%02X", j);
  549.     }
  550. }
  551. /***************************************************************************
  552. Function  : reset
  553. Parameters:
  554. Returns   : nothing
  555.  
  556. ***************************************************************************/
  557. void reset(void) {
  558.  
  559.     window(1, 1, 80, 25);
  560.     textcolor(LIGHTGRAY);
  561.     textbackground(BLACK);
  562.     _setcursortype(_NORMALCURSOR);
  563.     clrscr();
  564. }
  565. /***************************************************************************
  566. Function  : leave
  567. Parameters: a keystroke from user
  568. Returns   : nothing
  569.  
  570. ***************************************************************************/
  571. void leave(int *code) {
  572.  
  573.     struct text_info ti;    /* defined in CONIO.H */
  574.     int resp;
  575.  
  576.     gettextinfo(&ti);
  577.  
  578.     _setcursortype(_NOCURSOR);
  579.     window(2, 3, 79, 23);
  580.     gotoxy(22, 20);
  581.     textbackground(BLUE);
  582.     clreol();
  583.     gotoxy(31, 20);
  584.     cprintf("Do you wish to exit?");
  585.  
  586.     resp = getch();
  587.  
  588.     if (toupper(resp) != 'Y')
  589.         *code = 0;             /* change users key press to null */
  590.  
  591.     gotoxy(31, 20);
  592.     clreol();                  /* Remove prompt just displayed */
  593.  
  594.     window(ti.winleft, ti.wintop, ti.winright, ti.winbottom);
  595.     gotoxy(ti.curx, ti.cury);
  596.     textbackground(LIGHTGREEN);
  597.     _setcursortype(_NORMALCURSOR);
  598. }