summaryrefslogtreecommitdiff
path: root/src/iface_record.c
blob: 09adeb79f5e940d97c5825ae219b9a12c66037a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <stdlib.h>
#include <string.h>

#include "iface_internal.h"
#include "context_internal.h"
#include "debug.h"

struct sepol_iface {

	/* Interface name */
	char *name;

	/* Interface context */
	sepol_context_t *netif_con;

	/* Message context */
	sepol_context_t *netmsg_con;
};

struct sepol_iface_key {

	/* Interface name */
	const char *name;
};

/* Key */
int sepol_iface_key_create(sepol_handle_t * handle,
			   const char *name, sepol_iface_key_t ** key_ptr)
{

	sepol_iface_key_t *tmp_key =
	    (sepol_iface_key_t *) malloc(sizeof(sepol_iface_key_t));

	if (!tmp_key) {
		ERR(handle, "out of memory, could not create interface key");
		return STATUS_ERR;
	}

	tmp_key->name = name;

	*key_ptr = tmp_key;
	return STATUS_SUCCESS;
}

hidden_def(sepol_iface_key_create)

void sepol_iface_key_unpack(const sepol_iface_key_t * key, const char **name)
{

	*name = key->name;
}

hidden_def(sepol_iface_key_unpack)

int sepol_iface_key_extract(sepol_handle_t * handle,
			    const sepol_iface_t * iface,
			    sepol_iface_key_t ** key_ptr)
{

	if (sepol_iface_key_create(handle, iface->name, key_ptr) < 0) {
		ERR(handle, "could not extract key from "
		    "interface %s", iface->name);
		return STATUS_ERR;
	}

	return STATUS_SUCCESS;
}

void sepol_iface_key_free(sepol_iface_key_t * key)
{
	free(key);
}

int sepol_iface_compare(const sepol_iface_t * iface,
			const sepol_iface_key_t * key)
{

	return strcmp(iface->name, key->name);
}

int sepol_iface_compare2(const sepol_iface_t * iface,
			 const sepol_iface_t * iface2)
{

	return strcmp(iface->name, iface2->name);
}

/* Create */
int sepol_iface_create(sepol_handle_t * handle, sepol_iface_t ** iface)
{

	sepol_iface_t *tmp_iface =
	    (sepol_iface_t *) malloc(sizeof(sepol_iface_t));

	if (!tmp_iface) {
		ERR(handle, "out of memory, could not create "
		    "interface record");
		return STATUS_ERR;
	}

	tmp_iface->name = NULL;
	tmp_iface->netif_con = NULL;
	tmp_iface->netmsg_con = NULL;
	*iface = tmp_iface;

	return STATUS_SUCCESS;
}

hidden_def(sepol_iface_create)

/* Name */
const char *sepol_iface_get_name(const sepol_iface_t * iface)
{

	return iface->name;
}

hidden_def(sepol_iface_get_name)

int sepol_iface_set_name(sepol_handle_t * handle,
			 sepol_iface_t * iface, const char *name)
{

	char *tmp_name = strdup(name);
	if (!tmp_name) {
		ERR(handle, "out of memory, " "could not set interface name");
		return STATUS_ERR;
	}
	free(iface->name);
	iface->name = tmp_name;
	return STATUS_SUCCESS;
}

hidden_def(sepol_iface_set_name)

/* Interface Context */
sepol_context_t *sepol_iface_get_ifcon(const sepol_iface_t * iface)
{

	return iface->netif_con;
}

hidden_def(sepol_iface_get_ifcon)

int sepol_iface_set_ifcon(sepol_handle_t * handle,
			  sepol_iface_t * iface, sepol_context_t * con)
{

	sepol_context_t *newcon;

	if (sepol_context_clone(handle, con, &newcon) < 0) {
		ERR(handle, "out of memory, could not set interface context");
		return STATUS_ERR;
	}

	sepol_context_free(iface->netif_con);
	iface->netif_con = newcon;
	return STATUS_SUCCESS;
}

hidden_def(sepol_iface_set_ifcon)

/* Message Context */
sepol_context_t *sepol_iface_get_msgcon(const sepol_iface_t * iface)
{

	return iface->netmsg_con;
}

hidden_def(sepol_iface_get_msgcon)

int sepol_iface_set_msgcon(sepol_handle_t * handle,
			   sepol_iface_t * iface, sepol_context_t * con)
{

	sepol_context_t *newcon;
	if (sepol_context_clone(handle, con, &newcon) < 0) {
		ERR(handle, "out of memory, could not set message context");
		return STATUS_ERR;
	}

	sepol_context_free(iface->netmsg_con);
	iface->netmsg_con = newcon;
	return STATUS_SUCCESS;
}

hidden_def(sepol_iface_set_msgcon)

/* Deep copy clone */
int sepol_iface_clone(sepol_handle_t * handle,
		      const sepol_iface_t * iface, sepol_iface_t ** iface_ptr)
{

	sepol_iface_t *new_iface = NULL;
	if (sepol_iface_create(handle, &new_iface) < 0)
		goto err;

	if (sepol_iface_set_name(handle, new_iface, iface->name) < 0)
		goto err;

	if (iface->netif_con &&
	    (sepol_context_clone
	     (handle, iface->netif_con, &new_iface->netif_con) < 0))
		goto err;

	if (iface->netmsg_con &&
	    (sepol_context_clone
	     (handle, iface->netmsg_con, &new_iface->netmsg_con) < 0))
		goto err;

	*iface_ptr = new_iface;
	return STATUS_SUCCESS;

      err:
	ERR(handle, "could not clone interface record");
	sepol_iface_free(new_iface);
	return STATUS_ERR;
}

/* Destroy */
void sepol_iface_free(sepol_iface_t * iface)
{

	if (!iface)
		return;

	free(iface->name);
	sepol_context_free(iface->netif_con);
	sepol_context_free(iface->netmsg_con);
	free(iface);
}

hidden_def(sepol_iface_free)