Annotation of 43BSDReno/sbin/routed/af.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[] = "@(#)af.c       5.10 (Berkeley) 6/1/90";
        !            22: #endif /* not lint */
        !            23: 
        !            24: #include "defs.h"
        !            25: 
        !            26: /*
        !            27:  * Address family support routines
        !            28:  */
        !            29: int    inet_hash(), inet_netmatch(), inet_output(),
        !            30:        inet_portmatch(), inet_portcheck(),
        !            31:        inet_checkhost(), inet_rtflags(), inet_sendroute(), inet_canon();
        !            32: char   *inet_format();
        !            33: 
        !            34: #define NIL    { 0 }
        !            35: #define        INET \
        !            36:        { inet_hash,            inet_netmatch,          inet_output, \
        !            37:          inet_portmatch,       inet_portcheck,         inet_checkhost, \
        !            38:          inet_rtflags,         inet_sendroute,         inet_canon, \
        !            39:          inet_format \
        !            40:        }
        !            41: 
        !            42: struct afswitch afswitch[AF_MAX] = {
        !            43:        NIL,            /* 0- unused */
        !            44:        NIL,            /* 1- Unix domain, unused */
        !            45:        INET,           /* Internet */
        !            46: };
        !            47: 
        !            48: int af_max = sizeof(afswitch) / sizeof(afswitch[0]);
        !            49: 
        !            50: struct sockaddr_in inet_default = {
        !            51: #ifdef RTM_ADD
        !            52:        sizeof (inet_default),
        !            53: #endif
        !            54:        AF_INET, INADDR_ANY };
        !            55: 
        !            56: inet_hash(sin, hp)
        !            57:        register struct sockaddr_in *sin;
        !            58:        struct afhash *hp;
        !            59: {
        !            60:        register u_long n;
        !            61: 
        !            62:        n = inet_netof(sin->sin_addr);
        !            63:        if (n)
        !            64:            while ((n & 0xff) == 0)
        !            65:                n >>= 8;
        !            66:        hp->afh_nethash = n;
        !            67:        hp->afh_hosthash = ntohl(sin->sin_addr.s_addr);
        !            68:        hp->afh_hosthash &= 0x7fffffff;
        !            69: }
        !            70: 
        !            71: inet_netmatch(sin1, sin2)
        !            72:        struct sockaddr_in *sin1, *sin2;
        !            73: {
        !            74: 
        !            75:        return (inet_netof(sin1->sin_addr) == inet_netof(sin2->sin_addr));
        !            76: }
        !            77: 
        !            78: /*
        !            79:  * Verify the message is from the right port.
        !            80:  */
        !            81: inet_portmatch(sin)
        !            82:        register struct sockaddr_in *sin;
        !            83: {
        !            84:        
        !            85:        return (sin->sin_port == sp->s_port);
        !            86: }
        !            87: 
        !            88: /*
        !            89:  * Verify the message is from a "trusted" port.
        !            90:  */
        !            91: inet_portcheck(sin)
        !            92:        struct sockaddr_in *sin;
        !            93: {
        !            94: 
        !            95:        return (ntohs(sin->sin_port) <= IPPORT_RESERVED);
        !            96: }
        !            97: 
        !            98: /*
        !            99:  * Internet output routine.
        !           100:  */
        !           101: inet_output(s, flags, sin, size)
        !           102:        int s, flags;
        !           103:        struct sockaddr_in *sin;
        !           104:        int size;
        !           105: {
        !           106:        struct sockaddr_in dst;
        !           107: 
        !           108:        dst = *sin;
        !           109:        sin = &dst;
        !           110:        if (sin->sin_port == 0)
        !           111:                sin->sin_port = sp->s_port;
        !           112:        if (sin->sin_len == 0)
        !           113:                sin->sin_len = sizeof (*sin);
        !           114:        if (sendto(s, packet, size, flags, sin, sizeof (*sin)) < 0)
        !           115:                perror("sendto");
        !           116: }
        !           117: 
        !           118: /*
        !           119:  * Return 1 if the address is believed
        !           120:  * for an Internet host -- THIS IS A KLUDGE.
        !           121:  */
        !           122: inet_checkhost(sin)
        !           123:        struct sockaddr_in *sin;
        !           124: {
        !           125:        u_long i = ntohl(sin->sin_addr.s_addr);
        !           126: 
        !           127: #ifndef IN_EXPERIMENTAL
        !           128: #define        IN_EXPERIMENTAL(i)      (((long) (i) & 0xe0000000) == 0xe0000000)
        !           129: #endif
        !           130: 
        !           131:        if (IN_EXPERIMENTAL(i) || sin->sin_port != 0)
        !           132:                return (0);
        !           133:        if (i != 0 && (i & 0xff000000) == 0)
        !           134:                return (0);
        !           135:        for (i = 0; i < sizeof(sin->sin_zero)/sizeof(sin->sin_zero[0]); i++)
        !           136:                if (sin->sin_zero[i])
        !           137:                        return (0);
        !           138:        return (1);
        !           139: }
        !           140: 
        !           141: inet_canon(sin)
        !           142:        struct sockaddr_in *sin;
        !           143: {
        !           144: 
        !           145:        sin->sin_port = 0;
        !           146:        sin->sin_len = sizeof(*sin);
        !           147: }
        !           148: 
        !           149: char *
        !           150: inet_format(sin)
        !           151:        struct sockaddr_in *sin;
        !           152: {
        !           153:        char *inet_ntoa();
        !           154: 
        !           155:        return (inet_ntoa(sin->sin_addr));
        !           156: }

unix.superglobalmegacorp.com

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