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

Defined functions

inet_lnaof defined in line 77; never used
inet_makeaddr defined in line 22; used 4 times
inet_netof defined in line 49; used 4 times
inet_rtflags defined in line 111; used 2 times
inet_sendsubnet defined in line 153; used 2 times

Defined variables

sccsid defined in line 8; never used
Last modified: 1987-07-21
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3054
Valid CSS Valid XHTML 1.0 Strict