aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Willi <martin@revosec.ch>2010-02-24 11:45:18 +0100
committerMartin Willi <martin@revosec.ch>2010-02-26 11:44:34 +0100
commit9cb2360e4f3216b2de999400833f727b92fa8028 (patch)
tree86ff9da3892d53be5307d055f18d42d2bc3a1493
parentaf2c43fdc7a977846290e2132fcea1926b0332cf (diff)
downloadstrongswan-9cb2360e4f3216b2de999400833f727b92fa8028.tar.bz2
strongswan-9cb2360e4f3216b2de999400833f727b92fa8028.tar.xz
Added locking to dynamic socket list
-rw-r--r--src/charon/plugins/socket_dynamic/socket_dynamic_socket.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/charon/plugins/socket_dynamic/socket_dynamic_socket.c b/src/charon/plugins/socket_dynamic/socket_dynamic_socket.c
index e1f34de1e..f88c1dd6b 100644
--- a/src/charon/plugins/socket_dynamic/socket_dynamic_socket.c
+++ b/src/charon/plugins/socket_dynamic/socket_dynamic_socket.c
@@ -38,6 +38,7 @@
#include <daemon.h>
#include <threading/thread.h>
+#include <threading/rwlock.h>
#include <utils/hashtable.h>
/* Maximum size of a packet */
@@ -91,6 +92,11 @@ struct private_socket_dynamic_socket_t {
hashtable_t *sockets;
/**
+ * Lock for sockets hashtable
+ */
+ rwlock_t *lock;
+
+ /**
* Notification pipe to signal receiver
*/
int notify[2];
@@ -146,6 +152,7 @@ static int build_fds(private_socket_dynamic_socket_t *this, fd_set *fds)
FD_SET(this->notify[0], fds);
maxfd = this->notify[0];
+ this->lock->read_lock(this->lock);
enumerator = this->sockets->create_enumerator(this->sockets);
while (enumerator->enumerate(enumerator, &key, &value))
{
@@ -153,6 +160,7 @@ static int build_fds(private_socket_dynamic_socket_t *this, fd_set *fds)
maxfd = max(maxfd, value->fd);
}
enumerator->destroy(enumerator);
+ this->lock->unlock(this->lock);
return maxfd + 1;
}
@@ -165,6 +173,7 @@ static dynsock_t* scan_fds(private_socket_dynamic_socket_t *this, fd_set *fds)
enumerator_t *enumerator;
dynsock_t *key, *value, *selected = NULL;
+ this->lock->read_lock(this->lock);
enumerator = this->sockets->create_enumerator(this->sockets);
while (enumerator->enumerate(enumerator, &key, &value))
{
@@ -175,6 +184,8 @@ static dynsock_t* scan_fds(private_socket_dynamic_socket_t *this, fd_set *fds)
}
}
enumerator->destroy(enumerator);
+ this->lock->unlock(this->lock);
+
return selected;
}
@@ -428,7 +439,9 @@ static dynsock_t *find_socket(private_socket_dynamic_socket_t *this,
char buf[] = {0x01};
int fd;
+ this->lock->read_lock(this->lock);
skt = this->sockets->get(this->sockets, &lookup);
+ this->lock->unlock(this->lock);
if (skt)
{
return skt;
@@ -444,7 +457,9 @@ static dynsock_t *find_socket(private_socket_dynamic_socket_t *this,
.port = port,
.fd = fd,
);
+ this->lock->write_lock(this->lock);
this->sockets->put(this->sockets, skt, skt);
+ this->lock->unlock(this->lock);
/* notify receiver thread to reread socket list */
ignore_result(write(this->notify[1], buf, sizeof(buf)));
@@ -567,6 +582,7 @@ METHOD(socket_dynamic_socket_t, destroy, void,
}
enumerator->destroy(enumerator);
this->sockets->destroy(this->sockets);
+ this->lock->destroy(this->lock);
close(this->notify[0]);
close(this->notify[1]);
@@ -588,6 +604,7 @@ socket_dynamic_socket_t *socket_dynamic_socket_create()
},
.destroy = _destroy,
},
+ .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
);
if (pipe(this->notify) != 0)