Skip to content
telnet.c 24.1 KiB
Newer Older
Daniel Stenberg's avatar
Daniel Stenberg committed
/*****************************************************************************
 *                                  _   _ ____  _     
 *  Project                     ___| | | |  _ \| |    
 *                             / __| | | | |_) | |    
 *                            | (__| |_| |  _ <| |___ 
 *                             \___|\___/|_| \_\_____|
 *
Daniel Stenberg's avatar
Daniel Stenberg committed
 * Copyright (C) 2000, Daniel Stenberg, <daniel@haxx.se>, et al.
Daniel Stenberg's avatar
Daniel Stenberg committed
 *
Daniel Stenberg's avatar
Daniel Stenberg committed
 * In order to be useful for every potential user, curl and libcurl are
 * dual-licensed under the MPL and the MIT/X-derivate licenses.
Daniel Stenberg's avatar
Daniel Stenberg committed
 *
Daniel Stenberg's avatar
Daniel Stenberg committed
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the MPL or the MIT/X-derivate
 * licenses. You may pick one of these licenses.
Daniel Stenberg's avatar
Daniel Stenberg committed
 *
Daniel Stenberg's avatar
Daniel Stenberg committed
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
Daniel Stenberg's avatar
Daniel Stenberg committed
 *
Daniel Stenberg's avatar
Daniel Stenberg committed
 * $Id$
 *****************************************************************************/
Daniel Stenberg's avatar
Daniel Stenberg committed

Daniel Stenberg's avatar
Daniel Stenberg committed
/* -- WIN32 approved -- */
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <errno.h>

#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
#include <winsock.h>
#include <time.h>
#include <io.h>
#else
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <netinet/in.h>
#include <sys/time.h>
#include <sys/resource.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <netdb.h>
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#include <sys/ioctl.h>
#include <signal.h>

#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif

#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif


#endif

#include "urldata.h"
#include <curl/curl.h>
Daniel Stenberg's avatar
Daniel Stenberg committed
#include "sendf.h"

#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>

#define  TELOPTS
#define  TELCMDS

#include "arpa_telnet.h"

#define SUBBUFSIZE 512

#define  SB_CLEAR()  subpointer = subbuffer;
#define  SB_TERM()   { subend = subpointer; SB_CLEAR(); }
#define  SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
            *subpointer++ = (c); \
         }

#define  SB_GET() ((*subpointer++)&0xff)
#define  SB_PEEK()   ((*subpointer)&0xff)
#define  SB_EOF() (subpointer >= subend)
#define  SB_LEN() (subend - subpointer)

void telrcv(struct connectdata *,
Daniel Stenberg's avatar
Daniel Stenberg committed
	    unsigned char *inbuf,	/* Data received from socket */
	    int count);			/* Number of bytes received */

static void printoption(struct UrlData *data,
			const char *direction,
			int cmd, int option);

static void negotiate(struct connectdata *);
static void send_negotiation(struct connectdata *, int cmd, int option);
static void set_local_option(struct connectdata *, int cmd, int option);
static void set_remote_option(struct connectdata *, int cmd, int option);
Daniel Stenberg's avatar
Daniel Stenberg committed

static void printsub(struct UrlData *data,
		     int direction, unsigned char *pointer, int length);
static void suboption(struct connectdata *);
Daniel Stenberg's avatar
Daniel Stenberg committed

/* suboptions */
static char subbuffer[SUBBUFSIZE];
static char *subpointer, *subend;    /* buffer for sub-options */

/*
 * Telnet receiver states for fsm
 */
static enum
{
   TS_DATA = 0,
   TS_IAC,
   TS_WILL,
   TS_WONT,
   TS_DO,
   TS_DONT,
   TS_CR,
   TS_SB,   /* sub-option collection */
   TS_SE   /* looking for sub-option end */
} telrcv_state;

/* For negotiation compliant to RFC 1143 */
#define NO	0
#define YES 	1
#define WANTYES	2
#define WANTNO	3

#define EMPTY	 0
#define OPPOSITE 1

static int please_negotiate = 0;
static int already_negotiated = 0;
Daniel Stenberg's avatar
Daniel Stenberg committed
static int us[256]; 
static int usq[256]; 
static int us_preferred[256]; 
static int him[256]; 
static int himq[256]; 
static int him_preferred[256]; 

static char *subopt_ttype = NULL; /* Set with suboption TTYPE */
static char *subopt_xdisploc = NULL; /* Set with suboption XDISPLOC */
static struct curl_slist *telnet_vars = NULL; /* Environment variables */

void init_telnet(struct connectdata *conn)
Daniel Stenberg's avatar
Daniel Stenberg committed
{
  telrcv_state = TS_DATA;

  /* Init suboptions */
  SB_CLEAR();

  /* Set all options to NO */
  memset(us, NO, 256);
  memset(usq, NO, 256);
  memset(us_preferred, NO, 256);
  memset(him, NO, 256);
  memset(himq, NO, 256);
  memset(him_preferred, NO, 256);

  /* Set the options we want by default */
  us_preferred[TELOPT_BINARY] = YES;
  us_preferred[TELOPT_SGA] = YES;
  him_preferred[TELOPT_BINARY] = YES;
  him_preferred[TELOPT_SGA] = YES;
static void negotiate(struct connectdata *conn)
Daniel Stenberg's avatar
Daniel Stenberg committed
{
Daniel Stenberg's avatar
Daniel Stenberg committed
   
  for(i = 0;i < NTELOPTS;i++)
  {
    if(us_preferred[i] == YES)
      set_local_option(conn, i, YES);
Daniel Stenberg's avatar
Daniel Stenberg committed
      
    if(him_preferred[i] == YES)
      set_remote_option(conn, i, YES);
  }
Daniel Stenberg's avatar
Daniel Stenberg committed
}

static void printoption(struct UrlData *data,
			const char *direction, int cmd, int option)
{
  char *fmt;
  char *opt;
Daniel Stenberg's avatar
Daniel Stenberg committed
   
Loading
Loading full blame…