Annotation of 43BSDReno/sbin/routed/inet.c, revision 1.1

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: (1) source distributions retain this entire copyright
        !             7:  * notice and comment, and (2) distributions including binaries display
        !             8:  * the following acknowledgement:  ``This product includes software
        !             9:  * developed by the University of California, Berkeley and its contributors''
        !            10:  * in the documentation or other materials provided with the distribution
        !            11:  * and in all advertising materials mentioning features or use of this
        !            12:  * software. Neither the name of the University nor the names of its
        !            13:  * contributors may be used to endorse or promote products derived
        !            14:  * from this software without specific prior written permission.
        !            15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            18:  */
        !            19: 
        !            20: #ifndef lint
        !            21: static char sccsid[] = "@(#)inet.c     5.8 (Berkeley) 6/1/90";
        !            22: #endif /* not lint */
        !            23: 
        !            24: /*
        !            25:  * Temporarily, copy these routines from the kernel,
        !            26:  * as we need to know about subnets.
        !            27:  */
        !            28: #include "defs.h"
        !            29: 
        !            30: extern struct interface *ifnet;
        !            31: 
        !            32: /*
        !            33:  * Formulate an Internet address from network + host.
        !            34:  */
        !            35: struct in_addr
        !            36: inet_makeaddr(net, host)
        !            37:        u_long net, host;
        !            38: {
        !            39:        register struct interface *ifp;
        !            40:        register u_long mask;
        !            41:        u_long addr;
        !            42: 
        !            43:        if (IN_CLASSA(net))
        !            44:                mask = IN_CLASSA_HOST;
        !            45:        else if (IN_CLASSB(net))
        !            46:                mask = IN_CLASSB_HOST;
        !            47:        else
        !            48:                mask = IN_CLASSC_HOST;
        !            49:        for (ifp = ifnet; ifp; ifp = ifp->int_next)
        !            50:                if ((ifp->int_netmask & net) == ifp->int_net) {
        !            51:                        mask = ~ifp->int_subnetmask;
        !            52:                        break;
        !            53:                }
        !            54:        addr = net | (host & mask);
        !            55:        addr = htonl(addr);
        !            56:        return (*(struct in_addr *)&addr);
        !            57: }
        !            58: 
        !            59: /*
        !            60:  * Return the network number from an internet address.
        !            61:  */
        !            62: inet_netof(in)
        !            63:        struct in_addr in;
        !            64: {
        !            65:        register u_long i = ntohl(in.s_addr);
        !            66:        register u_long net;
        !            67:        register struct interface *ifp;
        !            68: 
        !            69:        if (IN_CLASSA(i))
        !            70:                net = i & IN_CLASSA_NET;
        !            71:        else if (IN_CLASSB(i))
        !            72:                net = i & IN_CLASSB_NET;
        !            73:        else
        !            74:                net = i & IN_CLASSC_NET;
        !            75: 
        !            76:        /*
        !            77:         * Check whether network is a subnet;
        !            78:         * if so, return subnet number.
        !            79:         */
        !            80:        for (ifp = ifnet; ifp; ifp = ifp->int_next)
        !            81:                if ((ifp->int_netmask & net) == ifp->int_net)
        !            82:                        return (i & ifp->int_subnetmask);
        !            83:        return (net);
        !            84: }
        !            85: 
        !            86: /*
        !            87:  * Return the host portion of an internet address.
        !            88:  */
        !            89: inet_lnaof(in)
        !            90:        struct in_addr in;
        !            91: {
        !            92:        register u_long i = ntohl(in.s_addr);
        !            93:        register u_long net, host;
        !            94:        register struct interface *ifp;
        !            95: 
        !            96:        if (IN_CLASSA(i)) {
        !            97:                net = i & IN_CLASSA_NET;
        !            98:                host = i & IN_CLASSA_HOST;
        !            99:        } else if (IN_CLASSB(i)) {
        !           100:                net = i & IN_CLASSB_NET;
        !           101:                host = i & IN_CLASSB_HOST;
        !           102:        } else {
        !           103:                net = i & IN_CLASSC_NET;
        !           104:                host = i & IN_CLASSC_HOST;
        !           105:        }
        !           106: 
        !           107:        /*
        !           108:         * Check whether network is a subnet;
        !           109:         * if so, use the modified interpretation of `host'.
        !           110:         */
        !           111:        for (ifp = ifnet; ifp; ifp = ifp->int_next)
        !           112:                if ((ifp->int_netmask & net) == ifp->int_net)
        !           113:                        return (host &~ ifp->int_subnetmask);
        !           114:        return (host);
        !           115: }
        !           116: 
        !           117: /*
        !           118:  * Return RTF_HOST if the address is
        !           119:  * for an Internet host, RTF_SUBNET for a subnet,
        !           120:  * 0 for a network.
        !           121:  */
        !           122: inet_rtflags(sin)
        !           123:        struct sockaddr_in *sin;
        !           124: {
        !           125:        register u_long i = ntohl(sin->sin_addr.s_addr);
        !           126:        register u_long net, host;
        !           127:        register struct interface *ifp;
        !           128: 
        !           129:        if (IN_CLASSA(i)) {
        !           130:                net = i & IN_CLASSA_NET;
        !           131:                host = i & IN_CLASSA_HOST;
        !           132:        } else if (IN_CLASSB(i)) {
        !           133:                net = i & IN_CLASSB_NET;
        !           134:                host = i & IN_CLASSB_HOST;
        !           135:        } else {
        !           136:                net = i & IN_CLASSC_NET;
        !           137:                host = i & IN_CLASSC_HOST;
        !           138:        }
        !           139: 
        !           140:        /*
        !           141:         * Check whether this network is subnetted;
        !           142:         * if so, check whether this is a subnet or a host.
        !           143:         */
        !           144:        for (ifp = ifnet; ifp; ifp = ifp->int_next)
        !           145:                if (net == ifp->int_net) {
        !           146:                        if (host &~ ifp->int_subnetmask)
        !           147:                                return (RTF_HOST);
        !           148:                        else if (ifp->int_subnetmask != ifp->int_netmask)
        !           149:                                return (RTF_SUBNET);
        !           150:                        else
        !           151:                                return (0);             /* network */
        !           152:                }
        !           153:        if (host == 0)
        !           154:                return (0);     /* network */
        !           155:        else
        !           156:                return (RTF_HOST);
        !           157: }
        !           158: 
        !           159: /*
        !           160:  * Return true if a route to subnet/host of route rt should be sent to dst.
        !           161:  * Send it only if dst is on the same logical network if not "internal",
        !           162:  * otherwise only if the route is the "internal" route for the logical net.
        !           163:  */
        !           164: inet_sendroute(rt, dst)
        !           165:        struct rt_entry *rt;
        !           166:        struct sockaddr_in *dst;
        !           167: {
        !           168:        register u_long r =
        !           169:            ntohl(((struct sockaddr_in *)&rt->rt_dst)->sin_addr.s_addr);
        !           170:        register u_long d = ntohl(dst->sin_addr.s_addr);
        !           171: 
        !           172:        if (IN_CLASSA(r)) {
        !           173:                if ((r & IN_CLASSA_NET) == (d & IN_CLASSA_NET)) {
        !           174:                        if ((r & IN_CLASSA_HOST) == 0)
        !           175:                                return ((rt->rt_state & RTS_INTERNAL) == 0);
        !           176:                        return (1);
        !           177:                }
        !           178:                if (r & IN_CLASSA_HOST)
        !           179:                        return (0);
        !           180:                return ((rt->rt_state & RTS_INTERNAL) != 0);
        !           181:        } else if (IN_CLASSB(r)) {
        !           182:                if ((r & IN_CLASSB_NET) == (d & IN_CLASSB_NET)) {
        !           183:                        if ((r & IN_CLASSB_HOST) == 0)
        !           184:                                return ((rt->rt_state & RTS_INTERNAL) == 0);
        !           185:                        return (1);
        !           186:                }
        !           187:                if (r & IN_CLASSB_HOST)
        !           188:                        return (0);
        !           189:                return ((rt->rt_state & RTS_INTERNAL) != 0);
        !           190:        } else {
        !           191:                if ((r & IN_CLASSC_NET) == (d & IN_CLASSC_NET)) {
        !           192:                        if ((r & IN_CLASSC_HOST) == 0)
        !           193:                                return ((rt->rt_state & RTS_INTERNAL) == 0);
        !           194:                        return (1);
        !           195:                }
        !           196:                if (r & IN_CLASSC_HOST)
        !           197:                        return (0);
        !           198:                return ((rt->rt_state & RTS_INTERNAL) != 0);
        !           199:        }
        !           200: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.