Annotation of embedaddon/freevrrpd/vrrp_multicast.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (c) 2001,2002 Sebastien Petit <spe@bsdfr.org>
        !             3:  *
        !             4:  * Redistribution and use in source forms, with and without modification,
        !             5:  * are permitted provided that the following conditions are met:
        !             6:  * 1. Redistributions of source code must retain the above copyright notice,
        !             7:  *    this list of conditions and the following disclaimer.
        !             8:  * 2. Redistributions in binary form must reproduce the above copyright notice,
        !             9:  *    this list of conditions and the following disclaimer in the documentation
        !            10:  *    and/or other materials provided with the distribution. Obviously, it
        !            11:  *    would be nice if you gave credit where credit is due but requiring it
        !            12:  *    would be too onerous.
        !            13:  * 3. All advertising materials mentioning features or use of this software
        !            14:  *    must display the following acknowledgement:
        !            15:  *      This product includes software developed by Sebastien Petit.
        !            16:  * 4. Neither the name of its contributors may be used to endorse or promote
        !            17:  *    products derived from this software without specific prior written
        !            18:  *    permission.
        !            19:  *
        !            20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
        !            21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            30:  * SUCH DAMAGE.
        !            31:  *
        !            32:  * $Id: vrrp_multicast.c,v 1.7 2004/04/01 15:40:52 spe Exp $
        !            33:  */
        !            34: 
        !            35: #include <errno.h>
        !            36: #include "vrrp_multicast.h"
        !            37: 
        !            38: /* join multicast group with ip address */
        !            39: char 
        !            40: vrrp_multicast_join_group(int sd, u_char * multicast_ip, struct in_addr * interface_ip)
        !            41: {
        !            42:        struct ip_mreq  imr;
        !            43: 
        !            44:        bzero(&imr, sizeof(imr));
        !            45:        imr.imr_multiaddr.s_addr = inet_addr(multicast_ip);
        !            46:        imr.imr_interface.s_addr = interface_ip->s_addr;
        !            47:        if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imr, sizeof(imr)) == -1) {
        !            48:                syslog(LOG_ERR, "cannot join multicast group %s with ip source %s [ IP_ADD_MEMBERSHIP ]", multicast_ip, inet_ntoa(*interface_ip));
        !            49:                return -1;
        !            50:        }
        !            51:        return 0;
        !            52: }
        !            53: 
        !            54: /* Set multicast ttl IP */
        !            55: char 
        !            56: vrrp_multicast_set_ttl(int sd, u_char ttl)
        !            57: {
        !            58:        if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) == -1) {
        !            59:                syslog(LOG_ERR, "cannot set multicast TTL: %s", strerror(errno));
        !            60:                return -1;
        !            61:        }
        !            62:        return 0;
        !            63: }
        !            64: 
        !            65: char 
        !            66: vrrp_multicast_set_if(int sd, struct in_addr * addr, char *if_name)
        !            67: {
        !            68:        if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, addr, sizeof(*addr)) == -1) {
        !            69:                syslog(LOG_ERR, "cannot setsockopt IP_MULTICAST_IF on primary address of %s: %s", if_name, strerror(errno));
        !            70:                return -1;
        !            71:        }
        !            72:        return 0;
        !            73: }
        !            74: 
        !            75: int vrrp_multicast_set_loopback(int sd, u_char flag) {
        !            76:        if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP, &flag, sizeof(flag)) == -1) {
        !            77:                syslog(LOG_ERR, "cannot setsockopt IP_MULTICAST_LOOP: %s", strerror(errno));
        !            78:                return -1;
        !            79:        }
        !            80: 
        !            81:        return 0;
        !            82: }
        !            83: 
        !            84: /* Open VRRP socket and join multicast group */
        !            85: char 
        !            86: vrrp_multicast_set_socket(struct vrrp_vr * vr)
        !            87: {
        !            88:        if (vrrp_multicast_join_group(vr->sd, VRRP_MULTICAST_IP, (struct in_addr *) & vr->vr_if->ip_addrs[0]) == -1) {
        !            89:                close(vr->sd);
        !            90:                return -1;
        !            91:        }
        !            92:        if (vrrp_multicast_set_ttl(vr->sd, VRRP_MULTICAST_TTL) == -1) {
        !            93:                close(vr->sd);
        !            94:                return -1;
        !            95:        }
        !            96:        /* XXX don't need to force interface for multicast. Let the kernel
        !            97:         * choose by itself. this setsockopt doesn't work with ipsec/ah...
        !            98:         * don't know why */
        !            99:        if (vrrp_multicast_set_if(vr->sd, &vr->vr_if->ip_addrs[0], vr->vr_if->if_name) == -1) {
        !           100:                close(vr->sd);
        !           101:                return -1;
        !           102:        }
        !           103:        if (vrrp_multicast_set_loopback(vr->sd, VRRP_MULTICAST_DISABLE_LOOPBACK) == -1) {
        !           104:                close(vr->sd);
        !           105:                return -1;
        !           106:        }
        !           107: 
        !           108:        return 0;
        !           109: }
        !           110: 
        !           111: char 
        !           112: vrrp_multicast_open_socket(struct vrrp_vr * vr)
        !           113: {
        !           114:        if (vrrp_network_open_socket(vr) == -1)
        !           115:                return -1;
        !           116:        if (vrrp_multicast_set_socket(vr) == -1)
        !           117:                return -1;
        !           118: 
        !           119:        return 0;
        !           120: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>