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