|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1983 Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution and use in source and binary forms are permitted ! 6: * provided that this notice is preserved and that due credit is given ! 7: * to the University of California at Berkeley. The name of the University ! 8: * may not be used to endorse or promote products derived from this ! 9: * software without specific prior written permission. This software ! 10: * is provided ``as is'' without express or implied warranty. ! 11: */ ! 12: ! 13: #ifndef lint ! 14: static char sccsid[] = "@(#)inet.c 5.6 (Berkeley) 2/16/88"; ! 15: #endif /* not lint */ ! 16: ! 17: /* ! 18: * Temporarily, copy these routines from the kernel, ! 19: * as we need to know about subnets. ! 20: */ ! 21: #include "defs.h" ! 22: ! 23: extern struct interface *ifnet; ! 24: ! 25: /* ! 26: * Formulate an Internet address from network + host. ! 27: */ ! 28: struct in_addr ! 29: inet_makeaddr(net, host) ! 30: u_long net, host; ! 31: { ! 32: register struct interface *ifp; ! 33: register u_long mask; ! 34: u_long addr; ! 35: ! 36: if (IN_CLASSA(net)) ! 37: mask = IN_CLASSA_HOST; ! 38: else if (IN_CLASSB(net)) ! 39: mask = IN_CLASSB_HOST; ! 40: else ! 41: mask = IN_CLASSC_HOST; ! 42: for (ifp = ifnet; ifp; ifp = ifp->int_next) ! 43: if ((ifp->int_netmask & net) == ifp->int_net) { ! 44: mask = ~ifp->int_subnetmask; ! 45: break; ! 46: } ! 47: addr = net | (host & mask); ! 48: addr = htonl(addr); ! 49: return (*(struct in_addr *)&addr); ! 50: } ! 51: ! 52: /* ! 53: * Return the network number from an internet address. ! 54: */ ! 55: inet_netof(in) ! 56: struct in_addr in; ! 57: { ! 58: register u_long i = ntohl(in.s_addr); ! 59: register u_long net; ! 60: register struct interface *ifp; ! 61: ! 62: if (IN_CLASSA(i)) ! 63: net = i & IN_CLASSA_NET; ! 64: else if (IN_CLASSB(i)) ! 65: net = i & IN_CLASSB_NET; ! 66: else ! 67: net = i & IN_CLASSC_NET; ! 68: ! 69: /* ! 70: * Check whether network is a subnet; ! 71: * if so, return subnet number. ! 72: */ ! 73: for (ifp = ifnet; ifp; ifp = ifp->int_next) ! 74: if ((ifp->int_netmask & net) == ifp->int_net) ! 75: return (i & ifp->int_subnetmask); ! 76: return (net); ! 77: } ! 78: ! 79: /* ! 80: * Return the host portion of an internet address. ! 81: */ ! 82: inet_lnaof(in) ! 83: struct in_addr in; ! 84: { ! 85: register u_long i = ntohl(in.s_addr); ! 86: register u_long net, host; ! 87: register struct interface *ifp; ! 88: ! 89: if (IN_CLASSA(i)) { ! 90: net = i & IN_CLASSA_NET; ! 91: host = i & IN_CLASSA_HOST; ! 92: } else if (IN_CLASSB(i)) { ! 93: net = i & IN_CLASSB_NET; ! 94: host = i & IN_CLASSB_HOST; ! 95: } else { ! 96: net = i & IN_CLASSC_NET; ! 97: host = i & IN_CLASSC_HOST; ! 98: } ! 99: ! 100: /* ! 101: * Check whether network is a subnet; ! 102: * if so, use the modified interpretation of `host'. ! 103: */ ! 104: for (ifp = ifnet; ifp; ifp = ifp->int_next) ! 105: if ((ifp->int_netmask & net) == ifp->int_net) ! 106: return (host &~ ifp->int_subnetmask); ! 107: return (host); ! 108: } ! 109: ! 110: /* ! 111: * Return RTF_HOST if the address is ! 112: * for an Internet host, RTF_SUBNET for a subnet, ! 113: * 0 for a network. ! 114: */ ! 115: inet_rtflags(sin) ! 116: struct sockaddr_in *sin; ! 117: { ! 118: register u_long i = ntohl(sin->sin_addr.s_addr); ! 119: register u_long net, host; ! 120: register struct interface *ifp; ! 121: ! 122: if (IN_CLASSA(i)) { ! 123: net = i & IN_CLASSA_NET; ! 124: host = i & IN_CLASSA_HOST; ! 125: } else if (IN_CLASSB(i)) { ! 126: net = i & IN_CLASSB_NET; ! 127: host = i & IN_CLASSB_HOST; ! 128: } else { ! 129: net = i & IN_CLASSC_NET; ! 130: host = i & IN_CLASSC_HOST; ! 131: } ! 132: ! 133: /* ! 134: * Check whether this network is subnetted; ! 135: * if so, check whether this is a subnet or a host. ! 136: */ ! 137: for (ifp = ifnet; ifp; ifp = ifp->int_next) ! 138: if (net == ifp->int_net) { ! 139: if (host &~ ifp->int_subnetmask) ! 140: return (RTF_HOST); ! 141: else if (ifp->int_subnetmask != ifp->int_netmask) ! 142: return (RTF_SUBNET); ! 143: else ! 144: return (0); /* network */ ! 145: } ! 146: if (host == 0) ! 147: return (0); /* network */ ! 148: else ! 149: return (RTF_HOST); ! 150: } ! 151: ! 152: /* ! 153: * Return true if a route to subnet/host of route rt should be sent to dst. ! 154: * Send it only if dst is on the same logical network if not "internal", ! 155: * otherwise only if the route is the "internal" route for the logical net. ! 156: */ ! 157: inet_sendroute(rt, dst) ! 158: struct rt_entry *rt; ! 159: struct sockaddr_in *dst; ! 160: { ! 161: register u_long r = ! 162: ntohl(((struct sockaddr_in *)&rt->rt_dst)->sin_addr.s_addr); ! 163: register u_long d = ntohl(dst->sin_addr.s_addr); ! 164: ! 165: if (IN_CLASSA(r)) { ! 166: if ((r & IN_CLASSA_NET) == (d & IN_CLASSA_NET)) { ! 167: if ((r & IN_CLASSA_HOST) == 0) ! 168: return ((rt->rt_state & RTS_INTERNAL) == 0); ! 169: return (1); ! 170: } ! 171: if (r & IN_CLASSA_HOST) ! 172: return (0); ! 173: return ((rt->rt_state & RTS_INTERNAL) != 0); ! 174: } else if (IN_CLASSB(r)) { ! 175: if ((r & IN_CLASSB_NET) == (d & IN_CLASSB_NET)) { ! 176: if ((r & IN_CLASSB_HOST) == 0) ! 177: return ((rt->rt_state & RTS_INTERNAL) == 0); ! 178: return (1); ! 179: } ! 180: if (r & IN_CLASSB_HOST) ! 181: return (0); ! 182: return ((rt->rt_state & RTS_INTERNAL) != 0); ! 183: } else { ! 184: if ((r & IN_CLASSC_NET) == (d & IN_CLASSC_NET)) { ! 185: if ((r & IN_CLASSC_HOST) == 0) ! 186: return ((rt->rt_state & RTS_INTERNAL) == 0); ! 187: return (1); ! 188: } ! 189: if (r & IN_CLASSC_HOST) ! 190: return (0); ! 191: return ((rt->rt_state & RTS_INTERNAL) != 0); ! 192: } ! 193: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.