Annotation of embedaddon/freevrrpd/vrrp_list.c, revision 1.1.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_list.c,v 1.1.1.1 2004/02/15 12:55:26 spe Exp $
                     33:  */
                     34: 
                     35: #include <errno.h>
                     36: #include "vrrp_list.h"
                     37: 
                     38: /*
                     39:  * We use a double chained list with sentinels ---  --- |f|->|d|->NULL
                     40:  * NULL<-| |<-| | ---  ---
                     41:  */
                     42: 
                     43: /*
                     44:  * Initialise la liste chainee avec un premier element: l'adresse reelle du
                     45:  * VRID
                     46:  */
                     47: char 
                     48: vrrp_list_initialize(struct vrrp_vr * vr, struct ether_addr * ethaddr)
                     49: {
                     50:        vr->vr_if->p = (struct vrrp_ethaddr_list *) malloc(sizeof(*(vr->vr_if->p)));
                     51:        vr->vr_if->d = (struct vrrp_ethaddr_list *) malloc(sizeof(*(vr->vr_if->d)));
                     52:        if (!vr->vr_if->p || !vr->vr_if->d) {
                     53:                syslog(LOG_ERR, "Can't allocate memory for vrrp_list_initialize: %s", strerror(errno));
                     54:                return -1;
                     55:        }
                     56:        bzero(vr->vr_if->p, sizeof(*vr->vr_if->p));
                     57:        bzero(vr->vr_if->d, sizeof(*vr->vr_if->d));
                     58:        vr->vr_if->p->previous = NULL;
                     59:        vr->vr_if->p->next = vr->vr_if->d;
                     60:        vr->vr_if->d->previous = vr->vr_if->p;
                     61:        vr->vr_if->d->next = NULL;
                     62:        if (vrrp_list_add(vr, ethaddr) == -1) {
                     63:                free(vr->vr_if->p);
                     64:                free(vr->vr_if->d);
                     65:                return -1;
                     66:        }
                     67:        return 0;
                     68: }
                     69: 
                     70: /*
                     71:  * Ajoute un nouvel element dans la liste
                     72:  */
                     73: char 
                     74: vrrp_list_add(struct vrrp_vr * vr, struct ether_addr *ethaddr)
                     75: {
                     76:        struct vrrp_ethaddr_list *n;
                     77: 
                     78:        if (!(n = (struct vrrp_ethaddr_list *) malloc(sizeof(*n)))) {
                     79:                syslog(LOG_ERR, "Can't allocate memory for vrrp_list_add: %s", strerror(errno));
                     80:                return -1;
                     81:        }
                     82:        bzero(n, sizeof(*n));
                     83:        bcopy(ethaddr, &n->ethaddr, sizeof(struct ether_addr));
                     84:        n->previous = vr->vr_if->d->previous;
                     85:        n->next = vr->vr_if->d;
                     86:        vr->vr_if->d->previous->next = n;
                     87:        vr->vr_if->d->previous = n;
                     88: 
                     89:        return 0;
                     90: }
                     91: 
                     92: /*
                     93:  * Enleve un element de la liste
                     94:  */
                     95: char 
                     96: vrrp_list_delete(struct vrrp_vr * vr, struct ether_addr ethaddr)
                     97: {
                     98:        struct vrrp_ethaddr_list *e = vr->vr_if->p;
                     99: 
                    100:        while (e->next && bcmp(&ethaddr, &e->ethaddr, sizeof(ethaddr)))
                    101:                e = e->next;
                    102:        if (!e->next)
                    103:                return -1;
                    104:        e->next->previous = e->previous;
                    105:        e->previous->next = e->next;
                    106:        free(e);
                    107: 
                    108:        return 0;
                    109: }
                    110: 
                    111: struct ether_addr 
                    112: vrrp_list_get_first(struct vrrp_vr * vr)
                    113: {
                    114:        return vr->vr_if->p->next->ethaddr;
                    115: }
                    116: 
                    117: /*
                    118:  * Renvoie l'adresse MAC du dernier element
                    119:  */
                    120: struct ether_addr 
                    121: vrrp_list_get_last(struct vrrp_vr * vr)
                    122: {
                    123:        return vr->vr_if->d->previous->ethaddr;
                    124: }
                    125: 
                    126: void 
                    127: vrrp_list_destroy(struct vrrp_vr * vr)
                    128: {
                    129:        vr->vr_if->d = vr->vr_if->d->previous;
                    130:        while (vr->vr_if->d != vr->vr_if->p) {
                    131:                free(vr->vr_if->d->next);
                    132:                vr->vr_if->d = vr->vr_if->d->previous;
                    133:        }
                    134:        free(vr->vr_if->d);
                    135:        free(vr->vr_if->p);
                    136: 
                    137:        return;
                    138: }

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