Esempio n. 1
0
/* TCP CLI debug handler */
DSL_int_t DSL_CPE_TcpDebugCliHandle ( DSL_CPE_Thread_Params_t *params )
{
   DSL_int_t nSockFd, nRet;
   DSL_int_t nWidth;
   DSL_CPE_fd_set_t readFds, tempFds;
   DSL_Socket_t nSocketAccept;
   DSL_sockaddr_in_t myAddr;
   /* client address*/
   DSL_sockaddr_in_t remoteAddr;
#ifdef LINUX
   DSL_int_t nSockOpt = 1;
   DSL_TimeVal_t sendTimeout;
   DSL_SockOptLinger_t structLg;
#endif
   DSL_int_t c;
   /*number of requests*/
   DSL_int_t n = 0;
   /* stores the client information */
   DSL_CPE_TcpDebugCliClientInfo_t *clientInfo;
   DSL_int_t nClients = 0;
   DSL_CPE_Control_Context_t *pCtrlContext = (DSL_CPE_Control_Context_t*)params->nArg1;

   clientInfo = DSL_CPE_Malloc(DSL_FD_SETSIZE * sizeof(DSL_CPE_TcpDebugCliClientInfo_t));
   if (clientInfo == DSL_NULL)
   {
      return -1;
   }

   /* init variables */
   memset((char *) &readFds, 0x0, sizeof(DSL_CPE_fd_set_t));
   memset((char *) &tempFds, 0x0, sizeof(DSL_CPE_fd_set_t));

   /* Socket Address */
   memset((char *) &myAddr, '\0', sizeof(DSL_sockaddr_in_t));
   myAddr.sin_family = AF_INET;
   myAddr.sin_port = DSL_CPE_Htons(DSL_CPE_TCP_CLI_PORT); /* Listens at port 2001 */

   if (sTcpMessagesSocketAddr)
   {
      if (DSL_CPE_StringToAddress(sTcpMessagesSocketAddr, &myAddr.sin_addr) == 0)
      {
         DSL_CPE_Free(clientInfo);

         DSL_CCA_DEBUG(DSL_CCA_DBG_ERR, (DSL_CPE_PREFIX
            "ERROR - invalid IP address specified" DSL_CPE_CRLF));

         return -1;
      }

      DSL_CCA_DEBUG(DSL_CCA_DBG_MSG, (DSL_CPE_PREFIX
         "TCP CLI debug server: Address: %s" DSL_CPE_CRLF,
         DSL_CPE_AddressToString(myAddr.sin_addr)));
   }
   else
   {
      DSL_CCA_DEBUG(DSL_CCA_DBG_MSG, (DSL_CPE_PREFIX
         "Using multihomed host for TCP messages..." DSL_CPE_CRLF));

      myAddr.sin_addr.s_addr = DSL_CPE_Htonl(DSL_CPE_INADDR_ANY);
   }

   /* Initialise file descriptors to zero */
   DSL_CPE_FD_ZERO(&readFds);
   DSL_CPE_FD_ZERO(&tempFds);

   /* Set up socket parameters */
   if ((DSL_CPE_Socket(SOCK_STREAM, (DSL_Socket_t*)&nSockFd)) == -1)
   {
      DSL_CPE_Free(clientInfo);

      DSL_CCA_DEBUG(DSL_CCA_DBG_ERR, (DSL_CPE_PREFIX
         "ERROR - Socket was not created" DSL_CPE_CRLF));
      return 0;
   }

#ifdef LINUX
   /*$$ ND: I'm not sure whether all of options below are supported
        by another OS than Linux */

   /* set the socket to be reuseable */
   DSL_CPE_SockOptSet(nSockFd, SOL_SOCKET, SO_REUSEADDR, &nSockOpt,
      sizeof(nSockOpt));

   /* set up the struct timeval for the timeout If connected socket fails
      to respond to these messages, the connection is broken and processes
      writing to that socket are notified with an ENETRSET errno. This option
      takes an int value in the optval argument. This is a BOOL option */
   nSockOpt = 1;
   DSL_CPE_SockOptSet(nSockFd, SOL_SOCKET, SO_KEEPALIVE, &nSockOpt,
      sizeof(nSockOpt) );

   /* SO_DEBUG */
   /* Enables recording of debugging information */
   nSockOpt = 1;
   DSL_CPE_SockOptSet(nSockFd, SOL_SOCKET, SO_DEBUG, &nSockOpt, sizeof(nSockOpt) );

   /* SO_LINGER linger on close if data present SO_LINGER controls the action
      taken when unsent messages are queued on socket and a close(2)
      is performed */
   structLg.l_onoff=0;
   structLg.l_linger=0;
   DSL_CPE_SockOptSet(nSockFd, SOL_SOCKET, SO_LINGER, &structLg,
      sizeof(DSL_SockOptLinger_t) );

   /* SO_RCVTIME0 and SO_SNDTIME0 Specify the sending or receiving timeouts
      until reporting an error. They are fixed to a protocol specific setting
      in Linux adn cannot be read or written They can be easily emulated
      using alarm */
   sendTimeout.tv_sec=60;
   sendTimeout.tv_usec=0;
   DSL_CPE_SockOptSet(nSockFd, SOL_SOCKET, SO_SNDTIMEO, &sendTimeout,
      sizeof(DSL_TimeVal_t) );
#endif

   for (c = 0; c < DSL_FD_SETSIZE; c++)
   {
      clientInfo[c].fd = -1;
      clientInfo[c].buf = 0;
   }

   /* Set info in the Control Context*/
   pCtrlContext->pDebugCliClientInfo = clientInfo;

   if (DSL_CPE_SocketBind(nSockFd, &myAddr) == -1)
   {
      DSL_CCA_DEBUG(DSL_CCA_DBG_ERR, (DSL_CPE_PREFIX
         "ERROR - Could not bind to socket" DSL_CPE_CRLF));
   }
   else
   {
      if (listen(nSockFd, 1) == -1)
      {
         DSL_CCA_DEBUG(DSL_CCA_DBG_ERR, (DSL_CPE_PREFIX
            "ERROR - Listening Error" DSL_CPE_CRLF));
      }
      else
      {
         DSL_CCA_DEBUG(DSL_CCA_DBG_MSG, (DSL_CPE_PREFIX
            "TCP_IP Socket for CLI is Listening" DSL_CPE_CRLF));

         c = 0;

         DSL_CPE_FD_SET(nSockFd, &readFds);

         nWidth = nSockFd + 1;
         /* Listen to the selected port */
         while(1)
         {
            /* Update the local file descriptor by the copy in the task parameter  */
            memcpy(&tempFds, &readFds, sizeof(DSL_CPE_fd_set_t));

            /* Wait for incoming events */
            n = DSL_CPE_Select(nWidth, &tempFds, &tempFds, (DSL_uint32_t)-1);

            /* Look if messages were received */
            /* New connection */
            if (DSL_CPE_FD_ISSET(nSockFd,&tempFds))
            {
               /* Received socket stream Accept connection from socket*/
               nSocketAccept = DSL_CPE_Accept(nSockFd, &remoteAddr);

               DSL_CCA_DEBUG(DSL_CCA_DBG_MSG, (DSL_CPE_PREFIX
                  "Connected from %s" DSL_CPE_CRLF, DSL_CPE_AddressToString(remoteAddr.sin_addr)));

               /* store information to clientInfo table */
               for (c = 0; c < DSL_FD_SETSIZE; c++)
               {
                  if (nClients >= DSL_FD_SETSIZE)
                  {
                     DSL_CCA_DEBUG(DSL_CCA_DBG_ERR, (DSL_CPE_PREFIX
                        "ERROR - Too many clients" DSL_CPE_CRLF));

                     DSL_CPE_SocketClose(nSocketAccept);
                     continue;
                  }

                   if (clientInfo[c].fd < 0)
                   {
                     clientInfo[c].out = DSL_CPE_FdOpen((DSL_int_t)nSocketAccept, "w");
                     if (clientInfo[c].out != DSL_NULL)
                     {
                        if (clientInfo[c].buf != DSL_NULL)
                        {
                           DSL_CPE_Free(clientInfo[c].buf);
                        }

                        clientInfo[c].buf =
                           DSL_CPE_Malloc(DSL_CPE_TCP_CLI_COMMAND_LENGTH_MAX);

                        if (clientInfo[c].buf == DSL_NULL)
                        {
                           DSL_CCA_DEBUG(DSL_CCA_DBG_ERR, (DSL_CPE_PREFIX
                              "ERROR - Could not allocate buffer for CLI command" DSL_CPE_CRLF));
                        }
                        else
                        {
                          clientInfo[c].fd = (DSL_int_t)nSocketAccept;
                          clientInfo[c].client_addr = remoteAddr;
                          nClients++;
                          /* update fd to readFds */
                           if ((DSL_int_t)nSocketAccept >= nWidth)
                           {
                              nWidth = (DSL_int_t)nSocketAccept + 1;
                           }

                           DSL_CPE_FD_SET((DSL_int_t)nSocketAccept,&readFds);
                          n--;
                        }
                     }
                     else
                     {
                        DSL_CCA_DEBUG(DSL_CCA_DBG_ERR, (DSL_CPE_PREFIX
                           "ERROR - Could not create file structure for socket"DSL_CPE_CRLF));
                     }
                     break;
                 }
               }
            }

            /* check fd for all clients  */
            for (c = 0; c < DSL_FD_SETSIZE; c++)
            {
               if (n == 0)
               {
                  break;
               }

               if ((nSocketAccept = (DSL_Socket_t)clientInfo[c].fd) < 0)
               {
                 continue;
               }

               /* Check for receive time-out */
               if (DSL_CPE_FD_ISSET((DSL_int_t)nSocketAccept, &tempFds))
               {
                 n--;
                 nRet = DSL_CPE_TcpCliHandle((DSL_CPE_Control_Context_t*) params->nArg1,
                     &clientInfo[c], (DSL_int_t)nSocketAccept);

                  if (nRet == -ENODATA) /* read 0 bytes, client closed the connection */
                  {
                     DSL_CCA_DEBUG(DSL_CCA_DBG_WRN, (DSL_CPE_PREFIX
                        "Connection closed from %s." DSL_CPE_CRLF,
                        DSL_CPE_AddressToString(clientInfo[c].client_addr.sin_addr)));

                     if (clientInfo[c].out != DSL_NULL)
                     {
                        DSL_CPE_FClose(clientInfo[c].out);
                        clientInfo[c].out = DSL_NULL;
                        if (clientInfo[c].buf != DSL_NULL)
                        {
                           DSL_CPE_Free(clientInfo[c].buf);
                           clientInfo[c].buf = DSL_NULL;
                        }
                     }
                     else
                     {
                          DSL_CPE_SocketClose(nSocketAccept);
                     }

                     DSL_CPE_FD_CLR((DSL_int_t)nSocketAccept, &readFds);
                    clientInfo[c].fd = -1;
                    nClients--;
                  }
               }
            }
         }
      }
   }

   for (c = 0; c < DSL_FD_SETSIZE; c++)
   {
      if ((nSocketAccept = (DSL_Socket_t)clientInfo[c].fd) != -1)
      {
         DSL_CPE_SocketClose(nSocketAccept);
      }
      if (clientInfo[c].buf != DSL_NULL)
      {
         DSL_CPE_Free(clientInfo[c].buf);
      }
   }

   DSL_CPE_SocketClose(nSockFd);

   return 0;
}
Esempio n. 2
0
/**
   Handle a incoming command form the Debug & Trace interface
*/
DTI_STATIC DSL_int_t DSL_CPE_CLI_Dti_Exec(
                        DSL_void_t        *pCliDtiDescriptor,
                        const DSL_char_t  *pCmdIn,
                        DSL_char_t        *pResultOut,
                        DSL_int_t         *pResultBufSize_byte,
                        DSL_int_t         *pResultCode)
{
   DSL_int_t retVal = 0, writtenBytes = 0;
   DSL_CPE_Dti_Context_t *pCliDtiContext = (DSL_CPE_Dti_Context_t *)pCliDtiDescriptor;
   DSL_CPE_File_t *pOutStream = DSL_CPE_STDOUT;

   if (pResultOut && pResultBufSize_byte)
   {
      memset(pResultOut, 0, *pResultBufSize_byte);
   }
   else
   {
      DSL_CPE_FPrintf(DSL_CPE_STDOUT, DSL_CPE_PREFIX"oops, missing DTI result buffer" DSL_CPE_CRLF);
      return DSL_ERROR;
   }

   if (!pCliDtiDescriptor)
   {
      DSL_CPE_FPrintf(DSL_CPE_STDOUT, DSL_CPE_PREFIX"oops, missing CLI DTI handle" DSL_CPE_CRLF);
      writtenBytes = DSL_CPE_snprintf(pResultOut, *pResultBufSize_byte,
                        "nReturn=-1 nError=\"oops, missing CLI DTI handle\n\r\"");

      *pResultBufSize_byte = writtenBytes;
      return DSL_ERROR;
   }

   if (!pCmdIn)
   {
      DSL_CPE_FPrintf(DSL_CPE_STDOUT, DSL_CPE_PREFIX"oops, missing DTI command" DSL_CPE_CRLF);
      writtenBytes = DSL_CPE_snprintf(pResultOut, *pResultBufSize_byte,
                        "nReturn=-1 nError=\"oops, missing DTI command\n\r\"");

      *pResultBufSize_byte = writtenBytes;
      return DSL_ERROR;
   }

#if defined(IFXOS_HAVE_MEMORY_FILE) && (IFXOS_HAVE_MEMORY_FILE == 1)
   pOutStream = DSL_CPE_FMemOpen(pResultOut, *pResultBufSize_byte - 1, "w");
   if(pOutStream == NULL)
   {
      DSL_CPE_FPrintf(DSL_CPE_STDOUT, DSL_CPE_PREFIX"oops, cannot open DTI stream" DSL_CPE_CRLF);
      writtenBytes = DSL_CPE_snprintf(pResultOut, *pResultBufSize_byte,
                        "nReturn=-1 nError=\"oops, cannot open DTI stream\n\r\"");

      *pResultBufSize_byte = writtenBytes;
      return DSL_ERROR;
   }
#endif

   /*
      execute the given DTI command.
   */
   retVal = DSL_CPE_CLI_Dti_Command_Exec(pCliDtiContext, pCmdIn, pOutStream);

#if defined(IFXOS_HAVE_MEMORY_FILE) && (IFXOS_HAVE_MEMORY_FILE == 1)
   DSL_CPE_FClose(pOutStream);

   writtenBytes = strlen(pResultOut);
   pResultOut[writtenBytes] = '\0';
#else
   writtenBytes = DSL_CPE_snprintf(pResultOut, *pResultBufSize_byte,
                     "nReturn=%d nWarning=\"result written to stdout\n\r\"", retVal);
#endif

   *pResultBufSize_byte = writtenBytes;

   return DSL_SUCCESS;
}
Esempio n. 3
0
int ifx__DslCpeCliAccess (struct soap *soap_server, char *pCommand, char **pReturn )
{
   char cmd[256] = "";
   char *pcArg = DSL_NULL;
   Soap_env_t *pSoapEnv = soap_server->user;
   DSL_CPE_File_t *stream = DSL_NULL;
   struct soap_multipart *attachment;
#ifdef DSL_CPE_SOAP_FW_UPDATE
   DSL_CPE_Firmware_t nFirmware;
   DSL_CPE_Firmware_t nFirmware2;

   memset(&nFirmware, 0, sizeof(nFirmware));
   memset(&nFirmware2, 0, sizeof(nFirmware2));
#endif /* DSL_CPE_SOAP_FW_UPDATE */

   if(pCommand == DSL_NULL)
   {
      sprintf(soap_result, "nReturn=-1 nError=\"oops, CLI command received via SOAP is zero\n\r\"");
      *pReturn = soap_result;
      return SOAP_OK;
   }

   /* get command name */
   sscanf(pCommand, "%255s", cmd);

   if(strlen(cmd) < strlen(pCommand))
   {
      pcArg = pCommand + strlen(cmd) + 1;
   }

   for (attachment = soap_server->dime.list; attachment; attachment = attachment->next)
   {
   #ifdef DSL_CPE_SOAP_FW_UPDATE
      if(attachment->type && (strcmp(attachment->type, "image/firmware1") == 0))
      {
         fprintf(stderr,  "DIME attachment: firmware1.bin\n\r");
         nFirmware.pData = (DSL_uint8_t *)attachment->ptr;
         nFirmware.nSize = attachment->size;
      }
      else if(attachment->type && (strcmp(attachment->type, "image/firmware2") == 0))
      {
         fprintf(stderr,  "DIME attachment: firmware2.bin\n\r");
         nFirmware2.pData = (DSL_uint8_t *)attachment->ptr;
         nFirmware2.nSize = attachment->size;
      }
      else
      {
         fprintf(stderr,  "DSL: cannot decode DIME attachment\n\r");
         fprintf(stderr,  "DIME attachment:\n\r");
         fprintf(stderr,  "Memory=%p\n\r", attachment->ptr);
         fprintf(stderr,  "Size=%u\n\r", (unsigned int)attachment->size);
         fprintf(stderr,  "Type=%s\n\r", attachment->type ? attachment->type:"null");
         fprintf(stderr,  "ID=%s\n\r", attachment->id ? attachment->id:"null");
      }
   #else
      fprintf(stderr,  "Firmware download via DIME attachment not supported.\n\r");
   #endif
   }

#ifdef DSL_CPE_SOAP_FW_UPDATE
   if((nFirmware.pData != DSL_NULL) || (nFirmware2.pData != DSL_NULL))
   {
      DSL_CPE_SoapFirmwareUpdate(&nFirmware, &nFirmware2);
   }
#endif /* DSL_CPE_SOAP_FW_UPDATE */

   memset(soap_result, 0, sizeof(soap_result));

   stream = DSL_CPE_FMemOpen(soap_result, sizeof(soap_result)-1, "w");

   if(stream != DSL_NULL)
   {
      DSL_CPE_CliDeviceCommandExecute(pSoapEnv->pContext, -1, cmd, pcArg, stream);
   }
   else
   {
      DSL_CCA_DEBUG(DSL_CCA_DBG_ERR, (DSL_CPE_PREFIX
         "cannot open SOAP stream" DSL_CPE_CRLF));
   }

   DSL_CPE_FClose(stream);

   *pReturn = soap_result;

   return SOAP_OK;
}