VirtualBox

Changeset 82687 in vbox for trunk/src/VBox/Runtime/tools


Ignore:
Timestamp:
Jan 9, 2020 10:45:36 AM (5 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
135606
Message:

IPRT/FTP: More protocol handling and callback work. bugref:9437

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/tools/RTFTPServer.cpp

    r82671 r82687  
    5757*********************************************************************************************************************************/
    5858/** Set by the signal handler when the FTP server shall be terminated. */
    59 static volatile bool g_fCanceled = false;
     59static volatile bool  g_fCanceled  = false;
     60static char          *g_pszRootDir = NULL;
    6061
    6162
     
    145146}
    146147
     148static DECLCALLBACK(int) onUserConnect(PRTFTPCALLBACKDATA pData, const char *pcszUser)
     149{
     150    RT_NOREF(pData, pcszUser);
     151
     152    RTPrintf("User '%s' connected", pcszUser);
     153
     154    return VINF_SUCCESS;
     155}
     156
     157static DECLCALLBACK(int) onUserAuthenticate(PRTFTPCALLBACKDATA pData, const char *pcszUser, const char *pcszPassword)
     158{
     159    RT_NOREF(pData, pcszUser, pcszPassword);
     160
     161    RTPrintf("Authenticating user '%s' ...", pcszUser);
     162
     163    return VINF_SUCCESS;
     164}
     165
     166static DECLCALLBACK(int) onUserDisonnect(PRTFTPCALLBACKDATA pData)
     167{
     168    RT_NOREF(pData);
     169
     170    RTPrintf("User disconnected");
     171
     172    return VINF_SUCCESS;
     173}
     174
     175static DECLCALLBACK(int) onPathSetCurrent(PRTFTPCALLBACKDATA pData, const char *pcszCWD)
     176{
     177    RT_NOREF(pData, pcszCWD);
     178
     179    RTPrintf("Setting current directory to '%s'\n", pcszCWD);
     180
     181    return VINF_SUCCESS;
     182}
     183
     184static DECLCALLBACK(int) onPathGetCurrent(PRTFTPCALLBACKDATA pData, char *pszPWD, size_t cbPWD)
     185{
     186    RT_NOREF(pData, pszPWD, cbPWD);
     187
     188    return VINF_SUCCESS;
     189}
     190
     191static DECLCALLBACK(int) onList(PRTFTPCALLBACKDATA pData, void **ppvData, size_t *pcbData)
     192{
     193    RT_NOREF(pData, ppvData, pcbData);
     194
     195    return VINF_SUCCESS;
     196}
     197
    147198int main(int argc, char **argv)
    148199{
     
    154205    char     szAddress[64]         = "localhost";
    155206    uint16_t uPort                 = 2121;
    156 
    157     /* By default use the current directory as serving root directory. */
    158     char     szRootDir[RTPATH_MAX];
    159     rc = RTPathGetCurrent(szRootDir, sizeof(szRootDir));
    160     if (RT_FAILURE(rc))
    161         return RTMsgErrorExit(RTEXITCODE_FAILURE, "Retrieving current directory failed: %Rrc", rc);
    162207
    163208    /*
     
    190235            case 'p':
    191236                uPort = ValueUnion.u16;
     237                break;
     238
     239            case 'r':
     240                g_pszRootDir = RTStrDup(ValueUnion.psz);
    192241                break;
    193242
     
    224273    }
    225274
     275    if (!g_pszRootDir)
     276    {
     277        char szRootDir[RTPATH_MAX];
     278
     279        /* By default use the current directory as serving root directory. */
     280        rc = RTPathGetCurrent(szRootDir, sizeof(szRootDir));
     281        if (RT_FAILURE(rc))
     282            return RTMsgErrorExit(RTEXITCODE_FAILURE, "Retrieving current directory failed: %Rrc", rc);
     283
     284        g_pszRootDir = RTStrDup(szRootDir);
     285        if (!g_pszRootDir)
     286            return RTMsgErrorExit(RTEXITCODE_FAILURE, "Allocating current directory failed");
     287    }
     288
    226289    /* Install signal handler. */
    227290    rc = signalHandlerInstall();
     
    231294         * Create the FTP server instance.
    232295         */
     296        RTFTPSERVERCALLBACKS Callbacks;
     297        RT_ZERO(Callbacks);
     298        Callbacks.pfnOnUserConnect      = onUserConnect;
     299        Callbacks.pfnOnUserAuthenticate = onUserAuthenticate;
     300        Callbacks.pfnOnUserDisconnect   = onUserDisonnect;
     301        Callbacks.pfnOnPathSetCurrent   = onPathSetCurrent;
     302        Callbacks.pfnOnPathGetCurrent   = onPathGetCurrent;
     303        Callbacks.pfnOnList             = onList;
     304
    233305        RTFTPSERVER hFTPServer;
    234         rc = RTFTPServerCreate(&hFTPServer, szAddress, uPort, szRootDir);
     306        rc = RTFTPServerCreate(&hFTPServer, szAddress, uPort, &Callbacks);
    235307        if (RT_SUCCESS(rc))
    236308        {
    237309            RTPrintf("Starting FTP server at %s:%RU16 ...\n", szAddress, uPort);
    238             RTPrintf("Root directory is '%s'\n", szRootDir);
     310            RTPrintf("Root directory is '%s'\n", g_pszRootDir);
    239311
    240312            RTPrintf("Running FTP server ...\n");
     
    264336    }
    265337
     338    RTStrFree(g_pszRootDir);
     339
    266340    /* Set rcExit on failure in case we forgot to do so before. */
    267341    if (RT_FAILURE(rc))
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette