sys-rtems.c

来自「RTEMS (Real-Time Executive for Multiproc」· C语言 代码 · 共 1,339 行 · 第 1/3 页

C
1,339
字号
    ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);}/* * get_pty - get a pty master/slave pair and chown the slave side * to the uid given.  Assumes slave_name points to >= 12 bytes of space. */intget_pty(master_fdp, slave_fdp, slave_name, uid)    int *master_fdp;    int *slave_fdp;    char *slave_name;    int uid;{    return 1;}/* * open_ppp_loopback - open the device we use for getting * packets in demand mode, and connect it to a ppp interface. * Here we use a pty. */intopen_ppp_loopback(){    return loop_master;}/* * output - Output PPP packet. */voidoutput(unit, p, len)    int unit;    u_char *p;    int len;{    if (debug);	dbglog("sent %P", p, len);/*    printf("sent packet [%d]\n", len); */    if (write(ttyfd, p, len) < 0) {	if (errno != EIO)	    error("write: %m");    }}voidppp_delay(void){  rtems_interval     ticks;  /* recommended delay to help negotiation */  ticks = 300000/rtems_bsdnet_microseconds_per_tick;  rtems_task_wake_after(ticks);}/* * wait_input - wait until there is data available, * for the length of time specified by *timo (indefinite * if timo is NULL). */voidwait_input(timo)    struct timeval *timo;{  rtems_event_set    events;  rtems_interval     ticks = 0;  rtems_option       wait = RTEMS_WAIT;  if(timo) {    if(timo->tv_sec == 0 && timo->tv_usec == 0)      wait = RTEMS_NO_WAIT;    else {      ticks = (timo->tv_sec * 1000000 + timo->tv_usec) /        rtems_bsdnet_microseconds_per_tick;      if(ticks <= 0)        ticks = 1;    }  }  rtems_event_receive(RTEMS_EVENT_31, RTEMS_EVENT_ANY | wait, ticks, &events);}/* * read_packet - get a PPP packet from the serial device. */intread_packet(buf)    u_char *buf;{    int len;    if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {	if (errno == EWOULDBLOCK || errno == EINTR) len = -1;	/*fatal("read: %m"); */    }/*    printf("read packet [%d]\n", len); */    return len;}/* * get_loop_output - read characters from the loopback, form them * into frames, and detect when we want to bring the real link up. * Return value is 1 if we need to bring up the link, 0 otherwise. */intget_loop_output(){    int rv = 0;    int n;    while ((n = read(loop_master, inbuf, sizeof(inbuf))) >= 0) {	if (loop_chars(inbuf, n))	    rv = 1;    }    if (n == 0)	fatal("eof on loopback");    if (errno != EWOULDBLOCK)	fatal("read from loopback: %m");    return rv;}/* * ppp_send_config - configure the transmit characteristics of * the ppp interface. */voidppp_send_config(unit, mtu, asyncmap, pcomp, accomp)    int unit, mtu;    u_int32_t asyncmap;    int pcomp, accomp;{    u_int x;    struct ifreq ifr;    strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));    ifr.ifr_mtu = mtu;    if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0)	fatal("ioctl(SIOCSIFMTU): %m");    if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)	fatal("ioctl(PPPIOCSASYNCMAP): %m");    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)	fatal("ioctl (PPPIOCGFLAGS): %m");    x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;    x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;/*    x = sync_serial ? x | SC_SYNC : x & ~SC_SYNC; */    if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)	fatal("ioctl(PPPIOCSFLAGS): %m");}/* * ppp_set_xaccm - set the extended transmit ACCM for the interface. */voidppp_set_xaccm(unit, accm)    int unit;    ext_accm accm;{    if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)	warn("ioctl(set extended ACCM): %m");}/* * ppp_recv_config - configure the receive-side characteristics of * the ppp interface. */voidppp_recv_config(unit, mru, asyncmap, pcomp, accomp)    int unit, mru;    u_int32_t asyncmap;    int pcomp, accomp;{    int x;    if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)	fatal("ioctl(PPPIOCSMRU): %m");    if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)	fatal("ioctl(PPPIOCSRASYNCMAP): %m");    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)	fatal("ioctl (PPPIOCGFLAGS): %m");    x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;    if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)	fatal("ioctl(PPPIOCSFLAGS): %m");}/* * ccp_test - ask kernel whether a given compression method * is acceptable for use.  Returns 1 if the method and parameters * are OK, 0 if the method is known but the parameters are not OK * (e.g. code size should be reduced), or -1 if the method is unknown. */intccp_test(unit, opt_ptr, opt_len, for_transmit)    int unit, opt_len, for_transmit;    u_char *opt_ptr;{    struct ppp_option_data data;    data.ptr = opt_ptr;    data.length = opt_len;    data.transmit = for_transmit;    if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)	return 1;    return (errno == ENOBUFS)? 0: -1;}/* * ccp_flags_set - inform kernel about the current state of CCP. */voidccp_flags_set(unit, isopen, isup)    int unit, isopen, isup;{    int x;    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {	error("ioctl (PPPIOCGFLAGS): %m");	return;    }    x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;    x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;    if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)	error("ioctl(PPPIOCSFLAGS): %m");}/* * ccp_fatal_error - returns 1 if decompression was disabled as a * result of an error detected after decompression of a packet, * 0 otherwise.  This is necessary because of patent nonsense. */intccp_fatal_error(unit)    int unit;{    int x;    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {	error("ioctl(PPPIOCGFLAGS): %m");	return 0;    }    return x & SC_DC_FERROR;}/* * get_idle_time - return how long the link has been idle. */intget_idle_time(u, ip)    int u;    struct ppp_idle *ip;{    return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;}/* * get_ppp_stats - return statistics for the link. */intget_ppp_stats(u, stats)    int u;    struct pppd_stats *stats;{    struct ifpppstatsreq req;    memset (&req, 0, sizeof (req));    strlcpy(req.ifr_name, ifname, sizeof(req.ifr_name));    if (ioctl(sockfd, SIOCGPPPSTATS, &req) < 0) {	error("Couldn't get PPP statistics: %m");	return 0;    }    stats->bytes_in = req.stats.p.ppp_ibytes;    stats->bytes_out = req.stats.p.ppp_obytes;    return 1;}#ifdef PPP_FILTER/* * set_filters - transfer the pass and active filters to the kernel. */intset_filters(pass, active)    struct bpf_program *pass, *active;{    int ret = 1;    if (pass->bf_len > 0) {	if (ioctl(ppp_fd, PPPIOCSPASS, pass) < 0) {	    error("Couldn't set pass-filter in kernel: %m");	    ret = 0;	}    }    if (active->bf_len > 0) {	if (ioctl(ppp_fd, PPPIOCSACTIVE, active) < 0) {	    error("Couldn't set active-filter in kernel: %m");	    ret = 0;	}    }    return ret;}#endif/* * sifvjcomp - config tcp header compression */intsifvjcomp(u, vjcomp, cidcomp, maxcid)    int u, vjcomp, cidcomp, maxcid;{    u_int x;    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {	error("ioctl (PPPIOCGFLAGS): %m");	return 0;    }    x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;    x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;    if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {	error("ioctl(PPPIOCSFLAGS): %m");	return 0;    }    if (vjcomp && ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {	error("ioctl(PPPIOCSFLAGS): %m");	return 0;    }    return 1;}/* * sifup - Config the interface up and enable IP packets to pass. */intsifup(u)    int u;{    struct ifreq ifr;    strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));    if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {	error("ioctl (SIOCGIFFLAGS): %m");	return 0;    }    ifr.ifr_flags |= IFF_UP;    if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {	error("ioctl(SIOCSIFFLAGS): %m");	return 0;    }    if_is_up = 1;    return 1;}/* * sifnpmode - Set the mode for handling packets for a given NP. */intsifnpmode(u, proto, mode)    int u;    int proto;    enum NPmode mode;{    struct npioctl npi;    npi.protocol = proto;    npi.mode = mode;    if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {	error("ioctl(set NP %d mode to %d): %m", proto, mode);	return 0;    }    return 1;}/* * sifdown - Config the interface down and disable IP. */intsifdown(u)    int u;{    struct ifreq ifr;    int rv;    struct npioctl npi;    rv = 1;    npi.protocol = PPP_IP;    npi.mode = NPMODE_ERROR;    ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);    /* ignore errors, because ppp_fd might have been closed by now. */    strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));    if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {	error("ioctl (SIOCGIFFLAGS): %m");	rv = 0;    } else {	ifr.ifr_flags &= ~IFF_UP;	if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {	    error("ioctl(SIOCSIFFLAGS): %m");	    rv = 0;	} else	    if_is_up = 0;    }    return rv;}/* * SET_SA_FAMILY - set the sa_family field of a struct sockaddr, * if it exists. */#define SET_SA_FAMILY(addr, family)		\    BZERO((char *) &(addr), sizeof(addr));	\    addr.sa_family = (family); 			\    addr.sa_len = sizeof(addr);/* * sifaddr - Config the interface IP addresses and netmask. */intsifaddr(u, o, h, m)    int u;    u_int32_t o, h, m;{    struct ifaliasreq ifra;    struct ifreq ifr;    strlcpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));    SET_SA_FAMILY(ifra.ifra_addr, AF_INET);    ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;    SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);    ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;    if (m != 0) {	SET_SA_FAMILY(ifra.ifra_mask, AF_INET);	((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;    } else	BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));    BZERO(&ifr, sizeof(ifr));    strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));    if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifr) < 0) {

⌨️ 快捷键说明

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