2 * Copyright (C) 2011 Martin Willi
4 * Copyright (C) secunet Security Networks AG
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>.
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
17 #include "duplicheck_notify.h"
18 #include "duplicheck_msg.h"
20 #include <sys/types.h>
22 #include <sys/socket.h>
28 #include <threading/mutex.h>
29 #include <threading/thread.h>
30 #include <collections/linked_list.h>
31 #include <processing/jobs/callback_job.h>
34 typedef struct private_duplicheck_notify_t private_duplicheck_notify_t
;
37 * Private data of an duplicheck_notify_t object.
39 struct private_duplicheck_notify_t
{
42 * Public duplicheck_notify_t interface.
44 duplicheck_notify_t
public;
52 * List of connected clients, as stream_t
54 linked_list_t
*connected
;
57 * stream service accepting connections
59 stream_service_t
*service
;
63 * Accept duplicheck notification connections
65 static bool on_accept(private_duplicheck_notify_t
*this, stream_t
*stream
)
67 this->mutex
->lock(this->mutex
);
68 this->connected
->insert_last(this->connected
, stream
);
69 this->mutex
->unlock(this->mutex
);
74 METHOD(duplicheck_notify_t
, send_
, void,
75 private_duplicheck_notify_t
*this, identification_t
*id
)
77 enumerator_t
*enumerator
;
83 len
= snprintf(buf
, sizeof(buf
), "%Y", id
);
84 if (len
> 0 && len
< sizeof(buf
))
88 this->mutex
->lock(this->mutex
);
89 enumerator
= this->connected
->create_enumerator(this->connected
);
90 while (enumerator
->enumerate(enumerator
, &stream
))
92 if (!stream
->write_all(stream
, &nlen
, sizeof(nlen
)) ||
93 !stream
->write_all(stream
, buf
, len
))
95 DBG1(DBG_CFG
, "sending duplicheck notify failed: %s",
97 this->connected
->remove_at(this->connected
, enumerator
);
98 stream
->destroy(stream
);
101 enumerator
->destroy(enumerator
);
102 this->mutex
->unlock(this->mutex
);
106 METHOD(duplicheck_notify_t
, destroy
, void,
107 private_duplicheck_notify_t
*this)
109 DESTROY_IF(this->service
);
110 this->connected
->destroy_offset(this->connected
, offsetof(stream_t
, destroy
));
111 this->mutex
->destroy(this->mutex
);
118 duplicheck_notify_t
*duplicheck_notify_create()
120 private_duplicheck_notify_t
*this;
128 .connected
= linked_list_create(),
129 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
132 uri
= lib
->settings
->get_str(lib
->settings
,
133 "%s.plugins.duplicheck.socket", "unix://" DUPLICHECK_SOCKET
,
135 this->service
= lib
->streams
->create_service(lib
->streams
, uri
, 3);
138 DBG1(DBG_CFG
, "creating duplicheck socket failed");
142 this->service
->on_accept(this->service
, (stream_service_cb_t
)on_accept
,
143 this, JOB_PRIO_CRITICAL
, 1);
145 return &this->public;