added .gitignore files to NM applet
[strongswan.git] / src / charon / plugins / nm / nm_handler.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "nm_handler.h"
17
18 #include <daemon.h>
19
20 typedef struct private_nm_handler_t private_nm_handler_t;
21
22 /**
23 * Private data of an nm_handler_t object.
24 */
25 struct private_nm_handler_t {
26
27 /**
28 * Public nm_handler_t interface.
29 */
30 nm_handler_t public;
31
32 /**
33 * list of received DNS server attributes, pointer to 4 byte data
34 */
35 linked_list_t *dns;
36
37 /**
38 * list of received NBNS server attributes, pointer to 4 byte data
39 */
40 linked_list_t *nbns;
41 };
42
43 /**
44 * Implementation of attribute_handler_t.handle
45 */
46 static bool handle(private_nm_handler_t *this, ike_sa_t *ike_sa,
47 configuration_attribute_type_t type, chunk_t data)
48 {
49 linked_list_t *list;
50
51 switch (type)
52 {
53 case INTERNAL_IP4_DNS:
54 list = this->dns;
55 break;
56 case INTERNAL_IP4_NBNS:
57 list = this->nbns;
58 break;
59 default:
60 return FALSE;
61 }
62 if (data.len != 4)
63 {
64 return FALSE;
65 }
66 list->insert_last(list, chunk_clone(data).ptr);
67 return TRUE;
68 }
69
70 /**
71 * convert plain byte ptrs to handy chunk during enumeration
72 */
73 static bool filter_chunks(void* null, char **in, chunk_t *out)
74 {
75 *out = chunk_create(*in, 4);
76 return TRUE;
77 }
78
79 /**
80 * Implementation of nm_handler_t.create_enumerator
81 */
82 static enumerator_t* create_enumerator(private_nm_handler_t *this,
83 configuration_attribute_type_t type)
84 {
85 linked_list_t *list;
86
87 switch (type)
88 {
89 case INTERNAL_IP4_DNS:
90 list = this->dns;
91 break;
92 case INTERNAL_IP4_NBNS:
93 list = this->nbns;
94 break;
95 default:
96 return enumerator_create_empty();
97 }
98 return enumerator_create_filter(list->create_enumerator(list),
99 (void*)filter_chunks, NULL, NULL);
100 }
101
102 /**
103 * Implementation of nm_handler_t.reset
104 */
105 static void reset(private_nm_handler_t *this)
106 {
107 void *data;
108
109 while (this->dns->remove_last(this->dns, (void**)&data) == SUCCESS)
110 {
111 free(data);
112 }
113 while (this->nbns->remove_last(this->nbns, (void**)&data) == SUCCESS)
114 {
115 free(data);
116 }
117 }
118
119 /**
120 * Implementation of nm_handler_t.destroy.
121 */
122 static void destroy(private_nm_handler_t *this)
123 {
124 reset(this);
125 this->dns->destroy(this->dns);
126 this->nbns->destroy(this->nbns);
127 free(this);
128 }
129
130 /**
131 * See header
132 */
133 nm_handler_t *nm_handler_create()
134 {
135 private_nm_handler_t *this = malloc_thing(private_nm_handler_t);
136
137 this->public.handler.handle = (bool(*)(attribute_handler_t*, ike_sa_t*, configuration_attribute_type_t, chunk_t))handle;
138 this->public.handler.release = (void(*)(attribute_handler_t*, ike_sa_t*, configuration_attribute_type_t, chunk_t))nop;
139 this->public.create_enumerator = (enumerator_t*(*)(nm_handler_t*, configuration_attribute_type_t type))create_enumerator;
140 this->public.reset = (void(*)(nm_handler_t*))reset;
141 this->public.destroy = (void(*)(nm_handler_t*))destroy;
142
143 this->dns = linked_list_create();
144 this->nbns = linked_list_create();
145
146 return &this->public;
147 }
148