]> git.sur5r.net Git - ngadmin/blob - lib/src/misc.c
Move basic networking code to raw library
[ngadmin] / lib / src / misc.c
1
2 #include <ngadmin.h>
3
4 #include <nsdp/attr.h>
5 #include <nsdp/protocol.h>
6 #include <nsdp/misc.h>
7
8 #include "lib.h"
9 #include "network.h"
10
11
12 int ngadmin_setName (struct ngadmin *nga, const char *name)
13 {
14         List *attr;
15         struct attr *at;
16         struct swi_attr *sa;
17         int ret = ERR_OK;
18         
19         
20         if (nga == NULL)
21                 return ERR_INVARG;
22         
23         sa = nga->current;
24         if (sa == NULL)
25                 return ERR_NOTLOG;
26         
27         
28         attr = createEmptyList();
29         if (name == NULL)
30                 at = newEmptyAttr(ATTR_NAME);
31         else
32                 at = newAttr(ATTR_NAME, strlen(name), strdup(name));
33         pushBackList(attr, at);
34         ret = writeRequest(nga, attr);
35         if (ret != ERR_OK)
36                 goto end;
37          
38         /* successful, also update local name */
39         if (name == NULL)
40                 memset(sa->name, '\0', NAME_SIZE);
41         else
42                 strncpy(sa->name, name, NAME_SIZE);
43         
44 end:
45         return ret;
46 }
47
48
49 int ngadmin_changePassword (struct ngadmin *nga, const char* pass)
50 {
51         List *attr;
52         struct attr *at;
53         int ret = ERR_OK;
54         
55         
56         if (nga == NULL || pass == NULL)
57                 return ERR_INVARG;
58         else if (nga->current == NULL)
59                 return ERR_NOTLOG;
60         
61         
62         attr = createEmptyList();
63         at = newAttr(ATTR_NEW_PASSWORD, strlen(pass), strdup(pass));
64         if (nga->encrypt_pass)
65                 passwordEndecode(at->data, at->size);
66         pushBackList(attr, at);
67         ret = writeRequest(nga, attr);
68         if (ret != ERR_OK)
69                 goto end;
70         
71         /* successful, also update local password */
72         strncpy(nga->password, pass, PASSWORD_MAX);
73         
74 end:
75         
76         return ret;
77 }
78
79
80 int ngadmin_restart (struct ngadmin *nga)
81 {
82         List *attr;
83         
84         
85         attr = createEmptyList();
86         pushBackList(attr, newByteAttr(ATTR_RESTART, 1));
87         
88         
89         return writeRequest(nga, attr);
90 }
91
92
93 int ngadmin_defaults (struct ngadmin *nga)
94 {
95         List *attr;
96         int ret = ERR_OK;
97         
98         
99         attr = createEmptyList();
100         pushBackList(attr, newByteAttr(ATTR_DEFAULTS, 1));
101         ret = writeRequest(nga, attr);
102         if (ret != ERR_OK)
103                 goto end;
104         
105         
106         /* successful: delog and clean list */
107         free(nga->swi_tab);
108         nga->swi_tab = NULL;
109         nga->swi_count = 0;
110         nga->current = NULL;
111         
112 end:
113         return ret;
114 }
115
116