Home | History | Annotate | Line # | Download | only in ldpd
main.c revision 1.1
      1 /* $NetBSD: main.c,v 1.1 2010/12/08 07:20:14 kefren Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Mihai Chelaru <kefren (at) NetBSD.org>
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <netinet/in.h>
     33 #include <sys/stat.h>
     34 #include <sys/socket.h>
     35 #include <sys/select.h>
     36 #include <arpa/inet.h>
     37 
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 #include <strings.h>
     41 #include <unistd.h>
     42 
     43 #include "ldp.h"
     44 #include "ldp_command.h"
     45 #include "socketops.h"
     46 #include "tlv.h"
     47 #include "pdu.h"
     48 #include "fsm.h"
     49 #include "ldp_errors.h"
     50 #include "mpls_interface.h"
     51 
     52 extern int      ls;		/* TCP listening socket */
     53 extern int	dont_catch;
     54 extern int	command_port;
     55 extern int	command_socket;
     56 
     57 extern int	debug_f, warn_f, syslog_f;
     58 
     59 extern	struct sockaddr	mplssockaddr;
     60 
     61 void print_usage(char *myself)
     62 {
     63 	printf("\nUsage: %s [-hdDW] [-p PORT]\n\n", myself);
     64 }
     65 
     66 int
     67 main(int argc, char *argv[])
     68 {
     69 	int ch, forkres, dontfork = 0;
     70 
     71 	while((ch = getopt(argc, argv, "dDfhp:W")) != -1)
     72 		switch(ch) {
     73 		case 'd':
     74 			dont_catch = 1;
     75 			break;
     76 		case 'D':
     77 			debug_f = 1;
     78 			break;
     79 		case 'f':
     80 			dontfork = 1;
     81 			break;
     82 		case 'p':
     83 			if ((command_port = atoi(optarg)) < 1) {
     84 				print_usage(argv[0]);
     85 				return -1;
     86 			}
     87 			break;
     88 		case 'W':
     89 			warn_f = 1;
     90 			break;
     91 		case 'h':
     92 		default:
     93 			print_usage(argv[0]);
     94 			return -1;
     95 			break;
     96 		}
     97 	if (geteuid()) {
     98 		fatalp("You have to run this as ROOT\n");
     99 		return -1;
    100 	}
    101 	if (set_my_ldp_id()) {
    102 		fatalp("Cannot set LDP ID\n");
    103 		return -1;
    104 	}
    105 	if (mplssockaddr.sa_len == 0) {
    106 		fatalp("You need one mpls interface up and an IP "
    107 		    "address set for it\n");
    108 		return -1;
    109 	}
    110 	if (mpls_start_ldp() == -1)
    111 		return -1;
    112 	if (!strcmp(LDP_ID, "0.0.0.0")) {
    113 		fatalp("Cannot set my LDP ID.\nAre you sure you've "
    114 		    "got a non-loopback INET interface UP ?\n");
    115 		return -1;
    116 	}
    117 	init_command_sockets();
    118 	if ((command_socket = create_command_socket(command_port)) < 1) {
    119 		fatalp("Cannot create command socket\n");
    120 		return -1;
    121 	}
    122 	if (create_hello_socket() < 1) {
    123 		fatalp("Cannot create hello socket\n");
    124 		return -1;
    125 	}
    126 
    127 	ls = create_listening_socket();
    128 
    129 	if (ls < 0) {
    130 		fatalp("Cannot create listening socket\n");
    131 		return -1;
    132 	}
    133 
    134 	if (dontfork == 1)
    135 		the_big_loop();
    136 
    137 	forkres = fork();
    138 	if (forkres == 0) {
    139 		syslog_f = 1;
    140 		the_big_loop();
    141 	}
    142 	if (forkres < 0)
    143 		perror("fork");
    144 
    145 	return 0;
    146 }
    147