home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / ftp_srv2 / ftp_srv.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-12-17  |  15.1 KB  |  474 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "FTP SERVER"
  5.    ClientHeight    =   4650
  6.    ClientLeft      =   1470
  7.    ClientTop       =   2820
  8.    ClientWidth     =   7335
  9.    Height          =   5055
  10.    Icon            =   FTP_SRV.FRX:0000
  11.    Left            =   1410
  12.    LinkTopic       =   "Form1"
  13.    ScaleHeight     =   4650
  14.    ScaleWidth      =   7335
  15.    Top             =   2475
  16.    Width           =   7455
  17.    Begin TextBox Text1 
  18.       Enabled         =   0   'False
  19.       Height          =   612
  20.       Left            =   120
  21.       MultiLine       =   -1  'True
  22.       TabIndex        =   4
  23.       Text            =   "If you want any explanation or wish my collaboration for any project, contact me through e-mail at UNISYSTEM@DNS.OMNIA.IT (refer to Anastasi Lorenzo)."
  24.       Top             =   3960
  25.       Width           =   7092
  26.    End
  27.    Begin PictureBox EndCmd 
  28.       BackColor       =   &H000000FF&
  29.       Height          =   1000
  30.       Left            =   0
  31.       ScaleHeight     =   975
  32.       ScaleWidth      =   975
  33.       TabIndex        =   0
  34.       Top             =   0
  35.       Width           =   1000
  36.    End
  37.    Begin Timer Timer1 
  38.       Enabled         =   0   'False
  39.       Index           =   4
  40.       Interval        =   50
  41.       Left            =   2040
  42.       Top             =   4680
  43.    End
  44.    Begin Timer Timer1 
  45.       Enabled         =   0   'False
  46.       Index           =   3
  47.       Interval        =   50
  48.       Left            =   1560
  49.       Top             =   4680
  50.    End
  51.    Begin Timer Timer1 
  52.       Enabled         =   0   'False
  53.       Index           =   2
  54.       Interval        =   50
  55.       Left            =   1080
  56.       Top             =   4680
  57.    End
  58.    Begin Timer Timer1 
  59.       Enabled         =   0   'False
  60.       Index           =   1
  61.       Interval        =   50
  62.       Left            =   600
  63.       Top             =   4680
  64.    End
  65.    Begin Timer Timer1 
  66.       Enabled         =   0   'False
  67.       Index           =   0
  68.       Interval        =   50
  69.       Left            =   120
  70.       Top             =   4680
  71.    End
  72.    Begin PictureBox StatusBar 
  73.       BackColor       =   &H000000FF&
  74.       Height          =   1000
  75.       Left            =   0
  76.       ScaleHeight     =   975
  77.       ScaleWidth      =   975
  78.       TabIndex        =   2
  79.       Top             =   0
  80.       Width           =   1000
  81.    End
  82.    Begin PictureBox Frame3D1 
  83.       BackColor       =   &H000000FF&
  84.       Height          =   1000
  85.       Left            =   0
  86.       ScaleHeight     =   975
  87.       ScaleWidth      =   975
  88.       TabIndex        =   3
  89.       Top             =   0
  90.       Width           =   1000
  91.       Begin ListBox LogWnd 
  92.          BackColor       =   &H00000000&
  93.          ForeColor       =   &H0000FF00&
  94.          Height          =   2328
  95.          Left            =   240
  96.          TabIndex        =   1
  97.          Top             =   360
  98.          Width           =   6612
  99.       End
  100.    End
  101.    Begin PictureBox VBServer1 
  102.       BackColor       =   &H000000FF&
  103.       Height          =   1000
  104.       Left            =   0
  105.       ScaleHeight     =   975
  106.       ScaleWidth      =   975
  107.       TabIndex        =   5
  108.       Top             =   0
  109.       Width           =   1000
  110.    End
  111. Sub EndCmd_Click ()
  112. Dim i As Integer
  113.   'close all connection
  114.   For i = 1 To MAX_N_USERS
  115.     If users(i).control_slot <> INVALID_SOCKET Then
  116.       'close control slot
  117.       retf = CloseTheSocket(users(i).control_slot)
  118.     End If
  119.     If users(i).data_slot <> INVALID_SOCKET Then
  120.       'close data slot
  121.       retf = CloseTheSocket(users(i).data_slot)
  122.     End If
  123.   Next
  124.   'end FTP server
  125.   retf = WSACleanup()
  126.   End
  127. End Sub
  128. Sub Form_Load ()
  129. Dim i As Integer
  130. Dim hdr As String, item As String
  131.   '--- Initialization
  132.   'an FTP command is terminated by Carriage_Return +
  133.   'Line_Feed
  134.   crlf = Chr$(13) + Chr$(10)
  135.   'possible sintax errors in FTP commands
  136.   sintax_error_list(0) = "200 Command Ok."
  137.   sintax_error_list(1) = "202 Command not implemented, superfluous at this site."
  138.   sintax_error_list(2) = "500 Sintax error, command unrecognized."
  139.   sintax_error_list(3) = "501 Sintax error in parameters or arguments."
  140.   sintax_error_list(4) = "502 Command not implemented."
  141.   sintax_error_list(6) = "504 Command not implemented for that parameter."
  142.   'initializes the list which contains the names,
  143.   'passwords, access rights and default directory
  144.   'recognized by the server
  145.   Open WORK_DIR + "\ftp_srv.ini" For Input As NF_INI
  146.   Line Input #NF_INI, hdr  'usernames
  147.   If hdr = "[usernames]" Then
  148.     For i = 1 To N_RECOGNIZED_USERS
  149.       Line Input #NF_INI, item
  150.       usernames_list(i) = item
  151.     Next
  152.   Else
  153.     StatusBar.Caption = "Error in INI file!"
  154.     End
  155.   End If
  156.   Line Input #NF_INI, hdr  'passwords
  157.   If hdr = "[passwords]" Then
  158.     For i = 1 To N_RECOGNIZED_USERS
  159.       Line Input #NF_INI, item
  160.       passwords_list(i) = item
  161.     Next
  162.   Else
  163.     StatusBar.Caption = "Error in INI file!"
  164.     End
  165.   End If
  166.   Line Input #NF_INI, hdr  'access rights
  167.   If hdr = "[access_rights]" Then
  168.     For i = 1 To N_RECOGNIZED_USERS
  169.       Line Input #NF_INI, item
  170.       access_rights_list(i) = item
  171.     Next
  172.   Else
  173.     StatusBar.Caption = "Error in INI file!"
  174.     End
  175.   End If
  176.   Line Input #NF_INI, hdr  'default directories
  177.   If hdr = "[default_dirs]" Then
  178.     For i = 1 To N_RECOGNIZED_USERS
  179.       Line Input #NF_INI, item
  180.       default_dir_list(i) = item
  181.     Next
  182.   Else
  183.     StatusBar.Caption = "Error in INI file!"
  184.     End
  185.   End If
  186.   Close #NF_INI
  187.   'initializes the records which contain the
  188.   'informations on the connected users
  189.   For i = 1 To MAX_N_USERS
  190.     users(i).list_index = 0
  191.     users(i).control_slot = INVALID_SLOT
  192.     users(i).data_slot = INVALID_SLOT
  193.     users(i).IP_address = ""
  194.     users(i).Port = 0
  195.     users(i).data_representation = "A"
  196.     users(i).data_format_ctrls = "N"
  197.     users(i).data_structure = "F"
  198.     users(i).data_tx_mode = "S"
  199.     users(i).cur_dir = ""
  200.     users(i).state = 0
  201.     users(i).full = False
  202.   Next
  203.   'begins SERVER mode on port 21
  204.   VBServer1.Port = 21
  205.   VBServer1.SelectAction = FD_ACCEPT Or FD_CLOSE Or FD_READ Or FD_WRITE
  206.   VBServer1.OpenFlag = True
  207.   ServerSlot = VBServer1.SocketNumber
  208. End Sub
  209. Sub Form_Unload (Cancel As Integer)
  210.   retf = WSACleanup()
  211. End Sub
  212. Sub Timer1_Timer (index As Integer)
  213. Dim close_data_cnt As Integer
  214. Dim error_on_data_cnt As Integer
  215. Select Case files_info(index).retr_stor
  216.   Case 0:
  217.   '--- R E T R  Command
  218.   If files_info(index).data_representation = "A" Then
  219.     If Not files_info(index).open_file Then
  220.       'open file
  221.       Open files_info(index).full_name For Input Lock Write As #index
  222.       files_info(index).open_file = True
  223.     End If
  224.     'sends the file on data connection;
  225.     'data are sent a line at a time
  226.     If files_info(index).try_again Then
  227.       're-send old line
  228.     Else
  229.       Line Input #index, files_info(index).buffer
  230.     End If
  231.     retf = send_data(files_info(index).buffer + crlf, index)
  232.     If retf < 0 Then 'SOCKET_ERROR
  233.       retf = WSAGetLastError()
  234.       If retf = WSAEWOULDBLOCK Then
  235.         files_info(index).try_again = True
  236.       Else
  237.         'error on sending
  238.         error_on_data_cnt = True
  239.         close_data_cnt = True
  240.       End If
  241.     Else
  242.       files_info(index).try_again = False
  243.     End If
  244.     If EOF(index) Then close_data_cnt = True
  245.   Else  'binary transfer
  246.     If Not files_info(index).open_file Then
  247.       'open file
  248.       Open files_info(index).full_name For Binary Lock Write As #index
  249.       files_info(index).open_file = True
  250.     End If
  251.     'sends file on data connection;
  252.     'data are sent in blocks of 1024 bytes
  253.     If files_info(index).next_block = 0 Then
  254.       files_info(index).file_len = LOF(index)
  255.       '# of blocks
  256.       files_info(index).blocks = Int(files_info(index).file_len / 1024)
  257.       '# of remaining bytes
  258.       files_info(index).spare_bytes = files_info(index).file_len Mod 1024
  259.       files_info(index).buffer = String$(1024, " ")
  260.     End If
  261.     If files_info(index).next_block < files_info(index).blocks Then
  262.       'sends blocks
  263.       Get #index, files_info(index).next_byte + 1, files_info(index).buffer
  264.       retf = send_data(files_info(index).buffer, index)
  265.       If retf < 0 Then
  266.         retf = WSAGetLastError()
  267.         If retf = WSAEWOULDBLOCK Then
  268.           'try again
  269.         Else
  270.           error_on_data_cnt = True
  271.           close_data_cnt = True
  272.         End If
  273.       Else
  274.         'next block
  275.         files_info(index).next_block = files_info(index).next_block + 1
  276.         files_info(index).next_byte = files_info(index).next_byte + 1024
  277.       End If
  278.     Else
  279.       'sends remaining bytes
  280.       files_info(index).buffer = String$(files_info(index).spare_bytes, " ")
  281.       Get #index, , files_info(index).buffer
  282.       retf = send_data(files_info(index).buffer, index)
  283.       close_data_cnt = True
  284.     End If
  285.   End If
  286.   If close_data_cnt Then
  287.     're-initialize files_info record
  288.     files_info(index).open_file = False
  289.     files_info(index).next_block = 0 'blocks count
  290.     files_info(index).next_byte = 0 'pointer to next block
  291.     files_info(index).try_again = False
  292.     'close file
  293.     Close #index
  294.     'replies to user
  295.     If error_on_data_cnt Then
  296.       retf = send_reply("550 RETR command not executed.", index)
  297.     Else
  298.       retf = send_reply("226 RETR command completed.", index)
  299.     End If
  300.     'close data connection
  301.     retf = close_data_connect(index)
  302.     'disables timer
  303.     timer1(index).Enabled = False
  304.   End If
  305.   Case 1:
  306.   '--- S T O R  Command
  307.   If files_info(index).data_representation = "A" Then
  308.     If Not files_info(index).open_file Then
  309.       'open file
  310.       Open files_info(index).full_name For Output Lock Read Write As #index
  311.       files_info(index).open_file = True
  312.     End If
  313.     'receives file on data connection;
  314.     'data are received a line at a time
  315.     retf = receive_data(files_info(index).buffer, index)
  316.     If retf < 0 Then 'SOCKET_ERROR
  317.       retf = WSAGetLastError()
  318.       If retf = WSAEWOULDBLOCK Then
  319.         'try_again
  320.       Else
  321.         'error on receiving
  322.         error_on_data_cnt = True
  323.         close_data_cnt = True
  324.       End If
  325.     ElseIf retf = 0 Then
  326.       'connection closed by peer
  327.       close_data_cnt = True
  328.     Else 'retf > 0
  329.       'write on file
  330.       dummy$ = Left$(files_info(index).buffer, retf)
  331.       Print #index, dummy$
  332.     End If
  333.   Else  'binary transfer
  334.     If Not files_info(index).open_file Then
  335.       'open file
  336.       Open files_info(index).full_name For Binary Lock Read Write As #index
  337.       files_info(index).open_file = True
  338.     End If
  339.     'receives file on data connection;
  340.     retf = receive_data(files_info(index).buffer, index)
  341.     If retf < 0 Then
  342.       retf = WSAGetLastError()
  343.       If retf = WSAEWOULDBLOCK Then
  344.         'try again
  345.       Else
  346.         error_on_data_cnt = True
  347.         close_data_cnt = True
  348.       End If
  349.     ElseIf retf = 0 Then
  350.       'connection closed by peer
  351.       close_data_cnt = True
  352.     Else
  353.       dummy$ = Left$(files_info(index).buffer, retf)
  354.       Put #index, , dummy$
  355.     End If
  356.   End If
  357.   If close_data_cnt Then
  358.     're-initialize files_info record
  359.     files_info(index).open_file = False
  360.     files_info(index).next_block = 0 'blocks count
  361.     files_info(index).next_byte = 0 'pointer to next block
  362.     files_info(index).try_again = False
  363.     'close file
  364.     Close #index
  365.     'replies to user
  366.     If error_on_data_cnt Then
  367.       retf = send_reply("550 STOR command not executed.", index)
  368.     Else
  369.       retf = send_reply("226 STOR command completed.", index)
  370.     End If
  371.     'closes data connection
  372.     retf = close_data_connect(index)
  373.     'disables timer
  374.     timer1(index).Enabled = False
  375.   End If
  376. End Select
  377. End Sub
  378. Sub VBServer1_Message (MsgVal As Integer, wparam As Integer, lparam As Long)
  379. Dim NewSlot As Integer
  380. Dim SendBuffer As String
  381. Dim lenBuffer As Integer 'send-buffer lenght
  382. Dim RecvBuffer As String
  383. Dim BytesRead As Integer 'receive-buffer lenght
  384. Dim i As Integer
  385. Dim fixstr As String * 1024
  386.   'event on server slot
  387.   If wparam = ServerSlot Then
  388.     retf = GetSelectEventSocket(lparam)
  389.     '--- FD_ACCEPT
  390.     If retf = FD_ACCEPT Then
  391.       'try to accept new TCP connection
  392.       NewSlot = acceptSocket(ServerSlot)
  393.       If NewSlot = INVALID_SOCKET Then
  394.         msg$ = "Error during an attempt at connection."
  395.         StatusBar.Caption = msg$
  396.       Else 'NewSlot OK
  397.         'new service request
  398.         If num_users >= MAX_N_USERS Then
  399.           'the number of users exceeds the
  400.           'maximum allowed
  401.           SendBuffer = "421 Service not available at this time, closing control connection." + crlf
  402.           lenBuffer = Len(SendBuffer)
  403.           retf = SendSocket(NewSlot, SendBuffer, lenBuffer, 0)
  404.           'close connection
  405.           retf = CloseTheSocket(NewSlot)
  406.         Else
  407.                        SendBuffer = "220-Welcome in this demo site!" + crlf
  408.           SendBuffer = SendBuffer + "220-The software implementing this FTP is entirely realized in VB 3.0" + crlf
  409.           SendBuffer = SendBuffer + "220-You must consider the packet as a demo version only!" + crlf
  410.           SendBuffer = SendBuffer + "220 Have a good time ... (L.Anastasi)" + crlf
  411.           lenBuffer = Len(SendBuffer)
  412.           'send welcome message
  413.           retf = SendSocket(NewSlot, SendBuffer, lenBuffer, 0)
  414.           'increases the number of connected users
  415.           num_users = num_users + 1
  416.           'registers the slot number in the first
  417.           'free user record
  418.           For i = 1 To MAX_N_USERS
  419.             If Not users(i).full Then
  420.               users(i).control_slot = NewSlot
  421.               users(i).full = True
  422.               Exit For
  423.             End If
  424.           Next
  425.         End If  'If num_users
  426.       End If  'If NewSlot
  427.     End If  'If retf
  428.     Exit Sub
  429.   End If  'If wparam
  430.   'event on control slots
  431.   For i = 1 To MAX_N_USERS
  432.     If wparam = users(i).control_slot Then
  433.       retf = GetSelectEventSocket(lparam)
  434.       '--- FD_READ
  435.       If retf = FD_READ Then
  436.         'store read bytes in RecvBuffer
  437.         BytesRead = RecvSocket(wparam, fixstr, 1024, 0)
  438.         RecvBuffer = Left$(fixstr, BytesRead)
  439.         'if received string is a command then
  440.         'executes it
  441.         If InStr(RecvBuffer, crlf) > 0 Then
  442.           retf = exec_FTP_cmd(i, RecvBuffer)
  443.         End If
  444.       '--- FD_CLOSE
  445.       ElseIf retf = FD_CLOSE Then
  446.         'connection closed by client
  447.         retf = CloseTheSocket(wparam)
  448.         'frees the user record
  449.         users(i).control_slot = INVALID_SOCKET
  450.         users(i).full = False
  451.       End If
  452.       Exit Sub
  453.     End If
  454.   Next
  455.   'event on data slots
  456.   For i = 1 To MAX_N_USERS
  457.     If wparam = users(i).data_slot Then
  458.       retf = GetSelectEventSocket(lparam)
  459.       '--- FD_WRITE
  460.       If retf = FD_WRITE Then
  461.         'enables sending
  462.       '--- FD_CLOSE
  463.       ElseIf retf = FD_CLOSE Then
  464.         'connection closed by client
  465.         retf = CloseTheSocket(wparam)
  466.         'reinitilizes data slot
  467.         users(i).data_slot = INVALID_SOCKET
  468.         users(i).state = 2
  469.       End If
  470.       Exit Sub
  471.     End If
  472.   Next
  473. End Sub
  474.