Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.96 KB | None | 0 0
  1. /*
  2.     March 28, 2013 (Thursday)
  3.     Module: DxP.c used to create DxP.dll
  4.     Description:  This module implements the DxP communications protocol.  
  5. */
  6.  
  7. ///// INCLUDES
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <winsock2.h>
  12. #include <windows.h>
  13. #include "DxP.h"
  14. #include "dxp_common.h"
  15.  
  16. #if defined(__cplusplus)
  17. #define EXPORTED extern "C" __declspec( dllexport )
  18. #else
  19. #define EXPORTED __declspec( dllexport )
  20. #endif
  21.  
  22. ///// GLOBALS
  23.  
  24.  
  25. ///// STATIC GLOBALS (local to file)
  26.  
  27.  
  28. ///// STATIC FUNCTION PROTOTYPES (optional)
  29.  
  30.  
  31. ///// STATIC FUNCTION DEFINITIONS (optional)
  32.  
  33.  
  34. ///// FUNCTION DEFINITIONS
  35. /* Error checking should be performed in following code */
  36. BOOL APIENTRY LibMain(HANDLE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
  37. {
  38.  
  39.     switch(fdwReason) {
  40.  
  41.         case DLL_PROCESS_ATTACH:
  42.             /* do process initialization */        
  43.  
  44.         break;
  45.  
  46.         case DLL_THREAD_ATTACH:
  47.             /* do thread initialization */         
  48.  
  49.         break;
  50.  
  51.         case DLL_THREAD_DETACH:
  52.             /* do thread cleanup */        
  53.  
  54.         break;
  55.  
  56.         case DLL_PROCESS_DETACH:
  57.             /* do process cleanup */           
  58.  
  59.         break;
  60.  
  61.     } //end switch
  62.  
  63.     return(1);        /* indicate success */
  64.  
  65.     /* returning 0 indicates initialization failure */
  66.  
  67. }
  68.  
  69. EXPORTED int DxP(char *ipAddress, int ipPort, char *username, char *password, int cmndClass, int cmndDesc, int param, void *payload, int payloadSize, void *returnData, int *returnDataSize)
  70. {
  71.     // this function implements the DxP v1.4 communications protocol
  72.     char hello[12];                     // hello string is hello-000 null terminated
  73.     char recvbuf[MAX_RECEIVE_BUF_SIZE]; // receive buffer
  74.     char packet[MAX_PACKET_SIZE];       // final header+payload to send to DxP device
  75.     char byte1, byte2;
  76.     int iResult, bytesSent, bytesRecv, packetSize;
  77.     unsigned short newseq;
  78.     WSADATA wsaData;                    // for Winsock
  79.     SOCKET m_socket;
  80.     struct sockaddr_in clientService;
  81.     tHeader header;
  82.    
  83.     //printf("DxP: function entered\r\n");
  84.    
  85.     // before we do anything, we need to validate some of input parameters
  86.     // do user name
  87.     if(strlen(username) > MAX_USERNAME_SIZE)
  88.         return DXP_ERROR_INVALID_PARAMETER;
  89.    
  90.     if(strlen(password) > MAX_PASSWORD_SIZE)
  91.         return DXP_ERROR_INVALID_PARAMETER;
  92.    
  93.     ///////////////////////////////////////////////////////////////////////////////
  94.    
  95.     // setup initial hello message
  96.     strcpy(hello, "hello-000");
  97.    
  98.     // zero out the header structure
  99.     memset((char *)&header, 0, sizeof(tHeader));
  100.    
  101.     // zero out packet
  102.     memset(packet, 0, sizeof(packet));
  103.    
  104.     // Initialize Winsock.
  105.     iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  106.     if(iResult != NO_ERROR) {
  107.         printf("Client: Error at WSAStartup().\n");
  108.         return DXP_ERROR_WSA_STARTUP;
  109.     }
  110.    
  111.     //printf("DxP: WSAStartup() called\r\n");
  112.  
  113.     // Create a socket.
  114.     m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  115.     if(m_socket == INVALID_SOCKET) {
  116.         //printf("Client: socket() - Error at socket(): %ld\n", WSAGetLastError());
  117.         closesocket(m_socket);
  118.         WSACleanup();
  119.         return DXP_ERROR_SOCKET;
  120.     }
  121.    
  122.     //printf("DxP: socket created\r\n");
  123.  
  124.     // Connect to the DxP server device.
  125.     clientService.sin_family = AF_INET;
  126.     clientService.sin_addr.s_addr = inet_addr(ipAddress);
  127.     clientService.sin_port = htons(ipPort);
  128.     if(connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR) {
  129.         //printf("Failed to connect to IP address %s, port %d\n", ipAddress, ipPort);
  130.         closesocket(m_socket);
  131.         WSACleanup();
  132.         return DXP_ERROR_CONNECT_FAIL;
  133.     }
  134.     else {
  135.         // successful connection!
  136.        
  137.         //printf("Connection made to %s\n", ipAddress);
  138.     }
  139.  
  140.     // Send and receive data.
  141.  
  142.     // Sends Hello to Server
  143.     //printf("DxP: sending hello...\r\n");
  144.     bytesSent = send(m_socket, hello, 10, 0);
  145.     if(bytesSent == SOCKET_ERROR) {
  146.         //printf("Client: send() error %ld.\n", WSAGetLastError());
  147.         closesocket(m_socket);
  148.         WSACleanup();
  149.         return DXP_ERROR_SEND;
  150.     }
  151.    
  152.     //printf("DxP: hello sent, waiting for sequence #\r\n");
  153.  
  154.     // Receives Sequence from Server
  155.     bytesRecv = recv(m_socket, recvbuf, 2, 0);
  156.     if((bytesRecv == 0) || (bytesRecv == WSAECONNRESET)) {
  157.         //printf("Sequence not Returned - Fail \n");
  158.         closesocket(m_socket);
  159.         WSACleanup();
  160.         return DXP_ERROR_RECEIVE;
  161.     }
  162.    
  163.     // convert sequence # to Sequence +1
  164.     byte1 = recvbuf[0];
  165.     byte2 = recvbuf[1];
  166.  
  167.     newseq = (unsigned short)byte2;
  168.     newseq <<= 8;           // shift to left 8 bits
  169.     newseq |= byte1;        // added lower 8 bits of sequence #
  170.     newseq++;               // increment to get new sequence #
  171.    
  172.     //printf("DxP: sequence # received, new sequence # is %d\r\n", newseq);
  173.  
  174.     // build header
  175.     header.command  = cmndClass;
  176.     strcpy(header.username, username);
  177.     strcpy(header.password, password);
  178.     header.desc     = cmndDesc;
  179.     header.param    = param;
  180.     header.seq      = newseq;
  181.            
  182.     // copy header to the packet buffer
  183.     memcpy(packet, (char *)&header, sizeof(tHeader));
  184.    
  185.     // append the payload from the user (only if there is a payload!)
  186.     if(payload != NULL)
  187.         memcpy(&packet[sizeof(tHeader)], (char *)payload, payloadSize);
  188.    
  189.     // calculate packet size
  190.     packetSize = sizeof(tHeader) + payloadSize;
  191.    
  192.     // send the packet!
  193.     bytesSent = send(m_socket, packet, packetSize, 0);
  194.    
  195.     //printf("DxP: sent DxP payload (packet)\r\n");
  196.  
  197.     // get the response
  198.     // receive response 0x00 is OK
  199.     bytesRecv = recv(m_socket, recvbuf, MAX_RECEIVE_BUF_SIZE, 0);
  200.     if((bytesRecv == 0) || (bytesRecv == WSAECONNRESET)) {
  201.         //printf("DxP::Connection Closed by %s (bytesRecv=%d)\n", ipAddress, bytesRecv);
  202.         closesocket(m_socket);
  203.         WSACleanup();
  204.         return DXP_ERROR_RECEIVE;
  205.     }
  206.  
  207.     if(bytesRecv < 0) {
  208.         //printf("DxP: bytesRecv < 0\r\n");
  209.         closesocket(m_socket);
  210.         WSACleanup();
  211.         return DXP_ERROR_RECEIVE;
  212.     }
  213.     else {
  214.         // we received some data, so send it back to the user!
  215.         //printf("DxP: data receive normal-bytesRecv=%d\r\n", bytesRecv);
  216.         memcpy((void *)returnData, recvbuf, bytesRecv);
  217.         *returnDataSize = bytesRecv;       
  218.     }
  219.  
  220.     // We are done.  Close socket
  221.     closesocket(m_socket);
  222.     Sleep(200);     // delay to allow socket to close
  223.     WSACleanup();
  224.     return DXP_SUCCESS;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement