/* * Determine the local and remote user, tty, and machines */ void get_names(int argc, char **argv) { char hostname[MAXHOSTNAMELEN]; char *his_name, *my_name; char *my_machine_name, *his_machine_name; const char *his_tty; char *cp; if (argc < 2 ) usage(); if (!isatty(0)) errx(1, "standard input must be a tty, not a pipe or a file"); if ((my_name = getlogin()) == NULL) { struct passwd *pw; if ((pw = getpwuid(getuid())) == NULL) errx(1, "you don't exist. Go away"); my_name = pw->pw_name; } gethostname(hostname, sizeof (hostname)); my_machine_name = hostname; /* check for, and strip out, the machine name of the target */ for (cp = argv[1]; *cp && !strchr("@:!", *cp); cp++) ; if (*cp == '\0') { /* this is a local to local talk */ his_name = argv[1]; his_machine_name = my_machine_name; } else { if (*cp++ == '@') { /* user@host */ his_name = argv[1]; his_machine_name = cp; } else { /* host!user or host:user */ his_name = cp; his_machine_name = argv[1]; } *--cp = '\0'; } if (argc > 2) his_tty = argv[2]; /* tty name is arg 2 */ else his_tty = ""; get_addrs(my_machine_name, his_machine_name); /* * Initialize the message template. */ msg.vers = TALK_VERSION; msg.addr.sa_family = htons(AF_INET); msg.ctl_addr.sa_family = htons(AF_INET); msg.id_num = htonl(0); strncpy(msg.l_name, my_name, NAME_SIZE); msg.l_name[NAME_SIZE - 1] = '\0'; strncpy(msg.r_name, his_name, NAME_SIZE); msg.r_name[NAME_SIZE - 1] = '\0'; strncpy(msg.r_tty, his_tty, TTY_SIZE); msg.r_tty[TTY_SIZE - 1] = '\0'; }
int manage_link(int fd) { char *p, *e, *cp; char ifname[IF_NAMESIZE]; ssize_t bytes; struct rt_msghdr *rtm; struct if_announcemsghdr *ifan; struct if_msghdr *ifm; struct ifa_msghdr *ifam; struct rt rt; struct sockaddr *sa, *rti_info[RTAX_MAX]; int len; #ifdef RTM_CHGADDR struct sockaddr_dl sdl; unsigned char *hwaddr; #endif for (;;) { if (ioctl(fd, FIONREAD, &len) == -1) return -1; if (link_buflen < len) { p = realloc(link_buf, len); if (p == NULL) return -1; link_buf = p; link_buflen = len; } bytes = read(fd, link_buf, link_buflen); if (bytes == -1) { if (errno == EAGAIN) return 0; if (errno == EINTR) continue; return -1; } e = link_buf + bytes; for (p = link_buf; p < e; p += rtm->rtm_msglen) { rtm = (struct rt_msghdr *)(void *)p; switch(rtm->rtm_type) { #ifdef RTM_IFANNOUNCE case RTM_IFANNOUNCE: ifan = (struct if_announcemsghdr *)(void *)p; switch(ifan->ifan_what) { case IFAN_ARRIVAL: handle_interface(1, ifan->ifan_name); break; case IFAN_DEPARTURE: handle_interface(-1, ifan->ifan_name); break; } break; #endif case RTM_IFINFO: ifm = (struct if_msghdr *)(void *)p; memset(ifname, 0, sizeof(ifname)); if (if_indextoname(ifm->ifm_index, ifname)) handle_interface(0, ifname); break; case RTM_DELETE: if (!(rtm->rtm_addrs & RTA_DST) || !(rtm->rtm_addrs & RTA_GATEWAY) || !(rtm->rtm_addrs & RTA_NETMASK)) break; if (rtm->rtm_pid == getpid()) break; cp = (char *)(void *)(rtm + 1); sa = (struct sockaddr *)(void *)cp; if (sa->sa_family != AF_INET) break; get_addrs(rtm->rtm_addrs, cp, rti_info); rt.iface = NULL; rt.next = NULL; COPYOUT(rt.dest, rti_info[RTAX_DST]); COPYOUT(rt.net, rti_info[RTAX_NETMASK]); COPYOUT(rt.gate, rti_info[RTAX_GATEWAY]); route_deleted(&rt); break; #ifdef RTM_CHGADDR case RTM_CHGADDR: /* FALLTHROUGH */ #endif case RTM_DELADDR: /* FALLTHROUGH */ case RTM_NEWADDR: ifam = (struct ifa_msghdr *)(void *)p; if (!if_indextoname(ifam->ifam_index, ifname)) break; cp = (char *)(void *)(ifam + 1); get_addrs(ifam->ifam_addrs, cp, rti_info); if (rti_info[RTAX_IFA] == NULL) break; switch (rti_info[RTAX_IFA]->sa_family) { #ifdef RTM_CHGADDR case AF_LINK: if (rtm->rtm_type != RTM_CHGADDR) break; memcpy(&sdl, rti_info[RTAX_IFA], rti_info[RTAX_IFA]->sa_len); hwaddr = xmalloc(sdl.sdl_alen); memcpy(hwaddr, LLADDR(&sdl), sdl.sdl_alen); handle_hwaddr(ifname, hwaddr, sdl.sdl_alen); break; #endif case AF_INET: case 255: /* FIXME: Why 255? */ COPYOUT(rt.dest, rti_info[RTAX_IFA]); COPYOUT(rt.net, rti_info[RTAX_NETMASK]); COPYOUT(rt.gate, rti_info[RTAX_BRD]); handle_ifa(rtm->rtm_type, ifname, &rt.dest, &rt.net, &rt.gate); break; } break; } } } }
int manage_link(int fd) { char *p, *e, *cp; char ifname[IF_NAMESIZE]; ssize_t bytes; struct rt_msghdr *rtm; struct if_announcemsghdr *ifan; struct if_msghdr *ifm; struct ifa_msghdr *ifam; struct sockaddr *sa, *rti_info[RTAX_MAX]; int len; struct sockaddr_dl sdl; #ifdef INET struct rt rt; #endif #if defined(INET6) && !defined(LISTEN_DAD) struct in6_addr ia6; struct sockaddr_in6 *sin6; int ifa_flags; #endif for (;;) { if (ioctl(fd, FIONREAD, &len) == -1) return -1; if (link_buflen < len) { p = realloc(link_buf, len); if (p == NULL) return -1; link_buf = p; link_buflen = len; } bytes = read(fd, link_buf, link_buflen); if (bytes == -1) { if (errno == EAGAIN) return 0; if (errno == EINTR) continue; return -1; } e = link_buf + bytes; for (p = link_buf; p < e; p += rtm->rtm_msglen) { rtm = (struct rt_msghdr *)(void *)p; // Ignore messages generated by us if (rtm->rtm_pid == getpid()) break; switch(rtm->rtm_type) { #ifdef RTM_IFANNOUNCE case RTM_IFANNOUNCE: ifan = (struct if_announcemsghdr *)(void *)p; switch(ifan->ifan_what) { case IFAN_ARRIVAL: handle_interface(1, ifan->ifan_name); break; case IFAN_DEPARTURE: handle_interface(-1, ifan->ifan_name); break; } break; #endif case RTM_IFINFO: ifm = (struct if_msghdr *)(void *)p; memset(ifname, 0, sizeof(ifname)); if (!(if_indextoname(ifm->ifm_index, ifname))) break; switch (ifm->ifm_data.ifi_link_state) { case LINK_STATE_DOWN: len = LINK_DOWN; break; case LINK_STATE_UP: len = LINK_UP; break; default: /* handle_carrier will re-load * the interface flags and check for * IFF_RUNNING as some drivers that * don't handle link state also don't * set IFF_RUNNING when this routing * message is generated. * As such, it is a race ...*/ len = LINK_UNKNOWN; break; } handle_carrier(len, ifm->ifm_flags, ifname); break; case RTM_DELETE: if (~rtm->rtm_addrs & (RTA_DST | RTA_GATEWAY | RTA_NETMASK)) break; cp = (char *)(void *)(rtm + 1); sa = (struct sockaddr *)(void *)cp; if (sa->sa_family != AF_INET) break; #ifdef INET get_addrs(rtm->rtm_addrs, cp, rti_info); memset(&rt, 0, sizeof(rt)); rt.iface = NULL; COPYOUT(rt.dest, rti_info[RTAX_DST]); COPYOUT(rt.net, rti_info[RTAX_NETMASK]); COPYOUT(rt.gate, rti_info[RTAX_GATEWAY]); ipv4_routedeleted(&rt); #endif break; #ifdef RTM_CHGADDR case RTM_CHGADDR: /* FALLTHROUGH */ #endif case RTM_DELADDR: /* FALLTHROUGH */ case RTM_NEWADDR: ifam = (struct ifa_msghdr *)(void *)p; if (!if_indextoname(ifam->ifam_index, ifname)) break; cp = (char *)(void *)(ifam + 1); get_addrs(ifam->ifam_addrs, cp, rti_info); if (rti_info[RTAX_IFA] == NULL) break; switch (rti_info[RTAX_IFA]->sa_family) { case AF_LINK: #ifdef RTM_CHGADDR if (rtm->rtm_type != RTM_CHGADDR) break; #else if (rtm->rtm_type != RTM_NEWADDR) break; #endif memcpy(&sdl, rti_info[RTAX_IFA], rti_info[RTAX_IFA]->sa_len); handle_hwaddr(ifname, (const unsigned char*)CLLADDR(&sdl), sdl.sdl_alen); break; #ifdef INET case AF_INET: case 255: /* FIXME: Why 255? */ COPYOUT(rt.dest, rti_info[RTAX_IFA]); COPYOUT(rt.net, rti_info[RTAX_NETMASK]); COPYOUT(rt.gate, rti_info[RTAX_BRD]); ipv4_handleifa(rtm->rtm_type, NULL, ifname, &rt.dest, &rt.net, &rt.gate); break; #endif #if defined(INET6) && !defined(LISTEN_DAD) case AF_INET6: sin6 = (struct sockaddr_in6*)(void *) rti_info[RTAX_IFA]; memcpy(ia6.s6_addr, sin6->sin6_addr.s6_addr, sizeof(ia6.s6_addr)); if (rtm->rtm_type == RTM_NEWADDR) { ifa_flags = in6_addr_flags( ifname, &ia6); if (ifa_flags == -1) break; } else ifa_flags = 0; ipv6_handleifa(rtm->rtm_type, NULL, ifname, &ia6, ifa_flags); break; #endif } break; } } } }
/* * Determine the local and remote user, tty, and machines */ void get_names(int argc, char *argv[]) { char hostname[HOST_NAME_MAX+1]; char *his_name, *my_name; char *my_machine_name, *his_machine_name; char *his_tty; char *cp; char *names; if (argc > 1 && !strcmp(argv[1], "-H")) { argv[1] = argv[0]; ++argv; --argc; high_print = 1; } if (argc > 1 && !strcmp(argv[1], "-s")) { argv[1] = argv[0]; ++argv; --argc; smooth_scroll = TRUE; } if ((argc < 2 ) || ('@' == argv[1][0])) { extern char *__progname; fprintf(stderr, "usage: %s [-Hs] person [ttyname]\n", __progname); exit(1); } if (!isatty(STDIN_FILENO)) errx(1, "standard input must be a tty, not a pipe or a file"); if ((my_name = getlogin()) == NULL) { struct passwd *pw; if ((pw = getpwuid(getuid())) == NULL) errx(1, "you don't exist in the passwd file."); my_name = pw->pw_name; } gethostname(hostname, sizeof (hostname)); my_machine_name = hostname; /* check for, and strip out, the machine name of the target */ names = strdup(argv[1]); if (names == NULL) errx(1, "out of memory"); for (cp = names; *cp && !strchr("@:!.", *cp); cp++) ; if (*cp == '\0') { /* this is a local to local talk */ his_name = names; his_machine_name = my_machine_name; } else { if (*cp++ == '@') { /* user@host */ his_name = names; his_machine_name = cp; } else { /* host.user or host!user or host:user */ his_name = cp; his_machine_name = names; } *--cp = '\0'; } if (argc > 2) his_tty = argv[2]; /* tty name is arg 2 */ else his_tty = ""; get_addrs(my_machine_name, his_machine_name); /* * Initialize the message template. */ msg.vers = TALK_VERSION; msg.addr.sa_family = htons(AF_INET); msg.ctl_addr.sa_family = htons(AF_INET); msg.id_num = htonl(0); strncpy(msg.l_name, my_name, NAME_SIZE); msg.l_name[NAME_SIZE - 1] = '\0'; strncpy(msg.r_name, his_name, NAME_SIZE); msg.r_name[NAME_SIZE - 1] = '\0'; strncpy(msg.r_tty, his_tty, TTY_SIZE); msg.r_tty[TTY_SIZE - 1] = '\0'; free(names); }
int if_managelink(struct dhcpcd_ctx *ctx) { /* route and ifwatchd like a msg buf size of 2048 */ char msg[2048], *p, *e, *cp; ssize_t bytes; struct rt_msghdr *rtm; struct if_announcemsghdr *ifan; struct if_msghdr *ifm; struct ifa_msghdr *ifam; struct sockaddr *sa, *rti_info[RTAX_MAX]; int len; struct sockaddr_dl sdl; struct interface *ifp; #ifdef INET struct rt rt; #endif #ifdef INET6 struct rt6 rt6; struct in6_addr ia6; struct sockaddr_in6 *sin6; int ifa_flags; #endif bytes = read(ctx->link_fd, msg, sizeof(msg)); if (bytes == -1) return -1; if (bytes == 0) return 0; e = msg + bytes; for (p = msg; p < e; p += rtm->rtm_msglen) { rtm = (struct rt_msghdr *)(void *)p; // Ignore messages generated by us if (rtm->rtm_pid == getpid()) break; switch(rtm->rtm_type) { #ifdef RTM_IFANNOUNCE case RTM_IFANNOUNCE: ifan = (struct if_announcemsghdr *)(void *)p; switch(ifan->ifan_what) { case IFAN_ARRIVAL: dhcpcd_handleinterface(ctx, 1, ifan->ifan_name); break; case IFAN_DEPARTURE: dhcpcd_handleinterface(ctx, -1, ifan->ifan_name); break; } break; #endif case RTM_IFINFO: ifm = (struct if_msghdr *)(void *)p; if ((ifp = if_findindex(ctx, ifm->ifm_index)) == NULL) break; switch (ifm->ifm_data.ifi_link_state) { case LINK_STATE_DOWN: len = LINK_DOWN; break; case LINK_STATE_UP: len = LINK_UP; break; default: /* handle_carrier will re-load * the interface flags and check for * IFF_RUNNING as some drivers that * don't handle link state also don't * set IFF_RUNNING when this routing * message is generated. * As such, it is a race ...*/ len = LINK_UNKNOWN; break; } dhcpcd_handlecarrier(ctx, len, (unsigned int)ifm->ifm_flags, ifp->name); break; case RTM_DELETE: if (~rtm->rtm_addrs & (RTA_DST | RTA_GATEWAY | RTA_NETMASK)) break; cp = (char *)(void *)(rtm + 1); sa = (struct sockaddr *)(void *)cp; get_addrs(rtm->rtm_addrs, cp, rti_info); switch (sa->sa_family) { #ifdef INET case AF_INET: memset(&rt, 0, sizeof(rt)); rt.iface = NULL; COPYOUT(rt.dest, rti_info[RTAX_DST]); COPYOUT(rt.net, rti_info[RTAX_NETMASK]); COPYOUT(rt.gate, rti_info[RTAX_GATEWAY]); ipv4_routedeleted(ctx, &rt); break; #endif #ifdef INET6 case AF_INET6: memset(&rt6, 0, sizeof(rt6)); rt6.iface = NULL; COPYOUT6(rt6.dest, rti_info[RTAX_DST]); COPYOUT6(rt6.net, rti_info[RTAX_NETMASK]); COPYOUT6(rt6.gate, rti_info[RTAX_GATEWAY]); ipv6_routedeleted(ctx, &rt6); break; #endif } #ifdef RTM_CHGADDR case RTM_CHGADDR: /* FALLTHROUGH */ #endif case RTM_DELADDR: /* FALLTHROUGH */ case RTM_NEWADDR: ifam = (struct ifa_msghdr *)(void *)p; if ((ifp = if_findindex(ctx, ifam->ifam_index)) == NULL) break; cp = (char *)(void *)(ifam + 1); get_addrs(ifam->ifam_addrs, cp, rti_info); if (rti_info[RTAX_IFA] == NULL) break; switch (rti_info[RTAX_IFA]->sa_family) { case AF_LINK: #ifdef RTM_CHGADDR if (rtm->rtm_type != RTM_CHGADDR) break; #else if (rtm->rtm_type != RTM_NEWADDR) break; #endif memcpy(&sdl, rti_info[RTAX_IFA], rti_info[RTAX_IFA]->sa_len); dhcpcd_handlehwaddr(ctx, ifp->name, (const unsigned char*)CLLADDR(&sdl), sdl.sdl_alen); break; #ifdef INET case AF_INET: case 255: /* FIXME: Why 255? */ COPYOUT(rt.dest, rti_info[RTAX_IFA]); COPYOUT(rt.net, rti_info[RTAX_NETMASK]); COPYOUT(rt.gate, rti_info[RTAX_BRD]); ipv4_handleifa(ctx, rtm->rtm_type, NULL, ifp->name, &rt.dest, &rt.net, &rt.gate); break; #endif #ifdef INET6 case AF_INET6: sin6 = (struct sockaddr_in6*)(void *) rti_info[RTAX_IFA]; ia6 = sin6->sin6_addr; #ifdef __KAME__ if (IN6_IS_ADDR_LINKLOCAL(&ia6)) ia6.s6_addr[2] = ia6.s6_addr[3] = '\0'; #endif if (rtm->rtm_type == RTM_NEWADDR) { ifa_flags = if_addrflags6(&ia6, ifp); if (ifa_flags == -1) break; } else ifa_flags = 0; ipv6_handleifa(ctx, rtm->rtm_type, NULL, ifp->name, &ia6, ifa_flags); break; #endif } break; } } return 0; }
/* * Determine the local and remote user, tty, and machines */ int get_names (int argc, char *argv[]) { char *his_name, *my_name; char *my_machine_name, *his_machine_name; char *his_tty; register char *cp; if ((my_name = getlogin ()) == NULL) { struct passwd *pw; if ((pw = getpwuid (getuid ())) == NULL) { printf ("You don't exist. Go away.\n"); exit (-1); } my_name = pw->pw_name; } my_machine_name = localhost (); if (!my_machine_name) { perror ("Cannot get local hostname"); exit (-1); } /* check for, and strip out, the machine name of the target */ for (cp = argv[0]; *cp && !strchr ("@:!.", *cp); cp++) ; if (*cp == '\0') { /* this is a local to local talk */ his_name = argv[0]; his_machine_name = my_machine_name; } else { if (*cp++ == '@') { /* user@host */ his_name = argv[0]; his_machine_name = cp; } else { /* host.user or host!user or host:user */ his_name = cp; his_machine_name = argv[0]; } *--cp = '\0'; } if (argc > 1) his_tty = argv[1]; /* tty name is arg 2 */ else his_tty = ""; get_addrs (my_machine_name, his_machine_name); /* * Initialize the message template. */ msg.vers = TALK_VERSION; msg.addr.sa_family = htons (AF_INET); msg.ctl_addr.sa_family = htons (AF_INET); msg.id_num = htonl (0); strncpy (msg.l_name, my_name, NAME_SIZE); msg.l_name[NAME_SIZE - 1] = '\0'; strncpy (msg.r_name, his_name, NAME_SIZE); msg.r_name[NAME_SIZE - 1] = '\0'; strncpy (msg.r_tty, his_tty, TTY_SIZE); msg.r_tty[TTY_SIZE - 1] = '\0'; free (my_machine_name); return 0; }