in_addr_t anubis_random_ip_address(const char *expression) { //get param anubis_srand(); char *buffer = anubis_parse_param("random_ip_address", expression); if(!buffer) return 0; in_addr_t addr; if(strlen(buffer) == 0) { do { addr = (in_addr_t)random(); }//end do while(addr == INADDR_ANY || addr == INADDR_BROADCAST || IN_MULTICAST(addr) || IN_PRIVATE(addr) || IN_EXPERIMENTAL(addr) || IN_LOOPBACK(addr) || IN_ZERONET(addr) || IN_LOCAL_GROUP(addr)); return addr; }//end if else { anubis_err("random_ip_address(): \"%s\" unknown parameter\n", buffer); return 0; }//end if }//end anubis_random_ip_address
/* Check if valid address */ static int valid_addr(struct in_addr *ina) { in_addr_t addr; addr = ntohl(ina->s_addr); if (IN_ZERONET(addr) || IN_LOOPBACK(addr) || IN_LINKLOCAL(addr)) return 0; return 1; }
static int icmp_reflect(struct nm_if *nmif, char *inbuf, int inlen) { int hlen; struct icmp *icmp; struct ip *ip; struct in_addr t; struct inet_addr *broadaddr, *src; ip = (struct ip *)inbuf; if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || IN_EXPERIMENTAL(ntohl(ip->ip_src.s_addr)) || IN_ZERONET(ntohl(ip->ip_src.s_addr)) ) { pktcnt.icmp_badaddr++; return (0); } t = ip->ip_dst; ip->ip_dst = ip->ip_src; /* * If the incoming packet was addressed directly to one of our * own addresses, use dst as the src for the reply. */ if (inet_our_addr(&t) != NULL) ip->ip_src = t; else if ((broadaddr = inet_our_broadcast(&t)) != NULL) { /* * If the incoming packet was addressed to one of our broadcast * addresses, use the non-broadcast address. */ ip->ip_src = broadaddr->addr.sin_addr; } else { /* * Use the address from the interface where the ICMP packet * come in. */ src = inet_get_if_addr(nmif); if (src == NULL) return (-1); ip->ip_src = src->addr.sin_addr; } ip->ip_ttl = IPDEFTTL; hlen = ip->ip_hl << 2; icmp = (struct icmp *)(inbuf + hlen); icmp->icmp_cksum = 0; icmp->icmp_cksum = in_cksum(inbuf + hlen, inlen - hlen); return (ip_output(nmif, inbuf, inlen)); }
/* Find IPv4 address of default outbound LAN interface */ int getaddr(char *iface, struct in_addr *ina) { struct ifaddrs *ifaddr, *ifa; char ifname[17] = { 0 }; char buf[20] = { 0 }; int rc = -1; if (!iface) iface = getifname(ifname, sizeof(ifname)); rc = getifaddrs(&ifaddr); if (rc) return -1; for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) { if (!ifa->ifa_addr) continue; if (ifa->ifa_flags & IFF_LOOPBACK) continue; if (!(ifa->ifa_flags & IFF_MULTICAST)) continue; if (ifa->ifa_addr->sa_family != AF_INET) continue; if (iface && strcmp(iface, ifa->ifa_name)) continue; rc = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), buf, sizeof(buf), NULL, 0, NI_NUMERICHOST); if (!rc) { if (!inet_aton(buf, ina)) continue; if (!valid_addr(ina)) continue; break; } } freeifaddrs(ifaddr); if (rc || IN_ZERONET(ntohl(ina->s_addr))) return -1; return 0; }
/* Get the IP address from interface */ static RC_TYPE do_ip_check_interface(DYN_DNS_CLIENT *p_self) { struct ifreq ifr; in_addr_t new_ip; char *new_ip_str; int i; if (p_self == NULL) { return RC_INVALID_POINTER; } if (p_self->check_interface) { logit(LOG_INFO, MODULE_TAG "Checking for IP# change, querying interface %s", p_self->check_interface); int sd = socket(PF_INET, SOCK_DGRAM, 0); if (sd < 0) { int code = os_get_socket_error(); logit(LOG_WARNING, MODULE_TAG "Failed opening network socket: %s", strerror(code)); return RC_IP_OS_SOCKET_INIT_FAILED; } memset(&ifr, 0, sizeof(struct ifreq)); ifr.ifr_addr.sa_family = AF_INET; snprintf(ifr.ifr_name, IFNAMSIZ, p_self->check_interface); if (ioctl(sd, SIOCGIFADDR, &ifr) != -1) { new_ip = ntohl(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr); new_ip_str = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr); } else { int code = os_get_socket_error(); logit(LOG_ERR, MODULE_TAG "Failed reading IP address of interface %s: %s", p_self->check_interface, strerror(code)); return RC_ERROR; } close(sd); } else { return RC_ERROR; } if (IN_ZERONET(new_ip) || IN_LOOPBACK(new_ip) || IN_LINKLOCAL(new_ip) || IN_MULTICAST(new_ip) || IN_EXPERIMENTAL(new_ip)) { logit(LOG_WARNING, MODULE_TAG "Interface %s has invalid IP# %s", p_self->check_interface, new_ip_str); return RC_ERROR; } int anychange = 0; for (i = 0; i < p_self->info_count; i++) { DYNDNS_INFO_TYPE *info = &p_self->info[i]; info->my_ip_has_changed = strcmp(info->my_ip_address.name, new_ip_str) != 0; if (info->my_ip_has_changed) { anychange++; strcpy(info->my_ip_address.name, new_ip_str); } } if (!anychange) { logit(LOG_INFO, MODULE_TAG "No IP# change detected, still at %s", new_ip_str); } return RC_OK; }