⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pppipcp.c

📁 uCLinux下的一个TCP/IP协议栈源码
💻 C
📖 第 1 页 / 共 2 页
字号:
				side_p->work.compression = side_p->want.compression;
				side_p->work.slots = side_p->want.slots;
				side_p->work.slot_compress = side_p->want.slot_compress;
			} else {
				side_p->work.compression = PPP_COMPR_PROTOCOL;
				side_p->work.slots = IPCP_SLOT_DEFAULT;
				side_p->work.slot_compress = IPCP_SLOT_COMPRESS;
			}
			option_result = CONFIG_NAK;
			break;
		};
		break;

	default:
		option_result = CONFIG_REJ;
		break;
	};

	if (option_p->type > IPCP_OPTION_LIMIT
	 || !(side_p->will_negotiate & (1 << option_p->type))) {
		option_result = CONFIG_REJ;
	}

	if ( toss < 0 )
		return -1;

	if ( !request  &&  toss > 0 ) {
		/* toss extra bytes in option */
		while( toss-- > 0 ) {
			if ( pullchar(bpp) == -1 )
				return -1;
		}
	}

	return (option_result);
}


/************************************************************************/
/* Check options requested by the remote host */
static int
ipcp_request(
struct fsm_s *fsm_p,
struct config_hdr *config,
struct mbuf **data
){
	struct ipcp_s *ipcp_p = fsm_p->pdv;
	int32 signed_length = config->len;
	struct mbuf *reply_bp = NULL;	/* reply packet */
	int reply_result = CONFIG_ACK;		/* reply to request */
	uint16 desired;				/* desired to negotiate */
	struct option_hdr option;		/* option header storage */
	int option_result;			/* option reply */

	PPP_DEBUG_ROUTINES("ipcp_request()");
	ipcp_p->remote.work.negotiate = FALSE;	/* clear flags */

	/* Process options requested by remote host */
	while (signed_length > 0  &&  ntohopt(&option, data) != -1) {
		if ((signed_length -= option.len) < 0) {
			PPP_DEBUG_CHECKS("IPCP REQ: bad header length");
			free_p(data);
			free_p(&reply_bp);
			return -1;
		}

		if ( ( option_result = ipcp_check( data, ipcp_p,
				&(ipcp_p->remote), &option, TRUE ) ) == -1 ) {
			PPP_DEBUG_CHECKS("IPCP REQ: ran out of data");
			free_p(data);
			free_p(&reply_bp);
			return -1;
		}

#ifdef PPP_DEBUG_OPTIONS
if (PPPtrace & PPP_DEBUG_OPTIONS) {
	trace_log(PPPiface, "IPCP REQ: result %s, option %d, length %d",
		fsmCodes[option_result],
		option.type,
		option.len);
}
#endif
		if ( option_result < reply_result ) {
			continue;
		} else if ( option_result > reply_result ) {
			/* Discard current list of replies */
			free_p(&reply_bp);
			reply_bp = NULL;
			reply_result = option_result;
		}

		/* remember that we processed option */
		if ( option_result != CONFIG_REJ
		 && option.type <= IPCP_OPTION_LIMIT ) {
			ipcp_p->remote.work.negotiate |= (1 << option.type);
		}

		/* Add option response to the return list */
		ipcp_option( &reply_bp, &(ipcp_p->remote.work),
			option.type, option.len, data );
	}

	/* Now check for any missing options which are desired */
	if ( fsm_p->retry_nak > 0
	 &&  (desired = ipcp_p->remote.want.negotiate
		       & ~ipcp_p->remote.work.negotiate) != 0 ) {
		switch ( reply_result ) {
		case CONFIG_ACK:
			free_p(&reply_bp);
			reply_bp = NULL;
			reply_result = CONFIG_NAK;
			/* fallthru */
		case CONFIG_NAK:
			ipcp_makeoptions( &reply_bp, &(ipcp_p->remote.want),
				desired );
			fsm_p->retry_nak--;
			break;
		case CONFIG_REJ:
			/* do nothing */
			break;
		};
	} else if ( reply_result == CONFIG_NAK ) {
		/* if too many NAKs, reject instead */
		if ( fsm_p->retry_nak > 0 )
			fsm_p->retry_nak--;
		else
			reply_result = CONFIG_REJ;
	}

	/* Send ACK/NAK/REJ to remote host */
	fsm_send(fsm_p, reply_result, config->id, &reply_bp);
	free_p(data);
	return (reply_result != CONFIG_ACK);
}


