Debian Codegraph @

ax25ipd/ax25ipd.c

raw
1 /* ax25ipd.c main entrypoint
2 *
3 * Copyright 1991, Michael Westerhof, Sun Microsystems, Inc.
4 * This software may be freely used, distributed, or modified, providing
5 * this header is not removed.
6 *
7 */
8
9 /*
10 * cleaned up and prototyped for inclusion into the standard linux ax25
11 * toolset in january 1997 by rob mayfield, vk5xxx/vk5zeu
12 */
13
14 #include <limits.h>
15 #include <setjmp.h>
16 #include <signal.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <syslog.h>
21 #include <unistd.h>
22
23 #include <netax25/daemon.h>
24 #include <config.h>
25 #include <getopt.h>
26
27 #include "../pathnames.h"
28 #include "ax25ipd.h"
29
30 int udp_mode; /* true if we need a UDP socket */
31 int ip_mode; /* true if we need the raw IP socket */
32 unsigned short my_udp; /* the UDP port to use (network byte order) */
33 char ttydevice[PATH_MAX]; /* the tty device for serial comms */
34 int ttyspeed; /* The baud rate on the tty device */
35 unsigned char mycallsign[7]; /* My callsign, shifted ASCII with SSID */
36 unsigned char mycallsign2[7]; /* My seconds port callsign, shifted ASCII with SSID */
37 unsigned char myalias[7]; /* An alias to use */
38 unsigned char myalias2[7]; /* An alias for second port */
39 char bc_text[128]; /* The text for beacon messages */
40 int bc_interval; /* The interval, in seconds, between beacons */
41 int bc_every; /* true=every, false=after */
42 int digi; /* True if we are connected to a TNC */
43 int loglevel; /* Verbosity level */
44 struct ax25ipd_stats stats; /* Usage statistics */
45
46 int dual_port; /* addition for dual port flag */
47
48 static jmp_buf restart_env;
49
50 static int opt_version;
51 static int opt_loglevel;
52 static int opt_nofork;
53 static int opt_help;
54 static char opt_configfile[PATH_MAX];
55 static char opt_ttydevice[PATH_MAX];
56
57 static struct option options[] = {
58 {"version", 0, NULL, 'v'},
59 {"loglevel", 1, NULL, 'l'},
60 {"help", 0, NULL, 'h'},
61 {"configfile", 1, NULL, 'c'},
62 {"ttydevice", 1, NULL, 'd'},
63 {"nofork", 0, NULL, 'f'},
64 {NULL, 0, NULL, 0}
65 };
66
67 static void greet_world(void)
68 {
69 printf("\nax25ipd %s\n", VERSION);
70 printf
71 ("Copyright 1991, Michael Westerhof, Sun Microsystems, Inc.\n");
72 printf
73 ("This software may be freely used, distributed, or modified, providing\nthis header is not removed\n\n");
74 fflush(stdout);
75 }
76
77 static void do_stats(void)
78 {
79 int save_loglevel;
80
81 /* save the old loglevel, and force at least loglevel 1 */
82 save_loglevel = loglevel;
83 loglevel = 1;
84
85 printf("\nSIGUSR1 signal: statistics and configuration report\n");
86
87 greet_world();
88
89 dump_config();
90 dump_routes();
91 dump_params();
92
93 printf("\nInput stats:\n");
94 printf("KISS input packets: %d\n", stats.kiss_in);
95 printf(" too big: %d\n", stats.kiss_toobig);
96 printf(" bad type: %d\n", stats.kiss_badtype);
97 printf(" too short: %d\n", stats.kiss_tooshort);
98 printf(" not for me: %d\n", stats.kiss_not_for_me);
99 printf(" I am destination: %d\n", stats.kiss_i_am_dest);
100 printf(" no route found: %d\n", stats.kiss_no_ip_addr);
101 printf("UDP input packets: %d\n", stats.udp_in);
102 printf("IP input packets: %d\n", stats.ip_in);
103 printf(" failed CRC test: %d\n", stats.ip_failed_crc);
104 printf(" too short: %d\n", stats.ip_tooshort);
105 printf(" not for me: %d\n", stats.ip_not_for_me);
106 printf(" I am destination: %d\n", stats.ip_i_am_dest);
107 printf("\nOutput stats:\n");
108 printf("KISS output packets: %d\n", stats.kiss_out);
109 printf(" beacons: %d\n", stats.kiss_beacon_outs);
110 printf("UDP output packets: %d\n", stats.udp_out);
111 printf("IP output packets: %d\n", stats.ip_out);
112 printf("\n");
113
114 fflush(stdout);
115
116 /* restore the old loglevel */
117 loglevel = save_loglevel;
118 }
119
120 static void hupper(int i)
121 {
122 printf("\nSIGHUP!\n");
123 longjmp(restart_env, 1);
124 }
125
126 static void usr1_handler(int i)
127 {
128 printf("\nSIGUSR1!\n");
129 do_stats();
130 }
131
132 static void int_handler(int i)
133 {
134 printf("\nSIGINT!\n");
135 do_stats();
136 exit(1);
137 }
138
139 static void term_handler(int i)
140 {
141 printf("\nSIGTERM!\n");
142 do_stats();
143 exit(1);
144 }
145
146 int main(int argc, char **argv)
147 {
148 if (setjmp(restart_env) == 0) {
149 signal(SIGHUP, hupper);
150 }
151
152 *opt_configfile = 0;
153 *opt_ttydevice = 0;
154
155 /* set up the handler for statistics reporting */
156 signal(SIGUSR1, usr1_handler);
157 signal(SIGINT, int_handler);
158 signal(SIGTERM, term_handler);
159
160 while (1) {
161 int c;
162
163 c = getopt_long(argc, argv, "c:d:fhl:v", options, NULL);
164 if (c == -1)
165 break;
166
167 switch (c) {
168 case 'c':
169 strncpy(opt_configfile, optarg, sizeof(opt_configfile)-1);
170 opt_configfile[sizeof(opt_configfile)-1] = 0;
171 break;
172 case 'd':
173 strncpy(opt_ttydevice, optarg, sizeof(opt_ttydevice)-1);
174 opt_ttydevice[sizeof(opt_ttydevice)-1] = 0;
175 break;
176 case 'f':
177 opt_nofork = 1;
178 break;
179 case 'h':
180 opt_help = 1;
181 break;
182 case 'v':
183 opt_version = 1;
184 break;
185 case 'l':
186 opt_loglevel = atoi(optarg);
187 break;
188 default:
189 opt_help = 1;
190 break;
191 }
192 }
193
194 if (optind < argc) {
195 printf("Unknown argument '%s' ...\n\n", argv[optind++]);
196 opt_help = 1;
197 }
198
199 if (opt_version == 1) {
200 greet_world();
201 exit(0);
202 }
203 if (opt_help == 1) {
204 greet_world();
205 printf("Usage:\n");
206 printf("%s [flags]\n", argv[0]);
207 printf("\nFlags:\n");
208 printf
209 (" --version, -v Print version of program\n");
210 printf(" --help, -h This help screen\n");
211 printf
212 (" --loglevel NUM, -l NUM Set logging level to NUM\n");
213 printf
214 (" --configfile FILE, -c FILE Set configuration file to FILE\n");
215 printf
216 (" --ttydevice TTYDEV, -d TTYDEV Set device parameter to TTYDEV\n");
217 printf
218 (" --nofork, -f Do not put daemon in background\n");
219 exit(0);
220 }
221
222 /* Initialize all routines */
223 config_init();
224 kiss_init();
225 route_init();
226 process_init();
227 io_init();
228
229 /* read config file */
230 config_read(opt_configfile);
231
232 if (opt_ttydevice[0] != '\0') {
233 strncpy(ttydevice, opt_ttydevice, sizeof(ttydevice)-1);
234 ttydevice[sizeof(ttydevice)-1] = '\0';
235 }
236
237 /* print the current config and route info */
238 dump_config();
239 dump_routes();
240 dump_params();
241
242 /* Open the IO stuff */
243 io_open();
244
245 /* if we get this far without error, let's fork off ! :-) */
246 if (opt_nofork == 0) {
247 if (!daemon_start(TRUE)) {
248 syslog(LOG_DAEMON | LOG_CRIT, "ax25ipd: cannot become a daemon\n");
249 return 1;
250 }
251 }
252
253 /* we need to close stdin, stdout, stderr: because otherwise
254 * scripting like ttyname=$(ax25ipd | tail -1) does not work
255 */
256 if (!isatty(1)) {
257 fflush(stdout);
258 fflush(stderr);
259 close(0);
260 close(1);
261 close(2);
262 }
263
264 /* and let the games begin */
265 io_start();
266
267 return 0;
268 }