]> git.ipfire.org Git - people/ms/network.git/blob - src/networkctl/main.c
0ba72849fcca4f2c826b56764e426d0498add8de
[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 <unistd.h>
25
26 #include <systemd/sd-bus.h>
27
28 #include "command.h"
29 #include "port.h"
30 #include "zone.h"
31
32 static int networkctl_status(sd_bus* bus, int argc, char* argv[]) {
33 printf("%s called\n", __FUNCTION__);
34 return 0;
35 }
36
37 static int networkctl_main(sd_bus* bus, int argc, char* argv[]) {
38 static const struct command commands[] = {
39 { "port", 0, networkctl_port },
40 { "status", 0, networkctl_status },
41 { "zone", 0, networkctl_zone },
42 { NULL },
43 };
44
45 return command_dispatch(bus, commands, argc, argv);
46 }
47
48 static int version(void) {
49 printf("networkctl %s\n", PACKAGE_VERSION);
50
51 return 0;
52 }
53
54 static int help(void) {
55 printf(
56 "%s [OPTIONS...] COMMAND\n\n"
57 "Options:\n"
58 " -h --help Show help\n"
59 " --version Show version\n",
60 program_invocation_short_name
61 );
62
63 return 0;
64 }
65
66 static int parse_argv(int argc, char* argv[]) {
67 enum {
68 ARG_VERSION,
69 };
70
71 static const struct option options[] = {
72 { "help", no_argument, NULL, 'h' },
73 { "version", no_argument, NULL, ARG_VERSION },
74 { NULL },
75 };
76 int c;
77
78 for (;;) {
79 c = getopt_long(argc, argv, "h", options, NULL);
80 if (c < 0)
81 break;
82
83 switch (c) {
84 case 'h':
85 return help();
86
87 case ARG_VERSION:
88 return version();
89
90 case '?':
91 return -EINVAL;
92
93 default:
94 break;
95 }
96 }
97
98 return 0;
99 }
100
101 int main(int argc, char* argv[]) {
102 sd_bus* bus = NULL;
103 int r;
104
105 // Parse command line arguments
106 r = parse_argv(argc, argv);
107 if (r)
108 goto ERROR;
109
110 // Connect to system bus
111 r = sd_bus_open_system(&bus);
112 if (r < 0) {
113 fprintf(stderr, "Could not connect to system bus: %m\n");
114 goto ERROR;
115 }
116
117 // Run a command
118 r = networkctl_main(bus, argc - 1, argv + 1);
119
120 ERROR:
121 if (bus)
122 sd_bus_flush_close_unref(bus);
123
124 return r;
125 }