/************************************************************************/
/* Process configuration ACK sent by remote host */
static int
ipcp_ack(
struct fsm_s *fsm_p,
struct config_hdr *config,
struct mbuf **data
){
	struct mbuf *req_bp;
	int error = FALSE;

	PPP_DEBUG_ROUTINES("ipcp_ack()");

	/* ID field must match last request we sent */
	if (config->id != fsm_p->lastid) {
		PPP_DEBUG_CHECKS("IPCP ACK: wrong ID");
		free_p(data);
		return -1;
	}

	/* Get a copy of last request we sent */
	req_bp = ipcp_makereq(fsm_p);

	/* Overall buffer length should match */
	if (config->len != len_p(req_bp)) {
		PPP_DEBUG_CHECKS("IPCP ACK: buffer length mismatch");
		error = TRUE;
	} else {
		register int req_char;
		register int ack_char;

		/* Each byte should match */
		while ((req_char = pullchar(&req_bp)) != -1) {
			if ((ack_char = pullchar(data)) == -1
			 || ack_char != req_char ) {
				PPP_DEBUG_CHECKS("IPCP ACK: data mismatch");
				error = TRUE;
				break;
			}
		}
	}
	free_p(&req_bp);
	free_p(data);

	if (error) {
		return -1;
	}

	PPP_DEBUG_CHECKS("IPCP ACK: valid");
	return 0;
}


/************************************************************************/
/* Process configuration NAK sent by remote host */
static int
ipcp_nak(
struct fsm_s *fsm_p,
struct config_hdr *config,
struct mbuf **data
){
	struct ipcp_s *ipcp_p = fsm_p->pdv;
	struct ipcp_side_s *local_p = &(ipcp_p->local);
	int32 signed_length = config->len;
	struct option_hdr option;
	int last_option = 0;
	int result;

	PPP_DEBUG_ROUTINES("ipcp_nak()");

	/* ID field must match last request we sent */
	if (config->id != fsm_p->lastid) {
		PPP_DEBUG_CHECKS("IPCP NAK: wrong ID");
		free_p(data);
		return -1;
	}

	/* First, process in order.  Then, process extra "important" options */
	while (signed_length > 0  &&  ntohopt(&option, data) != -1) {
		if ((signed_length -= option.len) < 0) {
			PPP_DEBUG_CHECKS("IPCP NAK: bad header length");
			free_p(data);
			return -1;
		}
		if ( option.type > IPCP_OPTION_LIMIT ) {
			PPP_DEBUG_CHECKS("IPCP NAK: option out of range");
		} else if ( option.type < last_option
		 || !(local_p->work.negotiate & (1 << option.type)) ) {
			if (local_p->work.negotiate & (1 << option.type)) {
				PPP_DEBUG_CHECKS("IPCP NAK: option out of order");
				free_p(data);
				return -1;		/* was requested */
			}
			local_p->work.negotiate |= (1 << option.type);
			last_option = IPCP_OPTION_LIMIT + 1;
		} else {
			last_option = option.type;
		}
		if ( ( result = ipcp_check( data, ipcp_p,
				local_p, &option, FALSE ) ) == -1 ) {
			PPP_DEBUG_CHECKS("IPCP NAK: ran out of data");
			free_p(data);
			return -1;
		}
		/* update the negotiation status */
		if ( result == CONFIG_REJ
		  && option.type <= IPCP_OPTION_LIMIT ) {
			local_p->work.negotiate &= ~(1 << option.type);
		}
	}
	PPP_DEBUG_CHECKS("IPCP NAK: valid");
	free_p(data);
	return 0;
}


