- added file offset parameter to SendFileOverNXCP
authorAlex Kalimulin <alex@netxms.org>
Sat, 23 May 2009 17:19:59 +0000 (17:19 +0000)
committerAlex Kalimulin <alex@netxms.org>
Sat, 23 May 2009 17:19:59 +0000 (17:19 +0000)
include/nxcpapi.h
src/libnetxms/nxcp.cpp
src/libnxcl/session.cpp
src/server/core/session.cpp
src/server/libnxsrv/agent.cpp

index 8743e79..67756f3 100644 (file)
@@ -193,7 +193,7 @@ CSCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawNXCPMessage(WORD wCode, DWORD dwId,
                                                         CSCP_MESSAGE *pBuffer);
 TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
 BOOL LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, DWORD dwId, const TCHAR *pszFile,
-                                           CSCP_ENCRYPTION_CONTEXT *pCtx);
+                                           CSCP_ENCRYPTION_CONTEXT *pCtx, long offset);
 BOOL LIBNETXMS_EXPORTABLE NXCPGetPeerProtocolVersion(SOCKET hSocket, int *pnVersion);
    
 BOOL LIBNETXMS_EXPORTABLE InitCryptoLib(DWORD dwEnabledCiphers);
index 865680f..2fa0e31 100644 (file)
@@ -451,7 +451,7 @@ CSCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawNXCPMessage(WORD wCode, DWORD dwId,
 //
 
 BOOL LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, DWORD dwId, const TCHAR *pszFile,
-                                           CSCP_ENCRYPTION_CONTEXT *pCtx)
+                                           CSCP_ENCRYPTION_CONTEXT *pCtx, long offset)
 {
 #ifndef UNDER_CE
    int hFile, iBytes;
@@ -467,64 +467,70 @@ BOOL LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, DWORD dwId, const TCH
 #ifndef UNDER_CE
    hFile = _topen(pszFile, O_RDONLY | O_BINARY);
    if (hFile != -1)
+   {
+          if (lseek(hFile, offset, offset < 0 ? SEEK_END : SEEK_SET) == 0)
+          {
 #else
    hFile = _tfopen(pszFile, _T("rb"));
    if (hFile != NULL)
-#endif
    {
-      // Allocate message and prepare it's header
-      pMsg = (CSCP_MESSAGE *)malloc(FILE_BUFFER_SIZE + CSCP_HEADER_SIZE + 8);
-      pMsg->dwId = htonl(dwId);
-      pMsg->wCode = htons(CMD_FILE_DATA);
-      pMsg->wFlags = htons(MF_BINARY);
-      
-      while(1)
-      {
+          if (fseek(hFile, offset, offset < 0 ? SEEK_END : SEEK_SET) == 0)
+          {
+#endif
+                 // Allocate message and prepare it's header
+                 pMsg = (CSCP_MESSAGE *)malloc(FILE_BUFFER_SIZE + CSCP_HEADER_SIZE + 8);
+                 pMsg->dwId = htonl(dwId);
+                 pMsg->wCode = htons(CMD_FILE_DATA);
+                 pMsg->wFlags = htons(MF_BINARY);
+
+                 while(1)
+                 {
 #ifndef UNDER_CE
-         iBytes = read(hFile, pMsg->df, FILE_BUFFER_SIZE);
-         if (iBytes < 0)
-            break;
+                        iBytes = read(hFile, pMsg->df, FILE_BUFFER_SIZE);
+                        if (iBytes < 0)
+                               break;
 #else
-         iBytes = fread(pMsg->df, 1, FILE_BUFFER_SIZE, hFile);
-         if (ferror(hFile))
-                       {
-            break;     // Read error
-                       }
+                        iBytes = fread(pMsg->df, 1, FILE_BUFFER_SIZE, hFile);
+                        if (ferror(hFile))
+                               {
+                               break;  // Read error
+                               }
 #endif
 
-         // Message should be aligned to 8 bytes boundary
-         dwPadding = (8 - (((DWORD)iBytes + CSCP_HEADER_SIZE) % 8)) & 7;
-         pMsg->dwSize = htonl((DWORD)iBytes + CSCP_HEADER_SIZE + dwPadding);
-         pMsg->dwNumVars = htonl((DWORD)iBytes);   // dwNumVars contains actual data size for binary message
-         if (iBytes < FILE_BUFFER_SIZE)
-            pMsg->wFlags |= htons(MF_END_OF_FILE);
-
-         if (pCtx != NULL)
-         {
-            pEnMsg = CSCPEncryptMessage(pCtx, pMsg);
-            if (pEnMsg != NULL)
-            {
-               SendEx(hSocket, (char *)pEnMsg, ntohl(pEnMsg->dwSize), 0);
-               free(pEnMsg);
-            }
-         }
-         else
-         {
-            if (SendEx(hSocket, (char *)pMsg, (DWORD)iBytes + CSCP_HEADER_SIZE + dwPadding, 0) <= 0)
+                        // Message should be aligned to 8 bytes boundary
+                        dwPadding = (8 - (((DWORD)iBytes + CSCP_HEADER_SIZE) % 8)) & 7;
+                        pMsg->dwSize = htonl((DWORD)iBytes + CSCP_HEADER_SIZE + dwPadding);
+                        pMsg->dwNumVars = htonl((DWORD)iBytes);   // dwNumVars contains actual data size for binary message
+                        if (iBytes < FILE_BUFFER_SIZE)
+                               pMsg->wFlags |= htons(MF_END_OF_FILE);
+
+                        if (pCtx != NULL)
+                        {
+                               pEnMsg = CSCPEncryptMessage(pCtx, pMsg);
+                               if (pEnMsg != NULL)
                                {
-                                       break;  // Send error
+                                  SendEx(hSocket, (char *)pEnMsg, ntohl(pEnMsg->dwSize), 0);
+                                  free(pEnMsg);
                                }
-         }
-
-         if (iBytes < FILE_BUFFER_SIZE)
-         {
-            // End of file
-            bResult = TRUE;
-            break;
-         }
-      }
-
-      free(pMsg);
+                        }
+                        else
+                        {
+                               if (SendEx(hSocket, (char *)pMsg, (DWORD)iBytes + CSCP_HEADER_SIZE + dwPadding, 0) <= 0)
+                                       {
+                                               break;  // Send error
+                                       }
+                        }
+
+                        if (iBytes < FILE_BUFFER_SIZE)
+                        {
+                               // End of file
+                               bResult = TRUE;
+                               break;
+                        }
+                 }
+
+                 free(pMsg);
+         }
 #ifndef UNDER_CE
       close(hFile);
 #else
index 8ebca91..bd450e5 100644 (file)
@@ -882,7 +882,7 @@ DWORD NXCL_Session::LoadUserDB(void)
 
 DWORD NXCL_Session::SendFile(DWORD dwRqId, TCHAR *pszFileName)
 {
-   return SendFileOverNXCP(m_hSocket, dwRqId, pszFileName, m_pCtx) ? RCC_SUCCESS : RCC_IO_ERROR;
+   return SendFileOverNXCP(m_hSocket, dwRqId, pszFileName, m_pCtx, 0) ? RCC_SUCCESS : RCC_IO_ERROR;
 }
 
 
index a5bce6e..e7165d1 100644 (file)
@@ -1260,7 +1260,7 @@ void ClientSession::OnFileUpload(BOOL bSuccess)
 
 BOOL ClientSession::SendFile(const TCHAR *file, DWORD dwRqId)
 {
-       return SendFileOverNXCP(m_hSocket, dwRqId, file, m_pCtx);
+       return SendFileOverNXCP(m_hSocket, dwRqId, file, m_pCtx, 0);
 }
 
 
@@ -3486,7 +3486,7 @@ void ClientSession::SendMIB(DWORD dwRqId)
    // Send compiled MIB file
    strcpy(szBuffer, g_szDataDir);
    strcat(szBuffer, DFILE_COMPILED_MIB);
-   SendFileOverNXCP(m_hSocket, dwRqId, szBuffer, m_pCtx);
+   SendFileOverNXCP(m_hSocket, dwRqId, szBuffer, m_pCtx, 0);
 }
 
 
@@ -7449,7 +7449,7 @@ void ClientSession::SendSubmapBkImage(CSCPMessage *pRequest)
    // Send bitmap file
    if (bSuccess)
    {
-      SendFileOverNXCP(m_hSocket, pRequest->GetId(), szBuffer, m_pCtx);
+      SendFileOverNXCP(m_hSocket, pRequest->GetId(), szBuffer, m_pCtx, 0);
    }
 }
 
@@ -10010,7 +10010,7 @@ void ClientSession::GetServerFile(CSCPMessage *pRequest)
                if (_taccess(fname, 0) == 0)
                {
                        DebugPrintf(5, _T("Sending file %s"), name);
-                       if (SendFileOverNXCP(m_hSocket, pRequest->GetId(), fname, m_pCtx))
+                       if (SendFileOverNXCP(m_hSocket, pRequest->GetId(), fname, m_pCtx, 0))
                        {
                                DebugPrintf(5, _T("File %s was succesfully sent"), name);
                      msg.SetVariable(VID_RCC, RCC_SUCCESS);
index 3e1b8b5..63a7f87 100644 (file)
@@ -980,7 +980,7 @@ DWORD AgentConnection::UploadFile(const TCHAR *pszFile)
 
    if (dwResult == ERR_SUCCESS)
    {
-      if (SendFileOverNXCP(m_hSocket, dwRqId, pszFile, m_pCtx))
+      if (SendFileOverNXCP(m_hSocket, dwRqId, pszFile, m_pCtx, 0))
          dwResult = WaitForRCC(dwRqId, m_dwCommandTimeout);
       else
          dwResult = ERR_IO_FAILURE;