network.c

来自「Linux下的socks的程序源程序,用于socks的代理服务」· C语言 代码 · 共 163 行

C
163
字号
/* * 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[] = "@(#)network.c	5.2 (Berkeley) 3/1/91";#endif /* not lint */#include <sys/types.h>#include <sys/socket.h>#include <sys/time.h>#include <errno.h>#if defined(ISC)#include <net/errno.h>#endif /* #if defined(ISC) */#include <arpa/telnet.h>#include "ring.h"#include "defines.h"#include "externs.h"#include "fdset.h"Ring		netoring, netiring;unsigned char	netobuf[2*BUFSIZ], netibuf[BUFSIZ];/* * Initialize internal network data structures. */    voidinit_network(){    if (ring_init(&netoring, netobuf, sizeof netobuf) != 1) {	exit(1);    }    if (ring_init(&netiring, netibuf, sizeof netibuf) != 1) {	exit(1);    }    NetTrace = stdout;}/* * Check to see if any out-of-band data exists on a socket (for * Telnet "synch" processing). */    intstilloob(){    static struct timeval timeout = { 0 };    fd_set	excepts;    int value;    do {	FD_ZERO(&excepts);	FD_SET(net, &excepts);	value = select(net+1, (fd_set *)0, (fd_set *)0, &excepts, &timeout);    } while ((value == -1) && (errno == EINTR));    if (value < 0) {	perror("select");	(void) quit();	/* NOTREACHED */    }    if (FD_ISSET(net, &excepts)) {	return 1;    } else {	return 0;    }}/* *  setneturg() * *	Sets "neturg" to the current location. */    voidsetneturg(){    ring_mark(&netoring);}/* *  netflush *		Send as much data as possible to the network, *	handling requests for urgent data. * *		The return value indicates whether we did any *	useful work. */    intnetflush(){    register int n, n1;    if ((n1 = n = ring_full_consecutive(&netoring)) > 0) {	if (!ring_at_mark(&netoring)) {	    n = send(net, netoring.consume, n, 0);	/* normal write */	} else {	    /*	     * In 4.2 (and 4.3) systems, there is some question about	     * what byte in a sendOOB operation is the "OOB" data.	     * To make ourselves compatible, we only send ONE byte	     * out of band, the one WE THINK should be OOB (though	     * we really have more the TCP philosophy of urgent data	     * rather than the Unix philosophy of OOB data).	     */	    n = send(net, netoring.consume, 1, MSG_OOB);/* URGENT data */	}    }    if (n < 0) {	if (errno != ENOBUFS && errno != EWOULDBLOCK) {	    setcommandmode();	    perror(hostname);	    (void)NetClose(net);	    ring_clear_mark(&netoring);	    longjmp(peerdied, -1);	    /*NOTREACHED*/	}	n = 0;    }    if (netdata && n) {	Dump('>', netoring.consume, n);    }    if (n) {	ring_consumed(&netoring, n);	/*	 * If we sent all, and more to send, then recurse to pick	 * up the other half.	 */	if ((n1 == n) && ring_full_consecutive(&netoring)) {	    (void) netflush();	}	return 1;    } else {	return 0;    }}

⌨️ 快捷键说明

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