/**************************************************************************** add an interface to the linked list of interfaces ****************************************************************************/ static void add_interface(TALLOC_CTX *mem_ctx, struct in_addr ip, struct in_addr nmask, struct interface **interfaces) { struct interface *iface; struct in_addr bcast; if (iface_find(*interfaces, ip, false)) { DEBUG(3,("not adding duplicate interface %s\n",inet_ntoa(ip))); return; } iface = talloc(*interfaces == NULL ? mem_ctx : *interfaces, struct interface); if (iface == NULL) return; ZERO_STRUCTPN(iface); iface->ip = ip; iface->nmask = nmask; bcast.s_addr = MKBCADDR(iface->ip.s_addr, iface->nmask.s_addr); /* keep string versions too, to avoid people tripping over the implied static in inet_ntoa() */ iface->ip_s = talloc_strdup(iface, inet_ntoa(iface->ip)); iface->nmask_s = talloc_strdup(iface, inet_ntoa(iface->nmask)); if (nmask.s_addr != ~0) { iface->bcast_s = talloc_strdup(iface, inet_ntoa(bcast)); } DLIST_ADD_END(*interfaces, iface, struct interface *); DEBUG(3,("added interface ip=%s nmask=%s\n", iface->ip_s, iface->nmask_s)); }
/**************************************************************************** add an interface to the linked list of interfaces ****************************************************************************/ static void add_interface(struct in_addr ip, struct in_addr nmask) { struct interface *iface; if (iface_find(ip, False)) { DEBUG(3,("not adding duplicate interface %s\n",inet_ntoa(ip))); return; } #if !defined(__s390__) if (ip_equal(nmask, allones_ip)) { DEBUG(3,("not adding non-broadcast interface %s\n",inet_ntoa(ip))); return; } #endif iface = SMB_MALLOC_P(struct interface); if (!iface) return; ZERO_STRUCTPN(iface); iface->ip = ip; iface->nmask = nmask; iface->bcast.s_addr = MKBCADDR(iface->ip.s_addr, iface->nmask.s_addr); DLIST_ADD(local_interfaces, iface); DEBUG(2,("added interface ip=%s ",inet_ntoa(iface->ip))); DEBUG(2,("bcast=%s ",inet_ntoa(iface->bcast))); DEBUG(2,("nmask=%s\n",inet_ntoa(iface->nmask))); }
static void set_hostname(char *host) { const struct iflist *ifl = iface_find(host); if (ifl == NULL) { yell("no such hostname [%s]!", host); } else if (local_host_name && !strcmp(local_host_name, ifl->ifi_host)) { bitchsay("%s is already my hostname, yo.", local_host_name); } else { sa_addr_t *addr; sa_rc_t ret; malloc_strcpy(&local_host_name, ifl->ifi_host); sa_addr_create(&addr); ret = sa_addr_s2a(addr, ifl->ifi_addr, ifl->ifi_len); if (ret != SA_OK) { sa_addr_destroy(addr); bitchsay("couldn't set local hostname: %s", sa_error(ret)); } else { bitchsay("local host name is now %s", local_host_name); if (local_host_addr) sa_addr_destroy(local_host_addr); local_host_addr = addr; t_parse_command("RECONNECT", NULL); } } }
/** return true if an IP is one one of our local networks */ bool iface_is_local(struct interface *ifaces, const char *dest) { struct in_addr ip; ip.s_addr = interpret_addr(dest); if (iface_find(ifaces, ip, true)) { return true; } return false; }
/** return the local IP address that best matches a destination IP, or our first interface if none match */ const char *iface_best_ip(struct interface *ifaces, const char *dest) { struct interface *iface; struct in_addr ip; ip.s_addr = interpret_addr(dest); iface = iface_find(ifaces, ip, true); if (iface) { return iface->ip_s; } return iface_n_ip(ifaces, 0); }
static void add_interface(const struct iface_struct *ifs) { char addr[INET6_ADDRSTRLEN]; struct interface *iface; if (iface_find((const struct sockaddr *)&ifs->ip, False)) { DEBUG(3,("add_interface: not adding duplicate interface %s\n", print_sockaddr(addr, sizeof(addr), &ifs->ip) )); return; } if (!(ifs->flags & (IFF_BROADCAST|IFF_LOOPBACK))) { DEBUG(3,("not adding non-broadcast interface %s\n", ifs->name )); return; } iface = SMB_MALLOC_P(struct interface); if (!iface) { return; } ZERO_STRUCTPN(iface); iface->name = SMB_STRDUP(ifs->name); if (!iface->name) { SAFE_FREE(iface); return; } iface->flags = ifs->flags; iface->ip = ifs->ip; iface->netmask = ifs->netmask; iface->bcast = ifs->bcast; DLIST_ADD(local_interfaces, iface); DEBUG(2,("added interface %s ip=%s ", iface->name, print_sockaddr(addr, sizeof(addr), &iface->ip) )); DEBUG(2,("bcast=%s ", print_sockaddr(addr, sizeof(addr), &iface->bcast) )); DEBUG(2,("netmask=%s\n", print_sockaddr(addr, sizeof(addr), &iface->netmask) )); }
const struct sockaddr_storage *iface_ip(const struct sockaddr *ip) { struct interface *i = iface_find(ip, true); if (i) { return &i->ip; } /* Search for the first interface with * matching address family. */ for (i=local_interfaces;i;i=i->next) { if (i->ip.ss_family == ip->sa_family) { return &i->ip; } } return NULL; }
/* return True if a IP is directly reachable on one of our interfaces */ BOOL iface_local(struct in_addr ip) { return iface_find(ip, True) ? True : False; }
struct in_addr *iface_ip(struct in_addr ip) { struct interface *i = iface_find(ip, True); return(i ? &i->ip : &local_interfaces->ip); }
struct in_addr *iface_bcast(struct in_addr ip) { struct interface *i = iface_find(ip, True); return(i ? &i->bcast : &local_interfaces->bcast); }
bool iface_local(const struct sockaddr *ip) { return iface_find(ip, true) ? true : false; }