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