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