utilities.c

来自「Linux下的socks的程序源程序,用于socks的代理服务」· C语言 代码 · 共 819 行 · 第 1/2 页

C
819
字号
/* * Copyright (c) 1988 Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms are permitted provided * that: (1) source distributions retain this entire copyright notice and * comment, and (2) distributions including binaries display the following * acknowledgement:  ``This product includes software developed by the * University of California, Berkeley and its contributors'' in the * documentation or other materials provided with the distribution and in * all advertising materials mentioning features or use of this software. * 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */#ifndef lintstatic char sccsid[] = "@(#)utilities.c	5.2 (Berkeley) 3/1/91";#endif /* not lint */#define	TELOPTS#define	TELCMDS#define	SLC_NAMES#include <arpa/telnet.h>#include <sys/types.h>#if defined(ISC)#include <sys/bsdtypes.h>#endif /* #if defined(ISC) */#include <sys/time.h>#if defined(_SEQUENT_)#include <sys/select.h>#endif#include <ctype.h>#include "general.h"#include "fdset.h"#include "ring.h"#include "defines.h"#include "externs.h"FILE	*NetTrace = 0;		/* Not in bss, since needs to stay */int	prettydump;/* * upcase() * *	Upcase (in place) the argument. */    voidupcase(argument)    register char *argument;{    register int c;    while ((c = *argument) != 0) {	if (islower(c)) {	    *argument = toupper(c);	}	argument++;    }}/* * SetSockOpt() * * Compensate for differences in 4.2 and 4.3 systems. */    intSetSockOpt(fd, level, option, yesno)    int fd, level, option, yesno;{#ifndef	NOT43    return setsockopt(fd, level, option,				(char *)&yesno, sizeof yesno);#else	/* NOT43 */    if (yesno == 0) {		/* Can't do that in 4.2! */	fprintf(stderr, "Error: attempt to turn off an option 0x%x.\n",				option);	return -1;    }    return setsockopt(fd, level, option, 0, 0);#endif	/* NOT43 */}/* * The following are routines used to print out debugging information. */unsigned char NetTraceFile[256] = "(standard output)";    voidSetNetTrace(file)    register char *file;{    if (NetTrace && NetTrace != stdout)	fclose(NetTrace);    if (file  && (strcmp(file, "-") != 0)) {	NetTrace = fopen(file, "w");	if (NetTrace) {	    strcpy((char *)NetTraceFile, file);	    return;	}	fprintf(stderr, "Cannot open %s.\n", file);    }    NetTrace = stdout;    strcpy((char *)NetTraceFile, "(standard output)");}    voidDump(direction, buffer, length)    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;    extern pettydump;    offset = 0;    while (length) {	/* print one line */	fprintf(NetTrace, "%c 0x%x\t", direction, offset);	pThis = buffer;	if (prettydump) {	    buffer = buffer + min(length, BYTES_PER_LINE/2);	    while (pThis < buffer) {		fprintf(NetTrace, "%c%.2x",		    (((*pThis)&0xff) == 0xff) ? '*' : ' ',		    (*pThis)&0xff);		pThis++;	    }	    length -= BYTES_PER_LINE/2;	    offset += BYTES_PER_LINE/2;	} else {	    buffer = buffer + min(length, BYTES_PER_LINE);	    while (pThis < buffer) {		fprintf(NetTrace, "%.2x", (*pThis)&0xff);		pThis++;	    }	    length -= BYTES_PER_LINE;	    offset += BYTES_PER_LINE;	}	if (NetTrace == stdout) {	    fprintf(NetTrace, "\r\n");	} else {	    fprintf(NetTrace, "\n");	}	if (length < 0) {	    fflush(NetTrace);	    return;	}	/* find next unique line */    }    fflush(NetTrace);}	voidprintoption(direction, cmd, option)	char *direction;	int cmd, option;{	if (!showoptions)		return;	if (cmd == IAC) {		if (TELCMD_OK(option))		    fprintf(NetTrace, "%s IAC %s", direction, TELCMD(option));		else		    fprintf(NetTrace, "%s IAC %d", direction, option);	} else {		register char *fmt;		fmt = (cmd == WILL) ? "WILL" : (cmd == WONT) ? "WONT" :			(cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;		if (fmt) {		    fprintf(NetTrace, "%s %s ", direction, fmt);		    if (TELOPT_OK(option))			fprintf(NetTrace, "%s", TELOPT(option));		    else if (option == TELOPT_EXOPL)			fprintf(NetTrace, "EXOPL");		    else			fprintf(NetTrace, "%d", option);		} else		    fprintf(NetTrace, "%s %d %d", direction, cmd, option);	}	if (NetTrace == stdout)	    fprintf(NetTrace, "\r\n");	else	    fprintf(NetTrace, "\n");	return;}    voidoptionstatus(){    register int i;    extern char will_wont_resp[], do_dont_resp[];    for (i = 0; i < 256; i++) {	if (do_dont_resp[i]) {	    if (TELOPT_OK(i))		printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);	    else if (TELCMD_OK(i))		printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);	    else		printf("resp DO_DONT %d: %d\n", i,				do_dont_resp[i]);	    if (my_want_state_is_do(i)) {		if (TELOPT_OK(i))		    printf("want DO   %s\n", TELOPT(i));		else if (TELCMD_OK(i))		    printf("want DO   %s\n", TELCMD(i));		else		    printf("want DO   %d\n", i);	    } else {		if (TELOPT_OK(i))		    printf("want DONT %s\n", TELOPT(i));		else if (TELCMD_OK(i))		    printf("want DONT %s\n", TELCMD(i));		else		    printf("want DONT %d\n", i);	    }	} else {	    if (my_state_is_do(i)) {		if (TELOPT_OK(i))		    printf("     DO   %s\n", TELOPT(i));		else if (TELCMD_OK(i))		    printf("     DO   %s\n", TELCMD(i));		else		    printf("     DO   %d\n", i);	    }	}	if (will_wont_resp[i]) {	    if (TELOPT_OK(i))		printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);	    else if (TELCMD_OK(i))		printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);	    else		printf("resp WILL_WONT %d: %d\n",				i, will_wont_resp[i]);	    if (my_want_state_is_will(i)) {		if (TELOPT_OK(i))		    printf("want WILL %s\n", TELOPT(i));		else if (TELCMD_OK(i))		    printf("want WILL %s\n", TELCMD(i));		else		    printf("want WILL %d\n", i);	    } else {		if (TELOPT_OK(i))		    printf("want WONT %s\n", TELOPT(i));		else if (TELCMD_OK(i))		    printf("want WONT %s\n", TELCMD(i));		else		    printf("want WONT %d\n", i);	    }	} else {	    if (my_state_is_will(i)) {		if (TELOPT_OK(i))		    printf("     WILL %s\n", TELOPT(i));		else if (TELCMD_OK(i))		    printf("     WILL %s\n", TELCMD(i));		else		    printf("     WILL %d\n", i);	    }	}    }}    voidprintsub(direction, pointer, length)    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 ||	(want_status_response && (pointer[0] == TELOPT_STATUS))) {	if (direction) {	    fprintf(NetTrace, "%s IAC SB ",				(direction == '<')? "RCVD":"SENT");	    if (length >= 3) {		register int j;		i = pointer[length-2];		j = pointer[length-1];		if (i != IAC || j != SE) {		    fprintf(NetTrace, "(terminated by ");		    if (TELOPT_OK(i))			fprintf(NetTrace, "%s ", TELOPT(i));		    else if (TELCMD_OK(i))			fprintf(NetTrace, "%s ", TELCMD(i));		    else			fprintf(NetTrace, "%d ", i);		    if (TELOPT_OK(j))			fprintf(NetTrace, "%s", TELOPT(j));		    else if (TELCMD_OK(j))			fprintf(NetTrace, "%s", TELCMD(j));		    else			fprintf(NetTrace, "%d", j);		    fprintf(NetTrace, ", not IAC SE!) ");		}	    }	    length -= 2;	}	if (length < 1) {	    fprintf(NetTrace, "(Empty suboption\?\?\?)");	    return;	}	switch (pointer[0]) {	case TELOPT_TTYPE:	    fprintf(NetTrace, "TERMINAL-TYPE ");	    switch (pointer[1]) {	    case TELQUAL_IS:		fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);		break;	    case TELQUAL_SEND:		fprintf(NetTrace, "SEND");		break;	    default:		fprintf(NetTrace,				"- unknown qualifier %d (0x%x).",				pointer[1], pointer[1]);	    }	    break;	case TELOPT_TSPEED:	    fprintf(NetTrace, "TERMINAL-SPEED");	    if (length < 2) {		fprintf(NetTrace, " (empty suboption\?\?\?)");		break;	    }	    switch (pointer[1]) {	    case TELQUAL_IS:		fprintf(NetTrace, " IS ");		fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);		break;	    default:		if (pointer[1] == 1)		    fprintf(NetTrace, " SEND");		else		    fprintf(NetTrace, " %d (unknown)", pointer[1]);		for (i = 2; i < length; i++)		    fprintf(NetTrace, " ?%d?", pointer[i]);		break;	    }	    break;	case TELOPT_LFLOW:	    fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");	    if (length < 2) {		fprintf(NetTrace, " (empty suboption\?\?\?)");		break;	    }	    switch (pointer[1]) {	    case 0:		fprintf(NetTrace, " OFF"); break;	    case 1:		fprintf(NetTrace, " ON"); break;	    default:		fprintf(NetTrace, " %d (unknown)", pointer[1]);	    }	    for (i = 2; i < length; i++)		fprintf(NetTrace, " ?%d?", pointer[i]);	    break;	case TELOPT_NAWS:	    fprintf(NetTrace, "NAWS");	    if (length < 2) {		fprintf(NetTrace, " (empty suboption\?\?\?)");		break;	    }	    if (length == 2) {		fprintf(NetTrace, " ?%d?", pointer[1]);		break;	    }	    fprintf(NetTrace, " %d %d (%d)",		pointer[1], pointer[2],		(int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));	    if (length == 4) {		fprintf(NetTrace, " ?%d?", pointer[3]);		break;	    }	    fprintf(NetTrace, " %d %d (%d)",		pointer[3], pointer[4],		(int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));	    for (i = 5; i < length; i++)		fprintf(NetTrace, " ?%d?", pointer[i]);	    break;#if	defined(AUTHENTICATE)	case TELOPT_AUTHENTICATION:	    fprintf(NetTrace, "AUTHENTICATION");	    if (length < 2) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?