]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libcharon/plugins/duplicheck/duplicheck_notify.c
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / libcharon / plugins / duplicheck / duplicheck_notify.c
1 /*
2 * Copyright (C) 2011 Martin Willi
3 *
4 * Copyright (C) secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "duplicheck_notify.h"
18 #include "duplicheck_msg.h"
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <unistd.h>
25 #include <errno.h>
26
27 #include <daemon.h>
28 #include <threading/mutex.h>
29 #include <threading/thread.h>
30 #include <collections/linked_list.h>
31 #include <processing/jobs/callback_job.h>
32
33
34 typedef struct private_duplicheck_notify_t private_duplicheck_notify_t;
35
36 /**
37 * Private data of an duplicheck_notify_t object.
38 */
39 struct private_duplicheck_notify_t {
40
41 /**
42 * Public duplicheck_notify_t interface.
43 */
44 duplicheck_notify_t public;
45
46 /**
47 * Mutex to lock list
48 */
49 mutex_t *mutex;
50
51 /**
52 * List of connected clients, as stream_t
53 */
54 linked_list_t *connected;
55
56 /**
57 * stream service accepting connections
58 */
59 stream_service_t *service;
60 };
61
62 /**
63 * Accept duplicheck notification connections
64 */
65 static bool on_accept(private_duplicheck_notify_t *this, stream_t *stream)
66 {
67 this->mutex->lock(this->mutex);
68 this->connected->insert_last(this->connected, stream);
69 this->mutex->unlock(this->mutex);
70
71 return TRUE;
72 }
73
74 METHOD(duplicheck_notify_t, send_, void,
75 private_duplicheck_notify_t *this, identification_t *id)
76 {
77 enumerator_t *enumerator;
78 stream_t *stream;
79 uint16_t nlen;
80 char buf[512];
81 int len;
82
83 len = snprintf(buf, sizeof(buf), "%Y", id);
84 if (len > 0 && len < sizeof(buf))
85 {
86 nlen = htons(len);
87
88 this->mutex->lock(this->mutex);
89 enumerator = this->connected->create_enumerator(this->connected);
90 while (enumerator->enumerate(enumerator, &stream))
91 {
92 if (!stream->write_all(stream, &nlen, sizeof(nlen)) ||
93 !stream->write_all(stream, buf, len))
94 {
95 DBG1(DBG_CFG, "sending duplicheck notify failed: %s",
96 strerror(errno));
97 this->connected->remove_at(this->connected, enumerator);
98 stream->destroy(stream);
99 }
100 }
101 enumerator->destroy(enumerator);
102 this->mutex->unlock(this->mutex);
103 }
104 }
105
106 METHOD(duplicheck_notify_t, destroy, void,
107 private_duplicheck_notify_t *this)
108 {
109 DESTROY_IF(this->service);
110 this->connected->destroy_offset(this->connected, offsetof(stream_t, destroy));
111 this->mutex->destroy(this->mutex);
112 free(this);
113 }
114
115 /**
116 * See header
117 */
118 duplicheck_notify_t *duplicheck_notify_create()
119 {
120 private_duplicheck_notify_t *this;
121 char *uri;
122
123 INIT(this,
124 .public = {
125 .send = _send_,
126 .destroy = _destroy,
127 },
128 .connected = linked_list_create(),
129 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
130 );
131
132 uri = lib->settings->get_str(lib->settings,
133 "%s.plugins.duplicheck.socket", "unix://" DUPLICHECK_SOCKET,
134 lib->ns);
135 this->service = lib->streams->create_service(lib->streams, uri, 3);
136 if (!this->service)
137 {
138 DBG1(DBG_CFG, "creating duplicheck socket failed");
139 destroy(this);
140 return NULL;
141 }
142 this->service->on_accept(this->service, (stream_service_cb_t)on_accept,
143 this, JOB_PRIO_CRITICAL, 1);
144
145 return &this->public;
146 }