From ohcanada, 3 Years ago, written in Plain Text.
  1.     /* Perform terminal-type negotiation */
  2.     rc = send_packet (csock, do_term, sizeof(do_term),
  3.                         "IAC DO TERMINAL_TYPE");
  4.     if (rc < 0) return -1;
  5.  
  6.     rc = expect (csock, will_term, sizeof(will_term),
  7.                         "IAC WILL TERMINAL_TYPE");
  8.     if (rc < 0) return -1;
  9.  
  10.     /* Request terminal type */
  11.     rc = send_packet (csock, req_type, sizeof(req_type),
  12.                         "IAC SB TERMINAL_TYPE SEND IAC SE");
  13.     if (rc < 0) return -1;
  14.  
  15.     rc = recv_packet (csock, buf, sizeof(buf)-2, SE);
  16.     if (rc < 0) return -1;
  17.  
  18.     /* Ignore Putty Negotiate About Echo */
  19.     if (rc >= (int)sizeof(do_echo) &&
  20.         memcmp (buf, do_echo, sizeof(do_echo)) == 0)
  21.     {
  22.         TNSDEBUG2("console: DBGGM1: Received unsolicited DO ECHO\n");
  23.         memmove(buf, &buf[sizeof(do_echo)], (rc - sizeof(do_echo)));
  24.         rc -= sizeof(do_echo);
  25.     }
  26.  
  27.     /* Ignore First Putty Negotiate About SGA */
  28.     if (rc >= (int)sizeof(will_sga) &&
  29.         memcmp (buf, will_sga, sizeof(will_sga)) == 0)
  30.     {
  31.         TNSDEBUG2("console: DBGGM2: Received unsolicited WILL SGA\n");
  32.         memmove(buf, &buf[sizeof(will_sga)], (rc - sizeof(will_sga)));
  33.         rc -= sizeof(will_sga);
  34.     }
  35.  
  36.     /* Ignore Second Putty Negotiate About SGA */
  37.     if (rc >= (int)sizeof(do_sga) &&
  38.         memcmp (buf, do_sga, sizeof(do_sga)) == 0)
  39.     {
  40.         TNSDEBUG2("console: DBGGM3: Received unsolicited DO SGA\n");
  41.         memmove(buf, &buf[sizeof(do_sga)], (rc - sizeof(do_sga)));
  42.         rc -= sizeof(do_sga);
  43.     }
  44.  
  45.     /* Ignore Negotiate About Window Size */
  46.     if (rc >= (int)sizeof(will_naws) &&
  47.         memcmp (buf, will_naws, sizeof(will_naws)) == 0)
  48.     {
  49.         TNSDEBUG2("console: DBGGM4: Received unsolicited WILL NAWS\n");
  50.         memmove(buf, &buf[sizeof(will_naws)], (rc - sizeof(will_naws)));
  51.         rc -= sizeof(will_naws);
  52.     }
  53.  
  54.     if (rc < (int)(sizeof(type_is) + 2)
  55.         || memcmp(buf, type_is, sizeof(type_is)) != 0
  56.         || buf[rc-2] != IAC || buf[rc-1] != SE) {
  57.         TNSDEBUG2("console: DBG008: Expected IAC SB TERMINAL_TYPE IS\n");
  58.         return -1;
  59.     }
  60.  
  61.     buf[rc-2] = '\0';
  62.     termtype = (char *)(buf + sizeof(type_is));
  63.     TNSDEBUG2("console: DBG009: Received IAC SB TERMINAL_TYPE IS %s IAC SE\n",
  64.             termtype);
captcha