/* INTERFACE MANAGEMENT */ int ifconfig(char *name, int flags, ipaddr_t ipaddr, ipaddr_t netmask, ipaddr_t broadcast) { if_entry_p ife; int ret; mk_ip_table_rw(); EnterCritical(); if ((ife = get_if_by_name(name))) { bcopy(ipaddr, ife->ipaddr, 4); bcopy(netmask, ife->netmask, 4); bcopy(broadcast, ife->broadcast, 4); apply_netmask(ife->net, ipaddr, netmask); ife->flags = flags; ret = 0; } else { fprintf(stderr,"ifconfig: Device not found: %s\n",name); ret = -1; } ExitCritical(); mk_ip_table_ro(); return ret; }
/* searches only on interfaces */ int find_if(ipaddr_t ipaddr, int *ifnum) { if_entry_p ife; ipaddr_t net; int i; for (i = 0 ; i < GETNUMIFENTRIES; i++) { ife = GETIFP(i); if (ife->flags & IF_DOWN) continue; apply_netmask(net, ipaddr, ife->netmask); if (bcmp(&net,ife->net,4) == 0) { *ifnum = i; if (ife->flags & IF_BROADCAST && bcmp(ipaddr,ife->broadcast,4) == 0) { return IFRET_BROADCAST; } else if (ife->flags & IF_LOOPBACK) return IFRET_LOOPBACK; else if (bcmp(ipaddr,ife->ipaddr,4) == 0) /* if it is our address, and we are not loopback reject */ return IFRET_NOTFOUND; else return IFRET_RESOLVE; } } return IFRET_NOTFOUND; }
/* searches only on routes */ int find_route(ipaddr_t ipaddr, ipaddr_t gateway) { route_entry_p routep; ipaddr_t net; int i; for (i = 0 ; i < GETNUMROUTEENTRIES; i++) { routep = GETROUTEP(i); apply_netmask(net, ipaddr, routep->netmask); if (bcmp(&net,routep->net,4) == 0) { bcopy(routep->dst,gateway,4); return 0; } } return -1; }
bool ip_in_subnet(t_ip_addr *ip, t_ip_addr *subnet, int mask) { if ((ip == NULL) || (subnet == NULL)) { return false; } else if (ip->family != subnet->family) { return false; } /* Apply mask to client IP */ t_ip_addr test_ip; copy_ip(&test_ip, ip); if (apply_netmask(&test_ip, mask) == -1) { return false; } return same_ip(&test_ip, subnet); }