| File: | src/usr.sbin/ospf6d/carp.c |
| Warning: | line 63, column 7 Although the value stored to 'c' is used in the enclosing expression, the value is never actually read from 'c' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* $OpenBSD: carp.c,v 1.3 2009/09/30 12:22:03 claudio Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2006 Henning Brauer <henning@openbsd.org> |
| 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above |
| 8 | * copyright notice and this permission notice appear in all copies. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | */ |
| 18 | |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <sys/ioctl.h> |
| 22 | #include <net/if.h> |
| 23 | |
| 24 | #include <errno(*__errno()).h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include "ospf6d.h" |
| 30 | #include "log.h" |
| 31 | |
| 32 | struct carpgroup { |
| 33 | TAILQ_ENTRY(carpgroup)struct { struct carpgroup *tqe_next; struct carpgroup **tqe_prev ; } entry; |
| 34 | char *group; |
| 35 | int do_demote; |
| 36 | int changed_by; |
| 37 | }; |
| 38 | |
| 39 | TAILQ_HEAD(carpgroups, carpgroup)struct carpgroups { struct carpgroup *tqh_first; struct carpgroup **tqh_last; } carpgroups = |
| 40 | TAILQ_HEAD_INITIALIZER(carpgroups){ ((void*)0), &(carpgroups).tqh_first }; |
| 41 | |
| 42 | struct carpgroup *carp_group_find(char *group); |
| 43 | int carp_demote_ioctl(char *, int); |
| 44 | |
| 45 | struct carpgroup * |
| 46 | carp_group_find(char *group) |
| 47 | { |
| 48 | struct carpgroup *c; |
| 49 | |
| 50 | TAILQ_FOREACH(c, &carpgroups, entry)for((c) = ((&carpgroups)->tqh_first); (c) != ((void*)0 ); (c) = ((c)->entry.tqe_next)) |
| 51 | if (!strcmp(c->group, group)) |
| 52 | return (c); |
| 53 | |
| 54 | return (NULL((void*)0)); |
| 55 | } |
| 56 | |
| 57 | int |
| 58 | carp_demote_init(char *group, int force) |
| 59 | { |
| 60 | struct carpgroup *c; |
| 61 | int level; |
| 62 | |
| 63 | if ((c = carp_group_find(group)) == NULL((void*)0)) { |
Although the value stored to 'c' is used in the enclosing expression, the value is never actually read from 'c' | |
| 64 | if ((c = calloc(1, sizeof(struct carpgroup))) == NULL((void*)0)) { |
| 65 | log_warn("carp_demote_init calloc"); |
| 66 | return (-1); |
| 67 | } |
| 68 | if ((c->group = strdup(group)) == NULL((void*)0)) { |
| 69 | log_warn("carp_demote_init strdup"); |
| 70 | free(c); |
| 71 | return (-1); |
| 72 | } |
| 73 | |
| 74 | /* only demote if this group already is demoted */ |
| 75 | if ((level = carp_demote_get(group)) == -1) { |
| 76 | free(c->group); |
| 77 | free(c); |
| 78 | return (-1); |
| 79 | } |
| 80 | if (level > 0 || force) |
| 81 | c->do_demote = 1; |
| 82 | |
| 83 | TAILQ_INSERT_TAIL(&carpgroups, c, entry)do { (c)->entry.tqe_next = ((void*)0); (c)->entry.tqe_prev = (&carpgroups)->tqh_last; *(&carpgroups)->tqh_last = (c); (&carpgroups)->tqh_last = &(c)->entry.tqe_next ; } while (0); |
| 84 | } |
| 85 | |
| 86 | return (0); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | carp_demote_shutdown(void) |
| 91 | { |
| 92 | struct carpgroup *c; |
| 93 | |
| 94 | while ((c = TAILQ_FIRST(&carpgroups)((&carpgroups)->tqh_first)) != NULL((void*)0)) { |
| 95 | TAILQ_REMOVE(&carpgroups, c, entry)do { if (((c)->entry.tqe_next) != ((void*)0)) (c)->entry .tqe_next->entry.tqe_prev = (c)->entry.tqe_prev; else ( &carpgroups)->tqh_last = (c)->entry.tqe_prev; *(c)-> entry.tqe_prev = (c)->entry.tqe_next; ; ; } while (0); |
| 96 | if (c->do_demote && c->changed_by > 0) |
| 97 | carp_demote_ioctl(c->group, -c->changed_by); |
| 98 | |
| 99 | free(c->group); |
| 100 | free(c); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | int |
| 105 | carp_demote_get(char *group) |
| 106 | { |
| 107 | int s; |
| 108 | struct ifgroupreq ifgr; |
| 109 | |
| 110 | if ((s = socket(AF_INET2, SOCK_DGRAM2, 0)) == -1) { |
| 111 | log_warn("carp_demote_get: socket"); |
| 112 | return (-1); |
| 113 | } |
| 114 | |
| 115 | bzero(&ifgr, sizeof(ifgr)); |
| 116 | strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name)); |
| 117 | |
| 118 | if (ioctl(s, SIOCGIFGATTR(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof (struct ifgroupreq) & 0x1fff) << 16) | ((('i')) << 8) | ((139))), (caddr_t)&ifgr) == -1) { |
| 119 | if (errno(*__errno()) == ENOENT2) |
| 120 | log_warnx("group \"%s\" does not exist", group); |
| 121 | else |
| 122 | log_warn("carp_demote_get: ioctl"); |
| 123 | close(s); |
| 124 | return (-1); |
| 125 | } |
| 126 | |
| 127 | close(s); |
| 128 | return ((int)ifgr.ifgr_attribifgr_ifgru.ifgru_attrib.ifg_carp_demoted); |
| 129 | } |
| 130 | |
| 131 | int |
| 132 | carp_demote_set(char *group, int demote) |
| 133 | { |
| 134 | struct carpgroup *c; |
| 135 | |
| 136 | if ((c = carp_group_find(group)) == NULL((void*)0)) { |
| 137 | log_warnx("carp_group_find for %s returned NULL?!", group); |
| 138 | return (-1); |
| 139 | } |
| 140 | |
| 141 | if (c->changed_by + demote < 0) { |
| 142 | log_warnx("carp_demote_set: changed_by + demote < 0"); |
| 143 | return (-1); |
| 144 | } |
| 145 | |
| 146 | if (c->do_demote && carp_demote_ioctl(group, demote) == -1) |
| 147 | return (-1); |
| 148 | |
| 149 | c->changed_by += demote; |
| 150 | |
| 151 | /* enable demotion when we return to 0, i. e. all sessions up */ |
| 152 | if (demote < 0 && c->changed_by == 0) |
| 153 | c->do_demote = 1; |
| 154 | |
| 155 | return (0); |
| 156 | } |
| 157 | |
| 158 | int |
| 159 | carp_demote_ioctl(char *group, int demote) |
| 160 | { |
| 161 | int s, res; |
| 162 | struct ifgroupreq ifgr; |
| 163 | |
| 164 | if ((s = socket(AF_INET2, SOCK_DGRAM2, 0)) == -1) { |
| 165 | log_warn("carp_demote_get: socket"); |
| 166 | return (-1); |
| 167 | } |
| 168 | |
| 169 | bzero(&ifgr, sizeof(ifgr)); |
| 170 | strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name)); |
| 171 | ifgr.ifgr_attribifgr_ifgru.ifgru_attrib.ifg_carp_demoted = demote; |
| 172 | |
| 173 | if ((res = ioctl(s, SIOCSIFGATTR((unsigned long)0x80000000 | ((sizeof(struct ifgroupreq) & 0x1fff) << 16) | ((('i')) << 8) | ((140))), (caddr_t)&ifgr)) == -1) |
| 174 | log_warn("unable to %s the demote state " |
| 175 | "of group '%s'", (demote > 0) ? "increment" : "decrement", |
| 176 | group); |
| 177 | else |
| 178 | log_info("%s the demote state of group '%s'", |
| 179 | (demote > 0) ? "incremented" : "decremented", group); |
| 180 | |
| 181 | close(s); |
| 182 | return (res); |
| 183 | } |