summaryrefslogtreecommitdiff
path: root/usr.sbin/ldpd/main.c
blob: 41e2fcb2cd6f3c21bf4f4264791d7bbc8b972846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* $NetBSD: main.c,v 1.9 2013/07/31 06:58:23 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>
 *
 * 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 <netinet/in.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>

#include "ldp.h"
#include "ldp_command.h"
#include "socketops.h"
#include "tlv.h"
#include "pdu.h"
#include "fsm.h"
#include "ldp_errors.h"
#include "mpls_interface.h"
#include "conffile.h"

extern int ls;		/* TCP listening socket */
extern int dont_catch;
extern int command_port;
extern int command_socket;

extern int debug_f, warn_f, syslog_f;

extern struct sockaddr mplssockaddr;
extern struct in_addr conf_ldp_id;

void print_usage(char *myself)
{
	printf("\nUsage: %s [-DdfhW] [-c config_file] [-p port]\n\n", myself);
}

int 
main(int argc, char *argv[])
{
	int ch, forkres, dontfork = 0, cpf;
	char conffile[PATH_MAX + 1];

	strlcpy(conffile, CONFFILE, sizeof(conffile));
	while((ch = getopt(argc, argv, "c:dDfhp:W")) != -1)
		switch(ch) {
		case 'c':
			strlcpy(conffile, optarg, sizeof(conffile));
			break;
		case 'D':
			debug_f = 1;
			break;
		case 'd':
			dont_catch = 1;
			break;
		case 'f':
			dontfork = 1;
			break;
		case 'p':
			if ((command_port = atoi(optarg)) < 1) {
				print_usage(argv[0]);
				return EXIT_FAILURE;
			}
			break;
		case 'W':
			warn_f = 1;
			break;
		case 'h':
		default:
			print_usage(argv[0]);
			return EXIT_FAILURE;
			break;
		}

	cpf = conf_parsefile(conffile);
	if (cpf < 0 && strcmp(conffile, CONFFILE)) {
		fatalp("Cannot parse config file: %s\n", conffile);
		return EXIT_FAILURE;
	} else if (cpf > 0) {
		fatalp("Cannot parse line %d in config file\n", cpf);
		return EXIT_FAILURE;
	}

	if (set_my_ldp_id()) {
		fatalp("Cannot set LDP ID\n");
		return EXIT_FAILURE;
	}
	if (conf_ldp_id.s_addr != 0)
		strlcpy(my_ldp_id, inet_ntoa(conf_ldp_id), INET_ADDRSTRLEN);

	if (mplssockaddr.sa_len == 0) {
		fatalp("FATAL: Create an mpls interface using ifconfig\n"
		    "e.g. ifconfig mpls0 create up\n");
		return EXIT_FAILURE;
	}
	if (mpls_start_ldp() == -1)
		return EXIT_FAILURE;
	if (!strcmp(LDP_ID, "0.0.0.0")) {
		fatalp("Cannot set my LDP ID.\nAre you sure you've "
		    "got a non-loopback INET interface UP ?\n");
		return EXIT_FAILURE;
	}
	init_command_sockets();
	if ((command_socket = create_command_socket(command_port)) < 1) {
		fatalp("Cannot create command socket\n");
		return EXIT_FAILURE;
	}
	if (create_hello_sockets() != 0) {
		fatalp("Cannot create hello socket\n");
		return EXIT_FAILURE;
	}

	ls = create_listening_socket();

	if (ls < 0) {
		fatalp("Cannot create listening socket\n");
		return EXIT_FAILURE;
	}

	if (dontfork == 1)
		return the_big_loop();

	forkres = fork();
	if (forkres == 0) {
		syslog_f = 1;
		return the_big_loop();
	}
	if (forkres < 0)
		perror("fork");

	return EXIT_SUCCESS;
}