📄 sys-sunos4.c
字号:
intcifaddr(u, o, h) int u; u_int32_t o, h;{ struct rtentry rt; bzero(&rt, sizeof(rt)); rt.rt_dst.sa_family = AF_INET; INET_ADDR(rt.rt_dst) = h; rt.rt_gateway.sa_family = AF_INET; INET_ADDR(rt.rt_gateway) = o; rt.rt_flags = RTF_HOST; if (ioctl(sockfd, SIOCDELRT, &rt) < 0) error("Couldn't delete route through interface: %m"); ifaddrs[0] = 0; return 1;}/* * sifdefaultroute - assign a default route through the address given. */intsifdefaultroute(u, l, g) int u; u_int32_t l, g;{ struct rtentry rt; bzero(&rt, sizeof(rt)); rt.rt_dst.sa_family = AF_INET; INET_ADDR(rt.rt_dst) = 0; rt.rt_gateway.sa_family = AF_INET; INET_ADDR(rt.rt_gateway) = g; rt.rt_flags = RTF_GATEWAY; if (ioctl(sockfd, SIOCADDRT, &rt) < 0) { error("Can't add default route: %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 rtentry rt; bzero(&rt, sizeof(rt)); rt.rt_dst.sa_family = AF_INET; INET_ADDR(rt.rt_dst) = 0; rt.rt_gateway.sa_family = AF_INET; INET_ADDR(rt.rt_gateway) = g; rt.rt_flags = RTF_GATEWAY; if (ioctl(sockfd, SIOCDELRT, &rt) < 0) { error("Can't delete default route: %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)); if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) return 0; arpreq.arp_pa.sa_family = AF_INET; INET_ADDR(arpreq.arp_pa) = hisaddr; arpreq.arp_flags = ATF_PERM | ATF_PUBL; if (ioctl(sockfd, SIOCSARP, (caddr_t) &arpreq) < 0) { error("Couldn't set proxy ARP entry: %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)); arpreq.arp_pa.sa_family = AF_INET; INET_ADDR(arpreq.arp_pa) = hisaddr; if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) { error("Couldn't delete proxy ARP entry: %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]; int nit_fd; 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 = (struct ifreq *) ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) { 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; } } if (ifr >= ifend) return 0; info("found interface %s for proxy arp", ifr->ifr_name); /* * Grab the physical address for this interface. */ if ((nit_fd = open("/dev/nit", O_RDONLY)) < 0) { error("Couldn't open /dev/nit: %m"); return 0; } strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name)); if (ioctl(nit_fd, NIOCBIND, &ifreq) < 0 || ioctl(nit_fd, SIOCGIFADDR, &ifreq) < 0) { error("Couldn't get hardware address for %s: %m", ifreq.ifr_name); close(nit_fd); return 0; } hwaddr->sa_family = AF_UNSPEC; memcpy(hwaddr->sa_data, ifreq.ifr_addr.sa_data, 6); close(nit_fd); return 1;}/* * have_route_to - determine if the system has any route to * a given IP address. * For demand mode to work properly, we have to ignore routes * through our own interface. */int have_route_to(addr) u_int32_t addr;{ 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 = 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) continue; 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); } return mask;}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();}#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; }}#endif /* lock stuff removed *//* * 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;}/* * SunOS doesn't have strtoul :-( */unsigned longstrtoul(str, ptr, base) char *str, **ptr; int base;{ return (unsigned long) strtol(str, ptr, base);}/* * Or strerror :-( */extern char *sys_errlist[];extern int sys_nerr;char *strerror(n) int n;{ static char unknown[32]; if (n > 0 && n < sys_nerr) return sys_errlist[n]; slprintf(unknown, sizeof(unknown), "Error %d", n); return unknown;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -