]> git.sur5r.net Git - ngadmin/blob - lib/src/libconf.c
f94fcca04811f23354cc7bc75b6a3b92b0f0904c
[ngadmin] / lib / src / libconf.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 static const struct timeval default_timeout = {.tv_sec = 4, .tv_usec = 0};
12
13
14 struct ngadmin* ngadmin_init (const char *iface)
15 {
16         struct ngadmin *nga;
17         
18         
19         /* allocate main structure */
20         nga = malloc(sizeof(struct ngadmin));
21         memset(nga, 0, sizeof(struct ngadmin));
22         nga->globalbroad = true;
23         
24         strncpy(nga->iface, iface, IFNAMSIZ - 1);
25         
26         if (startNetwork(nga) < 0) {
27                 free(nga);
28                 return NULL;
29         }
30         
31         nga->timeout = default_timeout;
32         if (updateTimeout(nga) < 0) {
33                 free(nga);
34                 return NULL;
35         }
36         
37         
38         return nga;
39 }
40
41
42 int ngadmin_close (struct ngadmin *nga)
43 {
44         if (nga == NULL)
45                 return ERR_INVARG;
46                 
47         stopNetwork(nga);
48         free(nga->swi_tab);
49         free(nga);
50         
51         return ERR_OK;
52 }
53
54
55 int ngadmin_forceInterface (struct ngadmin *nga)
56 {
57         if (nga == NULL)
58                 return ERR_INVARG;
59         
60         return forceInterface(nga) == 0 ? ERR_OK : ERR_NET;
61 }
62
63
64 int ngadmin_setKeepBroadcasting (struct ngadmin *nga, bool value)
65 {
66         if (nga == NULL)
67                 return ERR_INVARG;
68         
69         nga->keepbroad = value;
70         
71         return ERR_OK;
72 }
73
74
75 int ngadmin_useGlobalBroadcast (struct ngadmin *nga, bool value)
76 {
77         if (nga == NULL)
78                 return ERR_INVARG;
79         
80         if (setBroadcastType(nga, value) == 0)
81                 return ERR_OK;
82         else
83                 return ERR_NET;
84 }
85
86
87 int ngadmin_setPassword (struct ngadmin *nga, const char *pass)
88 {
89         if (nga == NULL)
90                 return ERR_INVARG;
91         
92         strncpy(nga->password, pass, PASSWORD_MAX);
93         
94         return ERR_OK;
95 }
96
97
98 int ngadmin_setTimeout (struct ngadmin *nga, const struct timeval *tv)
99 {
100         int ret = ERR_OK;
101         
102         
103         if (nga == NULL || tv == NULL)
104                 return ERR_INVARG;
105         
106         nga->timeout = *tv;
107         if (updateTimeout(nga) < 0)
108                 ret = ERR_NET;
109         
110         
111         return ret;
112 }
113
114