/************************************************************************/
/* Process configuration reject sent by remote host */
static int
ipcp_reject(
struct fsm_s *fsm_p,
struct config_hdr *config,
struct mbuf **data
){
	struct ipcp_s *ipcp_p = fsm_p->pdv;
	struct ipcp_side_s *local_p = &(ipcp_p->local);
	int32 signed_length = config->len;
	struct option_hdr option;
	int last_option = 0;

	PPP_DEBUG_ROUTINES("ipcp_reject()");

	/* ID field must match last request we sent */
	if (config->id != fsm_p->lastid) {
		PPP_DEBUG_CHECKS("IPCP REJ: wrong ID");
		free_p(data);
		return -1;
	}

	/* Process in order, checking for errors */
	while (signed_length > 0  &&  ntohopt(&option, data) != -1) {
		register int k;

		if ((signed_length -= option.len) < 0) {
			PPP_DEBUG_CHECKS("IPCP REJ: bad header length");
			free_p(data);
			return -1;
		}
		if ( option.type > IPCP_OPTION_LIMIT ) {
			PPP_DEBUG_CHECKS("IPCP REJ: option out of range");
		} else if (option.type < last_option
		 || !(local_p->work.negotiate & (1 << option.type))) {
			PPP_DEBUG_CHECKS("IPCP REJ: option out of order");
			free_p(data);
			return -1;
		}
		for ( k = option.len - OPTION_HDR_LEN; k-- > 0; ) {
			if ( pullchar(data) == -1 ) {
				PPP_DEBUG_CHECKS("IPCP REJ: ran out of data");
				free_p(data);
				return -1;
			}
		}
		last_option = option.type;

		if ( option.type <= IPCP_OPTION_LIMIT ) {
			local_p->work.negotiate &= ~(1 << option.type);
		}
	}
	PPP_DEBUG_CHECKS("IPCP REJ: valid");
	free_p(data);
	return 0;
}


/************************************************************************/
/*			I N I T I A L I Z A T I O N			*/
/************************************************************************/

/* Reset configuration options before request */
static void
ipcp_reset(fsm_p)
struct fsm_s *fsm_p;
{
	struct ipcp_s *ipcp_p =	fsm_p->pdv;

	PPP_DEBUG_ROUTINES("ipcp_reset()");

	ASSIGN( ipcp_p->local.work, ipcp_p->local.want );
	ipcp_p->local.work.other = ipcp_p->remote.want.address;
	ipcp_p->local.will_negotiate |= ipcp_p->local.want.negotiate;

	ipcp_p->remote.work.negotiate = FALSE;
	ipcp_p->remote.will_negotiate |= ipcp_p->remote.want.negotiate;
}


/************************************************************************/
/* After termination */
static void
ipcp_stopping(fsm_p)
struct fsm_s *fsm_p;
{
	PPP_DEBUG_ROUTINES("ipcp_stopping()");
}


/************************************************************************/
/* Close IPCP */
static void
ipcp_closing(fsm_p)
struct fsm_s *fsm_p;
{
	struct ipcp_s *ipcp_p = 	fsm_p->pdv;

	/* free old slhc configuration, if any */
	slhc_free( ipcp_p->slhcp );
	ipcp_p->slhcp = NULL;

#ifdef notdef
	if (PPPtrace > 1)
		trace_log(PPPiface,"%s PPP/IPCP Drop route to peer (%s)",
			ifp->name,
			inet_ntoa(ipcp_p->local.work.other);

	rt_drop(ipcp_p->local.work.other, (unsigned int)32);
#endif
}


/************************************************************************/
/* configuration negotiation complete */
static void
ipcp_opening(fsm_p)
struct fsm_s *fsm_p;
{
	struct ipcp_s *ipcp_p = 	fsm_p->pdv;
	struct iface *ifp = 		fsm_p->ppp_p->iface;
	int32 address = ipcp_p->local.work.address;
	int rslots = 0;
	int tslots = 0;

	/* Set our IP address to reflect negotiated option */
	if (address != ifp->addr) {
		/* address not the same as last time */
		if (Ip_addr == 0L) {
			/* no global address */
			Ip_addr = address;
		} else if ( Ip_addr == ifp->addr ) {
			/* global was same as local; must be replaced */
			/* !!! TO DO: reset tcp connections */
			Ip_addr = address;
		}
		ifp->addr = address;

		if (PPPtrace > 1)
			trace_log(PPPiface,"%s PPP/IPCP Setting new IP address: %s",
				ifp->name,
				inet_ntoa(address));
	}

#ifdef notdef
	rt_add(ipcp_p->local.work.other, (unsigned int)32, (int32)0,
		ifp, (int32)1, (int32)0, (char)1);

