⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesvr.cxx

📁 这个是微软WINCE的OBEX协议例子
💻 CXX
📖 第 1 页 / 共 5 页
字号:
        ObexVariant aPropVar[1];
        memset (&o, 0, sizeof(o));
        o.uiOp = pOT->pObex->uiOp;
        o.fFinal = TRUE;
        o.uiResp = OBEX_STAT_UNAUTHORIZED;
        o.aPropID = aPropId;
        o.aPropVar = aPropVar;
        o.cProp = 1;
        o.uiResp = OBEX_STAT_UNAUTHORIZED;
        o.aPropID[0] = OBEX_HID_AUTH_CHALLENGE;
        o.aPropVar[0].caub.cuc = 18;
        o.aPropVar[0].caub.puc = pConn->nonce;


        //
        //  Send off the packet and return
        //
        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (OBEX_RESP_RESPOND, pOT->uiTransactionId, &o);    
        EnterCriticalSection (&g_cs);
        return TRUE;
    }

    // 
    //  If the nonce is set, we have simply need to inspect the packet they gave
    //     to make sure it jives with what we are expecting... it not 
    //     reject
    //
    else if(!pConn->fIsAuthenticated && pConn->fNonceSet)
    {
        //
        //  Once they respond with something, invalidate our NONCE
        //
        pConn->fNonceSet = FALSE;
        
        for (int i = 0 ; i < (int)pOT->pObex->cProp ; i++) 
        {
            if (pOT->pObex->aPropID[i] == OBEX_HID_AUTH_RESPONSE)
            {
                //
                //  Find the NONCE sent by the client as a challenge to us
                //
                UINT uiResponseSize = pOT->pObex->aPropVar[i].caub.cuc;
                BYTE *pTempResponse = pOT->pObex->aPropVar[i].caub.puc;
                BYTE *pResponse = NULL;
                    
                //find the nonce sent by the client
                while(uiResponseSize)
                {
                    if(*pTempResponse == 0 && *(pTempResponse + 1) == 0x10)
                    {
                        pResponse = pTempResponse + 2;
                        break;
                    }            
                    uiResponseSize -= (*(pTempResponse + 1)) + 2;
                    pTempResponse += (*(pTempResponse + 1)) + 2;
                }
                pTempResponse = NULL;

                //
                //  If no response, send off an error
                //
                if(!pResponse)
                {                        
                    IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : ERROR!!! recieved a response to our challenge, but it isnt what was expected!\n"));
                    LeaveCriticalSection (&g_cs);
                    pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
                    EnterCriticalSection (&g_cs);
                }
            
                WCHAR szPassword[_MAX_PATH];                
          
                //
                //  Fetch the password from the registry
                if(FAILED(FetchPassword(szPassword, _MAX_PATH)))
                {
                    IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : ERROR!!! recieved a response to our challenge, but it isnt what was expected!\n"));
                    LeaveCriticalSection (&g_cs);
                    pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
                    EnterCriticalSection (&g_cs);
                    return TRUE;
                }

                IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : Get -- building expected response from nonce of:\n"));                
                IFDBG(svslog_DumpBuff (DEBUG_FTP_TRACE, pConn->nonce, 18));
 
                //do the check
                BYTE bExpectedResponse[16];
                if((ERROR_SUCCESS != MakeResponse (szPassword, pConn->nonce, bExpectedResponse)) || (0 != memcmp(pResponse, bExpectedResponse, 16)))
                {
                    IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : ERROR!!! invalid password!!!!!!!!!\n"));
                    LeaveCriticalSection (&g_cs);
                    pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
                    EnterCriticalSection (&g_cs);
                    return TRUE;
                }
                IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : Authenicated!\n"));

                //
                //  Set the all-so-important authenticated flag!
                //
                pConn->fIsAuthenticated = TRUE;                
            }
        
        }
        
    }


    //
    //  now make sure we are authenticated.
    //
    if(!pConn->fIsAuthenticated)
    {
        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : ERROR!!! Not authenticated!!!!!!!!!\n"));
        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
        EnterCriticalSection (&g_cs);
        return TRUE;
    }

    //BUGBUG: for debugging only!  this forces authentication on every packet!
    //pConn->fIsAuthenticated = FALSE;
    //pConn->fNonceSet = FALSE;
    return FALSE;
}




