]> git.sur5r.net Git - ngadmin/blob - lib/src/mirror.c
Add error codes to handle bad replies and unknown errors
[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 (am->outport == 0) {
43                         memset(ports, 0, 1 + sa->ports);
44                 } else if (am->outport > 0 && at->size >= 1 + sa->ports) {
45                         if (at->size < sizeof(struct attr_mirror) + sa->ports) {
46                                 ret = ERR_BADREPLY;
47                                 goto end;
48                         }
49                         ports[0] = am->outport;
50                         memcpy(ports + 1, am->ports, sa->ports);
51                 }
52         }
53         
54         
55 end:
56         destroyList(attr, (void(*)(void*))freeAttr);
57         
58         
59         return ret;
60 }
61
62
63 int ngadmin_setMirror (struct ngadmin *nga, const char *ports)
64 {
65         List *attr;
66         struct swi_attr *sa;
67         struct attr_mirror *am;
68         
69         
70         if (nga == NULL)
71                 return ERR_INVARG;
72         
73         sa = nga->current;
74         if (sa == NULL)
75                 return ERR_NOTLOG;
76         
77         
78         am = malloc(sizeof(struct attr_mirror) + sa->ports);
79         if (am == NULL)
80                 return ERR_MEM;
81         
82         if (ports == NULL) {
83                 am->outport = 0;
84         } else {
85                 am->outport = ports[0];
86                 memcpy(am->ports, ports + 1, sa->ports);
87         }
88         
89         attr = createEmptyList();
90         pushBackList(attr, newAttr(ATTR_MIRROR, sizeof(struct attr_mirror) + sa->ports, am));
91         
92         
93         return writeRequest(nga, attr);
94 }
95