]> git.sur5r.net Git - ngadmin/blob - cli/com_netconf.c
Cli: refactor, change coding style
[ngadmin] / cli / com_netconf.c
1
2 #include "commands.h"
3
4
5 bool do_netconf_set (int nb, const char **com, struct ngadmin *nga)
6 {
7         int i, k;
8         const struct swi_attr *sa;
9         struct net_conf nc;
10         bool ret = true;
11         
12         
13         if (nb == 0) {
14                 printf("Usage: netconf set [dhcp yes|no] [ip <ip>] [mask <mask>] [gw <gw>]\n");
15                 return false;
16         }
17         
18         sa = ngadmin_getCurrentSwitch(nga);
19         if (sa == NULL) {
20                 printf("must be logged\n");
21                 return false;
22         }
23         
24         memset(&nc, 0, sizeof(struct net_conf));
25         
26         for (k = 0; k < nb; k += 2) {
27                 if (strcasecmp(com[k], "dhcp") == 0) {
28                         if (strcasecmp(com[k+1], "yes") == 0) {
29                                 nc.dhcp = true;
30                         } else if (strcasecmp(com[k+1], "no") == 0) {
31                                 nc.dhcp = false;
32                         } else {
33                                 printf("Incorrect DHCP value\n");
34                                 ret = false;
35                                 goto end;
36                         }
37                 } else if (strcasecmp(com[k], "ip") == 0) {
38                         if (inet_aton(com[k+1], &nc.ip) == 0) {
39                                 printf("Incorrect IP value\n");
40                                 ret = false;
41                                 goto end;
42                         }
43                 } else if (strcasecmp(com[k], "mask") == 0) {
44                         /* TODO: check if it is a correct mask */
45                         if (inet_aton(com[k+1], &nc.netmask) == 0) {
46                                 printf("Incorrect mask value\n");
47                                 ret = false;
48                                 goto end;
49                         }
50                 } else if (strcasecmp(com[k], "gw") == 0) {
51                         if (inet_aton(com[k+1], &nc.gw) == 0) {
52                                 printf("Incorrect gateway value\n");
53                                 ret = false;
54                                 goto end;
55                         }
56                 }
57         }
58         
59         i = ngadmin_setNetConf(nga, &nc);
60         if (i != ERR_OK) {
61                 printErrCode(i);
62                 ret = false;
63         }
64         
65 end:
66         
67         return ret;
68 }
69
70