Esempio n. 1
0
_mqx_int lw_telnet_server(void (_CODE_PTR_ user_fn)(void))
{
   uint_32        listensock, sock;
   sockaddr_in    addr;
   uint_32        error, option;
   MQX_FILE_PTR   sockfd, telnetfd;
   _mqx_uint      echoflag = IO_SERIAL_ECHO;
   pointer        old_stdin, old_stdout;

 
   /* Install device drivers for socket and telnet I/O */
   _io_socket_install("socket:");
   _io_telnet_install("telnet:");

   listensock = socket(PF_INET, SOCK_STREAM, 0);
   return_error_if(listensock == RTCS_SOCKET_ERROR);

   option = TELNETDCFG_BUFFER_SIZE;   
   error = setsockopt(listensock, SOL_TCP, OPT_TBSIZE, &option, sizeof(option));
   return_error_if(error != RTCS_OK);

   option = TELNETDCFG_BUFFER_SIZE;   
   error = setsockopt(listensock, SOL_TCP, OPT_RBSIZE, &option, sizeof(option));
   return_error_if(error != RTCS_OK);

   option = TELENETDCFG_TIMEWAIT_TIMEOUT;   
   error = setsockopt(listensock, SOL_TCP, OPT_TIMEWAIT_TIMEOUT, &option, sizeof(option));
   return_error_if(error != RTCS_OK);


   /* Bind the socket to the Telnet port */
   addr.sin_family      = AF_INET;
   addr.sin_port        = IPPORT_TELNET;
   addr.sin_addr.s_addr = INADDR_ANY;

   error = bind(listensock, &addr, sizeof(addr));
   return_error_if(error != RTCS_OK);

   /* Put the socket into listening mode */
   error = listen(listensock, 0);
   return_error_if(error != RTCS_OK);

   do {
      /* Wait for a connection */
      sock= accept(listensock, NULL, NULL);
      if (sock != RTCS_SOCKET_ERROR) {
         sockfd = fopen("socket:", (char_ptr)sock);
         if (sockfd != NULL) {
            telnetfd = fopen("telnet:", (char_ptr)sockfd);
            if (telnetfd != NULL) {

               ioctl(telnetfd, IO_IOCTL_SERIAL_SET_FLAGS, &echoflag);

               old_stdin = _io_set_handle(IO_STDIN, telnetfd);
               old_stdout = _io_set_handle(IO_STDOUT, telnetfd);

               (*user_fn)();

               _io_set_handle(IO_STDIN, old_stdin);
               _io_set_handle(IO_STDOUT, old_stdout);

               /*
               ** Allow some time for queued data to go out.
               */
               RTCS_time_delay(100);

               fclose(telnetfd);
            }
            fclose(sockfd);
         }
         shutdown(sock, FLAG_CLOSE_TX);
      } 
   }  while (sock != RTCS_SOCKET_ERROR);
   shutdown(listensock, FLAG_CLOSE_TX);
   return RTCS_OK;
} 
Esempio n. 2
0
uint_32 TELNET_connect
   (
      _ip_address    ipaddress
   )
{ /* Body */
   MQX_FILE_PTR   sockfd, telnetfd;
   sockaddr_in    addr;
   uint_32        sock;
   uint_32        error;
   boolean        work;
   int_32         c;

   /*
   ** Install device driver for socket and telnet
   */
   _io_socket_install("socket:");
   _io_telnet_install("telnet:");

   sock = socket(PF_INET, SOCK_STREAM, 0);
   if (sock == RTCS_SOCKET_ERROR) {
      return RTCSERR_OUT_OF_SOCKETS;
   } /* Endif */

   addr.sin_family      = AF_INET;
   addr.sin_port        = 0;
   addr.sin_addr.s_addr = INADDR_ANY;
   error = bind(sock,(const sockaddr *)&addr, sizeof(addr));
   if (error != RTCS_OK) {
      return error;
   } /* Endif */

   addr.sin_port        = IPPORT_TELNET;
   addr.sin_addr.s_addr = ipaddress;

   error = connect(sock, (const sockaddr *)(&addr), sizeof(addr));
   if (error != RTCS_OK) {
      shutdown(sock, FLAG_ABORT_CONNECTION);
      return error;
   } /* Endif */

   sockfd = fopen("socket:", (char_ptr)sock);
   if (sockfd == NULL) {
      shutdown(sock, FLAG_ABORT_CONNECTION);
      return RTCSERR_FOPEN_FAILED;
   } /* Endif */

   telnetfd = fopen("telnet:", (char_ptr)sockfd);
   if (telnetfd == NULL) {
      fclose(sockfd);
      shutdown(sock, FLAG_ABORT_CONNECTION);
      return RTCSERR_FOPEN_FAILED;
   } /* Endif */

   /* Set the console stream to the client  */
   ioctl(telnetfd, IO_IOCTL_SET_STREAM, (uint_32_ptr)((void _PTR_)stdin));
   while (TRUE) {

      work = FALSE;
      if (fstatus(stdin)) {
         work = TRUE;
         c = (int_32)fgetc(stdin);
         if (fputc(c & 0x7F, telnetfd) == IO_EOF)  {
            break;   
         }
      } /* Endif */

      if (fstatus(telnetfd)) {
         work = TRUE;
         c = (int_32)fgetc(telnetfd);
         if (c == IO_EOF) {
            break;
         }/* Endif */
         fputc(c & 0x7F, stdout);
      } /* Endif */

      /* Let another task run if there is no I/O */
      if (!work) {
         RTCS_time_delay(1);
      } /* Endif */

   } /* Endwhile */

   fclose(telnetfd);
   fclose(sockfd);
   shutdown(sock, FLAG_CLOSE_TX);

   return RTCS_OK;

} /* Endbody */