void interface_note_activity(struct hostdata *hp) /* save interface I/O counts */ { ifinfo_t ifinfo; struct query *ctl; /* if not monitoring link, all done */ if (!hp->monitor) return; /* get the current I/O stats for the monitored link */ if (get_ifinfo(hp->monitor, &ifinfo)) /* update this and preceeding host entries using the link (they were already set during this pass but the I/O count has now changed and they need to be re-updated) */ for (ctl = querylist; ctl; ctl = ctl->next) { if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor)) ctl->server.monitor_io = ifinfo.rx_packets + ifinfo.tx_packets; /* do NOT update host entries following this one */ if (&ctl->server == hp) break; } #ifdef ACTIVITY_DEBUG (void) report(stdout, GT_("activity on %s -noted- as %d\n"), hp->monitor, hp->monitor_io); #endif }
int main(int argc, char **argv) { struct ifreq ifreq; char *devname = "eth0"; struct sockaddr_in saddr; //IP address get_ifinfo( devname, &ifreq, SIOCGIFADDR); memcpy(&saddr, &(ifreq.ifr_addr), sizeof(saddr)); printf("%s\n", inet_ntoa(saddr.sin_addr)); //MAC address get_ifinfo( devname, &ifreq, SIOCGIFHWADDR); printf("%02x:%02x:%02x:%02x:%02x:%02x\n", (unsigned char)ifreq.ifr_hwaddr.sa_data[0], (unsigned char)ifreq.ifr_hwaddr.sa_data[1], (unsigned char)ifreq.ifr_hwaddr.sa_data[2], (unsigned char)ifreq.ifr_hwaddr.sa_data[3], (unsigned char)ifreq.ifr_hwaddr.sa_data[4], (unsigned char)ifreq.ifr_hwaddr.sa_data[5]); //ifIndex get_ifinfo( devname, &ifreq, SIOCGIFINDEX); printf("%d\n", ifreq.ifr_ifindex); return 0; }
/* * Function: open_socket() * * Description: * This function opens a socket for capture/sending * * Argument: * ifname: interface name * * Return value: * socket file descriptor for receiving packets */ int open_socket(const char *ifname) { int sd; /* Socket to packets */ struct ifreq ifinfo; /* Interface information */ struct sockaddr_ll lla; /* Link-local address info for receiving */ /* Create a socket for capture */ if ((sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) fatal_error("socket()"); /* Make a socket into non-blocking mode */ if (fcntl(sd, F_SETFL, O_NONBLOCK) < 0) fatal_error("fcntl()"); /* Get the logical interface number */ get_ifinfo(&ifinfo, sd, ifname, SIOCGIFINDEX); /* Bind to the interface */ memset(&lla, '\0', sizeof(struct sockaddr_ll)); lla.sll_family = PF_PACKET; lla.sll_protocol = htons(ETH_P_ALL); lla.sll_ifindex = ifinfo.ifr_ifindex; if (bind(sd, (struct sockaddr *)&lla, sizeof(struct sockaddr_ll)) < 0) fatal_error("bind()"); /* Change into the promiscuous mode */ get_ifinfo(&ifinfo, sd, ifname, SIOCGIFFLAGS); ifinfo.ifr_flags = ifinfo.ifr_flags | IFF_PROMISC; if (ioctl(sd, SIOCSIFFLAGS, &ifinfo) < 0) fatal_error("ioctl()"); if (debug) fprintf(stderr, "%s is changed into promiscuous mode\n", ifname); if (debug) fprintf(stderr, "Packet receiving socket is %d\n", sd); return sd; }
/* * Initialize "ifentry". * (get information of each interface) */ void ifconfig() #ifdef HAVE_GETIFADDRS { extern struct ifinfo *ifentry; struct ifaddrs *ifap, *ifa; struct sockaddr_in6 *sin6; struct ifinfo *ife; int s; if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) fatal("<ifconfig>: socket"); if (getifaddrs(&ifap)) fatal("<ifconfig>: getifaddrs"); for (ifa = ifap; ifa; ifa = ifa->ifa_next) { if (ifa->ifa_addr->sa_family == AF_INET6) { sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { if ((ife = find_if_by_name(ifa->ifa_name)) == NULL) { /* ifreq */ /* new interface */ MALLOC(ife, struct ifinfo); MALLOC(ife->ifi_ifn, struct if_nameindex); ife->ifi_ifn->if_index = if_nametoindex(ifa->ifa_name); ife->ifi_ifn->if_name = (char *)malloc(strlen(ifa->ifa_name) +1); strcpy(ife->ifi_ifn->if_name, ifa->ifa_name); get_ifinfo(ife); if (ifentry != NULL) { /* (global) */ insque(ife, ifentry); } else { ife->ifi_next = ife; ife->ifi_prev = ife; ifentry = ife; } } else { if (!IN6_IS_ADDR_UNSPECIFIED(&ife->ifi_laddr)) fatalx("<ifconfig>: link-local address cannot be doubly defined"); } memcpy(&ife->ifi_laddr, &sin6->sin6_addr, sizeof(struct in6_addr)); #ifdef ADVANCEDAPI CLEAR_IN6_LINKLOCAL_IFINDEX(&ife->ifi_laddr);/* Toshiba's IPv6 macro */ #endif }
int main(int argc, char **argv){ int ifindex = 0; char *devname = "eth0"; char *ipaddr = "10.0.7.1"; char *src_macaddr = "00:12:34:56:78:9a"; //char *src_macaddr = "00:1c:25:20:c7:63"; char *dst_macaddr = "00:00:00:00:00:00"; struct ifreq ifreq; memset(&ifreq, '\0', sizeof(ifreq)); //ifindex get_ifinfo( devname, &ifreq, SIOCGIFINDEX); ifindex = ifreq.ifr_ifindex; send_gratuitous_arp(ifindex, devname, ipaddr, src_macaddr, dst_macaddr); return 0; }
/* * Function: specify_hw_addr() * * Description: * This function specifies the hardware address from the interface name * * Argument: * lladdr_p: pointer to the sockaddr_ll structure * ifname: interface name where icmpv6 messages go out * * Return value: * None * */ void specify_hw_addr(struct sockaddr_ll *lladdr_p, const char *ifname) { int sock_fd; /* Socket for ioctl() */ struct ifreq ifinfo; /* Interface information */ if ((sock_fd = socket(AF_PACKET, SOCK_DGRAM, 0)) < 0) fatal_error("socket()"); lladdr_p->sll_family = AF_PACKET; /* Always AF_PACKET */ lladdr_p->sll_protocol = htons(ETH_P_IPV6); /* IPv6 */ lladdr_p->sll_hatype = ARPHRD_ETHER; /* Header type */ lladdr_p->sll_pkttype = PACKET_HOST; /* Packet type */ lladdr_p->sll_halen = ETH_ALEN; /* Length of address */ /* Get the MAC address of the interface at source host */ get_ifinfo(&ifinfo, sock_fd, ifname, SIOCGIFHWADDR); memcpy(lladdr_p->sll_addr, ifinfo.ifr_hwaddr.sa_data, ETH_ALEN); /* Get the interface index */ lladdr_p->sll_ifindex = if_nametoindex(ifname); close(sock_fd); }
int interface_approve(struct hostdata *hp, flag domonitor) /* return TRUE if OK to poll, FALSE otherwise */ { ifinfo_t ifinfo; /* check interface IP address (range), if specified */ if (hp->interface) { /* get interface info */ if (!get_ifinfo(hp->interface, &ifinfo)) { (void) report(stdout, GT_("skipping poll of %s, %s down\n"), hp->pollname, hp->interface); return(FALSE); } /* check the IP addresses (range) */ if (!( /* check remote IP address */ ((ifinfo.dstaddr.s_addr != 0) && (ifinfo.dstaddr.s_addr & hp->interface_pair->interface_mask.s_addr) == hp->interface_pair->interface_address.s_addr) || /* check local IP address */ ((ifinfo.addr.s_addr & hp->interface_pair->interface_mask.s_addr) == hp->interface_pair->interface_address.s_addr) ) ) { (void) report(stdout, GT_("skipping poll of %s, %s IP address excluded\n"), hp->pollname, hp->interface); return(FALSE); } } /* if not monitoring link, all done */ if (!domonitor || !hp->monitor) return(TRUE); #ifdef ACTIVITY_DEBUG (void) report(stdout, GT_("activity on %s checked as %d\n"), hp->monitor, hp->monitor_io); #endif /* if monitoring, check link for activity if it is up */ if (get_ifinfo(hp->monitor, &ifinfo)) { int diff = (ifinfo.rx_packets + ifinfo.tx_packets) - hp->monitor_io; /* * There are three cases here: * * (a) If the new packet count is less than the recorded one, * probably pppd was restarted while fetchmail was running. * Don't skip. * * (b) newpacket count is greater than the old packet count, * but the difference is small and may just reflect the overhead * of a link shutdown. Skip. * * (c) newpacket count is greater than the old packet count, * and the difference is large. Connection is live. Don't skip. */ if (diff >= 0 && diff <= MONITOR_SLOP) { (void) report(stdout, GT_("skipping poll of %s, %s inactive\n"), hp->pollname, hp->monitor); return(FALSE); } } #ifdef ACTIVITY_DEBUG report(stdout, GT_("activity on %s was %d, is %d\n"), hp->monitor, hp->monitor_io, ifinfo.rx_packets + ifinfo.tx_packets); #endif return(TRUE); }