]> git.sur5r.net Git - ngadmin/blob - lib/src/libconf.c
Use portable way to handle timeouts
[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 timespec default_timeout = {.tv_sec = 4, .tv_nsec = 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         
33         
34         return nga;
35 }
36
37
38 int ngadmin_close (struct ngadmin *nga)
39 {
40         if (nga == NULL)
41                 return ERR_INVARG;
42                 
43         stopNetwork(nga);
44         free(nga->swi_tab);
45         free(nga);
46         
47         return ERR_OK;
48 }
49
50
51 int ngadmin_forceInterface (struct ngadmin *nga)
52 {
53         if (nga == NULL)
54                 return ERR_INVARG;
55         
56         return forceInterface(nga) == 0 ? ERR_OK : ERR_NET;
57 }
58
59
60 int ngadmin_setKeepBroadcasting (struct ngadmin *nga, bool value)
61 {
62         if (nga == NULL)
63                 return ERR_INVARG;
64         
65         nga->keepbroad = value;
66         
67         return ERR_OK;
68 }
69
70
71 int ngadmin_useGlobalBroadcast (struct ngadmin *nga, bool value)
72 {
73         if (nga == NULL)
74                 return ERR_INVARG;
75         
76         if (setBroadcastType(nga, value) == 0)
77                 return ERR_OK;
78         else
79                 return ERR_NET;
80 }
81
82
83 int ngadmin_setPassword (struct ngadmin *nga, const char *pass)
84 {
85         if (nga == NULL)
86                 return ERR_INVARG;
87         
88         strncpy(nga->password, pass, PASSWORD_MAX);
89         
90         return ERR_OK;
91 }
92
93
94 int ngadmin_setTimeout (struct ngadmin *nga, const struct timeval *tv)
95 {
96         int ret = ERR_OK;
97         
98         
99         if (nga == NULL || tv == NULL)
100                 return ERR_INVARG;
101         
102         nga->timeout.tv_sec = tv->tv_sec;
103         nga->timeout.tv_nsec = tv->tv_usec * 1000;
104         
105         return ret;
106 }
107
108