[PATCH 1/2] Added support for carp validation on BSD

Fredrik Danerklint fredan-gitbird at fredan.se
Wed Jan 18 19:03:47 CET 2012


---
 sysdep/bsd/check_carp.c |  102 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 102 insertions(+), 0 deletions(-)
 create mode 100644 sysdep/bsd/check_carp.c

diff --git a/sysdep/bsd/check_carp.c b/sysdep/bsd/check_carp.c
new file mode 100644
index 0000000..f6df437
--- /dev/null
+++ b/sysdep/bsd/check_carp.c
@@ -0,0 +1,102 @@
+/*      $FreeBSD: src/sbin/ifconfig/ifcarp.c,v 1.2.24.2 2010/02/07 06:22:28 ru Exp $ */
+/*      from $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $ */
+
+/*
+ * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
+ * Copyright (c) 2003 Ryan McBride. All rights reserved.
+ * Copyright (c) 1983, 1993
+ *      The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * This was ifconfig.c and if_carp.c from FreeBSD 8.2.
+ * Modified to only return the state of an carp interface if there is any.
+ * Will return an 0 (zero) or greater if the interface is an carp interface
+ * and it is configured. Otherwise it returns an negativ number.
+ * This file should be possible to compile standalone with:
+ * 'gcc check_carp.c -o check_carp'
+ * without the if 0 in this source file.
+ */
+
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <netinet/ip_carp.h>
+#include <stdio.h>
+#include <string.h>
+
+int check_carp(const char *name, int namelen) {
+    static struct ifreq ifr;
+
+    if (namelen > sizeof(ifr.ifr_name))
+	return -4;
+
+    memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
+    strncpy(ifr.ifr_name, name, namelen);
+
+    ifr.ifr_addr.sa_family = AF_LOCAL;
+
+    int s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
+    if (s < 0) {
+//	err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
+	return -3;
+    }
+
+    struct carpreq carpr;
+
+    memset((char *)&carpr, 0, sizeof(struct carpreq));
+    ifr.ifr_data = (caddr_t)&carpr;
+
+    if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1) 
+        return -2;
+
+#if 0
+    fprintf(stderr, "carpr.carpr_vhid = %i \n", carpr.carpr_vhid);
+#endif
+
+/*
+    carpr_state will/should return:
+    "INIT" == 0 
+    "BACKUP" == 1
+    "MASTER" == 2
+*/
+    if (carpr.carpr_vhid > 0) 
+	return carpr.carpr_state;
+    else
+	return -1;
+}
+
+#if 0
+int main(int argc, char **argv) {
+    int rc = check_carp("carp0", 5);
+
+    printf("check_carp = %i\n", rc);
+
+    return 0;
+}
+#endif
+
-- 
1.7.3.5


--------------010500000801000408070709
Content-Type: text/x-patch;
 name="0002-Added-support-for-carp-validation-on-BSD.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="0002-Added-support-for-carp-validation-on-BSD.patch"



More information about the Bird-users mailing list