]> git.meshlink.io Git - meshlink/blob - src/submesh.c
Add assert() calls to the library.
[meshlink] / src / submesh.c
1 /*
2     submesh.c -- submesh management
3     Copyright (C) 2019 Guus Sliepen <guus@meshlink.io>,
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "hash.h"
23 #include "logger.h"
24 #include "meshlink_internal.h"
25 #include "net.h"
26 #include "netutl.h"
27 #include "submesh.h"
28 #include "splay_tree.h"
29 #include "utils.h"
30 #include "xalloc.h"
31 #include "protocol.h"
32
33 void init_submeshes(meshlink_handle_t *mesh) {
34         assert(!mesh->submeshes);
35         mesh->submeshes = list_alloc((list_action_t)free_submesh);
36 }
37
38 void exit_submeshes(meshlink_handle_t *mesh) {
39         if(mesh->submeshes) {
40                 list_delete_list(mesh->submeshes);
41         }
42
43         mesh->submeshes = NULL;
44 }
45
46 submesh_t *new_submesh(void) {
47         submesh_t *s = xzalloc(sizeof(*s));
48
49         s->name = NULL;
50         s->priv = NULL;
51
52         return s;
53 }
54
55 void free_submesh(submesh_t *s) {
56         if(s->name) {
57                 free(s->name);
58         }
59
60         free(s);
61 }
62
63 static submesh_t *submesh_new(meshlink_handle_t *mesh, const char *submesh) {
64         submesh_t *s = NULL;
65         assert(submesh);
66
67         s = new_submesh();
68         s->name = xstrdup(submesh);
69
70         submesh_add(mesh, (submesh_t *)s);
71         return s;
72 }
73
74 submesh_t *create_submesh(meshlink_handle_t *mesh, const char *submesh) {
75         assert(submesh);
76         submesh_t *s = NULL;
77
78         if(0 == strcmp(submesh, CORE_MESH)) {
79                 logger(NULL, MESHLINK_ERROR, "Cannot create submesh handle for core mesh!\n");
80                 meshlink_errno = MESHLINK_EINVAL;
81                 return NULL;
82         }
83
84         if(!check_id(submesh)) {
85                 logger(NULL, MESHLINK_ERROR, "Invalid SubMesh Id!\n");
86                 meshlink_errno = MESHLINK_EINVAL;
87                 return NULL;
88         }
89
90         s = lookup_submesh(mesh, submesh);
91
92         if(s) {
93                 logger(NULL, MESHLINK_ERROR, "SubMesh Already exists!\n");
94                 meshlink_errno = MESHLINK_EEXIST;
95                 return NULL;
96         }
97
98         s = submesh_new(mesh, submesh);
99
100         meshlink_errno = MESHLINK_OK;
101         return s;
102 }
103
104 submesh_t *lookup_or_create_submesh(meshlink_handle_t *mesh, const char *submesh) {
105         assert(submesh);
106         submesh_t *s = NULL;
107
108         if(0 == strcmp(submesh, CORE_MESH)) {
109                 logger(NULL, MESHLINK_ERROR, "Cannot create submesh handle for core mesh!\n");
110                 meshlink_errno = MESHLINK_EINVAL;
111                 return NULL;
112         }
113
114         if(!check_id(submesh)) {
115                 logger(NULL, MESHLINK_ERROR, "Invalid SubMesh Id!\n");
116                 meshlink_errno = MESHLINK_EINVAL;
117                 return NULL;
118         }
119
120         s = lookup_submesh(mesh, submesh);
121
122         if(s) {
123                 meshlink_errno = MESHLINK_OK;
124                 return s;
125         }
126
127         s = submesh_new(mesh, submesh);
128
129         meshlink_errno = MESHLINK_OK;
130         return s;
131 }
132
133 void submesh_add(meshlink_handle_t *mesh, submesh_t *s) {
134         s->mesh = mesh;
135         list_insert_tail(mesh->submeshes, (void *)s);
136 }
137
138 void submesh_del(meshlink_handle_t *mesh, submesh_t *s) {
139         list_delete(mesh->submeshes, (void *)s);
140 }
141
142 submesh_t *lookup_submesh(struct meshlink_handle *mesh, const char *submesh_name) {
143         assert(submesh_name);
144
145         submesh_t *submesh = NULL;
146
147         if(!mesh->submeshes) {
148                 return NULL;
149         }
150
151         for list_each(submesh_t, s, mesh->submeshes) {
152                 if(!strcmp(submesh_name, s->name)) {
153                         submesh = s;
154                         break;
155                 }
156         }
157
158         return submesh;
159 }
160
161 bool submesh_allows_node(const submesh_t *submesh, const node_t *node) {
162         if(!node->submesh || !submesh || submesh == node->submesh) {
163                 return true;
164         } else {
165                 return false;
166         }
167 }