]> git.sur5r.net Git - ngadmin/blob - cli/com_mirror.c
Let commands handle themselves absence of arguments
[ngadmin] / cli / com_mirror.c
1
2 #include "commands.h"
3
4
5 bool do_mirror_disable (int argc, const char **argv UNUSED, struct ngadmin *nga)
6 {
7         int i;
8         
9         
10         if (argc > 0) {
11                 printf("this command takes no argument\n");
12                 return false;
13         }
14         
15         if (ngadmin_getCurrentSwitch(nga) == NULL) {
16                 printf("must be logged\n");
17                 return false;
18         }
19         
20         i = ngadmin_setMirror(nga, NULL);
21         printErrCode(i);
22         
23         
24         return true;
25 }
26
27
28 bool do_mirror_set (int argc, const char **argv, struct ngadmin *nga)
29 {
30         const struct swi_attr *sa;
31         char *ports = NULL;
32         bool ret = true;
33         int i, k = 0;
34         
35         
36         if (argc < 3) {
37                 printf("usage: mirror set <destination port> clone <port1> [<port2> ...]\n");
38                 goto end;
39         }
40         
41         sa = ngadmin_getCurrentSwitch(nga);
42         if (sa == NULL) {
43                 printf("must be logged\n");
44                 ret = false;
45                 goto end;
46         }
47         
48         ports = malloc((sa->ports + 1) * sizeof(char));
49         memset(ports, 0, sa->ports + 1);
50         
51         ports[0] = strtol(argv[k++], NULL, 0);
52         if (ports[0] < 1 || ports[0] > sa->ports || strcasecmp(argv[k++], "clone") != 0) {
53                 printf("syntax error\n");
54                 ret = false;
55                 goto end;
56         }
57         
58         while (k < argc) {
59                 i = strtol(argv[k++], NULL, 0);
60                 if (i < 1 || i > sa->ports) {
61                         printf("port out of range\n");
62                         ret = false;
63                         goto end;
64                 } else if (i == ports[0]) {
65                         printf("destination port cannot be in port list\n");
66                         ret = false;
67                         goto end;
68                 }
69                 ports[i] = 1;
70         }
71         
72         i = ngadmin_setMirror(nga, ports);
73         printErrCode(i);
74         
75 end:
76         free(ports);
77         
78         return ret;
79 }
80
81
82 bool do_mirror_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
83 {
84         const struct swi_attr *sa;
85         char *ports = NULL;
86         int i;
87         
88         
89         if (argc > 0) {
90                 printf("this command takes no argument\n");
91                 return false;
92         }
93         
94         sa = ngadmin_getCurrentSwitch(nga);
95         if (sa == NULL) {
96                 printf("must be logged\n");
97                 return false;
98         }
99         
100         ports = malloc((sa->ports + 1) * sizeof(char));
101         i = ngadmin_getMirror(nga, ports);
102         if (i != ERR_OK) {
103                 printErrCode(i);
104                 goto end;
105         }
106         
107         if (ports[0] == 0) {
108                 printf("port mirroring is disabled\n");
109                 goto end;
110         }
111         
112         printf("destination: %i\n", ports[0]);
113         printf("ports: ");
114         for (i = 1; i <= sa->ports; i++) {
115                 if (ports[i])
116                         printf("%i ", i);
117         }
118         printf("\n");
119         
120         
121 end:
122         free(ports);
123         
124         return true;
125 }
126
127