	if (PPPtrace > 1)
		trace_log(PPPiface,"%s PPP/IPCP Add route to peer (%s)",
			ifp->name,
			inet_ntoa(ipcp_p->local.work.other);
#endif

	/* free old slhc configuration, if any */
	slhc_free( ipcp_p->slhcp );
	ipcp_p->slhcp = NULL;

	if (ipcp_p->local.work.negotiate & IPCP_N_COMPRESS) {
		rslots = ipcp_p->local.work.slots;
	}
	if (ipcp_p->remote.work.negotiate & IPCP_N_COMPRESS) {
		tslots = ipcp_p->remote.work.slots;
	}

	if ( rslots != 0 || tslots != 0 ) {
		ipcp_p->slhcp = slhc_init( rslots, tslots );

		if (PPPtrace > 1)
			trace_log(PPPiface,"%s PPP/IPCP Compression enabled;"
				" Recv slots = %d, flag = %x;"
				" Xmit slots = %d, flag = %x",
				ifp->name,
				rslots,
				ipcp_p->local.work.slot_compress,
				tslots,
				ipcp_p->remote.work.slot_compress);
	}
}


/************************************************************************/
/* Check the address against all other assigned addresses */
static int32
ipcp_addr_idle(addr)
int32 addr;
{
	struct iface *ifp;

	/* Check if peer IP address is already in use on another interface */
	/* !!! need to look at *remote* address, not local! */
	for (ifp=Ifaces; ifp != NULL; ifp = ifp->next) {
		if (ifp->addr == addr)
			return 0L;
	}
	return addr;
}


/************************************************************************/
/* Assign the next unused address from a pool */
static int32
ipcp_poolnext(ipcp_p)
struct ipcp_s *ipcp_p;
{
	int32 i = 1L + ipcp_p->peer_max - ipcp_p->peer_min;
	int32 nextaddr = 0L;

	while ( i-- > 0  &&  nextaddr == 0L ) {
		if (++ipcp_p->local.want.other < ipcp_p->peer_min
		 || ipcp_p->local.want.other > ipcp_p->peer_max)
			ipcp_p->local.want.other = ipcp_p->peer_min;

		nextaddr = ipcp_addr_idle(ipcp_p->local.want.other);
	}
	return(nextaddr);
}


/************************************************************************/
/* Check if we have a specific IP address to assign to remote peer host */
/* !!! TO DO: subnet mask, and routing */
static int32
ipcp_lookuppeer(peerid)
char *peerid;
{
	char *buf;
	int32 peer_addr = 0L;

	if (peerid == NULL)
		return 0L;

	if ( (buf = userlookup( peerid, NULL, NULL,
			NULL, &peer_addr )) != NULL ) {
		free(buf);
	}
	return(peer_addr);
}


/************************************************************************/
/* Prepare to begin configuration exchange */
static void
ipcp_starting(fsm_p)
struct fsm_s *fsm_p;
{
	struct ipcp_s *ipcp_p =		fsm_p->pdv;

	PPP_DEBUG_ROUTINES("ipcp_starting()");

	/* If not already set, and we know the name of the peer,
	 * look in login file for an address
	 */
	if ( ipcp_p->remote.want.address == 0L ){
		ipcp_p->remote.want.address
		= ipcp_lookuppeer(fsm_p->ppp_p->peername);
	}

	/* If available, get next address from PPP pool */
	if ((ipcp_p->remote.want.address == 0L)
	 && (ipcp_p->peer_min != 0L)) {
		ipcp_p->remote.want.address = ipcp_poolnext(ipcp_p);
	}

	ipcp_p->local.want.address = fsm_p->ppp_p->iface->addr;
}


/************************************************************************/
static void
ipcp_free(fsm_p)
struct fsm_s *fsm_p;
{
	struct ipcp_s *ipcp_p = fsm_p->pdv;

	slhc_free( ipcp_p->slhcp );
}


/* Initialize configuration structure */
void
ipcp_init(ppp_p)
struct ppp_s *ppp_p;
{
	struct fsm_s *fsm_p = &(ppp_p->fsm[IPcp]);
	struct ipcp_s *ipcp_p;

	PPPtrace = ppp_p->trace;
	PPPiface = ppp_p->iface;

	PPP_DEBUG_ROUTINES("ipcp_init()");

	fsm_p->ppp_p = ppp_p;
	fsm_p->pdc = &ipcp_constants;
	fsm_p->pdv =
	ipcp_p = callocw(1,sizeof(struct ipcp_s));

	/* Set option parameters to first request defaults */
	ASSIGN( ipcp_p->local.want, ipcp_default );
	ipcp_p->local.will_negotiate = ipcp_negotiate;

	ASSIGN( ipcp_p->remote.want, ipcp_default );
	ASSIGN( ipcp_p->remote.work, ipcp_default);
	ipcp_p->remote.will_negotiate = ipcp_negotiate;

	fsm_init(fsm_p);
}


⌨️ 快捷键说明

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