utilities.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright 1994-2002 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
*/
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)utilities.c 8.1 (Berkeley) 6/6/93";
#endif /* not lint */
#define TELOPTS
#ifdef lint
static char *telcmds[] = {0};
static char *slc_names[] = {0};
static char *encrypt_names[] = {0};
static char *enctype_names[] = {0};
#else /* lint */
#define TELCMDS
#define SLC_NAMES
#endif /* lint */
#include <errno.h>
#include <ctype.h>
#include "general.h"
#include "ring.h"
#include "defines.h"
#include "externs.h"
int prettydump;
/*
* upcase()
*
* Upcase (in place) the argument.
*/
void
register char *argument;
{
register int c;
while ((c = *argument) != 0) {
if (islower(c)) {
}
argument++;
}
}
/*
* SetSockOpt()
*
* Compensate for differences in 4.2 and 4.3 systems.
*/
int
{
}
/*
* The following are routines used to print out debugging information.
*/
void
register char *file;
{
if (NetTrace) {
return;
}
}
}
void
char direction;
unsigned char *buffer;
int length;
{
#define BYTES_PER_LINE 32
#define min(x, y) ((x < y) ? x:y)
unsigned char *pThis;
int offset;
offset = 0;
while (length) {
/* print one line */
if (prettydump) {
(*pThis)&0xff);
pThis++;
}
} else {
pThis++;
}
length -= BYTES_PER_LINE;
offset += BYTES_PER_LINE;
}
} else {
}
if (length < 0) {
return;
}
/* find next unique line */
}
}
void
char *direction;
{
if (!showoptions)
return;
else
option);
} else {
register char *fmt;
if (fmt) {
else if (option == TELOPT_EXOPL)
else
} else
option);
}
} else {
}
}
void
{
register int i;
extern char will_wont_resp[], do_dont_resp[];
for (i = 0; i < SUBBUFSIZE; i++) {
if (do_dont_resp[i]) {
if (TELOPT_OK(i))
(void) printf("resp DO_DONT %s: %d\n",
TELOPT(i), do_dont_resp[i]);
else if (TELCMD_OK(i))
(void) printf("resp DO_DONT %s: %d\n",
TELCMD(i), do_dont_resp[i]);
else
(void) printf("resp DO_DONT %d: %d\n", i,
do_dont_resp[i]);
if (my_want_state_is_do(i)) {
if (TELOPT_OK(i))
(void) printf("want DO %s\n",
TELOPT(i));
else if (TELCMD_OK(i))
(void) printf("want DO %s\n",
TELCMD(i));
else
(void) printf("want DO %d\n", i);
} else {
if (TELOPT_OK(i))
(void) printf("want DONT %s\n",
TELOPT(i));
else if (TELCMD_OK(i))
(void) printf("want DONT %s\n",
TELCMD(i));
else
(void) printf("want DONT %d\n", i);
}
} else {
if (my_state_is_do(i)) {
if (TELOPT_OK(i))
(void) printf(" DO %s\n",
TELOPT(i));
else if (TELCMD_OK(i))
(void) printf(" DO %s\n",
TELCMD(i));
else
(void) printf(" DO %d\n", i);
}
}
if (will_wont_resp[i]) {
if (TELOPT_OK(i))
(void) printf("resp WILL_WONT %s: %d\n",
TELOPT(i), will_wont_resp[i]);
else if (TELCMD_OK(i))
(void) printf("resp WILL_WONT %s: %d\n",
TELCMD(i), will_wont_resp[i]);
else
(void) printf("resp WILL_WONT %d: %d\n",
i, will_wont_resp[i]);
if (my_want_state_is_will(i)) {
if (TELOPT_OK(i))
(void) printf("want WILL %s\n",
TELOPT(i));
else if (TELCMD_OK(i))
(void) printf("want WILL %s\n",
TELCMD(i));
else
(void) printf("want WILL %d\n", i);
} else {
if (TELOPT_OK(i))
(void) printf("want WONT %s\n",
TELOPT(i));
else if (TELCMD_OK(i))
(void) printf("want WONT %s\n",
TELCMD(i));
else
(void) printf("want WONT %d\n", i);
}
} else {
if (my_state_is_will(i)) {
if (TELOPT_OK(i))
(void) printf(" WILL %s\n",
TELOPT(i));
else if (TELCMD_OK(i))
(void) printf(" WILL %s\n",
TELCMD(i));
else
(void) printf(" WILL %d\n", i);
}
}
}
}
void
char direction; /* '<' or '>' */
unsigned char *pointer; /* where suboption data sits */
int length; /* length of suboption data */
{
register int i;
char buf[512];
extern int want_status_response;
if (showoptions || direction == 0 ||
if (direction) {
if (length >= 3) {
register int j;
"(terminated by ");
if (TELOPT_OK(i))
TELOPT(i));
else if (TELCMD_OK(i))
TELCMD(i));
else
i);
if (TELOPT_OK(j))
TELOPT(j));
else if (TELCMD_OK(j))
TELCMD(j));
else
j);
", not IAC SE!) ");
}
}
length -= 2;
}
if (length < 1) {
return;
}
switch (pointer[0]) {
case TELOPT_TTYPE:
switch (pointer[1]) {
case TELQUAL_IS:
length-2,
(char *)pointer+2);
break;
case TELQUAL_SEND:
break;
default:
"- unknown qualifier %d (0x%x).",
}
break;
case TELOPT_TSPEED:
if (length < 2) {
" (empty suboption??\?)");
break;
}
switch (pointer[1]) {
case TELQUAL_IS:
(char *)pointer+2);
break;
default:
else
for (i = 2; i < length; i++)
pointer[i]);
break;
}
break;
case TELOPT_LFLOW:
if (length < 2) {
" (empty suboption??\?)");
break;
}
switch (pointer[1]) {
case LFLOW_OFF:
break;
case LFLOW_ON:
break;
case LFLOW_RESTART_ANY:
break;
case LFLOW_RESTART_XON:
break;
default:
pointer[1]);
}
for (i = 2; i < length; i++)
pointer[i]);
break;
case TELOPT_NAWS:
if (length < 2) {
" (empty suboption??\?)");
break;
}
if (length == 2) {
break;
}
((unsigned int)pointer[2])));
if (length == 4) {
break;
}
((unsigned int)pointer[4])));
for (i = 5; i < length; i++)
break;
case TELOPT_AUTHENTICATION:
if (length < 2) {
" (empty suboption??\?)");
break;
}
switch (pointer[1]) {
case TELQUAL_REPLY:
case TELQUAL_IS:
"IS" : "REPLY");
else
pointer[2]);
if (length < 3) {
"(partial suboption??\?)");
break;
}
break;
case TELQUAL_SEND:
i = 2;
while (i < length) {
if (AUTHTYPE_NAME_OK(pointer[i]))
AUTHTYPE_NAME(pointer[i]));
else
pointer[i]);
if (++i >= length) {
"(partial "
"suboption??\?)");
break;
}
((pointer[i] & AUTH_WHO_MASK) ==
"CLIENT" : "SERVER",
((pointer[i] & AUTH_HOW_MASK) ==
"MUTUAL" : "ONE-WAY");
++i;
}
break;
case TELQUAL_NAME:
i = 2;
while (i < length)
break;
default:
for (i = 2; i < length; i++)
break;
}
break;
case TELOPT_ENCRYPT:
if (length < 2) {
" (empty suboption??\?)");
break;
}
switch (pointer[1]) {
case ENCRYPT_START:
break;
case ENCRYPT_END:
break;
case ENCRYPT_REQSTART:
break;
case ENCRYPT_REQEND:
break;
case ENCRYPT_IS:
case ENCRYPT_REPLY:
"IS" : "REPLY");
if (length < 3) {
"suboption??\?)");
break;
}
else
break;
case ENCRYPT_SUPPORT:
i = 2;
while (i < length) {
if (ENCTYPE_NAME_OK(pointer[i]))
ENCTYPE_NAME(pointer[i]));
else
pointer[i]);
i++;
}
break;
case ENCRYPT_ENC_KEYID:
goto encommon;
case ENCRYPT_DEC_KEYID:
goto encommon;
default:
pointer[1]);
for (i = 2; i < length; i++)
pointer[i]);
break;
}
break;
case TELOPT_LINEMODE:
if (length < 2) {
" (empty suboption??\?)");
break;
}
switch (pointer[1]) {
case WILL:
goto common;
case WONT:
goto common;
case DO:
goto common;
case DONT:
if (length < 3) {
"(no option??\?)");
break;
}
switch (pointer[2]) {
case LM_FORWARDMASK:
"Forward Mask");
for (i = 3; i < length; i++)
pointer[i]);
break;
default:
pointer[2]);
for (i = 3; i < length; i++)
" %d", pointer[i]);
break;
}
break;
case LM_SLC:
i+SLC_FUNC]));
else
case SLC_NOSUPPORT:
" NOSUPPORT");
break;
case SLC_CANTCHANGE:
" CANTCHANGE");
break;
case SLC_VARIABLE:
" VARIABLE");
break;
case SLC_DEFAULT:
" DEFAULT");
break;
}
"|ACK" : "",
"|FLUSHIN" : "",
"|FLUSHOUT" : "");
i++;
}
for (; i < length; i++)
pointer[i]);
break;
case LM_MODE:
if (length < 3) {
"(no mode??\?)");
break;
}
{
char tbuf[64];
"|TRAPSIG" : "",
"|SOFT_TAB" : "",
"|LIT_ECHO" : "",
}
pointer[2]);
for (i = 3; i < length; i++)
pointer[i]);
break;
default:
pointer[1]);
for (i = 2; i < length; i++)
pointer[i]);
}
break;
case TELOPT_STATUS: {
register char *cp;
register int j, k;
switch (pointer[1]) {
default:
" SEND");
else
" %d (unknown)",
pointer[1]);
for (i = 2; i < length; i++)
pointer[i]);
break;
case TELQUAL_IS:
if (--want_status_response < 0)
want_status_response = 0;
" IS\r\n");
else
" IS\n");
for (i = 2; i < length; i++) {
switch (pointer[i]) {
case DO:
cp = "DO";
goto common2;
case DONT:
cp = "DONT";
goto common2;
case WILL:
cp = "WILL";
goto common2;
case WONT:
cp = "WONT";
goto common2;
i++;
if (TELOPT_OK(
(int)pointer[i]))
(void) fprintf(
" %s %s",
cp,
i]));
else
(void) fprintf(
" %s %d",
cp,
pointer[i]);
(void) fprintf(
"\r\n");
else
(void) fprintf(
"\n");
break;
case SB:
" SB ");
i++;
j = k = i;
while (j < length) {
if (j+1 == length)
break;
j++;
else
break;
}
}
printsub(0,
&pointer[i], k - i);
if (i < length) {
i = j;
} else
i = j - 1;
else
break;
default:
" %d", pointer[i]);
break;
}
}
break;
}
break;
}
case TELOPT_XDISPLOC:
switch (pointer[1]) {
case TELQUAL_IS:
break;
case TELQUAL_SEND:
break;
default:
"- unknown qualifier %d (0x%x).",
}
break;
case TELOPT_NEW_ENVIRON:
#ifdef OLD_ENVIRON
goto env_common1;
case TELOPT_OLD_ENVIRON:
#endif
switch (pointer[1]) {
case TELQUAL_IS:
goto env_common;
case TELQUAL_SEND:
goto env_common;
case TELQUAL_INFO:
{
register int noquote = 2;
#if defined(ENV_HACK) && defined(OLD_ENVIRON)
extern int old_env_var, old_env_value;
#endif
for (i = 2; i < length; i++) {
switch (pointer[i]) {
case NEW_ENV_VALUE:
#ifdef OLD_ENVIRON
/* case NEW_ENV_OVAR: */
if (pointer[0] == TELOPT_OLD_ENVIRON) {
#ifdef ENV_HACK
if (old_env_var == OLD_ENV_VALUE)
"\" (VALUE) " + noquote);
else
#endif
"\" VAR " + noquote);
} else
#endif /* OLD_ENVIRON */
noquote = 2;
break;
case NEW_ENV_VAR:
#ifdef OLD_ENVIRON
/* case OLD_ENV_VALUE: */
if (pointer[0] == TELOPT_OLD_ENVIRON) {
#ifdef ENV_HACK
if (old_env_value == OLD_ENV_VAR)
"\" (VAR) " + noquote);
else
#endif
"\" VALUE " + noquote);
} else
#endif /* OLD_ENVIRON */
noquote = 2;
break;
case ENV_ESC:
noquote = 2;
break;
case ENV_USERVAR:
noquote = 2;
break;
default:
if (noquote) {
noquote = 0;
}
} else {
pointer[i]);
noquote = 2;
}
break;
}
}
if (!noquote)
break;
}
}
break;
default:
else
for (i = 1; i < length; i++)
break;
}
if (direction) {
else
}
}
}
/*
* EmptyTerminal - called to make sure that the terminal buffer is empty.
* Note that we consider the buffer to run all the
* way to the kernel (thus the select).
*/
static void
{
fd_set o;
FD_ZERO(&o);
if (TTYBYTES() == 0) {
/* wait for TTLOWAT */
} else {
while (TTYBYTES()) {
if (ttyflush(0) == -2) {
/* This will not return. */
fatal_tty_error("write");
}
/* wait for TTLOWAT */
}
}
}
static void
{
setconnmode(0);
do {
(void) telrcv(); /* Process any incoming data */
setconnmode(0);
EmptyTerminal(); /* Flush the path to the tty */
}
void
int returnCode;
{
SetForExit();
}
void
char *string;
int returnCode;
{
SetForExit();
}
#define BUFFER_CHUNK_SIZE 64
/* Round up to a multiple of BUFFER_CHUNK_SIZE */
/*
* Optionally allocate a buffer, and optionally read a string from a stream
* into the buffer, starting at the given offset. If the buffer isn't
* large enough for the given offset, or if buffer space is exhausted
* when reading the string, the size of the buffer is increased.
*
* A buffer can be supplied when the function is called, passing the
* buffer address via the first argument. The buffer size can be
* passed as well, in the second argument. If the second argument is
* NULL, the function makes no assumptions about the buffer size.
* The address of the buffer is returned via the first argument, and the
* buffer size via the second argument if this is not NULL.
* These returned values may differ from the supplied values if the buffer
* was reallocated.
*
* If no buffer is to be supplied, specify a buffer address of NULL, via
* the first argument.
*
* If the pointer to the buffer address is NULL, the function just returns
* NULL, and performs no other processing.
*
* If a NULL stream is passed, the function will just make sure the
* supplied buffer is large enough to hold the supplied offset,
* reallocating it if is too small or too large.
*
* The returned buffer will be a multiple of BUFFER_CHUNK_SIZE in size.
*
* The function stops reading from the stream when a newline is read,
* end of file is reached, or an error occurs. The newline is not
* returned in the buffer. The returned string will be NULL terminated.
*
* The function returns the address of the buffer if any characters
* are read and no error occurred, otherwise it returns NULL.
*
* If the function returns NULL, a buffer may have been allocated. The
* buffer address will be returned via the first argument, together with
* the buffer size if the second argument is not NULL.
*
*/
static char *
char **bufp;
unsigned int *cbufsiz;
unsigned int off;
{
unsigned int bufsiz;
char *buf;
char *nbuf;
return (NULL);
return (NULL);
}
return (buf);
for (;;) {
/* Expand the buffer as needed. */
/* Discard everything we read. */
break;
}
}
if (c == EOF || c == '\n') {
/* Retry if interrupted by a signal. */
continue;
}
/* No characters transferred? */
}
break;
}
}
return (buf);
}
/*
* Read a string from the supplied stream. Stop reading when a newline
* is read, end of file reached, or an error occurs.
*
* A buffer can be supplied by specifying the buffer address via the
* first argument. The buffer size can be passed via the second argument.
* If the second argument is NULL, the function makes no assumptions
* about the buffer size. The buffer will be reallocated if it is too
* small or too large for the returned string.
*
* If no buffer is to be supplied, specify a buffer address of NULL,
* via the first argument.
*
* If the first argument is NULL, the function just returns NULL, and
* performs no other processing.
*
* The function returns the address of the buffer if any characters are
* read and no error occurred.
*
* If the function returns NULL, a buffer may have been allocated. The
* buffer address and buffer size will be returned via the first argument,
* and the buffer size via the second argument, if this isn't NULL.
*/
char *
char **bufp;
unsigned int *bufsiz;
{
}
/*
* Allocate a buffer to hold a string of given length.
*
* An existing buffer can be reallocated by passing its address and via
* the first argument. The buffer size can be passed via the second
* argument. If the second argument is NULL, the function makes no
* assumptions about the buffer size.
*
* If no existing buffer is to be supplied, pass a NULL buffer address via
* the first argument.
*
* If the first argument is NULL, the function just returns NULL,
* and performs no other processing.
*/
char *
char **bufp;
unsigned int *bufsiz;
unsigned int size;
{
}
/*
* This function is similar to GetString(), except that the string read
* from the stream is appended to the supplied string.
*/
char *
char **bufp;
unsigned int *bufsiz;
char *str;
{
return (NULL);
}