📄 sys-osf.c
字号:
* sifaddr - Config the interface IP addresses and netmask. */intsifaddr(u, o, h, m) int u; u_int32_t o, h, m;{ struct ifreq ifr; struct ifaliasreq addreq; int ret; ret = 1; /* flush old address, if any */ bzero(&ifr, sizeof (ifr)); strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); SET_SA_FAMILY(ifr.ifr_addr, AF_INET); ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o; if ((ioctl(sockfd, (int)SIOCDIFADDR, (caddr_t) &ifr) < 0) && errno != EADDRNOTAVAIL) { error("ioctl(SIOCDIFADDR): %m"); ret = 0; } bzero(&addreq, sizeof (addreq)); strlcpy(addreq.ifra_name, ifname, sizeof (addreq.ifra_name)); SET_SA_FAMILY(addreq.ifra_addr, AF_INET); SET_SA_FAMILY(addreq.ifra_broadaddr, AF_INET); ((struct sockaddr_in *)&addreq.ifra_addr)->sin_addr.s_addr = o; ((struct sockaddr_in *)&addreq.ifra_broadaddr)->sin_addr.s_addr = h; if (m != 0) { ((struct sockaddr_in *)&addreq.ifra_mask)->sin_addr.s_addr = m; addreq.ifra_mask.sa_len = sizeof (struct sockaddr); info("Setting interface mask to %s\n", ip_ntoa(m)); } /* install new src/dst and (possibly) netmask */ if (ioctl(sockfd, SIOCPIFADDR, &addreq) < 0) { error("ioctl(SIOCPIFADDR): %m"); ret = 0; } ifr.ifr_data = (caddr_t)&link_mtu; if (ioctl(sockfd, SIOCSIPMTU, &ifr) < 0) { error("Couldn't set IP MTU: %m"); ret = 0; } ifaddrs[0] = o; ifaddrs[1] = h; return (ret);}/* * cifaddr - Clear the interface IP addresses, and delete routes * through the interface if possible. */intcifaddr(u, o, h) int u; u_int32_t o, h;{ struct ifreq ifr; ifaddrs[0] = 0; ifaddrs[1] = 0; bzero(&ifr, sizeof (ifr)); strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); SET_SA_FAMILY(ifr.ifr_addr, AF_INET); ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o; if (ioctl(sockfd, (int)SIOCDIFADDR, (caddr_t) &ifr) < 0) { error("ioctl(SIOCDIFADDR): %m"); return 0; } return 1;}/* * sifdefaultroute - assign a default route through the address given. */intsifdefaultroute(u, l, g) int u; u_int32_t l, g;{ struct ortentry rt; BZERO(&rt, sizeof(rt)); SET_SA_FAMILY(rt.rt_dst, AF_INET); SET_SA_FAMILY(rt.rt_gateway, AF_INET); ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g; rt.rt_flags = RTF_GATEWAY; if (ioctl(sockfd, (int)SIOCADDRT, &rt) < 0) { error("default route ioctl(SIOCADDRT): %m"); return 0; } default_route_gateway = g; return 1;}/* * cifdefaultroute - delete a default route through the address given. */intcifdefaultroute(u, l, g) int u; u_int32_t l, g;{ struct ortentry rt; BZERO(&rt, sizeof(rt)); SET_SA_FAMILY(rt.rt_dst, AF_INET); SET_SA_FAMILY(rt.rt_gateway, AF_INET); ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g; rt.rt_flags = RTF_GATEWAY; if (ioctl(sockfd, (int)SIOCDELRT, &rt) < 0) { error("default route ioctl(SIOCDELRT): %m"); return 0; } default_route_gateway = 0; return 1;}/* * sifproxyarp - Make a proxy ARP entry for the peer. */intsifproxyarp(unit, hisaddr) int unit; u_int32_t hisaddr;{ struct arpreq arpreq; BZERO(&arpreq, sizeof(arpreq)); /* * Get the hardware address of an interface on the same subnet * as our local address. */ if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) { warn("Cannot determine ethernet address for proxy ARP"); return 0; } SET_SA_FAMILY(arpreq.arp_pa, AF_INET); ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr; arpreq.arp_flags = ATF_PERM | ATF_PUBL; if (ioctl(sockfd, (int)SIOCSARP, (caddr_t)&arpreq) < 0) { error("ioctl(SIOCSARP): %m"); return 0; } proxy_arp_addr = hisaddr; return 1;}/* * cifproxyarp - Delete the proxy ARP entry for the peer. */intcifproxyarp(unit, hisaddr) int unit; u_int32_t hisaddr;{ struct arpreq arpreq; BZERO(&arpreq, sizeof(arpreq)); SET_SA_FAMILY(arpreq.arp_pa, AF_INET); ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr; if (ioctl(sockfd, (int)SIOCDARP, (caddr_t)&arpreq) < 0) { error("ioctl(SIOCDARP): %m"); return 0; } proxy_arp_addr = 0; return 1;}/* * get_ether_addr - get the hardware address of an interface on the * the same subnet as ipaddr. */#define MAX_IFS 32static intget_ether_addr(ipaddr, hwaddr) u_int32_t ipaddr; struct sockaddr *hwaddr;{ struct ifreq *ifr, *ifend; u_int32_t ina, mask; struct ifreq ifreq; struct ifconf ifc; struct ifreq ifs[MAX_IFS]; struct ifdevea ifdevreq; ifc.ifc_len = sizeof(ifs); ifc.ifc_req = ifs; if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) { error("ioctl(SIOCGIFCONF): %m"); return 0; } /* * Scan through looking for an interface with an Internet * address on the same subnet as `ipaddr'. */ ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len); for (ifr = ifc.ifc_req; ifr < ifend; ifr++) { if (ifr->ifr_addr.sa_family == AF_INET) { /* * Check that the interface is up, and not point-to-point * or loopback. */ strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name)); if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0) continue; if ((ifreq.ifr_flags & (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP)) != (IFF_UP|IFF_BROADCAST)) continue; /* * Get its netmask and check that it's on the right subnet. */ if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0) continue; ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr; mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr; if ((ipaddr & mask) != (ina & mask)) continue; break; } else { if (ifr->ifr_addr.sa_len > sizeof (ifr->ifr_addr)) ifr = (struct ifreq *)((caddr_t)ifr + (ifr->ifr_addr.sa_len - sizeof (ifr->ifr_addr))); } } if (ifr >= ifend) return 0; info("found interface %s for proxy arp", ifr->ifr_name); strlcpy(ifdevreq.ifr_name, ifr->ifr_name, sizeof(ifdevreq.ifr_name)); if (ioctl(sockfd, (int)SIOCRPHYSADDR, &ifdevreq) < 0) { perror("ioctl(SIOCRPHYSADDR)"); return(0); } hwaddr->sa_family = AF_UNSPEC; memcpy(hwaddr->sa_data, ifdevreq.current_pa, sizeof(ifdevreq.current_pa)); return 1;}#define WTMPFILE "/usr/adm/wtmp"voidlogwtmp(line, name, host) const char *line, *name, *host;{ int fd; struct stat buf; struct utmp ut; if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0) return; if (!fstat(fd, &buf)) { strncpy(ut.ut_line, line, sizeof(ut.ut_line)); strncpy(ut.ut_name, name, sizeof(ut.ut_name)); strncpy(ut.ut_host, host, sizeof(ut.ut_host)); (void)time(&ut.ut_time); if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp)) (void)ftruncate(fd, buf.st_size); } close(fd);}/* * Return user specified netmask, modified by any mask we might determine * for address `addr' (in network byte order). * Here we scan through the system's list of interfaces, looking for * any non-point-to-point interfaces which might appear to be on the same * network as `addr'. If we find any, we OR in their netmask to the * user-specified netmask. */u_int32_tGetMask(addr) u_int32_t addr;{ u_int32_t mask, nmask, ina; struct ifreq *ifr, *ifend, ifreq; struct ifconf ifc; addr = ntohl(addr); if (IN_CLASSA(addr)) /* determine network mask for address class */ nmask = IN_CLASSA_NET; else if (IN_CLASSB(addr)) nmask = IN_CLASSB_NET; else nmask = IN_CLASSC_NET; /* class D nets are disallowed by bad_ip_adrs */ mask = netmask | htonl(nmask); /* * Scan through the system's network interfaces. */ ifc.ifc_len = MAX_IFS * sizeof(struct ifreq); ifc.ifc_req = (struct ifreq *)alloca(ifc.ifc_len); if (ifc.ifc_req == 0) return mask; if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) { warn("Couldn't get system interface list: %m"); return mask; } ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len); for (ifr = ifc.ifc_req; ifr < ifend; ifr++) { /* * Check the interface's internet address. */ if (ifr->ifr_addr.sa_family == AF_INET) { ina = INET_ADDR(ifr->ifr_addr); if ((ntohl(ina) & nmask) != (addr & nmask)) continue; /* * Check that the interface is up, and not point-to-point or loopback. */ strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name)); if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0) continue; if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK)) != IFF_UP) continue; /* * Get its netmask and OR it into our mask. */ if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0) continue; mask |= INET_ADDR(ifreq.ifr_addr); break; } else { if (ifr->ifr_addr.sa_len > sizeof (ifr->ifr_addr)) ifr = (struct ifreq *)((caddr_t)ifr + (ifr->ifr_addr.sa_len - sizeof (ifr->ifr_addr))); } } return mask;}/* * have_route_to - determine if the system has any route to * a given IP address. `addr' is in network byte order. * For demand mode to work properly, we have to ignore routes * through our own interface. */int have_route_to(u_int32_t addr){ return -1; }static intstrioctl(fd, cmd, ptr, ilen, olen) int fd, cmd, ilen, olen; void *ptr;{ struct strioctl str; str.ic_cmd = cmd; str.ic_timout = 0; str.ic_len = ilen; str.ic_dp = ptr; if (ioctl(fd, I_STR, &str) == -1) return -1; if (str.ic_len != olen) dbglog("strioctl: expected %d bytes, got %d for cmd %x\n", olen, str.ic_len, cmd); return 0;}/* * Use the hostid as part of the random number seed. */intget_host_seed(){ return gethostid();}/* * 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;{ int i, mfd, sfd; char pty_name[12]; struct termios tios; sfd = -1; for (i = 0; i < 64; ++i) { slprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x", 'p' + i / 16, i % 16); mfd = open(pty_name, O_RDWR, 0); if (mfd >= 0) { pty_name[5] = 't'; sfd = open(pty_name, O_RDWR | O_NOCTTY, 0); if (sfd >= 0) break; close(mfd); } } if (sfd < 0) return 0; strlcpy(slave_name, pty_name, 12); *master_fdp = mfd; *slave_fdp = sfd; fchown(sfd, uid, -1); fchmod(sfd, S_IRUSR | S_IWUSR); if (tcgetattr(sfd, &tios) == 0) { tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB); tios.c_cflag |= CS8 | CREAD; tios.c_iflag = IGNPAR | CLOCAL; tios.c_oflag = 0; tios.c_lflag = 0; if (tcsetattr(sfd, TCSAFLUSH, &tios) < 0) warn("couldn't set attributes on pty: %m"); } else warn("couldn't get attributes on pty: %m"); return 1;}#if 0/* * Code for locking/unlocking the serial device. * This code is derived from chat.c. */#if !defined(HDB) && !defined(SUNOS3)#define HDB 1 /* ascii lock files are the default */#endif#ifndef LOCK_DIR# if HDB# define PIDSTRING# define LOCK_PREFIX "/usr/spool/locks/LCK.."# else /* HDB */# define LOCK_PREFIX "/usr/spool/uucp/LCK.."# endif /* HDB */#endif /* LOCK_DIR */static char *lock_file; /* name of lock file created *//* * lock - create a lock file for the named device. */intlock(dev) char *dev;{ char hdb_lock_buffer[12]; int fd, pid, n; char *p; size_t l; if ((p = strrchr(dev, '/')) != NULL) dev = p + 1; l = strlen(LOCK_PREFIX) + strlen(dev) + 1; lock_file = malloc(l); if (lock_file == NULL) novm("lock file name"); slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev); while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) { if (errno == EEXIST && (fd = open(lock_file, O_RDONLY, 0)) >= 0) { /* Read the lock file to find out who has the device locked */#ifdef PIDSTRING n = read(fd, hdb_lock_buffer, 11); if (n > 0) { hdb_lock_buffer[n] = 0; pid = atoi(hdb_lock_buffer); }#else n = read(fd, &pid, sizeof(pid));#endif if (n <= 0) { error("Can't read pid from lock file %s", lock_file); close(fd); } else { if (kill(pid, 0) == -1 && errno == ESRCH) { /* pid no longer exists - remove the lock file */ if (unlink(lock_file) == 0) { close(fd); notice("Removed stale lock on %s (pid %d)", dev, pid); continue; } else warn("Couldn't remove stale lock on %s", dev); } else notice("Device %s is locked by pid %d", dev, pid); } close(fd); } else error("Can't create lock file %s: %m", lock_file); free(lock_file); lock_file = NULL; return -1; }#ifdef PIDSTRING slprintf(hdb_lock_buffer, sizeof(hdb_lock_buffer), "%10d\n", getpid()); write(fd, hdb_lock_buffer, 11);#else pid = getpid(); write(fd, &pid, sizeof pid);#endif close(fd); return 0;}/* * unlock - remove our lockfile */voidunlock(){ if (lock_file) { unlink(lock_file); free(lock_file); lock_file = NULL; }}#endifintset_filters(pass, active) struct bpf_program *pass, *active;{ return 1;}intbpf_compile(program, buf, optimize) struct bpf_program *program; char *buf; int optimize;{ return 0;}char *bpf_geterr(){ return 0;}u_intbpf_filter(pc, p, wirelen, buflen) struct bpf_insn *pc; u_char *p; u_int wirelen; u_int buflen;{ return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -