Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/ldpd * add config file so one can control id, timer...



details:   https://anonhg.NetBSD.org/src/rev/273f1b0e134f
branches:  trunk
changeset: 760178:273f1b0e134f
user:      kefren <kefren%NetBSD.org@localhost>
date:      Thu Dec 30 11:29:21 2010 +0000

description:
* add config file so one can control id, timers and label assignment and
  use neighbour specific options - XXX: needs documentation
* add peer authentication using TCP_MD5SIG. Interoperability tested with
  Cisco IOS
* use SLIST_FOREACH_SAFE when deleting labels instead of re-looping.

diffstat:

 usr.sbin/ldpd/Makefile      |    5 +-
 usr.sbin/ldpd/TODO          |    3 +-
 usr.sbin/ldpd/conffile.c    |  299 ++++++++++++++++++++++++++++++++++++++++++++
 usr.sbin/ldpd/conffile.h    |   56 ++++++++
 usr.sbin/ldpd/label.c       |   35 ++--
 usr.sbin/ldpd/ldp.h         |    4 +-
 usr.sbin/ldpd/ldp_command.c |   17 +-
 usr.sbin/ldpd/ldp_peer.c    |   27 +++-
 usr.sbin/ldpd/ldpd.8        |    5 +-
 usr.sbin/ldpd/main.c        |   38 ++++-
 usr.sbin/ldpd/socketops.c   |   21 +-
 11 files changed, 451 insertions(+), 59 deletions(-)

diffs (truncated from 807 to 300 lines):

diff -r c7da6c99e780 -r 273f1b0e134f usr.sbin/ldpd/Makefile
--- a/usr.sbin/ldpd/Makefile    Thu Dec 30 04:01:59 2010 +0000
+++ b/usr.sbin/ldpd/Makefile    Thu Dec 30 11:29:21 2010 +0000
@@ -1,11 +1,12 @@
-# $NetBSD: Makefile,v 1.2 2010/12/18 04:25:37 joerg Exp $
+# $NetBSD: Makefile,v 1.3 2010/12/30 11:29:21 kefren Exp $
 
 .include <bsd.own.mk>
 
 PROG=   ldpd
 MAN=    ldpd.8
 
-SRCS=   fsm.c \
+SRCS=   conffile.c \
+       fsm.c \
        label.c \
        ldp_command.c \
        ldp_errors.c \
diff -r c7da6c99e780 -r 273f1b0e134f usr.sbin/ldpd/TODO
--- a/usr.sbin/ldpd/TODO        Thu Dec 30 04:01:59 2010 +0000
+++ b/usr.sbin/ldpd/TODO        Thu Dec 30 11:29:21 2010 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: TODO,v 1.1 2010/12/08 07:20:14 kefren Exp $
+# $NetBSD: TODO,v 1.2 2010/12/30 11:29:21 kefren Exp $
 
 TODO
 ====
@@ -7,6 +7,5 @@
 * document better Label Distribution (downstream on demand or
   unsolicited downstream), distribution control (independent or
   ordered) and retention mode (liberal or conservative) - kefren
-* config/options file
 * future: IPv6 support. Have no infrastructure to test right
   now - kefren
diff -r c7da6c99e780 -r 273f1b0e134f usr.sbin/ldpd/conffile.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/ldpd/conffile.c  Thu Dec 30 11:29:21 2010 +0000
@@ -0,0 +1,299 @@
+/* $NetBSD: conffile.c,v 1.1 2010/12/30 11:29:21 kefren Exp $ */
+
+/*
+ * Copyright (c) 2010 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Mihai Chelaru <kefren%NetBSD.org@localhost>
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
+#include <ctype.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "conffile.h"
+#include "ldp_errors.h"
+
+#define NextCommand(x) strsep(&x, " ")
+#define LINEMAXSIZE 1024
+
+extern int ldp_hello_time, ldp_keepalive_time, ldp_holddown_time, command_port,
+       min_label, max_label;
+int confh;
+struct in_addr conf_ldp_id;
+
+static int conf_dispatch(char*);
+static int conf_readline(char*, int);
+static int checkeol(char*);
+static int Fhellotime(char*);
+static int Fport(char*);
+static int Fholddown(char*);
+static int Fkeepalive(char*);
+static int Fmaxlabel(char*);
+static int Fminlabel(char*);
+static int Fldpid(char*);
+static int Fneighbour(char*);
+static int Gneighbour(struct conf_neighbour *, char *);
+
+struct conf_func {
+       char com[64];
+       int (* func)(char *);
+};
+
+struct conf_func main_commands[] = {
+       { "hello-time", Fhellotime },
+       { "keepalive-time", Fkeepalive },
+       { "holddown-time", Fholddown },
+       { "command-port", Fport },
+       { "min-label", Fminlabel },
+       { "max-label", Fmaxlabel },
+       { "LDP-ID", Fldpid },
+       { "neighbor", Fneighbour },
+       { "neighbour", Fneighbour },
+       { "", NULL },
+};
+
+/*
+ * Parses config file
+ */
+int
+conf_parsefile(char *fname)
+{
+       int i;
+       char buf[LINEMAXSIZE + 1];
+
+       SLIST_INIT(&conei_head);
+       conf_ldp_id.s_addr = 0;
+
+       confh = open(fname, O_RDONLY, 0);
+
+       if (confh == -1)
+               return E_CONF_IO;
+
+       for (i = 1; conf_readline(buf, sizeof(buf)) >= 0; i++)
+               if (conf_dispatch(buf) != 0) {
+                       close(confh);
+                       return i;
+               }
+
+       close(confh);
+       return 0;
+}
+
+/*
+ * Reads a line from config file
+ */
+int
+conf_readline(char *buf, int bufsize)
+{
+       int i;
+
+       for (i = 0; i < bufsize; i++) {
+               if (read(confh, &buf[i], 1) != 1) {
+                       if (i == 0)
+                               return E_CONF_IO;
+                       break;
+               }
+               if (buf[i] == '\n')
+                       break;
+               if (i == 0 && isspace((int)buf[i]) != 0) {
+                       i--;
+                       continue;
+               }
+       }
+       if (i == bufsize)
+               return E_CONF_MEM;
+       buf[i] = '\0';
+       return i;
+}
+
+/*
+ * Looks for a matching command on a line
+ */
+int
+conf_dispatch(char *line)
+{
+       int i, last_match = -1, matched = 0;
+       char *command, *nline = line;
+
+       if (strlen(line) == 0 || line[0] == '#')
+               return E_CONF_OK;
+       command = NextCommand(nline);
+       for (i = 0; main_commands[i].func != NULL; i++)
+               if (strncasecmp(main_commands[i].com, command,
+                   strlen(command)) == 0) {
+                       matched++;
+                       last_match = i;
+               }
+       if (matched == 0)
+               return E_CONF_NOMATCH;
+       else if (matched > 1)
+               return E_CONF_AMBIGUOUS;
+
+       if (checkeol(nline) != 0)
+               return E_CONF_PARAM;
+       return main_commands[last_match].func(nline);
+}
+
+/*
+ * Checks if a line is terminated or else if it contains
+ * a start block bracket. If it's semicolon terminated
+ * then trim it.
+ */
+int
+checkeol(char *line)
+{
+       if (line[strlen(line) - 1] == ';') {
+               line[strlen(line) - 1] = '\0';
+               return 0;
+       }
+       for (uint i = 0; i < strlen(line); i++)
+               if (line[i] == '{')
+                       return 0;
+       return -1;
+}
+
+/*
+ * Sets hello time
+ */
+int
+Fhellotime(char *line)
+{
+       int ht = atoi(line);
+       if (ht <= 0)
+               return E_CONF_PARAM;
+       ldp_hello_time = ht;
+       return 0;
+}
+
+/*
+ * Sets command port
+ */
+int
+Fport(char *line)
+{
+       int cp = atoi(line);
+       if (cp <= 0 || cp > 65535)
+               return E_CONF_PARAM;
+       command_port = cp;
+       return 0;
+}
+
+/*
+ * Sets neighbour keepalive
+ */
+int
+Fkeepalive(char *line)
+{
+       int kt = atoi(line);
+       if (kt <= 0)
+               return E_CONF_PARAM;
+       ldp_keepalive_time = kt;
+       return 0;
+}
+
+/*
+ * Sets neighbour holddown timer
+ */
+int
+Fholddown(char *line)
+{
+       int hdt = atoi(line);
+       if (hdt <= 0)
+               return E_CONF_PARAM;
+       ldp_holddown_time = hdt;
+       return 0;
+}
+
+int
+Fminlabel(char *line)
+{
+       int ml = atoi(line);
+       if (ml <= 0)
+               return E_CONF_PARAM;
+       min_label = ml;
+       return 0;
+}
+
+int
+Fmaxlabel(char *line)
+{
+       int ml = atoi(line);
+       if (ml <= 0)
+               return E_CONF_PARAM;
+       max_label = ml;
+       return 0;
+}
+
+int
+Fldpid(char *line)
+{
+       if (inet_pton(AF_INET, line, &conf_ldp_id) != 1)
+               return E_CONF_PARAM;
+       return 0;
+}
+



Home | Main Index | Thread Index | Old Index