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

📄 sys-ultrix.c

📁 unix and linux net driver
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * sys-ultrix.c - System-dependent procedures for setting up * PPP interfaces on Ultrix systems. * * Copyright (c) 1989 Carnegie Mellon University. * Copyright (c) 1995 The Australian National University. * All rights reserved. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed * by Carnegie Mellon University and The Australian National University. * The names of the Universities may not 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 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */#define RCSID	"$Id: sys-ultrix.c,v 1.33 1999/08/13 06:46:20 paulus Exp $"/* * TODO: */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdlib.h>#include <errno.h>#include <fcntl.h>#include <termios.h>#include <utmp.h>#include <sys/types.h>#include <sys/file.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <sys/time.h>#include <sys/errno.h>#include <sys/stat.h>#include <net/if.h>#include <net/ppp_defs.h>#include <net/if_ppp.h>#include <net/route.h>#include <netinet/in.h>#include "pppd.h"static const char rcsid[] = RCSID;static int initdisc = -1;	/* Initial TTY discipline for ppp_fd */static int initfdflags = -1;	/* Initial file descriptor flags for ppp_fd */static int ppp_fd = -1;		/* fd which is set to PPP discipline */static int restore_term;	/* 1 => we've munged the terminal */static struct termios inittermios; /* Initial TTY termios */static struct winsize wsinfo;	/* Initial window size info */static char *lock_file;		/* name of lock file created */static int loop_slave = -1;static int loop_master;static char loop_name[20];static unsigned char inbuf[512]; /* buffer for chars read from loopback */static int sockfd;		/* socket for doing interface ioctls */static fd_set in_fds;		/* set of fds that wait_input waits for */static int max_in_fd;		/* highest fd set in in_fds */static int if_is_up;		/* the interface is currently up */static u_int32_t ifaddrs[2];	/* local and remote addresses */static u_int32_t default_route_gateway;	/* gateway addr for default route */static u_int32_t proxy_arp_addr;	/* remote addr for proxy arp *//* Prototypes for procedures local to this file. */static int translate_speed __P((int));static int baud_rate_of __P((int));static int get_ether_addr __P((u_int32_t, struct sockaddr *));/* * sys_init - System-dependent initialization. */voidsys_init(){    /* Get an internet socket for doing socket ioctl's on. */    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)	fatal("Couldn't create IP socket: %m");    FD_ZERO(&in_fds);    max_in_fd = 0;}/* * sys_cleanup - restore any system state we modified before exiting: * mark the interface down, delete default route and/or proxy arp entry. * This shouldn't call die() because it's called from die(). */voidsys_cleanup(){    struct ifreq ifr;    if (if_is_up) {	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));	if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0	    && ((ifr.ifr_flags & IFF_UP) != 0)) {	    ifr.ifr_flags &= ~IFF_UP;	    ioctl(sockfd, SIOCSIFFLAGS, &ifr);	}    }    if (ifaddrs[0])	cifaddr(0, ifaddrs[0], ifaddrs[1]);    if (default_route_gateway)	cifdefaultroute(0, 0, default_route_gateway);    if (proxy_arp_addr)	cifproxyarp(0, proxy_arp_addr);}/* * sys_close - Clean up in a child process before execing. */voidsys_close(){    close(sockfd);    if (loop_slave >= 0) {	close(loop_slave);	close(loop_master);    }}/* * sys_check_options - check the options that the user specified */intsys_check_options(){    if (demand) {	option_error("Sorry - demand-dialling is not supported under Ultrix\n");	return 0;    }    return 1;}#if 0/* * daemon - Detach us from the terminal session. */intdaemon(nochdir, noclose)    int nochdir, noclose;{    int pid;    if ((pid = fork()) < 0)	return -1;    if (pid != 0)	exit(0);		/* parent dies */    setsid();    if (!nochdir)	chdir("/");    if (!noclose) {	fclose(stdin);		/* don't need stdin, stdout, stderr */	fclose(stdout);	fclose(stderr);    }    return 0;}#endif/* * ppp_available - check whether the system has any ppp interfaces * (in fact we check whether we can do an ioctl on ppp0). */intppp_available(){    int s, ok;    struct ifreq ifr;    extern char *no_ppp_msg;    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)	return 1;		/* can't tell */    strlcpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));    ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;    close(s);    no_ppp_msg = "\This system lacks kernel support for PPP.  To include PPP support\n\in the kernel, please follow the steps detailed in the README.ultrix\n\file in the ppp-2.2 distribution.\n";    return ok;}/* * establish_ppp - Turn the serial port into a ppp interface. */intestablish_ppp(fd)    int fd;{    int pppdisc = PPPDISC;    int x;    /*     * Save the old line discipline of fd, and set it to PPP.     */    if (ioctl(fd, TIOCGETD, &initdisc) < 0)	fatal("ioctl(TIOCGETD): %m");    if (ioctl(fd, TIOCSETD, &pppdisc) < 0)	fatal("ioctl(TIOCSETD): %m");    /*     * Find out which interface we were given.     */    if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {	fatal("ioctl(PPPIOCGUNIT): %m");    ppp_fd = fd;    /*     * Enable debug in the driver if requested.     */    if (kdebugflag) {	if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {	    warn("ioctl (PPPIOCGFLAGS): %m");	} else {	    x |= (kdebugflag & 0xFF) * SC_DEBUG;	    if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)		warn("ioctl(PPPIOCSFLAGS): %m");	}    }    /*     * Set device for non-blocking reads.     */    if ((initfdflags = fcntl(fd, F_GETFL)) == -1	|| fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {	warn("Couldn't set device to non-blocking mode: %m");    }    return fd;}/* * restore_loop - reattach the ppp unit to the loopback. */voidrestore_loop(){}/* * disestablish_ppp - Restore the serial port to normal operation. * This shouldn't call die() because it's called from die(). */voiddisestablish_ppp(fd)     int fd;{    /* Reset non-blocking mode on fd. */    if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)	warn("Couldn't restore device fd flags: %m");    initfdflags = -1;    /* Restore old line discipline. */    if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)	error("ioctl(TIOCSETD): %m");    initdisc = -1;    if (fd == ppp_fd)	ppp_fd = -1;}/* * Check whether the link seems not to be 8-bit clean. */voidclean_check(){    int x;    char *s;    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {	s = NULL;	switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {	case SC_RCV_B7_0:	    s = "bit 7 set to 1";	    break;	case SC_RCV_B7_1:	    s = "bit 7 set to 0";	    break;	case SC_RCV_EVNP:	    s = "odd parity";	    break;	case SC_RCV_ODDP:	    s = "even parity";	    break;	}	if (s != NULL) {	    warn("Serial link is not 8-bit clean:");	    warn("All received characters had %s", s);	}    }}/* * List of valid speeds. */struct speed {    int speed_int, speed_val;} speeds[] = {#ifdef B50    { 50, B50 },#endif#ifdef B75    { 75, B75 },#endif#ifdef B110    { 110, B110 },#endif#ifdef B134    { 134, B134 },#endif#ifdef B150    { 150, B150 },#endif#ifdef B200    { 200, B200 },#endif#ifdef B300    { 300, B300 },#endif#ifdef B600    { 600, B600 },#endif#ifdef B1200    { 1200, B1200 },#endif#ifdef B1800    { 1800, B1800 },#endif#ifdef B2000    { 2000, B2000 },#endif#ifdef B2400    { 2400, B2400 },#endif#ifdef B3600    { 3600, B3600 },#endif#ifdef B4800    { 4800, B4800 },#endif#ifdef B7200    { 7200, B7200 },#endif#ifdef B9600    { 9600, B9600 },#endif#ifdef B19200    { 19200, B19200 },#endif#ifdef B38400    { 38400, B38400 },#endif#ifdef EXTA    { 19200, EXTA },#endif#ifdef EXTB    { 38400, EXTB },#endif#ifdef B57600    { 57600, B57600 },#endif#ifdef B115200    { 115200, B115200 },#endif    { 0, 0 }};/* * Translate from bits/second to a speed_t. */inttranslate_speed(bps)    int bps;{    struct speed *speedp;    if (bps == 0)	return 0;    for (speedp = speeds; speedp->speed_int; speedp++)	if (bps == speedp->speed_int)	    return speedp->speed_val;    warn("speed %d not supported", bps);    return 0;}/* * Translate from a speed_t to bits/second. */intbaud_rate_of(speed)    int speed;{    struct speed *speedp;    if (speed == 0)	return 0;    for (speedp = speeds; speedp->speed_int; speedp++)	if (speed == speedp->speed_val)	    return speedp->speed_int;    return 0;}/* * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity, * at the requested speed, etc.  If `local' is true, set CLOCAL * regardless of whether the modem option was specified. */voidset_up_tty(fd, local)    int fd, local;{    int speed, x;    struct termios tios;    if (tcgetattr(fd, &tios) < 0)	fatal("tcgetattr: %m");    if (!restore_term) {	inittermios = tios;	ioctl(fd, TIOCGWINSZ, &wsinfo);    }    tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);#ifdef CRTSCTS    if (crtscts > 0 && !local)	tios.c_cflag |= CRTSCTS;    else if (crtscts < 0)	tios.c_cflag &= ~CRTSCTS;#endif	/* CRTSCTS */    tios.c_cflag |= CS8 | CREAD | HUPCL;    if (local || !modem)	tios.c_cflag |= CLOCAL;    tios.c_iflag = IGNBRK | IGNPAR;    tios.c_oflag = 0;    tios.c_lflag = 0;    tios.c_cc[VMIN] = 1;    tios.c_cc[VTIME] = 0;    if (crtscts == -2) {

⌨️ 快捷键说明

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