/******************************************************************************/
/*  RecursiveDeleteDirectory -- delete a directory and everything under it    */
/******************************************************************************/
static HRESULT RecursiveDeleteDirectory(LPCTSTR lpPath, LPCTSTR lpBaseDir) 
{
    WCHAR wcPath[MAX_PATH+10];
    WIN32_FIND_DATA wfd; 
    ASSERT(TRUE == CanWrite());
    
    if(NULL == lpPath || NULL == lpBaseDir || 0 == wcslen(lpPath) || 0 == wcslen(lpBaseDir)) {
        ASSERT(FALSE);
        return E_FAIL;
    }
        
    swprintf(wcPath, L"%s\\*.*", lpPath);
    HANDLE hSearch = FindFirstFile (wcPath, &wfd);
    HRESULT hr = S_OK;
    BOOL fNeedSlash = (lpPath[wcslen(lpPath)-1]=='\\')?FALSE:TRUE;
        
    if (INVALID_HANDLE_VALUE == hSearch)
        goto Done;
        
    do {
        if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
            if(0 != wcscmp(wfd.cFileName, lpPath)) {
                WCHAR wcDeleteDir[MAX_PATH];
                swprintf(wcDeleteDir, L"%s%s%s", lpPath, fNeedSlash?L"\\":L"", wfd.cFileName);
                hr = RecursiveDeleteDirectory(wcDeleteDir, lpBaseDir);
            }    
            else
                hr = S_OK;
        }
        else {
            WCHAR wcFullPath[MAX_PATH];
            swprintf(wcFullPath, L"%s%s%s", lpPath, fNeedSlash?L"\\":L"", wfd.cFileName);
            hr = (DeleteFile(wcFullPath)?S_OK:E_FAIL);
        }    
        
        if(FAILED(hr))
            goto Done;            
    } while (FindNextFile (hSearch, &wfd));

    //remove this directory (if its not the base) -- we dont want to remove
    //  our destination directory (dont send back error... this is okay)
    Done:

        if(0 != wcscmp(lpBaseDir, lpPath))
            hr = (RemoveDirectory(lpPath)?S_OK:E_FAIL);
        else 
            hr = S_OK;
        
        if (INVALID_HANDLE_VALUE != hSearch)
            FindClose(hSearch);
            
        return hr;        
}

