Queue IKEv1 INFORMATIONALS with higher priority to process notifies first
[strongswan.git] / src / libcharon / network / socket_manager.c
1 /*
2 * Copyright (C) 2010 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 * Copyright (C) 2010 Martin Willi
5 * Copyright (C) 2010 revosec AG
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "socket_manager.h"
19
20 #include <daemon.h>
21 #include <threading/thread.h>
22 #include <threading/rwlock.h>
23 #include <utils/linked_list.h>
24
25 typedef struct private_socket_manager_t private_socket_manager_t;
26
27 /**
28 * Private data of an socket_manager_t object.
29 */
30 struct private_socket_manager_t {
31
32 /**
33 * Public socket_manager_t interface.
34 */
35 socket_manager_t public;
36
37 /**
38 * List of registered socket constructors
39 */
40 linked_list_t *sockets;
41
42 /**
43 * Instantiated socket implementation
44 */
45 socket_t *socket;
46
47 /**
48 * The constructor used to create the current socket
49 */
50 socket_constructor_t create;
51
52 /**
53 * Lock for sockets list
54 */
55 rwlock_t *lock;
56 };
57
58 METHOD(socket_manager_t, receiver, status_t,
59 private_socket_manager_t *this, packet_t **packet)
60 {
61 status_t status;
62 this->lock->read_lock(this->lock);
63 if (!this->socket)
64 {
65 DBG1(DBG_NET, "no socket implementation registered, receiving failed");
66 this->lock->unlock(this->lock);
67 return NOT_SUPPORTED;
68 }
69 /* receive is blocking and the thread can be cancelled */
70 thread_cleanup_push((thread_cleanup_t)this->lock->unlock, this->lock);
71 status = this->socket->receive(this->socket, packet);
72 thread_cleanup_pop(TRUE);
73 return status;
74 }
75
76 METHOD(socket_manager_t, sender, status_t,
77 private_socket_manager_t *this, packet_t *packet)
78 {
79 status_t status;
80 this->lock->read_lock(this->lock);
81 if (!this->socket)
82 {
83 DBG1(DBG_NET, "no socket implementation registered, sending failed");
84 this->lock->unlock(this->lock);
85 return NOT_SUPPORTED;
86 }
87 status = this->socket->send(this->socket, packet);
88 this->lock->unlock(this->lock);
89 return status;
90 }
91
92 static void create_socket(private_socket_manager_t *this)
93 {
94 socket_constructor_t create;
95 /* remove constructors in order to avoid trying to create broken ones
96 * multiple times */
97 while (this->sockets->remove_first(this->sockets,
98 (void**)&create) == SUCCESS)
99 {
100 this->socket = create();
101 if (this->socket)
102 {
103 this->create = create;
104 break;
105 }
106 }
107 }
108
109 METHOD(socket_manager_t, add_socket, void,
110 private_socket_manager_t *this, socket_constructor_t create)
111 {
112 this->lock->write_lock(this->lock);
113 this->sockets->insert_last(this->sockets, create);
114 if (!this->socket)
115 {
116 create_socket(this);
117 }
118 this->lock->unlock(this->lock);
119 }
120
121 METHOD(socket_manager_t, remove_socket, void,
122 private_socket_manager_t *this, socket_constructor_t create)
123 {
124 this->lock->write_lock(this->lock);
125 this->sockets->remove(this->sockets, create, NULL);
126 if (this->create == create)
127 {
128 this->socket->destroy(this->socket);
129 this->socket = NULL;
130 this->create = NULL;
131 create_socket(this);
132 }
133 this->lock->unlock(this->lock);
134 }
135
136 METHOD(socket_manager_t, destroy, void,
137 private_socket_manager_t *this)
138 {
139 DESTROY_IF(this->socket);
140 this->sockets->destroy(this->sockets);
141 this->lock->destroy(this->lock);
142 free(this);
143 }
144
145 /**
146 * See header
147 */
148 socket_manager_t *socket_manager_create()
149 {
150 private_socket_manager_t *this;
151
152 INIT(this,
153 .public = {
154 .send = _sender,
155 .receive = _receiver,
156 .add_socket = _add_socket,
157 .remove_socket = _remove_socket,
158 .destroy = _destroy,
159 },
160 .sockets = linked_list_create(),
161 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
162 );
163
164 return &this->public;
165 }
166