Annotation of XNU/bsd/netinet/in_hostcache.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
        !             3:  *
        !             4:  * @APPLE_LICENSE_HEADER_START@
        !             5:  * 
        !             6:  * The contents of this file constitute Original Code as defined in and
        !             7:  * are subject to the Apple Public Source License Version 1.1 (the
        !             8:  * "License").  You may not use this file except in compliance with the
        !             9:  * License.  Please obtain a copy of the License at
        !            10:  * http://www.apple.com/publicsource and read it before using this file.
        !            11:  * 
        !            12:  * This Original Code and all software distributed under the License are
        !            13:  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
        !            14:  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
        !            15:  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
        !            16:  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
        !            17:  * License for the specific language governing rights and limitations
        !            18:  * under the License.
        !            19:  * 
        !            20:  * @APPLE_LICENSE_HEADER_END@
        !            21:  */
        !            22: /*
        !            23:  * Copyright 1997 Massachusetts Institute of Technology
        !            24:  *
        !            25:  * Permission to use, copy, modify, and distribute this software and
        !            26:  * its documentation for any purpose and without fee is hereby
        !            27:  * granted, provided that both the above copyright notice and this
        !            28:  * permission notice appear in all copies, that both the above
        !            29:  * copyright notice and this permission notice appear in all
        !            30:  * supporting documentation, and that the name of M.I.T. not be used
        !            31:  * in advertising or publicity pertaining to distribution of the
        !            32:  * software without specific, written prior permission.  M.I.T. makes
        !            33:  * no representations about the suitability of this software for any
        !            34:  * purpose.  It is provided "as is" without express or implied
        !            35:  * warranty.
        !            36:  * 
        !            37:  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
        !            38:  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
        !            39:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
        !            40:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
        !            41:  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
        !            42:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
        !            43:  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
        !            44:  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
        !            45:  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        !            46:  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
        !            47:  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            48:  * SUCH DAMAGE.
        !            49:  *
        !            50:  */
        !            51: 
        !            52: #include <sys/param.h>
        !            53: #include <sys/systm.h>
        !            54: #include <sys/malloc.h>
        !            55: #include <sys/socket.h>
        !            56: #include <sys/socketvar.h>
        !            57: 
        !            58: #include <net/hostcache.h>
        !            59: #include <net/if.h>
        !            60: #include <net/if_var.h>
        !            61: #include <net/route.h>
        !            62: 
        !            63: #include <netinet/in.h>
        !            64: #include <netinet/in_hostcache.h>
        !            65: #include <netinet/tcp.h>
        !            66: #include <netinet/tcp_timer.h>
        !            67: #include <netinet/tcp_var.h>
        !            68: 
        !            69: /*
        !            70:  * Manage the IP per-host cache (really a thin veneer over the generic
        !            71:  * per-host cache code).
        !            72:  */
        !            73: 
        !            74: /* Look up an entry -- can be called from interrupt context. */
        !            75: struct in_hcentry *
        !            76: inhc_lookup(struct sockaddr_in *sin)
        !            77: {
        !            78:        struct hcentry *hc;
        !            79: 
        !            80:        hc = hc_get((struct sockaddr *)sin);
        !            81:        return ((struct in_hcentry *)hc);
        !            82: }
        !            83: 
        !            84: /* Look up and possibly create an entry -- must be called from user mode. */
        !            85: struct in_hcentry *
        !            86: inhc_alloc(struct sockaddr_in *sin)
        !            87: {
        !            88:        struct in_hcentry *inhc;
        !            89:        struct rtentry *rt;
        !            90:        int error;
        !            91:        /* xxx mutual exclusion for smp */
        !            92: 
        !            93:        inhc = inhc_lookup(sin);
        !            94:        if (inhc != 0)
        !            95:                return inhc;
        !            96: 
        !            97:        rt = rtalloc1(inhc->inhc_hc.hc_host, 1, 0);
        !            98:        if (rt == 0)
        !            99:                return 0;
        !           100: 
        !           101:        MALLOC(inhc, struct in_hcentry *, sizeof *inhc, M_HOSTCACHE, M_WAITOK);
        !           102:        bzero(inhc, sizeof *inhc);
        !           103:        inhc->inhc_hc.hc_host = dup_sockaddr((struct sockaddr *)sin, 1);
        !           104:        if (in_broadcast(sin->sin_addr, rt->rt_ifp))
        !           105:                inhc->inhc_flags |= INHC_BROADCAST;
        !           106:        else if (((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->sin_addr.s_addr
        !           107:                == sin->sin_addr.s_addr)
        !           108:                inhc->inhc_flags |= INHC_LOCAL;
        !           109:        else if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
        !           110:                inhc->inhc_flags |= INHC_MULTICAST;
        !           111:        inhc->inhc_pmtu = rt->rt_rmx.rmx_mtu;
        !           112:        inhc->inhc_recvpipe = rt->rt_rmx.rmx_recvpipe;
        !           113:        inhc->inhc_sendpipe = rt->rt_rmx.rmx_sendpipe;
        !           114:        inhc->inhc_ssthresh = rt->rt_rmx.rmx_ssthresh;
        !           115:        if (rt->rt_rmx.rmx_locks & RTV_RTT)
        !           116:                inhc->inhc_rttmin = rt->rt_rmx.rmx_rtt
        !           117:                        / (RTM_RTTUNIT / TCP_RTT_SCALE);
        !           118:        inhc->inhc_hc.hc_rt = rt;
        !           119:        error = hc_insert(&inhc->inhc_hc);
        !           120:        if (error != 0) {
        !           121:                RTFREE(rt);
        !           122:                FREE(inhc, M_HOSTCACHE);
        !           123:                return 0;
        !           124:        }
        !           125:        /*
        !           126:         * We don't return the structure directly because hc_get() needs
        !           127:         * to be allowed to do its own processing.
        !           128:         */
        !           129:        return (inhc_lookup(sin));
        !           130: }
        !           131: 
        !           132: /*
        !           133:  * This is Van Jacobson's hash function for IPv4 addresses.
        !           134:  * It is designed to work with a power-of-two-sized hash table.
        !           135:  */
        !           136: static u_long
        !           137: inhc_hash(struct sockaddr *sa, u_long nbuckets)
        !           138: {
        !           139:        u_long ip;
        !           140: 
        !           141:        ip = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
        !           142:        return ((ip ^ (ip >> 23) ^ (ip >> 17)) & ~(nbuckets - 1));
        !           143: }
        !           144: 
        !           145: /*
        !           146:  * We don't need to do any special work... if there are no references,
        !           147:  * as the caller has already ensured, then it's OK to kill.
        !           148:  */
        !           149: static int
        !           150: inhc_delete(struct hcentry *hc)
        !           151: {
        !           152:        return 0;
        !           153: }
        !           154: 
        !           155: /*
        !           156:  * Return the next increment for the number of buckets in the hash table.
        !           157:  * Zero means ``do not bump''.
        !           158:  */
        !           159: static u_long
        !           160: inhc_bump(u_long oldsize)
        !           161: {
        !           162:        if (oldsize < 512)
        !           163:                return (oldsize << 1);
        !           164:        return 0;
        !           165: }
        !           166: 
        !           167: static struct hccallback inhc_cb = {
        !           168:        inhc_hash, inhc_delete, inhc_bump
        !           169: };
        !           170: 
        !           171: int
        !           172: inhc_init(void)
        !           173: {
        !           174: 
        !           175:        return (hc_init(AF_INET, &inhc_cb, 128, 0));
        !           176: }
        !           177: 

unix.superglobalmegacorp.com

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