home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / ipxsed.exe / IPXSND.C < prev    next >
Text File  |  1995-08-25  |  8KB  |  288 lines

  1. /*
  2.  
  3. Copyright (c) 1992 Novell, Inc.  All Rights Reserved.
  4.  
  5. THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
  6. TREATIES.  USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE
  7. LICENSE AGREEMENT ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK)
  8. THAT CONTAINS THIS WORK.
  9.  
  10. Pursuant to the SDK License Agreement, Novell hereby grants to
  11. Developer a royalty-free, non-exclusive license to include the
  12. sample code IPXSND.C and derivative binaries in its product.
  13. Novell grants to Developer worldwide distribution rights to market,
  14. distribute or sell the sample code IPXSND.C and derivative
  15. binaries as a component of Developer's product(s).  Novell shall
  16. have no obligations to Developer or Developer's customers with
  17. respect to this code.
  18.  
  19. DISCLAIMER:
  20.  
  21.     Novell, Inc. makes no representations or warranties with respect
  22. to the contents or use of this code, and specifically disclaims any
  23. express or implied warranties of merchantability or fitness for any
  24. particular purpose.  Further, Novell, Inc. reserves the right to revise
  25. this publication and to make changes to its content, at any time,
  26. without obligation to notify any person or entity of such revisions or
  27. changes.
  28.  
  29.     Further, Novell, Inc. makes no representations or warranties with
  30. respect to any software, and specifically disclaims any express or
  31. implied warranties of merchantability or fitness for any particular
  32. purpose.  Further, Novell, Inc. reserves the right to make changes to
  33. any and all parts of the software, at any time, without obligation to
  34. notify any person or entity of such changes.
  35.  
  36. ***************************************************************************
  37.     IPXSND.C
  38. **************************************************************************/
  39.  
  40. /**************************************************************************
  41. ** Description:  Sample code that demonstrates the how to set up to send
  42. **                  an IPX packet within Windows. The partner program to this
  43. **               one is IPXREC. 
  44. ** Compiler:     Borland C++ v4.5
  45. **
  46. ** Programmer :  Karl Bunnell
  47. ** Date:         08/24/1995
  48. **
  49. */
  50.  
  51.  
  52. #define NWWIN
  53.  
  54. #include <windows.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <nwcalls.h>
  58. #include <nwnet.h>
  59. #include <nwipxspx.h>
  60. #include <ctype.h>
  61. #include "ipxsnd.h"
  62.  
  63.  
  64. BOOL WINAPI MyPeekMessage( MSG FAR *lpMsg, HWND hWnd, UINT uFirst,
  65.                                     UINT uLast, UINT uCommand);
  66.  
  67. BOOL StartSendingPackets(IPXAddress *netAddress, HWND hwnd);
  68.  
  69. BOOL strToHex(char *ascii, unsigned char *hex, int length);
  70.  
  71. long FAR PASCAL _export WndProc(HWND, UINT, UINT, LONG);
  72.  
  73. HANDLE globalhInstance;
  74. static WORD sendCount=0;
  75. char    displayBuffer[60];
  76.  
  77. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  78.                                         LPSTR lpszCmdLine, int nCmdShow)
  79. {
  80.     static char    szAppName[] = "IPXSND";
  81.     HWND                hwnd;
  82.     MSG                    msg;
  83.     WNDCLASS        wndclass; 
  84.     BOOL            rval=0;
  85.  
  86.     if(!hPrevInstance)
  87.     {
  88.         wndclass.style                    = CS_HREDRAW | CS_VREDRAW;
  89.  
  90.         wndclass.lpfnWndProc        =    WndProc;
  91.         wndclass.cbClsExtra            =    0;
  92.         wndclass.cbWndExtra            =    DLGWINDOWEXTRA;
  93.         wndclass.hInstance            =    hInstance;
  94.         wndclass.hIcon                    =    LoadIcon(NULL, IDI_APPLICATION);
  95.         wndclass.hCursor                = LoadCursor(NULL, IDC_ARROW);
  96.         wndclass.hbrBackground    = COLOR_WINDOW + 1;
  97.         wndclass.lpszMenuName        =    NULL;
  98.         wndclass.lpszClassName    = szAppName;
  99.  
  100.         rval = RegisterClass(&wndclass);
  101.     }
  102.     globalhInstance = hInstance;
  103.     hwnd = CreateDialog(hInstance, szAppName, 0, NULL);
  104.     SendMessage(hwnd, WM_INITWINDOW, NULL, NULL);
  105.     ShowWindow(hwnd, nCmdShow);
  106.     while(GetMessage(&msg, NULL, 0,0))
  107.     {
  108.         TranslateMessage(&msg);
  109.         DispatchMessage(&msg);
  110.     }
  111.     return msg.wParam;
  112. }
  113.  
  114. long FAR PASCAL _export WndProc(HWND hwnd, UINT message, UINT wParam, LONG lParam)
  115. {
  116.     WORD  ccode;
  117.     static char inetString[27];
  118.     char    tempString[25];
  119.     static    IPXAddress netAddress;
  120.     DWORD        IPXTaskID=0L;
  121.     char        displayMessage[100];
  122.     int        i;
  123.     int        strCount = 0;
  124.  
  125.     switch(message)
  126.     {
  127.         case WM_INITWINDOW:
  128.             ccode = IPXInitialize(&IPXTaskID, 2, 576);
  129.             if(ccode)
  130.                 {
  131.                  wsprintf(displayMessage, "IPXInitialize returned %02X", ccode);
  132.                  MessageBox(NULL, (LPSTR)displayMessage, "Error!", IDOK);
  133.                  return FALSE;
  134.                 } 
  135.             break;
  136.  
  137.         case WM_COMMAND :
  138.             switch(wParam)
  139.             {
  140.                 case IDC_SEND :
  141.  
  142.                 SendDlgItemMessage(hwnd, IDC_NETADDR, WM_GETTEXT, sizeof(inetString), 
  143.                                             (LONG) (LPSTR) inetString);
  144.                 if(strlen(inetString) == 0)
  145.                     {
  146.                     MessageBox(NULL, (LPSTR)"You must enter a socket to send to!", "Error!", IDOK);
  147.                     break; 
  148.                     }
  149.  
  150.                 for(i=0; i<sizeof(inetString); ++i)
  151.                     {
  152.                     if(inetString[i] != ':')
  153.                         {
  154.                         tempString[strCount] = inetString[i];
  155.                         strCount +=1;
  156.                         }
  157.                     }
  158.  
  159.                 ccode = strToHex(tempString, (unsigned char *)&netAddress, 12);
  160.  
  161.                     StartSendingPackets(&netAddress, hwnd);
  162.  
  163.                 SendDlgItemMessage(hwnd, IDC_DISPLAY, WM_SETTEXT, 0, 
  164.                             (LONG) (LPSTR) displayBuffer);
  165.                                     
  166.                 break;
  167.  
  168.                 case IDC_STOP:
  169.                     PostMessage(hwnd, WM_DESTROY, 0, 0);
  170.                 break;
  171.  
  172.             }
  173.             return 0;
  174.  
  175.             
  176.         case WM_DESTROY :
  177.                 PostQuitMessage(0);
  178.                 return 0;
  179.     }
  180.     return DefWindowProc(hwnd, message, wParam, lParam);
  181. }
  182.  
  183.  
  184. BOOL StartSendingPackets(IPXAddress *netAddress, HWND hwnd)
  185. {
  186.     static IPXHeader   sHeader;
  187.     static ECB         sECB;
  188.     DWORD IPXTaskID = 0L;
  189.     int transportTime;
  190.     DISPLAYDATA     displayData;
  191.     MSG                msg;
  192.  
  193.     memcpy(sHeader.destination.network, netAddress->network, 4);
  194.     memcpy(sHeader.destination.node, netAddress->node,6);
  195.     memcpy(sHeader.destination.socket, netAddress->socket, 2);
  196.  
  197.     sHeader.length = NWWordSwap(sizeof(IPXHeader));
  198.     sHeader.packetType = 4;
  199.  
  200.     sECB.ESRAddress = 0;
  201.     sECB.inUseFlag = 0;
  202.     sECB.fragmentCount = 2;
  203.     sECB.socketNumber = *(WORD *)&netAddress->socket;
  204.     IPXGetLocalTarget( IPXTaskID,
  205.                      (BYTE far *)netAddress,
  206.                      (BYTE far *)sECB.immediateAddress,
  207.                      (int *)&transportTime );
  208.  
  209.     wsprintf(displayData.displayLineOne, "Packet: ");
  210.     wsprintf(displayData.displayLineTwo, "%d", sendCount +=1);
  211.  
  212.     sECB.fragmentDescriptor[0].address = (&sHeader);
  213.     sECB.fragmentDescriptor[0].size = sizeof(IPXHeader);
  214.     sECB.fragmentDescriptor[1].address = (&displayData);
  215.     sECB.fragmentDescriptor[1].size = sizeof(DISPLAYDATA);
  216.  
  217.         IPXSendPacket(IPXTaskID, &sECB);
  218.         while(sECB.inUseFlag)
  219.             {
  220.              IPXRelinquishControl();
  221.              while (MyPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  222.                 {
  223.                  TranslateMessage(&msg);
  224.                  DispatchMessage(&msg);
  225.                 }
  226.             }
  227.  
  228.     wsprintf(displayBuffer, "%s%s", displayData.displayLineOne, displayData.displayLineTwo);
  229.  
  230.  return 0;
  231. }
  232.  
  233.  
  234. BOOL WINAPI MyPeekMessage( MSG FAR *lpMsg, HWND hWnd, UINT uFirst,
  235.                                     UINT uLast, UINT uCommand)
  236. {
  237.      BOOL bPeek;
  238.      static BOOL bSwitch;
  239.  
  240.      bPeek = PeekMessage(lpMsg, hWnd, uFirst, uLast, uCommand);
  241.  
  242.      if(!bPeek)
  243.         {
  244.          if (bSwitch)
  245.             {
  246.              asm {
  247.                     mov ax, 0x1689
  248.                     mov bl, 1
  249.                     int 0x2F
  250.                     }
  251.             }
  252.  
  253.         
  254.     else
  255.         asm int 0x28
  256.     bSwitch = !bSwitch;
  257.  
  258.     }
  259.     return bPeek;
  260. }
  261.  
  262.  
  263.  
  264. BOOL strToHex(char *ascii, unsigned char *hex, int length)
  265. {
  266.     int      i, j;
  267.     char     tmp_char;
  268.  
  269.     for (i=0, j=0; i<2*length; i+=2,j++)
  270.         {
  271.         if (!isxdigit(tmp_char=toupper(ascii[i])) )
  272.             {
  273.             return 0;
  274.             }
  275.         else
  276.             hex[j] = ((tmp_char>'9') ? (tmp_char-0x37) : (tmp_char-0x30)) << 4;
  277.  
  278.         if(!isxdigit(tmp_char=toupper(ascii[i+1])) )
  279.             {
  280.             return 0;
  281.          }
  282.       else
  283.          hex[j] += (tmp_char > '9') ? (tmp_char-0x37) : (tmp_char-0x30);
  284.  
  285.       }
  286.    return 1;
  287. }
  288.