]> git.sur5r.net Git - ngadmin/blob - cli/src/com_netconf.c
Cli: add command to show network configuration details
[ngadmin] / cli / src / com_netconf.c
1
2 #include "commands.h"
3
4
5 int do_netconf_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
6 {
7         const struct swi_attr *sa;
8         const struct net_conf *nc;
9         
10         
11         if (argc > 0) {
12                 printf("this command takes no argument\n");
13                 return 1;
14         }
15         
16         sa = ngadmin_getCurrentSwitch(nga);
17         if (sa == NULL) {
18                 printf("must be logged\n");
19                 return 1;
20         }
21         nc = &sa->nc;
22         
23         printf("DHCP\t : %s\n", nc->dhcp ? "yes" : "no");
24         printf("IP\t : %s\n", inet_ntoa(nc->ip));
25         printf("Netmask\t : %s\n", inet_ntoa(nc->netmask));
26         printf("Gateway\t : %s\n", inet_ntoa(nc->gw));
27         
28         
29         return 0;
30 }
31
32
33 int do_netconf_set (int argc, const char **argv, struct ngadmin *nga)
34 {
35         int i, k, ret = 0;
36         const struct swi_attr *sa;
37         struct net_conf nc;
38         
39         
40         if (argc == 0) {
41                 printf("usage: netconf set [dhcp yes|no] [ip <ip>] [mask <mask>] [gw <gw>]\n");
42                 return 1;
43         }
44         
45         sa = ngadmin_getCurrentSwitch(nga);
46         if (sa == NULL) {
47                 printf("must be logged\n");
48                 return 1;
49         }
50         
51         memset(&nc, 0, sizeof(struct net_conf));
52         
53         for (k = 0; k < argc; k += 2) {
54                 if (strcasecmp(argv[k], "dhcp") == 0) {
55                         if (strcasecmp(argv[k + 1], "yes") == 0) {
56                                 nc.dhcp = true;
57                         } else if (strcasecmp(argv[k + 1], "no") == 0) {
58                                 nc.dhcp = 1;
59                         } else {
60                                 printf("Incorrect DHCP value\n");
61                                 ret = 1;
62                                 goto end;
63                         }
64                 } else if (strcasecmp(argv[k], "ip") == 0) {
65                         if (inet_aton(argv[k + 1], &nc.ip) == 0) {
66                                 printf("Incorrect IP value\n");
67                                 ret = 1;
68                                 goto end;
69                         }
70                 } else if (strcasecmp(argv[k], "mask") == 0) {
71                         /* TODO: check if it is a correct mask */
72                         if (inet_aton(argv[k + 1], &nc.netmask) == 0) {
73                                 printf("Incorrect mask value\n");
74                                 ret = 1;
75                                 goto end;
76                         }
77                 } else if (strcasecmp(argv[k], "gw") == 0) {
78                         if (inet_aton(argv[k + 1], &nc.gw) == 0) {
79                                 printf("Incorrect gateway value\n");
80                                 ret = 1;
81                                 goto end;
82                         }
83                 }
84         }
85         
86         i = ngadmin_setNetConf(nga, &nc);
87         if (i != ERR_OK) {
88                 printErrCode(i);
89                 ret = 1;
90         }
91         
92 end:
93         
94         return ret;
95 }
96
97