Beispiel #1
0
/*
** Read data from client to CGI script
**
** IN:
**      uint32_t ses_handle - handle to session used for reading
**      char*   buffer - user buffer to read data to
**      uint32_t length - size of buffer in bytes
**
** OUT:
**      none
**
** Return Value:
**      uint32_t - Number of bytes read
*/
uint32_t HTTPSRV_cgi_read(uint32_t ses_handle, char* buffer, uint32_t length)
{
    HTTPSRV_SESSION_STRUCT* session = (HTTPSRV_SESSION_STRUCT*) ses_handle;
    uint32_t retval;

    if 
        (
            (session == NULL) ||
            (buffer == NULL) ||
            (length == 0)
        )
    {
        return(0);
    }

    retval = httpsrv_read(session, buffer, length);

    if (retval > 0)
    {
        RTCS_ASSERT(retval <= session->request.content_length);
        if (retval <= session->request.content_length)
        {
            session->request.content_length -= retval;
        }
    }
    else
    {
        if (RTCS_geterror(session->sock) == RTCSERR_TCP_TIMED_OUT)
        {
            retval = 0;
        }
    }
    session->time = RTCS_time_get();
    return(retval);
}
Beispiel #2
0
/* Translates return codes returned from 
 * send() and recv() if need be. 
 */
static INLINE int TranslateReturnCode(int old, int sd)
{
    (void)sd;

#ifdef FREESCALE_MQX
    if (old == 0) {
        errno = SOCKET_EWOULDBLOCK;
        return -1;  /* convert to BSD style wouldblock as error */
    }

    if (old < 0) {
        errno = RTCS_geterror(sd);
        if (errno == RTCSERR_TCP_CONN_CLOSING)
            return 0;   /* convert to BSD style closing */
    }
#endif

    return old;
}
/* Read command from control socket */
static int ftpsrv_read_cmd(FTPSRV_SESSION_STRUCT* session)
{
    int32_t  result;
    uint32_t sock = session->control_sock;
    uint32_t received = 0;
    char*    tmp;
    char*    end;
    char*    buffer = session->buffer;
    
    while ((received < FTPSRV_BUF_SIZE) && !(end = strstr(buffer, "\r\n")))
    {
        result = recv(sock, buffer+received, FTPSRV_BUF_SIZE-received, 0);
        if (result == RTCS_ERROR)
        {
            uint32_t error = RTCS_geterror(sock);
            
            session->connected = FALSE;
            return(FTPSRV_ERROR);
        }
        received += result;
    }

    /* Null-terminate received data */
    if (end != NULL)
    {
        end[0] = '\0';
    }
    else
    {
        return(FTPSRV_ERROR);
    }

    /* Find a command and store it */
    session->command = strtok(buffer, " ");
    if (session->command == NULL)
    {
        return(FTPSRV_ERROR);
    }
    /* Find a parameter and store it */
    session->cmd_arg = strtok(NULL, " ");
    return(FTPSRV_OK);
}
Beispiel #4
0
/* Translates return codes returned from 
 * send() and recv() if need be. 
 */
static INLINE int TranslateReturnCode(int old, int sd)
{
    (void)sd;

#if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
    if (old == 0) {
        errno = SOCKET_EWOULDBLOCK;
        return -1;  /* convert to BSD style wouldblock as error */
    }

    if (old < 0) {
        errno = RTCS_geterror(sd);
        if (errno == RTCSERR_TCP_CONN_CLOSING)
            return 0;   /* convert to BSD style closing */
        if (errno == RTCSERR_TCP_CONN_RLSD)
            errno = SOCKET_ECONNRESET;
        if (errno == RTCSERR_TCP_TIMED_OUT)
            errno = SOCKET_EAGAIN;
    }
#elif defined(NUCLEUS)
    if (old < 0) {
        if (old == NU_NOT_CONNECTED) {
            /* Set received to 0 to show connection closed */
            old = 0;
        }
        else {
            /* Set error number */
            Nucleus_Net_Errno = old;

            /* Set received to -1 to indicate error to caller */
            old = -1;
        }
    }
    else {
        /* Clear error number */
        Nucleus_Net_Errno = 0;
    }
#endif

    return old;
}
Beispiel #5
0
/* Translates return codes returned from 
 * send() and recv() if need be. 
 */
