added second example scenario
[strongswan.git] / src / libcharon / network / socket_manager.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
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 "socket_manager.h"
17
18 #include <daemon.h>
19 #include <threading/thread.h>
20 #include <threading/rwlock.h>
21 #include <utils/linked_list.h>
22
23 typedef struct private_socket_manager_t private_socket_manager_t;
24
25 /**
26 * Private data of an socket_manager_t object.
27 */
28 struct private_socket_manager_t {
29
30 /**
31 * Public socket_manager_t interface.
32 */
33 socket_manager_t public;
34
35 /**
36 * List of registered socket
37 */
38 linked_list_t *sockets;
39
40 /**
41 * Lock for sockets list
42 */
43 rwlock_t *lock;
44 };
45
46 METHOD(socket_manager_t, receiver, status_t,
47 private_socket_manager_t *this, packet_t **packet)
48 {
49 socket_t *socket;
50 status_t status;
51
52 this->lock->read_lock(this->lock);
53 if (this->sockets->get_first(this->sockets, (void**)&socket) != SUCCESS)
54 {
55 DBG1(DBG_NET, "no socket implementation registered, receiving failed");
56 this->lock->unlock(this->lock);
57 return NOT_SUPPORTED;
58 }
59 /* receive is blocking and the thread can be cancelled */
60 thread_cleanup_push((thread_cleanup_t)this->lock->unlock, this->lock);
61 status = socket->receive(socket, packet);
62 thread_cleanup_pop(TRUE);
63 return status;
64 }
65
66 METHOD(socket_manager_t, sender, status_t,
67 private_socket_manager_t *this, packet_t *packet)
68 {
69 socket_t *socket;
70 status_t status;
71
72 this->lock->read_lock(this->lock);
73 if (this->sockets->get_first(this->sockets, (void**)&socket) != SUCCESS)
74 {
75 DBG1(DBG_NET, "no socket implementation registered, sending failed");
76 this->lock->unlock(this->lock);
77 return NOT_SUPPORTED;
78 }
79 status = socket->send(socket, packet);
80 this->lock->unlock(this->lock);
81 return status;
82 }
83
84 METHOD(socket_manager_t, add_socket, void,
85 private_socket_manager_t *this, socket_t *socket)
86 {
87 this->lock->write_lock(this->lock);
88 this->sockets->insert_last(this->sockets, socket);
89 this->lock->unlock(this->lock);
90 }
91
92 METHOD(socket_manager_t, remove_socket, void,
93 private_socket_manager_t *this, socket_t *socket)
94 {
95 this->lock->write_lock(this->lock);
96 this->sockets->remove(this->sockets, socket, NULL);
97 this->lock->unlock(this->lock);
98 }
99
100 METHOD(socket_manager_t, destroy, void,
101 private_socket_manager_t *this)
102 {
103 this->sockets->destroy(this->sockets);
104 this->lock->destroy(this->lock);
105 free(this);
106 }
107
108 /**
109 * See header
110 */
111 socket_manager_t *socket_manager_create()
112 {
113 private_socket_manager_t *this;
114
115 INIT(this,
116 .public = {
117 .send = _sender,
118 .receive = _receiver,
119 .add_socket = _add_socket,
120 .remove_socket = _remove_socket,
121 .destroy = _destroy,
122 },
123 .sockets = linked_list_create(),
124 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
125 );
126
127 return &this->public;
128 }
129