Added package section.
[dabba.git] / dabba / interface.c
blob6e084808f42b0e281d37467b33f2c2ca888e2a41
1 /**
2 * \file interface.c
3 * \author written by Emmanuel Roullit emmanuel.roullit@gmail.com (c) 2012
4 * \date 2012
5 */
7 /* __LICENSE_HEADER_BEGIN__ */
9 /*
10 * Copyright (C) 2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
28 /* __LICENSE_HEADER_END__ */
32 =head1 NAME
34 dabba interface - Manage network interface
36 =head1 SYNOPSIS
38 dabba interface <command> [<arguments>...] [--help]
40 =head1 DESCRIPTION
42 Give the user the possibility to manage the available network interfaces.
44 =head1 COMMANDS
46 =over
48 =item list
50 Fetch and print information about currenty supported interfaces.
51 The output is formatted in YAML.
53 =back
55 =head1 EXAMPLES
57 =over
59 =item dabba interface list
61 Output the list of network interface which can be used by dabba.
63 =back
65 =head1 AUTHOR
67 Written by Emmanuel Roullit <emmanuel.roullit@gmail.com>
69 =head1 BUGS
71 =over
73 =item Please report bugs to <https://github.com/eroullit/dabba/issues>
75 =item dabba project project page: <https://github.com/eroullit/dabba>
77 =back
79 =head1 COPYRIGHT
81 =over
83 =item Copyright © 2012 Emmanuel Roullit.
85 =item License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.
87 =item This is free software: you are free to change and redistribute it.
89 =item There is NO WARRANTY, to the extent permitted by law.
91 =back
93 =cut
97 #include <stdio.h>
98 #include <string.h>
99 #include <assert.h>
100 #include <errno.h>
101 #include <libdabba/macros.h>
102 #include <dabba/dabba.h>
103 #include <dabba/ipc.h>
105 static void display_interface_list_header(void)
107 printf("---\n");
108 printf(" interfaces:\n");
111 static void display_interface_list(const struct dabba_ipc_msg const *msg)
113 size_t a;
115 assert(msg);
116 assert(msg->msg_body.elem_nr <= ARRAY_SIZE(msg->msg_body.msg.ifconf));
118 for (a = 0; a < msg->msg_body.elem_nr; a++) {
119 printf(" - %s\n", msg->msg_body.msg.ifconf[a].name);
123 static void prepare_interface_list_query(struct dabba_ipc_msg *msg)
125 assert(msg);
126 msg->mtype = 1;
127 msg->msg_body.type = DABBA_IFCONF;
130 static void display_interface_list_msg_header(const struct dabba_ipc_msg const
131 *msg)
133 assert(msg);
135 switch (msg->msg_body.type) {
136 case DABBA_IFCONF:
137 display_interface_list_header();
138 break;
139 default:
140 break;
144 static void display_interface_list_msg(const struct dabba_ipc_msg const *msg)
146 assert(msg);
148 switch (msg->msg_body.type) {
149 case DABBA_IFCONF:
150 display_interface_list(msg);
151 break;
152 default:
153 break;
158 * \brief Request the current supported interface list
159 * \param[in] argc Argument counter
160 * \param[in] argv Argument vector
161 * \return 0 on success, else on failure.
163 * This function prepares an IPC message to query the supported network
164 * interfaces present on the system. Once the message is sent, it waits for the
165 * dabba daemon to reply.
168 int cmd_interface_list(int argc, const char **argv)
170 int rc;
171 struct dabba_ipc_msg msg;
173 assert(argc >= 0);
174 assert(argv);
176 memset(&msg, 0, sizeof(msg));
177 prepare_interface_list_query(&msg);
178 display_interface_list_msg_header(&msg);
180 do {
181 msg.msg_body.offset += msg.msg_body.elem_nr;
182 msg.msg_body.elem_nr = 0;
184 rc = dabba_ipc_msg(&msg);
186 if (rc)
187 break;
189 display_interface_list_msg(&msg);
190 } while (msg.msg_body.elem_nr);
192 return rc;
196 * \brief Parse which interface sub-command.
197 * \param[in] argc Argument counter
198 * \param[in] argv Argument vector
199 * \return 0 on success, ENOSYS if the sub-command does not exist,
200 * else on failure.
202 * This function parses the interface sub-command string and the rest of the
203 * argument vector to the proper sub-command handler.
206 int cmd_interface(int argc, const char **argv)
208 const char *cmd = argv[0];
209 size_t i;
210 static struct cmd_struct interface_commands[] = {
211 {"list", cmd_interface_list}
214 if (argc == 0 || cmd == NULL || !strcmp(cmd, "--help"))
215 cmd = "help";
217 for (i = 0; i < ARRAY_SIZE(interface_commands); i++) {
218 struct cmd_struct *p = interface_commands + i;
219 if (strcmp(p->cmd, cmd))
220 continue;
221 return (run_builtin(p, argc, argv));
224 return ENOSYS;