home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Mib / CRUECHAT.ZIP / CRUESERV.ASM < prev    next >
Assembly Source File  |  1998-12-11  |  8KB  |  391 lines

  1. .386
  2. locals
  3. jumps
  4. .model flat,STDCALL
  5.  
  6. include win32.inc
  7. include wsock32.inc
  8.  
  9. ;---------------------------------------------------------------------
  10.  
  11. L equ <LARGE>
  12.  
  13. WM_LISTSOCKET EQU WM_USER+100
  14.  
  15. IDD_TOTUSER      EQU 1000
  16. IDD_EXIT      EQU 1003
  17. IDI_ICON1     EQU 100
  18.  
  19. TRUE          EQU 1
  20. FALSE         EQU 0
  21. NULL          EQU 0
  22.  
  23. ;-----------
  24.  
  25. .data
  26.  
  27. szTitleName      db 'CrueChat 1.1 - Server version',0
  28. msg              MSGSTRUCT   <?>
  29. wc               WNDCLASS    <?>
  30. wsadata          WSAdata <?> 
  31. sin              sockaddr_in <?>
  32.  
  33. szNULL   db      0
  34. ymenu    dd      0
  35.  
  36. hInst            dd 0
  37. hMenu            dd 0
  38. hDlg             dd 0
  39.  
  40. dlg_start        db 'DLG_START',0
  41.  
  42. sockdes             db 4000 dup (0)                ;Can handle 1000 (1000*4 = 4000) connections.
  43. listsock         dd 0
  44. PORT             dd 1024
  45.  
  46. Connections         dd 0
  47. Connectionsstr     db '0',5 dup (0)
  48.  
  49. ReadBuffer         db 256 dup (0)
  50. Hex2Dec             db '%-4i',0
  51. temp             dd 0
  52. LengthOfString     dd 0
  53.  
  54. .code
  55.  
  56. ;---------------------------------------------------------
  57. ; This is where control is received from the loader.
  58. ;---------------------------------------------------------
  59.  
  60.  
  61. start:
  62.         push    0
  63.         call    GetModuleHandle         
  64.         mov     [hInst], eax            
  65.  
  66.         push    offset wsadata                    ;Set things up
  67.         push    0101h
  68.         call    WSAStartup
  69.  
  70.         mov     sin.sin_family, AF_INET         ;Convert the PORT
  71.         push    PORT
  72.         call    htons
  73.         mov     sin.sin_port,ax         
  74.  
  75.         push    offset szTitleName                ;We dont want more than one copy of the server program running
  76.         push    0
  77.         call    FindWindow              
  78.         or      eax,eax                 
  79.         jz      reg_class               
  80.         ret
  81.  
  82. reg_class:
  83.  
  84.         mov     [wc.clsStyle], CS_HREDRAW + CS_VREDRAW + CS_GLOBALCLASS
  85.         mov     [wc.clsLpfnWndProc], offset WndProc
  86.         mov     [wc.clsCbClsExtra], 0
  87.         mov     [wc.clsCbWndExtra], 0
  88.  
  89.         mov     eax, [hInst]
  90.         mov     [wc.clsHInstance], eax
  91.  
  92.         push    IDI_ICON1
  93.         push     eax
  94.         call    LoadIcon
  95.  
  96.         mov     [wc.clsHIcon], eax
  97.  
  98.         push    L IDC_ARROW             
  99.         push    L 0
  100.         call    LoadCursor
  101.         mov     [wc.clsHCursor], eax
  102.  
  103.         push    0                   
  104.         push    offset Main_DlgProc
  105.         push    0
  106.         push    offset dlg_start
  107.         push    [hInst]
  108.         call    DialogBoxParamA
  109.         jmp     finish
  110.  
  111. msg_loop:
  112.         push    L 0
  113.         push    L 0
  114.         push    L 0
  115.         push    offset msg
  116.         call    GetMessage
  117.  
  118.         cmp     ax, 0
  119.         je      end_loop
  120.  
  121.         push    offset msg
  122.         call    TranslateMessage
  123.  
  124.         push    offset msg
  125.         call    DispatchMessage
  126.  
  127.         jmp     msg_loop
  128.  
  129. end_loop:
  130.  
  131.         push    [msg.msWPARAM]
  132.         call    ExitProcess             
  133.  
  134. ;----------------------------------------------------------------------------
  135. ; In this part we will put the events we want to respond on.
  136. ; Key, MouseButtons, Resize, Menu, Commands.... (look at the win32.inc)
  137. ; You can leave the above without changes a modify the next part only
  138. ;----------------------------------------------------------------------------
  139.  
  140. WndProc proc hwnd:DWORD, wmsg:DWORD, wparam:DWORD, lparam:DWORD
  141.         push    esi
  142.         push    edi
  143.         push    ebx
  144.         jmp     defwndproc
  145. defwndproc:
  146.         push    [lparam]
  147.         push    [wparam]
  148.         push    [wmsg]
  149.         push    [hwnd]
  150.         call    DefWindowProc
  151.         jmp     finish
  152.  
  153. finish:
  154.         pop     ebx
  155.         pop     edi
  156.         pop     esi
  157.         ret
  158. WndProc endp
  159.  
  160. public WndProc
  161.  
  162.  
  163. ;----------------------
  164. ;Main Dialog
  165. ;----------------------
  166.  
  167. Main_DlgProc proc hwnd:DWORD, wmsg:DWORD, wparam:DWORD, lparam:DWORD
  168.  
  169.         push    ebx
  170.         push    esi
  171.         push    edi
  172.  
  173.         cmp        [wmsg],WM_LISTSOCKET
  174.         je        socket_event
  175.         cmp     [wmsg],WM_COMMAND
  176.         je      main_command
  177.         cmp        [wmsg],WM_INITDIALOG
  178.         je        main_init
  179.         cmp     [wmsg], WM_CLOSE
  180.         je      main_dlgdestroy
  181.         mov     eax,FALSE
  182.  
  183. main_finish:
  184.         pop     edi
  185.         pop     esi
  186.         pop     ebx
  187.         ret
  188.  
  189. socket_event:
  190.         mov        eax,lparam                        ;FD_ACCEPT?
  191.         cmp        ax,FD_ACCEPT
  192.         jne        go_on
  193.         shr     eax,16
  194.  
  195.         xor        ebx,ebx
  196.         call    CheckSocketList                    ;Find the next empty spot in our SocketHandle array to save away this new SocketHandle
  197.         
  198.         push    0
  199.         push    0
  200.         push    [listsock]
  201.         call    accept                            ;Accept the connection
  202.         inc        Connections
  203.         mov        [edi],eax
  204.  
  205.         push     offset Connections
  206.         push     offset Hex2Dec
  207.         push     offset Connectionsstr
  208.         call     wvsprintfA
  209.     
  210.         push    offset Connectionsstr
  211.         push    IDD_TOTUSER
  212.         push    [hwnd]
  213.         call    SetDlgItemTextA
  214.         jmp        go_on
  215.  
  216. go_on:
  217.  
  218.         mov        eax,lparam                        ;FD_READ?
  219.         cmp        ax,FD_READ
  220.         jne        go_on2
  221.  
  222.         mov        eax,wparam
  223.         xor        ebx,ebx
  224.         mov        bx,ax
  225.  
  226.         mov        temp,ebx                        ;temp holds the SocketHandle of the Client program we're recieving from
  227.  
  228.         push    0
  229.         push    256
  230.         push    offset ReadBuffer
  231.         push    ebx
  232.         call    recv
  233.         cmp     eax,-1
  234.         je        go_on2
  235.  
  236.         lea        esi,ReadBuffer
  237.         lea        edi,ReadBuffer
  238.  
  239.         mov        ecx,eax
  240. XorLoop:                                        ;Our MEGA-COOL encryption algorithm
  241.         xor        byte ptr [edi],19
  242.         inc        edi
  243.         loop    XorLoop
  244.  
  245. ;        lea        edi,ReadBuffer                    ;Add a linefeed
  246. ;        add        edi,eax
  247. ;        mov        byte ptr [edi],13
  248. ;        mov        byte ptr [edi+1],10
  249.  
  250.         lea        edi,sockdes                        ;Edi points to the array of connected SocketHandles.
  251. ;        add        eax,2
  252.         mov        LengthOfString,eax
  253.  
  254.         mov        ebx,temp
  255.         
  256. SendMore:
  257.         cmp        dword ptr [edi],ebx                ;Is this the SocketHandle we got the info from?
  258.         jne        PerhapsSend                        ;If not, continue
  259.         add        edi,4                            ;Dont send the string to the SocketHandle we got it from
  260.         jmp        SendMore
  261. PerhapsSend:
  262.         cmp        dword ptr [edi],0                ;Have we processed all the SocketHandle's`?
  263.         je        Senteverything                    ;If so, then we're done
  264.  
  265.         push    0                                ;Else, send the string to the SocketHandle that EDI points to
  266.         push    LengthOfString
  267.         push    offset ReadBuffer
  268.         push    dword ptr [edi]
  269.         call    send        
  270.  
  271.         add        edi,4
  272.         jmp        SendMore
  273.  
  274. Senteverything:
  275.  
  276.         mov        al,0                            ;Clear the string
  277.         mov        ecx,256
  278.         lea        edi,ReadBuffer
  279.         rep        stosb
  280.  
  281. go_on2:
  282.  
  283.         cmp        ax,FD_CLOSE                        ;FD_CLOSE msg?
  284.         jne        go_on3
  285.  
  286.         mov        eax,wparam
  287.         xor        ebx,ebx
  288.         mov        bx,ax
  289.  
  290.         call    CheckSocketList                    ;Get where in the SocketHandle array the current Handle is.
  291.         mov        dword ptr [edi],0                ;Remove it from the list!
  292.  
  293.         push    ebx                                ;Close down the socket
  294.         call    closesocket
  295.         dec        Connections
  296.  
  297.         push     offset Connections
  298.         push     offset Hex2Dec
  299.         push     offset Connectionsstr
  300.         call     wvsprintfA
  301.     
  302.         push    offset Connectionsstr
  303.         push    IDD_TOTUSER
  304.         push    [hwnd]
  305.         call    SetDlgItemTextA
  306.  
  307. go_on3:
  308.  
  309.         mov        eax,TRUE
  310.         jmp        main_finish
  311.  
  312. main_init:
  313.  
  314.         push    0                                ;Create the socket that will listen for incoming connections
  315.         push    SOCK_STREAM
  316.         push    PF_INET
  317.         call    socket
  318.         mov        listsock,eax
  319.  
  320.         push    FD_ACCEPT+FD_READ+FD_CLOSE         ;The msg's we want to respond on
  321.         push    WM_LISTSOCKET
  322.         push    [hwnd]
  323.         push    [listsock]
  324.         call    WSAAsyncSelect
  325.  
  326.         push    16                                ;We must bind this socket in order to use if for listening purposes
  327.         push    offset sin
  328.         push    [listsock]
  329.         call    bind
  330.  
  331.         push    2                                ;Start listening
  332.         push    [listsock]
  333.         call    listen
  334.  
  335.         push    offset Connectionsstr
  336.         push    IDD_TOTUSER
  337.         push    [hwnd]
  338.         call    SetDlgItemTextA
  339.  
  340.         mov        eax,TRUE
  341.         jmp        main_finish
  342.  
  343. main_command:
  344.  
  345.         cmp        [wparam],IDD_EXIT
  346.         jne     main_nothing
  347.  
  348. main_dlgdestroy:
  349.  
  350.         push    [listsock]                        ;Close the listening socket
  351.         call    closesocket
  352.  
  353.         lea        edi,sockdes                        ;Close all the connected sockets as well
  354. CheckOpenSockets:
  355.         cmp        dword ptr [edi],0
  356.         je        ClosedAllSockets
  357.  
  358.         push    edi
  359.         call    closesocket
  360.         add        edi,4
  361.         jmp        CheckOpenSockets
  362.  
  363. ClosedAllSockets:
  364.  
  365.         call    WSACleanup                        ;Clean up the mess we created
  366.  
  367.         push    L 0             
  368.         push    [hwnd]
  369.         call    EndDialog
  370.         mov     eax,TRUE        
  371.         jmp     main_finish
  372.  
  373. main_nothing:
  374.         mov     eax,FALSE
  375.         jmp     main_finish
  376.  
  377. Main_DlgProc    endp
  378.  
  379. ;*** EXTERNAL PROCEDURES ***
  380.  
  381. CheckSocketList:
  382.         lea        edi,sockdes-4
  383. CheckMoreSocket:
  384.         add        edi,4
  385.         cmp        [edi],ebx
  386.         jne        CheckMoreSocket
  387. ret
  388.  
  389. public Main_DlgProc
  390. end start
  391.