home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / pmvnc100.zip / network.c < prev    next >
C/C++ Source or Header  |  1999-09-10  |  5KB  |  228 lines

  1. /*
  2.  * network.c - PM VNC Viewer, Networking Supports
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <sys/select.h>
  14. #include <sys/time.h>
  15. #include <netinet/in.h>
  16. #include <arpa/inet.h>
  17. #include <netdb.h>
  18.  
  19. #define INCL_DOS
  20. #define INCL_PM
  21. #include <os2.h>
  22.  
  23. #include "pmvncdef.h"
  24.  
  25. HAB     habNetwork = NULLHANDLE ;
  26. HMQ     hmqNetwork = NULLHANDLE ;
  27.  
  28. /*
  29.  * VNC Server and Session Options
  30.  */
  31.  
  32. #define VNCPORTBASE     (5900)
  33.  
  34. static  int     ServerSock ;
  35.  
  36. /*
  37.  * Notifications to Window Thread
  38.  */
  39.  
  40. void    netFail(PUCHAR msg)
  41. {
  42.     WinPostMsg(hwndClient, WM_VNC_FAIL, NULL, MPFROMP(msg)) ;
  43. }
  44.  
  45. void    netNotify(int ev, MPARAM mp1, MPARAM mp2)
  46. {
  47.     WinPostMsg(hwndClient, ev, mp1, mp2) ;
  48. }
  49.  
  50. /*
  51.  * thread control variables
  52.  */
  53.  
  54. static  BOOL    stopThread = FALSE ;
  55. static  BOOL    doneThread = FALSE ;
  56.  
  57. /*
  58.  * netSend/Recv - network I/O with exact length
  59.  */
  60.  
  61. BOOL    netSend(PUCHAR buf, int len)
  62. {
  63.     int     cnt, num ;
  64.     
  65.     for (cnt = 0 ; cnt < len ;  ) {
  66.         if (stopThread) {
  67.         return FALSE ;
  68.     }
  69.     if ((num = send(ServerSock, (buf + cnt), (len - cnt), 0)) <= 0) {
  70.         return FALSE ;
  71.     }
  72.     cnt += num ;
  73.     }
  74.     return TRUE ;
  75. }
  76.  
  77. BOOL    netRecv(PUCHAR buf, int len)
  78. {
  79.     int     cnt, num ;
  80.     
  81.     for (cnt = 0 ; cnt < len ;  ) {
  82.         if (stopThread) {
  83.         return FALSE ;
  84.     }
  85.     if ((num = recv(ServerSock, (buf + cnt), (len - cnt), 0)) <= 0) {
  86.         return FALSE ;
  87.     }
  88.     cnt += num ;
  89.     }
  90.     return TRUE ;
  91. }
  92.  
  93. void    netDump(PUCHAR buf, int len)
  94. {
  95.     int     cnt ;
  96.     
  97.     for (cnt = 0 ; cnt < len ; cnt++) {
  98.         printf("%02x ", (buf[cnt] & 0xff)) ;
  99.     if ((cnt % 16) == 15) {
  100.         printf("\n") ;
  101.     }
  102.     }
  103.     printf("\n") ;
  104. }
  105.  
  106. /*
  107.  * netThread - networking thread
  108.  */
  109.  
  110. static  void    netThread(void *arg)
  111. {
  112.     UCHAR   host[256] ;
  113.     UCHAR   mesg[256] ;
  114.     ULONG   port      ;
  115.     ULONG   ipaddr    ;
  116.     struct  sockaddr_in server ;
  117.     struct  hostent     *hp    ;
  118.     
  119.     if (sscanf(SessServerName, "%[^:]:%d", host, &port) != 2) {
  120.         netFail("bad server spec.") ;
  121.         doneThread = TRUE ;
  122.     return ;
  123.     }
  124.     if (port < 100) {
  125.         port += VNCPORTBASE ;
  126.     }
  127.  
  128.     TRACE("Connect to Server %s:%d\n", host, port) ;
  129.  
  130.     if (isdigit(host[0])) {
  131.         ipaddr = inet_addr(host) ;
  132.     } else if ((hp = gethostbyname(host)) != NULL) {
  133.         ipaddr = *((ULONG *) hp->h_addr) ;
  134.     } else {
  135.         netFail("no such host") ;
  136.     doneThread = TRUE ;
  137.     return ;
  138.     }
  139.    
  140.     if ((ServerSock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  141.         netFail("failed to create socket") ;
  142.     doneThread = TRUE ;
  143.     return ;
  144.     }
  145.     
  146.     memset(&server, 0, sizeof(server)) ;
  147.     server.sin_family      = AF_INET ;
  148.     server.sin_addr.s_addr = ipaddr  ;
  149.     server.sin_port        = htons((USHORT) port) ;
  150.  
  151.     if (connect(ServerSock, (struct sockaddr *) &server, sizeof(server)) == -1) {
  152.         sprintf(mesg, "failed to connect (%d)", errno) ;
  153.         netFail(mesg) ;
  154.     doneThread = TRUE ;
  155.     return ;
  156.     }
  157.     
  158.     TRACE("Connected\n") ;
  159.     
  160.     habNetwork = WinInitialize(0) ;
  161.     hmqNetwork = WinCreateMsgQueue(habNetwork, 0) ;
  162.     
  163.     if (protoConnInit() != TRUE) {
  164.     WinDestroyMsgQueue(hmqNetwork) ;
  165.     WinTerminate(habNetwork) ;
  166.         doneThread = TRUE ;
  167.     return ;
  168.     }
  169.     if (protoSendFmtEnc() != TRUE) {
  170.     WinDestroyMsgQueue(hmqNetwork) ;
  171.     WinTerminate(habNetwork) ;
  172.         doneThread = TRUE ;
  173.         return ;
  174.     }
  175.     if (protoSendRequest(FALSE, NULL) != TRUE) {
  176.     WinDestroyMsgQueue(hmqNetwork) ;
  177.     WinTerminate(habNetwork) ;
  178.         doneThread = TRUE ;
  179.         return ;
  180.     }
  181.  
  182.     while (stopThread == FALSE) {
  183.         if (protoDispatch() != TRUE) {
  184.         break ;
  185.     }
  186.     }
  187.  
  188.     (*VncCtx->rectDone) () ;
  189.  
  190.     WinDestroyMsgQueue(hmqNetwork) ;
  191.     WinTerminate(habNetwork) ;
  192.     doneThread = TRUE ;
  193.     
  194.     return ;
  195. }
  196.  
  197. /*
  198.  * netStartup - startup networking thread
  199.  */
  200.  
  201. #define STKSIZ  (1024 * 64)     /* 32KB caused stack overflow */
  202.  
  203. BOOL    netStartup(HAB hab)
  204. {
  205.     if (_beginthread(netThread, NULL, STKSIZ, NULL) < 0) {
  206.         netFail("failed to start network thread") ;
  207.         return FALSE ;
  208.     }
  209.     return TRUE ;
  210. }
  211.  
  212. /*
  213.  * netFinish - terminate networking thread
  214.  */
  215.  
  216. void    netFinish(HAB hab)
  217. {
  218.     int     retry ;
  219.     
  220.     stopThread = TRUE ;
  221.     close(ServerSock) ;
  222.     
  223.     for (retry = 100 ; doneThread == FALSE && retry > 0 ; retry--) {
  224.         DosSleep(50) ;
  225.     }
  226.     return ;
  227. }
  228.