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