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