/******************************************************************************/
/*  ServiceRequst -- the starting function for when a packet arrives          */
/*        it is responsible for sending back appropriate replies no matter    */
/*        what was sent                                                       */
/******************************************************************************/
static int ServiceRequest (ObexTransaction *pOT) {
    IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest\n"));

    if (pOT->pObex->uiOp == (OBEX_OP_CONNECT & OBEX_OP_OPMASK)) {
        return ServiceConnectPacket(pOT);
    }


    //
    //  Search out the FileXFer connection
    //
    FileXFer *pParent = NULL;
    FileXFer *pConn = g_pConn;
    while (pConn) {
        if (pConn->uiConnectionId == pOT->uiConnectionId)
            break;

        pParent = pConn;
        pConn = pConn->pNext;
    }

    if (! pConn) {
        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : op %d : connection not found!\n", pOT->pObex->uiOp));
        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
        EnterCriticalSection (&g_cs);
        return TRUE;
    }



    //
    //   Process DISCONNECT packets
    //
    if (pOT->pObex->uiOp == (OBEX_OP_DISCONNECT & OBEX_OP_OPMASK)) {
        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_DISCONNECT : 0x%08x\n", pConn->uiConnectionId));

        if (! pParent)
            g_pConn = g_pConn->pNext;
        else
            pParent->pNext = pConn->pNext;

        CloseConnection (pOT, pConn);

        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (OBEX_RESP_OK, pOT->uiTransactionId, NULL);
        pOT->ObexExecute (OBEX_RESP_DISCONNECT, pOT->uiConnectionId, NULL);
        EnterCriticalSection (&g_cs);
        return TRUE;
    }

    //
    //   Process SET_PATH packets
    //
    if (pOT->pObex->uiOp == (OBEX_OP_SETPATH & OBEX_OP_OPMASK)) {
        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_SETPATH : 0x%08x\n", pConn->uiConnectionId));
        
        if (pConn->op != NONE) {
            IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : in the middle of transaction in conn 0x%08x\n", pConn->uiConnectionId));
            LeaveCriticalSection (&g_cs);
            pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
            EnterCriticalSection (&g_cs);
            return TRUE;
        }

        //
        //  If HandlePacketAuthentication handled the packet for us, just return TRUE
        //
        if(HandlePacketAuthentication(pOT, pConn))
        {
            IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_SETPATH : packet handled by authentication\n"));
            return TRUE;
        }
        

        if (pOT->pObex->sPktData.SetPathRequest.flags & 1) {    // Backup
            WCHAR *p = wcsrchr (pConn->szCurDir, L'\\');
            if (p)
                *p = NULL;
            else
                pConn->szCurDir[0] = L'\0';

            IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_SETPATH :: %s\\\n", pConn->szCurDir));

            LeaveCriticalSection (&g_cs);
            pOT->ObexExecute (OBEX_RESP_OK, pOT->uiTransactionId, NULL);
            EnterCriticalSection (&g_cs);
            return TRUE;
        }

        WCHAR    *szName = NULL;
        for (int i = 0 ; i < (int)pOT->pObex->cProp ; ++i) {
            if (pOT->pObex->aPropID[i] == OBEX_HID_NAME) {
                szName = pOT->pObex->aPropVar[i].pwsz;

                if (szName[0] == '\0')
                    szName = NULL;
                break;
            }
        }

    
        int iResp = OBEX_RESP_OK;

        if (! szName)
            pConn->szCurDir[0] = '\0';    // Rever to current
        else {
            int iNewName = wcslen (szName);
            int iOldName = wcslen (pConn->szCurDir);
            int iBaseName = wcslen (pConn->szBaseDir);
            if (iNewName + iOldName + iBaseName + 3 > _MAX_PATH) {
                IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_SETPATH :: path too long\n"));
                iResp = OBEX_RESP_REJECT;
            } else if (CheckNameForValidity (szName)) {
                WCHAR szNewDir[MAX_PATH];
                int iStart = 0;
                if (iBaseName > 0) {
                    memcpy (szNewDir, pConn->szBaseDir, sizeof (WCHAR) * iBaseName);
                    iStart += iBaseName;
                    szNewDir[iStart++] = '\\';
                }
                if (iOldName > 0) {
                    memcpy (szNewDir + iStart, pConn->szCurDir, sizeof (WCHAR) * iOldName);
                    iStart += iOldName;
                    szNewDir[iStart++] = '\\';
                }
                if (iStart == 0)
                    szNewDir[iStart++] = L'\\';
                memcpy (szNewDir + iStart, szName, sizeof (WCHAR) * (iNewName + 1));

                IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_SETPATH :: %s\n", szNewDir));


                DWORD dwAttr = GetFileAttributes (szNewDir);
                if (dwAttr == 0xffffffff) {
                    if (pOT->pObex->sPktData.SetPathRequest.flags & 2)    { // dont'c create
                        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_SETPATH :: %s\\ : does not exist - and creating not requested\n", szNewDir));
                        iResp = OBEX_RESP_REJECT;
                    } else if(FALSE == CanWrite()) {  //Check permissions to write... otherwise reject
                        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_SETPATH :: %s\\ : in read-only mode %d\n", szNewDir));
                        iResp = OBEX_RESP_REJECT;
                    } else if (0 == CreateDirectory (szNewDir, NULL)) {
                        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_SETPATH :: %s\\ : does not exist - and creation failed %d\n", szNewDir, GetLastError ()));
                        iResp = OBEX_RESP_REJECT;
                    } else
                        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_SETPATH :: %s\\ : does not exist - created\n", szNewDir));
                } else if ((dwAttr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
                    IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_SETPATH :: %s\\ : not a dir\n", szNewDir));
                    iResp = OBEX_RESP_REJECT;
                }

                if (iResp == OBEX_RESP_OK) { // Put stuff in new directory
                    if (pConn->szCurDir[0] == '\0')
                        memcpy (pConn->szCurDir, szName, sizeof(WCHAR) * (iNewName + 1));
                    else {
                        pConn->szCurDir[iOldName] = '\\';
                        memcpy (pConn->szCurDir + iOldName + 1, szName, sizeof(WCHAR) * (iNewName + 1));
                    }
                }
            } else {
                IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_SETPATH :: %s : invalid name\n", szName));
                iResp = OBEX_RESP_REJECT;
            }
        }

        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (iResp, pOT->uiTransactionId, NULL);
        EnterCriticalSection (&g_cs);
        return TRUE;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -