]> git.sur5r.net Git - ngadmin/blob - lib/src/mirror.c
Merge remote-tracking branch 'upstream/master'
[ngadmin] / lib / src / mirror.c
1
2 #include <ngadmin.h>
3
4 #include <nsdp/attr.h>
5 #include <nsdp/protocol.h>
6
7 #include "lib.h"
8 #include "network.h"
9
10
11 int ngadmin_getMirror (struct ngadmin *nga, char *ports)
12 {
13         List *attr;
14         struct attr *at;
15         int ret = ERR_OK;
16         struct swi_attr *sa;
17         struct attr_mirror *am;
18         
19         
20         if (nga == NULL || ports == NULL)
21                 return ERR_INVARG;
22         
23         sa = nga->current;
24         if (sa == NULL)
25                 return ERR_NOTLOG;
26         
27         
28         attr = createEmptyList();
29         pushBackList(attr, newEmptyAttr(ATTR_MIRROR));
30         ret = readRequest(nga, attr);
31         if (ret < 0)
32                 goto end;
33         
34         filterAttributes(attr, ATTR_MIRROR, ATTR_END);
35         
36         memset(ports, 0, 1 + sa->ports);
37         
38         if (attr->first != NULL) {
39                 at = attr->first->data;
40                 am = at->data;
41                 
42                 if (at->size == 0) {
43                         ret = ERR_BADREPLY;
44                         goto end;
45                 }
46                 if (am->outport == 0) {
47                         memset(ports, 0, 1 + sa->ports);
48                 } else if (am->outport > 0 && at->size >= 1 + sa->ports) {
49                         if (at->size < sizeof(struct attr_mirror) + sa->ports) {
50                                 ret = ERR_BADREPLY;
51                                 goto end;
52                         }
53                         ports[0] = am->outport;
54                         memcpy(ports + 1, am->ports, sa->ports);
55                 }
56         }
57         
58         
59 end:
60         destroyList(attr, (void(*)(void*))freeAttr);
61         
62         
63         return ret;
64 }
65
66
67 int ngadmin_setMirror (struct ngadmin *nga, const char *ports)
68 {
69         List *attr;
70         struct swi_attr *sa;
71         struct attr_mirror *am;
72         
73         
74         if (nga == NULL)
75                 return ERR_INVARG;
76         
77         sa = nga->current;
78         if (sa == NULL)
79                 return ERR_NOTLOG;
80         
81         
82         am = malloc(sizeof(struct attr_mirror) + sa->ports);
83         if (am == NULL)
84                 return ERR_MEM;
85         
86         if (ports == NULL) {
87                 am->outport = 0;
88         } else {
89                 am->outport = ports[0];
90                 memcpy(am->ports, ports + 1, sa->ports);
91         }
92         
93         attr = createEmptyList();
94         pushBackList(attr, newAttr(ATTR_MIRROR, sizeof(struct attr_mirror) + sa->ports, am));
95         
96         
97         return writeRequest(nga, attr);
98 }
99