]> git.ipfire.org Git - people/ms/network.git/blob - src/networkctl/main.c
Makefile: Fix typo in localstatedir
[people/ms/network.git] / src / networkctl / main.c
1 /*#############################################################################
2 # #
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2023 IPFire Network Development Team #
5 # #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
18 # #
19 #############################################################################*/
20
21 #include <errno.h>
22 #include <getopt.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #include <systemd/sd-bus.h>
28
29 #include "command.h"
30 #include "port.h"
31 #include "zone.h"
32
33 static int networkctl_status(sd_bus* bus, int argc, char* argv[]) {
34 printf("%s called\n", __FUNCTION__);
35 return 0;
36 }
37
38 static int networkctl_main(sd_bus* bus, int argc, char* argv[]) {
39 static const struct command commands[] = {
40 { "port", 0, networkctl_port },
41 { "status", 0, networkctl_status },
42 { "zone", 0, networkctl_zone },
43 { NULL },
44 };
45
46 return command_dispatch(bus, commands, argc, argv);
47 }
48
49 static void version(void) {
50 printf("networkctl %s\n", PACKAGE_VERSION);
51
52 exit(0);
53 }
54
55 static void help(void) {
56 printf(
57 "%s [OPTIONS...] COMMAND\n\n"
58 "Options:\n"
59 " -h --help Show help\n"
60 " --version Show version\n",
61 program_invocation_short_name
62 );
63
64 exit(0);
65 }
66
67 static int parse_argv(int argc, char* argv[]) {
68 enum {
69 ARG_VERSION,
70 };
71
72 static const struct option options[] = {
73 { "help", no_argument, NULL, 'h' },
74 { "version", no_argument, NULL, ARG_VERSION },
75 { NULL },
76 };
77 int c;
78
79 for (;;) {
80 c = getopt_long(argc, argv, "h", options, NULL);
81 if (c < 0)
82 break;
83
84 switch (c) {
85 case 'h':
86 help();
87
88 case ARG_VERSION:
89 version();
90
91 case '?':
92 return -EINVAL;
93
94 default:
95 break;
96 }
97 }
98
99 return 0;
100 }
101
102 int main(int argc, char* argv[]) {
103 sd_bus* bus = NULL;
104 int r;
105
106 // Parse command line arguments
107 r = parse_argv(argc, argv);
108 if (r)
109 goto ERROR;
110
111 // Connect to system bus
112 r = sd_bus_open_system(&bus);
113 if (r < 0) {
114 fprintf(stderr, "Could not connect to system bus: %m\n");
115 goto ERROR;
116 }
117
118 // Run a command
119 r = networkctl_main(bus, argc - 1, argv + 1);
120
121 ERROR:
122 if (bus)
123 sd_bus_flush_close_unref(bus);
124
125 return r;
126 }