static INLINE int TranslateReturnCode(int old, int sd)
{
    (void)sd;

#if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
    if (old == 0) {
        errno = SOCKET_EWOULDBLOCK;
        return -1;  /* convert to BSD style wouldblock as error */
    }

    if (old < 0) {
        errno = RTCS_geterror(sd);
        if (errno == RTCSERR_TCP_CONN_CLOSING)
            return 0;   /* convert to BSD style closing */
        if (errno == RTCSERR_TCP_CONN_RLSD)
            errno = SOCKET_ECONNRESET;
        if (errno == RTCSERR_TCP_TIMED_OUT)
            errno = SOCKET_EAGAIN;
    }
#endif

    return old;
}
Beispiel #6
0
_mqx_int httpd_readln(HTTPD_SESSION_STRUCT *session, char *buf, _mqx_int max_len) {
    int used = 0, ret;
    char *dst = buf;
    uint_32 err;

    HTTPD_ASSERT(session && dst);
    
    HTTPD_DEBUG(5, "readln session(%p) buf(%p) max_len(%d)\n", session, buf, max_len);

    while (used < max_len) {
#if HTTPDCFG_POLL_MODE
        ret = httpd_readch(session, dst);
#else
        ret = recv(session->sock, dst, 1, 0);
#endif

        if (ret <= 0) {
            err = RTCS_geterror(session->sock);
            *buf = 0;
            HTTPD_DEBUG(5, "readln used(%d) ret(%d) err(0x%x)\n", used, ret, err);
            return ret;
        }

        dst++;
        used++;

        if (*(dst - 1) == '\n') {
            break;
        }
    }

    *dst = 0;
    
    HTTPD_DEBUG(5, "readln len(%d)\n", used);
    return used;
}
void tcp_socket_task( uint_32 val)
{
  sockaddr_in    laddr, raddr;
  uint_32        sock, listensock;
  uint_32        error;
  uint_16        rlen;
  uint_16        sin_port_is = 3333; 
  /* Set up the TCP port : */
  laddr.sin_family      = AF_INET;
  laddr.sin_port        = sin_port_is;
  laddr.sin_addr.s_addr = INADDR_ANY;
  /* Create a stream socket: */
  sock  = socket(PF_INET, SOCK_STREAM, 0);// 1.创建包,类型
  if (sock  == RTCS_SOCKET_ERROR) 
  {
    printf("\nFailed to create the stream socket.");
    _task_block();
  }
  /* Bind the stream socket to a TCP port: */
  error  = bind(sock, &laddr, sizeof(laddr));// 2.绑定包,端口等
  if (error  != RTCS_OK) 
  {
    printf("\nFailed to bind the stream socket - 0x%lx", error);
    _task_block();
  }
  /* Set up the stream socket to listen on the TCP port: */
  error = listen(sock, 0);// 3.被动连接即服务器,监听
  if (error != RTCS_OK) {
    printf("\nlisten() failed - 0x%lx", error);
    _task_block();
  }
  listensock = sock;
  printf("\n\nSocket Server is active on port %u.\n",(uint_16)sin_port_is);
  for (;;)
  {
    sock = RTCS_selectset(&listensock,1,0);//RTCS_selectall(0);// 4.等待任何或专用socket活动套接字,
    if (sock == listensock)
    {
      /* Connection requested; accept it. */
      rlen = sizeof(raddr);
      sock = accept(listensock, &raddr, &rlen); // 5.被动连接即服务器,接受
      if (sock == RTCS_SOCKET_ERROR) 
      {
        printf("\naccept() failed, error 0x%lx",
               RTCS_geterror(listensock));
        continue;
      }
      /* Send back a quote: */   
      send(sock, "\nWelcome to PQ System!! ", strlen("\nWelcome to PQ System!! "), 0);// 6.发送包
      connect(sock, &raddr, rlen);
      _time_delay(1000);
     // int cnt=100;
#define TCPIP_SOCKET_DATA_LEN      100
      char socket_buf[TCPIP_SOCKET_DATA_LEN+1]="";
      socket_buf[TCPIP_SOCKET_DATA_LEN] = '\0';
//      char socket_s[10]="send";
      while(1)
      {
        /* wk @130331 --> recv()函数是事件触发机制的,如果调用了此函数,需要外部事件触发,此任务才能激活*/
        if (recv(sock, &socket_buf, TCPIP_SOCKET_DATA_LEN, 0) == RTCS_ERROR) // 6.接收包,出错则关连接
        {
          break;
        } 
        
        if(socket_buf[0]=='p')
          send(sock, "\nPower Quality Data!", strlen("\nPower Quality Data!"), 0);
        else if(socket_buf[0]=='e')
          send(sock, "\nEvent Data!", strlen("\nEvent Data!"), 0);
        else
          send(sock,"\nInvalid Order!",strlen("\nInvalid Order!"),0);
          
//        send(sock, socket_s, strlen(socket_s), 0);///////////回显for test
//        printf("%s",socket_buf);////////////////////回显for test
//        cnt--;
      }
      shutdown(sock, FLAG_CLOSE_TX);// 7.关包
    } 
    else
    {
      printf("\n TcpIP port socket is not right! ");
    }
    
  }
  
  
  
}
Beispiel #8
0
static void Clock_server_task
(
   pointer unused1,
   pointer unused2
)
{
   sockaddr_in    laddr, raddr={0};
   uint_32        sock, listensock;
   uint_32        error;
   uint_16        rlen;


   /* Clock server services port 999 */
   laddr.sin_family      = AF_INET;
   laddr.sin_port        = 999;
   laddr.sin_addr.s_addr = INADDR_ANY;


   /* Listen on TCP port */
   listensock= socket(PF_INET, SOCK_STREAM, 0);
   if (listensock == RTCS_HANDLE_ERROR) {
      printf("\nCreate stream socket failed");
      _task_block();
   } 
   error = bind(listensock, &laddr, sizeof(laddr));
   if (error != RTCS_OK) {
      printf("\nStream bind failed - 0x%lx", error);
      _task_block();
   } 
   error = listen(listensock, 0);
   if (error != RTCS_OK) {
      printf("\nListen failed - 0x%lx", error);
      _task_block();
   } 

   printf("\nClock Server active on port 999\n");

   for (;;) {
      /* Connection requested; accept it */
      rlen = sizeof(raddr);
      printf("Clock server: Waiting on accept\n");
      sock = accept(listensock, &raddr, &rlen);
      if (sock == RTCS_HANDLE_ERROR) {
         printf("\n\n*** Clock server: Accept failed, error 0x%lx *** \n\n\n",
            RTCS_geterror(listensock));
      } else {
         printf("Clock server: Connection from %ld.%ld.%ld.%ld, port %d, socket %x\n",
            (raddr.sin_addr.s_addr >> 24) & 0xFF,
            (raddr.sin_addr.s_addr >> 16) & 0xFF,
            (raddr.sin_addr.s_addr >>  8) & 0xFF,
             raddr.sin_addr.s_addr        & 0xFF,
             raddr.sin_port, sock);

         /* Create a task to look after it */
         printf("Clock server: detaching socket %x\n",sock);
#if USE_RTCS_ATTACH_DETACH
         RTCS_detachsock(sock);
#endif
         printf("Clock server: spawning child task\n");
         #if CREATE_WITH_RTCS
         RTCS_task_create("Clock_child", SHELL_CLOCK_CHILD_PRIO,
             SHELL_CLOCK_CHILD_STACK, Clock_child_task, (pointer) sock);
         #else
            {
               TASK_TEMPLATE_STRUCT    task_template = {0};
               task_template.TASK_NAME          = "Clock_child";
               task_template.TASK_PRIORITY      = SHELL_CLOCK_CHILD_PRIO;
               task_template.TASK_STACKSIZE     = SHELL_CLOCK_CHILD_STACK;
               task_template.TASK_ADDRESS       = Clock_child_task;
               task_template.CREATION_PARAMETER = (uint_32)sock;
               if (_task_create(0, 0, (uint_32)&task_template) == MQX_NULL_TASK_ID) {
                  printf("Clock server: failed to spawn child task\n");
               } 
            }
         #endif
      } 
   }
} /* Endbody */
Beispiel #9
0
bool_t pmap_init
   (
      void
   )
{ /* Body */
   int_32                  sock;
   sockaddr_in             laddr;
   SVCXPRT_PTR             xprt;
   PMAPLIST_PTR            pml;

   /* create and bind a UDP socket */
   sock = socket(PF_INET, SOCK_DGRAM, 0);
   if (sock == (int_32)RTCS_SOCKET_ERROR) {
      fprintf(stderr, "portmap: error creating UDP socket\n");
      return FALSE;
   } /* Endif */

   _mem_zero(&laddr, sizeof(laddr));
   laddr.sin_family = AF_INET;
   laddr.sin_port = PMAPPORT;
   laddr.sin_addr.s_addr = 0;
   if (bind(sock, &laddr, sizeof(laddr)) != RTCS_OK) {
      fprintf(stderr, "portmap: error binding UDP socket, rec=%08X\n",
         RTCS_geterror(sock));
      return FALSE;
   } /* Endif */

   /* create a UDP transport*/
   xprt = svcudp_create(sock);
   if (xprt == NULL) {
      fprintf(stderr, "portmap: error creating UDP transport, rec=%08X\n",
         RTCS_geterror(sock));
      return FALSE;
   } /* Endif */

   /* create an entry for the UDP transport */
   pml = (PMAPLIST_PTR)RTCS_mem_alloc_zero(sizeof(PMAPLIST));
   if (pml == NULL) {
      fprintf(stderr, "portmap: error creating UDP entry, can't alloc memory\n");
      return FALSE;
   } /* Endif */
   pml->pml_map.pm_prog = PMAPPROG;
   pml->pml_map.pm_vers = PMAPVERS;
   pml->pml_map.pm_prot = IPPROTO_UDP;
   pml->pml_map.pm_port = PMAPPORT;
   pml->pml_next = pml_head;
   pml_head = pml;

   /* create, bind and connect a TCP socket */
   sock = socket(PF_INET, SOCK_STREAM, 0);
   if (sock == (int_32)RTCS_SOCKET_ERROR) {
      fprintf(stderr, "portmap: error creating TCP socket\n");
      return FALSE;
   } /* Endif */

   _mem_zero(&laddr, sizeof(laddr));
   laddr.sin_family = AF_INET;
   laddr.sin_port = PMAPPORT;
   laddr.sin_addr.s_addr = 0;

   if (bind(sock, &laddr, sizeof(laddr)) != RTCS_OK) {
      fprintf(stderr, "portmap: error binding TCP socket, rec=%08X\n",
         RTCS_geterror(sock));
      return FALSE;
   } /* Endif */

   if (listen(sock, 0) != RTCS_OK) {
      fprintf(stderr, "portmap: error on socket listen, rec=%08X\n",
         RTCS_geterror(sock));
      return FALSE;
   } /* Endif */

   /* create a TCP transport*/
   xprt = svctcp_create(sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
   if (xprt == NULL) {
      fprintf(stderr, "portmap: error creating TCP transport, rec=%08X\n",
         RTCS_geterror(sock));
      return FALSE;
   } /* Endif */

   /* create an entry for the TCP transport */
   pml = (PMAPLIST_PTR)RTCS_mem_alloc_zero(sizeof(PMAPLIST));
   if (pml == NULL) {
      fprintf(stderr, "portmap: error creating TCP entry, task error=%08X\n",
         _task_get_error());
      return FALSE;
   } /* Endif */
   pml->pml_map.pm_prog = PMAPPROG;
   pml->pml_map.pm_vers = PMAPVERS;
   pml->pml_map.pm_prot = IPPROTO_TCP;
   pml->pml_map.pm_port = PMAPPORT;
   pml->pml_next = pml_head;
   pml_head = pml;

   /* register program */
   if (!svc_register(NULL, PMAPPROG, PMAPVERS, pmap_service, 0)) {
      fprintf(stderr, "portmap: error registering service\n");
      return FALSE;
   } /* Endif */

   return TRUE;
} /* Endbody */